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