aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/ssa/print.go
blob: aea9ce9e33acf40ac463a3ef89a8dfbe30a0514a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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

import (
	"bytes"
	"fmt"
	"io"

	"cmd/internal/notsha256"
	"cmd/internal/src"
)

func printFunc(f *Func) {
	f.Logf("%s", f)
}

func hashFunc(f *Func) []byte {
	h := notsha256.New()
	p := stringFuncPrinter{w: h, printDead: true}
	fprintFunc(p, f)
	return h.Sum(nil)
}

func (f *Func) String() string {
	var buf bytes.Buffer
	p := stringFuncPrinter{w: &buf, printDead: true}
	fprintFunc(p, f)
	return buf.String()
}

// rewriteHash returns a hash of f suitable for detecting rewrite cycles.
func (f *Func) rewriteHash() string {
	h := notsha256.New()
	p := stringFuncPrinter{w: h, printDead: false}
	fprintFunc(p, f)
	return fmt.Sprintf("%x", h.Sum(nil))
}

type funcPrinter interface {
	header(f *Func)
	startBlock(b *Block, reachable bool)
	endBlock(b *Block, reachable bool)
	value(v *Value, live bool)
	startDepCycle()
	endDepCycle()
	named(n LocalSlot, vals []*Value)
}

type stringFuncPrinter struct {
	w         io.Writer
	printDead bool
}

func (p stringFuncPrinter) header(f *Func) {
	fmt.Fprint(p.w, f.Name)
	fmt.Fprint(p.w, " ")
	fmt.Fprintln(p.w, f.Type)
}

func (p stringFuncPrinter) startBlock(b *Block, reachable bool) {
	if !p.printDead && !reachable {
		return
	}
	fmt.Fprintf(p.w, "  b%d:", b.ID)
	if len(b.Preds) > 0 {
		io.WriteString(p.w, " <-")
		for _, e := range b.Preds {
			pred := e.b
			fmt.Fprintf(p.w, " b%d", pred.ID)
		}
	}
	if !reachable {
		fmt.Fprint(p.w, " DEAD")
	}
	io.WriteString(p.w, "\n")
}

func (p stringFuncPrinter) endBlock(b *Block, reachable bool) {
	if !p.printDead && !reachable {
		return
	}
	fmt.Fprintln(p.w, "    "+b.LongString())
}

func StmtString(p src.XPos) string {
	linenumber := "(?) "
	if p.IsKnown() {
		pfx := ""
		if p.IsStmt() == src.PosIsStmt {
			pfx = "+"
		}
		if p.IsStmt() == src.PosNotStmt {
			pfx = "-"
		}
		linenumber = fmt.Sprintf("(%s%d) ", pfx, p.Line())
	}
	return linenumber
}

func (p stringFuncPrinter) value(v *Value, live bool) {
	if !p.printDead && !live {
		return
	}
	fmt.Fprintf(p.w, "    %s", StmtString(v.Pos))
	fmt.Fprint(p.w, v.LongString())
	if !live {
		fmt.Fprint(p.w, " DEAD")
	}
	fmt.Fprintln(p.w)
}

func (p stringFuncPrinter) startDepCycle() {
	fmt.Fprintln(p.w, "dependency cycle!")
}

func (p stringFuncPrinter) endDepCycle() {}

func (p stringFuncPrinter) named(n LocalSlot, vals []*Value) {
	fmt.Fprintf(p.w, "name %s: %v\n", n, vals)
}

func fprintFunc(p funcPrinter, f *Func) {
	reachable, live := findlive(f)
	defer f.retDeadcodeLive(live)
	p.header(f)
	printed := make([]bool, f.NumValues())
	for _, b := range f.Blocks {
		p.startBlock(b, reachable[b.ID])

		if f.scheduled {
			// Order of Values has been decided - print in that order.
			for _, v := range b.Values {
				p.value(v, live[v.ID])
				printed[v.ID] = true
			}
			p.endBlock(b, reachable[b.ID])
			continue
		}

		// print phis first since all value cycles contain a phi
		n := 0
		for _, v := range b.Values {
			if v.Op != OpPhi {
				continue
			}
			p.value(v, live[v.ID])
			printed[v.ID] = true
			n++
		}

		// print rest of values in dependency order
		for n < len(b.Values) {
			m := n
		outer:
			for _, v := range b.Values {
				if printed[v.ID] {
					continue
				}
				for _, w := range v.Args {
					// w == nil shouldn't happen, but if it does,
					// don't panic; we'll get a better diagnosis later.
					if w != nil && w.Block == b && !printed[w.ID] {
						continue outer
					}
				}
				p.value(v, live[v.ID])
				printed[v.ID] = true
				n++
			}
			if m == n {
				p.startDepCycle()
				for _, v := range b.Values {
					if printed[v.ID] {
						continue
					}
					p.value(v, live[v.ID])
					printed[v.ID] = true
					n++
				}
				p.endDepCycle()
			}
		}

		p.endBlock(b, reachable[b.ID])
	}
	for _, name := range f.Names {
		p.named(*name, f.NamedValues[*name])
	}
}