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