Skip to content

Commit 9a5d29b

Browse files
committed
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
1 parent 9077974 commit 9a5d29b

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

include/llvm/CodeGen/SwitchLoweringUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ struct BitTestBlock {
212212
BitTestInfo Cases;
213213
BranchProbability Prob;
214214
BranchProbability DefaultProb;
215+
bool OmitRangeCheck;
215216

216217
BitTestBlock(APInt F, APInt R, const Value *SV, unsigned Rg, MVT RgVT, bool E,
217218
bool CR, MachineBasicBlock *P, MachineBasicBlock *D,
218219
BitTestInfo C, BranchProbability Pr)
219220
: First(std::move(F)), Range(std::move(R)), SValue(SV), Reg(Rg),
220221
RegVT(RgVT), Emitted(E), ContiguousRange(CR), Parent(P), Default(D),
221-
Cases(std::move(C)), Prob(Pr) {}
222+
Cases(std::move(C)), Prob(Pr), OmitRangeCheck(false) {}
222223
};
223224

224225
/// Return the range of values within a range.

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,17 +2622,11 @@ void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
26222622
// Subtract the minimum value.
26232623
SDValue SwitchOp = getValue(B.SValue);
26242624
EVT VT = SwitchOp.getValueType();
2625-
SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
2626-
DAG.getConstant(B.First, dl, VT));
2627-
2628-
// Check range.
2629-
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2630-
SDValue RangeCmp = DAG.getSetCC(
2631-
dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
2632-
Sub.getValueType()),
2633-
Sub, DAG.getConstant(B.Range, dl, VT), ISD::SETUGT);
2625+
SDValue RangeSub =
2626+
DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
26342627

26352628
// Determine the type of the test operands.
2629+
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
26362630
bool UsePtrType = false;
26372631
if (!TLI.isTypeLegal(VT)) {
26382632
UsePtrType = true;
@@ -2645,6 +2639,7 @@ void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
26452639
break;
26462640
}
26472641
}
2642+
SDValue Sub = RangeSub;
26482643
if (UsePtrType) {
26492644
VT = TLI.getPointerTy(DAG.getDataLayout());
26502645
Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
@@ -2656,20 +2651,29 @@ void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
26562651

26572652
MachineBasicBlock* MBB = B.Cases[0].ThisBB;
26582653

2659-
addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
2654+
if (!B.OmitRangeCheck)
2655+
addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
26602656
addSuccessorWithProb(SwitchBB, MBB, B.Prob);
26612657
SwitchBB->normalizeSuccProbs();
26622658

2663-
SDValue BrRange = DAG.getNode(ISD::BRCOND, dl,
2664-
MVT::Other, CopyTo, RangeCmp,
2665-
DAG.getBasicBlock(B.Default));
2659+
SDValue Root = CopyTo;
2660+
if (!B.OmitRangeCheck) {
2661+
// Conditional branch to the default block.
2662+
SDValue RangeCmp = DAG.getSetCC(dl,
2663+
TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
2664+
RangeSub.getValueType()),
2665+
RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
2666+
ISD::SETUGT);
2667+
2668+
Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
2669+
DAG.getBasicBlock(B.Default));
2670+
}
26662671

26672672
// Avoid emitting unnecessary branches to the next block.
26682673
if (MBB != NextBlock(SwitchBB))
2669-
BrRange = DAG.getNode(ISD::BR, dl, MVT::Other, BrRange,
2670-
DAG.getBasicBlock(MBB));
2674+
Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
26712675

2672-
DAG.setRoot(BrRange);
2676+
DAG.setRoot(Root);
26732677
}
26742678

26752679
/// visitBitTestCase - this function produces one "bit test"
@@ -10164,8 +10168,6 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
1016410168
break;
1016510169
}
1016610170
case CC_BitTests: {
10167-
// FIXME: If Fallthrough is unreachable, skip the range check.
10168-
1016910171
// FIXME: Optimize away range check based on pivot comparisons.
1017010172
BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
1017110173

@@ -10186,6 +10188,11 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
1018610188
BTB->DefaultProb -= DefaultProb / 2;
1018710189
}
1018810190

10191+
if (FallthroughUnreachable) {
10192+
// Skip the range check if the fallthrough block is unreachable.
10193+
BTB->OmitRangeCheck = true;
10194+
}
10195+
1018910196
// If we're in the right place, emit the bit test header right now.
1019010197
if (CurMBB == SwitchMBB) {
1019110198
visitBitTestHeader(*BTB, SwitchMBB);

test/CodeGen/X86/switch-bt.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,12 @@ sw.epilog:
157157
}
158158

159159

160-
; TODO: Omit the range check when the default case is unreachable, see PR43129.
160+
; Omit the range check when the default case is unreachable, see PR43129.
161161
declare void @g(i32)
162162
define void @test5(i32 %x) {
163163

164164
; CHECK-LABEL: test5
165-
; CHECK: cmpl $8, %edi
166-
; CHECK: ja
165+
; CHECK-NOT: cmp
167166

168167
; 73 = 2^0 + 2^3 + 2^6
169168
; CHECK: movl $73

0 commit comments

Comments
 (0)