[llvm] [LAA] Only use inbounds/nusw in isNoWrap if the GEP is dereferenced. (PR #161445)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 1 14:43:04 PDT 2025


================
@@ -1021,9 +1024,22 @@ static bool isNoWrap(PredicatedScalarEvolution &PSE, const SCEVAddRecExpr *AR,
   // the distance between the previously accessed location and the wrapped
   // location will be larger than half the pointer index type space. In that
   // case, the GEP would be  poison and any memory access dependent on it would
-  // be immediate UB when executed.
+  // be immediate UB when executed. The reasoning can only be applied if the
+  // pointer is dereferenced at least at the last iteration. For now, check if
+  // it is dereferenced in every iteration.
   if (auto *GEP = dyn_cast_if_present<GetElementPtrInst>(Ptr);
-      GEP && GEP->hasNoUnsignedSignedWrap())
+      GEP && GEP->hasNoUnsignedSignedWrap() &&
+      (L->getHeader() == L->getLoopLatch() ||
+       (any_of(GEP->users(), [L, DT](User *U) {
+         if (!isa<LoadInst, StoreInst>(U))
+           return false;
+         BasicBlock *UserBB = cast<Instruction>(U)->getParent();
+         if (DT && !LoopAccessInfo::blockNeedsPredication(UserBB, L, DT))
+           return true;
+         return UserBB == L->getHeader() ||
+                (L->getExitingBlock() == L->getLoopLatch() &&
+                 UserBB == L->getLoopLatch());
+       }))))
----------------
fhahn wrote:

Thanks, I moved it into the if, together with the comment

https://github.com/llvm/llvm-project/pull/161445


More information about the llvm-commits mailing list