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