Silence a spurious warning
[oota-llvm.git] / lib / Analysis / ValueNumbering.cpp
index 58b045e3fb2728caded5bca2b83c183cc6d5e068..49b75bd6acc05b40f6ea6871ba1cb0883de01054 100644 (file)
@@ -1,16 +1,24 @@
 //===- ValueNumbering.cpp - Value #'ing Implementation ----------*- C++ -*-===//
+// 
+//                     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 implements the non-abstract Value Numbering methods as well as a
 // default implementation for the analysis group.
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/BasicValueNumbering.h"
+#include "llvm/Analysis/ValueNumbering.h"
 #include "llvm/Support/InstVisitor.h"
 #include "llvm/BasicBlock.h"
+#include "llvm/Instructions.h"
+#include "llvm/Pass.h"
 #include "llvm/Type.h"
-#include "llvm/iMemory.h"
-#include "llvm/InstrTypes.h"
+using namespace llvm;
 
 // Register the ValueNumbering interface, providing a nice name to refer to.
 static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
@@ -23,36 +31,54 @@ static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
 ValueNumbering::~ValueNumbering() {}
 
 //===----------------------------------------------------------------------===//
-// BasicValueNumbering Pass Implementation
+// Basic ValueNumbering Pass Implementation
 //===----------------------------------------------------------------------===//
 //
-// Because of the way .a files work, the implementation of the
-// BasicValueNumbering class MUST be in the ValueNumbering file itself, or else
-// we run the risk of ValueNumbering being used, but the default implementation
-// not being linked into the tool that uses it.  As such, we register and
-// implement the class here.
+// Because of the way .a files work, the implementation of the BasicVN class
+// MUST be in the ValueNumbering file itself, or else we run the risk of
+// ValueNumbering being used, but the default implementation not being linked
+// into the tool that uses it.  As such, we register and implement the class
+// here.
 //
+
 namespace {
+  /// BasicVN - This class is the default implementation of the ValueNumbering
+  /// interface.  It walks the SSA def-use chains to trivially identify
+  /// 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 {
+    /// getEqualNumberNodes - Return nodes with the same value number as the
+    /// specified Value.  This fills in the argument vector with any equal
+    /// values.
+    ///
+    /// This is where our implementation is.
+    ///
+    virtual void getEqualNumberNodes(Value *V1,
+                                     std::vector<Value*> &RetVals) const;
+  };
+
   // Register this pass...
-  RegisterOpt<BasicValueNumbering>
+  RegisterOpt<BasicVN>
   X("basicvn", "Basic Value Numbering (default GVN impl)");
 
   // Declare that we implement the ValueNumbering interface
-  RegisterAnalysisGroup<ValueNumbering, BasicValueNumbering, true> Y;
-}  // End of anonymous namespace
+  RegisterAnalysisGroup<ValueNumbering, BasicVN, true> Y;
 
-namespace {
-  /// BVNImpl - Implement BasicValueNumbering in terms of a visitor class that
+  /// BVNImpl - Implement BasicVN in terms of a visitor class that
   /// handles the different types of instructions as appropriate.
   ///
   struct BVNImpl : public InstVisitor<BVNImpl> {
     std::vector<Value*> &RetVals;
     BVNImpl(std::vector<Value*> &RV) : RetVals(RV) {}
 
-    void visitBinaryOperator(Instruction &I);
+    void handleBinaryInst(Instruction &I);
+    void visitBinaryOperator(BinaryOperator &I) {
+      handleBinaryInst((Instruction&)I);
+    }
     void visitGetElementPtrInst(GetElementPtrInst &I);
     void visitCastInst(CastInst &I);
-    void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
+    void visitShiftInst(ShiftInst &I) { handleBinaryInst((Instruction&)I); }
     void visitInstruction(Instruction &) {
       // Cannot value number calls or terminator instructions...
     }
@@ -62,8 +88,7 @@ namespace {
 // getEqualNumberNodes - Return nodes with the same value number as the
 // specified Value.  This fills in the argument vector with any equal values.
 //
-void BasicValueNumbering::getEqualNumberNodes(Value *V,
-                                           std::vector<Value*> &RetVals) const {
+void BasicVN::getEqualNumberNodes(Value *V, std::vector<Value*> &RetVals) const{
   assert(V->getType() != Type::VoidTy &&
          "Can only value number non-void values!");
   // We can only handle the case where I is an instruction!
@@ -78,17 +103,14 @@ void BVNImpl::visitCastInst(CastInst &CI) {
   
   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 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
+    if (CastInst *Other = dyn_cast<CastInst>(*UI))
+      // Check that the types are the same, since this code handles casts...
+      if (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 &&
-
-          // Check that the types are the same, since this code handles casts...
-          Other->getType() == I.getType()) {
-        
+          // Check to see if this new cast is not I.
+          Other != &I) {
         // These instructions are identical.  Add to list...
         RetVals.push_back(Other);
       }
@@ -100,7 +122,7 @@ void BVNImpl::visitCastInst(CastInst &CI) {
 //
 static inline bool isIdenticalBinaryInst(const Instruction &I1,
                                          const Instruction *I2) {
-  // Is it embeded in the same function?  (This could be false if LHS
+  // Is it embedded in the same function?  (This could be false if LHS
   // is a constant or global!)
   if (I1.getOpcode() != I2->getOpcode() ||
       I1.getParent()->getParent() != I2->getParent()->getParent())
@@ -111,22 +133,18 @@ static inline bool isIdenticalBinaryInst(const Instruction &I1,
       I1.getOperand(1) == I2->getOperand(1))
     return true;
   
-  // If the instruction is commutative and associative, the instruction can
-  // match if the operands are swapped!
+  // If the instruction is commutative, the instruction can match if the
+  // operands are swapped!
   //
   if ((I1.getOperand(0) == I2->getOperand(1) &&
        I1.getOperand(1) == I2->getOperand(0)) &&
-      (I1.getOpcode() == Instruction::Add || 
-       I1.getOpcode() == Instruction::Mul ||
-       I1.getOpcode() == Instruction::And || 
-       I1.getOpcode() == Instruction::Or  ||
-       I1.getOpcode() == Instruction::Xor))
+      I1.isCommutative())
     return true;
 
   return false;
 }
 
-void BVNImpl::visitBinaryOperator(Instruction &I) {
+void BVNImpl::handleBinaryInst(Instruction &I) {
   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
   Function *F = I.getParent()->getParent();
   
@@ -158,7 +176,14 @@ static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
 
 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
   Value *Op = I.getOperand(0);
-  Function *F = I.getParent()->getParent();
+
+  // 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);
+      break;
+    }
   
   for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
        UI != UE; ++UI)
@@ -169,3 +194,5 @@ void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
         RetVals.push_back(Other);
       }
 }
+
+void llvm::BasicValueNumberingStub() { }