From a509f2fdfa8ffb4ce9edaee84daca44bcb07ee78 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Fri, 14 Jun 2013 03:07:01 +0000 Subject: Emit initializers for static-storage-duration temporaries as constants where possible. llvm-svn: 183967 --- clang/lib/CodeGen/CGExpr.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'clang/lib/CodeGen/CGExpr.cpp') diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 034525cff678..cbad66c8bc80 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -321,6 +321,13 @@ LValue CodeGenFunction::EmitMaterializeTemporaryExpr( llvm::Value *Object = createReferenceTemporary(*this, M, E); LValue RefTempDst = MakeAddrLValue(Object, M->getType()); + if (llvm::GlobalVariable *Var = dyn_cast(Object)) { + // We should not have emitted the initializer for this temporary as a + // constant. + assert(!Var->hasInitializer()); + Var->setInitializer(CGM.EmitNullConstant(E->getType())); + } + EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false); pushTemporaryCleanup(*this, M, E, Object); @@ -343,7 +350,16 @@ LValue CodeGenFunction::EmitMaterializeTemporaryExpr( // Create and initialize the reference temporary. llvm::Value *Object = createReferenceTemporary(*this, M, E); - EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); + if (llvm::GlobalVariable *Var = dyn_cast(Object)) { + // If the temporary is a global and has a constant initializer, we may + // have already initialized it. + if (!Var->hasInitializer()) { + Var->setInitializer(CGM.EmitNullConstant(E->getType())); + EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); + } + } else { + EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); + } pushTemporaryCleanup(*this, M, E, Object); // Perform derived-to-base casts and/or field accesses, to get from the -- cgit v1.2.3