Add support for removing invoke instructions
[oota-llvm.git] / lib / Transforms / Scalar / GCSE.cpp
index 8ed8238806d53695ee6c69a2b51655c6ee3e1c65..7db7d4dcde71601ef8660ac6fca68ddbfb42d2bf 100644 (file)
@@ -160,11 +160,21 @@ void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
     ++NumCallRemoved; // Keep track of calls eliminated
   ++NumInstRemoved;   // Keep track of number of insts eliminated
 
+  // Update value numbering
+  getAnalysis<ValueNumbering>().deleteInstruction(I);
+
   // If we are not replacing the instruction with a constant, we cannot do
   // anything special.
   if (!isa<Constant>(V)) {
     I->replaceAllUsesWith(V);
 
+    if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
+      // Removing an invoke instruction requires adding a branch to the normal
+      // destination and removing PHI node entries in the exception destination.
+      new BranchInst(II->getNormalDest(), II);
+      II->getUnwindDest()->removePredecessor(II->getParent());
+    }
+    
     // Erase the instruction from the program.
     I->getParent()->getInstList().erase(I);
     return;
@@ -176,6 +186,13 @@ void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
   // Perform the replacement.
   I->replaceAllUsesWith(C);
 
+  if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
+    // Removing an invoke instruction requires adding a branch to the normal
+    // destination and removing PHI node entries in the exception destination.
+    new BranchInst(II->getNormalDest(), II);
+    II->getUnwindDest()->removePredecessor(II->getParent());
+  }
+
   // Erase the instruction from the program.
   I->getParent()->getInstList().erase(I);