[llvm] [LoopVectorize] Add support for reverse loops in isDereferenceableAndAlignedInLoop (PR #96752)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 01:07:20 PDT 2024


================
@@ -318,11 +321,24 @@ bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
       // TODO: generalize if a case found which warrants
       if (Offset->getAPInt().urem(Alignment.value()) != 0)
         return false;
+      if (StepIsNegative) {
+        // In the last iteration of the loop the address we access we will be
+        // lower than the first by (TC - 1) * Step. So we need to make sure
+        // that there is enough room in Offset to accomodate this.
+        APInt SubOffset = (TC - 1) * AbsStep;
+        if (Offset->getAPInt().ult(SubOffset))
+          return false;
+        // We can safely use the new base because the decrementing pointer is
+        // always guaranteed to be >= new base. The total access size needs to
+        // take into account the start offset and the loaded element size.
+        AccessSize = Offset->getAPInt() + EltSize;
----------------
david-arm wrote:

I tried doing that with an i8, but the offset return by SCEV is always 64-bit. So in practice I'd need a test with an alloca just less than UINT64_MAX bytes in size and an offset just less than UINT64_MAX, in order to exercise the overflow case. I wasn't sure if this was fragile, i.e. are allocas of such a large size even legal given that I don't think GEPs even support an offset greater than SINT64_MAX (since offsets are signed)? Having said that, I'm open to ideas about how to write a sensible test for this!

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


More information about the llvm-commits mailing list