[llvm-dev] How to get llvm SSA IR?

Qiuhong Pan via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 20 03:02:00 PDT 2019


Hi,

Now I want use llvm to do pointer analysis, and I use -mem2reg to get the
SSA IR, but when I analysis source code with pointer, the IR will not in
SSA format. For example, the following C code:
void test() {
int a, b, c, d;
int *p, *q, *m, *n;

p = &a;
get2(&p);
q = &b;
get2(&q);
m = &c;
get2(&m);
n = &d;
get2(&n);
p = q;
m = p;
p = n;
get2(&n);
}

I use the following command to get IR result:
clang -S -emit-llvm -Xclang -disable-O0-optnone -c new.c -o new.ll
opt -S -mem2reg new.ll -o new.opt.ll

It's IR:
define dso_local void @test() #0 {
entry:
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  %c = alloca i32, align 4
  %d = alloca i32, align 4
  %p = alloca i32*, align 4
  %q = alloca i32*, align 4
  %m = alloca i32*, align 4
  %n = alloca i32*, align 4
  store i32* %a, i32** %p, align 4
  call void @get2(i32** %p)
  store i32* %b, i32** %q, align 4
  call void @get2(i32** %q)
  store i32* %c, i32** %m, align 4
  call void @get2(i32** %m)
  store i32* %d, i32** %n, align 4
  call void @get2(i32** %n)
  %0 = load i32*, i32** %q, align 4
  store i32* %0, i32** %p, align 4
  %1 = load i32*, i32** %p, align 4
  store i32* %1, i32** %m, align 4
  %2 = load i32*, i32** %n, align 4
  store i32* %2, i32** %p, align 4
  call void @get2(i32** %n)
  ret void
}

I want to distinguish varitable p in p = q;p = n, like p0 = q;p1 = n. How
can I get the SSA IR that variables defined multiple times in the original
representation will be split into separate instances?

Also, when I use -mem2reg to get IR, they would do some optimization, such
as remove some unnecessary stack allocation. Can I get SSA IR without other
optimization?

Thank you,
panqaq
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190320/00da73c7/attachment.html>


More information about the llvm-dev mailing list