blob: 70643f2f1ccb789881d8dd2659f2b73fb953dc0f [file] [log] [blame]
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001//===-- PredicateSimplifier.cpp - Path Sensitive Simplifier ---------------===//
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nick Lewycky and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00008//===----------------------------------------------------------------------===//
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00009//
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 Lewycky09b7e4d2006-11-22 23:49:16 +000023//===----------------------------------------------------------------------===//
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000024//
Nick Lewycky09b7e4d2006-11-22 23:49:16 +000025// This pass focusses on four properties; equals, not equals, less-than
26// and less-than-or-equals-to. The greater-than forms are also held just
27// to allow walking from a lesser node to a greater one. These properties
28// are stored in a lattice; LE can become LT or EQ, NE can become LT or GT.
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000029//
Nick Lewycky09b7e4d2006-11-22 23:49:16 +000030// These relationships define a graph between values of the same type. Each
31// Value is stored in a map table that retrieves the associated Node. This
32// is how EQ relationships are stored; the map contains pointers to the
33// same node. The node contains a most canonical Value* form and the list of
34// known relationships.
35//
36// If two nodes are known to be inequal, then they will contain pointers to
37// each other with an "NE" relationship. If node getNode(%x) is less than
38// getNode(%y), then the %x node will contain <%y, GT> and %y will contain
39// <%x, LT>. This allows us to tie nodes together into a graph like this:
40//
41// %a < %b < %c < %d
42//
43// with four nodes representing the properties. The InequalityGraph provides
Nick Lewycky2fc338f2007-01-11 02:32:38 +000044// 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 Lewycky09b7e4d2006-11-22 23:49:16 +000049//
50// To create these properties, we wait until a branch or switch instruction
51// implies that a particular value is true (or false). The VRPSolver is
52// responsible for analyzing the variable and seeing what new inferences
53// can be made from each property. For example:
54//
55// %P = seteq int* %ptr, null
56// %a = or bool %P, %Q
57// br bool %a label %cond_true, label %cond_false
58//
59// For the true branch, the VRPSolver will start with %a EQ true and look at
60// the definition of %a and find that it can infer that %P and %Q are both
61// true. From %P being true, it can infer that %ptr NE null. For the false
62// branch it can't infer anything from the "or" instruction.
63//
64// Besides branches, we can also infer properties from instruction that may
65// have undefined behaviour in certain cases. For example, the dividend of
66// a division may never be zero. After the division instruction, we may assume
67// that the dividend is not equal to zero.
68//
69//===----------------------------------------------------------------------===//
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000070
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000071#define DEBUG_TYPE "predsimplify"
72#include "llvm/Transforms/Scalar.h"
73#include "llvm/Constants.h"
Nick Lewyckyf3450082006-10-22 19:53:27 +000074#include "llvm/DerivedTypes.h"
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000075#include "llvm/Instructions.h"
76#include "llvm/Pass.h"
Nick Lewycky2fc338f2007-01-11 02:32:38 +000077#include "llvm/ADT/DepthFirstIterator.h"
Nick Lewycky09b7e4d2006-11-22 23:49:16 +000078#include "llvm/ADT/SetOperations.h"
79#include "llvm/ADT/SmallVector.h"
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000080#include "llvm/ADT/Statistic.h"
81#include "llvm/ADT/STLExtras.h"
82#include "llvm/Analysis/Dominators.h"
Nick Lewycky09b7e4d2006-11-22 23:49:16 +000083#include "llvm/Analysis/ET-Forest.h"
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000084#include "llvm/Support/CFG.h"
Chris Lattnerf06bb652006-12-06 18:14:47 +000085#include "llvm/Support/Compiler.h"
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000086#include "llvm/Support/Debug.h"
Nick Lewycky77e030b2006-10-12 02:02:44 +000087#include "llvm/Support/InstVisitor.h"
Nick Lewycky09b7e4d2006-11-22 23:49:16 +000088#include "llvm/Transforms/Utils/Local.h"
89#include <algorithm>
90#include <deque>
Nick Lewycky09b7e4d2006-11-22 23:49:16 +000091#include <sstream>
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000092using namespace llvm;
93
Chris Lattner0e5255b2006-12-19 21:49:03 +000094STATISTIC(NumVarsReplaced, "Number of argument substitutions");
95STATISTIC(NumInstruction , "Number of instructions removed");
96STATISTIC(NumSimple , "Number of simple replacements");
Nick Lewycky2fc338f2007-01-11 02:32:38 +000097STATISTIC(NumBlocks , "Number of blocks marked unreachable");
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +000098
Chris Lattner0e5255b2006-12-19 21:49:03 +000099namespace {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000100 // SLT SGT ULT UGT EQ
101 // 0 1 0 1 0 -- GT 10
102 // 0 1 0 1 1 -- GE 11
103 // 0 1 1 0 0 -- SGTULT 12
104 // 0 1 1 0 1 -- SGEULE 13
105 // 0 1 1 1 0 -- SGTUNE 14
106 // 0 1 1 1 1 -- SGEUANY 15
107 // 1 0 0 1 0 -- SLTUGT 18
108 // 1 0 0 1 1 -- SLEUGE 19
109 // 1 0 1 0 0 -- LT 20
110 // 1 0 1 0 1 -- LE 21
111 // 1 0 1 1 0 -- SLTUNE 22
112 // 1 0 1 1 1 -- SLEUANY 23
113 // 1 1 0 1 0 -- SNEUGT 26
114 // 1 1 0 1 1 -- SANYUGE 27
115 // 1 1 1 0 0 -- SNEULT 28
116 // 1 1 1 0 1 -- SANYULE 29
117 // 1 1 1 1 0 -- NE 30
118 enum LatticeBits {
119 EQ_BIT = 1, UGT_BIT = 2, ULT_BIT = 4, SGT_BIT = 8, SLT_BIT = 16
120 };
121 enum LatticeVal {
122 GT = SGT_BIT | UGT_BIT,
123 GE = GT | EQ_BIT,
124 LT = SLT_BIT | ULT_BIT,
125 LE = LT | EQ_BIT,
126 NE = SLT_BIT | SGT_BIT | ULT_BIT | UGT_BIT,
127 SGTULT = SGT_BIT | ULT_BIT,
128 SGEULE = SGTULT | EQ_BIT,
129 SLTUGT = SLT_BIT | UGT_BIT,
130 SLEUGE = SLTUGT | EQ_BIT,
131 SNEULT = SLT_BIT | SGT_BIT | ULT_BIT,
132 SNEUGT = SLT_BIT | SGT_BIT | UGT_BIT,
133 SLTUNE = SLT_BIT | ULT_BIT | UGT_BIT,
134 SGTUNE = SGT_BIT | ULT_BIT | UGT_BIT,
135 SLEUANY = SLT_BIT | ULT_BIT | UGT_BIT | EQ_BIT,
136 SGEUANY = SGT_BIT | ULT_BIT | UGT_BIT | EQ_BIT,
137 SANYULE = SLT_BIT | SGT_BIT | ULT_BIT | EQ_BIT,
138 SANYUGE = SLT_BIT | SGT_BIT | UGT_BIT | EQ_BIT
139 };
140
141 static bool validPredicate(LatticeVal LV) {
142 switch (LV) {
143 case GT: case GE: case LT: case LE: case NE:
144 case SGTULT: case SGTUNE: case SGEULE:
145 case SLTUGT: case SLTUNE: case SLEUGE:
146 case SNEULT: case SNEUGT:
147 case SLEUANY: case SGEUANY: case SANYULE: case SANYUGE:
148 return true;
149 default:
150 return false;
151 }
152 }
153
154 /// reversePredicate - reverse the direction of the inequality
155 static LatticeVal reversePredicate(LatticeVal LV) {
156 unsigned reverse = LV ^ (SLT_BIT|SGT_BIT|ULT_BIT|UGT_BIT); //preserve EQ_BIT
157 if ((reverse & (SLT_BIT|SGT_BIT)) == 0)
158 reverse |= (SLT_BIT|SGT_BIT);
159
160 if ((reverse & (ULT_BIT|UGT_BIT)) == 0)
161 reverse |= (ULT_BIT|UGT_BIT);
162
163 LatticeVal Rev = static_cast<LatticeVal>(reverse);
164 assert(validPredicate(Rev) && "Failed reversing predicate.");
165 return Rev;
166 }
167
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000168 /// The InequalityGraph stores the relationships between values.
169 /// Each Value in the graph is assigned to a Node. Nodes are pointer
170 /// comparable for equality. The caller is expected to maintain the logical
171 /// consistency of the system.
172 ///
173 /// The InequalityGraph class may invalidate Node*s after any mutator call.
174 /// @brief The InequalityGraph stores the relationships between values.
175 class VISIBILITY_HIDDEN InequalityGraph {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000176 ETNode *TreeRoot;
177
178 InequalityGraph(); // DO NOT IMPLEMENT
179 InequalityGraph(InequalityGraph &); // DO NOT IMPLEMENT
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000180 public:
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000181 explicit InequalityGraph(ETNode *TreeRoot) : TreeRoot(TreeRoot) {}
182
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000183 class Node;
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +0000184
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000185 /// This is a StrictWeakOrdering predicate that sorts ETNodes by how many
186 /// children they have. With this, you can iterate through a list sorted by
187 /// this operation and the first matching entry is the most specific match
188 /// for your basic block. The order provided is total; ETNodes with the
189 /// same number of children are sorted by pointer address.
190 struct VISIBILITY_HIDDEN OrderByDominance {
191 bool operator()(const ETNode *LHS, const ETNode *RHS) const {
192 unsigned LHS_spread = LHS->getDFSNumOut() - LHS->getDFSNumIn();
193 unsigned RHS_spread = RHS->getDFSNumOut() - RHS->getDFSNumIn();
194 if (LHS_spread != RHS_spread) return LHS_spread < RHS_spread;
195 else return LHS < RHS;
196 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000197 };
198
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000199 /// An Edge is contained inside a Node making one end of the edge implicit
200 /// and contains a pointer to the other end. The edge contains a lattice
201 /// value specifying the relationship between the two nodes. Further, there
202 /// is an ETNode specifying which subtree of the dominator the edge applies.
203 class VISIBILITY_HIDDEN Edge {
204 public:
205 Edge(unsigned T, LatticeVal V, ETNode *ST)
206 : To(T), LV(V), Subtree(ST) {}
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000207
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000208 unsigned To;
209 LatticeVal LV;
210 ETNode *Subtree;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000211
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000212 bool operator<(const Edge &edge) const {
213 if (To != edge.To) return To < edge.To;
214 else return OrderByDominance()(Subtree, edge.Subtree);
215 }
216 bool operator<(unsigned to) const {
217 return To < to;
218 }
219 };
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000220
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000221 /// A single node in the InequalityGraph. This stores the canonical Value
222 /// for the node, as well as the relationships with the neighbours.
223 ///
224 /// Because the lists are intended to be used for traversal, it is invalid
225 /// for the node to list itself in LessEqual or GreaterEqual lists. The
226 /// fact that a node is equal to itself is implied, and may be checked
227 /// with pointer comparison.
228 /// @brief A single node in the InequalityGraph.
229 class VISIBILITY_HIDDEN Node {
230 friend class InequalityGraph;
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000231
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000232 typedef SmallVector<Edge, 4> RelationsType;
233 RelationsType Relations;
234
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000235 Value *Canonical;
Nick Lewyckycfff1c32006-09-20 17:04:01 +0000236
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000237 // TODO: can this idea improve performance?
238 //friend class std::vector<Node>;
239 //Node(Node &N) { RelationsType.swap(N.RelationsType); }
240
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000241 public:
242 typedef RelationsType::iterator iterator;
243 typedef RelationsType::const_iterator const_iterator;
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000244
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000245 Node(Value *V) : Canonical(V) {}
246
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000247 private:
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000248#ifndef NDEBUG
249 public:
Nick Lewycky5d6ede52007-01-11 02:38:21 +0000250 virtual ~Node() {}
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000251 virtual void dump() const {
252 dump(*cerr.stream());
253 }
254 private:
255 void dump(std::ostream &os) const {
256 os << *getValue() << ":\n";
257 for (Node::const_iterator NI = begin(), NE = end(); NI != NE; ++NI) {
258 static const std::string names[32] =
259 { "000000", "000001", "000002", "000003", "000004", "000005",
260 "000006", "000007", "000008", "000009", " >", " >=",
261 " s>u<", "s>=u<=", " s>", " s>=", "000016", "000017",
262 " s<u>", "s<=u>=", " <", " <=", " s<", " s<=",
263 "000024", "000025", " u>", " u>=", " u<", " u<=",
264 " !=", "000031" };
265 os << " " << names[NI->LV] << " " << NI->To
Nick Lewycky6ce36cf2007-01-15 14:30:07 +0000266 << " (" << NI->Subtree->getDFSNumIn() << ")\n";
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000267 }
268 }
269#endif
270
271 public:
272 iterator begin() { return Relations.begin(); }
273 iterator end() { return Relations.end(); }
274 const_iterator begin() const { return Relations.begin(); }
275 const_iterator end() const { return Relations.end(); }
276
277 iterator find(unsigned n, ETNode *Subtree) {
278 iterator E = end();
279 for (iterator I = std::lower_bound(begin(), E, n);
280 I != E && I->To == n; ++I) {
281 if (Subtree->DominatedBy(I->Subtree))
282 return I;
283 }
284 return E;
285 }
286
287 const_iterator find(unsigned n, ETNode *Subtree) const {
288 const_iterator E = end();
289 for (const_iterator I = std::lower_bound(begin(), E, n);
290 I != E && I->To == n; ++I) {
291 if (Subtree->DominatedBy(I->Subtree))
292 return I;
293 }
294 return E;
295 }
296
297 Value *getValue() const
298 {
299 return Canonical;
300 }
301
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000302 /// Updates the lattice value for a given node. Create a new entry if
303 /// one doesn't exist, otherwise it merges the values. The new lattice
304 /// value must not be inconsistent with any previously existing value.
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000305 void update(unsigned n, LatticeVal R, ETNode *Subtree) {
306 assert(validPredicate(R) && "Invalid predicate.");
307 iterator I = find(n, Subtree);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000308 if (I == end()) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000309 Edge edge(n, R, Subtree);
310 iterator Insert = std::lower_bound(begin(), end(), edge);
311 Relations.insert(Insert, edge);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000312 } else {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000313 LatticeVal LV = static_cast<LatticeVal>(I->LV & R);
314 assert(validPredicate(LV) && "Invalid union of lattice values.");
315 if (LV != I->LV) {
Nick Lewycky6ce36cf2007-01-15 14:30:07 +0000316 if (Subtree != I->Subtree) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000317 assert(Subtree->DominatedBy(I->Subtree) &&
318 "Find returned subtree that doesn't apply.");
319
320 Edge edge(n, R, Subtree);
321 iterator Insert = std::lower_bound(begin(), end(), edge);
Nick Lewycky6ce36cf2007-01-15 14:30:07 +0000322 Relations.insert(Insert, edge); // invalidates I
323 I = find(n, Subtree);
324 }
325
326 // Also, we have to tighten any edge that Subtree dominates.
327 for (iterator B = begin(); I->To == n; --I) {
328 if (I->Subtree->DominatedBy(Subtree)) {
329 LatticeVal LV = static_cast<LatticeVal>(I->LV & R);
330 assert(validPredicate(LV) && "Invalid union of lattice values.");
331 I->LV = LV;
332 }
333 if (I == B) break;
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000334 }
335 }
Nick Lewycky51ce8d62006-09-13 19:24:01 +0000336 }
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000337 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000338 };
339
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000340 private:
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000341 struct VISIBILITY_HIDDEN NodeMapEdge {
342 Value *V;
343 unsigned index;
344 ETNode *Subtree;
345
346 NodeMapEdge(Value *V, unsigned index, ETNode *Subtree)
347 : V(V), index(index), Subtree(Subtree) {}
348
349 bool operator==(const NodeMapEdge &RHS) const {
350 return V == RHS.V &&
351 Subtree == RHS.Subtree;
352 }
353
354 bool operator<(const NodeMapEdge &RHS) const {
355 if (V != RHS.V) return V < RHS.V;
356 return OrderByDominance()(Subtree, RHS.Subtree);
357 }
358
359 bool operator<(Value *RHS) const {
360 return V < RHS;
361 }
362 };
363
364 typedef std::vector<NodeMapEdge> NodeMapType;
365 NodeMapType NodeMap;
366
367 std::vector<Node> Nodes;
368
Zhou Sheng75b871f2007-01-11 12:24:14 +0000369 std::vector<std::pair<ConstantInt *, unsigned> > Constants;
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000370 void initializeConstant(Constant *C, unsigned index) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000371 ConstantInt *CI = dyn_cast<ConstantInt>(C);
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000372 if (!CI) return;
373
374 // XXX: instead of O(n) calls to addInequality, just find the 2, 3 or 4
375 // nodes that are nearest less than or greater than (signed or unsigned).
Zhou Sheng75b871f2007-01-11 12:24:14 +0000376 for (std::vector<std::pair<ConstantInt *, unsigned> >::iterator
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000377 I = Constants.begin(), E = Constants.end(); I != E; ++I) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000378 ConstantInt *Other = I->first;
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000379 if (CI->getType() == Other->getType()) {
380 unsigned lv = 0;
381
382 if (CI->getZExtValue() < Other->getZExtValue())
383 lv |= ULT_BIT;
384 else
385 lv |= UGT_BIT;
386
387 if (CI->getSExtValue() < Other->getSExtValue())
388 lv |= SLT_BIT;
389 else
390 lv |= SGT_BIT;
391
392 LatticeVal LV = static_cast<LatticeVal>(lv);
393 assert(validPredicate(LV) && "Not a valid predicate.");
394 if (!isRelatedBy(index, I->second, TreeRoot, LV))
395 addInequality(index, I->second, TreeRoot, LV);
396 }
397 }
398 Constants.push_back(std::make_pair(CI, index));
399 }
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000400
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000401 public:
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000402 /// node - returns the node object at a given index retrieved from getNode.
403 /// Index zero is reserved and may not be passed in here. The pointer
404 /// returned is valid until the next call to newNode or getOrInsertNode.
405 Node *node(unsigned index) {
406 assert(index != 0 && "Zero index is reserved for not found.");
407 assert(index <= Nodes.size() && "Index out of range.");
408 return &Nodes[index-1];
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000409 }
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000410
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000411 /// Returns the node currently representing Value V, or zero if no such
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000412 /// node exists.
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000413 unsigned getNode(Value *V, ETNode *Subtree) {
414 NodeMapType::iterator E = NodeMap.end();
415 NodeMapEdge Edge(V, 0, Subtree);
416 NodeMapType::iterator I = std::lower_bound(NodeMap.begin(), E, Edge);
417 while (I != E && I->V == V) {
418 if (Subtree->DominatedBy(I->Subtree))
419 return I->index;
420 ++I;
421 }
422 return 0;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000423 }
424
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000425 /// getOrInsertNode - always returns a valid node index, creating a node
426 /// to match the Value if needed.
427 unsigned getOrInsertNode(Value *V, ETNode *Subtree) {
428 if (unsigned n = getNode(V, Subtree))
429 return n;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000430 else
431 return newNode(V);
432 }
433
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000434 /// newNode - creates a new node for a given Value and returns the index.
435 unsigned newNode(Value *V) {
436 Nodes.push_back(Node(V));
437
438 NodeMapEdge MapEntry = NodeMapEdge(V, Nodes.size(), TreeRoot);
439 assert(!std::binary_search(NodeMap.begin(), NodeMap.end(), MapEntry) &&
440 "Attempt to create a duplicate Node.");
441 NodeMap.insert(std::lower_bound(NodeMap.begin(), NodeMap.end(),
442 MapEntry), MapEntry);
443
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000444 if (Constant *C = dyn_cast<Constant>(V))
445 initializeConstant(C, MapEntry.index);
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000446
447 return MapEntry.index;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000448 }
449
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000450 /// If the Value is in the graph, return the canonical form. Otherwise,
451 /// return the original Value.
452 Value *canonicalize(Value *V, ETNode *Subtree) {
453 if (isa<Constant>(V)) return V;
454
455 if (unsigned n = getNode(V, Subtree))
456 return node(n)->getValue();
457 else
458 return V;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000459 }
460
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000461 /// isRelatedBy - true iff n1 op n2
462 bool isRelatedBy(unsigned n1, unsigned n2, ETNode *Subtree, LatticeVal LV) {
463 if (n1 == n2) return LV & EQ_BIT;
464
465 Node *N1 = node(n1);
466 Node::iterator I = N1->find(n2, Subtree), E = N1->end();
467 if (I != E) return (I->LV & LV) == I->LV;
468
Nick Lewyckycfff1c32006-09-20 17:04:01 +0000469 return false;
470 }
471
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000472 // The add* methods assume that your input is logically valid and may
473 // assertion-fail or infinitely loop if you attempt a contradiction.
Nick Lewycky9d17c822006-10-25 23:48:24 +0000474
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000475 void addEquality(unsigned n, Value *V, ETNode *Subtree) {
476 assert(canonicalize(node(n)->getValue(), Subtree) == node(n)->getValue()
477 && "Node's 'canonical' choice isn't best within this subtree.");
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000478
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000479 // Suppose that we are given "%x -> node #1 (%y)". The problem is that
480 // we may already have "%z -> node #2 (%x)" somewhere above us in the
481 // graph. We need to find those edges and add "%z -> node #1 (%y)"
482 // to keep the lookups canonical.
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000483
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000484 std::vector<Value *> ToRepoint;
485 ToRepoint.push_back(V);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000486
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000487 if (unsigned Conflict = getNode(V, Subtree)) {
488 // XXX: NodeMap.size() exceeds 68000 entries compiling kimwitu++!
489 // This adds 57 seconds to the otherwise 3 second build. Unacceptable.
490 //
491 // IDEA: could we iterate 1..Nodes.size() calling getNode? It's
492 // O(n log n) but kimwitu++ only has about 300 nodes.
493 for (NodeMapType::iterator I = NodeMap.begin(), E = NodeMap.end();
494 I != E; ++I) {
495 if (I->index == Conflict && Subtree->DominatedBy(I->Subtree))
496 ToRepoint.push_back(I->V);
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000497 }
498 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000499
500 for (std::vector<Value *>::iterator VI = ToRepoint.begin(),
501 VE = ToRepoint.end(); VI != VE; ++VI) {
502 Value *V = *VI;
503
504 // XXX: review this code. This may be doing too many insertions.
505 NodeMapEdge Edge(V, n, Subtree);
506 NodeMapType::iterator E = NodeMap.end();
507 NodeMapType::iterator I = std::lower_bound(NodeMap.begin(), E, Edge);
508 if (I == E || I->V != V || I->Subtree != Subtree) {
509 // New Value
510 NodeMap.insert(I, Edge);
511 } else if (I != E && I->V == V && I->Subtree == Subtree) {
512 // Update best choice
513 I->index = n;
514 }
515
516#ifndef NDEBUG
517 Node *N = node(n);
518 if (isa<Constant>(V)) {
519 if (isa<Constant>(N->getValue())) {
520 assert(V == N->getValue() && "Constant equals different constant?");
521 }
522 }
523#endif
524 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +0000525 }
526
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000527 /// addInequality - Sets n1 op n2.
528 /// It is also an error to call this on an inequality that is already true.
529 void addInequality(unsigned n1, unsigned n2, ETNode *Subtree,
530 LatticeVal LV1) {
531 assert(n1 != n2 && "A node can't be inequal to itself.");
532
533 if (LV1 != NE)
534 assert(!isRelatedBy(n1, n2, Subtree, reversePredicate(LV1)) &&
535 "Contradictory inequality.");
536
537 Node *N1 = node(n1);
538 Node *N2 = node(n2);
539
540 // Suppose we're adding %n1 < %n2. Find all the %a < %n1 and
541 // add %a < %n2 too. This keeps the graph fully connected.
542 if (LV1 != NE) {
543 // Someone with a head for this sort of logic, please review this.
544 // Given that %x SLTUGT %y and %a SLEUANY %x, what is the relationship
545 // between %a and %y? I believe the below code is correct, but I don't
546 // think it's the most efficient solution.
547
548 unsigned LV1_s = LV1 & (SLT_BIT|SGT_BIT);
549 unsigned LV1_u = LV1 & (ULT_BIT|UGT_BIT);
550 for (Node::iterator I = N1->begin(), E = N1->end(); I != E; ++I) {
551 if (I->LV != NE && I->To != n2) {
552 ETNode *Local_Subtree = NULL;
553 if (Subtree->DominatedBy(I->Subtree))
554 Local_Subtree = Subtree;
555 else if (I->Subtree->DominatedBy(Subtree))
556 Local_Subtree = I->Subtree;
557
558 if (Local_Subtree) {
559 unsigned new_relationship = 0;
560 LatticeVal ILV = reversePredicate(I->LV);
561 unsigned ILV_s = ILV & (SLT_BIT|SGT_BIT);
562 unsigned ILV_u = ILV & (ULT_BIT|UGT_BIT);
563
564 if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s)
565 new_relationship |= ILV_s;
566
567 if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u)
568 new_relationship |= ILV_u;
569
570 if (new_relationship) {
571 if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0)
572 new_relationship |= (SLT_BIT|SGT_BIT);
573 if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0)
574 new_relationship |= (ULT_BIT|UGT_BIT);
575 if ((LV1 & EQ_BIT) && (ILV & EQ_BIT))
576 new_relationship |= EQ_BIT;
577
578 LatticeVal NewLV = static_cast<LatticeVal>(new_relationship);
579
580 node(I->To)->update(n2, NewLV, Local_Subtree);
581 N2->update(I->To, reversePredicate(NewLV), Local_Subtree);
582 }
583 }
584 }
585 }
586
587 for (Node::iterator I = N2->begin(), E = N2->end(); I != E; ++I) {
588 if (I->LV != NE && I->To != n1) {
589 ETNode *Local_Subtree = NULL;
590 if (Subtree->DominatedBy(I->Subtree))
591 Local_Subtree = Subtree;
592 else if (I->Subtree->DominatedBy(Subtree))
593 Local_Subtree = I->Subtree;
594
595 if (Local_Subtree) {
596 unsigned new_relationship = 0;
597 unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT);
598 unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT);
599
600 if (LV1_s != (SLT_BIT|SGT_BIT) && ILV_s == LV1_s)
601 new_relationship |= ILV_s;
602
603 if (LV1_u != (ULT_BIT|UGT_BIT) && ILV_u == LV1_u)
604 new_relationship |= ILV_u;
605
606 if (new_relationship) {
607 if ((new_relationship & (SLT_BIT|SGT_BIT)) == 0)
608 new_relationship |= (SLT_BIT|SGT_BIT);
609 if ((new_relationship & (ULT_BIT|UGT_BIT)) == 0)
610 new_relationship |= (ULT_BIT|UGT_BIT);
611 if ((LV1 & EQ_BIT) && (I->LV & EQ_BIT))
612 new_relationship |= EQ_BIT;
613
614 LatticeVal NewLV = static_cast<LatticeVal>(new_relationship);
615
616 N1->update(I->To, NewLV, Local_Subtree);
617 node(I->To)->update(n1, reversePredicate(NewLV), Local_Subtree);
618 }
619 }
620 }
621 }
622 }
623
624 N1->update(n2, LV1, Subtree);
625 N2->update(n1, reversePredicate(LV1), Subtree);
626 }
Nick Lewycky51ce8d62006-09-13 19:24:01 +0000627
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000628 /// Removes a Value from the graph, but does not delete any nodes. As this
629 /// method does not delete Nodes, V may not be the canonical choice for
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000630 /// a node with any relationships. It is invalid to call newNode on a Value
631 /// that has been removed.
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000632 void remove(Value *V) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000633 for (unsigned i = 0; i < NodeMap.size();) {
634 NodeMapType::iterator I = NodeMap.begin()+i;
635 assert((node(I->index)->getValue() != V || node(I->index)->begin() ==
636 node(I->index)->end()) && "Tried to delete in-use node.");
637 if (I->V == V) {
638#ifndef NDEBUG
639 if (node(I->index)->getValue() == V)
640 node(I->index)->Canonical = NULL;
641#endif
642 NodeMap.erase(I);
643 } else ++i;
Nick Lewycky9a22d7b2006-09-10 02:27:07 +0000644 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000645 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +0000646
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000647#ifndef NDEBUG
Nick Lewycky5d6ede52007-01-11 02:38:21 +0000648 virtual ~InequalityGraph() {}
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000649 virtual void dump() {
650 dump(*cerr.stream());
651 }
652
653 void dump(std::ostream &os) {
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000654 std::set<Node *> VisitedNodes;
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000655 for (NodeMapType::const_iterator I = NodeMap.begin(), E = NodeMap.end();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000656 I != E; ++I) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000657 Node *N = node(I->index);
Nick Lewycky6ce36cf2007-01-15 14:30:07 +0000658 os << *I->V << " == " << I->index
659 << "(" << I->Subtree->getDFSNumIn() << ")\n";
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000660 if (VisitedNodes.insert(N).second) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000661 os << I->index << ". ";
662 if (!N->getValue()) os << "(deleted node)\n";
663 else N->dump(os);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000664 }
665 }
666 }
667#endif
668 };
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +0000669
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000670 /// UnreachableBlocks keeps tracks of blocks that are for one reason or
671 /// another discovered to be unreachable. This is used to cull the graph when
672 /// analyzing instructions, and to mark blocks with the "unreachable"
673 /// terminator instruction after the function has executed.
674 class VISIBILITY_HIDDEN UnreachableBlocks {
675 private:
676 std::vector<BasicBlock *> DeadBlocks;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000677
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000678 public:
679 /// mark - mark a block as dead
680 void mark(BasicBlock *BB) {
681 std::vector<BasicBlock *>::iterator E = DeadBlocks.end();
682 std::vector<BasicBlock *>::iterator I =
683 std::lower_bound(DeadBlocks.begin(), E, BB);
684
685 if (I == E || *I != BB) DeadBlocks.insert(I, BB);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000686 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000687
688 /// isDead - returns whether a block is known to be dead already
689 bool isDead(BasicBlock *BB) {
690 std::vector<BasicBlock *>::iterator E = DeadBlocks.end();
691 std::vector<BasicBlock *>::iterator I =
692 std::lower_bound(DeadBlocks.begin(), E, BB);
693
694 return I != E && *I == BB;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000695 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000696
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000697 /// kill - replace the dead blocks' terminator with an UnreachableInst.
698 bool kill() {
699 bool modified = false;
700 for (std::vector<BasicBlock *>::iterator I = DeadBlocks.begin(),
701 E = DeadBlocks.end(); I != E; ++I) {
702 BasicBlock *BB = *I;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000703
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000704 DOUT << "unreachable block: " << BB->getName() << "\n";
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000705
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000706 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
707 SI != SE; ++SI) {
708 BasicBlock *Succ = *SI;
709 Succ->removePredecessor(BB);
Nick Lewycky9d17c822006-10-25 23:48:24 +0000710 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000711
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000712 TerminatorInst *TI = BB->getTerminator();
713 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
714 TI->eraseFromParent();
715 new UnreachableInst(BB);
716 ++NumBlocks;
717 modified = true;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000718 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000719 DeadBlocks.clear();
720 return modified;
Nick Lewycky9d17c822006-10-25 23:48:24 +0000721 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000722 };
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000723
724 /// VRPSolver keeps track of how changes to one variable affect other
725 /// variables, and forwards changes along to the InequalityGraph. It
726 /// also maintains the correct choice for "canonical" in the IG.
727 /// @brief VRPSolver calculates inferences from a new relationship.
728 class VISIBILITY_HIDDEN VRPSolver {
729 private:
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000730 struct Operation {
731 Value *LHS, *RHS;
732 ICmpInst::Predicate Op;
733
Nick Lewycky42944462007-01-13 02:05:28 +0000734 BasicBlock *ContextBB;
735 Instruction *ContextInst;
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000736 };
737 std::deque<Operation> WorkList;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000738
739 InequalityGraph &IG;
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000740 UnreachableBlocks &UB;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000741 ETForest *Forest;
742 ETNode *Top;
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000743 BasicBlock *TopBB;
744 Instruction *TopInst;
745 bool &modified;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000746
747 typedef InequalityGraph::Node Node;
748
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000749 /// IdomI - Determines whether one Instruction dominates another.
750 bool IdomI(Instruction *I1, Instruction *I2) const {
751 BasicBlock *BB1 = I1->getParent(),
752 *BB2 = I2->getParent();
753 if (BB1 == BB2) {
754 if (isa<TerminatorInst>(I1)) return false;
755 if (isa<TerminatorInst>(I2)) return true;
756 if (isa<PHINode>(I1) && !isa<PHINode>(I2)) return true;
757 if (!isa<PHINode>(I1) && isa<PHINode>(I2)) return false;
758
759 for (BasicBlock::const_iterator I = BB1->begin(), E = BB1->end();
760 I != E; ++I) {
761 if (&*I == I1) return true;
762 if (&*I == I2) return false;
763 }
764 assert(!"Instructions not found in parent BasicBlock?");
765 } else {
766 return Forest->properlyDominates(BB1, BB2);
767 }
768 return false;
769 }
770
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000771 /// Returns true if V1 is a better canonical value than V2.
772 bool compare(Value *V1, Value *V2) const {
773 if (isa<Constant>(V1))
774 return !isa<Constant>(V2);
775 else if (isa<Constant>(V2))
776 return false;
777 else if (isa<Argument>(V1))
778 return !isa<Argument>(V2);
779 else if (isa<Argument>(V2))
780 return false;
781
782 Instruction *I1 = dyn_cast<Instruction>(V1);
783 Instruction *I2 = dyn_cast<Instruction>(V2);
784
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000785 if (!I1 || !I2)
786 return V1->getNumUses() < V2->getNumUses();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000787
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000788 return IdomI(I1, I2);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000789 }
790
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000791 // below - true if the Instruction is dominated by the current context
792 // block or instruction
793 bool below(Instruction *I) {
794 if (TopInst)
795 return IdomI(TopInst, I);
796 else {
797 ETNode *Node = Forest->getNodeForBlock(I->getParent());
798 return Node == Top || Node->DominatedBy(Top);
799 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000800 }
801
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000802 bool makeEqual(Value *V1, Value *V2) {
803 DOUT << "makeEqual(" << *V1 << ", " << *V2 << ")\n";
Nick Lewycky9d17c822006-10-25 23:48:24 +0000804
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000805 if (V1 == V2) return true;
Nick Lewycky9d17c822006-10-25 23:48:24 +0000806
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000807 if (isa<Constant>(V1) && isa<Constant>(V2))
808 return false;
809
810 unsigned n1 = IG.getNode(V1, Top), n2 = IG.getNode(V2, Top);
811
812 if (n1 && n2) {
813 if (n1 == n2) return true;
814 if (IG.isRelatedBy(n1, n2, Top, NE)) return false;
815 }
816
817 if (n1) assert(V1 == IG.node(n1)->getValue() && "Value isn't canonical.");
818 if (n2) assert(V2 == IG.node(n2)->getValue() && "Value isn't canonical.");
819
820 if (compare(V2, V1)) { std::swap(V1, V2); std::swap(n1, n2); }
821
822 assert(!isa<Constant>(V2) && "Tried to remove a constant.");
823
824 SetVector<unsigned> Remove;
825 if (n2) Remove.insert(n2);
826
827 if (n1 && n2) {
828 // Suppose we're being told that %x == %y, and %x <= %z and %y >= %z.
829 // We can't just merge %x and %y because the relationship with %z would
830 // be EQ and that's invalid. What we're doing is looking for any nodes
831 // %z such that %x <= %z and %y >= %z, and vice versa.
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000832
833 Node *N1 = IG.node(n1);
Nick Lewycky6ce36cf2007-01-15 14:30:07 +0000834 Node *N2 = IG.node(n2);
835 Node::iterator end = N2->end();
836
837 // Find the intersection between N1 and N2 which is dominated by
838 // Top. If we find %x where N1 <= %x <= N2 (or >=) then add %x to
839 // Remove.
840 for (Node::iterator I = N1->begin(), E = N1->end(); I != E; ++I) {
841 if (!(I->LV & EQ_BIT) || !Top->DominatedBy(I->Subtree)) continue;
842
843 unsigned ILV_s = I->LV & (SLT_BIT|SGT_BIT);
844 unsigned ILV_u = I->LV & (ULT_BIT|UGT_BIT);
845 Node::iterator NI = N2->find(I->To, Top);
846 if (NI != end) {
847 LatticeVal NILV = reversePredicate(NI->LV);
848 unsigned NILV_s = NILV & (SLT_BIT|SGT_BIT);
849 unsigned NILV_u = NILV & (ULT_BIT|UGT_BIT);
850
851 if ((ILV_s != (SLT_BIT|SGT_BIT) && ILV_s == NILV_s) ||
852 (ILV_u != (ULT_BIT|UGT_BIT) && ILV_u == NILV_u))
853 Remove.insert(I->To);
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000854 }
855 }
856
857 // See if one of the nodes about to be removed is actually a better
858 // canonical choice than n1.
859 unsigned orig_n1 = n1;
860 std::vector<unsigned>::iterator DontRemove = Remove.end();
861 for (std::vector<unsigned>::iterator I = Remove.begin()+1 /* skip n2 */,
862 E = Remove.end(); I != E; ++I) {
863 unsigned n = *I;
864 Value *V = IG.node(n)->getValue();
865 if (compare(V, V1)) {
866 V1 = V;
867 n1 = n;
868 DontRemove = I;
869 }
870 }
871 if (DontRemove != Remove.end()) {
872 unsigned n = *DontRemove;
873 Remove.remove(n);
874 Remove.insert(orig_n1);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000875 }
876 }
Nick Lewycky9d17c822006-10-25 23:48:24 +0000877
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000878 // We'd like to allow makeEqual on two values to perform a simple
879 // substitution without every creating nodes in the IG whenever possible.
880 //
881 // The first iteration through this loop operates on V2 before going
882 // through the Remove list and operating on those too. If all of the
883 // iterations performed simple replacements then we exit early.
884 bool exitEarly = true;
885 unsigned i = 0;
886 for (Value *R = V2; i == 0 || i < Remove.size(); ++i) {
887 if (i) R = IG.node(Remove[i])->getValue(); // skip n2.
888
889 // Try to replace the whole instruction. If we can, we're done.
890 Instruction *I2 = dyn_cast<Instruction>(R);
891 if (I2 && below(I2)) {
892 std::vector<Instruction *> ToNotify;
893 for (Value::use_iterator UI = R->use_begin(), UE = R->use_end();
894 UI != UE;) {
895 Use &TheUse = UI.getUse();
896 ++UI;
897 if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser()))
898 ToNotify.push_back(I);
899 }
900
901 DOUT << "Simply removing " << *I2
902 << ", replacing with " << *V1 << "\n";
903 I2->replaceAllUsesWith(V1);
904 // leave it dead; it'll get erased later.
905 ++NumInstruction;
906 modified = true;
907
908 for (std::vector<Instruction *>::iterator II = ToNotify.begin(),
909 IE = ToNotify.end(); II != IE; ++II) {
910 opsToDef(*II);
911 }
912
913 continue;
914 }
915
916 // Otherwise, replace all dominated uses.
917 for (Value::use_iterator UI = R->use_begin(), UE = R->use_end();
918 UI != UE;) {
919 Use &TheUse = UI.getUse();
920 ++UI;
921 if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
922 if (below(I)) {
923 TheUse.set(V1);
924 modified = true;
925 ++NumVarsReplaced;
926 opsToDef(I);
927 }
928 }
929 }
930
931 // If that killed the instruction, stop here.
932 if (I2 && isInstructionTriviallyDead(I2)) {
933 DOUT << "Killed all uses of " << *I2
934 << ", replacing with " << *V1 << "\n";
935 continue;
936 }
937
938 // If we make it to here, then we will need to create a node for N1.
939 // Otherwise, we can skip out early!
940 exitEarly = false;
941 }
942
943 if (exitEarly) return true;
944
945 // Create N1.
946 // XXX: this should call newNode, but instead the node might be created
947 // in isRelatedBy. That's also a fixme.
948 if (!n1) n1 = IG.getOrInsertNode(V1, Top);
949
950 // Migrate relationships from removed nodes to N1.
951 Node *N1 = IG.node(n1);
952 for (std::vector<unsigned>::iterator I = Remove.begin(), E = Remove.end();
953 I != E; ++I) {
954 unsigned n = *I;
955 Node *N = IG.node(n);
956 for (Node::iterator NI = N->begin(), NE = N->end(); NI != NE; ++NI) {
957 if (Top == NI->Subtree || NI->Subtree->DominatedBy(Top)) {
958 if (NI->To == n1) {
959 assert((NI->LV & EQ_BIT) && "Node inequal to itself.");
960 continue;
961 }
962 if (Remove.count(NI->To))
963 continue;
964
965 IG.node(NI->To)->update(n1, reversePredicate(NI->LV), Top);
966 N1->update(NI->To, NI->LV, Top);
967 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +0000968 }
969 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000970
971 // Point V2 (and all items in Remove) to N1.
972 if (!n2)
973 IG.addEquality(n1, V2, Top);
974 else {
975 for (std::vector<unsigned>::iterator I = Remove.begin(),
976 E = Remove.end(); I != E; ++I) {
977 IG.addEquality(n1, IG.node(*I)->getValue(), Top);
978 }
979 }
980
981 // If !Remove.empty() then V2 = Remove[0]->getValue().
982 // Even when Remove is empty, we still want to process V2.
983 i = 0;
984 for (Value *R = V2; i == 0 || i < Remove.size(); ++i) {
985 if (i) R = IG.node(Remove[i])->getValue(); // skip n2.
986
Nick Lewycky6ce36cf2007-01-15 14:30:07 +0000987 if (Instruction *I2 = dyn_cast<Instruction>(R)) {
988 if (below(I2) ||
989 Top->DominatedBy(Forest->getNodeForBlock(I2->getParent())))
990 defToOps(I2);
991 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +0000992 for (Value::use_iterator UI = V2->use_begin(), UE = V2->use_end();
993 UI != UE;) {
994 Use &TheUse = UI.getUse();
995 ++UI;
996 if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
Nick Lewycky6ce36cf2007-01-15 14:30:07 +0000997 if (below(I) ||
998 Top->DominatedBy(Forest->getNodeForBlock(I->getParent())))
999 opsToDef(I);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001000 }
1001 }
1002 }
1003
1004 return true;
1005 }
1006
1007 /// cmpInstToLattice - converts an CmpInst::Predicate to lattice value
1008 /// Requires that the lattice value be valid; does not accept ICMP_EQ.
1009 static LatticeVal cmpInstToLattice(ICmpInst::Predicate Pred) {
1010 switch (Pred) {
1011 case ICmpInst::ICMP_EQ:
1012 assert(!"No matching lattice value.");
1013 return static_cast<LatticeVal>(EQ_BIT);
1014 default:
1015 assert(!"Invalid 'icmp' predicate.");
1016 case ICmpInst::ICMP_NE:
1017 return NE;
1018 case ICmpInst::ICMP_UGT:
1019 return SNEUGT;
1020 case ICmpInst::ICMP_UGE:
1021 return SANYUGE;
1022 case ICmpInst::ICMP_ULT:
1023 return SNEULT;
1024 case ICmpInst::ICMP_ULE:
1025 return SANYULE;
1026 case ICmpInst::ICMP_SGT:
1027 return SGTUNE;
1028 case ICmpInst::ICMP_SGE:
1029 return SGEUANY;
1030 case ICmpInst::ICMP_SLT:
1031 return SLTUNE;
1032 case ICmpInst::ICMP_SLE:
1033 return SLEUANY;
1034 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001035 }
Nick Lewycky9d17c822006-10-25 23:48:24 +00001036
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001037 public:
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001038 VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ETForest *Forest,
1039 bool &modified, BasicBlock *TopBB)
1040 : IG(IG),
1041 UB(UB),
1042 Forest(Forest),
1043 Top(Forest->getNodeForBlock(TopBB)),
1044 TopBB(TopBB),
1045 TopInst(NULL),
1046 modified(modified) {}
Nick Lewycky9d17c822006-10-25 23:48:24 +00001047
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001048 VRPSolver(InequalityGraph &IG, UnreachableBlocks &UB, ETForest *Forest,
1049 bool &modified, Instruction *TopInst)
1050 : IG(IG),
1051 UB(UB),
1052 Forest(Forest),
1053 TopInst(TopInst),
1054 modified(modified)
1055 {
1056 TopBB = TopInst->getParent();
1057 Top = Forest->getNodeForBlock(TopBB);
1058 }
1059
1060 bool isRelatedBy(Value *V1, Value *V2, ICmpInst::Predicate Pred) const {
1061 if (Constant *C1 = dyn_cast<Constant>(V1))
1062 if (Constant *C2 = dyn_cast<Constant>(V2))
1063 return ConstantExpr::getCompare(Pred, C1, C2) ==
Zhou Sheng75b871f2007-01-11 12:24:14 +00001064 ConstantInt::getTrue();
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001065
1066 // XXX: this is lousy. If we're passed a Constant, then we might miss
1067 // some relationships if it isn't in the IG because the relationships
1068 // added by initializeConstant are missing.
1069 if (isa<Constant>(V1)) IG.getOrInsertNode(V1, Top);
1070 if (isa<Constant>(V2)) IG.getOrInsertNode(V2, Top);
1071
1072 if (unsigned n1 = IG.getNode(V1, Top))
1073 if (unsigned n2 = IG.getNode(V2, Top)) {
1074 if (n1 == n2) return Pred == ICmpInst::ICMP_EQ ||
1075 Pred == ICmpInst::ICMP_ULE ||
1076 Pred == ICmpInst::ICMP_UGE ||
1077 Pred == ICmpInst::ICMP_SLE ||
1078 Pred == ICmpInst::ICMP_SGE;
1079 if (Pred == ICmpInst::ICMP_EQ) return false;
1080 return IG.isRelatedBy(n1, n2, Top, cmpInstToLattice(Pred));
1081 }
1082
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001083 return false;
1084 }
1085
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001086 /// add - adds a new property to the work queue
1087 void add(Value *V1, Value *V2, ICmpInst::Predicate Pred,
1088 Instruction *I = NULL) {
1089 DOUT << "adding " << *V1 << " " << Pred << " " << *V2;
1090 if (I) DOUT << " context: " << *I;
1091 else DOUT << " default context";
1092 DOUT << "\n";
1093
1094 WorkList.push_back(Operation());
1095 Operation &O = WorkList.back();
Nick Lewycky42944462007-01-13 02:05:28 +00001096 O.LHS = V1, O.RHS = V2, O.Op = Pred, O.ContextInst = I;
1097 O.ContextBB = I ? I->getParent() : TopBB;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001098 }
1099
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001100 /// defToOps - Given an instruction definition that we've learned something
1101 /// new about, find any new relationships between its operands.
1102 void defToOps(Instruction *I) {
1103 Instruction *NewContext = below(I) ? I : TopInst;
1104 Value *Canonical = IG.canonicalize(I, Top);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001105
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001106 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
1107 const Type *Ty = BO->getType();
1108 assert(!Ty->isFPOrFPVector() && "Float in work queue!");
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001109
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001110 Value *Op0 = IG.canonicalize(BO->getOperand(0), Top);
1111 Value *Op1 = IG.canonicalize(BO->getOperand(1), Top);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001112
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001113 // TODO: "and bool true, %x" EQ %y then %x EQ %y.
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001114
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001115 switch (BO->getOpcode()) {
1116 case Instruction::And: {
1117 // "and int %a, %b" EQ -1 then %a EQ -1 and %b EQ -1
1118 // "and bool %a, %b" EQ true then %a EQ true and %b EQ true
Zhou Sheng75b871f2007-01-11 12:24:14 +00001119 ConstantInt *CI = ConstantInt::getAllOnesValue(Ty);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001120 if (Canonical == CI) {
1121 add(CI, Op0, ICmpInst::ICMP_EQ, NewContext);
1122 add(CI, Op1, ICmpInst::ICMP_EQ, NewContext);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001123 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001124 } break;
1125 case Instruction::Or: {
1126 // "or int %a, %b" EQ 0 then %a EQ 0 and %b EQ 0
1127 // "or bool %a, %b" EQ false then %a EQ false and %b EQ false
1128 Constant *Zero = Constant::getNullValue(Ty);
1129 if (Canonical == Zero) {
1130 add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext);
1131 add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext);
1132 }
1133 } break;
1134 case Instruction::Xor: {
1135 // "xor bool true, %a" EQ true then %a EQ false
1136 // "xor bool true, %a" EQ false then %a EQ true
1137 // "xor bool false, %a" EQ true then %a EQ true
1138 // "xor bool false, %a" EQ false then %a EQ false
1139 // "xor int %c, %a" EQ %c then %a EQ 0
1140 // "xor int %c, %a" NE %c then %a NE 0
1141 // 1. Repeat all of the above, with order of operands reversed.
1142 Value *LHS = Op0;
1143 Value *RHS = Op1;
1144 if (!isa<Constant>(LHS)) std::swap(LHS, RHS);
1145
Nick Lewycky4a74a752007-01-12 00:02:12 +00001146 if (ConstantInt *CI = dyn_cast<ConstantInt>(Canonical)) {
1147 if (ConstantInt *Arg = dyn_cast<ConstantInt>(LHS)) {
1148 add(RHS, ConstantInt::get(CI->getType(), CI->getZExtValue() ^
1149 Arg->getZExtValue()),
1150 ICmpInst::ICMP_EQ, NewContext);
1151 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001152 }
1153 if (Canonical == LHS) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001154 if (isa<ConstantInt>(Canonical))
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001155 add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ,
1156 NewContext);
1157 } else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) {
1158 add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE,
1159 NewContext);
1160 }
1161 } break;
1162 default:
1163 break;
1164 }
1165 } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) {
1166 // "icmp ult int %a, int %y" EQ true then %a u< y
1167 // etc.
1168
Zhou Sheng75b871f2007-01-11 12:24:14 +00001169 if (Canonical == ConstantInt::getTrue()) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001170 add(IC->getOperand(0), IC->getOperand(1), IC->getPredicate(),
1171 NewContext);
Zhou Sheng75b871f2007-01-11 12:24:14 +00001172 } else if (Canonical == ConstantInt::getFalse()) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001173 add(IC->getOperand(0), IC->getOperand(1),
1174 ICmpInst::getInversePredicate(IC->getPredicate()), NewContext);
1175 }
1176 } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
1177 if (I->getType()->isFPOrFPVector()) return;
1178
1179 // Given: "%a = select bool %x, int %b, int %c"
1180 // %a EQ %b and %b NE %c then %x EQ true
1181 // %a EQ %c and %b NE %c then %x EQ false
1182
1183 Value *True = SI->getTrueValue();
1184 Value *False = SI->getFalseValue();
1185 if (isRelatedBy(True, False, ICmpInst::ICMP_NE)) {
1186 if (Canonical == IG.canonicalize(True, Top) ||
1187 isRelatedBy(Canonical, False, ICmpInst::ICMP_NE))
Zhou Sheng75b871f2007-01-11 12:24:14 +00001188 add(SI->getCondition(), ConstantInt::getTrue(),
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001189 ICmpInst::ICMP_EQ, NewContext);
1190 else if (Canonical == IG.canonicalize(False, Top) ||
1191 isRelatedBy(I, True, ICmpInst::ICMP_NE))
Zhou Sheng75b871f2007-01-11 12:24:14 +00001192 add(SI->getCondition(), ConstantInt::getFalse(),
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001193 ICmpInst::ICMP_EQ, NewContext);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001194 }
1195 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001196 // TODO: CastInst "%a = cast ... %b" where %a is EQ or NE a constant.
1197 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001198
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001199 /// opsToDef - A new relationship was discovered involving one of this
1200 /// instruction's operands. Find any new relationship involving the
1201 /// definition.
1202 void opsToDef(Instruction *I) {
1203 Instruction *NewContext = below(I) ? I : TopInst;
1204
1205 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
1206 Value *Op0 = IG.canonicalize(BO->getOperand(0), Top);
1207 Value *Op1 = IG.canonicalize(BO->getOperand(1), Top);
1208
Zhou Sheng75b871f2007-01-11 12:24:14 +00001209 if (ConstantInt *CI0 = dyn_cast<ConstantInt>(Op0))
1210 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(Op1)) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001211 add(BO, ConstantExpr::get(BO->getOpcode(), CI0, CI1),
1212 ICmpInst::ICMP_EQ, NewContext);
1213 return;
1214 }
1215
1216 // "%y = and bool true, %x" then %x EQ %y.
1217 // "%y = or bool false, %x" then %x EQ %y.
1218 if (BO->getOpcode() == Instruction::Or) {
1219 Constant *Zero = Constant::getNullValue(BO->getType());
1220 if (Op0 == Zero) {
1221 add(BO, Op1, ICmpInst::ICMP_EQ, NewContext);
1222 return;
1223 } else if (Op1 == Zero) {
1224 add(BO, Op0, ICmpInst::ICMP_EQ, NewContext);
1225 return;
1226 }
1227 } else if (BO->getOpcode() == Instruction::And) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001228 Constant *AllOnes = ConstantInt::getAllOnesValue(BO->getType());
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001229 if (Op0 == AllOnes) {
1230 add(BO, Op1, ICmpInst::ICMP_EQ, NewContext);
1231 return;
1232 } else if (Op1 == AllOnes) {
1233 add(BO, Op0, ICmpInst::ICMP_EQ, NewContext);
1234 return;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001235 }
1236 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001237
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001238 // "%x = add int %y, %z" and %x EQ %y then %z EQ 0
1239 // "%x = mul int %y, %z" and %x EQ %y then %z EQ 1
1240 // 1. Repeat all of the above, with order of operands reversed.
1241 // "%x = udiv int %y, %z" and %x EQ %y then %z EQ 1
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001242
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001243 Value *Known = Op0, *Unknown = Op1;
1244 if (Known != BO) std::swap(Known, Unknown);
1245 if (Known == BO) {
1246 const Type *Ty = BO->getType();
1247 assert(!Ty->isFPOrFPVector() && "Float in work queue!");
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001248
1249 switch (BO->getOpcode()) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001250 default: break;
1251 case Instruction::Xor:
1252 case Instruction::Or:
1253 case Instruction::Add:
1254 case Instruction::Sub:
Nick Lewycky4a74a752007-01-12 00:02:12 +00001255 add(Unknown, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ,
1256 NewContext);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001257 break;
1258 case Instruction::UDiv:
1259 case Instruction::SDiv:
1260 if (Unknown == Op0) break; // otherwise, fallthrough
1261 case Instruction::And:
1262 case Instruction::Mul:
Nick Lewycky4a74a752007-01-12 00:02:12 +00001263 if (isa<ConstantInt>(Unknown)) {
1264 Constant *One = ConstantInt::get(Ty, 1);
1265 add(Unknown, One, ICmpInst::ICMP_EQ, NewContext);
1266 }
Nick Lewycky9d17c822006-10-25 23:48:24 +00001267 break;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001268 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001269 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001270
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001271 // TODO: "%a = add int %b, 1" and %b > %z then %a >= %z.
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001272
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001273 } else if (ICmpInst *IC = dyn_cast<ICmpInst>(I)) {
1274 // "%a = icmp ult %b, %c" and %b u< %c then %a EQ true
1275 // "%a = icmp ult %b, %c" and %b u>= %c then %a EQ false
1276 // etc.
1277
1278 Value *Op0 = IG.canonicalize(IC->getOperand(0), Top);
1279 Value *Op1 = IG.canonicalize(IC->getOperand(1), Top);
1280
1281 ICmpInst::Predicate Pred = IC->getPredicate();
1282 if (isRelatedBy(Op0, Op1, Pred)) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001283 add(IC, ConstantInt::getTrue(), ICmpInst::ICMP_EQ, NewContext);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001284 } else if (isRelatedBy(Op0, Op1, ICmpInst::getInversePredicate(Pred))) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001285 add(IC, ConstantInt::getFalse(), ICmpInst::ICMP_EQ, NewContext);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001286 }
1287
Nick Lewycky4a74a752007-01-12 00:02:12 +00001288 // TODO: "bool %x s<u> %y" implies %x = true and %y = false.
1289
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001290 // TODO: make the predicate more strict, if possible.
1291
1292 } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
1293 // Given: "%a = select bool %x, int %b, int %c"
1294 // %x EQ true then %a EQ %b
1295 // %x EQ false then %a EQ %c
1296 // %b EQ %c then %a EQ %b
1297
1298 Value *Canonical = IG.canonicalize(SI->getCondition(), Top);
Zhou Sheng75b871f2007-01-11 12:24:14 +00001299 if (Canonical == ConstantInt::getTrue()) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001300 add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext);
Zhou Sheng75b871f2007-01-11 12:24:14 +00001301 } else if (Canonical == ConstantInt::getFalse()) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001302 add(SI, SI->getFalseValue(), ICmpInst::ICMP_EQ, NewContext);
1303 } else if (IG.canonicalize(SI->getTrueValue(), Top) ==
1304 IG.canonicalize(SI->getFalseValue(), Top)) {
1305 add(SI, SI->getTrueValue(), ICmpInst::ICMP_EQ, NewContext);
1306 }
Nick Lewyckyee32ee02007-01-12 01:23:53 +00001307 } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
1308 if (CI->getDestTy()->isFPOrFPVector()) return;
1309
1310 if (Constant *C = dyn_cast<Constant>(
1311 IG.canonicalize(CI->getOperand(0), Top))) {
1312 add(CI, ConstantExpr::getCast(CI->getOpcode(), C, CI->getDestTy()),
1313 ICmpInst::ICMP_EQ, NewContext);
1314 }
1315
1316 // TODO: "%a = cast ... %b" where %b is NE/LT/GT a constant.
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001317 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001318 }
1319
1320 /// solve - process the work queue
1321 /// Return false if a logical contradiction occurs.
1322 void solve() {
1323 //DOUT << "WorkList entry, size: " << WorkList.size() << "\n";
1324 while (!WorkList.empty()) {
1325 //DOUT << "WorkList size: " << WorkList.size() << "\n";
1326
1327 Operation &O = WorkList.front();
Nick Lewycky42944462007-01-13 02:05:28 +00001328 TopInst = O.ContextInst;
1329 TopBB = O.ContextBB;
1330 Top = Forest->getNodeForBlock(TopBB);
1331
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001332 O.LHS = IG.canonicalize(O.LHS, Top);
1333 O.RHS = IG.canonicalize(O.RHS, Top);
1334
1335 assert(O.LHS == IG.canonicalize(O.LHS, Top) && "Canonicalize isn't.");
1336 assert(O.RHS == IG.canonicalize(O.RHS, Top) && "Canonicalize isn't.");
1337
1338 DOUT << "solving " << *O.LHS << " " << O.Op << " " << *O.RHS;
Nick Lewycky42944462007-01-13 02:05:28 +00001339 if (O.ContextInst) DOUT << " context inst: " << *O.ContextInst;
1340 else DOUT << " context block: " << O.ContextBB->getName();
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001341 DOUT << "\n";
1342
1343 DEBUG(IG.dump());
1344
1345 // TODO: actually check the constants and add to UB.
1346 if (isa<Constant>(O.LHS) && isa<Constant>(O.RHS)) {
1347 WorkList.pop_front();
1348 continue;
1349 }
1350
1351 if (O.Op == ICmpInst::ICMP_EQ) {
1352 if (!makeEqual(O.LHS, O.RHS))
1353 UB.mark(TopBB);
1354 } else {
1355 LatticeVal LV = cmpInstToLattice(O.Op);
1356
1357 if ((LV & EQ_BIT) &&
1358 isRelatedBy(O.LHS, O.RHS, ICmpInst::getSwappedPredicate(O.Op))) {
1359 if (!makeEqual(O.LHS, O.RHS))
1360 UB.mark(TopBB);
1361 } else {
1362 if (isRelatedBy(O.LHS, O.RHS, ICmpInst::getInversePredicate(O.Op))){
1363 DOUT << "inequality contradiction!\n";
1364 WorkList.pop_front();
1365 continue;
1366 }
1367
1368 unsigned n1 = IG.getOrInsertNode(O.LHS, Top);
1369 unsigned n2 = IG.getOrInsertNode(O.RHS, Top);
1370
1371 if (n1 == n2) {
1372 if (O.Op != ICmpInst::ICMP_UGE && O.Op != ICmpInst::ICMP_ULE &&
1373 O.Op != ICmpInst::ICMP_SGE && O.Op != ICmpInst::ICMP_SLE)
1374 UB.mark(TopBB);
1375
1376 WorkList.pop_front();
1377 continue;
1378 }
1379
1380 if (IG.isRelatedBy(n1, n2, Top, LV)) {
1381 WorkList.pop_front();
1382 continue;
1383 }
1384
1385 IG.addInequality(n1, n2, Top, LV);
1386
Nick Lewycky6ce36cf2007-01-15 14:30:07 +00001387 if (Instruction *I1 = dyn_cast<Instruction>(O.LHS)) {
1388 if (below(I1) ||
1389 Top->DominatedBy(Forest->getNodeForBlock(I1->getParent())))
1390 defToOps(I1);
1391 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001392 if (isa<Instruction>(O.LHS) || isa<Argument>(O.LHS)) {
1393 for (Value::use_iterator UI = O.LHS->use_begin(),
1394 UE = O.LHS->use_end(); UI != UE;) {
1395 Use &TheUse = UI.getUse();
1396 ++UI;
1397 if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
Nick Lewycky6ce36cf2007-01-15 14:30:07 +00001398 if (below(I) ||
1399 Top->DominatedBy(Forest->getNodeForBlock(I->getParent())))
1400 opsToDef(I);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001401 }
1402 }
1403 }
Nick Lewycky6ce36cf2007-01-15 14:30:07 +00001404 if (Instruction *I2 = dyn_cast<Instruction>(O.RHS)) {
1405 if (below(I2) ||
1406 Top->DominatedBy(Forest->getNodeForBlock(I2->getParent())))
1407 defToOps(I2);
1408 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001409 if (isa<Instruction>(O.RHS) || isa<Argument>(O.RHS)) {
1410 for (Value::use_iterator UI = O.RHS->use_begin(),
1411 UE = O.RHS->use_end(); UI != UE;) {
1412 Use &TheUse = UI.getUse();
1413 ++UI;
1414 if (Instruction *I = dyn_cast<Instruction>(TheUse.getUser())) {
Nick Lewycky6ce36cf2007-01-15 14:30:07 +00001415 if (below(I) ||
1416 Top->DominatedBy(Forest->getNodeForBlock(I->getParent())))
1417
1418 opsToDef(I);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001419 }
1420 }
Nick Lewycky9d17c822006-10-25 23:48:24 +00001421 }
1422 }
Nick Lewycky9d17c822006-10-25 23:48:24 +00001423 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001424 WorkList.pop_front();
Nick Lewycky9d17c822006-10-25 23:48:24 +00001425 }
Nick Lewycky9d17c822006-10-25 23:48:24 +00001426 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001427 };
1428
1429 /// PredicateSimplifier - This class is a simplifier that replaces
1430 /// one equivalent variable with another. It also tracks what
1431 /// can't be equal and will solve setcc instructions when possible.
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001432 /// @brief Root of the predicate simplifier optimization.
1433 class VISIBILITY_HIDDEN PredicateSimplifier : public FunctionPass {
1434 DominatorTree *DT;
1435 ETForest *Forest;
1436 bool modified;
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001437 InequalityGraph *IG;
1438 UnreachableBlocks UB;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001439
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001440 std::vector<DominatorTree::Node *> WorkList;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001441
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001442 public:
1443 bool runOnFunction(Function &F);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001444
1445 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1446 AU.addRequiredID(BreakCriticalEdgesID);
1447 AU.addRequired<DominatorTree>();
1448 AU.addRequired<ETForest>();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001449 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001450
1451 private:
Nick Lewycky77e030b2006-10-12 02:02:44 +00001452 /// Forwards - Adds new properties into PropertySet and uses them to
1453 /// simplify instructions. Because new properties sometimes apply to
1454 /// a transition from one BasicBlock to another, this will use the
1455 /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the
1456 /// basic block with the new PropertySet.
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001457 /// @brief Performs abstract execution of the program.
1458 class VISIBILITY_HIDDEN Forwards : public InstVisitor<Forwards> {
Nick Lewycky77e030b2006-10-12 02:02:44 +00001459 friend class InstVisitor<Forwards>;
1460 PredicateSimplifier *PS;
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001461 DominatorTree::Node *DTNode;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001462
Nick Lewycky77e030b2006-10-12 02:02:44 +00001463 public:
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001464 InequalityGraph &IG;
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001465 UnreachableBlocks &UB;
Nick Lewycky77e030b2006-10-12 02:02:44 +00001466
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001467 Forwards(PredicateSimplifier *PS, DominatorTree::Node *DTNode)
1468 : PS(PS), DTNode(DTNode), IG(*PS->IG), UB(PS->UB) {}
Nick Lewycky77e030b2006-10-12 02:02:44 +00001469
1470 void visitTerminatorInst(TerminatorInst &TI);
1471 void visitBranchInst(BranchInst &BI);
1472 void visitSwitchInst(SwitchInst &SI);
1473
Nick Lewyckyf3450082006-10-22 19:53:27 +00001474 void visitAllocaInst(AllocaInst &AI);
Nick Lewycky77e030b2006-10-12 02:02:44 +00001475 void visitLoadInst(LoadInst &LI);
1476 void visitStoreInst(StoreInst &SI);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001477
Nick Lewycky77e030b2006-10-12 02:02:44 +00001478 void visitBinaryOperator(BinaryOperator &BO);
1479 };
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001480
1481 // Used by terminator instructions to proceed from the current basic
1482 // block to the next. Verifies that "current" dominates "next",
1483 // then calls visitBasicBlock.
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001484 void proceedToSuccessors(DominatorTree::Node *Current) {
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001485 for (DominatorTree::Node::iterator I = Current->begin(),
1486 E = Current->end(); I != E; ++I) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001487 WorkList.push_back(*I);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001488 }
1489 }
1490
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001491 void proceedToSuccessor(DominatorTree::Node *Next) {
1492 WorkList.push_back(Next);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001493 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001494
1495 // Visits each instruction in the basic block.
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001496 void visitBasicBlock(DominatorTree::Node *Node) {
1497 BasicBlock *BB = Node->getBlock();
1498 ETNode *ET = Forest->getNodeForBlock(BB);
Nick Lewycky6ce36cf2007-01-15 14:30:07 +00001499 DOUT << "Entering Basic Block: " << BB->getName()
1500 << " (" << ET->getDFSNumIn() << ")\n";
Bill Wendling22e978a2006-12-07 20:04:42 +00001501 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001502 visitInstruction(I++, Node, ET);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001503 }
1504 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001505
Nick Lewycky9a22d7b2006-09-10 02:27:07 +00001506 // Tries to simplify each Instruction and add new properties to
Nick Lewycky77e030b2006-10-12 02:02:44 +00001507 // the PropertySet.
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001508 void visitInstruction(Instruction *I, DominatorTree::Node *DT, ETNode *ET) {
Bill Wendling22e978a2006-12-07 20:04:42 +00001509 DOUT << "Considering instruction " << *I << "\n";
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001510 DEBUG(IG->dump());
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001511
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001512 // Sometimes instructions are killed in earlier analysis.
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001513 if (isInstructionTriviallyDead(I)) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001514 ++NumSimple;
1515 modified = true;
1516 IG->remove(I);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001517 I->eraseFromParent();
1518 return;
1519 }
1520
Nick Lewycky42944462007-01-13 02:05:28 +00001521#ifndef NDEBUG
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001522 // Try to replace the whole instruction.
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001523 Value *V = IG->canonicalize(I, ET);
1524 assert(V == I && "Late instruction canonicalization.");
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001525 if (V != I) {
1526 modified = true;
1527 ++NumInstruction;
Bill Wendling22e978a2006-12-07 20:04:42 +00001528 DOUT << "Removing " << *I << ", replacing with " << *V << "\n";
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001529 IG->remove(I);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001530 I->replaceAllUsesWith(V);
1531 I->eraseFromParent();
1532 return;
1533 }
1534
1535 // Try to substitute operands.
1536 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
1537 Value *Oper = I->getOperand(i);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001538 Value *V = IG->canonicalize(Oper, ET);
1539 assert(V == Oper && "Late operand canonicalization.");
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001540 if (V != Oper) {
1541 modified = true;
1542 ++NumVarsReplaced;
Bill Wendling22e978a2006-12-07 20:04:42 +00001543 DOUT << "Resolving " << *I;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001544 I->setOperand(i, V);
Bill Wendling22e978a2006-12-07 20:04:42 +00001545 DOUT << " into " << *I;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001546 }
1547 }
Nick Lewycky42944462007-01-13 02:05:28 +00001548#endif
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001549
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001550 DOUT << "push (%" << I->getParent()->getName() << ")\n";
1551 Forwards visit(this, DT);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001552 visit.visit(*I);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001553 DOUT << "pop (%" << I->getParent()->getName() << ")\n";
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001554 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001555 };
1556
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001557 bool PredicateSimplifier::runOnFunction(Function &F) {
1558 DT = &getAnalysis<DominatorTree>();
1559 Forest = &getAnalysis<ETForest>();
Nick Lewyckycfff1c32006-09-20 17:04:01 +00001560
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001561 Forest->updateDFSNumbers(); // XXX: should only act when numbers are out of date
1562
Bill Wendling22e978a2006-12-07 20:04:42 +00001563 DOUT << "Entering Function: " << F.getName() << "\n";
Nick Lewyckycfff1c32006-09-20 17:04:01 +00001564
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001565 modified = false;
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001566 BasicBlock *RootBlock = &F.getEntryBlock();
1567 IG = new InequalityGraph(Forest->getNodeForBlock(RootBlock));
1568 WorkList.push_back(DT->getRootNode());
Nick Lewyckycfff1c32006-09-20 17:04:01 +00001569
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001570 do {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001571 DominatorTree::Node *DTNode = WorkList.back();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001572 WorkList.pop_back();
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001573 if (!UB.isDead(DTNode->getBlock())) visitBasicBlock(DTNode);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001574 } while (!WorkList.empty());
Nick Lewyckycfff1c32006-09-20 17:04:01 +00001575
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001576 delete IG;
1577
1578 modified |= UB.kill();
Nick Lewyckycfff1c32006-09-20 17:04:01 +00001579
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001580 return modified;
Nick Lewycky8e559932006-09-02 19:40:38 +00001581 }
1582
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001583 void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001584 PS->proceedToSuccessors(DTNode);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001585 }
1586
1587 void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) {
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001588 if (BI.isUnconditional()) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001589 PS->proceedToSuccessors(DTNode);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001590 return;
1591 }
1592
1593 Value *Condition = BI.getCondition();
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001594 BasicBlock *TrueDest = BI.getSuccessor(0);
1595 BasicBlock *FalseDest = BI.getSuccessor(1);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001596
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001597 if (isa<Constant>(Condition) || TrueDest == FalseDest) {
1598 PS->proceedToSuccessors(DTNode);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001599 return;
1600 }
1601
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001602 for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001603 I != E; ++I) {
1604 BasicBlock *Dest = (*I)->getBlock();
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001605 DOUT << "Branch thinking about %" << Dest->getName()
Nick Lewycky6ce36cf2007-01-15 14:30:07 +00001606 << "(" << PS->Forest->getNodeForBlock(Dest)->getDFSNumIn() << ")\n";
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001607
1608 if (Dest == TrueDest) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001609 DOUT << "(" << DTNode->getBlock()->getName() << ") true set:\n";
1610 VRPSolver VRP(IG, UB, PS->Forest, PS->modified, Dest);
Zhou Sheng75b871f2007-01-11 12:24:14 +00001611 VRP.add(ConstantInt::getTrue(), Condition, ICmpInst::ICMP_EQ);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001612 VRP.solve();
1613 DEBUG(IG.dump());
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001614 } else if (Dest == FalseDest) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001615 DOUT << "(" << DTNode->getBlock()->getName() << ") false set:\n";
1616 VRPSolver VRP(IG, UB, PS->Forest, PS->modified, Dest);
Zhou Sheng75b871f2007-01-11 12:24:14 +00001617 VRP.add(ConstantInt::getFalse(), Condition, ICmpInst::ICMP_EQ);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001618 VRP.solve();
1619 DEBUG(IG.dump());
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001620 }
1621
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001622 PS->proceedToSuccessor(*I);
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001623 }
1624 }
1625
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001626 void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) {
1627 Value *Condition = SI.getCondition();
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001628
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001629 // Set the EQProperty in each of the cases BBs, and the NEProperties
1630 // in the default BB.
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001631
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001632 for (DominatorTree::Node::iterator I = DTNode->begin(), E = DTNode->end();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001633 I != E; ++I) {
1634 BasicBlock *BB = (*I)->getBlock();
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001635 DOUT << "Switch thinking about BB %" << BB->getName()
Nick Lewycky6ce36cf2007-01-15 14:30:07 +00001636 << "(" << PS->Forest->getNodeForBlock(BB)->getDFSNumIn() << ")\n";
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001637
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001638 VRPSolver VRP(IG, UB, PS->Forest, PS->modified, BB);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001639 if (BB == SI.getDefaultDest()) {
1640 for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i)
1641 if (SI.getSuccessor(i) != BB)
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001642 VRP.add(Condition, SI.getCaseValue(i), ICmpInst::ICMP_NE);
1643 VRP.solve();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001644 } else if (ConstantInt *CI = SI.findCaseDest(BB)) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001645 VRP.add(Condition, CI, ICmpInst::ICMP_EQ);
1646 VRP.solve();
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001647 }
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001648 PS->proceedToSuccessor(*I);
Nick Lewycky1d00f3e2006-10-03 15:19:11 +00001649 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001650 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001651
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001652 void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001653 VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &AI);
1654 VRP.add(Constant::getNullValue(AI.getType()), &AI, ICmpInst::ICMP_NE);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001655 VRP.solve();
1656 }
Nick Lewyckyf3450082006-10-22 19:53:27 +00001657
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001658 void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) {
1659 Value *Ptr = LI.getPointerOperand();
1660 // avoid "load uint* null" -> null NE null.
1661 if (isa<Constant>(Ptr)) return;
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001662
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001663 VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &LI);
1664 VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001665 VRP.solve();
1666 }
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001667
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001668 void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) {
1669 Value *Ptr = SI.getPointerOperand();
1670 if (isa<Constant>(Ptr)) return;
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001671
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001672 VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &SI);
1673 VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001674 VRP.solve();
1675 }
1676
1677 void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) {
1678 Instruction::BinaryOps ops = BO.getOpcode();
1679
1680 switch (ops) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001681 case Instruction::URem:
1682 case Instruction::SRem:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001683 case Instruction::UDiv:
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001684 case Instruction::SDiv: {
Nick Lewycky77e030b2006-10-12 02:02:44 +00001685 Value *Divisor = BO.getOperand(1);
Nick Lewycky2fc338f2007-01-11 02:32:38 +00001686 VRPSolver VRP(IG, UB, PS->Forest, PS->modified, &BO);
1687 VRP.add(Constant::getNullValue(Divisor->getType()), Divisor,
1688 ICmpInst::ICMP_NE);
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001689 VRP.solve();
Nick Lewycky5f8f9af2006-08-30 02:46:48 +00001690 break;
1691 }
1692 default:
1693 break;
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001694 }
Nick Lewycky5f8f9af2006-08-30 02:46:48 +00001695 }
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001696
Nick Lewycky09b7e4d2006-11-22 23:49:16 +00001697 RegisterPass<PredicateSimplifier> X("predsimplify",
1698 "Predicate Simplifier");
1699}
1700
1701FunctionPass *llvm::createPredicateSimplifierPass() {
1702 return new PredicateSimplifier();
Nick Lewyckyb2e8ae12006-08-28 22:44:55 +00001703}