Hello, guys.
I just tested -reg2mem pass to see how it changes my bitcode.
E.g., for the following simple C code:
-------------------------------------------------------------
int foo() {
int i,j;
int sum = 0;
for (i=0; i<10; i++)
{
sum += i;
for (j=0; j<3; j++)
sum += 2;
}
return sum;
}
-------------------------------------------------------------
I could get the corresponding LLVM assembly with llvm-gcc and llvm-dis:
-------------------------------------------------------------
int %foo() {
entry:
br label %bb8.outer
bb8.outer: ; preds = %bb10, %entry
%indvar26 = phi uint [ 0, %entry ], [ %indvar.next27, %bb10 ] ; <uint>
[#uses=2]
%sum.0.pn.ph = phi int [ 0, %entry ], [ %sum.1, %bb10 ] ; <int>
[#uses=1]
%i.0.0.ph = cast uint %indvar26 to int ; <int> [#uses=1]
br label %bb8
bb3: ; preds = %bb8
%indvar.next = add uint %indvar, 1 ; <uint> [#uses=1]
br label %bb8
bb8: ; preds = %bb3, %bb8.outer
%indvar = phi uint [ 0, %bb8.outer ], [ %indvar.next, %bb3 ] ; <uint>
[#uses=2]
%sum.0.pn = phi int [ %sum.1, %bb3 ], [ %sum.0.pn.ph, %bb8.outer ] ;
<int> [#uses=1]
%i.0.pn = phi int [ 2, %bb3 ], [ %i.0.0.ph, %bb8.outer ] ; <int>
[#uses=1]
%sum.1 = add int %i.0.pn, %sum.0.pn ; <int> [#uses=3]
%exitcond = seteq uint %indvar, 3 ; <bool> [#uses=1]
br bool %exitcond, label %bb10, label %bb3
bb10: ; preds = %bb8
%indvar.next27 = add uint %indvar26, 1 ; <uint> [#uses=2]
%exitcond28 = seteq uint %indvar.next27, 10 ; <bool> [#uses=1]
br bool %exitcond28, label %bb16, label %bb8.outer
bb16: ; preds = %bb10
ret int %sum.1
}
-------------------------------------------------------------
And I did "opt -reg2mem" for the bitcode, which is emitted like this:
-------------------------------------------------------------
int %foo() {
allocablock:
%indvar26 = alloca uint ; <uint*> [#uses=3]
%sum.0.pn.ph = alloca int ; <int*> [#uses=2]
%i.0.0.ph = alloca int ; <int*> [#uses=2]
%indvar.next = alloca uint ; <uint*> [#uses=2]
%indvar = alloca uint ; <uint*> [#uses=3]
%sum.1 = alloca int ; <int*> [#uses=4]
%indvar.next27 = alloca uint ; <uint*> [#uses=3]
br label %entry
entry: ; preds = %allocablock
br label %bb8.outer
bb8.outer: ; preds = %bb10.bb8.outer_crit_edge, %entry
%indvar26 = phi uint [ 0, %entry ], [ %indvar.next27.reload,
%bb10.bb8.outer_crit_edge ] ; <uint> [#uses=1]
%sum.0.pn.ph = phi int [ 0, %entry ], [ %sum.1.reload,
%bb10.bb8.outer_crit_edge ] ; <int> [#uses=1]
store uint %indvar26, uint* %indvar26
store int %sum.0.pn.ph, int* %sum.0.pn.ph
%indvar26.reload5 = load uint* %indvar26 ; <uint> [#uses=1]
%i.0.0.ph = cast uint %indvar26.reload5 to int ; <int> [#uses=1]
store int %i.0.0.ph, int* %i.0.0.ph
%i.0.0.ph.reload = load int* %i.0.0.ph ; <int> [#uses=1]
%sum.0.pn.ph.reload = load int* %sum.0.pn.ph ; <int> [#uses=1]
br label %bb8
bb3: ; preds = %bb8
%indvar.reload = load uint* %indvar ; <uint> [#uses=1]
%indvar.next = add uint %indvar.reload, 1 ; <uint> [#uses=1]
store uint %indvar.next, uint* %indvar.next
%sum.1.reload2 = load int* %sum.1 ; <int> [#uses=1]
%indvar.next.reload = load uint* %indvar.next ; <uint> [#uses=1]
br label %bb8
bb8: ; preds = %bb3, %bb8.outer
%indvar = phi uint [ 0, %bb8.outer ], [ %indvar.next.reload, %bb3 ] ;
<uint> [#uses=1]
%sum.0.pn = phi int [ %sum.1.reload2, %bb3 ], [ %sum.0.pn.ph.reload, %bb8.outer
] ; <int> [#uses=1]
%i.0.pn = phi int [ 2, %bb3 ], [ %i.0.0.ph.reload, %bb8.outer ] ; <int>
[#uses=1]
store uint %indvar, uint* %indvar
%sum.1 = add int %i.0.pn, %sum.0.pn ; <int> [#uses=1]
store int %sum.1, int* %sum.1
%indvar.reload4 = load uint* %indvar ; <uint> [#uses=1]
%exitcond = seteq uint %indvar.reload4, 3 ; <bool> [#uses=1]
br bool %exitcond, label %bb10, label %bb3
bb10: ; preds = %bb8
%indvar26.reload = load uint* %indvar26 ; <uint> [#uses=1]
%indvar.next27 = add uint %indvar26.reload, 1 ; <uint> [#uses=1]
store uint %indvar.next27, uint* %indvar.next27
%indvar.next27.reload1 = load uint* %indvar.next27 ; <uint> [#uses=1]
%exitcond28 = seteq uint %indvar.next27.reload1, 10 ; <bool> [#uses=1]
br bool %exitcond28, label %bb16, label %bb10.bb8.outer_crit_edge
bb10.bb8.outer_crit_edge: ; preds = %bb10
%indvar.next27.reload = load uint* %indvar.next27 ; <uint> [#uses=1]
%sum.1.reload = load int* %sum.1 ; <int> [#uses=1]
br label %bb8.outer
bb16: ; preds = %bb10
%sum.1.reload3 = load int* %sum.1 ; <int> [#uses=1]
ret int %sum.1.reload3
}
-------------------------------------------------------------
Sorry for this long code.
After -reg2mem, I could find any of PHIs weren't eliminated.
I just wonder if this is normal.
Thanks,
Seung J. Lee