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/Support/CFG.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Debug.h"
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
48 /// This class holds the mapping between values and value numbers. It is used
49 /// as an efficient mechanism to determine the expression-wise equivalence of
53 class VISIBILITY_HIDDEN ValueTable {
56 enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM,
57 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
58 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
59 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
60 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
61 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
62 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
65 ExpressionOpcode opcode;
71 bool operator< (const Expression& other) const {
72 if (opcode < other.opcode)
74 else if (opcode > other.opcode)
76 else if (type < other.type)
78 else if (type > other.type)
80 else if (firstVN < other.firstVN)
82 else if (firstVN > other.firstVN)
84 else if (secondVN < other.secondVN)
86 else if (secondVN > other.secondVN)
88 else if (thirdVN < other.thirdVN)
90 else if (thirdVN > other.thirdVN)
98 DenseMap<Value*, uint32_t> valueNumbering;
99 std::map<Expression, uint32_t> expressionNumbering;
101 uint32_t nextValueNumber;
103 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
104 Expression::ExpressionOpcode getOpcode(CmpInst* C);
105 Expression create_expression(BinaryOperator* BO);
106 Expression create_expression(CmpInst* C);
107 Expression create_expression(ShuffleVectorInst* V);
108 Expression create_expression(ExtractElementInst* C);
109 Expression create_expression(InsertElementInst* V);
110 Expression create_expression(SelectInst* V);
112 ValueTable() { nextValueNumber = 1; }
113 uint32_t lookup_or_add(Value* V);
114 uint32_t lookup(Value* V) const;
115 void add(Value* V, uint32_t num);
117 void erase(Value* v);
122 //===----------------------------------------------------------------------===//
123 // ValueTable Internal Functions
124 //===----------------------------------------------------------------------===//
125 ValueTable::Expression::ExpressionOpcode
126 ValueTable::getOpcode(BinaryOperator* BO) {
127 switch(BO->getOpcode()) {
128 case Instruction::Add:
129 return Expression::ADD;
130 case Instruction::Sub:
131 return Expression::SUB;
132 case Instruction::Mul:
133 return Expression::MUL;
134 case Instruction::UDiv:
135 return Expression::UDIV;
136 case Instruction::SDiv:
137 return Expression::SDIV;
138 case Instruction::FDiv:
139 return Expression::FDIV;
140 case Instruction::URem:
141 return Expression::UREM;
142 case Instruction::SRem:
143 return Expression::SREM;
144 case Instruction::FRem:
145 return Expression::FREM;
146 case Instruction::Shl:
147 return Expression::SHL;
148 case Instruction::LShr:
149 return Expression::LSHR;
150 case Instruction::AShr:
151 return Expression::ASHR;
152 case Instruction::And:
153 return Expression::AND;
154 case Instruction::Or:
155 return Expression::OR;
156 case Instruction::Xor:
157 return Expression::XOR;
159 // THIS SHOULD NEVER HAPPEN
161 assert(0 && "Binary operator with unknown opcode?");
162 return Expression::ADD;
166 ValueTable::Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
167 if (C->getOpcode() == Instruction::ICmp) {
168 switch (C->getPredicate()) {
169 case ICmpInst::ICMP_EQ:
170 return Expression::ICMPEQ;
171 case ICmpInst::ICMP_NE:
172 return Expression::ICMPNE;
173 case ICmpInst::ICMP_UGT:
174 return Expression::ICMPUGT;
175 case ICmpInst::ICMP_UGE:
176 return Expression::ICMPUGE;
177 case ICmpInst::ICMP_ULT:
178 return Expression::ICMPULT;
179 case ICmpInst::ICMP_ULE:
180 return Expression::ICMPULE;
181 case ICmpInst::ICMP_SGT:
182 return Expression::ICMPSGT;
183 case ICmpInst::ICMP_SGE:
184 return Expression::ICMPSGE;
185 case ICmpInst::ICMP_SLT:
186 return Expression::ICMPSLT;
187 case ICmpInst::ICMP_SLE:
188 return Expression::ICMPSLE;
190 // THIS SHOULD NEVER HAPPEN
192 assert(0 && "Comparison with unknown predicate?");
193 return Expression::ICMPEQ;
196 switch (C->getPredicate()) {
197 case FCmpInst::FCMP_OEQ:
198 return Expression::FCMPOEQ;
199 case FCmpInst::FCMP_OGT:
200 return Expression::FCMPOGT;
201 case FCmpInst::FCMP_OGE:
202 return Expression::FCMPOGE;
203 case FCmpInst::FCMP_OLT:
204 return Expression::FCMPOLT;
205 case FCmpInst::FCMP_OLE:
206 return Expression::FCMPOLE;
207 case FCmpInst::FCMP_ONE:
208 return Expression::FCMPONE;
209 case FCmpInst::FCMP_ORD:
210 return Expression::FCMPORD;
211 case FCmpInst::FCMP_UNO:
212 return Expression::FCMPUNO;
213 case FCmpInst::FCMP_UEQ:
214 return Expression::FCMPUEQ;
215 case FCmpInst::FCMP_UGT:
216 return Expression::FCMPUGT;
217 case FCmpInst::FCMP_UGE:
218 return Expression::FCMPUGE;
219 case FCmpInst::FCMP_ULT:
220 return Expression::FCMPULT;
221 case FCmpInst::FCMP_ULE:
222 return Expression::FCMPULE;
223 case FCmpInst::FCMP_UNE:
224 return Expression::FCMPUNE;
226 // THIS SHOULD NEVER HAPPEN
228 assert(0 && "Comparison with unknown predicate?");
229 return Expression::FCMPOEQ;
234 ValueTable::Expression ValueTable::create_expression(BinaryOperator* BO) {
237 e.firstVN = lookup_or_add(BO->getOperand(0));
238 e.secondVN = lookup_or_add(BO->getOperand(1));
240 e.type = BO->getType();
241 e.opcode = getOpcode(BO);
246 ValueTable::Expression ValueTable::create_expression(CmpInst* C) {
249 e.firstVN = lookup_or_add(C->getOperand(0));
250 e.secondVN = lookup_or_add(C->getOperand(1));
252 e.type = C->getType();
253 e.opcode = getOpcode(C);
258 ValueTable::Expression ValueTable::create_expression(ShuffleVectorInst* S) {
261 e.firstVN = lookup_or_add(S->getOperand(0));
262 e.secondVN = lookup_or_add(S->getOperand(1));
263 e.thirdVN = lookup_or_add(S->getOperand(2));
264 e.type = S->getType();
265 e.opcode = Expression::SHUFFLE;
270 ValueTable::Expression ValueTable::create_expression(ExtractElementInst* E) {
273 e.firstVN = lookup_or_add(E->getOperand(0));
274 e.secondVN = lookup_or_add(E->getOperand(1));
276 e.type = E->getType();
277 e.opcode = Expression::EXTRACT;
282 ValueTable::Expression ValueTable::create_expression(InsertElementInst* I) {
285 e.firstVN = lookup_or_add(I->getOperand(0));
286 e.secondVN = lookup_or_add(I->getOperand(1));
287 e.thirdVN = lookup_or_add(I->getOperand(2));
288 e.type = I->getType();
289 e.opcode = Expression::INSERT;
294 ValueTable::Expression ValueTable::create_expression(SelectInst* I) {
297 e.firstVN = lookup_or_add(I->getCondition());
298 e.secondVN = lookup_or_add(I->getTrueValue());
299 e.thirdVN = lookup_or_add(I->getFalseValue());
300 e.type = I->getType();
301 e.opcode = Expression::SELECT;
306 //===----------------------------------------------------------------------===//
307 // ValueTable External Functions
308 //===----------------------------------------------------------------------===//
310 /// lookup_or_add - Returns the value number for the specified value, assigning
311 /// it a new number if it did not have one before.
312 uint32_t ValueTable::lookup_or_add(Value* V) {
313 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
314 if (VI != valueNumbering.end())
318 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
319 Expression e = create_expression(BO);
321 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
322 if (EI != expressionNumbering.end()) {
323 valueNumbering.insert(std::make_pair(V, EI->second));
326 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
327 valueNumbering.insert(std::make_pair(V, nextValueNumber));
329 return nextValueNumber++;
331 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
332 Expression e = create_expression(C);
334 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
335 if (EI != expressionNumbering.end()) {
336 valueNumbering.insert(std::make_pair(V, EI->second));
339 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
340 valueNumbering.insert(std::make_pair(V, nextValueNumber));
342 return nextValueNumber++;
344 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
345 Expression e = create_expression(U);
347 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
348 if (EI != expressionNumbering.end()) {
349 valueNumbering.insert(std::make_pair(V, EI->second));
352 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
353 valueNumbering.insert(std::make_pair(V, nextValueNumber));
355 return nextValueNumber++;
357 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
358 Expression e = create_expression(U);
360 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
361 if (EI != expressionNumbering.end()) {
362 valueNumbering.insert(std::make_pair(V, EI->second));
365 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
366 valueNumbering.insert(std::make_pair(V, nextValueNumber));
368 return nextValueNumber++;
370 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
371 Expression e = create_expression(U);
373 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
374 if (EI != expressionNumbering.end()) {
375 valueNumbering.insert(std::make_pair(V, EI->second));
378 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
379 valueNumbering.insert(std::make_pair(V, nextValueNumber));
381 return nextValueNumber++;
383 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
384 Expression e = create_expression(U);
386 std::map<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
387 if (EI != expressionNumbering.end()) {
388 valueNumbering.insert(std::make_pair(V, EI->second));
391 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
392 valueNumbering.insert(std::make_pair(V, nextValueNumber));
394 return nextValueNumber++;
397 valueNumbering.insert(std::make_pair(V, nextValueNumber));
398 return nextValueNumber++;
402 /// lookup - Returns the value number of the specified value. Fails if
403 /// the value has not yet been numbered.
404 uint32_t ValueTable::lookup(Value* V) const {
405 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
406 if (VI != valueNumbering.end())
409 assert(0 && "Value not numbered?");
414 /// add - Add the specified value with the given value number, removing
415 /// its old number, if any
416 void ValueTable::add(Value* V, uint32_t num) {
417 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
418 if (VI != valueNumbering.end())
419 valueNumbering.erase(VI);
420 valueNumbering.insert(std::make_pair(V, num));
423 /// clear - Remove all entries from the ValueTable
424 void ValueTable::clear() {
425 valueNumbering.clear();
426 expressionNumbering.clear();
430 /// erase - Remove a value from the value numbering
431 void ValueTable::erase(Value* V) {
432 valueNumbering.erase(V);
435 /// size - Return the number of assigned value numbers
436 unsigned ValueTable::size() {
437 // NOTE: zero is never assigned
438 return nextValueNumber;
441 //===----------------------------------------------------------------------===//
443 //===----------------------------------------------------------------------===//
447 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
448 bool runOnFunction(Function &F);
450 static char ID; // Pass identification, replacement for typeid
451 GVNPRE() : FunctionPass((intptr_t)&ID) { }
455 std::vector<Instruction*> createdExpressions;
457 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > availableOut;
458 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > anticipatedIn;
460 // This transformation requires dominator postdominator info
461 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
462 AU.setPreservesCFG();
463 AU.addRequired<DominatorTree>();
467 // FIXME: eliminate or document these better
468 void dump(const SmallPtrSet<Value*, 16>& s) const;
469 void clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet);
470 Value* find_leader(SmallPtrSet<Value*, 16>& vals,
472 Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ);
473 void phi_translate_set(SmallPtrSet<Value*, 16>& anticIn, BasicBlock* pred,
474 BasicBlock* succ, SmallPtrSet<Value*, 16>& out);
476 void topo_sort(SmallPtrSet<Value*, 16>& set,
477 std::vector<Value*>& vec);
482 void val_insert(SmallPtrSet<Value*, 16>& s, Value* v);
483 void val_replace(SmallPtrSet<Value*, 16>& s, Value* v);
484 bool dependsOnInvoke(Value* V);
485 void buildsets_availout(BasicBlock::iterator I,
486 SmallPtrSet<Value*, 16>& currAvail,
487 SmallPtrSet<PHINode*, 16>& currPhis,
488 SmallPtrSet<Value*, 16>& currExps,
489 SmallPtrSet<Value*, 16>& currTemps,
490 BitVector& availNumbers,
491 BitVector& expNumbers);
492 bool buildsets_anticout(BasicBlock* BB,
493 SmallPtrSet<Value*, 16>& anticOut,
494 std::set<BasicBlock*>& visited);
495 unsigned buildsets_anticin(BasicBlock* BB,
496 SmallPtrSet<Value*, 16>& anticOut,
497 SmallPtrSet<Value*, 16>& currExps,
498 SmallPtrSet<Value*, 16>& currTemps,
499 std::set<BasicBlock*>& visited);
500 void buildsets(Function& F);
502 void insertion_pre(Value* e, BasicBlock* BB,
503 std::map<BasicBlock*, Value*>& avail,
504 SmallPtrSet<Value*, 16>& new_set);
505 unsigned insertion_mergepoint(std::vector<Value*>& workList,
506 df_iterator<DomTreeNode*>& D,
507 SmallPtrSet<Value*, 16>& new_set);
508 bool insertion(Function& F);
516 // createGVNPREPass - The public interface to this file...
517 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
519 RegisterPass<GVNPRE> X("gvnpre",
520 "Global Value Numbering/Partial Redundancy Elimination");
523 STATISTIC(NumInsertedVals, "Number of values inserted");
524 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
525 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
527 /// find_leader - Given a set and a value number, return the first
528 /// element of the set with that value number, or 0 if no such element
530 Value* GVNPRE::find_leader(SmallPtrSet<Value*, 16>& vals, uint32_t v) {
531 for (SmallPtrSet<Value*, 16>::iterator I = vals.begin(), E = vals.end();
533 if (v == VN.lookup(*I))
539 /// val_insert - Insert a value into a set only if there is not a value
540 /// with the same value number already in the set
541 void GVNPRE::val_insert(SmallPtrSet<Value*, 16>& s, Value* v) {
542 uint32_t num = VN.lookup(v);
543 Value* leader = find_leader(s, num);
548 /// val_replace - Insert a value into a set, replacing any values already in
549 /// the set that have the same value number
550 void GVNPRE::val_replace(SmallPtrSet<Value*, 16>& s, Value* v) {
551 uint32_t num = VN.lookup(v);
552 Value* leader = find_leader(s, num);
553 while (leader != 0) {
555 leader = find_leader(s, num);
560 /// phi_translate - Given a value, its parent block, and a predecessor of its
561 /// parent, translate the value into legal for the predecessor block. This
562 /// means translating its operands (and recursively, their operands) through
563 /// any phi nodes in the parent into values available in the predecessor
564 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
569 if (isa<BinaryOperator>(V) || isa<CmpInst>(V) ||
570 isa<ExtractElementInst>(V)) {
571 User* U = cast<User>(V);
574 if (isa<Instruction>(U->getOperand(0)))
575 newOp1 = phi_translate(U->getOperand(0), pred, succ);
577 newOp1 = U->getOperand(0);
583 if (isa<Instruction>(U->getOperand(1)))
584 newOp2 = phi_translate(U->getOperand(1), pred, succ);
586 newOp2 = U->getOperand(1);
591 if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
592 Instruction* newVal = 0;
593 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
594 newVal = BinaryOperator::create(BO->getOpcode(),
596 BO->getName()+".expr");
597 else if (CmpInst* C = dyn_cast<CmpInst>(U))
598 newVal = CmpInst::create(C->getOpcode(),
601 C->getName()+".expr");
602 else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
603 newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
605 uint32_t v = VN.lookup_or_add(newVal);
607 Value* leader = find_leader(availableOut[pred], v);
609 createdExpressions.push_back(newVal);
618 // Ternary Operations
619 } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
620 isa<SelectInst>(V)) {
621 User* U = cast<User>(V);
624 if (isa<Instruction>(U->getOperand(0)))
625 newOp1 = phi_translate(U->getOperand(0), pred, succ);
627 newOp1 = U->getOperand(0);
633 if (isa<Instruction>(U->getOperand(1)))
634 newOp2 = phi_translate(U->getOperand(1), pred, succ);
636 newOp2 = U->getOperand(1);
642 if (isa<Instruction>(U->getOperand(2)))
643 newOp3 = phi_translate(U->getOperand(2), pred, succ);
645 newOp3 = U->getOperand(2);
650 if (newOp1 != U->getOperand(0) ||
651 newOp2 != U->getOperand(1) ||
652 newOp3 != U->getOperand(2)) {
653 Instruction* newVal = 0;
654 if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
655 newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
656 S->getName()+".expr");
657 else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
658 newVal = new InsertElementInst(newOp1, newOp2, newOp3,
659 I->getName()+".expr");
660 else if (SelectInst* I = dyn_cast<SelectInst>(U))
661 newVal = new SelectInst(newOp1, newOp2, newOp3, I->getName()+".expr");
663 uint32_t v = VN.lookup_or_add(newVal);
665 Value* leader = find_leader(availableOut[pred], v);
667 createdExpressions.push_back(newVal);
677 } else if (PHINode* P = dyn_cast<PHINode>(V)) {
678 if (P->getParent() == succ)
679 return P->getIncomingValueForBlock(pred);
685 /// phi_translate_set - Perform phi translation on every element of a set
686 void GVNPRE::phi_translate_set(SmallPtrSet<Value*, 16>& anticIn,
687 BasicBlock* pred, BasicBlock* succ,
688 SmallPtrSet<Value*, 16>& out) {
689 for (SmallPtrSet<Value*, 16>::iterator I = anticIn.begin(),
690 E = anticIn.end(); I != E; ++I) {
691 Value* V = phi_translate(*I, pred, succ);
697 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of
698 /// whose inputs is an invoke instruction. If this is true, we cannot safely
699 /// PRE the instruction or anything that depends on it.
700 bool GVNPRE::dependsOnInvoke(Value* V) {
701 if (PHINode* p = dyn_cast<PHINode>(V)) {
702 for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I)
703 if (isa<InvokeInst>(*I))
711 /// clean - Remove all non-opaque values from the set whose operands are not
712 /// themselves in the set, as well as all values that depend on invokes (see
714 void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
715 std::vector<Value*> worklist;
716 worklist.reserve(set.size());
717 topo_sort(set, worklist);
719 for (unsigned i = 0; i < worklist.size(); ++i) {
720 Value* v = worklist[i];
723 if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
724 isa<ExtractElementInst>(v)) {
725 User* U = cast<User>(v);
727 bool lhsValid = !isa<Instruction>(U->getOperand(0));
728 lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0)));
730 lhsValid = !dependsOnInvoke(U->getOperand(0));
732 bool rhsValid = !isa<Instruction>(U->getOperand(1));
733 rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1)));
735 rhsValid = !dependsOnInvoke(U->getOperand(1));
737 if (!lhsValid || !rhsValid) {
739 presentInSet.flip(VN.lookup(U));
742 // Handle ternary ops
743 } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
744 isa<SelectInst>(v)) {
745 User* U = cast<User>(v);
747 bool lhsValid = !isa<Instruction>(U->getOperand(0));
748 lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0)));
750 lhsValid = !dependsOnInvoke(U->getOperand(0));
752 bool rhsValid = !isa<Instruction>(U->getOperand(1));
753 rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1)));
755 rhsValid = !dependsOnInvoke(U->getOperand(1));
757 bool thirdValid = !isa<Instruction>(U->getOperand(2));
758 thirdValid |= presentInSet.test(VN.lookup(U->getOperand(2)));
760 thirdValid = !dependsOnInvoke(U->getOperand(2));
762 if (!lhsValid || !rhsValid || !thirdValid) {
764 presentInSet.flip(VN.lookup(U));
770 /// topo_sort - Given a set of values, sort them by topological
771 /// order into the provided vector.
772 void GVNPRE::topo_sort(SmallPtrSet<Value*, 16>& set, std::vector<Value*>& vec) {
773 SmallPtrSet<Value*, 16> visited;
774 std::vector<Value*> stack;
775 for (SmallPtrSet<Value*, 16>::iterator I = set.begin(), E = set.end();
777 if (visited.count(*I) == 0)
780 while (!stack.empty()) {
781 Value* e = stack.back();
784 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
785 isa<ExtractElementInst>(e)) {
786 User* U = cast<User>(e);
787 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
788 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
790 if (l != 0 && isa<Instruction>(l) &&
791 visited.count(l) == 0)
793 else if (r != 0 && isa<Instruction>(r) &&
794 visited.count(r) == 0)
802 // Handle ternary ops
803 } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) ||
804 isa<SelectInst>(e)) {
805 User* U = cast<User>(e);
806 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
807 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
808 Value* m = find_leader(set, VN.lookup(U->getOperand(2)));
810 if (l != 0 && isa<Instruction>(l) &&
811 visited.count(l) == 0)
813 else if (r != 0 && isa<Instruction>(r) &&
814 visited.count(r) == 0)
816 else if (m != 0 && isa<Instruction>(m) &&
817 visited.count(m) == 0)
837 /// dump - Dump a set of values to standard error
838 void GVNPRE::dump(const SmallPtrSet<Value*, 16>& s) const {
840 for (SmallPtrSet<Value*, 16>::iterator I = s.begin(), E = s.end();
842 DOUT << "" << VN.lookup(*I) << ": ";
848 /// elimination - Phase 3 of the main algorithm. Perform full redundancy
849 /// elimination by walking the dominator tree and removing any instruction that
850 /// is dominated by another instruction with the same value number.
851 bool GVNPRE::elimination() {
852 DOUT << "\n\nPhase 3: Elimination\n\n";
854 bool changed_function = false;
856 std::vector<std::pair<Instruction*, Value*> > replace;
857 std::vector<Instruction*> erase;
859 DominatorTree& DT = getAnalysis<DominatorTree>();
861 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
862 E = df_end(DT.getRootNode()); DI != E; ++DI) {
863 BasicBlock* BB = DI->getBlock();
865 //DOUT << "Block: " << BB->getName() << "\n";
866 //dump(availableOut[BB]);
869 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
872 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) ||
873 isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) ||
874 isa<ExtractElementInst>(BI) || isa<SelectInst>(BI)) {
875 Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
878 if (Instruction* Instr = dyn_cast<Instruction>(leader))
879 if (Instr->getParent() != 0 && Instr != BI) {
880 replace.push_back(std::make_pair(BI, leader));
888 while (!replace.empty()) {
889 std::pair<Instruction*, Value*> rep = replace.back();
891 rep.first->replaceAllUsesWith(rep.second);
892 changed_function = true;
895 for (std::vector<Instruction*>::iterator I = erase.begin(), E = erase.end();
897 (*I)->eraseFromParent();
899 return changed_function;
902 /// cleanup - Delete any extraneous values that were created to represent
903 /// expressions without leaders.
904 void GVNPRE::cleanup() {
905 while (!createdExpressions.empty()) {
906 Instruction* I = createdExpressions.back();
907 createdExpressions.pop_back();
913 /// buildsets_availout - When calculating availability, handle an instruction
914 /// by inserting it into the appropriate sets
915 void GVNPRE::buildsets_availout(BasicBlock::iterator I,
916 SmallPtrSet<Value*, 16>& currAvail,
917 SmallPtrSet<PHINode*, 16>& currPhis,
918 SmallPtrSet<Value*, 16>& currExps,
919 SmallPtrSet<Value*, 16>& currTemps,
920 BitVector& availNumbers,
921 BitVector& expNumbers) {
923 if (PHINode* p = dyn_cast<PHINode>(I)) {
925 expNumbers.resize(VN.size());
926 availNumbers.resize(VN.size());
931 } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
932 isa<ExtractElementInst>(I)) {
933 User* U = cast<User>(I);
934 Value* leftValue = U->getOperand(0);
935 Value* rightValue = U->getOperand(1);
937 unsigned num = VN.lookup_or_add(U);
938 expNumbers.resize(VN.size());
939 availNumbers.resize(VN.size());
941 if (isa<Instruction>(leftValue))
942 if (!expNumbers.test(VN.lookup(leftValue))) {
943 currExps.insert(leftValue);
944 expNumbers.set(VN.lookup(leftValue));
947 if (isa<Instruction>(rightValue))
948 if (!expNumbers.test(VN.lookup(rightValue))) {
949 currExps.insert(rightValue);
950 expNumbers.set(VN.lookup(rightValue));
953 if (!expNumbers.test(VN.lookup(U))) {
958 // Handle ternary ops
959 } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) ||
960 isa<SelectInst>(I)) {
961 User* U = cast<User>(I);
962 Value* leftValue = U->getOperand(0);
963 Value* rightValue = U->getOperand(1);
964 Value* thirdValue = U->getOperand(2);
968 unsigned num = VN.lookup_or_add(U);
969 expNumbers.resize(VN.size());
970 availNumbers.resize(VN.size());
972 if (isa<Instruction>(leftValue))
973 if (!expNumbers.test(VN.lookup(leftValue))) {
974 currExps.insert(leftValue);
975 expNumbers.set(VN.lookup(leftValue));
977 if (isa<Instruction>(rightValue))
978 if (!expNumbers.test(VN.lookup(rightValue))) {
979 currExps.insert(rightValue);
980 expNumbers.set(VN.lookup(rightValue));
982 if (isa<Instruction>(thirdValue))
983 if (!expNumbers.test(VN.lookup(thirdValue))) {
984 currExps.insert(thirdValue);
985 expNumbers.set(VN.lookup(thirdValue));
988 if (!expNumbers.test(VN.lookup(U))) {
994 } else if (!I->isTerminator()){
996 expNumbers.resize(VN.size());
997 availNumbers.resize(VN.size());
1002 if (!I->isTerminator())
1003 if (!availNumbers.test(VN.lookup(I))) {
1004 currAvail.insert(I);
1005 availNumbers.set(VN.lookup(I));
1009 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
1010 /// set as a function of the ANTIC_IN set of the block's predecessors
1011 bool GVNPRE::buildsets_anticout(BasicBlock* BB,
1012 SmallPtrSet<Value*, 16>& anticOut,
1013 std::set<BasicBlock*>& visited) {
1014 if (BB->getTerminator()->getNumSuccessors() == 1) {
1015 if (BB->getTerminator()->getSuccessor(0) != BB &&
1016 visited.count(BB->getTerminator()->getSuccessor(0)) == 0) {
1017 DOUT << "DEFER: " << BB->getName() << "\n";
1021 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
1022 BB, BB->getTerminator()->getSuccessor(0), anticOut);
1024 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
1025 BasicBlock* first = BB->getTerminator()->getSuccessor(0);
1026 anticOut.insert(anticipatedIn[first].begin(), anticipatedIn[first].end());
1028 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
1029 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
1030 SmallPtrSet<Value*, 16>& succAnticIn = anticipatedIn[currSucc];
1032 std::vector<Value*> temp;
1034 for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(),
1035 E = anticOut.end(); I != E; ++I)
1036 if (succAnticIn.count(*I) == 0)
1039 for (std::vector<Value*>::iterator I = temp.begin(), E = temp.end();
1048 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
1049 /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
1050 /// sets populated in buildsets_availout
1051 unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
1052 SmallPtrSet<Value*, 16>& anticOut,
1053 SmallPtrSet<Value*, 16>& currExps,
1054 SmallPtrSet<Value*, 16>& currTemps,
1055 std::set<BasicBlock*>& visited) {
1056 SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB];
1057 unsigned old = anticIn.size();
1059 bool defer = buildsets_anticout(BB, anticOut, visited);
1065 BitVector numbers(VN.size());
1066 for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(),
1067 E = anticOut.end(); I != E; ++I) {
1068 unsigned num = VN.lookup_or_add(*I);
1069 numbers.resize(VN.size());
1071 if (isa<Instruction>(*I)) {
1076 for (SmallPtrSet<Value*, 16>::iterator I = currExps.begin(),
1077 E = currExps.end(); I != E; ++I) {
1078 if (!numbers.test(VN.lookup_or_add(*I))) {
1080 numbers.set(VN.lookup(*I));
1084 for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
1085 E = currTemps.end(); I != E; ++I) {
1087 numbers.flip(VN.lookup(*I));
1090 clean(anticIn, numbers);
1093 if (old != anticIn.size())
1099 /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
1100 /// and the ANTIC_IN sets.
1101 void GVNPRE::buildsets(Function& F) {
1102 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedExpressions;
1103 std::map<BasicBlock*, SmallPtrSet<PHINode*, 16> > generatedPhis;
1104 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
1106 DominatorTree &DT = getAnalysis<DominatorTree>();
1108 // Phase 1, Part 1: calculate AVAIL_OUT
1110 // Top-down walk of the dominator tree
1111 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1112 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1114 // Get the sets to update for this block
1115 SmallPtrSet<Value*, 16>& currExps = generatedExpressions[DI->getBlock()];
1116 SmallPtrSet<PHINode*, 16>& currPhis = generatedPhis[DI->getBlock()];
1117 SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
1118 SmallPtrSet<Value*, 16>& currAvail = availableOut[DI->getBlock()];
1120 BasicBlock* BB = DI->getBlock();
1122 // A block inherits AVAIL_OUT from its dominator
1123 if (DI->getIDom() != 0)
1124 currAvail.insert(availableOut[DI->getIDom()->getBlock()].begin(),
1125 availableOut[DI->getIDom()->getBlock()].end());
1127 BitVector availNumbers(VN.size());
1128 for (SmallPtrSet<Value*, 16>::iterator I = currAvail.begin(),
1129 E = currAvail.end(); I != E; ++I)
1130 availNumbers.set(VN.lookup(*I));
1132 BitVector expNumbers(VN.size());
1133 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1135 buildsets_availout(BI, currAvail, currPhis, currExps,
1136 currTemps, availNumbers, expNumbers);
1140 // Phase 1, Part 2: calculate ANTIC_IN
1142 std::set<BasicBlock*> visited;
1143 SmallPtrSet<BasicBlock*, 4> block_changed;
1144 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
1145 block_changed.insert(FI);
1147 bool changed = true;
1148 unsigned iterations = 0;
1152 SmallPtrSet<Value*, 16> anticOut;
1154 // Postorder walk of the CFG
1155 for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
1156 BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) {
1157 BasicBlock* BB = *BBI;
1159 if (block_changed.count(BB) != 0) {
1160 unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
1161 generatedTemporaries[BB], visited);
1170 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1172 block_changed.insert(*PI);
1175 block_changed.erase(BB);
1177 changed |= (ret == 2);
1185 DOUT << "ITERATIONS: " << iterations << "\n";
1188 /// insertion_pre - When a partial redundancy has been identified, eliminate it
1189 /// by inserting appropriate values into the predecessors and a phi node in
1191 void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
1192 std::map<BasicBlock*, Value*>& avail,
1193 SmallPtrSet<Value*, 16>& new_set) {
1194 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1195 Value* e2 = avail[*PI];
1196 if (!find_leader(availableOut[*PI], VN.lookup(e2))) {
1197 User* U = cast<User>(e2);
1200 if (isa<BinaryOperator>(U->getOperand(0)) ||
1201 isa<CmpInst>(U->getOperand(0)) ||
1202 isa<ShuffleVectorInst>(U->getOperand(0)) ||
1203 isa<ExtractElementInst>(U->getOperand(0)) ||
1204 isa<InsertElementInst>(U->getOperand(0)) ||
1205 isa<SelectInst>(U->getOperand(0)))
1206 s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
1208 s1 = U->getOperand(0);
1211 if (isa<BinaryOperator>(U->getOperand(1)) ||
1212 isa<CmpInst>(U->getOperand(1)) ||
1213 isa<ShuffleVectorInst>(U->getOperand(1)) ||
1214 isa<ExtractElementInst>(U->getOperand(1)) ||
1215 isa<InsertElementInst>(U->getOperand(1)) ||
1216 isa<SelectInst>(U->getOperand(1)))
1217 s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
1219 s2 = U->getOperand(1);
1221 // Ternary Operators
1223 if (isa<ShuffleVectorInst>(U) ||
1224 isa<InsertElementInst>(U) ||
1226 if (isa<BinaryOperator>(U->getOperand(2)) ||
1227 isa<CmpInst>(U->getOperand(2)) ||
1228 isa<ShuffleVectorInst>(U->getOperand(2)) ||
1229 isa<ExtractElementInst>(U->getOperand(2)) ||
1230 isa<InsertElementInst>(U->getOperand(2)) ||
1231 isa<SelectInst>(U->getOperand(2)))
1232 s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2)));
1234 s3 = U->getOperand(2);
1237 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
1238 newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
1239 BO->getName()+".gvnpre",
1240 (*PI)->getTerminator());
1241 else if (CmpInst* C = dyn_cast<CmpInst>(U))
1242 newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
1243 C->getName()+".gvnpre",
1244 (*PI)->getTerminator());
1245 else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
1246 newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
1247 (*PI)->getTerminator());
1248 else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
1249 newVal = new InsertElementInst(s1, s2, s3, S->getName()+".gvnpre",
1250 (*PI)->getTerminator());
1251 else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
1252 newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
1253 (*PI)->getTerminator());
1254 else if (SelectInst* S = dyn_cast<SelectInst>(U))
1255 newVal = new SelectInst(S->getCondition(), S->getTrueValue(),
1256 S->getFalseValue(), S->getName()+".gvnpre",
1257 (*PI)->getTerminator());
1260 VN.add(newVal, VN.lookup(U));
1262 SmallPtrSet<Value*, 16>& predAvail = availableOut[*PI];
1263 val_replace(predAvail, newVal);
1265 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1266 if (av != avail.end())
1268 avail.insert(std::make_pair(*PI, newVal));
1276 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1278 p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
1280 p->addIncoming(avail[*PI], *PI);
1283 VN.add(p, VN.lookup(e));
1284 val_replace(availableOut[BB], p);
1290 /// insertion_mergepoint - When walking the dom tree, check at each merge
1291 /// block for the possibility of a partial redundancy. If present, eliminate it
1292 unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
1293 df_iterator<DomTreeNode*>& D,
1294 SmallPtrSet<Value*, 16>& new_set) {
1295 bool changed_function = false;
1296 bool new_stuff = false;
1298 BasicBlock* BB = D->getBlock();
1299 for (unsigned i = 0; i < workList.size(); ++i) {
1300 Value* e = workList[i];
1302 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1303 isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
1304 isa<ShuffleVectorInst>(e) || isa<SelectInst>(e)) {
1305 if (find_leader(availableOut[D->getIDom()->getBlock()],
1309 std::map<BasicBlock*, Value*> avail;
1310 bool by_some = false;
1313 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
1315 Value *e2 = phi_translate(e, *PI, BB);
1316 Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
1319 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1320 if (av != avail.end())
1322 avail.insert(std::make_pair(*PI, e2));
1324 std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1325 if (av != avail.end())
1327 avail.insert(std::make_pair(*PI, e3));
1334 if (by_some && num_avail < std::distance(pred_begin(BB), pred_end(BB))) {
1335 insertion_pre(e, BB, avail, new_set);
1337 changed_function = true;
1343 unsigned retval = 0;
1344 if (changed_function)
1352 /// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for
1353 /// merge points. When one is found, check for a partial redundancy. If one is
1354 /// present, eliminate it. Repeat this walk until no changes are made.
1355 bool GVNPRE::insertion(Function& F) {
1356 bool changed_function = false;
1358 DominatorTree &DT = getAnalysis<DominatorTree>();
1360 std::map<BasicBlock*, SmallPtrSet<Value*, 16> > new_sets;
1361 bool new_stuff = true;
1364 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1365 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1366 BasicBlock* BB = DI->getBlock();
1371 SmallPtrSet<Value*, 16>& new_set = new_sets[BB];
1372 SmallPtrSet<Value*, 16>& availOut = availableOut[BB];
1373 SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB];
1377 // Replace leaders with leaders inherited from dominator
1378 if (DI->getIDom() != 0) {
1379 SmallPtrSet<Value*, 16>& dom_set = new_sets[DI->getIDom()->getBlock()];
1380 for (SmallPtrSet<Value*, 16>::iterator I = dom_set.begin(),
1381 E = dom_set.end(); I != E; ++I) {
1383 val_replace(availOut, *I);
1387 // If there is more than one predecessor...
1388 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
1389 std::vector<Value*> workList;
1390 workList.reserve(anticIn.size());
1391 topo_sort(anticIn, workList);
1393 unsigned result = insertion_mergepoint(workList, DI, new_set);
1395 changed_function = true;
1402 return changed_function;
1405 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1408 bool GVNPRE::runOnFunction(Function &F) {
1409 // Clean out global sets from any previous functions
1411 createdExpressions.clear();
1412 availableOut.clear();
1413 anticipatedIn.clear();
1415 bool changed_function = false;
1417 // Phase 1: BuildSets
1418 // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1421 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
1422 DOUT << "AVAIL_OUT: " << FI->getName() << "\n";
1423 dump(availableOut[FI]);
1425 DOUT << "ANTIC_IN: " << FI->getName() << "\n";
1426 dump(anticipatedIn[FI]);
1431 // This phase inserts values to make partially redundant values
1433 changed_function |= insertion(F);
1435 // Phase 3: Eliminate
1436 // This phase performs trivial full redundancy elimination
1437 changed_function |= elimination();
1440 // This phase cleans up values that were created solely
1441 // as leaders for expressions
1444 return changed_function;