@@ -193,17 +193,19 @@ RuntimeCheckingPtrGroup::RuntimeCheckingPtrGroup(
193
193
// / Returns \p A + \p B, if it is guaranteed not to unsigned wrap. Otherwise
194
194
// / return nullptr. \p A and \p B must have the same type.
195
195
static const SCEV *addSCEVNoOverflow (const SCEV *A, const SCEV *B,
196
- ScalarEvolution &SE) {
197
- if (!SE.willNotOverflow (Instruction::Add, /* IsSigned=*/ false , A, B))
196
+ ScalarEvolution &SE,
197
+ const Instruction *CtxI) {
198
+ if (!SE.willNotOverflow (Instruction::Add, /* IsSigned=*/ false , A, B, CtxI))
198
199
return nullptr ;
199
200
return SE.getAddExpr (A, B);
200
201
}
201
202
202
203
// / Returns \p A * \p B, if it is guaranteed not to unsigned wrap. Otherwise
203
204
// / return nullptr. \p A and \p B must have the same type.
204
205
static const SCEV *mulSCEVOverflow (const SCEV *A, const SCEV *B,
205
- ScalarEvolution &SE) {
206
- if (!SE.willNotOverflow (Instruction::Mul, /* IsSigned=*/ false , A, B))
206
+ ScalarEvolution &SE,
207
+ const Instruction *CtxI) {
208
+ if (!SE.willNotOverflow (Instruction::Mul, /* IsSigned=*/ false , A, B, CtxI))
207
209
return nullptr ;
208
210
return SE.getMulExpr (A, B);
209
211
}
@@ -232,11 +234,12 @@ evaluatePtrAddRecAtMaxBTCWillNotWrap(const SCEVAddRecExpr *AR,
232
234
Type *WiderTy = SE.getWiderType (MaxBTC->getType (), Step->getType ());
233
235
const SCEV *DerefBytesSCEV = SE.getConstant (WiderTy, DerefBytes);
234
236
237
+ // Context which dominates the entire loop.
238
+ auto *CtxI = L->getLoopPredecessor ()->getTerminator ();
235
239
// Check if we have a suitable dereferencable assumption we can use.
236
240
if (!StartPtrV->canBeFreed ()) {
237
241
RetainedKnowledge DerefRK = getKnowledgeValidInContext (
238
- StartPtrV, {Attribute::Dereferenceable}, *AC,
239
- L->getLoopPredecessor ()->getTerminator (), DT);
242
+ StartPtrV, {Attribute::Dereferenceable}, *AC, CtxI, DT);
240
243
if (DerefRK) {
241
244
DerefBytesSCEV = SE.getUMaxExpr (
242
245
DerefBytesSCEV, SE.getConstant (WiderTy, DerefRK.ArgValue ));
@@ -260,20 +263,21 @@ evaluatePtrAddRecAtMaxBTCWillNotWrap(const SCEVAddRecExpr *AR,
260
263
SE.getMinusSCEV (AR->getStart (), StartPtr), WiderTy);
261
264
262
265
const SCEV *OffsetAtLastIter =
263
- mulSCEVOverflow (MaxBTC, SE.getAbsExpr (Step, /* IsNSW=*/ false ), SE);
266
+ mulSCEVOverflow (MaxBTC, SE.getAbsExpr (Step, /* IsNSW=*/ false ), SE, CtxI );
264
267
if (!OffsetAtLastIter)
265
268
return false ;
266
269
267
270
const SCEV *OffsetEndBytes = addSCEVNoOverflow (
268
- OffsetAtLastIter, SE.getNoopOrZeroExtend (EltSize, WiderTy), SE);
271
+ OffsetAtLastIter, SE.getNoopOrZeroExtend (EltSize, WiderTy), SE, CtxI );
269
272
if (!OffsetEndBytes)
270
273
return false ;
271
274
272
275
if (IsKnownNonNegative) {
273
276
// For positive steps, check if
274
277
// (AR->getStart() - StartPtr) + (MaxBTC * Step) + EltSize <= DerefBytes,
275
278
// while making sure none of the computations unsigned wrap themselves.
276
- const SCEV *EndBytes = addSCEVNoOverflow (StartOffset, OffsetEndBytes, SE);
279
+ const SCEV *EndBytes =
280
+ addSCEVNoOverflow (StartOffset, OffsetEndBytes, SE, CtxI);
277
281
if (!EndBytes)
278
282
return false ;
279
283
return SE.isKnownPredicate (CmpInst::ICMP_ULE, EndBytes, DerefBytesSCEV);
0 commit comments