Remove this file; the code that it went with is no longer
[oota-llvm.git] / lib / Analysis / ValueNumbering.cpp
index 0050e06091cbd6fd9c863e7e143311f3d7fbe6bb..55323eaa9ed1be7b86aceb5246aa5d4d92756b21 100644 (file)
@@ -2,14 +2,18 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group 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.
 //
 //===----------------------------------------------------------------------===//
 //
 // This file implements the non-abstract Value Numbering methods as well as a
 // default implementation for the analysis group.
 //
+// The ValueNumbering analysis pass is mostly deprecated. It is only used by the
+// Global Common Subexpression Elimination pass, which is deprecated by the
+// Global Value Numbering pass (which does its value numbering on its own).
+//
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/Passes.h"
 #include "llvm/Instructions.h"
 #include "llvm/Pass.h"
 #include "llvm/Type.h"
+#include "llvm/Support/Compiler.h"
 using namespace llvm;
 
+char ValueNumbering::ID = 0;
 // Register the ValueNumbering interface, providing a nice name to refer to.
-static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
+static RegisterAnalysisGroup<ValueNumbering> V("Value Numbering");
 
 /// ValueNumbering destructor: DO NOT move this to the header file for
 /// ValueNumbering or else clients of the ValueNumbering class may not depend on
