[LLVMdev] Doubt with GVNPRE
ambika
ambika at cse.iitb.ac.in
Thu Mar 4 12:46:50 PST 2010
Hi,
I have a program as:
int main(int argc, char **argv)
{
int a,b,k,l;
scanf("%d%d",&a,&b);
if (argc > 1) {
k=a+b;
} else {
k=5;
}
l=a+b;
printf("%d,%d",k,l);
return 0;
}
Now i run the following on it:
llvm-gcc -O1 -emit-llvm -c -o 1.bc 1.c
llvm-dis 1.bc -o 1.ll
opt -gvnpre 1.bc -o 1p.bc // I believe this line writes
the code to 1p.bc after applying gvnpre to 1.bc
llvm-dis 1p.bc -o 1p.ll
Now 1.ll is as :
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
entry:
%b = alloca i32, align 4 ; <i32*> [#uses=3]
%a = alloca i32, align 4 ; <i32*> [#uses=3]
%0 = call i32 (i8*, ...)* @scanf(i8* getelementptr inbounds ([5 x i8]*
@.str, i32 0, i32 0), i32* %a, i32* %b) nounwind ; <i32> [#uses=0]
%1 = icmp sgt i32 %argc, 1 ; <i1> [#uses=1]
br i1 %1, label %bb, label %bb2
bb: ; preds = %entry
%2 = load i32* %a, align 4 ; <i32> [#uses=1]
%3 = load i32* %b, align 4 ; <i32> [#uses=1]
%4 = add nsw i32 %3, %2 ; <i32> [#uses=1]
br label %bb2
bb2: ; preds = %bb, %entry
%k.0 = phi i32 [ %4, %bb ], [ 5, %entry ] ; <i32> [#uses=1]
%5 = load i32* %a, align 4 ; <i32> [#uses=1]
%6 = load i32* %b, align 4 ; <i32> [#uses=1]
%7 = add nsw i32 %6, %5 ; <i32> [#uses=1]
%8 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x
i8]* @.str1, i32 0, i32 0), i32 %k.0, i32 %7) nounwind ; <i32> [#uses=0]
ret i32 0
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And 1p.ll as:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
entry:
%b = alloca i32, align 4 ; <i32*> [#uses=3]
%a = alloca i32, align 4 ; <i32*> [#uses=3]
%0 = call i32 (i8*, ...)* @scanf(i8* getelementptr inbounds ([5 x i8]*
@.str, i32 0, i32 0), i32* %a, i32* %b) nounwind ; <i32> [#uses=0]
%1 = icmp sgt i32 %argc, 1 ; <i1> [#uses=1]
br i1 %1, label %bb, label %entry.bb2_crit_edge
entry.bb2_crit_edge: ; preds = %entry
br label %bb2
bb: ; preds = %entry
%2 = load i32* %a, align 4 ; <i32> [#uses=1]
%3 = load i32* %b, align 4 ; <i32> [#uses=1]
%4 = add nsw i32 %3, %2 ; <i32> [#uses=1]
br label %bb2
bb2: ; preds = %bb,
%entry.bb2_crit_edge
%k.0 = phi i32 [ %4, %bb ], [ 5, %entry.bb2_crit_edge ] ; <i32> [#uses=1]
%5 = load i32* %a, align 4 ; <i32> [#uses=1]
%6 = load i32* %b, align 4 ; <i32> [#uses=1]
%7 = add nsw i32 %6, %5 ; <i32> [#uses=1]
%8 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x
i8]* @.str1, i32 0, i32 0), i32 %k.0, i32 %7) nounwind ; <i32> [#uses=0]
ret i32 0
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I believe GVNPRE should have remove partial redundancy by introducing an
extra computation in entry.bb2_crit_edge.
But this does not happen, redundancy still remains.
Can someone tell me where am I going wrong.
Thanks and Regards,
Ambika
More information about the llvm-dev
mailing list