2015-03-03 13:38:14 -08:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
package ssa
|
2015-03-03 13:38:14 -08:00
|
|
|
|
2015-05-28 16:45:33 -07:00
|
|
|
import "testing"
|
2015-03-03 13:38:14 -08:00
|
|
|
|
|
|
|
|
func TestDeadLoop(t *testing.T) {
|
2015-06-12 11:01:13 -07:00
|
|
|
c := NewConfig("amd64", DummyFrontend{t})
|
2015-05-29 16:20:33 -07:00
|
|
|
fun := Fun(c, "entry",
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
Bloc("entry",
|
2015-06-11 21:29:25 -07:00
|
|
|
Valu("mem", OpArg, TypeMem, 0, ".mem"),
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
Goto("exit")),
|
|
|
|
|
Bloc("exit",
|
|
|
|
|
Exit("mem")),
|
|
|
|
|
// dead loop
|
|
|
|
|
Bloc("deadblock",
|
|
|
|
|
// dead value in dead block
|
2015-06-11 21:29:25 -07:00
|
|
|
Valu("deadval", OpConst, TypeBool, 0, true),
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
If("deadval", "deadblock", "exit")))
|
|
|
|
|
|
|
|
|
|
CheckFunc(fun.f)
|
|
|
|
|
Deadcode(fun.f)
|
|
|
|
|
CheckFunc(fun.f)
|
|
|
|
|
|
|
|
|
|
for _, b := range fun.f.Blocks {
|
|
|
|
|
if b == fun.blocks["deadblock"] {
|
2015-03-03 13:38:14 -08:00
|
|
|
t.Errorf("dead block not removed")
|
|
|
|
|
}
|
|
|
|
|
for _, v := range b.Values {
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
if v == fun.values["deadval"] {
|
2015-03-03 13:38:14 -08:00
|
|
|
t.Errorf("control value of dead block not removed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDeadValue(t *testing.T) {
|
2015-06-12 11:01:13 -07:00
|
|
|
c := NewConfig("amd64", DummyFrontend{t})
|
2015-05-29 16:20:33 -07:00
|
|
|
fun := Fun(c, "entry",
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
Bloc("entry",
|
2015-06-11 21:29:25 -07:00
|
|
|
Valu("mem", OpArg, TypeMem, 0, ".mem"),
|
|
|
|
|
Valu("deadval", OpConst, TypeInt64, 37, nil),
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
Goto("exit")),
|
|
|
|
|
Bloc("exit",
|
|
|
|
|
Exit("mem")))
|
|
|
|
|
|
|
|
|
|
CheckFunc(fun.f)
|
|
|
|
|
Deadcode(fun.f)
|
|
|
|
|
CheckFunc(fun.f)
|
|
|
|
|
|
|
|
|
|
for _, b := range fun.f.Blocks {
|
2015-03-03 13:38:14 -08:00
|
|
|
for _, v := range b.Values {
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
if v == fun.values["deadval"] {
|
2015-03-03 13:38:14 -08:00
|
|
|
t.Errorf("dead value not removed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNeverTaken(t *testing.T) {
|
2015-06-12 11:01:13 -07:00
|
|
|
c := NewConfig("amd64", DummyFrontend{t})
|
2015-05-29 16:20:33 -07:00
|
|
|
fun := Fun(c, "entry",
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
Bloc("entry",
|
2015-06-11 21:29:25 -07:00
|
|
|
Valu("cond", OpConst, TypeBool, 0, false),
|
|
|
|
|
Valu("mem", OpArg, TypeMem, 0, ".mem"),
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
If("cond", "then", "else")),
|
|
|
|
|
Bloc("then",
|
|
|
|
|
Goto("exit")),
|
|
|
|
|
Bloc("else",
|
|
|
|
|
Goto("exit")),
|
|
|
|
|
Bloc("exit",
|
|
|
|
|
Exit("mem")))
|
|
|
|
|
|
|
|
|
|
CheckFunc(fun.f)
|
2015-05-28 16:45:33 -07:00
|
|
|
Opt(fun.f)
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
Deadcode(fun.f)
|
|
|
|
|
CheckFunc(fun.f)
|
|
|
|
|
|
|
|
|
|
if fun.blocks["entry"].Kind != BlockPlain {
|
2015-03-03 13:38:14 -08:00
|
|
|
t.Errorf("if(false) not simplified")
|
|
|
|
|
}
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
for _, b := range fun.f.Blocks {
|
|
|
|
|
if b == fun.blocks["then"] {
|
2015-03-03 13:38:14 -08:00
|
|
|
t.Errorf("then block still present")
|
|
|
|
|
}
|
|
|
|
|
for _, v := range b.Values {
|
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/9992
Reviewed-by: Keith Randall <khr@golang.org>
2015-05-12 12:06:45 -04:00
|
|
|
if v == fun.values["cond"] {
|
2015-03-03 13:38:14 -08:00
|
|
|
t.Errorf("constant condition still present")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|