Skip to content

[Sema] NFC: Reorder misc bits for better code gen #16690

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

Conversation

davezarzycki
Copy link
Contributor

@davezarzycki davezarzycki commented May 17, 2018

As a general rule, if one wants packs miscellaneous bits into what is
otherwise just a number, the low bits are preferable over the high bits,
at least on x86. On ppc64/ppc64le, the results are more of a wash. For
reference:

struct X {
unsigned kind : 3;
unsigned level : 29;
};

X example(X x, unsigned y) {
x.level += y;
return x;
}

bool example(X x) {
return x.level > 1;
}

==== x86_64 with 'kind' first ====

_Z7example1Xj:
leal (%rdi,%rsi,8), %eax
retq

_Z7example1X:
cmpl $15, %edi
seta %al
retq

==== x86_64 with 'kind' last ====

_Z7example1Xj:
leal (%rsi,%rdi), %eax
andl $536870911, %eax # imm = 0x1FFFFFFF
andl $-536870912, %edi # imm = 0xE0000000
orl %edi, %eax
retq

_Z7example1X:
testl $536870910, %edi # imm = 0x1FFFFFFE
setne %al
retq

==== PPC64 with 'kind' first ====

_Z7example1Xj:
add 5, 5, 4
rlwimi 5, 4, 0, 0, 2
stw 5, 0(3)
blr

_Z7example1X:
rlwinm 3, 3, 0, 3, 30
cntlzw 3, 3
srwi 3, 3, 5
xori 3, 3, 1
blr

==== PPC64 with 'kind' last ====

_Z7example1Xj:
slwi 5, 5, 3
add 4, 5, 4
stw 4, 0(3)
blr

_Z7example1X:
clrldi 3, 3, 32
subfic 3, 3, 15
rldicl 3, 3, 1, 63
blr

==== PPC64LE with 'kind' first ====

_Z7example1Xj:
slwi 4, 4, 3
add 3, 4, 3
blr

_Z7example1X:
clrldi 3, 3, 32
subfic 3, 3, 15
rldicl 3, 3, 1, 63
blr

==== PPC64LE with 'kind' last ====

_Z7example1Xj:
add 4, 4, 3
rlwimi 3, 4, 0, 3, 31
blr

_Z7example1X:
rlwinm 3, 3, 0, 3, 30
cntlzw 3, 3
srwi 3, 3, 5
xori 3, 3, 1
blr

As a general rule, if one wants packs miscellaneous bits into what is
otherwise just a number, the low bits are preferable over the high bits,
at least on x86. On ppc64/ppc64le, the results are more of a wash. For
reference:

struct X {
    unsigned kind : 3;
    unsigned level : 29;
};

X example(X x, unsigned y) {
    x.level += y;
    return x;
}

bool example(X x) {
    return x.level > 1;
}

==== x86_64 with 'kind' first ====

_Z7example1Xj:
        leal    (%rdi,%rsi,8), %eax
        retq

_Z7example1X:
        cmpl    $15, %edi
        seta    %al
        retq

==== x86_64 with 'kind' last ====

_Z7example1Xj:
        leal    (%rsi,%rdi), %eax
        andl    $536870911, %eax        # imm = 0x1FFFFFFF
        andl    $-536870912, %edi       # imm = 0xE0000000
        orl     %edi, %eax
        retq

_Z7example1X:
        testl   $536870910, %edi        # imm = 0x1FFFFFFE
        setne   %al
        retq

==== PPC64 with 'kind' first ====

_Z7example1Xj:
        add 5, 5, 4
        rlwimi 5, 4, 0, 0, 2
        stw 5, 0(3)
        blr

_Z7example1X:
        rlwinm 3, 3, 0, 3, 30
        cntlzw  3, 3
        srwi 3, 3, 5
        xori 3, 3, 1
        blr

==== PPC64 with 'kind' last ====

_Z7example1Xj:
        slwi 5, 5, 3
        add 4, 5, 4
        stw 4, 0(3)
        blr

_Z7example1X:
        clrldi  3, 3, 32
        subfic 3, 3, 15
        rldicl 3, 3, 1, 63
        blr

==== PPC64LE with 'kind' first ====

_Z7example1Xj:
        slwi 4, 4, 3
        add 3, 4, 3
        blr

_Z7example1X:
        clrldi  3, 3, 32
        subfic 3, 3, 15
        rldicl 3, 3, 1, 63
        blr

==== PPC64LE with 'kind' last ====

_Z7example1Xj:
        add 4, 4, 3
        rlwimi 3, 4, 0, 3, 31
        blr

_Z7example1X:
        rlwinm 3, 3, 0, 3, 30
        cntlzw  3, 3
        srwi 3, 3, 5
        xori 3, 3, 1
        blr
@davezarzycki
Copy link
Contributor Author

@swift-ci please smoke test

@davezarzycki davezarzycki merged commit 551809d into swiftlang:master May 17, 2018
@davezarzycki davezarzycki deleted the nfc_reorder_misc_partial_application_diag_bits branch May 17, 2018 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant