Commit c1544ff9 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/compile: move phi tighten after critical

The phi tighten pass moves rematerializable phi args
to the immediate predecessor of the phis.
This reduces value lifetimes for regalloc.

However, the critical edge removal pass can introduce
new blocks, which can change what a block's
immediate precedessor is. This can result in tightened
phi args being spilled unnecessarily.

This change moves the phi tighten pass after the
critical edge pass, when the block structure is stable.

This improves the code generated for

func f(s string) bool { return s == "abcde" }

Before this change:

"".f STEXT nosplit size=44 args=0x18 locals=0x0
	0x0000 00000 (x.go:3)	MOVQ	"".s+16(SP), AX
	0x0005 00005 (x.go:3)	CMPQ	AX, $5
	0x0009 00009 (x.go:3)	JNE	40
	0x000b 00011 (x.go:3)	MOVQ	"".s+8(SP), AX
	0x0010 00016 (x.go:3)	CMPL	(AX), $1684234849
	0x0016 00022 (x.go:3)	JNE	36
	0x0018 00024 (x.go:3)	CMPB	4(AX), $101
	0x001c 00028 (x.go:3)	SETEQ	AL
	0x001f 00031 (x.go:3)	MOVB	AL, "".~r1+24(SP)
	0x0023 00035 (x.go:3)	RET
	0x0024 00036 (x.go:3)	XORL	AX, AX
	0x0026 00038 (x.go:3)	JMP	31
	0x0028 00040 (x.go:3)	XORL	AX, AX
	0x002a 00042 (x.go:3)	JMP	31

Observe the duplicated blocks at the end.
After this change:

"".f STEXT nosplit size=40 args=0x18 locals=0x0
	0x0000 00000 (x.go:3)	MOVQ	"".s+16(SP), AX
	0x0005 00005 (x.go:3)	CMPQ	AX, $5
	0x0009 00009 (x.go:3)	JNE	36
	0x000b 00011 (x.go:3)	MOVQ	"".s+8(SP), AX
	0x0010 00016 (x.go:3)	CMPL	(AX), $1684234849
	0x0016 00022 (x.go:3)	JNE	36
	0x0018 00024 (x.go:3)	CMPB	4(AX), $101
	0x001c 00028 (x.go:3)	SETEQ	AL
	0x001f 00031 (x.go:3)	MOVB	AL, "".~r1+24(SP)
	0x0023 00035 (x.go:3)	RET
	0x0024 00036 (x.go:3)	XORL	AX, AX
	0x0026 00038 (x.go:3)	JMP	31

Change-Id: I12c81aa53b89456cb5809aa5396378245f3beda9
Reviewed-on: https://go-review.googlesource.com/c/go/+/172597
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent d68ac591
...@@ -414,9 +414,9 @@ var passes = [...]pass{ ...@@ -414,9 +414,9 @@ var passes = [...]pass{
{name: "late phielim", fn: phielim}, {name: "late phielim", fn: phielim},
{name: "late copyelim", fn: copyelim}, {name: "late copyelim", fn: copyelim},
{name: "tighten", fn: tighten}, // move values closer to their uses {name: "tighten", fn: tighten}, // move values closer to their uses
{name: "phi tighten", fn: phiTighten},
{name: "late deadcode", fn: deadcode}, {name: "late deadcode", fn: deadcode},
{name: "critical", fn: critical, required: true}, // remove critical edges {name: "critical", fn: critical, required: true}, // remove critical edges
{name: "phi tighten", fn: phiTighten}, // place rematerializable phi args near uses to reduce value lifetimes
{name: "likelyadjust", fn: likelyadjust}, {name: "likelyadjust", fn: likelyadjust},
{name: "layout", fn: layout, required: true}, // schedule blocks {name: "layout", fn: layout, required: true}, // schedule blocks
{name: "schedule", fn: schedule, required: true}, // schedule values {name: "schedule", fn: schedule, required: true}, // schedule values
...@@ -467,6 +467,8 @@ var passOrder = [...]constraint{ ...@@ -467,6 +467,8 @@ var passOrder = [...]constraint{
{"decompose builtin", "late opt"}, {"decompose builtin", "late opt"},
// decompose builtin is the last pass that may introduce new float ops, so run softfloat after it // decompose builtin is the last pass that may introduce new float ops, so run softfloat after it
{"decompose builtin", "softfloat"}, {"decompose builtin", "softfloat"},
// remove critical edges before phi tighten, so that phi args get better placement
{"critical", "phi tighten"},
// don't layout blocks until critical edges have been removed // don't layout blocks until critical edges have been removed
{"critical", "layout"}, {"critical", "layout"},
// regalloc requires the removal of all critical edges // regalloc requires the removal of all critical edges
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment