Changes For Bug 352
[oota-llvm.git] / lib / Transforms / Scalar / GCSE.cpp
index bbefe8157f4acead1cf28a2a6f4be96edbec2e71..776ff6603aa4b1af6ec2374293f1411e5c8faad0 100644 (file)
@@ -1,4 +1,4 @@
-//===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===//
+//===-- GCSE.cpp - SSA-based Global Common Subexpression Elimination ------===//
 // 
 //                     The LLVM Compiler Infrastructure
 //
@@ -22,8 +22,8 @@
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Analysis/ValueNumbering.h"
 #include "llvm/Transforms/Utils/Local.h"
-#include "Support/DepthFirstIterator.h"
-#include "Support/Statistic.h"
+#include "llvm/ADT/DepthFirstIterator.h"
+#include "llvm/ADT/Statistic.h"
 #include <algorithm>
 using namespace llvm;
 
@@ -33,6 +33,8 @@ namespace {
   Statistic<> NumCallRemoved("gcse", "Number of calls removed");
   Statistic<> NumNonInsts   ("gcse", "Number of instructions removed due "
                              "to non-instruction values");
+  Statistic<> NumArgsRepl   ("gcse", "Number of function arguments replaced "
+                             "with constant values");
 
   struct GCSE : public FunctionPass {
     virtual bool runOnFunction(Function &F);
@@ -68,6 +70,24 @@ bool GCSE::runOnFunction(Function &F) {
 
   std::vector<Value*> EqualValues;
 
+  // Check for value numbers of arguments.  If the value numbering
+  // implementation can prove that an incoming argument is a constant or global
+  // value address, substitute it, making the argument dead.
+  for (Function::aiterator AI = F.abegin(), E = F.aend(); AI != E; ++AI)
+    if (!AI->use_empty()) {
+      VN.getEqualNumberNodes(AI, EqualValues);
+      if (!EqualValues.empty()) {
+        for (unsigned i = 0, e = EqualValues.size(); i != e; ++i)
+          if (isa<Constant>(EqualValues[i])) {
+            AI->replaceAllUsesWith(EqualValues[i]);
+            ++NumArgsRepl;
+            Changed = true;
+            break;
+          }
+        EqualValues.clear();
+      }
+    }
+
   // Traverse the CFG of the function in dominator order, so that we see each
   // instruction after we see its operands.
   for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()),
@@ -161,12 +181,19 @@ void GCSE::ReplaceInstructionWith(Instruction *I, Value *V) {
   ++NumInstRemoved;   // Keep track of number of insts eliminated
 
   // Update value numbering
-  getAnalysis<ValueNumbering>().deleteInstruction(I);
+  getAnalysis<ValueNumbering>().deleteValue(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);
@@ -179,6 +206,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);