I am a newbie to LLVM, so I have to say sorry if I asked the question in the wrong place.
In some cases when I generate LLVM IR from machine assembly(with limited type information) I have to convert the pointers to I32, after the standard mem2reg pass there still are things like:inttoptr i32 %1 to i8*
While in fact this can be replaced by the I8* operations, because the I8* can be unsigned compared, and get rid of the redundant Instrs, Is there any pass
can do this, or is there any tricks to avoid these redundant conversions while retain the meaning of the program?
Sure, the instcombine pass will do this. Make sure to add the correct target data string though. For example, if I add the datalayout string for x86-32 darwin:
target datalayout = “e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128”
Instcombine produces:
define i8* @tag_on(i8* %ptr, i8* %end, i8 %tag) {
Entry:
icmp ugt i8* %end, %ptr ; :0 [#uses=1]
br i1 %0, label %B1, label %B2
B1: ; preds = %Entry
store i8 1, i8* %ptr
%ctg2 = getelementptr i8* %ptr, i32 1 ; <i8*> [#uses=1]
br label %B2
B2: ; preds = %B1, %Entry
%eax.0.in = phi i8* [ %ctg2, %B1 ], [ %ptr, %Entry ] ; <i8*> [#uses=4]
icmp ult i8* %eax.0.in, %end ; :1 [#uses=1]
br i1 %1, label %B3, label %exit
B3: ; preds = %B2
store i8 1, i8* %eax.0.in
%ctg21 = getelementptr i8* %eax.0.in, i32 1 ; <i8*> [#uses=1]
br label %exit
exit: ; preds = %B3, %B2
%eax.1.in = phi i8* [ %ctg21, %B3 ], [ %eax.0.in, %B2 ] ; <i8*> [#uses=1]
ret i8* %eax.1.in
}
Note that it eliminated all the ptrtoint casts (in this case) and infered some getelementptrs.
I’m sending this to the llvmdev list, which is a better place for high-level questions than llvm-commits.
-Chris