Reapplied r81355 with the problems fixed.
[oota-llvm.git] / lib / Transforms / Scalar / GVNPRE.cpp
index 80f745f333b1bb19b02b0df89f806af146b4f9fc..b7462f2ac214fbbe38d5898c53f75d5bd0741d8c 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the Owen Anderson and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -16,6 +16,9 @@
 // live ranges, and should be used with caution on platforms that are very 
 // sensitive to register pressure.
 //
+// Note that this pass does the value numbering itself, it does not use the
+// ValueNumbering analysis passes.
+//
 //===----------------------------------------------------------------------===//
 
 #define DEBUG_TYPE "gvnpre"
@@ -34,8 +37,8 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
 #include "llvm/Support/CFG.h"
-#include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/ErrorHandling.h"
 #include <algorithm>
 #include <deque>
 #include <map>
@@ -45,12 +48,15 @@ using namespace llvm;
 //                         ValueTable Class
 //===----------------------------------------------------------------------===//
 
+namespace {
+
 /// This class holds the mapping between values and value numbers.  It is used
 /// as an efficient mechanism to determine the expression-wise equivalence of
 /// two values.
 
 struct Expression {
-  enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM, 
+  enum ExpressionOpcode { ADD, FADD, SUB, FSUB, MUL, FMUL,
+                          UDIV, SDIV, FDIV, UREM, SREM,
                           FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ, 
                           ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE, 
                           ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ, 
@@ -70,7 +76,7 @@ struct Expression {
   SmallVector<uint32_t, 4> varargs;
   
   Expression() { }
-  Expression(ExpressionOpcode o) : opcode(o) { }
+  explicit Expression(ExpressionOpcode o) : opcode(o) { }
   
   bool operator==(const Expression &other) const {
     if (opcode != other.opcode)
@@ -123,9 +129,10 @@ struct Expression {
   }
 };
 
+}
 
 namespace {
-  class VISIBILITY_HIDDEN ValueTable {
+  class ValueTable {
     private:
       DenseMap<Value*, uint32_t> valueNumbering;
       DenseMap<Expression, uint32_t> expressionNumbering;
@@ -155,9 +162,14 @@ namespace {
 }
 
 namespace llvm {
-template <> struct DenseMapKeyInfo<Expression> {
-  static inline Expression getEmptyKey() { return Expression(Expression::EMPTY); }
-  static inline Expression getTombstoneKey() { return Expression(Expression::TOMBSTONE); }
+template <> struct DenseMapInfo<Expression> {
+  static inline Expression getEmptyKey() {
+    return Expression(Expression::EMPTY);
+  }
+  
+  static inline Expression getTombstoneKey() {
+    return Expression(Expression::TOMBSTONE);
+  }
   
   static unsigned getHashValue(const Expression e) {
     unsigned hash = e.opcode;
@@ -166,16 +178,19 @@ template <> struct DenseMapKeyInfo<Expression> {
     hash = e.secondVN + hash * 37;
     hash = e.thirdVN + hash * 37;
     
-    hash = (unsigned)((uintptr_t)e.type >> 4) ^
-            (unsigned)((uintptr_t)e.type >> 9) +
-            hash * 37;
+    hash = ((unsigned)((uintptr_t)e.type >> 4) ^
+            (unsigned)((uintptr_t)e.type >> 9)) +
+           hash * 37;
     
-    for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(), E = e.varargs.end();
-         I != E; ++I)
+    for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
+         E = e.varargs.end(); I != E; ++I)
       hash = *I + hash * 37;
     
     return hash;
   }
+  static bool isEqual(const Expression &LHS, const Expression &RHS) {
+    return LHS == RHS;
+  }
   static bool isPod() { return true; }
 };
 }
@@ -188,10 +203,16 @@ Expression::ExpressionOpcode
   switch(BO->getOpcode()) {
     case Instruction::Add:
       return Expression::ADD;
+    case Instruction::FAdd:
+      return Expression::FADD;
     case Instruction::Sub:
       return Expression::SUB;
+    case Instruction::FSub:
+      return Expression::FSUB;
     case Instruction::Mul:
       return Expression::MUL;
+    case Instruction::FMul:
+      return Expression::FMUL;
     case Instruction::UDiv:
       return Expression::UDIV;
     case Instruction::SDiv:
@@ -219,7 +240,7 @@ Expression::ExpressionOpcode
     
     // THIS SHOULD NEVER HAPPEN
     default:
-      assert(0 && "Binary operator with unknown opcode?");
+      llvm_unreachable("Binary operator with unknown opcode?");
       return Expression::ADD;
   }
 }
@@ -250,7 +271,7 @@ Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
       
       // THIS SHOULD NEVER HAPPEN
       default:
-        assert(0 && "Comparison with unknown predicate?");
+        llvm_unreachable("Comparison with unknown predicate?");
         return Expression::ICMPEQ;
     }
   } else {
@@ -286,7 +307,7 @@ Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
       
       // THIS SHOULD NEVER HAPPEN
       default:
-        assert(0 && "Comparison with unknown predicate?");
+        llvm_unreachable("Comparison with unknown predicate?");
         return Expression::FCMPOEQ;
     }
   }
@@ -322,7 +343,7 @@ Expression::ExpressionOpcode
     
     // THIS SHOULD NEVER HAPPEN
     default:
-      assert(0 && "Cast operator with unknown opcode?");
+      llvm_unreachable("Cast operator with unknown opcode?");
       return Expression::BITCAST;
   }
 }
