teach DSE and isInstructionTriviallyDead() about calloc
authorNuno Lopes <nunoplopes@sapo.pt>
Thu, 10 May 2012 17:14:00 +0000 (17:14 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Thu, 10 May 2012 17:14:00 +0000 (17:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156553 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/DeadStoreElimination.cpp
lib/Transforms/Utils/Local.cpp
test/Transforms/DeadStoreElimination/simple.ll

index c8c53606015aa98a198ef0cea9ed9d2d09b40eea..a46e802f4c2c03aa57291166022388f529c0d1d5 100644 (file)
@@ -282,6 +282,12 @@ static uint64_t getPointerSize(const Value *V, AliasAnalysis &AA) {
       return C->getZExtValue();
   }
 
+  if (const CallInst *CI = extractCallocCall(V)) {
+    if (const ConstantInt *C1 = dyn_cast<ConstantInt>(CI->getArgOperand(0)))
+      if (const ConstantInt *C2 = dyn_cast<ConstantInt>(CI->getArgOperand(1)))
+       return (C1->getValue() * C2->getValue()).getZExtValue();
+  }
+
   if (TD == 0)
     return AliasAnalysis::UnknownSize;
 
@@ -704,9 +710,11 @@ bool DSE::handleEndBlock(BasicBlock &BB) {
 
     // Okay, so these are dead heap objects, but if the pointer never escapes
     // then it's leaked by this function anyways.
-    if (CallInst *CI = extractMallocCall(I))
-      if (!PointerMayBeCaptured(CI, true, true))
-        DeadStackObjects.insert(CI);
+    CallInst *CI = extractMallocCall(I);
+    if (!CI)
+      CI = extractCallocCall(I);
+    if (CI && !PointerMayBeCaptured(CI, true, true))
+      DeadStackObjects.insert(CI);
   }
 
   // Treat byval arguments the same, stores to them are dead at the end of the
@@ -759,6 +767,11 @@ bool DSE::handleEndBlock(BasicBlock &BB) {
       continue;
     }
 
+    if (CallInst *CI = extractCallocCall(BBI)) {
+      DeadStackObjects.erase(CI);
+      continue;
+    }
+
     if (CallSite CS = cast<Value>(BBI)) {
       // If this call does not access memory, it can't be loading any of our
       // pointers.
index d1c4d5968231f6046de76523de61e6de950fc7c0..4e1a00fa2d3d13565ac3f1d73ed8e4963397365d 100644 (file)
@@ -260,7 +260,7 @@ bool llvm::isInstructionTriviallyDead(Instruction *I) {
       return isa<UndefValue>(II->getArgOperand(1));
   }
 
-  if (extractMallocCall(I)) return true;
+  if (extractMallocCall(I) || extractCallocCall(I)) return true;
 
   if (CallInst *CI = isFreeCall(I))
     if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0)))
index 81eb5a8c705e6dd25146148df3104cd740726c72..7c8a9b3337ebb7cb6d4ea01c737871bcd26f8ee9 100644 (file)
@@ -164,7 +164,7 @@ define i32* @test13() {
 }
 
 declare noalias i8* @malloc(i32)
-
+declare noalias i8* @calloc(i32, i32)
 
 
 define void @test14(i32* %Q) {
@@ -258,3 +258,11 @@ define void @test20() {
 }
 ; CHECK: @test20
 ; CHECK-NEXT: ret void
+
+; CHECK: @test21
+define void @test21() {
+  %m = call i8* @calloc(i32 9, i32 7)
+  store i8 0, i8* %m
+; CHECK-NEXT: ret void
+  ret void
+}