Commit 310d09bf authored by Michael Matloob's avatar Michael Matloob Committed by Alan Donovan

cmd/internal/ssa: utility functions to make Funcs

Adds a more convenient way to define Funcs for testing.
For instance,

  b1:
    v1 = Arg <mem> [.mem]
    Plain -> b2
  b2:
    Exit v1
  b3:
    v2 = Const <bool> [true]
    If v2 -> b3 b2

can be defined as

	 fun :=Fun("entry",
		Bloc("entry",
			Valu("mem", OpArg, TypeMem, ".mem"),
			Goto("exit")),
		Bloc("exit",
			Exit("mem")),
		Bloc("deadblock",
			Valu("deadval", OpConst, TypeBool, true),
			If("deadval", "deadblock", "exit")))

Also add an Equiv function to test two Funcs for equivalence.

Change-Id: If1633865aeefb8e765e772b6dad19250d93a413a
Reviewed-on: https://go-review.googlesource.com/9992Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent b3137966
...@@ -2,44 +2,35 @@ ...@@ -2,44 +2,35 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// TODO: these tests are pretty verbose. Is there a way to simplify package ssa
// building a small Func for testing?
package ssa_test
import ( import (
. "cmd/internal/ssa"
"testing" "testing"
) )
func TestDeadLoop(t *testing.T) { func TestDeadLoop(t *testing.T) {
f := new(Func) fun := Fun("entry",
entry := f.NewBlock(BlockPlain) Bloc("entry",
exit := f.NewBlock(BlockExit) Valu("mem", OpArg, TypeMem, ".mem"),
f.Entry = entry Goto("exit")),
addEdge(entry, exit) Bloc("exit",
mem := entry.NewValue(OpArg, TypeMem, ".mem") Exit("mem")),
exit.Control = mem
// dead loop // dead loop
deadblock := f.NewBlock(BlockIf) Bloc("deadblock",
addEdge(deadblock, deadblock)
addEdge(deadblock, exit)
// dead value in dead block // dead value in dead block
deadval := deadblock.NewValue(OpConst, TypeBool, true) Valu("deadval", OpConst, TypeBool, true),
deadblock.Control = deadval If("deadval", "deadblock", "exit")))
CheckFunc(f) CheckFunc(fun.f)
Deadcode(f) Deadcode(fun.f)
CheckFunc(f) CheckFunc(fun.f)
for _, b := range f.Blocks { for _, b := range fun.f.Blocks {
if b == deadblock { if b == fun.blocks["deadblock"] {
t.Errorf("dead block not removed") t.Errorf("dead block not removed")
} }
for _, v := range b.Values { for _, v := range b.Values {
if v == deadval { if v == fun.values["deadval"] {
t.Errorf("control value of dead block not removed") t.Errorf("control value of dead block not removed")
} }
} }
...@@ -47,23 +38,21 @@ func TestDeadLoop(t *testing.T) { ...@@ -47,23 +38,21 @@ func TestDeadLoop(t *testing.T) {
} }
func TestDeadValue(t *testing.T) { func TestDeadValue(t *testing.T) {
f := new(Func) fun := Fun("entry",
entry := f.NewBlock(BlockPlain) Bloc("entry",
exit := f.NewBlock(BlockExit) Valu("mem", OpArg, TypeMem, ".mem"),
f.Entry = entry Valu("deadval", OpConst, TypeInt64, int64(37)),
addEdge(entry, exit) Goto("exit")),
mem := entry.NewValue(OpArg, TypeMem, ".mem") Bloc("exit",
exit.Control = mem Exit("mem")))
deadval := entry.NewValue(OpConst, TypeInt64, int64(37)) CheckFunc(fun.f)
Deadcode(fun.f)
CheckFunc(f) CheckFunc(fun.f)
Deadcode(f)
CheckFunc(f) for _, b := range fun.f.Blocks {
for _, b := range f.Blocks {
for _, v := range b.Values { for _, v := range b.Values {
if v == deadval { if v == fun.values["deadval"] {
t.Errorf("dead value not removed") t.Errorf("dead value not removed")
} }
} }
...@@ -71,42 +60,34 @@ func TestDeadValue(t *testing.T) { ...@@ -71,42 +60,34 @@ func TestDeadValue(t *testing.T) {
} }
func TestNeverTaken(t *testing.T) { func TestNeverTaken(t *testing.T) {
f := new(Func) fun := Fun("entry",
entry := f.NewBlock(BlockIf) Bloc("entry",
exit := f.NewBlock(BlockExit) Valu("cond", OpConst, TypeBool, false),
then := f.NewBlock(BlockPlain) Valu("mem", OpArg, TypeMem, ".mem"),
else_ := f.NewBlock(BlockPlain) If("cond", "then", "else")),
f.Entry = entry Bloc("then",
addEdge(entry, then) Goto("exit")),
addEdge(entry, else_) Bloc("else",
addEdge(then, exit) Goto("exit")),
addEdge(else_, exit) Bloc("exit",
mem := entry.NewValue(OpArg, TypeMem, ".mem") Exit("mem")))
exit.Control = mem
CheckFunc(fun.f)
cond := entry.NewValue(OpConst, TypeBool, false) Deadcode(fun.f)
entry.Control = cond CheckFunc(fun.f)
CheckFunc(f) if fun.blocks["entry"].Kind != BlockPlain {
Deadcode(f)
CheckFunc(f)
if entry.Kind != BlockPlain {
t.Errorf("if(false) not simplified") t.Errorf("if(false) not simplified")
} }
for _, b := range f.Blocks { for _, b := range fun.f.Blocks {
if b == then { if b == fun.blocks["then"] {
t.Errorf("then block still present") t.Errorf("then block still present")
} }
for _, v := range b.Values { for _, v := range b.Values {
if v == cond { if v == fun.values["cond"] {
t.Errorf("constant condition still present") t.Errorf("constant condition still present")
} }
} }
} }
}
func addEdge(b, c *Block) {
b.Succs = append(b.Succs, c)
c.Preds = append(c.Preds, b)
} }
This diff is collapsed.
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