[llvm-commits] a question about type conversion propagation and elimination

Chris Lattner clattner at apple.com
Tue Jan 22 09:53:13 PST 2008


On Jan 22, 2008, at 2:10 AM, humeafo wrote:

> 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		; <i1>: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		; <i1>: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

>
> define i8* @tag_on(i8* %ptr, i8* %end, i8 %tag) {
> Entry:
>         ptrtoint i8* %end to i32                ; <i32>:0 [#uses=2]
>         ptrtoint i8* %ptr to i32                ; <i32>:1 [#uses=4]
>         icmp ugt i32 %0, %1             ; <i1>:2 [#uses=1]
>         br i1 %2, label %B1, label %B2
>
> B1:             ; preds = %Entry
>         inttoptr i32 %1 to i8*          ; <i8*>:3 [#uses=1]
>         store i8 1, i8* %3
>         add i32 %1, 1           ; <i32>:4 [#uses=1]
>         br label %B2
>
> B2:             ; preds = %Entry, %B1
>         %eax.0 = phi i32 [ %4, %B1 ], [ %1, %Entry ]            ;  
> <i32> [#uses=4]
>         icmp ugt i32 %0, %eax.0         ; <i1>:5 [#uses=1]
>         br i1 %5, label %B3, label %exit
>
> B3:             ; preds = %B2
>         inttoptr i32 %eax.0 to i8*              ; <i8*>:6 [#uses=1]
>         store i8 1, i8* %6
>         add i32 %eax.0, 1               ; <i32>:7 [#uses=1]
>         br label %exit
>
> exit:           ; preds = %B2, %B3
>         %eax.1 = phi i32 [ %7, %B3 ], [ %eax.0, %B2 ]           ;  
> <i32> [#uses=1]
>         inttoptr i32 %eax.1 to i8*              ; <i8*>:8 [#uses=1]
>         ret i8* %8
> }
>
> humeafo
> 2008-01-22
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20080122/eb6ca4b1/attachment.html>


More information about the llvm-commits mailing list