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