[cfe-commits] r66091 - in /cfe/trunk: lib/CodeGen/CGBlocks.cpp test/CodeGen/blocks-1.c

Mike Stump mrs at apple.com
Wed Mar 4 14:48:10 PST 2009


Author: mrs
Date: Wed Mar  4 16:48:06 2009
New Revision: 66091

URL: http://llvm.org/viewvc/llvm-project?rev=66091&view=rev
Log:
Fixup __block codegen in nested block literals.

Modified:
    cfe/trunk/lib/CodeGen/CGBlocks.cpp
    cfe/trunk/test/CodeGen/blocks-1.c

Modified: cfe/trunk/lib/CodeGen/CGBlocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBlocks.cpp?rev=66091&r1=66090&r2=66091&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Wed Mar  4 16:48:06 2009
@@ -81,8 +81,8 @@
   const llvm::PointerType *PtrToInt8Ty
     = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
-  // same thing as CreateRuntimeFunction if there's already a variable with
-  // the same name.
+  // same thing as CreateRuntimeFunction if there's already a variable with the
+  // same name.
   NSConcreteGlobalBlock
     = new llvm::GlobalVariable(PtrToInt8Ty, false, 
                                llvm::GlobalValue::ExternalLinkage,
@@ -99,8 +99,8 @@
   const llvm::PointerType *PtrToInt8Ty
     = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
-  // same thing as CreateRuntimeFunction if there's already a variable with
-  // the same name.
+  // same thing as CreateRuntimeFunction if there's already a variable with the
+  // same name.
   NSConcreteStackBlock
     = new llvm::GlobalVariable(PtrToInt8Ty, false, 
                                llvm::GlobalValue::ExternalLinkage,
@@ -129,15 +129,15 @@
   }
 }
 
-/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block
-/// can be declared as a global variable instead of on the stack.
+/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
+/// declared as a global variable instead of on the stack.
 static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
 {
   return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
 }
 
-// FIXME: Push most into CGM, passing down a few bits, like current
-// function name.
+// FIXME: Push most into CGM, passing down a few bits, like current function
+// name.
 llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
 
   std::string Name = CurFn->getName();
@@ -145,10 +145,9 @@
   CollectBlockDeclRefInfo(BE->getBody(), Info);
 
   // Check if the block can be global.
-  // FIXME: This test doesn't work for nested blocks yet.  Longer
-  // term, I'd like to just have one code path.  We should move
-  // this function into CGM and pass CGF, then we can just check to
-  // see if CGF is 0.
+  // FIXME: This test doesn't work for nested blocks yet.  Longer term, I'd like
+  // to just have one code path.  We should move this function into CGM and pass
+  // CGF, then we can just check to see if CGF is 0.
   if (0 && CanBlockBeGlobal(Info))
     return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
     
@@ -275,9 +274,23 @@
           llvm::Value *Loc = r.getScalarVal();
           const llvm::Type *Ty = Types[i+5];
           if  (BDRE->isByRef()) {
+            // E is now the address of the value field, instead, we want the
+            // address of the actual ByRef struct.  We optimize this slightly
+            // compared to gcc by not grabbing the forwarding slot as this must
+            // be done during Block_copy for us, and we can postpone the work
+            // until then.
+            uint64_t offset = BlockDecls[BDRE->getDecl()];
+            
+            llvm::Value *BlockLiteral = LoadBlockStruct();
+            
+            Loc = Builder.CreateGEP(BlockLiteral,
+                                    llvm::ConstantInt::get(llvm::Type::Int64Ty,
+                                                           offset),
+                                    "block.literal");
+            Ty = llvm::PointerType::get(Ty, 0);
             Loc = Builder.CreateBitCast(Loc, Ty);
-            Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
-            Loc = Builder.CreateBitCast(Loc, Ty);
+            Loc = Builder.CreateLoad(Loc, false);
+            // Loc = Builder.CreateBitCast(Loc, Ty);
           }
           Builder.CreateStore(Loc, Addr);
         } else if (r.isComplex())
@@ -479,7 +492,7 @@
   llvm::Value *V = Builder.CreateGEP(BlockLiteral,
                                      llvm::ConstantInt::get(llvm::Type::Int64Ty,
                                                             offset),
-                                     "tmp");
+                                     "block.literal");
   if (E->isByRef()) {
     bool needsCopyDispose = BlockRequiresCopying(E->getType());
     uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());

Modified: cfe/trunk/test/CodeGen/blocks-1.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/blocks-1.c?rev=66091&r1=66090&r2=66091&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/blocks-1.c (original)
+++ cfe/trunk/test/CodeGen/blocks-1.c Wed Mar  4 16:48:06 2009
@@ -1,14 +1,34 @@
 // RUN: clang %s -emit-llvm -o %t -fblocks -f__block
 #include <stdio.h>
 
+void test1() {
+  __block int a;
+  int b=2;
+  a=1;
+  printf("a is %d, b is %d\n", a, b);
+  ^{ a = 10; printf("a is %d, b is %d\n", a, b); }();
+  printf("a is %d, b is %d\n", a, b);
+  a = 1;
+  printf("a is %d, b is %d\n", a, b);
+}
+
+
+void test2() {
+  __block int a;
+  a=1;
+  printf("a is %d\n", a);
+  ^{
+    ^{
+      a = 10;
+    }();
+  }();
+  printf("a is %d\n", a);
+  a = 1;
+  printf("a is %d\n", a);
+}
+
 int main() {
-    __block int a;
-    int b=2;
-    a=1;
-    printf("a is %d, b is %d\n", a, b);
-    ^{ a = 10; printf("a is %d, b is %d\n", a, b); }();
-    printf("a is %d, b is %d\n", a, b);
-    a = 1;
-    printf("a is %d, b is %d\n", a, b);
-    return 0;
+  test1();
+  test2();
+  return 0;
 }





More information about the cfe-commits mailing list