2016-01-21 13:27:01 -08:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
|
|
package ssa
|
|
|
|
|
|
|
|
|
|
// trim removes blocks with no code in them.
|
|
|
|
|
// These blocks were inserted to remove critical edges.
|
|
|
|
|
func trim(f *Func) {
|
2016-04-28 16:52:47 -07:00
|
|
|
n := 0
|
2016-01-21 13:27:01 -08:00
|
|
|
for _, b := range f.Blocks {
|
|
|
|
|
if b.Kind != BlockPlain || len(b.Values) != 0 || len(b.Preds) != 1 {
|
2016-04-28 16:52:47 -07:00
|
|
|
f.Blocks[n] = b
|
|
|
|
|
n++
|
2016-01-21 13:27:01 -08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// TODO: handle len(b.Preds)>1 case.
|
|
|
|
|
|
|
|
|
|
// Splice b out of the graph.
|
2016-04-28 16:52:47 -07:00
|
|
|
p := b.Preds[0].b
|
|
|
|
|
i := b.Preds[0].i
|
|
|
|
|
s := b.Succs[0].b
|
|
|
|
|
j := b.Succs[0].i
|
|
|
|
|
p.Succs[i] = Edge{s, j}
|
|
|
|
|
s.Preds[j] = Edge{p, i}
|
2016-09-07 14:04:31 -07:00
|
|
|
f.invalidateCFG()
|
2016-01-21 13:27:01 -08:00
|
|
|
}
|
2016-04-28 16:52:47 -07:00
|
|
|
tail := f.Blocks[n:]
|
|
|
|
|
for i := range tail {
|
|
|
|
|
tail[i] = nil
|
2016-01-21 13:27:01 -08:00
|
|
|
}
|
2016-04-28 16:52:47 -07:00
|
|
|
f.Blocks = f.Blocks[:n]
|
2016-01-21 13:27:01 -08:00
|
|
|
}
|