[llvm-bugs] [Bug 44303] New: Missed optimization: removal of 'inc' and 'add' instructions

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Dec 14 19:54:53 PST 2019


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

            Bug ID: 44303
           Summary: Missed optimization: removal of 'inc' and 'add'
                    instructions
           Product: clang
           Version: trunk
          Hardware: PC
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jhg023 at bucknell.edu
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

uint64_t test( uint64_t a, uint64_t b, uint64_t n, int norm ) { // Line 1
      uint64_t prod = a * b;                                        // Line 2
      uint64_t r = ( norm - ( prod + 1 ) * n ) + n;                 // Line 3
      return ( r < n ) ? r : ( r - n );                             // Line 4
    }

The above function generates the following assembly, according to Godbolt
(https://godbolt.org/z/6AKLMu):

    test(unsigned long, unsigned long, unsigned long, int):                     
    # @test(unsigned long, unsigned long, unsigned long, int)
            imul    rdi, rsi
            movsxd  rax, ecx
            inc     rdi
            imul    rdi, rdx
            sub     rax, rdi
            add     rdx, rax
            cmovb   rax, rdx
            ret

As you can see, line 3 of the function is equivalent to the following snippet:

    uint64_t r = ( norm - prod * n );

With this change, I would expect the 'inc' and 'add' instructions to simply be
removed from the generated assembly. However, this isn't the case. In fact, it
generates assembly that's longer than the initial assembly
(https://godbolt.org/z/LkV4_D):

    test(unsigned long, unsigned long, unsigned long, int):                     
    # @test(unsigned long, unsigned long, unsigned long, int)
            imul    rdi, rsi
            movsxd  rax, ecx
            imul    rdi, rdx
            sub     rax, rdi
            xor     ecx, ecx
            cmp     rax, rdx
            cmovae  rcx, rdx
            sub     rax, rcx
            ret

Is this correct, or rather a missed optimization?

-- 
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/20191215/53af6e0c/attachment.html>


More information about the llvm-bugs mailing list