Skip to content

Commit cbefc36

Browse files
committed
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 llvm-svn: 373431
1 parent 70f7003 commit cbefc36

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

llvm/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.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 23 additions & 17 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);
@@ -2660,16 +2655,24 @@ void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
26602655
addSuccessorWithProb(SwitchBB, MBB, B.Prob);
26612656
SwitchBB->normalizeSuccProbs();
26622657

2663-
SDValue BrRange = DAG.getNode(ISD::BRCOND, dl,
2664-
MVT::Other, CopyTo, RangeCmp,
2665-
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+
}
26662670

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

2672-
DAG.setRoot(BrRange);
2675+
DAG.setRoot(Root);
26732676
}
26742677

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

@@ -10186,6 +10187,11 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
1018610187
BTB->DefaultProb -= DefaultProb / 2;
1018710188
}
1018810189

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

llvm/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)