[cfe-commits] r91246 - /cfe/trunk/lib/CodeGen/CGExprCXX.cpp

Anders Carlsson andersca at mac.com
Sun Dec 13 10:48:07 PST 2009


Author: andersca
Date: Sun Dec 13 12:48:07 2009
New Revision: 91246

URL: http://llvm.org/viewvc/llvm-project?rev=91246&view=rev
Log:
If the usual array deallocation function of a class takes two arguments, we need to pass the number of bytes allocated to the deallocation function, just not the number of bytes in a single element.

Modified:
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Sun Dec 13 12:48:07 2009
@@ -250,10 +250,31 @@
   DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy));
 
   if (DeleteFTy->getNumArgs() == 2) {
+    uint64_t DeleteTypeSize = getContext().getTypeSize(DeleteTy) / 8;
     QualType SizeTy = DeleteFTy->getArgType(1);
-    uint64_t SizeVal = getContext().getTypeSize(DeleteTy) / 8;
-    llvm::Constant *Size = llvm::ConstantInt::get(ConvertType(SizeTy),
-                                                  SizeVal);
+    llvm::Value *Size = 
+      llvm::ConstantInt::get(ConvertType(SizeTy), DeleteTypeSize);
+
+    if (DeleteFD->getOverloadedOperator() == OO_Array_Delete) {
+      // We need to get the number of elements in the array from the cookie.
+      uint64_t DeleteTypeAlign = getContext().getTypeAlign(DeleteTy);
+      unsigned CookiePadding = std::max(getContext().getTypeSize(SizeTy),
+                                        DeleteTypeAlign) / 8;
+      assert(CookiePadding && "CookiePadding should not be 0.");
+      
+      uint64_t CookieOffset =
+        CookiePadding - getContext().getTypeSize(SizeTy) / 8;
+      llvm::Value *NumElementsPtr =
+        Builder.CreateConstInBoundsGEP1_64(Ptr, CookieOffset);
+      NumElementsPtr = 
+        Builder.CreateBitCast(NumElementsPtr,
+                              ConvertType(SizeTy)->getPointerTo());
+      llvm::Value *NumElements = Builder.CreateLoad(NumElementsPtr);
+    
+      // Multiply the size with the number of elements.
+      Size = Builder.CreateMul(NumElements, Size);
+    }
+     
     DeleteArgs.push_back(std::make_pair(RValue::get(Size), SizeTy));
   }
 
@@ -303,31 +324,29 @@
           QualType SizeTy = getContext().getSizeType();
           uint64_t CookiePadding = std::max(getContext().getTypeSize(SizeTy),
                 static_cast<uint64_t>(getContext().getTypeAlign(DeleteTy))) / 8;
-          if (CookiePadding) {
-            llvm::Type *Ptr8Ty = 
-              llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
-            uint64_t CookieOffset =
-              CookiePadding - getContext().getTypeSize(SizeTy) / 8;
-            llvm::Value *AllocatedObjectPtr = 
-              Builder.CreateConstInBoundsGEP1_64(
-                            Builder.CreateBitCast(Ptr, Ptr8Ty), -CookiePadding);
-            llvm::Value *NumElementsPtr =
-              Builder.CreateConstInBoundsGEP1_64(AllocatedObjectPtr, 
-                                                 CookieOffset);
-            NumElementsPtr = Builder.CreateBitCast(NumElementsPtr,
-                                          ConvertType(SizeTy)->getPointerTo());
+          assert(CookiePadding && "CookiePadding should not be 0.");
+                 
+          const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
+          uint64_t CookieOffset =
+            CookiePadding - getContext().getTypeSize(SizeTy) / 8;
+          llvm::Value *AllocatedObjectPtr = 
+            Builder.CreateConstInBoundsGEP1_64(
+                          Builder.CreateBitCast(Ptr, Int8PtrTy), 
+                                               -CookiePadding);
+          llvm::Value *NumElementsPtr =
+            Builder.CreateConstInBoundsGEP1_64(AllocatedObjectPtr, 
+                                               CookieOffset);
+          NumElementsPtr = Builder.CreateBitCast(NumElementsPtr,
+                                        ConvertType(SizeTy)->getPointerTo());
             
-            llvm::Value *NumElements = 
-              Builder.CreateLoad(NumElementsPtr);
-            NumElements = 
-              Builder.CreateIntCast(NumElements, 
-                                    llvm::Type::getInt64Ty(VMContext), false, 
-                                    "count.tmp");
-            EmitCXXAggrDestructorCall(Dtor, NumElements, Ptr);
-            Ptr = AllocatedObjectPtr;
-          }
-        }
-        else if (Dtor->isVirtual()) {
+          llvm::Value *NumElements = Builder.CreateLoad(NumElementsPtr);
+          NumElements = 
+            Builder.CreateIntCast(NumElements, 
+                                  llvm::Type::getInt64Ty(VMContext), false, 
+                                  "count.tmp");
+          EmitCXXAggrDestructorCall(Dtor, NumElements, Ptr);
+          Ptr = AllocatedObjectPtr;
+        } else if (Dtor->isVirtual()) {
           const llvm::Type *Ty =
             CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(Dtor),
                                            /*isVariadic=*/false);





More information about the cfe-commits mailing list