[llvm] [SCEV] Infer loop max trip count from memory accesses (PR #70361)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 31 09:05:22 PDT 2023
================
@@ -8191,6 +8204,133 @@ ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
return getSmallConstantTripMultiple(L, ExitCount);
}
+/// Collect all load/store instructions that must be executed in every iteration
+/// of loop \p L .
+static void
+collectExecLoadStoreInsideLoop(const Loop *L, DominatorTree &DT,
+ SmallVector<Instruction *, 4> &MemInsts) {
+ // It is difficult to tell if the load/store instruction is executed on every
+ // iteration inside an irregular loop.
+ if (!L->isLoopSimplifyForm() || !L->isInnermost())
+ return;
+
+ // FIXME: To make the case more typical, we only analyze loops that have one
+ // exiting block and the block must be the latch. It is easier to capture
+ // loops with memory access that will be executed in every iteration.
+ const BasicBlock *LoopLatch = L->getLoopLatch();
+ assert(LoopLatch && "normal form loop doesn't have a latch");
+ if (L->getExitingBlock() != LoopLatch)
+ return;
+
+ // We will not continue if sanitizer is enabled.
+ const Function *F = LoopLatch->getParent();
+ if (F->hasFnAttribute(Attribute::SanitizeAddress) ||
+ F->hasFnAttribute(Attribute::SanitizeThread) ||
+ F->hasFnAttribute(Attribute::SanitizeMemory) ||
+ F->hasFnAttribute(Attribute::SanitizeHWAddress) ||
+ F->hasFnAttribute(Attribute::SanitizeMemTag))
+ return;
+
+ for (auto *BB : L->getBlocks()) {
+ // We need to make sure that max execution time of MemAccessBB in loop
+ // represents latch max excution time. The BB below should be skipped:
+ // Entry
+ // │
+ // ┌─────▼─────┐
+ // │Loop Header◄─────┐
+ // └──┬──────┬─┘ │
+ // │ │ │
+ // ┌────────▼──┐ ┌─▼─────┐ │
+ // │MemAccessBB│ │OtherBB│ │
+ // └────────┬──┘ └─┬─────┘ │
+ // │ │ │
+ // ┌─▼──────▼─┐ │
+ // │Loop Latch├─────┘
+ // └────┬─────┘
+ // ▼
+ // Exit
+ if (!DT.dominates(BB, LoopLatch))
+ continue;
+
+ for (Instruction &I : *BB) {
+ if (isa<LoadInst>(&I) || isa<StoreInst>(&I))
+ MemInsts.push_back(&I);
+ }
+ }
+}
+
+/// Return a SCEV representing the memory size of pointer \p V .
+static const SCEV *getCertainSizeOfMem(const SCEV *V, Type *RTy,
+ const DataLayout &DL,
+ const TargetLibraryInfo &TLI,
+ ScalarEvolution *SE) {
+ const SCEVUnknown *PtrBase = dyn_cast<SCEVUnknown>(V);
+ if (!PtrBase)
+ return nullptr;
+ Value *Ptr = PtrBase->getValue();
+ uint64_t Size = 0;
+ if (!llvm::getObjectSize(Ptr, Size, DL, &TLI))
+ return nullptr;
+ return SE->getConstant(RTy, Size);
+}
+
+/// Check if we can make sure that the indices of a GEP instruction will not
+/// wrap.
+static bool checkIndexWrap(Value *Ptr, ScalarEvolution *SE) {
+ auto *PtrGEP = dyn_cast<GetElementPtrInst>(Ptr);
+ if (!PtrGEP)
+ return false;
+ for (Value *Index : PtrGEP->indices()) {
+ Value *V = Index;
+ if (isa<ZExtInst>(V) || isa<SExtInst>(V))
+ V = cast<Instruction>(Index)->getOperand(0);
+ auto *SCEV = SE->getSCEV(V);
+ if (isa<SCEVCouldNotCompute>(SCEV))
+ return false;
+ auto *AryExpr = dyn_cast<SCEVNAryExpr>(SE->getSCEV(V));
+ if (!AryExpr)
+ continue;
+ if (!AryExpr->hasNoSelfWrap())
+ return false;
+ }
+ return true;
+}
+
+const SCEV *
+ScalarEvolution::getConstantMaxTripCountFromMemAccess(const Loop *L) {
+ SmallVector<Instruction *, 4> MemInsts;
+ collectExecLoadStoreInsideLoop(L, DT, MemInsts);
+
+ SmallVector<const SCEV *> InferCountColl;
+ const DataLayout &DL = getDataLayout();
+
+ for (Instruction *I : MemInsts) {
+ Value *Ptr = getLoadStorePointerOperand(I);
+ assert(Ptr && "empty pointer operand");
+ if (!checkIndexWrap(Ptr, this))
+ continue;
+ auto *AddRec = dyn_cast<SCEVAddRecExpr>(getSCEV(Ptr));
+ if (!AddRec || !AddRec->isAffine())
----------------
nikic wrote:
The example you shared cannot wrap due to presence of `add nuw`. If `%iv` wrapped, a poison value would reach the latch condition, which would result in undefined behavior. As such, we can assume that `%iv` does not wrap, and as such `%len` cannot be larger than 256. Am I missing something here?
https://github.com/llvm/llvm-project/pull/70361
More information about the llvm-commits
mailing list