1 //===-- PredicateSimplifier.cpp - Path Sensitive Simplifier -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===------------------------------------------------------------------===//
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:
14 // void test(int *p, int *q)
20 // foo(); // unreachable
23 //===------------------------------------------------------------------===//
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.
29 //===------------------------------------------------------------------===//
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"
47 typedef DominatorTree::Node DTNodeType;
51 NumVarsReplaced("predsimplify", "Number of argument substitutions");
53 NumInstruction("predsimplify", "Number of instructions removed");
57 /// Similar to EquivalenceClasses, this stores the set of equivalent
58 /// types. Beyond EquivalenceClasses, it allows us to specify which
59 /// element will act as leader.
60 template<typename ElemTy>
61 class VISIBILITY_HIDDEN Synonyms {
62 std::map<ElemTy, unsigned> mapping;
63 std::vector<ElemTy> leaders;
67 typedef unsigned iterator;
68 typedef const unsigned const_iterator;
70 Synonyms(PropertySet *PS) : PS(PS) {}
75 return leaders.empty();
78 iterator findLeader(ElemTy &e) {
79 typename std::map<ElemTy, unsigned>::iterator MI = mapping.find(e);
80 if (MI == mapping.end()) return 0;
85 const_iterator findLeader(ElemTy &e) const {
86 typename std::map<ElemTy, unsigned>::const_iterator MI =
88 if (MI == mapping.end()) return 0;
93 ElemTy &getLeader(iterator I) {
94 assert(I && I <= leaders.size() && "Illegal leader to get.");
98 const ElemTy &getLeader(const_iterator I) const {
99 assert(I && I <= leaders.size() && "Illegal leaders to get.");
104 void debug(std::ostream &os) const {
105 for (unsigned i = 1, e = leaders.size()+1; i != e; ++i) {
106 os << i << ". " << *getLeader(i) << ": [";
107 for (std::map<Value *, unsigned>::const_iterator
108 I = mapping.begin(), E = mapping.end(); I != E; ++I) {
109 if ((*I).second == i && (*I).first != leaders[i-1]) {
110 os << *(*I).first << " ";
120 void remove(ElemTy &e) {
121 ElemTy E = e; // The parameter to erase must not be a reference to
122 mapping.erase(E); // an element contained in the map.
125 /// Combine two sets referring to the same element, inserting the
126 /// elements as needed. Returns a valid iterator iff two already
127 /// existing disjoint synonym sets were combined. The iterator
128 /// points to the no longer existing element.
129 iterator unionSets(ElemTy E1, ElemTy E2);
131 /// Returns an iterator pointing to the synonym set containing
132 /// element e. If none exists, a new one is created and returned.
133 iterator findOrInsert(ElemTy &e) {
134 iterator I = findLeader(e);
137 leaders.push_back(e);
144 /// Represents the set of equivalent Value*s and provides insertion
145 /// and fast lookup. Also stores the set of inequality relationships.
147 /// Returns true if V1 is a better choice than V2.
148 bool compare(Value *V1, Value *V2) const {
149 if (isa<Constant>(V1)) {
150 if (!isa<Constant>(V2)) {
153 } else if (isa<Argument>(V1)) {
154 if (!isa<Constant>(V2) && !isa<Argument>(V2)) {
158 if (Instruction *I1 = dyn_cast<Instruction>(V1)) {
159 if (Instruction *I2 = dyn_cast<Instruction>(V2)) {
160 BasicBlock *BB1 = I1->getParent(),
161 *BB2 = I2->getParent();
163 for (BasicBlock::const_iterator I = BB1->begin(), E = BB1->end();
165 if (&*I == I1) return true;
166 if (&*I == I2) return false;
168 assert(0 && "Instructions not found in parent BasicBlock?");
170 return DT->getNode(BB1)->properlyDominates(DT->getNode(BB2));
178 /// Choose the canonical Value in a synonym set.
179 /// Leaves the more canonical choice in V1.
180 void order(Value *&V1, Value *&V2) const {
181 if (compare(V2, V1)) std::swap(V1, V2);
184 PropertySet(DominatorTree *DT) : union_find(this), DT(DT) {}
186 Synonyms<Value *> union_find;
188 typedef std::vector<Property>::iterator PropertyIterator;
189 typedef std::vector<Property>::const_iterator ConstPropertyIterator;
190 typedef Synonyms<Value *>::iterator SynonymIterator;
197 Value *canonicalize(Value *V) const {
198 Value *C = lookup(V);
202 Value *lookup(Value *V) const {
203 SynonymIterator SI = union_find.findLeader(V);
204 if (!SI) return NULL;
205 return union_find.getLeader(SI);
209 return union_find.empty();
212 void remove(Value *V) {
213 SynonymIterator I = union_find.findLeader(V);
216 union_find.remove(V);
218 for (PropertyIterator PI = Properties.begin(), PE = Properties.end();
221 if (P.I1 == I || P.I2 == I) Properties.erase(PI);
225 void addEqual(Value *V1, Value *V2) {
226 // If %x = 0. and %y = -0., seteq %x, %y is true, but
227 // copysign(%x) is not the same as copysign(%y).
228 if (V1->getType()->isFloatingPoint()) return;
231 if (isa<Constant>(V2)) return; // refuse to set false == true.
233 if (union_find.findLeader(V1) &&
234 union_find.findLeader(V1) == union_find.findLeader(V2))
237 SynonymIterator deleted = union_find.unionSets(V1, V2);
239 SynonymIterator replacement = union_find.findLeader(V1);
241 for (PropertyIterator I = Properties.begin(), E = Properties.end();
243 if (I->I1 == deleted) I->I1 = replacement;
244 else if (I->I1 > deleted) --I->I1;
245 if (I->I2 == deleted) I->I2 = replacement;
246 else if (I->I2 > deleted) --I->I2;
249 addImpliedProperties(EQ, V1, V2);
252 void addNotEqual(Value *V1, Value *V2) {
253 // If %x = NAN then seteq %x, %x is false.
254 if (V1->getType()->isFloatingPoint()) return;
256 // For example, %x = setne int 0, 0 causes "0 != 0".
257 if (isa<Constant>(V1) && isa<Constant>(V2)) return;
259 if (findProperty(NE, V1, V2) != Properties.end())
263 SynonymIterator I1 = union_find.findOrInsert(V1),
264 I2 = union_find.findOrInsert(V2);
266 // Technically this means that the block is unreachable.
267 if (I1 == I2) return;
269 Properties.push_back(Property(NE, I1, I2));
270 addImpliedProperties(NE, V1, V2);
273 PropertyIterator findProperty(Ops Opcode, Value *V1, Value *V2) {
274 assert(Opcode != EQ && "Can't findProperty on EQ."
275 "Use the lookup method instead.");
277 SynonymIterator I1 = union_find.findLeader(V1),
278 I2 = union_find.findLeader(V2);
279 if (!I1 || !I2) return Properties.end();
282 find(Properties.begin(), Properties.end(), Property(Opcode, I1, I2));
285 ConstPropertyIterator
286 findProperty(Ops Opcode, Value *V1, Value *V2) const {
287 assert(Opcode != EQ && "Can't findProperty on EQ."
288 "Use the lookup method instead.");
290 SynonymIterator I1 = union_find.findLeader(V1),
291 I2 = union_find.findLeader(V2);
292 if (!I1 || !I2) return Properties.end();
295 find(Properties.begin(), Properties.end(), Property(Opcode, I1, I2));
299 // Represents Head OP [Tail1, Tail2, ...]
300 // For example: %x != %a, %x != %b.
301 struct VISIBILITY_HIDDEN Property {
302 typedef SynonymIterator Iter;
304 Property(Ops opcode, Iter i1, Iter i2)
305 : Opcode(opcode), I1(i1), I2(i2)
306 { assert(opcode != EQ && "Equality belongs in the synonym set, "
307 "not a property."); }
309 bool operator==(const Property &P) const {
310 return (Opcode == P.Opcode) &&
311 ((I1 == P.I1 && I2 == P.I2) ||
312 (I1 == P.I2 && I2 == P.I1));
319 void add(Ops Opcode, Value *V1, Value *V2, bool invert) {
322 if (invert) addNotEqual(V1, V2);
323 else addEqual(V1, V2);
326 if (invert) addEqual(V1, V2);
327 else addNotEqual(V1, V2);
330 assert(0 && "Unknown property opcode.");
334 void addToResolve(Value *V, std::list<Value *> &WorkList) {
335 if (!isa<Constant>(V) && !isa<BasicBlock>(V)) {
336 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end();
338 if (!isa<Constant>(*UI) && !isa<BasicBlock>(*UI)) {
339 WorkList.push_back(*UI);
345 void resolve(std::list<Value *> &WorkList) {
346 if (WorkList.empty()) return;
348 Value *V = WorkList.front();
349 WorkList.pop_front();
353 Instruction *I = dyn_cast<Instruction>(V);
356 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
357 Value *lhs = canonicalize(BO->getOperand(0)),
358 *rhs = canonicalize(BO->getOperand(1));
360 ConstantIntegral *CI1 = dyn_cast<ConstantIntegral>(lhs),
361 *CI2 = dyn_cast<ConstantIntegral>(rhs);
364 addToResolve(BO, WorkList);
365 addEqual(BO, ConstantExpr::get(BO->getOpcode(), CI1, CI2));
366 } else if (SetCondInst *SCI = dyn_cast<SetCondInst>(BO)) {
367 PropertySet::ConstPropertyIterator NE =
368 findProperty(PropertySet::NE, lhs, rhs);
370 if (NE != Properties.end()) {
371 switch (SCI->getOpcode()) {
372 case Instruction::SetEQ:
373 addToResolve(SCI, WorkList);
374 addEqual(SCI, ConstantBool::getFalse());
376 case Instruction::SetNE:
377 addToResolve(SCI, WorkList);
378 addEqual(SCI, ConstantBool::getTrue());
380 case Instruction::SetLE:
381 case Instruction::SetGE:
382 case Instruction::SetLT:
383 case Instruction::SetGT:
386 assert(0 && "Unknown opcode in SetCondInst.");
391 } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
392 Value *Condition = canonicalize(SI->getCondition());
393 if (ConstantBool *CB = dyn_cast<ConstantBool>(Condition)) {
394 addToResolve(SI, WorkList);
395 addEqual(SI, CB->getValue() ? SI->getTrueValue() : SI->getFalseValue());
398 if (!WorkList.empty()) resolve(WorkList);
401 // Finds the properties implied by an equivalence and adds them too.
402 // Example: ("seteq %a, %b", true, EQ) --> (%a, %b, EQ)
403 // ("seteq %a, %b", false, EQ) --> (%a, %b, NE)
404 void addImpliedProperties(Ops Opcode, Value *V1, Value *V2) {
407 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V2)) {
408 switch (BO->getOpcode()) {
409 case Instruction::SetEQ:
410 if (ConstantBool *V1CB = dyn_cast<ConstantBool>(V1))
411 add(Opcode, BO->getOperand(0), BO->getOperand(1),!V1CB->getValue());
413 case Instruction::SetNE:
414 if (ConstantBool *V1CB = dyn_cast<ConstantBool>(V1))
415 add(Opcode, BO->getOperand(0), BO->getOperand(1), V1CB->getValue());
417 case Instruction::SetLT:
418 case Instruction::SetGT:
419 if (V1 == ConstantBool::getTrue())
420 add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
422 case Instruction::SetLE:
423 case Instruction::SetGE:
424 if (V1 == ConstantBool::getFalse())
425 add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
427 case Instruction::And: {
428 ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1);
429 if (CI && CI->isAllOnesValue()) {
430 add(Opcode, V1, BO->getOperand(0), false);
431 add(Opcode, V1, BO->getOperand(1), false);
434 case Instruction::Or: {
435 ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1);
436 if (CI && CI->isNullValue()) {
437 add(Opcode, V1, BO->getOperand(0), false);
438 add(Opcode, V1, BO->getOperand(1), false);
441 case Instruction::Xor: {
442 if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1)) {
443 const Type *Ty = BO->getType();
444 if (CI->isAllOnesValue()) {
445 if (BO->getOperand(0) == V1)
446 add(Opcode, Constant::getNullValue(Ty),
447 BO->getOperand(1), false);
448 if (BO->getOperand(1) == V1)
449 add(Opcode, Constant::getNullValue(Ty),
450 BO->getOperand(0), false);
452 if (CI->isNullValue()) {
453 ConstantIntegral *Op0 =
454 dyn_cast<ConstantIntegral>(BO->getOperand(0));
455 ConstantIntegral *Op1 =
456 dyn_cast<ConstantIntegral>(BO->getOperand(1));
457 if (Op0 && Op0->isAllOnesValue())
458 add(Opcode, ConstantIntegral::getAllOnesValue(Ty),
459 BO->getOperand(1), false);
460 if (Op1 && Op1->isAllOnesValue())
461 add(Opcode, ConstantIntegral::getAllOnesValue(Ty),
462 BO->getOperand(0), false);
469 } else if (SelectInst *SI = dyn_cast<SelectInst>(V2)) {
470 if (Opcode != EQ && Opcode != NE) return;
472 ConstantBool *True = ConstantBool::get(Opcode==EQ),
473 *False = ConstantBool::get(Opcode!=EQ);
475 if (V1 == SI->getTrueValue())
476 addEqual(SI->getCondition(), True);
477 else if (V1 == SI->getFalseValue())
478 addEqual(SI->getCondition(), False);
479 else if (Opcode == EQ)
480 assert("Result of select not equal to either value.");
483 std::list<Value *> WorkList;
484 addToResolve(V1, WorkList);
485 addToResolve(V2, WorkList);
492 void debug(std::ostream &os) const {
493 static const char *OpcodeTable[] = { "EQ", "NE" };
495 union_find.debug(os);
496 for (std::vector<Property>::const_iterator I = Properties.begin(),
497 E = Properties.end(); I != E; ++I) {
498 os << (*I).I1 << " " << OpcodeTable[(*I).Opcode] << " "
505 std::vector<Property> Properties;
508 /// PredicateSimplifier - This class is a simplifier that replaces
509 /// one equivalent variable with another. It also tracks what
510 /// can't be equal and will solve setcc instructions when possible.
511 class PredicateSimplifier : public FunctionPass {
513 bool runOnFunction(Function &F);
514 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
517 /// Forwards - Adds new properties into PropertySet and uses them to
518 /// simplify instructions. Because new properties sometimes apply to
519 /// a transition from one BasicBlock to another, this will use the
520 /// PredicateSimplifier::proceedToSuccessor(s) interface to enter the
521 /// basic block with the new PropertySet.
522 class Forwards : public InstVisitor<Forwards> {
523 friend class InstVisitor<Forwards>;
524 PredicateSimplifier *PS;
528 Forwards(PredicateSimplifier *PS, PropertySet &KP) : PS(PS), KP(KP) {}
530 // Tries to simplify each Instruction and add new properties to
531 // the PropertySet. Returns true if it erase the instruction.
532 //void visitInstruction(Instruction *I);
534 void visitTerminatorInst(TerminatorInst &TI);
535 void visitBranchInst(BranchInst &BI);
536 void visitSwitchInst(SwitchInst &SI);
538 void visitAllocaInst(AllocaInst &AI);
539 void visitLoadInst(LoadInst &LI);
540 void visitStoreInst(StoreInst &SI);
541 void visitBinaryOperator(BinaryOperator &BO);
544 // Used by terminator instructions to proceed from the current basic
545 // block to the next. Verifies that "current" dominates "next",
546 // then calls visitBasicBlock.
547 void proceedToSuccessors(PropertySet &CurrentPS, BasicBlock *Current);
548 void proceedToSuccessor(PropertySet &Properties, BasicBlock *Next);
550 // Visits each instruction in the basic block.
551 void visitBasicBlock(BasicBlock *Block, PropertySet &KnownProperties);
553 // Tries to simplify each Instruction and add new properties to
555 void visitInstruction(Instruction *I, PropertySet &);
561 RegisterPass<PredicateSimplifier> X("predsimplify",
562 "Predicate Simplifier");
564 template <typename ElemTy>
565 typename Synonyms<ElemTy>::iterator
566 Synonyms<ElemTy>::unionSets(ElemTy E1, ElemTy E2) {
569 iterator I1 = findLeader(E1),
572 if (!I1 && !I2) { // neither entry is in yet
573 leaders.push_back(E1);
582 std::swap(getLeader(I2), E1);
591 if (I1 == I2) return 0;
593 // This is the case where we have two sets, [%a1, %a2, %a3] and
594 // [%p1, %p2, %p3] and someone says that %a2 == %p3. We need to
595 // combine the two synsets.
599 for (std::map<Value *, unsigned>::iterator I = mapping.begin(),
600 E = mapping.end(); I != E; ++I) {
601 if (I->second == I2) I->second = I1;
602 else if (I->second > I2) --I->second;
605 leaders.erase(leaders.begin() + I2 - 1);
611 FunctionPass *llvm::createPredicateSimplifierPass() {
612 return new PredicateSimplifier();
615 bool PredicateSimplifier::runOnFunction(Function &F) {
616 DT = &getAnalysis<DominatorTree>();
619 PropertySet KnownProperties(DT);
620 visitBasicBlock(DT->getRootNode()->getBlock(), KnownProperties);
624 void PredicateSimplifier::getAnalysisUsage(AnalysisUsage &AU) const {
625 AU.addRequiredID(BreakCriticalEdgesID);
626 AU.addRequired<DominatorTree>();
627 AU.setPreservesCFG();
628 AU.addPreservedID(BreakCriticalEdgesID);
631 void PredicateSimplifier::visitBasicBlock(BasicBlock *BB,
632 PropertySet &KnownProperties) {
633 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
634 visitInstruction(I++, KnownProperties);
638 void PredicateSimplifier::visitInstruction(Instruction *I,
639 PropertySet &KnownProperties) {
640 // Try to replace the whole instruction.
641 Value *V = KnownProperties.canonicalize(I);
645 DEBUG(std::cerr << "Removing " << *I);
646 KnownProperties.remove(I);
647 I->replaceAllUsesWith(V);
648 I->eraseFromParent();
652 // Try to substitute operands.
653 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
654 Value *Oper = I->getOperand(i);
655 Value *V = KnownProperties.canonicalize(Oper);
659 DEBUG(std::cerr << "Resolving " << *I);
661 DEBUG(std::cerr << "into " << *I);
665 Forwards visit(this, KnownProperties);
669 void PredicateSimplifier::proceedToSuccessors(PropertySet &KP,
670 BasicBlock *BBCurrent) {
671 DTNodeType *Current = DT->getNode(BBCurrent);
672 for (DTNodeType::iterator I = Current->begin(), E = Current->end();
674 PropertySet Copy(KP);
675 visitBasicBlock((*I)->getBlock(), Copy);
679 void PredicateSimplifier::proceedToSuccessor(PropertySet &KP, BasicBlock *BB) {
680 visitBasicBlock(BB, KP);
683 void PredicateSimplifier::Forwards::visitTerminatorInst(TerminatorInst &TI) {
684 PS->proceedToSuccessors(KP, TI.getParent());
687 void PredicateSimplifier::Forwards::visitBranchInst(BranchInst &BI) {
688 BasicBlock *BB = BI.getParent();
690 if (BI.isUnconditional()) {
691 PS->proceedToSuccessors(KP, BB);
695 Value *Condition = BI.getCondition();
697 BasicBlock *TrueDest = BI.getSuccessor(0),
698 *FalseDest = BI.getSuccessor(1);
700 if (isa<ConstantBool>(Condition) || TrueDest == FalseDest) {
701 PS->proceedToSuccessors(KP, BB);
705 DTNodeType *Node = PS->DT->getNode(BB);
706 for (DTNodeType::iterator I = Node->begin(), E = Node->end(); I != E; ++I) {
707 BasicBlock *Dest = (*I)->getBlock();
708 PropertySet DestProperties(KP);
710 if (Dest == TrueDest)
711 DestProperties.addEqual(ConstantBool::getTrue(), Condition);
712 else if (Dest == FalseDest)
713 DestProperties.addEqual(ConstantBool::getFalse(), Condition);
715 PS->proceedToSuccessor(DestProperties, Dest);
719 void PredicateSimplifier::Forwards::visitSwitchInst(SwitchInst &SI) {
720 Value *Condition = SI.getCondition();
722 // Set the EQProperty in each of the cases BBs,
723 // and the NEProperties in the default BB.
724 PropertySet DefaultProperties(KP);
726 DTNodeType *Node = PS->DT->getNode(SI.getParent());
727 for (DTNodeType::iterator I = Node->begin(), E = Node->end(); I != E; ++I) {
728 BasicBlock *BB = (*I)->getBlock();
730 PropertySet BBProperties(KP);
731 if (BB == SI.getDefaultDest()) {
732 for (unsigned i = 1, e = SI.getNumCases(); i < e; ++i)
733 if (SI.getSuccessor(i) != BB)
734 BBProperties.addNotEqual(Condition, SI.getCaseValue(i));
735 } else if (ConstantInt *CI = SI.findCaseDest(BB)) {
736 BBProperties.addEqual(Condition, CI);
738 PS->proceedToSuccessor(BBProperties, BB);
742 void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
743 KP.addNotEqual(Constant::getNullValue(AI.getType()), &AI);
746 void PredicateSimplifier::Forwards::visitLoadInst(LoadInst &LI) {
747 Value *Ptr = LI.getPointerOperand();
748 KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
751 void PredicateSimplifier::Forwards::visitStoreInst(StoreInst &SI) {
752 Value *Ptr = SI.getPointerOperand();
753 KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
756 void PredicateSimplifier::Forwards::visitBinaryOperator(BinaryOperator &BO) {
757 Instruction::BinaryOps ops = BO.getOpcode();
760 case Instruction::Div:
761 case Instruction::Rem: {
762 Value *Divisor = BO.getOperand(1);
763 KP.addNotEqual(Constant::getNullValue(Divisor->getType()), Divisor);