A simple case about SDiv

Hi,

I have a simple C case as following:

int test(int x, int y) {
return -x / -y;
}

With llvm-gcc -O1, I got:

define i32 @test(i32 %x, i32 %y) nounwind {
entry:
sub i32 0, %x ; :0 [#uses=1]
sub i32 0, %y ; :1 [#uses=1]
sdiv i32 %0, %1 ; :2 [#uses=1]
ret i32 %2
}

With llvm-gcc -O2, I got:

define i32 @test(i32 %x, i32 %y) nounwind {
entry:
sdiv i32 %x, %y ; :0 [#uses=1]
ret i32 %0
}

I wonder which pass does this transform.

I tried several passes, like -instcombine, -simplifycfg, -gcse -globaldce -globalopt -adce , but all failed to do this transform.

Anybody know this? Thanks.

Sheng.

I tried several passes, like -instcombine, -simplifycfg, -gcse -globaldce
-globalopt -adce , but all failed to do this transform.

Try "opt -std-compile-opts -debug-pass=Arguments"
If that does the simplification, then try bisecting
the set of passes it ran (printed thanks to -debug-pass)
to find out which combination did it.

Ciao,

Duncan.

Hi Duncan,

Thanks for your help.

But seems “opt -std-compile-opts” can’t do this simplication :frowning:

Any ideas?

Sheng.

2008/9/4 Duncan Sands <baldrick@free.fr>

No LLVM pass can do this transform; it assumes that integer overflow
is undefined, and the conversion to bitcode loses sign information.
The optimization level is probably triggering the gcc's folder to be
more aggressive.

-Eli

Any ideas?

Most likely it is the gcc folder doing it.
This gcc optimization is run in llvm-gcc
because it's basically impossible to turn
it off! You can check by passing
-mllvm -disable-llvm-optzns
to llvm-gcc along with -O2. If the
optimization still occurs then it was
gcc that did it.

Ciao,

Duncan.

2008/9/5 Duncan Sands <baldrick@free.fr>

Any ideas?

Most likely it is the gcc folder doing it.
This gcc optimization is run in llvm-gcc
because it’s basically impossible to turn
it off!

Agree. I found that gcc option -fstrict-overflow, with which gcc will do that simplication.
Defaultly, that option is turn on with -O2 or above.

You can check by passing
-mllvm -disable-llvm-optzns
to llvm-gcc along with -O2. If the
optimization still occurs then it was
gcc that did it.

eh… I got some error as following:

llvm-gcc -O2 -mllvm -disable-llvm-optzns -emit-llvm -c test.c -o test.ll -S

cc1: warning: unrecognized gcc debugging option: i
cc1: warning: unrecognized gcc debugging option: s
cc1: warning: unrecognized gcc debugging option: b
cc1: warning: unrecognized gcc debugging option: l
cc1: warning: unrecognized gcc debugging option: e
cc1: warning: unrecognized gcc debugging option: -
cc1: warning: unrecognized gcc debugging option: l
cc1: warning: unrecognized gcc debugging option: l
cc1: warning: unrecognized gcc debugging option: m
cc1: warning: unrecognized gcc debugging option: -
cc1: warning: unrecognized gcc debugging option: o
cc1: warning: unrecognized gcc debugging option: t
cc1: warning: unrecognized gcc debugging option: z
cc1: warning: unrecognized gcc debugging option: n
cc1: warning: unrecognized gcc debugging option: s
cc1: Unknown command line argument ‘-mtune=generic’. Try: ‘cc1 --help’

Am I missing something?

Sheng.