[PATCH] D133844: [AA] Improve the BasicAA analysis capability base on GEP

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 14 01:58:10 PDT 2022


Allen created this revision.
Allen added a reviewer: nikic.
Herald added subscribers: jeroen.dobbelaere, hiraditya.
Herald added a project: All.
Allen requested review of this revision.
Herald added subscribers: llvm-commits, alextsao1999.
Herald added a project: LLVM.

Improved effectively for a constant offset GEP if the scalable index is same.

  getelementptr <vscale x 4 x i32>, ptr %p, i64 1, i64 %i
  getelementptr <vscale x 4 x i32>, ptr %p, i64 1, i64 %j


https://reviews.llvm.org/D133844

Files:
  llvm/lib/Analysis/BasicAliasAnalysis.cpp
  llvm/test/Analysis/BasicAA/vscale.ll
  llvm/test/Transforms/GVN/vscale.ll


Index: llvm/test/Transforms/GVN/vscale.ll
===================================================================
--- llvm/test/Transforms/GVN/vscale.ll
+++ llvm/test/Transforms/GVN/vscale.ll
@@ -288,8 +288,7 @@
 ; CHECK-NEXT:    store i32 1, i32* [[GEP2]], align 4
 ; CHECK-NEXT:    br i1 [[C:%.*]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
 ; CHECK:       if.then:
-; CHECK-NEXT:    [[T:%.*]] = load i32, i32* [[GEP1]], align 4
-; CHECK-NEXT:    store i32 [[T]], i32* [[Q:%.*]], align 4
+; CHECK-NEXT:    store i32 0, i32* [[Q:%.*]], align 4
 ; CHECK-NEXT:    ret void
 ; CHECK:       if.else:
 ; CHECK-NEXT:    ret void
Index: llvm/test/Analysis/BasicAA/vscale.ll
===================================================================
--- llvm/test/Analysis/BasicAA/vscale.ll
+++ llvm/test/Analysis/BasicAA/vscale.ll
@@ -76,8 +76,8 @@
 ; CHECK-LABEL: gep_same_base_const_offset
 ; CHECK-DAG:  MayAlias:     i32* %gep1, <vscale x 4 x i32>* %p
 ; CHECK-DAG:  MayAlias:     i32* %gep2, <vscale x 4 x i32>* %p
-; TODO: AliasResult for gep1,gep2 can be improved as NoAlias
-; CHECK-DAG:  MayAlias:     i32* %gep1, i32* %gep2
+;  AliasResult for gep1,gep2 can be improved as NoAlias
+; CHECK-DAG:  NoAlias:     i32* %gep1, i32* %gep2
 define void @gep_same_base_const_offset(<vscale x 4 x i32>* %p) {
   %gep1 = getelementptr <vscale x 4 x i32>, <vscale x 4 x i32>* %p, i64 1, i64 0
   %gep2 = getelementptr <vscale x 4 x i32>, <vscale x 4 x i32>* %p, i64 1, i64 1
Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1107,6 +1107,20 @@
                                              : AliasResult::MayAlias;
   }
 
+  // If it can be confirmed NoAlias, no further decomposition is required.
+  if (isa<GEPOperator>(V2) && UnderlyingV1 == UnderlyingV2 &&
+      V1Size.hasValue() && V2Size.hasValue()) {
+    auto *EltTy1 = GEP1->getResultElementType();
+    auto *EltTy2 = cast<GEPOperator>(V2)->getResultElementType();
+    unsigned MaxIndexSize = DL.getMaxIndexSizeInBits() - 1;
+    TypeSize AllocaTypeSize = DL.getTypeAllocSize(EltTy1);
+    Optional<int64_t> Offset = isPointerOffset(GEP1, V2, DL);
+    if ((EltTy1 == EltTy2) && Offset && !AllocaTypeSize.isScalable() &&
+        std::max(V1Size.getValue(), V2Size.getValue()) <= std::abs(*Offset) &&
+        (std::abs(*Offset) < 1ULL << MaxIndexSize))
+      return AliasResult::NoAlias;
+  }
+
   DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
   DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133844.460011.patch
Type: text/x-patch
Size: 2650 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220914/3077c240/attachment.bin>


More information about the llvm-commits mailing list