LLVM API Documentation

GVN.cpp

Go to the documentation of this file.
00001 //===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass performs global value numbering to eliminate fully redundant
00011 // instructions.  It also performs simple dead load elimination.
00012 //
00013 // Note that this pass does the value numbering itself, it does not use the
00014 // ValueNumbering analysis passes.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #define DEBUG_TYPE "gvn"
00019 #include "llvm/Transforms/Scalar.h"
00020 #include "llvm/BasicBlock.h"
00021 #include "llvm/Constants.h"
00022 #include "llvm/DerivedTypes.h"
00023 #include "llvm/Function.h"
00024 #include "llvm/Instructions.h"
00025 #include "llvm/Value.h"
00026 #include "llvm/ADT/DenseMap.h"
00027 #include "llvm/ADT/DepthFirstIterator.h"
00028 #include "llvm/ADT/PostOrderIterator.h"
00029 #include "llvm/ADT/SmallPtrSet.h"
00030 #include "llvm/ADT/SmallVector.h"
00031 #include "llvm/ADT/Statistic.h"
00032 #include "llvm/Analysis/Dominators.h"
00033 #include "llvm/Analysis/AliasAnalysis.h"
00034 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
00035 #include "llvm/Support/CFG.h"
00036 #include "llvm/Support/CommandLine.h"
00037 #include "llvm/Support/Compiler.h"
00038 #include "llvm/Support/Debug.h"
00039 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00040 #include <cstdio>
00041 using namespace llvm;
00042 
00043 STATISTIC(NumGVNInstr,  "Number of instructions deleted");
00044 STATISTIC(NumGVNLoad,   "Number of loads deleted");
00045 STATISTIC(NumGVNPRE,    "Number of instructions PRE'd");
00046 STATISTIC(NumGVNBlocks, "Number of blocks merged");
00047 STATISTIC(NumPRELoad,   "Number of loads PRE'd");
00048 
00049 static cl::opt<bool> EnablePRE("enable-pre",
00050                                cl::init(true), cl::Hidden);
00051 cl::opt<bool> EnableLoadPRE("enable-load-pre"/*, cl::init(true)*/);
00052 
00053 //===----------------------------------------------------------------------===//
00054 //                         ValueTable Class
00055 //===----------------------------------------------------------------------===//
00056 
00057 /// This class holds the mapping between values and value numbers.  It is used
00058 /// as an efficient mechanism to determine the expression-wise equivalence of
00059 /// two values.
00060 namespace {
00061   struct VISIBILITY_HIDDEN Expression {
00062     enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM, 
00063                             FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ, 
00064                             ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE, 
00065                             ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ, 
00066                             FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE, 
00067                             FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE, 
00068                             FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
00069                             SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
00070                             FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT, 
00071                             PTRTOINT, INTTOPTR, BITCAST, GEP, CALL, CONSTANT,
00072                             EMPTY, TOMBSTONE };
00073 
00074     ExpressionOpcode opcode;
00075     const Type* type;
00076     uint32_t firstVN;
00077     uint32_t secondVN;
00078     uint32_t thirdVN;
00079     SmallVector<uint32_t, 4> varargs;
00080     Value* function;
00081   
00082     Expression() { }
00083     Expression(ExpressionOpcode o) : opcode(o) { }
00084   
00085     bool operator==(const Expression &other) const {
00086       if (opcode != other.opcode)
00087         return false;
00088       else if (opcode == EMPTY || opcode == TOMBSTONE)
00089         return true;
00090       else if (type != other.type)
00091         return false;
00092       else if (function != other.function)
00093         return false;
00094       else if (firstVN != other.firstVN)
00095         return false;
00096       else if (secondVN != other.secondVN)
00097         return false;
00098       else if (thirdVN != other.thirdVN)
00099         return false;
00100       else {
00101         if (varargs.size() != other.varargs.size())
00102           return false;
00103       
00104         for (size_t i = 0; i < varargs.size(); ++i)
00105           if (varargs[i] != other.varargs[i])
00106             return false;
00107     
00108         return true;
00109       }
00110     }
00111   
00112     bool operator!=(const Expression &other) const {
00113       return !(*this == other);
00114     }
00115   };
00116   
00117   class VISIBILITY_HIDDEN ValueTable {
00118     private:
00119       DenseMap<Value*, uint32_t> valueNumbering;
00120       DenseMap<Expression, uint32_t> expressionNumbering;
00121       AliasAnalysis* AA;
00122       MemoryDependenceAnalysis* MD;
00123       DominatorTree* DT;
00124   
00125       uint32_t nextValueNumber;
00126     
00127       Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
00128       Expression::ExpressionOpcode getOpcode(CmpInst* C);
00129       Expression::ExpressionOpcode getOpcode(CastInst* C);
00130       Expression create_expression(BinaryOperator* BO);
00131       Expression create_expression(CmpInst* C);
00132       Expression create_expression(ShuffleVectorInst* V);
00133       Expression create_expression(ExtractElementInst* C);
00134       Expression create_expression(InsertElementInst* V);
00135       Expression create_expression(SelectInst* V);
00136       Expression create_expression(CastInst* C);
00137       Expression create_expression(GetElementPtrInst* G);
00138       Expression create_expression(CallInst* C);
00139       Expression create_expression(Constant* C);
00140     public:
00141       ValueTable() : nextValueNumber(1) { }
00142       uint32_t lookup_or_add(Value* V);
00143       uint32_t lookup(Value* V) const;
00144       void add(Value* V, uint32_t num);
00145       void clear();
00146       void erase(Value* v);
00147       unsigned size();
00148       void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
00149       AliasAnalysis *getAliasAnalysis() const { return AA; }
00150       void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
00151       void setDomTree(DominatorTree* D) { DT = D; }
00152       uint32_t getNextUnusedValueNumber() { return nextValueNumber; }
00153       void verifyRemoved(const Value *) const;
00154   };
00155 }
00156 
00157 namespace llvm {
00158 template <> struct DenseMapInfo<Expression> {
00159   static inline Expression getEmptyKey() {
00160     return Expression(Expression::EMPTY);
00161   }
00162   
00163   static inline Expression getTombstoneKey() {
00164     return Expression(Expression::TOMBSTONE);
00165   }
00166   
00167   static unsigned getHashValue(const Expression e) {
00168     unsigned hash = e.opcode;
00169     
00170     hash = e.firstVN + hash * 37;
00171     hash = e.secondVN + hash * 37;
00172     hash = e.thirdVN + hash * 37;
00173     
00174     hash = ((unsigned)((uintptr_t)e.type >> 4) ^
00175             (unsigned)((uintptr_t)e.type >> 9)) +
00176            hash * 37;
00177     
00178     for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
00179          E = e.varargs.end(); I != E; ++I)
00180       hash = *I + hash * 37;
00181     
00182     hash = ((unsigned)((uintptr_t)e.function >> 4) ^
00183             (unsigned)((uintptr_t)e.function >> 9)) +
00184            hash * 37;
00185     
00186     return hash;
00187   }
00188   static bool isEqual(const Expression &LHS, const Expression &RHS) {
00189     return LHS == RHS;
00190   }
00191   static bool isPod() { return true; }
00192 };
00193 }
00194 
00195 //===----------------------------------------------------------------------===//
00196 //                     ValueTable Internal Functions
00197 //===----------------------------------------------------------------------===//
00198 Expression::ExpressionOpcode ValueTable::getOpcode(BinaryOperator* BO) {
00199   switch(BO->getOpcode()) {
00200   default: // THIS SHOULD NEVER HAPPEN
00201     assert(0 && "Binary operator with unknown opcode?");
00202   case Instruction::Add:  return Expression::ADD;
00203   case Instruction::Sub:  return Expression::SUB;
00204   case Instruction::Mul:  return Expression::MUL;
00205   case Instruction::UDiv: return Expression::UDIV;
00206   case Instruction::SDiv: return Expression::SDIV;
00207   case Instruction::FDiv: return Expression::FDIV;
00208   case Instruction::URem: return Expression::UREM;
00209   case Instruction::SRem: return Expression::SREM;
00210   case Instruction::FRem: return Expression::FREM;
00211   case Instruction::Shl:  return Expression::SHL;
00212   case Instruction::LShr: return Expression::LSHR;
00213   case Instruction::AShr: return Expression::ASHR;
00214   case Instruction::And:  return Expression::AND;
00215   case Instruction::Or:   return Expression::OR;
00216   case Instruction::Xor:  return Expression::XOR;
00217   }
00218 }
00219 
00220 Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
00221   if (isa<ICmpInst>(C) || isa<VICmpInst>(C)) {
00222     switch (C->getPredicate()) {
00223     default:  // THIS SHOULD NEVER HAPPEN
00224       assert(0 && "Comparison with unknown predicate?");
00225     case ICmpInst::ICMP_EQ:  return Expression::ICMPEQ;
00226     case ICmpInst::ICMP_NE:  return Expression::ICMPNE;
00227     case ICmpInst::ICMP_UGT: return Expression::ICMPUGT;
00228     case ICmpInst::ICMP_UGE: return Expression::ICMPUGE;
00229     case ICmpInst::ICMP_ULT: return Expression::ICMPULT;
00230     case ICmpInst::ICMP_ULE: return Expression::ICMPULE;
00231     case ICmpInst::ICMP_SGT: return Expression::ICMPSGT;
00232     case ICmpInst::ICMP_SGE: return Expression::ICMPSGE;
00233     case ICmpInst::ICMP_SLT: return Expression::ICMPSLT;
00234     case ICmpInst::ICMP_SLE: return Expression::ICMPSLE;
00235     }
00236   }
00237   assert((isa<FCmpInst>(C) || isa<VFCmpInst>(C)) && "Unknown compare");
00238   switch (C->getPredicate()) {
00239   default: // THIS SHOULD NEVER HAPPEN
00240     assert(0 && "Comparison with unknown predicate?");
00241   case FCmpInst::FCMP_OEQ: return Expression::FCMPOEQ;
00242   case FCmpInst::FCMP_OGT: return Expression::FCMPOGT;
00243   case FCmpInst::FCMP_OGE: return Expression::FCMPOGE;
00244   case FCmpInst::FCMP_OLT: return Expression::FCMPOLT;
00245   case FCmpInst::FCMP_OLE: return Expression::FCMPOLE;
00246   case FCmpInst::FCMP_ONE: return Expression::FCMPONE;
00247   case FCmpInst::FCMP_ORD: return Expression::FCMPORD;
00248   case FCmpInst::FCMP_UNO: return Expression::FCMPUNO;
00249   case FCmpInst::FCMP_UEQ: return Expression::FCMPUEQ;
00250   case FCmpInst::FCMP_UGT: return Expression::FCMPUGT;
00251   case FCmpInst::FCMP_UGE: return Expression::FCMPUGE;
00252   case FCmpInst::FCMP_ULT: return Expression::FCMPULT;
00253   case FCmpInst::FCMP_ULE: return Expression::FCMPULE;
00254   case FCmpInst::FCMP_UNE: return Expression::FCMPUNE;
00255   }
00256 }
00257 
00258 Expression::ExpressionOpcode ValueTable::getOpcode(CastInst* C) {
00259   switch(C->getOpcode()) {
00260   default: // THIS SHOULD NEVER HAPPEN
00261     assert(0 && "Cast operator with unknown opcode?");
00262   case Instruction::Trunc:    return Expression::TRUNC;
00263   case Instruction::ZExt:     return Expression::ZEXT;
00264   case Instruction::SExt:     return Expression::SEXT;
00265   case Instruction::FPToUI:   return Expression::FPTOUI;
00266   case Instruction::FPToSI:   return Expression::FPTOSI;
00267   case Instruction::UIToFP:   return Expression::UITOFP;
00268   case Instruction::SIToFP:   return Expression::SITOFP;
00269   case Instruction::FPTrunc:  return Expression::FPTRUNC;
00270   case Instruction::FPExt:    return Expression::FPEXT;
00271   case Instruction::PtrToInt: return Expression::PTRTOINT;
00272   case Instruction::IntToPtr: return Expression::INTTOPTR;
00273   case Instruction::BitCast:  return Expression::BITCAST;
00274   }
00275 }
00276 
00277 Expression ValueTable::create_expression(CallInst* C) {
00278   Expression e;
00279   
00280   e.type = C->getType();
00281   e.firstVN = 0;
00282   e.secondVN = 0;
00283   e.thirdVN = 0;
00284   e.function = C->getCalledFunction();
00285   e.opcode = Expression::CALL;
00286   
00287   for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
00288        I != E; ++I)
00289     e.varargs.push_back(lookup_or_add(*I));
00290   
00291   return e;
00292 }
00293 
00294 Expression ValueTable::create_expression(BinaryOperator* BO) {
00295   Expression e;
00296     
00297   e.firstVN = lookup_or_add(BO->getOperand(0));
00298   e.secondVN = lookup_or_add(BO->getOperand(1));
00299   e.thirdVN = 0;
00300   e.function = 0;
00301   e.type = BO->getType();
00302   e.opcode = getOpcode(BO);
00303   
00304   return e;
00305 }
00306 
00307 Expression ValueTable::create_expression(CmpInst* C) {
00308   Expression e;
00309     
00310   e.firstVN = lookup_or_add(C->getOperand(0));
00311   e.secondVN = lookup_or_add(C->getOperand(1));
00312   e.thirdVN = 0;
00313   e.function = 0;
00314   e.type = C->getType();
00315   e.opcode = getOpcode(C);
00316   
00317   return e;
00318 }
00319 
00320 Expression ValueTable::create_expression(CastInst* C) {
00321   Expression e;
00322     
00323   e.firstVN = lookup_or_add(C->getOperand(0));
00324   e.secondVN = 0;
00325   e.thirdVN = 0;
00326   e.function = 0;
00327   e.type = C->getType();
00328   e.opcode = getOpcode(C);
00329   
00330   return e;
00331 }
00332 
00333 Expression ValueTable::create_expression(ShuffleVectorInst* S) {
00334   Expression e;
00335     
00336   e.firstVN = lookup_or_add(S->getOperand(0));
00337   e.secondVN = lookup_or_add(S->getOperand(1));
00338   e.thirdVN = lookup_or_add(S->getOperand(2));
00339   e.function = 0;
00340   e.type = S->getType();
00341   e.opcode = Expression::SHUFFLE;
00342   
00343   return e;
00344 }
00345 
00346 Expression ValueTable::create_expression(ExtractElementInst* E) {
00347   Expression e;
00348     
00349   e.firstVN = lookup_or_add(E->getOperand(0));
00350   e.secondVN = lookup_or_add(E->getOperand(1));
00351   e.thirdVN = 0;
00352   e.function = 0;
00353   e.type = E->getType();
00354   e.opcode = Expression::EXTRACT;
00355   
00356   return e;
00357 }
00358 
00359 Expression ValueTable::create_expression(InsertElementInst* I) {
00360   Expression e;
00361     
00362   e.firstVN = lookup_or_add(I->getOperand(0));
00363   e.secondVN = lookup_or_add(I->getOperand(1));
00364   e.thirdVN = lookup_or_add(I->getOperand(2));
00365   e.function = 0;
00366   e.type = I->getType();
00367   e.opcode = Expression::INSERT;
00368   
00369   return e;
00370 }
00371 
00372 Expression ValueTable::create_expression(SelectInst* I) {
00373   Expression e;
00374     
00375   e.firstVN = lookup_or_add(I->getCondition());
00376   e.secondVN = lookup_or_add(I->getTrueValue());
00377   e.thirdVN = lookup_or_add(I->getFalseValue());
00378   e.function = 0;
00379   e.type = I->getType();
00380   e.opcode = Expression::SELECT;
00381   
00382   return e;
00383 }
00384 
00385 Expression ValueTable::create_expression(GetElementPtrInst* G) {
00386   Expression e;
00387   
00388   e.firstVN = lookup_or_add(G->getPointerOperand());
00389   e.secondVN = 0;
00390   e.thirdVN = 0;
00391   e.function = 0;
00392   e.type = G->getType();
00393   e.opcode = Expression::GEP;
00394   
00395   for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
00396        I != E; ++I)
00397     e.varargs.push_back(lookup_or_add(*I));
00398   
00399   return e;
00400 }
00401 
00402 //===----------------------------------------------------------------------===//
00403 //                     ValueTable External Functions
00404 //===----------------------------------------------------------------------===//
00405 
00406 /// add - Insert a value into the table with a specified value number.
00407 void ValueTable::add(Value* V, uint32_t num) {
00408   valueNumbering.insert(std::make_pair(V, num));
00409 }
00410 
00411 /// lookup_or_add - Returns the value number for the specified value, assigning
00412 /// it a new number if it did not have one before.
00413 uint32_t ValueTable::lookup_or_add(Value* V) {
00414   DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
00415   if (VI != valueNumbering.end())
00416     return VI->second;
00417   
00418   if (CallInst* C = dyn_cast<CallInst>(V)) {
00419     if (AA->doesNotAccessMemory(C)) {
00420       Expression e = create_expression(C);
00421     
00422       DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00423       if (EI != expressionNumbering.end()) {
00424         valueNumbering.insert(std::make_pair(V, EI->second));
00425         return EI->second;
00426       } else {
00427         expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00428         valueNumbering.insert(std::make_pair(V, nextValueNumber));
00429       
00430         return nextValueNumber++;
00431       }
00432     } else if (AA->onlyReadsMemory(C)) {
00433       Expression e = create_expression(C);
00434       
00435       if (expressionNumbering.find(e) == expressionNumbering.end()) {
00436         expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00437         valueNumbering.insert(std::make_pair(V, nextValueNumber));
00438         return nextValueNumber++;
00439       }
00440       
00441       MemDepResult local_dep = MD->getDependency(C);
00442       
00443       if (!local_dep.isDef() && !local_dep.isNonLocal()) {
00444         valueNumbering.insert(std::make_pair(V, nextValueNumber));
00445         return nextValueNumber++;
00446       }
00447 
00448       if (local_dep.isDef()) {
00449         CallInst* local_cdep = cast<CallInst>(local_dep.getInst());
00450         
00451         if (local_cdep->getNumOperands() != C->getNumOperands()) {
00452           valueNumbering.insert(std::make_pair(V, nextValueNumber));
00453           return nextValueNumber++;
00454         }
00455           
00456         for (unsigned i = 1; i < C->getNumOperands(); ++i) {
00457           uint32_t c_vn = lookup_or_add(C->getOperand(i));
00458           uint32_t cd_vn = lookup_or_add(local_cdep->getOperand(i));
00459           if (c_vn != cd_vn) {
00460             valueNumbering.insert(std::make_pair(V, nextValueNumber));
00461             return nextValueNumber++;
00462           }
00463         }
00464       
00465         uint32_t v = lookup_or_add(local_cdep);
00466         valueNumbering.insert(std::make_pair(V, v));
00467         return v;
00468       }
00469 
00470       // Non-local case.
00471       const MemoryDependenceAnalysis::NonLocalDepInfo &deps = 
00472         MD->getNonLocalCallDependency(CallSite(C));
00473       // FIXME: call/call dependencies for readonly calls should return def, not
00474       // clobber!  Move the checking logic to MemDep!
00475       CallInst* cdep = 0;
00476       
00477       // Check to see if we have a single dominating call instruction that is
00478       // identical to C.
00479       for (unsigned i = 0, e = deps.size(); i != e; ++i) {
00480         const MemoryDependenceAnalysis::NonLocalDepEntry *I = &deps[i];
00481         // Ignore non-local dependencies.
00482         if (I->second.isNonLocal())
00483           continue;
00484 
00485         // We don't handle non-depedencies.  If we already have a call, reject
00486         // instruction dependencies.
00487         if (I->second.isClobber() || cdep != 0) {
00488           cdep = 0;
00489           break;
00490         }
00491         
00492         CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->second.getInst());
00493         // FIXME: All duplicated with non-local case.
00494         if (NonLocalDepCall && DT->properlyDominates(I->first, C->getParent())){
00495           cdep = NonLocalDepCall;
00496           continue;
00497         }
00498         
00499         cdep = 0;
00500         break;
00501       }
00502       
00503       if (!cdep) {
00504         valueNumbering.insert(std::make_pair(V, nextValueNumber));
00505         return nextValueNumber++;
00506       }
00507       
00508       if (cdep->getNumOperands() != C->getNumOperands()) {
00509         valueNumbering.insert(std::make_pair(V, nextValueNumber));
00510         return nextValueNumber++;
00511       }
00512       for (unsigned i = 1; i < C->getNumOperands(); ++i) {
00513         uint32_t c_vn = lookup_or_add(C->getOperand(i));
00514         uint32_t cd_vn = lookup_or_add(cdep->getOperand(i));
00515         if (c_vn != cd_vn) {
00516           valueNumbering.insert(std::make_pair(V, nextValueNumber));
00517           return nextValueNumber++;
00518         }
00519       }
00520       
00521       uint32_t v = lookup_or_add(cdep);
00522       valueNumbering.insert(std::make_pair(V, v));
00523       return v;
00524       
00525     } else {
00526       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00527       return nextValueNumber++;
00528     }
00529   } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
00530     Expression e = create_expression(BO);
00531     
00532     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00533     if (EI != expressionNumbering.end()) {
00534       valueNumbering.insert(std::make_pair(V, EI->second));
00535       return EI->second;
00536     } else {
00537       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00538       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00539       
00540       return nextValueNumber++;
00541     }
00542   } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
00543     Expression e = create_expression(C);
00544     
00545     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00546     if (EI != expressionNumbering.end()) {
00547       valueNumbering.insert(std::make_pair(V, EI->second));
00548       return EI->second;
00549     } else {
00550       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00551       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00552       
00553       return nextValueNumber++;
00554     }
00555   } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
00556     Expression e = create_expression(U);
00557     
00558     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00559     if (EI != expressionNumbering.end()) {
00560       valueNumbering.insert(std::make_pair(V, EI->second));
00561       return EI->second;
00562     } else {
00563       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00564       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00565       
00566       return nextValueNumber++;
00567     }
00568   } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
00569     Expression e = create_expression(U);
00570     
00571     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00572     if (EI != expressionNumbering.end()) {
00573       valueNumbering.insert(std::make_pair(V, EI->second));
00574       return EI->second;
00575     } else {
00576       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00577       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00578       
00579       return nextValueNumber++;
00580     }
00581   } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
00582     Expression e = create_expression(U);
00583     
00584     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00585     if (EI != expressionNumbering.end()) {
00586       valueNumbering.insert(std::make_pair(V, EI->second));
00587       return EI->second;
00588     } else {
00589       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00590       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00591       
00592       return nextValueNumber++;
00593     }
00594   } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
00595     Expression e = create_expression(U);
00596     
00597     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00598     if (EI != expressionNumbering.end()) {
00599       valueNumbering.insert(std::make_pair(V, EI->second));
00600       return EI->second;
00601     } else {
00602       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00603       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00604       
00605       return nextValueNumber++;
00606     }
00607   } else if (CastInst* U = dyn_cast<CastInst>(V)) {
00608     Expression e = create_expression(U);
00609     
00610     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00611     if (EI != expressionNumbering.end()) {
00612       valueNumbering.insert(std::make_pair(V, EI->second));
00613       return EI->second;
00614     } else {
00615       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00616       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00617       
00618       return nextValueNumber++;
00619     }
00620   } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
00621     Expression e = create_expression(U);
00622     
00623     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
00624     if (EI != expressionNumbering.end()) {
00625       valueNumbering.insert(std::make_pair(V, EI->second));
00626       return EI->second;
00627     } else {
00628       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
00629       valueNumbering.insert(std::make_pair(V, nextValueNumber));
00630       
00631       return nextValueNumber++;
00632     }
00633   } else {
00634     valueNumbering.insert(std::make_pair(V, nextValueNumber));
00635     return nextValueNumber++;
00636   }
00637 }
00638 
00639 /// lookup - Returns the value number of the specified value. Fails if
00640 /// the value has not yet been numbered.
00641 uint32_t ValueTable::lookup(Value* V) const {
00642   DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
00643   assert(VI != valueNumbering.end() && "Value not numbered?");
00644   return VI->second;
00645 }
00646 
00647 /// clear - Remove all entries from the ValueTable
00648 void ValueTable::clear() {
00649   valueNumbering.clear();
00650   expressionNumbering.clear();
00651   nextValueNumber = 1;
00652 }
00653 
00654 /// erase - Remove a value from the value numbering
00655 void ValueTable::erase(Value* V) {
00656   valueNumbering.erase(V);
00657 }
00658 
00659 /// verifyRemoved - Verify that the value is removed from all internal data
00660 /// structures.
00661 void ValueTable::verifyRemoved(const Value *V) const {
00662   for (DenseMap<Value*, uint32_t>::iterator
00663          I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
00664     assert(I->first != V && "Inst still occurs in value numbering map!");
00665   }
00666 }
00667 
00668 //===----------------------------------------------------------------------===//
00669 //                                GVN Pass
00670 //===----------------------------------------------------------------------===//
00671 
00672 namespace {
00673   struct VISIBILITY_HIDDEN ValueNumberScope {
00674     ValueNumberScope* parent;
00675     DenseMap<uint32_t, Value*> table;
00676     
00677     ValueNumberScope(ValueNumberScope* p) : parent(p) { }
00678   };
00679 }
00680 
00681 namespace {
00682 
00683   class VISIBILITY_HIDDEN GVN : public FunctionPass {
00684     bool runOnFunction(Function &F);
00685   public:
00686     static char ID; // Pass identification, replacement for typeid
00687     GVN() : FunctionPass(&ID) { }
00688 
00689   private:
00690     MemoryDependenceAnalysis *MD;
00691     DominatorTree *DT;
00692 
00693     ValueTable VN;
00694     DenseMap<BasicBlock*, ValueNumberScope*> localAvail;
00695     
00696     typedef DenseMap<Value*, SmallPtrSet<Instruction*, 4> > PhiMapType;
00697     PhiMapType phiMap;
00698     
00699     
00700     // This transformation requires dominator postdominator info
00701     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00702       AU.addRequired<DominatorTree>();
00703       AU.addRequired<MemoryDependenceAnalysis>();
00704       AU.addRequired<AliasAnalysis>();
00705       
00706       AU.addPreserved<DominatorTree>();
00707       AU.addPreserved<AliasAnalysis>();
00708     }
00709   
00710     // Helper fuctions
00711     // FIXME: eliminate or document these better
00712     bool processLoad(LoadInst* L,
00713                      SmallVectorImpl<Instruction*> &toErase);
00714     bool processInstruction(Instruction* I,
00715                             SmallVectorImpl<Instruction*> &toErase);
00716     bool processNonLocalLoad(LoadInst* L,
00717                              SmallVectorImpl<Instruction*> &toErase);
00718     bool processBlock(BasicBlock* BB);
00719     Value *GetValueForBlock(BasicBlock *BB, Instruction* orig,
00720                             DenseMap<BasicBlock*, Value*> &Phis,
00721                             bool top_level = false);
00722     void dump(DenseMap<uint32_t, Value*>& d);
00723     bool iterateOnFunction(Function &F);
00724     Value* CollapsePhi(PHINode* p);
00725     bool isSafeReplacement(PHINode* p, Instruction* inst);
00726     bool performPRE(Function& F);
00727     Value* lookupNumber(BasicBlock* BB, uint32_t num);
00728     bool mergeBlockIntoPredecessor(BasicBlock* BB);
00729     Value* AttemptRedundancyElimination(Instruction* orig, unsigned valno);
00730     void cleanupGlobalSets();
00731     void verifyRemoved(const Instruction *I) const;
00732   };
00733   
00734   char GVN::ID = 0;
00735 }
00736 
00737 // createGVNPass - The public interface to this file...
00738 FunctionPass *llvm::createGVNPass() { return new GVN(); }
00739 
00740 static RegisterPass<GVN> X("gvn",
00741                            "Global Value Numbering");
00742 
00743 void GVN::dump(DenseMap<uint32_t, Value*>& d) {
00744   printf("{\n");
00745   for (DenseMap<uint32_t, Value*>::iterator I = d.begin(),
00746        E = d.end(); I != E; ++I) {
00747       printf("%d\n", I->first);
00748       I->second->dump();
00749   }
00750   printf("}\n");
00751 }
00752 
00753 Value* GVN::CollapsePhi(PHINode* p) {
00754   Value* constVal = p->hasConstantValue();
00755   if (!constVal) return 0;
00756   
00757   Instruction* inst = dyn_cast<Instruction>(constVal);
00758   if (!inst)
00759     return constVal;
00760     
00761   if (DT->dominates(inst, p))
00762     if (isSafeReplacement(p, inst))
00763       return inst;
00764   return 0;
00765 }
00766 
00767 bool GVN::isSafeReplacement(PHINode* p, Instruction* inst) {
00768   if (!isa<PHINode>(inst))
00769     return true;
00770   
00771   for (Instruction::use_iterator UI = p->use_begin(), E = p->use_end();
00772        UI != E; ++UI)
00773     if (PHINode* use_phi = dyn_cast<PHINode>(UI))
00774       if (use_phi->getParent() == inst->getParent())
00775         return false;
00776   
00777   return true;
00778 }
00779 
00780 /// GetValueForBlock - Get the value to use within the specified basic block.
00781 /// available values are in Phis.
00782 Value *GVN::GetValueForBlock(BasicBlock *BB, Instruction* orig,
00783                              DenseMap<BasicBlock*, Value*> &Phis,
00784                              bool top_level) { 
00785                                  
00786   // If we have already computed this value, return the previously computed val.
00787   DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
00788   if (V != Phis.end() && !top_level) return V->second;
00789   
00790   // If the block is unreachable, just return undef, since this path
00791   // can't actually occur at runtime.
00792   if (!DT->isReachableFromEntry(BB))
00793     return Phis[BB] = UndefValue::get(orig->getType());
00794   
00795   if (BasicBlock *Pred = BB->getSinglePredecessor()) {
00796     Value *ret = GetValueForBlock(Pred, orig, Phis);
00797     Phis[BB] = ret;
00798     return ret;
00799   }
00800 
00801   // Get the number of predecessors of this block so we can reserve space later.
00802   // If there is already a PHI in it, use the #preds from it, otherwise count.
00803   // Getting it from the PHI is constant time.
00804   unsigned NumPreds;
00805   if (PHINode *ExistingPN = dyn_cast<PHINode>(BB->begin()))
00806     NumPreds = ExistingPN->getNumIncomingValues();
00807   else
00808     NumPreds = std::distance(pred_begin(BB), pred_end(BB));
00809   
00810   // Otherwise, the idom is the loop, so we need to insert a PHI node.  Do so
00811   // now, then get values to fill in the incoming values for the PHI.
00812   PHINode *PN = PHINode::Create(orig->getType(), orig->getName()+".rle",
00813                                 BB->begin());
00814   PN->reserveOperandSpace(NumPreds);
00815   
00816   Phis.insert(std::make_pair(BB, PN));
00817   
00818   // Fill in the incoming values for the block.
00819   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
00820     Value* val = GetValueForBlock(*PI, orig, Phis);
00821     PN->addIncoming(val, *PI);
00822   }
00823   
00824   VN.getAliasAnalysis()->copyValue(orig, PN);
00825   
00826   // Attempt to collapse PHI nodes that are trivially redundant
00827   Value* v = CollapsePhi(PN);
00828   if (!v) {
00829     // Cache our phi construction results
00830     if (LoadInst* L = dyn_cast<LoadInst>(orig))
00831       phiMap[L->getPointerOperand()].insert(PN);
00832     else
00833       phiMap[orig].insert(PN);
00834     
00835     return PN;
00836   }
00837     
00838   PN->replaceAllUsesWith(v);
00839   if (isa<PointerType>(v->getType()))
00840     MD->invalidateCachedPointerInfo(v);
00841 
00842   for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
00843        E = Phis.end(); I != E; ++I)
00844     if (I->second == PN)
00845       I->second = v;
00846 
00847   DEBUG(cerr << "GVN removed: " << *PN);
00848   MD->removeInstruction(PN);
00849   PN->eraseFromParent();
00850   DEBUG(verifyRemoved(PN));
00851 
00852   Phis[BB] = v;
00853   return v;
00854 }
00855 
00856 /// IsValueFullyAvailableInBlock - Return true if we can prove that the value
00857 /// we're analyzing is fully available in the specified block.  As we go, keep
00858 /// track of which blocks we know are fully alive in FullyAvailableBlocks.  This
00859 /// map is actually a tri-state map with the following values:
00860 ///   0) we know the block *is not* fully available.
00861 ///   1) we know the block *is* fully available.
00862 ///   2) we do not know whether the block is fully available or not, but we are
00863 ///      currently speculating that it will be.
00864 ///   3) we are speculating for this block and have used that to speculate for
00865 ///      other blocks.
00866 static bool IsValueFullyAvailableInBlock(BasicBlock *BB, 
00867                             DenseMap<BasicBlock*, char> &FullyAvailableBlocks) {
00868   // Optimistically assume that the block is fully available and check to see
00869   // if we already know about this block in one lookup.
00870   std::pair<DenseMap<BasicBlock*, char>::iterator, char> IV = 
00871     FullyAvailableBlocks.insert(std::make_pair(BB, 2));
00872 
00873   // If the entry already existed for this block, return the precomputed value.
00874   if (!IV.second) {
00875     // If this is a speculative "available" value, mark it as being used for
00876     // speculation of other blocks.
00877     if (IV.first->second == 2)
00878       IV.first->second = 3;
00879     return IV.first->second != 0;
00880   }
00881   
00882   // Otherwise, see if it is fully available in all predecessors.
00883   pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
00884   
00885   // If this block has no predecessors, it isn't live-in here.
00886   if (PI == PE)
00887     goto SpeculationFailure;
00888   
00889   for (; PI != PE; ++PI)
00890     // If the value isn't fully available in one of our predecessors, then it
00891     // isn't fully available in this block either.  Undo our previous
00892     // optimistic assumption and bail out.
00893     if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks))
00894       goto SpeculationFailure;
00895   
00896   return true;
00897   
00898 // SpeculationFailure - If we get here, we found out that this is not, after
00899 // all, a fully-available block.  We have a problem if we speculated on this and
00900 // used the speculation to mark other blocks as available.
00901 SpeculationFailure:
00902   char &BBVal = FullyAvailableBlocks[BB];
00903   
00904   // If we didn't speculate on this, just return with it set to false.
00905   if (BBVal == 2) {
00906     BBVal = 0;
00907     return false;
00908   }
00909 
00910   // If we did speculate on this value, we could have blocks set to 1 that are
00911   // incorrect.  Walk the (transitive) successors of this block and mark them as
00912   // 0 if set to one.
00913   SmallVector<BasicBlock*, 32> BBWorklist;
00914   BBWorklist.push_back(BB);
00915   
00916   while (!BBWorklist.empty()) {
00917     BasicBlock *Entry = BBWorklist.pop_back_val();
00918     // Note that this sets blocks to 0 (unavailable) if they happen to not
00919     // already be in FullyAvailableBlocks.  This is safe.
00920     char &EntryVal = FullyAvailableBlocks[Entry];
00921     if (EntryVal == 0) continue;  // Already unavailable.
00922 
00923     // Mark as unavailable.
00924     EntryVal = 0;
00925     
00926     for (succ_iterator I = succ_begin(Entry), E = succ_end(Entry); I != E; ++I)
00927       BBWorklist.push_back(*I);
00928   }
00929   
00930   return false;
00931 }
00932 
00933 /// processNonLocalLoad - Attempt to eliminate a load whose dependencies are
00934 /// non-local by performing PHI construction.
00935 bool GVN::processNonLocalLoad(LoadInst *LI,
00936                               SmallVectorImpl<Instruction*> &toErase) {
00937   // Find the non-local dependencies of the load.
00938   SmallVector<MemoryDependenceAnalysis::NonLocalDepEntry, 64> Deps; 
00939   MD->getNonLocalPointerDependency(LI->getOperand(0), true, LI->getParent(),
00940                                    Deps);
00941   //DEBUG(cerr << "INVESTIGATING NONLOCAL LOAD: " << Deps.size() << *LI);
00942   
00943   // If we had to process more than one hundred blocks to find the
00944   // dependencies, this load isn't worth worrying about.  Optimizing
00945   // it will be too expensive.
00946   if (Deps.size() > 100)
00947     return false;
00948 
00949   // If we had a phi translation failure, we'll have a single entry which is a
00950   // clobber in the current block.  Reject this early.
00951   if (Deps.size() == 1 && Deps[0].second.isClobber())
00952     return false;
00953   
00954   // Filter out useless results (non-locals, etc).  Keep track of the blocks
00955   // where we have a value available in repl, also keep track of whether we see
00956   // dependencies that produce an unknown value for the load (such as a call
00957   // that could potentially clobber the load).
00958   SmallVector<std::pair<BasicBlock*, Value*>, 16> ValuesPerBlock;
00959   SmallVector<BasicBlock*, 16> UnavailableBlocks;
00960   
00961   for (unsigned i = 0, e = Deps.size(); i != e; ++i) {
00962     BasicBlock *DepBB = Deps[i].first;
00963     MemDepResult DepInfo = Deps[i].second;
00964     
00965     if (DepInfo.isClobber()) {
00966       UnavailableBlocks.push_back(DepBB);
00967       continue;
00968     }
00969     
00970     Instruction *DepInst = DepInfo.getInst();
00971     
00972     // Loading the allocation -> undef.
00973     if (isa<AllocationInst>(DepInst)) {
00974       ValuesPerBlock.push_back(std::make_pair(DepBB, 
00975                                               UndefValue::get(LI->getType())));
00976       continue;
00977     }
00978   
00979     if (StoreInst* S = dyn_cast<StoreInst>(DepInst)) {
00980       // Reject loads and stores that are to the same address but are of 
00981       // different types.
00982       // NOTE: 403.gcc does have this case (e.g. in readonly_fields_p) because
00983       // of bitfield access, it would be interesting to optimize for it at some
00984       // point.
00985       if (S->getOperand(0)->getType() != LI->getType()) {
00986         UnavailableBlocks.push_back(DepBB);
00987         continue;
00988       }
00989       
00990       ValuesPerBlock.push_back(std::make_pair(DepBB, S->getOperand(0)));
00991       
00992     } else if (LoadInst* LD = dyn_cast<LoadInst>(DepInst)) {
00993       if (LD->getType() != LI->getType()) {
00994         UnavailableBlocks.push_back(DepBB);
00995         continue;
00996       }
00997       ValuesPerBlock.push_back(std::make_pair(DepBB, LD));
00998     } else {
00999       UnavailableBlocks.push_back(DepBB);
01000       continue;
01001     }
01002   }
01003   
01004   // If we have no predecessors that produce a known value for this load, exit
01005   // early.
01006   if (ValuesPerBlock.empty()) return false;
01007   
01008   // If all of the instructions we depend on produce a known value for this
01009   // load, then it is fully redundant and we can use PHI insertion to compute
01010   // its value.  Insert PHIs and remove the fully redundant value now.
01011   if (UnavailableBlocks.empty()) {
01012     // Use cached PHI construction information from previous runs
01013     SmallPtrSet<Instruction*, 4> &p = phiMap[LI->getPointerOperand()];
01014     // FIXME: What does phiMap do? Are we positive it isn't getting invalidated?
01015     for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
01016          I != E; ++I) {
01017       if ((*I)->getParent() == LI->getParent()) {
01018         DEBUG(cerr << "GVN REMOVING NONLOCAL LOAD #1: " << *LI);
01019         LI->replaceAllUsesWith(*I);
01020         if (isa<PointerType>((*I)->getType()))
01021           MD->invalidateCachedPointerInfo(*I);
01022         toErase.push_back(LI);
01023         NumGVNLoad++;
01024         return true;
01025       }
01026       
01027       ValuesPerBlock.push_back(std::make_pair((*I)->getParent(), *I));
01028     }
01029     
01030     DEBUG(cerr << "GVN REMOVING NONLOCAL LOAD: " << *LI);
01031     
01032     DenseMap<BasicBlock*, Value*> BlockReplValues;
01033     BlockReplValues.insert(ValuesPerBlock.begin(), ValuesPerBlock.end());
01034     // Perform PHI construction.
01035     Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
01036     LI->replaceAllUsesWith(v);
01037     
01038     if (!isa<GlobalValue>(v))
01039       v->takeName(LI);
01040     if (isa<PointerType>(v->getType()))
01041       MD->invalidateCachedPointerInfo(v);
01042     toErase.push_back(LI);
01043     NumGVNLoad++;
01044     return true;
01045   }
01046   
01047   if (!EnablePRE || !EnableLoadPRE)
01048     return false;
01049 
01050   // Okay, we have *some* definitions of the value.  This means that the value
01051   // is available in some of our (transitive) predecessors.  Lets think about
01052   // doing PRE of this load.  This will involve inserting a new load into the
01053   // predecessor when it's not available.  We could do this in general, but
01054   // prefer to not increase code size.  As such, we only do this when we know
01055   // that we only have to insert *one* load (which means we're basically moving
01056   // the load, not inserting a new one).
01057   
01058   // Everything we do here is based on local predecessors of LI's block.  If it
01059   // only has one predecessor, bail now.
01060   BasicBlock *LoadBB = LI->getParent();
01061   if (LoadBB->getSinglePredecessor())
01062     return false;
01063   
01064   // If we have a repl set with LI itself in it, this means we have a loop where
01065   // at least one of the values is LI.  Since this means that we won't be able
01066   // to eliminate LI even if we insert uses in the other predecessors, we will
01067   // end up increasing code size.  Reject this by scanning for LI.
01068   for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
01069     if (ValuesPerBlock[i].second == LI)
01070       return false;
01071   
01072   // Okay, we have some hope :).  Check to see if the loaded value is fully
01073   // available in all but one predecessor.
01074   // FIXME: If we could restructure the CFG, we could make a common pred with
01075   // all the preds that don't have an available LI and insert a new load into
01076   // that one block.
01077   BasicBlock *UnavailablePred = 0;
01078 
01079   DenseMap<BasicBlock*, char> FullyAvailableBlocks;
01080   for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
01081     FullyAvailableBlocks[ValuesPerBlock[i].first] = true;
01082   for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
01083     FullyAvailableBlocks[UnavailableBlocks[i]] = false;
01084 
01085   for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
01086        PI != E; ++PI) {
01087     if (IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks))
01088       continue;
01089     
01090     // If this load is not available in multiple predecessors, reject it.
01091     if (UnavailablePred && UnavailablePred != *PI)
01092       return false;
01093     UnavailablePred = *PI;
01094   }
01095   
01096   assert(UnavailablePred != 0 &&
01097          "Fully available value should be eliminated above!");
01098   
01099   // If the loaded pointer is PHI node defined in this block, do PHI translation
01100   // to get its value in the predecessor.
01101   Value *LoadPtr = LI->getOperand(0)->DoPHITranslation(LoadBB, UnavailablePred);
01102   
01103   // Make sure the value is live in the predecessor.  If it was defined by a
01104   // non-PHI instruction in this block, we don't know how to recompute it above.
01105   if (Instruction *LPInst = dyn_cast<Instruction>(LoadPtr))
01106     if (!DT->dominates(LPInst->getParent(), UnavailablePred)) {
01107       DEBUG(cerr << "COULDN'T PRE LOAD BECAUSE PTR IS UNAVAILABLE IN PRED: "
01108                  << *LPInst << *LI << "\n");
01109       return false;
01110     }
01111   
01112   // We don't currently handle critical edges :(
01113   if (UnavailablePred->getTerminator()->getNumSuccessors() != 1) {
01114     DEBUG(cerr << "COULD NOT PRE LOAD BECAUSE OF CRITICAL EDGE '"
01115                 << UnavailablePred->getName() << "': " << *LI);
01116     return false;
01117   }
01118   
01119   // Okay, we can eliminate this load by inserting a reload in the predecessor
01120   // and using PHI construction to get the value in the other predecessors, do
01121   // it.
01122   DEBUG(cerr << "GVN REMOVING PRE LOAD: " << *LI);
01123   
01124   Value *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre", false,
01125                                 LI->getAlignment(),
01126                                 UnavailablePred->getTerminator());
01127   
01128   DenseMap<BasicBlock*, Value*> BlockReplValues;
01129   BlockReplValues.insert(ValuesPerBlock.begin(), ValuesPerBlock.end());
01130   BlockReplValues[UnavailablePred] = NewLoad;
01131   
01132   // Perform PHI construction.
01133   Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
01134   LI->replaceAllUsesWith(v);
01135   if (!isa<GlobalValue>(v))
01136     v->takeName(LI);
01137   if (isa<PointerType>(v->getType()))
01138     MD->invalidateCachedPointerInfo(v);
01139   toErase.push_back(LI);
01140   NumPRELoad++;
01141   return true;
01142 }
01143 
01144 /// processLoad - Attempt to eliminate a load, first by eliminating it
01145 /// locally, and then attempting non-local elimination if that fails.
01146 bool GVN::processLoad(LoadInst *L, SmallVectorImpl<Instruction*> &toErase) {
01147   if (L->isVolatile())
01148     return false;
01149   
01150   Value* pointer = L->getPointerOperand();
01151 
01152   // ... to a pointer that has been loaded from before...
01153   MemDepResult dep = MD->getDependency(L);
01154   
01155   // If the value isn't available, don't do anything!
01156   if (dep.isClobber())
01157     return false;
01158 
01159   // If it is defined in another block, try harder.
01160   if (dep.isNonLocal())
01161     return processNonLocalLoad(L, toErase);
01162 
01163   Instruction *DepInst = dep.getInst();
01164   if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
01165     // Only forward substitute stores to loads of the same type.
01166     // FIXME: Could do better!
01167     if (DepSI->getPointerOperand()->getType() != pointer->getType())
01168       return false;
01169     
01170     // Remove it!
01171     L->replaceAllUsesWith(DepSI->getOperand(0));
01172     if (isa<PointerType>(DepSI->getOperand(0)->getType()))
01173       MD->invalidateCachedPointerInfo(DepSI->getOperand(0));
01174     toErase.push_back(L);
01175     NumGVNLoad++;
01176     return true;
01177   }
01178 
01179   if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInst)) {
01180     // Only forward substitute stores to loads of the same type.
01181     // FIXME: Could do better! load i32 -> load i8 -> truncate on little endian.
01182     if (DepLI->getType() != L->getType())
01183       return false;
01184     
01185     // Remove it!
01186     L->replaceAllUsesWith(DepLI);
01187     if (isa<PointerType>(DepLI->getType()))
01188       MD->invalidateCachedPointerInfo(DepLI);
01189     toErase.push_back(L);
01190     NumGVNLoad++;
01191     return true;
01192   }
01193   
01194   // If this load really doesn't depend on anything, then we must be loading an
01195   // undef value.  This can happen when loading for a fresh allocation with no
01196   // intervening stores, for example.
01197   if (isa<AllocationInst>(DepInst)) {
01198     L->replaceAllUsesWith(UndefValue::get(L->getType()));
01199     toErase.push_back(L);
01200     NumGVNLoad++;
01201     return true;
01202   }
01203 
01204   return false;
01205 }
01206 
01207 Value* GVN::lookupNumber(BasicBlock* BB, uint32_t num) {
01208   DenseMap<BasicBlock*, ValueNumberScope*>::iterator I = localAvail.find(BB);
01209   if (I == localAvail.end())
01210     return 0;
01211   
01212   ValueNumberScope* locals = I->second;
01213   
01214   while (locals) {
01215     DenseMap<uint32_t, Value*>::iterator I = locals->table.find(num);
01216     if (I != locals->table.end())
01217       return I->second;
01218     else
01219       locals = locals->parent;
01220   }
01221   
01222   return 0;
01223 }
01224 
01225 /// AttemptRedundancyElimination - If the "fast path" of redundancy elimination
01226 /// by inheritance from the dominator fails, see if we can perform phi 
01227 /// construction to eliminate the redundancy.
01228 Value* GVN::AttemptRedundancyElimination(Instruction* orig, unsigned valno) {
01229   BasicBlock* BaseBlock = orig->getParent();
01230   
01231   SmallPtrSet<BasicBlock*, 4> Visited;
01232   SmallVector<BasicBlock*, 8> Stack;
01233   Stack.push_back(BaseBlock);
01234   
01235   DenseMap<BasicBlock*, Value*> Results;
01236   
01237   // Walk backwards through our predecessors, looking for instances of the
01238   // value number we're looking for.  Instances are recorded in the Results
01239   // map, which is then used to perform phi construction.
01240   while (!Stack.empty()) {
01241     BasicBlock* Current = Stack.back();
01242     Stack.pop_back();
01243     
01244     // If we've walked all the way to a proper dominator, then give up. Cases
01245     // where the instance is in the dominator will have been caught by the fast
01246     // path, and any cases that require phi construction further than this are
01247     // probably not worth it anyways.  Note that this is a SIGNIFICANT compile
01248     // time improvement.
01249     if (DT->properlyDominates(Current, orig->getParent())) return 0;
01250     
01251     DenseMap<BasicBlock*, ValueNumberScope*>::iterator LA =
01252                                                        localAvail.find(Current);
01253     if (LA == localAvail.end()) return 0;
01254     DenseMap<uint32_t, Value*>::iterator V = LA->second->table.find(valno);
01255     
01256     if (V != LA->second->table.end()) {
01257       // Found an instance, record it.
01258       Results.insert(std::make_pair(Current, V->second));
01259       continue;
01260     }
01261     
01262     // If we reach the beginning of the function, then give up.
01263     if (pred_begin(Current) == pred_end(Current))
01264       return 0;
01265     
01266     for (pred_iterator PI = pred_begin(Current), PE = pred_end(Current);
01267          PI != PE; ++PI)
01268       if (Visited.insert(*PI))
01269         Stack.push_back(*PI);
01270   }
01271   
01272   // If we didn't find instances, give up.  Otherwise, perform phi construction.
01273   if (Results.size() == 0)
01274     return 0;
01275   else
01276     return GetValueForBlock(BaseBlock, orig, Results, true);
01277 }
01278 
01279 /// processInstruction - When calculating availability, handle an instruction
01280 /// by inserting it into the appropriate sets
01281 bool GVN::processInstruction(Instruction *I,
01282                              SmallVectorImpl<Instruction*> &toErase) {
01283   if (LoadInst* L = dyn_cast<LoadInst>(I)) {
01284     bool changed = processLoad(L, toErase);
01285     
01286     if (!changed) {
01287       unsigned num = VN.lookup_or_add(L);
01288       localAvail[I->getParent()]->table.insert(std::make_pair(num, L));
01289     }
01290     
01291     return changed;
01292   }
01293   
01294   uint32_t nextNum = VN.getNextUnusedValueNumber();
01295   unsigned num = VN.lookup_or_add(I);
01296   
01297   // Allocations are always uniquely numbered, so we can save time and memory
01298   // by fast failing them.
01299   if (isa<AllocationInst>(I) || isa<TerminatorInst>(I)) {
01300     localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
01301     return false;
01302   }
01303   
01304   // Collapse PHI nodes
01305   if (PHINode* p = dyn_cast<PHINode>(I)) {
01306     Value* constVal = CollapsePhi(p);
01307     
01308     if (constVal) {
01309       for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
01310            PI != PE; ++PI)
01311         PI->second.erase(p);
01312         
01313       p->replaceAllUsesWith(constVal);
01314       if (isa<PointerType>(constVal->getType()))
01315         MD->invalidateCachedPointerInfo(constVal);
01316       VN.erase(p);
01317       
01318       toErase.push_back(p);
01319     } else {
01320       localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
01321     }
01322   
01323   // If the number we were assigned was a brand new VN, then we don't
01324   // need to do a lookup to see if the number already exists
01325   // somewhere in the domtree: it can't!
01326   } else if (num == nextNum) {
01327     localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
01328     
01329   // Perform fast-path value-number based elimination of values inherited from
01330   // dominators.
01331   } else if (Value* repl = lookupNumber(I->getParent(), num)) {
01332     // Remove it!
01333     VN.erase(I);
01334     I->replaceAllUsesWith(repl);
01335     if (isa<PointerType>(repl->getType()))
01336       MD->invalidateCachedPointerInfo(repl);
01337     toErase.push_back(I);
01338     return true;
01339 
01340 #if 0
01341   // Perform slow-pathvalue-number based elimination with phi construction.
01342   } else if (Value* repl = AttemptRedundancyElimination(I, num)) {
01343     // Remove it!
01344     VN.erase(I);
01345     I->replaceAllUsesWith(repl);
01346     if (isa<PointerType>(repl->getType()))
01347       MD->invalidateCachedPointerInfo(repl);
01348     toErase.push_back(I);
01349     return true;
01350 #endif
01351   } else {
01352     localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
01353   }
01354   
01355   return false;
01356 }
01357 
01358 /// runOnFunction - This is the main transformation entry point for a function.
01359 bool GVN::runOnFunction(Function& F) {
01360   MD = &getAnalysis<MemoryDependenceAnalysis>();
01361   DT = &getAnalysis<DominatorTree>();
01362   VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
01363   VN.setMemDep(MD);
01364   VN.setDomTree(DT);
01365   
01366   bool changed = false;
01367   bool shouldContinue = true;
01368   
01369   // Merge unconditional branches, allowing PRE to catch more
01370   // optimization opportunities.
01371   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
01372     BasicBlock* BB = FI;
01373     ++FI;
01374     bool removedBlock = MergeBlockIntoPredecessor(BB, this);
01375     if (removedBlock) NumGVNBlocks++;
01376     
01377     changed |= removedBlock;
01378   }
01379   
01380   unsigned Iteration = 0;
01381   
01382   while (shouldContinue) {
01383     DEBUG(cerr << "GVN iteration: " << Iteration << "\n");
01384     shouldContinue = iterateOnFunction(F);
01385     changed |= shouldContinue;
01386     ++Iteration;
01387   }
01388   
01389   if (EnablePRE) {
01390     bool PREChanged = true;
01391     while (PREChanged) {
01392       PREChanged = performPRE(F);
01393       changed |= PREChanged;
01394     }
01395   }
01396   // FIXME: Should perform GVN again after PRE does something.  PRE can move
01397   // computations into blocks where they become fully redundant.  Note that
01398   // we can't do this until PRE's critical edge splitting updates memdep.
01399   // Actually, when this happens, we should just fully integrate PRE into GVN.
01400 
01401   cleanupGlobalSets();
01402 
01403   return changed;
01404 }
01405 
01406 
01407 bool GVN::processBlock(BasicBlock* BB) {
01408   DomTreeNode* DTN = DT->getNode(BB);
01409   // FIXME: Kill off toErase by doing erasing eagerly in a helper function (and
01410   // incrementing BI before processing an instruction).
01411   SmallVector<Instruction*, 8> toErase;
01412   bool changed_function = false;
01413   
01414   if (DTN->getIDom())
01415     localAvail[BB] =
01416                   new ValueNumberScope(localAvail[DTN->getIDom()->getBlock()]);
01417   else
01418     localAvail[BB] = new ValueNumberScope(0);
01419   
01420   for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
01421        BI != BE;) {
01422     changed_function |= processInstruction(BI, toErase);
01423     if (toErase.empty()) {
01424       ++BI;
01425       continue;
01426     }
01427     
01428     // If we need some instructions deleted, do it now.
01429     NumGVNInstr += toErase.size();
01430     
01431     // Avoid iterator invalidation.
01432     bool AtStart = BI == BB->begin();
01433     if (!AtStart)
01434       --BI;
01435 
01436     for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
01437          E = toErase.end(); I != E; ++I) {
01438       DEBUG(cerr << "GVN removed: " << **I);
01439       MD->removeInstruction(*I);
01440       (*I)->eraseFromParent();
01441       DEBUG(verifyRemoved(*I));
01442     }
01443     toErase.clear();
01444 
01445     if (AtStart)
01446       BI = BB->begin();
01447     else
01448       ++BI;
01449   }
01450   
01451   return changed_function;
01452 }
01453 
01454 /// performPRE - Perform a purely local form of PRE that looks for diamond
01455 /// control flow patterns and attempts to perform simple PRE at the join point.
01456 bool GVN::performPRE(Function& F) {
01457   bool Changed = false;
01458   SmallVector<std::pair<TerminatorInst*, unsigned>, 4> toSplit;
01459   DenseMap<BasicBlock*, Value*> predMap;
01460   for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
01461        DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
01462     BasicBlock* CurrentBlock = *DI;
01463     
01464     // Nothing to PRE in the entry block.
01465     if (CurrentBlock == &F.getEntryBlock()) continue;
01466     
01467     for (BasicBlock::iterator BI = CurrentBlock->begin(),
01468          BE = CurrentBlock->end(); BI != BE; ) {
01469       Instruction *CurInst = BI++;
01470       
01471       if (isa<AllocationInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
01472           isa<PHINode>(CurInst) || CurInst->mayReadFromMemory() ||
01473           CurInst->mayWriteToMemory())
01474         continue;
01475       
01476       uint32_t valno = VN.lookup(CurInst);
01477       
01478       // Look for the predecessors for PRE opportunities.  We're
01479       // only trying to solve the basic diamond case, where
01480       // a value is computed in the successor and one predecessor,
01481       // but not the other.  We also explicitly disallow cases
01482       // where the successor is its own predecessor, because they're
01483       // more complicated to get right.
01484       unsigned numWith = 0;
01485       unsigned numWithout = 0;
01486       BasicBlock* PREPred = 0;
01487       predMap.clear();
01488 
01489       for (pred_iterator PI = pred_begin(CurrentBlock),
01490            PE = pred_end(CurrentBlock); PI != PE; ++PI) {
01491         // We're not interested in PRE where the block is its
01492         // own predecessor, on in blocks with predecessors
01493         // that are not reachable.
01494         if (*PI == CurrentBlock) {
01495           numWithout = 2;
01496           break;
01497         } else if (!localAvail.count(*PI))  {
01498           numWithout = 2;
01499           break;
01500         }
01501         
01502         DenseMap<uint32_t, Value*>::iterator predV = 
01503                                             localAvail[*PI]->table.find(valno);
01504         if (predV == localAvail[*PI]->table.end()) {
01505           PREPred = *PI;
01506           numWithout++;
01507         } else if (predV->second == CurInst) {
01508           numWithout = 2;
01509         } else {
01510           predMap[*PI] = predV->second;
01511           numWith++;
01512         }
01513       }
01514       
01515       // Don't do PRE when it might increase code size, i.e. when
01516       // we would need to insert instructions in more than one pred.
01517       if (numWithout != 1 || numWith == 0)
01518         continue;
01519       
01520       // We can't do PRE safely on a critical edge, so instead we schedule
01521       // the edge to be split and perform the PRE the next time we iterate
01522       // on the function.
01523       unsigned succNum = 0;
01524       for (unsigned i = 0, e = PREPred->getTerminator()->getNumSuccessors();
01525            i != e; ++i)
01526         if (PREPred->getTerminator()->getSuccessor(i) == CurrentBlock) {
01527           succNum = i;
01528           break;
01529         }
01530         
01531       if (isCriticalEdge(PREPred->getTerminator(), succNum)) {
01532         toSplit.push_back(std::make_pair(PREPred->getTerminator(), succNum));
01533         continue;
01534       }
01535       
01536       // Instantiate the expression the in predecessor that lacked it.
01537       // Because we are going top-down through the block, all value numbers
01538       // will be available in the predecessor by the time we need them.  Any
01539       // that weren't original present will have been instantiated earlier
01540       // in this loop.
01541       Instruction* PREInstr = CurInst->clone();
01542       bool success = true;
01543       for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) {
01544         Value *Op = PREInstr->getOperand(i);
01545         if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
01546           continue;
01547         
01548         if (Value *V = lookupNumber(PREPred, VN.lookup(Op))) {
01549           PREInstr->setOperand(i, V);
01550         } else {
01551           success = false;
01552           break;
01553         }
01554       }
01555       
01556       // Fail out if we encounter an operand that is not available in
01557       // the PRE predecessor.  This is typically because of loads which 
01558       // are not value numbered precisely.
01559       if (!success) {
01560         delete PREInstr;
01561         DEBUG(verifyRemoved(PREInstr));
01562         continue;
01563       }
01564       
01565       PREInstr->insertBefore(PREPred->getTerminator());
01566       PREInstr->setName(CurInst->getName() + ".pre");
01567       predMap[PREPred] = PREInstr;
01568       VN.add(PREInstr, valno);
01569       NumGVNPRE++;
01570       
01571       // Update the availability map to include the new instruction.
01572       localAvail[PREPred]->table.insert(std::make_pair(valno, PREInstr));
01573       
01574       // Create a PHI to make the value available in this block.
01575       PHINode* Phi = PHINode::Create(CurInst->getType(),
01576                                      CurInst->getName() + ".pre-phi",
01577                                      CurrentBlock->begin());
01578       for (pred_iterator PI = pred_begin(CurrentBlock),
01579            PE = pred_end(CurrentBlock); PI != PE; ++PI)
01580         Phi->addIncoming(predMap[*PI], *PI);
01581       
01582       VN.add(Phi, valno);
01583       localAvail[CurrentBlock]->table[valno] = Phi;
01584       
01585       CurInst->replaceAllUsesWith(Phi);
01586       if (isa<PointerType>(Phi->getType()))
01587         MD->invalidateCachedPointerInfo(Phi);
01588       VN.erase(CurInst);
01589       
01590       DEBUG(cerr << "GVN PRE removed: " << *CurInst);
01591       MD->removeInstruction(CurInst);
01592       CurInst->eraseFromParent();
01593       DEBUG(verifyRemoved(CurInst));
01594       Changed = true;
01595     }
01596   }
01597   
01598   for (SmallVector<std::pair<TerminatorInst*, unsigned>, 4>::iterator
01599        I = toSplit.begin(), E = toSplit.end(); I != E; ++I)
01600     SplitCriticalEdge(I->first, I->second, this);
01601   
01602   return Changed || toSplit.size();
01603 }
01604 
01605 /// iterateOnFunction - Executes one iteration of GVN
01606 bool GVN::iterateOnFunction(Function &F) {
01607   cleanupGlobalSets();
01608 
01609   // Top-down walk of the dominator tree
01610   bool changed = false;
01611 #if 0
01612   // Needed for value numbering with phi construction to work.
01613   ReversePostOrderTraversal<Function*> RPOT(&F);
01614   for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(),
01615        RE = RPOT.end(); RI != RE; ++RI)
01616     changed |= processBlock(*RI);
01617 #else
01618   for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
01619        DE = df_end(DT->getRootNode()); DI != DE; ++DI)
01620     changed |= processBlock(DI->getBlock());
01621 #endif
01622 
01623   return changed;
01624 }
01625 
01626 void GVN::cleanupGlobalSets() {
01627   VN.clear();
01628   phiMap.clear();
01629 
01630   for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
01631        I = localAvail.begin(), E = localAvail.end(); I != E; ++I)
01632     delete I->second;
01633   localAvail.clear();
01634 }
01635 
01636 /// verifyRemoved - Verify that the specified instruction does not occur in our
01637 /// internal data structures.
01638 void GVN::verifyRemoved(const Instruction *Inst) const {
01639   VN.verifyRemoved(Inst);
01640 
01641   // Walk through the PHI map to make sure the instruction isn't hiding in there
01642   // somewhere.
01643   for (PhiMapType::iterator
01644          I = phiMap.begin(), E = phiMap.end(); I != E; ++I) {
01645     assert(I->first != Inst && "Inst is still a key in PHI map!");
01646 
01647     for (SmallPtrSet<Instruction*, 4>::iterator
01648            II = I->second.begin(), IE = I->second.end(); II != IE; ++II) {
01649       assert(*II != Inst && "Inst is still a value in PHI map!");
01650     }
01651   }
01652 
01653   // Walk through the value number scope to make sure the instruction isn't
01654   // ferreted away in it.
01655   for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
01656          I = localAvail.begin(), E = localAvail.end(); I != E; ++I) {
01657     const ValueNumberScope *VNS = I->second;
01658 
01659     while (VNS) {
01660       for (DenseMap<uint32_t, Value*>::iterator
01661              II = VNS->table.begin(), IE = VNS->table.end(); II != IE; ++II) {
01662         assert(II->second != Inst && "Inst still in value numbering scope!");
01663       }
01664 
01665       VNS = VNS->parent;
01666     }
01667   }
01668 }