[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
       
    Sun Oct 26 19:57:51 PDT 2025
    
    
  
================
@@ -1023,8 +1027,22 @@ static bool isNoWrap(PredicatedScalarEvolution &PSE, const SCEVAddRecExpr *AR,
   // case, the GEP would be  poison and any memory access dependent on it would
   // be immediate UB when executed.
   if (auto *GEP = dyn_cast_if_present<GetElementPtrInst>(Ptr);
-      GEP && GEP->hasNoUnsignedSignedWrap())
-    return true;
+      GEP && GEP->hasNoUnsignedSignedWrap()) {
+    // For the above reasoning to apply, the pointer must be dereferenced in
+    // every iteration.
+    if (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))
----------------
fhahn wrote:
I think that's fine for the current uses, as we have the extra checks when expanding SCEV expressions for early-exit loops to make sure the expressions do not wrap.
https://github.com/llvm/llvm-project/pull/161445
    
    
More information about the llvm-commits
mailing list