Skip to content

Commit b53a5c2

Browse files
committed
fixup! [InstCombine] Relax the same-underlying-object constraint for the GEP canonicalization
1 parent 17baedd commit b53a5c2

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,13 +2477,31 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
24772477
Value *X = GEP.getPointerOperand();
24782478
Value *Y;
24792479
if (match(GEP.getOperand(1),
2480-
m_Sub(m_PtrToInt(m_Value(Y)), m_PtrToInt(m_Specific(X)))) &&
2481-
(all_of(GEP.users(),
2482-
[](User *U) {
2483-
return isa<ICmpInst>(U) || isa<PtrToIntInst>(U);
2484-
}) ||
2485-
getUnderlyingObject(X) == getUnderlyingObject(Y)))
2486-
return CastInst::CreatePointerBitCastOrAddrSpaceCast(Y, GEPType);
2480+
m_Sub(m_PtrToInt(m_Value(Y)), m_PtrToInt(m_Specific(X))))) {
2481+
std::optional<bool> HasSameUnderlyingObjectCache;
2482+
auto HasSameUnderlyingObject = [&] {
2483+
if (!HasSameUnderlyingObjectCache)
2484+
HasSameUnderlyingObjectCache =
2485+
getUnderlyingObject(X) == getUnderlyingObject(Y);
2486+
return *HasSameUnderlyingObjectCache;
2487+
};
2488+
std::optional<Value *> CastedValueCache;
2489+
auto GetCastedValue = [&] {
2490+
if (!CastedValueCache)
2491+
CastedValueCache =
2492+
Builder.CreatePointerBitCastOrAddrSpaceCast(Y, GEPType);
2493+
return *CastedValueCache;
2494+
};
2495+
2496+
bool Changed = false;
2497+
for (User *U : GEP.users())
2498+
if (isa<ICmpInst>(U) || isa<PtrToIntInst>(U) ||
2499+
HasSameUnderlyingObject()) {
2500+
U->replaceUsesOfWith(&GEP, GetCastedValue());
2501+
Changed = true;
2502+
}
2503+
return Changed ? &GEP : nullptr;
2504+
}
24872505
} else {
24882506
// Canonicalize (gep T* X, V / sizeof(T)) to (gep i8* X, V)
24892507
Value *V;

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,13 +1590,13 @@ if.else:
15901590

15911591
define i64 @test_used_by_both_invalid(ptr %a, ptr %b, ptr %c) {
15921592
; CHECK-LABEL: @test_used_by_both_invalid(
1593+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[B:%.*]], [[C:%.*]]
1594+
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1595+
; CHECK: if.then:
1596+
; CHECK-NEXT: [[PB:%.*]] = ptrtoint ptr [[B]] to i64
15931597
; CHECK-NEXT: [[PA:%.*]] = ptrtoint ptr [[A:%.*]] to i64
1594-
; CHECK-NEXT: [[PB:%.*]] = ptrtoint ptr [[B:%.*]] to i64
15951598
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PB]], [[PA]]
15961599
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[A]], i64 [[SUB]]
1597-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[GEP]], [[C:%.*]]
1598-
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1599-
; CHECK: if.then:
16001600
; CHECK-NEXT: [[VAL:%.*]] = load i64, ptr [[GEP]], align 8
16011601
; CHECK-NEXT: ret i64 [[VAL]]
16021602
; CHECK: if.else:

0 commit comments

Comments
 (0)