[cfe-commits] r81599 - in /cfe/trunk/lib/CodeGen: CGBlocks.cpp CGDecl.cpp CodeGenFunction.h

Anders Carlsson andersca at mac.com
Fri Sep 11 19:14:24 PDT 2009


Author: andersca
Date: Fri Sep 11 21:14:24 2009
New Revision: 81599

URL: http://llvm.org/viewvc/llvm-project?rev=81599&view=rev
Log:
For __block variables, cache the LLVM types as well as which LLVM field where the variable is stored.

Modified:
    cfe/trunk/lib/CodeGen/CGBlocks.cpp
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Fri Sep 11 21:14:24 2009
@@ -442,10 +442,10 @@
 }
 
 llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
-  uint64_t &offset = BlockDecls[E->getDecl()];
+  const ValueDecl *VD = E->getDecl();
+  
+  uint64_t &offset = BlockDecls[VD];
 
-  const llvm::Type *Ty;
-  Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
 
   // See if we have already allocated an offset for this variable.
   if (offset == 0) {
@@ -462,20 +462,23 @@
                                                          offset),
                                      "block.literal");
   if (E->isByRef()) {
-    bool needsCopyDispose = BlockRequiresCopying(E->getType());
     const llvm::Type *PtrStructTy
-      = llvm::PointerType::get(BuildByRefType(E->getDecl()), 0);
+      = llvm::PointerType::get(BuildByRefType(VD), 0);
     // The block literal will need a copy/destroy helper.
     BlockHasCopyDispose = true;
-    Ty = PtrStructTy;
+    
+    const llvm::Type *Ty = PtrStructTy;
     Ty = llvm::PointerType::get(Ty, 0);
     V = Builder.CreateBitCast(V, Ty);
     V = Builder.CreateLoad(V, false);
     V = Builder.CreateStructGEP(V, 1, "forwarding");
     V = Builder.CreateLoad(V, false);
     V = Builder.CreateBitCast(V, PtrStructTy);
-    V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
+    V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), 
+                                VD->getNameAsString());
   } else {
+    const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
+
     Ty = llvm::PointerType::get(Ty, 0);
     V = Builder.CreateBitCast(V, Ty);
   }

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Fri Sep 11 21:14:24 2009
@@ -199,6 +199,12 @@
   }
 }
 
+unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
+  assert(ByRefValueInfo.count(VD) && "Did not find value!");
+  
+  return ByRefValueInfo.find(VD)->second.second;
+}
+
 /// BuildByRefType - This routine changes a __block variable declared as T x
 ///   into:
 ///
@@ -212,8 +218,11 @@
 ///        T x;
 ///      } x
 ///
-/// Align is the alignment needed in bytes for x.
 const llvm::Type *CodeGenFunction::BuildByRefType(const ValueDecl *D) {
+  std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
+  if (Info.first)
+    return Info.first;
+  
   QualType Ty = D->getType();
   uint64_t Align = getContext().getDeclAlignInBytes(D);
   (void) Align;
@@ -237,7 +246,10 @@
   // FIXME: Align this on at least an Align boundary, assert if we can't.
   assert((Align <= unsigned(Target.getPointerAlign(0))/8)
          && "Can't align more than pointer yet");
-  Types[needsCopyDispose*2 + 4] = LTy;
+  
+  unsigned FieldNumber = needsCopyDispose*2 + 4;
+  
+  Types[FieldNumber] = LTy;
   
   const llvm::Type *T = llvm::StructType::get(VMContext, Types, false);
   
@@ -245,7 +257,10 @@
   CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(), 
                               ByRefTypeHolder.get());
   
-  return ByRefTypeHolder.get();
+  Info.first = ByRefTypeHolder.get();
+  Info.second = FieldNumber;
+  
+  return Info.first;
 }
 
 /// EmitLocalBlockVarDecl - Emit code and set up an entry in LocalDeclMap for a
@@ -371,10 +386,10 @@
 
   if (Init) {
     llvm::Value *Loc = DeclPtr;
-    if (isByRef) {
-      bool needsCopyDispose = BlockRequiresCopying(Ty);
-      Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x");
-    }
+    if (isByRef)
+      Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), 
+                                    D.getNameAsString());
+
     if (Ty->isReferenceType()) {
       RValue RV = EmitReferenceBindingToExpr(Init, Ty, /*IsInitializer=*/true);
       EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Ty);

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=81599&r1=81598&r2=81599&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Fri Sep 11 21:14:24 2009
@@ -292,6 +292,16 @@
   /// we know how many temporaries were created by a certain expression.
   llvm::SmallVector<size_t, 4> ConditionalTempDestructionStack;
 
+
+  /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
+  /// type as well as the field number that contains the actual data.
+  llvm::DenseMap<const ValueDecl *, std::pair<const llvm::Type *, 
+                                              unsigned> > ByRefValueInfo;
+  
+  /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
+  /// number that holds the value.
+  unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
+  
 public:
   CodeGenFunction(CodeGenModule &cgm);
 





More information about the cfe-commits mailing list