Fix some bugs, straighten stuff out, more work needs to be done.
[oota-llvm.git] / lib / Transforms / Scalar / GCSE.cpp
index b864760e688f83acedb465b5ee4bc554aca118b7..79fed781f93abb38015065fb204d87ee17d7886b 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/Scalar/GCSE.h"
-#include "llvm/Pass.h"
+#include "llvm/Transforms/Scalar.h"
 #include "llvm/InstrTypes.h"
 #include "llvm/iMemory.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Support/InstVisitor.h"
 #include "llvm/Support/InstIterator.h"
-#include <set>
 #include <algorithm>
 
 namespace {
@@ -30,6 +28,10 @@ namespace {
     DominatorSet        *DomSetInfo;
     ImmediateDominators *ImmDominator;
   public:
+    const char *getPassName() const {
+      return "Global Common Subexpression Elimination";
+    }
+
     virtual bool runOnFunction(Function *F);
 
     // Visitation methods, these are invoked depending on the type of
@@ -101,6 +103,8 @@ bool GCSE::runOnFunction(Function *F) {
 //
 void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
   Instruction *Second = *SI;
+  
+  //cerr << "DEL " << (void*)Second << Second;
 
   // Add the first instruction back to the worklist
   WorkList.insert(First);
@@ -123,9 +127,9 @@ void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
 //
 void GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
   // I has already been removed from the worklist, Other needs to be.
-  assert(WorkList.count(I) == 0 && WorkList.count(Other) &&
-         "I in worklist or Other not!");
-  WorkList.erase(Other);
+  assert(I != Other && WorkList.count(I) == 0 && "I shouldn't be on worklist!");
+
+  WorkList.erase(Other); // Other may not actually be on the worklist anymore...
 
   // Handle the easy case, where both instructions are in the same basic block
   BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();