[PATCH] D45510: [RFC][BasicAA] Return MayAlias for the pointer plus variable offset to structure object member

Shiva Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 10 20:16:10 PDT 2018


shiva0217 created this revision.

In the following case, pointer addoff calculate by "&x.q + off" may have a chance alias to x.p structure member.

struct X {

  int *p;
  int *q;

};
foo(int i, int j, int k, int off)
{

  struct X x;
  int **t, *addoff;
  x.p = &i;
  x.q = &j;
  t = &x.q;
  t += off;
  addoff = *t;
  return *addoff;

}


Repository:
  rL LLVM

https://reviews.llvm.org/D45510

Files:
  lib/Analysis/BasicAliasAnalysis.cpp
  test/Analysis/BasicAA/negoffset.ll


Index: test/Analysis/BasicAA/negoffset.ll
===================================================================
--- test/Analysis/BasicAA/negoffset.ll
+++ test/Analysis/BasicAA/negoffset.ll
@@ -61,8 +61,8 @@
 ; CHECK-DAG:  MayAlias:     i32* %a2.0, i32* %r2.1i
 ; CHECK-DAG:  NoAlias:      i32* %a1, i32* %r2.0
 ; CHECK-DAG:  NoAlias:      i32* %a1, i32* %r2.1
-; CHECK-DAG:  NoAlias:      i32* %a1, i32* %r2.i
-; CHECK-DAG:  NoAlias:      i32* %a1, i32* %r2.1i
+; CHECK-DAG:  MayAlias:     i32* %a1, i32* %r2.i
+; CHECK-DAG:  MayAlias:     i32* %a1, i32* %r2.1i
 %complex = type { i32, i32, [4 x i32] }
 define void @complex1(i32 %i) {
   %alloca = alloca %complex
@@ -79,9 +79,9 @@
 }
 
 ; CHECK-LABEL: Function: complex2:
-; CHECK-DAG: NoAlias: i32* %alloca, i32* %p120
-; CHECK-DAG: NoAlias: i32* %alloca, i32* %pi20
-; CHECK-DAG: NoAlias: i32* %alloca, i32* %pij1
+; CHECK-DAG: NoAlias:  i32* %alloca, i32* %p120
+; CHECK-DAG: MayAlias: i32* %alloca, i32* %pi20
+; CHECK-DAG: MayAlias: i32* %alloca, i32* %pij1
 ; CHECK-DAG: MayAlias: i32* %a3, i32* %pij1
 %inner = type { i32, i32 }
 %outer = type { i32, i32, [10 x %inner] }
@@ -96,3 +96,24 @@
   ret void
 }
 
+; CHECK-LABEL: Function: pointer_offset:
+; CHECK-DAG: MayAlias: i32** %add.ptr, i32** %p1
+; CHECK-DAG: MayAlias: i32** %add.ptr, i32** %q2
+%struct.X = type { i32*, i32* }
+define i32 @pointer_offset(i32 signext %i, i32 signext %j, i32 zeroext %off) {
+entry:
+  %i.addr = alloca i32
+  %j.addr = alloca i32
+  %x = alloca %struct.X
+  store i32 %i, i32* %i.addr
+  store i32 %j, i32* %j.addr
+  %0 = bitcast %struct.X* %x to i8*
+  %p1 = getelementptr inbounds %struct.X, %struct.X* %x, i32 0, i32 0
+  store i32* %i.addr, i32** %p1
+  %q2 = getelementptr inbounds %struct.X, %struct.X* %x, i32 0, i32 1
+  store i32* %j.addr, i32** %q2
+  %add.ptr = getelementptr inbounds i32*, i32** %q2, i32 %off
+  %1 = load i32*, i32** %add.ptr
+  %2 = load i32, i32* %1
+  ret i32 %2
+}
Index: lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- lib/Analysis/BasicAliasAnalysis.cpp
+++ lib/Analysis/BasicAliasAnalysis.cpp
@@ -1160,6 +1160,10 @@
   int64_t GEPBaseOffset = DecompGEP.StructOffset;
   if (DecompGEP.VarIndices.empty())
     GEPBaseOffset += DecompGEP.OtherOffset;
+  // We can't get GEP offset to identify pointer alias if the GEP has variable
+  // indeces.
+  else
+    return false;
 
   return (GEPBaseOffset >= ObjectBaseOffset + (int64_t)ObjectAccessSize);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45510.141945.patch
Type: text/x-patch
Size: 2496 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180411/918c4543/attachment.bin>


More information about the llvm-commits mailing list