[cfe-commits] r150926 - in /cfe/trunk: lib/CodeGen/CGExprAgg.cpp lib/CodeGen/CodeGenFunction.h test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp

Sebastian Redl sebastian.redl at getdesigned.at
Sun Feb 19 04:28:03 PST 2012


Author: cornedbee
Date: Sun Feb 19 06:28:02 2012
New Revision: 150926

URL: http://llvm.org/viewvc/llvm-project?rev=150926&view=rev
Log:
Get recursive initializer lists to work and add a test. Codegen of std::initializer_list is now complete. Onward to array new.

Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h
    cfe/trunk/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=150926&r1=150925&r2=150926&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Sun Feb 19 06:28:02 2012
@@ -80,7 +80,7 @@
 
   void EmitMoveFromReturnSlot(const Expr *E, RValue Src);
 
-  void EmitStdInitializerList(InitListExpr *InitList);
+  void EmitStdInitializerList(llvm::Value *DestPtr, InitListExpr *InitList);
   void EmitArrayInit(llvm::Value *DestPtr, llvm::ArrayType *AType,
                      QualType elementType, InitListExpr *E);
 
@@ -303,7 +303,8 @@
 
 /// \brief Emit the initializer for a std::initializer_list initialized with a
 /// real initializer list.
-void AggExprEmitter::EmitStdInitializerList(InitListExpr *initList) {
+void AggExprEmitter::EmitStdInitializerList(llvm::Value *destPtr,
+                                            InitListExpr *initList) {
   // We emit an array containing the elements, then have the init list point
   // at the array.
   ASTContext &ctx = CGF.getContext();
@@ -326,7 +327,6 @@
   }
 
   QualType elementPtr = ctx.getPointerType(element.withConst());
-  llvm::Value *destPtr = Dest.getAddr();
 
   // Start pointer.
   if (!ctx.hasSameType(field->getType(), elementPtr)) {
@@ -416,8 +416,15 @@
       if (endOfInit) Builder.CreateStore(element, endOfInit);
     }
 
-    LValue elementLV = CGF.MakeAddrLValue(element, elementType);
-    EmitInitializationToLValue(E->getInit(i), elementLV);
+    // If these are nested std::initializer_list inits, do them directly,
+    // because they are conceptually the same "location".
+    InitListExpr *initList = dyn_cast<InitListExpr>(E->getInit(i));
+    if (initList && initList->initializesStdInitializerList()) {
+      EmitStdInitializerList(element, initList);
+    } else {
+      LValue elementLV = CGF.MakeAddrLValue(element, elementType);
+      EmitInitializationToLValue(E->getInit(i), elementLV);
+    }
   }
 
   // Check whether there's a non-trivial array-fill expression.
@@ -875,7 +882,7 @@
     CGF.ErrorUnsupported(E, "GNU array range designator extension");
 
   if (E->initializesStdInitializerList()) {
-    EmitStdInitializerList(E);
+    EmitStdInitializerList(Dest.getAddr(), E);
     return;
   }
 
@@ -1267,11 +1274,31 @@
       cast<InitListExpr>(init)->initializesStdInitializerList()) {
     // We initialized this std::initializer_list with an initializer list.
     // A backing array was created. Push a cleanup for it.
-    EmitStdInitializerListCleanup(lvalue, cast<InitListExpr>(init));
+    EmitStdInitializerListCleanup(lvalue.getAddress(),
+                                  cast<InitListExpr>(init));
+  }
+}
+
+static void EmitRecursiveStdInitializerListCleanup(CodeGenFunction &CGF,
+                                                   llvm::Value *arrayStart,
+                                                   const InitListExpr *init) {
+  // Check if there are any recursive cleanups to do, i.e. if we have
+  //   std::initializer_list<std::initializer_list<obj>> list = {{obj()}};
+  // then we need to destroy the inner array as well.
+  for (unsigned i = 0, e = init->getNumInits(); i != e; ++i) {
+    const InitListExpr *subInit = dyn_cast<InitListExpr>(init->getInit(i));
+    if (!subInit || !subInit->initializesStdInitializerList())
+      continue;
+
+    // This one needs to be destroyed. Get the address of the std::init_list.
+    llvm::Value *offset = llvm::ConstantInt::get(CGF.SizeTy, i);
+    llvm::Value *loc = CGF.Builder.CreateInBoundsGEP(arrayStart, offset,
+                                                 "std.initlist");
+    CGF.EmitStdInitializerListCleanup(loc, subInit);
   }
 }
 
