8
votes

I am trying to use pointer analysis with LLVM and I find an implementation of the Andersen's pointer analysis. However, I am new to LLVM and I run into some problem.

For example, for this trivial C program (test.c)

int a;
int main()
{  
    int *p, *q;
    a = 20;
    p = &a;
    q = p;
   return 0;
}

The compiled LLVM IR is

  5 @a = common global i32 0, align 4
  6 
  7 ; Function Attrs: nounwind uwtable
  8 define i32 @main() #0 {
  9 entry:
 10   %retval = alloca i32, align 4
 11   %p = alloca i32*, align 8
 12   %q = alloca i32*, align 8
 13   store i32 0, i32* %retval
 14   store i32 20, i32* @a, align 4
 15   store i32* @a, i32** %p, align 8
 16   %0 = load i32** %p, align 8
 17   store i32* %0, i32** %q, align 8
 18   ret i32 0
 19 }

I run the alias analysis on the program as

$ opt -load ../Release+Asserts/lib/libAnders.so -anders-aa -aa-eval -print-all-alias-modref-info -disable-output < test.bc

the result is

Function: main: 5 pointers, 0 call sites
  NoAlias:  i32* %retval, i32** %p
  NoAlias:  i32* %retval, i32** %q
  NoAlias:  i32** %p, i32** %q
  NoAlias:  i32* %retval, i32* @a
  NoAlias:  i32* @a, i32** %p
  NoAlias:  i32* @a, i32** %q
  NoAlias:  i32* %0, i32* %retval
  NoAlias:  i32* %0, i32** %p
  NoAlias:  i32* %0, i32** %q
  MustAlias:    i32* %0, i32* @a

which confuses me because (p, q) and {a} are obvious alias. I run the -basicaa and get the similar result.

So I am wondering how the LLVM deal with pointer variables on the IR level? Does the result provide enough information that other analysis or optimization find it useful? And how other passes can use these information? Thanks.

1
Just a stab in the dark... Did you use any optimization levels when compiling test.c? I think the compilers need at least -O1 to ensure some analysis takes place. At -O0, no optimizations are needed and no analysis takes place. - jww
If I turn on -O1, in this case p and q will be eliminated. I am aware that LLVM analysis is not on source level, however, I am wondering if I can get results close to source level. - platinor

1 Answers

6
votes

If you read in LLVM documentation, it says the result is No Alias when

Another is when the two pointers are only ever used for reading memory.

So I think it might be because you are not using pointers to write to a memory. You should try modifying your code so that pointer writes to a memory.