[cfe-commits] r145087 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp lib/CodeGen/CGExprConstant.cpp lib/CodeGen/CodeGenModule.h test/CodeGenCXX/compound-literals.cpp

Richard Smith richard-llvm at metafoo.co.uk
Tue Nov 22 14:48:32 PST 2011


Author: rsmith
Date: Tue Nov 22 16:48:32 2011
New Revision: 145087

URL: http://llvm.org/viewvc/llvm-project?rev=145087&view=rev
Log:
Use static storage duration for file-scope compound literals, even when they
appear in non-constant initializers in C++.

Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/test/CodeGenCXX/compound-literals.cpp

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=145087&r1=145086&r2=145087&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Tue Nov 22 16:48:32 2011
@@ -1947,6 +1947,11 @@
 }
 
 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
+  if (E->isFileScope()) {
+    llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
+    return MakeAddrLValue(GlobalPtr, E->getType());
+  }
+
   llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
   const Expr *InitExpr = E->getInitializer();
   LValue Result = MakeAddrLValue(DeclPtr, E->getType());

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=145087&r1=145086&r2=145087&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Tue Nov 22 16:48:32 2011
@@ -1087,6 +1087,12 @@
   return C;
 }
 
+llvm::Constant *
+CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
+  assert(E->isFileScope() && "not a file-scope compound literal expr");
+  return ConstExprEmitter(*this, 0).EmitLValue(E);
+}
+
 static uint64_t getFieldOffset(ASTContext &C, const FieldDecl *field) {
   const ASTRecordLayout &layout = C.getASTRecordLayout(field->getParent());
   return layout.getFieldOffset(field->getFieldIndex());

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=145087&r1=145086&r2=145087&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Tue Nov 22 16:48:32 2011
@@ -599,6 +599,10 @@
   llvm::Constant *GetAddrOfConstantCString(const std::string &str,
                                            const char *GlobalName=0,
                                            unsigned Alignment=1);
+
+  /// GetAddrOfConstantCompoundLiteral - Returns a pointer to a constant global
+  /// variable for the given file-scope compound literal expression.
+  llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
   
   /// \brief Retrieve the record type that describes the state of an
   /// Objective-C fast enumeration loop (for..in).

Modified: cfe/trunk/test/CodeGenCXX/compound-literals.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/compound-literals.cpp?rev=145087&r1=145086&r2=145087&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/compound-literals.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/compound-literals.cpp Tue Nov 22 16:48:32 2011
@@ -37,3 +37,8 @@
   // CHECK-NEXT: ret i32 [[A0]]
   return v[0];
 }
+
+struct Z { int i[3]; };
+int *p = (Z){ {1, 2, 3} }.i;
+// CHECK: define {{.*}}__cxx_global_var_init()
+// CHECK: store i32* getelementptr inbounds (%struct.Z* @.compoundliteral, i32 0, i32 0, i32 0), i32** @p, align 8





More information about the cfe-commits mailing list