@@ -276,8 +276,20 @@ static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
276
276
bool llvm::isDereferenceableAndAlignedInLoop (
277
277
LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT,
278
278
AssumptionCache *AC, SmallVectorImpl<const SCEVPredicate *> *Predicates) {
279
- const SCEV *Ptr = SE.getSCEV (LI->getPointerOperand ());
280
- auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ptr);
279
+ const Align Alignment = LI->getAlign ();
280
+ auto &DL = LI->getDataLayout ();
281
+ Value *Ptr = LI->getPointerOperand ();
282
+ APInt EltSize (DL.getIndexTypeSizeInBits (Ptr->getType ()),
283
+ DL.getTypeStoreSize (LI->getType ()).getFixedValue ());
284
+
285
+ // If given a uniform (i.e. non-varying) address, see if we can prove the
286
+ // access is safe within the loop w/o needing predication.
287
+ if (L->isLoopInvariant (Ptr))
288
+ return isDereferenceableAndAlignedPointer (
289
+ Ptr, Alignment, EltSize, DL, L->getHeader ()->getFirstNonPHI (), AC, &DT);
290
+
291
+ const SCEV *PtrScev = SE.getSCEV (Ptr);
292
+ auto *AddRec = dyn_cast<SCEVAddRecExpr>(PtrScev);
281
293
282
294
// Check to see if we have a repeating access pattern and it's possible
283
295
// to prove all accesses are well aligned.
@@ -291,10 +303,6 @@ bool llvm::isDereferenceableAndAlignedInLoop(
291
303
// For the moment, restrict ourselves to the case where the access size is a
292
304
// multiple of the requested alignment and the base is aligned.
293
305
// TODO: generalize if a case found which warrants
294
- const Align Alignment = LI->getAlign ();
295
- auto &DL = LI->getDataLayout ();
296
- APInt EltSize (DL.getIndexTypeSizeInBits (Ptr->getType ()),
297
- DL.getTypeStoreSize (LI->getType ()).getFixedValue ());
298
306
if (EltSize.urem (Alignment.value ()) != 0 )
299
307
return false ;
300
308
@@ -307,8 +315,8 @@ bool llvm::isDereferenceableAndAlignedInLoop(
307
315
if (isa<SCEVCouldNotCompute>(MaxBECount))
308
316
return false ;
309
317
310
- const auto &[AccessStart, AccessEnd] =
311
- getStartAndEndForAccess ( L, Ptr , LI->getType (), MaxBECount, &SE, nullptr );
318
+ const auto &[AccessStart, AccessEnd] = getStartAndEndForAccess (
319
+ L, PtrScev , LI->getType (), MaxBECount, &SE, nullptr );
312
320
if (isa<SCEVCouldNotCompute>(AccessStart) ||
313
321
isa<SCEVCouldNotCompute>(AccessEnd))
314
322
return false ;
0 commit comments