Skip to content

Commit 0cfb231

Browse files
committed
cmd/compile: move hasdefer to Func
Passes toolstash -cmp. Updates #15756 Change-Id: Ia071dbbd7f2ee0f8433d8c37af4f7b588016244e Reviewed-on: https://go-review.googlesource.com/38231 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
1 parent 604e484 commit 0cfb231

File tree

6 files changed

+9
-7
lines changed

6 files changed

+9
-7
lines changed

src/cmd/compile/internal/gc/go.go

-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ var Stksize int64 // stack size for current frame
251251

252252
var stkptrsize int64 // prefix of stack containing pointers
253253

254-
var hasdefer bool // flag that curfn has defer statement
255-
256254
var Curfn *Node
257255

258256
var Widthptr int

src/cmd/compile/internal/gc/pgen.go

-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ func compile(fn *Node) {
340340
return
341341
}
342342

343-
hasdefer = false
344343
walk(fn)
345344
if nerrors != 0 {
346345
return

src/cmd/compile/internal/gc/plive.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ func livenessepilogue(lv *Liveness) {
10921092
// pointers to copy values back to the stack).
10931093
// TODO: if the output parameter is heap-allocated, then we
10941094
// don't need to keep the stack copy live?
1095-
if hasdefer {
1095+
if lv.fn.Func.HasDefer() {
10961096
for i, n := range lv.vars {
10971097
if n.Class == PPARAMOUT {
10981098
if n.IsOutputParamHeapAddr() {

src/cmd/compile/internal/gc/ssa.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func buildssa(fn *Node) *ssa.Func {
4747
s.pushLine(fn.Pos)
4848
defer s.popLine()
4949

50+
s.hasdefer = fn.Func.HasDefer()
5051
if fn.Func.Pragma&CgoUnsafeArgs != 0 {
5152
s.cgoUnsafeArgs = true
5253
}
@@ -218,6 +219,7 @@ type state struct {
218219
placeholder *ssa.Value
219220

220221
cgoUnsafeArgs bool
222+
hasdefer bool // whether the function contains a defer statement
221223
}
222224

223225
type funcLine struct {
@@ -877,7 +879,7 @@ func (s *state) stmt(n *Node) {
877879
// It returns a BlockRet block that ends the control flow. Its control value
878880
// will be set to the final memory state.
879881
func (s *state) exit() *ssa.Block {
880-
if hasdefer {
882+
if s.hasdefer {
881883
s.rtcall(Deferreturn, true, nil)
882884
}
883885

@@ -3189,7 +3191,7 @@ func (s *state) canSSA(n *Node) bool {
31893191
case PEXTERN:
31903192
return false
31913193
case PPARAMOUT:
3192-
if hasdefer {
3194+
if s.hasdefer {
31933195
// TODO: handle this case? Named return values must be
31943196
// in memory so that the deferred function can see them.
31953197
// Maybe do: if !strings.HasPrefix(n.String(), "~") { return false }

src/cmd/compile/internal/gc/syntax.go

+3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ const (
342342
funcReflectMethod // function calls reflect.Type.Method or MethodByName
343343
funcIsHiddenClosure
344344
funcNoFramePointer // Must not use a frame pointer for this function
345+
funcHasDefer // contains a defer statement
345346
)
346347

347348
func (f *Func) Dupok() bool { return f.flags&funcDupok != 0 }
@@ -350,13 +351,15 @@ func (f *Func) Needctxt() bool { return f.flags&funcNeedctxt != 0 }
350351
func (f *Func) ReflectMethod() bool { return f.flags&funcReflectMethod != 0 }
351352
func (f *Func) IsHiddenClosure() bool { return f.flags&funcIsHiddenClosure != 0 }
352353
func (f *Func) NoFramePointer() bool { return f.flags&funcNoFramePointer != 0 }
354+
func (f *Func) HasDefer() bool { return f.flags&funcHasDefer != 0 }
353355

354356
func (f *Func) SetDupok(b bool) { f.flags.set(funcDupok, b) }
355357
func (f *Func) SetWrapper(b bool) { f.flags.set(funcWrapper, b) }
356358
func (f *Func) SetNeedctxt(b bool) { f.flags.set(funcNeedctxt, b) }
357359
func (f *Func) SetReflectMethod(b bool) { f.flags.set(funcReflectMethod, b) }
358360
func (f *Func) SetIsHiddenClosure(b bool) { f.flags.set(funcIsHiddenClosure, b) }
359361
func (f *Func) SetNoFramePointer(b bool) { f.flags.set(funcNoFramePointer, b) }
362+
func (f *Func) SetHasDefer(b bool) { f.flags.set(funcHasDefer, b) }
360363

361364
type Op uint8
362365

src/cmd/compile/internal/gc/walk.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func walkstmt(n *Node) *Node {
247247
n.Right = walkstmt(n.Right)
248248

249249
case ODEFER:
250-
hasdefer = true
250+
Curfn.Func.SetHasDefer(true)
251251
switch n.Left.Op {
252252
case OPRINT, OPRINTN:
253253
n.Left = walkprintfunc(n.Left, &n.Ninit)

0 commit comments

Comments
 (0)