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&); // DO NOT IMPLEMENT
279 ScopedHTType::ScopeTy Scope;
280 LoadHTType::ScopeTy LoadScope;
281 CallHTType::ScopeTy CallScope;
284 // StackNode - contains all the needed information to create a stack for
285 // doing a depth first tranversal of the tree. This includes scopes for
286 // values, loads, and calls as well as the generation. There is a child
287 // iterator so that the children do not need to be store spearately.
290 StackNode(ScopedHTType *availableValues,
291 LoadHTType *availableLoads,
292 CallHTType *availableCalls,
293 unsigned cg, DomTreeNode *n,
294 DomTreeNode::iterator child, DomTreeNode::iterator end) :
295 CurrentGeneration(cg), ChildGeneration(cg), Node(n),
296 ChildIter(child), EndIter(end),
297 Scopes(availableValues, availableLoads, availableCalls),
301 unsigned currentGeneration() { return CurrentGeneration; }
302 unsigned childGeneration() { return ChildGeneration; }
303 void childGeneration(unsigned generation) { ChildGeneration = generation; }
304 DomTreeNode *node() { return Node; }
305 DomTreeNode::iterator childIter() { return ChildIter; }
306 DomTreeNode *nextChild() {
307 DomTreeNode *child = *ChildIter;
311 DomTreeNode::iterator end() { return EndIter; }
312 bool isProcessed() { return Processed; }
313 void process() { Processed = true; }
316 StackNode(const StackNode&); // DO NOT IMPLEMENT
319 unsigned CurrentGeneration;
320 unsigned ChildGeneration;
322 DomTreeNode::iterator ChildIter;
323 DomTreeNode::iterator EndIter;
328 bool processNode(DomTreeNode *Node);
330 // This transformation requires dominator postdominator info
331 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
332 AU.addRequired<DominatorTree>();
333 AU.addRequired<TargetLibraryInfo>();
334 AU.setPreservesCFG();
339 char EarlyCSE::ID = 0;
341 // createEarlyCSEPass - The public interface to this file.
342 FunctionPass *llvm::createEarlyCSEPass() {
343 return new EarlyCSE();
346 INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false)
347 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
348 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
349 INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
351 bool EarlyCSE::processNode(DomTreeNode *Node) {
352 BasicBlock *BB = Node->getBlock();
354 // If this block has a single predecessor, then the predecessor is the parent
355 // of the domtree node and all of the live out memory values are still current
356 // in this block. If this block has multiple predecessors, then they could
357 // have invalidated the live-out memory values of our parent value. For now,
358 // just be conservative and invalidate memory if this block has multiple
360 if (BB->getSinglePredecessor() == 0)
363 /// LastStore - Keep track of the last non-volatile store that we saw... for
364 /// as long as there in no instruction that reads memory. If we see a store
365 /// to the same location, we delete the dead store. This zaps trivial dead
366 /// stores which can occur in bitfield code among other things.
367 StoreInst *LastStore = 0;
369 bool Changed = false;
371 // See if any instructions in the block can be eliminated. If so, do it. If
372 // not, add them to AvailableValues.
373 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
374 Instruction *Inst = I++;
376 // Dead instructions should just be removed.
377 if (isInstructionTriviallyDead(Inst)) {
378 DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n');
379 Inst->eraseFromParent();
385 // If the instruction can be simplified (e.g. X+0 = X) then replace it with
386 // its simpler value.
387 if (Value *V = SimplifyInstruction(Inst, TD, TLI, DT)) {
388 DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
389 Inst->replaceAllUsesWith(V);
390 Inst->eraseFromParent();
396 // If this is a simple instruction that we can value number, process it.
397 if (SimpleValue::canHandle(Inst)) {
398 // See if the instruction has an available value. If so, use it.
399 if (Value *V = AvailableValues->lookup(Inst)) {
400 DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
401 Inst->replaceAllUsesWith(V);
402 Inst->eraseFromParent();
408 // Otherwise, just remember that this value is available.
409 AvailableValues->insert(Inst, Inst);
413 // If this is a non-volatile load, process it.
414 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
415 // Ignore volatile loads.
416 if (!LI->isSimple()) {
421 // If we have an available version of this load, and if it is the right
422 // generation, replace this instruction.
423 std::pair<Value*, unsigned> InVal =
424 AvailableLoads->lookup(Inst->getOperand(0));
425 if (InVal.first != 0 && InVal.second == CurrentGeneration) {
426 DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst << " to: "
427 << *InVal.first << '\n');
428 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
429 Inst->eraseFromParent();
435 // Otherwise, remember that we have this instruction.
436 AvailableLoads->insert(Inst->getOperand(0),
437 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
442 // If this instruction may read from memory, forget LastStore.
443 if (Inst->mayReadFromMemory())
446 // If this is a read-only call, process it.
447 if (CallValue::canHandle(Inst)) {
448 // If we have an available version of this call, and if it is the right
449 // generation, replace this instruction.
450 std::pair<Value*, unsigned> InVal = AvailableCalls->lookup(Inst);
451 if (InVal.first != 0 && InVal.second == CurrentGeneration) {
452 DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst << " to: "
453 << *InVal.first << '\n');
454 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
455 Inst->eraseFromParent();
461 // Otherwise, remember that we have this instruction.
462 AvailableCalls->insert(Inst,
463 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
467 // Okay, this isn't something we can CSE at all. Check to see if it is
468 // something that could modify memory. If so, our available memory values
469 // cannot be used so bump the generation count.
470 if (Inst->mayWriteToMemory()) {
473 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
474 // We do a trivial form of DSE if there are two stores to the same
475 // location with no intervening loads. Delete the earlier store.
477 LastStore->getPointerOperand() == SI->getPointerOperand()) {
478 DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore << " due to: "
480 LastStore->eraseFromParent();
487 // Okay, we just invalidated anything we knew about loaded values. Try
488 // to salvage *something* by remembering that the stored value is a live
489 // version of the pointer. It is safe to forward from volatile stores
490 // to non-volatile loads, so we don't have to check for volatility of
492 AvailableLoads->insert(SI->getPointerOperand(),
493 std::pair<Value*, unsigned>(SI->getValueOperand(), CurrentGeneration));
495 // Remember that this was the last store we saw for DSE.
506 bool EarlyCSE::runOnFunction(Function &F) {
507 std::deque<StackNode *> nodesToProcess;
509 TD = getAnalysisIfAvailable<TargetData>();
510 TLI = &getAnalysis<TargetLibraryInfo>();
511 DT = &getAnalysis<DominatorTree>();
513 // Tables that the pass uses when walking the domtree.
514 ScopedHTType AVTable;
515 AvailableValues = &AVTable;
516 LoadHTType LoadTable;
517 AvailableLoads = &LoadTable;
518 CallHTType CallTable;
519 AvailableCalls = &CallTable;
521 CurrentGeneration = 0;
522 bool Changed = false;
524 // Process the root node.
525 nodesToProcess.push_front(
526 new StackNode(AvailableValues, AvailableLoads, AvailableCalls,
527 CurrentGeneration, DT->getRootNode(),
528 DT->getRootNode()->begin(),
529 DT->getRootNode()->end()));
531 // Save the current generation.
532 unsigned LiveOutGeneration = CurrentGeneration;
534 // Process the stack.
535 while (!nodesToProcess.empty()) {
536 // Grab the first item off the stack. Set the current generation, remove
537 // the node from the stack, and process it.
538 StackNode *NodeToProcess = nodesToProcess.front();
540 // Initialize class members.
541 CurrentGeneration = NodeToProcess->currentGeneration();
543 // Check if the node needs to be processed.
544 if (!NodeToProcess->isProcessed()) {
546 Changed |= processNode(NodeToProcess->node());
547 NodeToProcess->childGeneration(CurrentGeneration);
548 NodeToProcess->process();
549 } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
550 // Push the next child onto the stack.
551 DomTreeNode *child = NodeToProcess->nextChild();
552 nodesToProcess.push_front(
553 new StackNode(AvailableValues,
556 NodeToProcess->childGeneration(), child,
557 child->begin(), child->end()));
559 // It has been processed, and there are no more children to process,
560 // so delete it and pop it off the stack.
561 delete NodeToProcess;
562 nodesToProcess.pop_front();
564 } // while (!nodes...)
566 // Reset the current generation.
567 CurrentGeneration = LiveOutGeneration;