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