1 //===- EarlyCSE.cpp - Simple and fast CSE pass ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs a simple dominator tree walk that eliminates trivially
11 // redundant instructions.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "early-cse"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Analysis/Dominators.h"
20 #include "llvm/Analysis/InstructionSimplify.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetLibraryInfo.h"
23 #include "llvm/Transforms/Utils/Local.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/RecyclingAllocator.h"
26 #include "llvm/ADT/ScopedHashTable.h"
27 #include "llvm/ADT/Statistic.h"
31 STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd");
32 STATISTIC(NumCSE, "Number of instructions CSE'd");
33 STATISTIC(NumCSELoad, "Number of load instructions CSE'd");
34 STATISTIC(NumCSECall, "Number of call instructions CSE'd");
35 STATISTIC(NumDSE, "Number of trivial dead stores removed");
37 static unsigned getHash(const void *V) {
38 return DenseMapInfo<const void*>::getHashValue(V);
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
46 /// SimpleValue - Instances of this struct represent available values in the
47 /// scoped hash table.
51 SimpleValue(Instruction *I) : Inst(I) {
52 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
55 bool isSentinel() const {
56 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() ||
57 Inst == DenseMapInfo<Instruction*>::getTombstoneKey();
60 static bool canHandle(Instruction *Inst) {
61 // This can only handle non-void readnone functions.
62 if (CallInst *CI = dyn_cast<CallInst>(Inst))
63 return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy();
64 return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) ||
65 isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) ||
66 isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) ||
67 isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) ||
68 isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst);
74 // SimpleValue is POD.
75 template<> struct isPodLike<SimpleValue> {
76 static const bool value = true;
79 template<> struct DenseMapInfo<SimpleValue> {
80 static inline SimpleValue getEmptyKey() {
81 return DenseMapInfo<Instruction*>::getEmptyKey();
83 static inline SimpleValue getTombstoneKey() {
84 return DenseMapInfo<Instruction*>::getTombstoneKey();
86 static unsigned getHashValue(SimpleValue Val);
87 static bool isEqual(SimpleValue LHS, SimpleValue RHS);
91 unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
92 Instruction *Inst = Val.Inst;
94 // Hash in all of the operands as pointers.
96 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
97 Res ^= getHash(Inst->getOperand(i)) << (i & 0xF);
99 if (CastInst *CI = dyn_cast<CastInst>(Inst))
100 Res ^= getHash(CI->getType());
101 else if (CmpInst *CI = dyn_cast<CmpInst>(Inst))
102 Res ^= CI->getPredicate();
103 else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst)) {
104 for (ExtractValueInst::idx_iterator I = EVI->idx_begin(),
105 E = EVI->idx_end(); I != E; ++I)
107 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst)) {
108 for (InsertValueInst::idx_iterator I = IVI->idx_begin(),
109 E = IVI->idx_end(); I != E; ++I)
112 // nothing extra to hash in.
113 assert((isa<CallInst>(Inst) ||
114 isa<BinaryOperator>(Inst) || isa<GetElementPtrInst>(Inst) ||
115 isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) ||
116 isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst)) &&
117 "Invalid/unknown instruction");
120 // Mix in the opcode.
121 return (Res << 1) ^ Inst->getOpcode();
124 bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
125 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
127 if (LHS.isSentinel() || RHS.isSentinel())
130 if (LHSI->getOpcode() != RHSI->getOpcode()) return false;
131 return LHSI->isIdenticalTo(RHSI);
134 //===----------------------------------------------------------------------===//
136 //===----------------------------------------------------------------------===//
139 /// CallValue - Instances of this struct represent available call values in
140 /// the scoped hash table.
144 CallValue(Instruction *I) : Inst(I) {
145 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
148 bool isSentinel() const {
149 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() ||
150 Inst == DenseMapInfo<Instruction*>::getTombstoneKey();
153 static bool canHandle(Instruction *Inst) {
154 // Don't value number anything that returns void.
155 if (Inst->getType()->isVoidTy())
158 CallInst *CI = dyn_cast<CallInst>(Inst);
159 if (CI == 0 || !CI->onlyReadsMemory())
168 template<> struct isPodLike<CallValue> {
169 static const bool value = true;
172 template<> struct DenseMapInfo<CallValue> {
173 static inline CallValue getEmptyKey() {
174 return DenseMapInfo<Instruction*>::getEmptyKey();
176 static inline CallValue getTombstoneKey() {
177 return DenseMapInfo<Instruction*>::getTombstoneKey();
179 static unsigned getHashValue(CallValue Val);
180 static bool isEqual(CallValue LHS, CallValue RHS);
183 unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) {
184 Instruction *Inst = Val.Inst;
185 // Hash in all of the operands as pointers.
187 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) {
188 assert(!Inst->getOperand(i)->getType()->isMetadataTy() &&
189 "Cannot value number calls with metadata operands");
190 Res ^= getHash(Inst->getOperand(i)) << (i & 0xF);
193 // Mix in the opcode.
194 return (Res << 1) ^ Inst->getOpcode();
197 bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
198 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
199 if (LHS.isSentinel() || RHS.isSentinel())
201 return LHSI->isIdenticalTo(RHSI);
205 //===----------------------------------------------------------------------===//
207 //===----------------------------------------------------------------------===//
211 /// EarlyCSE - This pass does a simple depth-first walk over the dominator
212 /// tree, eliminating trivially redundant instructions and using instsimplify
213 /// to canonicalize things as it goes. It is intended to be fast and catch
214 /// obvious cases so that instcombine and other passes are more effective. It
215 /// is expected that a later pass of GVN will catch the interesting/hard
217 class EarlyCSE : public FunctionPass {
219 const TargetData *TD;
220 const TargetLibraryInfo *TLI;
222 typedef RecyclingAllocator<BumpPtrAllocator,
223 ScopedHashTableVal<SimpleValue, Value*> > AllocatorTy;
224 typedef ScopedHashTable<SimpleValue, Value*, DenseMapInfo<SimpleValue>,
225 AllocatorTy> ScopedHTType;
227 /// AvailableValues - This scoped hash table contains the current values of
228 /// all of our simple scalar expressions. As we walk down the domtree, we
229 /// look to see if instructions are in this: if so, we replace them with what
230 /// we find, otherwise we insert them so that dominated values can succeed in
232 ScopedHTType *AvailableValues;
234 /// AvailableLoads - This scoped hash table contains the current values
235 /// of loads. This allows us to get efficient access to dominating loads when
236 /// we have a fully redundant load. In addition to the most recent load, we
237 /// keep track of a generation count of the read, which is compared against
238 /// the current generation count. The current generation count is
239 /// incremented after every possibly writing memory operation, which ensures
240 /// that we only CSE loads with other loads that have no intervening store.
241 typedef RecyclingAllocator<BumpPtrAllocator,
242 ScopedHashTableVal<Value*, std::pair<Value*, unsigned> > > LoadMapAllocator;
243 typedef ScopedHashTable<Value*, std::pair<Value*, unsigned>,
244 DenseMapInfo<Value*>, LoadMapAllocator> LoadHTType;
245 LoadHTType *AvailableLoads;
247 /// AvailableCalls - This scoped hash table contains the current values
248 /// of read-only call values. It uses the same generation count as loads.
249 typedef ScopedHashTable<CallValue, std::pair<Value*, unsigned> > CallHTType;
250 CallHTType *AvailableCalls;
252 /// CurrentGeneration - This is the current generation of the memory value.
253 unsigned CurrentGeneration;
256 explicit EarlyCSE() : FunctionPass(ID) {
257 initializeEarlyCSEPass(*PassRegistry::getPassRegistry());
260 bool runOnFunction(Function &F);
264 // NodeScope - almost a POD, but needs to call the constructors for the
265 // scoped hash tables so that a new scope gets pushed on. These are RAII so
266 // that the scope gets popped when the NodeScope is destroyed.
269 NodeScope(ScopedHTType *availableValues,
270 LoadHTType *availableLoads,
271 CallHTType *availableCalls) :
272 Scope(*availableValues),
273 LoadScope(*availableLoads),
274 CallScope(*availableCalls) {}
277 NodeScope(const NodeScope&) LLVM_DELETED_FUNCTION;
278 void operator=(const NodeScope&) LLVM_DELETED_FUNCTION;
280 ScopedHTType::ScopeTy Scope;
281 LoadHTType::ScopeTy LoadScope;
282 CallHTType::ScopeTy CallScope;
285 // StackNode - contains all the needed information to create a stack for
286 // doing a depth first tranversal of the tree. This includes scopes for
287 // values, loads, and calls as well as the generation. There is a child
288 // iterator so that the children do not need to be store spearately.
291 StackNode(ScopedHTType *availableValues,
292 LoadHTType *availableLoads,
293 CallHTType *availableCalls,
294 unsigned cg, DomTreeNode *n,
295 DomTreeNode::iterator child, DomTreeNode::iterator end) :
296 CurrentGeneration(cg), ChildGeneration(cg), Node(n),
297 ChildIter(child), EndIter(end),
298 Scopes(availableValues, availableLoads, availableCalls),
302 unsigned currentGeneration() { return CurrentGeneration; }
303 unsigned childGeneration() { return ChildGeneration; }
304 void childGeneration(unsigned generation) { ChildGeneration = generation; }
305 DomTreeNode *node() { return Node; }
306 DomTreeNode::iterator childIter() { return ChildIter; }
307 DomTreeNode *nextChild() {
308 DomTreeNode *child = *ChildIter;
312 DomTreeNode::iterator end() { return EndIter; }
313 bool isProcessed() { return Processed; }
314 void process() { Processed = true; }
317 StackNode(const StackNode&) LLVM_DELETED_FUNCTION;
318 void operator=(const StackNode&) LLVM_DELETED_FUNCTION;
321 unsigned CurrentGeneration;
322 unsigned ChildGeneration;
324 DomTreeNode::iterator ChildIter;
325 DomTreeNode::iterator EndIter;
330 bool processNode(DomTreeNode *Node);
332 // This transformation requires dominator postdominator info
333 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
334 AU.addRequired<DominatorTree>();
335 AU.addRequired<TargetLibraryInfo>();
336 AU.setPreservesCFG();
341 char EarlyCSE::ID = 0;
343 // createEarlyCSEPass - The public interface to this file.
344 FunctionPass *llvm::createEarlyCSEPass() {
345 return new EarlyCSE();
348 INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false)
349 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
350 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
351 INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
353 bool EarlyCSE::processNode(DomTreeNode *Node) {
354 BasicBlock *BB = Node->getBlock();
356 // If this block has a single predecessor, then the predecessor is the parent
357 // of the domtree node and all of the live out memory values are still current
358 // in this block. If this block has multiple predecessors, then they could
359 // have invalidated the live-out memory values of our parent value. For now,
360 // just be conservative and invalidate memory if this block has multiple
362 if (BB->getSinglePredecessor() == 0)
365 /// LastStore - Keep track of the last non-volatile store that we saw... for
366 /// as long as there in no instruction that reads memory. If we see a store
367 /// to the same location, we delete the dead store. This zaps trivial dead
368 /// stores which can occur in bitfield code among other things.
369 StoreInst *LastStore = 0;
371 bool Changed = false;
373 // See if any instructions in the block can be eliminated. If so, do it. If
374 // not, add them to AvailableValues.
375 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
376 Instruction *Inst = I++;
378 // Dead instructions should just be removed.
379 if (isInstructionTriviallyDead(Inst, TLI)) {
380 DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n');
381 Inst->eraseFromParent();
387 // If the instruction can be simplified (e.g. X+0 = X) then replace it with
388 // its simpler value.
389 if (Value *V = SimplifyInstruction(Inst, TD, TLI, DT)) {
390 DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
391 Inst->replaceAllUsesWith(V);
392 Inst->eraseFromParent();
398 // If this is a simple instruction that we can value number, process it.
399 if (SimpleValue::canHandle(Inst)) {
400 // See if the instruction has an available value. If so, use it.
401 if (Value *V = AvailableValues->lookup(Inst)) {
402 DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
403 Inst->replaceAllUsesWith(V);
404 Inst->eraseFromParent();
410 // Otherwise, just remember that this value is available.
411 AvailableValues->insert(Inst, Inst);
415 // If this is a non-volatile load, process it.
416 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
417 // Ignore volatile loads.
418 if (!LI->isSimple()) {
423 // If we have an available version of this load, and if it is the right
424 // generation, replace this instruction.
425 std::pair<Value*, unsigned> InVal =
426 AvailableLoads->lookup(Inst->getOperand(0));
427 if (InVal.first != 0 && InVal.second == CurrentGeneration) {
428 DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst << " to: "
429 << *InVal.first << '\n');
430 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
431 Inst->eraseFromParent();
437 // Otherwise, remember that we have this instruction.
438 AvailableLoads->insert(Inst->getOperand(0),
439 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
444 // If this instruction may read from memory, forget LastStore.
445 if (Inst->mayReadFromMemory())
448 // If this is a read-only call, process it.
449 if (CallValue::canHandle(Inst)) {
450 // If we have an available version of this call, and if it is the right
451 // generation, replace this instruction.
452 std::pair<Value*, unsigned> InVal = AvailableCalls->lookup(Inst);
453 if (InVal.first != 0 && InVal.second == CurrentGeneration) {
454 DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst << " to: "
455 << *InVal.first << '\n');
456 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
457 Inst->eraseFromParent();
463 // Otherwise, remember that we have this instruction.
464 AvailableCalls->insert(Inst,
465 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
469 // Okay, this isn't something we can CSE at all. Check to see if it is
470 // something that could modify memory. If so, our available memory values
471 // cannot be used so bump the generation count.
472 if (Inst->mayWriteToMemory()) {
475 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
476 // We do a trivial form of DSE if there are two stores to the same
477 // location with no intervening loads. Delete the earlier store.
479 LastStore->getPointerOperand() == SI->getPointerOperand()) {
480 DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore << " due to: "
482 LastStore->eraseFromParent();
489 // Okay, we just invalidated anything we knew about loaded values. Try
490 // to salvage *something* by remembering that the stored value is a live
491 // version of the pointer. It is safe to forward from volatile stores
492 // to non-volatile loads, so we don't have to check for volatility of
494 AvailableLoads->insert(SI->getPointerOperand(),
495 std::pair<Value*, unsigned>(SI->getValueOperand(), CurrentGeneration));
497 // Remember that this was the last store we saw for DSE.
508 bool EarlyCSE::runOnFunction(Function &F) {
509 std::deque<StackNode *> nodesToProcess;
511 TD = getAnalysisIfAvailable<TargetData>();
512 TLI = &getAnalysis<TargetLibraryInfo>();
513 DT = &getAnalysis<DominatorTree>();
515 // Tables that the pass uses when walking the domtree.
516 ScopedHTType AVTable;
517 AvailableValues = &AVTable;
518 LoadHTType LoadTable;
519 AvailableLoads = &LoadTable;
520 CallHTType CallTable;
521 AvailableCalls = &CallTable;
523 CurrentGeneration = 0;
524 bool Changed = false;
526 // Process the root node.
527 nodesToProcess.push_front(
528 new StackNode(AvailableValues, AvailableLoads, AvailableCalls,
529 CurrentGeneration, DT->getRootNode(),
530 DT->getRootNode()->begin(),
531 DT->getRootNode()->end()));
533 // Save the current generation.
534 unsigned LiveOutGeneration = CurrentGeneration;
536 // Process the stack.
537 while (!nodesToProcess.empty()) {
538 // Grab the first item off the stack. Set the current generation, remove
539 // the node from the stack, and process it.
540 StackNode *NodeToProcess = nodesToProcess.front();
542 // Initialize class members.
543 CurrentGeneration = NodeToProcess->currentGeneration();
545 // Check if the node needs to be processed.
546 if (!NodeToProcess->isProcessed()) {
548 Changed |= processNode(NodeToProcess->node());
549 NodeToProcess->childGeneration(CurrentGeneration);
550 NodeToProcess->process();
551 } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
552 // Push the next child onto the stack.
553 DomTreeNode *child = NodeToProcess->nextChild();
554 nodesToProcess.push_front(
555 new StackNode(AvailableValues,
558 NodeToProcess->childGeneration(), child,
559 child->begin(), child->end()));
561 // It has been processed, and there are no more children to process,
562 // so delete it and pop it off the stack.
563 delete NodeToProcess;
564 nodesToProcess.pop_front();
566 } // while (!nodes...)
568 // Reset the current generation.
569 CurrentGeneration = LiveOutGeneration;