From 39860d63e4c15fe8676bb8e042b42f575e31b9fb Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 6 Aug 2024 14:37:50 +0200 Subject: [PATCH] [BasicAA] Check nusw instead of inbounds For the offset scaling, this is sufficient to guarantee nsw. The other checks for inbounds in this file do need proper inbounds. --- llvm/lib/Analysis/BasicAliasAnalysis.cpp | 2 +- llvm/test/Analysis/BasicAA/struct-geps.ll | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp index e474899fb548ec..33131d80b35fad 100644 --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -701,7 +701,7 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL, // Scale by the type size. unsigned TypeSize = AllocTypeSize.getFixedValue(); - LE = LE.mul(APInt(IndexSize, TypeSize), GEPOp->isInBounds()); + LE = LE.mul(APInt(IndexSize, TypeSize), GEPOp->hasNoUnsignedSignedWrap()); Decomposed.Offset += LE.Offset.sext(MaxIndexSize); APInt Scale = LE.Scale.sext(MaxIndexSize); diff --git a/llvm/test/Analysis/BasicAA/struct-geps.ll b/llvm/test/Analysis/BasicAA/struct-geps.ll index c7ca731697e49f..008d73ef44dcea 100644 --- a/llvm/test/Analysis/BasicAA/struct-geps.ll +++ b/llvm/test/Analysis/BasicAA/struct-geps.ll @@ -51,6 +51,17 @@ define void @test_not_inbounds(ptr %st, i64 %i, i64 %j, i64 %k) { ret void } +; It is sufficient to have nusw instead of inbounds. +; CHECK-LABEL: test_nusw +; CHECK: NoAlias: i32* %x, i32* %y +define void @test_nusw(ptr %st, i64 %i, i64 %j, i64 %k) { + %x = getelementptr nusw %struct, ptr %st, i64 %i, i32 0 + %y = getelementptr nusw %struct, ptr %st, i64 %j, i32 1 + load i32, ptr %x + load i32, ptr %y + ret void +} + ; CHECK-LABEL: test_in_array ; CHECK-DAG: MayAlias: [1 x %struct]* %st, i32* %x