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