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