@@ -2477,29 +2477,18 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
2477
2477
Value *X = GEP.getPointerOperand ();
2478
2478
Value *Y;
2479
2479
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);
2496
2484
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
+ });
2503
2492
return Changed ? &GEP : nullptr ;
2504
2493
}
2505
2494
} else {
0 commit comments