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