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 //===----------------------------------------------------------------------===//
551 //===----------------------------------------------------------------------===//
555 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
556 bool runOnFunction(Function &F);
558 static char ID; // Pass identification, replacement for typeid
559 GVNPRE() : FunctionPass((intptr_t)&ID) { }
563 std::vector<Instruction*> createdExpressions;
565 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > availableOut;
566 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > anticipatedIn;
567 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedPhis;
569 // This transformation requires dominator postdominator info
570 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
571 AU.setPreservesCFG();
572 AU.addRequiredID(BreakCriticalEdgesID);
573 AU.addRequired<UnifyFunctionExitNodes>();
574 AU.addRequired<DominatorTree>();
578 // FIXME: eliminate or document these better
579 void dump(const SmallPtrSet<Value*, 16>& s) const;
580 void clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet);
581 Value* find_leader(SmallPtrSet<Value*, 16>& vals,
583 Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ);
584 void phi_translate_set(SmallPtrSet<Value*, 16>& anticIn, BasicBlock* pred,
585 BasicBlock* succ, SmallPtrSet<Value*, 16>& out);
587 void topo_sort(SmallPtrSet<Value*, 16>& set,
588 std::vector<Value*>& vec);
593 void val_insert(SmallPtrSet<Value*, 16>& s, Value* v);
594 void val_replace(SmallPtrSet<Value*, 16>& s, Value* v);
595 bool dependsOnInvoke(Value* V);
596 void buildsets_availout(BasicBlock::iterator I,
597 SmallPtrSet<Value*, 16>& currAvail,
598 SmallPtrSet<Value*, 16>& currPhis,
599 SmallPtrSet<Value*, 16>& currExps,
600 SmallPtrSet<Value*, 16>& currTemps,
601 BitVector& availNumbers,
602 BitVector& expNumbers);
603 bool buildsets_anticout(BasicBlock* BB,
604 SmallPtrSet<Value*, 16>& anticOut,
605 std::set<BasicBlock*>& visited);
606 unsigned buildsets_anticin(BasicBlock* BB,
607 SmallPtrSet<Value*, 16>& anticOut,
608 SmallPtrSet<Value*, 16>& currExps,
609 SmallPtrSet<Value*, 16>& currTemps,
610 std::set<BasicBlock*>& visited);
611 void buildsets(Function& F);
613 void insertion_pre(Value* e, BasicBlock* BB,
614 std::map<BasicBlock*, Value*>& avail,
615 std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_set);
616 unsigned insertion_mergepoint(std::vector<Value*>& workList,
617 df_iterator<DomTreeNode*>& D,
618 std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_set);
619 bool insertion(Function& F);
627 // createGVNPREPass - The public interface to this file...
628 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
630 RegisterPass<GVNPRE> X("gvnpre",
631 "Global Value Numbering/Partial Redundancy Elimination");
634 STATISTIC(NumInsertedVals, "Number of values inserted");
635 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
636 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
638 /// find_leader - Given a set and a value number, return the first
639 /// element of the set with that value number, or 0 if no such element
641 Value* GVNPRE::find_leader(SmallPtrSet<Value*, 16>& vals, uint32_t v) {
642 for (SmallPtrSet<Value*, 16>::iterator I = vals.begin(), E = vals.end();
644 if (v == VN.lookup(*I))
650 /// val_insert - Insert a value into a set only if there is not a value
651 /// with the same value number already in the set
652 void GVNPRE::val_insert(SmallPtrSet<Value*, 16>& s, Value* v) {
653 uint32_t num = VN.lookup(v);
654 Value* leader = find_leader(s, num);
659 /// val_replace - Insert a value into a set, replacing any values already in
660 /// the set that have the same value number
661 void GVNPRE::val_replace(SmallPtrSet<Value*, 16>& s, Value* v) {
662 uint32_t num = VN.lookup(v);
663 Value* leader = find_leader(s, num);
664 while (leader != 0) {
666 leader = find_leader(s, num);
671 /// phi_translate - Given a value, its parent block, and a predecessor of its
672 /// parent, translate the value into legal for the predecessor block. This
673 /// means translating its operands (and recursively, their operands) through
674 /// any phi nodes in the parent into values available in the predecessor
675 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
680 if (CastInst* U = dyn_cast<CastInst>(V)) {
682 if (isa<Instruction>(U->getOperand(0)))
683 newOp1 = phi_translate(U->getOperand(0), pred, succ);
685 newOp1 = U->getOperand(0);
690 if (newOp1 != U->getOperand(0)) {
691 Instruction* newVal = 0;
692 if (CastInst* C = dyn_cast<CastInst>(U))
693 newVal = CastInst::create(C->getOpcode(),
694 newOp1, C->getType(),
695 C->getName()+".expr");
697 uint32_t v = VN.lookup_or_add(newVal);
699 Value* leader = find_leader(availableOut[pred], v);
701 createdExpressions.push_back(newVal);
711 } if (isa<BinaryOperator>(V) || isa<CmpInst>(V) ||
712 isa<ExtractElementInst>(V)) {
713 User* U = cast<User>(V);
716 if (isa<Instruction>(U->getOperand(0)))
717 newOp1 = phi_translate(U->getOperand(0), pred, succ);
719 newOp1 = U->getOperand(0);
725 if (isa<Instruction>(U->getOperand(1)))
726 newOp2 = phi_translate(U->getOperand(1), pred, succ);
728 newOp2 = U->getOperand(1);
733 if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
734 Instruction* newVal = 0;
735 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
736 newVal = BinaryOperator::create(BO->getOpcode(),
738 BO->getName()+".expr");
739 else if (CmpInst* C = dyn_cast<CmpInst>(U))
740 newVal = CmpInst::create(C->getOpcode(),
743 C->getName()+".expr");
744 else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
745 newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
747 uint32_t v = VN.lookup_or_add(newVal);
749 Value* leader = find_leader(availableOut[pred], v);
751 createdExpressions.push_back(newVal);
760 // Ternary Operations
761 } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
762 isa<SelectInst>(V)) {
763 User* U = cast<User>(V);
766 if (isa<Instruction>(U->getOperand(0)))
767 newOp1 = phi_translate(U->getOperand(0), pred, succ);
769 newOp1 = U->getOperand(0);
775 if (isa<Instruction>(U->getOperand(1)))
776 newOp2 = phi_translate(U->getOperand(1), pred, succ);
778 newOp2 = U->getOperand(1);
784 if (isa<Instruction>(U->getOperand(2)))
785 newOp3 = phi_translate(U->getOperand(2), pred, succ);
787 newOp3 = U->getOperand(2);
792 if (newOp1 != U->getOperand(0) ||
793 newOp2 != U->getOperand(1) ||
794 newOp3 != U->getOperand(2)) {
795 Instruction* newVal = 0;
796 if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
797 newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
798 S->getName()+".expr");
799 else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
800 newVal = new InsertElementInst(newOp1, newOp2, newOp3,
801 I->getName()+".expr");
802 else if (SelectInst* I = dyn_cast<SelectInst>(U))
803 newVal = new SelectInst(newOp1, newOp2, newOp3, I->getName()+".expr");
805 uint32_t v = VN.lookup_or_add(newVal);
807 Value* leader = find_leader(availableOut[pred], v);
809 createdExpressions.push_back(newVal);
819 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
821 if (isa<Instruction>(U->getPointerOperand()))
822 newOp1 = phi_translate(U->getPointerOperand(), pred, succ);
824 newOp1 = U->getPointerOperand();
829 bool changed_idx = false;
830 std::vector<Value*> newIdx;
831 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
833 if (isa<Instruction>(*I)) {
834 Value* newVal = phi_translate(*I, pred, succ);
835 newIdx.push_back(newVal);
839 newIdx.push_back(*I);
842 if (newOp1 != U->getPointerOperand() || changed_idx) {
843 Instruction* newVal = new GetElementPtrInst(newOp1,
844 &newIdx[0], newIdx.size(),
845 U->getName()+".expr");
847 uint32_t v = VN.lookup_or_add(newVal);
849 Value* leader = find_leader(availableOut[pred], v);
851 createdExpressions.push_back(newVal);
861 } else if (PHINode* P = dyn_cast<PHINode>(V)) {
862 if (P->getParent() == succ)
863 return P->getIncomingValueForBlock(pred);
869 /// phi_translate_set - Perform phi translation on every element of a set
870 void GVNPRE::phi_translate_set(SmallPtrSet<Value*, 16>& anticIn,
871 BasicBlock* pred, BasicBlock* succ,
872 SmallPtrSet<Value*, 16>& out) {
873 for (SmallPtrSet<Value*, 16>::iterator I = anticIn.begin(),
874 E = anticIn.end(); I != E; ++I) {
875 Value* V = phi_translate(*I, pred, succ);
881 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of
882 /// whose inputs is an invoke instruction. If this is true, we cannot safely
883 /// PRE the instruction or anything that depends on it.
884 bool GVNPRE::dependsOnInvoke(Value* V) {
885 if (PHINode* p = dyn_cast<PHINode>(V)) {
886 for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I)
887 if (isa<InvokeInst>(*I))
895 /// clean - Remove all non-opaque values from the set whose operands are not
896 /// themselves in the set, as well as all values that depend on invokes (see
898 void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
899 std::vector<Value*> worklist;
900 worklist.reserve(set.size());
901 topo_sort(set, worklist);
903 for (unsigned i = 0; i < worklist.size(); ++i) {
904 Value* v = worklist[i];
907 if (CastInst* U = dyn_cast<CastInst>(v)) {
908 bool lhsValid = !isa<Instruction>(U->getOperand(0));
909 lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0)));
911 lhsValid = !dependsOnInvoke(U->getOperand(0));
915 presentInSet.flip(VN.lookup(U));
919 } else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
920 isa<ExtractElementInst>(v)) {
921 User* U = cast<User>(v);
923 bool lhsValid = !isa<Instruction>(U->getOperand(0));
924 lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0)));
926 lhsValid = !dependsOnInvoke(U->getOperand(0));
928 bool rhsValid = !isa<Instruction>(U->getOperand(1));
929 rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1)));
931 rhsValid = !dependsOnInvoke(U->getOperand(1));
933 if (!lhsValid || !rhsValid) {
935 presentInSet.flip(VN.lookup(U));
938 // Handle ternary ops
939 } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
940 isa<SelectInst>(v)) {
941 User* U = cast<User>(v);
943 bool lhsValid = !isa<Instruction>(U->getOperand(0));
944 lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0)));
946 lhsValid = !dependsOnInvoke(U->getOperand(0));
948 bool rhsValid = !isa<Instruction>(U->getOperand(1));
949 rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1)));
951 rhsValid = !dependsOnInvoke(U->getOperand(1));
953 bool thirdValid = !isa<Instruction>(U->getOperand(2));
954 thirdValid |= presentInSet.test(VN.lookup(U->getOperand(2)));
956 thirdValid = !dependsOnInvoke(U->getOperand(2));
958 if (!lhsValid || !rhsValid || !thirdValid) {
960 presentInSet.flip(VN.lookup(U));
963 // Handle varargs ops
964 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) {
965 bool ptrValid = !isa<Instruction>(U->getPointerOperand());
966 ptrValid |= presentInSet.test(VN.lookup(U->getPointerOperand()));
968 ptrValid = !dependsOnInvoke(U->getPointerOperand());
970 bool varValid = true;
971 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
974 varValid &= !isa<Instruction>(*I) || presentInSet.test(VN.lookup(*I));
975 varValid &= !dependsOnInvoke(*I);
978 if (!ptrValid || !varValid) {
980 presentInSet.flip(VN.lookup(U));
986 /// topo_sort - Given a set of values, sort them by topological
987 /// order into the provided vector.
988 void GVNPRE::topo_sort(SmallPtrSet<Value*, 16>& set, std::vector<Value*>& vec) {
989 SmallPtrSet<Value*, 16> visited;
990 std::vector<Value*> stack;
991 for (SmallPtrSet<Value*, 16>::iterator I = set.begin(), E = set.end();
993 if (visited.count(*I) == 0)
996 while (!stack.empty()) {
997 Value* e = stack.back();
1000 if (CastInst* U = dyn_cast<CastInst>(e)) {
1001 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1003 if (l != 0 && isa<Instruction>(l) &&
1004 visited.count(l) == 0)
1012 // Handle binary ops
1013 } else if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1014 isa<ExtractElementInst>(e)) {
1015 User* U = cast<User>(e);
1016 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1017 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1019 if (l != 0 && isa<Instruction>(l) &&
1020 visited.count(l) == 0)
1022 else if (r != 0 && isa<Instruction>(r) &&
1023 visited.count(r) == 0)
1031 // Handle ternary ops
1032 } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) ||
1033 isa<SelectInst>(e)) {
1034 User* U = cast<User>(e);
1035 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1036 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1037 Value* m = find_leader(set, VN.lookup(U->getOperand(2)));
1039 if (l != 0 && isa<Instruction>(l) &&
1040 visited.count(l) == 0)
1042 else if (r != 0 && isa<Instruction>(r) &&
1043 visited.count(r) == 0)
1045 else if (m != 0 && isa<Instruction>(m) &&
1046 visited.count(m) == 0)
1054 // Handle vararg ops
1055 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) {
1056 Value* p = find_leader(set, VN.lookup(U->getPointerOperand()));
1058 if (p != 0 && isa<Instruction>(p) &&
1059 visited.count(p) == 0)
1062 bool push_va = false;
1063 for (GetElementPtrInst::op_iterator I = U->idx_begin(),
1064 E = U->idx_end(); I != E; ++I) {
1065 Value * v = find_leader(set, VN.lookup(*I));
1066 if (v != 0 && isa<Instruction>(v) && visited.count(v) == 0) {
1079 // Handle opaque ops
1091 /// dump - Dump a set of values to standard error
1092 void GVNPRE::dump(const SmallPtrSet<Value*, 16>& s) const {
1094 for (SmallPtrSet<Value*, 16>::iterator I = s.begin(), E = s.end();
1096 DOUT << "" << VN.lookup(*I) << ": ";
1097 DEBUG((*I)->dump());
1102 /// elimination - Phase 3 of the main algorithm. Perform full redundancy
1103 /// elimination by walking the dominator tree and removing any instruction that
1104 /// is dominated by another instruction with the same value number.
1105 bool GVNPRE::elimination() {
1106 DOUT << "\n\nPhase 3: Elimination\n\n";
1108 bool changed_function = false;
1110 std::vector<std::pair<Instruction*, Value*> > replace;
1111 std::vector<Instruction*> erase;
1113 DominatorTree& DT = getAnalysis<DominatorTree>();
1115 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1116 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1117 BasicBlock* BB = DI->getBlock();
1119 //DOUT << "Block: " << BB->getName() << "\n";
1120 //dump(availableOut[BB]);
1123 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1126 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) ||
1127 isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) ||
1128 isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) ||
1129 isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) {
1130 Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
1133 if (Instruction* Instr = dyn_cast<Instruction>(leader))
1134 if (Instr->getParent() != 0 && Instr != BI) {
1135 replace.push_back(std::make_pair(BI, leader));
1136 erase.push_back(BI);
1143 while (!replace.empty()) {
1144 std::pair<Instruction*, Value*> rep = replace.back();
1146 rep.first->replaceAllUsesWith(rep.second);
1147 changed_function = true;
1150 for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
1152 (*I)->eraseFromParent();
1154 return changed_function;
1157 /// cleanup - Delete any extraneous values that were created to represent
1158 /// expressions without leaders.
1159 void GVNPRE::cleanup() {
1160 while (!createdExpressions.empty()) {
1161 Instruction* I = createdExpressions.back();
1162 createdExpressions.pop_back();
1168 /// buildsets_availout - When calculating availability, handle an instruction
1169 /// by inserting it into the appropriate sets
1170 void GVNPRE::buildsets_availout(BasicBlock::iterator I,
1171 SmallPtrSet<Value*, 16>& currAvail,
1172 SmallPtrSet<Value*, 16>& currPhis,
1173 SmallPtrSet<Value*, 16>& currExps,
1174 SmallPtrSet<Value*, 16>& currTemps,
1175 BitVector& availNumbers,
1176 BitVector& expNumbers) {
1178 if (PHINode* p = dyn_cast<PHINode>(I)) {
1179 VN.lookup_or_add(p);
1180 expNumbers.resize(VN.size());
1181 availNumbers.resize(VN.size());
1186 } else if (CastInst* U = dyn_cast<CastInst>(I)) {
1187 Value* leftValue = U->getOperand(0);
1189 unsigned num = VN.lookup_or_add(U);
1190 expNumbers.resize(VN.size());
1191 availNumbers.resize(VN.size());
1193 if (isa<Instruction>(leftValue))
1194 if (!expNumbers.test(VN.lookup(leftValue))) {
1195 currExps.insert(leftValue);
1196 expNumbers.set(VN.lookup(leftValue));
1199 if (!expNumbers.test(VN.lookup(U))) {
1201 expNumbers.set(num);
1204 // Handle binary ops
1205 } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
1206 isa<ExtractElementInst>(I)) {
1207 User* U = cast<User>(I);
1208 Value* leftValue = U->getOperand(0);
1209 Value* rightValue = U->getOperand(1);
1211 unsigned num = VN.lookup_or_add(U);
1212 expNumbers.resize(VN.size());
1213 availNumbers.resize(VN.size());
1215 if (isa<Instruction>(leftValue))
1216 if (!expNumbers.test(VN.lookup(leftValue))) {
1217 currExps.insert(leftValue);
1218 expNumbers.set(VN.lookup(leftValue));
1221 if (isa<Instruction>(rightValue))
1222 if (!expNumbers.test(VN.lookup(rightValue))) {
1223 currExps.insert(rightValue);
1224 expNumbers.set(VN.lookup(rightValue));
1227 if (!expNumbers.test(VN.lookup(U))) {
1229 expNumbers.set(num);
1232 // Handle ternary ops
1233 } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) ||
1234 isa<SelectInst>(I)) {
1235 User* U = cast<User>(I);
1236 Value* leftValue = U->getOperand(0);
1237 Value* rightValue = U->getOperand(1);
1238 Value* thirdValue = U->getOperand(2);
1240 VN.lookup_or_add(U);
1242 unsigned num = VN.lookup_or_add(U);
1243 expNumbers.resize(VN.size());
1244 availNumbers.resize(VN.size());
1246 if (isa<Instruction>(leftValue))
1247 if (!expNumbers.test(VN.lookup(leftValue))) {
1248 currExps.insert(leftValue);
1249 expNumbers.set(VN.lookup(leftValue));
1251 if (isa<Instruction>(rightValue))
1252 if (!expNumbers.test(VN.lookup(rightValue))) {
1253 currExps.insert(rightValue);
1254 expNumbers.set(VN.lookup(rightValue));
1256 if (isa<Instruction>(thirdValue))
1257 if (!expNumbers.test(VN.lookup(thirdValue))) {
1258 currExps.insert(thirdValue);
1259 expNumbers.set(VN.lookup(thirdValue));
1262 if (!expNumbers.test(VN.lookup(U))) {
1264 expNumbers.set(num);
1267 // Handle vararg ops
1268 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) {
1269 Value* ptrValue = U->getPointerOperand();
1271 VN.lookup_or_add(U);
1273 unsigned num = VN.lookup_or_add(U);
1274 expNumbers.resize(VN.size());
1275 availNumbers.resize(VN.size());
1277 if (isa<Instruction>(ptrValue))
1278 if (!expNumbers.test(VN.lookup(ptrValue))) {
1279 currExps.insert(ptrValue);
1280 expNumbers.set(VN.lookup(ptrValue));
1283 for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end();
1285 if (isa<Instruction>(*OI) && !expNumbers.test(VN.lookup(*OI))) {
1286 currExps.insert(*OI);
1287 expNumbers.set(VN.lookup(*OI));
1290 if (!expNumbers.test(VN.lookup(U))) {
1292 expNumbers.set(num);
1295 // Handle opaque ops
1296 } else if (!I->isTerminator()){
1297 VN.lookup_or_add(I);
1298 expNumbers.resize(VN.size());
1299 availNumbers.resize(VN.size());
1301 currTemps.insert(I);
1304 if (!I->isTerminator())
1305 if (!availNumbers.test(VN.lookup(I))) {
1306 currAvail.insert(I);
1307 availNumbers.set(VN.lookup(I));
1311 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
1312 /// set as a function of the ANTIC_IN set of the block's predecessors
1313 bool GVNPRE::buildsets_anticout(BasicBlock* BB,
1314 SmallPtrSet<Value*, 16>& anticOut,
1315 std::set<BasicBlock*>& visited) {
1316 if (BB->getTerminator()->getNumSuccessors() == 1) {
1317 if (BB->getTerminator()->getSuccessor(0) != BB &&
1318 visited.count(BB->getTerminator()->getSuccessor(0)) == 0) {
1319 DOUT << "DEFER: " << BB->getName() << "\n";
1323 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
1324 BB, BB->getTerminator()->getSuccessor(0), anticOut);
1326 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
1327 BasicBlock* first = BB->getTerminator()->getSuccessor(0);
1328 anticOut.insert(anticipatedIn[first].begin(), anticipatedIn[first].end());
1330 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
1331 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
1332 SmallPtrSet<Value*, 16>& succAnticIn = anticipatedIn[currSucc];
1334 std::vector<Value*> temp;
1336 for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(),
1337 E = anticOut.end(); I != E; ++I)
1338 if (find_leader(succAnticIn, VN.lookup(*I)) == 0)
1341 for (std::vector<Value*>::iterator I = temp.begin(), E = temp.end();
1350 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
1351 /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
1352 /// sets populated in buildsets_availout
1353 unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
1354 SmallPtrSet<Value*, 16>& anticOut,
1355 SmallPtrSet<Value*, 16>& currExps,
1356 SmallPtrSet<Value*, 16>& currTemps,
1357 std::set<BasicBlock*>& visited) {
1358 SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB];
1359 unsigned old = anticIn.size();
1361 bool defer = buildsets_anticout(BB, anticOut, visited);
1367 BitVector numbers(VN.size());
1368 for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(),
1369 E = anticOut.end(); I != E; ++I) {
1370 unsigned num = VN.lookup_or_add(*I);
1371 numbers.resize(VN.size());
1373 if (isa<Instruction>(*I)) {
1378 for (SmallPtrSet<Value*, 16>::iterator I = currExps.begin(),
1379 E = currExps.end(); I != E; ++I) {
1380 if (!numbers.test(VN.lookup_or_add(*I))) {
1382 numbers.set(VN.lookup(*I));
1386 for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
1387 E = currTemps.end(); I != E; ++I) {
1389 numbers.flip(VN.lookup(*I));
1392 clean(anticIn, numbers);
1395 if (old != anticIn.size())
1401 /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
1402 /// and the ANTIC_IN sets.
1403 void GVNPRE::buildsets(Function& F) {
1404 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedExpressions;
1405 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
1407 DominatorTree &DT = getAnalysis<DominatorTree>();
1409 // Phase 1, Part 1: calculate AVAIL_OUT
1411 // Top-down walk of the dominator tree
1412 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1413 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1415 // Get the sets to update for this block
1416 SmallPtrSet<Value*, 16>& currExps = generatedExpressions[DI->getBlock()];
1417 SmallPtrSet<Value*, 16>& currPhis = generatedPhis[DI->getBlock()];
1418 SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
1419 SmallPtrSet<Value*, 16>& currAvail = availableOut[DI->getBlock()];
1421 BasicBlock* BB = DI->getBlock();
1423 // A block inherits AVAIL_OUT from its dominator
1424 if (DI->getIDom() != 0)
1425 currAvail.insert(availableOut[DI->getIDom()->getBlock()].begin(),
1426 availableOut[DI->getIDom()->getBlock()].end());
1428 BitVector availNumbers(VN.size());
1429 for (SmallPtrSet<Value*, 16>::iterator I = currAvail.begin(),
1430 E = currAvail.end(); I != E; ++I)
1431 availNumbers.set(VN.lookup(*I));
1433 BitVector expNumbers(VN.size());
1434 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1436 buildsets_availout(BI, currAvail, currPhis, currExps,
1437 currTemps, availNumbers, expNumbers);
1441 // Phase 1, Part 2: calculate ANTIC_IN
1443 std::set<BasicBlock*> visited;
1444 SmallPtrSet<BasicBlock*, 4> block_changed;
1445 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
1446 block_changed.insert(FI);
1448 bool changed = true;
1449 unsigned iterations = 0;
1453 SmallPtrSet<Value*, 16> anticOut;
1455 // Postorder walk of the CFG
1456 for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
1457 BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) {
1458 BasicBlock* BB = *BBI;
1460 if (block_changed.count(BB) != 0) {
1461 unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
1462 generatedTemporaries[BB], visited);
1471 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1473 block_changed.insert(*PI);
1476 block_changed.erase(BB);
1478 changed |= (ret == 2);
1486 DOUT << "ITERATIONS: " << iterations << "\n";
1489 /// insertion_pre - When a partial redundancy has been identified, eliminate it
1490 /// by inserting appropriate values into the predecessors and a phi node in
1492 void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
1493 std::map<BasicBlock*, Value*>& avail,
1494 std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_sets) {
1495 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1496 DOUT << "PRED: " << (*PI)->getName() << "\n";
1497 Value* e2 = avail[*PI];
1498 if (!find_leader(availableOut[*PI], VN.lookup(e2))) {
1499 User* U = cast<User>(e2);
1502 if (isa<BinaryOperator>(U->getOperand(0)) ||
1503 isa<CmpInst>(U->getOperand(0)) ||
1504 isa<ShuffleVectorInst>(U->getOperand(0)) ||
1505 isa<ExtractElementInst>(U->getOperand(0)) ||
1506 isa<InsertElementInst>(U->getOperand(0)) ||
1507 isa<SelectInst>(U->getOperand(0)) ||
1508 isa<CastInst>(U->getOperand(0)) ||
1509 isa<GetElementPtrInst>(U->getOperand(0)))
1510 s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
1512 s1 = U->getOperand(0);
1516 if (isa<BinaryOperator>(U) ||
1518 isa<ShuffleVectorInst>(U) ||
1519 isa<ExtractElementInst>(U) ||
1520 isa<InsertElementInst>(U) ||
1522 if (isa<BinaryOperator>(U->getOperand(1)) ||
1523 isa<CmpInst>(U->getOperand(1)) ||
1524 isa<ShuffleVectorInst>(U->getOperand(1)) ||
1525 isa<ExtractElementInst>(U->getOperand(1)) ||
1526 isa<InsertElementInst>(U->getOperand(1)) ||
1527 isa<SelectInst>(U->getOperand(1)) ||
1528 isa<CastInst>(U->getOperand(1)) ||
1529 isa<GetElementPtrInst>(U->getOperand(1))) {
1530 s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
1532 s2 = U->getOperand(1);
1535 // Ternary Operators
1537 if (isa<ShuffleVectorInst>(U) ||
1538 isa<InsertElementInst>(U) ||
1540 if (isa<BinaryOperator>(U->getOperand(2)) ||
1541 isa<CmpInst>(U->getOperand(2)) ||
1542 isa<ShuffleVectorInst>(U->getOperand(2)) ||
1543 isa<ExtractElementInst>(U->getOperand(2)) ||
1544 isa<InsertElementInst>(U->getOperand(2)) ||
1545 isa<SelectInst>(U->getOperand(2)) ||
1546 isa<CastInst>(U->getOperand(2)) ||
1547 isa<GetElementPtrInst>(U->getOperand(2))) {
1548 s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2)));
1550 s3 = U->getOperand(2);
1554 std::vector<Value*> sVarargs;
1555 if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) {
1556 for (GetElementPtrInst::op_iterator OI = G->idx_begin(),
1557 OE = G->idx_end(); OI != OE; ++OI) {
1558 if (isa<BinaryOperator>(*OI) ||
1559 isa<CmpInst>(*OI) ||
1560 isa<ShuffleVectorInst>(*OI) ||
1561 isa<ExtractElementInst>(*OI) ||
1562 isa<InsertElementInst>(*OI) ||
1563 isa<SelectInst>(*OI) ||
1564 isa<CastInst>(*OI) ||
1565 isa<GetElementPtrInst>(*OI)) {
1566 sVarargs.push_back(find_leader(availableOut[*PI],
1569 sVarargs.push_back(*OI);
1575 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
1576 newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
1577 BO->getName()+".gvnpre",
1578 (*PI)->getTerminator());
1579 else if (CmpInst* C = dyn_cast<CmpInst>(U))
1580 newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
1581 C->getName()+".gvnpre",
1582 (*PI)->getTerminator());
1583 else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
1584 newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
1585 (*PI)->getTerminator());
1586 else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
1587 newVal = new InsertElementInst(s1, s2, s3, S->getName()+".gvnpre",
1588 (*PI)->getTerminator());
1589 else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
1590 newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
1591 (*PI)->getTerminator());
1592 else if (SelectInst* S = dyn_cast<SelectInst>(U))
1593 newVal = new SelectInst(s1, s2, s3, S->getName()+".gvnpre",
1594 (*PI)->getTerminator());
1595 else if (CastInst* C = dyn_cast<CastInst>(U))
1596 newVal = CastInst::create(C->getOpcode(), s1, C->getType(),
1597 C->getName()+".gvnpre",
1598 (*PI)->getTerminator());
1599 else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U))
1600 newVal = new GetElementPtrInst(s1, &sVarargs[0], sVarargs.size(),
1601 G->getName()+".gvnpre",
1602 (*PI)->getTerminator());
1605 VN.add(newVal, VN.lookup(U));
1607 SmallPtrSet<Value*, 16>& predAvail = availableOut[*PI];
1608 val_replace(predAvail, newVal);
1609 val_replace(new_sets[*PI], newVal);
1611 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1612 if (av != avail.end())
1614 avail.insert(std::make_pair(*PI, newVal));
1622 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1624 p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
1626 p->addIncoming(avail[*PI], *PI);
1629 VN.add(p, VN.lookup(e));
1630 val_replace(availableOut[BB], p);
1631 generatedPhis[BB].insert(p);
1632 new_sets[BB].insert(p);
1637 /// insertion_mergepoint - When walking the dom tree, check at each merge
1638 /// block for the possibility of a partial redundancy. If present, eliminate it
1639 unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
1640 df_iterator<DomTreeNode*>& D,
1641 std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_sets) {
1642 bool changed_function = false;
1643 bool new_stuff = false;
1645 BasicBlock* BB = D->getBlock();
1646 for (unsigned i = 0; i < workList.size(); ++i) {
1647 Value* e = workList[i];
1649 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1650 isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
1651 isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) ||
1652 isa<GetElementPtrInst>(e)) {
1653 if (find_leader(availableOut[D->getIDom()->getBlock()],
1657 std::map<BasicBlock*, Value*> avail;
1658 bool by_some = false;
1659 bool all_same = true;
1660 Value * first_s = 0;
1662 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
1664 Value *e2 = phi_translate(e, *PI, BB);
1665 Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
1668 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1669 if (av != avail.end())
1671 avail.insert(std::make_pair(*PI, e2));
1674 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1675 if (av != avail.end())
1677 avail.insert(std::make_pair(*PI, e3));
1682 else if (first_s != e3)
1687 if (by_some && !all_same &&
1688 !find_leader(generatedPhis[BB], VN.lookup(e))) {
1689 insertion_pre(e, BB, avail, new_sets);
1691 changed_function = true;
1697 unsigned retval = 0;
1698 if (changed_function)
1706 /// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for
1707 /// merge points. When one is found, check for a partial redundancy. If one is
1708 /// present, eliminate it. Repeat this walk until no changes are made.
1709 bool GVNPRE::insertion(Function& F) {
1710 bool changed_function = false;
1712 DominatorTree &DT = getAnalysis<DominatorTree>();
1714 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > new_sets;
1715 bool new_stuff = true;
1718 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1719 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1720 BasicBlock* BB = DI->getBlock();
1725 SmallPtrSet<Value*, 16>& availOut = availableOut[BB];
1726 SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB];
1728 // Replace leaders with leaders inherited from dominator
1729 if (DI->getIDom() != 0) {
1730 SmallPtrSet<Value*, 16>& dom_set = new_sets[DI->getIDom()->getBlock()];
1731 for (SmallPtrSet<Value*, 16>::iterator I = dom_set.begin(),
1732 E = dom_set.end(); I != E; ++I) {
1733 val_replace(new_sets[BB], *I);
1734 val_replace(availOut, *I);
1738 // If there is more than one predecessor...
1739 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
1740 std::vector<Value*> workList;
1741 workList.reserve(anticIn.size());
1742 topo_sort(anticIn, workList);
1744 unsigned result = insertion_mergepoint(workList, DI, new_sets);
1746 changed_function = true;
1753 return changed_function;
1756 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1759 bool GVNPRE::runOnFunction(Function &F) {
1760 // Clean out global sets from any previous functions
1762 createdExpressions.clear();
1763 availableOut.clear();
1764 anticipatedIn.clear();
1765 generatedPhis.clear();
1767 bool changed_function = false;
1769 // Phase 1: BuildSets
1770 // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1774 // This phase inserts values to make partially redundant values
1776 changed_function |= insertion(F);
1778 // Phase 3: Eliminate
1779 // This phase performs trivial full redundancy elimination
1780 changed_function |= elimination();
1783 // This phase cleans up values that were created solely
1784 // as leaders for expressions
1787 return changed_function;