[llvm] 96d2dc7 - [SCEVAA] Enhance SCEVAAResult::alias() to handle two pointers with different pointer bases (#91453)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 29 19:50:36 PDT 2024


Author: csstormq
Date: 2024-05-30T10:50:32+08:00
New Revision: 96d2dc7210db3ed3a4c9f6aa93c14d2ea90e67cc

URL: https://github.com/llvm/llvm-project/commit/96d2dc7210db3ed3a4c9f6aa93c14d2ea90e67cc
DIFF: https://github.com/llvm/llvm-project/commit/96d2dc7210db3ed3a4c9f6aa93c14d2ea90e67cc.diff

LOG: [SCEVAA] Enhance SCEVAAResult::alias() to handle two pointers with different pointer bases (#91453)

This patch enhances the SCEVAAResult::alias() interface to handle two
pointers with different pointer bases.

Before calling getMinusSCEV(), we firstly try to explicitly convert
these two pointers into ptrtoint expressions to do that.

Either both pointers are used with ptrtoint or neither, so we can't
end up with a ptr + int mix.

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp
    llvm/test/Analysis/ScalarEvolution/scev-aa.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp b/llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp
index af8232b03f1ed..7bcec7931b219 100644
--- a/llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp
@@ -61,6 +61,19 @@ AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
                                  ? static_cast<uint64_t>(LocB.Size.getValue())
                                  : MemoryLocation::UnknownSize);
 
+    // Firstly, try to convert the two pointers into ptrtoint expressions to
+    // handle two pointers with 
diff erent pointer bases.
+    // Either both pointers are used with ptrtoint or neither, so we can't end
+    // up with a ptr + int mix.
+    const SCEV *AInt =
+        SE.getPtrToIntExpr(AS, SE.getEffectiveSCEVType(AS->getType()));
+    const SCEV *BInt =
+        SE.getPtrToIntExpr(BS, SE.getEffectiveSCEVType(BS->getType()));
+    if (!isa<SCEVCouldNotCompute>(AInt) && !isa<SCEVCouldNotCompute>(BInt)) {
+      AS = AInt;
+      BS = BInt;
+    }
+
     // Compute the 
diff erence between the two pointers.
     const SCEV *BA = SE.getMinusSCEV(BS, AS);
 

diff  --git a/llvm/test/Analysis/ScalarEvolution/scev-aa.ll b/llvm/test/Analysis/ScalarEvolution/scev-aa.ll
index a81baa73a93bd..5610833e9c474 100644
--- a/llvm/test/Analysis/ScalarEvolution/scev-aa.ll
+++ b/llvm/test/Analysis/ScalarEvolution/scev-aa.ll
@@ -340,3 +340,29 @@ for.latch:
 for.end:
   ret void
 }
+
+; CHECK-LABEL: Function: test_
diff erent_pointer_bases_of_inttoptr: 2 pointers, 0 call sites
+; CHECK:   NoAlias:	<16 x i8>* %tmp5, <16 x i8>* %tmp7
+
+define void @test_
diff erent_pointer_bases_of_inttoptr() {
+entry:
+  br label %for.body
+
+for.body:
+  %tmp = phi i32 [ %next, %for.body ], [ 1, %entry ]
+  %tmp1 = shl nsw i32 %tmp, 1
+  %tmp2 = add nuw nsw i32 %tmp1, %tmp1
+  %tmp3 = mul nsw i32 %tmp2, 1408
+  %tmp4 = add nsw i32 %tmp3, 1408
+  %tmp5 = getelementptr inbounds i8, ptr inttoptr (i32 1024 to ptr), i32 %tmp1
+  %tmp6 = load <16 x i8>, ptr %tmp5, align 1
+  %tmp7 = getelementptr inbounds i8, ptr inttoptr (i32 4096 to ptr), i32 %tmp4
+  store <16 x i8> %tmp6, ptr %tmp7, align 1
+
+  %next = add i32 %tmp, 2
+  %exitcond = icmp slt i32 %next, 10
+  br i1 %exitcond, label %for.body, label %for.end
+
+for.end:
+  ret void
+}


        


More information about the llvm-commits mailing list