LLVM API Documentation
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