From 6cb213badf413f2cd9d55a0e4b05d102116b2169 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Sun, 12 Oct 2008 10:35:16 +0200 Subject: [PATCH] Only allocate the module file name once. Fixes #90. --- gen/arrays.cpp | 20 ++++---------------- gen/llvmhelpers.cpp | 23 +++++------------------ gen/statements.cpp | 20 ++++---------------- gen/todebug.cpp | 2 +- gen/toobj.cpp | 9 +++++---- ir/irmodule.cpp | 9 ++++++++- ir/irmodule.h | 3 ++- 7 files changed, 29 insertions(+), 57 deletions(-) diff --git a/gen/arrays.cpp b/gen/arrays.cpp index 4a33c000..b0f25942 100644 --- a/gen/arrays.cpp +++ b/gen/arrays.cpp @@ -1,6 +1,7 @@ #include "gen/llvm.h" #include "mtype.h" +#include "module.h" #include "dsymbol.h" #include "aggregate.h" #include "declaration.h" @@ -13,6 +14,7 @@ #include "gen/runtime.h" #include "gen/logger.h" #include "gen/dvalue.h" +#include "ir/irmodule.h" ////////////////////////////////////////////////////////////////////////////////////////// @@ -1072,25 +1074,11 @@ void DtoArrayBoundsCheck(Loc& loc, DValue* arr, DValue* index, bool isslice) llvm::AttrListPtr palist; // file param - // FIXME: every array bounds check creates a global for the filename !!! - LLConstant* c = DtoConstString(loc.filename); - - llvm::AllocaInst* alloc = gIR->func()->srcfileArg; - if (!alloc) - { - alloc = DtoAlloca(c->getType(), ".srcfile"); - gIR->func()->srcfileArg = alloc; - } - LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp"); - DtoStore(c->getOperand(0), ptr); - ptr = DtoGEPi(alloc, 0,1, "tmp"); - DtoStore(c->getOperand(1), ptr); - - args.push_back(alloc); + args.push_back(gIR->dmodule->ir.irModule->fileName); palist = palist.addAttr(1, llvm::Attribute::ByVal); // line param - c = DtoConstUint(loc.linnum); + LLConstant* c = DtoConstUint(loc.linnum); args.push_back(c); // call diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 932ab667..9df3526c 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -6,6 +6,7 @@ #include "id.h" #include "expression.h" #include "template.h" +#include "module.h" #include "gen/tollvm.h" #include "gen/llvmhelpers.h" @@ -19,6 +20,7 @@ #include "gen/functions.h" #include "gen/typeinf.h" #include "gen/todebug.h" +#include "ir/irmodule.h" #include @@ -110,7 +112,6 @@ llvm::AllocaInst* DtoAlloca(const LLType* lltype, LLValue* arraysize, const std: void DtoAssert(Loc* loc, DValue* msg) { std::vector args; - LLConstant* c; // func const char* fname = msg ? "_d_assert_msg" : "_d_assert"; @@ -120,9 +121,6 @@ void DtoAssert(Loc* loc, DValue* msg) llvm::AttrListPtr palist; int idx = 1; - // FIXME: every assert creates a global for the filename !!! - c = DtoConstString(loc->filename); - // msg param if (msg) { @@ -131,7 +129,7 @@ void DtoAssert(Loc* loc, DValue* msg) llvm::AllocaInst* alloc = gIR->func()->msgArg; if (!alloc) { - alloc = DtoAlloca(c->getType(), ".assertmsg"); + alloc = DtoAlloca(DtoArrayType(LLType::Int8Ty), ".assertmsg"); DtoSetArray(alloc, DtoArrayLen(s), DtoArrayPtr(s)); gIR->func()->msgArg = alloc; } @@ -145,23 +143,12 @@ void DtoAssert(Loc* loc, DValue* msg) } // file param - llvm::AllocaInst* alloc = gIR->func()->srcfileArg; - if (!alloc) - { - alloc = DtoAlloca(c->getType(), ".srcfile"); - gIR->func()->srcfileArg = alloc; - } - LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp"); - DtoStore(c->getOperand(0), ptr); - ptr = DtoGEPi(alloc, 0,1, "tmp"); - DtoStore(c->getOperand(1), ptr); - - args.push_back(alloc); + args.push_back(gIR->dmodule->ir.irModule->fileName); palist = palist.addAttr(idx++, llvm::Attribute::ByVal); // line param - c = DtoConstUint(loc->linnum); + LLConstant* c = DtoConstUint(loc->linnum); args.push_back(c); // call diff --git a/gen/statements.cpp b/gen/statements.cpp index 95e8bb8f..a790f0b0 100644 --- a/gen/statements.cpp +++ b/gen/statements.cpp @@ -16,6 +16,7 @@ #include "mtype.h" #include "hdrgen.h" #include "port.h" +#include "module.h" #include "gen/irstate.h" #include "gen/logger.h" @@ -27,6 +28,7 @@ #include "gen/dvalue.h" #include "ir/irfunction.h" +#include "ir/irmodule.h" #include "ir/irlandingpad.h" ////////////////////////////////////////////////////////////////////////////// @@ -1270,27 +1272,13 @@ void SwitchErrorStatement::toIR(IRState* p) int idx = 1; std::vector args; - LLConstant* c; // file param - // FIXME: every use creates a global for the filename !!! - c = DtoConstString(loc.filename); - llvm::AllocaInst* alloc = gIR->func()->srcfileArg; - if (!alloc) - { - alloc = DtoAlloca(c->getType(), ".srcfile"); - gIR->func()->srcfileArg = alloc; - } - LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp"); - DtoStore(c->getOperand(0), ptr); - ptr = DtoGEPi(alloc, 0,1, "tmp"); - DtoStore(c->getOperand(1), ptr); - - args.push_back(alloc); + args.push_back(gIR->dmodule->ir.irModule->fileName); palist = palist.addAttr(idx++, llvm::Attribute::ByVal); // line param - c = DtoConstUint(loc.linnum); + LLConstant* c = DtoConstUint(loc.linnum); args.push_back(c); // call diff --git a/gen/todebug.cpp b/gen/todebug.cpp index 6881d010..a3cbd1b3 100644 --- a/gen/todebug.cpp +++ b/gen/todebug.cpp @@ -628,7 +628,7 @@ LLGlobalVariable* DtoDwarfCompileUnit(Module* m) // we might be generating for an import if (!m->ir.irModule) - m->ir.irModule = new IrModule(m); + m->ir.irModule = new IrModule(m, m->srcfile->toChars()); else if (m->ir.irModule->dwarfCompileUnit) { if (m->ir.irModule->dwarfCompileUnit->getParent() == gIR->module) diff --git a/gen/toobj.cpp b/gen/toobj.cpp index 8474caac..ebf97669 100644 --- a/gen/toobj.cpp +++ b/gen/toobj.cpp @@ -89,16 +89,17 @@ void Module::genobjfile(int multiobj, char** envp) IrDsymbol::resetAll(); IrType::resetAll(); - // module ir state - // might already exist via import, just overwrite... - this->ir.irModule = new IrModule(this); - // name the module std::string mname(toChars()); if (md != 0) mname = md->toChars(); ir.module = new llvm::Module(mname); + // module ir state + // might already exist via import, just overwrite... + //FIXME: is there a good reason for overwriting? + this->ir.irModule = new IrModule(this, srcfile->toChars()); + // set target stuff std::string target_triple(global.params.tt_arch); target_triple.append(global.params.tt_os); diff --git a/ir/irmodule.cpp b/ir/irmodule.cpp index 9aef92cd..228732f9 100644 --- a/ir/irmodule.cpp +++ b/ir/irmodule.cpp @@ -1,9 +1,16 @@ #include "gen/llvm.h" +#include "gen/tollvm.h" +#include "gen/irstate.h" #include "ir/irmodule.h" -IrModule::IrModule(Module* module) +IrModule::IrModule(Module* module, const char* srcfilename) { M = module; + + LLConstant* slice = DtoConstString(srcfilename); + fileName = new llvm::GlobalVariable( + slice->getType(), true, LLGlobalValue::InternalLinkage, slice, ".modulefilename", gIR->module); + dwarfCompileUnit = NULL; } diff --git a/ir/irmodule.h b/ir/irmodule.h index 29fe2b01..aabfdbd0 100644 --- a/ir/irmodule.h +++ b/ir/irmodule.h @@ -7,12 +7,13 @@ struct Module; struct IrModule : IrBase { - IrModule(Module* module); + IrModule(Module* module, const char* srcfilename); virtual ~IrModule(); Module* M; LLGlobalVariable* dwarfCompileUnit; + LLGlobalVariable* fileName; }; #endif