Skip to content

Commit

Permalink
cmd/compile: optimize ssa if blocks for wasm architecture
Browse files Browse the repository at this point in the history
Check for the next block and accordingly place the successor blocks.
This saves an additional jump instruction if the next block is any one
of the successor blocks.

While at it, inline the logic of goToBlock.

Reduces the size of pkg/js_wasm by 264 bytes.

Change-Id: I671ac4322e6edcb0d7e590dcca27e074268068d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/195204
Run-TryBot: Agniva De Sarker <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Richard Musiol <[email protected]>
  • Loading branch information
agnivade committed Sep 21, 2019
1 parent f1b6d10 commit 9c384cc
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/cmd/compile/internal/wasm/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,35 @@ func ssaMarkMoves(s *gc.SSAGenState, b *ssa.Block) {
}

func ssaGenBlock(s *gc.SSAGenState, b, next *ssa.Block) {
goToBlock := func(block *ssa.Block, canFallthrough bool) {
if canFallthrough && block == next {
return
}
s.Br(obj.AJMP, block)
}

switch b.Kind {
case ssa.BlockPlain:
goToBlock(b.Succs[0].Block(), true)
if next != b.Succs[0].Block() {
s.Br(obj.AJMP, b.Succs[0].Block())
}

case ssa.BlockIf:
getValue32(s, b.Control)
s.Prog(wasm.AI32Eqz)
s.Prog(wasm.AIf)
goToBlock(b.Succs[1].Block(), false)
s.Prog(wasm.AEnd)
goToBlock(b.Succs[0].Block(), true)
switch next {
case b.Succs[0].Block():
// if false, jump to b.Succs[1]
getValue32(s, b.Control)
s.Prog(wasm.AI32Eqz)
s.Prog(wasm.AIf)
s.Br(obj.AJMP, b.Succs[1].Block())
s.Prog(wasm.AEnd)
case b.Succs[1].Block():
// if true, jump to b.Succs[0]
getValue32(s, b.Control)
s.Prog(wasm.AIf)
s.Br(obj.AJMP, b.Succs[0].Block())
s.Prog(wasm.AEnd)
default:
// if true, jump to b.Succs[0], else jump to b.Succs[1]
getValue32(s, b.Control)
s.Prog(wasm.AIf)
s.Br(obj.AJMP, b.Succs[0].Block())
s.Prog(wasm.AEnd)
s.Br(obj.AJMP, b.Succs[1].Block())
}

case ssa.BlockRet:
s.Prog(obj.ARET)
Expand All @@ -104,9 +115,11 @@ func ssaGenBlock(s *gc.SSAGenState, b, next *ssa.Block) {
s.Prog(wasm.AI64Eqz)
s.Prog(wasm.AI32Eqz)
s.Prog(wasm.AIf)
goToBlock(b.Succs[1].Block(), false)
s.Br(obj.AJMP, b.Succs[1].Block())
s.Prog(wasm.AEnd)
goToBlock(b.Succs[0].Block(), true)
if next != b.Succs[0].Block() {
s.Br(obj.AJMP, b.Succs[0].Block())
}

default:
panic("unexpected block")
Expand Down

0 comments on commit 9c384cc

Please sign in to comment.