Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1 | //===-- PredicateSimplifier.cpp - Path Sensitive Simplifier ---------------===// |
Nick Lewycky | 05450ae | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Nick Lewycky | 05450ae | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 24 | // |
Nick Lewycky | 4c70875 | 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 | 565706b | 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 | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 29 | // |
Nick Lewycky | 565706b | 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 | e677a0b | 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 | 565706b | 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 | 419c6f5 | 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 | 565706b | 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 | e677a0b | 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 | 565706b | 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 | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 62 | // branch it can't infer anything from the "and" instruction. |
Nick Lewycky | 565706b | 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 | 4c70875 | 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 | b01c77e | 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 | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 80 | // |
| 81 | //===----------------------------------------------------------------------===// |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 82 | |
Nick Lewycky | 05450ae | 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 | 802fe27 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 86 | #include "llvm/DerivedTypes.h" |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 87 | #include "llvm/Instructions.h" |
| 88 | #include "llvm/Pass.h" |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 89 | #include "llvm/ADT/DepthFirstIterator.h" |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 90 | #include "llvm/ADT/SetOperations.h" |
Reid Spencer | 6734b57 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 91 | #include "llvm/ADT/SetVector.h" |
Nick Lewycky | 05450ae | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 95 | #include "llvm/Analysis/ET-Forest.h" |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 96 | #include "llvm/Support/CFG.h" |
Chris Lattner | 02fc40e | 2006-12-06 18:14:47 +0000 | [diff] [blame] | 97 | #include "llvm/Support/Compiler.h" |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 98 | #include "llvm/Support/ConstantRange.h" |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 99 | #include "llvm/Support/Debug.h" |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 100 | #include "llvm/Support/InstVisitor.h" |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 101 | #include "llvm/Target/TargetData.h" |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 102 | #include "llvm/Transforms/Utils/Local.h" |
| 103 | #include <algorithm> |
| 104 | #include <deque> |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 105 | #include <sstream> |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 106 | using namespace llvm; |
| 107 | |
Chris Lattner | 438e08e | 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 | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 111 | STATISTIC(NumBlocks , "Number of blocks marked unreachable"); |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 112 | STATISTIC(NumSnuggle , "Number of comparisons snuggled"); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 438e08e | 2006-12-19 21:49:03 +0000 | [diff] [blame] | 114 | namespace { |
Nick Lewycky | 419c6f5 | 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 | 6a08f91 | 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 | 419c6f5 | 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 | 6a08f91 | 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 | 419c6f5 | 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 | 6a08f91 | 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 | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static bool validPredicate(LatticeVal LV) { |
| 157 | switch (LV) { |
Nick Lewycky | 4535175 | 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 | 419c6f5 | 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 | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 172 | |
Nick Lewycky | 419c6f5 | 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 | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 184 | /// This is a StrictWeakOrdering predicate that sorts ETNodes by how many |
Nick Lewycky | 4c70875 | 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 | e677a0b | 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 | 565706b | 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 | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 210 | public: |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 211 | explicit InequalityGraph(ETNode *TreeRoot) : TreeRoot(TreeRoot) {} |
| 212 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 213 | class Node; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 214 | |
Nick Lewycky | 419c6f5 | 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 | e677a0b | 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 | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 223 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 224 | unsigned To; |
| 225 | LatticeVal LV; |
| 226 | ETNode *Subtree; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 227 | |
Nick Lewycky | 419c6f5 | 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 | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 236 | |
Nick Lewycky | 565706b | 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 | 565706b | 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 | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 243 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 244 | typedef SmallVector<Edge, 4> RelationsType; |
| 245 | RelationsType Relations; |
| 246 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 247 | Value *Canonical; |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 248 | |
Nick Lewycky | 419c6f5 | 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 | 565706b | 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 | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 256 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 257 | Node(Value *V) : Canonical(V) {} |
| 258 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 259 | private: |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 260 | #ifndef NDEBUG |
| 261 | public: |
Nick Lewycky | bc00fec | 2007-01-11 02:38:21 +0000 | [diff] [blame] | 262 | virtual ~Node() {} |
Nick Lewycky | 419c6f5 | 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 | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 278 | << " (" << NI->Subtree->getDFSNumIn() << ")\n"; |
Nick Lewycky | 419c6f5 | 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 | 565706b | 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 | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 320 | if (I == end()) { |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 324 | } else { |
Nick Lewycky | 419c6f5 | 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 | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 328 | if (Subtree != I->Subtree) { |
Nick Lewycky | 419c6f5 | 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 | dd40258 | 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 | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 342 | assert(validPredicate(LV) && "Invalid union of lattice values"); |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 343 | I->LV = LV; |
| 344 | } |
| 345 | if (I == B) break; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Nick Lewycky | 977be25 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 348 | } |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 349 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 352 | private: |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 381 | public: |
Nick Lewycky | 419c6f5 | 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 | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 389 | } |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 390 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 391 | /// Returns the node currently representing Value V, or zero if no such |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 392 | /// node exists. |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 410 | else |
| 411 | return newNode(V); |
| 412 | } |
| 413 | |
Nick Lewycky | 419c6f5 | 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 | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 423 | return MapEntry.index; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Nick Lewycky | 419c6f5 | 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 | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | |
Nick Lewycky | 565706b | 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 | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 450 | |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 454 | |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 459 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 460 | std::vector<Value *> ToRepoint; |
| 461 | ToRepoint.push_back(V); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 462 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 463 | if (unsigned Conflict = getNode(V, Subtree)) { |
Nick Lewycky | 419c6f5 | 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 | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
Nick Lewycky | 419c6f5 | 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 | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Nick Lewycky | 419c6f5 | 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 | f3a9e36 | 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 | 419c6f5 | 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 | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 521 | |
Nick Lewycky | 419c6f5 | 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 | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 524 | |
Nick Lewycky | 419c6f5 | 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 | 419c6f5 | 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 | 977be25 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 599 | |
Nick Lewycky | e677a0b | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 603 | void remove(Value *V) { |
Nick Lewycky | 419c6f5 | 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 | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 606 | if (I->V == V) { |
Nick Lewycky | e677a0b | 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 | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 619 | NodeMap.erase(I); |
| 620 | } else ++i; |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 621 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 622 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 623 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 624 | #ifndef NDEBUG |
Nick Lewycky | bc00fec | 2007-01-11 02:38:21 +0000 | [diff] [blame] | 625 | virtual ~InequalityGraph() {} |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 631 | std::set<Node *> VisitedNodes; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 632 | for (NodeMapType::const_iterator I = NodeMap.begin(), E = NodeMap.end(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 633 | I != E; ++I) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 634 | Node *N = node(I->index); |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 635 | os << *I->V << " == " << I->index |
| 636 | << "(" << I->Subtree->getDFSNumIn() << ")\n"; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 637 | if (VisitedNodes.insert(N).second) { |
Nick Lewycky | 419c6f5 | 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 | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
| 644 | #endif |
| 645 | }; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 646 | |
Nick Lewycky | e677a0b | 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 | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 674 | TargetData *TD; |
| 675 | |
Nick Lewycky | e677a0b | 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 | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 722 | I->CR = CR.intersectWith(I->CR); |
| 723 | assert(!I->CR.isEmptySet() && |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 724 | "Empty intersection of ConstantRanges!"); |
Nick Lewycky | e677a0b | 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 | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 750 | APInt UMax(CR.getUnsignedMax()); |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 751 | if (UMax.isMaxValue()) |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 752 | return ConstantRange(W); |
| 753 | return ConstantRange(APInt::getMinValue(W), UMax + 1); |
| 754 | } |
| 755 | case ICmpInst::ICMP_SLE: { |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 756 | APInt SMax(CR.getSignedMax()); |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 757 | if (SMax.isMaxSignedValue() || (SMax+1).isMaxSignedValue()) |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 758 | return ConstantRange(W); |
| 759 | return ConstantRange(APInt::getSignedMinValue(W), SMax + 1); |
| 760 | } |
| 761 | case ICmpInst::ICMP_UGT: |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 762 | return ConstantRange(CR.getUnsignedMin() + 1, APInt::getNullValue(W)); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 763 | case ICmpInst::ICMP_SGT: |
| 764 | return ConstantRange(CR.getSignedMin() + 1, |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 765 | APInt::getSignedMinValue(W)); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 766 | case ICmpInst::ICMP_UGE: { |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 767 | APInt UMin(CR.getUnsignedMin()); |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 768 | if (UMin.isMinValue()) |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 769 | return ConstantRange(W); |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 770 | return ConstantRange(UMin, APInt::getNullValue(W)); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 771 | } |
| 772 | case ICmpInst::ICMP_SGE: { |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 773 | APInt SMin(CR.getSignedMin()); |
Zhou Sheng | c125c00 | 2007-04-26 16:42:07 +0000 | [diff] [blame] | 774 | if (SMin.isMinSignedValue()) |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 775 | return ConstantRange(W); |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 776 | return ConstantRange(SMin, APInt::getSignedMinValue(W)); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /// create - Creates a ConstantRange that matches the given LatticeVal |
| 782 | /// relation with a given integer. |
| 783 | ConstantRange create(LatticeVal LV, const ConstantRange &CR) { |
| 784 | assert(!CR.isEmptySet() && "Can't deal with empty set."); |
| 785 | |
| 786 | if (LV == NE) |
| 787 | return range(ICmpInst::ICMP_NE, CR); |
| 788 | |
| 789 | unsigned LV_s = LV & (SGT_BIT|SLT_BIT); |
| 790 | unsigned LV_u = LV & (UGT_BIT|ULT_BIT); |
| 791 | bool hasEQ = LV & EQ_BIT; |
| 792 | |
| 793 | ConstantRange Range(CR.getBitWidth()); |
| 794 | |
| 795 | if (LV_s == SGT_BIT) { |
| 796 | Range = Range.intersectWith(range( |
| 797 | hasEQ ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SGT, CR)); |
| 798 | } else if (LV_s == SLT_BIT) { |
| 799 | Range = Range.intersectWith(range( |
| 800 | hasEQ ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_SLT, CR)); |
| 801 | } |
| 802 | |
| 803 | if (LV_u == UGT_BIT) { |
| 804 | Range = Range.intersectWith(range( |
| 805 | hasEQ ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_UGT, CR)); |
| 806 | } else if (LV_u == ULT_BIT) { |
| 807 | Range = Range.intersectWith(range( |
| 808 | hasEQ ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT, CR)); |
| 809 | } |
| 810 | |
| 811 | return Range; |
| 812 | } |
| 813 | |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 814 | #ifndef NDEBUG |
| 815 | bool isCanonical(Value *V, ETNode *Subtree, VRPSolver *VRP); |
| 816 | #endif |
| 817 | |
| 818 | public: |
| 819 | |
| 820 | explicit ValueRanges(TargetData *TD) : TD(TD) {} |
| 821 | |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 822 | // rangeFromValue - converts a Value into a range. If the value is a |
| 823 | // constant it constructs the single element range, otherwise it performs |
| 824 | // a lookup. The width W must be retrieved from typeToWidth and may not |
| 825 | // be zero. |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 826 | ConstantRange rangeFromValue(Value *V, ETNode *Subtree, uint32_t W) { |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 827 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 828 | return ConstantRange(C->getValue()); |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 829 | } else if (isa<ConstantPointerNull>(V)) { |
| 830 | return ConstantRange(APInt::getNullValue(W)); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 831 | } else { |
| 832 | iterator I = find(V, Subtree); |
| 833 | if (I != end()) |
| 834 | return I->CR; |
| 835 | } |
| 836 | return ConstantRange(W); |
| 837 | } |
| 838 | |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 839 | // typeToWidth - returns the number of bits necessary to store a value of |
| 840 | // this type, or zero if unknown. |
| 841 | uint32_t typeToWidth(const Type *Ty) const { |
| 842 | if (TD) |
| 843 | return TD->getTypeSizeInBits(Ty); |
| 844 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 845 | if (const IntegerType *ITy = dyn_cast<IntegerType>(Ty)) |
| 846 | return ITy->getBitWidth(); |
| 847 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 851 | bool isRelatedBy(Value *V1, Value *V2, ETNode *Subtree, LatticeVal LV) { |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 852 | uint32_t W = typeToWidth(V1->getType()); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 853 | if (!W) return false; |
| 854 | |
| 855 | ConstantRange CR1 = rangeFromValue(V1, Subtree, W); |
| 856 | ConstantRange CR2 = rangeFromValue(V2, Subtree, W); |
| 857 | |
| 858 | // True iff all values in CR1 are LV to all values in CR2. |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 859 | switch (LV) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 860 | default: assert(!"Impossible lattice value!"); |
| 861 | case NE: |
| 862 | return CR1.intersectWith(CR2).isEmptySet(); |
| 863 | case ULT: |
| 864 | return CR1.getUnsignedMax().ult(CR2.getUnsignedMin()); |
| 865 | case ULE: |
| 866 | return CR1.getUnsignedMax().ule(CR2.getUnsignedMin()); |
| 867 | case UGT: |
| 868 | return CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()); |
| 869 | case UGE: |
| 870 | return CR1.getUnsignedMin().uge(CR2.getUnsignedMax()); |
| 871 | case SLT: |
| 872 | return CR1.getSignedMax().slt(CR2.getSignedMin()); |
| 873 | case SLE: |
| 874 | return CR1.getSignedMax().sle(CR2.getSignedMin()); |
| 875 | case SGT: |
| 876 | return CR1.getSignedMin().sgt(CR2.getSignedMax()); |
| 877 | case SGE: |
| 878 | return CR1.getSignedMin().sge(CR2.getSignedMax()); |
| 879 | case LT: |
| 880 | return CR1.getUnsignedMax().ult(CR2.getUnsignedMin()) && |
| 881 | CR1.getSignedMax().slt(CR2.getUnsignedMin()); |
| 882 | case LE: |
| 883 | return CR1.getUnsignedMax().ule(CR2.getUnsignedMin()) && |
| 884 | CR1.getSignedMax().sle(CR2.getUnsignedMin()); |
| 885 | case GT: |
| 886 | return CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()) && |
| 887 | CR1.getSignedMin().sgt(CR2.getSignedMax()); |
| 888 | case GE: |
| 889 | return CR1.getUnsignedMin().uge(CR2.getUnsignedMax()) && |
| 890 | CR1.getSignedMin().sge(CR2.getSignedMax()); |
| 891 | case SLTUGT: |
| 892 | return CR1.getSignedMax().slt(CR2.getSignedMin()) && |
| 893 | CR1.getUnsignedMin().ugt(CR2.getUnsignedMax()); |
| 894 | case SLEUGE: |
| 895 | return CR1.getSignedMax().sle(CR2.getSignedMin()) && |
| 896 | CR1.getUnsignedMin().uge(CR2.getUnsignedMax()); |
| 897 | case SGTULT: |
| 898 | return CR1.getSignedMin().sgt(CR2.getSignedMax()) && |
| 899 | CR1.getUnsignedMax().ult(CR2.getUnsignedMin()); |
| 900 | case SGEULE: |
| 901 | return CR1.getSignedMin().sge(CR2.getSignedMax()) && |
| 902 | CR1.getUnsignedMax().ule(CR2.getUnsignedMin()); |
| 903 | } |
| 904 | } |
| 905 | |
Nick Lewycky | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 906 | void addToWorklist(Value *V, Constant *C, ICmpInst::Predicate Pred, |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 907 | VRPSolver *VRP); |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 908 | void markBlock(VRPSolver *VRP); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 909 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 910 | void mergeInto(Value **I, unsigned n, Value *New, ETNode *Subtree, |
| 911 | VRPSolver *VRP) { |
| 912 | assert(isCanonical(New, Subtree, VRP) && "Best choice not canonical?"); |
| 913 | |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 914 | uint32_t W = typeToWidth(New->getType()); |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 915 | if (!W) return; |
| 916 | |
| 917 | ConstantRange CR_New = rangeFromValue(New, Subtree, W); |
| 918 | ConstantRange Merged = CR_New; |
| 919 | |
| 920 | for (; n != 0; ++I, --n) { |
| 921 | ConstantRange CR_Kill = rangeFromValue(*I, Subtree, W); |
| 922 | if (CR_Kill.isFullSet()) continue; |
| 923 | Merged = Merged.intersectWith(CR_Kill); |
| 924 | } |
| 925 | |
| 926 | if (Merged.isFullSet() || Merged == CR_New) return; |
| 927 | |
Nick Lewycky | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 928 | applyRange(New, Merged, Subtree, VRP); |
| 929 | } |
| 930 | |
| 931 | void applyRange(Value *V, const ConstantRange &CR, ETNode *Subtree, |
| 932 | VRPSolver *VRP) { |
| 933 | assert(isCanonical(V, Subtree, VRP) && "Value not canonical."); |
| 934 | |
| 935 | if (const APInt *I = CR.getSingleElement()) { |
| 936 | const Type *Ty = V->getType(); |
| 937 | if (Ty->isInteger()) { |
| 938 | addToWorklist(V, ConstantInt::get(*I), ICmpInst::ICMP_EQ, VRP); |
| 939 | return; |
| 940 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 941 | assert(*I == 0 && "Pointer is null but not zero?"); |
| 942 | addToWorklist(V, ConstantPointerNull::get(PTy), |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 943 | ICmpInst::ICMP_EQ, VRP); |
Nick Lewycky | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 944 | return; |
| 945 | } |
| 946 | } |
| 947 | |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 948 | ConstantRange Merged = CR.intersectWith( |
| 949 | rangeFromValue(V, Subtree, CR.getBitWidth())); |
| 950 | if (Merged.isEmptySet()) { |
| 951 | markBlock(VRP); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | update(V, Merged, Subtree); |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Nick Lewycky | a995d92 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 958 | void addNotEquals(Value *V1, Value *V2, ETNode *Subtree, VRPSolver *VRP) { |
| 959 | uint32_t W = typeToWidth(V1->getType()); |
| 960 | if (!W) return; |
| 961 | |
| 962 | ConstantRange CR1 = rangeFromValue(V1, Subtree, W); |
| 963 | ConstantRange CR2 = rangeFromValue(V2, Subtree, W); |
| 964 | |
| 965 | if (const APInt *I = CR1.getSingleElement()) { |
| 966 | if (CR2.isFullSet()) { |
| 967 | ConstantRange NewCR2(CR1.getUpper(), CR1.getLower()); |
| 968 | applyRange(V2, NewCR2, Subtree, VRP); |
| 969 | } else if (*I == CR2.getLower()) { |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 970 | APInt NewLower(CR2.getLower() + 1), |
| 971 | NewUpper(CR2.getUpper()); |
Nick Lewycky | a995d92 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 972 | if (NewLower == NewUpper) |
| 973 | NewLower = NewUpper = APInt::getMinValue(W); |
| 974 | |
| 975 | ConstantRange NewCR2(NewLower, NewUpper); |
| 976 | applyRange(V2, NewCR2, Subtree, VRP); |
| 977 | } else if (*I == CR2.getUpper() - 1) { |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 978 | APInt NewLower(CR2.getLower()), |
| 979 | NewUpper(CR2.getUpper() - 1); |
Nick Lewycky | a995d92 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 980 | if (NewLower == NewUpper) |
| 981 | NewLower = NewUpper = APInt::getMinValue(W); |
| 982 | |
| 983 | ConstantRange NewCR2(NewLower, NewUpper); |
| 984 | applyRange(V2, NewCR2, Subtree, VRP); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | if (const APInt *I = CR2.getSingleElement()) { |
| 989 | if (CR1.isFullSet()) { |
| 990 | ConstantRange NewCR1(CR2.getUpper(), CR2.getLower()); |
| 991 | applyRange(V1, NewCR1, Subtree, VRP); |
| 992 | } else if (*I == CR1.getLower()) { |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 993 | APInt NewLower(CR1.getLower() + 1), |
| 994 | NewUpper(CR1.getUpper()); |
Nick Lewycky | a995d92 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 995 | if (NewLower == NewUpper) |
| 996 | NewLower = NewUpper = APInt::getMinValue(W); |
| 997 | |
| 998 | ConstantRange NewCR1(NewLower, NewUpper); |
| 999 | applyRange(V1, NewCR1, Subtree, VRP); |
| 1000 | } else if (*I == CR1.getUpper() - 1) { |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 1001 | APInt NewLower(CR1.getLower()), |
| 1002 | NewUpper(CR1.getUpper() - 1); |
Nick Lewycky | a995d92 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 1003 | if (NewLower == NewUpper) |
| 1004 | NewLower = NewUpper = APInt::getMinValue(W); |
| 1005 | |
| 1006 | ConstantRange NewCR1(NewLower, NewUpper); |
| 1007 | applyRange(V1, NewCR1, Subtree, VRP); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1012 | void addInequality(Value *V1, Value *V2, ETNode *Subtree, LatticeVal LV, |
| 1013 | VRPSolver *VRP) { |
| 1014 | assert(!isRelatedBy(V1, V2, Subtree, LV) && "Asked to do useless work."); |
| 1015 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1016 | assert(isCanonical(V1, Subtree, VRP) && "Value not canonical."); |
| 1017 | assert(isCanonical(V2, Subtree, VRP) && "Value not canonical."); |
| 1018 | |
Nick Lewycky | a995d92 | 2007-04-07 04:49:12 +0000 | [diff] [blame] | 1019 | if (LV == NE) { |
| 1020 | addNotEquals(V1, V2, Subtree, VRP); |
| 1021 | return; |
| 1022 | } |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1023 | |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 1024 | uint32_t W = typeToWidth(V1->getType()); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1025 | if (!W) return; |
| 1026 | |
| 1027 | ConstantRange CR1 = rangeFromValue(V1, Subtree, W); |
| 1028 | ConstantRange CR2 = rangeFromValue(V2, Subtree, W); |
| 1029 | |
| 1030 | if (!CR1.isSingleElement()) { |
| 1031 | ConstantRange NewCR1 = CR1.intersectWith(create(LV, CR2)); |
Nick Lewycky | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1032 | if (NewCR1 != CR1) |
| 1033 | applyRange(V1, NewCR1, Subtree, VRP); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | if (!CR2.isSingleElement()) { |
| 1037 | ConstantRange NewCR2 = CR2.intersectWith(create(reversePredicate(LV), |
| 1038 | CR1)); |
Nick Lewycky | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1039 | if (NewCR2 != CR2) |
| 1040 | applyRange(V2, NewCR2, Subtree, VRP); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | }; |
| 1044 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1045 | /// UnreachableBlocks keeps tracks of blocks that are for one reason or |
| 1046 | /// another discovered to be unreachable. This is used to cull the graph when |
| 1047 | /// analyzing instructions, and to mark blocks with the "unreachable" |
| 1048 | /// terminator instruction after the function has executed. |
| 1049 | class VISIBILITY_HIDDEN UnreachableBlocks { |
| 1050 | private: |
| 1051 | std::vector<BasicBlock *> DeadBlocks; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1052 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1053 | public: |
| 1054 | /// mark - mark a block as dead |
| 1055 | void mark(BasicBlock *BB) { |
| 1056 | std::vector<BasicBlock *>::iterator E = DeadBlocks.end(); |
| 1057 | std::vector<BasicBlock *>::iterator I = |
| 1058 | std::lower_bound(DeadBlocks.begin(), E, BB); |
| 1059 | |
| 1060 | if (I == E || *I != BB) DeadBlocks.insert(I, BB); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1061 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1062 | |
| 1063 | /// isDead - returns whether a block is known to be dead already |
| 1064 | bool isDead(BasicBlock *BB) { |
| 1065 | std::vector<BasicBlock *>::iterator E = DeadBlocks.end(); |
| 1066 | std::vector<BasicBlock *>::iterator I = |
| 1067 | std::lower_bound(DeadBlocks.begin(), E, BB); |
| 1068 | |
| 1069 | return I != E && *I == BB; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1070 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1071 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1072 | /// kill - replace the dead blocks' terminator with an UnreachableInst. |
| 1073 | bool kill() { |
| 1074 | bool modified = false; |
| 1075 | for (std::vector<BasicBlock *>::iterator I = DeadBlocks.begin(), |
| 1076 | E = DeadBlocks.end(); I != E; ++I) { |
| 1077 | BasicBlock *BB = *I; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1078 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1079 | DOUT << "unreachable block: " << BB->getName() << "\n"; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1080 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1081 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); |
| 1082 | SI != SE; ++SI) { |
| 1083 | BasicBlock *Succ = *SI; |
| 1084 | Succ->removePredecessor(BB); |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1085 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1086 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1087 | TerminatorInst *TI = BB->getTerminator(); |
| 1088 | TI->replaceAllUsesWith(UndefValue::get(TI->getType())); |
| 1089 | TI->eraseFromParent(); |
| 1090 | new UnreachableInst(BB); |
| 1091 | ++NumBlocks; |
| 1092 | modified = true; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1093 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1094 | DeadBlocks.clear(); |
| 1095 | return modified; |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1096 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1097 | }; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1098 | |
| 1099 | /// VRPSolver keeps track of how changes to one variable affect other |
| 1100 | /// variables, and forwards changes along to the InequalityGraph. It |
| 1101 | /// also maintains the correct choice for "canonical" in the IG. |
| 1102 | /// @brief VRPSolver calculates inferences from a new relationship. |
| 1103 | class VISIBILITY_HIDDEN VRPSolver { |
| 1104 | private: |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1105 | friend class ValueRanges; |
| 1106 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1107 | struct Operation { |
| 1108 | Value *LHS, *RHS; |
| 1109 | ICmpInst::Predicate Op; |
| 1110 | |
Nick Lewycky | 0be7f47 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1111 | BasicBlock *ContextBB; |
| 1112 | Instruction *ContextInst; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1113 | }; |
| 1114 | std::deque<Operation> WorkList; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1115 | |
| 1116 | InequalityGraph &IG; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1117 | UnreachableBlocks &UB; |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1118 | ValueRanges &VR; |
| 1119 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1120 | ETForest *Forest; |
| 1121 | ETNode *Top; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1122 | BasicBlock *TopBB; |
| 1123 | Instruction *TopInst; |
| 1124 | bool &modified; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1125 | |
| 1126 | typedef InequalityGraph::Node Node; |
| 1127 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1128 | /// IdomI - Determines whether one Instruction dominates another. |
| 1129 | bool IdomI(Instruction *I1, Instruction *I2) const { |
| 1130 | BasicBlock *BB1 = I1->getParent(), |
| 1131 | *BB2 = I2->getParent(); |
| 1132 | if (BB1 == BB2) { |
| 1133 | if (isa<TerminatorInst>(I1)) return false; |
| 1134 | if (isa<TerminatorInst>(I2)) return true; |
| 1135 | if (isa<PHINode>(I1) && !isa<PHINode>(I2)) return true; |
| 1136 | if (!isa<PHINode>(I1) && isa<PHINode>(I2)) return false; |
| 1137 | |
| 1138 | for (BasicBlock::const_iterator I = BB1->begin(), E = BB1->end(); |
| 1139 | I != E; ++I) { |
| 1140 | if (&*I == I1) return true; |
| 1141 | if (&*I == I2) return false; |
| 1142 | } |
| 1143 | assert(!"Instructions not found in parent BasicBlock?"); |
| 1144 | } else { |
| 1145 | return Forest->properlyDominates(BB1, BB2); |
| 1146 | } |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1150 | /// Returns true if V1 is a better canonical value than V2. |
| 1151 | bool compare(Value *V1, Value *V2) const { |
| 1152 | if (isa<Constant>(V1)) |
| 1153 | return !isa<Constant>(V2); |
| 1154 | else if (isa<Constant>(V2)) |
| 1155 | return false; |
| 1156 | else if (isa<Argument>(V1)) |
| 1157 | return !isa<Argument>(V2); |
| 1158 | else if (isa<Argument>(V2)) |
| 1159 | return false; |
| 1160 | |
| 1161 | Instruction *I1 = dyn_cast<Instruction>(V1); |
| 1162 | Instruction *I2 = dyn_cast<Instruction>(V2); |
| 1163 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1164 | if (!I1 || !I2) |
| 1165 | return V1->getNumUses() < V2->getNumUses(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1166 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1167 | return IdomI(I1, I2); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1170 | // below - true if the Instruction is dominated by the current context |
| 1171 | // block or instruction |
| 1172 | bool below(Instruction *I) { |
| 1173 | if (TopInst) |
| 1174 | return IdomI(TopInst, I); |
| 1175 | else { |
| 1176 | ETNode *Node = Forest->getNodeForBlock(I->getParent()); |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1177 | return Node->DominatedBy(Top); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1178 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1181 | bool makeEqual(Value *V1, Value *V2) { |
| 1182 | DOUT << "makeEqual(" << *V1 << ", " << *V2 << ")\n"; |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1183 | |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 1184 | assert(V1->getType() == V2->getType() && |
| 1185 | "Can't make two values with different types equal."); |
| 1186 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1187 | if (V1 == V2) return true; |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1188 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1189 | if (isa<Constant>(V1) && isa<Constant>(V2)) |
| 1190 | return false; |
| 1191 | |
| 1192 | unsigned n1 = IG.getNode(V1, Top), n2 = IG.getNode(V2, Top); |
| 1193 | |
| 1194 | if (n1 && n2) { |
| 1195 | if (n1 == n2) return true; |
| 1196 | if (IG.isRelatedBy(n1, n2, Top, NE)) return false; |
| 1197 | } |
| 1198 | |
| 1199 | if (n1) assert(V1 == IG.node(n1)->getValue() && "Value isn't canonical."); |
| 1200 | if (n2) assert(V2 == IG.node(n2)->getValue() && "Value isn't canonical."); |
| 1201 | |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1202 | assert(!compare(V2, V1) && "Please order parameters to makeEqual."); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1203 | |
| 1204 | assert(!isa<Constant>(V2) && "Tried to remove a constant."); |
| 1205 | |
| 1206 | SetVector<unsigned> Remove; |
| 1207 | if (n2) Remove.insert(n2); |
| 1208 | |
| 1209 | if (n1 && n2) { |
| 1210 | // Suppose we're being told that %x == %y, and %x <= %z and %y >= %z. |
| 1211 | // We can't just merge %x and %y because the relationship with %z would |
| 1212 | // be EQ and that's invalid. What we're doing is looking for any nodes |
| 1213 | // %z such that %x <= %z and %y >= %z, and vice versa. |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1214 | |
| 1215 | Node *N1 = IG.node(n1); |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1216 | Node *N2 = IG.node(n2); |
| 1217 | Node::iterator end = N2->end(); |
| 1218 | |
| 1219 | // Find the intersection between N1 and N2 which is dominated by |
| 1220 | // Top. If we find %x where N1 <= %x <= N2 (or >=) then add %x to |
| 1221 | // Remove. |
| 1222 | for (Node::iterator I = N1->begin(), E = N1->end(); I != E; ++I) { |
| 1223 | if (!(I->LV & EQ_BIT) || !Top->DominatedBy(I->Subtree)) continue; |
| 1224 | |
| 1225 | unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT); |
| 1226 | unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT); |
| 1227 | Node::iterator NI = N2->find(I->To, Top); |
| 1228 | if (NI != end) { |
| 1229 | LatticeVal NILV = reversePredicate(NI->LV); |
| 1230 | unsigned NILV_s = NILV & (SLT_BIT|SGT_BIT); |
| 1231 | unsigned NILV_u = NILV & (ULT_BIT|UGT_BIT); |
| 1232 | |
| 1233 | if ((ILV_s != (SLT_BIT|SGT_BIT) && ILV_s == NILV_s) || |
| 1234 | (ILV_u != (ULT_BIT|UGT_BIT) && ILV_u == NILV_u)) |
| 1235 | Remove.insert(I->To); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | // See if one of the nodes about to be removed is actually a better |
| 1240 | // canonical choice than n1. |
| 1241 | unsigned orig_n1 = n1; |
Reid Spencer | 7af9a13 | 2007-01-17 02:23:37 +0000 | [diff] [blame] | 1242 | SetVector<unsigned>::iterator DontRemove = Remove.end(); |
| 1243 | for (SetVector<unsigned>::iterator I = Remove.begin()+1 /* skip n2 */, |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1244 | E = Remove.end(); I != E; ++I) { |
| 1245 | unsigned n = *I; |
| 1246 | Value *V = IG.node(n)->getValue(); |
| 1247 | if (compare(V, V1)) { |
| 1248 | V1 = V; |
| 1249 | n1 = n; |
| 1250 | DontRemove = I; |
| 1251 | } |
| 1252 | } |
| 1253 | if (DontRemove != Remove.end()) { |
| 1254 | unsigned n = *DontRemove; |
| 1255 | Remove.remove(n); |
| 1256 | Remove.insert(orig_n1); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1259 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1260 | // We'd like to allow makeEqual on two values to perform a simple |
| 1261 | // substitution without every creating nodes in the IG whenever possible. |
| 1262 | // |
| 1263 | // The first iteration through this loop operates on V2 before going |
| 1264 | // through the Remove list and operating on those too. If all of the |
| 1265 | // iterations performed simple replacements then we exit early. |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1266 | bool mergeIGNode = false; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1267 | unsigned i = 0; |
| 1268 | for (Value *R = V2; i == 0 || i < Remove.size(); ++i) { |
| 1269 | if (i) R = IG.node(Remove[i])->getValue(); // skip n2. |
| 1270 | |
| 1271 | // Try to replace the whole instruction. If we can, we're done. |
| 1272 | Instruction *I2 = dyn_cast<Instruction>(R); |
| 1273 | if (I2 && below(I2)) { |
| 1274 | std::vector<Instruction *> ToNotify; |
| 1275 | for (Value::use_iterator UI = R->use_begin(), UE = R->use_end(); |
| 1276 | UI != UE;) { |
| 1277 | Use &TheUse = UI.getUse(); |
| 1278 | ++UI; |
| 1279 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) |
| 1280 | ToNotify.push_back(I); |
| 1281 | } |
| 1282 | |
| 1283 | DOUT << "Simply removing " << *I2 |
| 1284 | << ", replacing with " << *V1 << "\n"; |
| 1285 | I2->replaceAllUsesWith(V1); |
| 1286 | // leave it dead; it'll get erased later. |
| 1287 | ++NumInstruction; |
| 1288 | modified = true; |
| 1289 | |
| 1290 | for (std::vector<Instruction *>::iterator II = ToNotify.begin(), |
| 1291 | IE = ToNotify.end(); II != IE; ++II) { |
| 1292 | opsToDef(*II); |
| 1293 | } |
| 1294 | |
| 1295 | continue; |
| 1296 | } |
| 1297 | |
| 1298 | // Otherwise, replace all dominated uses. |
| 1299 | for (Value::use_iterator UI = R->use_begin(), UE = R->use_end(); |
| 1300 | UI != UE;) { |
| 1301 | Use &TheUse = UI.getUse(); |
| 1302 | ++UI; |
| 1303 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
| 1304 | if (below(I)) { |
| 1305 | TheUse.set(V1); |
| 1306 | modified = true; |
| 1307 | ++NumVarsReplaced; |
| 1308 | opsToDef(I); |
| 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | // If that killed the instruction, stop here. |
| 1314 | if (I2 && isInstructionTriviallyDead(I2)) { |
| 1315 | DOUT << "Killed all uses of " << *I2 |
| 1316 | << ", replacing with " << *V1 << "\n"; |
| 1317 | continue; |
| 1318 | } |
| 1319 | |
| 1320 | // If we make it to here, then we will need to create a node for N1. |
| 1321 | // Otherwise, we can skip out early! |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1322 | mergeIGNode = true; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1325 | if (!isa<Constant>(V1)) { |
| 1326 | if (Remove.empty()) { |
| 1327 | VR.mergeInto(&V2, 1, V1, Top, this); |
| 1328 | } else { |
| 1329 | std::vector<Value*> RemoveVals; |
| 1330 | RemoveVals.reserve(Remove.size()); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1331 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1332 | for (SetVector<unsigned>::iterator I = Remove.begin(), |
| 1333 | E = Remove.end(); I != E; ++I) { |
| 1334 | Value *V = IG.node(*I)->getValue(); |
| 1335 | if (!V->use_empty()) |
| 1336 | RemoveVals.push_back(V); |
| 1337 | } |
| 1338 | VR.mergeInto(&RemoveVals[0], RemoveVals.size(), V1, Top, this); |
| 1339 | } |
| 1340 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1341 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1342 | if (mergeIGNode) { |
| 1343 | // Create N1. |
| 1344 | if (!n1) n1 = IG.newNode(V1); |
| 1345 | |
| 1346 | // Migrate relationships from removed nodes to N1. |
| 1347 | Node *N1 = IG.node(n1); |
| 1348 | for (SetVector<unsigned>::iterator I = Remove.begin(), E = Remove.end(); |
| 1349 | I != E; ++I) { |
| 1350 | unsigned n = *I; |
| 1351 | Node *N = IG.node(n); |
| 1352 | for (Node::iterator NI = N->begin(), NE = N->end(); NI != NE; ++NI) { |
| 1353 | if (NI->Subtree->DominatedBy(Top)) { |
| 1354 | if (NI->To == n1) { |
| 1355 | assert((NI->LV & EQ_BIT) && "Node inequal to itself."); |
| 1356 | continue; |
| 1357 | } |
| 1358 | if (Remove.count(NI->To)) |
| 1359 | continue; |
| 1360 | |
| 1361 | IG.node(NI->To)->update(n1, reversePredicate(NI->LV), Top); |
| 1362 | N1->update(NI->To, NI->LV, Top); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1363 | } |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1364 | } |
| 1365 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1366 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1367 | // Point V2 (and all items in Remove) to N1. |
| 1368 | if (!n2) |
| 1369 | IG.addEquality(n1, V2, Top); |
| 1370 | else { |
| 1371 | for (SetVector<unsigned>::iterator I = Remove.begin(), |
| 1372 | E = Remove.end(); I != E; ++I) { |
| 1373 | IG.addEquality(n1, IG.node(*I)->getValue(), Top); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | // If !Remove.empty() then V2 = Remove[0]->getValue(). |
| 1378 | // Even when Remove is empty, we still want to process V2. |
| 1379 | i = 0; |
| 1380 | for (Value *R = V2; i == 0 || i < Remove.size(); ++i) { |
| 1381 | if (i) R = IG.node(Remove[i])->getValue(); // skip n2. |
| 1382 | |
| 1383 | if (Instruction *I2 = dyn_cast<Instruction>(R)) { |
| 1384 | if (below(I2) || |
| 1385 | Top->DominatedBy(Forest->getNodeForBlock(I2->getParent()))) |
| 1386 | defToOps(I2); |
| 1387 | } |
| 1388 | for (Value::use_iterator UI = V2->use_begin(), UE = V2->use_end(); |
| 1389 | UI != UE;) { |
| 1390 | Use &TheUse = UI.getUse(); |
| 1391 | ++UI; |
| 1392 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
| 1393 | if (below(I) || |
| 1394 | Top->DominatedBy(Forest->getNodeForBlock(I->getParent()))) |
| 1395 | opsToDef(I); |
| 1396 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1397 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1398 | } |
| 1399 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1400 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1401 | // re-opsToDef all dominated users of V1. |
| 1402 | if (Instruction *I = dyn_cast<Instruction>(V1)) { |
| 1403 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1404 | UI != UE;) { |
| 1405 | Use &TheUse = UI.getUse(); |
| 1406 | ++UI; |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1407 | Value *V = TheUse.getUser(); |
| 1408 | if (!V->use_empty()) { |
| 1409 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
| 1410 | if (below(Inst) || |
| 1411 | Top->DominatedBy(Forest->getNodeForBlock(Inst->getParent()))) |
| 1412 | opsToDef(Inst); |
| 1413 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | /// cmpInstToLattice - converts an CmpInst::Predicate to lattice value |
| 1422 | /// Requires that the lattice value be valid; does not accept ICMP_EQ. |
| 1423 | static LatticeVal cmpInstToLattice(ICmpInst::Predicate Pred) { |
| 1424 | switch (Pred) { |
| 1425 | case ICmpInst::ICMP_EQ: |
| 1426 | assert(!"No matching lattice value."); |
| 1427 | return static_cast<LatticeVal>(EQ_BIT); |
| 1428 | default: |
| 1429 | assert(!"Invalid 'icmp' predicate."); |
| 1430 | case ICmpInst::ICMP_NE: |
| 1431 | return NE; |
| 1432 | case ICmpInst::ICMP_UGT: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1433 | return UGT; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1434 | case ICmpInst::ICMP_UGE: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1435 | return UGE; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1436 | case ICmpInst::ICMP_ULT: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1437 | return ULT; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1438 | case ICmpInst::ICMP_ULE: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1439 | return ULE; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1440 | case ICmpInst::ICMP_SGT: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1441 | return SGT; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1442 | case ICmpInst::ICMP_SGE: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1443 | return SGE; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1444 | case ICmpInst::ICMP_SLT: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1445 | return SLT; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1446 | case ICmpInst::ICMP_SLE: |
Nick Lewycky | 6a08f91 | 2007-01-29 02:56:54 +0000 | [diff] [blame] | 1447 | return SLE; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1448 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1449 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1450 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1451 | public: |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1452 | VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ValueRanges &VR, |
| 1453 | ETForest *Forest, bool &modified, BasicBlock *TopBB) |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1454 | : IG(IG), |
| 1455 | UB(UB), |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1456 | VR(VR), |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1457 | Forest(Forest), |
| 1458 | Top(Forest->getNodeForBlock(TopBB)), |
| 1459 | TopBB(TopBB), |
| 1460 | TopInst(NULL), |
| 1461 | modified(modified) {} |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1462 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1463 | VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ValueRanges &VR, |
| 1464 | ETForest *Forest, bool &modified, Instruction *TopInst) |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1465 | : IG(IG), |
| 1466 | UB(UB), |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1467 | VR(VR), |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1468 | Forest(Forest), |
| 1469 | TopInst(TopInst), |
| 1470 | modified(modified) |
| 1471 | { |
| 1472 | TopBB = TopInst->getParent(); |
| 1473 | Top = Forest->getNodeForBlock(TopBB); |
| 1474 | } |
| 1475 | |
| 1476 | bool isRelatedBy(Value *V1, Value *V2, ICmpInst::Predicate Pred) const { |
| 1477 | if (Constant *C1 = dyn_cast<Constant>(V1)) |
| 1478 | if (Constant *C2 = dyn_cast<Constant>(V2)) |
| 1479 | return ConstantExpr::getCompare(Pred, C1, C2) == |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1480 | ConstantInt::getTrue(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1481 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1482 | if (unsigned n1 = IG.getNode(V1, Top)) |
| 1483 | if (unsigned n2 = IG.getNode(V2, Top)) { |
| 1484 | if (n1 == n2) return Pred == ICmpInst::ICMP_EQ || |
| 1485 | Pred == ICmpInst::ICMP_ULE || |
| 1486 | Pred == ICmpInst::ICMP_UGE || |
| 1487 | Pred == ICmpInst::ICMP_SLE || |
| 1488 | Pred == ICmpInst::ICMP_SGE; |
| 1489 | if (Pred == ICmpInst::ICMP_EQ) return false; |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1490 | if (IG.isRelatedBy(n1, n2, Top, cmpInstToLattice(Pred))) return true; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1493 | if (Pred == ICmpInst::ICMP_EQ) return V1 == V2; |
| 1494 | return VR.isRelatedBy(V1, V2, Top, cmpInstToLattice(Pred)); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1497 | /// add - adds a new property to the work queue |
| 1498 | void add(Value *V1, Value *V2, ICmpInst::Predicate Pred, |
| 1499 | Instruction *I = NULL) { |
| 1500 | DOUT << "adding " << *V1 << " " << Pred << " " << *V2; |
| 1501 | if (I) DOUT << " context: " << *I; |
| 1502 | else DOUT << " default context"; |
| 1503 | DOUT << "\n"; |
| 1504 | |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 1505 | assert(V1->getType() == V2->getType() && |
| 1506 | "Can't relate two values with different types."); |
| 1507 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1508 | WorkList.push_back(Operation()); |
| 1509 | Operation &O = WorkList.back(); |
Nick Lewycky | 0be7f47 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1510 | O.LHS = V1, O.RHS = V2, O.Op = Pred, O.ContextInst = I; |
| 1511 | O.ContextBB = I ? I->getParent() : TopBB; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1514 | /// defToOps - Given an instruction definition that we've learned something |
| 1515 | /// new about, find any new relationships between its operands. |
| 1516 | void defToOps(Instruction *I) { |
| 1517 | Instruction *NewContext = below(I) ? I : TopInst; |
| 1518 | Value *Canonical = IG.canonicalize(I, Top); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1519 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1520 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 1521 | const Type *Ty = BO->getType(); |
| 1522 | assert(!Ty->isFPOrFPVector() && "Float in work queue!"); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1523 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1524 | Value *Op0 = IG.canonicalize(BO->getOperand(0), Top); |
| 1525 | Value *Op1 = IG.canonicalize(BO->getOperand(1), Top); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1526 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1527 | // TODO: "and i32 -1, %x" EQ %y then %x EQ %y. |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1528 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1529 | switch (BO->getOpcode()) { |
| 1530 | case Instruction::And: { |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1531 | // "and i32 %a, %b" EQ -1 then %a EQ -1 and %b EQ -1 |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1532 | ConstantInt *CI = ConstantInt::getAllOnesValue(Ty); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1533 | if (Canonical == CI) { |
| 1534 | add(CI, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1535 | add(CI, Op1, ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1536 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1537 | } break; |
| 1538 | case Instruction::Or: { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1539 | // "or i32 %a, %b" EQ 0 then %a EQ 0 and %b EQ 0 |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1540 | Constant *Zero = Constant::getNullValue(Ty); |
| 1541 | if (Canonical == Zero) { |
| 1542 | add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1543 | add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext); |
| 1544 | } |
| 1545 | } break; |
| 1546 | case Instruction::Xor: { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1547 | // "xor i32 %c, %a" EQ %b then %a EQ %c ^ %b |
| 1548 | // "xor i32 %c, %a" EQ %c then %a EQ 0 |
| 1549 | // "xor i32 %c, %a" NE %c then %a NE 0 |
| 1550 | // Repeat the above, with order of operands reversed. |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1551 | Value *LHS = Op0; |
| 1552 | Value *RHS = Op1; |
| 1553 | if (!isa<Constant>(LHS)) std::swap(LHS, RHS); |
| 1554 | |
Nick Lewycky | c2a7d09 | 2007-01-12 00:02:12 +0000 | [diff] [blame] | 1555 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Canonical)) { |
| 1556 | if (ConstantInt *Arg = dyn_cast<ConstantInt>(LHS)) { |
Reid Spencer | af3e946 | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 1557 | add(RHS, ConstantInt::get(CI->getValue() ^ Arg->getValue()), |
Nick Lewycky | c2a7d09 | 2007-01-12 00:02:12 +0000 | [diff] [blame] | 1558 | ICmpInst::ICMP_EQ, NewContext); |
| 1559 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1560 | } |
| 1561 | if (Canonical == LHS) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1562 | if (isa<ConstantInt>(Canonical)) |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1563 | add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ, |
| 1564 | NewContext); |
| 1565 | } else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) { |
| 1566 | add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE, |
| 1567 | NewContext); |
| 1568 | } |
| 1569 | } break; |
| 1570 | default: |
| 1571 | break; |
| 1572 | } |
| 1573 | } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1574 | // "icmp ult i32 %a, %y" EQ true then %a u< y |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1575 | // etc. |
| 1576 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1577 | if (Canonical == ConstantInt::getTrue()) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1578 | add(IC->getOperand(0), IC->getOperand(1), IC->getPredicate(), |
| 1579 | NewContext); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1580 | } else if (Canonical == ConstantInt::getFalse()) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1581 | add(IC->getOperand(0), IC->getOperand(1), |
| 1582 | ICmpInst::getInversePredicate(IC->getPredicate()), NewContext); |
| 1583 | } |
| 1584 | } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { |
| 1585 | if (I->getType()->isFPOrFPVector()) return; |
| 1586 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1587 | // Given: "%a = select i1 %x, i32 %b, i32 %c" |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1588 | // %a EQ %b and %b NE %c then %x EQ true |
| 1589 | // %a EQ %c and %b NE %c then %x EQ false |
| 1590 | |
| 1591 | Value *True = SI->getTrueValue(); |
| 1592 | Value *False = SI->getFalseValue(); |
| 1593 | if (isRelatedBy(True, False, ICmpInst::ICMP_NE)) { |
| 1594 | if (Canonical == IG.canonicalize(True, Top) || |
| 1595 | isRelatedBy(Canonical, False, ICmpInst::ICMP_NE)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1596 | add(SI->getCondition(), ConstantInt::getTrue(), |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1597 | ICmpInst::ICMP_EQ, NewContext); |
| 1598 | else if (Canonical == IG.canonicalize(False, Top) || |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1599 | isRelatedBy(Canonical, True, ICmpInst::ICMP_NE)) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1600 | add(SI->getCondition(), ConstantInt::getFalse(), |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1601 | ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1602 | } |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1603 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
| 1604 | for (GetElementPtrInst::op_iterator OI = GEPI->idx_begin(), |
| 1605 | OE = GEPI->idx_end(); OI != OE; ++OI) { |
| 1606 | ConstantInt *Op = dyn_cast<ConstantInt>(IG.canonicalize(*OI, Top)); |
| 1607 | if (!Op || !Op->isZero()) return; |
| 1608 | } |
| 1609 | // TODO: The GEPI indices are all zero. Copy from definition to operand, |
| 1610 | // jumping the type plane as needed. |
| 1611 | if (isRelatedBy(GEPI, Constant::getNullValue(GEPI->getType()), |
| 1612 | ICmpInst::ICMP_NE)) { |
| 1613 | Value *Ptr = GEPI->getPointerOperand(); |
| 1614 | add(Ptr, Constant::getNullValue(Ptr->getType()), ICmpInst::ICMP_NE, |
| 1615 | NewContext); |
| 1616 | } |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1617 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 1618 | const Type *SrcTy = CI->getSrcTy(); |
| 1619 | |
| 1620 | Value *TheCI = IG.canonicalize(CI, Top); |
| 1621 | uint32_t W = VR.typeToWidth(SrcTy); |
| 1622 | if (!W) return; |
| 1623 | ConstantRange CR = VR.rangeFromValue(TheCI, Top, W); |
| 1624 | |
| 1625 | if (CR.isFullSet()) return; |
| 1626 | |
| 1627 | switch (CI->getOpcode()) { |
| 1628 | default: break; |
| 1629 | case Instruction::ZExt: |
| 1630 | case Instruction::SExt: |
| 1631 | VR.applyRange(IG.canonicalize(CI->getOperand(0), Top), |
| 1632 | CR.truncate(W), Top, this); |
| 1633 | break; |
| 1634 | case Instruction::BitCast: |
| 1635 | VR.applyRange(IG.canonicalize(CI->getOperand(0), Top), |
| 1636 | CR, Top, this); |
| 1637 | break; |
| 1638 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1639 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1640 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1641 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1642 | /// opsToDef - A new relationship was discovered involving one of this |
| 1643 | /// instruction's operands. Find any new relationship involving the |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1644 | /// definition, or another operand. |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1645 | void opsToDef(Instruction *I) { |
| 1646 | Instruction *NewContext = below(I) ? I : TopInst; |
| 1647 | |
| 1648 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 1649 | Value *Op0 = IG.canonicalize(BO->getOperand(0), Top); |
| 1650 | Value *Op1 = IG.canonicalize(BO->getOperand(1), Top); |
| 1651 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1652 | if (ConstantInt *CI0 = dyn_cast<ConstantInt>(Op0)) |
| 1653 | if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Op1)) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1654 | add(BO, ConstantExpr::get(BO->getOpcode(), CI0, CI1), |
| 1655 | ICmpInst::ICMP_EQ, NewContext); |
| 1656 | return; |
| 1657 | } |
| 1658 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1659 | // "%y = and i1 true, %x" then %x EQ %y |
| 1660 | // "%y = or i1 false, %x" then %x EQ %y |
| 1661 | // "%x = add i32 %y, 0" then %x EQ %y |
| 1662 | // "%x = mul i32 %y, 0" then %x EQ 0 |
| 1663 | |
| 1664 | Instruction::BinaryOps Opcode = BO->getOpcode(); |
Nick Lewycky | 3f64b1a | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1665 | const Type *Ty = BO->getType(); |
| 1666 | assert(!Ty->isFPOrFPVector() && "Float in work queue!"); |
| 1667 | |
| 1668 | Constant *Zero = Constant::getNullValue(Ty); |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1669 | ConstantInt *AllOnes = ConstantInt::getAllOnesValue(Ty); |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1670 | |
| 1671 | switch (Opcode) { |
| 1672 | default: break; |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1673 | case Instruction::LShr: |
| 1674 | case Instruction::AShr: |
| 1675 | case Instruction::Shl: |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1676 | case Instruction::Sub: |
Nick Lewycky | 3f64b1a | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1677 | if (Op1 == Zero) { |
| 1678 | add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1679 | return; |
| 1680 | } |
| 1681 | break; |
| 1682 | case Instruction::Or: |
| 1683 | if (Op0 == AllOnes || Op1 == AllOnes) { |
| 1684 | add(BO, AllOnes, ICmpInst::ICMP_EQ, NewContext); |
| 1685 | return; |
| 1686 | } // fall-through |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1687 | case Instruction::Xor: |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1688 | case Instruction::Add: |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1689 | if (Op0 == Zero) { |
| 1690 | add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); |
| 1691 | return; |
| 1692 | } else if (Op1 == Zero) { |
| 1693 | add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1694 | return; |
| 1695 | } |
Nick Lewycky | 3f64b1a | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | case Instruction::And: |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1698 | if (Op0 == AllOnes) { |
| 1699 | add(BO, Op1, ICmpInst::ICMP_EQ, NewContext); |
| 1700 | return; |
| 1701 | } else if (Op1 == AllOnes) { |
| 1702 | add(BO, Op0, ICmpInst::ICMP_EQ, NewContext); |
| 1703 | return; |
| 1704 | } |
Nick Lewycky | 3f64b1a | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1705 | // fall-through |
| 1706 | case Instruction::Mul: |
| 1707 | if (Op0 == Zero || Op1 == Zero) { |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1708 | add(BO, Zero, ICmpInst::ICMP_EQ, NewContext); |
| 1709 | return; |
| 1710 | } |
Nick Lewycky | 3f64b1a | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1711 | break; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1712 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1713 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1714 | // "%x = add i32 %y, %z" and %x EQ %y then %z EQ 0 |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1715 | // "%x = add i32 %y, %z" and %x EQ %z then %y EQ 0 |
| 1716 | // "%x = shl i32 %y, %z" and %x EQ %y and %y NE 0 then %z EQ 0 |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1717 | // "%x = udiv i32 %y, %z" and %x EQ %y then %z EQ 1 |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1718 | |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1719 | Value *Known = Op0, *Unknown = Op1, |
| 1720 | *TheBO = IG.canonicalize(BO, Top); |
| 1721 | if (Known != TheBO) std::swap(Known, Unknown); |
| 1722 | if (Known == TheBO) { |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1723 | switch (Opcode) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1724 | default: break; |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1725 | case Instruction::LShr: |
| 1726 | case Instruction::AShr: |
| 1727 | case Instruction::Shl: |
| 1728 | if (!isRelatedBy(Known, Zero, ICmpInst::ICMP_NE)) break; |
| 1729 | // otherwise, fall-through. |
| 1730 | case Instruction::Sub: |
| 1731 | if (Unknown == Op1) break; |
| 1732 | // otherwise, fall-through. |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1733 | case Instruction::Xor: |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1734 | case Instruction::Add: |
Nick Lewycky | 3f64b1a | 2007-03-18 22:58:46 +0000 | [diff] [blame] | 1735 | add(Unknown, Zero, ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1736 | break; |
| 1737 | case Instruction::UDiv: |
| 1738 | case Instruction::SDiv: |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1739 | if (Unknown == Op1) break; |
| 1740 | if (isRelatedBy(Known, Zero, ICmpInst::ICMP_NE)) { |
Nick Lewycky | c2a7d09 | 2007-01-12 00:02:12 +0000 | [diff] [blame] | 1741 | Constant *One = ConstantInt::get(Ty, 1); |
| 1742 | add(Unknown, One, ICmpInst::ICMP_EQ, NewContext); |
| 1743 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1744 | break; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1745 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1746 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1747 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1748 | // TODO: "%a = add i32 %b, 1" and %b > %z then %a >= %z. |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1749 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1750 | } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) { |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1751 | // "%a = icmp ult i32 %b, %c" and %b u< %c then %a EQ true |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1752 | // "%a = icmp ult i32 %b, %c" and %b u>= %c then %a EQ false |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1753 | // etc. |
| 1754 | |
| 1755 | Value *Op0 = IG.canonicalize(IC->getOperand(0), Top); |
| 1756 | Value *Op1 = IG.canonicalize(IC->getOperand(1), Top); |
| 1757 | |
| 1758 | ICmpInst::Predicate Pred = IC->getPredicate(); |
| 1759 | if (isRelatedBy(Op0, Op1, Pred)) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1760 | add(IC, ConstantInt::getTrue(), ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1761 | } else if (isRelatedBy(Op0, Op1, ICmpInst::getInversePredicate(Pred))) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1762 | add(IC, ConstantInt::getFalse(), ICmpInst::ICMP_EQ, NewContext); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1765 | } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1766 | if (I->getType()->isFPOrFPVector()) return; |
| 1767 | |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 1768 | // Given: "%a = select i1 %x, i32 %b, i32 %c" |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1769 | // %x EQ true then %a EQ %b |
| 1770 | // %x EQ false then %a EQ %c |
| 1771 | // %b EQ %c then %a EQ %b |
| 1772 | |
| 1773 | Value *Canonical = IG.canonicalize(SI->getCondition(), Top); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1774 | if (Canonical == ConstantInt::getTrue()) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1775 | add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1776 | } else if (Canonical == ConstantInt::getFalse()) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1777 | add(SI, SI->getFalseValue(), ICmpInst::ICMP_EQ, NewContext); |
| 1778 | } else if (IG.canonicalize(SI->getTrueValue(), Top) == |
| 1779 | IG.canonicalize(SI->getFalseValue(), Top)) { |
| 1780 | add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext); |
| 1781 | } |
Nick Lewycky | 28c5b15 | 2007-01-12 01:23:53 +0000 | [diff] [blame] | 1782 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1783 | const Type *DestTy = CI->getDestTy(); |
| 1784 | if (DestTy->isFPOrFPVector()) return; |
Nick Lewycky | 28c5b15 | 2007-01-12 01:23:53 +0000 | [diff] [blame] | 1785 | |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1786 | Value *Op = IG.canonicalize(CI->getOperand(0), Top); |
| 1787 | Instruction::CastOps Opcode = CI->getOpcode(); |
| 1788 | |
| 1789 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 1790 | add(CI, ConstantExpr::getCast(Opcode, C, DestTy), |
Nick Lewycky | 28c5b15 | 2007-01-12 01:23:53 +0000 | [diff] [blame] | 1791 | ICmpInst::ICMP_EQ, NewContext); |
| 1792 | } |
| 1793 | |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1794 | uint32_t W = VR.typeToWidth(DestTy); |
| 1795 | Value *TheCI = IG.canonicalize(CI, Top); |
| 1796 | ConstantRange CR = VR.rangeFromValue(Op, Top, W); |
| 1797 | |
| 1798 | if (!CR.isFullSet()) { |
| 1799 | switch (Opcode) { |
| 1800 | default: break; |
| 1801 | case Instruction::ZExt: |
| 1802 | VR.applyRange(TheCI, CR.zeroExtend(W), Top, this); |
| 1803 | break; |
| 1804 | case Instruction::SExt: |
| 1805 | VR.applyRange(TheCI, CR.signExtend(W), Top, this); |
| 1806 | break; |
| 1807 | case Instruction::Trunc: { |
| 1808 | ConstantRange Result = CR.truncate(W); |
| 1809 | if (!Result.isFullSet()) |
| 1810 | VR.applyRange(TheCI, Result, Top, this); |
| 1811 | } break; |
| 1812 | case Instruction::BitCast: |
| 1813 | VR.applyRange(TheCI, CR, Top, this); |
| 1814 | break; |
| 1815 | // TODO: other casts? |
| 1816 | } |
| 1817 | } |
Nick Lewycky | 27e4da9 | 2007-03-22 02:02:51 +0000 | [diff] [blame] | 1818 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
| 1819 | for (GetElementPtrInst::op_iterator OI = GEPI->idx_begin(), |
| 1820 | OE = GEPI->idx_end(); OI != OE; ++OI) { |
| 1821 | ConstantInt *Op = dyn_cast<ConstantInt>(IG.canonicalize(*OI, Top)); |
| 1822 | if (!Op || !Op->isZero()) return; |
| 1823 | } |
| 1824 | // TODO: The GEPI indices are all zero. Copy from operand to definition, |
| 1825 | // jumping the type plane as needed. |
| 1826 | Value *Ptr = GEPI->getPointerOperand(); |
| 1827 | if (isRelatedBy(Ptr, Constant::getNullValue(Ptr->getType()), |
| 1828 | ICmpInst::ICMP_NE)) { |
| 1829 | add(GEPI, Constant::getNullValue(GEPI->getType()), ICmpInst::ICMP_NE, |
| 1830 | NewContext); |
| 1831 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1832 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /// solve - process the work queue |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1836 | void solve() { |
| 1837 | //DOUT << "WorkList entry, size: " << WorkList.size() << "\n"; |
| 1838 | while (!WorkList.empty()) { |
| 1839 | //DOUT << "WorkList size: " << WorkList.size() << "\n"; |
| 1840 | |
| 1841 | Operation &O = WorkList.front(); |
Nick Lewycky | 0be7f47 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1842 | TopInst = O.ContextInst; |
| 1843 | TopBB = O.ContextBB; |
| 1844 | Top = Forest->getNodeForBlock(TopBB); |
| 1845 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1846 | O.LHS = IG.canonicalize(O.LHS, Top); |
| 1847 | O.RHS = IG.canonicalize(O.RHS, Top); |
| 1848 | |
| 1849 | assert(O.LHS == IG.canonicalize(O.LHS, Top) && "Canonicalize isn't."); |
| 1850 | assert(O.RHS == IG.canonicalize(O.RHS, Top) && "Canonicalize isn't."); |
| 1851 | |
| 1852 | DOUT << "solving " << *O.LHS << " " << O.Op << " " << *O.RHS; |
Nick Lewycky | 0be7f47 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 1853 | if (O.ContextInst) DOUT << " context inst: " << *O.ContextInst; |
| 1854 | else DOUT << " context block: " << O.ContextBB->getName(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1855 | DOUT << "\n"; |
| 1856 | |
| 1857 | DEBUG(IG.dump()); |
| 1858 | |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1859 | // If they're both Constant, skip it. Check for contradiction and mark |
| 1860 | // the BB as unreachable if so. |
| 1861 | if (Constant *CI_L = dyn_cast<Constant>(O.LHS)) { |
| 1862 | if (Constant *CI_R = dyn_cast<Constant>(O.RHS)) { |
| 1863 | if (ConstantExpr::getCompare(O.Op, CI_L, CI_R) == |
| 1864 | ConstantInt::getFalse()) |
| 1865 | UB.mark(TopBB); |
| 1866 | |
| 1867 | WorkList.pop_front(); |
| 1868 | continue; |
| 1869 | } |
| 1870 | } |
| 1871 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1872 | if (compare(O.LHS, O.RHS)) { |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1873 | std::swap(O.LHS, O.RHS); |
| 1874 | O.Op = ICmpInst::getSwappedPredicate(O.Op); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | if (O.Op == ICmpInst::ICMP_EQ) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1878 | if (!makeEqual(O.RHS, O.LHS)) |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1879 | UB.mark(TopBB); |
| 1880 | } else { |
| 1881 | LatticeVal LV = cmpInstToLattice(O.Op); |
| 1882 | |
| 1883 | if ((LV & EQ_BIT) && |
| 1884 | isRelatedBy(O.LHS, O.RHS, ICmpInst::getSwappedPredicate(O.Op))) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1885 | if (!makeEqual(O.RHS, O.LHS)) |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1886 | UB.mark(TopBB); |
| 1887 | } else { |
| 1888 | if (isRelatedBy(O.LHS, O.RHS, ICmpInst::getInversePredicate(O.Op))){ |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1889 | UB.mark(TopBB); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1890 | WorkList.pop_front(); |
| 1891 | continue; |
| 1892 | } |
| 1893 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1894 | unsigned n1 = IG.getNode(O.LHS, Top); |
| 1895 | unsigned n2 = IG.getNode(O.RHS, Top); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1896 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1897 | if (n1 && n1 == n2) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1898 | if (O.Op != ICmpInst::ICMP_UGE && O.Op != ICmpInst::ICMP_ULE && |
| 1899 | O.Op != ICmpInst::ICMP_SGE && O.Op != ICmpInst::ICMP_SLE) |
| 1900 | UB.mark(TopBB); |
| 1901 | |
| 1902 | WorkList.pop_front(); |
| 1903 | continue; |
| 1904 | } |
| 1905 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1906 | if (VR.isRelatedBy(O.LHS, O.RHS, Top, LV) || |
| 1907 | (n1 && n2 && IG.isRelatedBy(n1, n2, Top, LV))) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1908 | WorkList.pop_front(); |
| 1909 | continue; |
| 1910 | } |
| 1911 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1912 | VR.addInequality(O.LHS, O.RHS, Top, LV, this); |
| 1913 | if ((!isa<ConstantInt>(O.RHS) && !isa<ConstantInt>(O.LHS)) || |
| 1914 | LV == NE) { |
| 1915 | if (!n1) n1 = IG.newNode(O.LHS); |
| 1916 | if (!n2) n2 = IG.newNode(O.RHS); |
| 1917 | IG.addInequality(n1, n2, Top, LV); |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1920 | if (Instruction *I1 = dyn_cast<Instruction>(O.LHS)) { |
| 1921 | if (below(I1) || |
| 1922 | Top->DominatedBy(Forest->getNodeForBlock(I1->getParent()))) |
| 1923 | defToOps(I1); |
| 1924 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1925 | if (isa<Instruction>(O.LHS) || isa<Argument>(O.LHS)) { |
| 1926 | for (Value::use_iterator UI = O.LHS->use_begin(), |
| 1927 | UE = O.LHS->use_end(); UI != UE;) { |
| 1928 | Use &TheUse = UI.getUse(); |
| 1929 | ++UI; |
| 1930 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1931 | if (below(I) || |
| 1932 | Top->DominatedBy(Forest->getNodeForBlock(I->getParent()))) |
| 1933 | opsToDef(I); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | } |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1937 | if (Instruction *I2 = dyn_cast<Instruction>(O.RHS)) { |
| 1938 | if (below(I2) || |
| 1939 | Top->DominatedBy(Forest->getNodeForBlock(I2->getParent()))) |
| 1940 | defToOps(I2); |
| 1941 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1942 | if (isa<Instruction>(O.RHS) || isa<Argument>(O.RHS)) { |
| 1943 | for (Value::use_iterator UI = O.RHS->use_begin(), |
| 1944 | UE = O.RHS->use_end(); UI != UE;) { |
| 1945 | Use &TheUse = UI.getUse(); |
| 1946 | ++UI; |
| 1947 | if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) { |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 1948 | if (below(I) || |
| 1949 | Top->DominatedBy(Forest->getNodeForBlock(I->getParent()))) |
| 1950 | |
| 1951 | opsToDef(I); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1952 | } |
| 1953 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1954 | } |
| 1955 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1956 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1957 | WorkList.pop_front(); |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1958 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 1959 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1960 | }; |
| 1961 | |
Nick Lewycky | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1962 | void ValueRanges::addToWorklist(Value *V, Constant *C, |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1963 | ICmpInst::Predicate Pred, VRPSolver *VRP) { |
Nick Lewycky | f3a9e36 | 2007-04-07 03:36:51 +0000 | [diff] [blame] | 1964 | VRP->add(V, C, Pred, VRP->TopInst); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Nick Lewycky | ac4d664 | 2007-04-07 15:48:32 +0000 | [diff] [blame] | 1967 | void ValueRanges::markBlock(VRPSolver *VRP) { |
| 1968 | VRP->UB.mark(VRP->TopBB); |
| 1969 | } |
| 1970 | |
Nick Lewycky | 1eda0f6 | 2007-03-18 01:09:32 +0000 | [diff] [blame] | 1971 | #ifndef NDEBUG |
| 1972 | bool ValueRanges::isCanonical(Value *V, ETNode *Subtree, VRPSolver *VRP) { |
| 1973 | return V == VRP->IG.canonicalize(V, Subtree); |
| 1974 | } |
| 1975 | #endif |
| 1976 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1977 | /// PredicateSimplifier - This class is a simplifier that replaces |
| 1978 | /// one equivalent variable with another. It also tracks what |
| 1979 | /// can't be equal and will solve setcc instructions when possible. |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1980 | /// @brief Root of the predicate simplifier optimization. |
| 1981 | class VISIBILITY_HIDDEN PredicateSimplifier : public FunctionPass { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 1982 | DominatorTree *DT; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1983 | ETForest *Forest; |
| 1984 | bool modified; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 1985 | InequalityGraph *IG; |
| 1986 | UnreachableBlocks UB; |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 1987 | ValueRanges *VR; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1988 | |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 1989 | std::vector<DominatorTree::Node *> WorkList; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1990 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1991 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 1992 | static char ID; // Pass identifcation, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 1993 | PredicateSimplifier() : FunctionPass((intptr_t)&ID) {} |
| 1994 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1995 | bool runOnFunction(Function &F); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 1996 | |
| 1997 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 1998 | AU.addRequiredID(BreakCriticalEdgesID); |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 1999 | AU.addRequired<DominatorTree>(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2000 | AU.addRequired<ETForest>(); |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 2001 | AU.addRequired<TargetData>(); |
| 2002 | AU.addPreserved<TargetData>(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2003 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2004 | |
| 2005 | private: |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2006 | /// Forwards - Adds new properties into PropertySet and uses them to |
| 2007 | /// simplify instructions. Because new properties sometimes apply to |
| 2008 | /// a transition from one BasicBlock to another, this will use the |
| 2009 | /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the |
| 2010 | /// basic block with the new PropertySet. |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2011 | /// @brief Performs abstract execution of the program. |
| 2012 | class VISIBILITY_HIDDEN Forwards : public InstVisitor<Forwards> { |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2013 | friend class InstVisitor<Forwards>; |
| 2014 | PredicateSimplifier *PS; |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2015 | DominatorTree::Node *DTNode; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2016 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2017 | public: |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2018 | InequalityGraph &IG; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2019 | UnreachableBlocks &UB; |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2020 | ValueRanges &VR; |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2021 | |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2022 | Forwards(PredicateSimplifier *PS, DominatorTree::Node *DTNode) |
| 2023 | : PS(PS), DTNode(DTNode), IG(*PS->IG), UB(PS->UB), VR(*PS->VR) {} |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2024 | |
| 2025 | void visitTerminatorInst(TerminatorInst &TI); |
| 2026 | void visitBranchInst(BranchInst &BI); |
| 2027 | void visitSwitchInst(SwitchInst &SI); |
| 2028 | |
Nick Lewycky | 802fe27 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 2029 | void visitAllocaInst(AllocaInst &AI); |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2030 | void visitLoadInst(LoadInst &LI); |
| 2031 | void visitStoreInst(StoreInst &SI); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2032 | |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2033 | void visitSExtInst(SExtInst &SI); |
| 2034 | void visitZExtInst(ZExtInst &ZI); |
| 2035 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2036 | void visitBinaryOperator(BinaryOperator &BO); |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2037 | void visitICmpInst(ICmpInst &IC); |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2038 | }; |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2039 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2040 | // Used by terminator instructions to proceed from the current basic |
| 2041 | // block to the next. Verifies that "current" dominates "next", |
| 2042 | // then calls visitBasicBlock. |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2043 | void proceedToSuccessors(DominatorTree::Node *Current) { |
| 2044 | for (DominatorTree::Node::iterator I = Current->begin(), |
| 2045 | E = Current->end(); I != E; ++I) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2046 | WorkList.push_back(*I); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2050 | void proceedToSuccessor(DominatorTree::Node *Next) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2051 | WorkList.push_back(Next); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2052 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2053 | |
| 2054 | // Visits each instruction in the basic block. |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2055 | void visitBasicBlock(DominatorTree::Node *Node) { |
| 2056 | BasicBlock *BB = Node->getBlock(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2057 | ETNode *ET = Forest->getNodeForBlock(BB); |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 2058 | DOUT << "Entering Basic Block: " << BB->getName() |
| 2059 | << " (" << ET->getDFSNumIn() << ")\n"; |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2060 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2061 | visitInstruction(I++, Node, ET); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2062 | } |
| 2063 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2064 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 2065 | // Tries to simplify each Instruction and add new properties to |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 2066 | // the PropertySet. |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2067 | void visitInstruction(Instruction *I, DominatorTree::Node *DT, ETNode *ET) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2068 | DOUT << "Considering instruction " << *I << "\n"; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2069 | DEBUG(IG->dump()); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2070 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2071 | // Sometimes instructions are killed in earlier analysis. |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2072 | if (isInstructionTriviallyDead(I)) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2073 | ++NumSimple; |
| 2074 | modified = true; |
| 2075 | IG->remove(I); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2076 | I->eraseFromParent(); |
| 2077 | return; |
| 2078 | } |
| 2079 | |
Nick Lewycky | 0be7f47 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 2080 | #ifndef NDEBUG |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2081 | // Try to replace the whole instruction. |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2082 | Value *V = IG->canonicalize(I, ET); |
| 2083 | assert(V == I && "Late instruction canonicalization."); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2084 | if (V != I) { |
| 2085 | modified = true; |
| 2086 | ++NumInstruction; |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2087 | DOUT << "Removing " << *I << ", replacing with " << *V << "\n"; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2088 | IG->remove(I); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2089 | I->replaceAllUsesWith(V); |
| 2090 | I->eraseFromParent(); |
| 2091 | return; |
| 2092 | } |
| 2093 | |
| 2094 | // Try to substitute operands. |
| 2095 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 2096 | Value *Oper = I->getOperand(i); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2097 | Value *V = IG->canonicalize(Oper, ET); |
| 2098 | assert(V == Oper && "Late operand canonicalization."); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2099 | if (V != Oper) { |
| 2100 | modified = true; |
| 2101 | ++NumVarsReplaced; |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2102 | DOUT << "Resolving " << *I; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2103 | I->setOperand(i, V); |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2104 | DOUT << " into " << *I; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
Nick Lewycky | 0be7f47 | 2007-01-13 02:05:28 +0000 | [diff] [blame] | 2107 | #endif |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2108 | |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2109 | std::string name = I->getParent()->getName(); |
| 2110 | DOUT << "push (%" << name << ")\n"; |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2111 | Forwards visit(this, DT); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2112 | visit.visit(*I); |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2113 | DOUT << "pop (%" << name << ")\n"; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2114 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2115 | }; |
| 2116 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2117 | bool PredicateSimplifier::runOnFunction(Function &F) { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2118 | DT = &getAnalysis<DominatorTree>(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2119 | Forest = &getAnalysis<ETForest>(); |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2120 | |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 2121 | TargetData *TD = &getAnalysis<TargetData>(); |
| 2122 | |
Chris Lattner | e34e9a2 | 2007-04-14 23:32:02 +0000 | [diff] [blame] | 2123 | // XXX: should only act when numbers are out of date |
| 2124 | Forest->updateDFSNumbers(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2125 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 2126 | DOUT << "Entering Function: " << F.getName() << "\n"; |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2127 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2128 | modified = false; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2129 | BasicBlock *RootBlock = &F.getEntryBlock(); |
| 2130 | IG = new InequalityGraph(Forest->getNodeForBlock(RootBlock)); |
Nick Lewycky | b01c77e | 2007-04-07 03:16:12 +0000 | [diff] [blame] | 2131 | VR = new ValueRanges(TD); |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2132 | WorkList.push_back(DT->getRootNode()); |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2133 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2134 | do { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2135 | DominatorTree::Node *DTNode = WorkList.back(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2136 | WorkList.pop_back(); |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2137 | if (!UB.isDead(DTNode->getBlock())) visitBasicBlock(DTNode); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2138 | } while (!WorkList.empty()); |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2139 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2140 | delete VR; |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2141 | delete IG; |
| 2142 | |
| 2143 | modified |= UB.kill(); |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 2144 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2145 | return modified; |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2148 | void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2149 | PS->proceedToSuccessors(DTNode); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) { |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2153 | if (BI.isUnconditional()) { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2154 | PS->proceedToSuccessors(DTNode); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2155 | return; |
| 2156 | } |
| 2157 | |
| 2158 | Value *Condition = BI.getCondition(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2159 | BasicBlock *TrueDest = BI.getSuccessor(0); |
| 2160 | BasicBlock *FalseDest = BI.getSuccessor(1); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2161 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2162 | if (isa<Constant>(Condition) || TrueDest == FalseDest) { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2163 | PS->proceedToSuccessors(DTNode); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2164 | return; |
| 2165 | } |
| 2166 | |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2167 | for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end(); |
| 2168 | I != E; ++I) { |
| 2169 | BasicBlock *Dest = (*I)->getBlock(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2170 | DOUT << "Branch thinking about %" << Dest->getName() |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 2171 | << "(" << PS->Forest->getNodeForBlock(Dest)->getDFSNumIn() << ")\n"; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2172 | |
| 2173 | if (Dest == TrueDest) { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2174 | DOUT << "(" << DTNode->getBlock()->getName() << ") true set:\n"; |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2175 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, Dest); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2176 | VRP.add(ConstantInt::getTrue(), Condition, ICmpInst::ICMP_EQ); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2177 | VRP.solve(); |
| 2178 | DEBUG(IG.dump()); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2179 | } else if (Dest == FalseDest) { |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2180 | DOUT << "(" << DTNode->getBlock()->getName() << ") false set:\n"; |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2181 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, Dest); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2182 | VRP.add(ConstantInt::getFalse(), Condition, ICmpInst::ICMP_EQ); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2183 | VRP.solve(); |
| 2184 | DEBUG(IG.dump()); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2187 | PS->proceedToSuccessor(*I); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2191 | void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) { |
| 2192 | Value *Condition = SI.getCondition(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2193 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2194 | // Set the EQProperty in each of the cases BBs, and the NEProperties |
| 2195 | // in the default BB. |
Owen Anderson | ab0e4d3 | 2007-04-25 04:18:54 +0000 | [diff] [blame] | 2196 | |
| 2197 | for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end(); |
| 2198 | I != E; ++I) { |
| 2199 | BasicBlock *BB = (*I)->getBlock(); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2200 | DOUT << "Switch thinking about BB %" << BB->getName() |
Nick Lewycky | dd40258 | 2007-01-15 14:30:07 +0000 | [diff] [blame] | 2201 | << "(" << PS->Forest->getNodeForBlock(BB)->getDFSNumIn() << ")\n"; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2202 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2203 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, BB); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2204 | if (BB == SI.getDefaultDest()) { |
| 2205 | for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i) |
| 2206 | if (SI.getSuccessor(i) != BB) |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2207 | VRP.add(Condition, SI.getCaseValue(i), ICmpInst::ICMP_NE); |
| 2208 | VRP.solve(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2209 | } else if (ConstantInt *CI = SI.findCaseDest(BB)) { |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2210 | VRP.add(Condition, CI, ICmpInst::ICMP_EQ); |
| 2211 | VRP.solve(); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2212 | } |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2213 | PS->proceedToSuccessor(*I); |
Nick Lewycky | a73a654 | 2006-10-03 15:19:11 +0000 | [diff] [blame] | 2214 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2215 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2216 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2217 | void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2218 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &AI); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2219 | VRP.add(Constant::getNullValue(AI.getType()), &AI, ICmpInst::ICMP_NE); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2220 | VRP.solve(); |
| 2221 | } |
Nick Lewycky | 802fe27 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 2222 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2223 | void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) { |
| 2224 | Value *Ptr = LI.getPointerOperand(); |
| 2225 | // avoid "load uint* null" -> null NE null. |
| 2226 | if (isa<Constant>(Ptr)) return; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2227 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2228 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &LI); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2229 | VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2230 | VRP.solve(); |
| 2231 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2232 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2233 | void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) { |
| 2234 | Value *Ptr = SI.getPointerOperand(); |
| 2235 | if (isa<Constant>(Ptr)) return; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2236 | |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2237 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &SI); |
Nick Lewycky | 419c6f5 | 2007-01-11 02:32:38 +0000 | [diff] [blame] | 2238 | VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE); |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2239 | VRP.solve(); |
| 2240 | } |
| 2241 | |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2242 | void PredicateSimplifier::Forwards::visitSExtInst(SExtInst &SI) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2243 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &SI); |
Reid Spencer | af3e946 | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2244 | uint32_t SrcBitWidth = cast<IntegerType>(SI.getSrcTy())->getBitWidth(); |
| 2245 | uint32_t DstBitWidth = cast<IntegerType>(SI.getDestTy())->getBitWidth(); |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2246 | APInt Min(APInt::getHighBitsSet(DstBitWidth, DstBitWidth-SrcBitWidth+1)); |
| 2247 | APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth-1)); |
Reid Spencer | af3e946 | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2248 | VRP.add(ConstantInt::get(Min), &SI, ICmpInst::ICMP_SLE); |
| 2249 | VRP.add(ConstantInt::get(Max), &SI, ICmpInst::ICMP_SGE); |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2250 | VRP.solve(); |
| 2251 | } |
| 2252 | |
| 2253 | void PredicateSimplifier::Forwards::visitZExtInst(ZExtInst &ZI) { |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2254 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &ZI); |
Reid Spencer | af3e946 | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2255 | uint32_t SrcBitWidth = cast<IntegerType>(ZI.getSrcTy())->getBitWidth(); |
| 2256 | uint32_t DstBitWidth = cast<IntegerType>(ZI.getDestTy())->getBitWidth(); |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2257 | APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth)); |
Reid Spencer | af3e946 | 2007-03-03 00:48:31 +0000 | [diff] [blame] | 2258 | VRP.add(ConstantInt::get(Max), &ZI, ICmpInst::ICMP_UGE); |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2259 | VRP.solve(); |
| 2260 | } |
| 2261 | |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2262 | void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) { |
| 2263 | Instruction::BinaryOps ops = BO.getOpcode(); |
| 2264 | |
| 2265 | switch (ops) { |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2266 | default: break; |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2267 | case Instruction::URem: |
| 2268 | case Instruction::SRem: |
| 2269 | case Instruction::UDiv: |
| 2270 | case Instruction::SDiv: { |
| 2271 | Value *Divisor = BO.getOperand(1); |
Nick Lewycky | e677a0b | 2007-03-10 18:12:48 +0000 | [diff] [blame] | 2272 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
Nick Lewycky | 4535175 | 2007-02-04 23:43:05 +0000 | [diff] [blame] | 2273 | VRP.add(Constant::getNullValue(Divisor->getType()), Divisor, |
| 2274 | ICmpInst::ICMP_NE); |
| 2275 | VRP.solve(); |
| 2276 | break; |
| 2277 | } |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | switch (ops) { |
| 2281 | default: break; |
| 2282 | case Instruction::Shl: { |
| 2283 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2284 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_UGE); |
| 2285 | VRP.solve(); |
| 2286 | } break; |
| 2287 | case Instruction::AShr: { |
| 2288 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2289 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_SLE); |
| 2290 | VRP.solve(); |
| 2291 | } break; |
| 2292 | case Instruction::LShr: |
| 2293 | case Instruction::UDiv: { |
| 2294 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2295 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_ULE); |
| 2296 | VRP.solve(); |
| 2297 | } break; |
| 2298 | case Instruction::URem: { |
| 2299 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2300 | VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_ULE); |
| 2301 | VRP.solve(); |
| 2302 | } break; |
| 2303 | case Instruction::And: { |
| 2304 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2305 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_ULE); |
| 2306 | VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_ULE); |
| 2307 | VRP.solve(); |
| 2308 | } break; |
| 2309 | case Instruction::Or: { |
| 2310 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &BO); |
| 2311 | VRP.add(&BO, BO.getOperand(0), ICmpInst::ICMP_UGE); |
| 2312 | VRP.add(&BO, BO.getOperand(1), ICmpInst::ICMP_UGE); |
| 2313 | VRP.solve(); |
| 2314 | } break; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | void PredicateSimplifier::Forwards::visitICmpInst(ICmpInst &IC) { |
| 2319 | // If possible, squeeze the ICmp predicate into something simpler. |
| 2320 | // Eg., if x = [0, 4) and we're being asked icmp uge %x, 3 then change |
| 2321 | // the predicate to eq. |
| 2322 | |
Nick Lewycky | 8ac40dd | 2007-04-07 02:30:14 +0000 | [diff] [blame] | 2323 | // XXX: once we do full PHI handling, modifying the instruction in the |
| 2324 | // Forwards visitor will cause missed optimizations. |
| 2325 | |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2326 | ICmpInst::Predicate Pred = IC.getPredicate(); |
| 2327 | |
Nick Lewycky | 8ac40dd | 2007-04-07 02:30:14 +0000 | [diff] [blame] | 2328 | switch (Pred) { |
| 2329 | default: break; |
| 2330 | case ICmpInst::ICMP_ULE: Pred = ICmpInst::ICMP_ULT; break; |
| 2331 | case ICmpInst::ICMP_UGE: Pred = ICmpInst::ICMP_UGT; break; |
| 2332 | case ICmpInst::ICMP_SLE: Pred = ICmpInst::ICMP_SLT; break; |
| 2333 | case ICmpInst::ICMP_SGE: Pred = ICmpInst::ICMP_SGT; break; |
| 2334 | } |
| 2335 | if (Pred != IC.getPredicate()) { |
| 2336 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &IC); |
| 2337 | if (VRP.isRelatedBy(IC.getOperand(1), IC.getOperand(0), |
| 2338 | ICmpInst::ICMP_NE)) { |
| 2339 | ++NumSnuggle; |
| 2340 | PS->modified = true; |
| 2341 | IC.setPredicate(Pred); |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | Pred = IC.getPredicate(); |
| 2346 | |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2347 | if (ConstantInt *Op1 = dyn_cast<ConstantInt>(IC.getOperand(1))) { |
| 2348 | ConstantInt *NextVal = 0; |
Nick Lewycky | 8ac40dd | 2007-04-07 02:30:14 +0000 | [diff] [blame] | 2349 | switch (Pred) { |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2350 | default: break; |
| 2351 | case ICmpInst::ICMP_SLT: |
| 2352 | case ICmpInst::ICMP_ULT: |
| 2353 | if (Op1->getValue() != 0) |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2354 | NextVal = ConstantInt::get(Op1->getValue()-1); |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2355 | break; |
| 2356 | case ICmpInst::ICMP_SGT: |
| 2357 | case ICmpInst::ICMP_UGT: |
| 2358 | if (!Op1->getValue().isAllOnesValue()) |
Zhou Sheng | 223d65b | 2007-04-19 05:35:00 +0000 | [diff] [blame] | 2359 | NextVal = ConstantInt::get(Op1->getValue()+1); |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2360 | break; |
| 2361 | |
| 2362 | } |
| 2363 | if (NextVal) { |
| 2364 | VRPSolver VRP(IG, UB, VR, PS->Forest, PS->modified, &IC); |
| 2365 | if (VRP.isRelatedBy(IC.getOperand(0), NextVal, |
| 2366 | ICmpInst::getInversePredicate(Pred))) { |
| 2367 | ICmpInst *NewIC = new ICmpInst(ICmpInst::ICMP_EQ, IC.getOperand(0), |
| 2368 | NextVal, "", &IC); |
| 2369 | NewIC->takeName(&IC); |
| 2370 | IC.replaceAllUsesWith(NewIC); |
| 2371 | IG.remove(&IC); // XXX: prove this isn't necessary |
| 2372 | IC.eraseFromParent(); |
| 2373 | ++NumSnuggle; |
| 2374 | PS->modified = true; |
Nick Lewycky | 4c70875 | 2007-03-16 02:37:39 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | } |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 2378 | } |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2379 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 2380 | char PredicateSimplifier::ID = 0; |
Nick Lewycky | 565706b | 2006-11-22 23:49:16 +0000 | [diff] [blame] | 2381 | RegisterPass<PredicateSimplifier> X("predsimplify", |
| 2382 | "Predicate Simplifier"); |
| 2383 | } |
| 2384 | |
| 2385 | FunctionPass *llvm::createPredicateSimplifierPass() { |
| 2386 | return new PredicateSimplifier(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 2387 | } |