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.
|
|
|
|
|
|
|
|
|
|
package ssa
|
|
|
|
|
|
2015-06-06 16:03:33 -07:00
|
|
|
import "fmt"
|
2015-03-03 13:38:14 -08:00
|
|
|
|
|
|
|
|
// Block represents a basic block in the control flow graph of a function.
|
|
|
|
|
type Block struct {
|
|
|
|
|
// A unique identifier for the block. The system will attempt to allocate
|
|
|
|
|
// these IDs densely, but no guarantees.
|
|
|
|
|
ID ID
|
|
|
|
|
|
|
|
|
|
// The kind of block this is.
|
|
|
|
|
Kind BlockKind
|
|
|
|
|
|
|
|
|
|
// Subsequent blocks, if any. The number and order depend on the block kind.
|
2015-05-05 16:19:12 -07:00
|
|
|
// All successors must be distinct (to make phi values in successors unambiguous).
|
2015-03-03 13:38:14 -08:00
|
|
|
Succs []*Block
|
|
|
|
|
|
|
|
|
|
// Inverse of successors.
|
|
|
|
|
// The order is significant to Phi nodes in the block.
|
|
|
|
|
Preds []*Block
|
|
|
|
|
// TODO: predecessors is a pain to maintain. Can we somehow order phi
|
|
|
|
|
// arguments by block id and have this field computed explicitly when needed?
|
|
|
|
|
|
|
|
|
|
// A value that determines how the block is exited. Its value depends on the kind
|
|
|
|
|
// of the block. For instance, a BlockIf has a boolean control value and BlockExit
|
|
|
|
|
// has a memory control value.
|
|
|
|
|
Control *Value
|
|
|
|
|
|
2015-09-08 21:28:44 -07:00
|
|
|
// Auxiliary info for the block. Its value depends on the Kind.
|
|
|
|
|
Aux interface{}
|
|
|
|
|
|
2015-05-05 16:19:12 -07:00
|
|
|
// The unordered set of Values that define the operation of this block.
|
2015-03-03 13:38:14 -08:00
|
|
|
// The list must include the control value, if any. (TODO: need this last condition?)
|
2015-05-05 16:19:12 -07:00
|
|
|
// After the scheduling pass, this list is ordered.
|
2015-03-03 13:38:14 -08:00
|
|
|
Values []*Value
|
|
|
|
|
|
|
|
|
|
// The containing function
|
|
|
|
|
Func *Func
|
2015-05-30 01:03:06 -04:00
|
|
|
|
|
|
|
|
// Line number for block's control operation
|
|
|
|
|
Line int32
|
2015-08-11 17:28:56 -07:00
|
|
|
|
|
|
|
|
// Likely direction for branches.
|
|
|
|
|
// If BranchLikely, Succs[0] is the most likely branch taken.
|
|
|
|
|
// If BranchUnlikely, Succs[1] is the most likely branch taken.
|
|
|
|
|
// Ignored if len(Succs) < 2.
|
|
|
|
|
// Fatal if not BranchUnknown and len(Succs) > 2.
|
|
|
|
|
Likely BranchPrediction
|
2015-03-03 13:38:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// kind control successors
|
|
|
|
|
// ------------------------------------------
|
|
|
|
|
// Exit return mem []
|
|
|
|
|
// Plain nil [next]
|
|
|
|
|
// If a boolean Value [then, else]
|
|
|
|
|
// Call mem [nopanic, panic] (control opcode should be OpCall or OpStaticCall)
|
2015-05-28 16:45:33 -07:00
|
|
|
type BlockKind int32
|
2015-03-03 13:38:14 -08:00
|
|
|
|
|
|
|
|
// short form print
|
|
|
|
|
func (b *Block) String() string {
|
|
|
|
|
return fmt.Sprintf("b%d", b.ID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// long form print
|
|
|
|
|
func (b *Block) LongString() string {
|
2015-06-06 16:03:33 -07:00
|
|
|
s := b.Kind.String()
|
2015-09-08 21:28:44 -07:00
|
|
|
if b.Aux != nil {
|
|
|
|
|
s += fmt.Sprintf(" %s", b.Aux)
|
|
|
|
|
}
|
2015-03-03 13:38:14 -08:00
|
|
|
if b.Control != nil {
|
|
|
|
|
s += fmt.Sprintf(" %s", b.Control)
|
|
|
|
|
}
|
|
|
|
|
if len(b.Succs) > 0 {
|
|
|
|
|
s += " ->"
|
|
|
|
|
for _, c := range b.Succs {
|
|
|
|
|
s += " " + c.String()
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-11 17:28:56 -07:00
|
|
|
switch b.Likely {
|
|
|
|
|
case BranchUnlikely:
|
|
|
|
|
s += " (unlikely)"
|
|
|
|
|
case BranchLikely:
|
|
|
|
|
s += " (likely)"
|
|
|
|
|
}
|
2015-03-03 13:38:14 -08:00
|
|
|
return s
|
|
|
|
|
}
|
2015-06-12 11:01:13 -07:00
|
|
|
|
2015-08-28 21:36:29 -05:00
|
|
|
// AddEdgeTo adds an edge from block b to block c. Used during building of the
|
|
|
|
|
// SSA graph; do not use on an already-completed SSA graph.
|
|
|
|
|
func (b *Block) AddEdgeTo(c *Block) {
|
|
|
|
|
b.Succs = append(b.Succs, c)
|
|
|
|
|
c.Preds = append(c.Preds, b)
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-24 14:03:39 -07:00
|
|
|
func (b *Block) Logf(msg string, args ...interface{}) { b.Func.Logf(msg, args...) }
|
|
|
|
|
func (b *Block) Fatalf(msg string, args ...interface{}) { b.Func.Fatalf(msg, args...) }
|
|
|
|
|
func (b *Block) Unimplementedf(msg string, args ...interface{}) { b.Func.Unimplementedf(msg, args...) }
|
2015-08-11 17:28:56 -07:00
|
|
|
|
|
|
|
|
type BranchPrediction int8
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
BranchUnlikely = BranchPrediction(-1)
|
|
|
|
|
BranchUnknown = BranchPrediction(0)
|
|
|
|
|
BranchLikely = BranchPrediction(+1)
|
|
|
|
|
)
|