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

cmd/compile: optimize away useless ANDs and ORs #14367

Closed
dsnet opened this issue Feb 17, 2016 · 5 comments
Closed

cmd/compile: optimize away useless ANDs and ORs #14367

dsnet opened this issue Feb 17, 2016 · 5 comments

Comments

@dsnet
Copy link
Member

dsnet commented Feb 17, 2016

Using go1.5.3

The following code has a few useless expressions:

return 0 | // Useless OR with 0
    ((n & 0xff) << 56) | // Useless AND with 0xff followed by left-shift 56
    ((n & 0xff00) << 40) |
    ((n & 0xff0000) << 24) |
    ((n & 0xff000000) << 8) |
    ((n & 0xff00000000) >> 8) |
    ((n & 0xff0000000000) >> 24) |
    ((n & 0xff000000000000) >> 40) |
    ((n & 0xff00000000000000) >> 56) // Useless AND with 0xff00... followed by right-shift 56

The instructions with # marks are unnecessary. It would be nice if the compiler did not generate them.

"".bswap64a t=1 size=160 value=0 args=0x10 locals=0x0
    ...
    0x0005 00005 (byteswap.go:14)    MOVQ    AX, BX
#   0x0008 00008 (byteswap.go:14)    ANDQ    $255, BX
    0x000f 00015 (byteswap.go:14)    SHLQ    $56, BX
#   0x0013 00019 (byteswap.go:14)    ORQ $0, BX
    0x0017 00023 (byteswap.go:14)    MOVQ    AX, BP
    0x001a 00026 (byteswap.go:14)    ANDQ    $65280, BP
    0x0021 00033 (byteswap.go:14)    SHLQ    $40, BP
    0x0025 00037 (byteswap.go:14)    ORQ BP, BX
    0x0028 00040 (byteswap.go:14)    MOVQ    AX, BP
    0x002b 00043 (byteswap.go:14)    ANDQ    $16711680, BP
    0x0032 00050 (byteswap.go:14)    SHLQ    $24, BP
    0x0036 00054 (byteswap.go:14)    ORQ BP, BX
    0x0039 00057 (byteswap.go:14)    MOVQ    $4278190080, BP
    0x003e 00062 (byteswap.go:14)    ANDQ    AX, BP
    0x0041 00065 (byteswap.go:14)    SHLQ    $8, BP
    0x0045 00069 (byteswap.go:14)    ORQ BP, BX
    0x0048 00072 (byteswap.go:14)    MOVQ    $1095216660480, BP
    0x0052 00082 (byteswap.go:14)    ANDQ    AX, BP
    0x0055 00085 (byteswap.go:14)    SHRQ    $8, BP
    0x0059 00089 (byteswap.go:14)    ORQ BP, BX
    0x005c 00092 (byteswap.go:14)    MOVQ    $280375465082880, BP
    0x0066 00102 (byteswap.go:14)    ANDQ    AX, BP
    0x0069 00105 (byteswap.go:14)    SHRQ    $24, BP
    0x006d 00109 (byteswap.go:14)    ORQ BP, BX
    0x0070 00112 (byteswap.go:14)    MOVQ    $71776119061217280, BP
    0x007a 00122 (byteswap.go:14)    ANDQ    AX, BP
    0x007d 00125 (byteswap.go:14)    SHRQ    $40, BP
    0x0081 00129 (byteswap.go:14)    ORQ BP, BX
#   0x0084 00132 (byteswap.go:14)    MOVQ    $-72057594037927936, BP
    0x008e 00142 (byteswap.go:14)    ANDQ    AX, BP
    0x0091 00145 (byteswap.go:14)    SHRQ    $56, BP
    0x0095 00149 (byteswap.go:14)    ORQ BP, BX
    0x0098 00152 (byteswap.go:14)    MOVQ    BX, "".~r1+16(FP)
    0x009d 00157 (byteswap.go:14)    RET
@bradfitz
Copy link
Contributor

Have you tried the ssa branch?

@dsnet
Copy link
Member Author

dsnet commented Feb 17, 2016

Just tried it on dev.ssa bc1fb32

It elides instructions for OR zero, but strangely does a triple shift in some places:

"".byteswap t=1 size=160 value=0 args=0x10 locals=0x0
    ...
    0x0005 00005 (byteswap.go:5)    MOVQ    AX, CX
    0x0008 00008 (byteswap.go:5)    SHLQ    $56, CX
#   0x000c 00012 (byteswap.go:5)    SHRQ    $56, CX
#   0x0010 00016 (byteswap.go:5)    SHLQ    $56, CX
    0x0014 00020 (byteswap.go:6)    MOVQ    AX, DX
    0x0017 00023 (byteswap.go:6)    ANDQ    $65280, DX
    0x001e 00030 (byteswap.go:6)    SHLQ    $40, DX
    0x0022 00034 (byteswap.go:6)    ORQ DX, CX
    0x0025 00037 (byteswap.go:7)    MOVQ    AX, DX
    0x0028 00040 (byteswap.go:7)    ANDQ    $16711680, DX
    0x002f 00047 (byteswap.go:7)    SHLQ    $24, DX
    0x0033 00051 (byteswap.go:7)    ORQ DX, CX
    0x0036 00054 (byteswap.go:8)    MOVQ    $4278190080, DX
    0x003b 00059 (byteswap.go:8)    ANDQ    AX, DX
    0x003e 00062 (byteswap.go:8)    SHLQ    $8, DX
    0x0042 00066 (byteswap.go:8)    ORQ DX, CX
    0x0045 00069 (byteswap.go:9)    MOVQ    $1095216660480, DX
    0x004f 00079 (byteswap.go:9)    ANDQ    AX, DX
    0x0052 00082 (byteswap.go:9)    SHRQ    $8, DX
    0x0056 00086 (byteswap.go:9)    ORQ DX, CX
    0x0059 00089 (byteswap.go:10)   MOVQ    $280375465082880, DX
    0x0063 00099 (byteswap.go:10)   ANDQ    AX, DX
    0x0066 00102 (byteswap.go:10)   SHRQ    $24, DX
    0x006a 00106 (byteswap.go:10)   ORQ DX, CX
    0x006d 00109 (byteswap.go:11)   MOVQ    $71776119061217280, DX
    0x0077 00119 (byteswap.go:11)   ANDQ    AX, DX
    0x007a 00122 (byteswap.go:11)   SHRQ    $40, DX
    0x007e 00126 (byteswap.go:11)   ORQ DX, CX
    0x0081 00129 (byteswap.go:12)   SHRQ    $56, AX
#   0x0085 00133 (byteswap.go:12)   SHLQ    $56, AX
#   0x0089 00137 (byteswap.go:12)   SHRQ    $56, AX
    0x008d 00141 (byteswap.go:12)   ORQ CX, AX
    0x0090 00144 (byteswap.go:12)   MOVQ    AX, "".~r1+16(FP)
    0x0095 00149 (byteswap.go:12)   RET

@tzneal
Copy link
Member

tzneal commented Feb 18, 2016

I'll look at this one, I introduced the AND to double shift :-)

https://go-review.googlesource.com/19485/

@gopherbot
Copy link

CL https://golang.org/cl/19690 mentions this issue.

@dsnet dsnet changed the title cmd/compile: optimize away useless expressions cmd/compile: optimize away useless A Feb 19, 2016
@dsnet dsnet changed the title cmd/compile: optimize away useless A cmd/compile: optimize away useless AND and ORs Feb 19, 2016
@dsnet dsnet changed the title cmd/compile: optimize away useless AND and ORs cmd/compile: optimize away useless ANDs and ORs Feb 19, 2016
@bradfitz bradfitz added this to the Go1.7 milestone Apr 10, 2016
@randall77
Copy link
Contributor

The final AND 0xff00000000000000 is gone.
There is a MOVBQZX at the start that seems unnecessary, but it would need to be replaced with a MOVQ anyway, so it doesn't look like a big loss. I'm going to mark this as closed.

@golang golang locked and limited conversation to collaborators Apr 10, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants