Commit 5949524f authored by Alexandru Moșoi's avatar Alexandru Moșoi Committed by Alexandru Moșoi

[dev.ssa] cmd/compile/internal/ssa: handle phis in fuse.

Change-Id: Idd880cc6c1e5dc34dddbdea0841a7a718d2fa836
Reviewed-on: https://go-review.googlesource.com/19544Reviewed-by: default avatarDavid Chase <drchase@google.com>
parent e4bee4be
...@@ -17,15 +17,15 @@ func fuse(f *Func) { ...@@ -17,15 +17,15 @@ func fuse(f *Func) {
// fuseBlockIf handles the following cases where s0 and s1 are empty blocks. // fuseBlockIf handles the following cases where s0 and s1 are empty blocks.
// //
// b b b // b b b b
// / \ | \ / | // / \ | \ / | | |
// s0 s1 | s1 s0 | // s0 s1 | s1 s0 | | |
// \ / | / \ | // \ / | / \ | | |
// ss ss ss // ss ss ss ss
// //
// If ss doesn't contain any Phi ops and s0 & s1 are empty then the branch // If all Phi ops in ss have identical variables for slots corresponding to
// can be dropped. // s0, s1 and b then the branch can be dropped.
// TODO: If ss doesn't contain any Phi ops, are s0 and s1 dead code anyway? // TODO: If ss doesn't contain any OpPhis, are s0 and s1 dead code anyway.
func fuseBlockIf(b *Block) bool { func fuseBlockIf(b *Block) bool {
if b.Kind != BlockIf { if b.Kind != BlockIf {
return false return false
...@@ -34,13 +34,13 @@ func fuseBlockIf(b *Block) bool { ...@@ -34,13 +34,13 @@ func fuseBlockIf(b *Block) bool {
var ss0, ss1 *Block var ss0, ss1 *Block
s0 := b.Succs[0] s0 := b.Succs[0]
if s0.Kind != BlockPlain || len(s0.Preds) != 1 || len(s0.Values) != 0 { if s0.Kind != BlockPlain || len(s0.Preds) != 1 || len(s0.Values) != 0 {
s0, ss0 = nil, s0 s0, ss0 = b, s0
} else { } else {
ss0 = s0.Succs[0] ss0 = s0.Succs[0]
} }
s1 := b.Succs[1] s1 := b.Succs[1]
if s1.Kind != BlockPlain || len(s1.Preds) != 1 || len(s1.Values) != 0 { if s1.Kind != BlockPlain || len(s1.Preds) != 1 || len(s1.Values) != 0 {
s1, ss1 = nil, s1 s1, ss1 = b, s1
} else { } else {
ss1 = s1.Succs[0] ss1 = s1.Succs[0]
} }
...@@ -50,41 +50,63 @@ func fuseBlockIf(b *Block) bool { ...@@ -50,41 +50,63 @@ func fuseBlockIf(b *Block) bool {
} }
ss := ss0 ss := ss0
// TODO: Handle OpPhi operations. We can still replace OpPhi if the // s0 and s1 are equal with b if the corresponding block is missing
// slots corresponding to b, s0 and s1 point to the same variable. // (2nd, 3rd and 4th case in the figure).
i0, i1 := -1, -1
for i, p := range ss.Preds {
if p == s0 {
i0 = i
}
if p == s1 {
i1 = i
}
}
if i0 == -1 || i1 == -1 {
b.Fatalf("invalid predecessors")
}
for _, v := range ss.Values { for _, v := range ss.Values {
if v.Op == OpPhi { if v.Op == OpPhi && v.Args[i0] != v.Args[i1] {
return false return false
} }
} }
// Now we have two following b->ss, b->s0->ss and b->s1->ss, // Now we have two of following b->ss, b->s0->ss and b->s1->ss,
// with s0 and s1 empty if exist. // with s0 and s1 empty if exist.
// We can replace it with b->ss without if ss has no phis // We can replace it with b->ss without if all OpPhis in ss
// which is checked above. // have identical predecessors (verified above).
// No critical edge is introduced because b will have one successor. // No critical edge is introduced because b will have one successor.
if s0 != nil { if s0 != b && s1 != b {
ss.removePred(s0) ss.removePred(s0)
// Replace edge b->s1->ss with b->ss.
// We need to keep a slot for Phis corresponding to b.
for i := range b.Succs {
if b.Succs[i] == s1 {
b.Succs[i] = ss
}
}
for i := range ss.Preds {
if ss.Preds[i] == s1 {
ss.Preds[i] = b
} }
if s1 != nil {
ss.removePred(s1)
} }
if s0 != nil && s1 != nil { } else if s0 != b {
// Add an edge if both edges are removed, otherwise b is no longer connected to ss. ss.removePred(s0)
ss.Preds = append(ss.Preds, b) } else if s1 != b {
ss.removePred(s1)
} }
b.Kind = BlockPlain b.Kind = BlockPlain
b.Control = nil b.Control = nil
b.Succs = append(b.Succs[:0], ss) b.Succs = append(b.Succs[:0], ss)
// Trash the empty blocks s0 & s1. // Trash the empty blocks s0 & s1.
if s0 != nil { if s0 != b {
s0.Kind = BlockInvalid s0.Kind = BlockInvalid
s0.Values = nil s0.Values = nil
s0.Succs = nil s0.Succs = nil
s0.Preds = nil s0.Preds = nil
} }
if s1 != nil { if s1 != b {
s1.Kind = BlockInvalid s1.Kind = BlockInvalid
s1.Values = nil s1.Values = nil
s1.Succs = nil s1.Succs = nil
......
...@@ -65,6 +65,40 @@ func TestFuseEliminatesBothBranches(t *testing.T) { ...@@ -65,6 +65,40 @@ func TestFuseEliminatesBothBranches(t *testing.T) {
} }
} }
func TestFuseHandlesPhis(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",
Valu("phi", OpPhi, ptrType, 0, nil, "ptr1", "ptr1"),
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) { func TestFuseEliminatesEmptyBlocks(t *testing.T) {
c := NewConfig("amd64", DummyFrontend{t}, nil, true) c := NewConfig("amd64", DummyFrontend{t}, nil, true)
fun := Fun(c, "entry", fun := Fun(c, "entry",
......
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