@@ -556,7 +577,7 @@ uint32_t ValueTable::lookup(Value* V) const {
   if (VI != valueNumbering.end())
     return VI->second;
   else
-    assert(0 && "Value not numbered?");
+    llvm_unreachable("Value not numbered?");
   
   return 0;
 }
@@ -588,6 +609,8 @@ unsigned ValueTable::size() {
   return nextValueNumber;
 }
 
+namespace {
+
 //===----------------------------------------------------------------------===//
 //                       ValueNumberedSet Class
 //===----------------------------------------------------------------------===//
@@ -644,17 +667,18 @@ class ValueNumberedSet {
     }
 };
 
+}
+
 //===----------------------------------------------------------------------===//
 //                         GVNPRE Pass
 //===----------------------------------------------------------------------===//
 
 namespace {
-
-  class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
+  class GVNPRE : public FunctionPass {
     bool runOnFunction(Function &F);
   public:
     static char ID; // Pass identification, replacement for typeid
-    GVNPRE() : FunctionPass((intptr_t)&ID) { }
+    GVNPRE() : FunctionPass(&ID) {}
 
   private:
     ValueTable VN;
@@ -723,7 +747,7 @@ namespace {
 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
 
 static RegisterPass<GVNPRE> X("gvnpre",
-                              "Global Value Numbering/Partial Redundancy Elimination");
+                      "Global Value Numbering/Partial Redundancy Elimination");
 
 
 STATISTIC(NumInsertedVals, "Number of values inserted");
@@ -742,7 +766,7 @@ Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) {
     if (v == VN.lookup(*I))
       return *I;
   
-  assert(0 && "No leader found, but present bit is set?");
+  llvm_unreachable("No leader found, but present bit is set?");
   return 0;
 }
 
@@ -789,7 +813,7 @@ Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
     if (newOp1 != U->getOperand(0)) {
       Instruction* newVal = 0;
       if (CastInst* C = dyn_cast<CastInst>(U))
-        newVal = CastInst::create(C->getOpcode(),
+        newVal = CastInst::Create(C->getOpcode(),
                                   newOp1, C->getType(),
                                   C->getName()+".expr");
       
@@ -832,16 +856,17 @@ Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
     if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
       Instruction* newVal = 0;
       if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
-        newVal = BinaryOperator::create(BO->getOpcode(),
+        newVal = BinaryOperator::Create(BO->getOpcode(),
                                         newOp1, newOp2,
                                         BO->getName()+".expr");
       else if (CmpInst* C = dyn_cast<CmpInst>(U))
-        newVal = CmpInst::create(C->getOpcode(),
+        newVal = CmpInst::Create(C->getOpcode(),
                                  C->getPredicate(),
                                  newOp1, newOp2,
                                  C->getName()+".expr");
       else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
-        newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
+        newVal = ExtractElementInst::Create(newOp1, newOp2, 
+                                            E->getName()+".expr");
       
       uint32_t v = VN.lookup_or_add(newVal);
       
@@ -894,12 +919,13 @@ Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
       Instruction* newVal = 0;
       if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
         newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
-                                       S->getName()+".expr");
+                                       S->getName() + ".expr");
       else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
-        newVal = new InsertElementInst(newOp1, newOp2, newOp3,
-                                       I->getName()+".expr");
+        newVal = InsertElementInst::Create(newOp1, newOp2, newOp3,
+                                           I->getName() + ".expr");
       else if (SelectInst* I = dyn_cast<SelectInst>(U))
-        newVal = new SelectInst(newOp1, newOp2, newOp3, I->getName()+".expr");
+        newVal = SelectInst::Create(newOp1, newOp2, newOp3,
+                                    I->getName() + ".expr");
       
       uint32_t v = VN.lookup_or_add(newVal);
       
@@ -939,9 +965,10 @@ Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
       }
     
     if (newOp1 != U->getPointerOperand() || changed_idx) {
-      Instruction* newVal = new GetElementPtrInst(newOp1,
-                                       &newIdx[0], newIdx.size(),
-                                       U->getName()+".expr");
+      Instruction* newVal =
+          GetElementPtrInst::Create(newOp1,
+                                    newIdx.begin(), newIdx.end(),
+                                    U->getName()+".expr");
       
       uint32_t v = VN.lookup_or_add(newVal);
       
@@ -1191,13 +1218,13 @@ void GVNPRE::topo_sort(ValueNumberedSet& set, SmallVector<Value*, 8>& vec) {
 
 /// dump - Dump a set of values to standard error
 void GVNPRE::dump(ValueNumberedSet& s) const {
-  DOUT << "{ ";
+  DEBUG(errs() << "{ ");
   for (ValueNumberedSet::iterator I = s.begin(), E = s.end();
        I != E; ++I) {
-    DOUT << "" << VN.lookup(*I) << ": ";
+    DEBUG(errs() << "" << VN.lookup(*I) << ": ");
     DEBUG((*I)->dump());
   }
-  DOUT << "}\n\n";
+  DEBUG(errs() << "}\n\n");
 }
 
 /// elimination - Phase 3 of the main algorithm.  Perform full redundancy 
@@ -1223,7 +1250,8 @@ bool GVNPRE::elimination() {
           isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) ||
           isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) {
         
-        if (availableOut[BB].test(VN.lookup(BI)) && !availableOut[BB].count(BI)) {
+        if (availableOut[BB].test(VN.lookup(BI)) &&
+            !availableOut[BB].count(BI)) {
           Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
           if (Instruction* Instr = dyn_cast<Instruction>(leader))
             if (Instr->getParent() != 0 && Instr != BI) {
@@ -1243,8 +1271,8 @@ bool GVNPRE::elimination() {
     changed_function = true;
   }
     
-  for (SmallVector<Instruction*, 8>::iterator I = erase.begin(), E = erase.end();
-       I != E; ++I)
+  for (SmallVector<Instruction*, 8>::iterator I = erase.begin(),
+       E = erase.end(); I != E; ++I)
      (*I)->eraseFromParent();
   
   return changed_function;
@@ -1590,7 +1618,7 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
           isa<ShuffleVectorInst>(U) ||
           isa<ExtractElementInst>(U) ||
           isa<InsertElementInst>(U) ||
-          isa<SelectInst>(U))
+          isa<SelectInst>(U)) {
         if (isa<BinaryOperator>(U->getOperand(1)) || 
             isa<CmpInst>(U->getOperand(1)) ||
             isa<ShuffleVectorInst>(U->getOperand(1)) ||
@@ -1603,12 +1631,13 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
         } else {
           s2 = U->getOperand(1);
         }
+      }
       
       // Ternary Operators
       Value* s3 = 0;
       if (isa<ShuffleVectorInst>(U) ||
           isa<InsertElementInst>(U) ||
-          isa<SelectInst>(U))
+          isa<SelectInst>(U)) {
         if (isa<BinaryOperator>(U->getOperand(2)) || 
             isa<CmpInst>(U->getOperand(2)) ||
             isa<ShuffleVectorInst>(U->getOperand(2)) ||
@@ -1621,6 +1650,7 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
         } else {
           s3 = U->getOperand(2);
         }
+      }
       
       // Vararg operators
       SmallVector<Value*, 4> sVarargs;
@@ -1645,35 +1675,35 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
       
       Value* newVal = 0;
       if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
-        newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
+        newVal = BinaryOperator::Create(BO->getOpcode(), s1, s2,
                                         BO->getName()+".gvnpre",
                                         (*PI)->getTerminator());
       else if (CmpInst* C = dyn_cast<CmpInst>(U))
-        newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
+        newVal = CmpInst::Create(C->getOpcode(),
+                                 C->getPredicate(), s1, s2,
                                  C->getName()+".gvnpre", 
                                  (*PI)->getTerminator());
       else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
         newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
                                        (*PI)->getTerminator());
       else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
-        newVal = new InsertElementInst(s1, s2, s3, S->getName()+".gvnpre",
-                                       (*PI)->getTerminator());
+        newVal = InsertElementInst::Create(s1, s2, s3, S->getName()+".gvnpre",
+                                           (*PI)->getTerminator());
       else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
-        newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
+        newVal = ExtractElementInst::Create(s1, s2, S->getName()+".gvnpre",
                                         (*PI)->getTerminator());
       else if (SelectInst* S = dyn_cast<SelectInst>(U))
-        newVal = new SelectInst(s1, s2, s3, S->getName()+".gvnpre",
-                                (*PI)->getTerminator());
+        newVal = SelectInst::Create(s1, s2, s3, S->getName()+".gvnpre",
+                                    (*PI)->getTerminator());
       else if (CastInst* C = dyn_cast<CastInst>(U))
-        newVal = CastInst::create(C->getOpcode(), s1, C->getType(),
+        newVal = CastInst::Create(C->getOpcode(), s1, C->getType(),
                                   C->getName()+".gvnpre", 
                                   (*PI)->getTerminator());
       else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U))
-        newVal = new GetElementPtrInst(s1, &sVarargs[0], sVarargs.size(), 
-                                       G->getName()+".gvnpre", 
-                                       (*PI)->getTerminator());
-                                
-                  
+        newVal = GetElementPtrInst::Create(s1, sVarargs.begin(), sVarargs.end(),
+                                           G->getName()+".gvnpre", 
+                                           (*PI)->getTerminator());
+
       VN.add(newVal, VN.lookup(U));
                   
       ValueNumberedSet& predAvail = availableOut[*PI];
@@ -1694,7 +1724,7 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
               
   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
     if (p == 0)
-      p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
+      p = PHINode::Create(avail[*PI]->getType(), "gvnpre-join", BB->begin());
     
     p->addIncoming(avail[*PI], *PI);
   }