Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1 | //===-- PredicateSimplifier.cpp - Path Sensitive Simplifier ---------------===// |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 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 | // |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 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 | // |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 24 | // |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 25 | // The InequalityGraph focusses on four properties; equals, not equals, |
| 26 | // less-than and less-than-or-equals-to. The greater-than forms are also held |
| 27 | // just to allow walking from a lesser node to a greater one. These properties |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 28 | // are stored in a lattice; LE can become LT or EQ, NE can become LT or GT. |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 29 | // |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 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 |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 32 | // is how EQ relationships are stored; the map contains pointers from equal |
| 33 | // Value to the same node. The node contains a most canonical Value* form |
| 34 | // and the list of known relationships with other nodes. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 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 |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 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. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 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 | // |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 55 | // %P = icmp ne i32* %ptr, null |
| 56 | // %a = and i1 %P, %Q |
| 57 | // br i1 %a label %cond_true, label %cond_false |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 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 |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 62 | // branch it can't infer anything from the "and" instruction. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 70 | // |
| 71 | // The ValueRanges class stores the known integer bounds of a Value. When we |
| 72 | // encounter i8 %a u< %b, the ValueRanges stores that %a = [1, 255] and |
| 73 | // %b = [0, 254]. Because we store these by Value*, you should always |
| 74 | // canonicalize through the InequalityGraph first. |
| 75 | // |
| 76 | // It never stores an empty range, because that means that the code is |
| 77 | // unreachable. It never stores a single-element range since that's an equality |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 78 | // relationship and better stored in the InequalityGraph, nor an empty range |
| 79 | // since that is better stored in UnreachableBlocks. |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 80 | // |
| 81 | //===----------------------------------------------------------------------===// |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 82 | |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 83 | #define DEBUG_TYPE "predsimplify" |
| 84 | #include "llvm/Transforms/Scalar.h" |
| 85 | #include "llvm/Constants.h" |
Nick Lewycky | f345008 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 86 | #include "llvm/DerivedTypes.h" |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 87 | #include "llvm/Instructions.h" |
| 88 | #include "llvm/Pass.h" |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 89 | #include "llvm/ADT/DepthFirstIterator.h" |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 90 | #include "llvm/ADT/SetOperations.h" |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 91 | #include "llvm/ADT/SetVector.h" |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 92 | #include "llvm/ADT/Statistic.h" |
| 93 | #include "llvm/ADT/STLExtras.h" |
| 94 | #include "llvm/Analysis/Dominators.h" |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 95 | #include "llvm/Analysis/ET-Forest.h" |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 96 | #include "llvm/Support/CFG.h" |
Chris Lattner | f06bb65 | 2006-12-06 18:14:47 +0000 | [diff] [blame] | 97 | #include "llvm/Support/Compiler.h" |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 98 | #include "llvm/Support/ConstantRange.h" |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 99 | #include "llvm/Support/Debug.h" |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 100 | #include "llvm/Support/InstVisitor.h" |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 101 | #include "llvm/Target/TargetData.h" |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 102 | #include "llvm/Transforms/Utils/Local.h" |
| 103 | #include <algorithm> |
| 104 | #include <deque> |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 105 | #include <sstream> |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 106 | using namespace llvm; |
| 107 | |
Chris Lattner | 0e5255b | 2006-12-19 21:49:03 +0000 | [diff] [blame] | 108 | STATISTIC(NumVarsReplaced, "Number of argument substitutions"); |
| 109 | STATISTIC(NumInstruction , "Number of instructions removed"); |
| 110 | STATISTIC(NumSimple , "Number of simple replacements"); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 111 | STATISTIC(NumBlocks , "Number of blocks marked unreachable"); |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 112 | STATISTIC(NumSnuggle , "Number of comparisons snuggled"); |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 0e5255b | 2006-12-19 21:49:03 +0000 | [diff] [blame] | 114 | namespace { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 115 | // SLT SGT ULT UGT EQ |
| 116 | // 0 1 0 1 0 -- GT 10 |
| 117 | // 0 1 0 1 1 -- GE 11 |
| 118 | // 0 1 1 0 0 -- SGTULT 12 |
| 119 | // 0 1 1 0 1 -- SGEULE 13 |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 120 | // 0 1 1 1 0 -- SGT 14 |
| 121 | // 0 1 1 1 1 -- SGE 15 |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 122 | // 1 0 0 1 0 -- SLTUGT 18 |
| 123 | // 1 0 0 1 1 -- SLEUGE 19 |
| 124 | // 1 0 1 0 0 -- LT 20 |
| 125 | // 1 0 1 0 1 -- LE 21 |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 126 | // 1 0 1 1 0 -- SLT 22 |
| 127 | // 1 0 1 1 1 -- SLE 23 |
| 128 | // 1 1 0 1 0 -- UGT 26 |
| 129 | // 1 1 0 1 1 -- UGE 27 |
| 130 | // 1 1 1 0 0 -- ULT 28 |
| 131 | // 1 1 1 0 1 -- ULE 29 |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 132 | // 1 1 1 1 0 -- NE 30 |
| 133 | enum LatticeBits { |
| 134 | EQ_BIT = 1, UGT_BIT = 2, ULT_BIT = 4, SGT_BIT = 8, SLT_BIT = 16 |
| 135 | }; |
| 136 | enum LatticeVal { |
| 137 | GT = SGT_BIT | UGT_BIT, |
| 138 | GE = GT | EQ_BIT, |
| 139 | LT = SLT_BIT | ULT_BIT, |
| 140 | LE = LT | EQ_BIT, |
| 141 | NE = SLT_BIT | SGT_BIT | ULT_BIT | UGT_BIT, |
| 142 | SGTULT = SGT_BIT | ULT_BIT, |
| 143 | SGEULE = SGTULT | EQ_BIT, |
| 144 | SLTUGT = SLT_BIT | UGT_BIT, |
| 145 | SLEUGE = SLTUGT | EQ_BIT, |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 146 | ULT = SLT_BIT | SGT_BIT | ULT_BIT, |
| 147 | UGT = SLT_BIT | SGT_BIT | UGT_BIT, |
| 148 | SLT = SLT_BIT | ULT_BIT | UGT_BIT, |
| 149 | SGT = SGT_BIT | ULT_BIT | UGT_BIT, |
| 150 | SLE = SLT | EQ_BIT, |
| 151 | SGE = SGT | EQ_BIT, |
| 152 | ULE = ULT | EQ_BIT, |
| 153 | UGE = UGT | EQ_BIT |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static bool validPredicate(LatticeVal LV) { |
| 157 | switch (LV) { |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 158 | case GT: case GE: case LT: case LE: case NE: |
| 159 | case SGTULT: case SGT: case SGEULE: |
| 160 | case SLTUGT: case SLT: case SLEUGE: |
| 161 | case ULT: case UGT: |
| 162 | case SLE: case SGE: case ULE: case UGE: |
| 163 | return true; |
| 164 | default: |
| 165 | return false; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | /// reversePredicate - reverse the direction of the inequality |
| 170 | static LatticeVal reversePredicate(LatticeVal LV) { |
| 171 | unsigned reverse = LV ^ (SLT_BIT|SGT_BIT|ULT_BIT|UGT_BIT); //preserve EQ_BIT |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 172 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 173 | if ((reverse & (SLT_BIT|SGT_BIT)) == 0) |
| 174 | reverse |= (SLT_BIT|SGT_BIT); |
| 175 | |
| 176 | if ((reverse & (ULT_BIT|UGT_BIT)) == 0) |
| 177 | reverse |= (ULT_BIT|UGT_BIT); |
| 178 | |
| 179 | LatticeVal Rev = static_cast<LatticeVal>(reverse); |
| 180 | assert(validPredicate(Rev) && "Failed reversing predicate."); |
| 181 | return Rev; |
| 182 | } |
| 183 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 184 | /// This is a StrictWeakOrdering predicate that sorts ETNodes by how many |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 185 | /// descendants they have. With this, you can iterate through a list sorted |
| 186 | /// by this operation and the first matching entry is the most specific |
| 187 | /// match for your basic block. The order provided is stable; ETNodes with |
| 188 | /// the same number of children are sorted by pointer address. |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 189 | struct VISIBILITY_HIDDEN OrderByDominance { |
| 190 | bool operator()(const ETNode *LHS, const ETNode *RHS) const { |
| 191 | unsigned LHS_spread = LHS->getDFSNumOut() - LHS->getDFSNumIn(); |
| 192 | unsigned RHS_spread = RHS->getDFSNumOut() - RHS->getDFSNumIn(); |
| 193 | if (LHS_spread != RHS_spread) return LHS_spread < RHS_spread; |
| 194 | else return LHS < RHS; |
| 195 | } |
| 196 | }; |
| 197 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 198 | /// The InequalityGraph stores the relationships between values. |
| 199 | /// Each Value in the graph is assigned to a Node. Nodes are pointer |
| 200 | /// comparable for equality. The caller is expected to maintain the logical |
| 201 | /// consistency of the system. |
| 202 | /// |
| 203 | /// The InequalityGraph class may invalidate Node*s after any mutator call. |
| 204 | /// @brief The InequalityGraph stores the relationships between values. |
| 205 | class VISIBILITY_HIDDEN InequalityGraph { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 206 | ETNode *TreeRoot; |
| 207 | |
| 208 | InequalityGraph(); // DO NOT IMPLEMENT |
| 209 | InequalityGraph(InequalityGraph &); // DO NOT IMPLEMENT |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 210 | public: |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 211 | explicit InequalityGraph(ETNode *TreeRoot) : TreeRoot(TreeRoot) {} |
| 212 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 213 | class Node; |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 214 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 215 | /// An Edge is contained inside a Node making one end of the edge implicit |
| 216 | /// and contains a pointer to the other end. The edge contains a lattice |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 217 | /// value specifying the relationship and an ETNode specifying the root |
| 218 | /// in the dominator tree to which this edge applies. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 219 | class VISIBILITY_HIDDEN Edge { |
| 220 | public: |
| 221 | Edge(unsigned T, LatticeVal V, ETNode *ST) |
| 222 | : To(T), LV(V), Subtree(ST) {} |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 223 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 224 | unsigned To; |
| 225 | LatticeVal LV; |
| 226 | ETNode *Subtree; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 227 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 228 | bool operator<(const Edge &edge) const { |
| 229 | if (To != edge.To) return To < edge.To; |
| 230 | else return OrderByDominance()(Subtree, edge.Subtree); |
| 231 | } |
| 232 | bool operator<(unsigned to) const { |
| 233 | return To < to; |
| 234 | } |
| 235 | }; |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 236 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 237 | /// A single node in the InequalityGraph. This stores the canonical Value |
| 238 | /// for the node, as well as the relationships with the neighbours. |
| 239 | /// |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 240 | /// @brief A single node in the InequalityGraph. |
| 241 | class VISIBILITY_HIDDEN Node { |
| 242 | friend class InequalityGraph; |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 243 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 244 | typedef SmallVector<Edge, 4> RelationsType; |
| 245 | RelationsType Relations; |
| 246 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 247 | Value *Canonical; |
Nick Lewycky | cfff1c3 | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 248 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 249 | // TODO: can this idea improve performance? |
| 250 | //friend class std::vector<Node>; |
| 251 | //Node(Node &N) { RelationsType.swap(N.RelationsType); } |
| 252 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 253 | public: |
| 254 | typedef RelationsType::iterator iterator; |
| 255 | typedef RelationsType::const_iterator const_iterator; |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 256 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 257 | Node(Value *V) : Canonical(V) {} |
| 258 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 259 | private: |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 260 | #ifndef NDEBUG |
| 261 | public: |
Nick Lewycky | 5d6ede5 | 2007-01-11 02:38:21 +0000 | [diff] [blame] | 262 | virtual ~Node() {} |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 263 | virtual void dump() const { |
| 264 | dump(*cerr.stream()); |
| 265 | } |
| 266 | private: |
| 267 | void dump(std::ostream &os) const { |
| 268 | os << *getValue() << ":\n"; |
| 269 | for (Node::const_iterator NI = begin(), NE = end(); NI != NE; ++NI) { |
| 270 | static const std::string names[32] = |
| 271 | { "000000", "000001", "000002", "000003", "000004", "000005", |
| 272 | "000006", "000007", "000008", "000009", " >", " >=", |
| 273 | " s>u<", "s>=u<=", " s>", " s>=", "000016", "000017", |
| 274 | " s<u>", "s<=u>=", " <", " <=", " s<", " s<=", |
| 275 | "000024", "000025", " u>", " u>=", " u<", " u<=", |
| 276 | " !=", "000031" }; |
| 277 | os << " " << names[NI->LV] << " " << NI->To |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 278 | << " (" << NI->Subtree->getDFSNumIn() << ")\n"; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | public: |
| 284 | iterator begin() { return Relations.begin(); } |
| 285 | iterator end() { return Relations.end(); } |
| 286 | const_iterator begin() const { return Relations.begin(); } |
| 287 | const_iterator end() const { return Relations.end(); } |
| 288 | |
| 289 | iterator find(unsigned n, ETNode *Subtree) { |
| 290 | iterator E = end(); |
| 291 | for (iterator I = std::lower_bound(begin(), E, n); |
| 292 | I != E && I->To == n; ++I) { |
| 293 | if (Subtree->DominatedBy(I->Subtree)) |
| 294 | return I; |
| 295 | } |
| 296 | return E; |
| 297 | } |
| 298 | |
| 299 | const_iterator find(unsigned n, ETNode *Subtree) const { |
| 300 | const_iterator E = end(); |
| 301 | for (const_iterator I = std::lower_bound(begin(), E, n); |
| 302 | I != E && I->To == n; ++I) { |
| 303 | if (Subtree->DominatedBy(I->Subtree)) |
| 304 | return I; |
| 305 | } |
| 306 | return E; |
| 307 | } |
| 308 | |
| 309 | Value *getValue() const |
| 310 | { |
| 311 | return Canonical; |
| 312 | } |
| 313 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 314 | /// Updates the lattice value for a given node. Create a new entry if |
| 315 | /// one doesn't exist, otherwise it merges the values. The new lattice |
| 316 | /// value must not be inconsistent with any previously existing value. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 317 | void update(unsigned n, LatticeVal R, ETNode *Subtree) { |
| 318 | assert(validPredicate(R) && "Invalid predicate."); |
| 319 | iterator I = find(n, Subtree); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 320 | if (I == end()) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 321 | Edge edge(n, R, Subtree); |
| 322 | iterator Insert = std::lower_bound(begin(), end(), edge); |
| 323 | Relations.insert(Insert, edge); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 324 | } else { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 325 | LatticeVal LV = static_cast<LatticeVal>(I->LV & R); |
| 326 | assert(validPredicate(LV) && "Invalid union of lattice values."); |
| 327 | if (LV != I->LV) { |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 328 | if (Subtree != I->Subtree) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 329 | assert(Subtree->DominatedBy(I->Subtree) && |
| 330 | "Find returned subtree that doesn't apply."); |
| 331 | |
| 332 | Edge edge(n, R, Subtree); |
| 333 | iterator Insert = std::lower_bound(begin(), end(), edge); |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 334 | Relations.insert(Insert, edge); // invalidates I |
| 335 | I = find(n, Subtree); |
| 336 | } |
| 337 | |
| 338 | // Also, we have to tighten any edge that Subtree dominates. |
| 339 | for (iterator B = begin(); I->To == n; --I) { |
| 340 | if (I->Subtree->DominatedBy(Subtree)) { |
| 341 | LatticeVal LV = static_cast<LatticeVal>(I->LV & R); |
Chris Lattner | 28d921d | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 342 | assert(validPredicate(LV) && "Invalid union of lattice values"); |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 343 | I->LV = LV; |
| 344 | } |
| 345 | if (I == B) break; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Nick Lewycky | 51ce8d6 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 348 | } |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 349 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 352 | private: |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 353 | struct VISIBILITY_HIDDEN NodeMapEdge { |
| 354 | Value *V; |
| 355 | unsigned index; |
| 356 | ETNode *Subtree; |
| 357 | |
| 358 | NodeMapEdge(Value *V, unsigned index, ETNode *Subtree) |
| 359 | : V(V), index(index), Subtree(Subtree) {} |
| 360 | |
| 361 | bool operator==(const NodeMapEdge &RHS) const { |
| 362 | return V == RHS.V && |
| 363 | Subtree == RHS.Subtree; |
| 364 | } |
| 365 | |
| 366 | bool operator<(const NodeMapEdge &RHS) const { |
| 367 | if (V != RHS.V) return V < RHS.V; |
| 368 | return OrderByDominance()(Subtree, RHS.Subtree); |
| 369 | } |
| 370 | |
| 371 | bool operator<(Value *RHS) const { |
| 372 | return V < RHS; |
| 373 | } |
| 374 | }; |
| 375 | |
| 376 | typedef std::vector<NodeMapEdge> NodeMapType; |
| 377 | NodeMapType NodeMap; |
| 378 | |
| 379 | std::vector<Node> Nodes; |
| 380 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 381 | public: |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 382 | /// node - returns the node object at a given index retrieved from getNode. |
| 383 | /// Index zero is reserved and may not be passed in here. The pointer |
| 384 | /// returned is valid until the next call to newNode or getOrInsertNode. |
| 385 | Node *node(unsigned index) { |
| 386 | assert(index != 0 && "Zero index is reserved for not found."); |
| 387 | assert(index <= Nodes.size() && "Index out of range."); |
| 388 | return &Nodes[index-1]; |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 389 | } |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 390 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 391 | /// Returns the node currently representing Value V, or zero if no such |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 392 | /// node exists. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 393 | unsigned getNode(Value *V, ETNode *Subtree) { |
| 394 | NodeMapType::iterator E = NodeMap.end(); |
| 395 | NodeMapEdge Edge(V, 0, Subtree); |
| 396 | NodeMapType::iterator I = std::lower_bound(NodeMap.begin(), E, Edge); |
| 397 | while (I != E && I->V == V) { |
| 398 | if (Subtree->DominatedBy(I->Subtree)) |
| 399 | return I->index; |
| 400 | ++I; |
| 401 | } |
| 402 | return 0; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 405 | /// getOrInsertNode - always returns a valid node index, creating a node |
| 406 | /// to match the Value if needed. |
| 407 | unsigned getOrInsertNode(Value *V, ETNode *Subtree) { |
| 408 | if (unsigned n = getNode(V, Subtree)) |
| 409 | return n; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 410 | else |
| 411 | return newNode(V); |
| 412 | } |
| 413 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 414 | /// newNode - creates a new node for a given Value and returns the index. |
| 415 | unsigned newNode(Value *V) { |
| 416 | Nodes.push_back(Node(V)); |
| 417 | |
| 418 | NodeMapEdge MapEntry = NodeMapEdge(V, Nodes.size(), TreeRoot); |
| 419 | assert(!std::binary_search(NodeMap.begin(), NodeMap.end(), MapEntry) && |
| 420 | "Attempt to create a duplicate Node."); |
| 421 | NodeMap.insert(std::lower_bound(NodeMap.begin(), NodeMap.end(), |
| 422 | MapEntry), MapEntry); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 423 | return MapEntry.index; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 426 | /// If the Value is in the graph, return the canonical form. Otherwise, |
| 427 | /// return the original Value. |
| 428 | Value *canonicalize(Value *V, ETNode *Subtree) { |
| 429 | if (isa<Constant>(V)) return V; |
| 430 | |
| 431 | if (unsigned n = getNode(V, Subtree)) |
| 432 | return node(n)->getValue(); |
| 433 | else |
| 434 | return V; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 437 | /// isRelatedBy - true iff n1 op n2 |
| 438 | bool isRelatedBy(unsigned n1, unsigned n2, ETNode *Subtree, LatticeVal LV) { |
| 439 | if (n1 == n2) return LV & EQ_BIT; |
| 440 | |
| 441 | Node *N1 = node(n1); |
| 442 | Node::iterator I = N1->find(n2, Subtree), E = N1->end(); |
| 443 | if (I != E) return (I->LV & LV) == I->LV; |
| 444 | |
Nick Lewycky | cfff1c3 | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 448 | // The add* methods assume that your input is logically valid and may |
| 449 | // assertion-fail or infinitely loop if you attempt a contradiction. |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 450 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 451 | void addEquality(unsigned n, Value *V, ETNode *Subtree) { |
| 452 | assert(canonicalize(node(n)->getValue(), Subtree) == node(n)->getValue() |
| 453 | && "Node's 'canonical' choice isn't best within this subtree."); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 454 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 455 | // Suppose that we are given "%x -> node #1 (%y)". The problem is that |
| 456 | // we may already have "%z -> node #2 (%x)" somewhere above us in the |
| 457 | // graph. We need to find those edges and add "%z -> node #1 (%y)" |
| 458 | // to keep the lookups canonical. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 459 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 460 | std::vector<Value *> ToRepoint; |
| 461 | ToRepoint.push_back(V); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 462 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 463 | if (unsigned Conflict = getNode(V, Subtree)) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 464 | for (NodeMapType::iterator I = NodeMap.begin(), E = NodeMap.end(); |
| 465 | I != E; ++I) { |
| 466 | if (I->index == Conflict && Subtree->DominatedBy(I->Subtree)) |
| 467 | ToRepoint.push_back(I->V); |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 470 | |
| 471 | for (std::vector<Value *>::iterator VI = ToRepoint.begin(), |
| 472 | VE = ToRepoint.end(); VI != VE; ++VI) { |
| 473 | Value *V = *VI; |
| 474 | |
| 475 | // XXX: review this code. This may be doing too many insertions. |
| 476 | NodeMapEdge Edge(V, n, Subtree); |
| 477 | NodeMapType::iterator E = NodeMap.end(); |
| 478 | NodeMapType::iterator I = std::lower_bound(NodeMap.begin(), E, Edge); |
| 479 | if (I == E || I->V != V || I->Subtree != Subtree) { |
| 480 | // New Value |
| 481 | NodeMap.insert(I, Edge); |
| 482 | } else if (I != E && I->V == V && I->Subtree == Subtree) { |
| 483 | // Update best choice |
| 484 | I->index = n; |
| 485 | } |
| 486 | |
| 487 | #ifndef NDEBUG |
| 488 | Node *N = node(n); |
| 489 | if (isa<Constant>(V)) { |
| 490 | if (isa<Constant>(N->getValue())) { |
| 491 | assert(V == N->getValue() && "Constant equals different constant?"); |
| 492 | } |
| 493 | } |
| 494 | #endif |
| 495 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 498 | /// addInequality - Sets n1 op n2. |
| 499 | /// It is also an error to call this on an inequality that is already true. |
| 500 | void addInequality(unsigned n1, unsigned n2, ETNode *Subtree, |
| 501 | LatticeVal LV1) { |
| 502 | assert(n1 != n2 && "A node can't be inequal to itself."); |
| 503 | |
| 504 | if (LV1 != NE) |
| 505 | assert(!isRelatedBy(n1, n2, Subtree, reversePredicate(LV1)) && |
| 506 | "Contradictory inequality."); |
| 507 | |
| 508 | Node *N1 = node(n1); |
| 509 | Node *N2 = node(n2); |
| 510 | |
| 511 | // Suppose we're adding %n1 < %n2. Find all the %a < %n1 and |
| 512 | // add %a < %n2 too. This keeps the graph fully connected. |
| 513 | if (LV1 != NE) { |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 514 | // Break up the relationship into signed and unsigned comparison parts. |
| 515 | // If the signed parts of %a op1 %n1 match that of %n1 op2 %n2, and |
| 516 | // op1 and op2 aren't NE, then add %a op3 %n2. The new relationship |
| 517 | // should have the EQ_BIT iff it's set for both op1 and op2. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 518 | |
| 519 | unsigned LV1_s = LV1 & (SLT_BIT|SGT_BIT); |
| 520 | unsigned LV1_u = LV1 & (ULT_BIT|UGT_BIT); |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 521 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 522 | for (Node::iterator I = N1->begin(), E = N1->end(); I != E; ++I) { |
| 523 | if (I->LV != NE && I->To != n2) { |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 524 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 525 | ETNode *Local_Subtree = NULL; |
| 526 | if (Subtree->DominatedBy(I->Subtree)) |
| 527 | Local_Subtree = Subtree; |
| 528 | else if (I->Subtree->DominatedBy(Subtree)) |
| 529 | Local_Subtree = I->Subtree; |
| 530 | |
| 531 | if (Local_Subtree) { |
| 532 | unsigned new_relationship = 0; |
| 533 | LatticeVal ILV = reversePredicate(I->LV); |
| 534 | unsigned ILV_s = ILV & (SLT_BIT|SGT_BIT); |
| 535 | unsigned ILV_u = ILV & (ULT_BIT|UGT_BIT); |
| 536 | |
| 537 | if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s) |
| 538 | new_relationship |= ILV_s; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 539 | if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u) |
| 540 | new_relationship |= ILV_u; |
| 541 | |
| 542 | if (new_relationship) { |
| 543 | if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0) |
| 544 | new_relationship |= (SLT_BIT|SGT_BIT); |
| 545 | if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0) |
| 546 | new_relationship |= (ULT_BIT|UGT_BIT); |
| 547 | if ((LV1 & EQ_BIT) && (ILV & EQ_BIT)) |
| 548 | new_relationship |= EQ_BIT; |
| 549 | |
| 550 | LatticeVal NewLV = static_cast<LatticeVal>(new_relationship); |
| 551 | |
| 552 | node(I->To)->update(n2, NewLV, Local_Subtree); |
| 553 | N2->update(I->To, reversePredicate(NewLV), Local_Subtree); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | for (Node::iterator I = N2->begin(), E = N2->end(); I != E; ++I) { |
| 560 | if (I->LV != NE && I->To != n1) { |
| 561 | ETNode *Local_Subtree = NULL; |
| 562 | if (Subtree->DominatedBy(I->Subtree)) |
| 563 | Local_Subtree = Subtree; |
| 564 | else if (I->Subtree->DominatedBy(Subtree)) |
| 565 | Local_Subtree = I->Subtree; |
| 566 | |
| 567 | if (Local_Subtree) { |
| 568 | unsigned new_relationship = 0; |
| 569 | unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT); |
| 570 | unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT); |
| 571 | |
| 572 | if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s) |
| 573 | new_relationship |= ILV_s; |
| 574 | |
| 575 | if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u) |
| 576 | new_relationship |= ILV_u; |
| 577 | |
| 578 | if (new_relationship) { |
| 579 | if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0) |
| 580 | new_relationship |= (SLT_BIT|SGT_BIT); |
| 581 | if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0) |
| 582 | new_relationship |= (ULT_BIT|UGT_BIT); |
| 583 | if ((LV1 & EQ_BIT) && (I->LV & EQ_BIT)) |
| 584 | new_relationship |= EQ_BIT; |
| 585 | |
| 586 | LatticeVal NewLV = static_cast<LatticeVal>(new_relationship); |
| 587 | |
| 588 | N1->update(I->To, NewLV, Local_Subtree); |
| 589 | node(I->To)->update(n1, reversePredicate(NewLV), Local_Subtree); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | N1->update(n2, LV1, Subtree); |
| 597 | N2->update(n1, reversePredicate(LV1), Subtree); |
| 598 | } |
Nick Lewycky | 51ce8d6 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 599 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 600 | /// remove - Removes a Value from the graph. If the value is the canonical |
| 601 | /// choice for a Node, destroys the Node from the graph deleting all edges |
| 602 | /// to and from it. This method does not renumber the nodes. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 603 | void remove(Value *V) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 604 | for (unsigned i = 0; i < NodeMap.size();) { |
| 605 | NodeMapType::iterator I = NodeMap.begin()+i; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 606 | if (I->V == V) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 607 | Node *N = node(I->index); |
| 608 | if (node(I->index)->getValue() == V) { |
| 609 | for (Node::iterator NI = N->begin(), NE = N->end(); NI != NE; ++NI){ |
| 610 | Node::iterator Iter = node(NI->To)->find(I->index, TreeRoot); |
| 611 | do { |
| 612 | node(NI->To)->Relations.erase(Iter); |
| 613 | Iter = node(NI->To)->find(I->index, TreeRoot); |
| 614 | } while (Iter != node(NI->To)->end()); |
| 615 | } |
| 616 | N->Canonical = NULL; |
| 617 | } |
| 618 | N->Relations.clear(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 619 | NodeMap.erase(I); |
| 620 | } else ++i; |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 621 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 622 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 623 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 624 | #ifndef NDEBUG |
Nick Lewycky | 5d6ede5 | 2007-01-11 02:38:21 +0000 | [diff] [blame] | 625 | virtual ~InequalityGraph() {} |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 626 | virtual void dump() { |
| 627 | dump(*cerr.stream()); |
| 628 | } |
| 629 | |
| 630 | void dump(std::ostream &os) { |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 631 | std::set<Node *> VisitedNodes; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 632 | for (NodeMapType::const_iterator I = NodeMap.begin(), E = NodeMap.end(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 633 | I != E; ++I) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 634 | Node *N = node(I->index); |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 635 | os << *I->V << " == " << I->index |
| 636 | << "(" << I->Subtree->getDFSNumIn() << ")\n"; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 637 | if (VisitedNodes.insert(N).second) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 638 | os << I->index << ". "; |
| 639 | if (!N->getValue()) os << "(deleted node)\n"; |
| 640 | else N->dump(os); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
| 644 | #endif |
| 645 | }; |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 646 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 647 | class VRPSolver; |
| 648 | |
| 649 | /// ValueRanges tracks the known integer ranges and anti-ranges of the nodes |
| 650 | /// in the InequalityGraph. |
| 651 | class VISIBILITY_HIDDEN ValueRanges { |
| 652 | |
| 653 | /// A ScopedRange ties an InequalityGraph node with a ConstantRange under |
| 654 | /// the scope of a rooted subtree in the dominator tree. |
| 655 | class VISIBILITY_HIDDEN ScopedRange { |
| 656 | public: |
| 657 | ScopedRange(Value *V, ConstantRange CR, ETNode *ST) |
| 658 | : V(V), CR(CR), Subtree(ST) {} |
| 659 | |
| 660 | Value *V; |
| 661 | ConstantRange CR; |
| 662 | ETNode *Subtree; |
| 663 | |
| 664 | bool operator<(const ScopedRange &range) const { |
| 665 | if (V != range.V) return V < range.V; |
| 666 | else return OrderByDominance()(Subtree, range.Subtree); |
| 667 | } |
| 668 | |
| 669 | bool operator<(const Value *value) const { |
| 670 | return V < value; |
| 671 | } |
| 672 | }; |
| 673 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 674 | TargetData *TD; |
| 675 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 676 | std::vector<ScopedRange> Ranges; |
| 677 | typedef std::vector<ScopedRange>::iterator iterator; |
| 678 | |
| 679 | // XXX: this is a copy of the code in InequalityGraph::Node. Perhaps a |
| 680 | // intrusive domtree-scoped container is in order? |
| 681 | |
| 682 | iterator begin() { return Ranges.begin(); } |
| 683 | iterator end() { return Ranges.end(); } |
| 684 | |
| 685 | iterator find(Value *V, ETNode *Subtree) { |
| 686 | iterator E = end(); |
| 687 | for (iterator I = std::lower_bound(begin(), E, V); |
| 688 | I != E && I->V == V; ++I) { |
| 689 | if (Subtree->DominatedBy(I->Subtree)) |
| 690 | return I; |
| 691 | } |
| 692 | return E; |
| 693 | } |
| 694 | |
| 695 | void update(Value *V, ConstantRange CR, ETNode *Subtree) { |
| 696 | assert(!CR.isEmptySet() && "Empty ConstantRange!"); |
| 697 | if (CR.isFullSet()) return; |
| 698 | |
| 699 | iterator I = find(V, Subtree); |
| 700 | if (I == end()) { |
| 701 | ScopedRange range(V, CR, Subtree); |
| 702 | iterator Insert = std::lower_bound(begin(), end(), range); |
| 703 | Ranges.insert(Insert, range); |
| 704 | } else { |
| 705 | CR = CR.intersectWith(I->CR); |
| 706 | assert(!CR.isEmptySet() && "Empty intersection of ConstantRanges!"); |
| 707 | |
| 708 | if (CR != I->CR) { |
| 709 | if (Subtree != I->Subtree) { |
| 710 | assert(Subtree->DominatedBy(I->Subtree) && |
| 711 | "Find returned subtree that doesn't apply."); |
| 712 | |
| 713 | ScopedRange range(V, CR, Subtree); |
| 714 | iterator Insert = std::lower_bound(begin(), end(), range); |
| 715 | Ranges.insert(Insert, range); // invalidates I |
| 716 | I = find(V, Subtree); |
| 717 | } |
| 718 | |
| 719 | // Also, we have to tighten any edge that Subtree dominates. |
| 720 | for (iterator B = begin(); I->V == V; --I) { |
| 721 | if (I->Subtree->DominatedBy(Subtree)) { |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 722 | I->CR = CR.intersectWith(I->CR); |
| 723 | assert(!I->CR.isEmptySet() && |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 724 | "Empty intersection of ConstantRanges!"); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 725 | } |
| 726 | if (I == B) break; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /// range - Creates a ConstantRange representing the set of all values |
| 733 | /// that match the ICmpInst::Predicate with any of the values in CR. |
| 734 | ConstantRange range(ICmpInst::Predicate ICmpOpcode, |
| 735 | const ConstantRange &CR) { |
| 736 | uint32_t W = CR.getBitWidth(); |
| 737 | switch (ICmpOpcode) { |
| 738 | default: assert(!"Invalid ICmp opcode to range()"); |
| 739 | case ICmpInst::ICMP_EQ: |
| 740 | return ConstantRange(CR.getLower(), CR.getUpper()); |
| 741 | case ICmpInst::ICMP_NE: |
| 742 | if (CR.isSingleElement()) |
| 743 | return ConstantRange(CR.getUpper(), CR.getLower()); |
| 744 | return ConstantRange(W); |
| 745 | case ICmpInst::ICMP_ULT: |
| 746 | return ConstantRange(APInt::getMinValue(W), CR.getUnsignedMax()); |
| 747 | case ICmpInst::ICMP_SLT: |
| 748 | return ConstantRange(APInt::getSignedMinValue(W), CR.getSignedMax()); |
| 749 | case ICmpInst::ICMP_ULE: { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 750 | APInt UMax(CR.getUnsignedMax()); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 751 | if (UMax == APInt::getMaxValue(W)) |
| 752 | return ConstantRange(W); |
| 753 | return ConstantRange(APInt::getMinValue(W), UMax + 1); |
| 754 | } |
| 755 | case ICmpInst::ICMP_SLE: { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 756 | APInt SMax(CR.getSignedMax()); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 757 | if (SMax == APInt::getSignedMaxValue(W) || |
| 758 | SMax + 1 == APInt::getSignedMaxValue(W)) |
| 759 | return ConstantRange(W); |
| 760 | return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); |
| 761 | } |
| 762 | case ICmpInst::ICMP_UGT: |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 763 | return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W)); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 764 | case ICmpInst::ICMP_SGT: |
| 765 | return ConstantRange(CR.getSignedMin() + 1, |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 766 | APInt::getSignedMinValue(W)); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 767 | case ICmpInst::ICMP_UGE: { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 768 | APInt UMin(CR.getUnsignedMin()); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 769 | if (UMin == APInt::getMinValue(W)) |
| 770 | return ConstantRange(W); |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 771 | return ConstantRange(UMin, APInt::getNullValue(W)); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 772 | } |
| 773 | case ICmpInst::ICMP_SGE: { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 774 | APInt SMin(CR.getSignedMin()); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 775 | if (SMin == APInt::getSignedMinValue(W)) |
| 776 | return ConstantRange(W); |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 777 | return ConstantRange(SMin, APInt::getSignedMinValue(W)); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /// create - Creates a ConstantRange that matches the given LatticeVal |
| 783 | /// relation with a given integer. |
| 784 | ConstantRange create(LatticeVal LV, const ConstantRange &CR) { |
| 785 | assert(!CR.isEmptySet() && "Can't deal with empty set."); |
| 786 | |
| 787 | if (LV == NE) |
| 788 | return range(ICmpInst::ICMP_NE, CR); |
| 789 | |
| 790 | unsigned LV_s = LV & (SGT_BIT|SLT_BIT); |
| 791 | unsigned LV_u = LV & (UGT_BIT|ULT_BIT); |
| 792 | bool hasEQ = LV & EQ_BIT; |
| 793 | |
| 794 | ConstantRange Range(CR.getBitWidth()); |
| 795 | |
| 796 | if (LV_s == SGT_BIT) { |
| 797 | Range = Range.intersectWith(range( |
| 798 | hasEQ ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SGT, CR)); |
| 799 | } else if (LV_s == SLT_BIT) { |
| 800 | Range = Range.intersectWith(range( |
| 801 | hasEQ ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_SLT, CR)); |
| 802 | } |
| 803 | |
| 804 | if (LV_u == UGT_BIT) { |
| 805 | Range = Range.intersectWith(range( |
| 806 | hasEQ ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_UGT, CR)); |
| 807 | } else if (LV_u == ULT_BIT) { |
| 808 | Range = Range.intersectWith(range( |
| 809 | hasEQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT, CR)); |
| 810 | } |
| 811 | |
| 812 | return Range; |
| 813 | } |
| 814 | |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 815 | #ifndef NDEBUG |
| 816 | bool isCanonical(Value *V, ETNode *Subtree, VRPSolver *VRP); |
| 817 | #endif |
| 818 | |
| 819 | public: |
| 820 | |
| 821 | explicit ValueRanges(TargetData *TD) : TD(TD) {} |
| 822 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 823 | // rangeFromValue - converts a Value into a range. If the value is a |
| 824 | // constant it constructs the single element range, otherwise it performs |
| 825 | // a lookup. The width W must be retrieved from typeToWidth and may not |
| 826 | // be zero. |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 827 | ConstantRange rangeFromValue(Value *V, ETNode *Subtree, uint32_t W) { |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 828 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 829 | return ConstantRange(C->getValue()); |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 830 | } else if (isa<ConstantPointerNull>(V)) { |
| 831 | return ConstantRange(APInt::getNullValue(W)); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 832 | } else { |
| 833 | iterator I = find(V, Subtree); |
| 834 | if (I != end()) |
| 835 | return I->CR; |
| 836 | } |
| 837 | return ConstantRange(W); |
| 838 | } |
| 839 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 840 | // typeToWidth - returns the number of bits necessary to store a value of |
| 841 | // this type, or zero if unknown. |
| 842 | uint32_t typeToWidth(const Type *Ty) const { |
| 843 | if (TD) |
| 844 | return TD->getTypeSizeInBits(Ty); |
| 845 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 846 | if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) |
| 847 | return ITy->getBitWidth(); |
| 848 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 849 | return 0; |
| 850 | } |
| 851 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 852 | bool isRelatedBy(Value *V1, Value *V2, ETNode *Subtree, LatticeVal LV) { |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 853 | uint32_t W = typeToWidth(V1->getType()); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 854 | if (!W) return false; |
| 855 | |
| 856 | ConstantRange CR1 = rangeFromValue(V1, Subtree, W); |
| 857 | ConstantRange CR2 = rangeFromValue(V2, Subtree, W); |
| 858 | |
| 859 | // True iff all values in CR1 are LV to all values in CR2. |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 860 | switch (LV) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 861 | default: assert(!"Impossible lattice value!"); |
| 862 | case NE: |
| 863 | return CR1.intersectWith(CR2).isEmptySet(); |
| 864 | case ULT: |
| 865 | return CR1.getUnsignedMax().ult(CR2.getUnsignedMin()); |
| 866 | case ULE: |
| 867 | return CR1.getUnsignedMax().ule(CR2.getUnsignedMin()); |
| 868 | case UGT: |
| 869 | return CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()); |
| 870 | case UGE: |
| 871 | return CR1.getUnsignedMin().uge(CR2.getUnsignedMax()); |
| 872 | case SLT: |
| 873 | return CR1.getSignedMax().slt(CR2.getSignedMin()); |
| 874 | case SLE: |
| 875 | return CR1.getSignedMax().sle(CR2.getSignedMin()); |
| 876 | case SGT: |
| 877 | return CR1.getSignedMin().sgt(CR2.getSignedMax()); |
| 878 | case SGE: |
| 879 | return CR1.getSignedMin().sge(CR2.getSignedMax()); |
| 880 | case LT: |
| 881 | return CR1.getUnsignedMax().ult(CR2.getUnsignedMin()) && |
| 882 | CR1.getSignedMax().slt(CR2.getUnsignedMin()); |
| 883 | case LE: |
| 884 | return CR1.getUnsignedMax().ule(CR2.getUnsignedMin()) && |
| 885 | CR1.getSignedMax().sle(CR2.getUnsignedMin()); |
| 886 | case GT: |
| 887 | return CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()) && |
| 888 | CR1.getSignedMin().sgt(CR2.getSignedMax()); |
| 889 | case GE: |
| 890 | return CR1.getUnsignedMin().uge(CR2.getUnsignedMax()) && |
| 891 | CR1.getSignedMin().sge(CR2.getSignedMax()); |
| 892 | case SLTUGT: |
| 893 | return CR1.getSignedMax().slt(CR2.getSignedMin()) && |
| 894 | CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()); |
| 895 | case SLEUGE: |
| 896 | return CR1.getSignedMax().sle(CR2.getSignedMin()) && |
| 897 | CR1.getUnsignedMin().uge(CR2.getUnsignedMax()); |
| 898 | case SGTULT: |
| 899 | return CR1.getSignedMin().sgt(CR2.getSignedMax()) && |
| 900 | CR1.getUnsignedMax().ult(CR2.getUnsignedMin()); |
| 901 | case SGEULE: |
| 902 | return CR1.getSignedMin().sge(CR2.getSignedMax()) && |
| 903 | CR1.getUnsignedMax().ule(CR2.getUnsignedMin()); |
| 904 | } |
| 905 | } |
| 906 | |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 907 | void addToWorklist(Value *V, Constant *C, ICmpInst::Predicate Pred, |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 908 | VRPSolver *VRP); |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 909 | void markBlock(VRPSolver *VRP); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 910 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 911 | void mergeInto(Value **I, unsigned n, Value *New, ETNode *Subtree, |
| 912 | VRPSolver *VRP) { |
| 913 | assert(isCanonical(New, Subtree, VRP) && "Best choice not canonical?"); |
| 914 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 915 | uint32_t W = typeToWidth(New->getType()); |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 916 | if (!W) return; |
| 917 | |
| 918 | ConstantRange CR_New = rangeFromValue(New, Subtree, W); |
| 919 | ConstantRange Merged = CR_New; |
| 920 | |
| 921 | for (; n != 0; ++I, --n) { |
| 922 | ConstantRange CR_Kill = rangeFromValue(*I, Subtree, W); |
| 923 | if (CR_Kill.isFullSet()) continue; |
| 924 | Merged = Merged.intersectWith(CR_Kill); |
| 925 | } |
| 926 | |
| 927 | if (Merged.isFullSet() || Merged == CR_New) return; |
| 928 | |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 929 | applyRange(New, Merged, Subtree, VRP); |
| 930 | } |
| 931 | |
| 932 | void applyRange(Value *V, const ConstantRange &CR, ETNode *Subtree, |
| 933 | VRPSolver *VRP) { |
| 934 | assert(isCanonical(V, Subtree, VRP) && "Value not canonical."); |
| 935 | |
| 936 | if (const APInt *I = CR.getSingleElement()) { |
| 937 | const Type *Ty = V->getType(); |
| 938 | if (Ty->isInteger()) { |
| 939 | addToWorklist(V, ConstantInt::get(*I), ICmpInst::ICMP_EQ, VRP); |
| 940 | return; |
| 941 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 942 | assert(*I == 0 && "Pointer is null but not zero?"); |
| 943 | addToWorklist(V, ConstantPointerNull::get(PTy), |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 944 | ICmpInst::ICMP_EQ, VRP); |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | } |
| 948 | |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 949 | ConstantRange Merged = CR.intersectWith( |
| 950 | rangeFromValue(V, Subtree, CR.getBitWidth())); |
| 951 | if (Merged.isEmptySet()) { |
| 952 | markBlock(VRP); |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | update(V, Merged, Subtree); |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Nick Lewycky | 93f5410 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 959 | void addNotEquals(Value *V1, Value *V2, ETNode *Subtree, VRPSolver *VRP) { |
| 960 | uint32_t W = typeToWidth(V1->getType()); |
| 961 | if (!W) return; |
| 962 | |
| 963 | ConstantRange CR1 = rangeFromValue(V1, Subtree, W); |
| 964 | ConstantRange CR2 = rangeFromValue(V2, Subtree, W); |
| 965 | |
| 966 | if (const APInt *I = CR1.getSingleElement()) { |
| 967 | if (CR2.isFullSet()) { |
| 968 | ConstantRange NewCR2(CR1.getUpper(), CR1.getLower()); |
| 969 | applyRange(V2, NewCR2, Subtree, VRP); |
| 970 | } else if (*I == CR2.getLower()) { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 971 | APInt NewLower(CR2.getLower() + 1), |
| 972 | NewUpper(CR2.getUpper()); |
Nick Lewycky | 93f5410 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 973 | if (NewLower == NewUpper) |
| 974 | NewLower = NewUpper = APInt::getMinValue(W); |
| 975 | |
| 976 | ConstantRange NewCR2(NewLower, NewUpper); |
| 977 | applyRange(V2, NewCR2, Subtree, VRP); |
| 978 | } else if (*I == CR2.getUpper() - 1) { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 979 | APInt NewLower(CR2.getLower()), |
| 980 | NewUpper(CR2.getUpper() - 1); |
Nick Lewycky | 93f5410 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 981 | if (NewLower == NewUpper) |
| 982 | NewLower = NewUpper = APInt::getMinValue(W); |
| 983 | |
| 984 | ConstantRange NewCR2(NewLower, NewUpper); |
| 985 | applyRange(V2, NewCR2, Subtree, VRP); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | if (const APInt *I = CR2.getSingleElement()) { |
| 990 | if (CR1.isFullSet()) { |
| 991 | ConstantRange NewCR1(CR2.getUpper(), CR2.getLower()); |
| 992 | applyRange(V1, NewCR1, Subtree, VRP); |
| 993 | } else if (*I == CR1.getLower()) { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 994 | APInt NewLower(CR1.getLower() + 1), |
| 995 | NewUpper(CR1.getUpper()); |
Nick Lewycky | 93f5410 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 996 | if (NewLower == NewUpper) |
| 997 | NewLower = NewUpper = APInt::getMinValue(W); |
| 998 | |
| 999 | ConstantRange NewCR1(NewLower, NewUpper); |
| 1000 | applyRange(V1, NewCR1, Subtree, VRP); |
| 1001 | } else if (*I == CR1.getUpper() - 1) { |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 1002 | APInt NewLower(CR1.getLower()), |
| 1003 | NewUpper(CR1.getUpper() - 1); |
Nick Lewycky | 93f5410 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 1004 | if (NewLower == NewUpper) |
| 1005 | NewLower = NewUpper = APInt::getMinValue(W); |
| 1006 | |
| 1007 | ConstantRange NewCR1(NewLower, NewUpper); |
| 1008 | applyRange(V1, NewCR1, Subtree, VRP); |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1013 | void addInequality(Value *V1, Value *V2, ETNode *Subtree, LatticeVal LV, |
| 1014 | VRPSolver *VRP) { |
| 1015 | assert(!isRelatedBy(V1, V2, Subtree, LV) && "Asked to do useless work."); |
| 1016 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1017 | assert(isCanonical(V1, Subtree, VRP) && "Value not canonical."); |
| 1018 | assert(isCanonical(V2, Subtree, VRP) && "Value not canonical."); |
| 1019 | |
Nick Lewycky | 93f5410 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 1020 | if (LV == NE) { |
| 1021 | addNotEquals(V1, V2, Subtree, VRP); |
| 1022 | return; |
| 1023 | } |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1024 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 1025 | uint32_t W = typeToWidth(V1->getType()); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1026 | if (!W) return; |
| 1027 | |
| 1028 | ConstantRange CR1 = rangeFromValue(V1, Subtree, W); |
| 1029 | ConstantRange CR2 = rangeFromValue(V2, Subtree, W); |
| 1030 | |
| 1031 | if (!CR1.isSingleElement()) { |
| 1032 | ConstantRange NewCR1 = CR1.intersectWith(create(LV, CR2)); |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1033 | if (NewCR1 != CR1) |
| 1034 | applyRange(V1, NewCR1, Subtree, VRP); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if (!CR2.isSingleElement()) { |
| 1038 | ConstantRange NewCR2 = CR2.intersectWith(create(reversePredicate(LV), |
| 1039 | CR1)); |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1040 | if (NewCR2 != CR2) |
| 1041 | applyRange(V2, NewCR2, Subtree, VRP); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | }; |
| 1045 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1046 | /// UnreachableBlocks keeps tracks of blocks that are for one reason or |
| 1047 | /// another discovered to be unreachable. This is used to cull the graph when |
| 1048 | /// analyzing instructions, and to mark blocks with the "unreachable" |
| 1049 | /// terminator instruction after the function has executed. |
| 1050 | class VISIBILITY_HIDDEN UnreachableBlocks { |
| 1051 | private: |
| 1052 | std::vector<BasicBlock *> DeadBlocks; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1053 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1054 | public: |
| 1055 | /// mark - mark a block as dead |
| 1056 | void mark(BasicBlock *BB) { |
| 1057 | std::vector<BasicBlock *>::iterator E = DeadBlocks.end(); |
| 1058 | std::vector<BasicBlock *>::iterator I = |
| 1059 | std::lower_bound(DeadBlocks.begin(), E, BB); |
| 1060 | |
| 1061 | if (I == E || *I != BB) DeadBlocks.insert(I, BB); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1062 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1063 | |
| 1064 | /// isDead - returns whether a block is known to be dead already |
| 1065 | bool isDead(BasicBlock *BB) { |
| 1066 | std::vector<BasicBlock *>::iterator E = DeadBlocks.end(); |
| 1067 | std::vector<BasicBlock *>::iterator I = |
| 1068 | std::lower_bound(DeadBlocks.begin(), E, BB); |
| 1069 | |
| 1070 | return I != E && *I == BB; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1071 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1072 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1073 | /// kill - replace the dead blocks' terminator with an UnreachableInst. |
| 1074 | bool kill() { |
| 1075 | bool modified = false; |
| 1076 | for (std::vector<BasicBlock *>::iterator I = DeadBlocks.begin(), |
| 1077 | E = DeadBlocks.end(); I != E; ++I) { |
| 1078 | BasicBlock *BB = *I; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1079 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1080 | DOUT << "unreachable block: " << BB->getName() << "\n"; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1081 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1082 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 1083 | SI != SE; ++SI) { |
| 1084 | BasicBlock *Succ = *SI; |
| 1085 | Succ->removePredecessor(BB); |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1086 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1087 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1088 | TerminatorInst *TI = BB->getTerminator(); |
| 1089 | TI->replaceAllUsesWith(UndefValue::get(TI->getType())); |
| 1090 | TI->eraseFromParent(); |
| 1091 | new UnreachableInst(BB); |
| 1092 | ++NumBlocks; |
| 1093 | modified = true; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1094 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1095 | DeadBlocks.clear(); |
| 1096 | return modified; |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1097 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1098 | }; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1099 | |
| 1100 | /// VRPSolver keeps track of how changes to one variable affect other |
| 1101 | /// variables, and forwards changes along to the InequalityGraph. It |
| 1102 | /// also maintains the correct choice for "canonical" in the IG. |
| 1103 | /// @brief VRPSolver calculates inferences from a new relationship. |
| 1104 | class VISIBILITY_HIDDEN VRPSolver { |
| 1105 | private: |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1106 | friend class ValueRanges; |
| 1107 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1108 | struct Operation { |
| 1109 | Value *LHS, *RHS; |
| 1110 | ICmpInst::Predicate Op; |
| 1111 | |
Nick Lewycky | 4294446 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1112 | BasicBlock *ContextBB; |
| 1113 | Instruction *ContextInst; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1114 | }; |
| 1115 | std::deque<Operation> WorkList; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1116 | |
| 1117 | InequalityGraph &IG; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1118 | UnreachableBlocks &UB; |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1119 | ValueRanges &VR; |
| 1120 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1121 | ETForest *Forest; |
| 1122 | ETNode *Top; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1123 | BasicBlock *TopBB; |
| 1124 | Instruction *TopInst; |
| 1125 | bool &modified; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1126 | |
| 1127 | typedef InequalityGraph::Node Node; |
| 1128 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1129 | /// IdomI - Determines whether one Instruction dominates another. |
| 1130 | bool IdomI(Instruction *I1, Instruction *I2) const { |
| 1131 | BasicBlock *BB1 = I1->getParent(), |
| 1132 | *BB2 = I2->getParent(); |
| 1133 | if (BB1 == BB2) { |
| 1134 | if (isa<TerminatorInst>(I1)) return false; |
| 1135 | if (isa<TerminatorInst>(I2)) return true; |
| 1136 | if (isa<PHINode>(I1) && !isa<PHINode>(I2)) return true; |
| 1137 | if (!isa<PHINode>(I1) && isa<PHINode>(I2)) return false; |
| 1138 | |
| 1139 | for (BasicBlock::const_iterator I = BB1->begin(), E = BB1->end(); |
| 1140 | I != E; ++I) { |
| 1141 | if (&*I == I1) return true; |
| 1142 | if (&*I == I2) return false; |
| 1143 | } |
| 1144 | assert(!"Instructions not found in parent BasicBlock?"); |
| 1145 | } else { |
| 1146 | return Forest->properlyDominates(BB1, BB2); |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1151 | /// Returns true if V1 is a better canonical value than V2. |
| 1152 | bool compare(Value *V1, Value *V2) const { |
| 1153 | if (isa<Constant>(V1)) |
| 1154 | return !isa<Constant>(V2); |
| 1155 | else if (isa<Constant>(V2)) |
| 1156 | return false; |
| 1157 | else if (isa<Argument>(V1)) |
| 1158 | return !isa<Argument>(V2); |
| 1159 | else if (isa<Argument>(V2)) |
| 1160 | return false; |
| 1161 | |
| 1162 | Instruction *I1 = dyn_cast<Instruction>(V1); |
| 1163 | Instruction *I2 = dyn_cast<Instruction>(V2); |
| 1164 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1165 | if (!I1 || !I2) |
| 1166 | return V1->getNumUses() < V2->getNumUses(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1167 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1168 | return IdomI(I1, I2); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1171 | // below - true if the Instruction is dominated by the current context |
| 1172 | // block or instruction |
| 1173 | bool below(Instruction *I) { |
| 1174 | if (TopInst) |
| 1175 | return IdomI(TopInst, I); |
| 1176 | else { |
| 1177 | ETNode *Node = Forest->getNodeForBlock(I->getParent()); |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1178 | return Node->DominatedBy(Top); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1179 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1182 | bool makeEqual(Value *V1, Value *V2) { |
| 1183 | DOUT << "makeEqual(" << *V1 << ", " << *V2 << ")\n"; |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1184 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 1185 | assert(V1->getType() == V2->getType() && |
| 1186 | "Can't make two values with different types equal."); |
| 1187 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1188 | if (V1 == V2) return true; |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1189 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1190 | if (isa<Constant>(V1) && isa<Constant>(V2)) |
| 1191 | return false; |
| 1192 | |
| 1193 | unsigned n1 = IG.getNode(V1, Top), n2 = IG.getNode(V2, Top); |
| 1194 | |
| 1195 | if (n1 && n2) { |
| 1196 | if (n1 == n2) return true; |
| 1197 | if (IG.isRelatedBy(n1, n2, Top, NE)) return false; |
| 1198 | } |
| 1199 | |
| 1200 | if (n1) assert(V1 == IG.node(n1)->getValue() && "Value isn't canonical."); |
| 1201 | if (n2) assert(V2 == IG.node(n2)->getValue() && "Value isn't canonical."); |
| 1202 | |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1203 | assert(!compare(V2, V1) && "Please order parameters to makeEqual."); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1204 | |
| 1205 | assert(!isa<Constant>(V2) && "Tried to remove a constant."); |
| 1206 | |
| 1207 | SetVector<unsigned> Remove; |
| 1208 | if (n2) Remove.insert(n2); |
| 1209 | |
| 1210 | if (n1 && n2) { |
| 1211 | // Suppose we're being told that %x == %y, and %x <= %z and %y >= %z. |
| 1212 | // We can't just merge %x and %y because the relationship with %z would |
| 1213 | // be EQ and that's invalid. What we're doing is looking for any nodes |
| 1214 | // %z such that %x <= %z and %y >= %z, and vice versa. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1215 | |
| 1216 | Node *N1 = IG.node(n1); |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1217 | Node *N2 = IG.node(n2); |
| 1218 | Node::iterator end = N2->end(); |
| 1219 | |
| 1220 | // Find the intersection between N1 and N2 which is dominated by |
| 1221 | // Top. If we find %x where N1 <= %x <= N2 (or >=) then add %x to |
| 1222 | // Remove. |
| 1223 | for (Node::iterator I = N1->begin(), E = N1->end(); I != E; ++I) { |
| 1224 | if (!(I->LV & EQ_BIT) || !Top->DominatedBy(I->Subtree)) continue; |
| 1225 | |
| 1226 | unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT); |
| 1227 | unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT); |
| 1228 | Node::iterator NI = N2->find(I->To, Top); |
| 1229 | if (NI != end) { |
| 1230 | LatticeVal NILV = reversePredicate(NI->LV); |
| 1231 | unsigned NILV_s = NILV & (SLT_BIT|SGT_BIT); |
| 1232 | unsigned NILV_u = NILV & (ULT_BIT|UGT_BIT); |
| 1233 | |
| 1234 | if ((ILV_s != (SLT_BIT|SGT_BIT) && ILV_s == NILV_s) || |
| 1235 | (ILV_u != (ULT_BIT|UGT_BIT) && ILV_u == NILV_u)) |
| 1236 | Remove.insert(I->To); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | // See if one of the nodes about to be removed is actually a better |
| 1241 | // canonical choice than n1. |
| 1242 | unsigned orig_n1 = n1; |
Reid Spencer | a8a1547 | 2007-01-17 02:23:37 +0000 | [diff] [blame] | 1243 | SetVector<unsigned>::iterator DontRemove = Remove.end(); |
| 1244 | for (SetVector<unsigned>::iterator I = Remove.begin()+1 /* skip n2 */, |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1245 | E = Remove.end(); I != E; ++I) { |
| 1246 | unsigned n = *I; |
| 1247 | Value *V = IG.node(n)->getValue(); |
| 1248 | if (compare(V, V1)) { |
| 1249 | V1 = V; |
| 1250 | n1 = n; |
| 1251 | DontRemove = I; |
| 1252 | } |
| 1253 | } |
| 1254 | if (DontRemove != Remove.end()) { |
| 1255 | unsigned n = *DontRemove; |
| 1256 | Remove.remove(n); |
| 1257 | Remove.insert(orig_n1); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1260 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1261 | // We'd like to allow makeEqual on two values to perform a simple |
| 1262 | // substitution without every creating nodes in the IG whenever possible. |
| 1263 | // |
| 1264 | // The first iteration through this loop operates on V2 before going |
| 1265 | // through the Remove list and operating on those too. If all of the |
| 1266 | // iterations performed simple replacements then we exit early. |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1267 | bool mergeIGNode = false; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1268 | unsigned i = 0; |
| 1269 | for (Value *R = V2; i == 0 || i < Remove.size(); ++i) { |
| 1270 | if (i) R = IG.node(Remove[i])->getValue(); // skip n2. |
| 1271 | |
| 1272 | // Try to replace the whole instruction. If we can, we're done. |
| 1273 | Instruction *I2 = dyn_cast<Instruction>(R); |
| 1274 | if (I2 && below(I2)) { |
| 1275 | std::vector<Instruction *> ToNotify; |
| 1276 | for (Value::use_iterator UI = R->use_begin(), UE = R->use_end(); |
| 1277 | UI != UE;) { |
| 1278 | Use &TheUse = UI.getUse(); |
| 1279 | ++UI; |
| 1280 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) |
| 1281 | ToNotify.push_back(I); |
| 1282 | } |
| 1283 | |
| 1284 | DOUT << "Simply removing " << *I2 |
| 1285 | << ", replacing with " << *V1 << "\n"; |
| 1286 | I2->replaceAllUsesWith(V1); |
| 1287 | // leave it dead; it'll get erased later. |
| 1288 | ++NumInstruction; |
| 1289 | modified = true; |
| 1290 | |
| 1291 | for (std::vector<Instruction *>::iterator II = ToNotify.begin(), |
| 1292 | IE = ToNotify.end(); II != IE; ++II) { |
| 1293 | opsToDef(*II); |
| 1294 | } |
| 1295 | |
| 1296 | continue; |
| 1297 | } |
| 1298 | |
| 1299 | // Otherwise, replace all dominated uses. |
| 1300 | for (Value::use_iterator UI = R->use_begin(), UE = R->use_end(); |
| 1301 | UI != UE;) { |
| 1302 | Use &TheUse = UI.getUse(); |
| 1303 | ++UI; |
| 1304 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
| 1305 | if (below(I)) { |
| 1306 | TheUse.set(V1); |
| 1307 | modified = true; |
| 1308 | ++NumVarsReplaced; |
| 1309 | opsToDef(I); |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | // If that killed the instruction, stop here. |
| 1315 | if (I2 && isInstructionTriviallyDead(I2)) { |
| 1316 | DOUT << "Killed all uses of " << *I2 |
| 1317 | << ", replacing with " << *V1 << "\n"; |
| 1318 | continue; |
| 1319 | } |
| 1320 | |
| 1321 | // If we make it to here, then we will need to create a node for N1. |
| 1322 | // Otherwise, we can skip out early! |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1323 | mergeIGNode = true; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1326 | if (!isa<Constant>(V1)) { |
| 1327 | if (Remove.empty()) { |
| 1328 | VR.mergeInto(&V2, 1, V1, Top, this); |
| 1329 | } else { |
| 1330 | std::vector<Value*> RemoveVals; |
| 1331 | RemoveVals.reserve(Remove.size()); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1332 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1333 | for (SetVector<unsigned>::iterator I = Remove.begin(), |
| 1334 | E = Remove.end(); I != E; ++I) { |
| 1335 | Value *V = IG.node(*I)->getValue(); |
| 1336 | if (!V->use_empty()) |
| 1337 | RemoveVals.push_back(V); |
| 1338 | } |
| 1339 | VR.mergeInto(&RemoveVals[0], RemoveVals.size(), V1, Top, this); |
| 1340 | } |
| 1341 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1342 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1343 | if (mergeIGNode) { |
| 1344 | // Create N1. |
| 1345 | if (!n1) n1 = IG.newNode(V1); |
| 1346 | |
| 1347 | // Migrate relationships from removed nodes to N1. |
| 1348 | Node *N1 = IG.node(n1); |
| 1349 | for (SetVector<unsigned>::iterator I = Remove.begin(), E = Remove.end(); |
| 1350 | I != E; ++I) { |
| 1351 | unsigned n = *I; |
| 1352 | Node *N = IG.node(n); |
| 1353 | for (Node::iterator NI = N->begin(), NE = N->end(); NI != NE; ++NI) { |
| 1354 | if (NI->Subtree->DominatedBy(Top)) { |
| 1355 | if (NI->To == n1) { |
| 1356 | assert((NI->LV & EQ_BIT) && "Node inequal to itself."); |
| 1357 | continue; |
| 1358 | } |
| 1359 | if (Remove.count(NI->To)) |
| 1360 | continue; |
| 1361 | |
| 1362 | IG.node(NI->To)->update(n1, reversePredicate(NI->LV), Top); |
| 1363 | N1->update(NI->To, NI->LV, Top); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1364 | } |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1367 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1368 | // Point V2 (and all items in Remove) to N1. |
| 1369 | if (!n2) |
| 1370 | IG.addEquality(n1, V2, Top); |
| 1371 | else { |
| 1372 | for (SetVector<unsigned>::iterator I = Remove.begin(), |
| 1373 | E = Remove.end(); I != E; ++I) { |
| 1374 | IG.addEquality(n1, IG.node(*I)->getValue(), Top); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | // If !Remove.empty() then V2 = Remove[0]->getValue(). |
| 1379 | // Even when Remove is empty, we still want to process V2. |
| 1380 | i = 0; |
| 1381 | for (Value *R = V2; i == 0 || i < Remove.size(); ++i) { |
| 1382 | if (i) R = IG.node(Remove[i])->getValue(); // skip n2. |
| 1383 | |
| 1384 | if (Instruction *I2 = dyn_cast<Instruction>(R)) { |
| 1385 | if (below(I2) || |
| 1386 | Top->DominatedBy(Forest->getNodeForBlock(I2->getParent()))) |
| 1387 | defToOps(I2); |
| 1388 | } |
| 1389 | for (Value::use_iterator UI = V2->use_begin(), UE = V2->use_end(); |
| 1390 | UI != UE;) { |
| 1391 | Use &TheUse = UI.getUse(); |
| 1392 | ++UI; |
| 1393 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
| 1394 | if (below(I) || |
| 1395 | Top->DominatedBy(Forest->getNodeForBlock(I->getParent()))) |
| 1396 | opsToDef(I); |
| 1397 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1398 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1399 | } |
| 1400 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1401 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1402 | // re-opsToDef all dominated users of V1. |
| 1403 | if (Instruction *I = dyn_cast<Instruction>(V1)) { |
| 1404 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1405 | UI != UE;) { |
| 1406 | Use &TheUse = UI.getUse(); |
| 1407 | ++UI; |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1408 | Value *V = TheUse.getUser(); |
| 1409 | if (!V->use_empty()) { |
| 1410 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
| 1411 | if (below(Inst) || |
| 1412 | Top->DominatedBy(Forest->getNodeForBlock(Inst->getParent()))) |
| 1413 | opsToDef(Inst); |
| 1414 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | return true; |
| 1420 | } |
| 1421 | |
| 1422 | /// cmpInstToLattice - converts an CmpInst::Predicate to lattice value |
| 1423 | /// Requires that the lattice value be valid; does not accept ICMP_EQ. |
| 1424 | static LatticeVal cmpInstToLattice(ICmpInst::Predicate Pred) { |
| 1425 | switch (Pred) { |
| 1426 | case ICmpInst::ICMP_EQ: |
| 1427 | assert(!"No matching lattice value."); |
| 1428 | return static_cast<LatticeVal>(EQ_BIT); |
| 1429 | default: |
| 1430 | assert(!"Invalid 'icmp' predicate."); |
| 1431 | case ICmpInst::ICMP_NE: |
| 1432 | return NE; |
| 1433 | case ICmpInst::ICMP_UGT: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1434 | return UGT; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1435 | case ICmpInst::ICMP_UGE: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1436 | return UGE; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1437 | case ICmpInst::ICMP_ULT: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1438 | return ULT; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1439 | case ICmpInst::ICMP_ULE: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1440 | return ULE; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1441 | case ICmpInst::ICMP_SGT: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1442 | return SGT; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1443 | case ICmpInst::ICMP_SGE: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1444 | return SGE; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1445 | case ICmpInst::ICMP_SLT: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1446 | return SLT; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1447 | case ICmpInst::ICMP_SLE: |
Nick Lewycky | 5663980 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1448 | return SLE; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1449 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1450 | } |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1451 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1452 | public: |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1453 | VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ValueRanges &VR, |
| 1454 | ETForest *Forest, bool &modified, BasicBlock *TopBB) |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1455 | : IG(IG), |
| 1456 | UB(UB), |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1457 | VR(VR), |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1458 | Forest(Forest), |
| 1459 | Top(Forest->getNodeForBlock(TopBB)), |
| 1460 | TopBB(TopBB), |
| 1461 | TopInst(NULL), |
| 1462 | modified(modified) {} |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1463 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1464 | VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ValueRanges &VR, |
| 1465 | ETForest *Forest, bool &modified, Instruction *TopInst) |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1466 | : IG(IG), |
| 1467 | UB(UB), |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1468 | VR(VR), |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1469 | Forest(Forest), |
| 1470 | TopInst(TopInst), |
| 1471 | modified(modified) |
| 1472 | { |
| 1473 | TopBB = TopInst->getParent(); |
| 1474 | Top = Forest->getNodeForBlock(TopBB); |
| 1475 | } |
| 1476 | |
| 1477 | bool isRelatedBy(Value *V1, Value *V2, ICmpInst::Predicate Pred) const { |
| 1478 | if (Constant *C1 = dyn_cast<Constant>(V1)) |
| 1479 | if (Constant *C2 = dyn_cast<Constant>(V2)) |
| 1480 | return ConstantExpr::getCompare(Pred, C1, C2) == |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1481 | ConstantInt::getTrue(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1482 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1483 | if (unsigned n1 = IG.getNode(V1, Top)) |
| 1484 | if (unsigned n2 = IG.getNode(V2, Top)) { |
| 1485 | if (n1 == n2) return Pred == ICmpInst::ICMP_EQ || |
| 1486 | Pred == ICmpInst::ICMP_ULE || |
| 1487 | Pred == ICmpInst::ICMP_UGE || |
| 1488 | Pred == ICmpInst::ICMP_SLE || |
| 1489 | Pred == ICmpInst::ICMP_SGE; |
| 1490 | if (Pred == ICmpInst::ICMP_EQ) return false; |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1491 | if (IG.isRelatedBy(n1, n2, Top, cmpInstToLattice(Pred))) return true; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1494 | if (Pred == ICmpInst::ICMP_EQ) return V1 == V2; |
| 1495 | return VR.isRelatedBy(V1, V2, Top, cmpInstToLattice(Pred)); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1498 | /// add - adds a new property to the work queue |
| 1499 | void add(Value *V1, Value *V2, ICmpInst::Predicate Pred, |
| 1500 | Instruction *I = NULL) { |
| 1501 | DOUT << "adding " << *V1 << " " << Pred << " " << *V2; |
| 1502 | if (I) DOUT << " context: " << *I; |
| 1503 | else DOUT << " default context"; |
| 1504 | DOUT << "\n"; |
| 1505 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 1506 | assert(V1->getType() == V2->getType() && |
| 1507 | "Can't relate two values with different types."); |
| 1508 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1509 | WorkList.push_back(Operation()); |
| 1510 | Operation &O = WorkList.back(); |
Nick Lewycky | 4294446 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1511 | O.LHS = V1, O.RHS = V2, O.Op = Pred, O.ContextInst = I; |
| 1512 | O.ContextBB = I ? I->getParent() : TopBB; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1515 | /// defToOps - Given an instruction definition that we've learned something |
| 1516 | /// new about, find any new relationships between its operands. |
| 1517 | void defToOps(Instruction *I) { |
| 1518 | Instruction *NewContext = below(I) ? I : TopInst; |
| 1519 | Value *Canonical = IG.canonicalize(I, Top); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1520 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1521 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 1522 | const Type *Ty = BO->getType(); |
| 1523 | assert(!Ty->isFPOrFPVector() && "Float in work queue!"); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1524 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1525 | Value *Op0 = IG.canonicalize(BO->getOperand(0), Top); |
| 1526 | Value *Op1 = IG.canonicalize(BO->getOperand(1), Top); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1527 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1528 | // TODO: "and i32 -1, %x" EQ %y then %x EQ %y. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1529 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1530 | switch (BO->getOpcode()) { |
| 1531 | case Instruction::And: { |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1532 | // "and i32 %a, %b" EQ -1 then %a EQ -1 and %b EQ -1 |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1533 | ConstantInt *CI = ConstantInt::getAllOnesValue(Ty); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1534 | if (Canonical == CI) { |
| 1535 | add(CI, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1536 | add(CI, Op1, ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1537 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1538 | } break; |
| 1539 | case Instruction::Or: { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1540 | // "or i32 %a, %b" EQ 0 then %a EQ 0 and %b EQ 0 |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1541 | Constant *Zero = Constant::getNullValue(Ty); |
| 1542 | if (Canonical == Zero) { |
| 1543 | add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1544 | add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext); |
| 1545 | } |
| 1546 | } break; |
| 1547 | case Instruction::Xor: { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1548 | // "xor i32 %c, %a" EQ %b then %a EQ %c ^ %b |
| 1549 | // "xor i32 %c, %a" EQ %c then %a EQ 0 |
| 1550 | // "xor i32 %c, %a" NE %c then %a NE 0 |
| 1551 | // Repeat the above, with order of operands reversed. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1552 | Value *LHS = Op0; |
| 1553 | Value *RHS = Op1; |
| 1554 | if (!isa<Constant>(LHS)) std::swap(LHS, RHS); |
| 1555 | |
Nick Lewycky | 4a74a75 | 2007-01-12 00:02:12 +0000 | [diff] [blame] | 1556 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Canonical)) { |
| 1557 | if (ConstantInt *Arg = dyn_cast<ConstantInt>(LHS)) { |
Reid Spencer | c34dedf | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 1558 | add(RHS, ConstantInt::get(CI->getValue() ^ Arg->getValue()), |
Nick Lewycky | 4a74a75 | 2007-01-12 00:02:12 +0000 | [diff] [blame] | 1559 | ICmpInst::ICMP_EQ, NewContext); |
| 1560 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1561 | } |
| 1562 | if (Canonical == LHS) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1563 | if (isa<ConstantInt>(Canonical)) |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1564 | add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ, |
| 1565 | NewContext); |
| 1566 | } else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) { |
| 1567 | add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE, |
| 1568 | NewContext); |
| 1569 | } |
| 1570 | } break; |
| 1571 | default: |
| 1572 | break; |
| 1573 | } |
| 1574 | } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1575 | // "icmp ult i32 %a, %y" EQ true then %a u< y |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1576 | // etc. |
| 1577 | |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1578 | if (Canonical == ConstantInt::getTrue()) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1579 | add(IC->getOperand(0), IC->getOperand(1), IC->getPredicate(), |
| 1580 | NewContext); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1581 | } else if (Canonical == ConstantInt::getFalse()) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1582 | add(IC->getOperand(0), IC->getOperand(1), |
| 1583 | ICmpInst::getInversePredicate(IC->getPredicate()), NewContext); |
| 1584 | } |
| 1585 | } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { |
| 1586 | if (I->getType()->isFPOrFPVector()) return; |
| 1587 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1588 | // Given: "%a = select i1 %x, i32 %b, i32 %c" |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1589 | // %a EQ %b and %b NE %c then %x EQ true |
| 1590 | // %a EQ %c and %b NE %c then %x EQ false |
| 1591 | |
| 1592 | Value *True = SI->getTrueValue(); |
| 1593 | Value *False = SI->getFalseValue(); |
| 1594 | if (isRelatedBy(True, False, ICmpInst::ICMP_NE)) { |
| 1595 | if (Canonical == IG.canonicalize(True, Top) || |
| 1596 | isRelatedBy(Canonical, False, ICmpInst::ICMP_NE)) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1597 | add(SI->getCondition(), ConstantInt::getTrue(), |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1598 | ICmpInst::ICMP_EQ, NewContext); |
| 1599 | else if (Canonical == IG.canonicalize(False, Top) || |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1600 | isRelatedBy(Canonical, True, ICmpInst::ICMP_NE)) |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1601 | add(SI->getCondition(), ConstantInt::getFalse(), |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1602 | ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1603 | } |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1604 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
| 1605 | for (GetElementPtrInst::op_iterator OI = GEPI->idx_begin(), |
| 1606 | OE = GEPI->idx_end(); OI != OE; ++OI) { |
| 1607 | ConstantInt *Op = dyn_cast<ConstantInt>(IG.canonicalize(*OI, Top)); |
| 1608 | if (!Op || !Op->isZero()) return; |
| 1609 | } |
| 1610 | // TODO: The GEPI indices are all zero. Copy from definition to operand, |
| 1611 | // jumping the type plane as needed. |
| 1612 | if (isRelatedBy(GEPI, Constant::getNullValue(GEPI->getType()), |
| 1613 | ICmpInst::ICMP_NE)) { |
| 1614 | Value *Ptr = GEPI->getPointerOperand(); |
| 1615 | add(Ptr, Constant::getNullValue(Ptr->getType()), ICmpInst::ICMP_NE, |
| 1616 | NewContext); |
| 1617 | } |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1618 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 1619 | const Type *SrcTy = CI->getSrcTy(); |
| 1620 | |
| 1621 | Value *TheCI = IG.canonicalize(CI, Top); |
| 1622 | uint32_t W = VR.typeToWidth(SrcTy); |
| 1623 | if (!W) return; |
| 1624 | ConstantRange CR = VR.rangeFromValue(TheCI, Top, W); |
| 1625 | |
| 1626 | if (CR.isFullSet()) return; |
| 1627 | |
| 1628 | switch (CI->getOpcode()) { |
| 1629 | default: break; |
| 1630 | case Instruction::ZExt: |
| 1631 | case Instruction::SExt: |
| 1632 | VR.applyRange(IG.canonicalize(CI->getOperand(0), Top), |
| 1633 | CR.truncate(W), Top, this); |
| 1634 | break; |
| 1635 | case Instruction::BitCast: |
| 1636 | VR.applyRange(IG.canonicalize(CI->getOperand(0), Top), |
| 1637 | CR, Top, this); |
| 1638 | break; |
| 1639 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1640 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1641 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1642 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1643 | /// opsToDef - A new relationship was discovered involving one of this |
| 1644 | /// instruction's operands. Find any new relationship involving the |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1645 | /// definition, or another operand. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1646 | void opsToDef(Instruction *I) { |
| 1647 | Instruction *NewContext = below(I) ? I : TopInst; |
| 1648 | |
| 1649 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 1650 | Value *Op0 = IG.canonicalize(BO->getOperand(0), Top); |
| 1651 | Value *Op1 = IG.canonicalize(BO->getOperand(1), Top); |
| 1652 | |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1653 | if (ConstantInt *CI0 = dyn_cast<ConstantInt>(Op0)) |
| 1654 | if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1655 | add(BO, ConstantExpr::get(BO->getOpcode(), CI0, CI1), |
| 1656 | ICmpInst::ICMP_EQ, NewContext); |
| 1657 | return; |
| 1658 | } |
| 1659 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1660 | // "%y = and i1 true, %x" then %x EQ %y |
| 1661 | // "%y = or i1 false, %x" then %x EQ %y |
| 1662 | // "%x = add i32 %y, 0" then %x EQ %y |
| 1663 | // "%x = mul i32 %y, 0" then %x EQ 0 |
| 1664 | |
| 1665 | Instruction::BinaryOps Opcode = BO->getOpcode(); |
Nick Lewycky | db204ec | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1666 | const Type *Ty = BO->getType(); |
| 1667 | assert(!Ty->isFPOrFPVector() && "Float in work queue!"); |
| 1668 | |
| 1669 | Constant *Zero = Constant::getNullValue(Ty); |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1670 | ConstantInt *AllOnes = ConstantInt::getAllOnesValue(Ty); |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1671 | |
| 1672 | switch (Opcode) { |
| 1673 | default: break; |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1674 | case Instruction::LShr: |
| 1675 | case Instruction::AShr: |
| 1676 | case Instruction::Shl: |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1677 | case Instruction::Sub: |
Nick Lewycky | db204ec | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1678 | if (Op1 == Zero) { |
| 1679 | add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1680 | return; |
| 1681 | } |
| 1682 | break; |
| 1683 | case Instruction::Or: |
| 1684 | if (Op0 == AllOnes || Op1 == AllOnes) { |
| 1685 | add(BO, AllOnes, ICmpInst::ICMP_EQ, NewContext); |
| 1686 | return; |
| 1687 | } // fall-through |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1688 | case Instruction::Xor: |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1689 | case Instruction::Add: |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1690 | if (Op0 == Zero) { |
| 1691 | add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); |
| 1692 | return; |
| 1693 | } else if (Op1 == Zero) { |
| 1694 | add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1695 | return; |
| 1696 | } |
Nick Lewycky | db204ec | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1697 | break; |
| 1698 | case Instruction::And: |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1699 | if (Op0 == AllOnes) { |
| 1700 | add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); |
| 1701 | return; |
| 1702 | } else if (Op1 == AllOnes) { |
| 1703 | add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1704 | return; |
| 1705 | } |
Nick Lewycky | db204ec | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1706 | // fall-through |
| 1707 | case Instruction::Mul: |
| 1708 | if (Op0 == Zero || Op1 == Zero) { |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1709 | add(BO, Zero, ICmpInst::ICMP_EQ, NewContext); |
| 1710 | return; |
| 1711 | } |
Nick Lewycky | db204ec | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1712 | break; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1713 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1714 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1715 | // "%x = add i32 %y, %z" and %x EQ %y then %z EQ 0 |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1716 | // "%x = add i32 %y, %z" and %x EQ %z then %y EQ 0 |
| 1717 | // "%x = shl i32 %y, %z" and %x EQ %y and %y NE 0 then %z EQ 0 |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1718 | // "%x = udiv i32 %y, %z" and %x EQ %y then %z EQ 1 |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1719 | |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1720 | Value *Known = Op0, *Unknown = Op1, |
| 1721 | *TheBO = IG.canonicalize(BO, Top); |
| 1722 | if (Known != TheBO) std::swap(Known, Unknown); |
| 1723 | if (Known == TheBO) { |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1724 | switch (Opcode) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1725 | default: break; |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1726 | case Instruction::LShr: |
| 1727 | case Instruction::AShr: |
| 1728 | case Instruction::Shl: |
| 1729 | if (!isRelatedBy(Known, Zero, ICmpInst::ICMP_NE)) break; |
| 1730 | // otherwise, fall-through. |
| 1731 | case Instruction::Sub: |
| 1732 | if (Unknown == Op1) break; |
| 1733 | // otherwise, fall-through. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1734 | case Instruction::Xor: |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1735 | case Instruction::Add: |
Nick Lewycky | db204ec | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1736 | add(Unknown, Zero, ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1737 | break; |
| 1738 | case Instruction::UDiv: |
| 1739 | case Instruction::SDiv: |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1740 | if (Unknown == Op1) break; |
| 1741 | if (isRelatedBy(Known, Zero, ICmpInst::ICMP_NE)) { |
Nick Lewycky | 4a74a75 | 2007-01-12 00:02:12 +0000 | [diff] [blame] | 1742 | Constant *One = ConstantInt::get(Ty, 1); |
| 1743 | add(Unknown, One, ICmpInst::ICMP_EQ, NewContext); |
| 1744 | } |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1745 | break; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1746 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1747 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1748 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1749 | // TODO: "%a = add i32 %b, 1" and %b > %z then %a >= %z. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1750 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1751 | } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) { |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1752 | // "%a = icmp ult i32 %b, %c" and %b u< %c then %a EQ true |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1753 | // "%a = icmp ult i32 %b, %c" and %b u>= %c then %a EQ false |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1754 | // etc. |
| 1755 | |
| 1756 | Value *Op0 = IG.canonicalize(IC->getOperand(0), Top); |
| 1757 | Value *Op1 = IG.canonicalize(IC->getOperand(1), Top); |
| 1758 | |
| 1759 | ICmpInst::Predicate Pred = IC->getPredicate(); |
| 1760 | if (isRelatedBy(Op0, Op1, Pred)) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1761 | add(IC, ConstantInt::getTrue(), ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1762 | } else if (isRelatedBy(Op0, Op1, ICmpInst::getInversePredicate(Pred))) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1763 | add(IC, ConstantInt::getFalse(), ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1766 | } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1767 | if (I->getType()->isFPOrFPVector()) return; |
| 1768 | |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1769 | // Given: "%a = select i1 %x, i32 %b, i32 %c" |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1770 | // %x EQ true then %a EQ %b |
| 1771 | // %x EQ false then %a EQ %c |
| 1772 | // %b EQ %c then %a EQ %b |
| 1773 | |
| 1774 | Value *Canonical = IG.canonicalize(SI->getCondition(), Top); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1775 | if (Canonical == ConstantInt::getTrue()) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1776 | add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1777 | } else if (Canonical == ConstantInt::getFalse()) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1778 | add(SI, SI->getFalseValue(), ICmpInst::ICMP_EQ, NewContext); |
| 1779 | } else if (IG.canonicalize(SI->getTrueValue(), Top) == |
| 1780 | IG.canonicalize(SI->getFalseValue(), Top)) { |
| 1781 | add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext); |
| 1782 | } |
Nick Lewycky | ee32ee0 | 2007-01-12 01:23:53 +0000 | [diff] [blame] | 1783 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1784 | const Type *DestTy = CI->getDestTy(); |
| 1785 | if (DestTy->isFPOrFPVector()) return; |
Nick Lewycky | ee32ee0 | 2007-01-12 01:23:53 +0000 | [diff] [blame] | 1786 | |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1787 | Value *Op = IG.canonicalize(CI->getOperand(0), Top); |
| 1788 | Instruction::CastOps Opcode = CI->getOpcode(); |
| 1789 | |
| 1790 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 1791 | add(CI, ConstantExpr::getCast(Opcode, C, DestTy), |
Nick Lewycky | ee32ee0 | 2007-01-12 01:23:53 +0000 | [diff] [blame] | 1792 | ICmpInst::ICMP_EQ, NewContext); |
| 1793 | } |
| 1794 | |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1795 | uint32_t W = VR.typeToWidth(DestTy); |
| 1796 | Value *TheCI = IG.canonicalize(CI, Top); |
| 1797 | ConstantRange CR = VR.rangeFromValue(Op, Top, W); |
| 1798 | |
| 1799 | if (!CR.isFullSet()) { |
| 1800 | switch (Opcode) { |
| 1801 | default: break; |
| 1802 | case Instruction::ZExt: |
| 1803 | VR.applyRange(TheCI, CR.zeroExtend(W), Top, this); |
| 1804 | break; |
| 1805 | case Instruction::SExt: |
| 1806 | VR.applyRange(TheCI, CR.signExtend(W), Top, this); |
| 1807 | break; |
| 1808 | case Instruction::Trunc: { |
| 1809 | ConstantRange Result = CR.truncate(W); |
| 1810 | if (!Result.isFullSet()) |
| 1811 | VR.applyRange(TheCI, Result, Top, this); |
| 1812 | } break; |
| 1813 | case Instruction::BitCast: |
| 1814 | VR.applyRange(TheCI, CR, Top, this); |
| 1815 | break; |
| 1816 | // TODO: other casts? |
| 1817 | } |
| 1818 | } |
Nick Lewycky | b0da7ed | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1819 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
| 1820 | for (GetElementPtrInst::op_iterator OI = GEPI->idx_begin(), |
| 1821 | OE = GEPI->idx_end(); OI != OE; ++OI) { |
| 1822 | ConstantInt *Op = dyn_cast<ConstantInt>(IG.canonicalize(*OI, Top)); |
| 1823 | if (!Op || !Op->isZero()) return; |
| 1824 | } |
| 1825 | // TODO: The GEPI indices are all zero. Copy from operand to definition, |
| 1826 | // jumping the type plane as needed. |
| 1827 | Value *Ptr = GEPI->getPointerOperand(); |
| 1828 | if (isRelatedBy(Ptr, Constant::getNullValue(Ptr->getType()), |
| 1829 | ICmpInst::ICMP_NE)) { |
| 1830 | add(GEPI, Constant::getNullValue(GEPI->getType()), ICmpInst::ICMP_NE, |
| 1831 | NewContext); |
| 1832 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1833 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | /// solve - process the work queue |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1837 | void solve() { |
| 1838 | //DOUT << "WorkList entry, size: " << WorkList.size() << "\n"; |
| 1839 | while (!WorkList.empty()) { |
| 1840 | //DOUT << "WorkList size: " << WorkList.size() << "\n"; |
| 1841 | |
| 1842 | Operation &O = WorkList.front(); |
Nick Lewycky | 4294446 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1843 | TopInst = O.ContextInst; |
| 1844 | TopBB = O.ContextBB; |
| 1845 | Top = Forest->getNodeForBlock(TopBB); |
| 1846 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1847 | O.LHS = IG.canonicalize(O.LHS, Top); |
| 1848 | O.RHS = IG.canonicalize(O.RHS, Top); |
| 1849 | |
| 1850 | assert(O.LHS == IG.canonicalize(O.LHS, Top) && "Canonicalize isn't."); |
| 1851 | assert(O.RHS == IG.canonicalize(O.RHS, Top) && "Canonicalize isn't."); |
| 1852 | |
| 1853 | DOUT << "solving " << *O.LHS << " " << O.Op << " " << *O.RHS; |
Nick Lewycky | 4294446 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1854 | if (O.ContextInst) DOUT << " context inst: " << *O.ContextInst; |
| 1855 | else DOUT << " context block: " << O.ContextBB->getName(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1856 | DOUT << "\n"; |
| 1857 | |
| 1858 | DEBUG(IG.dump()); |
| 1859 | |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1860 | // If they're both Constant, skip it. Check for contradiction and mark |
| 1861 | // the BB as unreachable if so. |
| 1862 | if (Constant *CI_L = dyn_cast<Constant>(O.LHS)) { |
| 1863 | if (Constant *CI_R = dyn_cast<Constant>(O.RHS)) { |
| 1864 | if (ConstantExpr::getCompare(O.Op, CI_L, CI_R) == |
| 1865 | ConstantInt::getFalse()) |
| 1866 | UB.mark(TopBB); |
| 1867 | |
| 1868 | WorkList.pop_front(); |
| 1869 | continue; |
| 1870 | } |
| 1871 | } |
| 1872 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1873 | if (compare(O.LHS, O.RHS)) { |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1874 | std::swap(O.LHS, O.RHS); |
| 1875 | O.Op = ICmpInst::getSwappedPredicate(O.Op); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | if (O.Op == ICmpInst::ICMP_EQ) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1879 | if (!makeEqual(O.RHS, O.LHS)) |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1880 | UB.mark(TopBB); |
| 1881 | } else { |
| 1882 | LatticeVal LV = cmpInstToLattice(O.Op); |
| 1883 | |
| 1884 | if ((LV & EQ_BIT) && |
| 1885 | isRelatedBy(O.LHS, O.RHS, ICmpInst::getSwappedPredicate(O.Op))) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1886 | if (!makeEqual(O.RHS, O.LHS)) |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1887 | UB.mark(TopBB); |
| 1888 | } else { |
| 1889 | if (isRelatedBy(O.LHS, O.RHS, ICmpInst::getInversePredicate(O.Op))){ |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1890 | UB.mark(TopBB); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1891 | WorkList.pop_front(); |
| 1892 | continue; |
| 1893 | } |
| 1894 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1895 | unsigned n1 = IG.getNode(O.LHS, Top); |
| 1896 | unsigned n2 = IG.getNode(O.RHS, Top); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1897 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1898 | if (n1 && n1 == n2) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1899 | if (O.Op != ICmpInst::ICMP_UGE && O.Op != ICmpInst::ICMP_ULE && |
| 1900 | O.Op != ICmpInst::ICMP_SGE && O.Op != ICmpInst::ICMP_SLE) |
| 1901 | UB.mark(TopBB); |
| 1902 | |
| 1903 | WorkList.pop_front(); |
| 1904 | continue; |
| 1905 | } |
| 1906 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1907 | if (VR.isRelatedBy(O.LHS, O.RHS, Top, LV) || |
| 1908 | (n1 && n2 && IG.isRelatedBy(n1, n2, Top, LV))) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1909 | WorkList.pop_front(); |
| 1910 | continue; |
| 1911 | } |
| 1912 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1913 | VR.addInequality(O.LHS, O.RHS, Top, LV, this); |
| 1914 | if ((!isa<ConstantInt>(O.RHS) && !isa<ConstantInt>(O.LHS)) || |
| 1915 | LV == NE) { |
| 1916 | if (!n1) n1 = IG.newNode(O.LHS); |
| 1917 | if (!n2) n2 = IG.newNode(O.RHS); |
| 1918 | IG.addInequality(n1, n2, Top, LV); |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1921 | if (Instruction *I1 = dyn_cast<Instruction>(O.LHS)) { |
| 1922 | if (below(I1) || |
| 1923 | Top->DominatedBy(Forest->getNodeForBlock(I1->getParent()))) |
| 1924 | defToOps(I1); |
| 1925 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1926 | if (isa<Instruction>(O.LHS) || isa<Argument>(O.LHS)) { |
| 1927 | for (Value::use_iterator UI = O.LHS->use_begin(), |
| 1928 | UE = O.LHS->use_end(); UI != UE;) { |
| 1929 | Use &TheUse = UI.getUse(); |
| 1930 | ++UI; |
| 1931 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1932 | if (below(I) || |
| 1933 | Top->DominatedBy(Forest->getNodeForBlock(I->getParent()))) |
| 1934 | opsToDef(I); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | } |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1938 | if (Instruction *I2 = dyn_cast<Instruction>(O.RHS)) { |
| 1939 | if (below(I2) || |
| 1940 | Top->DominatedBy(Forest->getNodeForBlock(I2->getParent()))) |
| 1941 | defToOps(I2); |
| 1942 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1943 | if (isa<Instruction>(O.RHS) || isa<Argument>(O.RHS)) { |
| 1944 | for (Value::use_iterator UI = O.RHS->use_begin(), |
| 1945 | UE = O.RHS->use_end(); UI != UE;) { |
| 1946 | Use &TheUse = UI.getUse(); |
| 1947 | ++UI; |
| 1948 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1949 | if (below(I) || |
| 1950 | Top->DominatedBy(Forest->getNodeForBlock(I->getParent()))) |
| 1951 | |
| 1952 | opsToDef(I); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1955 | } |
| 1956 | } |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1957 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1958 | WorkList.pop_front(); |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1959 | } |
Nick Lewycky | 9d17c82 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1960 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1961 | }; |
| 1962 | |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1963 | void ValueRanges::addToWorklist(Value *V, Constant *C, |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1964 | ICmpInst::Predicate Pred, VRPSolver *VRP) { |
Nick Lewycky | 3bb6de8 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1965 | VRP->add(V, C, Pred, VRP->TopInst); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Nick Lewycky | d4f51a8 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1968 | void ValueRanges::markBlock(VRPSolver *VRP) { |
| 1969 | VRP->UB.mark(VRP->TopBB); |
| 1970 | } |
| 1971 | |
Nick Lewycky | 17d20fd | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1972 | #ifndef NDEBUG |
| 1973 | bool ValueRanges::isCanonical(Value *V, ETNode *Subtree, VRPSolver *VRP) { |
| 1974 | return V == VRP->IG.canonicalize(V, Subtree); |
| 1975 | } |
| 1976 | #endif |
| 1977 | |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1978 | /// PredicateSimplifier - This class is a simplifier that replaces |
| 1979 | /// one equivalent variable with another. It also tracks what |
| 1980 | /// can't be equal and will solve setcc instructions when possible. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1981 | /// @brief Root of the predicate simplifier optimization. |
| 1982 | class VISIBILITY_HIDDEN PredicateSimplifier : public FunctionPass { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 1983 | DominatorTree *DT; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1984 | ETForest *Forest; |
| 1985 | bool modified; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1986 | InequalityGraph *IG; |
| 1987 | UnreachableBlocks UB; |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1988 | ValueRanges *VR; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1989 | |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 1990 | std::vector<DominatorTree::Node *> WorkList; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1991 | |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1992 | public: |
| 1993 | bool runOnFunction(Function &F); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1994 | |
| 1995 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 1996 | AU.addRequiredID(BreakCriticalEdgesID); |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 1997 | AU.addRequired<DominatorTree>(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1998 | AU.addRequired<ETForest>(); |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 1999 | AU.addRequired<TargetData>(); |
| 2000 | AU.addPreserved<TargetData>(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2001 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2002 | |
| 2003 | private: |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2004 | /// Forwards - Adds new properties into PropertySet and uses them to |
| 2005 | /// simplify instructions. Because new properties sometimes apply to |
| 2006 | /// a transition from one BasicBlock to another, this will use the |
| 2007 | /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the |
| 2008 | /// basic block with the new PropertySet. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2009 | /// @brief Performs abstract execution of the program. |
| 2010 | class VISIBILITY_HIDDEN Forwards : public InstVisitor<Forwards> { |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2011 | friend class InstVisitor<Forwards>; |
| 2012 | PredicateSimplifier *PS; |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2013 | DominatorTree::Node *DTNode; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2014 | |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2015 | public: |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2016 | InequalityGraph &IG; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2017 | UnreachableBlocks &UB; |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2018 | ValueRanges &VR; |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2019 | |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2020 | Forwards(PredicateSimplifier *PS, DominatorTree::Node *DTNode) |
| 2021 | : PS(PS), DTNode(DTNode), IG(*PS->IG), UB(PS->UB), VR(*PS->VR) {} |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2022 | |
| 2023 | void visitTerminatorInst(TerminatorInst &TI); |
| 2024 | void visitBranchInst(BranchInst &BI); |
| 2025 | void visitSwitchInst(SwitchInst &SI); |
| 2026 | |
Nick Lewycky | f345008 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 2027 | void visitAllocaInst(AllocaInst &AI); |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2028 | void visitLoadInst(LoadInst &LI); |
| 2029 | void visitStoreInst(StoreInst &SI); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2030 | |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2031 | void visitSExtInst(SExtInst &SI); |
| 2032 | void visitZExtInst(ZExtInst &ZI); |
| 2033 | |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2034 | void visitBinaryOperator(BinaryOperator &BO); |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2035 | void visitICmpInst(ICmpInst &IC); |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2036 | }; |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2037 | |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2038 | // Used by terminator instructions to proceed from the current basic |
| 2039 | // block to the next. Verifies that "current" dominates "next", |
| 2040 | // then calls visitBasicBlock. |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2041 | void proceedToSuccessors(DominatorTree::Node *Current) { |
| 2042 | for (DominatorTree::Node::iterator I = Current->begin(), |
| 2043 | E = Current->end(); I != E; ++I) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2044 | WorkList.push_back(*I); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2048 | void proceedToSuccessor(DominatorTree::Node *Next) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2049 | WorkList.push_back(Next); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2050 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Visits each instruction in the basic block. |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2053 | void visitBasicBlock(DominatorTree::Node *Node) { |
| 2054 | BasicBlock *BB = Node->getBlock(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2055 | ETNode *ET = Forest->getNodeForBlock(BB); |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 2056 | DOUT << "Entering Basic Block: " << BB->getName() |
| 2057 | << " (" << ET->getDFSNumIn() << ")\n"; |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2058 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2059 | visitInstruction(I++, Node, ET); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2060 | } |
| 2061 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2062 | |
Nick Lewycky | 9a22d7b | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 2063 | // Tries to simplify each Instruction and add new properties to |
Nick Lewycky | 77e030b | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2064 | // the PropertySet. |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2065 | void visitInstruction(Instruction *I, DominatorTree::Node *DT, ETNode *ET) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2066 | DOUT << "Considering instruction " << *I << "\n"; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2067 | DEBUG(IG->dump()); |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2068 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2069 | // Sometimes instructions are killed in earlier analysis. |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2070 | if (isInstructionTriviallyDead(I)) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2071 | ++NumSimple; |
| 2072 | modified = true; |
| 2073 | IG->remove(I); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2074 | I->eraseFromParent(); |
| 2075 | return; |
| 2076 | } |
| 2077 | |
Nick Lewycky | 4294446 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 2078 | #ifndef NDEBUG |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2079 | // Try to replace the whole instruction. |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2080 | Value *V = IG->canonicalize(I, ET); |
| 2081 | assert(V == I && "Late instruction canonicalization."); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2082 | if (V != I) { |
| 2083 | modified = true; |
| 2084 | ++NumInstruction; |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2085 | DOUT << "Removing " << *I << ", replacing with " << *V << "\n"; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2086 | IG->remove(I); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2087 | I->replaceAllUsesWith(V); |
| 2088 | I->eraseFromParent(); |
| 2089 | return; |
| 2090 | } |
| 2091 | |
| 2092 | // Try to substitute operands. |
| 2093 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 2094 | Value *Oper = I->getOperand(i); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2095 | Value *V = IG->canonicalize(Oper, ET); |
| 2096 | assert(V == Oper && "Late operand canonicalization."); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2097 | if (V != Oper) { |
| 2098 | modified = true; |
| 2099 | ++NumVarsReplaced; |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2100 | DOUT << "Resolving " << *I; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2101 | I->setOperand(i, V); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2102 | DOUT << " into " << *I; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2103 | } |
| 2104 | } |
Nick Lewycky | 4294446 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 2105 | #endif |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2106 | |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2107 | std::string name = I->getParent()->getName(); |
| 2108 | DOUT << "push (%" << name << ")\n"; |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2109 | Forwards visit(this, DT); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2110 | visit.visit(*I); |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2111 | DOUT << "pop (%" << name << ")\n"; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2112 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2113 | }; |
| 2114 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2115 | bool PredicateSimplifier::runOnFunction(Function &F) { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2116 | DT = &getAnalysis<DominatorTree>(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2117 | Forest = &getAnalysis<ETForest>(); |
Nick Lewycky | cfff1c3 | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2118 | |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 2119 | TargetData *TD = &getAnalysis<TargetData>(); |
| 2120 | |
Chris Lattner | 28d921d | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 2121 | // XXX: should only act when numbers are out of date |
| 2122 | Forest->updateDFSNumbers(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2123 | |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2124 | DOUT << "Entering Function: " << F.getName() << "\n"; |
Nick Lewycky | cfff1c3 | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2125 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2126 | modified = false; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2127 | BasicBlock *RootBlock = &F.getEntryBlock(); |
| 2128 | IG = new InequalityGraph(Forest->getNodeForBlock(RootBlock)); |
Nick Lewycky | 12d44ab | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 2129 | VR = new ValueRanges(TD); |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2130 | WorkList.push_back(DT->getRootNode()); |
Nick Lewycky | cfff1c3 | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2131 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2132 | do { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2133 | DominatorTree::Node *DTNode = WorkList.back(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2134 | WorkList.pop_back(); |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2135 | if (!UB.isDead(DTNode->getBlock())) visitBasicBlock(DTNode); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2136 | } while (!WorkList.empty()); |
Nick Lewycky | cfff1c3 | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2137 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2138 | delete VR; |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2139 | delete IG; |
| 2140 | |
| 2141 | modified |= UB.kill(); |
Nick Lewycky | cfff1c3 | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2142 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2143 | return modified; |
Nick Lewycky | 8e55993 | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2146 | void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2147 | PS->proceedToSuccessors(DTNode); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) { |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2151 | if (BI.isUnconditional()) { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2152 | PS->proceedToSuccessors(DTNode); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2153 | return; |
| 2154 | } |
| 2155 | |
| 2156 | Value *Condition = BI.getCondition(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2157 | BasicBlock *TrueDest = BI.getSuccessor(0); |
| 2158 | BasicBlock *FalseDest = BI.getSuccessor(1); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2159 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2160 | if (isa<Constant>(Condition) || TrueDest == FalseDest) { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2161 | PS->proceedToSuccessors(DTNode); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2162 | return; |
| 2163 | } |
| 2164 | |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2165 | for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end(); |
| 2166 | I != E; ++I) { |
| 2167 | BasicBlock *Dest = (*I)->getBlock(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2168 | DOUT << "Branch thinking about %" << Dest->getName() |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 2169 | << "(" << PS->Forest->getNodeForBlock(Dest)->getDFSNumIn() << ")\n"; |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2170 | |
| 2171 | if (Dest == TrueDest) { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2172 | DOUT << "(" << DTNode->getBlock()->getName() << ") true set:\n"; |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2173 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, Dest); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2174 | VRP.add(ConstantInt::getTrue(), Condition, ICmpInst::ICMP_EQ); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2175 | VRP.solve(); |
| 2176 | DEBUG(IG.dump()); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2177 | } else if (Dest == FalseDest) { |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2178 | DOUT << "(" << DTNode->getBlock()->getName() << ") false set:\n"; |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2179 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, Dest); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2180 | VRP.add(ConstantInt::getFalse(), Condition, ICmpInst::ICMP_EQ); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2181 | VRP.solve(); |
| 2182 | DEBUG(IG.dump()); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2185 | PS->proceedToSuccessor(*I); |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2189 | void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) { |
| 2190 | Value *Condition = SI.getCondition(); |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2191 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2192 | // Set the EQProperty in each of the cases BBs, and the NEProperties |
| 2193 | // in the default BB. |
Owen Anderson | 510fefc | 2007-04-25 04:18:54 +0000 | [diff] [blame^] | 2194 | |
| 2195 | for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end(); |
| 2196 | I != E; ++I) { |
| 2197 | BasicBlock *BB = (*I)->getBlock(); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2198 | DOUT << "Switch thinking about BB %" << BB->getName() |
Nick Lewycky | 6ce36cf | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 2199 | << "(" << PS->Forest->getNodeForBlock(BB)->getDFSNumIn() << ")\n"; |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2200 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2201 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, BB); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2202 | if (BB == SI.getDefaultDest()) { |
| 2203 | for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i) |
| 2204 | if (SI.getSuccessor(i) != BB) |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2205 | VRP.add(Condition, SI.getCaseValue(i), ICmpInst::ICMP_NE); |
| 2206 | VRP.solve(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2207 | } else if (ConstantInt *CI = SI.findCaseDest(BB)) { |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2208 | VRP.add(Condition, CI, ICmpInst::ICMP_EQ); |
| 2209 | VRP.solve(); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2210 | } |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2211 | PS->proceedToSuccessor(*I); |
Nick Lewycky | 1d00f3e | 2006-10-03 15:19:11 +0000 | [diff] [blame] | 2212 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2213 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2214 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2215 | void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2216 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &AI); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2217 | VRP.add(Constant::getNullValue(AI.getType()), &AI, ICmpInst::ICMP_NE); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2218 | VRP.solve(); |
| 2219 | } |
Nick Lewycky | f345008 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 2220 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2221 | void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) { |
| 2222 | Value *Ptr = LI.getPointerOperand(); |
| 2223 | // avoid "load uint* null" -> null NE null. |
| 2224 | if (isa<Constant>(Ptr)) return; |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2225 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2226 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &LI); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2227 | VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2228 | VRP.solve(); |
| 2229 | } |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2230 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2231 | void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) { |
| 2232 | Value *Ptr = SI.getPointerOperand(); |
| 2233 | if (isa<Constant>(Ptr)) return; |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2234 | |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2235 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &SI); |
Nick Lewycky | 2fc338f | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2236 | VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE); |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2237 | VRP.solve(); |
| 2238 | } |
| 2239 | |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2240 | void PredicateSimplifier::Forwards::visitSExtInst(SExtInst &SI) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2241 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &SI); |
Reid Spencer | c34dedf | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2242 | uint32_t SrcBitWidth = cast<IntegerType>(SI.getSrcTy())->getBitWidth(); |
| 2243 | uint32_t DstBitWidth = cast<IntegerType>(SI.getDestTy())->getBitWidth(); |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2244 | APInt Min(APInt::getHighBitsSet(DstBitWidth, DstBitWidth-SrcBitWidth+1)); |
| 2245 | APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth-1)); |
Reid Spencer | c34dedf | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2246 | VRP.add(ConstantInt::get(Min), &SI, ICmpInst::ICMP_SLE); |
| 2247 | VRP.add(ConstantInt::get(Max), &SI, ICmpInst::ICMP_SGE); |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2248 | VRP.solve(); |
| 2249 | } |
| 2250 | |
| 2251 | void PredicateSimplifier::Forwards::visitZExtInst(ZExtInst &ZI) { |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2252 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &ZI); |
Reid Spencer | c34dedf | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2253 | uint32_t SrcBitWidth = cast<IntegerType>(ZI.getSrcTy())->getBitWidth(); |
| 2254 | uint32_t DstBitWidth = cast<IntegerType>(ZI.getDestTy())->getBitWidth(); |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2255 | APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth)); |
Reid Spencer | c34dedf | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2256 | VRP.add(ConstantInt::get(Max), &ZI, ICmpInst::ICMP_UGE); |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2257 | VRP.solve(); |
| 2258 | } |
| 2259 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2260 | void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) { |
| 2261 | Instruction::BinaryOps ops = BO.getOpcode(); |
| 2262 | |
| 2263 | switch (ops) { |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2264 | default: break; |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2265 | case Instruction::URem: |
| 2266 | case Instruction::SRem: |
| 2267 | case Instruction::UDiv: |
| 2268 | case Instruction::SDiv: { |
| 2269 | Value *Divisor = BO.getOperand(1); |
Nick Lewycky | d9bd0bc | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2270 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
Nick Lewycky | 1524595 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2271 | VRP.add(Constant::getNullValue(Divisor->getType()), Divisor, |
| 2272 | ICmpInst::ICMP_NE); |
| 2273 | VRP.solve(); |
| 2274 | break; |
| 2275 | } |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | switch (ops) { |
| 2279 | default: break; |
| 2280 | case Instruction::Shl: { |
| 2281 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2282 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_UGE); |
| 2283 | VRP.solve(); |
| 2284 | } break; |
| 2285 | case Instruction::AShr: { |
| 2286 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2287 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_SLE); |
| 2288 | VRP.solve(); |
| 2289 | } break; |
| 2290 | case Instruction::LShr: |
| 2291 | case Instruction::UDiv: { |
| 2292 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2293 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_ULE); |
| 2294 | VRP.solve(); |
| 2295 | } break; |
| 2296 | case Instruction::URem: { |
| 2297 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2298 | VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_ULE); |
| 2299 | VRP.solve(); |
| 2300 | } break; |
| 2301 | case Instruction::And: { |
| 2302 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2303 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_ULE); |
| 2304 | VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_ULE); |
| 2305 | VRP.solve(); |
| 2306 | } break; |
| 2307 | case Instruction::Or: { |
| 2308 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2309 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_UGE); |
| 2310 | VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_UGE); |
| 2311 | VRP.solve(); |
| 2312 | } break; |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | void PredicateSimplifier::Forwards::visitICmpInst(ICmpInst &IC) { |
| 2317 | // If possible, squeeze the ICmp predicate into something simpler. |
| 2318 | // Eg., if x = [0, 4) and we're being asked icmp uge %x, 3 then change |
| 2319 | // the predicate to eq. |
| 2320 | |
Nick Lewycky | eeb01b4 | 2007-04-07 02:30:14 +0000 | [diff] [blame] | 2321 | // XXX: once we do full PHI handling, modifying the instruction in the |
| 2322 | // Forwards visitor will cause missed optimizations. |
| 2323 | |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2324 | ICmpInst::Predicate Pred = IC.getPredicate(); |
| 2325 | |
Nick Lewycky | eeb01b4 | 2007-04-07 02:30:14 +0000 | [diff] [blame] | 2326 | switch (Pred) { |
| 2327 | default: break; |
| 2328 | case ICmpInst::ICMP_ULE: Pred = ICmpInst::ICMP_ULT; break; |
| 2329 | case ICmpInst::ICMP_UGE: Pred = ICmpInst::ICMP_UGT; break; |
| 2330 | case ICmpInst::ICMP_SLE: Pred = ICmpInst::ICMP_SLT; break; |
| 2331 | case ICmpInst::ICMP_SGE: Pred = ICmpInst::ICMP_SGT; break; |
| 2332 | } |
| 2333 | if (Pred != IC.getPredicate()) { |
| 2334 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &IC); |
| 2335 | if (VRP.isRelatedBy(IC.getOperand(1), IC.getOperand(0), |
| 2336 | ICmpInst::ICMP_NE)) { |
| 2337 | ++NumSnuggle; |
| 2338 | PS->modified = true; |
| 2339 | IC.setPredicate(Pred); |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | Pred = IC.getPredicate(); |
| 2344 | |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2345 | if (ConstantInt *Op1 = dyn_cast<ConstantInt>(IC.getOperand(1))) { |
| 2346 | ConstantInt *NextVal = 0; |
Nick Lewycky | eeb01b4 | 2007-04-07 02:30:14 +0000 | [diff] [blame] | 2347 | switch (Pred) { |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2348 | default: break; |
| 2349 | case ICmpInst::ICMP_SLT: |
| 2350 | case ICmpInst::ICMP_ULT: |
| 2351 | if (Op1->getValue() != 0) |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2352 | NextVal = ConstantInt::get(Op1->getValue()-1); |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2353 | break; |
| 2354 | case ICmpInst::ICMP_SGT: |
| 2355 | case ICmpInst::ICMP_UGT: |
| 2356 | if (!Op1->getValue().isAllOnesValue()) |
Zhou Sheng | 82fcf3c | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2357 | NextVal = ConstantInt::get(Op1->getValue()+1); |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2358 | break; |
| 2359 | |
| 2360 | } |
| 2361 | if (NextVal) { |
| 2362 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &IC); |
| 2363 | if (VRP.isRelatedBy(IC.getOperand(0), NextVal, |
| 2364 | ICmpInst::getInversePredicate(Pred))) { |
| 2365 | ICmpInst *NewIC = new ICmpInst(ICmpInst::ICMP_EQ, IC.getOperand(0), |
| 2366 | NextVal, "", &IC); |
| 2367 | NewIC->takeName(&IC); |
| 2368 | IC.replaceAllUsesWith(NewIC); |
| 2369 | IG.remove(&IC); // XXX: prove this isn't necessary |
| 2370 | IC.eraseFromParent(); |
| 2371 | ++NumSnuggle; |
| 2372 | PS->modified = true; |
Nick Lewycky | 4f73de2 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | } |
Nick Lewycky | 5f8f9af | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 2376 | } |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2377 | |
Nick Lewycky | 09b7e4d | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2378 | RegisterPass<PredicateSimplifier> X("predsimplify", |
| 2379 | "Predicate Simplifier"); |
| 2380 | } |
| 2381 | |
| 2382 | FunctionPass *llvm::createPredicateSimplifierPass() { |
| 2383 | return new PredicateSimplifier(); |
Nick Lewycky | b2e8ae1 | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2384 | } |