A dead malloc, a free(NULL) and a free(undef) are all trivially dead
authorNick Lewycky <nicholas@mxc.ca>
Mon, 24 Oct 2011 04:35:36 +0000 (04:35 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Mon, 24 Oct 2011 04:35:36 +0000 (04:35 +0000)
instructions.

This doesn't introduce any optimizations we weren't doing before (except
potentially due to pass ordering issues), now passes will eliminate them sooner
as part of their own cleanups.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142787 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 7034feb227ad94c21c222e84515219b8a23edcc8..134ab71050a33e384e01b9ef992d553c2e2b9ba4 100644 (file)
@@ -28,6 +28,7 @@
 #include "llvm/Analysis/DIBuilder.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Analysis/InstructionSimplify.h"
+#include "llvm/Analysis/MemoryBuiltins.h"
 #include "llvm/Analysis/ProfileInfo.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/Target/TargetData.h"
@@ -257,6 +258,13 @@ bool llvm::isInstructionTriviallyDead(Instruction *I) {
         II->getIntrinsicID() == Intrinsic::lifetime_end)
       return isa<UndefValue>(II->getArgOperand(1));
   }
+
+  if (extractMallocCall(I)) return true;
+
+  if (CallInst *CI = isFreeCall(I))
+    if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0)))
+      return C->isNullValue() || isa<UndefValue>(C);
+
   return false;
 }
 
index 1703ee961f5cf9eae6064c875b4ddb8612835a36..81eb5a8c705e6dd25146148df3104cd740726c72 100644 (file)
@@ -257,5 +257,4 @@ define void @test20() {
   ret void
 }
 ; CHECK: @test20
-; CHECK-NOT: store
-; CHECK: ret void
+; CHECK-NEXT: ret void