https://bugs.llvm.org/show_bug.cgi?id=42848

            Bug ID: 42848
           Summary: Missed Optimization: redundant ternary expression not
                    removed
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedb...@nondot.org
          Reporter: jameshamm1...@gmail.com
                CC: htmldevelo...@gmail.com, llvm-bugs@lists.llvm.org

Missed optimization on a ternary expression which has two equivalent output
paths.

Example code (https://godbolt.org/z/6dYE7d) compiled with clang (trunk,
currently 10.0.0) -Ofast -march=skylake

int f(int a, int b) {
    return (a == b) ? (a + b) : (a + a);
}

produces

f(int, int):                                 # @f(int, int)
        lea     ecx, [rsi + rdi]
        lea     eax, [rdi + rdi]
        cmp     edi, esi
        cmove   eax, ecx
        ret

Since a == b, the code can be reduced to

int f(int a, int b) {
    return (a == b) ? (a + a) : (a + a);
}

which produces the much shorter assembly

f(int, int):                                 # @f(int, int)
        lea     eax, [rdi + rdi]
        ret


This result is correct according to alive (https://rise4fun.com/Alive/Ozp)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to