Ask questionscmd/compile: branch elimination opportunites when comparing constants

<!-- Please answer these questions before submitting your issue. Thanks! For questions please use one of our forums: -->

What version of Go are you using (go version)?

<pre> latest


Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?

<details><summary><code>go env</code> Output</summary><br><pre> $ go env linux/ppc64le but I see it in the SSA on x86 </pre></details>

What did you do?

<!-- If possible, provide a recipe for reproducing the error. A complete runnable program is good. A link on is best. --> Inspecting code for other reasons, found cases where there are constant compares and subsequent branches against those constants, resulting in several unnecessary branches.

What did you expect to see?

No unnecessary branches.

What did you see instead?

Unnecessary branching due to constant compares of known values.

This example comes from the ssa output of bytes.(Buffer).WriteByte. Here are some snippets:

v18 00011 (+107) MOVD 8(R6), R7 v38 00012 (107) MOVD 16(R6), R5 v26 00013 (107) SUB R7, R5, R8 v70 00014 (107) CMP R8, $1 // This could be BLT 36. Block starting at 46 sets R3 to 0 then branches to a CMP R3, $0. b1 00015 (107) BLT 46 <--- Change to BLT 36

v54 00019 (108) MOVD R4, 8(R6) // Once the block at 46 is eliminated as a predecessor of this block, then the next 3 instructions are not needed, since the CMPW has only a single predecessor and it is comparing 0 against 1 and the BEQ will never happen. v45 00020 (108) MOVD $1, R3 <---- Remove v52 00021 (+265) CMPW R3, $0 <---- Remove b9 00022 (+266) BEQ 36 <---- Remove

v24 00023 (+269) PCDATA $1, $1 v24 00024 (+269) MOVD 8(R6), R4 v34 00025 (+269) PCDATA $0, $2 v34 00026 (269) MOVD (R6), R5 v88 00027 (269) CMPU R4, R7

v63 00036 (267) PCDATA $1, $0 v63 00037 (+267) MOVD R6, 32(R1) v81 00038 (267) MOVD $1, R3 v65 00039 (267) MOVD R3, 40(R1) v66 00040 (+267) CALL "".(*Buffer).grow(SB) v68 00041 (267) MOVD 48(R1), R7 v36 00042 (269) PCDATA $0, $1 v36 00043 (269) PCDATA $1, $1 v36 00044 (269) MOVD "".b(R1), R6 b11 00045 (267) JMP 23

// The MOVD $0, R3 and JMP 21 could be replaced by JMP 36 since the successor is CMP $0, R3 followed by BEQ 36. Once it is decided that 36 is the successor, I think the MOVD $0, R7 could also go away since the block starting at 36 doesn't use it. That also means the predecessor of this block could branch directly to 36. v21 00046 (267) PCDATA $1, $0 <--- Remove v21 00047 (267) MOVD $0, R7 <--- Remove v25 00048 (267) MOVD $0, R3 <--- Remove b2 00049 (+265) JMP 21 <---- Remove

This scenario is common in stdlib functions. It looks like the same situation occurs in x86, so it is not specific to ppc64/ppc64le.

@josharian I think I reported this problem last fall and you commented on it, but I can't find the issue where that was done.


Answer questions erifan

Hi, I'm doing the third one.


Related questions

cmd/link: segmentation fault during mach-o linking hot 6
cmd/vet: potential false positive in the "suspect or" check hot 4
cmd/go: cannot find module providing package error stops `go get` processing hot 3
vendor/ undefined: errors.Frame ... hot 2
cmd/cgo error: runtime: unknown pc 0x7fff5c805b86 hot 2
runtime: crash with "invalid pc-encoded table" hot 2
internal/poll: transparently support new linux io_uring interface hot 2
crypto/x509: 'certificate signed by unknown authority' on mipsle hot 2
x/tools/cmd/gopls: "build cache is required, but could not be located: GOCACHE is not defined and $HOME is not defined" hot 2
Plis fixit! Its not good!!! hot 2
cmd/go: needs a better error than "missing dot in first path element" when GOROOT is set incorrectly hot 2
encoding/json: invalid use of ,string struct tag, trying to unmarshal "" into int hot 2
Golang show warning after updating to 1.13 on OSX Catalina 10.15 hot 2
x/mobile: gomobile bind is failing with latest version [+cafc553] of gomobile hot 2
cmd/go: "found, but does not contain package" error refers to replaced version instead of its replacement hot 2
Github User Rank List