[llvm] [BasicAA] Handle disjoint or as add in DecomposeGEP. (PR #78209)

David Green via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 15 12:42:29 PST 2024


https://github.com/davemgreen created https://github.com/llvm/llvm-project/pull/78209

This removes the MaskedValueIsZero check in decomposing geps in BasicAA, using the isDisjoint flags instead. This relies on the disjoint flags being present. The other alternative is to keep the old MaskedValueIsZero check too.

>From af70bb44e2aec68eae7a8e48f30a2ea703cc8e53 Mon Sep 17 00:00:00 2001
From: David Green <david.green at arm.com>
Date: Mon, 15 Jan 2024 20:41:39 +0000
Subject: [PATCH] [BasicAA] Handle disjoint or as add in DecomposeGEP.

This removes the MaskedValueIsZero check in decomposing geps in BasicAA, using
the isDisjoint flags instead. This relies on the disjoint flags being present.
The other alternative is to keep the old MaskedValueIsZero check too.
---
 llvm/lib/Analysis/BasicAliasAnalysis.cpp |  6 ++----
 llvm/test/Analysis/BasicAA/gep-alias.ll  | 17 ++++++++++++++++-
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index a4a0846df7af15..ab1ab106659e7e 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -393,10 +393,8 @@ static LinearExpression GetLinearExpression(
         // further.
         return Val;
       case Instruction::Or:
-        // X|C == X+C if all the bits in C are unset in X.  Otherwise we can't
-        // analyze it.
-        if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(),
-                               SimplifyQuery(DL, DT, AC, BOp)))
+        // X|C == X+C if it is disjoint.  Otherwise we can't analyze it.
+        if (!cast<PossiblyDisjointInst>(BOp)->isDisjoint())
           return Val;
 
         [[fallthrough]];
diff --git a/llvm/test/Analysis/BasicAA/gep-alias.ll b/llvm/test/Analysis/BasicAA/gep-alias.ll
index 30298625426641..132cc496b8f7d9 100644
--- a/llvm/test/Analysis/BasicAA/gep-alias.ll
+++ b/llvm/test/Analysis/BasicAA/gep-alias.ll
@@ -114,7 +114,7 @@ define i32 @test5_as1_same_size(ptr addrspace(1) %p, i16 %i) {
 define i32 @test6(ptr %p, i64 %i1) {
   %i = shl i64 %i1, 2
   %pi = getelementptr i32, ptr %p, i64 %i
-  %i.next = or i64 %i, 1
+  %i.next = or disjoint i64 %i, 1
   %pi.next = getelementptr i32, ptr %p, i64 %i.next
   %x = load i32, ptr %pi
   store i32 42, ptr %pi.next
@@ -125,6 +125,21 @@ define i32 @test6(ptr %p, i64 %i1) {
 ; CHECK: ret i32 0
 }
 
+; P[i] != p[(i*4)|2048] with disjoint or
+define i32 @test6_higheror(ptr %p, i64 %i1) {
+  %i = shl nuw nsw i64 %i1, 2
+  %pi = getelementptr i32, ptr %p, i64 %i
+  %i.next = or disjoint i64 %i, 2048
+  %pi.next = getelementptr i32, ptr %p, i64 %i.next
+  %x = load i32, ptr %pi
+  store i32 42, ptr %pi.next
+  %y = load i32, ptr %pi
+  %z = sub i32 %x, %y
+  ret i32 %z
+; CHECK-LABEL: @test6_higheror(
+; CHECK: ret i32 0
+}
+
 ; P[1] != P[i*4]
 define i32 @test7(ptr %p, i64 %i) {
   %pi = getelementptr i32, ptr %p, i64 1



More information about the llvm-commits mailing list