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