Debian Codegraph @

go/ssa/lift.go

raw symbols SCIP
1 // Copyright 2013 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 ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/
6
7 // This file defines the lifting pass which tries to "lift" Alloc
8 // cells (new/local variables) into SSA registers, replacing loads
9 // with the dominating stored value, eliminating loads and stores, and
10 // inserting φ-nodes as needed.
11
12 // Cited papers and resources:
13 //
14 // Ron Cytron et al. 1991. Efficiently computing SSA form...
15 // http://doi.acm.org/10.1145/115372.115320
16 //
17 // Cooper, Harvey, Kennedy. 2001. A Simple, Fast Dominance Algorithm.
18 // Software Practice and Experience 2001, 4:1-10.
19 // http://www.hipersoft.rice.edu/grads/publications/dom14.pdf
20 //
21 // Daniel Berlin, llvmdev mailing list, 2012.
22 // http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-January/046638.html
23 // (Be sure to expand the whole thread.)
24
25 // TODO(adonovan): opt: there are many optimizations worth evaluating, and
26 // the conventional wisdom for SSA construction is that a simple
27 // algorithm well engineered often beats those of better asymptotic
28 // complexity on all but the most egregious inputs.
29 //
30 // Danny Berlin suggests that the Cooper et al. algorithm for
31 // computing the dominance frontier is superior to Cytron et al.
32 // Furthermore he recommends that rather than computing the DF for the
33 // whole function then renaming all alloc cells, it may be cheaper to
34 // compute the DF for each alloc cell separately and throw it away.
35 //
36 // Consider exploiting liveness information to avoid creating dead
37 // φ-nodes which we then immediately remove.
38 //
39 // Also see many other "TODO: opt" suggestions in the code.
40
41 import (
42 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
43 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
44 "math/bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/"
45 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
46 "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/"
47
48 "golang.org/x/tools/internal/typeparamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/"
49 )
50
51 // If true, show diagnostic information at each step of lifting.
52 // Very verbose.
53 const debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. = false
54
55 // domFrontier maps each block to the set of blocks in its dominance
56 // frontier. The outer slice is conceptually a map keyed by
57 // Block.Index. The inner slice is conceptually a set, possibly
58 // containing duplicates.
59 //
60 // TODO(adonovan): opt: measure impact of dups; consider a packed bit
61 // representation, e.g. big.Int, and bitwise parallel operations for
62 // the union step in the Children loop.
63 //
64 // domFrontier's methods mutate the slice's elements but not its
65 // length, so their receivers needn't be pointers.
66 type domFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier# [][]*BasicBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#
67
68 func (df domFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#) addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#add().(u, v *BasicBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#) {
69 p := &df[u.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Index.]
70 *p = append(*p, v)
71 }
72
73 // build builds the dominance frontier df for the dominator (sub)tree
74 // rooted at u, using the Cytron et al. algorithm.
75 //
76 // TODO(adonovan): opt: consider Berlin approach, computing pruned SSA
77 // by pruning the entire IDF computation, rather than merely pruning
78 // the DF -> IDF step.
79 func (df domFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#) buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#build().(u *BasicBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#) {
80 // Encounter each node u in postorder of dom tree.
81 for _, child := range u.domscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#dom..childrenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domInfo#children. {
82 df.buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#build().(child)
83 }
84 for _, vb := range u.Succsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Succs. {
85 if v := vb.domscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#dom.; v.idomscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domInfo#idom. != u {
86 df.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#add().(u, vb)
87 }
88 }
89 for _, w := range u.domscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#dom..childrenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domInfo#children. {
90 for _, vb := range df[w.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Index.] {
91 // TODO(adonovan): opt: use word-parallel bitwise union.
92 if v := vb.domscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#dom.; v.idomscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domInfo#idom. != u {
93 df.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#add().(u, vb)
94 }
95 }
96 }
97 }
98
99 func buildDomFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/buildDomFrontier().(fn *Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) domFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier# {
100 df := make(domFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#, len(fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks.))
101 df.buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#build().(fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks.[0])
102 if fn.Recoverscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Recover. != nil {
103 df.buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#build().(fn.Recoverscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Recover.)
104 }
105 return df
106 }
107
108 func removeInstrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/removeInstr().(refs []Instructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#, instr Instructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#) []Instructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction# {
109 return slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.DeleteFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/DeleteFunc().(refs, func(i Instructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#) bool { return i == instr })
110 }
111
112 // lift replaces local and new Allocs accessed only with
113 // load/store by SSA registers, inserting φ-nodes where necessary.
114 // The result is a program in classical pruned SSA form.
115 //
116 // Preconditions:
117 // - fn has no dead blocks (blockopt has run).
118 // - Def/use info (Operands and Referrers) is up-to-date.
119 // - The dominator tree is up-to-date.
120 func liftscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/lift().(fn *Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) {
121 // TODO(adonovan): opt: lots of little optimizations may be
122 // worthwhile here, especially if they cause us to avoid
123 // buildDomFrontier. For example:
124 //
125 // - Alloc never loaded? Eliminate.
126 // - Alloc never stored? Replace all loads with a zero constant.
127 // - Alloc stored once? Replace loads with dominating store;
128 // don't forget that an Alloc is itself an effective store
129 // of zero.
130 // - Alloc used only within a single block?
131 // Use degenerate algorithm avoiding φ-nodes.
132 // - Consider synergy with scalar replacement of aggregates (SRA).
133 // e.g. *(&x.f) where x is an Alloc.
134 // Perhaps we'd get better results if we generated this as x.f
135 // i.e. Field(x, .f) instead of Load(FieldIndex(x, .f)).
136 // Unclear.
137 //
138 // But we will start with the simplest correct code.
139 df := buildDomFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/buildDomFrontier().(fn)
140
141 if debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. {
142 title := false
143 for i, blocks := range df {
144 if blocks != nil {
145 if !title {
146 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "Dominance frontier of %s:\n", fn)
147 title = true
148 }
149 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "\t%s: %s\n", fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks.[i], blocks)
150 }
151 }
152 }
153
154 newPhis := make(newPhiMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhiMap#)
155
156 // During this pass we will replace some BasicBlock.Instrs
157 // (allocs, loads and stores) with nil, keeping a count in
158 // BasicBlock.gaps. At the end we will reset Instrs to the
159 // concatenation of all non-dead newPhis and non-nil Instrs
160 // for the block, reusing the original array if space permits.
161
162 // While we're here, we also eliminate 'rundefers'
163 // instructions and ssa:deferstack() in functions that contain no
164 // 'defer' instructions. For now, we also eliminate
165 // 's = ssa:deferstack()' calls if s doesn't escape, replacing s
166 // with nil in Defer{DeferStack: s}. This has the same meaning,
167 // but allows eliminating the intrinsic function `ssa:deferstack()`
168 // (unless it is needed due to range-over-func instances). This gives
169 // ssa users more time to support range-over-func.
170 usesDefer := false
171 deferstackAlloc, deferstackCall := deferstackPreamblescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/deferstackPreamble().(fn)
172 eliminateDeferStack := deferstackAlloc != nil && !deferstackAlloc.Heapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#Heap.
173
174 // A counter used to generate ~unique ids for Phi nodes, as an
175 // aid to debugging. We use large numbers to make them highly
176 // visible. All nodes are renumbered later.
177 fresh := 1000
178
179 // Determine which allocs we can lift and number them densely.
180 // The renaming phase uses this numbering for compact maps.
181 numAllocs := 0
182 for _, b := range fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. {
183 b.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps. = 0
184 b.rundefersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#rundefers. = 0
185 for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. {
186 switch instr := instr.(type) {
187 case *Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#:
188 index := -1
189 if liftAllocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/liftAlloc().(df, instr, newPhis, &fresh) {
190 index = numAllocs
191 numAllocs++
192 }
193 instr.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index. = index
194 case *Deferscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Defer#:
195 usesDefer = true
196 if eliminateDeferStack {
197 // Clear DeferStack and remove references to loads
198 if instr.DeferStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Defer#DeferStack. != nil {
199 if refs := instr.DeferStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Defer#DeferStack..Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Referrers.(); refs != nil {
200 *refs = removeInstrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/removeInstr().(*refs, instr)
201 }
202 instr.DeferStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Defer#DeferStack. = nil
203 }
204 }
205 case *RunDefersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/RunDefers#:
206 b.rundefersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#rundefers.++
207 }
208 }
209 }
210
211 // renaming maps an alloc (keyed by index) to its replacement
212 // value. Initially the renaming contains nil, signifying the
213 // zero constant of the appropriate type; we construct the
214 // Const lazily at most once on each path through the domtree.
215 // TODO(adonovan): opt: cache per-function not per subtree.
216 renaming := make([]Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#, numAllocs)
217
218 // Renaming.
219 renamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/rename().(fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks.[0], renaming, newPhis)
220
221 // Eliminate dead φ-nodes.
222 removeDeadPhisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/removeDeadPhis().(fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks., newPhis)
223
224 // Eliminate ssa:deferstack() call.
225 if eliminateDeferStack {
226 b := deferstackCall.blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/anInstruction#block.
227 for i, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. {
228 if instr == deferstackCall {
229 b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs.[i] = nil
230 b.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps.++
231 break
232 }
233 }
234 }
235
236 // Prepend remaining live φ-nodes to each block.
237 for _, b := range fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks. {
238 nps := newPhis[b]
239 j := len(nps)
240
241 rundefersToKill := b.rundefersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#rundefers.
242 if usesDefer {
243 rundefersToKill = 0
244 }
245
246 if j+b.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps.+rundefersToKill == 0 {
247 continue // fast path: no new phis or gaps
248 }
249
250 // Compact nps + non-nil Instrs into a new slice.
251 // TODO(adonovan): opt: compact in situ (rightwards)
252 // if Instrs has sufficient space or slack.
253 dst := make([]Instructionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#, len(b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs.)+j-b.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps.-rundefersToKill)
254 for i, np := range nps {
255 dst[i] = np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi.
256 }
257 for _, instr := range b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. {
258 if instr == nil {
259 continue
260 }
261 if !usesDefer {
262 if _, ok := instr.(*RunDefersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/RunDefers#); ok {
263 continue
264 }
265 }
266 dst[j] = instr
267 j++
268 }
269 b.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. = dst
270 }
271
272 // Remove any fn.Locals that were lifted.
273 j := 0
274 for _, l := range fn.Localsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Locals. {
275 if l.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index. < 0 {
276 fn.Localsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Locals.[j] = l
277 j++
278 }
279 }
280 // Nil out fn.Locals[j:] to aid GC.
281 for i := j; i < len(fn.Localsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Locals.); i++ {
282 fn.Localsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Locals.[i] = nil
283 }
284 fn.Localsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Locals. = fn.Localsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Locals.[:j]
285 }
286
287 // removeDeadPhis removes φ-nodes not transitively needed by a
288 // non-Phi, non-DebugRef instruction.
289 func removeDeadPhisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/removeDeadPhis().(blocks []*BasicBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#, newPhis newPhiMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhiMap#) {
290 // First pass: find the set of "live" φ-nodes: those reachable
291 // from some non-Phi instruction.
292 //
293 // We compute reachability in reverse, starting from each φ,
294 // rather than forwards, starting from each live non-Phi
295 // instruction, because this way visits much less of the
296 // Value graph.
297 livePhis := make(map[*Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#]bool)
298 for _, npList := range newPhis {
299 for _, np := range npList {
300 phi := np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi.
301 if !livePhis[phi] && phiHasDirectReferrerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/phiHasDirectReferrer().(phi) {
302 markLivePhiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/markLivePhi().(livePhis, phi)
303 }
304 }
305 }
306
307 // Existing φ-nodes due to && and || operators
308 // are all considered live (see Go issue 19622).
309 for _, b := range blocks {
310 for _, phi := range b.phisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#phis().() {
311 markLivePhiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/markLivePhi().(livePhis, phi.(*Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#))
312 }
313 }
314
315 // Second pass: eliminate unused phis from newPhis.
316 for block, npList := range newPhis {
317 j := 0
318 for _, np := range npList {
319 if livePhis[np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi.] {
320 npList[j] = np
321 j++
322 } else {
323 // discard it, first removing it from referrers
324 for _, val := range np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi..Edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#Edges. {
325 if refs := val.Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Referrers.(); refs != nil {
326 *refs = removeInstrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/removeInstr().(*refs, np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi.)
327 }
328 }
329 np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi..blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/anInstruction#block. = nil
330 }
331 }
332 newPhis[block] = npList[:j]
333 }
334 }
335
336 // markLivePhi marks phi, and all φ-nodes transitively reachable via
337 // its Operands, live.
338 func markLivePhiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/markLivePhi().(livePhis map[*Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#]bool, phi *Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#) {
339 livePhis[phi] = true
340 for _, rand := range phi.Operandsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#Operands().(nil) {
341 if q, ok := (*rand).(*Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#); ok {
342 if !livePhis[q] {
343 markLivePhiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/markLivePhi().(livePhis, q)
344 }
345 }
346 }
347 }
348
349 // phiHasDirectReferrer reports whether phi is directly referred to by
350 // a non-Phi instruction. Such instructions are the
351 // roots of the liveness traversal.
352 func phiHasDirectReferrerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/phiHasDirectReferrer().(phi *Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#) bool {
353 for _, instr := range *phi.Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#Referrers().() {
354 if _, ok := instr.(*Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#); !ok {
355 return true
356 }
357 }
358 return false
359 }
360
361 type blockSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet# struct{ bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/.Intscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#Int. } // (inherit methods from Int)
362
363 // add adds b to the set and returns true if the set changed.
364 func (s *blockSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#) addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#add().(b *BasicBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#) bool {
365 i := b.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Index.
366 if s.Bitscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#Bit().(i) != 0 {
367 return false
368 }
369 s.SetBitscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#SetBit().(&s.Intscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#Int., i, 1)
370 return true
371 }
372
373 // take removes an arbitrary element from a set s and
374 // returns its index, or returns -1 if empty.
375 func (s *blockSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#) takescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#take().() int {
376 l := s.BitLenscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#BitLen().()
377 for i := range l {
378 if s.Bitscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#Bit().(i) == 1 {
379 s.SetBitscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#SetBit().(&s.Intscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#Int., i, 0)
380 return i
381 }
382 }
383 return -1
384 }
385
386 // newPhi is a pair of a newly introduced φ-node and the lifted Alloc
387 // it replaces.
388 type newPhiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi# struct {
389 phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi. *Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#
390 allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#alloc. *Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#
391 }
392
393 // newPhiMap records for each basic block, the set of newPhis that
394 // must be prepended to the block.
395 type newPhiMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhiMap# map[*BasicBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#][]newPhiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#
396
397 // liftAlloc determines whether alloc can be lifted into registers,
398 // and if so, it populates newPhis with all the φ-nodes it may require
399 // and returns true.
400 //
401 // fresh is a source of fresh ids for phi nodes.
402 func liftAllocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/liftAlloc().(df domFrontierscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domFrontier#, alloc *Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#, newPhis newPhiMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhiMap#, fresh *int) bool {
403 // Don't lift result values in functions that defer
404 // calls that may recover from panic.
405 if fn := alloc.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/anInstruction#Parent().(); fn.Recoverscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Recover. != nil {
406 if slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Containsscip-go gomod github.com/golang/go/src go1.25.0 slices/Contains().(fn.resultsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#results., alloc) {
407 return false
408 }
409 }
410
411 // Compute defblocks, the set of blocks containing a
412 // definition of the alloc cell.
413 var defblocks blockSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#
414 for _, instr := range *alloc.Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#Referrers().() {
415 // Bail out if we discover the alloc is not liftable;
416 // the only operations permitted to use the alloc are
417 // loads/stores into the cell, and DebugRef.
418 switch instr := instr.(type) {
419 case *Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#:
420 if instr.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Val. == alloc {
421 return false // address used as value
422 }
423 if instr.Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Addr. != alloc {
424 panic("Alloc.Referrers is inconsistent")
425 }
426 defblocks.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#add().(instr.Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/anInstruction#Block().())
427 case *UnOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/UnOp#:
428 if instr.Opscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/UnOp#Op. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.MULscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/MUL. {
429 return false // not a load
430 }
431 if instr.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/UnOp#X. != alloc {
432 panic("Alloc.Referrers is inconsistent")
433 }
434 case *DebugRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#:
435 // ok
436 default:
437 return false // some other instruction
438 }
439 }
440 // The Alloc itself counts as a (zero) definition of the cell.
441 defblocks.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#add().(alloc.Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/anInstruction#Block().())
442
443 if debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. {
444 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "\tlifting ", alloc, alloc.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#Name().())
445 }
446
447 fn := alloc.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/anInstruction#Parent().()
448
449 // Φ-insertion.
450 //
451 // What follows is the body of the main loop of the insert-φ
452 // function described by Cytron et al, but instead of using
453 // counter tricks, we just reset the 'hasAlready' and 'work'
454 // sets each iteration. These are bitmaps so it's pretty cheap.
455 //
456 // TODO(adonovan): opt: recycle slice storage for W,
457 // hasAlready, defBlocks across liftAlloc calls.
458 var hasAlready blockSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#
459
460 // Initialize W and work to defblocks.
461 var work blockSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet# = defblocks // blocks seen
462 var W blockSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet# // blocks to do
463 W.Setscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#Set().(&defblocks.Intscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#Int.)
464
465 // Traverse iterated dominance frontier, inserting φ-nodes.
466 for i := W.takescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#take().(); i != -1; i = W.takescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#take().() {
467 u := fn.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#Blocks.[i]
468 for _, v := range df[u.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Index.] {
469 if hasAlready.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#add().(v) {
470 // Create φ-node.
471 // It will be prepended to v.Instrs later, if needed.
472 phi := &Phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#{
473 Edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#Edges.: make([]Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#, len(v.Predsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Preds.)),
474 Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#Comment.: alloc.Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#Comment.,
475 }
476 // This is merely a debugging aid:
477 phi.setNumscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#setNum().(*fresh)
478 *fresh++
479
480 phi.posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#pos. = alloc.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#Pos().()
481 phi.setTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#setType().(typeparamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/.MustDerefscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/MustDeref().(alloc.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#Type().()))
482 phi.blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/anInstruction#block. = v
483 if debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. {
484 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "\tplace %s = %s at block %s\n", phi.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#Name().(), phi, v)
485 }
486 newPhis[v] = append(newPhis[v], newPhiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#{phi, alloc})
487
488 if work.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#add().(v) {
489 W.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/blockSet#add().(v)
490 }
491 }
492 }
493 }
494
495 return true
496 }
497
498 // replaceAll replaces all intraprocedural uses of x with y,
499 // updating x.Referrers and y.Referrers.
500 // Precondition: x.Referrers() != nil, i.e. x must be local to some function.
501 func replaceAllscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/replaceAll().(x, y Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#) {
502 var rands []*Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#
503 pxrefs := x.Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Referrers.()
504 pyrefs := y.Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Referrers.()
505 for _, instr := range *pxrefs {
506 rands = instr.Operandsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Instruction#Operands.(rands[:0]) // recycle storage
507 for _, rand := range rands {
508 if *rand != nil {
509 if *rand == x {
510 *rand = y
511 }
512 }
513 }
514 if pyrefs != nil {
515 *pyrefs = append(*pyrefs, instr) // dups ok
516 }
517 }
518 *pxrefs = nil // x is now unreferenced
519 }
520
521 // renamed returns the value to which alloc is being renamed,
522 // constructing it lazily if it's the implicit zero initialization.
523 func renamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/renamed().(renaming []Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#, alloc *Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#) Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value# {
524 v := renaming[alloc.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index.]
525 if v == nil {
526 v = zeroConstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/zeroConst().(typeparamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/.MustDerefscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/MustDeref().(alloc.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#Type().()))
527 renaming[alloc.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index.] = v
528 }
529 return v
530 }
531
532 // rename implements the (Cytron et al) SSA renaming algorithm, a
533 // preorder traversal of the dominator tree replacing all loads of
534 // Alloc cells with the value stored to that cell by the dominating
535 // store instruction. For lifting, we need only consider loads,
536 // stores and φ-nodes.
537 //
538 // renaming is a map from *Alloc (keyed by index number) to its
539 // dominating stored value; newPhis[x] is the set of new φ-nodes to be
540 // prepended to block x.
541 func renamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/rename().(u *BasicBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#, renaming []Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#, newPhis newPhiMapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhiMap#) {
542 // Each φ-node becomes the new name for its associated Alloc.
543 for _, np := range newPhis[u] {
544 phi := np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi.
545 alloc := np.allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#alloc.
546 renaming[alloc.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index.] = phi
547 }
548
549 // Rename loads and stores of allocs.
550 for i, instr := range u.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs. {
551 switch instr := instr.(type) {
552 case *Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#:
553 if instr.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index. >= 0 { // store of zero to Alloc cell
554 // Replace dominated loads by the zero value.
555 renaming[instr.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index.] = nil
556 if debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. {
557 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "\tkill alloc %s\n", instr)
558 }
559 // Delete the Alloc.
560 u.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs.[i] = nil
561 u.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps.++
562 }
563
564 case *Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#:
565 if alloc, ok := instr.Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Addr..(*Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#); ok && alloc.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index. >= 0 { // store to Alloc cell
566 // Replace dominated loads by the stored value.
567 renaming[alloc.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index.] = instr.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Val.
568 if debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. {
569 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "\tkill store %s; new value: %s\n",
570 instr, instr.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Val..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Name.())
571 }
572 // Remove the store from the referrer list of the stored value.
573 if refs := instr.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Val..Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Referrers.(); refs != nil {
574 *refs = removeInstrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/removeInstr().(*refs, instr)
575 }
576 // Delete the Store.
577 u.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs.[i] = nil
578 u.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps.++
579 }
580
581 case *UnOpscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/UnOp#:
582 if instr.Opscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/UnOp#Op. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.MULscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/MUL. {
583 if alloc, ok := instr.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/UnOp#X..(*Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#); ok && alloc.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index. >= 0 { // load of Alloc cell
584 newval := renamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/renamed().(renaming, alloc)
585 if debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. {
586 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "\tupdate load %s = %s with %s\n",
587 instr.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#Name().(), instr, newval.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Name.())
588 }
589 // Replace all references to
590 // the loaded value by the
591 // dominating stored value.
592 replaceAllscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/replaceAll().(instr, newval)
593 // Delete the Load.
594 u.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs.[i] = nil
595 u.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps.++
596 }
597 }
598
599 case *DebugRefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#:
600 if alloc, ok := instr.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#X..(*Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#); ok && alloc.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#index. >= 0 { // ref of Alloc cell
601 if instr.IsAddrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#IsAddr. {
602 instr.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#X. = renamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/renamed().(renaming, alloc)
603 instr.IsAddrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#IsAddr. = false
604
605 // Add DebugRef to instr.X's referrers.
606 if refs := instr.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#X..Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Referrers.(); refs != nil {
607 *refs = append(*refs, instr)
608 }
609 } else {
610 // A source expression denotes the address
611 // of an Alloc that was optimized away.
612 instr.Xscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/DebugRef#X. = nil
613
614 // Delete the DebugRef.
615 u.Instrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Instrs.[i] = nil
616 u.gapsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#gaps.++
617 }
618 }
619 }
620 }
621
622 // For each φ-node in a CFG successor, rename the edge.
623 for _, v := range u.Succsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#Succs. {
624 phis := newPhis[v]
625 if len(phis) == 0 {
626 continue
627 }
628 i := v.predIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#predIndex().(u)
629 for _, np := range phis {
630 phi := np.phiscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#phi.
631 alloc := np.allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/newPhi#alloc.
632 newval := renamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/renamed().(renaming, alloc)
633 if debugLiftingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/debugLifting. {
634 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "\tsetphi %s edge %s -> %s (#%d) (alloc=%s) := %s\n",
635 phi.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#Name().(), u, v, i, alloc.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/register#Name().(), newval.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Name.())
636 }
637 phi.Edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Phi#Edges.[i] = newval
638 if prefs := newval.Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#Referrers.(); prefs != nil {
639 *prefs = append(*prefs, phi)
640 }
641 }
642 }
643
644 // Continue depth-first recursion over domtree, pushing a
645 // fresh copy of the renaming map for each subtree.
646 for i, v := range u.domscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#dom..childrenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domInfo#children. {
647 r := renaming
648 if i < len(u.domscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/BasicBlock#dom..childrenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/domInfo#children.)-1 {
649 // On all but the final iteration, we must make
650 // a copy to avoid destructive update.
651 r = make([]Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Value#, len(renaming))
652 copy(r, renaming)
653 }
654 renamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/rename().(v, r, newPhis)
655 }
656
657 }
658
659 // deferstackPreamble returns the *Alloc and ssa:deferstack() call for fn.deferstack.
660 func deferstackPreamblescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/deferstackPreamble().(fn *Functionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#) (*Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#, *Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#) {
661 if alloc, _ := fn.varsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#vars.[fn.deferstackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Function#deferstack.].(*Allocscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#); alloc != nil {
662 for _, ref := range *alloc.Referrersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Alloc#Referrers().() {
663 if ref, _ := ref.(*Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#); ref != nil && ref.Addrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Addr. == alloc {
664 if call, _ := ref.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Store#Val..(*Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/Call#); call != nil {
665 return alloc, call
666 }
667 }
668 }
669 }
670 return nil, nil
671 }