*FINALLY* Fix a really nasty nondeterministic bug that has been haunting us
[oota-llvm.git] / lib / Analysis / ValueNumbering.cpp
index d472651b3ea173e2f0730b3bb0325c7ec72e3008..49b75bd6acc05b40f6ea6871ba1cb0883de01054 100644 (file)
 #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"
-
-namespace llvm {
+using namespace llvm;
 
 // Register the ValueNumbering interface, providing a nice name to refer to.
 static RegisterAnalysisGroup<ValueNumbering> X("Value Numbering");
@@ -104,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?
+    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);
       }
@@ -180,6 +176,14 @@ static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
 
 void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
   Value *Op = I.getOperand(0);
+
+  // 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)
@@ -191,6 +195,4 @@ void BVNImpl::visitGetElementPtrInst(GetElementPtrInst &I) {
       }
 }
 
-void BasicValueNumberingStub() { }
-
-} // End llvm namespace
+void llvm::BasicValueNumberingStub() { }