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/Analysis/Dominators.h"
27 #include "llvm/ADT/BitVector.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/DepthFirstIterator.h"
30 #include "llvm/ADT/PostOrderIterator.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/CFG.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Debug.h"
43 //===----------------------------------------------------------------------===//
45 //===----------------------------------------------------------------------===//
47 /// This class holds the mapping between values and value numbers. It is used
48 /// as an efficient mechanism to determine the expression-wise equivalence of
52 class VISIBILITY_HIDDEN ValueTable {
55 enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM,
56 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
57 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
58 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
59 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
60 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
61 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
64 ExpressionOpcode opcode;
69 bool operator< (const Expression& other) const {
70 if (opcode < other.opcode)
72 else if (opcode > other.opcode)
74 else if (firstVN < other.firstVN)
76 else if (firstVN > other.firstVN)
78 else if (secondVN < other.secondVN)
80 else if (secondVN > other.secondVN)
82 else if (thirdVN < other.thirdVN)
84 else if (thirdVN > other.thirdVN)
92 DenseMap<Value*, uint32_t> valueNumbering;
93 std::map<Expression, uint32_t> expressionNumbering;
95 uint32_t nextValueNumber;
97 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
98 Expression::ExpressionOpcode getOpcode(CmpInst* C);
99 Expression create_expression(BinaryOperator* BO);
100 Expression create_expression(CmpInst* C);
101 Expression create_expression(ShuffleVectorInst* V);
102 Expression create_expression(ExtractElementInst* C);
103 Expression create_expression(InsertElementInst* V);
104 Expression create_expression(SelectInst* V);
106 ValueTable() { nextValueNumber = 1; }
107 uint32_t lookup_or_add(Value* V);
108 uint32_t lookup(Value* V) const;
109 void add(Value* V, uint32_t num);
111 void erase(Value* v);
116 //===----------------------------------------------------------------------===//
117 // ValueTable Internal Functions
118 //===----------------------------------------------------------------------===//
119 ValueTable::Expression::ExpressionOpcode
120 ValueTable::getOpcode(BinaryOperator* BO) {
121 switch(BO->getOpcode()) {
122 case Instruction::Add:
123 return Expression::ADD;
124 case Instruction::Sub:
125 return Expression::SUB;
126 case Instruction::Mul:
127 return Expression::MUL;
128 case Instruction::UDiv:
129 return Expression::UDIV;
130 case Instruction::SDiv:
131 return Expression::SDIV;
132 case Instruction::FDiv:
133 return Expression::FDIV;
134 case Instruction::URem:
135 return Expression::UREM;
136 case Instruction::SRem:
137 return Expression::SREM;
138 case Instruction::FRem:
139 return Expression::FREM;
140 case Instruction::Shl:
141 return Expression::SHL;
142 case Instruction::LShr:
143 return Expression::LSHR;
144 case Instruction::AShr:
145 return Expression::ASHR;
146 case Instruction::And:
147 return Expression::AND;
148 case Instruction::Or:
149 return Expression::OR;
150 case Instruction::Xor:
151 return Expression::XOR;
153 // THIS SHOULD NEVER HAPPEN
155 assert(0 && "Binary operator with unknown opcode?");
156 return Expression::ADD;
160 ValueTable::Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
161 if (C->getOpcode() == Instruction::ICmp) {
162 switch (C->getPredicate()) {
163 case ICmpInst::ICMP_EQ:
164 return Expression::ICMPEQ;
165 case ICmpInst::ICMP_NE:
166 return Expression::ICMPNE;
167 case ICmpInst::ICMP_UGT:
168 return Expression::ICMPUGT;
169 case ICmpInst::ICMP_UGE:
170 return Expression::ICMPUGE;
171 case ICmpInst::ICMP_ULT:
172 return Expression::ICMPULT;
173 case ICmpInst::ICMP_ULE:
174 return Expression::ICMPULE;
175 case ICmpInst::ICMP_SGT:
176 return Expression::ICMPSGT;
177 case ICmpInst::ICMP_SGE:
178 return Expression::ICMPSGE;
179 case ICmpInst::ICMP_SLT:
180 return Expression::ICMPSLT;
181 case ICmpInst::ICMP_SLE:
182 return Expression::ICMPSLE;
184 // THIS SHOULD NEVER HAPPEN
186 assert(0 && "Comparison with unknown predicate?");
187 return Expression::ICMPEQ;
190 switch (C->getPredicate()) {
191 case FCmpInst::FCMP_OEQ:
192 return Expression::FCMPOEQ;
193 case FCmpInst::FCMP_OGT:
194 return Expression::FCMPOGT;
195 case FCmpInst::FCMP_OGE:
196 return Expression::FCMPOGE;
197 case FCmpInst::FCMP_OLT:
198 return Expression::FCMPOLT;
199 case FCmpInst::FCMP_OLE:
200 return Expression::FCMPOLE;
201 case FCmpInst::FCMP_ONE:
202 return Expression::FCMPONE;
203 case FCmpInst::FCMP_ORD:
204 return Expression::FCMPORD;
205 case FCmpInst::FCMP_UNO:
206 return Expression::FCMPUNO;
207 case FCmpInst::FCMP_UEQ:
208 return Expression::FCMPUEQ;
209 case FCmpInst::FCMP_UGT:
210 return Expression::FCMPUGT;
211 case FCmpInst::FCMP_UGE:
212 return Expression::FCMPUGE;
213 case FCmpInst::FCMP_ULT:
214 return Expression::FCMPULT;
215 case FCmpInst::FCMP_ULE:
216 return Expression::FCMPULE;
217 case FCmpInst::FCMP_UNE:
218 return Expression::FCMPUNE;
220 // THIS SHOULD NEVER HAPPEN
222 assert(0 && "Comparison with unknown predicate?");
223 return Expression::FCMPOEQ;
228 ValueTable::Expression ValueTable::create_expression(BinaryOperator* BO) {
231 e.firstVN = lookup_or_add(BO->getOperand(0));
232 e.secondVN = lookup_or_add(BO->getOperand(1));
234 e.opcode = getOpcode(BO);
239 ValueTable::Expression ValueTable::create_expression(CmpInst* C) {
242 e.firstVN = lookup_or_add(C->getOperand(0));
243 e.secondVN = lookup_or_add(C->getOperand(1));
245 e.opcode = getOpcode(C);
250 ValueTable::Expression ValueTable::create_expression(ShuffleVectorInst* S) {
253 e.firstVN = lookup_or_add(S->getOperand(0));
254 e.secondVN = lookup_or_add(S->getOperand(1));
255 e.thirdVN = lookup_or_add(S->getOperand(2));
256 e.opcode = Expression::SHUFFLE;
261 ValueTable::Expression ValueTable::create_expression(ExtractElementInst* E) {
264 e.firstVN = lookup_or_add(E->getOperand(0));
265 e.secondVN = lookup_or_add(E->getOperand(1));
267 e.opcode = Expression::EXTRACT;
272 ValueTable::Expression ValueTable::create_expression(InsertElementInst* I) {
275 e.firstVN = lookup_or_add(I->getOperand(0));
276 e.secondVN = lookup_or_add(I->getOperand(1));
277 e.thirdVN = lookup_or_add(I->getOperand(2));
278 e.opcode = Expression::INSERT;
283 ValueTable::Expression ValueTable::create_expression(SelectInst* I) {
286 e.firstVN = lookup_or_add(I->getCondition());
287 e.secondVN = lookup_or_add(I->getTrueValue());
288 e.thirdVN = lookup_or_add(I->getFalseValue());
289 e.opcode = Expression::SELECT;
294 //===----------------------------------------------------------------------===//
295 // ValueTable External Functions
296 //===----------------------------------------------------------------------===//
298 /// lookup_or_add - Returns the value number for the specified value, assigning
299 /// it a new number if it did not have one before.
300 uint32_t ValueTable::lookup_or_add(Value* V) {
301 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
302 if (VI != valueNumbering.end())
306 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
307 Expression e = create_expression(BO);
309 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
310 if (EI != expressionNumbering.end()) {
311 valueNumbering.insert(std::make_pair(V, EI->second));
314 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
315 valueNumbering.insert(std::make_pair(V, nextValueNumber));
317 return nextValueNumber++;
319 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
320 Expression e = create_expression(C);
322 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
323 if (EI != expressionNumbering.end()) {
324 valueNumbering.insert(std::make_pair(V, EI->second));
327 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
328 valueNumbering.insert(std::make_pair(V, nextValueNumber));
330 return nextValueNumber++;
332 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
333 Expression e = create_expression(U);
335 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
336 if (EI != expressionNumbering.end()) {
337 valueNumbering.insert(std::make_pair(V, EI->second));
340 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
341 valueNumbering.insert(std::make_pair(V, nextValueNumber));
343 return nextValueNumber++;
345 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
346 Expression e = create_expression(U);
348 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
349 if (EI != expressionNumbering.end()) {
350 valueNumbering.insert(std::make_pair(V, EI->second));
353 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
354 valueNumbering.insert(std::make_pair(V, nextValueNumber));
356 return nextValueNumber++;
358 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
359 Expression e = create_expression(U);
361 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
362 if (EI != expressionNumbering.end()) {
363 valueNumbering.insert(std::make_pair(V, EI->second));
366 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
367 valueNumbering.insert(std::make_pair(V, nextValueNumber));
369 return nextValueNumber++;
371 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
372 Expression e = create_expression(U);
374 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
375 if (EI != expressionNumbering.end()) {
376 valueNumbering.insert(std::make_pair(V, EI->second));
379 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
380 valueNumbering.insert(std::make_pair(V, nextValueNumber));
382 return nextValueNumber++;
385 valueNumbering.insert(std::make_pair(V, nextValueNumber));
386 return nextValueNumber++;
390 /// lookup - Returns the value number of the specified value. Fails if
391 /// the value has not yet been numbered.
392 uint32_t ValueTable::lookup(Value* V) const {
393 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
394 if (VI != valueNumbering.end())
397 assert(0 && "Value not numbered?");
402 /// add - Add the specified value with the given value number, removing
403 /// its old number, if any
404 void ValueTable::add(Value* V, uint32_t num) {
405 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
406 if (VI != valueNumbering.end())
407 valueNumbering.erase(VI);
408 valueNumbering.insert(std::make_pair(V, num));
411 /// clear - Remove all entries from the ValueTable
412 void ValueTable::clear() {
413 valueNumbering.clear();
414 expressionNumbering.clear();
418 /// erase - Remove a value from the value numbering
419 void ValueTable::erase(Value* V) {
420 valueNumbering.erase(V);
423 /// size - Return the number of assigned value numbers
424 unsigned ValueTable::size() {
425 // NOTE: zero is never assigned
426 return nextValueNumber;
429 //===----------------------------------------------------------------------===//
431 //===----------------------------------------------------------------------===//
435 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
436 bool runOnFunction(Function &F);
438 static char ID; // Pass identification, replacement for typeid
439 GVNPRE() : FunctionPass((intptr_t)&ID) { }
443 std::vector<Instruction*> createdExpressions;
445 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > availableOut;
446 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > anticipatedIn;
448 // This transformation requires dominator postdominator info
449 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
450 AU.setPreservesCFG();
451 AU.addRequired<DominatorTree>();
455 // FIXME: eliminate or document these better
456 void dump(const SmallPtrSet<Value*, 16>& s) const;
457 void clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet);
458 Value* find_leader(SmallPtrSet<Value*, 16>& vals,
460 Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ);
461 void phi_translate_set(SmallPtrSet<Value*, 16>& anticIn, BasicBlock* pred,
462 BasicBlock* succ, SmallPtrSet<Value*, 16>& out);
464 void topo_sort(SmallPtrSet<Value*, 16>& set,
465 std::vector<Value*>& vec);
470 void val_insert(SmallPtrSet<Value*, 16>& s, Value* v);
471 void val_replace(SmallPtrSet<Value*, 16>& s, Value* v);
472 bool dependsOnInvoke(Value* V);
473 void buildsets_availout(BasicBlock::iterator I,
474 SmallPtrSet<Value*, 16>& currAvail,
475 SmallPtrSet<PHINode*, 16>& currPhis,
476 SmallPtrSet<Value*, 16>& currExps,
477 SmallPtrSet<Value*, 16>& currTemps,
478 BitVector& availNumbers,
479 BitVector& expNumbers);
480 bool buildsets_anticout(BasicBlock* BB,
481 SmallPtrSet<Value*, 16>& anticOut,
482 std::set<BasicBlock*>& visited);
483 unsigned buildsets_anticin(BasicBlock* BB,
484 SmallPtrSet<Value*, 16>& anticOut,
485 SmallPtrSet<Value*, 16>& currExps,
486 SmallPtrSet<Value*, 16>& currTemps,
487 std::set<BasicBlock*>& visited);
488 void buildsets(Function& F);
490 void insertion_pre(Value* e, BasicBlock* BB,
491 std::map<BasicBlock*, Value*>& avail,
492 SmallPtrSet<Value*, 16>& new_set);
493 unsigned insertion_mergepoint(std::vector<Value*>& workList,
494 df_iterator<DomTreeNode*>& D,
495 SmallPtrSet<Value*, 16>& new_set);
496 bool insertion(Function& F);
504 // createGVNPREPass - The public interface to this file...
505 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
507 RegisterPass<GVNPRE> X("gvnpre",
508 "Global Value Numbering/Partial Redundancy Elimination");
511 STATISTIC(NumInsertedVals, "Number of values inserted");
512 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
513 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
515 /// find_leader - Given a set and a value number, return the first
516 /// element of the set with that value number, or 0 if no such element
518 Value* GVNPRE::find_leader(SmallPtrSet<Value*, 16>& vals, uint32_t v) {
519 for (SmallPtrSet<Value*, 16>::iterator I = vals.begin(), E = vals.end();
521 if (v == VN.lookup(*I))
527 /// val_insert - Insert a value into a set only if there is not a value
528 /// with the same value number already in the set
529 void GVNPRE::val_insert(SmallPtrSet<Value*, 16>& s, Value* v) {
530 uint32_t num = VN.lookup(v);
531 Value* leader = find_leader(s, num);
536 /// val_replace - Insert a value into a set, replacing any values already in
537 /// the set that have the same value number
538 void GVNPRE::val_replace(SmallPtrSet<Value*, 16>& s, Value* v) {
539 uint32_t num = VN.lookup(v);
540 Value* leader = find_leader(s, num);
541 while (leader != 0) {
543 leader = find_leader(s, num);
548 /// phi_translate - Given a value, its parent block, and a predecessor of its
549 /// parent, translate the value into legal for the predecessor block. This
550 /// means translating its operands (and recursively, their operands) through
551 /// any phi nodes in the parent into values available in the predecessor
552 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
557 if (isa<BinaryOperator>(V) || isa<CmpInst>(V) ||
558 isa<ExtractElementInst>(V)) {
559 User* U = cast<User>(V);
562 if (isa<Instruction>(U->getOperand(0)))
563 newOp1 = phi_translate(U->getOperand(0), pred, succ);
565 newOp1 = U->getOperand(0);
571 if (isa<Instruction>(U->getOperand(1)))
572 newOp2 = phi_translate(U->getOperand(1), pred, succ);
574 newOp2 = U->getOperand(1);
579 if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
580 Instruction* newVal = 0;
581 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
582 newVal = BinaryOperator::create(BO->getOpcode(),
584 BO->getName()+".expr");
585 else if (CmpInst* C = dyn_cast<CmpInst>(U))
586 newVal = CmpInst::create(C->getOpcode(),
589 C->getName()+".expr");
590 else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
591 newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
593 uint32_t v = VN.lookup_or_add(newVal);
595 Value* leader = find_leader(availableOut[pred], v);
597 createdExpressions.push_back(newVal);
606 // Ternary Operations
607 } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
608 isa<SelectInst>(V)) {
609 User* U = cast<User>(V);
612 if (isa<Instruction>(U->getOperand(0)))
613 newOp1 = phi_translate(U->getOperand(0), pred, succ);
615 newOp1 = U->getOperand(0);
621 if (isa<Instruction>(U->getOperand(1)))
622 newOp2 = phi_translate(U->getOperand(1), pred, succ);
624 newOp2 = U->getOperand(1);
630 if (isa<Instruction>(U->getOperand(2)))
631 newOp3 = phi_translate(U->getOperand(2), pred, succ);
633 newOp3 = U->getOperand(2);
638 if (newOp1 != U->getOperand(0) ||
639 newOp2 != U->getOperand(1) ||
640 newOp3 != U->getOperand(2)) {
641 Instruction* newVal = 0;
642 if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
643 newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
644 S->getName()+".expr");
645 else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
646 newVal = new InsertElementInst(newOp1, newOp2, newOp3,
647 I->getName()+".expr");
648 else if (SelectInst* I = dyn_cast<SelectInst>(U))
649 newVal = new SelectInst(newOp1, newOp2, newOp3, I->getName()+".expr");
651 uint32_t v = VN.lookup_or_add(newVal);
653 Value* leader = find_leader(availableOut[pred], v);
655 createdExpressions.push_back(newVal);
665 } else if (PHINode* P = dyn_cast<PHINode>(V)) {
666 if (P->getParent() == succ)
667 return P->getIncomingValueForBlock(pred);
673 /// phi_translate_set - Perform phi translation on every element of a set
674 void GVNPRE::phi_translate_set(SmallPtrSet<Value*, 16>& anticIn,
675 BasicBlock* pred, BasicBlock* succ,
676 SmallPtrSet<Value*, 16>& out) {
677 for (SmallPtrSet<Value*, 16>::iterator I = anticIn.begin(),
678 E = anticIn.end(); I != E; ++I) {
679 Value* V = phi_translate(*I, pred, succ);
685 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of
686 /// whose inputs is an invoke instruction. If this is true, we cannot safely
687 /// PRE the instruction or anything that depends on it.
688 bool GVNPRE::dependsOnInvoke(Value* V) {
689 if (PHINode* p = dyn_cast<PHINode>(V)) {
690 for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I)
691 if (isa<InvokeInst>(*I))
699 /// clean - Remove all non-opaque values from the set whose operands are not
700 /// themselves in the set, as well as all values that depend on invokes (see
702 void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
703 std::vector<Value*> worklist;
704 worklist.reserve(set.size());
705 topo_sort(set, worklist);
707 for (unsigned i = 0; i < worklist.size(); ++i) {
708 Value* v = worklist[i];
711 if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
712 isa<ExtractElementInst>(v)) {
713 User* U = cast<User>(v);
715 bool lhsValid = !isa<Instruction>(U->getOperand(0));
716 lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0)));
718 lhsValid = !dependsOnInvoke(U->getOperand(0));
720 bool rhsValid = !isa<Instruction>(U->getOperand(1));
721 rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1)));
723 rhsValid = !dependsOnInvoke(U->getOperand(1));
725 if (!lhsValid || !rhsValid) {
727 presentInSet.flip(VN.lookup(U));
730 // Handle ternary ops
731 } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
732 isa<SelectInst>(v)) {
733 User* U = cast<User>(v);
735 bool lhsValid = !isa<Instruction>(U->getOperand(0));
736 lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0)));
738 lhsValid = !dependsOnInvoke(U->getOperand(0));
740 bool rhsValid = !isa<Instruction>(U->getOperand(1));
741 rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1)));
743 rhsValid = !dependsOnInvoke(U->getOperand(1));
745 bool thirdValid = !isa<Instruction>(U->getOperand(2));
746 thirdValid |= presentInSet.test(VN.lookup(U->getOperand(2)));
748 thirdValid = !dependsOnInvoke(U->getOperand(2));
750 if (!lhsValid || !rhsValid || !thirdValid) {
752 presentInSet.flip(VN.lookup(U));
758 /// topo_sort - Given a set of values, sort them by topological
759 /// order into the provided vector.
760 void GVNPRE::topo_sort(SmallPtrSet<Value*, 16>& set, std::vector<Value*>& vec) {
761 SmallPtrSet<Value*, 16> visited;
762 std::vector<Value*> stack;
763 for (SmallPtrSet<Value*, 16>::iterator I = set.begin(), E = set.end();
765 if (visited.count(*I) == 0)
768 while (!stack.empty()) {
769 Value* e = stack.back();
772 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
773 isa<ExtractElementInst>(e)) {
774 User* U = cast<User>(e);
775 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
776 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
778 if (l != 0 && isa<Instruction>(l) &&
779 visited.count(l) == 0)
781 else if (r != 0 && isa<Instruction>(r) &&
782 visited.count(r) == 0)
790 // Handle ternary ops
791 } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) ||
792 isa<SelectInst>(e)) {
793 User* U = cast<User>(e);
794 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
795 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
796 Value* m = find_leader(set, VN.lookup(U->getOperand(2)));
798 if (l != 0 && isa<Instruction>(l) &&
799 visited.count(l) == 0)
801 else if (r != 0 && isa<Instruction>(r) &&
802 visited.count(r) == 0)
804 else if (m != 0 && isa<Instruction>(m) &&
805 visited.count(m) == 0)
825 /// dump - Dump a set of values to standard error
826 void GVNPRE::dump(const SmallPtrSet<Value*, 16>& s) const {
828 for (SmallPtrSet<Value*, 16>::iterator I = s.begin(), E = s.end();
830 DOUT << "" << VN.lookup(*I) << ": ";
836 /// elimination - Phase 3 of the main algorithm. Perform full redundancy
837 /// elimination by walking the dominator tree and removing any instruction that
838 /// is dominated by another instruction with the same value number.
839 bool GVNPRE::elimination() {
840 DOUT << "\n\nPhase 3: Elimination\n\n";
842 bool changed_function = false;
844 std::vector<std::pair<Instruction*, Value*> > replace;
845 std::vector<Instruction*> erase;
847 DominatorTree& DT = getAnalysis<DominatorTree>();
849 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
850 E = df_end(DT.getRootNode()); DI != E; ++DI) {
851 BasicBlock* BB = DI->getBlock();
853 //DOUT << "Block: " << BB->getName() << "\n";
854 //dump(availableOut[BB]);
857 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
860 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) ||
861 isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) ||
862 isa<ExtractElementInst>(BI) || isa<SelectInst>(BI)) {
863 Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
866 if (Instruction* Instr = dyn_cast<Instruction>(leader))
867 if (Instr->getParent() != 0 && Instr != BI) {
868 replace.push_back(std::make_pair(BI, leader));
876 while (!replace.empty()) {
877 std::pair<Instruction*, Value*> rep = replace.back();
879 rep.first->replaceAllUsesWith(rep.second);
880 changed_function = true;
883 for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
885 (*I)->eraseFromParent();
887 return changed_function;
890 /// cleanup - Delete any extraneous values that were created to represent
891 /// expressions without leaders.
892 void GVNPRE::cleanup() {
893 while (!createdExpressions.empty()) {
894 Instruction* I = createdExpressions.back();
895 createdExpressions.pop_back();
901 /// buildsets_availout - When calculating availability, handle an instruction
902 /// by inserting it into the appropriate sets
903 void GVNPRE::buildsets_availout(BasicBlock::iterator I,
904 SmallPtrSet<Value*, 16>& currAvail,
905 SmallPtrSet<PHINode*, 16>& currPhis,
906 SmallPtrSet<Value*, 16>& currExps,
907 SmallPtrSet<Value*, 16>& currTemps,
908 BitVector& availNumbers,
909 BitVector& expNumbers) {
911 if (PHINode* p = dyn_cast<PHINode>(I)) {
913 expNumbers.resize(VN.size());
914 availNumbers.resize(VN.size());
919 } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
920 isa<ExtractElementInst>(I)) {
921 User* U = cast<User>(I);
922 Value* leftValue = U->getOperand(0);
923 Value* rightValue = U->getOperand(1);
925 unsigned num = VN.lookup_or_add(U);
926 expNumbers.resize(VN.size());
927 availNumbers.resize(VN.size());
929 if (isa<Instruction>(leftValue))
930 if (!expNumbers.test(VN.lookup(leftValue))) {
931 currExps.insert(leftValue);
932 expNumbers.set(VN.lookup(leftValue));
935 if (isa<Instruction>(rightValue))
936 if (!expNumbers.test(VN.lookup(rightValue))) {
937 currExps.insert(rightValue);
938 expNumbers.set(VN.lookup(rightValue));
941 if (!expNumbers.test(VN.lookup(U))) {
946 // Handle ternary ops
947 } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) ||
948 isa<SelectInst>(I)) {
949 User* U = cast<User>(I);
950 Value* leftValue = U->getOperand(0);
951 Value* rightValue = U->getOperand(1);
952 Value* thirdValue = U->getOperand(2);
956 unsigned num = VN.lookup_or_add(U);
957 expNumbers.resize(VN.size());
958 availNumbers.resize(VN.size());
960 if (isa<Instruction>(leftValue))
961 if (!expNumbers.test(VN.lookup(leftValue))) {
962 currExps.insert(leftValue);
963 expNumbers.set(VN.lookup(leftValue));
965 if (isa<Instruction>(rightValue))
966 if (!expNumbers.test(VN.lookup(rightValue))) {
967 currExps.insert(rightValue);
968 expNumbers.set(VN.lookup(rightValue));
970 if (isa<Instruction>(thirdValue))
971 if (!expNumbers.test(VN.lookup(thirdValue))) {
972 currExps.insert(thirdValue);
973 expNumbers.set(VN.lookup(thirdValue));
976 if (!expNumbers.test(VN.lookup(U))) {
982 } else if (!I->isTerminator()){
984 expNumbers.resize(VN.size());
985 availNumbers.resize(VN.size());
990 if (!I->isTerminator())
991 if (!availNumbers.test(VN.lookup(I))) {
993 availNumbers.set(VN.lookup(I));
997 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
998 /// set as a function of the ANTIC_IN set of the block's predecessors
999 bool GVNPRE::buildsets_anticout(BasicBlock* BB,
1000 SmallPtrSet<Value*, 16>& anticOut,
1001 std::set<BasicBlock*>& visited) {
1002 if (BB->getTerminator()->getNumSuccessors() == 1) {
1003 if (BB->getTerminator()->getSuccessor(0) != BB &&
1004 visited.count(BB->getTerminator()->getSuccessor(0)) == 0) {
1005 DOUT << "DEFER: " << BB->getName() << "\n";
1009 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
1010 BB, BB->getTerminator()->getSuccessor(0), anticOut);
1012 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
1013 BasicBlock* first = BB->getTerminator()->getSuccessor(0);
1014 anticOut.insert(anticipatedIn[first].begin(), anticipatedIn[first].end());
1016 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
1017 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
1018 SmallPtrSet<Value*, 16>& succAnticIn = anticipatedIn[currSucc];
1020 std::vector<Value*> temp;
1022 for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(),
1023 E = anticOut.end(); I != E; ++I)
1024 if (succAnticIn.count(*I) == 0)
1027 for (std::vector<Value*>::iterator I = temp.begin(), E = temp.end();
1036 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
1037 /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
1038 /// sets populated in buildsets_availout
1039 unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
1040 SmallPtrSet<Value*, 16>& anticOut,
1041 SmallPtrSet<Value*, 16>& currExps,
1042 SmallPtrSet<Value*, 16>& currTemps,
1043 std::set<BasicBlock*>& visited) {
1044 SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB];
1045 unsigned old = anticIn.size();
1047 bool defer = buildsets_anticout(BB, anticOut, visited);
1053 BitVector numbers(VN.size());
1054 for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(),
1055 E = anticOut.end(); I != E; ++I) {
1056 unsigned num = VN.lookup_or_add(*I);
1057 numbers.resize(VN.size());
1059 if (isa<Instruction>(*I)) {
1064 for (SmallPtrSet<Value*, 16>::iterator I = currExps.begin(),
1065 E = currExps.end(); I != E; ++I) {
1066 if (!numbers.test(VN.lookup_or_add(*I))) {
1068 numbers.set(VN.lookup(*I));
1072 for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
1073 E = currTemps.end(); I != E; ++I) {
1075 numbers.flip(VN.lookup(*I));
1078 clean(anticIn, numbers);
1081 if (old != anticIn.size())
1087 /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
1088 /// and the ANTIC_IN sets.
1089 void GVNPRE::buildsets(Function& F) {
1090 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedExpressions;
1091 std::map<BasicBlock*, SmallPtrSet<PHINode*, 16> > generatedPhis;
1092 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
1094 DominatorTree &DT = getAnalysis<DominatorTree>();
1096 // Phase 1, Part 1: calculate AVAIL_OUT
1098 // Top-down walk of the dominator tree
1099 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1100 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1102 // Get the sets to update for this block
1103 SmallPtrSet<Value*, 16>& currExps = generatedExpressions[DI->getBlock()];
1104 SmallPtrSet<PHINode*, 16>& currPhis = generatedPhis[DI->getBlock()];
1105 SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
1106 SmallPtrSet<Value*, 16>& currAvail = availableOut[DI->getBlock()];
1108 BasicBlock* BB = DI->getBlock();
1110 // A block inherits AVAIL_OUT from its dominator
1111 if (DI->getIDom() != 0)
1112 currAvail.insert(availableOut[DI->getIDom()->getBlock()].begin(),
1113 availableOut[DI->getIDom()->getBlock()].end());
1115 BitVector availNumbers(VN.size());
1116 for (SmallPtrSet<Value*, 16>::iterator I = currAvail.begin(),
1117 E = currAvail.end(); I != E; ++I)
1118 availNumbers.set(VN.lookup(*I));
1120 BitVector expNumbers(VN.size());
1121 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1123 buildsets_availout(BI, currAvail, currPhis, currExps,
1124 currTemps, availNumbers, expNumbers);
1128 // Phase 1, Part 2: calculate ANTIC_IN
1130 std::set<BasicBlock*> visited;
1131 SmallPtrSet<BasicBlock*, 4> block_changed;
1132 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
1133 block_changed.insert(FI);
1135 bool changed = true;
1136 unsigned iterations = 0;
1140 SmallPtrSet<Value*, 16> anticOut;
1142 // Postorder walk of the CFG
1143 for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
1144 BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) {
1145 BasicBlock* BB = *BBI;
1147 if (block_changed.count(BB) != 0) {
1148 unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
1149 generatedTemporaries[BB], visited);
1158 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1160 block_changed.insert(*PI);
1163 block_changed.erase(BB);
1165 changed |= (ret == 2);
1173 DOUT << "ITERATIONS: " << iterations << "\n";
1176 /// insertion_pre - When a partial redundancy has been identified, eliminate it
1177 /// by inserting appropriate values into the predecessors and a phi node in
1179 void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
1180 std::map<BasicBlock*, Value*>& avail,
1181 SmallPtrSet<Value*, 16>& new_set) {
1182 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1183 Value* e2 = avail[*PI];
1184 if (!find_leader(availableOut[*PI], VN.lookup(e2))) {
1185 User* U = cast<User>(e2);
1188 if (isa<BinaryOperator>(U->getOperand(0)) ||
1189 isa<CmpInst>(U->getOperand(0)) ||
1190 isa<ShuffleVectorInst>(U->getOperand(0)) ||
1191 isa<ExtractElementInst>(U->getOperand(0)) ||
1192 isa<InsertElementInst>(U->getOperand(0)) ||
1193 isa<SelectInst>(U->getOperand(0)))
1194 s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
1196 s1 = U->getOperand(0);
1199 if (isa<BinaryOperator>(U->getOperand(1)) ||
1200 isa<CmpInst>(U->getOperand(1)) ||
1201 isa<ShuffleVectorInst>(U->getOperand(1)) ||
1202 isa<ExtractElementInst>(U->getOperand(1)) ||
1203 isa<InsertElementInst>(U->getOperand(1)) ||
1204 isa<SelectInst>(U->getOperand(1)))
1205 s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
1207 s2 = U->getOperand(1);
1209 // Ternary Operators
1211 if (isa<ShuffleVectorInst>(U) ||
1212 isa<InsertElementInst>(U) ||
1214 if (isa<BinaryOperator>(U->getOperand(2)) ||
1215 isa<CmpInst>(U->getOperand(2)) ||
1216 isa<ShuffleVectorInst>(U->getOperand(2)) ||
1217 isa<ExtractElementInst>(U->getOperand(2)) ||
1218 isa<InsertElementInst>(U->getOperand(2)) ||
1219 isa<SelectInst>(U->getOperand(2)))
1220 s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2)));
1222 s3 = U->getOperand(2);
1225 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
1226 newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
1227 BO->getName()+".gvnpre",
1228 (*PI)->getTerminator());
1229 else if (CmpInst* C = dyn_cast<CmpInst>(U))
1230 newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
1231 C->getName()+".gvnpre",
1232 (*PI)->getTerminator());
1233 else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
1234 newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
1235 (*PI)->getTerminator());
1236 else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
1237 newVal = new InsertElementInst(s1, s2, s3, S->getName()+".gvnpre",
1238 (*PI)->getTerminator());
1239 else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
1240 newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
1241 (*PI)->getTerminator());
1242 else if (SelectInst* S = dyn_cast<SelectInst>(U))
1243 newVal = new SelectInst(S->getCondition(), S->getTrueValue(),
1244 S->getFalseValue(), S->getName()+".gvnpre",
1245 (*PI)->getTerminator());
1248 VN.add(newVal, VN.lookup(U));
1250 SmallPtrSet<Value*, 16>& predAvail = availableOut[*PI];
1251 val_replace(predAvail, newVal);
1253 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1254 if (av != avail.end())
1256 avail.insert(std::make_pair(*PI, newVal));
1264 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1266 p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
1268 p->addIncoming(avail[*PI], *PI);
1271 VN.add(p, VN.lookup(e));
1272 val_replace(availableOut[BB], p);
1278 /// insertion_mergepoint - When walking the dom tree, check at each merge
1279 /// block for the possibility of a partial redundancy. If present, eliminate it
1280 unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
1281 df_iterator<DomTreeNode*>& D,
1282 SmallPtrSet<Value*, 16>& new_set) {
1283 bool changed_function = false;
1284 bool new_stuff = false;
1286 BasicBlock* BB = D->getBlock();
1287 for (unsigned i = 0; i < workList.size(); ++i) {
1288 Value* e = workList[i];
1290 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1291 isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
1292 isa<ShuffleVectorInst>(e) || isa<SelectInst>(e)) {
1293 if (find_leader(availableOut[D->getIDom()->getBlock()],
1297 std::map<BasicBlock*, Value*> avail;
1298 bool by_some = false;
1301 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
1303 Value *e2 = phi_translate(e, *PI, BB);
1304 Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
1307 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1308 if (av != avail.end())
1310 avail.insert(std::make_pair(*PI, e2));
1312 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1313 if (av != avail.end())
1315 avail.insert(std::make_pair(*PI, e3));
1322 if (by_some && num_avail < std::distance(pred_begin(BB), pred_end(BB))) {
1323 insertion_pre(e, BB, avail, new_set);
1325 changed_function = true;
1331 unsigned retval = 0;
1332 if (changed_function)
1340 /// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for
1341 /// merge points. When one is found, check for a partial redundancy. If one is
1342 /// present, eliminate it. Repeat this walk until no changes are made.
1343 bool GVNPRE::insertion(Function& F) {
1344 bool changed_function = false;
1346 DominatorTree &DT = getAnalysis<DominatorTree>();
1348 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > new_sets;
1349 bool new_stuff = true;
1352 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1353 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1354 BasicBlock* BB = DI->getBlock();
1359 SmallPtrSet<Value*, 16>& new_set = new_sets[BB];
1360 SmallPtrSet<Value*, 16>& availOut = availableOut[BB];
1361 SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB];
1365 // Replace leaders with leaders inherited from dominator
1366 if (DI->getIDom() != 0) {
1367 SmallPtrSet<Value*, 16>& dom_set = new_sets[DI->getIDom()->getBlock()];
1368 for (SmallPtrSet<Value*, 16>::iterator I = dom_set.begin(),
1369 E = dom_set.end(); I != E; ++I) {
1371 val_replace(availOut, *I);
1375 // If there is more than one predecessor...
1376 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
1377 std::vector<Value*> workList;
1378 workList.reserve(anticIn.size());
1379 topo_sort(anticIn, workList);
1381 unsigned result = insertion_mergepoint(workList, DI, new_set);
1383 changed_function = true;
1390 return changed_function;
1393 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1396 bool GVNPRE::runOnFunction(Function &F) {
1397 // Clean out global sets from any previous functions
1399 createdExpressions.clear();
1400 availableOut.clear();
1401 anticipatedIn.clear();
1403 bool changed_function = false;
1405 // Phase 1: BuildSets
1406 // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1409 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
1410 DOUT << "AVAIL_OUT: " << FI->getName() << "\n";
1411 dump(availableOut[FI]);
1413 DOUT << "ANTIC_IN: " << FI->getName() << "\n";
1414 dump(anticipatedIn[FI]);
1419 // This phase inserts values to make partially redundant values
1421 changed_function |= insertion(F);
1423 // Phase 3: Eliminate
1424 // This phase performs trivial full redundancy elimination
1425 changed_function |= elimination();
1428 // This phase cleans up values that were created solely
1429 // as leaders for expressions
1432 return changed_function;