[llvm] [BasicAA] Check for Overflow using vscale_range (PR #81144)
David Green via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 12 01:57:53 PST 2024
https://github.com/davemgreen updated https://github.com/llvm/llvm-project/pull/81144
>From 15191a704dc35c3c230298961b311a60586550d4 Mon Sep 17 00:00:00 2001
From: David Green <david.green at arm.com>
Date: Mon, 12 Feb 2024 09:57:42 +0000
Subject: [PATCH] [BasicAA] Check for Overflow using vscale_range
This extends #80818 when IsNSW is lost (possibly due to looking through
multiple GEPs), to check the vscale_range for an access that will not overflow
even with the maximum range.
---
llvm/lib/Analysis/BasicAliasAnalysis.cpp | 24 +++++++++++++++++-------
llvm/test/Analysis/BasicAA/vscale.ll | 6 +++---
2 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index 682b0a2ecacb32..2ea29177a5cf0f 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1173,7 +1173,7 @@ AliasResult BasicAAResult::aliasGEP(
// VScale Alias Analysis - Given one scalable offset between accesses and a
// scalable typesize, we can divide each side by vscale, treating both values
// as a constant. We prove that Offset/vscale >= TypeSize/vscale.
- if (DecompGEP1.VarIndices.size() == 1 && DecompGEP1.VarIndices[0].IsNSW &&
+ if (DecompGEP1.VarIndices.size() == 1 &&
DecompGEP1.VarIndices[0].Val.TruncBits == 0 &&
DecompGEP1.Offset.isZero() &&
PatternMatch::match(DecompGEP1.VarIndices[0].Val.V,
@@ -1183,12 +1183,22 @@ AliasResult BasicAAResult::aliasGEP(
ScalableVar.IsNegated ? -ScalableVar.Scale : ScalableVar.Scale;
LocationSize VLeftSize = Scale.isNegative() ? V1Size : V2Size;
- // Note that we do not check that the typesize is scalable, as vscale >= 1
- // so noalias still holds so long as the dependency distance is at least as
- // big as the typesize.
- if (VLeftSize.hasValue() &&
- Scale.abs().uge(VLeftSize.getValue().getKnownMinValue()))
- return AliasResult::NoAlias;
+ // Check if the offset is known to not overflow, if it does then attempt to
+ // prove it with the known values of vscale_range.
+ bool Overflows = !DecompGEP1.VarIndices[0].IsNSW;
+ if (Overflows) {
+ ConstantRange CR = getVScaleRange(&F, Scale.getBitWidth());
+ (void)CR.getSignedMax().smul_ov(Scale, Overflows);
+ }
+
+ if (!Overflows) {
+ // Note that we do not check that the typesize is scalable, as vscale >= 1
+ // so noalias still holds so long as the dependency distance is at least
+ // as big as the typesize.
+ if (VLeftSize.hasValue() &&
+ Scale.abs().uge(VLeftSize.getValue().getKnownMinValue()))
+ return AliasResult::NoAlias;
+ }
}
// Bail on analysing scalable LocationSize
diff --git a/llvm/test/Analysis/BasicAA/vscale.ll b/llvm/test/Analysis/BasicAA/vscale.ll
index 895ae1e0023321..18aad4ffe73f49 100644
--- a/llvm/test/Analysis/BasicAA/vscale.ll
+++ b/llvm/test/Analysis/BasicAA/vscale.ll
@@ -471,8 +471,8 @@ define void @vscale_negativescale(ptr %p) vscale_range(1,16) {
; CHECK-LABEL: onevscale
; CHECK-DAG: MustAlias: <vscale x 4 x i32>* %vp161, <vscale x 4 x i32>* %vp162
-; CHECK-DAG: MayAlias: <vscale x 4 x i32>* %vp161, <vscale x 4 x i32>* %vp161b
-; CHECK-DAG: MayAlias: <vscale x 4 x i32>* %vp161b, <vscale x 4 x i32>* %vp162
+; CHECK-DAG: NoAlias: <vscale x 4 x i32>* %vp161, <vscale x 4 x i32>* %vp161b
+; CHECK-DAG: NoAlias: <vscale x 4 x i32>* %vp161b, <vscale x 4 x i32>* %vp162
define void @onevscale(ptr %p) vscale_range(1,16) {
%v1 = call i64 @llvm.vscale.i64()
%vp1 = mul nsw i64 %v1, 16
@@ -489,7 +489,7 @@ define void @onevscale(ptr %p) vscale_range(1,16) {
; CHECK-LABEL: twovscales
; CHECK-DAG: MayAlias: <vscale x 4 x i32>* %vp161, <vscale x 4 x i32>* %vp162
-; CHECK-DAG: MayAlias: <vscale x 4 x i32>* %vp161, <vscale x 4 x i32>* %vp161b
+; CHECK-DAG: NoAlias: <vscale x 4 x i32>* %vp161, <vscale x 4 x i32>* %vp161b
; CHECK-DAG: MayAlias: <vscale x 4 x i32>* %vp161b, <vscale x 4 x i32>* %vp162
define void @twovscales(ptr %p) vscale_range(1,16) {
%v1 = call i64 @llvm.vscale.i64()
More information about the llvm-commits
mailing list