Skip to content

Commit 44220ba

Browse files
committed
[InstCombine] Don't require GEP in indexed compare fold
The indexed compare fold folds comparisons like p+a == p+b to a == b, even in cases where the a/b are complex (e.g. via multiple geps, or phis). Currently, it requires that the LHS is actually a GEP, but this requirement isn't really necessary: We can handle the pattern p == p+b as well. This patch removes the GEP requirement, allowing additional comparisons to be optimized away.
1 parent 50aacb9 commit 44220ba

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -626,32 +626,34 @@ static Value *rewriteGEPAsOffset(Value *Start, Value *Base, GEPNoWrapFlags NW,
626626
return NewInsts[Start];
627627
}
628628

629-
/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
629+
/// Converts (CMP LHS, RHS) if this change would make RHS a constant.
630630
/// We can look through PHIs, GEPs and casts in order to determine a common base
631-
/// between GEPLHS and RHS.
632-
static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
633-
CmpPredicate Cond,
631+
/// between LHS and RHS.
632+
static Instruction *transformToIndexedCompare(Value *LHS, Value *RHS,
633+
ICmpInst::Predicate Cond,
634634
const DataLayout &DL,
635635
InstCombiner &IC) {
636-
// FIXME: Support vector of pointers.
637-
if (GEPLHS->getType()->isVectorTy())
636+
if (ICmpInst::isSigned(Cond))
638637
return nullptr;
639638

640-
if (!GEPLHS->hasAllConstantIndices())
639+
// FIXME: Support vector of pointers.
640+
if (!LHS->getType()->isPointerTy())
641641
return nullptr;
642642

643-
APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
643+
APInt Offset(DL.getIndexTypeSizeInBits(LHS->getType()), 0);
644644
Value *PtrBase =
645-
GEPLHS->stripAndAccumulateConstantOffsets(DL, Offset,
646-
/*AllowNonInbounds*/ false);
645+
LHS->stripAndAccumulateConstantOffsets(DL, Offset,
646+
/*AllowNonInbounds*/ false);
647647

648648
// Bail if we looked through addrspacecast.
649-
if (PtrBase->getType() != GEPLHS->getType())
649+
if (PtrBase->getType() != LHS->getType())
650650
return nullptr;
651651

652652
// The set of nodes that will take part in this transformation.
653653
SetVector<Value *> Nodes;
654-
GEPNoWrapFlags NW = GEPLHS->getNoWrapFlags();
654+
GEPNoWrapFlags NW = GEPNoWrapFlags::all();
655+
if (auto *GEPLHS = dyn_cast<GEPOperator>(LHS))
656+
NW = GEPLHS->getNoWrapFlags();
655657
if (!canRewriteGEPAsOffset(RHS, PtrBase, NW, DL, Nodes))
656658
return nullptr;
657659

@@ -800,10 +802,7 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
800802
return replaceInstUsesWith(I, Cmp);
801803
}
802804

803-
// Otherwise, the base pointers are different and the indices are
804-
// different. Try convert this to an indexed compare by looking through
805-
// PHIs/casts.
806-
return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
805+
return nullptr;
807806
}
808807

809808
if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands() &&
@@ -851,9 +850,7 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
851850
}
852851
}
853852

854-
// Try convert this to an indexed compare by looking through PHIs/casts as a
855-
// last resort.
856-
return transformToIndexedCompare(GEPLHS, RHS, Cond, DL, *this);
853+
return nullptr;
857854
}
858855

