[llvm] [SCEV] Do not consider base pointer defining scope for offset flags in `getGEPExpr` (PR #222713)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 17 12:24:20 PDT 2026
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/222713
>From 721b00cd64933f09c04bbb54c04da0cdcf17b7dc Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Thu, 10 Sep 2026 18:06:44 +0200
Subject: [PATCH 1/2] [SCEV] Precommit test (NFC)
---
.../gep-nowrap-flags-offset-scope.ll | 96 +++++++++++++++++++
1 file changed, 96 insertions(+)
create mode 100644 llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll
diff --git a/llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll b/llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll
new file mode 100644
index 0000000000000..3e21d900319e6
--- /dev/null
+++ b/llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll
@@ -0,0 +1,96 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
+; RUN: opt -disable-output "-passes=print<scalar-evolution>" -S < %s 2>&1 | FileCheck %s
+
+; Ensure GEP offset no-wrap flags are not inferred from the base pointer defining scope.
+; In the example below, the flags might not hold for the offset expression in general;
+; and, as SCEV expressions are interned, they would incorrectly apply to the addition in
+; the exit block, which may overflow.
+
+ at g = external global ptr
+ at h = external global ptr
+
+define void @gep_offset_different_scope(i1 %c) {
+; CHECK-LABEL: 'gep_offset_different_scope'
+; CHECK-NEXT: Classifying expressions for: @gep_offset_different_scope
+; CHECK-NEXT: %i = call i64 @opaque()
+; CHECK-NEXT: --> %i U: full-set S: full-set
+; CHECK-NEXT: %base = load ptr, ptr @g, align 8
+; CHECK-NEXT: --> %base U: full-set S: full-set
+; CHECK-NEXT: %gep = getelementptr inbounds nuw i16, ptr %base, i64 %i
+; CHECK-NEXT: --> ((2 * %i)<nuw><nsw> + %base)<nuw> U: full-set S: full-set
+; CHECK-NEXT: %val = load i16, ptr %gep, align 2
+; CHECK-NEXT: --> %val U: full-set S: full-set
+; CHECK-NEXT: %res = add i64 %i, %i
+; CHECK-NEXT: --> (2 * %i)<nuw><nsw> U: [0,-1) S: [-9223372036854775808,9223372036854775807)
+;
+entry:
+ %i = call i64 @opaque()
+ br i1 %c, label %if, label %exit
+
+if:
+ %base = load ptr, ptr @g, align 8
+ %gep = getelementptr inbounds nuw i16, ptr %base, i64 %i
+ %val = load i16, ptr %gep, align 2
+ br label %exit
+
+exit:
+ %res = add i64 %i, %i
+ ret void
+}
+
+define void @multi_index_gep_mixed_scope(ptr %p, i1 %c) {
+; CHECK-LABEL: 'multi_index_gep_mixed_scope'
+; CHECK-NEXT: Classifying expressions for: @multi_index_gep_mixed_scope
+; CHECK-NEXT: %i = call i64 @opaque()
+; CHECK-NEXT: --> %i U: full-set S: full-set
+; CHECK-NEXT: %j = load i64, ptr @g, align 8
+; CHECK-NEXT: --> %j U: full-set S: full-set
+; CHECK-NEXT: %gep = getelementptr inbounds nuw [4 x i16], ptr %p, i64 %i, i64 %j
+; CHECK-NEXT: --> ((2 * %j)<nuw><nsw> + (8 * %i)<nuw><nsw> + %p)<nuw> U: full-set S: full-set
+; CHECK-NEXT: %val = load i16, ptr %gep, align 2
+; CHECK-NEXT: --> %val U: full-set S: full-set
+; CHECK-NEXT: %res = mul i64 %i, 8
+; CHECK-NEXT: --> (8 * %i)<nuw><nsw> U: [0,-7) S: [-9223372036854775808,9223372036854775801)
+;
+entry:
+ %i = call i64 @opaque()
+ br i1 %c, label %if, label %exit
+
+if:
+ %j = load i64, ptr @g, align 8
+ %gep = getelementptr inbounds nuw [4 x i16], ptr %p, i64 %i, i64 %j
+ %val = load i16, ptr %gep, align 2
+ br label %exit
+
+exit:
+ %res = mul i64 %i, 8
+ ret void
+}
+
+define void @multi_indices_gep_same_scope(ptr %p, i1 %c) {
+; CHECK-LABEL: 'multi_indices_gep_same_scope'
+; CHECK-NEXT: Classifying expressions for: @multi_indices_gep_same_scope
+; CHECK-NEXT: %i = load i64, ptr @g, align 8
+; CHECK-NEXT: --> %i U: full-set S: full-set
+; CHECK-NEXT: %j = load i64, ptr @h, align 8
+; CHECK-NEXT: --> %j U: full-set S: full-set
+; CHECK-NEXT: %gep = getelementptr inbounds nuw [4 x i16], ptr %p, i64 %i, i64 %j
+; CHECK-NEXT: --> ((2 * %j)<nuw><nsw> + (8 * %i)<nuw><nsw> + %p)<nuw> U: full-set S: full-set
+; CHECK-NEXT: %val = load i16, ptr %gep, align 2
+; CHECK-NEXT: --> %val U: full-set S: full-set
+;
+entry:
+ br i1 %c, label %if, label %exit
+
+if:
+ %i = load i64, ptr @g, align 8
+ %j = load i64, ptr @h, align 8
+ %gep = getelementptr inbounds nuw [4 x i16], ptr %p, i64 %i, i64 %j
+ %val = load i16, ptr %gep, align 2
+ br label %exit
+
+exit:
+ ret void
+}
+
+declare i64 @opaque()
>From 1d88740bccb35f588b9ab07646230188495f21ea Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Thu, 17 Sep 2026 20:13:01 +0200
Subject: [PATCH 2/2] [SCEV] Do not consider base pointer defining scope for
offset flags in `getGEPExpr`
The defining scope of the base pointer of a GEP was previously being
used to infer no-wrap flags on the SCEV sub-expressions for the offset,
which however only depend on the index operands. Ensure the scope bound
for the offset flags is therefore derived from those alone.
Fixes: https://github.com/llvm/llvm-project/issues/221524.
---
llvm/include/llvm/Analysis/ScalarEvolution.h | 8 ++---
llvm/lib/Analysis/ScalarEvolution.cpp | 36 +++++++++++++------
.../gep-nowrap-flags-offset-scope.ll | 8 ++---
3 files changed, 34 insertions(+), 18 deletions(-)
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 7fddd4ca4119f..fc4e194fa7598 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -816,10 +816,10 @@ class ScalarEvolution {
/// \p IndexExprs The expressions for the indices.
LLVM_ABI const SCEV *getGEPExpr(GEPOperator *GEP,
ArrayRef<SCEVUse> IndexExprs);
- LLVM_ABI const SCEV *getGEPExpr(SCEVUse BaseExpr,
- ArrayRef<SCEVUse> IndexExprs,
- Type *SrcElementTy,
- GEPNoWrapFlags NW = GEPNoWrapFlags::none());
+ LLVM_ABI const SCEV *
+ getGEPExpr(SCEVUse BaseExpr, ArrayRef<SCEVUse> IndexExprs, Type *SrcElementTy,
+ GEPNoWrapFlags BaseNW = GEPNoWrapFlags::none(),
+ GEPNoWrapFlags OffsetNW = GEPNoWrapFlags::none());
LLVM_ABI const SCEV *getAbsExpr(const SCEV *Op, bool IsNSW);
LLVM_ABI const SCEV *getMinMaxExpr(SCEVTypes Kind,
SmallVectorImpl<SCEVUse> &Operands);
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 39cd6ff1ec473..18bcbcccdd555 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -3790,28 +3790,44 @@ const SCEV *ScalarEvolution::getGEPExpr(GEPOperator *GEP,
const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
// getSCEV(Base)->getType() has the same address space as Base->getType()
// because SCEV::getType() preserves the address space.
- GEPNoWrapFlags NW = GEP->getNoWrapFlags();
- if (NW != GEPNoWrapFlags::none()) {
+ GEPNoWrapFlags BaseNW = GEP->getNoWrapFlags();
+ GEPNoWrapFlags OffsetNW = GEP->getNoWrapFlags();
+ if (BaseNW != GEPNoWrapFlags::none()) {
// We'd like to propagate flags from the IR to the corresponding SCEV nodes,
// but to do that, we have to ensure that said flag is valid in the entire
// defined scope of the SCEV.
// TODO: non-instructions have global scope. We might be able to prove
// some global scope cases
auto *GEPI = dyn_cast<Instruction>(GEP);
- if (!GEPI || !isSCEVExprNeverPoison(GEPI))
- NW = GEPNoWrapFlags::none();
+ if (!GEPI || !isSCEVExprNeverPoison(GEPI)) {
+ BaseNW = OffsetNW = GEPNoWrapFlags::none();
+ } else if (any_of(IndexExprs, [&](const SCEVUse &IndexExpr) {
+ const SCEV *Idx = IndexExpr;
+ return !isa<SCEVConstant>(Idx) &&
+ !isGuaranteedToTransferExecutionTo(
+ getDefiningScopeBound({Idx}), GEPI);
+ })) {
+ // The offset flags should not take into account the base pointer
+ // defining scope, as the offset only depends on the indices. Thus,
+ // derive the scope bound for the offset flags from each index operand
+ // individually.
+ OffsetNW = GEPNoWrapFlags::none();
+ }
}
- return getGEPExpr(BaseExpr, IndexExprs, GEP->getSourceElementType(), NW);
+ return getGEPExpr(BaseExpr, IndexExprs, GEP->getSourceElementType(), BaseNW,
+ OffsetNW);
}
const SCEV *ScalarEvolution::getGEPExpr(SCEVUse BaseExpr,
ArrayRef<SCEVUse> IndexExprs,
- Type *SrcElementTy, GEPNoWrapFlags NW) {
+ Type *SrcElementTy,
+ GEPNoWrapFlags BaseNW,
+ GEPNoWrapFlags OffsetNW) {
SCEV::NoWrapFlags OffsetWrap = SCEV::FlagAnyWrap;
- if (NW.hasNoUnsignedSignedWrap())
+ if (OffsetNW.hasNoUnsignedSignedWrap())
OffsetWrap = setFlags(OffsetWrap, SCEV::FlagNSW);
- if (NW.hasNoUnsignedWrap())
+ if (OffsetNW.hasNoUnsignedWrap())
OffsetWrap = setFlags(OffsetWrap, SCEV::FlagNUW);
Type *CurTy = BaseExpr->getType();
@@ -3859,8 +3875,8 @@ const SCEV *ScalarEvolution::getGEPExpr(SCEVUse BaseExpr,
// Add the base address and the offset. We cannot use the nsw flag, as the
// base address is unsigned. However, if we know that the offset is
// non-negative, we can use nuw.
- bool NUW = NW.hasNoUnsignedWrap() ||
- (NW.hasNoUnsignedSignedWrap() && isKnownNonNegative(Offset));
+ bool NUW = BaseNW.hasNoUnsignedWrap() ||
+ (BaseNW.hasNoUnsignedSignedWrap() && isKnownNonNegative(Offset));
SCEV::NoWrapFlags BaseWrap = NUW ? SCEV::FlagNUW : SCEV::FlagAnyWrap;
const SCEV *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
assert(BaseExpr->getType() == GEPExpr->getType() &&
diff --git a/llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll b/llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll
index 3e21d900319e6..1f014261f6e4a 100644
--- a/llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll
+++ b/llvm/test/Analysis/ScalarEvolution/gep-nowrap-flags-offset-scope.ll
@@ -17,11 +17,11 @@ define void @gep_offset_different_scope(i1 %c) {
; CHECK-NEXT: %base = load ptr, ptr @g, align 8
; CHECK-NEXT: --> %base U: full-set S: full-set
; CHECK-NEXT: %gep = getelementptr inbounds nuw i16, ptr %base, i64 %i
-; CHECK-NEXT: --> ((2 * %i)<nuw><nsw> + %base)<nuw> U: full-set S: full-set
+; CHECK-NEXT: --> ((2 * %i) + %base)<nuw> U: full-set S: full-set
; CHECK-NEXT: %val = load i16, ptr %gep, align 2
; CHECK-NEXT: --> %val U: full-set S: full-set
; CHECK-NEXT: %res = add i64 %i, %i
-; CHECK-NEXT: --> (2 * %i)<nuw><nsw> U: [0,-1) S: [-9223372036854775808,9223372036854775807)
+; CHECK-NEXT: --> (2 * %i) U: [0,-1) S: [-9223372036854775808,9223372036854775807)
;
entry:
%i = call i64 @opaque()
@@ -46,11 +46,11 @@ define void @multi_index_gep_mixed_scope(ptr %p, i1 %c) {
; CHECK-NEXT: %j = load i64, ptr @g, align 8
; CHECK-NEXT: --> %j U: full-set S: full-set
; CHECK-NEXT: %gep = getelementptr inbounds nuw [4 x i16], ptr %p, i64 %i, i64 %j
-; CHECK-NEXT: --> ((2 * %j)<nuw><nsw> + (8 * %i)<nuw><nsw> + %p)<nuw> U: full-set S: full-set
+; CHECK-NEXT: --> ((2 * %j) + (8 * %i) + %p) U: full-set S: full-set
; CHECK-NEXT: %val = load i16, ptr %gep, align 2
; CHECK-NEXT: --> %val U: full-set S: full-set
; CHECK-NEXT: %res = mul i64 %i, 8
-; CHECK-NEXT: --> (8 * %i)<nuw><nsw> U: [0,-7) S: [-9223372036854775808,9223372036854775801)
+; CHECK-NEXT: --> (8 * %i) U: [0,-7) S: [-9223372036854775808,9223372036854775801)
;
entry:
%i = call i64 @opaque()
More information about the llvm-commits
mailing list