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 //===------------------------------------------------------------------===//
32 // * Check handling of NAN in floating point types
34 #define DEBUG_TYPE "predsimplify"
35 #include "llvm/Transforms/Scalar.h"
36 #include "llvm/Constants.h"
37 #include "llvm/Instructions.h"
38 #include "llvm/Pass.h"
39 #include "llvm/ADT/EquivalenceClasses.h"
40 #include "llvm/ADT/Statistic.h"
41 #include "llvm/ADT/STLExtras.h"
42 #include "llvm/Analysis/Dominators.h"
43 #include "llvm/Support/CFG.h"
44 #include "llvm/Support/Debug.h"
50 NumVarsReplaced("predsimplify", "Number of argument substitutions");
52 NumInstruction("predsimplify", "Number of instructions removed");
54 NumSwitchCases("predsimplify", "Number of switch cases removed");
56 NumBranches("predsimplify", "Number of branches made unconditional");
58 /// Used for choosing the canonical Value in a synonym set.
59 /// Leaves the better one in V1. Returns whether a swap took place.
60 static void order(Value *&V1, Value *&V2) {
61 if (isa<Constant>(V2)) {
62 if (!isa<Constant>(V1)) {
66 } else if (isa<Argument>(V2)) {
67 if (!isa<Constant>(V1) && !isa<Argument>(V1)) {
72 if (User *U1 = dyn_cast<User>(V1)) {
73 for (User::const_op_iterator I = U1->op_begin(), E = U1->op_end();
84 /// Represents the set of equivalent Value*s and provides insertion
85 /// and fast lookup. Also stores the set of inequality relationships.
88 class EquivalenceClasses<Value *> union_find;
90 typedef std::vector<Property>::iterator PropertyIterator;
91 typedef std::vector<Property>::const_iterator ConstPropertyIterator;
98 Value *canonicalize(Value *V) const {
103 Value *lookup(Value *V) const {
104 EquivalenceClasses<Value *>::member_iterator SI =
105 union_find.findLeader(V);
106 if (SI == union_find.member_end()) return NULL;
111 return union_find.empty();
114 void addEqual(Value *V1, Value *V2) {
116 if (isa<Constant>(V2)) return; // refuse to set false == true.
118 union_find.unionSets(V1, V2);
119 addImpliedProperties(EQ, V1, V2);
122 void addNotEqual(Value *V1, Value *V2) {
123 DEBUG(std::cerr << "not equal: " << *V1 << " and " << *V2 << "\n");
124 V1 = canonicalize(V1);
125 V2 = canonicalize(V2);
127 // Does the property already exist?
128 for (PropertyIterator I = Properties.begin(), E = Properties.end();
130 if (I->Opcode != NE) continue;
132 I->V1 = canonicalize(I->V1);
133 I->V2 = canonicalize(I->V2);
134 if ((I->V1 == V1 && I->V2 == V2) ||
135 (I->V1 == V2 && I->V2 == V1)) {
141 Properties.push_back(Property(NE, V1, V2));
142 addImpliedProperties(NE, V1, V2);
145 PropertyIterator findProperty(Ops Opcode, Value *V1, Value *V2) {
146 assert(Opcode != EQ && "Can't findProperty on EQ."
147 "Use the lookup method instead.");
151 if (!V1 || !V2) return Properties.end();
153 // Does the property already exist?
154 for (PropertyIterator I = Properties.begin(), E = Properties.end();
156 if (I->Opcode != Opcode) continue;
158 I->V1 = canonicalize(I->V1);
159 I->V2 = canonicalize(I->V2);
160 if ((I->V1 == V1 && I->V2 == V2) ||
161 (I->V1 == V2 && I->V2 == V1)) {
165 return Properties.end();
168 ConstPropertyIterator
169 findProperty(Ops Opcode, Value *V1, Value *V2) const {
170 assert(Opcode != EQ && "Can't findProperty on EQ."
171 "Use the lookup method instead.");
175 if (!V1 || !V2) return Properties.end();
177 // Does the property already exist?
178 for (ConstPropertyIterator I = Properties.begin(),
179 E = Properties.end(); I != E; ++I) {
180 if (I->Opcode != Opcode) continue;
182 Value *v1 = lookup(I->V1),
184 if (!v1 || !v2) continue;
185 if ((v1 == V1 && v2 == V2) ||
186 (v1 == V2 && v2 == V1)) {
190 return Properties.end();
194 // Represents Head OP [Tail1, Tail2, ...]
195 // For example: %x != %a, %x != %b.
197 Property(Ops opcode, Value *v1, Value *v2)
198 : Opcode(opcode), V1(v1), V2(v2)
199 { assert(opcode != EQ && "Equality belongs in the synonym set,"
200 "not a property."); }
202 bool operator<(const Property &rhs) const {
203 if (Opcode != rhs.Opcode) return Opcode < rhs.Opcode;
204 if (V1 != rhs.V1) return V1 < rhs.V1;
212 void add(Ops Opcode, Value *V1, Value *V2, bool invert) {
215 if (invert) addNotEqual(V1, V2);
216 else addEqual(V1, V2);
219 if (invert) addEqual(V1, V2);
220 else addNotEqual(V1, V2);
223 assert(0 && "Unknown property opcode.");
227 // Finds the properties implied by a synonym and adds them too.
228 // Example: ("seteq %a, %b", true, EQ) --> (%a, %b, EQ)
229 // ("seteq %a, %b", false, EQ) --> (%a, %b, NE)
230 void addImpliedProperties(Ops Opcode, Value *V1, Value *V2) {
233 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V2)) {
234 switch (BO->getOpcode()) {
235 case Instruction::SetEQ:
236 if (V1 == ConstantBool::True)
237 add(Opcode, BO->getOperand(0), BO->getOperand(1), false);
238 if (V1 == ConstantBool::False)
239 add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
241 case Instruction::SetNE:
242 if (V1 == ConstantBool::True)
243 add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
244 if (V1 == ConstantBool::False)
245 add(Opcode, BO->getOperand(0), BO->getOperand(1), false);
247 case Instruction::SetLT:
248 case Instruction::SetGT:
249 if (V1 == ConstantBool::True)
250 add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
252 case Instruction::SetLE:
253 case Instruction::SetGE:
254 if (V1 == ConstantBool::False)
255 add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
257 case Instruction::And:
258 if (V1 == ConstantBool::True) {
259 add(Opcode, ConstantBool::True, BO->getOperand(0), false);
260 add(Opcode, ConstantBool::True, BO->getOperand(1), false);
263 case Instruction::Or:
264 if (V1 == ConstantBool::False) {
265 add(Opcode, ConstantBool::False, BO->getOperand(0), false);
266 add(Opcode, ConstantBool::False, BO->getOperand(1), false);
269 case Instruction::Xor:
270 if (V1 == ConstantBool::True) {
271 if (BO->getOperand(0) == ConstantBool::True)
272 add(Opcode, ConstantBool::False, BO->getOperand(1), false);
273 if (BO->getOperand(1) == ConstantBool::True)
274 add(Opcode, ConstantBool::False, BO->getOperand(0), false);
276 if (V1 == ConstantBool::False) {
277 if (BO->getOperand(0) == ConstantBool::True)
278 add(Opcode, ConstantBool::True, BO->getOperand(1), false);
279 if (BO->getOperand(1) == ConstantBool::True)
280 add(Opcode, ConstantBool::True, BO->getOperand(0), false);
289 std::map<Value *, unsigned> SynonymMap;
290 std::vector<Value *> Synonyms;
293 void debug(std::ostream &os) const {
296 std::vector<Property> Properties;
299 /// PredicateSimplifier - This class is a simplifier that replaces
300 /// one equivalent variable with another. It also tracks what
301 /// can't be equal and will solve setcc instructions when possible.
302 class PredicateSimplifier : public FunctionPass {
304 bool runOnFunction(Function &F);
305 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
308 // Try to replace the Use of the instruction with something simpler.
309 Value *resolve(SetCondInst *SCI, const PropertySet &);
310 Value *resolve(BinaryOperator *BO, const PropertySet &);
311 Value *resolve(SelectInst *SI, const PropertySet &);
312 Value *resolve(Value *V, const PropertySet &);
314 // Used by terminator instructions to proceed from the current basic
315 // block to the next. Verifies that "current" dominates "next",
316 // then calls visitBasicBlock.
317 void proceedToSuccessor(PropertySet &CurrentPS, PropertySet &NextPS,
318 DominatorTree::Node *Current, DominatorTree::Node *Next);
319 void proceedToSuccessor(PropertySet &CurrentPS,
320 DominatorTree::Node *Current, DominatorTree::Node *Next);
322 // Visits each instruction in the basic block.
323 void visitBasicBlock(DominatorTree::Node *DTNode,
324 PropertySet &KnownProperties);
326 // For each instruction, add the properties to KnownProperties.
327 void visit(Instruction *I, DominatorTree::Node *, PropertySet &);
328 void visit(TerminatorInst *TI, DominatorTree::Node *, PropertySet &);
329 void visit(BranchInst *BI, DominatorTree::Node *, PropertySet &);
330 void visit(SwitchInst *SI, DominatorTree::Node *, PropertySet);
331 void visit(LoadInst *LI, DominatorTree::Node *, PropertySet &);
332 void visit(StoreInst *SI, DominatorTree::Node *, PropertySet &);
333 void visit(BinaryOperator *BO, DominatorTree::Node *, PropertySet &);
339 RegisterPass<PredicateSimplifier> X("predsimplify",
340 "Predicate Simplifier");
343 FunctionPass *llvm::createPredicateSimplifierPass() {
344 return new PredicateSimplifier();
347 bool PredicateSimplifier::runOnFunction(Function &F) {
348 DT = &getAnalysis<DominatorTree>();
351 PropertySet KnownProperties;
352 visitBasicBlock(DT->getRootNode(), KnownProperties);
356 void PredicateSimplifier::getAnalysisUsage(AnalysisUsage &AU) const {
357 AU.addRequired<DominatorTree>();
360 // resolve catches cases addProperty won't because it wasn't used as a
361 // condition in the branch, and that visit won't, because the instruction
362 // was defined outside of the range that the properties apply to.
363 Value *PredicateSimplifier::resolve(SetCondInst *SCI,
364 const PropertySet &KP) {
365 // Attempt to resolve the SetCondInst to a boolean.
367 Value *SCI0 = SCI->getOperand(0),
368 *SCI1 = SCI->getOperand(1);
369 PropertySet::ConstPropertyIterator NE =
370 KP.findProperty(PropertySet::NE, SCI0, SCI1);
372 if (NE != KP.Properties.end()) {
373 switch (SCI->getOpcode()) {
374 case Instruction::SetEQ:
375 return ConstantBool::False;
376 case Instruction::SetNE:
377 return ConstantBool::True;
378 case Instruction::SetLE:
379 case Instruction::SetGE:
380 case Instruction::SetLT:
381 case Instruction::SetGT:
384 assert(0 && "Unknown opcode in SetCondInst.");
389 SCI0 = KP.canonicalize(SCI0);
390 SCI1 = KP.canonicalize(SCI1);
392 ConstantIntegral *CI1 = dyn_cast<ConstantIntegral>(SCI0),
393 *CI2 = dyn_cast<ConstantIntegral>(SCI1);
395 if (!CI1 || !CI2) return SCI;
397 switch(SCI->getOpcode()) {
398 case Instruction::SetLE:
399 case Instruction::SetGE:
400 case Instruction::SetEQ:
401 if (CI1->getRawValue() == CI2->getRawValue())
402 return ConstantBool::True;
404 return ConstantBool::False;
405 case Instruction::SetLT:
406 case Instruction::SetGT:
407 case Instruction::SetNE:
408 if (CI1->getRawValue() == CI2->getRawValue())
409 return ConstantBool::False;
411 return ConstantBool::True;
413 assert(0 && "Unknown opcode in SetContInst.");
418 Value *PredicateSimplifier::resolve(BinaryOperator *BO,
419 const PropertySet &KP) {
420 if (SetCondInst *SCI = dyn_cast<SetCondInst>(BO))
421 return resolve(SCI, KP);
423 DEBUG(std::cerr << "BO->getOperand(1) = " << *BO->getOperand(1) << "\n");
425 Value *lhs = resolve(BO->getOperand(0), KP),
426 *rhs = resolve(BO->getOperand(1), KP);
427 ConstantIntegral *CI1 = dyn_cast<ConstantIntegral>(lhs);
428 ConstantIntegral *CI2 = dyn_cast<ConstantIntegral>(rhs);
430 DEBUG(std::cerr << "resolveBO: lhs = " << *lhs
431 << ", rhs = " << *rhs << "\n");
432 if (CI1) DEBUG(std::cerr << "CI1 = " << *CI1);
433 if (CI2) DEBUG(std::cerr << "CI2 = " << *CI2);
435 if (!CI1 || !CI2) return BO;
437 Value *V = ConstantExpr::get(BO->getOpcode(), CI1, CI2);
442 Value *PredicateSimplifier::resolve(SelectInst *SI, const PropertySet &KP) {
443 Value *Condition = resolve(SI->getCondition(), KP);
444 if (Condition == ConstantBool::True)
445 return resolve(SI->getTrueValue(), KP);
446 else if (Condition == ConstantBool::False)
447 return resolve(SI->getFalseValue(), KP);
451 Value *PredicateSimplifier::resolve(Value *V, const PropertySet &KP) {
452 if (isa<Constant>(V) || isa<BasicBlock>(V) || KP.empty()) return V;
454 V = KP.canonicalize(V);
456 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
457 return resolve(BO, KP);
458 else if (SelectInst *SI = dyn_cast<SelectInst>(V))
459 return resolve(SI, KP);
464 void PredicateSimplifier::visitBasicBlock(DominatorTree::Node *DTNode,
465 PropertySet &KnownProperties) {
466 BasicBlock *BB = DTNode->getBlock();
467 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
468 visit(I, DTNode, KnownProperties);
472 void PredicateSimplifier::visit(Instruction *I, DominatorTree::Node *DTNode,
473 PropertySet &KnownProperties) {
474 DEBUG(std::cerr << "Considering instruction " << *I << "\n");
475 DEBUG(KnownProperties.debug(std::cerr));
477 // Substitute values known to be equal.
478 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
479 Value *Oper = I->getOperand(i);
480 Value *V = resolve(Oper, KnownProperties);
481 assert(V && "resolve not supposed to return NULL.");
485 DEBUG(std::cerr << "resolving " << *I);
487 DEBUG(std::cerr << "into " << *I);
491 Value *V = resolve(I, KnownProperties);
492 assert(V && "resolve not supposed to return NULL.");
496 I->replaceAllUsesWith(V);
497 I->eraseFromParent();
500 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(I))
501 visit(TI, DTNode, KnownProperties);
502 else if (LoadInst *LI = dyn_cast<LoadInst>(I))
503 visit(LI, DTNode, KnownProperties);
504 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
505 visit(SI, DTNode, KnownProperties);
506 else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
507 visit(BO, DTNode, KnownProperties);
510 void PredicateSimplifier::proceedToSuccessor(PropertySet &CurrentPS,
511 PropertySet &NextPS, DominatorTree::Node *Current,
512 DominatorTree::Node *Next) {
513 if (Next->getBlock()->getSinglePredecessor() == Current->getBlock())
514 proceedToSuccessor(NextPS, Current, Next);
516 proceedToSuccessor(CurrentPS, Current, Next);
519 void PredicateSimplifier::proceedToSuccessor(PropertySet &KP,
520 DominatorTree::Node *Current, DominatorTree::Node *Next) {
521 if (Current->properlyDominates(Next))
522 visitBasicBlock(Next, KP);
525 void PredicateSimplifier::visit(TerminatorInst *TI,
526 DominatorTree::Node *Node, PropertySet &KP){
527 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
531 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
536 for (unsigned i = 0, E = TI->getNumSuccessors(); i != E; ++i) {
537 BasicBlock *BB = TI->getSuccessor(i);
538 PropertySet KPcopy(KP);
539 proceedToSuccessor(KPcopy, Node, DT->getNode(TI->getSuccessor(i)));
543 void PredicateSimplifier::visit(BranchInst *BI,
544 DominatorTree::Node *Node, PropertySet &KP){
545 if (BI->isUnconditional()) {
546 proceedToSuccessor(KP, Node, DT->getNode(BI->getSuccessor(0)));
550 Value *Condition = BI->getCondition();
552 BasicBlock *TrueDest = BI->getSuccessor(0),
553 *FalseDest = BI->getSuccessor(1);
555 if (Condition == ConstantBool::True) {
556 FalseDest->removePredecessor(BI->getParent());
557 BI->setUnconditionalDest(TrueDest);
560 proceedToSuccessor(KP, Node, DT->getNode(TrueDest));
562 } else if (Condition == ConstantBool::False) {
563 TrueDest->removePredecessor(BI->getParent());
564 BI->setUnconditionalDest(FalseDest);
567 proceedToSuccessor(KP, Node, DT->getNode(FalseDest));
571 PropertySet TrueProperties(KP), FalseProperties(KP);
572 DEBUG(std::cerr << "true set:\n");
573 TrueProperties.addEqual(ConstantBool::True, Condition);
574 DEBUG(std::cerr << "false set:\n");
575 FalseProperties.addEqual(ConstantBool::False, Condition);
577 PropertySet KPcopy(KP);
578 proceedToSuccessor(KP, TrueProperties, Node, DT->getNode(TrueDest));
579 proceedToSuccessor(KPcopy, FalseProperties, Node, DT->getNode(FalseDest));
582 void PredicateSimplifier::visit(SwitchInst *SI,
583 DominatorTree::Node *DTNode, PropertySet KP) {
584 Value *Condition = SI->getCondition();
586 // If there's an NEProperty covering this SwitchInst, we may be able to
587 // eliminate one of the cases.
588 if (Value *C = KP.lookup(Condition)) {
590 for (PropertySet::ConstPropertyIterator I = KP.Properties.begin(),
591 E = KP.Properties.end(); I != E; ++I) {
592 if (I->Opcode != PropertySet::NE) continue;
593 Value *V1 = KP.lookup(I->V1),
594 *V2 = KP.lookup(I->V2);
595 if (V1 != C && V2 != C) continue;
597 // Is one side a number?
598 ConstantInt *CI = dyn_cast<ConstantInt>(KP.lookup(I->V1));
599 if (!CI) CI = dyn_cast<ConstantInt>(KP.lookup(I->V2));
602 unsigned i = SI->findCaseValue(CI);
604 SI->getSuccessor(i)->removePredecessor(SI->getParent());
613 // Set the EQProperty in each of the cases BBs,
614 // and the NEProperties in the default BB.
615 PropertySet DefaultProperties(KP);
617 DominatorTree::Node *Node = DT->getNode(SI->getParent()),
618 *DefaultNode = DT->getNode(SI->getSuccessor(0));
619 if (!Node->dominates(DefaultNode)) DefaultNode = NULL;
621 for (unsigned I = 1, E = SI->getNumCases(); I < E; ++I) {
622 ConstantInt *CI = SI->getCaseValue(I);
624 BasicBlock *SuccBB = SI->getSuccessor(I);
625 PropertySet copy(KP);
626 if (SuccBB->getSinglePredecessor()) {
627 PropertySet NewProperties(KP);
628 NewProperties.addEqual(Condition, CI);
629 proceedToSuccessor(copy, NewProperties, DTNode, DT->getNode(SuccBB));
631 proceedToSuccessor(copy, DTNode, DT->getNode(SuccBB));
634 DefaultProperties.addNotEqual(Condition, CI);
638 proceedToSuccessor(DefaultProperties, DTNode, DefaultNode);
641 void PredicateSimplifier::visit(LoadInst *LI,
642 DominatorTree::Node *, PropertySet &KP) {
643 Value *Ptr = LI->getPointerOperand();
644 KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
647 void PredicateSimplifier::visit(StoreInst *SI,
648 DominatorTree::Node *, PropertySet &KP) {
649 Value *Ptr = SI->getPointerOperand();
650 KP.addNotEqual(Constant::getNullValue(Ptr->getType()), Ptr);
653 void PredicateSimplifier::visit(BinaryOperator *BO,
654 DominatorTree::Node *, PropertySet &KP) {
655 Instruction::BinaryOps ops = BO->getOpcode();
658 case Instruction::Div:
659 case Instruction::Rem: {
660 Value *Divisor = BO->getOperand(1);
661 KP.addNotEqual(Constant::getNullValue(Divisor->getType()), Divisor);
668 // Some other things we could do:
669 // In f=x*y, if x != 1 && y != 1 then f != x && f != y.
670 // In f=x+y, if x != 0 then f != y and if y != 0 then f != x.