Skip to content

Commit b273470

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

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,29 +2477,18 @@ 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-
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-
2480+
m_Sub(m_PtrToInt(m_Value(Y)), m_PtrToInt(m_Specific(X)))) &&
2481+
GEPType == Y->getType()) {
2482+
bool HasSameUnderlyingObject =
2483+
getUnderlyingObject(X) == getUnderlyingObject(Y);
24962484
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-
}
2485+
GEP.replaceUsesWithIf(Y, [&](Use &U) {
2486+
bool ShouldReplace = HasSameUnderlyingObject ||
2487+
isa<ICmpInst>(U.getUser()) ||
2488+
isa<PtrToIntInst>(U.getUser());
2489+
Changed |= ShouldReplace;
2490+
return ShouldReplace;
2491+
});
25032492
return Changed ? &GEP : nullptr;
25042493
}
25052494
} else {

0 commit comments

Comments
 (0)