[LLVMbugs] [Bug 5189] New: trichotomy of scalars not optimized

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Wed Oct 14 10:21:13 PDT 2009


http://llvm.org/bugs/show_bug.cgi?id=5189

           Summary: trichotomy of scalars not optimized
           Product: libraries
           Version: 2.6
          Platform: Macintosh
        OS/Version: Mac System 9.x
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Scalar Optimizations
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: ggreif at gmail.com
                CC: llvmbugs at cs.uiuc.edu


look at this code:

//////////////////////////////
extern int compare(int X, int Y);
int compare(int X, int Y) {
  return !(X < Y) && !(X > Y);
}

int main(int argc, char **argv) {
  return compare(argc, 42);
}
//////////////////////////////

llvm-gcc compiles it to equality comparison:

define i32 @main(i32 %argc, i8** nocapture %argv) nounwind readnone {
entry:
        %0 = icmp eq i32 %argc, 42              ; <i1> [#uses=1]
        %1 = zext i1 %0 to i32          ; <i32> [#uses=1]
        ret i32 %1
}





But no such luck with pretty recent clang:

MacBook-de-Patricia:bin ggreif$ ./clang -x c -S -o - -emit-llvm - -O2
extern int compare(int X, int Y);
int compare(int X, int Y) {
  return !(X < Y) && !(X > Y);
}

int main(int argc, char **argv) {
  return compare(argc, 42);
}
; ModuleID = '-'
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"
target triple = "i386-apple-darwin9.0"

define i32 @compare(i32 %X, i32 %Y) nounwind readnone {
  %1 = icmp slt i32 %X, %Y                        ; <i1> [#uses=1]
  br i1 %1, label %4, label %2

; <label>:2                                       ; preds = %0
  %3 = icmp sle i32 %X, %Y                        ; <i1> [#uses=1]
  %phitmp = zext i1 %3 to i32                     ; <i32> [#uses=1]
  ret i32 %phitmp

; <label>:4                                       ; preds = %0
  ret i32 0
}

define i32 @main(i32 %argc, i8** nocapture %argv) nounwind readnone {
  %1 = icmp slt i32 %argc, 42                     ; <i1> [#uses=1]
  br i1 %1, label %compare.exit, label %2

; <label>:2                                       ; preds = %0
  %3 = icmp slt i32 %argc, 43                     ; <i1> [#uses=1]
  %phitmp.i = zext i1 %3 to i32                   ; <i32> [#uses=1]
  ret i32 %phitmp.i

compare.exit:                                     ; preds = %0
  ret i32 0
}




And also the x86 backend does not get it:

MacBook-de-Patricia:bin ggreif$ ./clang -x c -S -o -  - -O2
extern int compare(int X, int Y);
int compare(int X, int Y) {
  return !(X < Y) && !(X > Y);
}

int main(int argc, char **argv) {
  return compare(argc, 42);
}
^D

        .section        __TEXT,__text,regular,pure_instructions
        .align  4, 0x90
        .globl  _compare
_compare:
        pushl   %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %eax
        movl    8(%ebp), %ecx
        cmpl    %eax, %ecx
        jl      LBB1_2
        cmpl    %eax, %ecx
        setle   %al
        movzbl  %al, %eax
        popl    %ebp
        ret
LBB1_2:
        xorl    %eax, %eax
        popl    %ebp
        ret


        .align  4, 0x90
        .globl  _main
_main:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        cmpl    $42, %eax
        jl      LBB2_2
        cmpl    $43, %eax
        setl    %al
        movzbl  %al, %eax
        popl    %ebp
        ret
LBB2_2:
        xorl    %eax, %eax
        popl    %ebp
        ret
        .subsections_via_symbols



Of course when done right this should do the right thing with pointers too and
structs where the members are obeying trichotomy (transitively) in C++. It
would definitely help in some STL algorithms.


-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list