[LLVMdev] Simplifying boolean expressions

Vladimir Prus ghost at cs.msu.su
Wed May 4 05:35:07 PDT 2005


Hello!
Suppose I have a code like this:

       %tmp.aux = cast bool %tmp.24 to int
        %tmp.x = xor int  %tmp.aux, 1                    ; negates tmp.24
        %tmp.xx = cast int %tmp.x to bool
        %tmp.y = or bool %tmp.xx, %tmp.24          ;  will be always true 
        br bool %tmp.y, label %next6, label %next7

Is there an optimization in LLVM that will recognize that %tmp.y is always 
true, and replace the entire basic block with unconditional jump?

I've tried running 'opt' on the attached file, but did not get the desired 
effect.

The full story is that I'll be generating code like above, and while I 
probably can detect such redundant situation, I was hoping I can save myself 
some work, and just run LLVM optimizer at the produced code.

TIA,
Volodya





-------------- next part --------------
; ModuleID = 'a.o'
target endian = little
target pointersize = 32
target triple = "i686-pc-linux-gnu"
deplibs = [ "stdc++", "c", "crtend" ]
%i = external global int		; <int*> [#uses=2]
%j = external global int		; <int*> [#uses=3]

implementation   ; Functions:

int %_Z3fooi(int %k) {
entry:
	%tmp.24 = setgt int %k, 0		; <bool> [#uses=1]
	%tmp.35 = load int* %i		; <int> [#uses=2]
	br label %next1
;	br bool %tmp.24, label %no_exit.preheader, label %loopexit

next1:
	br bool %tmp.24, label %next2, label %next3
next2:
	%tmp.100 = add int %tmp.35, 1
	store int %tmp.100, int* %j
	br label %next3
next3:  
	br bool %tmp.24, label %next4, label %next5
next4:
	%tmp.102 = add int %tmp.35, 4
	store int %tmp.102, int* %j
	br label %next5
next5:  
	%tmp.aux = cast bool %tmp.24 to int
	%tmp.x = xor int  %tmp.aux, 1
	%tmp.xx = cast int %tmp.x to bool
	%tmp.y = or bool %tmp.xx, %tmp.24
	br bool %tmp.y, label %next6, label %next7
next6:
	ret int 1
next7:  ret int 2
}


More information about the llvm-dev mailing list