llvm.org GIT mirror llvm / 9a5d29b
Reapply r373431 "Switch lowering: omit range check for bit tests when default is unreachable (PR43129)" This was reverted in r373454 due to breaking the expensive-checks bot. This version addresses that by omitting the addSuccessorWithProb() call when omitting the range check. > Switch lowering: omit range check for bit tests when default is unreachable (PR43129) > > This is modeled after the same functionality for jump tables, which was > added in r357067. > > Differential revision: https://reviews.llvm.org/D68131 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373477 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 7 months ago
3 changed file(s) with 31 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
211211 BitTestInfo Cases;
212212 BranchProbability Prob;
213213 BranchProbability DefaultProb;
214 bool OmitRangeCheck;
214215
215216 BitTestBlock(APInt F, APInt R, const Value *SV, unsigned Rg, MVT RgVT, bool E,
216217 bool CR, MachineBasicBlock *P, MachineBasicBlock *D,
217218 BitTestInfo C, BranchProbability Pr)
218219 : First(std::move(F)), Range(std::move(R)), SValue(SV), Reg(Rg),
219220 RegVT(RgVT), Emitted(E), ContiguousRange(CR), Parent(P), Default(D),
220 Cases(std::move(C)), Prob(Pr) {}
221 Cases(std::move(C)), Prob(Pr), OmitRangeCheck(false) {}
221222 };
222223
223224 /// Return the range of values within a range.
26212621 // Subtract the minimum value.
26222622 SDValue SwitchOp = getValue(B.SValue);
26232623 EVT VT = SwitchOp.getValueType();
2624 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
2625 DAG.getConstant(B.First, dl, VT));
2626
2627 // Check range.
2624 SDValue RangeSub =
2625 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
2626
2627 // Determine the type of the test operands.
26282628 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2629 SDValue RangeCmp = DAG.getSetCC(
2630 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
2631 Sub.getValueType()),
2632 Sub, DAG.getConstant(B.Range, dl, VT), ISD::SETUGT);
2633
2634 // Determine the type of the test operands.
26352629 bool UsePtrType = false;
26362630 if (!TLI.isTypeLegal(VT)) {
26372631 UsePtrType = true;
26442638 break;
26452639 }
26462640 }
2641 SDValue Sub = RangeSub;
26472642 if (UsePtrType) {
26482643 VT = TLI.getPointerTy(DAG.getDataLayout());
26492644 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
26552650
26562651 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
26572652
2658 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
2653 if (!B.OmitRangeCheck)
2654 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
26592655 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
26602656 SwitchBB->normalizeSuccProbs();
26612657
2662 SDValue BrRange = DAG.getNode(ISD::BRCOND, dl,
2663 MVT::Other, CopyTo, RangeCmp,
2664 DAG.getBasicBlock(B.Default));
2658 SDValue Root = CopyTo;
2659 if (!B.OmitRangeCheck) {
2660 // Conditional branch to the default block.
2661 SDValue RangeCmp = DAG.getSetCC(dl,
2662 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
2663 RangeSub.getValueType()),
2664 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
2665 ISD::SETUGT);
2666
2667 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
2668 DAG.getBasicBlock(B.Default));
2669 }
26652670
26662671 // Avoid emitting unnecessary branches to the next block.
26672672 if (MBB != NextBlock(SwitchBB))
2668 BrRange = DAG.getNode(ISD::BR, dl, MVT::Other, BrRange,
2669 DAG.getBasicBlock(MBB));
2670
2671 DAG.setRoot(BrRange);
2673 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
2674
2675 DAG.setRoot(Root);
26722676 }
26732677
26742678 /// visitBitTestCase - this function produces one "bit test"
1016310167 break;
1016410168 }
1016510169 case CC_BitTests: {
10166 // FIXME: If Fallthrough is unreachable, skip the range check.
10167
1016810170 // FIXME: Optimize away range check based on pivot comparisons.
1016910171 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
1017010172
1018310185 if (!BTB->ContiguousRange) {
1018410186 BTB->Prob += DefaultProb / 2;
1018510187 BTB->DefaultProb -= DefaultProb / 2;
10188 }
10189
10190 if (FallthroughUnreachable) {
10191 // Skip the range check if the fallthrough block is unreachable.
10192 BTB->OmitRangeCheck = true;
1018610193 }
1018710194
1018810195 // If we're in the right place, emit the bit test header right now.
156156 }
157157
158158
159 ; TODO: Omit the range check when the default case is unreachable, see PR43129.
159 ; Omit the range check when the default case is unreachable, see PR43129.
160160 declare void @g(i32)
161161 define void @test5(i32 %x) {
162162
163163 ; CHECK-LABEL: test5
164 ; CHECK: cmpl $8, %edi
165 ; CHECK: ja
164 ; CHECK-NOT: cmp
166165
167166 ; 73 = 2^0 + 2^3 + 2^6
168167 ; CHECK: movl $73