Update comments and documentation to reflect that GCSE and ValueNumbering are
[oota-llvm.git] / lib / Transforms / Scalar / GVNPRE.cpp
1 //===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
18 //
19 // Note that this pass does the value numbering itself, it does not use the
20 // ValueNumbering analysis passes.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #define DEBUG_TYPE "gvnpre"
25 #include "llvm/Value.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Function.h"
29 #include "llvm/DerivedTypes.h"
30 #include "llvm/Analysis/Dominators.h"
31 #include "llvm/ADT/BitVector.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/DepthFirstIterator.h"
34 #include "llvm/ADT/PostOrderIterator.h"
35 #include "llvm/ADT/SmallPtrSet.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
39 #include "llvm/Support/CFG.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/Debug.h"
42 #include <algorithm>
43 #include <deque>
44 #include <map>
45 using namespace llvm;
46
47 //===----------------------------------------------------------------------===//
48 //                         ValueTable Class
49 //===----------------------------------------------------------------------===//
50
51 namespace {
52
53 /// This class holds the mapping between values and value numbers.  It is used
54 /// as an efficient mechanism to determine the expression-wise equivalence of
55 /// two values.
56
57 struct Expression {
58   enum ExpressionOpcode { ADD, SUB, MUL, UDIV, SDIV, FDIV, UREM, SREM, 
59                           FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ, 
60                           ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE, 
61                           ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ, 
62                           FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE, 
63                           FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE, 
64                           FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
65                           SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
66                           FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT, 
67                           PTRTOINT, INTTOPTR, BITCAST, GEP, EMPTY,
68                           TOMBSTONE };
69
70   ExpressionOpcode opcode;
71   const Type* type;
72   uint32_t firstVN;
73   uint32_t secondVN;
74   uint32_t thirdVN;
75   SmallVector<uint32_t, 4> varargs;
76   
77   Expression() { }
78   explicit Expression(ExpressionOpcode o) : opcode(o) { }
79   
80   bool operator==(const Expression &other) const {
81     if (opcode != other.opcode)
82       return false;
83     else if (opcode == EMPTY || opcode == TOMBSTONE)
84       return true;
85     else if (type != other.type)
86       return false;
87     else if (firstVN != other.firstVN)
88       return false;
89     else if (secondVN != other.secondVN)
90       return false;
91     else if (thirdVN != other.thirdVN)
92       return false;
93     else {
94       if (varargs.size() != other.varargs.size())
95         return false;
96       
97       for (size_t i = 0; i < varargs.size(); ++i)
98         if (varargs[i] != other.varargs[i])
99           return false;
100     
101       return true;
102     }
103   }
104   
105   bool operator!=(const Expression &other) const {
106     if (opcode != other.opcode)
107       return true;
108     else if (opcode == EMPTY || opcode == TOMBSTONE)
109       return false;
110     else if (type != other.type)
111       return true;
112     else if (firstVN != other.firstVN)
113       return true;
114     else if (secondVN != other.secondVN)
115       return true;
116     else if (thirdVN != other.thirdVN)
117       return true;
118     else {
119       if (varargs.size() != other.varargs.size())
120         return true;
121       
122       for (size_t i = 0; i < varargs.size(); ++i)
123         if (varargs[i] != other.varargs[i])
124           return true;
125     
126       return false;
127     }
128   }
129 };
130
131 }
132
133 namespace {
134   class VISIBILITY_HIDDEN ValueTable {
135     private:
136       DenseMap<Value*, uint32_t> valueNumbering;
137       DenseMap<Expression, uint32_t> expressionNumbering;
138   
139       uint32_t nextValueNumber;
140     
141       Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
142       Expression::ExpressionOpcode getOpcode(CmpInst* C);
143       Expression::ExpressionOpcode getOpcode(CastInst* C);
144       Expression create_expression(BinaryOperator* BO);
145       Expression create_expression(CmpInst* C);
146       Expression create_expression(ShuffleVectorInst* V);
147       Expression create_expression(ExtractElementInst* C);
148       Expression create_expression(InsertElementInst* V);
149       Expression create_expression(SelectInst* V);
150       Expression create_expression(CastInst* C);
151       Expression create_expression(GetElementPtrInst* G);
152     public:
153       ValueTable() { nextValueNumber = 1; }
154       uint32_t lookup_or_add(Value* V);
155       uint32_t lookup(Value* V) const;
156       void add(Value* V, uint32_t num);
157       void clear();
158       void erase(Value* v);
159       unsigned size();
160   };
161 }
162
163 namespace llvm {
164 template <> struct DenseMapInfo<Expression> {
165   static inline Expression getEmptyKey() {
166     return Expression(Expression::EMPTY);
167   }
168   
169   static inline Expression getTombstoneKey() {
170     return Expression(Expression::TOMBSTONE);
171   }
172   
173   static unsigned getHashValue(const Expression e) {
174     unsigned hash = e.opcode;
175     
176     hash = e.firstVN + hash * 37;
177     hash = e.secondVN + hash * 37;
178     hash = e.thirdVN + hash * 37;
179     
180     hash = ((unsigned)((uintptr_t)e.type >> 4) ^
181             (unsigned)((uintptr_t)e.type >> 9)) +
182            hash * 37;
183     
184     for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
185          E = e.varargs.end(); I != E; ++I)
186       hash = *I + hash * 37;
187     
188     return hash;
189   }
190   static bool isEqual(const Expression &LHS, const Expression &RHS) {
191     return LHS == RHS;
192   }
193   static bool isPod() { return true; }
194 };
195 }
196
197 //===----------------------------------------------------------------------===//
198 //                     ValueTable Internal Functions
199 //===----------------------------------------------------------------------===//
200 Expression::ExpressionOpcode 
201                              ValueTable::getOpcode(BinaryOperator* BO) {
202   switch(BO->getOpcode()) {
203     case Instruction::Add:
204       return Expression::ADD;
205     case Instruction::Sub:
206       return Expression::SUB;
207     case Instruction::Mul:
208       return Expression::MUL;
209     case Instruction::UDiv:
210       return Expression::UDIV;
211     case Instruction::SDiv:
212       return Expression::SDIV;
213     case Instruction::FDiv:
214       return Expression::FDIV;
215     case Instruction::URem:
216       return Expression::UREM;
217     case Instruction::SRem:
218       return Expression::SREM;
219     case Instruction::FRem:
220       return Expression::FREM;
221     case Instruction::Shl:
222       return Expression::SHL;
223     case Instruction::LShr:
224       return Expression::LSHR;
225     case Instruction::AShr:
226       return Expression::ASHR;
227     case Instruction::And:
228       return Expression::AND;
229     case Instruction::Or:
230       return Expression::OR;
231     case Instruction::Xor:
232       return Expression::XOR;
233     
234     // THIS SHOULD NEVER HAPPEN
235     default:
236       assert(0 && "Binary operator with unknown opcode?");
237       return Expression::ADD;
238   }
239 }
240
241 Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
242   if (C->getOpcode() == Instruction::ICmp) {
243     switch (C->getPredicate()) {
244       case ICmpInst::ICMP_EQ:
245         return Expression::ICMPEQ;
246       case ICmpInst::ICMP_NE:
247         return Expression::ICMPNE;
248       case ICmpInst::ICMP_UGT:
249         return Expression::ICMPUGT;
250       case ICmpInst::ICMP_UGE:
251         return Expression::ICMPUGE;
252       case ICmpInst::ICMP_ULT:
253         return Expression::ICMPULT;
254       case ICmpInst::ICMP_ULE:
255         return Expression::ICMPULE;
256       case ICmpInst::ICMP_SGT:
257         return Expression::ICMPSGT;
258       case ICmpInst::ICMP_SGE:
259         return Expression::ICMPSGE;
260       case ICmpInst::ICMP_SLT:
261         return Expression::ICMPSLT;
262       case ICmpInst::ICMP_SLE:
263         return Expression::ICMPSLE;
264       
265       // THIS SHOULD NEVER HAPPEN
266       default:
267         assert(0 && "Comparison with unknown predicate?");
268         return Expression::ICMPEQ;
269     }
270   } else {
271     switch (C->getPredicate()) {
272       case FCmpInst::FCMP_OEQ:
273         return Expression::FCMPOEQ;
274       case FCmpInst::FCMP_OGT:
275         return Expression::FCMPOGT;
276       case FCmpInst::FCMP_OGE:
277         return Expression::FCMPOGE;
278       case FCmpInst::FCMP_OLT:
279         return Expression::FCMPOLT;
280       case FCmpInst::FCMP_OLE:
281         return Expression::FCMPOLE;
282       case FCmpInst::FCMP_ONE:
283         return Expression::FCMPONE;
284       case FCmpInst::FCMP_ORD:
285         return Expression::FCMPORD;
286       case FCmpInst::FCMP_UNO:
287         return Expression::FCMPUNO;
288       case FCmpInst::FCMP_UEQ:
289         return Expression::FCMPUEQ;
290       case FCmpInst::FCMP_UGT:
291         return Expression::FCMPUGT;
292       case FCmpInst::FCMP_UGE:
293         return Expression::FCMPUGE;
294       case FCmpInst::FCMP_ULT:
295         return Expression::FCMPULT;
296       case FCmpInst::FCMP_ULE:
297         return Expression::FCMPULE;
298       case FCmpInst::FCMP_UNE:
299         return Expression::FCMPUNE;
300       
301       // THIS SHOULD NEVER HAPPEN
302       default:
303         assert(0 && "Comparison with unknown predicate?");
304         return Expression::FCMPOEQ;
305     }
306   }
307 }
308
309 Expression::ExpressionOpcode 
310                              ValueTable::getOpcode(CastInst* C) {
311   switch(C->getOpcode()) {
312     case Instruction::Trunc:
313       return Expression::TRUNC;
314     case Instruction::ZExt:
315       return Expression::ZEXT;
316     case Instruction::SExt:
317       return Expression::SEXT;
318     case Instruction::FPToUI:
319       return Expression::FPTOUI;
320     case Instruction::FPToSI:
321       return Expression::FPTOSI;
322     case Instruction::UIToFP:
323       return Expression::UITOFP;
324     case Instruction::SIToFP:
325       return Expression::SITOFP;
326     case Instruction::FPTrunc:
327       return Expression::FPTRUNC;
328     case Instruction::FPExt:
329       return Expression::FPEXT;
330     case Instruction::PtrToInt:
331       return Expression::PTRTOINT;
332     case Instruction::IntToPtr:
333       return Expression::INTTOPTR;
334     case Instruction::BitCast:
335       return Expression::BITCAST;
336     
337     // THIS SHOULD NEVER HAPPEN
338     default:
339       assert(0 && "Cast operator with unknown opcode?");
340       return Expression::BITCAST;
341   }
342 }
343
344 Expression ValueTable::create_expression(BinaryOperator* BO) {
345   Expression e;
346     
347   e.firstVN = lookup_or_add(BO->getOperand(0));
348   e.secondVN = lookup_or_add(BO->getOperand(1));
349   e.thirdVN = 0;
350   e.type = BO->getType();
351   e.opcode = getOpcode(BO);
352   
353   return e;
354 }
355
356 Expression ValueTable::create_expression(CmpInst* C) {
357   Expression e;
358     
359   e.firstVN = lookup_or_add(C->getOperand(0));
360   e.secondVN = lookup_or_add(C->getOperand(1));
361   e.thirdVN = 0;
362   e.type = C->getType();
363   e.opcode = getOpcode(C);
364   
365   return e;
366 }
367
368 Expression ValueTable::create_expression(CastInst* C) {
369   Expression e;
370     
371   e.firstVN = lookup_or_add(C->getOperand(0));
372   e.secondVN = 0;
373   e.thirdVN = 0;
374   e.type = C->getType();
375   e.opcode = getOpcode(C);
376   
377   return e;
378 }
379
380 Expression ValueTable::create_expression(ShuffleVectorInst* S) {
381   Expression e;
382     
383   e.firstVN = lookup_or_add(S->getOperand(0));
384   e.secondVN = lookup_or_add(S->getOperand(1));
385   e.thirdVN = lookup_or_add(S->getOperand(2));
386   e.type = S->getType();
387   e.opcode = Expression::SHUFFLE;
388   
389   return e;
390 }
391
392 Expression ValueTable::create_expression(ExtractElementInst* E) {
393   Expression e;
394     
395   e.firstVN = lookup_or_add(E->getOperand(0));
396   e.secondVN = lookup_or_add(E->getOperand(1));
397   e.thirdVN = 0;
398   e.type = E->getType();
399   e.opcode = Expression::EXTRACT;
400   
401   return e;
402 }
403
404 Expression ValueTable::create_expression(InsertElementInst* I) {
405   Expression e;
406     
407   e.firstVN = lookup_or_add(I->getOperand(0));
408   e.secondVN = lookup_or_add(I->getOperand(1));
409   e.thirdVN = lookup_or_add(I->getOperand(2));
410   e.type = I->getType();
411   e.opcode = Expression::INSERT;
412   
413   return e;
414 }
415
416 Expression ValueTable::create_expression(SelectInst* I) {
417   Expression e;
418     
419   e.firstVN = lookup_or_add(I->getCondition());
420   e.secondVN = lookup_or_add(I->getTrueValue());
421   e.thirdVN = lookup_or_add(I->getFalseValue());
422   e.type = I->getType();
423   e.opcode = Expression::SELECT;
424   
425   return e;
426 }
427
428 Expression ValueTable::create_expression(GetElementPtrInst* G) {
429   Expression e;
430     
431   e.firstVN = lookup_or_add(G->getPointerOperand());
432   e.secondVN = 0;
433   e.thirdVN = 0;
434   e.type = G->getType();
435   e.opcode = Expression::GEP;
436   
437   for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
438        I != E; ++I)
439     e.varargs.push_back(lookup_or_add(*I));
440   
441   return e;
442 }
443
444 //===----------------------------------------------------------------------===//
445 //                     ValueTable External Functions
446 //===----------------------------------------------------------------------===//
447
448 /// lookup_or_add - Returns the value number for the specified value, assigning
449 /// it a new number if it did not have one before.
450 uint32_t ValueTable::lookup_or_add(Value* V) {
451   DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
452   if (VI != valueNumbering.end())
453     return VI->second;
454   
455   
456   if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
457     Expression e = create_expression(BO);
458     
459     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
460     if (EI != expressionNumbering.end()) {
461       valueNumbering.insert(std::make_pair(V, EI->second));
462       return EI->second;
463     } else {
464       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
465       valueNumbering.insert(std::make_pair(V, nextValueNumber));
466       
467       return nextValueNumber++;
468     }
469   } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
470     Expression e = create_expression(C);
471     
472     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
473     if (EI != expressionNumbering.end()) {
474       valueNumbering.insert(std::make_pair(V, EI->second));
475       return EI->second;
476     } else {
477       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
478       valueNumbering.insert(std::make_pair(V, nextValueNumber));
479       
480       return nextValueNumber++;
481     }
482   } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
483     Expression e = create_expression(U);
484     
485     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
486     if (EI != expressionNumbering.end()) {
487       valueNumbering.insert(std::make_pair(V, EI->second));
488       return EI->second;
489     } else {
490       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
491       valueNumbering.insert(std::make_pair(V, nextValueNumber));
492       
493       return nextValueNumber++;
494     }
495   } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
496     Expression e = create_expression(U);
497     
498     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
499     if (EI != expressionNumbering.end()) {
500       valueNumbering.insert(std::make_pair(V, EI->second));
501       return EI->second;
502     } else {
503       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
504       valueNumbering.insert(std::make_pair(V, nextValueNumber));
505       
506       return nextValueNumber++;
507     }
508   } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
509     Expression e = create_expression(U);
510     
511     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
512     if (EI != expressionNumbering.end()) {
513       valueNumbering.insert(std::make_pair(V, EI->second));
514       return EI->second;
515     } else {
516       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
517       valueNumbering.insert(std::make_pair(V, nextValueNumber));
518       
519       return nextValueNumber++;
520     }
521   } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
522     Expression e = create_expression(U);
523     
524     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
525     if (EI != expressionNumbering.end()) {
526       valueNumbering.insert(std::make_pair(V, EI->second));
527       return EI->second;
528     } else {
529       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
530       valueNumbering.insert(std::make_pair(V, nextValueNumber));
531       
532       return nextValueNumber++;
533     }
534   } else if (CastInst* U = dyn_cast<CastInst>(V)) {
535     Expression e = create_expression(U);
536     
537     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
538     if (EI != expressionNumbering.end()) {
539       valueNumbering.insert(std::make_pair(V, EI->second));
540       return EI->second;
541     } else {
542       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
543       valueNumbering.insert(std::make_pair(V, nextValueNumber));
544       
545       return nextValueNumber++;
546     }
547   } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
548     Expression e = create_expression(U);
549     
550     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
551     if (EI != expressionNumbering.end()) {
552       valueNumbering.insert(std::make_pair(V, EI->second));
553       return EI->second;
554     } else {
555       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
556       valueNumbering.insert(std::make_pair(V, nextValueNumber));
557       
558       return nextValueNumber++;
559     }
560   } else {
561     valueNumbering.insert(std::make_pair(V, nextValueNumber));
562     return nextValueNumber++;
563   }
564 }
565
566 /// lookup - Returns the value number of the specified value. Fails if
567 /// the value has not yet been numbered.
568 uint32_t ValueTable::lookup(Value* V) const {
569   DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
570   if (VI != valueNumbering.end())
571     return VI->second;
572   else
573     assert(0 && "Value not numbered?");
574   
575   return 0;
576 }
577
578 /// add - Add the specified value with the given value number, removing
579 /// its old number, if any
580 void ValueTable::add(Value* V, uint32_t num) {
581   DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
582   if (VI != valueNumbering.end())
583     valueNumbering.erase(VI);
584   valueNumbering.insert(std::make_pair(V, num));
585 }
586
587 /// clear - Remove all entries from the ValueTable
588 void ValueTable::clear() {
589   valueNumbering.clear();
590   expressionNumbering.clear();
591   nextValueNumber = 1;
592 }
593
594 /// erase - Remove a value from the value numbering
595 void ValueTable::erase(Value* V) {
596   valueNumbering.erase(V);
597 }
598
599 /// size - Return the number of assigned value numbers
600 unsigned ValueTable::size() {
601   // NOTE: zero is never assigned
602   return nextValueNumber;
603 }
604
605 namespace {
606
607 //===----------------------------------------------------------------------===//
608 //                       ValueNumberedSet Class
609 //===----------------------------------------------------------------------===//
610
611 class ValueNumberedSet {
612   private:
613     SmallPtrSet<Value*, 8> contents;
614     BitVector numbers;
615   public:
616     ValueNumberedSet() { numbers.resize(1); }
617     ValueNumberedSet(const ValueNumberedSet& other) {
618       numbers = other.numbers;
619       contents = other.contents;
620     }
621     
622     typedef SmallPtrSet<Value*, 8>::iterator iterator;
623     
624     iterator begin() { return contents.begin(); }
625     iterator end() { return contents.end(); }
626     
627     bool insert(Value* v) { return contents.insert(v); }
628     void insert(iterator I, iterator E) { contents.insert(I, E); }
629     void erase(Value* v) { contents.erase(v); }
630     unsigned count(Value* v) { return contents.count(v); }
631     size_t size() { return contents.size(); }
632     
633     void set(unsigned i)  {
634       if (i >= numbers.size())
635         numbers.resize(i+1);
636       
637       numbers.set(i);
638     }
639     
640     void operator=(const ValueNumberedSet& other) {
641       contents = other.contents;
642       numbers = other.numbers;
643     }
644     
645     void reset(unsigned i)  {
646       if (i < numbers.size())
647         numbers.reset(i);
648     }
649     
650     bool test(unsigned i)  {
651       if (i >= numbers.size())
652         return false;
653       
654       return numbers.test(i);
655     }
656     
657     void clear() {
658       contents.clear();
659       numbers.clear();
660     }
661 };
662
663 }
664
665 //===----------------------------------------------------------------------===//
666 //                         GVNPRE Pass
667 //===----------------------------------------------------------------------===//
668
669 namespace {
670
671   class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
672     bool runOnFunction(Function &F);
673   public:
674     static char ID; // Pass identification, replacement for typeid
675     GVNPRE() : FunctionPass((intptr_t)&ID) { }
676
677   private:
678     ValueTable VN;
679     SmallVector<Instruction*, 8> createdExpressions;
680     
681     DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
682     DenseMap<BasicBlock*, ValueNumberedSet> anticipatedIn;
683     DenseMap<BasicBlock*, ValueNumberedSet> generatedPhis;
684     
685     // This transformation requires dominator postdominator info
686     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
687       AU.setPreservesCFG();
688       AU.addRequiredID(BreakCriticalEdgesID);
689       AU.addRequired<UnifyFunctionExitNodes>();
690       AU.addRequired<DominatorTree>();
691     }
692   
693     // Helper fuctions
694     // FIXME: eliminate or document these better
695     void dump(ValueNumberedSet& s) const ;
696     void clean(ValueNumberedSet& set) ;
697     Value* find_leader(ValueNumberedSet& vals, uint32_t v) ;
698     Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) ;
699     void phi_translate_set(ValueNumberedSet& anticIn, BasicBlock* pred,
700                            BasicBlock* succ, ValueNumberedSet& out) ;
701     
702     void topo_sort(ValueNumberedSet& set,
703                    SmallVector<Value*, 8>& vec) ;
704     
705     void cleanup() ;
706     bool elimination() ;
707     
708     void val_insert(ValueNumberedSet& s, Value* v) ;
709     void val_replace(ValueNumberedSet& s, Value* v) ;
710     bool dependsOnInvoke(Value* V) ;
711     void buildsets_availout(BasicBlock::iterator I,
712                             ValueNumberedSet& currAvail,
713                             ValueNumberedSet& currPhis,
714                             ValueNumberedSet& currExps,
715                             SmallPtrSet<Value*, 16>& currTemps);
716     bool buildsets_anticout(BasicBlock* BB,
717                             ValueNumberedSet& anticOut,
718                             SmallPtrSet<BasicBlock*, 8>& visited);
719     unsigned buildsets_anticin(BasicBlock* BB,
720                            ValueNumberedSet& anticOut,
721                            ValueNumberedSet& currExps,
722                            SmallPtrSet<Value*, 16>& currTemps,
723                            SmallPtrSet<BasicBlock*, 8>& visited);
724     void buildsets(Function& F) ;
725     
726     void insertion_pre(Value* e, BasicBlock* BB,
727                        DenseMap<BasicBlock*, Value*>& avail,
728                        std::map<BasicBlock*,ValueNumberedSet>& new_set);
729     unsigned insertion_mergepoint(SmallVector<Value*, 8>& workList,
730                                   df_iterator<DomTreeNode*>& D,
731                       std::map<BasicBlock*, ValueNumberedSet>& new_set);
732     bool insertion(Function& F) ;
733   
734   };
735   
736   char GVNPRE::ID = 0;
737   
738 }
739
740 // createGVNPREPass - The public interface to this file...
741 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
742
743 static RegisterPass<GVNPRE> X("gvnpre",
744                       "Global Value Numbering/Partial Redundancy Elimination");
745
746
747 STATISTIC(NumInsertedVals, "Number of values inserted");
748 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
749 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
750
751 /// find_leader - Given a set and a value number, return the first
752 /// element of the set with that value number, or 0 if no such element
753 /// is present
754 Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) {
755   if (!vals.test(v))
756     return 0;
757   
758   for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
759        I != E; ++I)
760     if (v == VN.lookup(*I))
761       return *I;
762   
763   assert(0 && "No leader found, but present bit is set?");
764   return 0;
765 }
766
767 /// val_insert - Insert a value into a set only if there is not a value
768 /// with the same value number already in the set
769 void GVNPRE::val_insert(ValueNumberedSet& s, Value* v) {
770   uint32_t num = VN.lookup(v);
771   if (!s.test(num))
772     s.insert(v);
773 }
774
775 /// val_replace - Insert a value into a set, replacing any values already in
776 /// the set that have the same value number
777 void GVNPRE::val_replace(ValueNumberedSet& s, Value* v) {
778   if (s.count(v)) return;
779   
780   uint32_t num = VN.lookup(v);
781   Value* leader = find_leader(s, num);
782   if (leader != 0)
783     s.erase(leader);
784   s.insert(v);
785   s.set(num);
786 }
787
788 /// phi_translate - Given a value, its parent block, and a predecessor of its
789 /// parent, translate the value into legal for the predecessor block.  This 
790 /// means translating its operands (and recursively, their operands) through
791 /// any phi nodes in the parent into values available in the predecessor
792 Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
793   if (V == 0)
794     return 0;
795   
796   // Unary Operations
797   if (CastInst* U = dyn_cast<CastInst>(V)) {
798     Value* newOp1 = 0;
799     if (isa<Instruction>(U->getOperand(0)))
800       newOp1 = phi_translate(U->getOperand(0), pred, succ);
801     else
802       newOp1 = U->getOperand(0);
803     
804     if (newOp1 == 0)
805       return 0;
806     
807     if (newOp1 != U->getOperand(0)) {
808       Instruction* newVal = 0;
809       if (CastInst* C = dyn_cast<CastInst>(U))
810         newVal = CastInst::Create(C->getOpcode(),
811                                   newOp1, C->getType(),
812                                   C->getName()+".expr");
813       
814       uint32_t v = VN.lookup_or_add(newVal);
815       
816       Value* leader = find_leader(availableOut[pred], v);
817       if (leader == 0) {
818         createdExpressions.push_back(newVal);
819         return newVal;
820       } else {
821         VN.erase(newVal);
822         delete newVal;
823         return leader;
824       }
825     }
826   
827   // Binary Operations
828   } if (isa<BinaryOperator>(V) || isa<CmpInst>(V) || 
829       isa<ExtractElementInst>(V)) {
830     User* U = cast<User>(V);
831     
832     Value* newOp1 = 0;
833     if (isa<Instruction>(U->getOperand(0)))
834       newOp1 = phi_translate(U->getOperand(0), pred, succ);
835     else
836       newOp1 = U->getOperand(0);
837     
838     if (newOp1 == 0)
839       return 0;
840     
841     Value* newOp2 = 0;
842     if (isa<Instruction>(U->getOperand(1)))
843       newOp2 = phi_translate(U->getOperand(1), pred, succ);
844     else
845       newOp2 = U->getOperand(1);
846     
847     if (newOp2 == 0)
848       return 0;
849     
850     if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
851       Instruction* newVal = 0;
852       if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
853         newVal = BinaryOperator::Create(BO->getOpcode(),
854                                         newOp1, newOp2,
855                                         BO->getName()+".expr");
856       else if (CmpInst* C = dyn_cast<CmpInst>(U))
857         newVal = CmpInst::Create(C->getOpcode(),
858                                  C->getPredicate(),
859                                  newOp1, newOp2,
860                                  C->getName()+".expr");
861       else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
862         newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
863       
864       uint32_t v = VN.lookup_or_add(newVal);
865       
866       Value* leader = find_leader(availableOut[pred], v);
867       if (leader == 0) {
868         createdExpressions.push_back(newVal);
869         return newVal;
870       } else {
871         VN.erase(newVal);
872         delete newVal;
873         return leader;
874       }
875     }
876   
877   // Ternary Operations
878   } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
879              isa<SelectInst>(V)) {
880     User* U = cast<User>(V);
881     
882     Value* newOp1 = 0;
883     if (isa<Instruction>(U->getOperand(0)))
884       newOp1 = phi_translate(U->getOperand(0), pred, succ);
885     else
886       newOp1 = U->getOperand(0);
887     
888     if (newOp1 == 0)
889       return 0;
890     
891     Value* newOp2 = 0;
892     if (isa<Instruction>(U->getOperand(1)))
893       newOp2 = phi_translate(U->getOperand(1), pred, succ);
894     else
895       newOp2 = U->getOperand(1);
896     
897     if (newOp2 == 0)
898       return 0;
899     
900     Value* newOp3 = 0;
901     if (isa<Instruction>(U->getOperand(2)))
902       newOp3 = phi_translate(U->getOperand(2), pred, succ);
903     else
904       newOp3 = U->getOperand(2);
905     
906     if (newOp3 == 0)
907       return 0;
908     
909     if (newOp1 != U->getOperand(0) ||
910         newOp2 != U->getOperand(1) ||
911         newOp3 != U->getOperand(2)) {
912       Instruction* newVal = 0;
913       if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
914         newVal = new ShuffleVectorInst(newOp1, newOp2, newOp3,
915                                        S->getName() + ".expr");
916       else if (InsertElementInst* I = dyn_cast<InsertElementInst>(U))
917         newVal = InsertElementInst::Create(newOp1, newOp2, newOp3,
918                                            I->getName() + ".expr");
919       else if (SelectInst* I = dyn_cast<SelectInst>(U))
920         newVal = SelectInst::Create(newOp1, newOp2, newOp3,
921                                     I->getName() + ".expr");
922       
923       uint32_t v = VN.lookup_or_add(newVal);
924       
925       Value* leader = find_leader(availableOut[pred], v);
926       if (leader == 0) {
927         createdExpressions.push_back(newVal);
928         return newVal;
929       } else {
930         VN.erase(newVal);
931         delete newVal;
932         return leader;
933       }
934     }
935   
936   // Varargs operators
937   } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
938     Value* newOp1 = 0;
939     if (isa<Instruction>(U->getPointerOperand()))
940       newOp1 = phi_translate(U->getPointerOperand(), pred, succ);
941     else
942       newOp1 = U->getPointerOperand();
943     
944     if (newOp1 == 0)
945       return 0;
946     
947     bool changed_idx = false;
948     SmallVector<Value*, 4> newIdx;
949     for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
950          I != E; ++I)
951       if (isa<Instruction>(*I)) {
952         Value* newVal = phi_translate(*I, pred, succ);
953         newIdx.push_back(newVal);
954         if (newVal != *I)
955           changed_idx = true;
956       } else {
957         newIdx.push_back(*I);
958       }
959     
960     if (newOp1 != U->getPointerOperand() || changed_idx) {
961       Instruction* newVal =
962           GetElementPtrInst::Create(newOp1,
963                                     newIdx.begin(), newIdx.end(),
964                                     U->getName()+".expr");
965       
966       uint32_t v = VN.lookup_or_add(newVal);
967       
968       Value* leader = find_leader(availableOut[pred], v);
969       if (leader == 0) {
970         createdExpressions.push_back(newVal);
971         return newVal;
972       } else {
973         VN.erase(newVal);
974         delete newVal;
975         return leader;
976       }
977     }
978   
979   // PHI Nodes
980   } else if (PHINode* P = dyn_cast<PHINode>(V)) {
981     if (P->getParent() == succ)
982       return P->getIncomingValueForBlock(pred);
983   }
984   
985   return V;
986 }
987
988 /// phi_translate_set - Perform phi translation on every element of a set
989 void GVNPRE::phi_translate_set(ValueNumberedSet& anticIn,
990                               BasicBlock* pred, BasicBlock* succ,
991                               ValueNumberedSet& out) {
992   for (ValueNumberedSet::iterator I = anticIn.begin(),
993        E = anticIn.end(); I != E; ++I) {
994     Value* V = phi_translate(*I, pred, succ);
995     if (V != 0 && !out.test(VN.lookup_or_add(V))) {
996       out.insert(V);
997       out.set(VN.lookup(V));
998     }
999   }
1000 }
1001
1002 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of 
1003 /// whose inputs is an invoke instruction.  If this is true, we cannot safely
1004 /// PRE the instruction or anything that depends on it.
1005 bool GVNPRE::dependsOnInvoke(Value* V) {
1006   if (PHINode* p = dyn_cast<PHINode>(V)) {
1007     for (PHINode::op_iterator I = p->op_begin(), E = p->op_end(); I != E; ++I)
1008       if (isa<InvokeInst>(*I))
1009         return true;
1010     return false;
1011   } else {
1012     return false;
1013   }
1014 }
1015
1016 /// clean - Remove all non-opaque values from the set whose operands are not
1017 /// themselves in the set, as well as all values that depend on invokes (see 
1018 /// above)
1019 void GVNPRE::clean(ValueNumberedSet& set) {
1020   SmallVector<Value*, 8> worklist;
1021   worklist.reserve(set.size());
1022   topo_sort(set, worklist);
1023   
1024   for (unsigned i = 0; i < worklist.size(); ++i) {
1025     Value* v = worklist[i];
1026     
1027     // Handle unary ops
1028     if (CastInst* U = dyn_cast<CastInst>(v)) {
1029       bool lhsValid = !isa<Instruction>(U->getOperand(0));
1030       lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1031       if (lhsValid)
1032         lhsValid = !dependsOnInvoke(U->getOperand(0));
1033       
1034       if (!lhsValid) {
1035         set.erase(U);
1036         set.reset(VN.lookup(U));
1037       }
1038     
1039     // Handle binary ops
1040     } else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
1041         isa<ExtractElementInst>(v)) {
1042       User* U = cast<User>(v);
1043       
1044       bool lhsValid = !isa<Instruction>(U->getOperand(0));
1045       lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1046       if (lhsValid)
1047         lhsValid = !dependsOnInvoke(U->getOperand(0));
1048     
1049       bool rhsValid = !isa<Instruction>(U->getOperand(1));
1050       rhsValid |= set.test(VN.lookup(U->getOperand(1)));
1051       if (rhsValid)
1052         rhsValid = !dependsOnInvoke(U->getOperand(1));
1053       
1054       if (!lhsValid || !rhsValid) {
1055         set.erase(U);
1056         set.reset(VN.lookup(U));
1057       }
1058     
1059     // Handle ternary ops
1060     } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
1061                isa<SelectInst>(v)) {
1062       User* U = cast<User>(v);
1063     
1064       bool lhsValid = !isa<Instruction>(U->getOperand(0));
1065       lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1066       if (lhsValid)
1067         lhsValid = !dependsOnInvoke(U->getOperand(0));
1068       
1069       bool rhsValid = !isa<Instruction>(U->getOperand(1));
1070       rhsValid |= set.test(VN.lookup(U->getOperand(1)));
1071       if (rhsValid)
1072         rhsValid = !dependsOnInvoke(U->getOperand(1));
1073       
1074       bool thirdValid = !isa<Instruction>(U->getOperand(2));
1075       thirdValid |= set.test(VN.lookup(U->getOperand(2)));
1076       if (thirdValid)
1077         thirdValid = !dependsOnInvoke(U->getOperand(2));
1078     
1079       if (!lhsValid || !rhsValid || !thirdValid) {
1080         set.erase(U);
1081         set.reset(VN.lookup(U));
1082       }
1083     
1084     // Handle varargs ops
1085     } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) {
1086       bool ptrValid = !isa<Instruction>(U->getPointerOperand());
1087       ptrValid |= set.test(VN.lookup(U->getPointerOperand()));
1088       if (ptrValid)
1089         ptrValid = !dependsOnInvoke(U->getPointerOperand());
1090       
1091       bool varValid = true;
1092       for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
1093            I != E; ++I)
1094         if (varValid) {
1095           varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I));
1096           varValid &= !dependsOnInvoke(*I);
1097         }
1098     
1099       if (!ptrValid || !varValid) {
1100         set.erase(U);
1101         set.reset(VN.lookup(U));
1102       }
1103     }
1104   }
1105 }
1106
1107 /// topo_sort - Given a set of values, sort them by topological
1108 /// order into the provided vector.
1109 void GVNPRE::topo_sort(ValueNumberedSet& set, SmallVector<Value*, 8>& vec) {
1110   SmallPtrSet<Value*, 16> visited;
1111   SmallVector<Value*, 8> stack;
1112   for (ValueNumberedSet::iterator I = set.begin(), E = set.end();
1113        I != E; ++I) {
1114     if (visited.count(*I) == 0)
1115       stack.push_back(*I);
1116     
1117     while (!stack.empty()) {
1118       Value* e = stack.back();
1119       
1120       // Handle unary ops
1121       if (CastInst* U = dyn_cast<CastInst>(e)) {
1122         Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1123     
1124         if (l != 0 && isa<Instruction>(l) &&
1125             visited.count(l) == 0)
1126           stack.push_back(l);
1127         else {
1128           vec.push_back(e);
1129           visited.insert(e);
1130           stack.pop_back();
1131         }
1132       
1133       // Handle binary ops
1134       } else if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1135           isa<ExtractElementInst>(e)) {
1136         User* U = cast<User>(e);
1137         Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1138         Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1139     
1140         if (l != 0 && isa<Instruction>(l) &&
1141             visited.count(l) == 0)
1142           stack.push_back(l);
1143         else if (r != 0 && isa<Instruction>(r) &&
1144                  visited.count(r) == 0)
1145           stack.push_back(r);
1146         else {
1147           vec.push_back(e);
1148           visited.insert(e);
1149           stack.pop_back();
1150         }
1151       
1152       // Handle ternary ops
1153       } else if (isa<InsertElementInst>(e) || isa<ShuffleVectorInst>(e) ||
1154                  isa<SelectInst>(e)) {
1155         User* U = cast<User>(e);
1156         Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1157         Value* r = find_leader(set, VN.lookup(U->getOperand(1)));
1158         Value* m = find_leader(set, VN.lookup(U->getOperand(2)));
1159       
1160         if (l != 0 && isa<Instruction>(l) &&
1161             visited.count(l) == 0)
1162           stack.push_back(l);
1163         else if (r != 0 && isa<Instruction>(r) &&
1164                  visited.count(r) == 0)
1165           stack.push_back(r);
1166         else if (m != 0 && isa<Instruction>(m) &&
1167                  visited.count(m) == 0)
1168           stack.push_back(m);
1169         else {
1170           vec.push_back(e);
1171           visited.insert(e);
1172           stack.pop_back();
1173         }
1174       
1175       // Handle vararg ops
1176       } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) {
1177         Value* p = find_leader(set, VN.lookup(U->getPointerOperand()));
1178         
1179         if (p != 0 && isa<Instruction>(p) &&
1180             visited.count(p) == 0)
1181           stack.push_back(p);
1182         else {
1183           bool push_va = false;
1184           for (GetElementPtrInst::op_iterator I = U->idx_begin(),
1185                E = U->idx_end(); I != E; ++I) {
1186             Value * v = find_leader(set, VN.lookup(*I));
1187             if (v != 0 && isa<Instruction>(v) && visited.count(v) == 0) {
1188               stack.push_back(v);
1189               push_va = true;
1190             }
1191           }
1192           
1193           if (!push_va) {
1194             vec.push_back(e);
1195             visited.insert(e);
1196             stack.pop_back();
1197           }
1198         }
1199       
1200       // Handle opaque ops
1201       } else {
1202         visited.insert(e);
1203         vec.push_back(e);
1204         stack.pop_back();
1205       }
1206     }
1207     
1208     stack.clear();
1209   }
1210 }
1211
1212 /// dump - Dump a set of values to standard error
1213 void GVNPRE::dump(ValueNumberedSet& s) const {
1214   DOUT << "{ ";
1215   for (ValueNumberedSet::iterator I = s.begin(), E = s.end();
1216        I != E; ++I) {
1217     DOUT << "" << VN.lookup(*I) << ": ";
1218     DEBUG((*I)->dump());
1219   }
1220   DOUT << "}\n\n";
1221 }
1222
1223 /// elimination - Phase 3 of the main algorithm.  Perform full redundancy 
1224 /// elimination by walking the dominator tree and removing any instruction that 
1225 /// is dominated by another instruction with the same value number.
1226 bool GVNPRE::elimination() {
1227   bool changed_function = false;
1228   
1229   SmallVector<std::pair<Instruction*, Value*>, 8> replace;
1230   SmallVector<Instruction*, 8> erase;
1231   
1232   DominatorTree& DT = getAnalysis<DominatorTree>();
1233   
1234   for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1235          E = df_end(DT.getRootNode()); DI != E; ++DI) {
1236     BasicBlock* BB = DI->getBlock();
1237     
1238     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1239          BI != BE; ++BI) {
1240
1241       if (isa<BinaryOperator>(BI) || isa<CmpInst>(BI) ||
1242           isa<ShuffleVectorInst>(BI) || isa<InsertElementInst>(BI) ||
1243           isa<ExtractElementInst>(BI) || isa<SelectInst>(BI) ||
1244           isa<CastInst>(BI) || isa<GetElementPtrInst>(BI)) {
1245         
1246         if (availableOut[BB].test(VN.lookup(BI)) &&
1247             !availableOut[BB].count(BI)) {
1248           Value *leader = find_leader(availableOut[BB], VN.lookup(BI));
1249           if (Instruction* Instr = dyn_cast<Instruction>(leader))
1250             if (Instr->getParent() != 0 && Instr != BI) {
1251               replace.push_back(std::make_pair(BI, leader));
1252               erase.push_back(BI);
1253               ++NumEliminated;
1254             }
1255         }
1256       }
1257     }
1258   }
1259   
1260   while (!replace.empty()) {
1261     std::pair<Instruction*, Value*> rep = replace.back();
1262     replace.pop_back();
1263     rep.first->replaceAllUsesWith(rep.second);
1264     changed_function = true;
1265   }
1266     
1267   for (SmallVector<Instruction*, 8>::iterator I = erase.begin(),
1268        E = erase.end(); I != E; ++I)
1269      (*I)->eraseFromParent();
1270   
1271   return changed_function;
1272 }
1273
1274 /// cleanup - Delete any extraneous values that were created to represent
1275 /// expressions without leaders.
1276 void GVNPRE::cleanup() {
1277   while (!createdExpressions.empty()) {
1278     Instruction* I = createdExpressions.back();
1279     createdExpressions.pop_back();
1280     
1281     delete I;
1282   }
1283 }
1284
1285 /// buildsets_availout - When calculating availability, handle an instruction
1286 /// by inserting it into the appropriate sets
1287 void GVNPRE::buildsets_availout(BasicBlock::iterator I,
1288                                 ValueNumberedSet& currAvail,
1289                                 ValueNumberedSet& currPhis,
1290                                 ValueNumberedSet& currExps,
1291                                 SmallPtrSet<Value*, 16>& currTemps) {
1292   // Handle PHI nodes
1293   if (PHINode* p = dyn_cast<PHINode>(I)) {
1294     unsigned num = VN.lookup_or_add(p);
1295     
1296     currPhis.insert(p);
1297     currPhis.set(num);
1298   
1299   // Handle unary ops
1300   } else if (CastInst* U = dyn_cast<CastInst>(I)) {
1301     Value* leftValue = U->getOperand(0);
1302     
1303     unsigned num = VN.lookup_or_add(U);
1304       
1305     if (isa<Instruction>(leftValue))
1306       if (!currExps.test(VN.lookup(leftValue))) {
1307         currExps.insert(leftValue);
1308         currExps.set(VN.lookup(leftValue));
1309       }
1310     
1311     if (!currExps.test(num)) {
1312       currExps.insert(U);
1313       currExps.set(num);
1314     }
1315   
1316   // Handle binary ops
1317   } else if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
1318              isa<ExtractElementInst>(I)) {
1319     User* U = cast<User>(I);
1320     Value* leftValue = U->getOperand(0);
1321     Value* rightValue = U->getOperand(1);
1322     
1323     unsigned num = VN.lookup_or_add(U);
1324       
1325     if (isa<Instruction>(leftValue))
1326       if (!currExps.test(VN.lookup(leftValue))) {
1327         currExps.insert(leftValue);
1328         currExps.set(VN.lookup(leftValue));
1329       }
1330     
1331     if (isa<Instruction>(rightValue))
1332       if (!currExps.test(VN.lookup(rightValue))) {
1333         currExps.insert(rightValue);
1334         currExps.set(VN.lookup(rightValue));
1335       }
1336     
1337     if (!currExps.test(num)) {
1338       currExps.insert(U);
1339       currExps.set(num);
1340     }
1341     
1342   // Handle ternary ops
1343   } else if (isa<InsertElementInst>(I) || isa<ShuffleVectorInst>(I) ||
1344              isa<SelectInst>(I)) {
1345     User* U = cast<User>(I);
1346     Value* leftValue = U->getOperand(0);
1347     Value* rightValue = U->getOperand(1);
1348     Value* thirdValue = U->getOperand(2);
1349       
1350     VN.lookup_or_add(U);
1351     
1352     unsigned num = VN.lookup_or_add(U);
1353     
1354     if (isa<Instruction>(leftValue))
1355       if (!currExps.test(VN.lookup(leftValue))) {
1356         currExps.insert(leftValue);
1357         currExps.set(VN.lookup(leftValue));
1358       }
1359     if (isa<Instruction>(rightValue))
1360       if (!currExps.test(VN.lookup(rightValue))) {
1361         currExps.insert(rightValue);
1362         currExps.set(VN.lookup(rightValue));
1363       }
1364     if (isa<Instruction>(thirdValue))
1365       if (!currExps.test(VN.lookup(thirdValue))) {
1366         currExps.insert(thirdValue);
1367         currExps.set(VN.lookup(thirdValue));
1368       }
1369     
1370     if (!currExps.test(num)) {
1371       currExps.insert(U);
1372       currExps.set(num);
1373     }
1374     
1375   // Handle vararg ops
1376   } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) {
1377     Value* ptrValue = U->getPointerOperand();
1378       
1379     VN.lookup_or_add(U);
1380     
1381     unsigned num = VN.lookup_or_add(U);
1382     
1383     if (isa<Instruction>(ptrValue))
1384       if (!currExps.test(VN.lookup(ptrValue))) {
1385         currExps.insert(ptrValue);
1386         currExps.set(VN.lookup(ptrValue));
1387       }
1388     
1389     for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end();
1390          OI != OE; ++OI)
1391       if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) {
1392         currExps.insert(*OI);
1393         currExps.set(VN.lookup(*OI));
1394       }
1395     
1396     if (!currExps.test(VN.lookup(U))) {
1397       currExps.insert(U);
1398       currExps.set(num);
1399     }
1400     
1401   // Handle opaque ops
1402   } else if (!I->isTerminator()){
1403     VN.lookup_or_add(I);
1404     
1405     currTemps.insert(I);
1406   }
1407     
1408   if (!I->isTerminator())
1409     if (!currAvail.test(VN.lookup(I))) {
1410       currAvail.insert(I);
1411       currAvail.set(VN.lookup(I));
1412     }
1413 }
1414
1415 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
1416 /// set as a function of the ANTIC_IN set of the block's predecessors
1417 bool GVNPRE::buildsets_anticout(BasicBlock* BB,
1418                                 ValueNumberedSet& anticOut,
1419                                 SmallPtrSet<BasicBlock*, 8>& visited) {
1420   if (BB->getTerminator()->getNumSuccessors() == 1) {
1421     if (BB->getTerminator()->getSuccessor(0) != BB &&
1422         visited.count(BB->getTerminator()->getSuccessor(0)) == 0) {
1423       return true;
1424     }
1425     else {
1426       phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
1427                         BB,  BB->getTerminator()->getSuccessor(0), anticOut);
1428     }
1429   } else if (BB->getTerminator()->getNumSuccessors() > 1) {
1430     BasicBlock* first = BB->getTerminator()->getSuccessor(0);
1431     for (ValueNumberedSet::iterator I = anticipatedIn[first].begin(),
1432          E = anticipatedIn[first].end(); I != E; ++I) {
1433       anticOut.insert(*I);
1434       anticOut.set(VN.lookup(*I));
1435     }
1436     
1437     for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
1438       BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
1439       ValueNumberedSet& succAnticIn = anticipatedIn[currSucc];
1440       
1441       SmallVector<Value*, 16> temp;
1442       
1443       for (ValueNumberedSet::iterator I = anticOut.begin(),
1444            E = anticOut.end(); I != E; ++I)
1445         if (!succAnticIn.test(VN.lookup(*I)))
1446           temp.push_back(*I);
1447
1448       for (SmallVector<Value*, 16>::iterator I = temp.begin(), E = temp.end();
1449            I != E; ++I) {
1450         anticOut.erase(*I);
1451         anticOut.reset(VN.lookup(*I));
1452       }
1453     }
1454   }
1455   
1456   return false;
1457 }
1458
1459 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
1460 /// each block.  ANTIC_IN is then a function of ANTIC_OUT and the GEN
1461 /// sets populated in buildsets_availout
1462 unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
1463                                ValueNumberedSet& anticOut,
1464                                ValueNumberedSet& currExps,
1465                                SmallPtrSet<Value*, 16>& currTemps,
1466                                SmallPtrSet<BasicBlock*, 8>& visited) {
1467   ValueNumberedSet& anticIn = anticipatedIn[BB];
1468   unsigned old = anticIn.size();
1469       
1470   bool defer = buildsets_anticout(BB, anticOut, visited);
1471   if (defer)
1472     return 0;
1473   
1474   anticIn.clear();
1475   
1476   for (ValueNumberedSet::iterator I = anticOut.begin(),
1477        E = anticOut.end(); I != E; ++I) {
1478     anticIn.insert(*I);
1479     anticIn.set(VN.lookup(*I));
1480   }
1481   for (ValueNumberedSet::iterator I = currExps.begin(),
1482        E = currExps.end(); I != E; ++I) {
1483     if (!anticIn.test(VN.lookup(*I))) {
1484       anticIn.insert(*I);
1485       anticIn.set(VN.lookup(*I));
1486     }
1487   } 
1488   
1489   for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
1490        E = currTemps.end(); I != E; ++I) {
1491     anticIn.erase(*I);
1492     anticIn.reset(VN.lookup(*I));
1493   }
1494   
1495   clean(anticIn);
1496   anticOut.clear();
1497   
1498   if (old != anticIn.size())
1499     return 2;
1500   else
1501     return 1;
1502 }
1503
1504 /// buildsets - Phase 1 of the main algorithm.  Construct the AVAIL_OUT
1505 /// and the ANTIC_IN sets.
1506 void GVNPRE::buildsets(Function& F) {
1507   DenseMap<BasicBlock*, ValueNumberedSet> generatedExpressions;
1508   DenseMap<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
1509
1510   DominatorTree &DT = getAnalysis<DominatorTree>();   
1511   
1512   // Phase 1, Part 1: calculate AVAIL_OUT
1513   
1514   // Top-down walk of the dominator tree
1515   for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1516          E = df_end(DT.getRootNode()); DI != E; ++DI) {
1517     
1518     // Get the sets to update for this block
1519     ValueNumberedSet& currExps = generatedExpressions[DI->getBlock()];
1520     ValueNumberedSet& currPhis = generatedPhis[DI->getBlock()];
1521     SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
1522     ValueNumberedSet& currAvail = availableOut[DI->getBlock()];     
1523     
1524     BasicBlock* BB = DI->getBlock();
1525   
1526     // A block inherits AVAIL_OUT from its dominator
1527     if (DI->getIDom() != 0)
1528       currAvail = availableOut[DI->getIDom()->getBlock()];
1529
1530     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1531          BI != BE; ++BI)
1532       buildsets_availout(BI, currAvail, currPhis, currExps,
1533                          currTemps);
1534       
1535   }
1536
1537   // Phase 1, Part 2: calculate ANTIC_IN
1538   
1539   SmallPtrSet<BasicBlock*, 8> visited;
1540   SmallPtrSet<BasicBlock*, 4> block_changed;
1541   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
1542     block_changed.insert(FI);
1543   
1544   bool changed = true;
1545   unsigned iterations = 0;
1546   
1547   while (changed) {
1548     changed = false;
1549     ValueNumberedSet anticOut;
1550     
1551     // Postorder walk of the CFG
1552     for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
1553          BBE = po_end(&F.getEntryBlock()); BBI != BBE; ++BBI) {
1554       BasicBlock* BB = *BBI;
1555       
1556       if (block_changed.count(BB) != 0) {
1557         unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
1558                                          generatedTemporaries[BB], visited);
1559       
1560         if (ret == 0) {
1561           changed = true;
1562           continue;
1563         } else {
1564           visited.insert(BB);
1565         
1566           if (ret == 2)
1567            for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1568                  PI != PE; ++PI) {
1569               block_changed.insert(*PI);
1570            }
1571           else
1572             block_changed.erase(BB);
1573         
1574           changed |= (ret == 2);
1575         }
1576       }
1577     }
1578     
1579     iterations++;
1580   }
1581 }
1582
1583 /// insertion_pre - When a partial redundancy has been identified, eliminate it
1584 /// by inserting appropriate values into the predecessors and a phi node in
1585 /// the main block
1586 void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
1587                            DenseMap<BasicBlock*, Value*>& avail,
1588                     std::map<BasicBlock*, ValueNumberedSet>& new_sets) {
1589   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1590     Value* e2 = avail[*PI];
1591     if (!availableOut[*PI].test(VN.lookup(e2))) {
1592       User* U = cast<User>(e2);
1593       
1594       Value* s1 = 0;
1595       if (isa<BinaryOperator>(U->getOperand(0)) || 
1596           isa<CmpInst>(U->getOperand(0)) ||
1597           isa<ShuffleVectorInst>(U->getOperand(0)) ||
1598           isa<ExtractElementInst>(U->getOperand(0)) ||
1599           isa<InsertElementInst>(U->getOperand(0)) ||
1600           isa<SelectInst>(U->getOperand(0)) ||
1601           isa<CastInst>(U->getOperand(0)) ||
1602           isa<GetElementPtrInst>(U->getOperand(0)))
1603         s1 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(0)));
1604       else
1605         s1 = U->getOperand(0);
1606       
1607       Value* s2 = 0;
1608       
1609       if (isa<BinaryOperator>(U) || 
1610           isa<CmpInst>(U) ||
1611           isa<ShuffleVectorInst>(U) ||
1612           isa<ExtractElementInst>(U) ||
1613           isa<InsertElementInst>(U) ||
1614           isa<SelectInst>(U)) {
1615         if (isa<BinaryOperator>(U->getOperand(1)) || 
1616             isa<CmpInst>(U->getOperand(1)) ||
1617             isa<ShuffleVectorInst>(U->getOperand(1)) ||
1618             isa<ExtractElementInst>(U->getOperand(1)) ||
1619             isa<InsertElementInst>(U->getOperand(1)) ||
1620             isa<SelectInst>(U->getOperand(1)) ||
1621             isa<CastInst>(U->getOperand(1)) ||
1622             isa<GetElementPtrInst>(U->getOperand(1))) {
1623           s2 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(1)));
1624         } else {
1625           s2 = U->getOperand(1);
1626         }
1627       }
1628       
1629       // Ternary Operators
1630       Value* s3 = 0;
1631       if (isa<ShuffleVectorInst>(U) ||
1632           isa<InsertElementInst>(U) ||
1633           isa<SelectInst>(U)) {
1634         if (isa<BinaryOperator>(U->getOperand(2)) || 
1635             isa<CmpInst>(U->getOperand(2)) ||
1636             isa<ShuffleVectorInst>(U->getOperand(2)) ||
1637             isa<ExtractElementInst>(U->getOperand(2)) ||
1638             isa<InsertElementInst>(U->getOperand(2)) ||
1639             isa<SelectInst>(U->getOperand(2)) ||
1640             isa<CastInst>(U->getOperand(2)) ||
1641             isa<GetElementPtrInst>(U->getOperand(2))) {
1642           s3 = find_leader(availableOut[*PI], VN.lookup(U->getOperand(2)));
1643         } else {
1644           s3 = U->getOperand(2);
1645         }
1646       }
1647       
1648       // Vararg operators
1649       SmallVector<Value*, 4> sVarargs;
1650       if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U)) {
1651         for (GetElementPtrInst::op_iterator OI = G->idx_begin(),
1652              OE = G->idx_end(); OI != OE; ++OI) {
1653           if (isa<BinaryOperator>(*OI) || 
1654               isa<CmpInst>(*OI) ||
1655               isa<ShuffleVectorInst>(*OI) ||
1656               isa<ExtractElementInst>(*OI) ||
1657               isa<InsertElementInst>(*OI) ||
1658               isa<SelectInst>(*OI) ||
1659               isa<CastInst>(*OI) ||
1660               isa<GetElementPtrInst>(*OI)) {
1661             sVarargs.push_back(find_leader(availableOut[*PI], 
1662                                VN.lookup(*OI)));
1663           } else {
1664             sVarargs.push_back(*OI);
1665           }
1666         }
1667       }
1668       
1669       Value* newVal = 0;
1670       if (BinaryOperator* BO = dyn_cast<BinaryOperator>(U))
1671         newVal = BinaryOperator::Create(BO->getOpcode(), s1, s2,
1672                                         BO->getName()+".gvnpre",
1673                                         (*PI)->getTerminator());
1674       else if (CmpInst* C = dyn_cast<CmpInst>(U))
1675         newVal = CmpInst::Create(C->getOpcode(), C->getPredicate(), s1, s2,
1676                                  C->getName()+".gvnpre", 
1677                                  (*PI)->getTerminator());
1678       else if (ShuffleVectorInst* S = dyn_cast<ShuffleVectorInst>(U))
1679         newVal = new ShuffleVectorInst(s1, s2, s3, S->getName()+".gvnpre",
1680                                        (*PI)->getTerminator());
1681       else if (InsertElementInst* S = dyn_cast<InsertElementInst>(U))
1682         newVal = InsertElementInst::Create(s1, s2, s3, S->getName()+".gvnpre",
1683                                            (*PI)->getTerminator());
1684       else if (ExtractElementInst* S = dyn_cast<ExtractElementInst>(U))
1685         newVal = new ExtractElementInst(s1, s2, S->getName()+".gvnpre",
1686                                         (*PI)->getTerminator());
1687       else if (SelectInst* S = dyn_cast<SelectInst>(U))
1688         newVal = SelectInst::Create(s1, s2, s3, S->getName()+".gvnpre",
1689                                     (*PI)->getTerminator());
1690       else if (CastInst* C = dyn_cast<CastInst>(U))
1691         newVal = CastInst::Create(C->getOpcode(), s1, C->getType(),
1692                                   C->getName()+".gvnpre", 
1693                                   (*PI)->getTerminator());
1694       else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(U))
1695         newVal = GetElementPtrInst::Create(s1, sVarargs.begin(), sVarargs.end(),
1696                                            G->getName()+".gvnpre", 
1697                                            (*PI)->getTerminator());
1698
1699       VN.add(newVal, VN.lookup(U));
1700                   
1701       ValueNumberedSet& predAvail = availableOut[*PI];
1702       val_replace(predAvail, newVal);
1703       val_replace(new_sets[*PI], newVal);
1704       predAvail.set(VN.lookup(newVal));
1705             
1706       DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1707       if (av != avail.end())
1708         avail.erase(av);
1709       avail.insert(std::make_pair(*PI, newVal));
1710                   
1711       ++NumInsertedVals;
1712     }
1713   }
1714               
1715   PHINode* p = 0;
1716               
1717   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1718     if (p == 0)
1719       p = PHINode::Create(avail[*PI]->getType(), "gvnpre-join", BB->begin());
1720     
1721     p->addIncoming(avail[*PI], *PI);
1722   }
1723
1724   VN.add(p, VN.lookup(e));
1725   val_replace(availableOut[BB], p);
1726   availableOut[BB].set(VN.lookup(e));
1727   generatedPhis[BB].insert(p);
1728   generatedPhis[BB].set(VN.lookup(e));
1729   new_sets[BB].insert(p);
1730   new_sets[BB].set(VN.lookup(e));
1731               
1732   ++NumInsertedPhis;
1733 }
1734
1735 /// insertion_mergepoint - When walking the dom tree, check at each merge
1736 /// block for the possibility of a partial redundancy.  If present, eliminate it
1737 unsigned GVNPRE::insertion_mergepoint(SmallVector<Value*, 8>& workList,
1738                                       df_iterator<DomTreeNode*>& D,
1739                     std::map<BasicBlock*, ValueNumberedSet >& new_sets) {
1740   bool changed_function = false;
1741   bool new_stuff = false;
1742   
1743   BasicBlock* BB = D->getBlock();
1744   for (unsigned i = 0; i < workList.size(); ++i) {
1745     Value* e = workList[i];
1746           
1747     if (isa<BinaryOperator>(e) || isa<CmpInst>(e) ||
1748         isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
1749         isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) ||
1750         isa<GetElementPtrInst>(e)) {
1751       if (availableOut[D->getIDom()->getBlock()].test(VN.lookup(e)))
1752         continue;
1753             
1754       DenseMap<BasicBlock*, Value*> avail;
1755       bool by_some = false;
1756       bool all_same = true;
1757       Value * first_s = 0;
1758             
1759       for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
1760            ++PI) {
1761         Value *e2 = phi_translate(e, *PI, BB);
1762         Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
1763               
1764         if (e3 == 0) {
1765           DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1766           if (av != avail.end())
1767             avail.erase(av);
1768           avail.insert(std::make_pair(*PI, e2));
1769           all_same = false;
1770         } else {
1771           DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1772           if (av != avail.end())
1773             avail.erase(av);
1774           avail.insert(std::make_pair(*PI, e3));
1775                 
1776           by_some = true;
1777           if (first_s == 0)
1778             first_s = e3;
1779           else if (first_s != e3)
1780             all_same = false;
1781         }
1782       }
1783             
1784       if (by_some && !all_same &&
1785           !generatedPhis[BB].test(VN.lookup(e))) {
1786         insertion_pre(e, BB, avail, new_sets);
1787               
1788         changed_function = true;
1789         new_stuff = true;
1790       }
1791     }
1792   }
1793   
1794   unsigned retval = 0;
1795   if (changed_function)
1796     retval += 1;
1797   if (new_stuff)
1798     retval += 2;
1799   
1800   return retval;
1801 }
1802
1803 /// insert - Phase 2 of the main algorithm.  Walk the dominator tree looking for
1804 /// merge points.  When one is found, check for a partial redundancy.  If one is
1805 /// present, eliminate it.  Repeat this walk until no changes are made.
1806 bool GVNPRE::insertion(Function& F) {
1807   bool changed_function = false;
1808
1809   DominatorTree &DT = getAnalysis<DominatorTree>();  
1810   
1811   std::map<BasicBlock*, ValueNumberedSet> new_sets;
1812   bool new_stuff = true;
1813   while (new_stuff) {
1814     new_stuff = false;
1815     for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1816          E = df_end(DT.getRootNode()); DI != E; ++DI) {
1817       BasicBlock* BB = DI->getBlock();
1818       
1819       if (BB == 0)
1820         continue;
1821       
1822       ValueNumberedSet& availOut = availableOut[BB];
1823       ValueNumberedSet& anticIn = anticipatedIn[BB];
1824       
1825       // Replace leaders with leaders inherited from dominator
1826       if (DI->getIDom() != 0) {
1827         ValueNumberedSet& dom_set = new_sets[DI->getIDom()->getBlock()];
1828         for (ValueNumberedSet::iterator I = dom_set.begin(),
1829              E = dom_set.end(); I != E; ++I) {
1830           val_replace(new_sets[BB], *I);
1831           val_replace(availOut, *I);
1832         }
1833       }
1834       
1835       // If there is more than one predecessor...
1836       if (pred_begin(BB) != pred_end(BB) && ++pred_begin(BB) != pred_end(BB)) {
1837         SmallVector<Value*, 8> workList;
1838         workList.reserve(anticIn.size());
1839         topo_sort(anticIn, workList);
1840         
1841         unsigned result = insertion_mergepoint(workList, DI, new_sets);
1842         if (result & 1)
1843           changed_function = true;
1844         if (result & 2)
1845           new_stuff = true;
1846       }
1847     }
1848   }
1849   
1850   return changed_function;
1851 }
1852
1853 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1854 // function.
1855 //
1856 bool GVNPRE::runOnFunction(Function &F) {
1857   // Clean out global sets from any previous functions
1858   VN.clear();
1859   createdExpressions.clear();
1860   availableOut.clear();
1861   anticipatedIn.clear();
1862   generatedPhis.clear();
1863  
1864   bool changed_function = false;
1865   
1866   // Phase 1: BuildSets
1867   // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1868   buildsets(F);
1869   
1870   // Phase 2: Insert
1871   // This phase inserts values to make partially redundant values
1872   // fully redundant
1873   changed_function |= insertion(F);
1874   
1875   // Phase 3: Eliminate
1876   // This phase performs trivial full redundancy elimination
1877   changed_function |= elimination();
1878   
1879   // Phase 4: Cleanup
1880   // This phase cleans up values that were created solely
1881   // as leaders for expressions
1882   cleanup();
1883   
1884   return changed_function;
1885 }