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 }
On Wed, 4 May 2005, Vladimir Prus wrote:> 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.I will enhance the -instcombine pass to do this, hopefully today. -Chris -- http://nondot.org/sabre/ http://llvm.cs.uiuc.edu/
On Wed, 4 May 2005, Vladimir Prus wrote:> > %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?Actually, the -instcombine pass already does this. Please try it out and let me know if it doesn't do what you want. -Chris -- http://nondot.org/sabre/ http://llvm.cs.uiuc.edu/
On Wednesday 04 May 2005 18:34, Chris Lattner wrote:> On Wed, 4 May 2005, Vladimir Prus wrote: > > %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? > > Actually, the -instcombine pass already does this. Please try it out and > let me know if it doesn't do what you want.It does work! For some reason, I was assuming that running 'opt' without arguments would run some "reasonable" set of optimizations, while in reality, it does not run any. Thanks, Volodya