Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer analysis in LLVM

Tags:

c

pointers

llvm

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.

like image 629
platinor Avatar asked Mar 24 '15 00:03

platinor


1 Answers

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.

like image 105
coder hacker Avatar answered Sep 20 '22 16:09

coder hacker