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