Update GEP constructors to use an iterator interface to fix
[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 was developed by the Owen Anderson and is distributed under
6 // the University of Illinois Open Source 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 //===----------------------------------------------------------------------===//
20
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"
39 #include <algorithm>
40 #include <deque>
41 #include <map>
42 using namespace llvm;
43
44 //===----------------------------------------------------------------------===//
45 //                         ValueTable Class
46 //===----------------------------------------------------------------------===//
47
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
50 /// two values.
51
52 struct Expression {
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,
63                           TOMBSTONE };
64
65   ExpressionOpcode opcode;
66   const Type* type;
67   uint32_t firstVN;
68   uint32_t secondVN;
69   uint32_t thirdVN;
70   SmallVector<uint32_t, 4> varargs;
71   
72   Expression() { }
73   Expression(ExpressionOpcode o) : opcode(o) { }
74   
75   bool operator==(const Expression &other) const {
76     if (opcode != other.opcode)
77       return false;
78     else if (opcode == EMPTY || opcode == TOMBSTONE)
79       return true;
80     else if (type != other.type)
81       return false;
82     else if (firstVN != other.firstVN)
83       return false;
84     else if (secondVN != other.secondVN)
85       return false;
86     else if (thirdVN != other.thirdVN)
87       return false;
88     else {
89       if (varargs.size() != other.varargs.size())
90         return false;
91       
92       for (size_t i = 0; i < varargs.size(); ++i)
93         if (varargs[i] != other.varargs[i])
94           return false;
95     
96       return true;
97     }
98   }
99   
100   bool operator!=(const Expression &other) const {
101     if (opcode != other.opcode)
102       return true;
103     else if (opcode == EMPTY || opcode == TOMBSTONE)
104       return false;
105     else if (type != other.type)
106       return true;
107     else if (firstVN != other.firstVN)
108       return true;
109     else if (secondVN != other.secondVN)
110       return true;
111     else if (thirdVN != other.thirdVN)
112       return true;
113     else {
114       if (varargs.size() != other.varargs.size())
115         return true;
116       
117       for (size_t i = 0; i < varargs.size(); ++i)
118         if (varargs[i] != other.varargs[i])
119           return true;
120     
121       return false;
122     }
123   }
124 };
125
126
127 namespace {
128   class VISIBILITY_HIDDEN ValueTable {
129     private:
130       DenseMap<Value*, uint32_t> valueNumbering;
131       DenseMap<Expression, uint32_t> expressionNumbering;
132   
133       uint32_t nextValueNumber;
134     
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);
146     public:
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);
151       void clear();
152       void erase(Value* v);
153       unsigned size();
154   };
155 }
156
157 namespace llvm {
158 template <> struct DenseMapKeyInfo<Expression> {
159   static inline Expression getEmptyKey() {
160     return Expression(Expression::EMPTY);
161   }
162   
163   static inline Expression getTombstoneKey() {
164     return Expression(Expression::TOMBSTONE);
165   }
166   
167   static unsigned getHashValue(const Expression e) {
168     unsigned hash = e.opcode;
169     
170     hash = e.firstVN + hash * 37;
171     hash = e.secondVN + hash * 37;
172     hash = e.thirdVN + hash * 37;
173     
174     hash = (unsigned)((uintptr_t)e.type >> 4) ^
175             (unsigned)((uintptr_t)e.type >> 9) +
176             hash * 37;
177     
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;
181     
182     return hash;
183   }
184   static bool isPod() { return true; }
185 };
186 }
187
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;
224     
225     // THIS SHOULD NEVER HAPPEN
226     default:
227       assert(0 && "Binary operator with unknown opcode?");
228       return Expression::ADD;
229   }
230 }
231
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;
255       
256       // THIS SHOULD NEVER HAPPEN
257       default:
258         assert(0 && "Comparison with unknown predicate?");
259         return Expression::ICMPEQ;
260     }
261   } else {
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;
291       
292       // THIS SHOULD NEVER HAPPEN
293       default:
294         assert(0 && "Comparison with unknown predicate?");
295         return Expression::FCMPOEQ;
296     }
297   }
298 }
299
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;
327     
328     // THIS SHOULD NEVER HAPPEN
329     default:
330       assert(0 && "Cast operator with unknown opcode?");
331       return Expression::BITCAST;
332   }
333 }
334
335 Expression ValueTable::create_expression(BinaryOperator* BO) {
336   Expression e;
337     
338   e.firstVN = lookup_or_add(BO->getOperand(0));
339   e.secondVN = lookup_or_add(BO->getOperand(1));
340   e.thirdVN = 0;
341   e.type = BO->getType();
342   e.opcode = getOpcode(BO);
343   
344   return e;
345 }
346
347 Expression ValueTable::create_expression(CmpInst* C) {
348   Expression e;
349     
350   e.firstVN = lookup_or_add(C->getOperand(0));
351   e.secondVN = lookup_or_add(C->getOperand(1));
352   e.thirdVN = 0;
353   e.type = C->getType();
354   e.opcode = getOpcode(C);
355   
356   return e;
357 }
358
359 Expression ValueTable::create_expression(CastInst* C) {
360   Expression e;
361     
362   e.firstVN = lookup_or_add(C->getOperand(0));
363   e.secondVN = 0;
364   e.thirdVN = 0;
365   e.type = C->getType();
366   e.opcode = getOpcode(C);
367   
368   return e;
369 }
370
371 Expression ValueTable::create_expression(ShuffleVectorInst* S) {
372   Expression e;
373     
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;
379   
380   return e;
381 }
382
383 Expression ValueTable::create_expression(ExtractElementInst* E) {
384   Expression e;
385     
386   e.firstVN = lookup_or_add(E->getOperand(0));
387   e.secondVN = lookup_or_add(E->getOperand(1));
388   e.thirdVN = 0;
389   e.type = E->getType();
390   e.opcode = Expression::EXTRACT;
391   
392   return e;
393 }
394
395 Expression ValueTable::create_expression(InsertElementInst* I) {
396   Expression e;
397     
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;
403   
404   return e;
405 }
406
407 Expression ValueTable::create_expression(SelectInst* I) {
408   Expression e;
409     
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;
415   
416   return e;
417 }
418
419 Expression ValueTable::create_expression(GetElementPtrInst* G) {
420   Expression e;
421     
422   e.firstVN = lookup_or_add(G->getPointerOperand());
423   e.secondVN = 0;
424   e.thirdVN = 0;
425   e.type = G->getType();
426   e.opcode = Expression::GEP;
427   
428   for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
429        I != E; ++I)
430     e.varargs.push_back(lookup_or_add(*I));
431   
432   return e;
433 }
434
435 //===----------------------------------------------------------------------===//
436 //                     ValueTable External Functions
437 //===----------------------------------------------------------------------===//
438
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())
444     return VI->second;
445   
446   
447   if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
448     Expression e = create_expression(BO);
449     
450     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
451     if (EI != expressionNumbering.end()) {
452       valueNumbering.insert(std::make_pair(V, EI->second));
453       return EI->second;
454     } else {
455       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
456       valueNumbering.insert(std::make_pair(V, nextValueNumber));
457       
458       return nextValueNumber++;
459     }
460   } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
461     Expression e = create_expression(C);
462     
463     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
464     if (EI != expressionNumbering.end()) {
465       valueNumbering.insert(std::make_pair(V, EI->second));
466       return EI->second;
467     } else {
468       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
469       valueNumbering.insert(std::make_pair(V, nextValueNumber));
470       
471       return nextValueNumber++;
472     }
473   } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
474     Expression e = create_expression(U);
475     
476     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
477     if (EI != expressionNumbering.end()) {
478       valueNumbering.insert(std::make_pair(V, EI->second));
479       return EI->second;
480     } else {
481       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
482       valueNumbering.insert(std::make_pair(V, nextValueNumber));
483       
484       return nextValueNumber++;
485     }
486   } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
487     Expression e = create_expression(U);
488     
489     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
490     if (EI != expressionNumbering.end()) {
491       valueNumbering.insert(std::make_pair(V, EI->second));
492       return EI->second;
493     } else {
494       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
495       valueNumbering.insert(std::make_pair(V, nextValueNumber));
496       
497       return nextValueNumber++;
498     }
499   } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
500     Expression e = create_expression(U);
501     
502     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
503     if (EI != expressionNumbering.end()) {
504       valueNumbering.insert(std::make_pair(V, EI->second));
505       return EI->second;
506     } else {
507       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
508       valueNumbering.insert(std::make_pair(V, nextValueNumber));
509       
510       return nextValueNumber++;
511     }
512   } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
513     Expression e = create_expression(U);
514     
515     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
516     if (EI != expressionNumbering.end()) {
517       valueNumbering.insert(std::make_pair(V, EI->second));
518       return EI->second;
519     } else {
520       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
521       valueNumbering.insert(std::make_pair(V, nextValueNumber));
522       
523       return nextValueNumber++;
524     }
525   } else if (CastInst* U = dyn_cast<CastInst>(V)) {
526     Expression e = create_expression(U);
527     
528     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
529     if (EI != expressionNumbering.end()) {
530       valueNumbering.insert(std::make_pair(V, EI->second));
531       return EI->second;
532     } else {
533       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
534       valueNumbering.insert(std::make_pair(V, nextValueNumber));
535       
536       return nextValueNumber++;
537     }
538   } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
539     Expression e = create_expression(U);
540     
541     DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
542     if (EI != expressionNumbering.end()) {
543       valueNumbering.insert(std::make_pair(V, EI->second));
544       return EI->second;
545     } else {
546       expressionNumbering.insert(std::make_pair(e, nextValueNumber));
547       valueNumbering.insert(std::make_pair(V, nextValueNumber));
548       
549       return nextValueNumber++;
550     }
551   } else {
552     valueNumbering.insert(std::make_pair(V, nextValueNumber));
553     return nextValueNumber++;
554   }
555 }
556
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())
562     return VI->second;
563   else
564     assert(0 && "Value not numbered?");
565   
566   return 0;
567 }
568
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));
576 }
577
578 /// clear - Remove all entries from the ValueTable
579 void ValueTable::clear() {
580   valueNumbering.clear();
581   expressionNumbering.clear();
582   nextValueNumber = 1;
583 }
584
585 /// erase - Remove a value from the value numbering
586 void ValueTable::erase(Value* V) {
587   valueNumbering.erase(V);
588 }
589
590 /// size - Return the number of assigned value numbers
591 unsigned ValueTable::size() {
592   // NOTE: zero is never assigned
593   return nextValueNumber;
594 }
595
596 //===----------------------------------------------------------------------===//
597 //                       ValueNumberedSet Class
598 //===----------------------------------------------------------------------===//
599
600 class ValueNumberedSet {
601   private:
602     SmallPtrSet<Value*, 8> contents;
603     BitVector numbers;
604   public:
605     ValueNumberedSet() { numbers.resize(1); }
606     ValueNumberedSet(const ValueNumberedSet& other) {
607       numbers = other.numbers;
608       contents = other.contents;
609     }
610     
611     typedef SmallPtrSet<Value*, 8>::iterator iterator;
612     
613     iterator begin() { return contents.begin(); }
614     iterator end() { return contents.end(); }
615     
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(); }
621     
622     void set(unsigned i)  {
623       if (i >= numbers.size())
624         numbers.resize(i+1);
625       
626       numbers.set(i);
627     }
628     
629     void operator=(const ValueNumberedSet& other) {
630       contents = other.contents;
631       numbers = other.numbers;
632     }
633     
634     void reset(unsigned i)  {
635       if (i < numbers.size())
636         numbers.reset(i);
637     }
638     
639     bool test(unsigned i)  {
640       if (i >= numbers.size())
641         return false;
642       
643       return numbers.test(i);
644     }
645     
646     void clear() {
647       contents.clear();
648       numbers.clear();
649     }
650 };
651
652 //===----------------------------------------------------------------------===//
653 //                         GVNPRE Pass
654 //===----------------------------------------------------------------------===//
655
656 namespace {
657
658   class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
659     bool runOnFunction(Function &F);
660   public:
661     static char ID; // Pass identification, replacement for typeid
662     GVNPRE() : FunctionPass((intptr_t)&ID) { }
663
664   private:
665     ValueTable VN;
666     SmallVector<Instruction*, 8> createdExpressions;
667     
668     DenseMap<BasicBlock*, ValueNumberedSet> availableOut;
669     DenseMap<BasicBlock*, ValueNumberedSet> anticipatedIn;
670     DenseMap<BasicBlock*, ValueNumberedSet> generatedPhis;
671     
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>();
678     }
679   
680     // Helper fuctions
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) ;
688     
689     void topo_sort(ValueNumberedSet& set,
690                    SmallVector<Value*, 8>& vec) ;
691     
692     void cleanup() ;
693     bool elimination() ;
694     
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) ;
712     
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) ;
720   
721   };
722   
723   char GVNPRE::ID = 0;
724   
725 }
726
727 // createGVNPREPass - The public interface to this file...
728 FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
729
730 static RegisterPass<GVNPRE> X("gvnpre",
731                       "Global Value Numbering/Partial Redundancy Elimination");
732
733
734 STATISTIC(NumInsertedVals, "Number of values inserted");
735 STATISTIC(NumInsertedPhis, "Number of PHI nodes inserted");
736 STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
737
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
740 /// is present
741 Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) {
742   if (!vals.test(v))
743     return 0;
744   
745   for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
746        I != E; ++I)
747     if (v == VN.lookup(*I))
748       return *I;
749   
750   assert(0 && "No leader found, but present bit is set?");
751   return 0;
752 }
753
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);
758   if (!s.test(num))
759     s.insert(v);
760 }
761
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;
766   
767   uint32_t num = VN.lookup(v);
768   Value* leader = find_leader(s, num);
769   if (leader != 0)
770     s.erase(leader);
771   s.insert(v);
772   s.set(num);
773 }
774
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) {
780   if (V == 0)
781     return 0;
782   
783   // Unary Operations
784   if (CastInst* U = dyn_cast<CastInst>(V)) {
785     Value* newOp1 = 0;
786     if (isa<Instruction>(U->getOperand(0)))
787       newOp1 = phi_translate(U->getOperand(0), pred, succ);
788     else
789       newOp1 = U->getOperand(0);
790     
791     if (newOp1 == 0)
792       return 0;
793     
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");
800       
801       uint32_t v = VN.lookup_or_add(newVal);
802       
803       Value* leader = find_leader(availableOut[pred], v);
804       if (leader == 0) {
805         createdExpressions.push_back(newVal);
806         return newVal;
807       } else {
808         VN.erase(newVal);
809         delete newVal;
810         return leader;
811       }
812     }
813   
814   // Binary Operations
815   } if (isa<BinaryOperator>(V) || isa<CmpInst>(V) || 
816       isa<ExtractElementInst>(V)) {
817     User* U = cast<User>(V);
818     
819     Value* newOp1 = 0;
820     if (isa<Instruction>(U->getOperand(0)))
821       newOp1 = phi_translate(U->getOperand(0), pred, succ);
822     else
823       newOp1 = U->getOperand(0);
824     
825     if (newOp1 == 0)
826       return 0;
827     
828     Value* newOp2 = 0;
829     if (isa<Instruction>(U->getOperand(1)))
830       newOp2 = phi_translate(U->getOperand(1), pred, succ);
831     else
832       newOp2 = U->getOperand(1);
833     
834     if (newOp2 == 0)
835       return 0;
836     
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(),
841                                         newOp1, newOp2,
842                                         BO->getName()+".expr");
843       else if (CmpInst* C = dyn_cast<CmpInst>(U))
844         newVal = CmpInst::create(C->getOpcode(),
845                                  C->getPredicate(),
846                                  newOp1, newOp2,
847                                  C->getName()+".expr");
848       else if (ExtractElementInst* E = dyn_cast<ExtractElementInst>(U))
849         newVal = new ExtractElementInst(newOp1, newOp2, E->getName()+".expr");
850       
851       uint32_t v = VN.lookup_or_add(newVal);
852       
853       Value* leader = find_leader(availableOut[pred], v);
854       if (leader == 0) {
855         createdExpressions.push_back(newVal);
856         return newVal;
857       } else {
858         VN.erase(newVal);
859         delete newVal;
860         return leader;
861       }
862     }
863   
864   // Ternary Operations
865   } else if (isa<ShuffleVectorInst>(V) || isa<InsertElementInst>(V) ||
866              isa<SelectInst>(V)) {
867     User* U = cast<User>(V);
868     
869     Value* newOp1 = 0;
870     if (isa<Instruction>(U->getOperand(0)))
871       newOp1 = phi_translate(U->getOperand(0), pred, succ);
872     else
873       newOp1 = U->getOperand(0);
874     
875     if (newOp1 == 0)
876       return 0;
877     
878     Value* newOp2 = 0;
879     if (isa<Instruction>(U->getOperand(1)))
880       newOp2 = phi_translate(U->getOperand(1), pred, succ);
881     else
882       newOp2 = U->getOperand(1);
883     
884     if (newOp2 == 0)
885       return 0;
886     
887     Value* newOp3 = 0;
888     if (isa<Instruction>(U->getOperand(2)))
889       newOp3 = phi_translate(U->getOperand(2), pred, succ);
890     else
891       newOp3 = U->getOperand(2);
892     
893     if (newOp3 == 0)
894       return 0;
895     
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");
908       
909       uint32_t v = VN.lookup_or_add(newVal);
910       
911       Value* leader = find_leader(availableOut[pred], v);
912       if (leader == 0) {
913         createdExpressions.push_back(newVal);
914         return newVal;
915       } else {
916         VN.erase(newVal);
917         delete newVal;
918         return leader;
919       }
920     }
921   
922   // Varargs operators
923   } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
924     Value* newOp1 = 0;
925     if (isa<Instruction>(U->getPointerOperand()))
926       newOp1 = phi_translate(U->getPointerOperand(), pred, succ);
927     else
928       newOp1 = U->getPointerOperand();
929     
930     if (newOp1 == 0)
931       return 0;
932     
933     bool changed_idx = false;
934     SmallVector<Value*, 4> newIdx;
935     for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
936          I != E; ++I)
937       if (isa<Instruction>(*I)) {
938         Value* newVal = phi_translate(*I, pred, succ);
939         newIdx.push_back(newVal);
940         if (newVal != *I)
941           changed_idx = true;
942       } else {
943         newIdx.push_back(*I);
944       }
945     
946     if (newOp1 != U->getPointerOperand() || changed_idx) {
947       Instruction* newVal = new GetElementPtrInst(newOp1,
948                                        newIdx.begin(), newIdx.end(),
949                                        U->getName()+".expr");
950       
951       uint32_t v = VN.lookup_or_add(newVal);
952       
953       Value* leader = find_leader(availableOut[pred], v);
954       if (leader == 0) {
955         createdExpressions.push_back(newVal);
956         return newVal;
957       } else {
958         VN.erase(newVal);
959         delete newVal;
960         return leader;
961       }
962     }
963   
964   // PHI Nodes
965   } else if (PHINode* P = dyn_cast<PHINode>(V)) {
966     if (P->getParent() == succ)
967       return P->getIncomingValueForBlock(pred);
968   }
969   
970   return V;
971 }
972
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))) {
981       out.insert(V);
982       out.set(VN.lookup(V));
983     }
984   }
985 }
986
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))
994         return true;
995     return false;
996   } else {
997     return false;
998   }
999 }
1000
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 
1003 /// above)
1004 void GVNPRE::clean(ValueNumberedSet& set) {
1005   SmallVector<Value*, 8> worklist;
1006   worklist.reserve(set.size());
1007   topo_sort(set, worklist);
1008   
1009   for (unsigned i = 0; i < worklist.size(); ++i) {
1010     Value* v = worklist[i];
1011     
1012     // Handle unary ops
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)));
1016       if (lhsValid)
1017         lhsValid = !dependsOnInvoke(U->getOperand(0));
1018       
1019       if (!lhsValid) {
1020         set.erase(U);
1021         set.reset(VN.lookup(U));
1022       }
1023     
1024     // Handle binary ops
1025     } else if (isa<BinaryOperator>(v) || isa<CmpInst>(v) ||
1026         isa<ExtractElementInst>(v)) {
1027       User* U = cast<User>(v);
1028       
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       bool rhsValid = !isa<Instruction>(U->getOperand(1));
1035       rhsValid |= set.test(VN.lookup(U->getOperand(1)));
1036       if (rhsValid)
1037         rhsValid = !dependsOnInvoke(U->getOperand(1));
1038       
1039       if (!lhsValid || !rhsValid) {
1040         set.erase(U);
1041         set.reset(VN.lookup(U));
1042       }
1043     
1044     // Handle ternary ops
1045     } else if (isa<ShuffleVectorInst>(v) || isa<InsertElementInst>(v) ||
1046                isa<SelectInst>(v)) {
1047       User* U = cast<User>(v);
1048     
1049       bool lhsValid = !isa<Instruction>(U->getOperand(0));
1050       lhsValid |= set.test(VN.lookup(U->getOperand(0)));
1051       if (lhsValid)
1052         lhsValid = !dependsOnInvoke(U->getOperand(0));
1053       
1054       bool rhsValid = !isa<Instruction>(U->getOperand(1));
1055       rhsValid |= set.test(VN.lookup(U->getOperand(1)));
1056       if (rhsValid)
1057         rhsValid = !dependsOnInvoke(U->getOperand(1));
1058       
1059       bool thirdValid = !isa<Instruction>(U->getOperand(2));
1060       thirdValid |= set.test(VN.lookup(U->getOperand(2)));
1061       if (thirdValid)
1062         thirdValid = !dependsOnInvoke(U->getOperand(2));
1063     
1064       if (!lhsValid || !rhsValid || !thirdValid) {
1065         set.erase(U);
1066         set.reset(VN.lookup(U));
1067       }
1068     
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()));
1073       if (ptrValid)
1074         ptrValid = !dependsOnInvoke(U->getPointerOperand());
1075       
1076       bool varValid = true;
1077       for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
1078            I != E; ++I)
1079         if (varValid) {
1080           varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I));
1081           varValid &= !dependsOnInvoke(*I);
1082         }
1083     
1084       if (!ptrValid || !varValid) {
1085         set.erase(U);
1086         set.reset(VN.lookup(U));
1087       }
1088     }
1089   }
1090 }
1091
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();
1098        I != E; ++I) {
1099     if (visited.count(*I) == 0)
1100       stack.push_back(*I);
1101     
1102     while (!stack.empty()) {
1103       Value* e = stack.back();
1104       
1105       // Handle unary ops
1106       if (CastInst* U = dyn_cast<CastInst>(e)) {
1107         Value* l = find_leader(set, VN.lookup(U->getOperand(0)));
1108     
1109         if (l != 0 && isa<Instruction>(l) &&
1110             visited.count(l) == 0)
1111           stack.push_back(l);
1112         else {
1113           vec.push_back(e);
1114           visited.insert(e);
1115           stack.pop_back();
1116         }
1117       
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)));
1124     
1125         if (l != 0 && isa<Instruction>(l) &&
1126             visited.count(l) == 0)
1127           stack.push_back(l);
1128         else if (r != 0 && isa<Instruction>(r) &&
1129                  visited.count(r) == 0)
1130           stack.push_back(r);
1131         else {
1132           vec.push_back(e);
1133           visited.insert(e);
1134           stack.pop_back();
1135         }
1136       
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)));
1144       
1145         if (l != 0 && isa<Instruction>(l) &&
1146             visited.count(l) == 0)
1147           stack.push_back(l);
1148         else if (r != 0 && isa<Instruction>(r) &&
1149                  visited.count(r) == 0)
1150           stack.push_back(r);
1151         else if (m != 0 && isa<Instruction>(m) &&
1152                  visited.count(m) == 0)
1153           stack.push_back(m);
1154         else {
1155           vec.push_back(e);
1156           visited.insert(e);
1157           stack.pop_back();
1158         }
1159       
1160       // Handle vararg ops
1161       } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(e)) {
1162         Value* p = find_leader(set, VN.lookup(U->getPointerOperand()));
1163         
1164         if (p != 0 && isa<Instruction>(p) &&
1165             visited.count(p) == 0)
1166           stack.push_back(p);
1167         else {
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) {
1173               stack.push_back(v);
1174               push_va = true;
1175             }
1176           }
1177           
1178           if (!push_va) {
1179             vec.push_back(e);
1180             visited.insert(e);
1181             stack.pop_back();
1182           }
1183         }
1184       
1185       // Handle opaque ops
1186       } else {
1187         visited.insert(e);
1188         vec.push_back(e);
1189         stack.pop_back();
1190       }
1191     }
1192     
1193     stack.clear();
1194   }
1195 }
1196
1197 /// dump - Dump a set of values to standard error
1198 void GVNPRE::dump(ValueNumberedSet& s) const {
1199   DOUT << "{ ";
1200   for (ValueNumberedSet::iterator I = s.begin(), E = s.end();
1201        I != E; ++I) {
1202     DOUT << "" << VN.lookup(*I) << ": ";
1203     DEBUG((*I)->dump());
1204   }
1205   DOUT << "}\n\n";
1206 }
1207
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;
1213   
1214   SmallVector<std::pair<Instruction*, Value*>, 8> replace;
1215   SmallVector<Instruction*, 8> erase;
1216   
1217   DominatorTree& DT = getAnalysis<DominatorTree>();
1218   
1219   for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1220          E = df_end(DT.getRootNode()); DI != E; ++DI) {
1221     BasicBlock* BB = DI->getBlock();
1222     
1223     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1224          BI != BE; ++BI) {
1225
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)) {
1230         
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);
1238               ++NumEliminated;
1239             }
1240         }
1241       }
1242     }
1243   }
1244   
1245   while (!replace.empty()) {
1246     std::pair<Instruction*, Value*> rep = replace.back();
1247     replace.pop_back();
1248     rep.first->replaceAllUsesWith(rep.second);
1249     changed_function = true;
1250   }
1251     
1252   for (SmallVector<Instruction*, 8>::iterator I = erase.begin(),
1253        E = erase.end(); I != E; ++I)
1254      (*I)->eraseFromParent();
1255   
1256   return changed_function;
1257 }
1258
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();
1265     
1266     delete I;
1267   }
1268 }
1269
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) {
1277   // Handle PHI nodes
1278   if (PHINode* p = dyn_cast<PHINode>(I)) {
1279     unsigned num = VN.lookup_or_add(p);
1280     
1281     currPhis.insert(p);
1282     currPhis.set(num);
1283   
1284   // Handle unary ops
1285   } else if (CastInst* U = dyn_cast<CastInst>(I)) {
1286     Value* leftValue = U->getOperand(0);
1287     
1288     unsigned num = VN.lookup_or_add(U);
1289       
1290     if (isa<Instruction>(leftValue))
1291       if (!currExps.test(VN.lookup(leftValue))) {
1292         currExps.insert(leftValue);
1293         currExps.set(VN.lookup(leftValue));
1294       }
1295     
1296     if (!currExps.test(num)) {
1297       currExps.insert(U);
1298       currExps.set(num);
1299     }
1300   
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);
1307     
1308     unsigned num = VN.lookup_or_add(U);
1309       
1310     if (isa<Instruction>(leftValue))
1311       if (!currExps.test(VN.lookup(leftValue))) {
1312         currExps.insert(leftValue);
1313         currExps.set(VN.lookup(leftValue));
1314       }
1315     
1316     if (isa<Instruction>(rightValue))
1317       if (!currExps.test(VN.lookup(rightValue))) {
1318         currExps.insert(rightValue);
1319         currExps.set(VN.lookup(rightValue));
1320       }
1321     
1322     if (!currExps.test(num)) {
1323       currExps.insert(U);
1324       currExps.set(num);
1325     }
1326     
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);
1334       
1335     VN.lookup_or_add(U);
1336     
1337     unsigned num = VN.lookup_or_add(U);
1338     
1339     if (isa<Instruction>(leftValue))
1340       if (!currExps.test(VN.lookup(leftValue))) {
1341         currExps.insert(leftValue);
1342         currExps.set(VN.lookup(leftValue));
1343       }
1344     if (isa<Instruction>(rightValue))
1345       if (!currExps.test(VN.lookup(rightValue))) {
1346         currExps.insert(rightValue);
1347         currExps.set(VN.lookup(rightValue));
1348       }
1349     if (isa<Instruction>(thirdValue))
1350       if (!currExps.test(VN.lookup(thirdValue))) {
1351         currExps.insert(thirdValue);
1352         currExps.set(VN.lookup(thirdValue));
1353       }
1354     
1355     if (!currExps.test(num)) {
1356       currExps.insert(U);
1357       currExps.set(num);
1358     }
1359     
1360   // Handle vararg ops
1361   } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(I)) {
1362     Value* ptrValue = U->getPointerOperand();
1363       
1364     VN.lookup_or_add(U);
1365     
1366     unsigned num = VN.lookup_or_add(U);
1367     
1368     if (isa<Instruction>(ptrValue))
1369       if (!currExps.test(VN.lookup(ptrValue))) {
1370         currExps.insert(ptrValue);
1371         currExps.set(VN.lookup(ptrValue));
1372       }
1373     
1374     for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end();
1375          OI != OE; ++OI)
1376       if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) {
1377         currExps.insert(*OI);
1378         currExps.set(VN.lookup(*OI));
1379       }
1380     
1381     if (!currExps.test(VN.lookup(U))) {
1382       currExps.insert(U);
1383       currExps.set(num);
1384     }
1385     
1386   // Handle opaque ops
1387   } else if (!I->isTerminator()){
1388     VN.lookup_or_add(I);
1389     
1390     currTemps.insert(I);
1391   }
1392     
1393   if (!I->isTerminator())
1394     if (!currAvail.test(VN.lookup(I))) {
1395       currAvail.insert(I);
1396       currAvail.set(VN.lookup(I));
1397     }
1398 }
1399
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) {
1408       return true;
1409     }
1410     else {
1411       phi_translate_set(anticipatedIn[BB->getTerminator()->getSuccessor(0)],
1412                         BB,  BB->getTerminator()->getSuccessor(0), anticOut);
1413     }
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));
1420     }
1421     
1422     for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
1423       BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
1424       ValueNumberedSet& succAnticIn = anticipatedIn[currSucc];
1425       
1426       SmallVector<Value*, 16> temp;
1427       
1428       for (ValueNumberedSet::iterator I = anticOut.begin(),
1429            E = anticOut.end(); I != E; ++I)
1430         if (!succAnticIn.test(VN.lookup(*I)))
1431           temp.push_back(*I);
1432
1433       for (SmallVector<Value*, 16>::iterator I = temp.begin(), E = temp.end();
1434            I != E; ++I) {
1435         anticOut.erase(*I);
1436         anticOut.reset(VN.lookup(*I));
1437       }
1438     }
1439   }
1440   
1441   return false;
1442 }
1443
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();
1454       
1455   bool defer = buildsets_anticout(BB, anticOut, visited);
1456   if (defer)
1457     return 0;
1458   
1459   anticIn.clear();
1460   
1461   for (ValueNumberedSet::iterator I = anticOut.begin(),
1462        E = anticOut.end(); I != E; ++I) {
1463     anticIn.insert(*I);
1464     anticIn.set(VN.lookup(*I));
1465   }
1466   for (ValueNumberedSet::iterator I = currExps.begin(),
1467        E = currExps.end(); I != E; ++I) {
1468     if (!anticIn.test(VN.lookup(*I))) {
1469       anticIn.insert(*I);
1470       anticIn.set(VN.lookup(*I));
1471     }
1472   } 
1473   
1474   for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
1475        E = currTemps.end(); I != E; ++I) {
1476     anticIn.erase(*I);
1477     anticIn.reset(VN.lookup(*I));
1478   }
1479   
1480   clean(anticIn);
1481   anticOut.clear();
1482   
1483   if (old != anticIn.size())
1484     return 2;
1485   else
1486     return 1;
1487 }
1488
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;
1494
1495   DominatorTree &DT = getAnalysis<DominatorTree>();   
1496   
1497   // Phase 1, Part 1: calculate AVAIL_OUT
1498   
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) {
1502     
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()];     
1508     
1509     BasicBlock* BB = DI->getBlock();
1510   
1511     // A block inherits AVAIL_OUT from its dominator
1512     if (DI->getIDom() != 0)
1513       currAvail = availableOut[DI->getIDom()->getBlock()];
1514
1515     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1516          BI != BE; ++BI)
1517       buildsets_availout(BI, currAvail, currPhis, currExps,
1518                          currTemps);
1519       
1520   }
1521
1522   // Phase 1, Part 2: calculate ANTIC_IN
1523   
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);
1528   
1529   bool changed = true;
1530   unsigned iterations = 0;
1531   
1532   while (changed) {
1533     changed = false;
1534     ValueNumberedSet anticOut;
1535     
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;
1540       
1541       if (block_changed.count(BB) != 0) {
1542         unsigned ret = buildsets_anticin(BB, anticOut,generatedExpressions[BB],
1543                                          generatedTemporaries[BB], visited);
1544       
1545         if (ret == 0) {
1546           changed = true;
1547           continue;
1548         } else {
1549           visited.insert(BB);
1550         
1551           if (ret == 2)
1552            for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1553                  PI != PE; ++PI) {
1554               block_changed.insert(*PI);
1555            }
1556           else
1557             block_changed.erase(BB);
1558         
1559           changed |= (ret == 2);
1560         }
1561       }
1562     }
1563     
1564     iterations++;
1565   }
1566 }
1567
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
1570 /// the main block
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);
1578       
1579       Value* s1 = 0;
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)));
1589       else
1590         s1 = U->getOperand(0);
1591       
1592       Value* s2 = 0;
1593       
1594       if (isa<BinaryOperator>(U) || 
1595           isa<CmpInst>(U) ||
1596           isa<ShuffleVectorInst>(U) ||
1597           isa<ExtractElementInst>(U) ||
1598           isa<InsertElementInst>(U) ||
1599           isa<SelectInst>(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)));
1609         } else {
1610           s2 = U->getOperand(1);
1611         }
1612       
1613       // Ternary Operators
1614       Value* s3 = 0;
1615       if (isa<ShuffleVectorInst>(U) ||
1616           isa<InsertElementInst>(U) ||
1617           isa<SelectInst>(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)));
1627         } else {
1628           s3 = U->getOperand(2);
1629         }
1630       
1631       // Vararg operators
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], 
1645                                VN.lookup(*OI)));
1646           } else {
1647             sVarargs.push_back(*OI);
1648           }
1649         }
1650       }
1651       
1652       Value* newVal = 0;
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.begin(), sVarargs.end(), 
1679                                        G->getName()+".gvnpre", 
1680                                        (*PI)->getTerminator());
1681                                 
1682                   
1683       VN.add(newVal, VN.lookup(U));
1684                   
1685       ValueNumberedSet& predAvail = availableOut[*PI];
1686       val_replace(predAvail, newVal);
1687       val_replace(new_sets[*PI], newVal);
1688       predAvail.set(VN.lookup(newVal));
1689             
1690       DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1691       if (av != avail.end())
1692         avail.erase(av);
1693       avail.insert(std::make_pair(*PI, newVal));
1694                   
1695       ++NumInsertedVals;
1696     }
1697   }
1698               
1699   PHINode* p = 0;
1700               
1701   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
1702     if (p == 0)
1703       p = new PHINode(avail[*PI]->getType(), "gvnpre-join", BB->begin());
1704     
1705     p->addIncoming(avail[*PI], *PI);
1706   }
1707
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));
1715               
1716   ++NumInsertedPhis;
1717 }
1718
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;
1726   
1727   BasicBlock* BB = D->getBlock();
1728   for (unsigned i = 0; i < workList.size(); ++i) {
1729     Value* e = workList[i];
1730           
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)))
1736         continue;
1737             
1738       DenseMap<BasicBlock*, Value*> avail;
1739       bool by_some = false;
1740       bool all_same = true;
1741       Value * first_s = 0;
1742             
1743       for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;
1744            ++PI) {
1745         Value *e2 = phi_translate(e, *PI, BB);
1746         Value *e3 = find_leader(availableOut[*PI], VN.lookup(e2));
1747               
1748         if (e3 == 0) {
1749           DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1750           if (av != avail.end())
1751             avail.erase(av);
1752           avail.insert(std::make_pair(*PI, e2));
1753           all_same = false;
1754         } else {
1755           DenseMap<BasicBlock*, Value*>::iterator av = avail.find(*PI);
1756           if (av != avail.end())
1757             avail.erase(av);
1758           avail.insert(std::make_pair(*PI, e3));
1759                 
1760           by_some = true;
1761           if (first_s == 0)
1762             first_s = e3;
1763           else if (first_s != e3)
1764             all_same = false;
1765         }
1766       }
1767             
1768       if (by_some && !all_same &&
1769           !generatedPhis[BB].test(VN.lookup(e))) {
1770         insertion_pre(e, BB, avail, new_sets);
1771               
1772         changed_function = true;
1773         new_stuff = true;
1774       }
1775     }
1776   }
1777   
1778   unsigned retval = 0;
1779   if (changed_function)
1780     retval += 1;
1781   if (new_stuff)
1782     retval += 2;
1783   
1784   return retval;
1785 }
1786
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;
1792
1793   DominatorTree &DT = getAnalysis<DominatorTree>();  
1794   
1795   std::map<BasicBlock*, ValueNumberedSet> new_sets;
1796   bool new_stuff = true;
1797   while (new_stuff) {
1798     new_stuff = false;
1799     for (df_iterator<DomTreeNode*> DI = df_begin(DT.getRootNode()),
1800          E = df_end(DT.getRootNode()); DI != E; ++DI) {
1801       BasicBlock* BB = DI->getBlock();
1802       
1803       if (BB == 0)
1804         continue;
1805       
1806       ValueNumberedSet& availOut = availableOut[BB];
1807       ValueNumberedSet& anticIn = anticipatedIn[BB];
1808       
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);
1816         }
1817       }
1818       
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);
1824         
1825         unsigned result = insertion_mergepoint(workList, DI, new_sets);
1826         if (result & 1)
1827           changed_function = true;
1828         if (result & 2)
1829           new_stuff = true;
1830       }
1831     }
1832   }
1833   
1834   return changed_function;
1835 }
1836
1837 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1838 // function.
1839 //
1840 bool GVNPRE::runOnFunction(Function &F) {
1841   // Clean out global sets from any previous functions
1842   VN.clear();
1843   createdExpressions.clear();
1844   availableOut.clear();
1845   anticipatedIn.clear();
1846   generatedPhis.clear();
1847  
1848   bool changed_function = false;
1849   
1850   // Phase 1: BuildSets
1851   // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1852   buildsets(F);
1853   
1854   // Phase 2: Insert
1855   // This phase inserts values to make partially redundant values
1856   // fully redundant
1857   changed_function |= insertion(F);
1858   
1859   // Phase 3: Eliminate
1860   // This phase performs trivial full redundancy elimination
1861   changed_function |= elimination();
1862   
1863   // Phase 4: Cleanup
1864   // This phase cleans up values that were created solely
1865   // as leaders for expressions
1866   cleanup();
1867   
1868   return changed_function;
1869 }