-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dev.ssa] cmd/compile/internal/ssa: transform degenerate control blocks
* In cases where we end up with empty branches like in if a then jmp b else jmp b; the flow can be replaced by a; jmp b. The following functions is optimized as follows: func f(a bool, x int) int { v := 0 if a { v = -1 } else { v = -1 } return x | v } Before this change: 02819 (arith_ssa.go:362) VARDEF "".~r2+16(FP) 02820 (arith_ssa.go:362) MOVQ $0, "".~r2+16(FP) 02821 (arith_ssa.go:362) MOVB "".a(FP), AX 02822 (arith_ssa.go:362) TESTB AX, AX 02823 (arith_ssa.go:364) JEQ 2824 02824 (arith_ssa.go:369) VARDEF "".~r2+16(FP) 02825 (arith_ssa.go:369) MOVQ $-1, "".~r2+16(FP) 02826 (arith_ssa.go:369) RET After this change: 02819 (arith_ssa.go:362) VARDEF "".~r2+16(FP) 02820 (arith_ssa.go:369) VARDEF "".~r2+16(FP) 02821 (arith_ssa.go:369) MOVQ $-1, "".~r2+16(FP) 02822 (arith_ssa.go:369) RET Updates #14277 Change-Id: Ibe7d284f43406c704903632a4fcf2a4a64059686 Reviewed-on: https://go-review.googlesource.com/19464 Reviewed-by: Keith Randall <khr@golang.org> Run-TryBot: Alexandru Moșoi <alexandru@mosoi.ro> TryBot-Result: Gobot Gobot <gobot@golang.org>
- Loading branch information
Showing
3 changed files
with
218 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package ssa | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFuseEliminatesOneBranch(t *testing.T) { | ||
ptrType := &TypeImpl{Size_: 8, Ptr: true, Name: "testptr"} // dummy for testing | ||
c := NewConfig("amd64", DummyFrontend{t}, nil, true) | ||
fun := Fun(c, "entry", | ||
Bloc("entry", | ||
Valu("mem", OpInitMem, TypeMem, 0, nil), | ||
Valu("sb", OpSB, TypeInvalid, 0, nil), | ||
Goto("checkPtr")), | ||
Bloc("checkPtr", | ||
Valu("ptr1", OpLoad, ptrType, 0, nil, "sb", "mem"), | ||
Valu("nilptr", OpConstNil, ptrType, 0, nil, "sb"), | ||
Valu("bool1", OpNeqPtr, TypeBool, 0, nil, "ptr1", "nilptr"), | ||
If("bool1", "then", "exit")), | ||
Bloc("then", | ||
Goto("exit")), | ||
Bloc("exit", | ||
Exit("mem"))) | ||
|
||
CheckFunc(fun.f) | ||
fuse(fun.f) | ||
|
||
for _, b := range fun.f.Blocks { | ||
if b == fun.blocks["then"] && b.Kind != BlockInvalid { | ||
t.Errorf("then was not eliminated, but should have") | ||
} | ||
} | ||
} | ||
|
||
func TestFuseEliminatesBothBranches(t *testing.T) { | ||
ptrType := &TypeImpl{Size_: 8, Ptr: true, Name: "testptr"} // dummy for testing | ||
c := NewConfig("amd64", DummyFrontend{t}, nil, true) | ||
fun := Fun(c, "entry", | ||
Bloc("entry", | ||
Valu("mem", OpInitMem, TypeMem, 0, nil), | ||
Valu("sb", OpSB, TypeInvalid, 0, nil), | ||
Goto("checkPtr")), | ||
Bloc("checkPtr", | ||
Valu("ptr1", OpLoad, ptrType, 0, nil, "sb", "mem"), | ||
Valu("nilptr", OpConstNil, ptrType, 0, nil, "sb"), | ||
Valu("bool1", OpNeqPtr, TypeBool, 0, nil, "ptr1", "nilptr"), | ||
If("bool1", "then", "else")), | ||
Bloc("then", | ||
Goto("exit")), | ||
Bloc("else", | ||
Goto("exit")), | ||
Bloc("exit", | ||
Exit("mem"))) | ||
|
||
CheckFunc(fun.f) | ||
fuse(fun.f) | ||
|
||
for _, b := range fun.f.Blocks { | ||
if b == fun.blocks["then"] && b.Kind != BlockInvalid { | ||
t.Errorf("then was not eliminated, but should have") | ||
} | ||
if b == fun.blocks["else"] && b.Kind != BlockInvalid { | ||
t.Errorf("then was not eliminated, but should have") | ||
} | ||
} | ||
} | ||
|
||
func TestFuseEliminatesEmptyBlocks(t *testing.T) { | ||
c := NewConfig("amd64", DummyFrontend{t}, nil, true) | ||
fun := Fun(c, "entry", | ||
Bloc("entry", | ||
Valu("mem", OpInitMem, TypeMem, 0, nil), | ||
Valu("sb", OpSB, TypeInvalid, 0, nil), | ||
Goto("z0")), | ||
Bloc("z1", | ||
Goto("z2")), | ||
Bloc("z3", | ||
Goto("exit")), | ||
Bloc("z2", | ||
Goto("z3")), | ||
Bloc("z0", | ||
Goto("z1")), | ||
Bloc("exit", | ||
Exit("mem"), | ||
)) | ||
|
||
CheckFunc(fun.f) | ||
fuse(fun.f) | ||
|
||
for k, b := range fun.blocks { | ||
if k[:1] == "z" && b.Kind != BlockInvalid { | ||
t.Errorf("%s was not eliminated, but should have", k) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters