Allow GVN to eliminate redundant calls to functions without side effects.
[oota-llvm.git] / lib / Transforms / Scalar / GVN.cpp
index 555a9f352cc2dd2be9f0fc74bda6bc1e09724a90..7cbd617a2ebfca45ead3d1199542ca1fa82e885d 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #define DEBUG_TYPE "gvn"
-#include "llvm/Value.h"
+
 #include "llvm/Transforms/Scalar.h"
-#include "llvm/Instructions.h"
-#include "llvm/Function.h"
+#include "llvm/BasicBlock.h"
+#include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/Analysis/Dominators.h"
+#include "llvm/Function.h"
+#include "llvm/Instructions.h"
+#include "llvm/Value.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/Dominators.h"
+#include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/Compiler.h"
@@ -48,7 +52,7 @@ namespace {
                             FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
                             SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
                             FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT, 
-                            PTRTOINT, INTTOPTR, BITCAST, GEP, EMPTY,
+                            PTRTOINT, INTTOPTR, BITCAST, GEP, CALL, EMPTY,
                             TOMBSTONE };
 
     ExpressionOpcode opcode;
@@ -57,6 +61,7 @@ namespace {
     uint32_t secondVN;
     uint32_t thirdVN;
     SmallVector<uint32_t, 4> varargs;
+    Value* function;
   
     Expression() { }
     Expression(ExpressionOpcode o) : opcode(o) { }
@@ -68,6 +73,8 @@ namespace {
         return true;
       else if (type != other.type)
         return false;
+      else if (function != other.function)
+        return false;
       else if (firstVN != other.firstVN)
         return false;
       else if (secondVN != other.secondVN)
@@ -93,6 +100,8 @@ namespace {
         return false;
       else if (type != other.type)
         return true;
+      else if (function != other.function)
+        return true;
       else if (firstVN != other.firstVN)
         return true;
       else if (secondVN != other.secondVN)
@@ -116,6 +125,7 @@ namespace {
     private:
       DenseMap<Value*, uint32_t> valueNumbering;
       DenseMap<Expression, uint32_t> expressionNumbering;
+      AliasAnalysis* AA;
   
       uint32_t nextValueNumber;
     
@@ -130,21 +140,28 @@ namespace {
       Expression create_expression(SelectInst* V);
       Expression create_expression(CastInst* C);
       Expression create_expression(GetElementPtrInst* G);
+      Expression create_expression(CallInst* C);
     public:
-      ValueTable() { nextValueNumber = 1; }
+      ValueTable() : nextValueNumber(1) { }
       uint32_t lookup_or_add(Value* V);
       uint32_t lookup(Value* V) const;
       void add(Value* V, uint32_t num);
       void clear();
       void erase(Value* v);
       unsigned size();
+      void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
   };
 }
 
 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;
@@ -157,12 +174,19 @@ template <> struct DenseMapKeyInfo<Expression> {
             (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;
     
+    hash = (unsigned)((uintptr_t)e.function >> 4) ^
+            (unsigned)((uintptr_t)e.function >> 9) +
+            hash * 37;
+    
     return hash;
   }
+  static bool isEqual(const Expression &LHS, const Expression &RHS) {
+    return LHS == RHS;
+  }
   static bool isPod() { return true; }
 };
 }
@@ -314,12 +338,30 @@ Expression::ExpressionOpcode
   }
 }
 
+Expression ValueTable::create_expression(CallInst* C) {
+  Expression e;
+  
+  e.type = C->getType();
+  e.firstVN = 0;
+  e.secondVN = 0;
+  e.thirdVN = 0;
+  e.function = C->getCalledFunction();
+  e.opcode = Expression::CALL;
+  
+  for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
+       I != E; ++I)
+    e.varargs.push_back(lookup_or_add(*I));
+  
+  return e;
+}
+
 Expression ValueTable::create_expression(BinaryOperator* BO) {
   Expression e;
     
   e.firstVN = lookup_or_add(BO->getOperand(0));
   e.secondVN = lookup_or_add(BO->getOperand(1));
   e.thirdVN = 0;
+  e.function = 0;
   e.type = BO->getType();
   e.opcode = getOpcode(BO);
   
@@ -332,6 +374,7 @@ Expression ValueTable::create_expression(CmpInst* C) {
   e.firstVN = lookup_or_add(C->getOperand(0));
   e.secondVN = lookup_or_add(C->getOperand(1));
   e.thirdVN = 0;
+  e.function = 0;
   e.type = C->getType();
   e.opcode = getOpcode(C);
   
@@ -344,6 +387,7 @@ Expression ValueTable::create_expression(CastInst* C) {
   e.firstVN = lookup_or_add(C->getOperand(0));
   e.secondVN = 0;
   e.thirdVN = 0;
+  e.function = 0;
   e.type = C->getType();
   e.opcode = getOpcode(C);
   
@@ -356,6 +400,7 @@ Expression ValueTable::create_expression(ShuffleVectorInst* S) {
   e.firstVN = lookup_or_add(S->getOperand(0));
   e.secondVN = lookup_or_add(S->getOperand(1));
   e.thirdVN = lookup_or_add(S->getOperand(2));
+  e.function = 0;
   e.type = S->getType();
   e.opcode = Expression::SHUFFLE;
   
@@ -368,6 +413,7 @@ Expression ValueTable::create_expression(ExtractElementInst* E) {
   e.firstVN = lookup_or_add(E->getOperand(0));
   e.secondVN = lookup_or_add(E->getOperand(1));
   e.thirdVN = 0;
+  e.function = 0;
   e.type = E->getType();
   e.opcode = Expression::EXTRACT;
   
@@ -380,6 +426,7 @@ Expression ValueTable::create_expression(InsertElementInst* I) {
   e.firstVN = lookup_or_add(I->getOperand(0));
   e.secondVN = lookup_or_add(I->getOperand(1));
   e.thirdVN = lookup_or_add(I->getOperand(2));
+  e.function = 0;
   e.type = I->getType();
   e.opcode = Expression::INSERT;
   
@@ -392,6 +439,7 @@ Expression ValueTable::create_expression(SelectInst* I) {
   e.firstVN = lookup_or_add(I->getCondition());
   e.secondVN = lookup_or_add(I->getTrueValue());
   e.thirdVN = lookup_or_add(I->getFalseValue());
+  e.function = 0;
   e.type = I->getType();
   e.opcode = Expression::SELECT;
   
@@ -404,6 +452,7 @@ Expression ValueTable::create_expression(GetElementPtrInst* G) {
   e.firstVN = lookup_or_add(G->getPointerOperand());
   e.secondVN = 0;
   e.thirdVN = 0;
+  e.function = 0;
   e.type = G->getType();
   e.opcode = Expression::GEP;
   
@@ -425,8 +474,26 @@ uint32_t ValueTable::lookup_or_add(Value* V) {
   if (VI != valueNumbering.end())
     return VI->second;
   
-  
-  if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
+  if (CallInst* C = dyn_cast<CallInst>(V)) {
+    if (C->getCalledFunction() &&
+        AA->doesNotAccessMemory(C->getCalledFunction())) {
+      Expression e = create_expression(C);
+    
+      DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
+      if (EI != expressionNumbering.end()) {
+        valueNumbering.insert(std::make_pair(V, EI->second));
+        return EI->second;
+      } else {
+        expressionNumbering.insert(std::make_pair(e, nextValueNumber));
+        valueNumbering.insert(std::make_pair(V, nextValueNumber));
+      
+        return nextValueNumber++;
+      }
+    } else {
+      valueNumbering.insert(std::make_pair(V, nextValueNumber));
+      return nextValueNumber++;
+    }
+  } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
     Expression e = create_expression(BO);
     
     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
@@ -555,6 +622,11 @@ void ValueTable::clear() {
   nextValueNumber = 1;
 }
 
+/// erase - Remove a value from the value numbering
+void ValueTable::erase(Value* V) {
+  valueNumbering.erase(V);
+}
+
 //===----------------------------------------------------------------------===//
 //                       ValueNumberedSet Class
 //===----------------------------------------------------------------------===//
@@ -629,11 +701,17 @@ namespace {
     
     DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
     
+    typedef DenseMap<Value*, SmallPtrSet<Instruction*, 4> > PhiMapType;
+    PhiMapType phiMap;
+    
+    
     // This transformation requires dominator postdominator info
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.setPreservesCFG();
       AU.addRequired<DominatorTree>();
       AU.addRequired<MemoryDependenceAnalysis>();
+      AU.addRequired<AliasAnalysis>();
+      AU.addPreserved<AliasAnalysis>();
       AU.addPreserved<MemoryDependenceAnalysis>();
     }
   
@@ -648,6 +726,15 @@ namespace {
                             ValueNumberedSet& currAvail,
                             DenseMap<Value*, LoadInst*>& lastSeenLoad,
                             SmallVector<Instruction*, 4>& toErase);
+    bool processNonLocalLoad(LoadInst* L,
+                             SmallVector<Instruction*, 4>& toErase);
+    Value *GetValueForBlock(BasicBlock *BB, LoadInst* orig,
+                            DenseMap<BasicBlock*, Value*> &Phis,
+                            bool top_level = false);
+    void dump(DenseMap<BasicBlock*, Value*>& d);
+    bool iterateOnFunction(Function &F);
+    Value* CollapsePhi(PHINode* p);
+    bool isSafeReplacement(PHINode* p, Instruction* inst);
   };
   
   char GVN::ID = 0;
@@ -687,6 +774,168 @@ void GVN::val_insert(ValueNumberedSet& s, Value* v) {
     s.insert(v);
 }
 
+void GVN::dump(DenseMap<BasicBlock*, Value*>& d) {
+  printf("{\n");
+  for (DenseMap<BasicBlock*, Value*>::iterator I = d.begin(),
+       E = d.end(); I != E; ++I) {
+    if (I->second == MemoryDependenceAnalysis::None)
+      printf("None\n");
+    else
+      I->second->dump();
+  }
+  printf("}\n");
+}
+
+Value* GVN::CollapsePhi(PHINode* p) {
+  DominatorTree &DT = getAnalysis<DominatorTree>();
+  Value* constVal = p->hasConstantValue();
+  
+  if (constVal) {
+    if (Instruction* inst = dyn_cast<Instruction>(constVal)) {
+      if (DT.dominates(inst, p))
+        if (isSafeReplacement(p, inst))
+          return inst;
+    } else {
+      return constVal;
+    }
+  }
+  
+  return 0;
+}
+
+bool GVN::isSafeReplacement(PHINode* p, Instruction* inst) {
+  if (!isa<PHINode>(inst))
+    return true;
+  
+  for (Instruction::use_iterator UI = p->use_begin(), E = p->use_end();
+       UI != E; ++UI)
+    if (PHINode* use_phi = dyn_cast<PHINode>(UI))
+      if (use_phi->getParent() == inst->getParent())
+        return false;
+  
+  return true;
+}
+
+/// GetValueForBlock - Get the value to use within the specified basic block.
+/// available values are in Phis.
+Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig,
+                               DenseMap<BasicBlock*, Value*> &Phis,
+                               bool top_level) { 
+                                 
+  // If we have already computed this value, return the previously computed val.
+  DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
+  if (V != Phis.end() && !top_level) return V->second;
+  
+  BasicBlock* singlePred = BB->getSinglePredecessor();
+  if (singlePred) {
+    Value *ret = GetValueForBlock(singlePred, orig, Phis);
+    Phis[BB] = ret;
+    return ret;
+  }
+  // Otherwise, the idom is the loop, so we need to insert a PHI node.  Do so
+  // now, then get values to fill in the incoming values for the PHI.
+  PHINode *PN = new PHINode(orig->getType(), orig->getName()+".rle",
+                            BB->begin());
+  PN->reserveOperandSpace(std::distance(pred_begin(BB), pred_end(BB)));
+  
+  if (Phis.count(BB) == 0)
+    Phis.insert(std::make_pair(BB, PN));
+  
+  // Fill in the incoming values for the block.
+  for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
+    Value* val = GetValueForBlock(*PI, orig, Phis);
+    
+    PN->addIncoming(val, *PI);
+  }
+  
+  // Attempt to collapse PHI nodes that are trivially redundant
+  Value* v = CollapsePhi(PN);
+  if (v) {
+    MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
+
+    MD.removeInstruction(PN);
+    PN->replaceAllUsesWith(v);
+
+    for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
+         E = Phis.end(); I != E; ++I)
+      if (I->second == PN)
+        I->second = v;
+
+    PN->eraseFromParent();
+
+    Phis[BB] = v;
+
+    return v;
+  }
+
+  // Cache our phi construction results
+  phiMap[orig->getPointerOperand()].insert(PN);
+  return PN;
+}
+
+/// processNonLocalLoad - Attempt to eliminate a load whose dependencies are
+/// non-local by performing PHI construction.
+bool GVN::processNonLocalLoad(LoadInst* L,
+                              SmallVector<Instruction*, 4>& toErase) {
+  MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
+  
+  // Find the non-local dependencies of the load
+  DenseMap<BasicBlock*, Value*> deps;
+  MD.getNonLocalDependency(L, deps);
+  
+  DenseMap<BasicBlock*, Value*> repl;
+  
+  // Filter out useless results (non-locals, etc)
+  for (DenseMap<BasicBlock*, Value*>::iterator I = deps.begin(), E = deps.end();
+       I != E; ++I)
+    if (I->second == MemoryDependenceAnalysis::None) {
+      return false;
+    } else if (I->second == MemoryDependenceAnalysis::NonLocal) {
+      continue;
+    } else if (StoreInst* S = dyn_cast<StoreInst>(I->second)) {
+      if (S->getPointerOperand() == L->getPointerOperand())
+        repl[I->first] = S->getOperand(0);
+      else
+        return false;
+    } else if (LoadInst* LD = dyn_cast<LoadInst>(I->second)) {
+      if (LD->getPointerOperand() == L->getPointerOperand())
+        repl[I->first] = LD;
+      else
+        return false;
+    } else {
+      return false;
+    }
+  
+  // Use cached PHI construction information from previous runs
+  SmallPtrSet<Instruction*, 4>& p = phiMap[L->getPointerOperand()];
+  for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
+       I != E; ++I) {
+    if ((*I)->getParent() == L->getParent()) {
+      MD.removeInstruction(L);
+      L->replaceAllUsesWith(*I);
+      toErase.push_back(L);
+      NumGVNLoad++;
+      
+      return true;
+    } else {
+      repl.insert(std::make_pair((*I)->getParent(), *I));
+    }
+  }
+  
+  // Perform PHI construction
+  SmallPtrSet<BasicBlock*, 4> visited;
+  Value* v = GetValueForBlock(L->getParent(), L, repl, true);
+  
+  MD.removeInstruction(L);
+  L->replaceAllUsesWith(v);
+  toErase.push_back(L);
+  NumGVNLoad++;
+
+  return true;
+}
+
+/// processLoad - Attempt to eliminate a load, first by eliminating it
+/// locally, and then attempting non-local elimination if that fails.
 bool GVN::processLoad(LoadInst* L,
                          DenseMap<Value*, LoadInst*>& lastLoad,
                          SmallVector<Instruction*, 4>& toErase) {
@@ -700,9 +949,23 @@ bool GVN::processLoad(LoadInst* L,
   
   // ... to a pointer that has been loaded from before...
   MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
+  bool removedNonLocal = false;
   Instruction* dep = MD.getDependency(L);
+  if (dep == MemoryDependenceAnalysis::NonLocal &&
+      L->getParent() != &L->getParent()->getParent()->getEntryBlock()) {
+    removedNonLocal = processNonLocalLoad(L, toErase);
+    
+    if (!removedNonLocal)
+      last = L;
+    
+    return removedNonLocal;
+  }
+  
+  
   bool deletedLoad = false;
   
+  // Walk up the dependency chain until we either find
+  // a dependency we can use, or we can't walk any further
   while (dep != MemoryDependenceAnalysis::None &&
          dep != MemoryDependenceAnalysis::NonLocal &&
          (isa<LoadInst>(dep) || isa<StoreInst>(dep))) {
@@ -746,7 +1009,7 @@ bool GVN::processLoad(LoadInst* L,
   return deletedLoad;
 }
 
-/// buildsets_availout - When calculating availability, handle an instruction
+/// processInstruction - When calculating availability, handle an instruction
 /// by inserting it into the appropriate sets
 bool GVN::processInstruction(Instruction* I,
                                 ValueNumberedSet& currAvail,
@@ -758,9 +1021,24 @@ bool GVN::processInstruction(Instruction* I,
   
   unsigned num = VN.lookup_or_add(I);
   
-  if (currAvail.test(num)) {
+  // Collapse PHI nodes
+  if (PHINode* p = dyn_cast<PHINode>(I)) {
+    Value* constVal = CollapsePhi(p);
+    
+    if (constVal) {
+      for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
+           PI != PE; ++PI)
+        if (PI->second.count(p))
+          PI->second.erase(p);
+        
+      p->replaceAllUsesWith(constVal);
+      toErase.push_back(p);
+    }
+  // Perform value-number based elimination
+  } else if (currAvail.test(num)) {
     Value* repl = find_leader(currAvail, num);
     
+    VN.erase(I);
     I->replaceAllUsesWith(repl);
     toErase.push_back(I);
     return true;
@@ -775,10 +1053,27 @@ bool GVN::processInstruction(Instruction* I,
 // GVN::runOnFunction - This is the main transformation entry point for a
 // function.
 //
-bool GVN::runOnFunction(Function &F) {
+bool GVN::runOnFunction(Function& F) {
+  VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
+  
+  bool changed = false;
+  bool shouldContinue = true;
+  
+  while (shouldContinue) {
+    shouldContinue = iterateOnFunction(F);
+    changed |= shouldContinue;
+  }
+  
+  return changed;
+}
+
+
+// GVN::iterateOnFunction - Executes one iteration of GVN
+bool GVN::iterateOnFunction(Function &F) {
   // Clean out global sets from any previous functions
   VN.clear();
   availableOut.clear();
+  phiMap.clear();
  
   bool changed_function = false;
   
@@ -801,16 +1096,22 @@ bool GVN::runOnFunction(Function &F) {
       currAvail = availableOut[DI->getIDom()->getBlock()];
 
     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
-         BI != BE; ++BI) {
-      processInstruction(BI, currAvail, lastSeenLoad, toErase);
+         BI != BE; ) {
+      changed_function |= processInstruction(BI, currAvail,
+                                             lastSeenLoad, toErase);
+      
+      NumGVNInstr += toErase.size();
+      
+      // Avoid iterator invalidation
+      ++BI;
+      
+      for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
+           E = toErase.end(); I != E; ++I)
+        (*I)->eraseFromParent();
+      
+      toErase.clear();
     }
   }
   
-  NumGVNInstr = toErase.size();
-  
-  for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
-       E = toErase.end(); I != E; ++I)
-    (*I)->eraseFromParent();
-  
   return changed_function;
 }