Hi,
I would like to know how to obtain LLVM IR such that the temporaries to which the value to a variable is loaded are directly used later wherever there are its uses instead of loading into new registers for each uses?
Consider the following C program
#include <stdio.h>
int main()
{
int a,b, c, d;
scanf("%d", &a);
scanf("%d", &b);
c = (a + b);
d = (a + b);
return 0;
}
I tried mem2reg pass for the same. The LLVM IR after trying mem2reg optimisation is given below;
bb:
%i1 = alloca i32, align 4
%i2 = alloca i32, align 4
%i5 = bitcast i32* %i1 to i8*
call void @llvm.lifetime.start.p0i8(i64 4, i8* %i5) #3
%i6 = bitcast i32* %i2 to i8*
call void @llvm.lifetime.start.p0i8(i64 4, i8* %i6) #3
%i9 = call i32 (i8*, ...) @__isoc99_scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %i1)
%i10 = call i32 (i8*, ...) @__isoc99_scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %i2)
%i11 = load i32, i32* %i1, align 4, !tbaa !3
%i12 = load i32, i32* %i2, align 4, !tbaa !3
%i13 = add nsw i32 %i11, %i12
%i14 = load i32, i32* %i1, align 4, !tbaa !3
%i15 = load i32, i32* %i2, align 4, !tbaa !3
%i16 = add nsw i32 %i14, %i15
%i19 = bitcast i32* %i2 to i8*
call void @llvm.lifetime.end.p0i8(i64 4, i8* %i19) #3
%i20 = bitcast i32* %i1 to i8*
call void @llvm.lifetime.end.p0i8(i64 4, i8* %i20) #3
ret i32 0
If we see in the IR the same variable “a” and “b” are used at two places but for each use there are different loads to temporaries. How can i generate IR such that %i11 and %i12 are used in the second “add” instruction too ?