[llvm-dev] Question about a May-alias case

jingu@codeplay.com via llvm-dev llvm-dev at lists.llvm.org
Wed Jun 13 05:58:59 PDT 2018


Hello All,

I have a question about a May-alias case. Let's look at one simple C 
example.

char *buf[4];
char c;
void test(int idx) {
   char *a = buf[3 - idx];
   char *b = buf[idx];
   *a = *b;
   c++;
   *a = *b;
}

We can imagine the second "*a = *b" could be removed. Let's look at the 
IR snippet with -O3 for above example.

   1 define void @test(i32 %idx) {
   2 entry:
   3   %sub = sub nsw i32 3, %idx
   4   %idxprom = sext i32 %sub to i64
   5   %arrayidx = getelementptr inbounds [4 x i8*], [4 x i8*]* @buf, 
i64 0, i64 %idxprom
   6   %0 = load i8*, i8** %arrayidx, align 8
   7   %idxprom1 = sext i32 %idx to i64
   8   %arrayidx2 = getelementptr inbounds [4 x i8*], [4 x i8*]* @buf, 
i64 0, i64 %idxprom1
   9   %1 = load i8*, i8** %arrayidx2, align 8
  10   %2 = load i8, i8* %1, align 1
  11   store i8 %2, i8* %0, align 1
  12   %3 = load i8, i8* @c, align 1
  13   %inc = add nsw i8 %3, 1
  14   store i8 %inc, i8* @c, align 1
  15   %4 = load i8, i8* %1, align 1
  16   store i8 %4, i8* %0, align 1
  17   ret void
  18 }

You can see the '%2' and '%4' are same but the %4 is not removed because 
of May-alias. Let's look at the result of AliasSet.

Alias sets for function 'test':
Alias Set Tracker: 2 alias sets for 5 pointer values.
   AliasSet[0x4e72580, 5] may alias, Mod/Ref   Pointers: (i8** 
%arrayidx, 8), (i8** %arrayidx2, 8), (i8* %1, 1), (i8* %0, 1), (i8* @c, 1)
   AliasSet[0x4e76ef0, 1] must alias, Ref        forwarding to 0x4e72580

You can see May-alias with %1 and @c. It seems if pointer operand points 
double pointer or more level pointer, alias analysis can not go through 
it. There are some options or ways to avoid May-alias for above case? It 
is not easy to find them. If I missed something, please let me know.

Thanks,

JinGu Kang



More information about the llvm-dev mailing list