[llvm-bugs] [Bug 42848] New: Missed Optimization: redundant ternary expression not removed

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jul 31 11:13:53 PDT 2019


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: unassignedbugs at nondot.org
          Reporter: jameshamm1995 at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190731/a0b36eec/attachment.html>


More information about the llvm-bugs mailing list