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