d1fe10668c1c042291ec4a06204f8379a8534ede
[oota-llvm.git] / lib / Transforms / Scalar / PredicateSimplifier.cpp
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
26 // conditional that assures us of that fact. Properties are stored as
27 // relationships between two values.
28 //
29 //===------------------------------------------------------------------===//
30
31 #define DEBUG_TYPE "predsimplify"
32 #include "llvm/Transforms/Scalar.h"
33 #include "llvm/Constants.h"
34 #include "llvm/DerivedTypes.h"
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"
42 #include "llvm/Support/InstVisitor.h"
43 #include <iostream>
44 using namespace llvm;
45
46 typedef DominatorTree::Node DTNodeType;
47
48 namespace {
49   Statistic<>
50   NumVarsReplaced("predsimplify", "Number of argument substitutions");
51   Statistic<>
52   NumInstruction("predsimplify", "Number of instructions removed");
53
54   class PropertySet;
55
56   /// Similar to EquivalenceClasses, this stores the set of equivalent
57   /// types. Beyond EquivalenceClasses, it allows us to specify which
58   /// element will act as leader.
59   template<typename ElemTy>
60   class VISIBILITY_HIDDEN Synonyms {
61     std::map<ElemTy, unsigned> mapping;
62     std::vector<ElemTy> leaders;
63     PropertySet *PS;
64
65   public:
66     typedef unsigned iterator;
67     typedef const unsigned const_iterator;
68
69     Synonyms(PropertySet *PS) : PS(PS) {}
70
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) {
93       assert(I && I <= leaders.size() && "Illegal leader to get.");
94       return leaders[I-1];
95     }
96
97     const ElemTy &getLeader(const_iterator I) const {
98       assert(I && I <= leaders.size() && "Illegal leaders to get.");
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) {
105         os << i << ". " << *getLeader(i) << ": [";
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 << "  ";
110           }
111         }
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
122     /// points to the no longer existing element.
123     iterator unionSets(ElemTy E1, ElemTy E2);
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
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 {
141     /// Returns true if V1 is a better choice than V2.
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
170     struct Property;
171   public:
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
180     Synonyms<Value *> union_find;
181
182     typedef std::vector<Property>::iterator       PropertyIterator;
183     typedef std::vector<Property>::const_iterator ConstPropertyIterator;
184     typedef Synonyms<Value *>::iterator  SynonymIterator;
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 {
197       SynonymIterator SI = union_find.findLeader(V);
198       if (!SI) return NULL;
199       return union_find.getLeader(SI);
200     }
201
202     bool empty() const {
203       return union_find.empty();
204     }
205
206     void addEqual(Value *V1, Value *V2) {
207       // If %x = 0. and %y = -0., seteq %x, %y is true, but
208       // copysign(%x) is not the same as copysign(%y).
209       if (V1->getType()->isFloatingPoint()) return;
210
211       order(V1, V2);
212       if (isa<Constant>(V2)) return; // refuse to set false == true.
213
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       }
226       addImpliedProperties(EQ, V1, V2);
227     }
228
229     void addNotEqual(Value *V1, Value *V2) {
230       // If %x = NAN then seteq %x, %x is false.
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;
235
236       if (findProperty(NE, V1, V2) != Properties.end())
237         return; // found.
238
239       // Add the property.
240       SynonymIterator I1 = union_find.findOrInsert(V1),
241                       I2 = union_find.findOrInsert(V2);
242
243       // Technically this means that the block is unreachable.
244       if (I1 == I2) return;
245
246       Properties.push_back(Property(NE, I1, I2));
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
254       SynonymIterator I1 = union_find.findLeader(V1),
255                       I2 = union_find.findLeader(V2);
256       if (!I1 || !I2) return Properties.end();
257
258       return
259       find(Properties.begin(), Properties.end(), Property(Opcode, I1, I2));
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
267       SynonymIterator I1 = union_find.findLeader(V1),
268                       I2 = union_find.findLeader(V2);
269       if (!I1 || !I2) return Properties.end();
270
271       return
272       find(Properties.begin(), Properties.end(), Property(Opcode, I1, I2));
273     }
274
275   private:
276     // Represents Head OP [Tail1, Tail2, ...]
277     // For example: %x != %a, %x != %b.
278     struct VISIBILITY_HIDDEN Property {
279       typedef SynonymIterator Iter;
280
281       Property(Ops opcode, Iter i1, Iter i2)
282         : Opcode(opcode), I1(i1), I2(i2)
283       { assert(opcode != EQ && "Equality belongs in the synonym set, "
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       }
291
292       Ops Opcode;
293       Iter I1, I2;
294     };
295
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
311     // Finds the properties implied by an equivalence and adds them too.
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:
320           if (ConstantBool *V1CB = dyn_cast<ConstantBool>(V1))
321             add(Opcode, BO->getOperand(0), BO->getOperand(1),!V1CB->getValue());
322           break;
323         case Instruction::SetNE:
324           if (ConstantBool *V1CB = dyn_cast<ConstantBool>(V1))
325             add(Opcode, BO->getOperand(0), BO->getOperand(1), V1CB->getValue());
326           break;
327         case Instruction::SetLT:
328         case Instruction::SetGT:
329           if (V1 == ConstantBool::getTrue())
330             add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
331           break;
332         case Instruction::SetLE:
333         case Instruction::SetGE:
334           if (V1 == ConstantBool::getFalse())
335             add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
336           break;
337         case Instruction::And: {
338           ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1);
339           if (CI && CI->isAllOnesValue()) {
340             add(Opcode, V1, BO->getOperand(0), false);
341             add(Opcode, V1, BO->getOperand(1), false);
342           }
343         } break;
344         case Instruction::Or: {
345           ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1);
346           if (CI && CI->isNullValue()) {
347             add(Opcode, V1, BO->getOperand(0), false);
348             add(Opcode, V1, BO->getOperand(1), false);
349           }
350         } break;
351         case Instruction::Xor: {
352           if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1)) {
353             const Type *Ty = BO->getType();
354             if (CI->isAllOnesValue()) {
355               if (BO->getOperand(0) == V1)
356                 add(Opcode, Constant::getNullValue(Ty),
357                     BO->getOperand(1), false);
358               if (BO->getOperand(1) == V1)
359                 add(Opcode, Constant::getNullValue(Ty),
360                     BO->getOperand(0), false);
361             }
362             if (CI->isNullValue()) {
363               ConstantIntegral *Op0 =
364                   dyn_cast<ConstantIntegral>(BO->getOperand(0));
365               ConstantIntegral *Op1 =
366                   dyn_cast<ConstantIntegral>(BO->getOperand(1));
367               if (Op0 && Op0->isAllOnesValue())
368                 add(Opcode, ConstantIntegral::getAllOnesValue(Ty),
369                     BO->getOperand(1), false);
370               if (Op1 && Op1->isAllOnesValue())
371                 add(Opcode, ConstantIntegral::getAllOnesValue(Ty),
372                     BO->getOperand(0), false);
373             }
374           }
375         } break;
376         default:
377           break;
378         }
379       } else if (SelectInst *SI = dyn_cast<SelectInst>(V2)) {
380         if (Opcode != EQ && Opcode != NE) return;
381
382         ConstantBool *True  = ConstantBool::get(Opcode==EQ),
383                      *False = ConstantBool::get(Opcode!=EQ);
384
385         if (V1 == SI->getTrueValue())
386           addEqual(SI->getCondition(), True);
387         else if (V1 == SI->getFalseValue())
388           addEqual(SI->getCondition(), False);
389         else if (Opcode == EQ)
390           assert("Result of select not equal to either value.");
391       }
392     }
393
394     DominatorTree *DT;
395   public:
396 #ifdef DEBUG
397     void debug(std::ostream &os) const {
398       static const char *OpcodeTable[] = { "EQ", "NE" };
399
400       union_find.debug(os);
401       for (std::vector<Property>::const_iterator I = Properties.begin(),
402            E = Properties.end(); I != E; ++I) {
403         os << (*I).I1 << " " << OpcodeTable[(*I).Opcode] << " "
404            << (*I).I2 << "\n";
405       }
406       os << "\n";
407     }
408 #endif
409
410     std::vector<Property> Properties;
411   };
412
413   /// PredicateSimplifier - This class is a simplifier that replaces
414   /// one equivalent variable with another. It also tracks what
415   /// can't be equal and will solve setcc instructions when possible.
416   class PredicateSimplifier : public FunctionPass {
417   public:
418     bool runOnFunction(Function &F);
419     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
420
421   private:
422     /// Backwards - Try to replace the Use of the instruction with
423     /// something simpler. This resolves a value by walking backwards
424     /// through its definition and the operands of that definition to
425     /// see if any values can now be solved for with the properties
426     /// that are in effect now, but weren't at definition time.
427     class Backwards : public InstVisitor<Backwards, Value &> {
428       friend class InstVisitor<Backwards, Value &>;
429       const PropertySet &KP;
430
431       Value &visitSetCondInst(SetCondInst &SCI);
432       Value &visitBinaryOperator(BinaryOperator &BO);
433       Value &visitSelectInst(SelectInst &SI);
434       Value &visitInstruction(Instruction &I);
435
436     public:
437       explicit Backwards(const PropertySet &KP) : KP(KP) {}
438
439       Value *resolve(Value *V);
440     };
441
442     /// Forwards - Adds new properties into PropertySet and uses them to
443     /// simplify instructions. Because new properties sometimes apply to
444     /// a transition from one BasicBlock to another, this will use the
445     /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the
446     /// basic block with the new PropertySet.
447     class Forwards : public InstVisitor<Forwards> {
448       friend class InstVisitor<Forwards>;
449       PredicateSimplifier *PS;
450     public:
451       PropertySet &KP;
452
453       Forwards(PredicateSimplifier *PS, PropertySet &KP) : PS(PS), KP(KP) {}
454
455       // Tries to simplify each Instruction and add new properties to
456       // the PropertySet. Returns true if it erase the instruction.
457       //void visitInstruction(Instruction *I);
458
459       void visitTerminatorInst(TerminatorInst &TI);
460       void visitBranchInst(BranchInst &BI);
461       void visitSwitchInst(SwitchInst &SI);
462
463       void visitAllocaInst(AllocaInst &AI);
464       void visitLoadInst(LoadInst &LI);
465       void visitStoreInst(StoreInst &SI);
466       void visitBinaryOperator(BinaryOperator &BO);
467     };
468
469     // Used by terminator instructions to proceed from the current basic
470     // block to the next. Verifies that "current" dominates "next",
471     // then calls visitBasicBlock.
472     void proceedToSuccessors(PropertySet &CurrentPS, BasicBlock *Current);
473     void proceedToSuccessor(PropertySet &Properties, BasicBlock *Next);
474
475     // Visits each instruction in the basic block.
476     void visitBasicBlock(BasicBlock *Block, PropertySet &KnownProperties);
477
478     // Tries to simplify each Instruction and add new properties to
479     // the PropertySet.
480     void visitInstruction(Instruction *I, PropertySet &);
481
482     DominatorTree *DT;
483     bool modified;
484   };
485
486   RegisterPass<PredicateSimplifier> X("predsimplify",
487                                       "Predicate Simplifier");
488
489   template <typename ElemTy>
490   typename Synonyms<ElemTy>::iterator
491   Synonyms<ElemTy>::unionSets(ElemTy E1, ElemTy E2) {
492     PS->order(E1, E2);
493
494     iterator I1 = findLeader(E1),
495              I2 = findLeader(E2);
496
497     if (!I1 && !I2) { // neither entry is in yet
498       leaders.push_back(E1);
499       I1 = leaders.size();
500       mapping[E1] = I1;
501       mapping[E2] = I1;
502       return 0;
503     }
504
505     if (!I1 && I2) {
506       mapping[E1] = I2;
507       std::swap(getLeader(I2), E1);
508       return 0;
509     }
510
511     if (I1 && !I2) {
512       mapping[E2] = I1;
513       return 0;
514     }
515
516     if (I1 == I2) return 0;
517
518     // This is the case where we have two sets, [%a1, %a2, %a3] and
519     // [%p1, %p2, %p3] and someone says that %a2 == %p3. We need to
520     // combine the two synsets.
521
522     if (I1 > I2) --I1;
523
524     for (std::map<Value *, unsigned>::iterator I = mapping.begin(),
525          E = mapping.end(); I != E; ++I) {
526       if (I->second == I2) I->second = I1;
527       else if (I->second > I2) --I->second;
528     }
529
530     leaders.erase(leaders.begin() + I2 - 1);
531
532     return I2;
533   }
534 }
535
536 FunctionPass *llvm::createPredicateSimplifierPass() {
537   return new PredicateSimplifier();
538 }
539
540 bool PredicateSimplifier::runOnFunction(Function &F) {
541   DT = &getAnalysis<DominatorTree>();
542
543   modified = false;
544   PropertySet KnownProperties(DT);
545   visitBasicBlock(DT->getRootNode()->getBlock(), KnownProperties);
546   return modified;
547 }
548
549 void PredicateSimplifier::getAnalysisUsage(AnalysisUsage &AU) const {
550   AU.addRequiredID(BreakCriticalEdgesID);
551   AU.addRequired<DominatorTree>();
552   AU.setPreservesCFG();
553   AU.addPreservedID(BreakCriticalEdgesID);
554 }
555
556 Value &PredicateSimplifier::Backwards::visitSetCondInst(SetCondInst &SCI) {
557   Value &vBO = visitBinaryOperator(SCI);
558   if (&vBO !=  &SCI) return vBO;
559
560   Value *SCI0 = resolve(SCI.getOperand(0)),
561         *SCI1 = resolve(SCI.getOperand(1));
562
563   PropertySet::ConstPropertyIterator NE =
564       KP.findProperty(PropertySet::NE, SCI0, SCI1);
565
566   if (NE != KP.Properties.end()) {
567     switch (SCI.getOpcode()) {
568       case Instruction::SetEQ: return *ConstantBool::getFalse();
569       case Instruction::SetNE: return *ConstantBool::getTrue();
570       case Instruction::SetLE:
571       case Instruction::SetGE:
572       case Instruction::SetLT:
573       case Instruction::SetGT:
574         break;
575       default:
576         assert(0 && "Unknown opcode in SetCondInst.");
577         break;
578     }
579   }
580   return SCI;
581 }
582
583 Value &PredicateSimplifier::Backwards::visitBinaryOperator(BinaryOperator &BO) {
584   Value *V = KP.canonicalize(&BO);
585   if (V != &BO) return *V;
586
587   Value *lhs = resolve(BO.getOperand(0)),
588         *rhs = resolve(BO.getOperand(1));
589
590   ConstantIntegral *CI1 = dyn_cast<ConstantIntegral>(lhs),
591                    *CI2 = dyn_cast<ConstantIntegral>(rhs);
592
593   if (CI1 && CI2) return *ConstantExpr::get(BO.getOpcode(), CI1, CI2);
594
595   return BO;
596 }
597
598 Value &PredicateSimplifier::Backwards::visitSelectInst(SelectInst &SI) {
599   Value *V = KP.canonicalize(&SI);
600   if (V != &SI) return *V;
601
602   Value *Condition = resolve(SI.getCondition());
603   if (ConstantBool *CB = dyn_cast<ConstantBool>(Condition))
604     return *resolve(CB->getValue() ? SI.getTrueValue() : SI.getFalseValue());
605   return SI;
606 }
607
608 Value &PredicateSimplifier::Backwards::visitInstruction(Instruction &I) {
609   return *KP.canonicalize(&I);
610 }
611
612 Value *PredicateSimplifier::Backwards::resolve(Value *V) {
613   if (isa<Constant>(V) || isa<BasicBlock>(V) || KP.empty()) return V;
614
615   if (Instruction *I = dyn_cast<Instruction>(V)) return &visit(*I);
616   return KP.canonicalize(V);
617 }
618
619 void PredicateSimplifier::visitBasicBlock(BasicBlock *BB,
620                                           PropertySet &KnownProperties) {
621   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
622     visitInstruction(I++, KnownProperties);
623   }
624 }
625
626 void PredicateSimplifier::visitInstruction(Instruction *I,
627                                            PropertySet &KnownProperties) {
628   // Try to replace the whole instruction.
629   Backwards resolve(KnownProperties);
630   Value *V = resolve.resolve(I);
631   if (V != I) {
632     modified = true;
633     ++NumInstruction;
634     DEBUG(std::cerr << "Removing " << *I);
635     I->replaceAllUsesWith(V);
636     I->eraseFromParent();
637     return;
638   }
639
640   // Try to substitute operands.
641   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
642     Value *Oper = I->getOperand(i);
643     Value *V = resolve.resolve(Oper);
644     if (V != Oper) {
645       modified = true;
646       ++NumVarsReplaced;
647       DEBUG(std::cerr << "Resolving " << *I);
648       I->setOperand(i, V);
649       DEBUG(std::cerr << "into " << *I);
650     }
651   }
652
653   Forwards visit(this, KnownProperties);
654   visit.visit(*I);
655 }
656
657 void PredicateSimplifier::proceedToSuccessors(PropertySet &KP,
658                                               BasicBlock *BBCurrent) {
659   DTNodeType *Current = DT->getNode(BBCurrent);
660   for (DTNodeType::iterator I = Current->begin(), E = Current->end();
661        I != E; ++I) {
662     PropertySet Copy(KP);
663     visitBasicBlock((*I)->getBlock(), Copy);
664   }
665 }
666
667 void PredicateSimplifier::proceedToSuccessor(PropertySet &KP, BasicBlock *BB) {
668   visitBasicBlock(BB, KP);
669 }
670
671 void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) {
672   PS->proceedToSuccessors(KP, TI.getParent());
673 }
674
675 void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) {
676   BasicBlock *BB = BI.getParent();
677
678   if (BI.isUnconditional()) {
679     PS->proceedToSuccessors(KP, BB);
680     return;
681   }
682
683   Value *Condition = BI.getCondition();
684
685   BasicBlock *TrueDest  = BI.getSuccessor(0),
686              *FalseDest = BI.getSuccessor(1);
687
688   if (isa<ConstantBool>(Condition) || TrueDest == FalseDest) {
689     PS->proceedToSuccessors(KP, BB);
690     return;
691   }
692
693   DTNodeType *Node = PS->DT->getNode(BB);
694   for (DTNodeType::iterator I = Node->begin(), E = Node->end(); I != E; ++I) {
695     BasicBlock *Dest = (*I)->getBlock();
696     PropertySet DestProperties(KP);
697
698     if (Dest == TrueDest)
699       DestProperties.addEqual(ConstantBool::getTrue(), Condition);
700     else if (Dest == FalseDest)
701       DestProperties.addEqual(ConstantBool::getFalse(), Condition);
702
703     PS->proceedToSuccessor(DestProperties, Dest);
704   }
705 }
706
707 void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) {
708   Value *Condition = SI.getCondition();
709
710   // Set the EQProperty in each of the cases BBs,
711   // and the NEProperties in the default BB.
712   PropertySet DefaultProperties(KP);
713
714   DTNodeType *Node = PS->DT->getNode(SI.getParent());
715   for (DTNodeType::iterator I = Node->begin(), E = Node->end(); I != E; ++I) {
716     BasicBlock *BB = (*I)->getBlock();
717
718     PropertySet BBProperties(KP);
719     if (BB == SI.getDefaultDest()) {
720       for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i)
721         if (SI.getSuccessor(i) != BB)
722           BBProperties.addNotEqual(Condition, SI.getCaseValue(i));
723     } else if (ConstantInt *CI = SI.findCaseDest(BB)) {
724       BBProperties.addEqual(Condition, CI);
725     }
726     PS->proceedToSuccessor(BBProperties, BB);
727   }
728 }
729
730 void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
731   KP.addNotEqual(Constant::getNullValue(AI.getType()), &AI);
732 }
733
734 void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) {
735   Value *Ptr = LI.getPointerOperand();
736   KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
737 }
738
739 void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) {
740   Value *Ptr = SI.getPointerOperand();
741   KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
742 }
743
744 void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) {
745   Instruction::BinaryOps ops = BO.getOpcode();
746
747   switch (ops) {
748     case Instruction::Div:
749     case Instruction::Rem: {
750       Value *Divisor = BO.getOperand(1);
751       KP.addNotEqual(Constant::getNullValue(Divisor->getType()), Divisor);
752       break;
753     }
754     default:
755       break;
756   }
757 }