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/SmallVector.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
36 #include "llvm/Support/CFG.h"
37 #include "llvm/Support/Compiler.h"
38 #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 enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM,
54 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
55 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
56 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
57 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
58 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
59 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
60 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
61 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
62 PTRTOINT, INTTOPTR, BITCAST, GEP, EMPTY,
65 ExpressionOpcode opcode;
70 SmallVector<uint32_t, 4> varargs;
73 Expression(ExpressionOpcode o) : opcode(o) { }
75 bool operator==(const Expression &other) const {
76 if (opcode != other.opcode)
78 else if (opcode == EMPTY || opcode == TOMBSTONE)
80 else if (type != other.type)
82 else if (firstVN != other.firstVN)
84 else if (secondVN != other.secondVN)
86 else if (thirdVN != other.thirdVN)
89 if (varargs.size() != other.varargs.size())
92 for (size_t i = 0; i < varargs.size(); ++i)
93 if (varargs[i] != other.varargs[i])
100 bool operator!=(const Expression &other) const {
101 if (opcode != other.opcode)
103 else if (opcode == EMPTY || opcode == TOMBSTONE)
105 else if (type != other.type)
107 else if (firstVN != other.firstVN)
109 else if (secondVN != other.secondVN)
111 else if (thirdVN != other.thirdVN)
114 if (varargs.size() != other.varargs.size())
117 for (size_t i = 0; i < varargs.size(); ++i)
118 if (varargs[i] != other.varargs[i])
128 class VISIBILITY_HIDDEN ValueTable {
130 DenseMap<Value*, uint32_t> valueNumbering;
131 DenseMap<Expression, uint32_t> expressionNumbering;
133 uint32_t nextValueNumber;
135 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
136 Expression::ExpressionOpcode getOpcode(CmpInst* C);
137 Expression::ExpressionOpcode getOpcode(CastInst* C);
138 Expression create_expression(BinaryOperator* BO);
139 Expression create_expression(CmpInst* C);
140 Expression create_expression(ShuffleVectorInst* V);
141 Expression create_expression(ExtractElementInst* C);
142 Expression create_expression(InsertElementInst* V);
143 Expression create_expression(SelectInst* V);
144 Expression create_expression(CastInst* C);
145 Expression create_expression(GetElementPtrInst* G);
147 ValueTable() { nextValueNumber = 1; }
148 uint32_t lookup_or_add(Value* V);
149 uint32_t lookup(Value* V) const;
150 void add(Value* V, uint32_t num);
152 void erase(Value* v);
158 template <> struct DenseMapKeyInfo<Expression> {
159 static inline Expression getEmptyKey() {
160 return Expression(Expression::EMPTY);
163 static inline Expression getTombstoneKey() {
164 return Expression(Expression::TOMBSTONE);
167 static unsigned getHashValue(const Expression e) {
168 unsigned hash = e.opcode;
170 hash = e.firstVN + hash * 37;
171 hash = e.secondVN + hash * 37;
172 hash = e.thirdVN + hash * 37;
174 hash = (unsigned)((uintptr_t)e.type >> 4) ^
175 (unsigned)((uintptr_t)e.type >> 9) +
178 for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
179 E = e.varargs.end(); I != E; ++I)
180 hash = *I + hash * 37;
184 static bool isPod() { return true; }
188 //===----------------------------------------------------------------------===//
189 // ValueTable Internal Functions
190 //===----------------------------------------------------------------------===//
191 Expression::ExpressionOpcode
192 ValueTable::getOpcode(BinaryOperator* BO) {
193 switch(BO->getOpcode()) {
194 case Instruction::Add:
195 return Expression::ADD;
196 case Instruction::Sub:
197 return Expression::SUB;
198 case Instruction::Mul:
199 return Expression::MUL;
200 case Instruction::UDiv:
201 return Expression::UDIV;
202 case Instruction::SDiv:
203 return Expression::SDIV;
204 case Instruction::FDiv:
205 return Expression::FDIV;
206 case Instruction::URem:
207 return Expression::UREM;
208 case Instruction::SRem:
209 return Expression::SREM;
210 case Instruction::FRem:
211 return Expression::FREM;
212 case Instruction::Shl:
213 return Expression::SHL;
214 case Instruction::LShr:
215 return Expression::LSHR;
216 case Instruction::AShr:
217 return Expression::ASHR;
218 case Instruction::And:
219 return Expression::AND;
220 case Instruction::Or:
221 return Expression::OR;
222 case Instruction::Xor:
223 return Expression::XOR;
225 // THIS SHOULD NEVER HAPPEN
227 assert(0 && "Binary operator with unknown opcode?");
228 return Expression::ADD;
232 Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
233 if (C->getOpcode() == Instruction::ICmp) {
234 switch (C->getPredicate()) {
235 case ICmpInst::ICMP_EQ:
236 return Expression::ICMPEQ;
237 case ICmpInst::ICMP_NE:
238 return Expression::ICMPNE;
239 case ICmpInst::ICMP_UGT:
240 return Expression::ICMPUGT;
241 case ICmpInst::ICMP_UGE:
242 return Expression::ICMPUGE;
243 case ICmpInst::ICMP_ULT:
244 return Expression::ICMPULT;
245 case ICmpInst::ICMP_ULE:
246 return Expression::ICMPULE;
247 case ICmpInst::ICMP_SGT:
248 return Expression::ICMPSGT;
249 case ICmpInst::ICMP_SGE:
250 return Expression::ICMPSGE;
251 case ICmpInst::ICMP_SLT:
252 return Expression::ICMPSLT;
253 case ICmpInst::ICMP_SLE:
254 return Expression::ICMPSLE;
256 // THIS SHOULD NEVER HAPPEN
258 assert(0 && "Comparison with unknown predicate?");
259 return Expression::ICMPEQ;
262 switch (C->getPredicate()) {
263 case FCmpInst::FCMP_OEQ:
264 return Expression::FCMPOEQ;
265 case FCmpInst::FCMP_OGT:
266 return Expression::FCMPOGT;
267 case FCmpInst::FCMP_OGE:
268 return Expression::FCMPOGE;
269 case FCmpInst::FCMP_OLT:
270 return Expression::FCMPOLT;
271 case FCmpInst::FCMP_OLE:
272 return Expression::FCMPOLE;
273 case FCmpInst::FCMP_ONE:
274 return Expression::FCMPONE;
275 case FCmpInst::FCMP_ORD:
276 return Expression::FCMPORD;
277 case FCmpInst::FCMP_UNO:
278 return Expression::FCMPUNO;
279 case FCmpInst::FCMP_UEQ:
280 return Expression::FCMPUEQ;
281 case FCmpInst::FCMP_UGT:
282 return Expression::FCMPUGT;
283 case FCmpInst::FCMP_UGE:
284 return Expression::FCMPUGE;
285 case FCmpInst::FCMP_ULT:
286 return Expression::FCMPULT;
287 case FCmpInst::FCMP_ULE:
288 return Expression::FCMPULE;
289 case FCmpInst::FCMP_UNE:
290 return Expression::FCMPUNE;
292 // THIS SHOULD NEVER HAPPEN
294 assert(0 && "Comparison with unknown predicate?");
295 return Expression::FCMPOEQ;
300 Expression::ExpressionOpcode
301 ValueTable::getOpcode(CastInst* C) {
302 switch(C->getOpcode()) {
303 case Instruction::Trunc:
304 return Expression::TRUNC;
305 case Instruction::ZExt:
306 return Expression::ZEXT;
307 case Instruction::SExt:
308 return Expression::SEXT;
309 case Instruction::FPToUI:
310 return Expression::FPTOUI;
311 case Instruction::FPToSI:
312 return Expression::FPTOSI;
313 case Instruction::UIToFP:
314 return Expression::UITOFP;
315 case Instruction::SIToFP:
316 return Expression::SITOFP;
317 case Instruction::FPTrunc:
318 return Expression::FPTRUNC;
319 case Instruction::FPExt:
320 return Expression::FPEXT;
321 case Instruction::PtrToInt:
322 return Expression::PTRTOINT;
323 case Instruction::IntToPtr:
324 return Expression::INTTOPTR;
325 case Instruction::BitCast:
326 return Expression::BITCAST;
328 // THIS SHOULD NEVER HAPPEN
330 assert(0 && "Cast operator with unknown opcode?");
331 return Expression::BITCAST;
335 Expression ValueTable::create_expression(BinaryOperator* BO) {
338 e.firstVN = lookup_or_add(BO->getOperand(0));
339 e.secondVN = lookup_or_add(BO->getOperand(1));
341 e.type = BO->getType();
342 e.opcode = getOpcode(BO);
347 Expression ValueTable::create_expression(CmpInst* C) {
350 e.firstVN = lookup_or_add(C->getOperand(0));
351 e.secondVN = lookup_or_add(C->getOperand(1));
353 e.type = C->getType();
354 e.opcode = getOpcode(C);
359 Expression ValueTable::create_expression(CastInst* C) {
362 e.firstVN = lookup_or_add(C->getOperand(0));
365 e.type = C->getType();
366 e.opcode = getOpcode(C);
371 Expression ValueTable::create_expression(ShuffleVectorInst* S) {
374 e.firstVN = lookup_or_add(S->getOperand(0));
375 e.secondVN = lookup_or_add(S->getOperand(1));
376 e.thirdVN = lookup_or_add(S->getOperand(2));
377 e.type = S->getType();
378 e.opcode = Expression::SHUFFLE;
383 Expression ValueTable::create_expression(ExtractElementInst* E) {
386 e.firstVN = lookup_or_add(E->getOperand(0));
387 e.secondVN = lookup_or_add(E->getOperand(1));
389 e.type = E->getType();
390 e.opcode = Expression::EXTRACT;
395 Expression ValueTable::create_expression(InsertElementInst* I) {
398 e.firstVN = lookup_or_add(I->getOperand(0));
399 e.secondVN = lookup_or_add(I->getOperand(1));
400 e.thirdVN = lookup_or_add(I->getOperand(2));
401 e.type = I->getType();
402 e.opcode = Expression::INSERT;
407 Expression ValueTable::create_expression(SelectInst* I) {
410 e.firstVN = lookup_or_add(I->getCondition());
411 e.secondVN = lookup_or_add(I->getTrueValue());
412 e.thirdVN = lookup_or_add(I->getFalseValue());
413 e.type = I->getType();
414 e.opcode = Expression::SELECT;
419 Expression ValueTable::create_expression(GetElementPtrInst* G) {
422 e.firstVN = lookup_or_add(G->getPointerOperand());
425 e.type = G->getType();
426 e.opcode = Expression::GEP;
428 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
430 e.varargs.push_back(lookup_or_add(*I));
435 //===----------------------------------------------------------------------===//
436 // ValueTable External Functions
437 //===----------------------------------------------------------------------===//
439 /// lookup_or_add - Returns the value number for the specified value, assigning
440 /// it a new number if it did not have one before.
441 uint32_t ValueTable::lookup_or_add(Value* V) {
442 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
443 if (VI != valueNumbering.end())
447 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
448 Expression e = create_expression(BO);
450 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
451 if (EI != expressionNumbering.end()) {
452 valueNumbering.insert(std::make_pair(V, EI->second));
455 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
456 valueNumbering.insert(std::make_pair(V, nextValueNumber));
458 return nextValueNumber++;
460 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
461 Expression e = create_expression(C);
463 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
464 if (EI != expressionNumbering.end()) {
465 valueNumbering.insert(std::make_pair(V, EI->second));
468 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
469 valueNumbering.insert(std::make_pair(V, nextValueNumber));
471 return nextValueNumber++;
473 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
474 Expression e = create_expression(U);
476 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
477 if (EI != expressionNumbering.end()) {
478 valueNumbering.insert(std::make_pair(V, EI->second));
481 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
482 valueNumbering.insert(std::make_pair(V, nextValueNumber));
484 return nextValueNumber++;
486 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
487 Expression e = create_expression(U);
489 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
490 if (EI != expressionNumbering.end()) {
491 valueNumbering.insert(std::make_pair(V, EI->second));
494 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
495 valueNumbering.insert(std::make_pair(V, nextValueNumber));
497 return nextValueNumber++;
499 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
500 Expression e = create_expression(U);
502 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
503 if (EI != expressionNumbering.end()) {
504 valueNumbering.insert(std::make_pair(V, EI->second));
507 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
508 valueNumbering.insert(std::make_pair(V, nextValueNumber));
510 return nextValueNumber++;
512 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
513 Expression e = create_expression(U);
515 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
516 if (EI != expressionNumbering.end()) {
517 valueNumbering.insert(std::make_pair(V, EI->second));
520 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
521 valueNumbering.insert(std::make_pair(V, nextValueNumber));
523 return nextValueNumber++;
525 } else if (CastInst* U = dyn_cast<CastInst>(V)) {
526 Expression e = create_expression(U);
528 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
529 if (EI != expressionNumbering.end()) {
530 valueNumbering.insert(std::make_pair(V, EI->second));
533 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
534 valueNumbering.insert(std::make_pair(V, nextValueNumber));
536 return nextValueNumber++;
538 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
539 Expression e = create_expression(U);
541 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
542 if (EI != expressionNumbering.end()) {
543 valueNumbering.insert(std::make_pair(V, EI->second));
546 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
547 valueNumbering.insert(std::make_pair(V, nextValueNumber));
549 return nextValueNumber++;
552 valueNumbering.insert(std::make_pair(V, nextValueNumber));
553 return nextValueNumber++;
557 /// lookup - Returns the value number of the specified value. Fails if
558 /// the value has not yet been numbered.
559 uint32_t ValueTable::lookup(Value* V) const {
560 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
561 if (VI != valueNumbering.end())
564 assert(0 && "Value not numbered?");
569 /// add - Add the specified value with the given value number, removing
570 /// its old number, if any
571 void ValueTable::add(Value* V, uint32_t num) {
572 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
573 if (VI != valueNumbering.end())
574 valueNumbering.erase(VI);
575 valueNumbering.insert(std::make_pair(V, num));
578 /// clear - Remove all entries from the ValueTable
579 void ValueTable::clear() {
580 valueNumbering.clear();
581 expressionNumbering.clear();
585 /// erase - Remove a value from the value numbering
586 void ValueTable::erase(Value* V) {
587 valueNumbering.erase(V);
590 /// size - Return the number of assigned value numbers
591 unsigned ValueTable::size() {
592 // NOTE: zero is never assigned
593 return nextValueNumber;
596 //===----------------------------------------------------------------------===//
597 // ValueNumberedSet Class
598 //===----------------------------------------------------------------------===//
600 class ValueNumberedSet {
602 SmallPtrSet<Value*, 8> contents;
605 ValueNumberedSet() { numbers.resize(1); }
606 ValueNumberedSet(const ValueNumberedSet& other) {
607 numbers = other.numbers;
608 contents = other.contents;
611 typedef SmallPtrSet<Value*, 8>::iterator iterator;
613 iterator begin() { return contents.begin(); }
614 iterator end() { return contents.end(); }
616 bool insert(Value* v) { return contents.insert(v); }
617 void insert(iterator I, iterator E) { contents.insert(I, E); }
618 void erase(Value* v) { contents.erase(v); }
619 unsigned count(Value* v) { return contents.count(v); }
620 size_t size() { return contents.size(); }
622 void set(unsigned i) {
623 if (i >= numbers.size())
629 void operator=(const ValueNumberedSet& other) {
630 contents = other.contents;
631 numbers = other.numbers;
634 void reset(unsigned i) {
635 if (i < numbers.size())
639 bool test(unsigned i) {
640 if (i >= numbers.size())
643 return numbers.test(i);
652 //===----------------------------------------------------------------------===//
654 //===----------------------------------------------------------------------===//
658 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
659 bool runOnFunction(Function &F);
661 static char ID; // Pass identification, replacement for typeid
662 GVNPRE() : FunctionPass((intptr_t)&ID) { }
666 SmallVector<Instruction*, 8> createdExpressions;
668 DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
669 DenseMap<BasicBlock*, ValueNumberedSet> anticipatedIn;
670 DenseMap<BasicBlock*, ValueNumberedSet> generatedPhis;
672 // This transformation requires dominator postdominator info
673 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
674 AU.setPreservesCFG();
675 AU.addRequiredID(BreakCriticalEdgesID);
676 AU.addRequired<UnifyFunctionExitNodes>();
677 AU.addRequired<DominatorTree>();
681 // FIXME: eliminate or document these better
682 void dump(ValueNumberedSet& s) const ;
683 void clean(ValueNumberedSet& set) ;
684 Value* find_leader(ValueNumberedSet& vals, uint32_t v) ;
685 Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) ;
686 void phi_translate_set(ValueNumberedSet& anticIn, BasicBlock* pred,
687 BasicBlock* succ, ValueNumberedSet& out) ;
689 void topo_sort(ValueNumberedSet& set,
690 SmallVector<Value*, 8>& vec) ;
695 void val_insert(ValueNumberedSet& s, Value* v) ;
696 void val_replace(ValueNumberedSet& s, Value* v) ;
697 bool dependsOnInvoke(Value* V) ;
698 void buildsets_availout(BasicBlock::iterator I,
699 ValueNumberedSet& currAvail,
700 ValueNumberedSet& currPhis,
701 ValueNumberedSet& currExps,
702 SmallPtrSet<Value*, 16>& currTemps);
703 bool buildsets_anticout(BasicBlock* BB,
704 ValueNumberedSet& anticOut,
705 SmallPtrSet<BasicBlock*, 8>& visited);
706 unsigned buildsets_anticin(BasicBlock* BB,
707 ValueNumberedSet& anticOut,
708 ValueNumberedSet& currExps,
709 SmallPtrSet<Value*, 16>& currTemps,
710 SmallPtrSet<BasicBlock*, 8>& visited);
711 void buildsets(Function& F) ;
713 void insertion_pre(Value* e, BasicBlock* BB,
714 DenseMap<BasicBlock*, Value*>& avail,
715 std::map<BasicBlock*,ValueNumberedSet>& new_set);
716 unsigned insertion_mergepoint(SmallVector<Value*, 8>& workList,
717 df_iterator<DomTreeNode*>& D,
718 std::map<BasicBlock*, ValueNumberedSet>& new_set);
719 bool insertion(Function& F) ;
727 // createGVNPREPass - The public interface to this file...
728 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
730 static RegisterPass<GVNPRE> X("gvnpre",
731 "Global Value Numbering/Partial Redundancy Elimination");
734 STATISTIC(NumInsertedVals, "Number of values inserted");
735 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
736 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
738 /// find_leader - Given a set and a value number, return the first
739 /// element of the set with that value number, or 0 if no such element
741 Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) {
745 for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
747 if (v == VN.lookup(*I))
750 assert(0 && "No leader found, but present bit is set?");
754 /// val_insert - Insert a value into a set only if there is not a value
755 /// with the same value number already in the set
756 void GVNPRE::val_insert(ValueNumberedSet& s, Value* v) {
757 uint32_t num = VN.lookup(v);
762 /// val_replace - Insert a value into a set, replacing any values already in
763 /// the set that have the same value number
764 void GVNPRE::val_replace(ValueNumberedSet& s, Value* v) {
765 if (s.count(v)) return;
767 uint32_t num = VN.lookup(v);
768 Value* leader = find_leader(s, num);
775 /// phi_translate - Given a value, its parent block, and a predecessor of its
776 /// parent, translate the value into legal for the predecessor block. This
777 /// means translating its operands (and recursively, their operands) through
778 /// any phi nodes in the parent into values available in the predecessor
779 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
784 if (CastInst* U = dyn_cast<CastInst>(V)) {
786 if (isa<Instruction>(U->getOperand(0)))
787 newOp1 = phi_translate(U->getOperand(0), pred, succ);
789 newOp1 = U->getOperand(0);
794 if (newOp1 != U->getOperand(0)) {
795 Instruction* newVal = 0;
796 if (CastInst* C = dyn_cast<CastInst>(U))
797 newVal = CastInst::create(C->getOpcode(),
798 newOp1, C->getType(),
799 C->getName()+".expr");
801 uint32_t v = VN.lookup_or_add(newVal);
803 Value* leader = find_leader(availableOut[pred], v);
805 createdExpressions.push_back(newVal);
815 } if (isa<BinaryOperator>(V) || isa<CmpInst>(V) ||
816 isa<ExtractElementInst>(V)) {
817 User* U = cast<User>(V);
820 if (isa<Instruction>(U->getOperand(0)))
821 newOp1 = phi_translate(U->getOperand(0), pred, succ);
823 newOp1 = U->getOperand(0);
829 if (isa<Instruction>(U->getOperand(1)))
830 newOp2 = phi_translate(U->getOperand(1), pred, succ);
832 newOp2 = U->getOperand(1);
837 if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
838 Instruction* newVal = 0;
839 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
840 newVal = BinaryOperator::create(BO->getOpcode(),
842 BO->getName()+".expr");
843 else if (CmpInst* C = dyn_cast<CmpInst>(U))
844 newVal = CmpInst::create(C->getOpcode(),
847 C->getName()+".expr");
848 else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
849 newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
851 uint32_t v = VN.lookup_or_add(newVal);
853 Value* leader = find_leader(availableOut[pred], v);
855 createdExpressions.push_back(newVal);
864 // Ternary Operations
865 } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
866 isa<SelectInst>(V)) {
867 User* U = cast<User>(V);
870 if (isa<Instruction>(U->getOperand(0)))
871 newOp1 = phi_translate(U->getOperand(0), pred, succ);
873 newOp1 = U->getOperand(0);
879 if (isa<Instruction>(U->getOperand(1)))
880 newOp2 = phi_translate(U->getOperand(1), pred, succ);
882 newOp2 = U->getOperand(1);
888 if (isa<Instruction>(U->getOperand(2)))
889 newOp3 = phi_translate(U->getOperand(2), pred, succ);
891 newOp3 = U->getOperand(2);
896 if (newOp1 != U->getOperand(0) ||
897 newOp2 != U->getOperand(1) ||
898 newOp3 != U->getOperand(2)) {
899 Instruction* newVal = 0;
900 if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
901 newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
902 S->getName()+".expr");
903 else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
904 newVal = new InsertElementInst(newOp1, newOp2, newOp3,
905 I->getName()+".expr");
906 else if (SelectInst* I = dyn_cast<SelectInst>(U))
907 newVal = new SelectInst(newOp1, newOp2, newOp3, I->getName()+".expr");
909 uint32_t v = VN.lookup_or_add(newVal);
911 Value* leader = find_leader(availableOut[pred], v);
913 createdExpressions.push_back(newVal);
923 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
925 if (isa<Instruction>(U->getPointerOperand()))
926 newOp1 = phi_translate(U->getPointerOperand(), pred, succ);
928 newOp1 = U->getPointerOperand();
933 bool changed_idx = false;
934 SmallVector<Value*, 4> newIdx;
935 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
937 if (isa<Instruction>(*I)) {
938 Value* newVal = phi_translate(*I, pred, succ);
939 newIdx.push_back(newVal);
943 newIdx.push_back(*I);
946 if (newOp1 != U->getPointerOperand() || changed_idx) {
947 Instruction* newVal = new GetElementPtrInst(newOp1,
948 &newIdx[0], newIdx.size(),
949 U->getName()+".expr");
951 uint32_t v = VN.lookup_or_add(newVal);
953 Value* leader = find_leader(availableOut[pred], v);
955 createdExpressions.push_back(newVal);
965 } else if (PHINode* P = dyn_cast<PHINode>(V)) {
966 if (P->getParent() == succ)
967 return P->getIncomingValueForBlock(pred);
973 /// phi_translate_set - Perform phi translation on every element of a set
974 void GVNPRE::phi_translate_set(ValueNumberedSet& anticIn,
975 BasicBlock* pred, BasicBlock* succ,
976 ValueNumberedSet& out) {
977 for (ValueNumberedSet::iterator I = anticIn.begin(),
978 E = anticIn.end(); I != E; ++I) {
979 Value* V = phi_translate(*I, pred, succ);
980 if (V != 0 && !out.test(VN.lookup_or_add(V))) {
982 out.set(VN.lookup(V));
987 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of
988 /// whose inputs is an invoke instruction. If this is true, we cannot safely
989 /// PRE the instruction or anything that depends on it.
990 bool GVNPRE::dependsOnInvoke(Value* V) {
991 if (PHINode* p = dyn_cast<PHINode>(V)) {
992 for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I)
993 if (isa<InvokeInst>(*I))
1001 /// clean - Remove all non-opaque values from the set whose operands are not
1002 /// themselves in the set, as well as all values that depend on invokes (see
1004 void GVNPRE::clean(ValueNumberedSet& set) {
1005 SmallVector<Value*, 8> worklist;
1006 worklist.reserve(set.size());
1007 topo_sort(set, worklist);
1009 for (unsigned i = 0; i < worklist.size(); ++i) {
1010 Value* v = worklist[i];
1013 if (CastInst* U = dyn_cast<CastInst>(v)) {
1014 bool lhsValid = !isa<Instruction>(U->getOperand(0));
1015 lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1017 lhsValid = !dependsOnInvoke(U->getOperand(0));
1021 set.reset(VN.lookup(U));
1024 // Handle binary ops
1025 } else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
1026 isa<ExtractElementInst>(v)) {
1027 User* U = cast<User>(v);
1029 bool lhsValid = !isa<Instruction>(U->getOperand(0));
1030 lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1032 lhsValid = !dependsOnInvoke(U->getOperand(0));
1034 bool rhsValid = !isa<Instruction>(U->getOperand(1));
1035 rhsValid |= set.test(VN.lookup(U->getOperand(1)));
1037 rhsValid = !dependsOnInvoke(U->getOperand(1));
1039 if (!lhsValid || !rhsValid) {
1041 set.reset(VN.lookup(U));
1044 // Handle ternary ops
1045 } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
1046 isa<SelectInst>(v)) {
1047 User* U = cast<User>(v);
1049 bool lhsValid = !isa<Instruction>(U->getOperand(0));
1050 lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1052 lhsValid = !dependsOnInvoke(U->getOperand(0));
1054 bool rhsValid = !isa<Instruction>(U->getOperand(1));
1055 rhsValid |= set.test(VN.lookup(U->getOperand(1)));
1057 rhsValid = !dependsOnInvoke(U->getOperand(1));
1059 bool thirdValid = !isa<Instruction>(U->getOperand(2));
1060 thirdValid |= set.test(VN.lookup(U->getOperand(2)));
1062 thirdValid = !dependsOnInvoke(U->getOperand(2));
1064 if (!lhsValid || !rhsValid || !thirdValid) {
1066 set.reset(VN.lookup(U));
1069 // Handle varargs ops
1070 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) {
1071 bool ptrValid = !isa<Instruction>(U->getPointerOperand());
1072 ptrValid |= set.test(VN.lookup(U->getPointerOperand()));
1074 ptrValid = !dependsOnInvoke(U->getPointerOperand());
1076 bool varValid = true;
1077 for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
1080 varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I));
1081 varValid &= !dependsOnInvoke(*I);
1084 if (!ptrValid || !varValid) {
1086 set.reset(VN.lookup(U));
1092 /// topo_sort - Given a set of values, sort them by topological
1093 /// order into the provided vector.
1094 void GVNPRE::topo_sort(ValueNumberedSet& set, SmallVector<Value*, 8>& vec) {
1095 SmallPtrSet<Value*, 16> visited;
1096 SmallVector<Value*, 8> stack;
1097 for (ValueNumberedSet::iterator I = set.begin(), E = set.end();
1099 if (visited.count(*I) == 0)
1100 stack.push_back(*I);
1102 while (!stack.empty()) {
1103 Value* e = stack.back();
1106 if (CastInst* U = dyn_cast<CastInst>(e)) {
1107 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1109 if (l != 0 && isa<Instruction>(l) &&
1110 visited.count(l) == 0)
1118 // Handle binary ops
1119 } else if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1120 isa<ExtractElementInst>(e)) {
1121 User* U = cast<User>(e);
1122 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1123 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1125 if (l != 0 && isa<Instruction>(l) &&
1126 visited.count(l) == 0)
1128 else if (r != 0 && isa<Instruction>(r) &&
1129 visited.count(r) == 0)
1137 // Handle ternary ops
1138 } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) ||
1139 isa<SelectInst>(e)) {
1140 User* U = cast<User>(e);
1141 Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1142 Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1143 Value* m = find_leader(set, VN.lookup(U->getOperand(2)));
1145 if (l != 0 && isa<Instruction>(l) &&
1146 visited.count(l) == 0)
1148 else if (r != 0 && isa<Instruction>(r) &&
1149 visited.count(r) == 0)
1151 else if (m != 0 && isa<Instruction>(m) &&
1152 visited.count(m) == 0)
1160 // Handle vararg ops
1161 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) {
1162 Value* p = find_leader(set, VN.lookup(U->getPointerOperand()));
1164 if (p != 0 && isa<Instruction>(p) &&
1165 visited.count(p) == 0)
1168 bool push_va = false;
1169 for (GetElementPtrInst::op_iterator I = U->idx_begin(),
1170 E = U->idx_end(); I != E; ++I) {
1171 Value * v = find_leader(set, VN.lookup(*I));
1172 if (v != 0 && isa<Instruction>(v) && visited.count(v) == 0) {
1185 // Handle opaque ops
1197 /// dump - Dump a set of values to standard error
1198 void GVNPRE::dump(ValueNumberedSet& s) const {
1200 for (ValueNumberedSet::iterator I = s.begin(), E = s.end();
1202 DOUT << "" << VN.lookup(*I) << ": ";
1203 DEBUG((*I)->dump());
1208 /// elimination - Phase 3 of the main algorithm. Perform full redundancy
1209 /// elimination by walking the dominator tree and removing any instruction that
1210 /// is dominated by another instruction with the same value number.
1211 bool GVNPRE::elimination() {
1212 bool changed_function = false;
1214 SmallVector<std::pair<Instruction*, Value*>, 8> replace;
1215 SmallVector<Instruction*, 8> erase;
1217 DominatorTree& DT = getAnalysis<DominatorTree>();
1219 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1220 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1221 BasicBlock* BB = DI->getBlock();
1223 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1226 if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) ||
1227 isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) ||
1228 isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) ||
1229 isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) {
1231 if (availableOut[BB].test(VN.lookup(BI)) &&
1232 !availableOut[BB].count(BI)) {
1233 Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
1234 if (Instruction* Instr = dyn_cast<Instruction>(leader))
1235 if (Instr->getParent() != 0 && Instr != BI) {
1236 replace.push_back(std::make_pair(BI, leader));
1237 erase.push_back(BI);
1245 while (!replace.empty()) {
1246 std::pair<Instruction*, Value*> rep = replace.back();
1248 rep.first->replaceAllUsesWith(rep.second);
1249 changed_function = true;
1252 for (SmallVector<Instruction*, 8>::iterator I = erase.begin(),
1253 E = erase.end(); I != E; ++I)
1254 (*I)->eraseFromParent();
1256 return changed_function;
1259 /// cleanup - Delete any extraneous values that were created to represent
1260 /// expressions without leaders.
1261 void GVNPRE::cleanup() {
1262 while (!createdExpressions.empty()) {
1263 Instruction* I = createdExpressions.back();
1264 createdExpressions.pop_back();
1270 /// buildsets_availout - When calculating availability, handle an instruction
1271 /// by inserting it into the appropriate sets
1272 void GVNPRE::buildsets_availout(BasicBlock::iterator I,
1273 ValueNumberedSet& currAvail,
1274 ValueNumberedSet& currPhis,
1275 ValueNumberedSet& currExps,
1276 SmallPtrSet<Value*, 16>& currTemps) {
1278 if (PHINode* p = dyn_cast<PHINode>(I)) {
1279 unsigned num = VN.lookup_or_add(p);
1285 } else if (CastInst* U = dyn_cast<CastInst>(I)) {
1286 Value* leftValue = U->getOperand(0);
1288 unsigned num = VN.lookup_or_add(U);
1290 if (isa<Instruction>(leftValue))
1291 if (!currExps.test(VN.lookup(leftValue))) {
1292 currExps.insert(leftValue);
1293 currExps.set(VN.lookup(leftValue));
1296 if (!currExps.test(num)) {
1301 // Handle binary ops
1302 } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
1303 isa<ExtractElementInst>(I)) {
1304 User* U = cast<User>(I);
1305 Value* leftValue = U->getOperand(0);
1306 Value* rightValue = U->getOperand(1);
1308 unsigned num = VN.lookup_or_add(U);
1310 if (isa<Instruction>(leftValue))
1311 if (!currExps.test(VN.lookup(leftValue))) {
1312 currExps.insert(leftValue);
1313 currExps.set(VN.lookup(leftValue));
1316 if (isa<Instruction>(rightValue))
1317 if (!currExps.test(VN.lookup(rightValue))) {
1318 currExps.insert(rightValue);
1319 currExps.set(VN.lookup(rightValue));
1322 if (!currExps.test(num)) {
1327 // Handle ternary ops
1328 } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) ||
1329 isa<SelectInst>(I)) {
1330 User* U = cast<User>(I);
1331 Value* leftValue = U->getOperand(0);
1332 Value* rightValue = U->getOperand(1);
1333 Value* thirdValue = U->getOperand(2);
1335 VN.lookup_or_add(U);
1337 unsigned num = VN.lookup_or_add(U);
1339 if (isa<Instruction>(leftValue))
1340 if (!currExps.test(VN.lookup(leftValue))) {
1341 currExps.insert(leftValue);
1342 currExps.set(VN.lookup(leftValue));
1344 if (isa<Instruction>(rightValue))
1345 if (!currExps.test(VN.lookup(rightValue))) {
1346 currExps.insert(rightValue);
1347 currExps.set(VN.lookup(rightValue));
1349 if (isa<Instruction>(thirdValue))
1350 if (!currExps.test(VN.lookup(thirdValue))) {
1351 currExps.insert(thirdValue);
1352 currExps.set(VN.lookup(thirdValue));
1355 if (!currExps.test(num)) {
1360 // Handle vararg ops
1361 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) {
1362 Value* ptrValue = U->getPointerOperand();
1364 VN.lookup_or_add(U);
1366 unsigned num = VN.lookup_or_add(U);
1368 if (isa<Instruction>(ptrValue))
1369 if (!currExps.test(VN.lookup(ptrValue))) {
1370 currExps.insert(ptrValue);
1371 currExps.set(VN.lookup(ptrValue));
1374 for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end();
1376 if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) {
1377 currExps.insert(*OI);
1378 currExps.set(VN.lookup(*OI));
1381 if (!currExps.test(VN.lookup(U))) {
1386 // Handle opaque ops
1387 } else if (!I->isTerminator()){
1388 VN.lookup_or_add(I);
1390 currTemps.insert(I);
1393 if (!I->isTerminator())
1394 if (!currAvail.test(VN.lookup(I))) {
1395 currAvail.insert(I);
1396 currAvail.set(VN.lookup(I));
1400 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
1401 /// set as a function of the ANTIC_IN set of the block's predecessors
1402 bool GVNPRE::buildsets_anticout(BasicBlock* BB,
1403 ValueNumberedSet& anticOut,
1404 SmallPtrSet<BasicBlock*, 8>& visited) {
1405 if (BB->getTerminator()->getNumSuccessors() == 1) {
1406 if (BB->getTerminator()->getSuccessor(0) != BB &&
1407 visited.count(BB->getTerminator()->getSuccessor(0)) == 0) {
1411 phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
1412 BB, BB->getTerminator()->getSuccessor(0), anticOut);
1414 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
1415 BasicBlock* first = BB->getTerminator()->getSuccessor(0);
1416 for (ValueNumberedSet::iterator I = anticipatedIn[first].begin(),
1417 E = anticipatedIn[first].end(); I != E; ++I) {
1418 anticOut.insert(*I);
1419 anticOut.set(VN.lookup(*I));
1422 for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
1423 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
1424 ValueNumberedSet& succAnticIn = anticipatedIn[currSucc];
1426 SmallVector<Value*, 16> temp;
1428 for (ValueNumberedSet::iterator I = anticOut.begin(),
1429 E = anticOut.end(); I != E; ++I)
1430 if (!succAnticIn.test(VN.lookup(*I)))
1433 for (SmallVector<Value*, 16>::iterator I = temp.begin(), E = temp.end();
1436 anticOut.reset(VN.lookup(*I));
1444 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
1445 /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
1446 /// sets populated in buildsets_availout
1447 unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
1448 ValueNumberedSet& anticOut,
1449 ValueNumberedSet& currExps,
1450 SmallPtrSet<Value*, 16>& currTemps,
1451 SmallPtrSet<BasicBlock*, 8>& visited) {
1452 ValueNumberedSet& anticIn = anticipatedIn[BB];
1453 unsigned old = anticIn.size();
1455 bool defer = buildsets_anticout(BB, anticOut, visited);
1461 for (ValueNumberedSet::iterator I = anticOut.begin(),
1462 E = anticOut.end(); I != E; ++I) {
1464 anticIn.set(VN.lookup(*I));
1466 for (ValueNumberedSet::iterator I = currExps.begin(),
1467 E = currExps.end(); I != E; ++I) {
1468 if (!anticIn.test(VN.lookup(*I))) {
1470 anticIn.set(VN.lookup(*I));
1474 for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
1475 E = currTemps.end(); I != E; ++I) {
1477 anticIn.reset(VN.lookup(*I));
1483 if (old != anticIn.size())
1489 /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
1490 /// and the ANTIC_IN sets.
1491 void GVNPRE::buildsets(Function& F) {
1492 DenseMap<BasicBlock*, ValueNumberedSet> generatedExpressions;
1493 DenseMap<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
1495 DominatorTree &DT = getAnalysis<DominatorTree>();
1497 // Phase 1, Part 1: calculate AVAIL_OUT
1499 // Top-down walk of the dominator tree
1500 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1501 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1503 // Get the sets to update for this block
1504 ValueNumberedSet& currExps = generatedExpressions[DI->getBlock()];
1505 ValueNumberedSet& currPhis = generatedPhis[DI->getBlock()];
1506 SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
1507 ValueNumberedSet& currAvail = availableOut[DI->getBlock()];
1509 BasicBlock* BB = DI->getBlock();
1511 // A block inherits AVAIL_OUT from its dominator
1512 if (DI->getIDom() != 0)
1513 currAvail = availableOut[DI->getIDom()->getBlock()];
1515 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1517 buildsets_availout(BI, currAvail, currPhis, currExps,
1522 // Phase 1, Part 2: calculate ANTIC_IN
1524 SmallPtrSet<BasicBlock*, 8> visited;
1525 SmallPtrSet<BasicBlock*, 4> block_changed;
1526 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
1527 block_changed.insert(FI);
1529 bool changed = true;
1530 unsigned iterations = 0;
1534 ValueNumberedSet anticOut;
1536 // Postorder walk of the CFG
1537 for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
1538 BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) {
1539 BasicBlock* BB = *BBI;
1541 if (block_changed.count(BB) != 0) {
1542 unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
1543 generatedTemporaries[BB], visited);
1552 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1554 block_changed.insert(*PI);
1557 block_changed.erase(BB);
1559 changed |= (ret == 2);
1568 /// insertion_pre - When a partial redundancy has been identified, eliminate it
1569 /// by inserting appropriate values into the predecessors and a phi node in
1571 void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
1572 DenseMap<BasicBlock*, Value*>& avail,
1573 std::map<BasicBlock*, ValueNumberedSet>& new_sets) {
1574 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1575 Value* e2 = avail[*PI];
1576 if (!availableOut[*PI].test(VN.lookup(e2))) {
1577 User* U = cast<User>(e2);
1580 if (isa<BinaryOperator>(U->getOperand(0)) ||
1581 isa<CmpInst>(U->getOperand(0)) ||
1582 isa<ShuffleVectorInst>(U->getOperand(0)) ||
1583 isa<ExtractElementInst>(U->getOperand(0)) ||
1584 isa<InsertElementInst>(U->getOperand(0)) ||
1585 isa<SelectInst>(U->getOperand(0)) ||
1586 isa<CastInst>(U->getOperand(0)) ||
1587 isa<GetElementPtrInst>(U->getOperand(0)))
1588 s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
1590 s1 = U->getOperand(0);
1594 if (isa<BinaryOperator>(U) ||
1596 isa<ShuffleVectorInst>(U) ||
1597 isa<ExtractElementInst>(U) ||
1598 isa<InsertElementInst>(U) ||
1600 if (isa<BinaryOperator>(U->getOperand(1)) ||
1601 isa<CmpInst>(U->getOperand(1)) ||
1602 isa<ShuffleVectorInst>(U->getOperand(1)) ||
1603 isa<ExtractElementInst>(U->getOperand(1)) ||
1604 isa<InsertElementInst>(U->getOperand(1)) ||
1605 isa<SelectInst>(U->getOperand(1)) ||
1606 isa<CastInst>(U->getOperand(1)) ||
1607 isa<GetElementPtrInst>(U->getOperand(1))) {
1608 s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
1610 s2 = U->getOperand(1);
1613 // Ternary Operators
1615 if (isa<ShuffleVectorInst>(U) ||
1616 isa<InsertElementInst>(U) ||
1618 if (isa<BinaryOperator>(U->getOperand(2)) ||
1619 isa<CmpInst>(U->getOperand(2)) ||
1620 isa<ShuffleVectorInst>(U->getOperand(2)) ||
1621 isa<ExtractElementInst>(U->getOperand(2)) ||
1622 isa<InsertElementInst>(U->getOperand(2)) ||
1623 isa<SelectInst>(U->getOperand(2)) ||
1624 isa<CastInst>(U->getOperand(2)) ||
1625 isa<GetElementPtrInst>(U->getOperand(2))) {
1626 s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2)));
1628 s3 = U->getOperand(2);
1632 SmallVector<Value*, 4> sVarargs;
1633 if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) {
1634 for (GetElementPtrInst::op_iterator OI = G->idx_begin(),
1635 OE = G->idx_end(); OI != OE; ++OI) {
1636 if (isa<BinaryOperator>(*OI) ||
1637 isa<CmpInst>(*OI) ||
1638 isa<ShuffleVectorInst>(*OI) ||
1639 isa<ExtractElementInst>(*OI) ||
1640 isa<InsertElementInst>(*OI) ||
1641 isa<SelectInst>(*OI) ||
1642 isa<CastInst>(*OI) ||
1643 isa<GetElementPtrInst>(*OI)) {
1644 sVarargs.push_back(find_leader(availableOut[*PI],
1647 sVarargs.push_back(*OI);
1653 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
1654 newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
1655 BO->getName()+".gvnpre",
1656 (*PI)->getTerminator());
1657 else if (CmpInst* C = dyn_cast<CmpInst>(U))
1658 newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
1659 C->getName()+".gvnpre",
1660 (*PI)->getTerminator());
1661 else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
1662 newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
1663 (*PI)->getTerminator());
1664 else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
1665 newVal = new InsertElementInst(s1, s2, s3, S->getName()+".gvnpre",
1666 (*PI)->getTerminator());
1667 else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
1668 newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
1669 (*PI)->getTerminator());
1670 else if (SelectInst* S = dyn_cast<SelectInst>(U))
1671 newVal = new SelectInst(s1, s2, s3, S->getName()+".gvnpre",
1672 (*PI)->getTerminator());
1673 else if (CastInst* C = dyn_cast<CastInst>(U))
1674 newVal = CastInst::create(C->getOpcode(), s1, C->getType(),
1675 C->getName()+".gvnpre",
1676 (*PI)->getTerminator());
1677 else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U))
1678 newVal = new GetElementPtrInst(s1, &sVarargs[0], sVarargs.size(),
1679 G->getName()+".gvnpre",
1680 (*PI)->getTerminator());
1683 VN.add(newVal, VN.lookup(U));
1685 ValueNumberedSet& predAvail = availableOut[*PI];
1686 val_replace(predAvail, newVal);
1687 val_replace(new_sets[*PI], newVal);
1688 predAvail.set(VN.lookup(newVal));
1690 DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1691 if (av != avail.end())
1693 avail.insert(std::make_pair(*PI, newVal));
1701 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1703 p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
1705 p->addIncoming(avail[*PI], *PI);
1708 VN.add(p, VN.lookup(e));
1709 val_replace(availableOut[BB], p);
1710 availableOut[BB].set(VN.lookup(e));
1711 generatedPhis[BB].insert(p);
1712 generatedPhis[BB].set(VN.lookup(e));
1713 new_sets[BB].insert(p);
1714 new_sets[BB].set(VN.lookup(e));
1719 /// insertion_mergepoint - When walking the dom tree, check at each merge
1720 /// block for the possibility of a partial redundancy. If present, eliminate it
1721 unsigned GVNPRE::insertion_mergepoint(SmallVector<Value*, 8>& workList,
1722 df_iterator<DomTreeNode*>& D,
1723 std::map<BasicBlock*, ValueNumberedSet >& new_sets) {
1724 bool changed_function = false;
1725 bool new_stuff = false;
1727 BasicBlock* BB = D->getBlock();
1728 for (unsigned i = 0; i < workList.size(); ++i) {
1729 Value* e = workList[i];
1731 if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1732 isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
1733 isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) ||
1734 isa<GetElementPtrInst>(e)) {
1735 if (availableOut[D->getIDom()->getBlock()].test(VN.lookup(e)))
1738 DenseMap<BasicBlock*, Value*> avail;
1739 bool by_some = false;
1740 bool all_same = true;
1741 Value * first_s = 0;
1743 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
1745 Value *e2 = phi_translate(e, *PI, BB);
1746 Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
1749 DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1750 if (av != avail.end())
1752 avail.insert(std::make_pair(*PI, e2));
1755 DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1756 if (av != avail.end())
1758 avail.insert(std::make_pair(*PI, e3));
1763 else if (first_s != e3)
1768 if (by_some && !all_same &&
1769 !generatedPhis[BB].test(VN.lookup(e))) {
1770 insertion_pre(e, BB, avail, new_sets);
1772 changed_function = true;
1778 unsigned retval = 0;
1779 if (changed_function)
1787 /// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for
1788 /// merge points. When one is found, check for a partial redundancy. If one is
1789 /// present, eliminate it. Repeat this walk until no changes are made.
1790 bool GVNPRE::insertion(Function& F) {
1791 bool changed_function = false;
1793 DominatorTree &DT = getAnalysis<DominatorTree>();
1795 std::map<BasicBlock*, ValueNumberedSet> new_sets;
1796 bool new_stuff = true;
1799 for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1800 E = df_end(DT.getRootNode()); DI != E; ++DI) {
1801 BasicBlock* BB = DI->getBlock();
1806 ValueNumberedSet& availOut = availableOut[BB];
1807 ValueNumberedSet& anticIn = anticipatedIn[BB];
1809 // Replace leaders with leaders inherited from dominator
1810 if (DI->getIDom() != 0) {
1811 ValueNumberedSet& dom_set = new_sets[DI->getIDom()->getBlock()];
1812 for (ValueNumberedSet::iterator I = dom_set.begin(),
1813 E = dom_set.end(); I != E; ++I) {
1814 val_replace(new_sets[BB], *I);
1815 val_replace(availOut, *I);
1819 // If there is more than one predecessor...
1820 if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
1821 SmallVector<Value*, 8> workList;
1822 workList.reserve(anticIn.size());
1823 topo_sort(anticIn, workList);
1825 unsigned result = insertion_mergepoint(workList, DI, new_sets);
1827 changed_function = true;
1834 return changed_function;
1837 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1840 bool GVNPRE::runOnFunction(Function &F) {
1841 // Clean out global sets from any previous functions
1843 createdExpressions.clear();
1844 availableOut.clear();
1845 anticipatedIn.clear();
1846 generatedPhis.clear();
1848 bool changed_function = false;
1850 // Phase 1: BuildSets
1851 // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1855 // This phase inserts values to make partially redundant values
1857 changed_function |= insertion(F);
1859 // Phase 3: Eliminate
1860 // This phase performs trivial full redundancy elimination
1861 changed_function |= elimination();
1864 // This phase cleans up values that were created solely
1865 // as leaders for expressions
1868 return changed_function;