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