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 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/ADT/Hashing.h"
17 #include "llvm/ADT/ScopedHashTable.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/AssumptionTracker.h"
20 #include "llvm/Analysis/InstructionSimplify.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/RecyclingAllocator.h"
27 #include "llvm/Target/TargetLibraryInfo.h"
28 #include "llvm/Transforms/Utils/Local.h"
32 #define DEBUG_TYPE "early-cse"
34 STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd");
35 STATISTIC(NumCSE, "Number of instructions CSE'd");
36 STATISTIC(NumCSELoad, "Number of load instructions CSE'd");
37 STATISTIC(NumCSECall, "Number of call instructions CSE'd");
38 STATISTIC(NumDSE, "Number of trivial dead stores removed");
40 static unsigned getHash(const void *V) {
41 return DenseMapInfo<const void*>::getHashValue(V);
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
49 /// SimpleValue - Instances of this struct represent available values in the
50 /// scoped hash table.
54 SimpleValue(Instruction *I) : Inst(I) {
55 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
58 bool isSentinel() const {
59 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() ||
60 Inst == DenseMapInfo<Instruction*>::getTombstoneKey();
63 static bool canHandle(Instruction *Inst) {
64 // This can only handle non-void readnone functions.
65 if (CallInst *CI = dyn_cast<CallInst>(Inst))
66 return CI->doesNotAccessMemory() && !CI->getType()->isVoidTy();
67 return isa<CastInst>(Inst) || isa<BinaryOperator>(Inst) ||
68 isa<GetElementPtrInst>(Inst) || isa<CmpInst>(Inst) ||
69 isa<SelectInst>(Inst) || isa<ExtractElementInst>(Inst) ||
70 isa<InsertElementInst>(Inst) || isa<ShuffleVectorInst>(Inst) ||
71 isa<ExtractValueInst>(Inst) || isa<InsertValueInst>(Inst);
77 template<> struct DenseMapInfo<SimpleValue> {
78 static inline SimpleValue getEmptyKey() {
79 return DenseMapInfo<Instruction*>::getEmptyKey();
81 static inline SimpleValue getTombstoneKey() {
82 return DenseMapInfo<Instruction*>::getTombstoneKey();
84 static unsigned getHashValue(SimpleValue Val);
85 static bool isEqual(SimpleValue LHS, SimpleValue RHS);
89 unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
90 Instruction *Inst = Val.Inst;
91 // Hash in all of the operands as pointers.
92 if (BinaryOperator* BinOp = dyn_cast<BinaryOperator>(Inst)) {
93 Value *LHS = BinOp->getOperand(0);
94 Value *RHS = BinOp->getOperand(1);
95 if (BinOp->isCommutative() && BinOp->getOperand(0) > BinOp->getOperand(1))
98 if (isa<OverflowingBinaryOperator>(BinOp)) {
99 // Hash the overflow behavior
101 BinOp->hasNoSignedWrap() * OverflowingBinaryOperator::NoSignedWrap |
102 BinOp->hasNoUnsignedWrap() * OverflowingBinaryOperator::NoUnsignedWrap;
103 return hash_combine(BinOp->getOpcode(), Overflow, LHS, RHS);
106 return hash_combine(BinOp->getOpcode(), LHS, RHS);
109 if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) {
110 Value *LHS = CI->getOperand(0);
111 Value *RHS = CI->getOperand(1);
112 CmpInst::Predicate Pred = CI->getPredicate();
113 if (Inst->getOperand(0) > Inst->getOperand(1)) {
115 Pred = CI->getSwappedPredicate();
117 return hash_combine(Inst->getOpcode(), Pred, LHS, RHS);
120 if (CastInst *CI = dyn_cast<CastInst>(Inst))
121 return hash_combine(CI->getOpcode(), CI->getType(), CI->getOperand(0));
123 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Inst))
124 return hash_combine(EVI->getOpcode(), EVI->getOperand(0),
125 hash_combine_range(EVI->idx_begin(), EVI->idx_end()));
127 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(Inst))
128 return hash_combine(IVI->getOpcode(), IVI->getOperand(0),
130 hash_combine_range(IVI->idx_begin(), IVI->idx_end()));
132 assert((isa<CallInst>(Inst) || isa<BinaryOperator>(Inst) ||
133 isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) ||
134 isa<ExtractElementInst>(Inst) || isa<InsertElementInst>(Inst) ||
135 isa<ShuffleVectorInst>(Inst)) && "Invalid/unknown instruction");
137 // Mix in the opcode.
138 return hash_combine(Inst->getOpcode(),
139 hash_combine_range(Inst->value_op_begin(),
140 Inst->value_op_end()));
143 bool DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS, SimpleValue RHS) {
144 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
146 if (LHS.isSentinel() || RHS.isSentinel())
149 if (LHSI->getOpcode() != RHSI->getOpcode()) return false;
150 if (LHSI->isIdenticalTo(RHSI)) return true;
152 // If we're not strictly identical, we still might be a commutable instruction
153 if (BinaryOperator *LHSBinOp = dyn_cast<BinaryOperator>(LHSI)) {
154 if (!LHSBinOp->isCommutative())
157 assert(isa<BinaryOperator>(RHSI)
158 && "same opcode, but different instruction type?");
159 BinaryOperator *RHSBinOp = cast<BinaryOperator>(RHSI);
161 // Check overflow attributes
162 if (isa<OverflowingBinaryOperator>(LHSBinOp)) {
163 assert(isa<OverflowingBinaryOperator>(RHSBinOp)
164 && "same opcode, but different operator type?");
165 if (LHSBinOp->hasNoUnsignedWrap() != RHSBinOp->hasNoUnsignedWrap() ||
166 LHSBinOp->hasNoSignedWrap() != RHSBinOp->hasNoSignedWrap())
171 return LHSBinOp->getOperand(0) == RHSBinOp->getOperand(1) &&
172 LHSBinOp->getOperand(1) == RHSBinOp->getOperand(0);
174 if (CmpInst *LHSCmp = dyn_cast<CmpInst>(LHSI)) {
175 assert(isa<CmpInst>(RHSI)
176 && "same opcode, but different instruction type?");
177 CmpInst *RHSCmp = cast<CmpInst>(RHSI);
179 return LHSCmp->getOperand(0) == RHSCmp->getOperand(1) &&
180 LHSCmp->getOperand(1) == RHSCmp->getOperand(0) &&
181 LHSCmp->getSwappedPredicate() == RHSCmp->getPredicate();
187 //===----------------------------------------------------------------------===//
189 //===----------------------------------------------------------------------===//
192 /// CallValue - Instances of this struct represent available call values in
193 /// the scoped hash table.
197 CallValue(Instruction *I) : Inst(I) {
198 assert((isSentinel() || canHandle(I)) && "Inst can't be handled!");
201 bool isSentinel() const {
202 return Inst == DenseMapInfo<Instruction*>::getEmptyKey() ||
203 Inst == DenseMapInfo<Instruction*>::getTombstoneKey();
206 static bool canHandle(Instruction *Inst) {
207 // Don't value number anything that returns void.
208 if (Inst->getType()->isVoidTy())
211 CallInst *CI = dyn_cast<CallInst>(Inst);
212 if (!CI || !CI->onlyReadsMemory())
220 template<> struct DenseMapInfo<CallValue> {
221 static inline CallValue getEmptyKey() {
222 return DenseMapInfo<Instruction*>::getEmptyKey();
224 static inline CallValue getTombstoneKey() {
225 return DenseMapInfo<Instruction*>::getTombstoneKey();
227 static unsigned getHashValue(CallValue Val);
228 static bool isEqual(CallValue LHS, CallValue RHS);
231 unsigned DenseMapInfo<CallValue>::getHashValue(CallValue Val) {
232 Instruction *Inst = Val.Inst;
233 // Hash in all of the operands as pointers.
235 for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) {
236 assert(!Inst->getOperand(i)->getType()->isMetadataTy() &&
237 "Cannot value number calls with metadata operands");
238 Res ^= getHash(Inst->getOperand(i)) << (i & 0xF);
241 // Mix in the opcode.
242 return (Res << 1) ^ Inst->getOpcode();
245 bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
246 Instruction *LHSI = LHS.Inst, *RHSI = RHS.Inst;
247 if (LHS.isSentinel() || RHS.isSentinel())
249 return LHSI->isIdenticalTo(RHSI);
253 //===----------------------------------------------------------------------===//
255 //===----------------------------------------------------------------------===//
259 /// EarlyCSE - This pass does a simple depth-first walk over the dominator
260 /// tree, eliminating trivially redundant instructions and using instsimplify
261 /// to canonicalize things as it goes. It is intended to be fast and catch
262 /// obvious cases so that instcombine and other passes are more effective. It
263 /// is expected that a later pass of GVN will catch the interesting/hard
265 class EarlyCSE : public FunctionPass {
267 const DataLayout *DL;
268 const TargetLibraryInfo *TLI;
270 AssumptionTracker *AT;
271 typedef RecyclingAllocator<BumpPtrAllocator,
272 ScopedHashTableVal<SimpleValue, Value*> > AllocatorTy;
273 typedef ScopedHashTable<SimpleValue, Value*, DenseMapInfo<SimpleValue>,
274 AllocatorTy> ScopedHTType;
276 /// AvailableValues - This scoped hash table contains the current values of
277 /// all of our simple scalar expressions. As we walk down the domtree, we
278 /// look to see if instructions are in this: if so, we replace them with what
279 /// we find, otherwise we insert them so that dominated values can succeed in
281 ScopedHTType *AvailableValues;
283 /// AvailableLoads - This scoped hash table contains the current values
284 /// of loads. This allows us to get efficient access to dominating loads when
285 /// we have a fully redundant load. In addition to the most recent load, we
286 /// keep track of a generation count of the read, which is compared against
287 /// the current generation count. The current generation count is
288 /// incremented after every possibly writing memory operation, which ensures
289 /// that we only CSE loads with other loads that have no intervening store.
290 typedef RecyclingAllocator<BumpPtrAllocator,
291 ScopedHashTableVal<Value*, std::pair<Value*, unsigned> > > LoadMapAllocator;
292 typedef ScopedHashTable<Value*, std::pair<Value*, unsigned>,
293 DenseMapInfo<Value*>, LoadMapAllocator> LoadHTType;
294 LoadHTType *AvailableLoads;
296 /// AvailableCalls - This scoped hash table contains the current values
297 /// of read-only call values. It uses the same generation count as loads.
298 typedef ScopedHashTable<CallValue, std::pair<Value*, unsigned> > CallHTType;
299 CallHTType *AvailableCalls;
301 /// CurrentGeneration - This is the current generation of the memory value.
302 unsigned CurrentGeneration;
305 explicit EarlyCSE() : FunctionPass(ID) {
306 initializeEarlyCSEPass(*PassRegistry::getPassRegistry());
309 bool runOnFunction(Function &F) override;
313 // NodeScope - almost a POD, but needs to call the constructors for the
314 // scoped hash tables so that a new scope gets pushed on. These are RAII so
315 // that the scope gets popped when the NodeScope is destroyed.
318 NodeScope(ScopedHTType *availableValues,
319 LoadHTType *availableLoads,
320 CallHTType *availableCalls) :
321 Scope(*availableValues),
322 LoadScope(*availableLoads),
323 CallScope(*availableCalls) {}
326 NodeScope(const NodeScope&) LLVM_DELETED_FUNCTION;
327 void operator=(const NodeScope&) LLVM_DELETED_FUNCTION;
329 ScopedHTType::ScopeTy Scope;
330 LoadHTType::ScopeTy LoadScope;
331 CallHTType::ScopeTy CallScope;
334 // StackNode - contains all the needed information to create a stack for
335 // doing a depth first tranversal of the tree. This includes scopes for
336 // values, loads, and calls as well as the generation. There is a child
337 // iterator so that the children do not need to be store spearately.
340 StackNode(ScopedHTType *availableValues,
341 LoadHTType *availableLoads,
342 CallHTType *availableCalls,
343 unsigned cg, DomTreeNode *n,
344 DomTreeNode::iterator child, DomTreeNode::iterator end) :
345 CurrentGeneration(cg), ChildGeneration(cg), Node(n),
346 ChildIter(child), EndIter(end),
347 Scopes(availableValues, availableLoads, availableCalls),
351 unsigned currentGeneration() { return CurrentGeneration; }
352 unsigned childGeneration() { return ChildGeneration; }
353 void childGeneration(unsigned generation) { ChildGeneration = generation; }
354 DomTreeNode *node() { return Node; }
355 DomTreeNode::iterator childIter() { return ChildIter; }
356 DomTreeNode *nextChild() {
357 DomTreeNode *child = *ChildIter;
361 DomTreeNode::iterator end() { return EndIter; }
362 bool isProcessed() { return Processed; }
363 void process() { Processed = true; }
366 StackNode(const StackNode&) LLVM_DELETED_FUNCTION;
367 void operator=(const StackNode&) LLVM_DELETED_FUNCTION;
370 unsigned CurrentGeneration;
371 unsigned ChildGeneration;
373 DomTreeNode::iterator ChildIter;
374 DomTreeNode::iterator EndIter;
379 bool processNode(DomTreeNode *Node);
381 // This transformation requires dominator postdominator info
382 void getAnalysisUsage(AnalysisUsage &AU) const override {
383 AU.addRequired<AssumptionTracker>();
384 AU.addRequired<DominatorTreeWrapperPass>();
385 AU.addRequired<TargetLibraryInfo>();
386 AU.setPreservesCFG();
391 char EarlyCSE::ID = 0;
393 // createEarlyCSEPass - The public interface to this file.
394 FunctionPass *llvm::createEarlyCSEPass() {
395 return new EarlyCSE();
398 INITIALIZE_PASS_BEGIN(EarlyCSE, "early-cse", "Early CSE", false, false)
399 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
400 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
401 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
402 INITIALIZE_PASS_END(EarlyCSE, "early-cse", "Early CSE", false, false)
404 bool EarlyCSE::processNode(DomTreeNode *Node) {
405 BasicBlock *BB = Node->getBlock();
407 // If this block has a single predecessor, then the predecessor is the parent
408 // of the domtree node and all of the live out memory values are still current
409 // in this block. If this block has multiple predecessors, then they could
410 // have invalidated the live-out memory values of our parent value. For now,
411 // just be conservative and invalidate memory if this block has multiple
413 if (!BB->getSinglePredecessor())
416 /// LastStore - Keep track of the last non-volatile store that we saw... for
417 /// as long as there in no instruction that reads memory. If we see a store
418 /// to the same location, we delete the dead store. This zaps trivial dead
419 /// stores which can occur in bitfield code among other things.
420 StoreInst *LastStore = nullptr;
422 bool Changed = false;
424 // See if any instructions in the block can be eliminated. If so, do it. If
425 // not, add them to AvailableValues.
426 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
427 Instruction *Inst = I++;
429 // Dead instructions should just be removed.
430 if (isInstructionTriviallyDead(Inst, TLI)) {
431 DEBUG(dbgs() << "EarlyCSE DCE: " << *Inst << '\n');
432 Inst->eraseFromParent();
438 // If the instruction can be simplified (e.g. X+0 = X) then replace it with
439 // its simpler value.
440 if (Value *V = SimplifyInstruction(Inst, DL, TLI, DT, AT)) {
441 DEBUG(dbgs() << "EarlyCSE Simplify: " << *Inst << " to: " << *V << '\n');
442 Inst->replaceAllUsesWith(V);
443 Inst->eraseFromParent();
449 // If this is a simple instruction that we can value number, process it.
450 if (SimpleValue::canHandle(Inst)) {
451 // See if the instruction has an available value. If so, use it.
452 if (Value *V = AvailableValues->lookup(Inst)) {
453 DEBUG(dbgs() << "EarlyCSE CSE: " << *Inst << " to: " << *V << '\n');
454 Inst->replaceAllUsesWith(V);
455 Inst->eraseFromParent();
461 // Otherwise, just remember that this value is available.
462 AvailableValues->insert(Inst, Inst);
466 // If this is a non-volatile load, process it.
467 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
468 // Ignore volatile loads.
469 if (!LI->isSimple()) {
474 // If we have an available version of this load, and if it is the right
475 // generation, replace this instruction.
476 std::pair<Value*, unsigned> InVal =
477 AvailableLoads->lookup(Inst->getOperand(0));
478 if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
479 DEBUG(dbgs() << "EarlyCSE CSE LOAD: " << *Inst << " to: "
480 << *InVal.first << '\n');
481 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
482 Inst->eraseFromParent();
488 // Otherwise, remember that we have this instruction.
489 AvailableLoads->insert(Inst->getOperand(0),
490 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
495 // If this instruction may read from memory, forget LastStore.
496 if (Inst->mayReadFromMemory())
499 // If this is a read-only call, process it.
500 if (CallValue::canHandle(Inst)) {
501 // If we have an available version of this call, and if it is the right
502 // generation, replace this instruction.
503 std::pair<Value*, unsigned> InVal = AvailableCalls->lookup(Inst);
504 if (InVal.first != nullptr && InVal.second == CurrentGeneration) {
505 DEBUG(dbgs() << "EarlyCSE CSE CALL: " << *Inst << " to: "
506 << *InVal.first << '\n');
507 if (!Inst->use_empty()) Inst->replaceAllUsesWith(InVal.first);
508 Inst->eraseFromParent();
514 // Otherwise, remember that we have this instruction.
515 AvailableCalls->insert(Inst,
516 std::pair<Value*, unsigned>(Inst, CurrentGeneration));
520 // Okay, this isn't something we can CSE at all. Check to see if it is
521 // something that could modify memory. If so, our available memory values
522 // cannot be used so bump the generation count.
523 if (Inst->mayWriteToMemory()) {
526 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
527 // We do a trivial form of DSE if there are two stores to the same
528 // location with no intervening loads. Delete the earlier store.
530 LastStore->getPointerOperand() == SI->getPointerOperand()) {
531 DEBUG(dbgs() << "EarlyCSE DEAD STORE: " << *LastStore << " due to: "
533 LastStore->eraseFromParent();
540 // Okay, we just invalidated anything we knew about loaded values. Try
541 // to salvage *something* by remembering that the stored value is a live
542 // version of the pointer. It is safe to forward from volatile stores
543 // to non-volatile loads, so we don't have to check for volatility of
545 AvailableLoads->insert(SI->getPointerOperand(),
546 std::pair<Value*, unsigned>(SI->getValueOperand(), CurrentGeneration));
548 // Remember that this was the last store we saw for DSE.
559 bool EarlyCSE::runOnFunction(Function &F) {
560 if (skipOptnoneFunction(F))
563 // Note, deque is being used here because there is significant performance gains
564 // over vector when the container becomes very large due to the specific access
565 // patterns. For more information see the mailing list discussion on this:
566 // http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120116/135228.html
567 std::deque<StackNode *> nodesToProcess;
569 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
570 DL = DLP ? &DLP->getDataLayout() : nullptr;
571 TLI = &getAnalysis<TargetLibraryInfo>();
572 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
573 AT = &getAnalysis<AssumptionTracker>();
575 // Tables that the pass uses when walking the domtree.
576 ScopedHTType AVTable;
577 AvailableValues = &AVTable;
578 LoadHTType LoadTable;
579 AvailableLoads = &LoadTable;
580 CallHTType CallTable;
581 AvailableCalls = &CallTable;
583 CurrentGeneration = 0;
584 bool Changed = false;
586 // Process the root node.
587 nodesToProcess.push_back(
588 new StackNode(AvailableValues, AvailableLoads, AvailableCalls,
589 CurrentGeneration, DT->getRootNode(),
590 DT->getRootNode()->begin(),
591 DT->getRootNode()->end()));
593 // Save the current generation.
594 unsigned LiveOutGeneration = CurrentGeneration;
596 // Process the stack.
597 while (!nodesToProcess.empty()) {
598 // Grab the first item off the stack. Set the current generation, remove
599 // the node from the stack, and process it.
600 StackNode *NodeToProcess = nodesToProcess.back();
602 // Initialize class members.
603 CurrentGeneration = NodeToProcess->currentGeneration();
605 // Check if the node needs to be processed.
606 if (!NodeToProcess->isProcessed()) {
608 Changed |= processNode(NodeToProcess->node());
609 NodeToProcess->childGeneration(CurrentGeneration);
610 NodeToProcess->process();
611 } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
612 // Push the next child onto the stack.
613 DomTreeNode *child = NodeToProcess->nextChild();
614 nodesToProcess.push_back(
615 new StackNode(AvailableValues,
618 NodeToProcess->childGeneration(), child,
619 child->begin(), child->end()));
621 // It has been processed, and there are no more children to process,
622 // so delete it and pop it off the stack.
623 delete NodeToProcess;
624 nodesToProcess.pop_back();
626 } // while (!nodes...)
628 // Reset the current generation.
629 CurrentGeneration = LiveOutGeneration;