Skip to content

Commit d561b6f

Browse files
committed
[InstCombine] Fold (select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (select C, Idx, 0)) (PR50183) (REAPPLIED)
As discussed on PR50183, we already fold to prefer 'select-of-idx' vs 'select-of-gep': define <4 x i32>* @select0a(<4 x i32>* %a0, i64 %a1, i1 %a2, i64 %a3) { %gep0 = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %a1 %gep1 = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %a3 %sel = select i1 %a2, <4 x i32>* %gep0, <4 x i32>* %gep1 ret <4 x i32>* %sel } --> define <4 x i32>* @select1a(<4 x i32>* %a0, i64 %a1, i1 %a2, i64 %a3) { %sel = select i1 %a2, i64 %a1, i64 %a3 %gep = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %sel ret <4 x i32>* %gep } This patch adds basic handling for the 'fallthrough' cases where the gep idx == 0 has been folded away to the base address: define <4 x i32>* @select0(<4 x i32>* %a0, i64 %a1, i1 %a2) { %gep = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %a1 %sel = select i1 %a2, <4 x i32>* %a0, <4 x i32>* %gep ret <4 x i32>* %sel } --> define <4 x i32>* @Select1(<4 x i32>* %a0, i64 %a1, i1 %a2) { %sel = select i1 %a2, i64 0, i64 %a1 %gep = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %sel ret <4 x i32>* %gep } Reapplied with a fix for the bpf "-bpf-disable-avoid-speculation" tests Differential Revision: https://reviews.llvm.org/D105901
1 parent 12d04ce commit d561b6f

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,6 +2933,33 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
29332933
if (Instruction *I = foldSelectExtConst(SI))
29342934
return I;
29352935

2936+
// Fold (select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (select C, Idx, 0))
2937+
// Fold (select C, Ptr, (gep Ptr, Idx)) -> (gep Ptr, (select C, 0, Idx))
2938+
auto SelectGepWithBase = [&](GetElementPtrInst *Gep, Value *Base,
2939+
bool Swap) -> GetElementPtrInst * {
2940+
Value *Ptr = Gep->getPointerOperand();
2941+
if (Gep->getNumOperands() != 2 || Gep->getPointerOperand() != Base ||
2942+
!Gep->hasOneUse())
2943+
return nullptr;
2944+
Type *ElementType = Gep->getResultElementType();
2945+
Value *Idx = Gep->getOperand(1);
2946+
Value *NewT = Idx;
2947+
Value *NewF = Constant::getNullValue(Idx->getType());
2948+
if (Swap)
2949+
std::swap(NewT, NewF);
2950+
Value *NewSI =
2951+
Builder.CreateSelect(CondVal, NewT, NewF, SI.getName() + ".idx", &SI);
2952+
return Gep->isInBounds()
2953+
? GetElementPtrInst::CreateInBounds(ElementType, Ptr, {NewSI})
2954+
: GetElementPtrInst::Create(ElementType, Ptr, {NewSI});
2955+
};
2956+
if (auto *TrueGep = dyn_cast<GetElementPtrInst>(TrueVal))
2957+
if (auto *NewGep = SelectGepWithBase(TrueGep, FalseVal, false))
2958+
return NewGep;
2959+
if (auto *FalseGep = dyn_cast<GetElementPtrInst>(FalseVal))
2960+
if (auto *NewGep = SelectGepWithBase(FalseGep, TrueVal, true))
2961+
return NewGep;
2962+
29362963
// See if we can fold the select into one of our operands.
29372964
if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) {
29382965
if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))

llvm/test/CodeGen/BPF/adjust-opt-speculative1.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ if.end: ; preds = %if.then, %entry
4949
; CHECK: [[LABEL]]:
5050
; CHECK: r0 = [[REG6]]
5151

52-
; CHECK-DISABLE: r0 = [[REG6]]
53-
; CHECK-DISABLE: r0 += [[REG1:r[0-9]+]]
54-
; CHECK-DISABLE: [[REG2:r[0-9]+]] = 8
55-
; CHECK-DISABLE: if [[REG2]] > [[REG1]] goto [[LABEL:.*]]
56-
; CHECK-DISABLE: r0 = [[REG6]]
52+
; CHECK-DISABLE: [[REG1:r[0-9]+]] = 8
53+
; CHECK-DISABLE: if [[REG1]] > r0 goto [[LABEL:.*]]
54+
; CHECK-DISABLE: r0 = 0
5755
; CHECK-DISABLE: [[LABEL]]:
56+
; CHECK-DISABLE: [[REG6]] += r0
57+
; CHECK-DISABLE: r0 = [[REG6]]
5858

5959
; CHECK-COMMON: exit
6060

llvm/test/CodeGen/BPF/adjust-opt-speculative2.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ if.end: ; preds = %if.then, %entry
5656
; CHECK-DISABLE: [[REG1:r[0-9]+]] = r0
5757
; CHECK-DISABLE: [[REG1]] <<= 32
5858
; CHECK-DISABLE: [[REG1]] >>= 32
59-
; CHECK-DISABLE: r0 = [[REG6]]
60-
; CHECK-DISABLE: r0 += [[REG1]]
6159
; CHECK-DISABLE: [[REG2:r[0-9]+]] = 8
6260
; CHECK-DISABLE: if [[REG2]] > [[REG1]] goto [[LABEL:.*]]
63-
; CHECK-DISABLE: r0 = [[REG6]]
61+
; CHECK-DISABLE: r0 = 0
6462
; CHECK-DISABLE: [[LABEL]]:
63+
; CHECK-DISABLE: r0 <<= 32
64+
; CHECK-DISABLE: r0 >>= 32
65+
; CHECK-DISABLE: [[REG6]] += r0
66+
; CHECK-DISABLE: r0 = [[REG6]]
6567

6668
; CHECK-COMMON: exit
6769

llvm/test/Transforms/InstCombine/select-gep.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ define i32* @test2(i32* %p, i64 %x, i64 %y) {
7474
; PR50183
7575
define i32* @test2a(i32* %p, i64 %x, i64 %y) {
7676
; CHECK-LABEL: @test2a(
77-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[X:%.*]]
78-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y:%.*]]
79-
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i32* [[GEP]], i32* [[P]]
77+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
78+
; CHECK-NEXT: [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 [[X]], i64 0
79+
; CHECK-NEXT: [[SELECT:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
8080
; CHECK-NEXT: ret i32* [[SELECT]]
8181
;
8282
%gep = getelementptr inbounds i32, i32* %p, i64 %x
@@ -88,9 +88,9 @@ define i32* @test2a(i32* %p, i64 %x, i64 %y) {
8888
; PR50183
8989
define i32* @test2b(i32* %p, i64 %x, i64 %y) {
9090
; CHECK-LABEL: @test2b(
91-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[X:%.*]]
92-
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X]], [[Y:%.*]]
93-
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i32* [[P]], i32* [[GEP]]
91+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
92+
; CHECK-NEXT: [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 0, i64 [[X]]
93+
; CHECK-NEXT: [[SELECT:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
9494
; CHECK-NEXT: ret i32* [[SELECT]]
9595
;
9696
%gep = getelementptr inbounds i32, i32* %p, i64 %x

0 commit comments

Comments
 (0)