Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing ANDI optimization #343

Closed
nwf opened this issue Sep 27, 2019 · 2 comments
Closed

Missing ANDI optimization #343

nwf opened this issue Sep 27, 2019 · 2 comments

Comments

@nwf
Copy link
Member

nwf commented Sep 27, 2019

Consider

#include <stdint.h>

uint64_t foo(uint64_t a)
{
    uint64_t b = a / 16;
    uint64_t c = b & 0x7UL;
    return ((uint64_t)1UL << c);
}

At present, the MIPS backend produces:

foo(unsigned long):                                # @foo(unsigned long)
        sll     $1, $4, 0
        srl     $1, $1, 4
        andi    $1, $1, 7
        daddiu  $2, $zero, 1
        jr      $ra
        dsllv   $2, $2, $1

The sll is introduced because the srl requires that its input be zero extended (which, well, seems silly, but so it goes). In any case, because andi has a 16-bit immediate, some arithmetic and lookahead could find that 0x7 << 4 fits and so make this be

        andi    $1, $4, 0x70
        srl     $1, $1, 4
        daddiu  $2, $zero, 1
        jr      $ra
        dsllv   $2, $2, $1

Alternatively, whatever's concluding that it can use 32-bit values internally could stop doing that and this could instead just be

        dsrl    $1, $4, 4
        andi    $1, $1, 7
        daddiu  $2, $zero, 1
        jr      $ra
        dsllv   $2, $2, $1
@arichardson
Copy link
Member

Reported upstream as https://bugs.llvm.org/show_bug.cgi?id=43481

@arichardson
Copy link
Member

Fixed upstream in llvm/llvm-project@e711a0c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants