suboptimal code for range check
| Bugzilla Link | 35557 |
| Resolution | INVALID |
| Resolved on | Dec 07, 2017 09:25 |
| Version | trunk |
| OS | Linux |
| CC | @chandlerc,@vns-mn,@rotateright |
Extended Description
clang version 6.0.0 (trunk 320027)
int foobar(int x) { return x > 16 && x < 32; }
-O3 produces:
4004d0: 31 c0 xor %eax,%eax 4004d2: 83 ff 11 cmp $0x11,%edi 4004d5: 7c 08 jl 4004df <foobar+0xf> 4004d7: 31 c0 xor %eax,%eax 4004d9: 83 ff 20 cmp $0x20,%edi 4004dc: 0f 9c c0 setl %al 4004df: c3 retq
a more optimal code would be:
400480: 83 ef 11 sub $0x11,%edi 400483: 31 c0 xor %eax,%eax 400485: 83 ff 0e cmp $0xe,%edi 400488: 0f 96 c0 setbe %al 40048b: c3 retq
Besides the sub trick, it's unclear why llvm generates a duplicate "xor %eax,%eax". That may be a second bug.
Doh! Sorry, I had some weird local diff. Then it's a bug in sanitizer coverage: https://github.com/google/sanitizers/issues/893#issuecomment-350036791 I guess with that diff I actually wanted to work around the coverage bug some time ago.
Not sure what exactly you want:
Include '-S' still. I want to see if the IR is different than this:
$ ./clang 35557.c -O3 -fomit-frame-pointer -S -o - -fomit-frame-pointer -target x86_64-unknown-gnu-linux -emit-llvm ; ModuleID = '35557.c' source_filename = "35557.c" target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"
; Function Attrs: norecurse nounwind readnone uwtable define i32 @foobar(i32 %x) local_unnamed_addr #0 { entry: %x.off = add i32 %x, -17 %0 = icmp ult i32 %x.off, 15 %land.ext = zext i1 %0 to i32 ret i32 %land.ext }
...
Not sure what exactly you want:
$ clang /tmp/test.c -O3 -c -emit-llvm -o -
BC��5b
0$LY����>-U��!
�
!�#�A�I29��
�b�EB�
B�2K
2B�H� CF��2B�H�"�PAQ������!FQ &������@(
�qxy��rz�|hyx�zpr(rhrH{Hr(�6��x�zhs��̡2"́��� d�"��"ㄡ�L���r�5��y(9�5�<���w��p0�rhs��6h�p�t�!�a� �aʡ ��s�k<D��@�!�Ap�q x���zv�s zt��r�!L��0|�;�;��6�wXwx�{p�6�tp�z��68w�� Hl(��2� L�� &G�C�(�t���f=�C8�ÌB�yxs�q ��3 B��Ρf0=�C8���=�C=�=�x�tpyH�ppzpvx�p ���0n0��P3��!�!�af0�;��;�C9�<��<�;��v{h7h�rh7��p��pv(v�vx�w���q�r��y��,����0bȡ��̡���a�!ā�aC9�C9�C9�C9��8�C8�;��/��<��;�;�� �i�pX�rp�thx�t�t���S��P�@� �P3 (���A�!܁�����fQ8�C:��;�P$v{h7�wxx�QL�P3j�a�!��~���!�aT��8��;�C=�C9��<�C;��;�Ì
�y��w�z(r��\���P�0#��A�����y #bH C�
9$��@���h"2�!GȐQ8�wchar_sizeclang version 6.0.0 (trunk 320027)�0�
3
C@�2�������\������\�����F �T����\�����F
�
�`���!�!����<��;�;�=��<�C8��a C,R@# � 7\1K �� � a A,#q 2"��;e #� DPP�* 5����$����$] �ufoobarmain6.0.0svnx86_64-unknown-linux-gnu/tmp/test.clinux2$ 62;9;c
I backtracked to r320027 and specified the target as you've shown it, but I still don't see the same results. What do you get if you '-emit-llvm'?
$ cat 35557.c int foobar(int x) { return x > 16 && x < 32; }
$ ./clang 35557.c -O3 -fomit-frame-pointer -S -o - -fomit-frame-pointer -target x86_64-unknown-gnu-linux .text .file "35557.c" .globl foobar # -- Begin function foobar .p2align 4, 0x90 .type foobar,@function foobar: # @foobar .cfi_startproc
%bb.0: # %entry
addl $-17, %edi
xorl %eax, %eax
cmpl $15, %edi
setb %al
retq
.Lfunc_end0: .size foobar, .Lfunc_end0-foobar .cfi_endproc # -- End function
.ident "clang version 6.0.0 (trunk 320027)"
.section ".note.GNU-stack","",@progbits
Here is what I see:
$ cat test.c int foobar(int x) { return x > 16 && x < 32; }
$ clang -v clang version 6.0.0 (trunk 320027) Target: x86_64-unknown-linux-gnu
$ clang test.c -O3 -fomit-frame-pointer -S -o - -fomit-frame-pointer .text .file "test1.c" .globl foobar # -- Begin function foobar .p2align 4, 0x90 .type foobar,@function foobar: # @foobar .cfi_startproc
%bb.0:
xorl %eax, %eax
cmpl $17, %edi
jl .LBB0_2
%bb.1:
xorl %eax, %eax
cmpl $32, %edi
setl %al
.LBB0_2: retq .Lfunc_end0: .size foobar, .Lfunc_end0-foobar .cfi_endproc # -- End function
.ident "clang version 6.0.0 (trunk 320027)"
.section ".note.GNU-stack","",@progbits
Are we missing some context to reproduce?
$ ./clang -v clang version 6.0.0 (trunk 320045) Target: x86_64-apple-darwin17.2.0 Thread model: posix
$ ./clang -O3 -fomit-frame-pointer 35557.c -S -o - addl $-17, %edi xorl %eax, %eax cmpl $15, %edi setb %al retq