1 //===- GVN.cpp - Eliminate redundant values and loads ------------===//
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 global value numbering to eliminate fully redundant
11 // instructions. It also performs simple dead load elimination.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "gvn"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/BasicBlock.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Value.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/DepthFirstIterator.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/SparseBitVector.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Analysis/Dominators.h"
30 #include "llvm/Analysis/AliasAnalysis.h"
31 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
32 #include "llvm/Support/CFG.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
38 STATISTIC(NumGVNInstr, "Number of instructions deleted");
39 STATISTIC(NumGVNLoad, "Number of loads deleted");
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
45 /// This class holds the mapping between values and value numbers. It is used
46 /// as an efficient mechanism to determine the expression-wise equivalence of
49 struct VISIBILITY_HIDDEN Expression {
50 enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM,
51 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
52 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
53 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
54 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
55 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
56 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
57 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
58 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
59 PTRTOINT, INTTOPTR, BITCAST, GEP, CALL, EMPTY,
62 ExpressionOpcode opcode;
67 SmallVector<uint32_t, 4> varargs;
71 Expression(ExpressionOpcode o) : opcode(o) { }
73 bool operator==(const Expression &other) const {
74 if (opcode != other.opcode)
76 else if (opcode == EMPTY || opcode == TOMBSTONE)
78 else if (type != other.type)
80 else if (function != other.function)
82 else if (firstVN != other.firstVN)
84 else if (secondVN != other.secondVN)
86 else if (thirdVN != other.thirdVN)
89 if (varargs.size() != other.varargs.size())
92 for (size_t i = 0; i < varargs.size(); ++i)
93 if (varargs[i] != other.varargs[i])
100 bool operator!=(const Expression &other) const {
101 if (opcode != other.opcode)
103 else if (opcode == EMPTY || opcode == TOMBSTONE)
105 else if (type != other.type)
107 else if (function != other.function)
109 else if (firstVN != other.firstVN)
111 else if (secondVN != other.secondVN)
113 else if (thirdVN != other.thirdVN)
116 if (varargs.size() != other.varargs.size())
119 for (size_t i = 0; i < varargs.size(); ++i)
120 if (varargs[i] != other.varargs[i])
128 class VISIBILITY_HIDDEN ValueTable {
130 DenseMap<Value*, uint32_t> valueNumbering;
131 DenseMap<Expression, uint32_t> expressionNumbering;
133 MemoryDependenceAnalysis* MD;
135 uint32_t nextValueNumber;
137 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
138 Expression::ExpressionOpcode getOpcode(CmpInst* C);
139 Expression::ExpressionOpcode getOpcode(CastInst* C);
140 Expression create_expression(BinaryOperator* BO);
141 Expression create_expression(CmpInst* C);
142 Expression create_expression(ShuffleVectorInst* V);
143 Expression create_expression(ExtractElementInst* C);
144 Expression create_expression(InsertElementInst* V);
145 Expression create_expression(SelectInst* V);
146 Expression create_expression(CastInst* C);
147 Expression create_expression(GetElementPtrInst* G);
148 Expression create_expression(CallInst* C);
150 ValueTable() : nextValueNumber(1) { }
151 uint32_t lookup_or_add(Value* V);
152 uint32_t lookup(Value* V) const;
153 void add(Value* V, uint32_t num);
155 void erase(Value* v);
157 void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
158 void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
163 template <> struct DenseMapInfo<Expression> {
164 static inline Expression getEmptyKey() {
165 return Expression(Expression::EMPTY);
168 static inline Expression getTombstoneKey() {
169 return Expression(Expression::TOMBSTONE);
172 static unsigned getHashValue(const Expression e) {
173 unsigned hash = e.opcode;
175 hash = e.firstVN + hash * 37;
176 hash = e.secondVN + hash * 37;
177 hash = e.thirdVN + hash * 37;
179 hash = ((unsigned)((uintptr_t)e.type >> 4) ^
180 (unsigned)((uintptr_t)e.type >> 9)) +
183 for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
184 E = e.varargs.end(); I != E; ++I)
185 hash = *I + hash * 37;
187 hash = ((unsigned)((uintptr_t)e.function >> 4) ^
188 (unsigned)((uintptr_t)e.function >> 9)) +
193 static bool isEqual(const Expression &LHS, const Expression &RHS) {
196 static bool isPod() { return true; }
200 //===----------------------------------------------------------------------===//
201 // ValueTable Internal Functions
202 //===----------------------------------------------------------------------===//
203 Expression::ExpressionOpcode ValueTable::getOpcode(BinaryOperator* BO) {
204 switch(BO->getOpcode()) {
205 default: // THIS SHOULD NEVER HAPPEN
206 assert(0 && "Binary operator with unknown opcode?");
207 case Instruction::Add: return Expression::ADD;
208 case Instruction::Sub: return Expression::SUB;
209 case Instruction::Mul: return Expression::MUL;
210 case Instruction::UDiv: return Expression::UDIV;
211 case Instruction::SDiv: return Expression::SDIV;
212 case Instruction::FDiv: return Expression::FDIV;
213 case Instruction::URem: return Expression::UREM;
214 case Instruction::SRem: return Expression::SREM;
215 case Instruction::FRem: return Expression::FREM;
216 case Instruction::Shl: return Expression::SHL;
217 case Instruction::LShr: return Expression::LSHR;
218 case Instruction::AShr: return Expression::ASHR;
219 case Instruction::And: return Expression::AND;
220 case Instruction::Or: return Expression::OR;
221 case Instruction::Xor: return Expression::XOR;
225 Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
226 if (isa<ICmpInst>(C)) {
227 switch (C->getPredicate()) {
228 default: // THIS SHOULD NEVER HAPPEN
229 assert(0 && "Comparison with unknown predicate?");
230 case ICmpInst::ICMP_EQ: return Expression::ICMPEQ;
231 case ICmpInst::ICMP_NE: return Expression::ICMPNE;
232 case ICmpInst::ICMP_UGT: return Expression::ICMPUGT;
233 case ICmpInst::ICMP_UGE: return Expression::ICMPUGE;
234 case ICmpInst::ICMP_ULT: return Expression::ICMPULT;
235 case ICmpInst::ICMP_ULE: return Expression::ICMPULE;
236 case ICmpInst::ICMP_SGT: return Expression::ICMPSGT;
237 case ICmpInst::ICMP_SGE: return Expression::ICMPSGE;
238 case ICmpInst::ICMP_SLT: return Expression::ICMPSLT;
239 case ICmpInst::ICMP_SLE: return Expression::ICMPSLE;
242 assert(isa<FCmpInst>(C) && "Unknown compare");
243 switch (C->getPredicate()) {
244 default: // THIS SHOULD NEVER HAPPEN
245 assert(0 && "Comparison with unknown predicate?");
246 case FCmpInst::FCMP_OEQ: return Expression::FCMPOEQ;
247 case FCmpInst::FCMP_OGT: return Expression::FCMPOGT;
248 case FCmpInst::FCMP_OGE: return Expression::FCMPOGE;
249 case FCmpInst::FCMP_OLT: return Expression::FCMPOLT;
250 case FCmpInst::FCMP_OLE: return Expression::FCMPOLE;
251 case FCmpInst::FCMP_ONE: return Expression::FCMPONE;
252 case FCmpInst::FCMP_ORD: return Expression::FCMPORD;
253 case FCmpInst::FCMP_UNO: return Expression::FCMPUNO;
254 case FCmpInst::FCMP_UEQ: return Expression::FCMPUEQ;
255 case FCmpInst::FCMP_UGT: return Expression::FCMPUGT;
256 case FCmpInst::FCMP_UGE: return Expression::FCMPUGE;
257 case FCmpInst::FCMP_ULT: return Expression::FCMPULT;
258 case FCmpInst::FCMP_ULE: return Expression::FCMPULE;
259 case FCmpInst::FCMP_UNE: return Expression::FCMPUNE;
263 Expression::ExpressionOpcode ValueTable::getOpcode(CastInst* C) {
264 switch(C->getOpcode()) {
265 default: // THIS SHOULD NEVER HAPPEN
266 assert(0 && "Cast operator with unknown opcode?");
267 case Instruction::Trunc: return Expression::TRUNC;
268 case Instruction::ZExt: return Expression::ZEXT;
269 case Instruction::SExt: return Expression::SEXT;
270 case Instruction::FPToUI: return Expression::FPTOUI;
271 case Instruction::FPToSI: return Expression::FPTOSI;
272 case Instruction::UIToFP: return Expression::UITOFP;
273 case Instruction::SIToFP: return Expression::SITOFP;
274 case Instruction::FPTrunc: return Expression::FPTRUNC;
275 case Instruction::FPExt: return Expression::FPEXT;
276 case Instruction::PtrToInt: return Expression::PTRTOINT;
277 case Instruction::IntToPtr: return Expression::INTTOPTR;
278 case Instruction::BitCast: return Expression::BITCAST;
282 Expression ValueTable::create_expression(CallInst* C) {
285 e.type = C->getType();
289 e.function = C->getCalledFunction();
290 e.opcode = Expression::CALL;
292 for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
294 e.varargs.push_back(lookup_or_add(*I));
299 Expression ValueTable::create_expression(BinaryOperator* BO) {
302 e.firstVN = lookup_or_add(BO->getOperand(0));
303 e.secondVN = lookup_or_add(BO->getOperand(1));
306 e.type = BO->getType();
307 e.opcode = getOpcode(BO);
312 Expression ValueTable::create_expression(CmpInst* C) {
315 e.firstVN = lookup_or_add(C->getOperand(0));
316 e.secondVN = lookup_or_add(C->getOperand(1));
319 e.type = C->getType();
320 e.opcode = getOpcode(C);
325 Expression ValueTable::create_expression(CastInst* C) {
328 e.firstVN = lookup_or_add(C->getOperand(0));
332 e.type = C->getType();
333 e.opcode = getOpcode(C);
338 Expression ValueTable::create_expression(ShuffleVectorInst* S) {
341 e.firstVN = lookup_or_add(S->getOperand(0));
342 e.secondVN = lookup_or_add(S->getOperand(1));
343 e.thirdVN = lookup_or_add(S->getOperand(2));
345 e.type = S->getType();
346 e.opcode = Expression::SHUFFLE;
351 Expression ValueTable::create_expression(ExtractElementInst* E) {
354 e.firstVN = lookup_or_add(E->getOperand(0));
355 e.secondVN = lookup_or_add(E->getOperand(1));
358 e.type = E->getType();
359 e.opcode = Expression::EXTRACT;
364 Expression ValueTable::create_expression(InsertElementInst* I) {
367 e.firstVN = lookup_or_add(I->getOperand(0));
368 e.secondVN = lookup_or_add(I->getOperand(1));
369 e.thirdVN = lookup_or_add(I->getOperand(2));
371 e.type = I->getType();
372 e.opcode = Expression::INSERT;
377 Expression ValueTable::create_expression(SelectInst* I) {
380 e.firstVN = lookup_or_add(I->getCondition());
381 e.secondVN = lookup_or_add(I->getTrueValue());
382 e.thirdVN = lookup_or_add(I->getFalseValue());
384 e.type = I->getType();
385 e.opcode = Expression::SELECT;
390 Expression ValueTable::create_expression(GetElementPtrInst* G) {
393 e.firstVN = lookup_or_add(G->getPointerOperand());
397 e.type = G->getType();
398 e.opcode = Expression::GEP;
400 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
402 e.varargs.push_back(lookup_or_add(*I));
407 //===----------------------------------------------------------------------===//
408 // ValueTable External Functions
409 //===----------------------------------------------------------------------===//
411 /// lookup_or_add - Returns the value number for the specified value, assigning
412 /// it a new number if it did not have one before.
413 uint32_t ValueTable::lookup_or_add(Value* V) {
414 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
415 if (VI != valueNumbering.end())
418 if (CallInst* C = dyn_cast<CallInst>(V)) {
419 if (AA->doesNotAccessMemory(C)) {
420 Expression e = create_expression(C);
422 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
423 if (EI != expressionNumbering.end()) {
424 valueNumbering.insert(std::make_pair(V, EI->second));
427 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
428 valueNumbering.insert(std::make_pair(V, nextValueNumber));
430 return nextValueNumber++;
432 } else if (AA->onlyReadsMemory(C)) {
433 Expression e = create_expression(C);
435 Instruction* dep = MD->getDependency(C);
437 if (dep == MemoryDependenceAnalysis::NonLocal ||
438 !isa<CallInst>(dep)) {
439 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
440 valueNumbering.insert(std::make_pair(V, nextValueNumber));
442 return nextValueNumber++;
445 CallInst* cdep = cast<CallInst>(dep);
446 Expression d_exp = create_expression(cdep);
449 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
450 valueNumbering.insert(std::make_pair(V, nextValueNumber));
452 return nextValueNumber++;
454 uint32_t v = expressionNumbering[d_exp];
455 valueNumbering.insert(std::make_pair(V, v));
460 valueNumbering.insert(std::make_pair(V, nextValueNumber));
461 return nextValueNumber++;
463 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
464 Expression e = create_expression(BO);
466 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
467 if (EI != expressionNumbering.end()) {
468 valueNumbering.insert(std::make_pair(V, EI->second));
471 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
472 valueNumbering.insert(std::make_pair(V, nextValueNumber));
474 return nextValueNumber++;
476 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
477 Expression e = create_expression(C);
479 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
480 if (EI != expressionNumbering.end()) {
481 valueNumbering.insert(std::make_pair(V, EI->second));
484 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
485 valueNumbering.insert(std::make_pair(V, nextValueNumber));
487 return nextValueNumber++;
489 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
490 Expression e = create_expression(U);
492 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
493 if (EI != expressionNumbering.end()) {
494 valueNumbering.insert(std::make_pair(V, EI->second));
497 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
498 valueNumbering.insert(std::make_pair(V, nextValueNumber));
500 return nextValueNumber++;
502 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
503 Expression e = create_expression(U);
505 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
506 if (EI != expressionNumbering.end()) {
507 valueNumbering.insert(std::make_pair(V, EI->second));
510 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
511 valueNumbering.insert(std::make_pair(V, nextValueNumber));
513 return nextValueNumber++;
515 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
516 Expression e = create_expression(U);
518 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
519 if (EI != expressionNumbering.end()) {
520 valueNumbering.insert(std::make_pair(V, EI->second));
523 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
524 valueNumbering.insert(std::make_pair(V, nextValueNumber));
526 return nextValueNumber++;
528 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
529 Expression e = create_expression(U);
531 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
532 if (EI != expressionNumbering.end()) {
533 valueNumbering.insert(std::make_pair(V, EI->second));
536 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
537 valueNumbering.insert(std::make_pair(V, nextValueNumber));
539 return nextValueNumber++;
541 } else if (CastInst* U = dyn_cast<CastInst>(V)) {
542 Expression e = create_expression(U);
544 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
545 if (EI != expressionNumbering.end()) {
546 valueNumbering.insert(std::make_pair(V, EI->second));
549 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
550 valueNumbering.insert(std::make_pair(V, nextValueNumber));
552 return nextValueNumber++;
554 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
555 Expression e = create_expression(U);
557 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
558 if (EI != expressionNumbering.end()) {
559 valueNumbering.insert(std::make_pair(V, EI->second));
562 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
563 valueNumbering.insert(std::make_pair(V, nextValueNumber));
565 return nextValueNumber++;
568 valueNumbering.insert(std::make_pair(V, nextValueNumber));
569 return nextValueNumber++;
573 /// lookup - Returns the value number of the specified value. Fails if
574 /// the value has not yet been numbered.
575 uint32_t ValueTable::lookup(Value* V) const {
576 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
577 assert(VI != valueNumbering.end() && "Value not numbered?");
581 /// clear - Remove all entries from the ValueTable
582 void ValueTable::clear() {
583 valueNumbering.clear();
584 expressionNumbering.clear();
588 /// erase - Remove a value from the value numbering
589 void ValueTable::erase(Value* V) {
590 valueNumbering.erase(V);
593 //===----------------------------------------------------------------------===//
594 // ValueNumberedSet Class
595 //===----------------------------------------------------------------------===//
597 class VISIBILITY_HIDDEN ValueNumberedSet {
599 SmallPtrSet<Value*, 8> contents;
600 SparseBitVector<64> numbers;
602 ValueNumberedSet() { }
603 ValueNumberedSet(const ValueNumberedSet& other) {
604 numbers = other.numbers;
605 contents = other.contents;
608 typedef SmallPtrSet<Value*, 8>::iterator iterator;
610 iterator begin() { return contents.begin(); }
611 iterator end() { return contents.end(); }
613 bool insert(Value* v) { return contents.insert(v); }
614 void insert(iterator I, iterator E) { contents.insert(I, E); }
615 void erase(Value* v) { contents.erase(v); }
616 unsigned count(Value* v) { return contents.count(v); }
617 size_t size() { return contents.size(); }
619 void set(unsigned i) {
623 void operator=(const ValueNumberedSet& other) {
624 contents = other.contents;
625 numbers = other.numbers;
628 void reset(unsigned i) {
632 bool test(unsigned i) {
633 return numbers.test(i);
638 //===----------------------------------------------------------------------===//
640 //===----------------------------------------------------------------------===//
644 class VISIBILITY_HIDDEN GVN : public FunctionPass {
645 bool runOnFunction(Function &F);
647 static char ID; // Pass identification, replacement for typeid
648 GVN() : FunctionPass((intptr_t)&ID) { }
653 DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
655 typedef DenseMap<Value*, SmallPtrSet<Instruction*, 4> > PhiMapType;
659 // This transformation requires dominator postdominator info
660 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
661 AU.setPreservesCFG();
662 AU.addRequired<DominatorTree>();
663 AU.addRequired<MemoryDependenceAnalysis>();
664 AU.addRequired<AliasAnalysis>();
665 AU.addPreserved<AliasAnalysis>();
666 AU.addPreserved<MemoryDependenceAnalysis>();
670 // FIXME: eliminate or document these better
671 Value* find_leader(ValueNumberedSet& vals, uint32_t v) ;
672 void val_insert(ValueNumberedSet& s, Value* v);
673 bool processLoad(LoadInst* L,
674 DenseMap<Value*, LoadInst*> &lastLoad,
675 SmallVectorImpl<Instruction*> &toErase);
676 bool processInstruction(Instruction* I,
677 ValueNumberedSet& currAvail,
678 DenseMap<Value*, LoadInst*>& lastSeenLoad,
679 SmallVectorImpl<Instruction*> &toErase);
680 bool processNonLocalLoad(LoadInst* L,
681 SmallVectorImpl<Instruction*> &toErase);
682 Value *GetValueForBlock(BasicBlock *BB, LoadInst* orig,
683 DenseMap<BasicBlock*, Value*> &Phis,
684 bool top_level = false);
685 void dump(DenseMap<BasicBlock*, Value*>& d);
686 bool iterateOnFunction(Function &F);
687 Value* CollapsePhi(PHINode* p);
688 bool isSafeReplacement(PHINode* p, Instruction* inst);
694 // createGVNPass - The public interface to this file...
695 FunctionPass *llvm::createGVNPass() { return new GVN(); }
697 static RegisterPass<GVN> X("gvn",
698 "Global Value Numbering");
700 /// find_leader - Given a set and a value number, return the first
701 /// element of the set with that value number, or 0 if no such element
703 Value* GVN::find_leader(ValueNumberedSet& vals, uint32_t v) {
707 for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
709 if (v == VN.lookup(*I))
712 assert(0 && "No leader found, but present bit is set?");
716 /// val_insert - Insert a value into a set only if there is not a value
717 /// with the same value number already in the set
718 void GVN::val_insert(ValueNumberedSet& s, Value* v) {
719 uint32_t num = VN.lookup(v);
724 void GVN::dump(DenseMap<BasicBlock*, Value*>& d) {
726 for (DenseMap<BasicBlock*, Value*>::iterator I = d.begin(),
727 E = d.end(); I != E; ++I) {
728 if (I->second == MemoryDependenceAnalysis::None)
736 Value* GVN::CollapsePhi(PHINode* p) {
737 DominatorTree &DT = getAnalysis<DominatorTree>();
738 Value* constVal = p->hasConstantValue();
740 if (!constVal) return 0;
742 Instruction* inst = dyn_cast<Instruction>(constVal);
746 if (DT.dominates(inst, p))
747 if (isSafeReplacement(p, inst))
752 bool GVN::isSafeReplacement(PHINode* p, Instruction* inst) {
753 if (!isa<PHINode>(inst))
756 for (Instruction::use_iterator UI = p->use_begin(), E = p->use_end();
758 if (PHINode* use_phi = dyn_cast<PHINode>(UI))
759 if (use_phi->getParent() == inst->getParent())
765 /// GetValueForBlock - Get the value to use within the specified basic block.
766 /// available values are in Phis.
767 Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig,
768 DenseMap<BasicBlock*, Value*> &Phis,
771 // If we have already computed this value, return the previously computed val.
772 DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
773 if (V != Phis.end() && !top_level) return V->second;
775 BasicBlock* singlePred = BB->getSinglePredecessor();
777 Value *ret = GetValueForBlock(singlePred, orig, Phis);
782 // Otherwise, the idom is the loop, so we need to insert a PHI node. Do so
783 // now, then get values to fill in the incoming values for the PHI.
784 PHINode *PN = PHINode::Create(orig->getType(), orig->getName()+".rle",
786 PN->reserveOperandSpace(std::distance(pred_begin(BB), pred_end(BB)));
788 if (Phis.count(BB) == 0)
789 Phis.insert(std::make_pair(BB, PN));
791 // Fill in the incoming values for the block.
792 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
793 Value* val = GetValueForBlock(*PI, orig, Phis);
794 PN->addIncoming(val, *PI);
797 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
798 AA.copyValue(orig, PN);
800 // Attempt to collapse PHI nodes that are trivially redundant
801 Value* v = CollapsePhi(PN);
803 // Cache our phi construction results
804 phiMap[orig->getPointerOperand()].insert(PN);
808 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
810 MD.removeInstruction(PN);
811 PN->replaceAllUsesWith(v);
813 for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
814 E = Phis.end(); I != E; ++I)
818 PN->eraseFromParent();
824 /// processNonLocalLoad - Attempt to eliminate a load whose dependencies are
825 /// non-local by performing PHI construction.
826 bool GVN::processNonLocalLoad(LoadInst* L,
827 SmallVectorImpl<Instruction*> &toErase) {
828 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
830 // Find the non-local dependencies of the load
831 DenseMap<BasicBlock*, Value*> deps;
832 MD.getNonLocalDependency(L, deps);
834 DenseMap<BasicBlock*, Value*> repl;
836 // Filter out useless results (non-locals, etc)
837 for (DenseMap<BasicBlock*, Value*>::iterator I = deps.begin(), E = deps.end();
839 if (I->second == MemoryDependenceAnalysis::None)
842 if (I->second == MemoryDependenceAnalysis::NonLocal)
845 if (StoreInst* S = dyn_cast<StoreInst>(I->second)) {
846 if (S->getPointerOperand() != L->getPointerOperand())
848 repl[I->first] = S->getOperand(0);
849 } else if (LoadInst* LD = dyn_cast<LoadInst>(I->second)) {
850 if (LD->getPointerOperand() != L->getPointerOperand())
858 // Use cached PHI construction information from previous runs
859 SmallPtrSet<Instruction*, 4>& p = phiMap[L->getPointerOperand()];
860 for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
862 if ((*I)->getParent() == L->getParent()) {
863 MD.removeInstruction(L);
864 L->replaceAllUsesWith(*I);
865 toErase.push_back(L);
870 repl.insert(std::make_pair((*I)->getParent(), *I));
873 // Perform PHI construction
874 SmallPtrSet<BasicBlock*, 4> visited;
875 Value* v = GetValueForBlock(L->getParent(), L, repl, true);
877 MD.removeInstruction(L);
878 L->replaceAllUsesWith(v);
879 toErase.push_back(L);
885 /// processLoad - Attempt to eliminate a load, first by eliminating it
886 /// locally, and then attempting non-local elimination if that fails.
887 bool GVN::processLoad(LoadInst *L, DenseMap<Value*, LoadInst*> &lastLoad,
888 SmallVectorImpl<Instruction*> &toErase) {
889 if (L->isVolatile()) {
890 lastLoad[L->getPointerOperand()] = L;
894 Value* pointer = L->getPointerOperand();
895 LoadInst*& last = lastLoad[pointer];
897 // ... to a pointer that has been loaded from before...
898 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
899 bool removedNonLocal = false;
900 Instruction* dep = MD.getDependency(L);
901 if (dep == MemoryDependenceAnalysis::NonLocal &&
902 L->getParent() != &L->getParent()->getParent()->getEntryBlock()) {
903 removedNonLocal = processNonLocalLoad(L, toErase);
905 if (!removedNonLocal)
908 return removedNonLocal;
912 bool deletedLoad = false;
914 // Walk up the dependency chain until we either find
915 // a dependency we can use, or we can't walk any further
916 while (dep != MemoryDependenceAnalysis::None &&
917 dep != MemoryDependenceAnalysis::NonLocal &&
918 (isa<LoadInst>(dep) || isa<StoreInst>(dep))) {
919 // ... that depends on a store ...
920 if (StoreInst* S = dyn_cast<StoreInst>(dep)) {
921 if (S->getPointerOperand() == pointer) {
923 MD.removeInstruction(L);
925 L->replaceAllUsesWith(S->getOperand(0));
926 toErase.push_back(L);
931 // Whether we removed it or not, we can't
935 // If we don't depend on a store, and we haven't
936 // been loaded before, bail.
938 } else if (dep == last) {
940 MD.removeInstruction(L);
942 L->replaceAllUsesWith(last);
943 toErase.push_back(L);
949 dep = MD.getDependency(L, dep);
953 if (dep != MemoryDependenceAnalysis::None &&
954 dep != MemoryDependenceAnalysis::NonLocal &&
955 isa<AllocationInst>(dep)) {
956 // Check that this load is actually from the
957 // allocation we found
958 Value* v = L->getOperand(0);
960 if (BitCastInst *BC = dyn_cast<BitCastInst>(v))
961 v = BC->getOperand(0);
962 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(v))
963 v = GEP->getOperand(0);
968 // If this load depends directly on an allocation, there isn't
969 // anything stored there; therefore, we can optimize this load
971 MD.removeInstruction(L);
973 L->replaceAllUsesWith(UndefValue::get(L->getType()));
974 toErase.push_back(L);
986 /// processInstruction - When calculating availability, handle an instruction
987 /// by inserting it into the appropriate sets
988 bool GVN::processInstruction(Instruction *I, ValueNumberedSet &currAvail,
989 DenseMap<Value*, LoadInst*> &lastSeenLoad,
990 SmallVectorImpl<Instruction*> &toErase) {
991 if (LoadInst* L = dyn_cast<LoadInst>(I))
992 return processLoad(L, lastSeenLoad, toErase);
994 // Allocations are always uniquely numbered, so we can save time and memory
995 // by fast failing them.
996 if (isa<AllocationInst>(I))
999 unsigned num = VN.lookup_or_add(I);
1001 // Collapse PHI nodes
1002 if (PHINode* p = dyn_cast<PHINode>(I)) {
1003 Value* constVal = CollapsePhi(p);
1006 for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
1008 if (PI->second.count(p))
1009 PI->second.erase(p);
1011 p->replaceAllUsesWith(constVal);
1012 toErase.push_back(p);
1014 // Perform value-number based elimination
1015 } else if (currAvail.test(num)) {
1016 Value* repl = find_leader(currAvail, num);
1019 MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
1020 MD.removeInstruction(I);
1023 I->replaceAllUsesWith(repl);
1024 toErase.push_back(I);
1026 } else if (!I->isTerminator()) {
1028 currAvail.insert(I);
1034 // GVN::runOnFunction - This is the main transformation entry point for a
1037 bool GVN::runOnFunction(Function& F) {
1038 VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
1039 VN.setMemDep(&getAnalysis<MemoryDependenceAnalysis>());
1041 bool changed = false;
1042 bool shouldContinue = true;
1044 while (shouldContinue) {
1045 shouldContinue = iterateOnFunction(F);
1046 changed |= shouldContinue;
1053 // GVN::iterateOnFunction - Executes one iteration of GVN
1054 bool GVN::iterateOnFunction(Function &F) {
1055 // Clean out global sets from any previous functions
1057 availableOut.clear();
1060 bool changed_function = false;
1062 DominatorTree &DT = getAnalysis<DominatorTree>();
1064 SmallVector<Instruction*, 8> toErase;
1065 DenseMap<Value*, LoadInst*> lastSeenLoad;
1066 DenseMap<DomTreeNode*, size_t> numChildrenVisited;
1068 // Top-down walk of the dominator tree
1069 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1070 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1072 // Get the set to update for this block
1073 ValueNumberedSet& currAvail = availableOut[DI->getBlock()];
1074 lastSeenLoad.clear();
1076 BasicBlock* BB = DI->getBlock();
1078 // A block inherits AVAIL_OUT from its dominator
1079 if (DI->getIDom() != 0) {
1080 currAvail = availableOut[DI->getIDom()->getBlock()];
1082 numChildrenVisited[DI->getIDom()]++;
1084 if (numChildrenVisited[DI->getIDom()] == DI->getIDom()->getNumChildren()) {
1085 availableOut.erase(DI->getIDom()->getBlock());
1086 numChildrenVisited.erase(DI->getIDom());
1090 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1092 changed_function |= processInstruction(BI, currAvail,
1093 lastSeenLoad, toErase);
1094 if (toErase.empty()) {
1099 // If we need some instructions deleted, do it now.
1100 NumGVNInstr += toErase.size();
1102 // Avoid iterator invalidation.
1103 bool AtStart = BI == BB->begin();
1107 for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
1108 E = toErase.end(); I != E; ++I)
1109 (*I)->eraseFromParent();
1120 return changed_function;