|
13 | 13 | #include "llvm/Analysis/Loads.h"
|
14 | 14 | #include "llvm/Analysis/AliasAnalysis.h"
|
15 | 15 | #include "llvm/Analysis/AssumeBundleQueries.h"
|
| 16 | +#include "llvm/Analysis/LoopAccessAnalysis.h" |
16 | 17 | #include "llvm/Analysis/LoopInfo.h"
|
17 | 18 | #include "llvm/Analysis/MemoryBuiltins.h"
|
18 | 19 | #include "llvm/Analysis/MemoryLocation.h"
|
@@ -276,84 +277,85 @@ static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
|
276 | 277 | bool llvm::isDereferenceableAndAlignedInLoop(
|
277 | 278 | LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT,
|
278 | 279 | AssumptionCache *AC, SmallVectorImpl<const SCEVPredicate *> *Predicates) {
|
279 |
| - auto &DL = LI->getDataLayout(); |
280 |
| - Value *Ptr = LI->getPointerOperand(); |
281 |
| - |
282 |
| - APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()), |
283 |
| - DL.getTypeStoreSize(LI->getType()).getFixedValue()); |
284 |
| - const Align Alignment = LI->getAlign(); |
| 280 | + const SCEV *Ptr = SE.getSCEV(LI->getPointerOperand()); |
| 281 | + auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ptr); |
285 | 282 |
|
286 |
| - Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI(); |
287 |
| - |
288 |
| - // If given a uniform (i.e. non-varying) address, see if we can prove the |
289 |
| - // access is safe within the loop w/o needing predication. |
290 |
| - if (L->isLoopInvariant(Ptr)) |
291 |
| - return isDereferenceableAndAlignedPointer(Ptr, Alignment, EltSize, DL, |
292 |
| - HeaderFirstNonPHI, AC, &DT); |
293 |
| - |
294 |
| - // Otherwise, check to see if we have a repeating access pattern where we can |
295 |
| - // prove that all accesses are well aligned and dereferenceable. |
296 |
| - auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Ptr)); |
| 283 | + // Check to see if we have a repeating access pattern and it's possible |
| 284 | + // to prove all accesses are well aligned. |
297 | 285 | if (!AddRec || AddRec->getLoop() != L || !AddRec->isAffine())
|
298 | 286 | return false;
|
| 287 | + |
299 | 288 | auto* Step = dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(SE));
|
300 | 289 | if (!Step)
|
301 | 290 | return false;
|
302 | 291 |
|
303 |
| - auto TC = SE.getSmallConstantMaxTripCount(L, Predicates); |
304 |
| - if (!TC) |
| 292 | + // For the moment, restrict ourselves to the case where the access size is a |
| 293 | + // multiple of the requested alignment and the base is aligned. |
| 294 | + // TODO: generalize if a case found which warrants |
| 295 | + const Align Alignment = LI->getAlign(); |
| 296 | + auto &DL = LI->getDataLayout(); |
| 297 | + APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()), |
| 298 | + DL.getTypeStoreSize(LI->getType()).getFixedValue()); |
| 299 | + if (EltSize.urem(Alignment.value()) != 0) |
305 | 300 | return false;
|
306 | 301 |
|
307 | 302 | // TODO: Handle overlapping accesses.
|
308 |
| - // We should be computing AccessSize as (TC - 1) * Step + EltSize. |
309 |
| - if (EltSize.sgt(Step->getAPInt())) |
| 303 | + if (EltSize.ugt(Step->getAPInt().abs())) |
310 | 304 | return false;
|
311 | 305 |
|
312 |
| - // Compute the total access size for access patterns with unit stride and |
313 |
| - // patterns with gaps. For patterns with unit stride, Step and EltSize are the |
314 |
| - // same. |
315 |
| - // For patterns with gaps (i.e. non unit stride), we are |
316 |
| - // accessing EltSize bytes at every Step. |
317 |
| - APInt AccessSize = TC * Step->getAPInt(); |
| 306 | + const SCEV *MaxBECount = |
| 307 | + SE.getPredicatedSymbolicMaxBackedgeTakenCount(L, *Predicates); |
| 308 | + if (isa<SCEVCouldNotCompute>(MaxBECount)) |
| 309 | + return false; |
318 | 310 |
|
319 |
| - assert(SE.isLoopInvariant(AddRec->getStart(), L) && |
320 |
| - "implied by addrec definition"); |
321 |
| - Value *Base = nullptr; |
322 |
| - if (auto *StartS = dyn_cast<SCEVUnknown>(AddRec->getStart())) { |
323 |
| - Base = StartS->getValue(); |
324 |
| - } else if (auto *StartS = dyn_cast<SCEVAddExpr>(AddRec->getStart())) { |
325 |
| - // Handle (NewBase + offset) as start value. |
326 |
| - const auto *Offset = dyn_cast<SCEVConstant>(StartS->getOperand(0)); |
327 |
| - const auto *NewBase = dyn_cast<SCEVUnknown>(StartS->getOperand(1)); |
328 |
| - if (StartS->getNumOperands() == 2 && Offset && NewBase) { |
329 |
| - // The following code below assumes the offset is unsigned, but GEP |
330 |
| - // offsets are treated as signed so we can end up with a signed value |
331 |
| - // here too. For example, suppose the initial PHI value is (i8 255), |
332 |
| - // the offset will be treated as (i8 -1) and sign-extended to (i64 -1). |
333 |
| - if (Offset->getAPInt().isNegative()) |
334 |
| - return false; |
| 311 | + const auto &[AccessStart, AccessEnd] = |
| 312 | + getStartAndEndForAccess(L, Ptr, LI->getType(), MaxBECount, &SE, nullptr); |
| 313 | + if (isa<SCEVCouldNotCompute>(AccessStart) || |
| 314 | + isa<SCEVCouldNotCompute>(AccessEnd)) |
| 315 | + return false; |
335 | 316 |
|
336 |
| - // For the moment, restrict ourselves to the case where the offset is a |
337 |
| - // multiple of the requested alignment and the base is aligned. |
338 |
| - // TODO: generalize if a case found which warrants |
339 |
| - if (Offset->getAPInt().urem(Alignment.value()) != 0) |
340 |
| - return false; |
341 |
| - Base = NewBase->getValue(); |
342 |
| - bool Overflow = false; |
343 |
| - AccessSize = AccessSize.uadd_ov(Offset->getAPInt(), Overflow); |
344 |
| - if (Overflow) |
345 |
| - return false; |
346 |
| - } |
347 |
| - } |
| 317 | + // Try to get the access size. |
| 318 | + const SCEV *PtrDiff = SE.getMinusSCEV(AccessEnd, AccessStart); |
| 319 | + APInt MaxPtrDiff = SE.getUnsignedRangeMax(PtrDiff); |
348 | 320 |
|
349 |
| - if (!Base) |
| 321 | + // If the (max) pointer difference is > 32 bits then it's unlikely to be |
| 322 | + // dereferenceable. |
| 323 | + if (MaxPtrDiff.getActiveBits() > 32) |
350 | 324 | return false;
|
351 | 325 |
|
352 |
| - // For the moment, restrict ourselves to the case where the access size is a |
353 |
| - // multiple of the requested alignment and the base is aligned. |
354 |
| - // TODO: generalize if a case found which warrants |
355 |
| - if (EltSize.urem(Alignment.value()) != 0) |
| 326 | + Value *Base = nullptr; |
| 327 | + APInt AccessSize; |
| 328 | + if (const SCEVUnknown *NewBase = dyn_cast<SCEVUnknown>(AccessStart)) { |
| 329 | + Base = NewBase->getValue(); |
| 330 | + AccessSize = MaxPtrDiff; |
| 331 | + } else if (auto *MinAdd = dyn_cast<SCEVAddExpr>(AccessStart)) { |
| 332 | + if (MinAdd->getNumOperands() != 2) |
| 333 | + return false; |
| 334 | + |
| 335 | + const auto *Offset = dyn_cast<SCEVConstant>(MinAdd->getOperand(0)); |
| 336 | + const auto *NewBase = dyn_cast<SCEVUnknown>(MinAdd->getOperand(1)); |
| 337 | + if (!Offset || !NewBase) |
| 338 | + return false; |
| 339 | + |
| 340 | + // The following code below assumes the offset is unsigned, but GEP |
| 341 | + // offsets are treated as signed so we can end up with a signed value |
| 342 | + // here too. For example, suppose the initial PHI value is (i8 255), |
| 343 | + // the offset will be treated as (i8 -1) and sign-extended to (i64 -1). |
| 344 | + if (Offset->getAPInt().isNegative()) |
| 345 | + return false; |
| 346 | + |
| 347 | + // For the moment, restrict ourselves to the case where the offset is a |
| 348 | + // multiple of the requested alignment and the base is aligned. |
| 349 | + // TODO: generalize if a case found which warrants |
| 350 | + if (Offset->getAPInt().urem(Alignment.value()) != 0) |
| 351 | + return false; |
| 352 | + |
| 353 | + AccessSize = MaxPtrDiff + Offset->getAPInt(); |
| 354 | + Base = NewBase->getValue(); |
| 355 | + } else |
356 | 356 | return false;
|
| 357 | + |
| 358 | + Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI(); |
357 | 359 | return isDereferenceableAndAlignedPointer(Base, Alignment, AccessSize, DL,
|
358 | 360 | HeaderFirstNonPHI, AC, &DT);
|
359 | 361 | }
|
|
0 commit comments