llvm.org GIT mirror llvm / 4ddfe69
PredicateInfo: Support switch statements Summary: Depends on D29606 and D29682 Makes us pass GVN's edge.ll (we also will pass a few other testcases they just need cleaning up). Thoughts on the Predicate* hiearchy of classes especially welcome :) (it's not clear to me how best to organize it, and currently, the getBlock* seems ... uglier than maybe wasting a field somewhere or something). Reviewers: davide Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D29747 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295889 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 2 years ago
7 changed file(s) with 312 addition(s) and 87 deletion(s). Raw diff Collapse all Expand all
9191 class raw_ostream;
9292 class OrderedBasicBlock;
9393
94 enum PredicateType { PT_Branch, PT_Assume };
94 enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
9595
9696 // Base class for all predicate information we provide.
9797 // All of our predicate information has at least a comparison.
102102 // This can be use by passes, when destroying predicateinfo, to know
103103 // whether they can just drop the intrinsic, or have to merge metadata.
104104 Value *OriginalOp;
105 Value *Condition;
106105 PredicateBase(const PredicateBase &) = delete;
107106 PredicateBase &operator=(const PredicateBase &) = delete;
108107 PredicateBase() = delete;
109108 virtual ~PredicateBase() = default;
110109
111110 protected:
112 PredicateBase(PredicateType PT, Value *Op, Value *Condition)
113 : Type(PT), OriginalOp(Op), Condition(Condition) {}
111 PredicateBase(PredicateType PT, Value *Op) : Type(PT), OriginalOp(Op) {}
112 };
113
114 class PredicateWithCondition : public PredicateBase {
115 public:
116 Value *Condition;
117 static inline bool classof(const PredicateBase *PB) {
118 return PB->Type == PT_Assume || PB->Type == PT_Branch || PB->Type == PT_Switch;
119 }
120
121 protected:
122 PredicateWithCondition(PredicateType PT, Value *Op, Value *Condition)
123 : PredicateBase(PT, Op), Condition(Condition) {}
114124 };
115125
116126 // Provides predicate information for assumes. Since assumes are always true,
117127 // we simply provide the assume instruction, so you can tell your relative
118128 // position to it.
119 class PredicateAssume : public PredicateBase {
129 class PredicateAssume : public PredicateWithCondition {
120130 public:
121131 IntrinsicInst *AssumeInst;
122132 PredicateAssume(Value *Op, IntrinsicInst *AssumeInst, Value *Condition)
123 : PredicateBase(PT_Assume, Op, Condition), AssumeInst(AssumeInst) {}
133 : PredicateWithCondition(PT_Assume, Op, Condition),
134 AssumeInst(AssumeInst) {}
124135 PredicateAssume() = delete;
125136 static inline bool classof(const PredicateBase *PB) {
126137 return PB->Type == PT_Assume;
127138 }
128139 };
129140
141 // Mixin class for edge predicates. The FROM block is the block where the
142 // predicate originates, and the TO block is the block where the predicate is
143 // valid.
144 class PredicateWithEdge : public PredicateWithCondition {
145 public:
146 BasicBlock *From;
147 BasicBlock *To;
148 PredicateWithEdge() = delete;
149 static inline bool classof(const PredicateBase *PB) {
150 return PB->Type == PT_Branch || PB->Type == PT_Switch;
151 }
152
153 protected:
154 PredicateWithEdge(PredicateType PType, Value *Op, BasicBlock *From,
155 BasicBlock *To, Value *Cond)
156 : PredicateWithCondition(PType, Op, Cond), From(From), To(To) {}
157 };
158
130159 // Provides predicate information for branches.
131 class PredicateBranch : public PredicateBase {
132 public:
133 // This is the block that is conditional upon the condition.
134 BasicBlock *BranchBB;
135 // This is one of the true/false successors of BranchBB.
136 BasicBlock *SplitBB;
160 class PredicateBranch : public PredicateWithEdge {
161 public:
137162 // If true, SplitBB is the true successor, otherwise it's the false successor.
138163 bool TrueEdge;
139164 PredicateBranch(Value *Op, BasicBlock *BranchBB, BasicBlock *SplitBB,
140165 Value *Condition, bool TakenEdge)
141 : PredicateBase(PT_Branch, Op, Condition), BranchBB(BranchBB),
142 SplitBB(SplitBB), TrueEdge(TakenEdge) {}
166 : PredicateWithEdge(PT_Branch, Op, BranchBB, SplitBB, Condition),
167 TrueEdge(TakenEdge) {}
143168 PredicateBranch() = delete;
144169 static inline bool classof(const PredicateBase *PB) {
145170 return PB->Type == PT_Branch;
171 }
172 };
173
174 class PredicateSwitch : public PredicateWithEdge {
175 public:
176 Value *CaseValue;
177 // This is the switch instruction.
178 SwitchInst *Switch;
179 PredicateSwitch(Value *Op, BasicBlock *SwitchBB, BasicBlock *TargetBB,
180 Value *CaseValue, SwitchInst *SI)
181 : PredicateWithEdge(PT_Switch, Op, SwitchBB, TargetBB,
182 SI->getCondition()),
183 CaseValue(CaseValue), Switch(SI) {}
184 PredicateSwitch() = delete;
185 static inline bool classof(const PredicateBase *PB) {
186 return PB->Type == PT_Switch;
146187 }
147188 };
148189
188229 void buildPredicateInfo();
189230 void processAssume(IntrinsicInst *, BasicBlock *, SmallPtrSetImpl &);
190231 void processBranch(BranchInst *, BasicBlock *, SmallPtrSetImpl &);
232 void processSwitch(SwitchInst *, BasicBlock *, SmallPtrSetImpl &);
191233 void renameUses(SmallPtrSetImpl &);
192234 using ValueDFS = PredicateInfoClasses::ValueDFS;
193235 typedef SmallVectorImpl ValueDFSStack;
854854 return nullptr;
855855
856856 DEBUG(dbgs() << "Found predicate info from instruction !\n");
857
858 auto *PWC = dyn_cast(PI);
859 if (!PWC)
860 return nullptr;
861
857862 auto *CopyOf = I->getOperand(0);
858 auto *Cond = dyn_cast(PI->Condition);
859 if (!Cond)
860 return nullptr;
863 auto *Cond = PWC->Condition;
861864
862865 // If this a copy of the condition, it must be either true or false depending
863866 // on the predicate info type and edge
864867 if (CopyOf == Cond) {
865 addPredicateUsers(PI, I);
868 // We should not need to add predicate users because the predicate info is
869 // already a use of this operand.
866870 if (isa(PI))
867871 return createConstantExpression(ConstantInt::getTrue(Cond->getType()));
868872 if (auto *PBranch = dyn_cast(PI)) {
870874 return createConstantExpression(ConstantInt::getTrue(Cond->getType()));
871875 return createConstantExpression(ConstantInt::getFalse(Cond->getType()));
872876 }
873 }
874 // Not a copy of the condition, so see what the predicates tell us about this
875 // value.
877 if (auto *PSwitch = dyn_cast(PI))
878 return createConstantExpression(cast(PSwitch->CaseValue));
879 }
880
876881 // Not a copy of the condition, so see what the predicates tell us about this
877882 // value. First, though, we check to make sure the value is actually a copy
878883 // of one of the condition operands. It's possible, in certain cases, for it
879884 // to be a copy of a predicateinfo copy. In particular, if two branch
880885 // operations use the same condition, and one branch dominates the other, we
881886 // will end up with a copy of a copy. This is currently a small deficiency in
882 // predicateinfo. What will end up happening here is that we will value
887 // predicateinfo. What will end up happening here is that we will value
883888 // number both copies the same anyway.
884 if (CopyOf != Cond->getOperand(0) && CopyOf != Cond->getOperand(1)) {
889
890 // Everything below relies on the condition being a comparison.
891 auto *Cmp = dyn_cast(Cond);
892 if (!Cmp)
893 return nullptr;
894
895 if (CopyOf != Cmp->getOperand(0) && CopyOf != Cmp->getOperand(1)) {
885896 DEBUG(dbgs() << "Copy is not of any condition operands!");
886897 return nullptr;
887898 }
888 Value *FirstOp = lookupOperandLeader(Cond->getOperand(0));
889 Value *SecondOp = lookupOperandLeader(Cond->getOperand(1));
899 Value *FirstOp = lookupOperandLeader(Cmp->getOperand(0));
900 Value *SecondOp = lookupOperandLeader(Cmp->getOperand(1));
890901 bool SwappedOps = false;
891902 // Sort the ops
892903 if (shouldSwapOperands(FirstOp, SecondOp)) {
893904 std::swap(FirstOp, SecondOp);
894905 SwappedOps = true;
895906 }
896
897 // Everything below relies on the condition being a comparison.
898 auto *Cmp = dyn_cast(Cond);
899907 CmpInst::Predicate Predicate =
900908 SwappedOps ? Cmp->getSwappedPredicate() : Cmp->getPredicate();
901909
10941102
10951103 // Avoid processing the same info twice
10961104 const PredicateBase *LastPredInfo = nullptr;
1097
10981105 // See if we know something about the comparison itself, like it is the target
10991106 // of an assume.
11001107 auto *CmpPI = PredInfo->getPredicateInfoFor(I);
11401147 if (PI == LastPredInfo)
11411148 continue;
11421149 LastPredInfo = PI;
1150
11431151 // TODO: Along the false edge, we may know more things too, like icmp of
11441152 // same operands is false.
11451153 // TODO: We only handle actual comparison conditions below, not and/or.
4848 static cl::opt VerifyPredicateInfo(
4949 "verify-predicateinfo", cl::init(false), cl::Hidden,
5050 cl::desc("Verify PredicateInfo in legacy printer pass."));
51 namespace {
5152 DEBUG_COUNTER(RenameCounter, "predicateinfo-rename",
5253 "Controls which variables are renamed with predicateinfo")
54 // Given a predicate info that is a type of branching terminator, get the
55 // branching block.
56 const BasicBlock *getBranchBlock(const PredicateBase *PB) {
57 assert(isa(PB) &&
58 "Only branches and switches should have PHIOnly defs that "
59 "require branch blocks.");
60 return cast(PB)->From;
61 }
62
63 // Given a predicate info that is a type of branching terminator, get the
64 // branching terminator.
65 static Instruction *getBranchTerminator(const PredicateBase *PB) {
66 assert(isa(PB) &&
67 "Not a predicate info type we know how to get a terminator from.");
68 return cast(PB)->From->getTerminator();
69 }
70
71 // Given a predicate info that is a type of branching terminator, get the
72 // edge this predicate info represents
73 const std::pair
74 getBlockEdge(const PredicateBase *PB) {
75 assert(isa(PB) &&
76 "Not a predicate info type we know how to get an edge from.");
77 const auto *PEdge = cast(PB);
78 return std::make_pair(PEdge->From, PEdge->To);
79 }
80 }
5381
5482 namespace llvm {
5583 namespace PredicateInfoClasses {
109137 }
110138
111139 // For a phi use, or a non-materialized def, return the edge it represents.
112 const std::pair<const BasicBlock *, const BasicBlock *>
140 const std::pair<BasicBlock *, BasicBlock *>
113141 getBlockEdge(const ValueDFS &VD) const {
114142 if (!VD.Def && VD.U) {
115143 auto *PHI = cast(VD.U->getUser());
116144 return std::make_pair(PHI->getIncomingBlock(*VD.U), PHI->getParent());
117145 }
118146 // This is really a non-materialized def.
119 auto *PBranch = cast(VD.PInfo);
120 return std::make_pair(PBranch->BranchBB, PBranch->SplitBB);
147 return ::getBlockEdge(VD.PInfo);
121148 }
122149
123150 // For two phi related values, return the ordering.
207234 auto *PHI = dyn_cast(VDUse.U->getUser());
208235 if (!PHI)
209236 return false;
210 // The only EdgeOnly defs should be branch info.
211 auto *PBranch = dyn_cast(Stack.back().PInfo);
212 assert(PBranch && "Only branches should have EdgeOnly defs");
213 // Check edge matches us.
237 // Check edge
214238 BasicBlock *EdgePred = PHI->getIncomingBlock(*VDUse.U);
215 if (EdgePred != PBranch->BranchBB)
239 if (EdgePred != getBranchBlock(Stack.back().PInfo))
216240 return false;
217241
218242 // Use dominates, which knows how to handle edge dominance.
219 return DT.dominates(BasicBlockEdge(PBranch->BranchBB, PBranch->SplitBB),
220 *VDUse.U);
243 return DT.dominates(getBlockEdge(Stack.back().PInfo), *VDUse.U);
221244 }
222245
223246 return (VDUse.DFSIn >= Stack.back().DFSIn &&
399422 CmpOperands.clear();
400423 }
401424 }
425 // Process a block terminating switch, and place relevant operations to be
426 // renamed into OpsToRename.
427 void PredicateInfo::processSwitch(SwitchInst *SI, BasicBlock *BranchBB,
428 SmallPtrSetImpl &OpsToRename) {
429 Value *Op = SI->getCondition();
430 if ((!isa(Op) && !isa(Op)) || Op->hasOneUse())
431 return;
432
433 // Remember how many outgoing edges there are to every successor.
434 SmallDenseMap SwitchEdges;
435 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
436 BasicBlock *TargetBlock = SI->getSuccessor(i);
437 ++SwitchEdges[TargetBlock];
438 }
439
440 // Now propagate info for each case value
441 for (auto C : SI->cases()) {
442 BasicBlock *TargetBlock = C.getCaseSuccessor();
443 if (SwitchEdges.lookup(TargetBlock) == 1) {
444 PredicateSwitch *PS = new PredicateSwitch(
445 Op, SI->getParent(), TargetBlock, C.getCaseValue(), SI);
446 addInfoFor(OpsToRename, Op, PS);
447 if (!TargetBlock->getSinglePredecessor())
448 EdgeUsesOnly.insert({BranchBB, TargetBlock});
449 }
450 }
451 }
402452
403453 // Build predicate info for our function
404454 void PredicateInfo::buildPredicateInfo() {
412462 if (!BI->isConditional())
413463 continue;
414464 processBranch(BI, BranchBB, OpsToRename);
465 } else if (auto *SI = dyn_cast(BranchBB->getTerminator())) {
466 processSwitch(SI, BranchBB, OpsToRename);
415467 }
416468 }
417469 for (auto &Assume : AC.assumptions()) {
421473 // Now rename all our operations.
422474 renameUses(OpsToRename);
423475 }
476
477 // Given the renaming stack, make all the operands currently on the stack real
478 // by inserting them into the IR. Return the last operation's value.
424479 Value *PredicateInfo::materializeStack(unsigned int &Counter,
425480 ValueDFSStack &RenameStack,
426481 Value *OrigOp) {
440495 RenameIter == RenameStack.begin() ? OrigOp : (RenameIter - 1)->Def;
441496 ValueDFS &Result = *RenameIter;
442497 auto *ValInfo = Result.PInfo;
443 // For branches, we can just place the operand in the branch block before
498 // For edge predicates, we can just place the operand in the block before
444499 // the terminator. For assume, we have to place it right before the assume
445500 // to ensure we dominate all of our uses. Always insert right before the
446501 // relevant instruction (terminator, assume), so that we insert in proper
447502 // order in the case of multiple predicateinfo in the same block.
448 if (isa(ValInfo)) {
449 auto *PBranch = cast(ValInfo);
450 IRBuilder<> B(PBranch->BranchBB->getTerminator());
503 if (isa(ValInfo)) {
504 IRBuilder<> B(getBranchTerminator(ValInfo));
451505 Function *IF = Intrinsic::getDeclaration(
452506 F.getParent(), Intrinsic::ssa_copy, Op->getType());
453507 CallInst *PIC =
514568 VD.DFSOut = DomNode->getDFSNumOut();
515569 VD.PInfo = PossibleCopy;
516570 OrderedUses.push_back(VD);
517 } else if (const auto *PBranch =
518 dyn_cast>(PossibleCopy)) {
571 } else if (isa>(PossibleCopy)) {
519572 // If we can only do phi uses, we treat it like it's in the branch
520573 // block, and handle it specially. We know that it goes last, and only
521574 // dominate phi uses.
522 if (EdgeUsesOnly.count({PBranch->BranchBB, PBranch->SplitBB})) {
575 auto BlockEdge = getBlockEdge(PossibleCopy);
576 if (EdgeUsesOnly.count(BlockEdge)) {
523577 VD.LocalNum = LN_Last;
524 auto *DomNode = DT.getNode(PBranch->BranchBB);
578 auto *DomNode = DT.getNode(BlockEdge.first);
525579 if (DomNode) {
526580 VD.DFSIn = DomNode->getDFSNumIn();
527581 VD.DFSOut = DomNode->getDFSNumOut();
534588 // insertion in the branch block).
535589 // Insert a possible copy at the split block and before the branch.
536590 VD.LocalNum = LN_First;
537 auto *DomNode = DT.getNode(PBranch->SplitBB);
591 auto *DomNode = DT.getNode(BlockEdge.second);
538592 if (DomNode) {
539593 VD.DFSIn = DomNode->getDFSNumIn();
540594 VD.DFSOut = DomNode->getDFSNumOut();
686740 formatted_raw_ostream &OS) {
687741 if (const auto *PI = PredInfo->getPredicateInfoFor(I)) {
688742 OS << "; Has predicate info\n";
689 if (const auto *PB = dyn_cast(PI))
743 if (const auto *PB = dyn_cast(PI)) {
690744 OS << "; branch predicate info { TrueEdge: " << PB->TrueEdge
691 << " Comparison:" << *PB->Condition << " }\n";
692 else if (const auto *PA = dyn_cast(PI))
745 << " Comparison:" << *PB->Condition << " Edge: [";
746 PB->From->printAsOperand(OS);
747 OS << ",";
748 PB->To->printAsOperand(OS);
749 OS << "] }\n";
750 } else if (const auto *PS = dyn_cast(PI)) {
751 OS << "; switch predicate info { CaseValue: " << *PS->CaseValue
752 << " Switch:" << *PS->Switch << " Edge: [";
753 PS->From->printAsOperand(OS);
754 OS << ",";
755 PS->To->printAsOperand(OS);
756 OS << "] }\n";
757 } else if (const auto *PA = dyn_cast(PI)) {
693758 OS << "; assume predicate info {"
694759 << " Comparison:" << *PA->Condition << " }\n";
760 }
695761 }
696762 }
697763 };
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt < %s -basicaa -newgvn -S | FileCheck %s
12
23
34 declare void @foo(i1)
45 declare void @bar(i32)
56
7 define void @test3(i32 %x, i32 %y) {
68 ; CHECK-LABEL: @test3(
7 define void @test3(i32 %x, i32 %y) {
9 ; CHECK-NEXT: [[XZ:%.*]] = icmp eq i32 [[X:%.*]], 0
10 ; CHECK-NEXT: [[YZ:%.*]] = icmp eq i32 [[Y:%.*]], 0
11 ; CHECK-NEXT: [[Z:%.*]] = and i1 [[XZ]], [[YZ]]
12 ; CHECK-NEXT: br i1 [[Z]], label [[BOTH_ZERO:%.*]], label [[NOPE:%.*]]
13 ; CHECK: both_zero:
14 ; CHECK-NEXT: call void @foo(i1 true)
15 ; CHECK-NEXT: call void @foo(i1 true)
16 ; CHECK-NEXT: call void @bar(i32 0)
17 ; CHECK-NEXT: call void @bar(i32 0)
18 ; CHECK-NEXT: ret void
19 ; CHECK: nope:
20 ; CHECK-NEXT: call void @foo(i1 false)
21 ; CHECK-NEXT: ret void
22 ;
823 %xz = icmp eq i32 %x, 0
924 %yz = icmp eq i32 %y, 0
1025 %z = and i1 %xz, %yz
1126 br i1 %z, label %both_zero, label %nope
1227 both_zero:
1328 call void @foo(i1 %xz)
14 ; CHECK: call void @foo(i1 true)
1529 call void @foo(i1 %yz)
16 ; CHECK: call void @foo(i1 true)
17 call void @bar(i32 %x)
18 ; CHECK: call void @bar(i32 0)
30 call void @bar(i32 %x)
1931 call void @bar(i32 %y)
20 ; CHECK: call void @bar(i32 0)
2132 ret void
2233 nope:
2334 call void @foo(i1 %z)
24 ; CHECK: call void @foo(i1 false)
25 ret void
26 }
27
35 ret void
36 }
37 define void @test4(i1 %b, i32 %x) {
38 ; CHECK-LABEL: @test4(
39 ; CHECK-NEXT: br i1 [[B:%.*]], label [[SW:%.*]], label [[CASE3:%.*]]
40 ; CHECK: sw:
41 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[DEFAULT:%.*]] [
42 ; CHECK-NEXT: i32 0, label [[CASE0:%.*]]
43 ; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
44 ; CHECK-NEXT: i32 2, label [[CASE0]]
45 ; CHECK-NEXT: i32 3, label [[CASE3]]
46 ; CHECK-NEXT: i32 4, label [[DEFAULT]]
47 ; CHECK-NEXT: ]
48 ; CHECK: default:
49 ; CHECK-NEXT: call void @bar(i32 [[X]])
50 ; CHECK-NEXT: ret void
51 ; CHECK: case0:
52 ; CHECK-NEXT: call void @bar(i32 [[X]])
53 ; CHECK-NEXT: ret void
54 ; CHECK: case1:
55 ; CHECK-NEXT: call void @bar(i32 1)
56 ; CHECK-NEXT: ret void
57 ; CHECK: case3:
58 ; CHECK-NEXT: call void @bar(i32 [[X]])
59 ; CHECK-NEXT: ret void
60 ;
61 br i1 %b, label %sw, label %case3
62 sw:
63 switch i32 %x, label %default [
64 i32 0, label %case0
65 i32 1, label %case1
66 i32 2, label %case0
67 i32 3, label %case3
68 i32 4, label %default
69 ]
70 default:
71 call void @bar(i32 %x)
72 ret void
73 case0:
74 call void @bar(i32 %x)
75 ret void
76 case1:
77 call void @bar(i32 %x)
78 ret void
79 case3:
80 call void @bar(i32 %x)
81 ret void
82 }
83
84 define i1 @test5(i32 %x, i32 %y) {
2885 ; CHECK-LABEL: @test5(
29 define i1 @test5(i32 %x, i32 %y) {
86 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
87 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
88 ; CHECK: same:
89 ; CHECK-NEXT: ret i1 false
90 ; CHECK: different:
91 ; CHECK-NEXT: ret i1 false
92 ;
3093 %cmp = icmp eq i32 %x, %y
3194 br i1 %cmp, label %same, label %different
3295
3396 same:
3497 %cmp2 = icmp ne i32 %x, %y
35 ; CHECK: ret i1 false
3698 ret i1 %cmp2
3799
38100 different:
39101 %cmp3 = icmp eq i32 %x, %y
40 ; CHECK: ret i1 false
41102 ret i1 %cmp3
42103 }
43104
44105
106 define i1 @test7(i32 %x, i32 %y) {
45107 ; CHECK-LABEL: @test7(
46 define i1 @test7(i32 %x, i32 %y) {
108 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
109 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
110 ; CHECK: same:
111 ; CHECK-NEXT: ret i1 false
112 ; CHECK: different:
113 ; CHECK-NEXT: ret i1 false
114 ;
47115 %cmp = icmp sgt i32 %x, %y
48116 br i1 %cmp, label %same, label %different
49117
50118 same:
51119 %cmp2 = icmp sle i32 %x, %y
52 ; CHECK: ret i1 false
53120 ret i1 %cmp2
54121
55122 different:
56123 %cmp3 = icmp sgt i32 %x, %y
57 ; CHECK: ret i1 false
58124 ret i1 %cmp3
59125 }
60126
127 define i1 @test7_fp(float %x, float %y) {
61128 ; CHECK-LABEL: @test7_fp(
62 define i1 @test7_fp(float %x, float %y) {
129 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[X:%.*]], [[Y:%.*]]
130 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
131 ; CHECK: same:
132 ; CHECK-NEXT: ret i1 false
133 ; CHECK: different:
134 ; CHECK-NEXT: ret i1 false
135 ;
63136 %cmp = fcmp ogt float %x, %y
64137 br i1 %cmp, label %same, label %different
65138
66139 same:
67140 %cmp2 = fcmp ule float %x, %y
68 ; CHECK: ret i1 false
69141 ret i1 %cmp2
70142
71143 different:
72144 %cmp3 = fcmp ogt float %x, %y
73 ; CHECK: ret i1 false
74145 ret i1 %cmp3
75146 }
76147
77148 ; PR1768
149 define i32 @test9(i32 %i, i32 %j) {
78150 ; CHECK-LABEL: @test9(
79 define i32 @test9(i32 %i, i32 %j) {
151 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[I:%.*]], [[J:%.*]]
152 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[RET:%.*]]
153 ; CHECK: cond_true:
154 ; CHECK-NEXT: ret i32 0
155 ; CHECK: ret:
156 ; CHECK-NEXT: ret i32 5
157 ;
80158 %cmp = icmp eq i32 %i, %j
81159 br i1 %cmp, label %cond_true, label %ret
82160
83161 cond_true:
84162 %diff = sub i32 %i, %j
85163 ret i32 %diff
86 ; CHECK: ret i32 0
87164
88165 ret:
89166 ret i32 5
90 ; CHECK: ret i32 5
91167 }
92168
93169 ; PR1768
170 define i32 @test10(i32 %j, i32 %i) {
94171 ; CHECK-LABEL: @test10(
95 define i32 @test10(i32 %j, i32 %i) {
172 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[I:%.*]], [[J:%.*]]
173 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[RET:%.*]]
174 ; CHECK: cond_true:
175 ; CHECK-NEXT: ret i32 0
176 ; CHECK: ret:
177 ; CHECK-NEXT: ret i32 5
178 ;
96179 %cmp = icmp eq i32 %i, %j
97180 br i1 %cmp, label %cond_true, label %ret
98181
99182 cond_true:
100183 %diff = sub i32 %i, %j
101184 ret i32 %diff
102 ; CHECK: ret i32 0
103185
104186 ret:
105187 ret i32 5
106 ; CHECK: ret i32 5
107188 }
108189
109190 declare i32 @yogibar()
110191
192 define i32 @test11(i32 %x) {
111193 ; CHECK-LABEL: @test11(
112 define i32 @test11(i32 %x) {
194 ; CHECK-NEXT: [[V0:%.*]] = call i32 @yogibar()
195 ; CHECK-NEXT: [[V1:%.*]] = call i32 @yogibar()
196 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[V0]], [[V1]]
197 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[NEXT:%.*]]
198 ; CHECK: cond_true:
199 ; CHECK-NEXT: ret i32 [[V0]]
200 ; CHECK: next:
201 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[X:%.*]], [[V0]]
202 ; CHECK-NEXT: br i1 [[CMP2]], label [[COND_TRUE2:%.*]], label [[NEXT2:%.*]]
203 ; CHECK: cond_true2:
204 ; CHECK-NEXT: ret i32 [[X]]
205 ; CHECK: next2:
206 ; CHECK-NEXT: ret i32 0
207 ;
113208 %v0 = call i32 @yogibar()
114209 %v1 = call i32 @yogibar()
115210 %cmp = icmp eq i32 %v0, %v1
117212
118213 cond_true:
119214 ret i32 %v1
120 ; CHECK: ret i32 %v0
121215
122216 next:
123217 %cmp2 = icmp eq i32 %x, %v0
125219
126220 cond_true2:
127221 ret i32 %v0
128 ; CHECK: ret i32 %x
129222
130223 next2:
131224 ret i32 0
132225 }
133226
227 define i32 @test12(i32 %x) {
134228 ; CHECK-LABEL: @test12(
135 define i32 @test12(i32 %x) {
229 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
230 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_FALSE:%.*]]
231 ; CHECK: cond_true:
232 ; CHECK-NEXT: br label [[RET:%.*]]
233 ; CHECK: cond_false:
234 ; CHECK-NEXT: br label [[RET]]
235 ; CHECK: ret:
236 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[COND_TRUE]] ], [ [[X]], [[COND_FALSE]] ]
237 ; CHECK-NEXT: ret i32 [[RES]]
238 ;
136239 %cmp = icmp eq i32 %x, 0
137240 br i1 %cmp, label %cond_true, label %cond_false
138241
144247
145248 ret:
146249 %res = phi i32 [ %x, %cond_true ], [ %x, %cond_false ]
147 ; CHECK: %res = phi i32 [ 0, %cond_true ], [ %x, %cond_false ]
148250 ret i32 %res
149251 }
None ; XFAIL: *
1 ; RUN: opt -newgvn -S < %s | FileCheck %s
21
32 define i32 @f1(i32 %x) {
132132 ; CHECK-LABEL: @test4(
133133 ; CHECK-NEXT: br i1 [[B:%.*]], label [[SW:%.*]], label [[CASE3:%.*]]
134134 ; CHECK: sw:
135 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[DEFAULT:%.*]] [
136 ; CHECK-NEXT: i32 0, label [[CASE0:%.*]]
135 ; CHECK: i32 0, label [[CASE0:%.*]]
137136 ; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
137 ; CHECK-NEXT: i32 2, label [[CASE0]]
138 ; CHECK-NEXT: i32 3, label [[CASE3]]
139 ; CHECK-NEXT: i32 4, label [[DEFAULT:%.*]]
140 ; CHECK-NEXT: ] Edge: [label [[SW]],label %case1] }
141 ; CHECK-NEXT: [[X_0:%.*]] = call i32 @llvm.ssa.copy.i32(i32 [[X:%.*]])
142 ; CHECK-NEXT: switch i32 [[X]], label [[DEFAULT]] [
143 ; CHECK-NEXT: i32 0, label [[CASE0]]
144 ; CHECK-NEXT: i32 1, label [[CASE1]]
138145 ; CHECK-NEXT: i32 2, label [[CASE0]]
139146 ; CHECK-NEXT: i32 3, label [[CASE3]]
140147 ; CHECK-NEXT: i32 4, label [[DEFAULT]]
146153 ; CHECK-NEXT: call void @bar(i32 [[X]])
147154 ; CHECK-NEXT: ret void
148155 ; CHECK: case1:
149 ; CHECK-NEXT: call void @bar(i32 [[X]])
156 ; CHECK-NEXT: call void @bar(i32 [[X_0]])
150157 ; CHECK-NEXT: ret void
151158 ; CHECK: case3:
152159 ; CHECK-NEXT: call void @bar(i32 [[X]])
5151 define i32 @f3(i32 %x) {
5252 ; CHECK-LABEL: @f3(
5353 ; CHECK-NEXT: bb0:
54 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[BB1:%.*]] [
54 ; CHECK: [[X_0:%.*]] = call i32 @llvm.ssa.copy.i32(i32 [[X:%.*]])
55 ; CHECK-NEXT: switch i32 [[X]], label [[BB1:%.*]] [
5556 ; CHECK-NEXT: i32 0, label [[BB2:%.*]]
5657 ; CHECK-NEXT: ]
5758 ; CHECK: bb1:
5859 ; CHECK-NEXT: br label [[BB2]]
5960 ; CHECK: bb2:
60 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[X]], [[BB0:%.*]] ], [ 0, [[BB1]] ]
61 ; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[X_0]], [[BB0:%.*]] ], [ 0, [[BB1]] ]
6162 ; CHECK-NEXT: [[FOO:%.*]] = add i32 [[COND]], [[X]]
6263 ; CHECK-NEXT: ret i32 [[FOO]]
6364 ;