@@ -2092,30 +2092,31 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
2092
2092
2093
2093
// To avoid duplicating the offset arithmetic, rewrite the GEP to use the
2094
2094
// computed offset. This may erase the original GEP, so be sure to cache the
2095
- // inbounds flag before emitting the offset.
2095
+ // nowrap flags before emitting the offset.
2096
2096
// TODO: We should probably do this even if there is only one GEP.
2097
2097
bool RewriteGEPs = GEP2 != nullptr ;
2098
2098
2099
2099
// Emit the offset of the GEP and an intptr_t.
2100
- bool GEP1IsInBounds = GEP1->isInBounds ();
2100
+ GEPNoWrapFlags GEP1NW = GEP1->getNoWrapFlags ();
2101
2101
Value *Result = EmitGEPOffset (GEP1, RewriteGEPs);
2102
2102
2103
2103
// If this is a single inbounds GEP and the original sub was nuw,
2104
2104
// then the final multiplication is also nuw.
2105
2105
if (auto *I = dyn_cast<Instruction>(Result))
2106
- if (IsNUW && !GEP2 && !Swapped && GEP1IsInBounds &&
2106
+ if (IsNUW && !GEP2 && !Swapped && GEP1NW. isInBounds () &&
2107
2107
I->getOpcode () == Instruction::Mul)
2108
2108
I->setHasNoUnsignedWrap ();
2109
2109
2110
2110
// If we have a 2nd GEP of the same base pointer, subtract the offsets.
2111
2111
// If both GEPs are inbounds, then the subtract does not have signed overflow.
2112
2112
// If both GEPs are nuw and the original sub is nuw, the new sub is also nuw.
2113
2113
if (GEP2) {
2114
+ GEPNoWrapFlags GEP2NW = GEP2->getNoWrapFlags ();
2114
2115
Value *Offset = EmitGEPOffset (GEP2, RewriteGEPs);
2115
2116
Result = Builder.CreateSub (Result, Offset, " gepdiff" ,
2116
- IsNUW && GEP1-> hasNoUnsignedWrap () &&
2117
- GEP2-> hasNoUnsignedWrap (),
2118
- GEP1IsInBounds && GEP2-> isInBounds ());
2117
+ IsNUW && GEP1NW. hasNoUnsignedWrap () &&
2118
+ GEP2NW. hasNoUnsignedWrap (),
2119
+ GEP1NW. isInBounds () && GEP2NW. isInBounds ());
2119
2120
}
2120
2121
2121
2122
// If we have p - gep(p, ...) then we have to negate the result.
0 commit comments