[llvm] 7cf7378 - [BasicAA] Don't treat non-inbounds GEP as nsw

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 29 13:30:52 PDT 2021


Author: Nikita Popov
Date: 2021-10-29T22:30:44+02:00
New Revision: 7cf7378a9d5566158b74613964b528761415c421

URL: https://github.com/llvm/llvm-project/commit/7cf7378a9d5566158b74613964b528761415c421
DIFF: https://github.com/llvm/llvm-project/commit/7cf7378a9d5566158b74613964b528761415c421.diff

LOG: [BasicAA] Don't treat non-inbounds GEP as nsw

The scale multiplication is only guaranteed to be nsw if the GEP
is inbounds (or the multiplication is trivial). Previously we were
only considering explicit muls in GEP indices.

Added: 
    

Modified: 
    llvm/lib/Analysis/BasicAliasAnalysis.cpp
    llvm/test/Analysis/BasicAA/struct-geps.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index ea02110f2547..43f55e843e6f 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -657,6 +657,7 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
 
       // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
       // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
+      LE.IsNSW &= GEPOp->isInBounds() || Scale.isOne();
       Decomposed.Offset += LE.Offset.sextOrTrunc(MaxPointerSize) * Scale;
       Scale *= LE.Scale.sextOrTrunc(MaxPointerSize);
 

diff  --git a/llvm/test/Analysis/BasicAA/struct-geps.ll b/llvm/test/Analysis/BasicAA/struct-geps.ll
index 56fdd2b129b7..58d0a7cfb531 100644
--- a/llvm/test/Analysis/BasicAA/struct-geps.ll
+++ b/llvm/test/Analysis/BasicAA/struct-geps.ll
@@ -36,9 +36,9 @@ define void @test_simple(%struct* %st, i64 %i, i64 %j, i64 %k) {
   ret void
 }
 
-; TODO: As the GEP is not inbounds, these pointers may alias due to overflow.
+; As the GEP is not inbounds, these pointers may alias due to overflow.
 ; CHECK-LABEL: test_not_inbounds
-; CHECK-DAG: NoAlias: i32* %x, i32* %y
+; CHECK: MayAlias: i32* %x, i32* %y
 define void @test_not_inbounds(%struct* %st, i64 %i, i64 %j, i64 %k) {
   %x = getelementptr %struct, %struct* %st, i64 %i, i32 0
   %y = getelementptr %struct, %struct* %st, i64 %j, i32 1


        


More information about the llvm-commits mailing list