LLVM API Documentation
00001 //===- EarlyCSE.cpp - Simple and fast CSE pass ----------------------------===// 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 a simple dominator tree walk that eliminates trivially 00011 // redundant instructions. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Transforms/Scalar.h" 00016 #include "llvm/ADT/Hashing.h" 00017 #include "llvm/ADT/ScopedHashTable.h" 00018 #include "llvm/ADT/Statistic.h" 00019 #include "llvm/Analysis/AssumptionTracker.h" 00020 #include "llvm/Analysis/InstructionSimplify.h" 00021 #include "llvm/IR/DataLayout.h" 00022 #include "llvm/IR/Dominators.h" 00023 #include "llvm/IR/Instructions.h" 00024 #include "llvm/Pass.h" 00025 #include "llvm/Support/Debug.h" 00026 #include "llvm/Support/RecyclingAllocator.h" 00027 #include "llvm/Target/TargetLibraryInfo.h" 00028 #include "llvm/Transforms/Utils/Local.h" 00029 #include <vector> 00030 using namespace llvm; 00031 00032 #define DEBUG_TYPE "early-cse" 00033 00034 STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd"); 00035 STATISTIC(NumCSE, "Number of instructions CSE'd"); 00036 STATISTIC(NumCSELoad, "Number of load instructions CSE'd"); 00037 STATISTIC(NumCSECall, "Number of call instructions CSE'd"); 00038 STATISTIC(NumDSE, "Number of trivial dead stores removed"); 00039 00040 static unsigned getHash(const void *V) { 00041 return DenseMapInfo<const void*>::getHashValue(V); 00042 } 00043 00044 //===----------------------------------------------------------------------===// 00045 // SimpleValue 00046 //===----------------------------------------------------------------------===// 00047 00048 namespace { 00049 /// SimpleValue - Instances of this struct represent available values in the 00050 /// scoped hash table. 00051 struct SimpleValue { 00052 Instruction *Inst; 00053 00054 SimpleValue(Instruction *I) : Inst(I) { 00055 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); 00056 } 00057 00058 bool isSentinel() const { 00059 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() || 00060 Inst == DenseMapInfo<Instruction*>::getTombstoneKey(); 00061 } 00062 00063 static bool canHandle(Instruction *Inst) { 00064 // This can only handle non-void readnone functions. 00065 if (CallInst *CI = dyn_cast<CallInst>(Inst)) 00066 return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy(); 00067 return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) || 00068 isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) || 00069 isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) || 00070 isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) || 00071 isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst); 00072 } 00073 }; 00074 } 00075 00076 namespace llvm { 00077 template<> struct DenseMapInfo<SimpleValue> { 00078 static inline SimpleValue getEmptyKey() { 00079 return DenseMapInfo<Instruction*>::getEmptyKey(); 00080 } 00081 static inline SimpleValue getTombstoneKey() { 00082 return DenseMapInfo<Instruction*>::getTombstoneKey(); 00083 } 00084 static unsigned getHashValue(SimpleValue Val); 00085 static bool isEqual(SimpleValue LHS, SimpleValue RHS); 00086 }; 00087 } 00088 00089 unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) { 00090 Instruction *Inst = Val.Inst; 00091 // Hash in all of the operands as pointers. 00092 if (BinaryOperator* BinOp = dyn_cast<BinaryOperator>(Inst)) { 00093 Value *LHS = BinOp->getOperand(0); 00094 Value *RHS = BinOp->getOperand(1); 00095 if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1)) 00096 std::swap(LHS, RHS); 00097 00098 if (isa<OverflowingBinaryOperator>(BinOp)) { 00099 // Hash the overflow behavior 00100 unsigned Overflow = 00101 BinOp->hasNoSignedWrap() * OverflowingBinaryOperator::NoSignedWrap | 00102 BinOp->hasNoUnsignedWrap() * OverflowingBinaryOperator::NoUnsignedWrap; 00103 return hash_combine(BinOp->getOpcode(), Overflow, LHS, RHS); 00104 } 00105 00106 return hash_combine(BinOp->getOpcode(), LHS, RHS); 00107 } 00108 00109 if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) { 00110 Value *LHS = CI->getOperand(0); 00111 Value *RHS = CI->getOperand(1); 00112 CmpInst::Predicate Pred = CI->getPredicate(); 00113 if (Inst->getOperand(0) > Inst->getOperand(1)) { 00114 std::swap(LHS, RHS); 00115 Pred = CI->getSwappedPredicate(); 00116 } 00117 return hash_combine(Inst->getOpcode(), Pred, LHS, RHS); 00118 } 00119 00120 if (CastInst *CI = dyn_cast<CastInst>(Inst)) 00121 return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0)); 00122 00123 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst)) 00124 return hash_combine(EVI->getOpcode(), EVI->getOperand(0), 00125 hash_combine_range(EVI->idx_begin(), EVI->idx_end())); 00126 00127 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst)) 00128 return hash_combine(IVI->getOpcode(), IVI->getOperand(0), 00129 IVI->getOperand(1), 00130 hash_combine_range(IVI->idx_begin(), IVI->idx_end())); 00131 00132 assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) || 00133 isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) || 00134 isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) || 00135 isa<ShuffleVectorInst>(Inst)) && "Invalid/unknown instruction"); 00136 00137 // Mix in the opcode. 00138 return hash_combine(Inst->getOpcode(), 00139 hash_combine_range(Inst->value_op_begin(), 00140 Inst->value_op_end())); 00141 } 00142 00143 bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) { 00144 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; 00145 00146 if (LHS.isSentinel() || RHS.isSentinel()) 00147 return LHSI == RHSI; 00148 00149 if (LHSI->getOpcode() != RHSI->getOpcode()) return false; 00150 if (LHSI->isIdenticalTo(RHSI)) return true; 00151 00152 // If we're not strictly identical, we still might be a commutable instruction 00153 if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) { 00154 if (!LHSBinOp->isCommutative()) 00155 return false; 00156 00157 assert(isa<BinaryOperator>(RHSI) 00158 && "same opcode, but different instruction type?"); 00159 BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI); 00160 00161 // Check overflow attributes 00162 if (isa<OverflowingBinaryOperator>(LHSBinOp)) { 00163 assert(isa<OverflowingBinaryOperator>(RHSBinOp) 00164 && "same opcode, but different operator type?"); 00165 if (LHSBinOp->hasNoUnsignedWrap() != RHSBinOp->hasNoUnsignedWrap() || 00166 LHSBinOp->hasNoSignedWrap() != RHSBinOp->hasNoSignedWrap()) 00167 return false; 00168 } 00169 00170 // Commuted equality 00171 return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) && 00172 LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0); 00173 } 00174 if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) { 00175 assert(isa<CmpInst>(RHSI) 00176 && "same opcode, but different instruction type?"); 00177 CmpInst *RHSCmp = cast<CmpInst>(RHSI); 00178 // Commuted equality 00179 return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) && 00180 LHSCmp->getOperand(1) == RHSCmp->getOperand(0) && 00181 LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate(); 00182 } 00183 00184 return false; 00185 } 00186 00187 //===----------------------------------------------------------------------===// 00188 // CallValue 00189 //===----------------------------------------------------------------------===// 00190 00191 namespace { 00192 /// CallValue - Instances of this struct represent available call values in 00193 /// the scoped hash table. 00194 struct CallValue { 00195 Instruction *Inst; 00196 00197 CallValue(Instruction *I) : Inst(I) { 00198 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!"); 00199 } 00200 00201 bool isSentinel() const { 00202 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() || 00203 Inst == DenseMapInfo<Instruction*>::getTombstoneKey(); 00204 } 00205 00206 static bool canHandle(Instruction *Inst) { 00207 // Don't value number anything that returns void. 00208 if (Inst->getType()->isVoidTy()) 00209 return false; 00210 00211 CallInst *CI = dyn_cast<CallInst>(Inst); 00212 if (!CI || !CI->onlyReadsMemory()) 00213 return false; 00214 return true; 00215 } 00216 }; 00217 } 00218 00219 namespace llvm { 00220 template<> struct DenseMapInfo<CallValue> { 00221 static inline CallValue getEmptyKey() { 00222 return DenseMapInfo<Instruction*>::getEmptyKey(); 00223 } 00224 static inline CallValue getTombstoneKey() { 00225 return DenseMapInfo<Instruction*>::getTombstoneKey(); 00226 } 00227 static unsigned getHashValue(CallValue Val); 00228 static bool isEqual(CallValue LHS, CallValue RHS); 00229 }; 00230 } 00231 unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) { 00232 Instruction *Inst = Val.Inst; 00233 // Hash in all of the operands as pointers. 00234 unsigned Res = 0; 00235 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) { 00236 assert(!Inst->getOperand(i)->getType()->isMetadataTy() && 00237 "Cannot value number calls with metadata operands"); 00238 Res ^= getHash(Inst->getOperand(i)) << (i & 0xF); 00239 } 00240 00241 // Mix in the opcode. 00242 return (Res << 1) ^ Inst->getOpcode(); 00243 } 00244 00245 bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) { 00246 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst; 00247 if (LHS.isSentinel() || RHS.isSentinel()) 00248 return LHSI == RHSI; 00249 return LHSI->isIdenticalTo(RHSI); 00250 } 00251 00252 00253 //===----------------------------------------------------------------------===// 00254 // EarlyCSE pass. 00255 //===----------------------------------------------------------------------===// 00256 00257 namespace { 00258 00259 /// EarlyCSE - This pass does a simple depth-first walk over the dominator 00260 /// tree, eliminating trivially redundant instructions and using instsimplify 00261 /// to canonicalize things as it goes. It is intended to be fast and catch 00262 /// obvious cases so that instcombine and other passes are more effective. It 00263 /// is expected that a later pass of GVN will catch the interesting/hard 00264 /// cases. 00265 class EarlyCSE : public FunctionPass { 00266 public: 00267 const DataLayout *DL; 00268 const TargetLibraryInfo *TLI; 00269 DominatorTree *DT; 00270 AssumptionTracker *AT; 00271 typedef RecyclingAllocator<BumpPtrAllocator, 00272 ScopedHashTableVal<SimpleValue, Value*> > AllocatorTy; 00273 typedef ScopedHashTable<SimpleValue, Value*, DenseMapInfo<SimpleValue>, 00274 AllocatorTy> ScopedHTType; 00275 00276 /// AvailableValues - This scoped hash table contains the current values of 00277 /// all of our simple scalar expressions. As we walk down the domtree, we 00278 /// look to see if instructions are in this: if so, we replace them with what 00279 /// we find, otherwise we insert them so that dominated values can succeed in 00280 /// their lookup. 00281 ScopedHTType *AvailableValues; 00282 00283 /// AvailableLoads - This scoped hash table contains the current values 00284 /// of loads. This allows us to get efficient access to dominating loads when 00285 /// we have a fully redundant load. In addition to the most recent load, we 00286 /// keep track of a generation count of the read, which is compared against 00287 /// the current generation count. The current generation count is 00288 /// incremented after every possibly writing memory operation, which ensures 00289 /// that we only CSE loads with other loads that have no intervening store. 00290 typedef RecyclingAllocator<BumpPtrAllocator, 00291 ScopedHashTableVal<Value*, std::pair<Value*, unsigned> > > LoadMapAllocator; 00292 typedef ScopedHashTable<Value*, std::pair<Value*, unsigned>, 00293 DenseMapInfo<Value*>, LoadMapAllocator> LoadHTType; 00294 LoadHTType *AvailableLoads; 00295 00296 /// AvailableCalls - This scoped hash table contains the current values 00297 /// of read-only call values. It uses the same generation count as loads. 00298 typedef ScopedHashTable<CallValue, std::pair<Value*, unsigned> > CallHTType; 00299 CallHTType *AvailableCalls; 00300 00301 /// CurrentGeneration - This is the current generation of the memory value. 00302 unsigned CurrentGeneration; 00303 00304 static char ID; 00305 explicit EarlyCSE() : FunctionPass(ID) { 00306 initializeEarlyCSEPass(*PassRegistry::getPassRegistry()); 00307 } 00308 00309 bool runOnFunction(Function &F) override; 00310 00311 private: 00312 00313 // NodeScope - almost a POD, but needs to call the constructors for the 00314 // scoped hash tables so that a new scope gets pushed on. These are RAII so 00315 // that the scope gets popped when the NodeScope is destroyed. 00316 class NodeScope { 00317 public: 00318 NodeScope(ScopedHTType *availableValues, 00319 LoadHTType *availableLoads, 00320 CallHTType *availableCalls) : 00321 Scope(*availableValues), 00322 LoadScope(*availableLoads), 00323 CallScope(*availableCalls) {} 00324 00325 private: 00326 NodeScope(const NodeScope&) LLVM_DELETED_FUNCTION; 00327 void operator=(const NodeScope&) LLVM_DELETED_FUNCTION; 00328 00329 ScopedHTType::ScopeTy Scope; 00330 LoadHTType::ScopeTy LoadScope; 00331 CallHTType::ScopeTy CallScope; 00332 }; 00333 00334 // StackNode - contains all the needed information to create a stack for 00335 // doing a depth first tranversal of the tree. This includes scopes for 00336 // values, loads, and calls as well as the generation. There is a child 00337 // iterator so that the children do not need to be store spearately. 00338 class StackNode { 00339 public: 00340 StackNode(ScopedHTType *availableValues, 00341 LoadHTType *availableLoads, 00342 CallHTType *availableCalls, 00343 unsigned cg, DomTreeNode *n, 00344 DomTreeNode::iterator child, DomTreeNode::iterator end) : 00345 CurrentGeneration(cg), ChildGeneration(cg), Node(n), 00346 ChildIter(child), EndIter(end), 00347 Scopes(availableValues, availableLoads, availableCalls), 00348 Processed(false) {} 00349 00350 // Accessors. 00351 unsigned currentGeneration() { return CurrentGeneration; } 00352 unsigned childGeneration() { return ChildGeneration; } 00353 void childGeneration(unsigned generation) { ChildGeneration = generation; } 00354 DomTreeNode *node() { return Node; } 00355 DomTreeNode::iterator childIter() { return ChildIter; } 00356 DomTreeNode *nextChild() { 00357 DomTreeNode *child = *ChildIter; 00358 ++ChildIter; 00359 return child; 00360 } 00361 DomTreeNode::iterator end() { return EndIter; } 00362 bool isProcessed() { return Processed; } 00363 void process() { Processed = true; } 00364 00365 private: 00366 StackNode(const StackNode&) LLVM_DELETED_FUNCTION; 00367 void operator=(const StackNode&) LLVM_DELETED_FUNCTION; 00368 00369 // Members. 00370 unsigned CurrentGeneration; 00371 unsigned ChildGeneration; 00372 DomTreeNode *Node; 00373 DomTreeNode::iterator ChildIter; 00374 DomTreeNode::iterator EndIter; 00375 NodeScope Scopes; 00376 bool Processed; 00377 }; 00378 00379 bool processNode(DomTreeNode *Node); 00380 00381 // This transformation requires dominator postdominator info 00382 void getAnalysisUsage(AnalysisUsage &AU) const override { 00383 AU.addRequired<AssumptionTracker>(); 00384 AU.addRequired<DominatorTreeWrapperPass>(); 00385 AU.addRequired<TargetLibraryInfo>(); 00386 AU.setPreservesCFG(); 00387 } 00388 }; 00389 } 00390 00391 char EarlyCSE::ID = 0; 00392 00393 // createEarlyCSEPass - The public interface to this file. 00394 FunctionPass *llvm::createEarlyCSEPass() { 00395 return new EarlyCSE(); 00396 } 00397 00398 INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false) 00399 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) 00400 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 00401 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) 00402 INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false) 00403 00404 bool EarlyCSE::processNode(DomTreeNode *Node) { 00405 BasicBlock *BB = Node->getBlock(); 00406 00407 // If this block has a single predecessor, then the predecessor is the parent 00408 // of the domtree node and all of the live out memory values are still current 00409 // in this block. If this block has multiple predecessors, then they could 00410 // have invalidated the live-out memory values of our parent value. For now, 00411 // just be conservative and invalidate memory if this block has multiple 00412 // predecessors. 00413 if (!BB->getSinglePredecessor()) 00414 ++CurrentGeneration; 00415 00416 /// LastStore - Keep track of the last non-volatile store that we saw... for 00417 /// as long as there in no instruction that reads memory. If we see a store 00418 /// to the same location, we delete the dead store. This zaps trivial dead 00419 /// stores which can occur in bitfield code among other things. 00420 StoreInst *LastStore = nullptr; 00421 00422 bool Changed = false; 00423 00424 // See if any instructions in the block can be eliminated. If so, do it. If 00425 // not, add them to AvailableValues. 00426 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 00427 Instruction *Inst = I++; 00428 00429 // Dead instructions should just be removed. 00430 if (isInstructionTriviallyDead(Inst, TLI)) { 00431 DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n'); 00432 Inst->eraseFromParent(); 00433 Changed = true; 00434 ++NumSimplify; 00435 continue; 00436 } 00437 00438 // If the instruction can be simplified (e.g. X+0 = X) then replace it with 00439 // its simpler value. 00440 if (Value *V = SimplifyInstruction(Inst, DL, TLI, DT, AT)) { 00441 DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n'); 00442 Inst->replaceAllUsesWith(V); 00443 Inst->eraseFromParent(); 00444 Changed = true; 00445 ++NumSimplify; 00446 continue; 00447 } 00448 00449 // If this is a simple instruction that we can value number, process it. 00450 if (SimpleValue::canHandle(Inst)) { 00451 // See if the instruction has an available value. If so, use it. 00452 if (Value *V = AvailableValues->lookup(Inst)) { 00453 DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n'); 00454 Inst->replaceAllUsesWith(V); 00455 Inst->eraseFromParent(); 00456 Changed = true; 00457 ++NumCSE; 00458 continue; 00459 } 00460 00461 // Otherwise, just remember that this value is available. 00462 AvailableValues->insert(Inst, Inst); 00463 continue; 00464 } 00465 00466 // If this is a non-volatile load, process it. 00467 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 00468 // Ignore volatile loads. 00469 if (!LI->isSimple()) { 00470 LastStore = nullptr; 00471 continue; 00472 } 00473 00474 // If we have an available version of this load, and if it is the right 00475 // generation, replace this instruction. 00476 std::pair<Value*, unsigned> InVal = 00477 AvailableLoads->lookup(Inst->getOperand(0)); 00478 if (InVal.first != nullptr && InVal.second == CurrentGeneration) { 00479 DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst << " to: " 00480 << *InVal.first << '\n'); 00481 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first); 00482 Inst->eraseFromParent(); 00483 Changed = true; 00484 ++NumCSELoad; 00485 continue; 00486 } 00487 00488 // Otherwise, remember that we have this instruction. 00489 AvailableLoads->insert(Inst->getOperand(0), 00490 std::pair<Value*, unsigned>(Inst, CurrentGeneration)); 00491 LastStore = nullptr; 00492 continue; 00493 } 00494 00495 // If this instruction may read from memory, forget LastStore. 00496 if (Inst->mayReadFromMemory()) 00497 LastStore = nullptr; 00498 00499 // If this is a read-only call, process it. 00500 if (CallValue::canHandle(Inst)) { 00501 // If we have an available version of this call, and if it is the right 00502 // generation, replace this instruction. 00503 std::pair<Value*, unsigned> InVal = AvailableCalls->lookup(Inst); 00504 if (InVal.first != nullptr && InVal.second == CurrentGeneration) { 00505 DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst << " to: " 00506 << *InVal.first << '\n'); 00507 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first); 00508 Inst->eraseFromParent(); 00509 Changed = true; 00510 ++NumCSECall; 00511 continue; 00512 } 00513 00514 // Otherwise, remember that we have this instruction. 00515 AvailableCalls->insert(Inst, 00516 std::pair<Value*, unsigned>(Inst, CurrentGeneration)); 00517 continue; 00518 } 00519 00520 // Okay, this isn't something we can CSE at all. Check to see if it is 00521 // something that could modify memory. If so, our available memory values 00522 // cannot be used so bump the generation count. 00523 if (Inst->mayWriteToMemory()) { 00524 ++CurrentGeneration; 00525 00526 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 00527 // We do a trivial form of DSE if there are two stores to the same 00528 // location with no intervening loads. Delete the earlier store. 00529 if (LastStore && 00530 LastStore->getPointerOperand() == SI->getPointerOperand()) { 00531 DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore << " due to: " 00532 << *Inst << '\n'); 00533 LastStore->eraseFromParent(); 00534 Changed = true; 00535 ++NumDSE; 00536 LastStore = nullptr; 00537 continue; 00538 } 00539 00540 // Okay, we just invalidated anything we knew about loaded values. Try 00541 // to salvage *something* by remembering that the stored value is a live 00542 // version of the pointer. It is safe to forward from volatile stores 00543 // to non-volatile loads, so we don't have to check for volatility of 00544 // the store. 00545 AvailableLoads->insert(SI->getPointerOperand(), 00546 std::pair<Value*, unsigned>(SI->getValueOperand(), CurrentGeneration)); 00547 00548 // Remember that this was the last store we saw for DSE. 00549 if (SI->isSimple()) 00550 LastStore = SI; 00551 } 00552 } 00553 } 00554 00555 return Changed; 00556 } 00557 00558 00559 bool EarlyCSE::runOnFunction(Function &F) { 00560 if (skipOptnoneFunction(F)) 00561 return false; 00562 00563 std::vector<StackNode *> nodesToProcess; 00564 00565 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 00566 DL = DLP ? &DLP->getDataLayout() : nullptr; 00567 TLI = &getAnalysis<TargetLibraryInfo>(); 00568 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 00569 AT = &getAnalysis<AssumptionTracker>(); 00570 00571 // Tables that the pass uses when walking the domtree. 00572 ScopedHTType AVTable; 00573 AvailableValues = &AVTable; 00574 LoadHTType LoadTable; 00575 AvailableLoads = &LoadTable; 00576 CallHTType CallTable; 00577 AvailableCalls = &CallTable; 00578 00579 CurrentGeneration = 0; 00580 bool Changed = false; 00581 00582 // Process the root node. 00583 nodesToProcess.push_back( 00584 new StackNode(AvailableValues, AvailableLoads, AvailableCalls, 00585 CurrentGeneration, DT->getRootNode(), 00586 DT->getRootNode()->begin(), 00587 DT->getRootNode()->end())); 00588 00589 // Save the current generation. 00590 unsigned LiveOutGeneration = CurrentGeneration; 00591 00592 // Process the stack. 00593 while (!nodesToProcess.empty()) { 00594 // Grab the first item off the stack. Set the current generation, remove 00595 // the node from the stack, and process it. 00596 StackNode *NodeToProcess = nodesToProcess.back(); 00597 00598 // Initialize class members. 00599 CurrentGeneration = NodeToProcess->currentGeneration(); 00600 00601 // Check if the node needs to be processed. 00602 if (!NodeToProcess->isProcessed()) { 00603 // Process the node. 00604 Changed |= processNode(NodeToProcess->node()); 00605 NodeToProcess->childGeneration(CurrentGeneration); 00606 NodeToProcess->process(); 00607 } else if (NodeToProcess->childIter() != NodeToProcess->end()) { 00608 // Push the next child onto the stack. 00609 DomTreeNode *child = NodeToProcess->nextChild(); 00610 nodesToProcess.push_back( 00611 new StackNode(AvailableValues, 00612 AvailableLoads, 00613 AvailableCalls, 00614 NodeToProcess->childGeneration(), child, 00615 child->begin(), child->end())); 00616 } else { 00617 // It has been processed, and there are no more children to process, 00618 // so delete it and pop it off the stack. 00619 delete NodeToProcess; 00620 nodesToProcess.pop_back(); 00621 } 00622 } // while (!nodes...) 00623 00624 // Reset the current generation. 00625 CurrentGeneration = LiveOutGeneration; 00626 00627 return Changed; 00628 }