859856
bool InstCombinerImpl::foldAllocaCmp(AllocaInst *Alloca) {
@@ -7312,6 +7309,10 @@ Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
73127309
if (Instruction *NI = foldGEPICmp(GEP, Op1, Pred, CxtI))
73137310
return NI;
73147311

7312+
if (Instruction *Res =
7313+
transformToIndexedCompare(Op0, Op1, Pred, getDataLayout(), *this))
7314+
return Res;
7315+
73157316
if (auto *SI = dyn_cast<SelectInst>(Op0))
73167317
if (Instruction *NI = foldSelectICmp(Pred, SI, Op1, CxtI))
73177318
return NI;

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,15 +686,15 @@ define i32 @test28() nounwind {
686686
; CHECK-NEXT: entry:
687687
; CHECK-NEXT: [[ORIENTATIONS:%.*]] = alloca [1 x [1 x %struct.x]], align 8
688688
; CHECK-NEXT: [[T3:%.*]] = call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str) #[[ATTR0]]
689-
; CHECK-NEXT: [[T45:%.*]] = getelementptr inbounds nuw i8, ptr [[ORIENTATIONS]], i64 1
690689
; CHECK-NEXT: br label [[BB10:%.*]]
691690
; CHECK: bb10:
692691
; CHECK-NEXT: [[INDVAR:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INDVAR_NEXT:%.*]], [[BB10]] ]
693692
; CHECK-NEXT: [[T12_REC:%.*]] = xor i32 [[INDVAR]], -1
694693
; CHECK-NEXT: [[TMP0:%.*]] = sext i32 [[T12_REC]] to i64
695-
; CHECK-NEXT: [[T12:%.*]] = getelementptr inbounds [[STRUCT_X:%.*]], ptr [[T45]], i64 [[TMP0]]
696-
; CHECK-NEXT: [[T16:%.*]] = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str1, ptr nonnull [[T12]]) #[[ATTR0]]
697-
; CHECK-NEXT: [[T84:%.*]] = icmp eq ptr [[T12]], [[ORIENTATIONS]]
694+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[ORIENTATIONS]], i64 [[TMP0]]
695+
; CHECK-NEXT: [[T12_PTR:%.*]] = getelementptr i8, ptr [[TMP1]], i64 1
696+
; CHECK-NEXT: [[T16:%.*]] = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str1, ptr nonnull [[T12_PTR]]) #[[ATTR0]]
697+
; CHECK-NEXT: [[T84:%.*]] = icmp eq i32 [[INDVAR]], 0
698698
; CHECK-NEXT: [[INDVAR_NEXT]] = add i32 [[INDVAR]], 1
699699
; CHECK-NEXT: br i1 [[T84]], label [[BB17:%.*]], label [[BB10]]
700700
; CHECK: bb17:

llvm/test/Transforms/InstCombine/pr39908.ll

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ target datalayout = "p:32:32"
77

88
define i1 @test(ptr %p, i32 %n) {
99
; CHECK-LABEL: @test(
10-
; CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [0 x %S], ptr [[P:%.*]], i32 0, i32 [[N:%.*]], i32 0, i32 0
11-
; CHECK-NEXT: [[LAST:%.*]] = getelementptr inbounds i8, ptr [[END]], i32 -8
12-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[LAST]], [[P]]
10+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[N:%.*]], 1
1311
; CHECK-NEXT: ret i1 [[CMP]]
1412
;
1513
%end = getelementptr inbounds [0 x %S], ptr %p, i32 0, i32 %n, i32 0, i32 0
@@ -22,9 +20,7 @@ define i1 @test(ptr %p, i32 %n) {
2220
define i1 @test64(ptr %p, i64 %n) {
2321
; CHECK-LABEL: @test64(
2422
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[N:%.*]] to i32
25-
; CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [0 x %S], ptr [[P:%.*]], i32 0, i32 [[TMP1]], i32 0, i32 0
26-
; CHECK-NEXT: [[LAST:%.*]] = getelementptr inbounds i8, ptr [[END]], i32 -8
27-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[LAST]], [[P]]
23+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
2824
; CHECK-NEXT: ret i1 [[CMP]]
2925
;
3026
%end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 0
@@ -37,9 +33,7 @@ define i1 @test64(ptr %p, i64 %n) {
3733
define i1 @test64_overflow(ptr %p, i64 %n) {
3834
; CHECK-LABEL: @test64_overflow(
3935
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[N:%.*]] to i32
40-
; CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [0 x %S], ptr [[P:%.*]], i32 0, i32 [[TMP1]], i32 0, i32 0
41-
; CHECK-NEXT: [[LAST:%.*]] = getelementptr inbounds i8, ptr [[END]], i32 -8
42-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[LAST]], [[P]]
36+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP1]], 1
4337
; CHECK-NEXT: ret i1 [[CMP]]
4438
;
4539
%end = getelementptr inbounds [0 x %S], ptr %p, i64 0, i64 %n, i32 0, i64 8589934592