-void CodeGenFunction::EmitStdInitializerListCleanup(LValue lvalue,
+void CodeGenFunction::EmitStdInitializerListCleanup(llvm::Value *loc,
                                                     const InitListExpr *init) {
   ASTContext &ctx = getContext();
   QualType element = GetStdInitializerListElementType(init->getType());
@@ -1283,10 +1310,12 @@
 
   // lvalue is the location of a std::initializer_list, which as its first
   // element has a pointer to the array we want to destroy.
-  llvm::Value *startPointer = Builder.CreateStructGEP(lvalue.getAddress(), 0,
-                                                      "startPointer");
-  llvm::Value *arrayAddress =
-      Builder.CreateBitCast(startPointer, arrayPtrType, "arrayAddress");
+  llvm::Value *startPointer = Builder.CreateStructGEP(loc, 0, "startPointer");
+  llvm::Value *startAddress = Builder.CreateLoad(startPointer, "startAddress");
 
+  ::EmitRecursiveStdInitializerListCleanup(*this, startAddress, init);
+
+  llvm::Value *arrayAddress =
+      Builder.CreateBitCast(startAddress, arrayPtrType, "arrayAddress");
   ::EmitStdInitializerListCleanup(*this, array, arrayAddress, init);
 }

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=150926&r1=150925&r2=150926&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Sun Feb 19 06:28:02 2012
@@ -1812,7 +1812,8 @@
   llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
 
   void MaybeEmitStdInitializerListCleanup(LValue lvalue, const Expr *init);
-  void EmitStdInitializerListCleanup(LValue lvalue, const InitListExpr *init);
+  void EmitStdInitializerListCleanup(llvm::Value *loc,
+                                     const InitListExpr *init);
 
   void EmitCheck(llvm::Value *, unsigned Size);
 

Modified: cfe/trunk/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp?rev=150926&r1=150925&r2=150926&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/cxx0x-initializer-stdinitializerlist.cpp Sun Feb 19 06:28:02 2012
@@ -136,3 +136,31 @@
   // CHECK: call void @_ZN10destroyme2D1Ev
   // CHECK: call void @_ZN10wantslist1D1Ev
 }
+
+void fn8() {
+  // CHECK: define void @_Z3fn8v
+  void target(std::initializer_list<std::initializer_list<destroyme1>>);
+  // objects should be destroyed before dm2, after call returns
+  // CHECK: call void @_Z6targetSt16initializer_listIS_I10destroyme1EE
+  std::initializer_list<destroyme1> inner;
+  target({ inner, { destroyme1() } });
+  // CHECK: call void @_ZN10destroyme1D1Ev
+  // Only one destroy loop, since only one inner init list is directly inited.
+  // CHECK-NOT: call void @_ZN10destroyme1D1Ev
+  destroyme2 dm2;
+  // CHECK: call void @_ZN10destroyme2D1Ev
+}
+
+void fn9() {
+  // CHECK: define void @_Z3fn9v
+  // objects should be destroyed after dm2
+  std::initializer_list<destroyme1> inner;
+  std::initializer_list<std::initializer_list<destroyme1>> list =
+      { inner, { destroyme1() } };
+  destroyme2 dm2;
+  // CHECK: call void @_ZN10destroyme2D1Ev
+  // CHECK: call void @_ZN10destroyme1D1Ev
+  // Only one destroy loop, since only one inner init list is directly inited.
+  // CHECK-NOT: call void @_ZN10destroyme1D1Ev
+  // CHECK: ret void
+}





More information about the cfe-commits mailing list