github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/compile/internal/ssa/trim.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa
     6  
     7  // trim removes blocks with no code in them.
     8  // These blocks were inserted to remove critical edges.
     9  func trim(f *Func) {
    10  	i := 0
    11  	for _, b := range f.Blocks {
    12  		if b.Kind != BlockPlain || len(b.Values) != 0 || len(b.Preds) != 1 {
    13  			f.Blocks[i] = b
    14  			i++
    15  			continue
    16  		}
    17  		// TODO: handle len(b.Preds)>1 case.
    18  
    19  		// Splice b out of the graph.
    20  		pred := b.Preds[0]
    21  		succ := b.Succs[0]
    22  		for j, s := range pred.Succs {
    23  			if s == b {
    24  				pred.Succs[j] = succ
    25  			}
    26  		}
    27  		for j, p := range succ.Preds {
    28  			if p == b {
    29  				succ.Preds[j] = pred
    30  			}
    31  		}
    32  	}
    33  	for j := i; j < len(f.Blocks); j++ {
    34  		f.Blocks[j] = nil
    35  	}
    36  	f.Blocks = f.Blocks[:i]
    37  }