Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 1 | //===-- PredicateSimplifier.cpp - Path Sensitive Simplifier -----------===// |
| 2 | // |
| 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 | // |
| 8 | //===------------------------------------------------------------------===// |
| 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 | // |
| 23 | //===------------------------------------------------------------------===// |
| 24 | // |
| 25 | // This optimization works by substituting %q for %p when protected by a |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 26 | // conditional that assures us of that fact. Properties are stored as |
| 27 | // relationships between two values. |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 28 | // |
| 29 | //===------------------------------------------------------------------===// |
| 30 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "predsimplify" |
| 32 | #include "llvm/Transforms/Scalar.h" |
| 33 | #include "llvm/Constants.h" |
Nick Lewycky | 802fe27 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 34 | #include "llvm/DerivedTypes.h" |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 35 | #include "llvm/Instructions.h" |
| 36 | #include "llvm/Pass.h" |
| 37 | #include "llvm/ADT/Statistic.h" |
| 38 | #include "llvm/ADT/STLExtras.h" |
| 39 | #include "llvm/Analysis/Dominators.h" |
| 40 | #include "llvm/Support/CFG.h" |
| 41 | #include "llvm/Support/Debug.h" |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 42 | #include "llvm/Support/InstVisitor.h" |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 43 | #include <iostream> |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 44 | #include <list> |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 47 | typedef DominatorTree::Node DTNodeType; |
| 48 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | Statistic<> |
| 51 | NumVarsReplaced("predsimplify", "Number of argument substitutions"); |
| 52 | Statistic<> |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 53 | NumInstruction("predsimplify", "Number of instructions removed"); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 54 | |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 55 | class PropertySet; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 56 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 57 | /// Similar to EquivalenceClasses, this stores the set of equivalent |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 58 | /// types. Beyond EquivalenceClasses, it allows us to specify which |
| 59 | /// element will act as leader. |
| 60 | template<typename ElemTy> |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 61 | class VISIBILITY_HIDDEN Synonyms { |
| 62 | std::map<ElemTy, unsigned> mapping; |
| 63 | std::vector<ElemTy> leaders; |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 64 | PropertySet *PS; |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 65 | |
| 66 | public: |
| 67 | typedef unsigned iterator; |
| 68 | typedef const unsigned const_iterator; |
| 69 | |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 70 | Synonyms(PropertySet *PS) : PS(PS) {} |
| 71 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 72 | // Inspection |
| 73 | |
| 74 | bool empty() const { |
| 75 | return leaders.empty(); |
| 76 | } |
| 77 | |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 78 | iterator findLeader(ElemTy &e) { |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 79 | typename std::map<ElemTy, unsigned>::iterator MI = mapping.find(e); |
| 80 | if (MI == mapping.end()) return 0; |
| 81 | |
| 82 | return MI->second; |
| 83 | } |
| 84 | |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 85 | const_iterator findLeader(ElemTy &e) const { |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 86 | typename std::map<ElemTy, unsigned>::const_iterator MI = |
| 87 | mapping.find(e); |
| 88 | if (MI == mapping.end()) return 0; |
| 89 | |
| 90 | return MI->second; |
| 91 | } |
| 92 | |
| 93 | ElemTy &getLeader(iterator I) { |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 94 | assert(I && I <= leaders.size() && "Illegal leader to get."); |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 95 | return leaders[I-1]; |
| 96 | } |
| 97 | |
| 98 | const ElemTy &getLeader(const_iterator I) const { |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 99 | assert(I && I <= leaders.size() && "Illegal leaders to get."); |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 100 | return leaders[I-1]; |
| 101 | } |
| 102 | |
| 103 | #ifdef DEBUG |
| 104 | void debug(std::ostream &os) const { |
| 105 | for (unsigned i = 1, e = leaders.size()+1; i != e; ++i) { |
Nick Lewycky | 668a1d0 | 2006-09-13 19:32:53 +0000 | [diff] [blame] | 106 | os << i << ". " << *getLeader(i) << ": ["; |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 107 | for (std::map<Value *, unsigned>::const_iterator |
| 108 | I = mapping.begin(), E = mapping.end(); I != E; ++I) { |
| 109 | if ((*I).second == i && (*I).first != leaders[i-1]) { |
| 110 | os << *(*I).first << " "; |
Nick Lewycky | 977be25 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 113 | os << "]\n"; |
| 114 | } |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | // Mutators |
| 119 | |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 120 | void remove(ElemTy &e) { |
| 121 | ElemTy E = e; // The parameter to erase must not be a reference to |
| 122 | mapping.erase(E); // an element contained in the map. |
| 123 | } |
| 124 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 125 | /// Combine two sets referring to the same element, inserting the |
| 126 | /// elements as needed. Returns a valid iterator iff two already |
| 127 | /// existing disjoint synonym sets were combined. The iterator |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 128 | /// points to the no longer existing element. |
| 129 | iterator unionSets(ElemTy E1, ElemTy E2); |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 130 | |
| 131 | /// Returns an iterator pointing to the synonym set containing |
| 132 | /// element e. If none exists, a new one is created and returned. |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 133 | iterator findOrInsert(ElemTy &e) { |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 134 | iterator I = findLeader(e); |
| 135 | if (I) return I; |
| 136 | |
| 137 | leaders.push_back(e); |
| 138 | I = leaders.size(); |
| 139 | mapping[e] = I; |
| 140 | return I; |
| 141 | } |
| 142 | }; |
| 143 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 144 | /// Represents the set of equivalent Value*s and provides insertion |
| 145 | /// and fast lookup. Also stores the set of inequality relationships. |
| 146 | class PropertySet { |
Nick Lewycky | 8f389d8 | 2006-09-23 15:13:08 +0000 | [diff] [blame] | 147 | /// Returns true if V1 is a better choice than V2. |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 148 | bool compare(Value *V1, Value *V2) const { |
| 149 | if (isa<Constant>(V1)) { |
| 150 | if (!isa<Constant>(V2)) { |
| 151 | return true; |
| 152 | } |
| 153 | } else if (isa<Argument>(V1)) { |
| 154 | if (!isa<Constant>(V2) && !isa<Argument>(V2)) { |
| 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | if (Instruction *I1 = dyn_cast<Instruction>(V1)) { |
| 159 | if (Instruction *I2 = dyn_cast<Instruction>(V2)) { |
| 160 | BasicBlock *BB1 = I1->getParent(), |
| 161 | *BB2 = I2->getParent(); |
| 162 | if (BB1 == BB2) { |
| 163 | for (BasicBlock::const_iterator I = BB1->begin(), E = BB1->end(); |
| 164 | I != E; ++I) { |
| 165 | if (&*I == I1) return true; |
| 166 | if (&*I == I2) return false; |
| 167 | } |
| 168 | assert(0 && "Instructions not found in parent BasicBlock?"); |
| 169 | } else |
| 170 | return DT->getNode(BB1)->properlyDominates(DT->getNode(BB2)); |
| 171 | } |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 176 | struct Property; |
| 177 | public: |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 178 | /// Choose the canonical Value in a synonym set. |
| 179 | /// Leaves the more canonical choice in V1. |
| 180 | void order(Value *&V1, Value *&V2) const { |
| 181 | if (compare(V2, V1)) std::swap(V1, V2); |
| 182 | } |
| 183 | |
| 184 | PropertySet(DominatorTree *DT) : union_find(this), DT(DT) {} |
| 185 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 186 | Synonyms<Value *> union_find; |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 187 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 188 | typedef std::vector<Property>::iterator PropertyIterator; |
| 189 | typedef std::vector<Property>::const_iterator ConstPropertyIterator; |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 190 | typedef Synonyms<Value *>::iterator SynonymIterator; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 191 | |
| 192 | enum Ops { |
| 193 | EQ, |
| 194 | NE |
| 195 | }; |
| 196 | |
| 197 | Value *canonicalize(Value *V) const { |
| 198 | Value *C = lookup(V); |
| 199 | return C ? C : V; |
| 200 | } |
| 201 | |
| 202 | Value *lookup(Value *V) const { |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 203 | SynonymIterator SI = union_find.findLeader(V); |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 204 | if (!SI) return NULL; |
| 205 | return union_find.getLeader(SI); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | bool empty() const { |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 209 | return union_find.empty(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 212 | void remove(Value *V) { |
| 213 | SynonymIterator I = union_find.findLeader(V); |
| 214 | if (!I) return; |
| 215 | |
| 216 | union_find.remove(V); |
| 217 | |
| 218 | for (PropertyIterator PI = Properties.begin(), PE = Properties.end(); |
| 219 | PI != PE;) { |
| 220 | Property &P = *PI++; |
| 221 | if (P.I1 == I || P.I2 == I) Properties.erase(PI); |
| 222 | } |
| 223 | } |
| 224 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 225 | void addEqual(Value *V1, Value *V2) { |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 226 | // If %x = 0. and %y = -0., seteq %x, %y is true, but |
| 227 | // copysign(%x) is not the same as copysign(%y). |
Nick Lewycky | 977be25 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 228 | if (V1->getType()->isFloatingPoint()) return; |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 229 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 230 | order(V1, V2); |
| 231 | if (isa<Constant>(V2)) return; // refuse to set false == true. |
| 232 | |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 233 | if (union_find.findLeader(V1) && |
| 234 | union_find.findLeader(V1) == union_find.findLeader(V2)) |
| 235 | return; // no-op |
| 236 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 237 | SynonymIterator deleted = union_find.unionSets(V1, V2); |
| 238 | if (deleted) { |
| 239 | SynonymIterator replacement = union_find.findLeader(V1); |
| 240 | // Move Properties |
| 241 | for (PropertyIterator I = Properties.begin(), E = Properties.end(); |
| 242 | I != E; ++I) { |
| 243 | if (I->I1 == deleted) I->I1 = replacement; |
| 244 | else if (I->I1 > deleted) --I->I1; |
| 245 | if (I->I2 == deleted) I->I2 = replacement; |
| 246 | else if (I->I2 > deleted) --I->I2; |
| 247 | } |
| 248 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 249 | addImpliedProperties(EQ, V1, V2); |
| 250 | } |
| 251 | |
| 252 | void addNotEqual(Value *V1, Value *V2) { |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 253 | // If %x = NAN then seteq %x, %x is false. |
Nick Lewycky | 977be25 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 254 | if (V1->getType()->isFloatingPoint()) return; |
| 255 | |
| 256 | // For example, %x = setne int 0, 0 causes "0 != 0". |
| 257 | if (isa<Constant>(V1) && isa<Constant>(V2)) return; |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 258 | |
Nick Lewycky | 6e39c7e | 2006-08-31 00:39:16 +0000 | [diff] [blame] | 259 | if (findProperty(NE, V1, V2) != Properties.end()) |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 260 | return; // no-op. |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 261 | |
| 262 | // Add the property. |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 263 | SynonymIterator I1 = union_find.findOrInsert(V1), |
| 264 | I2 = union_find.findOrInsert(V2); |
Nick Lewycky | 977be25 | 2006-09-13 19:24:01 +0000 | [diff] [blame] | 265 | |
| 266 | // Technically this means that the block is unreachable. |
| 267 | if (I1 == I2) return; |
| 268 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 269 | Properties.push_back(Property(NE, I1, I2)); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 270 | addImpliedProperties(NE, V1, V2); |
| 271 | } |
| 272 | |
| 273 | PropertyIterator findProperty(Ops Opcode, Value *V1, Value *V2) { |
| 274 | assert(Opcode != EQ && "Can't findProperty on EQ." |
| 275 | "Use the lookup method instead."); |
| 276 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 277 | SynonymIterator I1 = union_find.findLeader(V1), |
| 278 | I2 = union_find.findLeader(V2); |
| 279 | if (!I1 || !I2) return Properties.end(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 280 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 281 | return |
| 282 | find(Properties.begin(), Properties.end(), Property(Opcode, I1, I2)); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | ConstPropertyIterator |
| 286 | findProperty(Ops Opcode, Value *V1, Value *V2) const { |
| 287 | assert(Opcode != EQ && "Can't findProperty on EQ." |
| 288 | "Use the lookup method instead."); |
| 289 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 290 | SynonymIterator I1 = union_find.findLeader(V1), |
| 291 | I2 = union_find.findLeader(V2); |
| 292 | if (!I1 || !I2) return Properties.end(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 293 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 294 | return |
| 295 | find(Properties.begin(), Properties.end(), Property(Opcode, I1, I2)); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private: |
| 299 | // Represents Head OP [Tail1, Tail2, ...] |
| 300 | // For example: %x != %a, %x != %b. |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 301 | struct VISIBILITY_HIDDEN Property { |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 302 | typedef SynonymIterator Iter; |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 303 | |
| 304 | Property(Ops opcode, Iter i1, Iter i2) |
| 305 | : Opcode(opcode), I1(i1), I2(i2) |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 306 | { assert(opcode != EQ && "Equality belongs in the synonym set, " |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 307 | "not a property."); } |
| 308 | |
| 309 | bool operator==(const Property &P) const { |
| 310 | return (Opcode == P.Opcode) && |
| 311 | ((I1 == P.I1 && I2 == P.I2) || |
| 312 | (I1 == P.I2 && I2 == P.I1)); |
| 313 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 314 | |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 315 | Ops Opcode; |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 316 | Iter I1, I2; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 319 | void addToResolve(Value *V, std::list<Value *> &WorkList) { |
| 320 | if (!isa<Constant>(V) && !isa<BasicBlock>(V)) { |
| 321 | for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); |
| 322 | UI != UE; ++UI) { |
| 323 | if (!isa<Constant>(*UI) && !isa<BasicBlock>(*UI)) { |
| 324 | WorkList.push_back(*UI); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void resolve(std::list<Value *> &WorkList) { |
| 331 | if (WorkList.empty()) return; |
| 332 | |
| 333 | Value *V = WorkList.front(); |
| 334 | WorkList.pop_front(); |
| 335 | |
| 336 | if (empty()) return; |
| 337 | |
| 338 | Instruction *I = dyn_cast<Instruction>(V); |
| 339 | if (!I) return; |
| 340 | |
| 341 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 342 | Value *lhs = canonicalize(BO->getOperand(0)), |
| 343 | *rhs = canonicalize(BO->getOperand(1)); |
| 344 | |
| 345 | ConstantIntegral *CI1 = dyn_cast<ConstantIntegral>(lhs), |
| 346 | *CI2 = dyn_cast<ConstantIntegral>(rhs); |
| 347 | |
| 348 | if (CI1 && CI2) { |
| 349 | addToResolve(BO, WorkList); |
| 350 | addEqual(BO, ConstantExpr::get(BO->getOpcode(), CI1, CI2)); |
| 351 | } else if (SetCondInst *SCI = dyn_cast<SetCondInst>(BO)) { |
| 352 | PropertySet::ConstPropertyIterator NE = |
| 353 | findProperty(PropertySet::NE, lhs, rhs); |
| 354 | |
| 355 | if (NE != Properties.end()) { |
| 356 | switch (SCI->getOpcode()) { |
| 357 | case Instruction::SetEQ: |
| 358 | addToResolve(SCI, WorkList); |
| 359 | addEqual(SCI, ConstantBool::getFalse()); |
| 360 | break; |
| 361 | case Instruction::SetNE: |
| 362 | addToResolve(SCI, WorkList); |
| 363 | addEqual(SCI, ConstantBool::getTrue()); |
| 364 | break; |
| 365 | case Instruction::SetLE: |
| 366 | case Instruction::SetGE: |
| 367 | case Instruction::SetLT: |
| 368 | case Instruction::SetGT: |
| 369 | break; |
| 370 | default: |
| 371 | assert(0 && "Unknown opcode in SetCondInst."); |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { |
| 377 | Value *Condition = canonicalize(SI->getCondition()); |
| 378 | if (ConstantBool *CB = dyn_cast<ConstantBool>(Condition)) { |
| 379 | addToResolve(SI, WorkList); |
| 380 | addEqual(SI, CB->getValue() ? SI->getTrueValue() : SI->getFalseValue()); |
| 381 | } |
| 382 | } |
| 383 | if (!WorkList.empty()) resolve(WorkList); |
| 384 | } |
| 385 | |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 386 | void add(Ops Opcode, Value *V1, Value *V2, bool invert) { |
| 387 | switch (Opcode) { |
| 388 | case EQ: |
| 389 | if (invert) addNotEqual(V1, V2); |
| 390 | else addEqual(V1, V2); |
| 391 | break; |
| 392 | case NE: |
| 393 | if (invert) addEqual(V1, V2); |
| 394 | else addNotEqual(V1, V2); |
| 395 | break; |
| 396 | default: |
| 397 | assert(0 && "Unknown property opcode."); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /// Finds the properties implied by an equivalence and adds them too. |
| 402 | /// Example: ("seteq %a, %b", true, EQ) --> (%a, %b, EQ) |
| 403 | /// ("seteq %a, %b", false, EQ) --> (%a, %b, NE) |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 404 | void addImpliedProperties(Ops Opcode, Value *V1, Value *V2) { |
| 405 | order(V1, V2); |
| 406 | |
| 407 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V2)) { |
| 408 | switch (BO->getOpcode()) { |
| 409 | case Instruction::SetEQ: |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 410 | // "seteq int %a, %b" EQ true then %a EQ %b |
| 411 | // "seteq int %a, %b" EQ false then %a NE %b |
| 412 | // "seteq int %a, %b" NE true then %a NE %b |
| 413 | // "seteq int %a, %b" NE false then %a EQ %b |
Chris Lattner | 47811b7 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 414 | if (ConstantBool *V1CB = dyn_cast<ConstantBool>(V1)) |
| 415 | add(Opcode, BO->getOperand(0), BO->getOperand(1),!V1CB->getValue()); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 416 | break; |
| 417 | case Instruction::SetNE: |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 418 | // "setne int %a, %b" EQ true then %a NE %b |
| 419 | // "setne int %a, %b" EQ false then %a EQ %b |
| 420 | // "setne int %a, %b" NE true then %a EQ %b |
| 421 | // "setne int %a, %b" NE false then %a NE %b |
Chris Lattner | 47811b7 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 422 | if (ConstantBool *V1CB = dyn_cast<ConstantBool>(V1)) |
| 423 | add(Opcode, BO->getOperand(0), BO->getOperand(1), V1CB->getValue()); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 424 | break; |
| 425 | case Instruction::SetLT: |
| 426 | case Instruction::SetGT: |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 427 | // "setlt/gt int %a, %b" EQ true then %a NE %b |
| 428 | // "setlt/gt int %a, %b" NE false then %a NE %b |
| 429 | |
| 430 | // "setlt int %a, %b" NE true then %a EQ %b |
| 431 | |
| 432 | if (ConstantBool *CB = dyn_cast<ConstantBool>(V1)) { |
| 433 | if (CB->getValue() ^ Opcode==NE) |
| 434 | addNotEqual(BO->getOperand(0), BO->getOperand(1)); |
| 435 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 436 | break; |
| 437 | case Instruction::SetLE: |
| 438 | case Instruction::SetGE: |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 439 | // "setle/ge int %a, %b" EQ false then %a NE %b |
| 440 | // "setle/ge int %a, %b" NE true then %a NE %b |
| 441 | if (ConstantBool *CB = dyn_cast<ConstantBool>(V1)) { |
| 442 | if (CB->getValue() ^ Opcode==EQ) |
| 443 | addNotEqual(BO->getOperand(0), BO->getOperand(1)); |
| 444 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 445 | break; |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 446 | case Instruction::And: { |
| 447 | // "and int %a, %b" EQ 0xff then %a EQ 0xff and %b EQ 0xff |
| 448 | // "and bool %a, %b" EQ true then %a EQ true and %b EQ true |
| 449 | // "and bool %a, %b" NE false then %a EQ true and %b EQ true |
| 450 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1)) { |
| 451 | if (Opcode == EQ && CI->isAllOnesValue()) { |
| 452 | addEqual(CI, BO->getOperand(0)); |
| 453 | addEqual(CI, BO->getOperand(1)); |
| 454 | } else if (Opcode == NE && CI == ConstantBool::getFalse()) { |
| 455 | addEqual(ConstantBool::getTrue(), BO->getOperand(0)); |
| 456 | addEqual(ConstantBool::getTrue(), BO->getOperand(1)); |
| 457 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 458 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 459 | } break; |
| 460 | case Instruction::Or: { |
| 461 | // "or int %a, %b" EQ 0 then %a EQ 0 and %b EQ 0 |
| 462 | // "or bool %a, %b" EQ false then %a EQ false and %b EQ false |
| 463 | // "or bool %a, %b" NE true then %a EQ false and %b EQ false |
| 464 | if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1)) { |
| 465 | if (Opcode == EQ && CI->isNullValue()) { |
| 466 | addEqual(CI, BO->getOperand(0)); |
| 467 | addEqual(CI, BO->getOperand(1)); |
| 468 | } else if (Opcode == NE && CI == ConstantBool::getTrue()) { |
| 469 | addEqual(ConstantBool::getFalse(), BO->getOperand(0)); |
| 470 | addEqual(ConstantBool::getFalse(), BO->getOperand(1)); |
| 471 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 472 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 473 | } break; |
| 474 | case Instruction::Xor: { |
| 475 | // "xor bool true, %a" EQ true then %a = false |
| 476 | // "xor bool true, %a" EQ false then %a = true |
| 477 | // "xor bool false, %a" EQ true then %a = true |
| 478 | // "xor bool false, %a" EQ false then %a = false |
| 479 | // 1. Repeat all of the above, with "NE false" in place of |
| 480 | // "EQ true" and "NE true" in place of "EQ false". |
| 481 | // "xor int %c, %a" EQ %c then %a = 0 |
| 482 | // "xor int %c, %a" NE %c then %a != 0 |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 483 | // 2. Repeat all of the above, with order of operands reversed. |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 484 | |
| 485 | Value *LHS = BO->getOperand(0), *RHS = BO->getOperand(1); |
| 486 | if (!isa<Constant>(LHS)) std::swap(LHS, RHS); |
| 487 | |
| 488 | if (ConstantBool *CB = dyn_cast<ConstantBool>(V1)) { |
| 489 | if (ConstantBool *A = dyn_cast<ConstantBool>(LHS)) { |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 490 | addEqual(RHS, ConstantBool::get(A->getValue() ^ CB->getValue() |
| 491 | ^ Opcode==NE)); |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 492 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 493 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 494 | else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1)) { |
| 495 | if (ConstantIntegral *A = dyn_cast<ConstantIntegral>(LHS)) { |
| 496 | if (A == CI) |
| 497 | add(Opcode, RHS, Constant::getNullValue(A->getType()), false); |
| 498 | } |
Chris Lattner | ef2aa19 | 2006-10-24 00:36:21 +0000 | [diff] [blame] | 499 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 500 | } break; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 501 | default: |
| 502 | break; |
| 503 | } |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 504 | } else if (SelectInst *SI = dyn_cast<SelectInst>(V2)) { |
Chris Lattner | 47811b7 | 2006-09-28 23:35:22 +0000 | [diff] [blame] | 505 | ConstantBool *True = ConstantBool::get(Opcode==EQ), |
| 506 | *False = ConstantBool::get(Opcode!=EQ); |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 507 | |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 508 | if (V1 == canonicalize(SI->getTrueValue())) |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 509 | addEqual(SI->getCondition(), True); |
Nick Lewycky | 7043d00 | 2006-10-26 02:35:18 +0000 | [diff] [blame] | 510 | else if (V1 == canonicalize(SI->getFalseValue())) |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 511 | addEqual(SI->getCondition(), False); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 512 | } |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 513 | |
| 514 | std::list<Value *> WorkList; |
| 515 | addToResolve(V1, WorkList); |
| 516 | addToResolve(V2, WorkList); |
| 517 | resolve(WorkList); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 520 | DominatorTree *DT; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 521 | public: |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 522 | #ifdef DEBUG |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 523 | void debug(std::ostream &os) const { |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 524 | static const char *OpcodeTable[] = { "EQ", "NE" }; |
| 525 | |
| 526 | union_find.debug(os); |
| 527 | for (std::vector<Property>::const_iterator I = Properties.begin(), |
| 528 | E = Properties.end(); I != E; ++I) { |
| 529 | os << (*I).I1 << " " << OpcodeTable[(*I).Opcode] << " " |
| 530 | << (*I).I2 << "\n"; |
Nick Lewycky | 6e39c7e | 2006-08-31 00:39:16 +0000 | [diff] [blame] | 531 | } |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 532 | os << "\n"; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 533 | } |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 534 | #endif |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 535 | |
| 536 | std::vector<Property> Properties; |
| 537 | }; |
| 538 | |
| 539 | /// PredicateSimplifier - This class is a simplifier that replaces |
| 540 | /// one equivalent variable with another. It also tracks what |
| 541 | /// can't be equal and will solve setcc instructions when possible. |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 542 | class PredicateSimplifier : public FunctionPass { |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 543 | public: |
| 544 | bool runOnFunction(Function &F); |
| 545 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 546 | |
| 547 | private: |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 548 | /// Forwards - Adds new properties into PropertySet and uses them to |
| 549 | /// simplify instructions. Because new properties sometimes apply to |
| 550 | /// a transition from one BasicBlock to another, this will use the |
| 551 | /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the |
| 552 | /// basic block with the new PropertySet. |
| 553 | class Forwards : public InstVisitor<Forwards> { |
| 554 | friend class InstVisitor<Forwards>; |
| 555 | PredicateSimplifier *PS; |
| 556 | public: |
| 557 | PropertySet &KP; |
| 558 | |
| 559 | Forwards(PredicateSimplifier *PS, PropertySet &KP) : PS(PS), KP(KP) {} |
| 560 | |
| 561 | // Tries to simplify each Instruction and add new properties to |
| 562 | // the PropertySet. Returns true if it erase the instruction. |
| 563 | //void visitInstruction(Instruction *I); |
| 564 | |
| 565 | void visitTerminatorInst(TerminatorInst &TI); |
| 566 | void visitBranchInst(BranchInst &BI); |
| 567 | void visitSwitchInst(SwitchInst &SI); |
| 568 | |
Nick Lewycky | 802fe27 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 569 | void visitAllocaInst(AllocaInst &AI); |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 570 | void visitLoadInst(LoadInst &LI); |
| 571 | void visitStoreInst(StoreInst &SI); |
| 572 | void visitBinaryOperator(BinaryOperator &BO); |
| 573 | }; |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 574 | |
| 575 | // Used by terminator instructions to proceed from the current basic |
| 576 | // block to the next. Verifies that "current" dominates "next", |
| 577 | // then calls visitBasicBlock. |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 578 | void proceedToSuccessors(PropertySet &CurrentPS, BasicBlock *Current); |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 579 | void proceedToSuccessor(PropertySet &Properties, BasicBlock *Next); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 580 | |
| 581 | // Visits each instruction in the basic block. |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 582 | void visitBasicBlock(BasicBlock *Block, PropertySet &KnownProperties); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 583 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 584 | // Tries to simplify each Instruction and add new properties to |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 585 | // the PropertySet. |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 586 | void visitInstruction(Instruction *I, PropertySet &); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 587 | |
| 588 | DominatorTree *DT; |
| 589 | bool modified; |
| 590 | }; |
| 591 | |
| 592 | RegisterPass<PredicateSimplifier> X("predsimplify", |
| 593 | "Predicate Simplifier"); |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 594 | |
| 595 | template <typename ElemTy> |
| 596 | typename Synonyms<ElemTy>::iterator |
| 597 | Synonyms<ElemTy>::unionSets(ElemTy E1, ElemTy E2) { |
| 598 | PS->order(E1, E2); |
| 599 | |
| 600 | iterator I1 = findLeader(E1), |
| 601 | I2 = findLeader(E2); |
| 602 | |
| 603 | if (!I1 && !I2) { // neither entry is in yet |
| 604 | leaders.push_back(E1); |
| 605 | I1 = leaders.size(); |
| 606 | mapping[E1] = I1; |
| 607 | mapping[E2] = I1; |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | if (!I1 && I2) { |
| 612 | mapping[E1] = I2; |
| 613 | std::swap(getLeader(I2), E1); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | if (I1 && !I2) { |
| 618 | mapping[E2] = I1; |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | if (I1 == I2) return 0; |
| 623 | |
| 624 | // This is the case where we have two sets, [%a1, %a2, %a3] and |
| 625 | // [%p1, %p2, %p3] and someone says that %a2 == %p3. We need to |
| 626 | // combine the two synsets. |
| 627 | |
| 628 | if (I1 > I2) --I1; |
| 629 | |
| 630 | for (std::map<Value *, unsigned>::iterator I = mapping.begin(), |
| 631 | E = mapping.end(); I != E; ++I) { |
| 632 | if (I->second == I2) I->second = I1; |
| 633 | else if (I->second > I2) --I->second; |
| 634 | } |
| 635 | |
| 636 | leaders.erase(leaders.begin() + I2 - 1); |
| 637 | |
| 638 | return I2; |
| 639 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | FunctionPass *llvm::createPredicateSimplifierPass() { |
| 643 | return new PredicateSimplifier(); |
| 644 | } |
| 645 | |
| 646 | bool PredicateSimplifier::runOnFunction(Function &F) { |
| 647 | DT = &getAnalysis<DominatorTree>(); |
| 648 | |
| 649 | modified = false; |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 650 | PropertySet KnownProperties(DT); |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 651 | visitBasicBlock(DT->getRootNode()->getBlock(), KnownProperties); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 652 | return modified; |
| 653 | } |
| 654 | |
| 655 | void PredicateSimplifier::getAnalysisUsage(AnalysisUsage &AU) const { |
Nick Lewycky | 5c8c5d9 | 2006-10-03 14:52:23 +0000 | [diff] [blame] | 656 | AU.addRequiredID(BreakCriticalEdgesID); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 657 | AU.addRequired<DominatorTree>(); |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 658 | AU.setPreservesCFG(); |
Nick Lewycky | 5c8c5d9 | 2006-10-03 14:52:23 +0000 | [diff] [blame] | 659 | AU.addPreservedID(BreakCriticalEdgesID); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 662 | void PredicateSimplifier::visitBasicBlock(BasicBlock *BB, |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 663 | PropertySet &KnownProperties) { |
Nick Lewycky | cf2112a | 2006-09-13 18:55:37 +0000 | [diff] [blame] | 664 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 665 | visitInstruction(I++, KnownProperties); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 669 | void PredicateSimplifier::visitInstruction(Instruction *I, |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 670 | PropertySet &KnownProperties) { |
Nick Lewycky | dc08cd5 | 2006-09-10 02:27:07 +0000 | [diff] [blame] | 671 | // Try to replace the whole instruction. |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 672 | Value *V = KnownProperties.canonicalize(I); |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 673 | if (V != I) { |
| 674 | modified = true; |
| 675 | ++NumInstruction; |
Nick Lewycky | 406fc0c | 2006-09-20 17:04:01 +0000 | [diff] [blame] | 676 | DEBUG(std::cerr << "Removing " << *I); |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 677 | KnownProperties.remove(I); |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 678 | I->replaceAllUsesWith(V); |
Nick Lewycky | cf2112a | 2006-09-13 18:55:37 +0000 | [diff] [blame] | 679 | I->eraseFromParent(); |
Nick Lewycky | a3a68bd | 2006-09-02 19:40:38 +0000 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
| 683 | // Try to substitute operands. |
| 684 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 685 | Value *Oper = I->getOperand(i); |
Nick Lewycky | e63bf95 | 2006-10-25 23:48:24 +0000 | [diff] [blame] | 686 | Value *V = KnownProperties.canonicalize(Oper); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 687 | if (V != Oper) { |
| 688 | modified = true; |
| 689 | ++NumVarsReplaced; |
Nick Lewycky | 8f389d8 | 2006-09-23 15:13:08 +0000 | [diff] [blame] | 690 | DEBUG(std::cerr << "Resolving " << *I); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 691 | I->setOperand(i, V); |
| 692 | DEBUG(std::cerr << "into " << *I); |
| 693 | } |
| 694 | } |
| 695 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 696 | Forwards visit(this, KnownProperties); |
| 697 | visit.visit(*I); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 700 | void PredicateSimplifier::proceedToSuccessors(PropertySet &KP, |
| 701 | BasicBlock *BBCurrent) { |
| 702 | DTNodeType *Current = DT->getNode(BBCurrent); |
| 703 | for (DTNodeType::iterator I = Current->begin(), E = Current->end(); |
| 704 | I != E; ++I) { |
| 705 | PropertySet Copy(KP); |
| 706 | visitBasicBlock((*I)->getBlock(), Copy); |
| 707 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 710 | void PredicateSimplifier::proceedToSuccessor(PropertySet &KP, BasicBlock *BB) { |
| 711 | visitBasicBlock(BB, KP); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 714 | void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) { |
| 715 | PS->proceedToSuccessors(KP, TI.getParent()); |
| 716 | } |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 717 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 718 | void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) { |
| 719 | BasicBlock *BB = BI.getParent(); |
| 720 | |
| 721 | if (BI.isUnconditional()) { |
| 722 | PS->proceedToSuccessors(KP, BB); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 726 | Value *Condition = BI.getCondition(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 727 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 728 | BasicBlock *TrueDest = BI.getSuccessor(0), |
| 729 | *FalseDest = BI.getSuccessor(1); |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 730 | |
Nick Lewycky | f938099 | 2006-10-03 17:36:01 +0000 | [diff] [blame] | 731 | if (isa<ConstantBool>(Condition) || TrueDest == FalseDest) { |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 732 | PS->proceedToSuccessors(KP, BB); |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 736 | DTNodeType *Node = PS->DT->getNode(BB); |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 737 | for (DTNodeType::iterator I = Node->begin(), E = Node->end(); I != E; ++I) { |
Nick Lewycky | f938099 | 2006-10-03 17:36:01 +0000 | [diff] [blame] | 738 | BasicBlock *Dest = (*I)->getBlock(); |
| 739 | PropertySet DestProperties(KP); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 740 | |
Nick Lewycky | f938099 | 2006-10-03 17:36:01 +0000 | [diff] [blame] | 741 | if (Dest == TrueDest) |
| 742 | DestProperties.addEqual(ConstantBool::getTrue(), Condition); |
| 743 | else if (Dest == FalseDest) |
| 744 | DestProperties.addEqual(ConstantBool::getFalse(), Condition); |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 745 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 746 | PS->proceedToSuccessor(DestProperties, Dest); |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 747 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 750 | void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) { |
| 751 | Value *Condition = SI.getCondition(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 752 | |
| 753 | // Set the EQProperty in each of the cases BBs, |
| 754 | // and the NEProperties in the default BB. |
| 755 | PropertySet DefaultProperties(KP); |
| 756 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 757 | DTNodeType *Node = PS->DT->getNode(SI.getParent()); |
Nick Lewycky | 025f4c0 | 2006-09-18 21:09:35 +0000 | [diff] [blame] | 758 | for (DTNodeType::iterator I = Node->begin(), E = Node->end(); I != E; ++I) { |
| 759 | BasicBlock *BB = (*I)->getBlock(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 760 | |
Nick Lewycky | a73a654 | 2006-10-03 15:19:11 +0000 | [diff] [blame] | 761 | PropertySet BBProperties(KP); |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 762 | if (BB == SI.getDefaultDest()) { |
| 763 | for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i) |
| 764 | if (SI.getSuccessor(i) != BB) |
| 765 | BBProperties.addNotEqual(Condition, SI.getCaseValue(i)); |
| 766 | } else if (ConstantInt *CI = SI.findCaseDest(BB)) { |
Nick Lewycky | a73a654 | 2006-10-03 15:19:11 +0000 | [diff] [blame] | 767 | BBProperties.addEqual(Condition, CI); |
| 768 | } |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 769 | PS->proceedToSuccessor(BBProperties, BB); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 770 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Nick Lewycky | 802fe27 | 2006-10-22 19:53:27 +0000 | [diff] [blame] | 773 | void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) { |
| 774 | KP.addNotEqual(Constant::getNullValue(AI.getType()), &AI); |
| 775 | } |
| 776 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 777 | void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) { |
| 778 | Value *Ptr = LI.getPointerOperand(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 779 | KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr); |
| 780 | } |
| 781 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 782 | void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) { |
| 783 | Value *Ptr = SI.getPointerOperand(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 784 | KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr); |
| 785 | } |
| 786 | |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 787 | void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) { |
| 788 | Instruction::BinaryOps ops = BO.getOpcode(); |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 789 | |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 790 | switch (ops) { |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame^] | 791 | case Instruction::UDiv: |
| 792 | case Instruction::SDiv: |
| 793 | case Instruction::FDiv: |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 794 | case Instruction::Rem: { |
Nick Lewycky | 078ff41 | 2006-10-12 02:02:44 +0000 | [diff] [blame] | 795 | Value *Divisor = BO.getOperand(1); |
Nick Lewycky | 3947a76 | 2006-08-30 02:46:48 +0000 | [diff] [blame] | 796 | KP.addNotEqual(Constant::getNullValue(Divisor->getType()), Divisor); |
| 797 | break; |
| 798 | } |
| 799 | default: |
| 800 | break; |
| 801 | } |
Nick Lewycky | 05450ae | 2006-08-28 22:44:55 +0000 | [diff] [blame] | 802 | } |