[llvm] [Analysis] Bail out for negative offsets in isDereferenceableAndAlignedInLoop (PR #99490)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 18 06:20:02 PDT 2024
https://github.com/david-arm created https://github.com/llvm/llvm-project/pull/99490
This patch is effectively NFC because it doesn't actually affect any existing tests. It's not possible to even write a test for it, because by accident negative offsets are caught by a subsequent unsigned add overflow check. However, I think it's better to bail out explicitly for negative offsets so that it's more consistent with the unsigned remainder and add calculations.
>From a4e97392a4bfce93d1814c1bf3b24d5b025fdf92 Mon Sep 17 00:00:00 2001
From: David Sherwood <david.sherwood at arm.com>
Date: Thu, 18 Jul 2024 13:17:32 +0000
Subject: [PATCH] [Analysis] Bail out for negative offsets in
isDereferenceableAndAlignedInLoop
This patch is effectively NFC because it doesn't actually affect
any existing tests. It's not possible to even write a test for it,
because by accident negative offsets are caught by a subsequent
unsigned add overflow check. However, I think it's better to bail
out explicitly for negative offsets so that it's more consistent
with the unsigned remainder and add calculations.
---
llvm/lib/Analysis/Loads.cpp | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/llvm/lib/Analysis/Loads.cpp b/llvm/lib/Analysis/Loads.cpp
index 1d54a66705a2a..c19a276464e1e 100644
--- a/llvm/lib/Analysis/Loads.cpp
+++ b/llvm/lib/Analysis/Loads.cpp
@@ -313,6 +313,13 @@ bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
const auto *Offset = dyn_cast<SCEVConstant>(StartS->getOperand(0));
const auto *NewBase = dyn_cast<SCEVUnknown>(StartS->getOperand(1));
if (StartS->getNumOperands() == 2 && Offset && NewBase) {
+ // The following code below assumes the offset is unsigned, but GEP
+ // offsets are treated as signed so we can end up with a signed value
+ // here too. For example, suppose the initial PHI value is (i8 255),
+ // the offset will be treated as (i8 -1) and sign-extended to (i64 -1).
+ if (Offset->getAPInt().isNegative())
+ return false;
+
// For the moment, restrict ourselves to the case where the offset is a
// multiple of the requested alignment and the base is aligned.
// TODO: generalize if a case found which warrants
More information about the llvm-commits
mailing list