llvm/test/Transforms/PhaseOrdering/loop-access-checks.ll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ define void @test_fill_with_foreach([2 x i64] %elems.coerce) {
2424
; CHECK-NEXT: [[ELEMS_COERCE_FCA_0_EXTRACT:%.*]] = extractvalue [2 x i64] [[ELEMS_COERCE]], 0
2525
; CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[ELEMS_COERCE_FCA_0_EXTRACT]] to ptr
2626
; CHECK-NEXT: [[ELEMS_COERCE_FCA_1_EXTRACT:%.*]] = extractvalue [2 x i64] [[ELEMS_COERCE]], 1
27-
; CHECK-NEXT: [[ADD_PTR_I:%.*]] = getelementptr inbounds i32, ptr [[TMP0]], i64 [[ELEMS_COERCE_FCA_1_EXTRACT]]
27+
; CHECK-NEXT: [[ADD_PTR_I_IDX:%.*]] = shl nsw i64 [[ELEMS_COERCE_FCA_1_EXTRACT]], 2
2828
; CHECK-NEXT: [[CMP_NOT_I_I_I_I:%.*]] = icmp slt i64 [[ELEMS_COERCE_FCA_1_EXTRACT]], 0
2929
; CHECK-NEXT: br i1 [[CMP_NOT_I_I_I_I]], label [[ERROR:%.*]], label [[FOR_COND_PREHEADER_SPLIT:%.*]]
3030
; CHECK: for.cond.preheader.split:
@@ -36,10 +36,11 @@ define void @test_fill_with_foreach([2 x i64] %elems.coerce) {
3636
; CHECK-NEXT: tail call void @error()
3737
; CHECK-NEXT: br label [[COMMON_RET]]
3838
; CHECK: for.body:
39-
; CHECK-NEXT: [[__BEGIN1_SROA_0_03:%.*]] = phi ptr [ [[INCDEC_PTR_I:%.*]], [[FOR_BODY]] ], [ [[TMP0]], [[FOR_COND_PREHEADER_SPLIT]] ]
40-
; CHECK-NEXT: tail call void @use(ptr noundef nonnull align 4 dereferenceable(4) [[__BEGIN1_SROA_0_03]])
41-
; CHECK-NEXT: [[INCDEC_PTR_I]] = getelementptr inbounds nuw i8, ptr [[__BEGIN1_SROA_0_03]], i64 4
42-
; CHECK-NEXT: [[CMP_I_NOT:%.*]] = icmp eq ptr [[INCDEC_PTR_I]], [[ADD_PTR_I]]
39+
; CHECK-NEXT: [[__BEGIN1_SROA_0_0_IDX3:%.*]] = phi i64 [ [[__BEGIN1_SROA_0_0_ADD:%.*]], [[FOR_BODY]] ], [ 0, [[FOR_COND_PREHEADER_SPLIT]] ]
40+
; CHECK-NEXT: [[__BEGIN1_SROA_0_0_PTR4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0]], i64 [[__BEGIN1_SROA_0_0_IDX3]]
41+
; CHECK-NEXT: tail call void @use(ptr noundef nonnull align 4 dereferenceable(4) [[__BEGIN1_SROA_0_0_PTR4]])
42+
; CHECK-NEXT: [[__BEGIN1_SROA_0_0_ADD]] = add nuw nsw i64 [[__BEGIN1_SROA_0_0_IDX3]], 4
43+
; CHECK-NEXT: [[CMP_I_NOT:%.*]] = icmp eq i64 [[__BEGIN1_SROA_0_0_ADD]], [[ADD_PTR_I_IDX]]
4344
; CHECK-NEXT: br i1 [[CMP_I_NOT]], label [[COMMON_RET]], label [[FOR_BODY]]
4445
;
4546
entry:

0 commit comments

Comments
 (0)