Before promoting a malloc type, remove dead uses. This makes instcombine
authorChris Lattner <sabre@nondot.org>
Mon, 24 Oct 2005 06:22:12 +0000 (06:22 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 24 Oct 2005 06:22:12 +0000 (06:22 +0000)
more effective at promoting these allocations, catching them earlier in the
compile process.

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

lib/Transforms/Scalar/InstructionCombining.cpp

index 4c6b6a82d7d633a281a27385ae96616e0693acbe..09fee7cc4bd0e6ec3f57d8046f9f6819a6476190 100644 (file)
@@ -3769,6 +3769,26 @@ Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI,
   const PointerType *PTy = dyn_cast<PointerType>(CI.getType());
   if (AI.isArrayAllocation() || !PTy) return 0;
   
+  // Remove any uses of AI that are dead.
+  assert(!CI.use_empty() && "Dead instructions should be removed earlier!");
+  std::vector<Instruction*> DeadUsers;
+  for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) {
+    Instruction *User = cast<Instruction>(*UI++);
+    if (isInstructionTriviallyDead(User)) {
+      while (UI != E && *UI == User)
+        ++UI; // If this instruction uses AI more than once, don't break UI.
+      
+      // Add operands to the worklist.
+      AddUsesToWorkList(*User);
+      ++NumDeadInst;
+      DEBUG(std::cerr << "IC: DCE: " << *User);
+      
+      User->eraseFromParent();
+      removeFromWorkList(User);
+    }
+  }
+  
+  // Finally, if the instruction now has one use, delete it.
   if (!AI.hasOneUse()) return 0;
   
   // Get the type really allocated and the type casted to.