@@ -48,7 +54,11 @@ namespace {
   /// lexically identical expressions.  This does not require any ahead of time
   /// analysis, so it is a very fast default implementation.
   ///
-  struct BasicVN : public ImmutablePass, public ValueNumbering {
+  struct VISIBILITY_HIDDEN BasicVN 
+      : public ImmutablePass, public ValueNumbering {
+    static char ID; // Class identification, replacement for typeinfo
+    BasicVN() : ImmutablePass((intptr_t)&ID) {}
+
     /// getEqualNumberNodes - Return nodes with the same value number as the
     /// specified Value.  This fills in the argument vector with any equal
     /// values.
@@ -58,29 +68,37 @@ namespace {
     virtual void getEqualNumberNodes(Value *V1,
                                      std::vector<Value*> &RetVals) const;
   };
+}
 
-  // Register this pass...
-  RegisterOpt<BasicVN>
-  X("basicvn", "Basic Value Numbering (default GVN impl)");
+char BasicVN::ID = 0;
+// Register this pass...
+static RegisterPass<BasicVN>
+X("basicvn", "Basic Value Numbering (default GVN impl)", false, true);
 
-  // Declare that we implement the ValueNumbering interface
-  RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
+// Declare that we implement the ValueNumbering interface
+static RegisterAnalysisGroup<ValueNumbering, true> Y(X);
 
+namespace {
   /// BVNImpl - Implement BasicVN in terms of a visitor class that
   /// handles the different types of instructions as appropriate.
   ///
-  struct BVNImpl : public InstVisitor<BVNImpl> {
+  struct VISIBILITY_HIDDEN BVNImpl : public InstVisitor<BVNImpl> {
     std::vector<Value*> &RetVals;
-    BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
+    explicit BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
 
-    void handleBinaryInst(Instruction &I);
-    void visitBinaryOperator(BinaryOperator &I) {
-      handleBinaryInst((Instruction&)I);
-    }
-    void visitGetElementPtrInst(GetElementPtrInst &I);
     void visitCastInst(CastInst &I);
-    void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
-    void visitSelectInst(SelectInst &I);
+    void visitGetElementPtrInst(GetElementPtrInst &I);
+    void visitCmpInst(CmpInst &I);
+
+    void handleBinaryInst(Instruction &I);
+    void visitBinaryOperator(Instruction &I)     { handleBinaryInst(I); }
+    void visitShiftInst(Instruction &I)          { handleBinaryInst(I); }
+    void visitExtractElementInst(Instruction &I) { handleBinaryInst(I); }
+
+    void handleTernaryInst(Instruction &I);
+    void visitSelectInst(Instruction &I)         { handleTernaryInst(I); }
+    void visitInsertElementInst(Instruction &I)  { handleTernaryInst(I); }
+    void visitShuffleVectorInst(Instruction &I)  { handleTernaryInst(I); }
     void visitInstruction(Instruction &) {
       // Cannot value number calls or terminator instructions.
     }
@@ -108,8 +126,10 @@ void BVNImpl::visitCastInst(CastInst &CI) {
   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
        UI != UE; ++UI)
     if (CastInst *Other = dyn_cast<CastInst>(*UI))
-      // Check that the types are the same, since this code handles casts...
-      if (Other->getType() == I.getType() &&
+      // Check that the opcode is the same
+      if (Other->getOpcode() == Instruction::CastOps(I.getOpcode()) &&
+          // Check that the destination types are the same
+          Other->getType() == I.getType() &&
           // Is it embedded in the same function?  (This could be false if LHS
           // is a constant or global!)
           Other->getParent()->getParent() == F &&
@@ -120,6 +140,28 @@ void BVNImpl::visitCastInst(CastInst &CI) {
       }
 }
 
+void  BVNImpl::visitCmpInst(CmpInst &CI1) {
+  Value *LHS = CI1.getOperand(0);
+  for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
+       UI != UE; ++UI)
+    if (CmpInst *CI2 = dyn_cast<CmpInst>(*UI))
+      // Check to see if this compare instruction is not CI, but same opcode,
+      // same predicate, and in the same function.
+      if (CI2 != &CI1 && CI2->getOpcode() == CI1.getOpcode() &&
+          CI2->getPredicate() == CI1.getPredicate() &&
+          CI2->getParent()->getParent() == CI1.getParent()->getParent())
+        // If the operands are the same
+        if ((CI2->getOperand(0) == CI1.getOperand(0) &&
+            CI2->getOperand(1) == CI1.getOperand(1)) ||
+            // Or the compare is commutative and the operands are reversed 
+            (CI1.isCommutative() && 
+             CI2->getOperand(0) == CI1.getOperand(1) &&
+             CI2->getOperand(1) == CI1.getOperand(0)))
+          // Then the instructiosn are identical, add to list.
+          RetVals.push_back(CI2);
+}
+
+
 
 // isIdenticalBinaryInst - Return true if the two binary instructions are
 // identical.
@@ -132,6 +174,11 @@ static inline bool isIdenticalBinaryInst(const Instruction &I1,
       I1.getParent()->getParent() != I2->getParent()->getParent())
     return false;
 
+  // If they are CmpInst instructions, check their predicates
+  if (CmpInst *CI1 = dyn_cast<CmpInst>(&const_cast<Instruction&>(I1)))
+    if (CI1->getPredicate() != cast<CmpInst>(I2)->getPredicate())
+      return false;
+
   // They are identical if both operands are the same!
   if (I1.getOperand(0) == I2->getOperand(0) &&
       I1.getOperand(1) == I2->getOperand(1))
@@ -148,6 +195,24 @@ static inline bool isIdenticalBinaryInst(const Instruction &I1,
   return false;
 }
 
+// isIdenticalTernaryInst - Return true if the two ternary instructions are
+// identical.
+//
+static inline bool isIdenticalTernaryInst(const Instruction &I1,
+                                          const Instruction *I2) {
+  // Is it embedded in the same function?  (This could be false if LHS
+  // is a constant or global!)
+  if (I1.getParent()->getParent() != I2->getParent()->getParent())
+    return false;
+  
+  // They are identical if all operands are the same!
+  return I1.getOperand(0) == I2->getOperand(0) &&
+         I1.getOperand(1) == I2->getOperand(1) &&
+         I1.getOperand(2) == I2->getOperand(2);
+}
+
+
+
 void BVNImpl::handleBinaryInst(Instruction &I) {
   Value *LHS = I.getOperand(0);
 
@@ -183,9 +248,9 @@ void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
 
   // Try to pick a local operand if possible instead of a constant or a global
   // that might have a lot of uses.
-  for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
-    if (isa<Instruction>(I.getOperand(i)) || isa<Argument>(I.getOperand(i))) {
-      Op = I.getOperand(i);
+  for (User::op_iterator i = I.op_begin() + 1, e = I.op_end(); i != e; ++i)
+    if (isa<Instruction>(*i) || isa<Argument>(*i)) {
+      Op = *i;
       break;
     }
 
@@ -199,38 +264,23 @@ void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
       }
 }
 
-// isIdenticalSelectInst - Return true if the two select instructions are
-// identical.
-//
-static inline bool isIdenticalSelectInst(const SelectInst &I1,
-                                         const SelectInst *I2) {
-  // Is it embedded in the same function?  (This could be false if LHS
-  // is a constant or global!)
-  if (I1.getParent()->getParent() != I2->getParent()->getParent())
-    return false;
-  
-  // They are identical if both operands are the same!
-  return I1.getOperand(0) == I2->getOperand(0) &&
-         I1.getOperand(1) == I2->getOperand(1) &&
-         I1.getOperand(2) == I2->getOperand(2);
-    return true;
-  
-  return false;
-}
-
-void BVNImpl::visitSelectInst(SelectInst &I) {
-  Value *Cond = I.getOperand(0);
+void BVNImpl::handleTernaryInst(Instruction &I) {
+  Value *Op0 = I.getOperand(0);
+  Instruction *OtherInst;
   
-  for (Value::use_iterator UI = Cond->use_begin(), UE = Cond->use_end();
+  for (Value::use_iterator UI = Op0->use_begin(), UE = Op0->use_end();
        UI != UE; ++UI)
-    if (SelectInst *Other = dyn_cast<SelectInst>(*UI))
+    if ((OtherInst = dyn_cast<Instruction>(*UI)) && 
+        OtherInst->getOpcode() == I.getOpcode()) {
       // Check to see if this new select is not I, but has the same operands.
-      if (Other != &I && isIdenticalSelectInst(I, Other)) {
+      if (OtherInst != &I && isIdenticalTernaryInst(I, OtherInst)) {
         // These instructions are identical.  Handle the situation.
-        RetVals.push_back(Other);
+        RetVals.push_back(OtherInst);
       }
         
+    }
 }
 
 
-void llvm::BasicValueNumberingStub() { }
+// Ensure that users of ValueNumbering.h will link with this file
+DEFINING_FILE_FOR(BasicValueNumbering)