Start using PMStack. Now each pass is responsibe for assinging
[oota-llvm.git] / lib / Transforms / Scalar / PredicateSimplifier.cpp
1 //===-- PredicateSimplifier.cpp - Path Sensitive Simplifier ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Nick Lewycky and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Path-sensitive optimizer. In a branch where x == y, replace uses of
11 // x with y. Permits further optimization, such as the elimination of
12 // the unreachable call:
13 //
14 // void test(int *p, int *q)
15 // {
16 //   if (p != q)
17 //     return;
18 // 
19 //   if (*p != *q)
20 //     foo(); // unreachable
21 // }
22 //
23 //===----------------------------------------------------------------------===//
24 //
25 // This pass focusses on four properties; equals, not equals, less-than
26 // and less-than-or-equals-to. The greater-than forms are also held just
27 // to allow walking from a lesser node to a greater one. These properties
28 // are stored in a lattice; LE can become LT or EQ, NE can become LT or GT.
29 //
30 // These relationships define a graph between values of the same type. Each
31 // Value is stored in a map table that retrieves the associated Node. This
32 // is how EQ relationships are stored; the map contains pointers to the
33 // same node. The node contains a most canonical Value* form and the list of
34 // known relationships.
35 //
36 // If two nodes are known to be inequal, then they will contain pointers to
37 // each other with an "NE" relationship. If node getNode(%x) is less than
38 // getNode(%y), then the %x node will contain <%y, GT> and %y will contain
39 // <%x, LT>. This allows us to tie nodes together into a graph like this:
40 //
41 //   %a < %b < %c < %d
42 //
43 // with four nodes representing the properties. The InequalityGraph provides
44 // querying with "isRelatedBy" and mutators "addEquality" and "addInequality".
45 // To find a relationship, we start with one of the nodes any binary search
46 // through its list to find where the relationships with the second node start.
47 // Then we iterate through those to find the first relationship that dominates
48 // our context node.
49 //
50 // To create these properties, we wait until a branch or switch instruction
51 // implies that a particular value is true (or false). The VRPSolver is
52 // responsible for analyzing the variable and seeing what new inferences
53 // can be made from each property. For example:
54 //
55 //   %P = seteq int* %ptr, null
56 //   %a = or bool %P, %Q
57 //   br bool %a label %cond_true, label %cond_false
58 //
59 // For the true branch, the VRPSolver will start with %a EQ true and look at
60 // the definition of %a and find that it can infer that %P and %Q are both
61 // true. From %P being true, it can infer that %ptr NE null. For the false
62 // branch it can't infer anything from the "or" instruction.
63 //
64 // Besides branches, we can also infer properties from instruction that may
65 // have undefined behaviour in certain cases. For example, the dividend of
66 // a division may never be zero. After the division instruction, we may assume
67 // that the dividend is not equal to zero.
68 //
69 //===----------------------------------------------------------------------===//
70
71 #define DEBUG_TYPE "predsimplify"
72 #include "llvm/Transforms/Scalar.h"
73 #include "llvm/Constants.h"
74 #include "llvm/DerivedTypes.h"
75 #include "llvm/Instructions.h"
76 #include "llvm/Pass.h"
77 #include "llvm/ADT/DepthFirstIterator.h"
78 #include "llvm/ADT/SetOperations.h"
79 #include "llvm/ADT/SmallVector.h"
80 #include "llvm/ADT/Statistic.h"
81 #include "llvm/ADT/STLExtras.h"
82 #include "llvm/Analysis/Dominators.h"
83 #include "llvm/Analysis/ET-Forest.h"
84 #include "llvm/Support/CFG.h"
85 #include "llvm/Support/Compiler.h"
86 #include "llvm/Support/Debug.h"
87 #include "llvm/Support/InstVisitor.h"
88 #include "llvm/Transforms/Utils/Local.h"
89 #include <algorithm>
90 #include <deque>
91 #include <sstream>
92 using namespace llvm;
93
94 STATISTIC(NumVarsReplaced, "Number of argument substitutions");
95 STATISTIC(NumInstruction , "Number of instructions removed");
96 STATISTIC(NumSimple      , "Number of simple replacements");
97 STATISTIC(NumBlocks      , "Number of blocks marked unreachable");
98
99 namespace {
100   // SLT SGT ULT UGT EQ
101   //   0   1   0   1  0 -- GT                  10
102   //   0   1   0   1  1 -- GE                  11
103   //   0   1   1   0  0 -- SGTULT              12
104   //   0   1   1   0  1 -- SGEULE              13
105   //   0   1   1   1  0 -- SGTUNE              14
106   //   0   1   1   1  1 -- SGEUANY             15
107   //   1   0   0   1  0 -- SLTUGT              18
108   //   1   0   0   1  1 -- SLEUGE              19
109   //   1   0   1   0  0 -- LT                  20
110   //   1   0   1   0  1 -- LE                  21
111   //   1   0   1   1  0 -- SLTUNE              22
112   //   1   0   1   1  1 -- SLEUANY             23
113   //   1   1   0   1  0 -- SNEUGT              26
114   //   1   1   0   1  1 -- SANYUGE             27
115   //   1   1   1   0  0 -- SNEULT              28
116   //   1   1   1   0  1 -- SANYULE             29
117   //   1   1   1   1  0 -- NE                  30
118   enum LatticeBits {
119     EQ_BIT = 1, UGT_BIT = 2, ULT_BIT = 4, SGT_BIT = 8, SLT_BIT = 16
120   };
121   enum LatticeVal {
122     GT = SGT_BIT | UGT_BIT,
123     GE = GT | EQ_BIT,
124     LT = SLT_BIT | ULT_BIT,
125     LE = LT | EQ_BIT,
126     NE = SLT_BIT | SGT_BIT | ULT_BIT | UGT_BIT,
127     SGTULT = SGT_BIT | ULT_BIT,
128     SGEULE = SGTULT | EQ_BIT,
129     SLTUGT = SLT_BIT | UGT_BIT,
130     SLEUGE = SLTUGT | EQ_BIT,
131     SNEULT = SLT_BIT | SGT_BIT | ULT_BIT,
132     SNEUGT = SLT_BIT | SGT_BIT | UGT_BIT,
133     SLTUNE = SLT_BIT | ULT_BIT | UGT_BIT,
134     SGTUNE = SGT_BIT | ULT_BIT | UGT_BIT,
135     SLEUANY = SLT_BIT | ULT_BIT | UGT_BIT | EQ_BIT,
136     SGEUANY = SGT_BIT | ULT_BIT | UGT_BIT | EQ_BIT,
137     SANYULE = SLT_BIT | SGT_BIT | ULT_BIT | EQ_BIT,
138     SANYUGE = SLT_BIT | SGT_BIT | UGT_BIT | EQ_BIT
139   };
140
141   static bool validPredicate(LatticeVal LV) {
142     switch (LV) {
143     case GT: case GE: case LT: case LE: case NE:
144     case SGTULT: case SGTUNE: case SGEULE:
145     case SLTUGT: case SLTUNE: case SLEUGE:
146     case SNEULT: case SNEUGT:
147     case SLEUANY: case SGEUANY: case SANYULE: case SANYUGE:
148       return true;
149     default:
150       return false;
151     }
152   }
153
154   /// reversePredicate - reverse the direction of the inequality
155   static LatticeVal reversePredicate(LatticeVal LV) {
156     unsigned reverse = LV ^ (SLT_BIT|SGT_BIT|ULT_BIT|UGT_BIT); //preserve EQ_BIT
157     if ((reverse & (SLT_BIT|SGT_BIT)) == 0)
158       reverse |= (SLT_BIT|SGT_BIT);
159
160     if ((reverse & (ULT_BIT|UGT_BIT)) == 0)
161       reverse |= (ULT_BIT|UGT_BIT);
162
163     LatticeVal Rev = static_cast<LatticeVal>(reverse);
164     assert(validPredicate(Rev) && "Failed reversing predicate.");
165     return Rev;
166   }
167
168   /// The InequalityGraph stores the relationships between values.
169   /// Each Value in the graph is assigned to a Node. Nodes are pointer
170   /// comparable for equality. The caller is expected to maintain the logical
171   /// consistency of the system.
172   ///
173   /// The InequalityGraph class may invalidate Node*s after any mutator call.
174   /// @brief The InequalityGraph stores the relationships between values.
175   class VISIBILITY_HIDDEN InequalityGraph {
176     ETNode *TreeRoot;
177
178     InequalityGraph();                  // DO NOT IMPLEMENT
179     InequalityGraph(InequalityGraph &); // DO NOT IMPLEMENT
180   public:
181     explicit InequalityGraph(ETNode *TreeRoot) : TreeRoot(TreeRoot) {}
182
183     class Node;
184
185     /// This is a StrictWeakOrdering predicate that sorts ETNodes by how many
186     /// children they have. With this, you can iterate through a list sorted by
187     /// this operation and the first matching entry is the most specific match
188     /// for your basic block. The order provided is total; ETNodes with the
189     /// same number of children are sorted by pointer address.
190     struct VISIBILITY_HIDDEN OrderByDominance {
191       bool operator()(const ETNode *LHS, const ETNode *RHS) const {
192         unsigned LHS_spread = LHS->getDFSNumOut() - LHS->getDFSNumIn();
193         unsigned RHS_spread = RHS->getDFSNumOut() - RHS->getDFSNumIn();
194         if (LHS_spread != RHS_spread) return LHS_spread < RHS_spread;
195         else return LHS < RHS;
196       }
197     };
198
199     /// An Edge is contained inside a Node making one end of the edge implicit
200     /// and contains a pointer to the other end. The edge contains a lattice
201     /// value specifying the relationship between the two nodes. Further, there
202     /// is an ETNode specifying which subtree of the dominator the edge applies.
203     class VISIBILITY_HIDDEN Edge {
204     public:
205       Edge(unsigned T, LatticeVal V, ETNode *ST)
206         : To(T), LV(V), Subtree(ST) {}
207
208       unsigned To;
209       LatticeVal LV;
210       ETNode *Subtree;
211
212       bool operator<(const Edge &edge) const {
213         if (To != edge.To) return To < edge.To;
214         else return OrderByDominance()(Subtree, edge.Subtree);
215       }
216       bool operator<(unsigned to) const {
217         return To < to;
218       }
219     };
220
221     /// A single node in the InequalityGraph. This stores the canonical Value
222     /// for the node, as well as the relationships with the neighbours.
223     ///
224     /// Because the lists are intended to be used for traversal, it is invalid
225     /// for the node to list itself in LessEqual or GreaterEqual lists. The
226     /// fact that a node is equal to itself is implied, and may be checked
227     /// with pointer comparison.
228     /// @brief A single node in the InequalityGraph.
229     class VISIBILITY_HIDDEN Node {
230       friend class InequalityGraph;
231
232       typedef SmallVector<Edge, 4> RelationsType;
233       RelationsType Relations;
234
235       Value *Canonical;
236
237       // TODO: can this idea improve performance?
238       //friend class std::vector<Node>;
239       //Node(Node &N) { RelationsType.swap(N.RelationsType); }
240
241     public:
242       typedef RelationsType::iterator       iterator;
243       typedef RelationsType::const_iterator const_iterator;
244
245       Node(Value *V) : Canonical(V) {}
246
247     private:
248 #ifndef NDEBUG
249     public:
250       virtual ~Node() {}
251       virtual void dump() const {
252         dump(*cerr.stream());
253       }
254     private:
255       void dump(std::ostream &os) const  {
256         os << *getValue() << ":\n";
257         for (Node::const_iterator NI = begin(), NE = end(); NI != NE; ++NI) {
258           static const std::string names[32] =
259             { "000000", "000001", "000002", "000003", "000004", "000005",
260               "000006", "000007", "000008", "000009", "     >", "    >=",
261               "  s>u<", "s>=u<=", "    s>", "   s>=", "000016", "000017",
262               "  s<u>", "s<=u>=", "     <", "    <=", "    s<", "   s<=",
263               "000024", "000025", "    u>", "   u>=", "    u<", "   u<=",
264               "    !=", "000031" };
265           os << "  " << names[NI->LV] << " " << NI->To
266              << "(" << NI->Subtree << ")\n";
267         }
268       }
269 #endif
270
271     public:
272       iterator begin()             { return Relations.begin(); }
273       iterator end()               { return Relations.end();   }
274       const_iterator begin() const { return Relations.begin(); }
275       const_iterator end()   const { return Relations.end();   }
276
277       iterator find(unsigned n, ETNode *Subtree) {
278         iterator E = end();
279         for (iterator I = std::lower_bound(begin(), E, n);
280              I != E && I->To == n; ++I) {
281           if (Subtree->DominatedBy(I->Subtree))
282             return I;
283         }
284         return E;
285       }
286
287       const_iterator find(unsigned n, ETNode *Subtree) const {
288         const_iterator E = end();
289         for (const_iterator I = std::lower_bound(begin(), E, n);
290              I != E && I->To == n; ++I) {
291           if (Subtree->DominatedBy(I->Subtree))
292             return I;
293         }
294         return E;
295       }
296
297       Value *getValue() const
298       {
299         return Canonical;
300       }
301
302       /// Updates the lattice value for a given node. Create a new entry if
303       /// one doesn't exist, otherwise it merges the values. The new lattice
304       /// value must not be inconsistent with any previously existing value.
305       void update(unsigned n, LatticeVal R, ETNode *Subtree) {
306         assert(validPredicate(R) && "Invalid predicate.");
307         iterator I = find(n, Subtree);
308         if (I == end()) {
309           Edge edge(n, R, Subtree);
310           iterator Insert = std::lower_bound(begin(), end(), edge);
311           Relations.insert(Insert, edge);
312         } else {
313           LatticeVal LV = static_cast<LatticeVal>(I->LV & R);
314           assert(validPredicate(LV) && "Invalid union of lattice values.");
315           if (LV != I->LV) {
316             if (Subtree == I->Subtree)
317               I->LV = LV;
318             else {
319               assert(Subtree->DominatedBy(I->Subtree) &&
320                      "Find returned subtree that doesn't apply.");
321
322               Edge edge(n, R, Subtree);
323               iterator Insert = std::lower_bound(begin(), end(), edge);
324               Relations.insert(Insert, edge);
325             }
326           }
327         }
328       }
329     };
330
331   private:
332     struct VISIBILITY_HIDDEN NodeMapEdge {
333       Value *V;
334       unsigned index;
335       ETNode *Subtree;
336
337       NodeMapEdge(Value *V, unsigned index, ETNode *Subtree)
338         : V(V), index(index), Subtree(Subtree) {}
339
340       bool operator==(const NodeMapEdge &RHS) const {
341         return V == RHS.V &&
342                Subtree == RHS.Subtree;
343       }
344
345       bool operator<(const NodeMapEdge &RHS) const {
346         if (V != RHS.V) return V < RHS.V;
347         return OrderByDominance()(Subtree, RHS.Subtree);
348       }
349
350       bool operator<(Value *RHS) const {
351         return V < RHS;
352       }
353     };
354
355     typedef std::vector<NodeMapEdge> NodeMapType;
356     NodeMapType NodeMap;
357
358     std::vector<Node> Nodes;
359
360     std::vector<std::pair<ConstantInt *, unsigned> > Constants;
361     void initializeConstant(Constant *C, unsigned index) {
362       ConstantInt *CI = dyn_cast<ConstantInt>(C);
363       if (!CI) return;
364
365       // XXX: instead of O(n) calls to addInequality, just find the 2, 3 or 4
366       // nodes that are nearest less than or greater than (signed or unsigned).
367       for (std::vector<std::pair<ConstantInt *, unsigned> >::iterator
368            I = Constants.begin(), E = Constants.end(); I != E; ++I) {
369         ConstantInt *Other = I->first;
370         if (CI->getType() == Other->getType()) {
371           unsigned lv = 0;
372
373           if (CI->getZExtValue() < Other->getZExtValue())
374             lv |= ULT_BIT;
375           else
376             lv |= UGT_BIT;
377
378           if (CI->getSExtValue() < Other->getSExtValue())
379             lv |= SLT_BIT;
380           else
381             lv |= SGT_BIT;
382
383           LatticeVal LV = static_cast<LatticeVal>(lv);
384           assert(validPredicate(LV) && "Not a valid predicate.");
385           if (!isRelatedBy(index, I->second, TreeRoot, LV))
386             addInequality(index, I->second, TreeRoot, LV);
387         }
388       }
389       Constants.push_back(std::make_pair(CI, index));
390     }
391
392   public:
393     /// node - returns the node object at a given index retrieved from getNode.
394     /// Index zero is reserved and may not be passed in here. The pointer
395     /// returned is valid until the next call to newNode or getOrInsertNode.
396     Node *node(unsigned index) {
397       assert(index != 0 && "Zero index is reserved for not found.");
398       assert(index <= Nodes.size() && "Index out of range.");
399       return &Nodes[index-1];
400     }
401
402     /// Returns the node currently representing Value V, or zero if no such
403     /// node exists.
404     unsigned getNode(Value *V, ETNode *Subtree) {
405       NodeMapType::iterator E = NodeMap.end();
406       NodeMapEdge Edge(V, 0, Subtree);
407       NodeMapType::iterator I = std::lower_bound(NodeMap.begin(), E, Edge);
408       while (I != E && I->V == V) {
409         if (Subtree->DominatedBy(I->Subtree))
410           return I->index;
411         ++I;
412       }
413       return 0;
414     }
415
416     /// getOrInsertNode - always returns a valid node index, creating a node
417     /// to match the Value if needed.
418     unsigned getOrInsertNode(Value *V, ETNode *Subtree) {
419       if (unsigned n = getNode(V, Subtree))
420         return n;
421       else
422         return newNode(V);
423     }
424
425     /// newNode - creates a new node for a given Value and returns the index.
426     unsigned newNode(Value *V) {
427       Nodes.push_back(Node(V));
428
429       NodeMapEdge MapEntry = NodeMapEdge(V, Nodes.size(), TreeRoot);
430       assert(!std::binary_search(NodeMap.begin(), NodeMap.end(), MapEntry) &&
431              "Attempt to create a duplicate Node.");
432       NodeMap.insert(std::lower_bound(NodeMap.begin(), NodeMap.end(),
433                                       MapEntry), MapEntry);
434
435 #if 1
436       // This is the missing piece to turn on VRP.
437       if (Constant *C = dyn_cast<Constant>(V))
438         initializeConstant(C, MapEntry.index);
439 #endif
440
441       return MapEntry.index;
442     }
443
444     /// If the Value is in the graph, return the canonical form. Otherwise,
445     /// return the original Value.
446     Value *canonicalize(Value *V, ETNode *Subtree) {
447       if (isa<Constant>(V)) return V;
448
449       if (unsigned n = getNode(V, Subtree))
450         return node(n)->getValue();
451       else 
452         return V;
453     }
454
455     /// isRelatedBy - true iff n1 op n2
456     bool isRelatedBy(unsigned n1, unsigned n2, ETNode *Subtree, LatticeVal LV) {
457       if (n1 == n2) return LV & EQ_BIT;
458
459       Node *N1 = node(n1);
460       Node::iterator I = N1->find(n2, Subtree), E = N1->end();
461       if (I != E) return (I->LV & LV) == I->LV;
462
463       return false;
464     }
465
466     // The add* methods assume that your input is logically valid and may 
467     // assertion-fail or infinitely loop if you attempt a contradiction.
468
469     void addEquality(unsigned n, Value *V, ETNode *Subtree) {
470       assert(canonicalize(node(n)->getValue(), Subtree) == node(n)->getValue()
471              && "Node's 'canonical' choice isn't best within this subtree.");
472
473       // Suppose that we are given "%x -> node #1 (%y)". The problem is that
474       // we may already have "%z -> node #2 (%x)" somewhere above us in the
475       // graph. We need to find those edges and add "%z -> node #1 (%y)"
476       // to keep the lookups canonical.
477
478       std::vector<Value *> ToRepoint;
479       ToRepoint.push_back(V);
480
481       if (unsigned Conflict = getNode(V, Subtree)) {
482         // XXX: NodeMap.size() exceeds 68000 entries compiling kimwitu++!
483         // This adds 57 seconds to the otherwise 3 second build. Unacceptable.
484         //
485         // IDEA: could we iterate 1..Nodes.size() calling getNode? It's
486         // O(n log n) but kimwitu++ only has about 300 nodes.
487         for (NodeMapType::iterator I = NodeMap.begin(), E = NodeMap.end();
488              I != E; ++I) {
489           if (I->index == Conflict && Subtree->DominatedBy(I->Subtree))
490             ToRepoint.push_back(I->V);
491         }
492       }
493
494       for (std::vector<Value *>::iterator VI = ToRepoint.begin(),
495            VE = ToRepoint.end(); VI != VE; ++VI) {
496         Value *V = *VI;
497
498         // XXX: review this code. This may be doing too many insertions.
499         NodeMapEdge Edge(V, n, Subtree);
500         NodeMapType::iterator E = NodeMap.end();
501         NodeMapType::iterator I = std::lower_bound(NodeMap.begin(), E, Edge);
502         if (I == E || I->V != V || I->Subtree != Subtree) {
503           // New Value
504           NodeMap.insert(I, Edge);
505         } else if (I != E && I->V == V && I->Subtree == Subtree) {
506           // Update best choice
507           I->index = n;
508         }
509
510 #ifndef NDEBUG
511         Node *N = node(n);
512         if (isa<Constant>(V)) {
513           if (isa<Constant>(N->getValue())) {
514             assert(V == N->getValue() && "Constant equals different constant?");
515           }
516         }
517 #endif
518       }
519     }
520
521     /// addInequality - Sets n1 op n2.
522     /// It is also an error to call this on an inequality that is already true.
523     void addInequality(unsigned n1, unsigned n2, ETNode *Subtree,
524                        LatticeVal LV1) {
525       assert(n1 != n2 && "A node can't be inequal to itself.");
526
527       if (LV1 != NE)
528         assert(!isRelatedBy(n1, n2, Subtree, reversePredicate(LV1)) &&
529                "Contradictory inequality.");
530
531       Node *N1 = node(n1);
532       Node *N2 = node(n2);
533
534       // Suppose we're adding %n1 < %n2. Find all the %a < %n1 and
535       // add %a < %n2 too. This keeps the graph fully connected.
536       if (LV1 != NE) {
537         // Someone with a head for this sort of logic, please review this.
538         // Given that %x SLTUGT %y and %a SLEUANY %x, what is the relationship
539         // between %a and %y? I believe the below code is correct, but I don't
540         // think it's the most efficient solution.
541
542         unsigned LV1_s = LV1 & (SLT_BIT|SGT_BIT);
543         unsigned LV1_u = LV1 & (ULT_BIT|UGT_BIT);
544         for (Node::iterator I = N1->begin(), E = N1->end(); I != E; ++I) {
545           if (I->LV != NE && I->To != n2) {
546             ETNode *Local_Subtree = NULL;
547             if (Subtree->DominatedBy(I->Subtree))
548               Local_Subtree = Subtree;
549             else if (I->Subtree->DominatedBy(Subtree))
550               Local_Subtree = I->Subtree;
551
552             if (Local_Subtree) {
553               unsigned new_relationship = 0;
554               LatticeVal ILV = reversePredicate(I->LV);
555               unsigned ILV_s = ILV & (SLT_BIT|SGT_BIT);
556               unsigned ILV_u = ILV & (ULT_BIT|UGT_BIT);
557
558               if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s)
559                 new_relationship |= ILV_s;
560
561               if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u)
562                 new_relationship |= ILV_u;
563
564               if (new_relationship) {
565                 if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0)
566                   new_relationship |= (SLT_BIT|SGT_BIT);
567                 if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0)
568                   new_relationship |= (ULT_BIT|UGT_BIT);
569                 if ((LV1 & EQ_BIT) && (ILV & EQ_BIT))
570                   new_relationship |= EQ_BIT;
571
572                 LatticeVal NewLV = static_cast<LatticeVal>(new_relationship);
573
574                 node(I->To)->update(n2, NewLV, Local_Subtree);
575                 N2->update(I->To, reversePredicate(NewLV), Local_Subtree);
576               }
577             }
578           }
579         }
580
581         for (Node::iterator I = N2->begin(), E = N2->end(); I != E; ++I) {
582           if (I->LV != NE && I->To != n1) {
583             ETNode *Local_Subtree = NULL;
584             if (Subtree->DominatedBy(I->Subtree))
585               Local_Subtree = Subtree;
586             else if (I->Subtree->DominatedBy(Subtree))
587               Local_Subtree = I->Subtree;
588
589             if (Local_Subtree) {
590               unsigned new_relationship = 0;
591               unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT);
592               unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT);
593
594               if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s)
595                 new_relationship |= ILV_s;
596
597               if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u)
598                 new_relationship |= ILV_u;
599
600               if (new_relationship) {
601                 if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0)
602                   new_relationship |= (SLT_BIT|SGT_BIT);
603                 if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0)
604                   new_relationship |= (ULT_BIT|UGT_BIT);
605                 if ((LV1 & EQ_BIT) && (I->LV & EQ_BIT))
606                   new_relationship |= EQ_BIT;
607
608                 LatticeVal NewLV = static_cast<LatticeVal>(new_relationship);
609
610                 N1->update(I->To, NewLV, Local_Subtree);
611                 node(I->To)->update(n1, reversePredicate(NewLV), Local_Subtree);
612               }
613             }
614           }
615         }
616       }
617
618       N1->update(n2, LV1, Subtree);
619       N2->update(n1, reversePredicate(LV1), Subtree);
620     }
621
622     /// Removes a Value from the graph, but does not delete any nodes. As this
623     /// method does not delete Nodes, V may not be the canonical choice for
624     /// a node with any relationships. It is invalid to call newNode on a Value
625     /// that has been removed.
626     void remove(Value *V) {
627       for (unsigned i = 0; i < NodeMap.size();) {
628         NodeMapType::iterator I = NodeMap.begin()+i;
629         assert((node(I->index)->getValue() != V || node(I->index)->begin() ==
630                 node(I->index)->end()) && "Tried to delete in-use node.");
631         if (I->V == V) {
632 #ifndef NDEBUG
633           if (node(I->index)->getValue() == V)
634             node(I->index)->Canonical = NULL;
635 #endif
636           NodeMap.erase(I);
637         } else ++i;
638       }
639     }
640
641 #ifndef NDEBUG
642     virtual ~InequalityGraph() {}
643     virtual void dump() {
644       dump(*cerr.stream());
645     }
646
647     void dump(std::ostream &os) {
648     std::set<Node *> VisitedNodes;
649     for (NodeMapType::const_iterator I = NodeMap.begin(), E = NodeMap.end();
650          I != E; ++I) {
651       Node *N = node(I->index);
652       os << *I->V << " == " << I->index << "(" << I->Subtree << ")\n";
653       if (VisitedNodes.insert(N).second) {
654         os << I->index << ". ";
655         if (!N->getValue()) os << "(deleted node)\n";
656         else N->dump(os);
657       }
658     }
659   }
660 #endif
661   };
662
663   /// UnreachableBlocks keeps tracks of blocks that are for one reason or
664   /// another discovered to be unreachable. This is used to cull the graph when
665   /// analyzing instructions, and to mark blocks with the "unreachable"
666   /// terminator instruction after the function has executed.
667   class VISIBILITY_HIDDEN UnreachableBlocks {
668   private:
669     std::vector<BasicBlock *> DeadBlocks;
670
671   public:
672     /// mark - mark a block as dead
673     void mark(BasicBlock *BB) {
674       std::vector<BasicBlock *>::iterator E = DeadBlocks.end();
675       std::vector<BasicBlock *>::iterator I =
676         std::lower_bound(DeadBlocks.begin(), E, BB);
677
678       if (I == E || *I != BB) DeadBlocks.insert(I, BB);
679     }
680
681     /// isDead - returns whether a block is known to be dead already
682     bool isDead(BasicBlock *BB) {
683       std::vector<BasicBlock *>::iterator E = DeadBlocks.end();
684       std::vector<BasicBlock *>::iterator I =
685         std::lower_bound(DeadBlocks.begin(), E, BB);
686
687       return I != E && *I == BB;
688     }
689
690     /// kill - replace the dead blocks' terminator with an UnreachableInst.
691     bool kill() {
692       bool modified = false;
693       for (std::vector<BasicBlock *>::iterator I = DeadBlocks.begin(),
694            E = DeadBlocks.end(); I != E; ++I) {
695         BasicBlock *BB = *I;
696
697         DOUT << "unreachable block: " << BB->getName() << "\n";
698
699         for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
700              SI != SE; ++SI) {
701           BasicBlock *Succ = *SI;
702           Succ->removePredecessor(BB);
703         }
704
705         TerminatorInst *TI = BB->getTerminator();
706         TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
707         TI->eraseFromParent();
708         new UnreachableInst(BB);
709         ++NumBlocks;
710         modified = true;
711       }
712       DeadBlocks.clear();
713       return modified;
714     }
715   };
716
717   /// VRPSolver keeps track of how changes to one variable affect other
718   /// variables, and forwards changes along to the InequalityGraph. It
719   /// also maintains the correct choice for "canonical" in the IG.
720   /// @brief VRPSolver calculates inferences from a new relationship.
721   class VISIBILITY_HIDDEN VRPSolver {
722   private:
723     struct Operation {
724       Value *LHS, *RHS;
725       ICmpInst::Predicate Op;
726
727       Instruction *Context;
728     };
729     std::deque<Operation> WorkList;
730
731     InequalityGraph &IG;
732     UnreachableBlocks &UB;
733     ETForest *Forest;
734     ETNode *Top;
735     BasicBlock *TopBB;
736     Instruction *TopInst;
737     bool &modified;
738
739     typedef InequalityGraph::Node Node;
740
741     /// IdomI - Determines whether one Instruction dominates another.
742     bool IdomI(Instruction *I1, Instruction *I2) const {
743       BasicBlock *BB1 = I1->getParent(),
744                  *BB2 = I2->getParent();
745       if (BB1 == BB2) {
746         if (isa<TerminatorInst>(I1)) return false;
747         if (isa<TerminatorInst>(I2)) return true;
748         if (isa<PHINode>(I1) && !isa<PHINode>(I2)) return true;
749         if (!isa<PHINode>(I1) && isa<PHINode>(I2)) return false;
750
751         for (BasicBlock::const_iterator I = BB1->begin(), E = BB1->end();
752              I != E; ++I) {
753           if (&*I == I1) return true;
754           if (&*I == I2) return false;
755         }
756         assert(!"Instructions not found in parent BasicBlock?");
757       } else {
758         return Forest->properlyDominates(BB1, BB2);
759       }
760       return false;
761     }
762
763     /// Returns true if V1 is a better canonical value than V2.
764     bool compare(Value *V1, Value *V2) const {
765       if (isa<Constant>(V1))
766         return !isa<Constant>(V2);
767       else if (isa<Constant>(V2))
768         return false;
769       else if (isa<Argument>(V1))
770         return !isa<Argument>(V2);
771       else if (isa<Argument>(V2))
772         return false;
773
774       Instruction *I1 = dyn_cast<Instruction>(V1);
775       Instruction *I2 = dyn_cast<Instruction>(V2);
776
777       if (!I1 || !I2)
778         return V1->getNumUses() < V2->getNumUses();
779
780       return IdomI(I1, I2);
781     }
782
783     // below - true if the Instruction is dominated by the current context
784     // block or instruction
785     bool below(Instruction *I) {
786       if (TopInst)
787         return IdomI(TopInst, I);
788       else {
789         ETNode *Node = Forest->getNodeForBlock(I->getParent());
790         return Node == Top || Node->DominatedBy(Top);
791       }
792     }
793
794     bool makeEqual(Value *V1, Value *V2) {
795       DOUT << "makeEqual(" << *V1 << ", " << *V2 << ")\n";
796
797       if (V1 == V2) return true;
798
799       if (isa<Constant>(V1) && isa<Constant>(V2))
800         return false;
801
802       unsigned n1 = IG.getNode(V1, Top), n2 = IG.getNode(V2, Top);
803
804       if (n1 && n2) {
805         if (n1 == n2) return true;
806         if (IG.isRelatedBy(n1, n2, Top, NE)) return false;
807       }
808
809       if (n1) assert(V1 == IG.node(n1)->getValue() && "Value isn't canonical.");
810       if (n2) assert(V2 == IG.node(n2)->getValue() && "Value isn't canonical.");
811
812       if (compare(V2, V1)) { std::swap(V1, V2); std::swap(n1, n2); }
813
814       assert(!isa<Constant>(V2) && "Tried to remove a constant.");
815
816       SetVector<unsigned> Remove;
817       if (n2) Remove.insert(n2);
818
819       if (n1 && n2) {
820         // Suppose we're being told that %x == %y, and %x <= %z and %y >= %z.
821         // We can't just merge %x and %y because the relationship with %z would
822         // be EQ and that's invalid. What we're doing is looking for any nodes
823         // %z such that %x <= %z and %y >= %z, and vice versa.
824         //
825         // Also handle %a <= %b and %c <= %a when adding %b <= %c.
826
827         Node *N1 = IG.node(n1);
828         Node::iterator end = N1->end();
829         for (unsigned i = 0; i < Remove.size(); ++i) {
830           Node *N = IG.node(Remove[i]);
831           Value *V = N->getValue();
832           for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I) {
833             if (I->LV & EQ_BIT) {
834               if (Top == I->Subtree || Top->DominatedBy(I->Subtree)) {
835                 Node::iterator NI = N1->find(I->To, Top);
836                 if (NI != end) {
837                   if (!(NI->LV & EQ_BIT)) return false;
838                   if (isRelatedBy(V, IG.node(NI->To)->getValue(),
839                                   ICmpInst::ICMP_NE))
840                     return false;
841                   Remove.insert(NI->To);
842                 }
843               }
844             }
845           }
846         }
847
848         // See if one of the nodes about to be removed is actually a better
849         // canonical choice than n1.
850         unsigned orig_n1 = n1;
851         std::vector<unsigned>::iterator DontRemove = Remove.end();
852         for (std::vector<unsigned>::iterator I = Remove.begin()+1 /* skip n2 */,
853              E = Remove.end(); I != E; ++I) {
854           unsigned n = *I;
855           Value *V = IG.node(n)->getValue();
856           if (compare(V, V1)) {
857             V1 = V;
858             n1 = n;
859             DontRemove = I;
860           }
861         }
862         if (DontRemove != Remove.end()) {
863           unsigned n = *DontRemove;
864           Remove.remove(n);
865           Remove.insert(orig_n1);
866         }
867       }
868
869       // We'd like to allow makeEqual on two values to perform a simple
870       // substitution without every creating nodes in the IG whenever possible.
871       //
872       // The first iteration through this loop operates on V2 before going
873       // through the Remove list and operating on those too. If all of the
874       // iterations performed simple replacements then we exit early.
875       bool exitEarly = true;
876       unsigned i = 0;
877       for (Value *R = V2; i == 0 || i < Remove.size(); ++i) {
878         if (i) R = IG.node(Remove[i])->getValue(); // skip n2.
879
880         // Try to replace the whole instruction. If we can, we're done.
881         Instruction *I2 = dyn_cast<Instruction>(R);
882         if (I2 && below(I2)) {
883           std::vector<Instruction *> ToNotify;
884           for (Value::use_iterator UI = R->use_begin(), UE = R->use_end();
885                UI != UE;) {
886             Use &TheUse = UI.getUse();
887             ++UI;
888             if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser()))
889               ToNotify.push_back(I);
890           }
891
892           DOUT << "Simply removing " << *I2
893                << ", replacing with " << *V1 << "\n";
894           I2->replaceAllUsesWith(V1);
895           // leave it dead; it'll get erased later.
896           ++NumInstruction;
897           modified = true;
898
899           for (std::vector<Instruction *>::iterator II = ToNotify.begin(),
900                IE = ToNotify.end(); II != IE; ++II) {
901             opsToDef(*II);
902           }
903
904           continue;
905         }
906
907         // Otherwise, replace all dominated uses.
908         for (Value::use_iterator UI = R->use_begin(), UE = R->use_end();
909              UI != UE;) {
910           Use &TheUse = UI.getUse();
911           ++UI;
912           if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
913             if (below(I)) {
914               TheUse.set(V1);
915               modified = true;
916               ++NumVarsReplaced;
917               opsToDef(I);
918             }
919           }
920         }
921
922         // If that killed the instruction, stop here.
923         if (I2 && isInstructionTriviallyDead(I2)) {
924           DOUT << "Killed all uses of " << *I2
925                << ", replacing with " << *V1 << "\n";
926           continue;
927         }
928
929         // If we make it to here, then we will need to create a node for N1.
930         // Otherwise, we can skip out early!
931         exitEarly = false;
932       }
933
934       if (exitEarly) return true;
935
936       // Create N1.
937       // XXX: this should call newNode, but instead the node might be created
938       // in isRelatedBy. That's also a fixme.
939       if (!n1) n1 = IG.getOrInsertNode(V1, Top);
940
941       // Migrate relationships from removed nodes to N1.
942       Node *N1 = IG.node(n1);
943       for (std::vector<unsigned>::iterator I = Remove.begin(), E = Remove.end();
944            I != E; ++I) {
945         unsigned n = *I;
946         Node *N = IG.node(n);
947         for (Node::iterator NI = N->begin(), NE = N->end(); NI != NE; ++NI) {
948           if (Top == NI->Subtree || NI->Subtree->DominatedBy(Top)) {
949             if (NI->To == n1) {
950               assert((NI->LV & EQ_BIT) && "Node inequal to itself.");
951               continue;
952             }
953             if (Remove.count(NI->To))
954               continue;
955
956             IG.node(NI->To)->update(n1, reversePredicate(NI->LV), Top);
957             N1->update(NI->To, NI->LV, Top);
958           }
959         }
960       }
961
962       // Point V2 (and all items in Remove) to N1.
963       if (!n2)
964         IG.addEquality(n1, V2, Top);
965       else {
966         for (std::vector<unsigned>::iterator I = Remove.begin(),
967              E = Remove.end(); I != E; ++I) {
968           IG.addEquality(n1, IG.node(*I)->getValue(), Top);
969         }
970       }
971
972       // If !Remove.empty() then V2 = Remove[0]->getValue().
973       // Even when Remove is empty, we still want to process V2.
974       i = 0;
975       for (Value *R = V2; i == 0 || i < Remove.size(); ++i) {
976         if (i) R = IG.node(Remove[i])->getValue(); // skip n2.
977
978         if (Instruction *I2 = dyn_cast<Instruction>(R)) defToOps(I2);
979         for (Value::use_iterator UI = V2->use_begin(), UE = V2->use_end();
980              UI != UE;) {
981           Use &TheUse = UI.getUse();
982           ++UI;
983           if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
984             opsToDef(I);
985           }
986         }
987       }
988
989       return true;
990     }
991
992     /// cmpInstToLattice - converts an CmpInst::Predicate to lattice value
993     /// Requires that the lattice value be valid; does not accept ICMP_EQ.
994     static LatticeVal cmpInstToLattice(ICmpInst::Predicate Pred) {
995       switch (Pred) {
996         case ICmpInst::ICMP_EQ:
997           assert(!"No matching lattice value.");
998           return static_cast<LatticeVal>(EQ_BIT);
999         default:
1000           assert(!"Invalid 'icmp' predicate.");
1001         case ICmpInst::ICMP_NE:
1002           return NE;
1003         case ICmpInst::ICMP_UGT:
1004           return SNEUGT;
1005         case ICmpInst::ICMP_UGE:
1006           return SANYUGE;
1007         case ICmpInst::ICMP_ULT:
1008           return SNEULT;
1009         case ICmpInst::ICMP_ULE:
1010           return SANYULE;
1011         case ICmpInst::ICMP_SGT:
1012           return SGTUNE;
1013         case ICmpInst::ICMP_SGE:
1014           return SGEUANY;
1015         case ICmpInst::ICMP_SLT:
1016           return SLTUNE;
1017         case ICmpInst::ICMP_SLE:
1018           return SLEUANY;
1019       }
1020     }
1021
1022   public:
1023     VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ETForest *Forest,
1024               bool &modified, BasicBlock *TopBB)
1025       : IG(IG),
1026         UB(UB),
1027         Forest(Forest),
1028         Top(Forest->getNodeForBlock(TopBB)),
1029         TopBB(TopBB),
1030         TopInst(NULL),
1031         modified(modified) {}
1032
1033     VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ETForest *Forest,
1034               bool &modified, Instruction *TopInst)
1035       : IG(IG),
1036         UB(UB),
1037         Forest(Forest),
1038         TopInst(TopInst),
1039         modified(modified)
1040     {
1041       TopBB = TopInst->getParent();
1042       Top = Forest->getNodeForBlock(TopBB);
1043     }
1044
1045     bool isRelatedBy(Value *V1, Value *V2, ICmpInst::Predicate Pred) const {
1046       if (Constant *C1 = dyn_cast<Constant>(V1))
1047         if (Constant *C2 = dyn_cast<Constant>(V2))
1048           return ConstantExpr::getCompare(Pred, C1, C2) ==
1049                  ConstantInt::getTrue();
1050
1051       // XXX: this is lousy. If we're passed a Constant, then we might miss
1052       // some relationships if it isn't in the IG because the relationships
1053       // added by initializeConstant are missing.
1054       if (isa<Constant>(V1)) IG.getOrInsertNode(V1, Top);
1055       if (isa<Constant>(V2)) IG.getOrInsertNode(V2, Top);
1056
1057       if (unsigned n1 = IG.getNode(V1, Top))
1058         if (unsigned n2 = IG.getNode(V2, Top)) {
1059           if (n1 == n2) return Pred == ICmpInst::ICMP_EQ ||
1060                                Pred == ICmpInst::ICMP_ULE ||
1061                                Pred == ICmpInst::ICMP_UGE ||
1062                                Pred == ICmpInst::ICMP_SLE ||
1063                                Pred == ICmpInst::ICMP_SGE;
1064           if (Pred == ICmpInst::ICMP_EQ) return false;
1065           return IG.isRelatedBy(n1, n2, Top, cmpInstToLattice(Pred));
1066         }
1067
1068       return false;
1069     }
1070
1071     /// add - adds a new property to the work queue
1072     void add(Value *V1, Value *V2, ICmpInst::Predicate Pred,
1073              Instruction *I = NULL) {
1074       DOUT << "adding " << *V1 << " " << Pred << " " << *V2;
1075       if (I) DOUT << " context: " << *I;
1076       else DOUT << " default context";
1077       DOUT << "\n";
1078
1079       WorkList.push_back(Operation());
1080       Operation &O = WorkList.back();
1081       O.LHS = V1, O.RHS = V2, O.Op = Pred, O.Context = I;
1082     }
1083
1084     /// defToOps - Given an instruction definition that we've learned something
1085     /// new about, find any new relationships between its operands.
1086     void defToOps(Instruction *I) {
1087       Instruction *NewContext = below(I) ? I : TopInst;
1088       Value *Canonical = IG.canonicalize(I, Top);
1089
1090       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
1091         const Type *Ty = BO->getType();
1092         assert(!Ty->isFPOrFPVector() && "Float in work queue!");
1093
1094         Value *Op0 = IG.canonicalize(BO->getOperand(0), Top);
1095         Value *Op1 = IG.canonicalize(BO->getOperand(1), Top);
1096
1097         // TODO: "and bool true, %x" EQ %y then %x EQ %y.
1098
1099         switch (BO->getOpcode()) {
1100           case Instruction::And: {
1101             // "and int %a, %b"  EQ -1   then %a EQ -1   and %b EQ -1
1102             // "and bool %a, %b" EQ true then %a EQ true and %b EQ true
1103             ConstantInt *CI = ConstantInt::getAllOnesValue(Ty);
1104             if (Canonical == CI) {
1105               add(CI, Op0, ICmpInst::ICMP_EQ, NewContext);
1106               add(CI, Op1, ICmpInst::ICMP_EQ, NewContext);
1107             }
1108           } break;
1109           case Instruction::Or: {
1110             // "or int %a, %b"  EQ 0     then %a EQ 0     and %b EQ 0
1111             // "or bool %a, %b" EQ false then %a EQ false and %b EQ false
1112             Constant *Zero = Constant::getNullValue(Ty);
1113             if (Canonical == Zero) {
1114               add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext);
1115               add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext);
1116             }
1117           } break;
1118           case Instruction::Xor: {
1119             // "xor bool true,  %a" EQ true  then %a EQ false
1120             // "xor bool true,  %a" EQ false then %a EQ true
1121             // "xor bool false, %a" EQ true  then %a EQ true
1122             // "xor bool false, %a" EQ false then %a EQ false
1123             // "xor int %c, %a" EQ %c then %a EQ 0
1124             // "xor int %c, %a" NE %c then %a NE 0
1125             // 1. Repeat all of the above, with order of operands reversed.
1126             Value *LHS = Op0;
1127             Value *RHS = Op1;
1128             if (!isa<Constant>(LHS)) std::swap(LHS, RHS);
1129
1130             ConstantInt *CB, *A;
1131             if ((CB = dyn_cast<ConstantInt>(Canonical)) && 
1132                 CB->getType() == Type::Int1Ty) {
1133               if ((A = dyn_cast<ConstantInt>(LHS)) &&
1134                   A->getType() == Type::Int1Ty)
1135                 add(RHS, ConstantInt::get(A->getBoolValue() ^ 
1136                                           CB->getBoolValue()),
1137                                           ICmpInst::ICMP_EQ, NewContext);
1138             }
1139             if (Canonical == LHS) {
1140               if (isa<ConstantInt>(Canonical))
1141                 add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ,
1142                     NewContext);
1143             } else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) {
1144               add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE,
1145                   NewContext);
1146             }
1147           } break;
1148           default:
1149             break;
1150         }
1151       } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) {
1152         // "icmp ult int %a, int %y" EQ true then %a u< y
1153         // etc.
1154
1155         if (Canonical == ConstantInt::getTrue()) {
1156           add(IC->getOperand(0), IC->getOperand(1), IC->getPredicate(),
1157               NewContext);
1158         } else if (Canonical == ConstantInt::getFalse()) {
1159           add(IC->getOperand(0), IC->getOperand(1),
1160               ICmpInst::getInversePredicate(IC->getPredicate()), NewContext);
1161         }
1162       } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
1163         if (I->getType()->isFPOrFPVector()) return;
1164
1165         // Given: "%a = select bool %x, int %b, int %c"
1166         // %a EQ %b and %b NE %c then %x EQ true
1167         // %a EQ %c and %b NE %c then %x EQ false
1168
1169         Value *True  = SI->getTrueValue();
1170         Value *False = SI->getFalseValue();
1171         if (isRelatedBy(True, False, ICmpInst::ICMP_NE)) {
1172           if (Canonical == IG.canonicalize(True, Top) ||
1173               isRelatedBy(Canonical, False, ICmpInst::ICMP_NE))
1174             add(SI->getCondition(), ConstantInt::getTrue(),
1175                 ICmpInst::ICMP_EQ, NewContext);
1176           else if (Canonical == IG.canonicalize(False, Top) ||
1177                    isRelatedBy(I, True, ICmpInst::ICMP_NE))
1178             add(SI->getCondition(), ConstantInt::getFalse(),
1179                 ICmpInst::ICMP_EQ, NewContext);
1180         }
1181       }
1182       // TODO: CastInst "%a = cast ... %b" where %a is EQ or NE a constant.
1183     }
1184
1185     /// opsToDef - A new relationship was discovered involving one of this
1186     /// instruction's operands. Find any new relationship involving the
1187     /// definition.
1188     void opsToDef(Instruction *I) {
1189       Instruction *NewContext = below(I) ? I : TopInst;
1190
1191       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
1192         Value *Op0 = IG.canonicalize(BO->getOperand(0), Top);
1193         Value *Op1 = IG.canonicalize(BO->getOperand(1), Top);
1194
1195         if (ConstantInt *CI0 = dyn_cast<ConstantInt>(Op0))
1196           if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Op1)) {
1197             add(BO, ConstantExpr::get(BO->getOpcode(), CI0, CI1),
1198                 ICmpInst::ICMP_EQ, NewContext);
1199             return;
1200           }
1201
1202         // "%y = and bool true, %x" then %x EQ %y.
1203         // "%y = or bool false, %x" then %x EQ %y.
1204         if (BO->getOpcode() == Instruction::Or) {
1205           Constant *Zero = Constant::getNullValue(BO->getType());
1206           if (Op0 == Zero) {
1207             add(BO, Op1, ICmpInst::ICMP_EQ, NewContext);
1208             return;
1209           } else if (Op1 == Zero) {
1210             add(BO, Op0, ICmpInst::ICMP_EQ, NewContext);
1211             return;
1212           }
1213         } else if (BO->getOpcode() == Instruction::And) {
1214           Constant *AllOnes = ConstantInt::getAllOnesValue(BO->getType());
1215           if (Op0 == AllOnes) {
1216             add(BO, Op1, ICmpInst::ICMP_EQ, NewContext);
1217             return;
1218           } else if (Op1 == AllOnes) {
1219             add(BO, Op0, ICmpInst::ICMP_EQ, NewContext);
1220             return;
1221           }
1222         }
1223
1224         // "%x = add int %y, %z" and %x EQ %y then %z EQ 0
1225         // "%x = mul int %y, %z" and %x EQ %y then %z EQ 1
1226         // 1. Repeat all of the above, with order of operands reversed.
1227         // "%x = udiv int %y, %z" and %x EQ %y then %z EQ 1
1228
1229         Value *Known = Op0, *Unknown = Op1;
1230         if (Known != BO) std::swap(Known, Unknown);
1231         if (Known == BO) {
1232           const Type *Ty = BO->getType();
1233           assert(!Ty->isFPOrFPVector() && "Float in work queue!");
1234
1235           switch (BO->getOpcode()) {
1236             default: break;
1237             case Instruction::Xor:
1238             case Instruction::Or:
1239             case Instruction::Add:
1240             case Instruction::Sub:
1241               add(Unknown, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ, NewContext);
1242               break;
1243             case Instruction::UDiv:
1244             case Instruction::SDiv:
1245               if (Unknown == Op0) break; // otherwise, fallthrough
1246             case Instruction::And:
1247             case Instruction::Mul:
1248               Constant *One = NULL;
1249               if (isa<ConstantInt>(Unknown))
1250                 One = ConstantInt::get(Ty, 1);
1251               else if (isa<ConstantInt>(Unknown) && 
1252                        Unknown->getType() == Type::Int1Ty)
1253                 One = ConstantInt::getTrue();
1254
1255               if (One) add(Unknown, One, ICmpInst::ICMP_EQ, NewContext);
1256               break;
1257           }
1258         }
1259
1260         // TODO: "%a = add int %b, 1" and %b > %z then %a >= %z.
1261
1262       } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) {
1263         // "%a = icmp ult %b, %c" and %b u< %c  then %a EQ true
1264         // "%a = icmp ult %b, %c" and %b u>= %c then %a EQ false
1265         // etc.
1266
1267         Value *Op0 = IG.canonicalize(IC->getOperand(0), Top);
1268         Value *Op1 = IG.canonicalize(IC->getOperand(1), Top);
1269
1270         ICmpInst::Predicate Pred = IC->getPredicate();
1271         if (isRelatedBy(Op0, Op1, Pred)) {
1272           add(IC, ConstantInt::getTrue(), ICmpInst::ICMP_EQ, NewContext);
1273         } else if (isRelatedBy(Op0, Op1, ICmpInst::getInversePredicate(Pred))) {
1274           add(IC, ConstantInt::getFalse(), ICmpInst::ICMP_EQ, NewContext);
1275         }
1276
1277         // TODO: make the predicate more strict, if possible.
1278
1279       } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
1280         // Given: "%a = select bool %x, int %b, int %c"
1281         // %x EQ true  then %a EQ %b
1282         // %x EQ false then %a EQ %c
1283         // %b EQ %c then %a EQ %b
1284
1285         Value *Canonical = IG.canonicalize(SI->getCondition(), Top);
1286         if (Canonical == ConstantInt::getTrue()) {
1287           add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext);
1288         } else if (Canonical == ConstantInt::getFalse()) {
1289           add(SI, SI->getFalseValue(), ICmpInst::ICMP_EQ, NewContext);
1290         } else if (IG.canonicalize(SI->getTrueValue(), Top) ==
1291                    IG.canonicalize(SI->getFalseValue(), Top)) {
1292           add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext);
1293         }
1294       }
1295       // TODO: CastInst "%a = cast ... %b" where %b is EQ or NE a constant.
1296     }
1297
1298     /// solve - process the work queue
1299     /// Return false if a logical contradiction occurs.
1300     void solve() {
1301       //DOUT << "WorkList entry, size: " << WorkList.size() << "\n";
1302       while (!WorkList.empty()) {
1303         //DOUT << "WorkList size: " << WorkList.size() << "\n";
1304
1305         Operation &O = WorkList.front();
1306         if (O.Context) {
1307           TopInst = O.Context;
1308           Top = Forest->getNodeForBlock(TopInst->getParent());
1309         }
1310         O.LHS = IG.canonicalize(O.LHS, Top);
1311         O.RHS = IG.canonicalize(O.RHS, Top);
1312
1313         assert(O.LHS == IG.canonicalize(O.LHS, Top) && "Canonicalize isn't.");
1314         assert(O.RHS == IG.canonicalize(O.RHS, Top) && "Canonicalize isn't.");
1315
1316         DOUT << "solving " << *O.LHS << " " << O.Op << " " << *O.RHS;
1317         if (O.Context) DOUT << " context: " << *O.Context;
1318         else DOUT << " default context";
1319         DOUT << "\n";
1320
1321         DEBUG(IG.dump());
1322
1323         // TODO: actually check the constants and add to UB.
1324         if (isa<Constant>(O.LHS) && isa<Constant>(O.RHS)) {
1325           WorkList.pop_front();
1326           continue;
1327         }
1328
1329         if (O.Op == ICmpInst::ICMP_EQ) {
1330           if (!makeEqual(O.LHS, O.RHS))
1331             UB.mark(TopBB);
1332         } else {
1333           LatticeVal LV = cmpInstToLattice(O.Op);
1334
1335           if ((LV & EQ_BIT) &&
1336               isRelatedBy(O.LHS, O.RHS, ICmpInst::getSwappedPredicate(O.Op))) {
1337             if (!makeEqual(O.LHS, O.RHS))
1338               UB.mark(TopBB);
1339           } else {
1340             if (isRelatedBy(O.LHS, O.RHS, ICmpInst::getInversePredicate(O.Op))){
1341               DOUT << "inequality contradiction!\n";
1342               WorkList.pop_front();
1343               continue;
1344             }
1345
1346             unsigned n1 = IG.getOrInsertNode(O.LHS, Top);
1347             unsigned n2 = IG.getOrInsertNode(O.RHS, Top);
1348
1349             if (n1 == n2) {
1350               if (O.Op != ICmpInst::ICMP_UGE && O.Op != ICmpInst::ICMP_ULE &&
1351                   O.Op != ICmpInst::ICMP_SGE && O.Op != ICmpInst::ICMP_SLE)
1352                 UB.mark(TopBB);
1353
1354               WorkList.pop_front();
1355               continue;
1356             }
1357
1358             if (IG.isRelatedBy(n1, n2, Top, LV)) {
1359               WorkList.pop_front();
1360               continue;
1361             }
1362
1363             IG.addInequality(n1, n2, Top, LV);
1364
1365             if (Instruction *I1 = dyn_cast<Instruction>(O.LHS)) defToOps(I1);
1366             if (isa<Instruction>(O.LHS) || isa<Argument>(O.LHS)) {
1367               for (Value::use_iterator UI = O.LHS->use_begin(),
1368                    UE = O.LHS->use_end(); UI != UE;) {
1369                 Use &TheUse = UI.getUse();
1370                 ++UI;
1371                 if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
1372                   opsToDef(I);
1373                 }
1374               }
1375             }
1376             if (Instruction *I2 = dyn_cast<Instruction>(O.RHS)) defToOps(I2);
1377             if (isa<Instruction>(O.RHS) || isa<Argument>(O.RHS)) {
1378               for (Value::use_iterator UI = O.RHS->use_begin(),
1379                    UE = O.RHS->use_end(); UI != UE;) {
1380                 Use &TheUse = UI.getUse();
1381                 ++UI;
1382                 if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
1383                   opsToDef(I);
1384                 }
1385               }
1386             }
1387           }
1388         }
1389         WorkList.pop_front();
1390       }
1391     }
1392   };
1393
1394   /// PredicateSimplifier - This class is a simplifier that replaces
1395   /// one equivalent variable with another. It also tracks what
1396   /// can't be equal and will solve setcc instructions when possible.
1397   /// @brief Root of the predicate simplifier optimization.
1398   class VISIBILITY_HIDDEN PredicateSimplifier : public FunctionPass {
1399     DominatorTree *DT;
1400     ETForest *Forest;
1401     bool modified;
1402     InequalityGraph *IG;
1403     UnreachableBlocks UB;
1404
1405     std::vector<DominatorTree::Node *> WorkList;
1406
1407   public:
1408     bool runOnFunction(Function &F);
1409
1410     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1411       AU.addRequiredID(BreakCriticalEdgesID);
1412       AU.addRequired<DominatorTree>();
1413       AU.addRequired<ETForest>();
1414     }
1415
1416   private:
1417     /// Forwards - Adds new properties into PropertySet and uses them to
1418     /// simplify instructions. Because new properties sometimes apply to
1419     /// a transition from one BasicBlock to another, this will use the
1420     /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the
1421     /// basic block with the new PropertySet.
1422     /// @brief Performs abstract execution of the program.
1423     class VISIBILITY_HIDDEN Forwards : public InstVisitor<Forwards> {
1424       friend class InstVisitor<Forwards>;
1425       PredicateSimplifier *PS;
1426       DominatorTree::Node *DTNode;
1427
1428     public:
1429       InequalityGraph &IG;
1430       UnreachableBlocks &UB;
1431
1432       Forwards(PredicateSimplifier *PS, DominatorTree::Node *DTNode)
1433         : PS(PS), DTNode(DTNode), IG(*PS->IG), UB(PS->UB) {}
1434
1435       void visitTerminatorInst(TerminatorInst &TI);
1436       void visitBranchInst(BranchInst &BI);
1437       void visitSwitchInst(SwitchInst &SI);
1438
1439       void visitAllocaInst(AllocaInst &AI);
1440       void visitLoadInst(LoadInst &LI);
1441       void visitStoreInst(StoreInst &SI);
1442
1443       void visitBinaryOperator(BinaryOperator &BO);
1444     };
1445
1446     // Used by terminator instructions to proceed from the current basic
1447     // block to the next. Verifies that "current" dominates "next",
1448     // then calls visitBasicBlock.
1449     void proceedToSuccessors(DominatorTree::Node *Current) {
1450       for (DominatorTree::Node::iterator I = Current->begin(),
1451            E = Current->end(); I != E; ++I) {
1452         WorkList.push_back(*I);
1453       }
1454     }
1455
1456     void proceedToSuccessor(DominatorTree::Node *Next) {
1457       WorkList.push_back(Next);
1458     }
1459
1460     // Visits each instruction in the basic block.
1461     void visitBasicBlock(DominatorTree::Node *Node) {
1462       BasicBlock *BB = Node->getBlock();
1463       ETNode *ET = Forest->getNodeForBlock(BB);
1464       DOUT << "Entering Basic Block: " << BB->getName() << " (" << ET << ")\n";
1465       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
1466         visitInstruction(I++, Node, ET);
1467       }
1468     }
1469
1470     // Tries to simplify each Instruction and add new properties to
1471     // the PropertySet.
1472     void visitInstruction(Instruction *I, DominatorTree::Node *DT, ETNode *ET) {
1473       DOUT << "Considering instruction " << *I << "\n";
1474       DEBUG(IG->dump());
1475
1476       // Sometimes instructions are killed in earlier analysis.
1477       if (isInstructionTriviallyDead(I)) {
1478         ++NumSimple;
1479         modified = true;
1480         IG->remove(I);
1481         I->eraseFromParent();
1482         return;
1483       }
1484
1485       // Try to replace the whole instruction.
1486       Value *V = IG->canonicalize(I, ET);
1487       assert(V == I && "Late instruction canonicalization.");
1488       if (V != I) {
1489         modified = true;
1490         ++NumInstruction;
1491         DOUT << "Removing " << *I << ", replacing with " << *V << "\n";
1492         IG->remove(I);
1493         I->replaceAllUsesWith(V);
1494         I->eraseFromParent();
1495         return;
1496       }
1497
1498       // Try to substitute operands.
1499       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
1500         Value *Oper = I->getOperand(i);
1501         Value *V = IG->canonicalize(Oper, ET);
1502         assert(V == Oper && "Late operand canonicalization.");
1503         if (V != Oper) {
1504           modified = true;
1505           ++NumVarsReplaced;
1506           DOUT << "Resolving " << *I;
1507           I->setOperand(i, V);
1508           DOUT << " into " << *I;
1509         }
1510       }
1511
1512       DOUT << "push (%" << I->getParent()->getName() << ")\n";
1513       Forwards visit(this, DT);
1514       visit.visit(*I);
1515       DOUT << "pop (%" << I->getParent()->getName() << ")\n";
1516     }
1517   };
1518
1519   bool PredicateSimplifier::runOnFunction(Function &F) {
1520     DT = &getAnalysis<DominatorTree>();
1521     Forest = &getAnalysis<ETForest>();
1522
1523     Forest->updateDFSNumbers(); // XXX: should only act when numbers are out of date
1524
1525     DOUT << "Entering Function: " << F.getName() << "\n";
1526
1527     modified = false;
1528     BasicBlock *RootBlock = &F.getEntryBlock();
1529     IG = new InequalityGraph(Forest->getNodeForBlock(RootBlock));
1530     WorkList.push_back(DT->getRootNode());
1531
1532     do {
1533       DominatorTree::Node *DTNode = WorkList.back();
1534       WorkList.pop_back();
1535       if (!UB.isDead(DTNode->getBlock())) visitBasicBlock(DTNode);
1536     } while (!WorkList.empty());
1537
1538     delete IG;
1539
1540     modified |= UB.kill();
1541
1542     return modified;
1543   }
1544
1545   void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) {
1546     PS->proceedToSuccessors(DTNode);
1547   }
1548
1549   void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) {
1550     if (BI.isUnconditional()) {
1551       PS->proceedToSuccessors(DTNode);
1552       return;
1553     }
1554
1555     Value *Condition = BI.getCondition();
1556     BasicBlock *TrueDest  = BI.getSuccessor(0);
1557     BasicBlock *FalseDest = BI.getSuccessor(1);
1558
1559     if (isa<Constant>(Condition) || TrueDest == FalseDest) {
1560       PS->proceedToSuccessors(DTNode);
1561       return;
1562     }
1563
1564     for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end();
1565          I != E; ++I) {
1566       BasicBlock *Dest = (*I)->getBlock();
1567       DOUT << "Branch thinking about %" << Dest->getName()
1568            << "(" << PS->Forest->getNodeForBlock(Dest) << ")\n";
1569
1570       if (Dest == TrueDest) {
1571         DOUT << "(" << DTNode->getBlock()->getName() << ") true set:\n";
1572         VRPSolver VRP(IG, UB, PS->Forest, PS->modified, Dest);
1573         VRP.add(ConstantInt::getTrue(), Condition, ICmpInst::ICMP_EQ);
1574         VRP.solve();
1575         DEBUG(IG.dump());
1576       } else if (Dest == FalseDest) {
1577         DOUT << "(" << DTNode->getBlock()->getName() << ") false set:\n";
1578         VRPSolver VRP(IG, UB, PS->Forest, PS->modified, Dest);
1579         VRP.add(ConstantInt::getFalse(), Condition, ICmpInst::ICMP_EQ);
1580         VRP.solve();
1581         DEBUG(IG.dump());
1582       }
1583
1584       PS->proceedToSuccessor(*I);
1585     }
1586   }
1587
1588   void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) {
1589     Value *Condition = SI.getCondition();
1590
1591     // Set the EQProperty in each of the cases BBs, and the NEProperties
1592     // in the default BB.
1593
1594     for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end();
1595          I != E; ++I) {
1596       BasicBlock *BB = (*I)->getBlock();
1597       DOUT << "Switch thinking about BB %" << BB->getName()
1598            << "(" << PS->Forest->getNodeForBlock(BB) << ")\n";
1599
1600       VRPSolver VRP(IG, UB, PS->Forest, PS->modified, BB);
1601       if (BB == SI.getDefaultDest()) {
1602         for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i)
1603           if (SI.getSuccessor(i) != BB)
1604             VRP.add(Condition, SI.getCaseValue(i), ICmpInst::ICMP_NE);
1605         VRP.solve();
1606       } else if (ConstantInt *CI = SI.findCaseDest(BB)) {
1607         VRP.add(Condition, CI, ICmpInst::ICMP_EQ);
1608         VRP.solve();
1609       }
1610       PS->proceedToSuccessor(*I);
1611     }
1612   }
1613
1614   void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
1615     VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &AI);
1616     VRP.add(Constant::getNullValue(AI.getType()), &AI, ICmpInst::ICMP_NE);
1617     VRP.solve();
1618   }
1619
1620   void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) {
1621     Value *Ptr = LI.getPointerOperand();
1622     // avoid "load uint* null" -> null NE null.
1623     if (isa<Constant>(Ptr)) return;
1624
1625     VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &LI);
1626     VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
1627     VRP.solve();
1628   }
1629
1630   void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) {
1631     Value *Ptr = SI.getPointerOperand();
1632     if (isa<Constant>(Ptr)) return;
1633
1634     VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &SI);
1635     VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
1636     VRP.solve();
1637   }
1638
1639   void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) {
1640     Instruction::BinaryOps ops = BO.getOpcode();
1641
1642     switch (ops) {
1643     case Instruction::URem:
1644     case Instruction::SRem:
1645     case Instruction::UDiv:
1646     case Instruction::SDiv: {
1647       Value *Divisor = BO.getOperand(1);
1648       VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &BO);
1649       VRP.add(Constant::getNullValue(Divisor->getType()), Divisor,
1650               ICmpInst::ICMP_NE);
1651       VRP.solve();
1652       break;
1653     }
1654     default:
1655       break;
1656     }
1657   }
1658
1659   RegisterPass<PredicateSimplifier> X("predsimplify",
1660                                       "Predicate Simplifier");
1661 }
1662
1663 FunctionPass *llvm::createPredicateSimplifierPass() {
1664   return new PredicateSimplifier();
1665 }