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