From 47e212178e9e053d412719d4a91cf9ad3de6e23c Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Wed, 9 Oct 2013 04:34:02 +0200 Subject: [PATCH 1/2] Emit array literals as unnamed_addr. There is really no reason not to. --- gen/arrays.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/gen/arrays.cpp b/gen/arrays.cpp index 46a19fce..714fc1a0 100644 --- a/gen/arrays.cpp +++ b/gen/arrays.cpp @@ -473,6 +473,7 @@ void initializeArrayLiteral(IRState* p, ArrayLiteralExp* ale, LLValue* dstMem) constarr, ".arrayliteral" ); + gvar->setUnnamedAddr(true); DtoMemCpy(dstMem, gvar, DtoConstSize_t(getTypePaddedSize(constarr->getType()))); } } From 4987894468f30893fd0f90e4aa584bd61dfaf63c Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Wed, 9 Oct 2013 05:32:41 +0200 Subject: [PATCH 2/2] Also make storage for immutable array const initializers constant/unnamed_addr. --- gen/toir.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/gen/toir.cpp b/gen/toir.cpp index fe664974..2f794e13 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -2945,21 +2945,20 @@ LLConstant* ArrayLiteralExp::toConstElem(IRState* p) if (!dyn) return initval; - // we need to put the initializer in a global, and so we have a pointer to the array - // Important: don't make the global constant, since this const initializer might - // be used as an initializer for a static T[] - where modifying contents is allowed. - LLConstant* globalstore = new LLGlobalVariable(*gIR->module, - initval->getType(), false, LLGlobalValue::InternalLinkage, initval, + bool canBeConst = type->isConst() || type->isImmutable(); + llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module, + initval->getType(), canBeConst, llvm::GlobalValue::InternalLinkage, initval, ".dynarrayStorage"); - globalstore = DtoBitCast(globalstore, getPtrToType(arrtype)); + gvar->setUnnamedAddr(canBeConst); + llvm::Constant* store = DtoBitCast(gvar, getPtrToType(arrtype)); if (bt->ty == Tpointer) // we need to return pointer to the static array. - return globalstore; + return store; - // build a constant dynamic array reference with the .ptr field pointing into globalstore + // build a constant dynamic array reference with the .ptr field pointing into store LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) }; - LLConstant* globalstorePtr = llvm::ConstantExpr::getGetElementPtr(globalstore, idxs, true); + LLConstant* globalstorePtr = llvm::ConstantExpr::getGetElementPtr(store, idxs, true); return DtoConstSlice(DtoConstSize_t(elements->dim), globalstorePtr); }