1 //===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the Owen Anderson and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs a hybrid of global value numbering and partial redundancy
11 // elimination, known as GVN-PRE. It performs partial redundancy elimination on
12 // values, rather than lexical expressions, allowing a more comprehensive view
13 // the optimization. It replaces redundant values with uses of earlier
14 // occurences of the same value. While this is beneficial in that it eliminates
15 // unneeded computation, it also increases register pressure by creating large
16 // live ranges, and should be used with caution on platforms that are very
17 // sensitive to register pressure.
19 //===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "gvnpre"
22 #include "llvm/Value.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Function.h"
26 #include "llvm/DerivedTypes.h"
27 #include "llvm/Analysis/Dominators.h"
28 #include "llvm/ADT/BitVector.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/DepthFirstIterator.h"
31 #include "llvm/ADT/PostOrderIterator.h"
32 #include "llvm/ADT/SmallPtrSet.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
35 #include "llvm/Support/CFG.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/Debug.h"
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
49 /// This class holds the mapping between values and value numbers. It is used
50 /// as an efficient mechanism to determine the expression-wise equivalence of
54 class VISIBILITY_HIDDEN ValueTable {
57 enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM,
58 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
59 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
60 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
61 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
62 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
63 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
64 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
65 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
66 PTRTOINT, INTTOPTR, BITCAST, GEP};
68 ExpressionOpcode opcode;
73 std::vector<uint32_t> varargs;
75 bool operator< (const Expression& other) const {
76 if (opcode < other.opcode)
78 else if (opcode > other.opcode)
80 else if (type < other.type)
82 else if (type > other.type)
84 else if (firstVN < other.firstVN)
86 else if (firstVN > other.firstVN)
88 else if (secondVN < other.secondVN)
90 else if (secondVN > other.secondVN)
92 else if (thirdVN < other.thirdVN)
94 else if (thirdVN > other.thirdVN)
97 if (varargs.size() < other.varargs.size())
99 else if (varargs.size() > other.varargs.size())
102 for (size_t i = 0; i < varargs.size(); ++i)
103 if (varargs[i] < other.varargs[i])
105 else if (varargs[i] > other.varargs[i])
114 DenseMap<Value*, uint32_t> valueNumbering;
115 std::map<Expression, uint32_t> expressionNumbering;
117 uint32_t nextValueNumber;
119 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
120 Expression::ExpressionOpcode getOpcode(CmpInst* C);
121 Expression::ExpressionOpcode getOpcode(CastInst* C);
122 Expression create_expression(BinaryOperator* BO);
123 Expression create_expression(CmpInst* C);
124 Expression create_expression(ShuffleVectorInst* V);
125 Expression create_expression(ExtractElementInst* C);
126 Expression create_expression(InsertElementInst* V);
127 Expression create_expression(SelectInst* V);
128 Expression create_expression(CastInst* C);
129 Expression create_expression(GetElementPtrInst* G);
131 ValueTable() { nextValueNumber = 1; }
132 uint32_t lookup_or_add(Value* V);
133 uint32_t lookup(Value* V) const;
134 void add(Value* V, uint32_t num);
136 void erase(Value* v);
141 //===----------------------------------------------------------------------===//
142 // ValueTable Internal Functions
143 //===----------------------------------------------------------------------===//
144 ValueTable::Expression::ExpressionOpcode
145 ValueTable::getOpcode(BinaryOperator* BO) {
146 switch(BO->getOpcode()) {
147 case Instruction::Add:
148 return Expression::ADD;
149 case Instruction::Sub:
150 return Expression::SUB;
151 case Instruction::Mul:
152 return Expression::MUL;
153 case Instruction::UDiv:
154 return Expression::UDIV;
155 case Instruction::SDiv:
156 return Expression::SDIV;
157 case Instruction::FDiv:
158 return Expression::FDIV;
159 case Instruction::URem:
160 return Expression::UREM;
161 case Instruction::SRem:
162 return Expression::SREM;
163 case Instruction::FRem:
164 return Expression::FREM;
165 case Instruction::Shl:
166 return Expression::SHL;
167 case Instruction::LShr:
168 return Expression::LSHR;
169 case Instruction::AShr:
170 return Expression::ASHR;
171 case Instruction::And:
172 return Expression::AND;
173 case Instruction::Or:
174 return Expression::OR;
175 case Instruction::Xor:
176 return Expression::XOR;
178 // THIS SHOULD NEVER HAPPEN
180 assert(0 && "Binary operator with unknown opcode?");
181 return Expression::ADD;
185 ValueTable::Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
186 if (C->getOpcode() == Instruction::ICmp) {
187 switch (C->getPredicate()) {
188 case ICmpInst::ICMP_EQ:
189 return Expression::ICMPEQ;
190 case ICmpInst::ICMP_NE:
191 return Expression::ICMPNE;
192 case ICmpInst::ICMP_UGT:
193 return Expression::ICMPUGT;
194 case ICmpInst::ICMP_UGE:
195 return Expression::ICMPUGE;
196 case ICmpInst::ICMP_ULT:
197 return Expression::ICMPULT;
198 case ICmpInst::ICMP_ULE:
199 return Expression::ICMPULE;
200 case ICmpInst::ICMP_SGT:
201 return Expression::ICMPSGT;
202 case ICmpInst::ICMP_SGE:
203 return Expression::ICMPSGE;
204 case ICmpInst::ICMP_SLT:
205 return Expression::ICMPSLT;
206 case ICmpInst::ICMP_SLE:
207 return Expression::ICMPSLE;
209 // THIS SHOULD NEVER HAPPEN
211 assert(0 && "Comparison with unknown predicate?");
212 return Expression::ICMPEQ;
215 switch (C->getPredicate()) {
216 case FCmpInst::FCMP_OEQ:
217 return Expression::FCMPOEQ;
218 case FCmpInst::FCMP_OGT:
219 return Expression::FCMPOGT;
220 case FCmpInst::FCMP_OGE:
221 return Expression::FCMPOGE;
222 case FCmpInst::FCMP_OLT:
223 return Expression::FCMPOLT;
224 case FCmpInst::FCMP_OLE:
225 return Expression::FCMPOLE;
226 case FCmpInst::FCMP_ONE:
227 return Expression::FCMPONE;
228 case FCmpInst::FCMP_ORD:
229 return Expression::FCMPORD;
230 case FCmpInst::FCMP_UNO:
231 return Expression::FCMPUNO;
232 case FCmpInst::FCMP_UEQ:
233 return Expression::FCMPUEQ;
234 case FCmpInst::FCMP_UGT:
235 return Expression::FCMPUGT;
236 case FCmpInst::FCMP_UGE:
237 return Expression::FCMPUGE;
238 case FCmpInst::FCMP_ULT:
239 return Expression::FCMPULT;
240 case FCmpInst::FCMP_ULE:
241 return Expression::FCMPULE;
242 case FCmpInst::FCMP_UNE:
243 return Expression::FCMPUNE;
245 // THIS SHOULD NEVER HAPPEN
247 assert(0 && "Comparison with unknown predicate?");
248 return Expression::FCMPOEQ;
253 ValueTable::Expression::ExpressionOpcode
254 ValueTable::getOpcode(CastInst* C) {
255 switch(C->getOpcode()) {
256 case Instruction::Trunc:
257 return Expression::TRUNC;
258 case Instruction::ZExt:
259 return Expression::ZEXT;
260 case Instruction::SExt:
261 return Expression::SEXT;
262 case Instruction::FPToUI:
263 return Expression::FPTOUI;
264 case Instruction::FPToSI:
265 return Expression::FPTOSI;
266 case Instruction::UIToFP:
267 return Expression::UITOFP;
268 case Instruction::SIToFP:
269 return Expression::SITOFP;
270 case Instruction::FPTrunc:
271 return Expression::FPTRUNC;
272 case Instruction::FPExt:
273 return Expression::FPEXT;
274 case Instruction::PtrToInt:
275 return Expression::PTRTOINT;
276 case Instruction::IntToPtr:
277 return Expression::INTTOPTR;
278 case Instruction::BitCast:
279 return Expression::BITCAST;
281 // THIS SHOULD NEVER HAPPEN
283 assert(0 && "Cast operator with unknown opcode?");
284 return Expression::BITCAST;
288 ValueTable::Expression ValueTable::create_expression(BinaryOperator* BO) {
291 e.firstVN = lookup_or_add(BO->getOperand(0));
292 e.secondVN = lookup_or_add(BO->getOperand(1));
294 e.type = BO->getType();
295 e.opcode = getOpcode(BO);
300 ValueTable::Expression ValueTable::create_expression(CmpInst* C) {
303 e.firstVN = lookup_or_add(C->getOperand(0));
304 e.secondVN = lookup_or_add(C->getOperand(1));
306 e.type = C->getType();
307 e.opcode = getOpcode(C);
312 ValueTable::Expression ValueTable::create_expression(CastInst* C) {
315 e.firstVN = lookup_or_add(C->getOperand(0));
318 e.type = C->getType();
319 e.opcode = getOpcode(C);
324 ValueTable::Expression ValueTable::create_expression(ShuffleVectorInst* S) {
327 e.firstVN = lookup_or_add(S->getOperand(0));
328 e.secondVN = lookup_or_add(S->getOperand(1));
329 e.thirdVN = lookup_or_add(S->getOperand(2));
330 e.type = S->getType();
331 e.opcode = Expression::SHUFFLE;
336 ValueTable::Expression ValueTable::create_expression(ExtractElementInst* E) {
339 e.firstVN = lookup_or_add(E->getOperand(0));
340 e.secondVN = lookup_or_add(E->getOperand(1));
342 e.type = E->getType();
343 e.opcode = Expression::EXTRACT;
348 ValueTable::Expression ValueTable::create_expression(InsertElementInst* I) {
351 e.firstVN = lookup_or_add(I->getOperand(0));
352 e.secondVN = lookup_or_add(I->getOperand(1));
353 e.thirdVN = lookup_or_add(I->getOperand(2));
354 e.type = I->getType();
355 e.opcode = Expression::INSERT;
360 ValueTable::Expression ValueTable::create_expression(SelectInst* I) {
363 e.firstVN = lookup_or_add(I->getCondition());
364 e.secondVN = lookup_or_add(I->getTrueValue());
365 e.thirdVN = lookup_or_add(I->getFalseValue());
366 e.type = I->getType();
367 e.opcode = Expression::SELECT;
372 ValueTable::Expression ValueTable::create_expression(GetElementPtrInst* G) {
375 e.firstVN = lookup_or_add(G->getPointerOperand());
378 e.type = G->getType();
379 e.opcode = Expression::SELECT;
381 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
383 e.varargs.push_back(lookup_or_add(*I));
388 //===----------------------------------------------------------------------===//
389 // ValueTable External Functions
390 //===----------------------------------------------------------------------===//
392 /// lookup_or_add - Returns the value number for the specified value, assigning
393 /// it a new number if it did not have one before.
394 uint32_t ValueTable::lookup_or_add(Value* V) {
395 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
396 if (VI != valueNumbering.end())
400 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
401 Expression e = create_expression(BO);
403 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
404 if (EI != expressionNumbering.end()) {
405 valueNumbering.insert(std::make_pair(V, EI->second));
408 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
409 valueNumbering.insert(std::make_pair(V, nextValueNumber));
411 return nextValueNumber++;
413 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
414 Expression e = create_expression(C);
416 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
417 if (EI != expressionNumbering.end()) {
418 valueNumbering.insert(std::make_pair(V, EI->second));
421 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
422 valueNumbering.insert(std::make_pair(V, nextValueNumber));
424 return nextValueNumber++;
426 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
427 Expression e = create_expression(U);
429 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
430 if (EI != expressionNumbering.end()) {
431 valueNumbering.insert(std::make_pair(V, EI->second));
434 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
435 valueNumbering.insert(std::make_pair(V, nextValueNumber));
437 return nextValueNumber++;
439 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
440 Expression e = create_expression(U);
442 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
443 if (EI != expressionNumbering.end()) {
444 valueNumbering.insert(std::make_pair(V, EI->second));
447 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
448 valueNumbering.insert(std::make_pair(V, nextValueNumber));
450 return nextValueNumber++;
452 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
453 Expression e = create_expression(U);
455 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
456 if (EI != expressionNumbering.end()) {
457 valueNumbering.insert(std::make_pair(V, EI->second));
460 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
461 valueNumbering.insert(std::make_pair(V, nextValueNumber));
463 return nextValueNumber++;
465 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
466 Expression e = create_expression(U);
468 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
469 if (EI != expressionNumbering.end()) {
470 valueNumbering.insert(std::make_pair(V, EI->second));
473 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
474 valueNumbering.insert(std::make_pair(V, nextValueNumber));
476 return nextValueNumber++;
478 } else if (CastInst* U = dyn_cast<CastInst>(V)) {
479 Expression e = create_expression(U);
481 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
482 if (EI != expressionNumbering.end()) {
483 valueNumbering.insert(std::make_pair(V, EI->second));
486 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
487 valueNumbering.insert(std::make_pair(V, nextValueNumber));
489 return nextValueNumber++;
491 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
492 Expression e = create_expression(U);
494 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
495 if (EI != expressionNumbering.end()) {
496 valueNumbering.insert(std::make_pair(V, EI->second));
499 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
500 valueNumbering.insert(std::make_pair(V, nextValueNumber));
502 return nextValueNumber++;
505 valueNumbering.insert(std::make_pair(V, nextValueNumber));
506 return nextValueNumber++;
510 /// lookup - Returns the value number of the specified value. Fails if
511 /// the value has not yet been numbered.
512 uint32_t ValueTable::lookup(Value* V) const {
513 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
514 if (VI != valueNumbering.end())
517 assert(0 && "Value not numbered?");
522 /// add - Add the specified value with the given value number, removing
523 /// its old number, if any
524 void ValueTable::add(Value* V, uint32_t num) {
525 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
526 if (VI != valueNumbering.end())
527 valueNumbering.erase(VI);
528 valueNumbering.insert(std::make_pair(V, num));
531 /// clear - Remove all entries from the ValueTable
532 void ValueTable::clear() {
533 valueNumbering.clear();
534 expressionNumbering.clear();
538 /// erase - Remove a value from the value numbering
539 void ValueTable::erase(Value* V) {
540 valueNumbering.erase(V);
543 /// size - Return the number of assigned value numbers
544 unsigned ValueTable::size() {
545 // NOTE: zero is never assigned
546 return nextValueNumber;
549 //===----------------------------------------------------------------------===//
550 // ValueNumberedSet Class
551 //===----------------------------------------------------------------------===//
553 class ValueNumberedSet {
555 SmallPtrSet<Value*, 8> contents;
558 ValueNumberedSet() { numbers.resize(1); }
559 ValueNumberedSet(const ValueNumberedSet& other) {
560 numbers = other.numbers;
561 contents = other.contents;
564 typedef SmallPtrSet<Value*, 8>::iterator iterator;
566 iterator begin() { return contents.begin(); }
567 iterator end() { return contents.end(); }
569 bool insert(Value* v) { return contents.insert(v); }
570 void insert(iterator I, iterator E) { contents.insert(I, E); }
571 void erase(Value* v) { contents.erase(v); }
572 unsigned count(Value* v) { return contents.count(v); }
573 size_t size() { return contents.size(); }
575 void set(unsigned i) {
576 if (i >= numbers.size())
582 void operator=(const ValueNumberedSet& other) {
583 contents = other.contents;
584 numbers = other.numbers;
587 void reset(unsigned i) {
588 if (i < numbers.size())
592 bool test(unsigned i) {
593 if (i >= numbers.size())
596 return numbers.test(i);
605 //===----------------------------------------------------------------------===//
607 //===----------------------------------------------------------------------===//
611 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
612 bool runOnFunction(Function &F);
614 static char ID; // Pass identification, replacement for typeid
615 GVNPRE() : FunctionPass((intptr_t)&ID) { }
619 std::vector<Instruction*> createdExpressions;
621 DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
622 DenseMap<BasicBlock*, ValueNumberedSet> anticipatedIn;
623 DenseMap<BasicBlock*, ValueNumberedSet> generatedPhis;
625 // This transformation requires dominator postdominator info
626 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
627 AU.setPreservesCFG();
628 AU.addRequiredID(BreakCriticalEdgesID);
629 AU.addRequired<UnifyFunctionExitNodes>();
630 AU.addRequired<DominatorTree>();
634 // FIXME: eliminate or document these better
635 void dump(ValueNumberedSet& s) const ;
636 void clean(ValueNumberedSet& set) ;
637 Value* find_leader(ValueNumberedSet& vals, uint32_t v) ;
638 Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) ;
639 void phi_translate_set(ValueNumberedSet& anticIn, BasicBlock* pred,
640 BasicBlock* succ, ValueNumberedSet& out) ;
642 void topo_sort(ValueNumberedSet& set,
643 std::vector<Value*>& vec) ;
648 void val_insert(ValueNumberedSet& s, Value* v) ;
649 void val_replace(ValueNumberedSet& s, Value* v) ;
650 bool dependsOnInvoke(Value* V) ;
651 void buildsets_availout(BasicBlock::iterator I,
652 ValueNumberedSet& currAvail,
653 ValueNumberedSet& currPhis,
654 ValueNumberedSet& currExps,
655 SmallPtrSet<Value*, 16>& currTemps) ;
656 bool buildsets_anticout(BasicBlock* BB,
657 ValueNumberedSet& anticOut,
658 std::set<BasicBlock*>& visited) ;
659 unsigned buildsets_anticin(BasicBlock* BB,
660 ValueNumberedSet& anticOut,
661 ValueNumberedSet& currExps,
662 SmallPtrSet<Value*, 16>& currTemps,
663 std::set<BasicBlock*>& visited) ;
664 void buildsets(Function& F) ;
666 void insertion_pre(Value* e, BasicBlock* BB,
667 std::map<BasicBlock*, Value*>& avail,
668 std::map<BasicBlock*,ValueNumberedSet>& new_set) ;
669 unsigned insertion_mergepoint(std::vector<Value*>& workList,
670 df_iterator<DomTreeNode*>& D,
671 std::map<BasicBlock*, ValueNumberedSet>& new_set) ;
672 bool insertion(Function& F) ;
680 // createGVNPREPass - The public interface to this file...
681 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
683 RegisterPass<GVNPRE> X("gvnpre",
684 "Global Value Numbering/Partial Redundancy Elimination");
687 STATISTIC(NumInsertedVals, "Number of values inserted");
688 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
689 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
691 /// find_leader - Given a set and a value number, return the first
692 /// element of the set with that value number, or 0 if no such element
694 Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) {
698 for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
700 if (v == VN.lookup(*I))
703 assert(0 && "No leader found, but present bit is set?");
707 /// val_insert - Insert a value into a set only if there is not a value
708 /// with the same value number already in the set
709 void GVNPRE::val_insert(ValueNumberedSet& s, Value* v) {
710 uint32_t num = VN.lookup(v);
715 /// val_replace - Insert a value into a set, replacing any values already in
716 /// the set that have the same value number
717 void GVNPRE::val_replace(ValueNumberedSet& s, Value* v) {
718 uint32_t num = VN.lookup(v);
719 Value* leader = find_leader(s, num);
726 /// phi_translate - Given a value, its parent block, and a predecessor of its
727 /// parent, translate the value into legal for the predecessor block. This
728 /// means translating its operands (and recursively, their operands) through
729 /// any phi nodes in the parent into values available in the predecessor
730 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
735 if (CastInst* U = dyn_cast<CastInst>(V)) {
737 if (isa<Instruction>(U->getOperand(0)))
738 newOp1 = phi_translate(U->getOperand(0), pred, succ);
740 newOp1 = U->getOperand(0);
745 if (newOp1 != U->getOperand(0)) {
746 Instruction* newVal = 0;
747 if (CastInst* C = dyn_cast<CastInst>(U))
748 newVal = CastInst::create(C->getOpcode(),
749 newOp1, C->getType(),
750 C->getName()+".expr");
752 uint32_t v = VN.lookup_or_add(newVal);
754 Value* leader = find_leader(availableOut[pred], v);
756 createdExpressions.push_back(newVal);
766 } if (isa<BinaryOperator>(V) || isa<CmpInst>(V) ||
767 isa<ExtractElementInst>(V)) {
768 User* U = cast<User>(V);
771 if (isa<Instruction>(U->getOperand(0)))
772 newOp1 = phi_translate(U->getOperand(0), pred, succ);
774 newOp1 = U->getOperand(0);
780 if (isa<Instruction>(U->getOperand(1)))
781 newOp2 = phi_translate(U->getOperand(1), pred, succ);
783 newOp2 = U->getOperand(1);
788 if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
789 Instruction* newVal = 0;
790 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
791 newVal = BinaryOperator::create(BO->getOpcode(),
793 BO->getName()+".expr");
794 else if (CmpInst* C = dyn_cast<CmpInst>(U))
795 newVal = CmpInst::create(C->getOpcode(),
798 C->getName()+".expr");
799 else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
800 newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
802 uint32_t v = VN.lookup_or_add(newVal);
804 Value* leader = find_leader(availableOut[pred], v);
806 createdExpressions.push_back(newVal);
815 // Ternary Operations
816 } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
817 isa<SelectInst>(V)) {
818 User* U = cast<User>(V);
821 if (isa<Instruction>(U->getOperand(0)))
822 newOp1 = phi_translate(U->getOperand(0), pred, succ);
824 newOp1 = U->getOperand(0);
830 if (isa<Instruction>(U->getOperand(1)))
831 newOp2 = phi_translate(U->getOperand(1), pred, succ);
833 newOp2 = U->getOperand(1);
839 if (isa<Instruction>(U->getOperand(2)))
840 newOp3 = phi_translate(U->getOperand(2), pred, succ);
842 newOp3 = U->getOperand(2);
847 if (newOp1 != U->getOperand(0) ||
848 newOp2 != U->getOperand(1) ||
849 newOp3 != U->getOperand(2)) {
850 Instruction* newVal = 0;
851 if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
852 newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
853 S->getName()+".expr");
854 else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
855 newVal = new InsertElementInst(newOp1, newOp2, newOp3,
856 I->getName()+".expr");
857 else if (SelectInst* I = dyn_cast<SelectInst>(U))
858 newVal = new SelectInst(newOp1, newOp2, newOp3, I->getName()+".expr");
860 uint32_t v = VN.lookup_or_add(newVal);
862 Value* leader = find_leader(availableOut[pred], v);
864 createdExpressions.push_back(newVal);
874 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
876 if (isa<Instruction>(U->getPointerOperand()))
877 newOp1 = phi_translate(U->getPointerOperand(), pred, succ);
879 newOp1 = U->getPointerOperand();
884 bool changed_idx = false;
885 std::vector<Value*> newIdx;
886 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
888 if (isa<Instruction>(*I)) {
889 Value* newVal = phi_translate(*I, pred, succ);
890 newIdx.push_back(newVal);
894 newIdx.push_back(*I);
897 if (newOp1 != U->getPointerOperand() || changed_idx) {
898 Instruction* newVal = new GetElementPtrInst(newOp1,
899 &newIdx[0], newIdx.size(),
900 U->getName()+".expr");
902 uint32_t v = VN.lookup_or_add(newVal);
904 Value* leader = find_leader(availableOut[pred], v);
906 createdExpressions.push_back(newVal);
916 } else if (PHINode* P = dyn_cast<PHINode>(V)) {
917 if (P->getParent() == succ)
918 return P->getIncomingValueForBlock(pred);
924 /// phi_translate_set - Perform phi translation on every element of a set
925 void GVNPRE::phi_translate_set(ValueNumberedSet& anticIn,
926 BasicBlock* pred, BasicBlock* succ,
927 ValueNumberedSet& out) {
928 for (ValueNumberedSet::iterator I = anticIn.begin(),
929 E = anticIn.end(); I != E; ++I) {
930 Value* V = phi_translate(*I, pred, succ);
931 if (V != 0 && !out.test(VN.lookup_or_add(V))) {
933 out.set(VN.lookup(V));
938 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of
939 /// whose inputs is an invoke instruction. If this is true, we cannot safely
940 /// PRE the instruction or anything that depends on it.
941 bool GVNPRE::dependsOnInvoke(Value* V) {
942 if (PHINode* p = dyn_cast<PHINode>(V)) {
943 for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I)
944 if (isa<InvokeInst>(*I))
952 /// clean - Remove all non-opaque values from the set whose operands are not
953 /// themselves in the set, as well as all values that depend on invokes (see
955 void GVNPRE::clean(ValueNumberedSet& set) {
956 std::vector<Value*> worklist;
957 worklist.reserve(set.size());
958 topo_sort(set, worklist);
960 for (unsigned i = 0; i < worklist.size(); ++i) {
961 Value* v = worklist[i];
964 if (CastInst* U = dyn_cast<CastInst>(v)) {
965 bool lhsValid = !isa<Instruction>(U->getOperand(0));
966 lhsValid |= set.test(VN.lookup(U->getOperand(0)));
968 lhsValid = !dependsOnInvoke(U->getOperand(0));
972 set.reset(VN.lookup(U));
976 } else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
977 isa<ExtractElementInst>(v)) {
978 User* U = cast<User>(v);
980 bool lhsValid = !isa<Instruction>(U->getOperand(0));
981 lhsValid |= set.test(VN.lookup(U->getOperand(0)));
983 lhsValid = !dependsOnInvoke(U->getOperand(0));
985 bool rhsValid = !isa<Instruction>(U->getOperand(1));
986 rhsValid |= set.test(VN.lookup(U->getOperand(1)));
988 rhsValid = !dependsOnInvoke(U->getOperand(1));
990 if (!lhsValid || !rhsValid) {
992 set.reset(VN.lookup(U));
995 // Handle ternary ops
996 } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
997 isa<SelectInst>(v)) {
998 User* U = cast<User>(v);
1000 bool lhsValid = !isa<Instruction>(U->getOperand(0));
1001 lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1003 lhsValid = !dependsOnInvoke(U->getOperand(0));
1005 bool rhsValid = !isa<Instruction>(U->getOperand(1));
1006 rhsValid |= set.test(VN.lookup(U->getOperand(1)));
1008 rhsValid = !dependsOnInvoke(U->getOperand(1));
1010 bool thirdValid = !isa<Instruction>(U->getOperand(2));
1011 thirdValid |= set.test(VN.lookup(U->getOperand(2)));
1013 thirdValid = !dependsOnInvoke(U->getOperand(2));
1015 if (!lhsValid || !rhsValid || !thirdValid) {
1017 set.reset(VN.lookup(U));
1020 // Handle varargs ops
1021 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) {
1022 bool ptrValid = !isa<Instruction>(U->getPointerOperand());
1023 ptrValid |= set.test(VN.lookup(U->getPointerOperand()));
1025 ptrValid = !dependsOnInvoke(U->getPointerOperand());
1027 bool varValid = true;
1028 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
1031 varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I));
1032 varValid &= !dependsOnInvoke(*I);
1035 if (!ptrValid || !varValid) {
1037 set.reset(VN.lookup(U));
1043 /// topo_sort - Given a set of values, sort them by topological
1044 /// order into the provided vector.
1045 void GVNPRE::topo_sort(ValueNumberedSet& set, std::vector<Value*>& vec) {
1046 SmallPtrSet<Value*, 16> visited;
1047 std::vector<Value*> stack;
1048 for (ValueNumberedSet::iterator I = set.begin(), E = set.end();
1050 if (visited.count(*I) == 0)
1051 stack.push_back(*I);
1053 while (!stack.empty()) {
1054 Value* e = stack.back();
1057 if (CastInst* U = dyn_cast<CastInst>(e)) {
1058 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1060 if (l != 0 && isa<Instruction>(l) &&
1061 visited.count(l) == 0)
1069 // Handle binary ops
1070 } else if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1071 isa<ExtractElementInst>(e)) {
1072 User* U = cast<User>(e);
1073 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1074 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1076 if (l != 0 && isa<Instruction>(l) &&
1077 visited.count(l) == 0)
1079 else if (r != 0 && isa<Instruction>(r) &&
1080 visited.count(r) == 0)
1088 // Handle ternary ops
1089 } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) ||
1090 isa<SelectInst>(e)) {
1091 User* U = cast<User>(e);
1092 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1093 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1094 Value* m = find_leader(set, VN.lookup(U->getOperand(2)));
1096 if (l != 0 && isa<Instruction>(l) &&
1097 visited.count(l) == 0)
1099 else if (r != 0 && isa<Instruction>(r) &&
1100 visited.count(r) == 0)
1102 else if (m != 0 && isa<Instruction>(m) &&
1103 visited.count(m) == 0)
1111 // Handle vararg ops
1112 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) {
1113 Value* p = find_leader(set, VN.lookup(U->getPointerOperand()));
1115 if (p != 0 && isa<Instruction>(p) &&
1116 visited.count(p) == 0)
1119 bool push_va = false;
1120 for (GetElementPtrInst::op_iterator I = U->idx_begin(),
1121 E = U->idx_end(); I != E; ++I) {
1122 Value * v = find_leader(set, VN.lookup(*I));
1123 if (v != 0 && isa<Instruction>(v) && visited.count(v) == 0) {
1136 // Handle opaque ops
1148 /// dump - Dump a set of values to standard error
1149 void GVNPRE::dump(ValueNumberedSet& s) const {
1151 for (ValueNumberedSet::iterator I = s.begin(), E = s.end();
1153 DOUT << "" << VN.lookup(*I) << ": ";
1154 DEBUG((*I)->dump());
1159 /// elimination - Phase 3 of the main algorithm. Perform full redundancy
1160 /// elimination by walking the dominator tree and removing any instruction that
1161 /// is dominated by another instruction with the same value number.
1162 bool GVNPRE::elimination() {
1163 bool changed_function = false;
1165 std::vector<std::pair<Instruction*, Value*> > replace;
1166 std::vector<Instruction*> erase;
1168 DominatorTree& DT = getAnalysis<DominatorTree>();
1170 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1171 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1172 BasicBlock* BB = DI->getBlock();
1174 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1177 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) ||
1178 isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) ||
1179 isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) ||
1180 isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) {
1182 if (availableOut[BB].test(VN.lookup(BI)) && !availableOut[BB].count(BI)) {
1183 Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
1184 if (Instruction* Instr = dyn_cast<Instruction>(leader))
1185 if (Instr->getParent() != 0 && Instr != BI) {
1186 replace.push_back(std::make_pair(BI, leader));
1187 erase.push_back(BI);
1195 while (!replace.empty()) {
1196 std::pair<Instruction*, Value*> rep = replace.back();
1198 rep.first->replaceAllUsesWith(rep.second);
1199 changed_function = true;
1202 for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
1204 (*I)->eraseFromParent();
1206 return changed_function;
1209 /// cleanup - Delete any extraneous values that were created to represent
1210 /// expressions without leaders.
1211 void GVNPRE::cleanup() {
1212 while (!createdExpressions.empty()) {
1213 Instruction* I = createdExpressions.back();
1214 createdExpressions.pop_back();
1220 /// buildsets_availout - When calculating availability, handle an instruction
1221 /// by inserting it into the appropriate sets
1222 void GVNPRE::buildsets_availout(BasicBlock::iterator I,
1223 ValueNumberedSet& currAvail,
1224 ValueNumberedSet& currPhis,
1225 ValueNumberedSet& currExps,
1226 SmallPtrSet<Value*, 16>& currTemps) {
1228 if (PHINode* p = dyn_cast<PHINode>(I)) {
1229 unsigned num = VN.lookup_or_add(p);
1235 } else if (CastInst* U = dyn_cast<CastInst>(I)) {
1236 Value* leftValue = U->getOperand(0);
1238 unsigned num = VN.lookup_or_add(U);
1240 if (isa<Instruction>(leftValue))
1241 if (!currExps.test(VN.lookup(leftValue))) {
1242 currExps.insert(leftValue);
1243 currExps.set(VN.lookup(leftValue));
1246 if (!currExps.test(num)) {
1251 // Handle binary ops
1252 } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
1253 isa<ExtractElementInst>(I)) {
1254 User* U = cast<User>(I);
1255 Value* leftValue = U->getOperand(0);
1256 Value* rightValue = U->getOperand(1);
1258 unsigned num = VN.lookup_or_add(U);
1260 if (isa<Instruction>(leftValue))
1261 if (!currExps.test(VN.lookup(leftValue))) {
1262 currExps.insert(leftValue);
1263 currExps.set(VN.lookup(leftValue));
1266 if (isa<Instruction>(rightValue))
1267 if (!currExps.test(VN.lookup(rightValue))) {
1268 currExps.insert(rightValue);
1269 currExps.set(VN.lookup(rightValue));
1272 if (!currExps.test(num)) {
1277 // Handle ternary ops
1278 } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) ||
1279 isa<SelectInst>(I)) {
1280 User* U = cast<User>(I);
1281 Value* leftValue = U->getOperand(0);
1282 Value* rightValue = U->getOperand(1);
1283 Value* thirdValue = U->getOperand(2);
1285 VN.lookup_or_add(U);
1287 unsigned num = VN.lookup_or_add(U);
1289 if (isa<Instruction>(leftValue))
1290 if (!currExps.test(VN.lookup(leftValue))) {
1291 currExps.insert(leftValue);
1292 currExps.set(VN.lookup(leftValue));
1294 if (isa<Instruction>(rightValue))
1295 if (!currExps.test(VN.lookup(rightValue))) {
1296 currExps.insert(rightValue);
1297 currExps.set(VN.lookup(rightValue));
1299 if (isa<Instruction>(thirdValue))
1300 if (!currExps.test(VN.lookup(thirdValue))) {
1301 currExps.insert(thirdValue);
1302 currExps.set(VN.lookup(thirdValue));
1305 if (!currExps.test(num)) {
1310 // Handle vararg ops
1311 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) {
1312 Value* ptrValue = U->getPointerOperand();
1314 VN.lookup_or_add(U);
1316 unsigned num = VN.lookup_or_add(U);
1318 if (isa<Instruction>(ptrValue))
1319 if (!currExps.test(VN.lookup(ptrValue))) {
1320 currExps.insert(ptrValue);
1321 currExps.set(VN.lookup(ptrValue));
1324 for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end();
1326 if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) {
1327 currExps.insert(*OI);
1328 currExps.set(VN.lookup(*OI));
1331 if (!currExps.test(VN.lookup(U))) {
1336 // Handle opaque ops
1337 } else if (!I->isTerminator()){
1338 VN.lookup_or_add(I);
1340 currTemps.insert(I);
1343 if (!I->isTerminator())
1344 if (!currAvail.test(VN.lookup(I))) {
1345 currAvail.insert(I);
1346 currAvail.set(VN.lookup(I));
1350 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
1351 /// set as a function of the ANTIC_IN set of the block's predecessors
1352 bool GVNPRE::buildsets_anticout(BasicBlock* BB,
1353 ValueNumberedSet& anticOut,
1354 std::set<BasicBlock*>& visited) {
1355 if (BB->getTerminator()->getNumSuccessors() == 1) {
1356 if (BB->getTerminator()->getSuccessor(0) != BB &&
1357 visited.count(BB->getTerminator()->getSuccessor(0)) == 0) {
1361 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
1362 BB, BB->getTerminator()->getSuccessor(0), anticOut);
1364 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
1365 BasicBlock* first = BB->getTerminator()->getSuccessor(0);
1366 for (ValueNumberedSet::iterator I = anticipatedIn[first].begin(),
1367 E = anticipatedIn[first].end(); I != E; ++I) {
1368 anticOut.insert(*I);
1369 anticOut.set(VN.lookup(*I));
1372 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
1373 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
1374 ValueNumberedSet& succAnticIn = anticipatedIn[currSucc];
1376 std::vector<Value*> temp;
1378 for (ValueNumberedSet::iterator I = anticOut.begin(),
1379 E = anticOut.end(); I != E; ++I)
1380 if (!succAnticIn.test(VN.lookup(*I)))
1383 for (std::vector<Value*>::iterator I = temp.begin(), E = temp.end();
1386 anticOut.reset(VN.lookup(*I));
1394 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
1395 /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
1396 /// sets populated in buildsets_availout
1397 unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
1398 ValueNumberedSet& anticOut,
1399 ValueNumberedSet& currExps,
1400 SmallPtrSet<Value*, 16>& currTemps,
1401 std::set<BasicBlock*>& visited) {
1402 ValueNumberedSet& anticIn = anticipatedIn[BB];
1403 unsigned old = anticIn.size();
1405 bool defer = buildsets_anticout(BB, anticOut, visited);
1411 for (ValueNumberedSet::iterator I = anticOut.begin(),
1412 E = anticOut.end(); I != E; ++I) {
1414 anticIn.set(VN.lookup(*I));
1416 for (ValueNumberedSet::iterator I = currExps.begin(),
1417 E = currExps.end(); I != E; ++I) {
1418 if (!anticIn.test(VN.lookup(*I))) {
1420 anticIn.set(VN.lookup(*I));
1424 for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
1425 E = currTemps.end(); I != E; ++I) {
1427 anticIn.reset(VN.lookup(*I));
1433 if (old != anticIn.size())
1439 /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
1440 /// and the ANTIC_IN sets.
1441 void GVNPRE::buildsets(Function& F) {
1442 std::map<BasicBlock*, ValueNumberedSet> generatedExpressions;
1443 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
1445 DominatorTree &DT = getAnalysis<DominatorTree>();
1447 // Phase 1, Part 1: calculate AVAIL_OUT
1449 // Top-down walk of the dominator tree
1450 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1451 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1453 // Get the sets to update for this block
1454 ValueNumberedSet& currExps = generatedExpressions[DI->getBlock()];
1455 ValueNumberedSet& currPhis = generatedPhis[DI->getBlock()];
1456 SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
1457 ValueNumberedSet& currAvail = availableOut[DI->getBlock()];
1459 BasicBlock* BB = DI->getBlock();
1461 // A block inherits AVAIL_OUT from its dominator
1462 if (DI->getIDom() != 0)
1463 currAvail = availableOut[DI->getIDom()->getBlock()];
1465 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1467 buildsets_availout(BI, currAvail, currPhis, currExps,
1472 // Phase 1, Part 2: calculate ANTIC_IN
1474 std::set<BasicBlock*> visited;
1475 SmallPtrSet<BasicBlock*, 4> block_changed;
1476 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
1477 block_changed.insert(FI);
1479 bool changed = true;
1480 unsigned iterations = 0;
1484 ValueNumberedSet anticOut;
1486 // Postorder walk of the CFG
1487 for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
1488 BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) {
1489 BasicBlock* BB = *BBI;
1491 if (block_changed.count(BB) != 0) {
1492 unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
1493 generatedTemporaries[BB], visited);
1502 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1504 block_changed.insert(*PI);
1507 block_changed.erase(BB);
1509 changed |= (ret == 2);
1518 /// insertion_pre - When a partial redundancy has been identified, eliminate it
1519 /// by inserting appropriate values into the predecessors and a phi node in
1521 void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
1522 std::map<BasicBlock*, Value*>& avail,
1523 std::map<BasicBlock*, ValueNumberedSet>& new_sets) {
1524 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1525 Value* e2 = avail[*PI];
1526 if (!availableOut[*PI].test(VN.lookup(e2))) {
1527 User* U = cast<User>(e2);
1530 if (isa<BinaryOperator>(U->getOperand(0)) ||
1531 isa<CmpInst>(U->getOperand(0)) ||
1532 isa<ShuffleVectorInst>(U->getOperand(0)) ||
1533 isa<ExtractElementInst>(U->getOperand(0)) ||
1534 isa<InsertElementInst>(U->getOperand(0)) ||
1535 isa<SelectInst>(U->getOperand(0)) ||
1536 isa<CastInst>(U->getOperand(0)) ||
1537 isa<GetElementPtrInst>(U->getOperand(0)))
1538 s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
1540 s1 = U->getOperand(0);
1544 if (isa<BinaryOperator>(U) ||
1546 isa<ShuffleVectorInst>(U) ||
1547 isa<ExtractElementInst>(U) ||
1548 isa<InsertElementInst>(U) ||
1550 if (isa<BinaryOperator>(U->getOperand(1)) ||
1551 isa<CmpInst>(U->getOperand(1)) ||
1552 isa<ShuffleVectorInst>(U->getOperand(1)) ||
1553 isa<ExtractElementInst>(U->getOperand(1)) ||
1554 isa<InsertElementInst>(U->getOperand(1)) ||
1555 isa<SelectInst>(U->getOperand(1)) ||
1556 isa<CastInst>(U->getOperand(1)) ||
1557 isa<GetElementPtrInst>(U->getOperand(1))) {
1558 s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
1560 s2 = U->getOperand(1);
1563 // Ternary Operators
1565 if (isa<ShuffleVectorInst>(U) ||
1566 isa<InsertElementInst>(U) ||
1568 if (isa<BinaryOperator>(U->getOperand(2)) ||
1569 isa<CmpInst>(U->getOperand(2)) ||
1570 isa<ShuffleVectorInst>(U->getOperand(2)) ||
1571 isa<ExtractElementInst>(U->getOperand(2)) ||
1572 isa<InsertElementInst>(U->getOperand(2)) ||
1573 isa<SelectInst>(U->getOperand(2)) ||
1574 isa<CastInst>(U->getOperand(2)) ||
1575 isa<GetElementPtrInst>(U->getOperand(2))) {
1576 s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2)));
1578 s3 = U->getOperand(2);
1582 std::vector<Value*> sVarargs;
1583 if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) {
1584 for (GetElementPtrInst::op_iterator OI = G->idx_begin(),
1585 OE = G->idx_end(); OI != OE; ++OI) {
1586 if (isa<BinaryOperator>(*OI) ||
1587 isa<CmpInst>(*OI) ||
1588 isa<ShuffleVectorInst>(*OI) ||
1589 isa<ExtractElementInst>(*OI) ||
1590 isa<InsertElementInst>(*OI) ||
1591 isa<SelectInst>(*OI) ||
1592 isa<CastInst>(*OI) ||
1593 isa<GetElementPtrInst>(*OI)) {
1594 sVarargs.push_back(find_leader(availableOut[*PI],
1597 sVarargs.push_back(*OI);
1603 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
1604 newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
1605 BO->getName()+".gvnpre",
1606 (*PI)->getTerminator());
1607 else if (CmpInst* C = dyn_cast<CmpInst>(U))
1608 newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
1609 C->getName()+".gvnpre",
1610 (*PI)->getTerminator());
1611 else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
1612 newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
1613 (*PI)->getTerminator());
1614 else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
1615 newVal = new InsertElementInst(s1, s2, s3, S->getName()+".gvnpre",
1616 (*PI)->getTerminator());
1617 else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
1618 newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
1619 (*PI)->getTerminator());
1620 else if (SelectInst* S = dyn_cast<SelectInst>(U))
1621 newVal = new SelectInst(s1, s2, s3, S->getName()+".gvnpre",
1622 (*PI)->getTerminator());
1623 else if (CastInst* C = dyn_cast<CastInst>(U))
1624 newVal = CastInst::create(C->getOpcode(), s1, C->getType(),
1625 C->getName()+".gvnpre",
1626 (*PI)->getTerminator());
1627 else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U))
1628 newVal = new GetElementPtrInst(s1, &sVarargs[0], sVarargs.size(),
1629 G->getName()+".gvnpre",
1630 (*PI)->getTerminator());
1633 VN.add(newVal, VN.lookup(U));
1635 ValueNumberedSet& predAvail = availableOut[*PI];
1636 val_replace(predAvail, newVal);
1637 val_replace(new_sets[*PI], newVal);
1638 predAvail.set(VN.lookup(newVal));
1640 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1641 if (av != avail.end())
1643 avail.insert(std::make_pair(*PI, newVal));
1651 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1653 p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
1655 p->addIncoming(avail[*PI], *PI);
1658 VN.add(p, VN.lookup(e));
1659 val_replace(availableOut[BB], p);
1660 availableOut[BB].set(VN.lookup(e));
1661 generatedPhis[BB].insert(p);
1662 generatedPhis[BB].set(VN.lookup(e));
1663 new_sets[BB].insert(p);
1664 new_sets[BB].set(VN.lookup(e));
1669 /// insertion_mergepoint - When walking the dom tree, check at each merge
1670 /// block for the possibility of a partial redundancy. If present, eliminate it
1671 unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
1672 df_iterator<DomTreeNode*>& D,
1673 std::map<BasicBlock*, ValueNumberedSet >& new_sets) {
1674 bool changed_function = false;
1675 bool new_stuff = false;
1677 BasicBlock* BB = D->getBlock();
1678 for (unsigned i = 0; i < workList.size(); ++i) {
1679 Value* e = workList[i];
1681 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1682 isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
1683 isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) ||
1684 isa<GetElementPtrInst>(e)) {
1685 if (availableOut[D->getIDom()->getBlock()].test(VN.lookup(e)))
1688 std::map<BasicBlock*, Value*> avail;
1689 bool by_some = false;
1690 bool all_same = true;
1691 Value * first_s = 0;
1693 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
1695 Value *e2 = phi_translate(e, *PI, BB);
1696 Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
1699 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1700 if (av != avail.end())
1702 avail.insert(std::make_pair(*PI, e2));
1705 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1706 if (av != avail.end())
1708 avail.insert(std::make_pair(*PI, e3));
1713 else if (first_s != e3)
1718 if (by_some && !all_same &&
1719 !generatedPhis[BB].test(VN.lookup(e))) {
1720 insertion_pre(e, BB, avail, new_sets);
1722 changed_function = true;
1728 unsigned retval = 0;
1729 if (changed_function)
1737 /// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for
1738 /// merge points. When one is found, check for a partial redundancy. If one is
1739 /// present, eliminate it. Repeat this walk until no changes are made.
1740 bool GVNPRE::insertion(Function& F) {
1741 bool changed_function = false;
1743 DominatorTree &DT = getAnalysis<DominatorTree>();
1745 std::map<BasicBlock*, ValueNumberedSet> new_sets;
1746 bool new_stuff = true;
1749 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1750 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1751 BasicBlock* BB = DI->getBlock();
1756 ValueNumberedSet& availOut = availableOut[BB];
1757 ValueNumberedSet& anticIn = anticipatedIn[BB];
1759 // Replace leaders with leaders inherited from dominator
1760 if (DI->getIDom() != 0) {
1761 ValueNumberedSet& dom_set = new_sets[DI->getIDom()->getBlock()];
1762 for (ValueNumberedSet::iterator I = dom_set.begin(),
1763 E = dom_set.end(); I != E; ++I) {
1764 val_replace(new_sets[BB], *I);
1765 val_replace(availOut, *I);
1769 // If there is more than one predecessor...
1770 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
1771 std::vector<Value*> workList;
1772 workList.reserve(anticIn.size());
1773 topo_sort(anticIn, workList);
1775 unsigned result = insertion_mergepoint(workList, DI, new_sets);
1777 changed_function = true;
1784 return changed_function;
1787 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1790 bool GVNPRE::runOnFunction(Function &F) {
1791 // Clean out global sets from any previous functions
1793 createdExpressions.clear();
1794 availableOut.clear();
1795 anticipatedIn.clear();
1796 generatedPhis.clear();
1798 bool changed_function = false;
1800 // Phase 1: BuildSets
1801 // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1805 // This phase inserts values to make partially redundant values
1807 changed_function |= insertion(F);
1809 // Phase 3: Eliminate
1810 // This phase performs trivial full redundancy elimination
1811 changed_function |= elimination();
1814 // This phase cleans up values that were created solely
1815 // as leaders for expressions
1818 return changed_function;