Remove support for NOT instruction
[oota-llvm.git] / lib / Transforms / Scalar / GCSE.cpp
index 568f3db4f34802868aaf4763cdbe928409cd9c31..a99a502348ef72fdce9dee19f64757e1e6f8630e 100644 (file)
@@ -48,10 +48,9 @@ namespace {
     // instruction being checked.  They should return true if a common
     // subexpression was folded.
     //
-    bool visitUnaryOperator(Instruction &I);
     bool visitBinaryOperator(Instruction &I);
     bool visitGetElementPtrInst(GetElementPtrInst &I);
-    bool visitCastInst(CastInst &I){return visitUnaryOperator((Instruction&)I);}
+    bool visitCastInst(CastInst &I);
     bool visitShiftInst(ShiftInst &I) {
       return visitBinaryOperator((Instruction&)I);
     }
@@ -79,12 +78,12 @@ namespace {
     // This transformation requires dominator and immediate dominator info
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.preservesCFG();
-      AU.addRequired(DominatorSet::ID);
-      AU.addRequired(ImmediateDominators::ID); 
+      AU.addRequired<DominatorSet>();
+      AU.addRequired<ImmediateDominators>();
     }
   };
 
-  RegisterPass<GCSE> X("gcse", "Global Common Subexpression Elimination");
+  RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
 }
 
 // createGCSEPass - The public interface to this file...
@@ -192,6 +191,34 @@ void GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
   } else if (DomSetInfo->dominates(BB2, BB1)) {    // Other dom I?
     ReplaceInstWithInst(Other, I);
   } else {
+    // This code is disabled because it has several problems:
+    // One, the actual assumption is wrong, as shown by this code:
+    // int "test"(int %X, int %Y) {
+    //         %Z = add int %X, %Y
+    //         ret int %Z
+    // Unreachable:
+    //         %Q = add int %X, %Y
+    //         ret int %Q
+    // }
+    //
+    // Here there are no shared dominators.  Additionally, this had the habit of
+    // moving computations where they were not always computed.  For example, in
+    // a cast like this:
+    //  if (c) {
+    //    if (d)  ...
+    //    else ... X+Y ...
+    //  } else {
+    //    ... X+Y ...
+    //  }
+    // 
+    // In thiscase, the expression would be hoisted to outside the 'if' stmt,
+    // causing the expression to be evaluated, even for the if (d) path, which
+    // could cause problems, if, for example, it caused a divide by zero.  In
+    // general the problem this case is trying to solve is better addressed with
+    // PRE than GCSE.
+    //
+
+#if 0
     // Handle the most general case now.  In this case, neither I dom Other nor
     // Other dom I.  Because we are in SSA form, we are guaranteed that the
     // operands of the two instructions both dominate the uses, so we _know_
@@ -215,6 +242,7 @@ void GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
 
     // Eliminate 'Other' now.
     ReplaceInstWithInst(I, Other);
+#endif
   }
 }
 
@@ -225,14 +253,14 @@ void GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
 //
 //===----------------------------------------------------------------------===//
 
-bool GCSE::visitUnaryOperator(Instruction &I) {
+bool GCSE::visitCastInst(CastInst &I) {
   Value *Op = I.getOperand(0);
   Function *F = I.getParent()->getParent();
   
   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
        UI != UE; ++UI)
     if (Instruction *Other = dyn_cast<Instruction>(*UI))
-      // Check to see if this new binary operator is not I, but same operand...
+      // Check to see if this new cast is not I, but has the same operand...
       if (Other != &I && Other->getOpcode() == I.getOpcode() &&
           Other->getOperand(0) == Op &&     // Is the operand the same?
           // Is it embeded in the same function?  (This could be false if LHS