[llvm-commits] [llvm] r136732 - in /llvm/trunk: lib/Transforms/InstCombine/InstructionCombining.cpp test/Transforms/InstCombine/malloc-free-delete.ll

Nick Lewycky nicholas at mxc.ca
Tue Aug 2 15:08:02 PDT 2011


Author: nicholas
Date: Tue Aug  2 17:08:01 2011
New Revision: 136732

URL: http://llvm.org/viewvc/llvm-project?rev=136732&view=rev
Log:
Teach InstCombine that lifetime intrincs aren't a real user on the result of a
malloc call.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=136732&r1=136731&r2=136732&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Tue Aug  2 17:08:01 2011
@@ -1043,7 +1043,10 @@
 
 
 
-static bool IsOnlyNullComparedAndFreed(const Value &V) {
+static bool IsOnlyNullComparedAndFreed(const Value &V, int Depth = 0) {
+  if (Depth == 8)
+    return false;
+
   for (Value::const_use_iterator UI = V.use_begin(), UE = V.use_end();
        UI != UE; ++UI) {
     const User *U = *UI;
@@ -1052,6 +1055,20 @@
     if (const ICmpInst *ICI = dyn_cast<ICmpInst>(U))
       if (ICI->isEquality() && isa<ConstantPointerNull>(ICI->getOperand(1)))
         continue;
+    if (const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
+      if (IsOnlyNullComparedAndFreed(*BCI, Depth+1))
+        continue;
+    }
+    if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
+      if (GEPI->hasAllZeroIndices() &&
+          IsOnlyNullComparedAndFreed(*GEPI, Depth+1))
+        continue;
+    }
+    if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
+      if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
+          II->getIntrinsicID() == Intrinsic::lifetime_end)
+        continue;
+    }
     return false;
   }
   return true;
@@ -1064,22 +1081,29 @@
   if (IsOnlyNullComparedAndFreed(MI)) {
     for (Value::use_iterator UI = MI.use_begin(), UE = MI.use_end();
          UI != UE;) {
-      // We can assume that every remaining use is a free call or an icmp eq/ne
-      // to null, so the cast is safe.
+      // All the users permitted by IsOnlyNullComparedAndFreed are Instructions.
       Instruction *I = cast<Instruction>(*UI);
 
       // Early increment here, as we're about to get rid of the user.
       ++UI;
 
-      if (isFreeCall(I)) {
-        EraseInstFromFunction(*cast<CallInst>(I));
-        continue;
+      if (CallInst *CI = isFreeCall(I)) {
+        if (CI != I)
+          EraseInstFromFunction(*CI);
+        EraseInstFromFunction(*I);
+      } else if (ICmpInst *C = dyn_cast<ICmpInst>(I)) {
+        ReplaceInstUsesWith(*C,
+                            ConstantInt::get(Type::getInt1Ty(C->getContext()),
+                                             C->isFalseWhenEqual()));
+        EraseInstFromFunction(*C);
+      } else if (I->getType()->isVoidTy()) {
+        // An all-zero GEP or a bitcast.
+        ReplaceInstUsesWith(*I, UndefValue::get(I->getType()));
+        EraseInstFromFunction(*I);
+      } else {
+        // A lifetime intrinsic.
+        EraseInstFromFunction(*I);
       }
-      // Again, the cast is safe.
-      ICmpInst *C = cast<ICmpInst>(I);
-      ReplaceInstUsesWith(*C, ConstantInt::get(Type::getInt1Ty(C->getContext()),
-                                               C->isFalseWhenEqual()));
-      EraseInstFromFunction(*C);
     }
     return EraseInstFromFunction(MI);
   }

Modified: llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll?rev=136732&r1=136731&r2=136732&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/malloc-free-delete.ll Tue Aug  2 17:08:01 2011
@@ -12,7 +12,7 @@
 ; CHECK: ret i32 0
 }
 
-declare i8* @malloc(i32)
+declare noalias i8* @malloc(i32)
 declare void @free(i8*)
 
 define i1 @foo() {
@@ -23,3 +23,15 @@
   call void @free(i8* %m)
   ret i1 %z
 }
+
+declare void @llvm.lifetime.start(i64, i8*)
+declare void @llvm.lifetime.end(i64, i8*)
+
+define void @test3() {
+; CHECK: @test3
+; CHECK-NEXT: ret void
+  %a = call noalias i8* @malloc(i32 10)
+  call void @llvm.lifetime.start(i64 10, i8* %a)
+  call void @llvm.lifetime.end(i64 10, i8* %a)
+  ret void
+}





More information about the llvm-commits mailing list