Skip to content

[InstCombine] Relax the same-underlying-object constraint for the GEP canonicalization #76583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2471,14 +2471,26 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {

if (TyAllocSize == 1) {
// Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X)) to (bitcast Y),
// but only if both point to the same underlying object (otherwise
// provenance is not necessarily retained).
// but only if the result pointer is only used as if it were an integer,
// or both point to the same underlying object (otherwise provenance is
// not necessarily retained).
Value *X = GEP.getPointerOperand();
Value *Y;
if (match(GEP.getOperand(1),
m_Sub(m_PtrToInt(m_Value(Y)), m_PtrToInt(m_Specific(X)))) &&
getUnderlyingObject(X) == getUnderlyingObject(Y))
return CastInst::CreatePointerBitCastOrAddrSpaceCast(Y, GEPType);
GEPType == Y->getType()) {
bool HasSameUnderlyingObject =
getUnderlyingObject(X) == getUnderlyingObject(Y);
bool Changed = false;
GEP.replaceUsesWithIf(Y, [&](Use &U) {
bool ShouldReplace = HasSameUnderlyingObject ||
isa<ICmpInst>(U.getUser()) ||
isa<PtrToIntInst>(U.getUser());
Changed |= ShouldReplace;
return ShouldReplace;
});
return Changed ? &GEP : nullptr;
}
} else {
// Canonicalize (gep T* X, V / sizeof(T)) to (gep i8* X, V)
Value *V;
Expand Down
78 changes: 78 additions & 0 deletions llvm/test/Transforms/InstCombine/getelementptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1537,4 +1537,82 @@ define ptr @gep_ashr_without_exact(ptr %p, i64 %off) {
ret ptr %ptr
}

define i1 @test_only_used_by_icmp(ptr %a, ptr %b, ptr %c) {
; CHECK-LABEL: @test_only_used_by_icmp(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: ret i1 [[CMP]]
;
%pa = ptrtoint ptr %a to i64
%pb = ptrtoint ptr %b to i64
%sub = sub i64 %pb, %pa
%gep = getelementptr i8, ptr %a, i64 %sub
%cmp = icmp eq ptr %gep, %c
ret i1 %cmp
}

define i64 @test_only_used_by_ptrtoint(ptr %a, ptr %b) {
; CHECK-LABEL: @test_only_used_by_ptrtoint(
; CHECK-NEXT: [[VAL:%.*]] = ptrtoint ptr [[B:%.*]] to i64
; CHECK-NEXT: ret i64 [[VAL]]
;
%pa = ptrtoint ptr %a to i64
%pb = ptrtoint ptr %b to i64
%sub = sub i64 %pb, %pa
%gep = getelementptr i8, ptr %a, i64 %sub
%val = ptrtoint ptr %gep to i64
ret i64 %val
}

define i64 @test_used_by_both(ptr %a, ptr %b, ptr %c) {
; CHECK-LABEL: @test_used_by_both(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[VAL:%.*]] = ptrtoint ptr [[B]] to i64
; CHECK-NEXT: ret i64 [[VAL]]
; CHECK: if.else:
; CHECK-NEXT: ret i64 0
;
%pa = ptrtoint ptr %a to i64
%pb = ptrtoint ptr %b to i64
%sub = sub i64 %pb, %pa
%gep = getelementptr i8, ptr %a, i64 %sub
%cmp = icmp eq ptr %gep, %c
br i1 %cmp, label %if.then, label %if.else
if.then:
%val = ptrtoint ptr %gep to i64
ret i64 %val
if.else:
ret i64 0
}

; Negative tests

define i64 @test_used_by_both_invalid(ptr %a, ptr %b, ptr %c) {
; CHECK-LABEL: @test_used_by_both_invalid(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[PB:%.*]] = ptrtoint ptr [[B]] to i64
; CHECK-NEXT: [[PA:%.*]] = ptrtoint ptr [[A:%.*]] to i64
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PB]], [[PA]]
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[A]], i64 [[SUB]]
; CHECK-NEXT: [[VAL:%.*]] = load i64, ptr [[GEP]], align 8
; CHECK-NEXT: ret i64 [[VAL]]
; CHECK: if.else:
; CHECK-NEXT: ret i64 0
;
%pa = ptrtoint ptr %a to i64
%pb = ptrtoint ptr %b to i64
%sub = sub i64 %pb, %pa
%gep = getelementptr i8, ptr %a, i64 %sub
%cmp = icmp eq ptr %gep, %c
br i1 %cmp, label %if.then, label %if.else
if.then:
%val = load i64, ptr %gep, align 8
ret i64 %val
if.else:
ret i64 0
}

!0 = !{!"branch_weights", i32 2, i32 10}