1 // Copyright 2012 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.
11 "cmd/compile/internal/base"
12 "cmd/compile/internal/ir"
13 "cmd/compile/internal/reflectdata"
14 "cmd/compile/internal/staticinit"
15 "cmd/compile/internal/typecheck"
16 "cmd/compile/internal/types"
21 // Rewrite tree to use separate statements to enforce
22 // order of evaluation. Makes walk easier, because it
23 // can (after this runs) reorder at will within an expression.
25 // Rewrite m[k] op= r into m[k] = m[k] op r if op is / or %.
27 // Introduce temporaries as needed by runtime routines.
28 // For example, the map runtime routines take the map key
29 // by reference, so make sure all map keys are addressable
30 // by copying them to temporaries as needed.
31 // The same is true for channel operations.
33 // Arrange that map index expressions only appear in direct
34 // assignments x = m[k] or m[k] = x, never in larger expressions.
36 // Arrange that receive expressions only appear in direct assignments
37 // x = <-c or as standalone statements <-c, never in larger expressions.
39 // TODO(rsc): The temporary introduction during multiple assignments
40 // should be moved into this file, so that the temporaries can be cleaned
41 // and so that conversions implicit in the OAS2FUNC and OAS2RECV
42 // nodes can be made explicit and then have their temporaries cleaned.
44 // TODO(rsc): Goto and multilevel break/continue can jump over
45 // inserted VARKILL annotations. Work out a way to handle these.
46 // The current implementation is safe, in that it will execute correctly.
47 // But it won't reuse temporaries as aggressively as it might, and
48 // it can result in unnecessary zeroing of those variables in the function
51 // orderState holds state during the ordering process.
52 type orderState struct {
53 out []ir.Node // list of generated statements
54 temp []*ir.Name // stack of temporary variables
55 free map[string][]*ir.Name // free list of unused temporaries, by type.LinkString().
56 edit func(ir.Node) ir.Node // cached closure of o.exprNoLHS
59 // Order rewrites fn.Nbody to apply the ordering constraints
60 // described in the comment at the top of the file.
61 func order(fn *ir.Func) {
63 s := fmt.Sprintf("\nbefore order %v", fn.Sym())
64 ir.DumpList(s, fn.Body)
67 orderBlock(&fn.Body, map[string][]*ir.Name{})
70 // append typechecks stmt and appends it to out.
71 func (o *orderState) append(stmt ir.Node) {
72 o.out = append(o.out, typecheck.Stmt(stmt))
75 // newTemp allocates a new temporary with the given type,
76 // pushes it onto the temp stack, and returns it.
77 // If clear is true, newTemp emits code to zero the temporary.
78 func (o *orderState) newTemp(t *types.Type, clear bool) *ir.Name {
81 if a := o.free[key]; len(a) > 0 {
83 if !types.Identical(t, v.Type()) {
84 base.Fatalf("expected %L to have type %v", v, t)
86 o.free[key] = a[:len(a)-1]
91 o.append(ir.NewAssignStmt(base.Pos, v, nil))
94 o.temp = append(o.temp, v)
98 // copyExpr behaves like newTemp but also emits
99 // code to initialize the temporary to the value n.
100 func (o *orderState) copyExpr(n ir.Node) *ir.Name {
101 return o.copyExpr1(n, false)
104 // copyExprClear is like copyExpr but clears the temp before assignment.
105 // It is provided for use when the evaluation of tmp = n turns into
106 // a function call that is passed a pointer to the temporary as the output space.
107 // If the call blocks before tmp has been written,
108 // the garbage collector will still treat the temporary as live,
109 // so we must zero it before entering that call.
110 // Today, this only happens for channel receive operations.
111 // (The other candidate would be map access, but map access
112 // returns a pointer to the result data instead of taking a pointer
114 func (o *orderState) copyExprClear(n ir.Node) *ir.Name {
115 return o.copyExpr1(n, true)
118 func (o *orderState) copyExpr1(n ir.Node, clear bool) *ir.Name {
120 v := o.newTemp(t, clear)
121 o.append(ir.NewAssignStmt(base.Pos, v, n))
125 // cheapExpr returns a cheap version of n.
126 // The definition of cheap is that n is a variable or constant.
127 // If not, cheapExpr allocates a new tmp, emits tmp = n,
128 // and then returns tmp.
129 func (o *orderState) cheapExpr(n ir.Node) ir.Node {
135 case ir.ONAME, ir.OLITERAL, ir.ONIL:
137 case ir.OLEN, ir.OCAP:
138 n := n.(*ir.UnaryExpr)
139 l := o.cheapExpr(n.X)
143 a := ir.SepCopy(n).(*ir.UnaryExpr)
145 return typecheck.Expr(a)
151 // safeExpr returns a safe version of n.
152 // The definition of safe is that n can appear multiple times
153 // without violating the semantics of the original program,
154 // and that assigning to the safe version has the same effect
155 // as assigning to the original n.
157 // The intended use is to apply to x when rewriting x += y into x = x + y.
158 func (o *orderState) safeExpr(n ir.Node) ir.Node {
160 case ir.ONAME, ir.OLITERAL, ir.ONIL:
163 case ir.OLEN, ir.OCAP:
164 n := n.(*ir.UnaryExpr)
169 a := ir.SepCopy(n).(*ir.UnaryExpr)
171 return typecheck.Expr(a)
174 n := n.(*ir.SelectorExpr)
179 a := ir.SepCopy(n).(*ir.SelectorExpr)
181 return typecheck.Expr(a)
184 n := n.(*ir.SelectorExpr)
185 l := o.cheapExpr(n.X)
189 a := ir.SepCopy(n).(*ir.SelectorExpr)
191 return typecheck.Expr(a)
194 n := n.(*ir.StarExpr)
195 l := o.cheapExpr(n.X)
199 a := ir.SepCopy(n).(*ir.StarExpr)
201 return typecheck.Expr(a)
203 case ir.OINDEX, ir.OINDEXMAP:
204 n := n.(*ir.IndexExpr)
206 if n.X.Type().IsArray() {
211 r := o.cheapExpr(n.Index)
212 if l == n.X && r == n.Index {
215 a := ir.SepCopy(n).(*ir.IndexExpr)
218 return typecheck.Expr(a)
221 base.Fatalf("order.safeExpr %v", n.Op())
222 return nil // not reached
226 // isaddrokay reports whether it is okay to pass n's address to runtime routines.
227 // Taking the address of a variable makes the liveness and optimization analyses
228 // lose track of where the variable's lifetime ends. To avoid hurting the analyses
229 // of ordinary stack variables, those are not 'isaddrokay'. Temporaries are okay,
230 // because we emit explicit VARKILL instructions marking the end of those
231 // temporaries' lifetimes.
232 func isaddrokay(n ir.Node) bool {
233 return ir.IsAddressable(n) && (n.Op() != ir.ONAME || n.(*ir.Name).Class == ir.PEXTERN || ir.IsAutoTmp(n))
236 // addrTemp ensures that n is okay to pass by address to runtime routines.
237 // If the original argument n is not okay, addrTemp creates a tmp, emits
238 // tmp = n, and then returns tmp.
239 // The result of addrTemp MUST be assigned back to n, e.g.
240 // n.Left = o.addrTemp(n.Left)
241 func (o *orderState) addrTemp(n ir.Node) ir.Node {
242 if n.Op() == ir.OLITERAL || n.Op() == ir.ONIL {
243 // TODO: expand this to all static composite literal nodes?
244 n = typecheck.DefaultLit(n, nil)
245 types.CalcSize(n.Type())
246 vstat := readonlystaticname(n.Type())
247 var s staticinit.Schedule
248 s.StaticAssign(vstat, 0, n, n.Type())
250 base.Fatalf("staticassign of const generated code: %+v", n)
252 vstat = typecheck.Expr(vstat).(*ir.Name)
261 // mapKeyTemp prepares n to be a key in a map runtime call and returns n.
262 // It should only be used for map runtime calls which have *_fast* versions.
263 func (o *orderState) mapKeyTemp(t *types.Type, n ir.Node) ir.Node {
264 // Most map calls need to take the address of the key.
265 // Exception: map*_fast* calls. See golang.org/issue/19015.
273 kt = types.Types[types.TUINT32]
275 kt = types.Types[types.TUINT64]
276 case mapfast32ptr, mapfast64ptr:
277 kt = types.Types[types.TUNSAFEPTR]
279 kt = types.Types[types.TSTRING]
285 case nt.Kind() == kt.Kind(), nt.IsPtrShaped() && kt.IsPtrShaped():
286 // can directly convert (e.g. named type to underlying type, or one pointer to another)
287 return typecheck.Expr(ir.NewConvExpr(n.Pos(), ir.OCONVNOP, kt, n))
288 case nt.IsInteger() && kt.IsInteger():
289 // can directly convert (e.g. int32 to uint32)
290 if n.Op() == ir.OLITERAL && nt.IsSigned() {
291 // avoid constant overflow error
292 n = ir.NewConstExpr(constant.MakeUint64(uint64(ir.Int64Val(n))), n)
296 return typecheck.Expr(ir.NewConvExpr(n.Pos(), ir.OCONV, kt, n))
298 // Unsafe cast through memory.
299 // We'll need to do a load with type kt. Create a temporary of type kt to
300 // ensure sufficient alignment. nt may be under-aligned.
301 if uint8(kt.Alignment()) < uint8(nt.Alignment()) {
302 base.Fatalf("mapKeyTemp: key type is not sufficiently aligned, kt=%v nt=%v", kt, nt)
304 tmp := o.newTemp(kt, true)
306 var e ir.Node = typecheck.NodAddr(tmp)
307 e = ir.NewConvExpr(n.Pos(), ir.OCONVNOP, nt.PtrTo(), e)
308 e = ir.NewStarExpr(n.Pos(), e)
309 o.append(ir.NewAssignStmt(base.Pos, e, n))
314 // mapKeyReplaceStrConv replaces OBYTES2STR by OBYTES2STRTMP
315 // in n to avoid string allocations for keys in map lookups.
316 // Returns a bool that signals if a modification was made.
320 // x = m[T1{... Tn{..., string(k), ...}]
321 // where k is []byte, T1 to Tn is a nesting of struct and array literals,
322 // the allocation of backing bytes for the string can be avoided
323 // by reusing the []byte backing array. These are special cases
324 // for avoiding allocations when converting byte slices to strings.
325 // It would be nice to handle these generally, but because
326 // []byte keys are not allowed in maps, the use of string(k)
327 // comes up in important cases in practice. See issue 3512.
328 func mapKeyReplaceStrConv(n ir.Node) bool {
332 n := n.(*ir.ConvExpr)
333 n.SetOp(ir.OBYTES2STRTMP)
336 n := n.(*ir.CompLitExpr)
337 for _, elem := range n.List {
338 elem := elem.(*ir.StructKeyExpr)
339 if mapKeyReplaceStrConv(elem.Value) {
344 n := n.(*ir.CompLitExpr)
345 for _, elem := range n.List {
346 if elem.Op() == ir.OKEY {
347 elem = elem.(*ir.KeyExpr).Value
349 if mapKeyReplaceStrConv(elem) {
359 // markTemp returns the top of the temporary variable stack.
360 func (o *orderState) markTemp() ordermarker {
361 return ordermarker(len(o.temp))
364 // popTemp pops temporaries off the stack until reaching the mark,
365 // which must have been returned by markTemp.
366 func (o *orderState) popTemp(mark ordermarker) {
367 for _, n := range o.temp[mark:] {
368 key := n.Type().LinkString()
369 o.free[key] = append(o.free[key], n)
371 o.temp = o.temp[:mark]
374 // cleanTempNoPop emits VARKILL instructions to *out
375 // for each temporary above the mark on the temporary stack.
376 // It does not pop the temporaries from the stack.
377 func (o *orderState) cleanTempNoPop(mark ordermarker) []ir.Node {
379 for i := len(o.temp) - 1; i >= int(mark); i-- {
381 out = append(out, typecheck.Stmt(ir.NewUnaryExpr(base.Pos, ir.OVARKILL, n)))
386 // cleanTemp emits VARKILL instructions for each temporary above the
387 // mark on the temporary stack and removes them from the stack.
388 func (o *orderState) cleanTemp(top ordermarker) {
389 o.out = append(o.out, o.cleanTempNoPop(top)...)
393 // stmtList orders each of the statements in the list.
394 func (o *orderState) stmtList(l ir.Nodes) {
397 orderMakeSliceCopy(s[i:])
402 // orderMakeSliceCopy matches the pattern:
403 // m = OMAKESLICE([]T, x); OCOPY(m, s)
404 // and rewrites it to:
405 // m = OMAKESLICECOPY([]T, x, s); nil
406 func orderMakeSliceCopy(s []ir.Node) {
407 if base.Flag.N != 0 || base.Flag.Cfg.Instrumenting {
410 if len(s) < 2 || s[0] == nil || s[0].Op() != ir.OAS || s[1] == nil || s[1].Op() != ir.OCOPY {
414 as := s[0].(*ir.AssignStmt)
415 cp := s[1].(*ir.BinaryExpr)
416 if as.Y == nil || as.Y.Op() != ir.OMAKESLICE || ir.IsBlank(as.X) ||
417 as.X.Op() != ir.ONAME || cp.X.Op() != ir.ONAME || cp.Y.Op() != ir.ONAME ||
418 as.X.Name() != cp.X.Name() || cp.X.Name() == cp.Y.Name() {
419 // The line above this one is correct with the differing equality operators:
420 // we want as.X and cp.X to be the same name,
421 // but we want the initial data to be coming from a different name.
425 mk := as.Y.(*ir.MakeExpr)
426 if mk.Esc() == ir.EscNone || mk.Len == nil || mk.Cap != nil {
429 mk.SetOp(ir.OMAKESLICECOPY)
431 // Set bounded when m = OMAKESLICE([]T, len(s)); OCOPY(m, s)
432 mk.SetBounded(mk.Len.Op() == ir.OLEN && ir.SameSafeExpr(mk.Len.(*ir.UnaryExpr).X, cp.Y))
433 as.Y = typecheck.Expr(mk)
434 s[1] = nil // remove separate copy call
437 // edge inserts coverage instrumentation for libfuzzer.
438 func (o *orderState) edge() {
439 if base.Debug.Libfuzzer == 0 {
443 // Create a new uint8 counter to be allocated in section
444 // __libfuzzer_extra_counters.
445 counter := staticinit.StaticName(types.Types[types.TUINT8])
446 counter.SetLibfuzzerExtraCounter(true)
447 // As well as setting SetLibfuzzerExtraCounter, we preemptively set the
448 // symbol type to SLIBFUZZER_EXTRA_COUNTER so that the race detector
449 // instrumentation pass (which does not have access to the flags set by
450 // SetLibfuzzerExtraCounter) knows to ignore them. This information is
451 // lost by the time it reaches the compile step, so SetLibfuzzerExtraCounter
452 // is still necessary.
453 counter.Linksym().Type = objabi.SLIBFUZZER_EXTRA_COUNTER
456 incr := ir.NewAssignOpStmt(base.Pos, ir.OADD, counter, ir.NewInt(1))
460 // orderBlock orders the block of statements in n into a new slice,
461 // and then replaces the old slice in n with the new slice.
462 // free is a map that can be used to obtain temporary variables by type.
463 func orderBlock(n *ir.Nodes, free map[string][]*ir.Name) {
466 mark := order.markTemp()
469 order.cleanTemp(mark)
473 // exprInPlace orders the side effects in *np and
474 // leaves them as the init list of the final *np.
475 // The result of exprInPlace MUST be assigned back to n, e.g.
476 // n.Left = o.exprInPlace(n.Left)
477 func (o *orderState) exprInPlace(n ir.Node) ir.Node {
480 n = order.expr(n, nil)
481 n = ir.InitExpr(order.out, n)
483 // insert new temporaries from order
484 // at head of outer list.
485 o.temp = append(o.temp, order.temp...)
489 // orderStmtInPlace orders the side effects of the single statement *np
490 // and replaces it with the resulting statement list.
491 // The result of orderStmtInPlace MUST be assigned back to n, e.g.
492 // n.Left = orderStmtInPlace(n.Left)
493 // free is a map that can be used to obtain temporary variables by type.
494 func orderStmtInPlace(n ir.Node, free map[string][]*ir.Name) ir.Node {
497 mark := order.markTemp()
499 order.cleanTemp(mark)
500 return ir.NewBlockStmt(src.NoXPos, order.out)
503 // init moves n's init list to o.out.
504 func (o *orderState) init(n ir.Node) {
505 if ir.MayBeShared(n) {
506 // For concurrency safety, don't mutate potentially shared nodes.
507 // First, ensure that no work is required here.
508 if len(n.Init()) > 0 {
509 base.Fatalf("order.init shared node with ninit")
513 o.stmtList(ir.TakeInit(n))
516 // call orders the call expression n.
517 // n.Op is OCALLFUNC/OCALLINTER or a builtin like OCOPY.
518 func (o *orderState) call(nn ir.Node) {
519 if len(nn.Init()) > 0 {
520 // Caller should have already called o.init(nn).
521 base.Fatalf("%v with unexpected ninit", nn.Op())
523 if nn.Op() == ir.OCALLMETH {
524 base.FatalfAt(nn.Pos(), "OCALLMETH missed by typecheck")
527 // Builtin functions.
528 if nn.Op() != ir.OCALLFUNC && nn.Op() != ir.OCALLINTER {
529 switch n := nn.(type) {
531 base.Fatalf("unexpected call: %+v", n)
533 n.X = o.expr(n.X, nil)
535 n.X = o.expr(n.X, nil)
537 n.X = o.expr(n.X, nil)
538 n.Y = o.expr(n.Y, nil)
540 n.Len = o.expr(n.Len, nil)
541 n.Cap = o.expr(n.Cap, nil)
548 n := nn.(*ir.CallExpr)
549 typecheck.FixVariadicCall(n)
551 if isFuncPCIntrinsic(n) && isIfaceOfFunc(n.Args[0]) {
552 // For internal/abi.FuncPCABIxxx(fn), if fn is a defined function,
553 // do not introduce temporaries here, so it is easier to rewrite it
554 // to symbol address reference later in walk.
558 n.X = o.expr(n.X, nil)
562 // mapAssign appends n to o.out.
563 func (o *orderState) mapAssign(n ir.Node) {
566 base.Fatalf("order.mapAssign %v", n.Op())
569 n := n.(*ir.AssignStmt)
570 if n.X.Op() == ir.OINDEXMAP {
571 n.Y = o.safeMapRHS(n.Y)
573 o.out = append(o.out, n)
575 n := n.(*ir.AssignOpStmt)
576 if n.X.Op() == ir.OINDEXMAP {
577 n.Y = o.safeMapRHS(n.Y)
579 o.out = append(o.out, n)
583 func (o *orderState) safeMapRHS(r ir.Node) ir.Node {
584 // Make sure we evaluate the RHS before starting the map insert.
585 // We need to make sure the RHS won't panic. See issue 22881.
586 if r.Op() == ir.OAPPEND {
587 r := r.(*ir.CallExpr)
589 for i, n := range s {
590 s[i] = o.cheapExpr(n)
594 return o.cheapExpr(r)
597 // stmt orders the statement n, appending to o.out.
598 // Temporaries created during the statement are cleaned
599 // up using VARKILL instructions as possible.
600 func (o *orderState) stmt(n ir.Node) {
610 base.Fatalf("order.stmt %v", n.Op())
612 case ir.OVARKILL, ir.OVARLIVE, ir.OINLMARK:
613 o.out = append(o.out, n)
616 n := n.(*ir.AssignStmt)
618 n.X = o.expr(n.X, nil)
619 n.Y = o.expr(n.Y, n.X)
624 n := n.(*ir.AssignOpStmt)
626 n.X = o.expr(n.X, nil)
627 n.Y = o.expr(n.Y, nil)
629 if base.Flag.Cfg.Instrumenting || n.X.Op() == ir.OINDEXMAP && (n.AsOp == ir.ODIV || n.AsOp == ir.OMOD) {
630 // Rewrite m[k] op= r into m[k] = m[k] op r so
631 // that we can ensure that if op panics
632 // because r is zero, the panic happens before
633 // the map assignment.
634 // DeepCopy is a big hammer here, but safeExpr
635 // makes sure there is nothing too deep being copied.
636 l1 := o.safeExpr(n.X)
637 l2 := ir.DeepCopy(src.NoXPos, l1)
638 if l2.Op() == ir.OINDEXMAP {
639 l2 := l2.(*ir.IndexExpr)
643 r := o.expr(typecheck.Expr(ir.NewBinaryExpr(n.Pos(), n.AsOp, l2, n.Y)), nil)
644 as := typecheck.Stmt(ir.NewAssignStmt(n.Pos(), l1, r))
654 n := n.(*ir.AssignListStmt)
658 o.out = append(o.out, n)
661 // Special: avoid copy of func call n.Right
663 n := n.(*ir.AssignListStmt)
668 if ic, ok := call.(*ir.InlinedCallExpr); ok {
672 n.Rhs = ic.ReturnVars
675 o.out = append(o.out, n)
682 // Special: use temporary variables to hold result,
683 // so that runtime can take address of temporary.
684 // No temporary for blank assignment.
686 // OAS2MAPR: make sure key is addressable if needed,
687 // and make sure OINDEXMAP is not copied out.
688 case ir.OAS2DOTTYPE, ir.OAS2RECV, ir.OAS2MAPR:
689 n := n.(*ir.AssignListStmt)
693 switch r := n.Rhs[0]; r.Op() {
695 r := r.(*ir.TypeAssertExpr)
696 r.X = o.expr(r.X, nil)
697 case ir.ODYNAMICDOTTYPE2:
698 r := r.(*ir.DynamicTypeAssertExpr)
699 r.X = o.expr(r.X, nil)
700 r.T = o.expr(r.T, nil)
702 r := r.(*ir.UnaryExpr)
703 r.X = o.expr(r.X, nil)
705 r := r.(*ir.IndexExpr)
706 r.X = o.expr(r.X, nil)
707 r.Index = o.expr(r.Index, nil)
708 // See similar conversion for OINDEXMAP below.
709 _ = mapKeyReplaceStrConv(r.Index)
710 r.Index = o.mapKeyTemp(r.X.Type(), r.Index)
712 base.Fatalf("order.stmt: %v", r.Op())
718 // Special: does not save n onto out.
720 n := n.(*ir.BlockStmt)
723 // Special: n->left is not an expression; save as is.
733 o.out = append(o.out, n)
735 // Special: handle call arguments.
736 case ir.OCALLFUNC, ir.OCALLINTER:
737 n := n.(*ir.CallExpr)
740 o.out = append(o.out, n)
744 n := n.(*ir.InlinedCallExpr)
747 // discard results; double-check for no side effects
748 for _, result := range n.ReturnVars {
749 if staticinit.AnySideEffects(result) {
750 base.FatalfAt(result.Pos(), "inlined call result has side effects: %v", result)
754 case ir.OCHECKNIL, ir.OCLOSE, ir.OPANIC, ir.ORECV:
755 n := n.(*ir.UnaryExpr)
757 n.X = o.expr(n.X, nil)
758 o.out = append(o.out, n)
762 n := n.(*ir.BinaryExpr)
764 n.X = o.expr(n.X, nil)
765 n.Y = o.expr(n.Y, nil)
766 o.out = append(o.out, n)
769 case ir.OPRINT, ir.OPRINTN, ir.ORECOVERFP:
770 n := n.(*ir.CallExpr)
773 o.out = append(o.out, n)
776 // Special: order arguments to inner call but not call itself.
777 case ir.ODEFER, ir.OGO:
778 n := n.(*ir.GoDeferStmt)
782 o.out = append(o.out, n)
786 n := n.(*ir.CallExpr)
788 n.Args[0] = o.expr(n.Args[0], nil)
789 n.Args[1] = o.expr(n.Args[1], nil)
790 n.Args[1] = o.mapKeyTemp(n.Args[0].Type(), n.Args[1])
791 o.out = append(o.out, n)
794 // Clean temporaries from condition evaluation at
795 // beginning of loop body and after for statement.
799 n.Cond = o.exprInPlace(n.Cond)
800 n.Body.Prepend(o.cleanTempNoPop(t)...)
801 orderBlock(&n.Body, o.free)
802 n.Post = orderStmtInPlace(n.Post, o.free)
803 o.out = append(o.out, n)
806 // Clean temporaries from condition at
807 // beginning of both branches.
811 n.Cond = o.exprInPlace(n.Cond)
812 n.Body.Prepend(o.cleanTempNoPop(t)...)
813 n.Else.Prepend(o.cleanTempNoPop(t)...)
815 orderBlock(&n.Body, o.free)
816 orderBlock(&n.Else, o.free)
817 o.out = append(o.out, n)
820 // n.Right is the expression being ranged over.
821 // order it, and then make a copy if we need one.
822 // We almost always do, to ensure that we don't
823 // see any value changes made during the loop.
824 // Usually the copy is cheap (e.g., array pointer,
825 // chan, slice, string are all tiny).
826 // The exception is ranging over an array value
827 // (not a slice, not a pointer to array),
828 // which must make a copy to avoid seeing updates made during
829 // the range body. Ranging over an array value is uncommon though.
831 // Mark []byte(str) range expression to reuse string backing storage.
832 // It is safe because the storage cannot be mutated.
833 n := n.(*ir.RangeStmt)
834 if n.X.Op() == ir.OSTR2BYTES {
835 n.X.(*ir.ConvExpr).SetOp(ir.OSTR2BYTESTMP)
839 n.X = o.expr(n.X, nil)
842 xt := typecheck.RangeExprType(n.X.Type())
845 base.Fatalf("order.stmt range %v", n.Type())
847 case types.TARRAY, types.TSLICE:
848 if n.Value == nil || ir.IsBlank(n.Value) {
849 // for i := range x will only use x once, to compute len(x).
850 // No need to copy it.
855 case types.TCHAN, types.TSTRING:
856 // chan, string, slice, array ranges use value multiple times.
860 if r.Type().IsString() && r.Type() != types.Types[types.TSTRING] {
861 r = ir.NewConvExpr(base.Pos, ir.OCONV, nil, r)
862 r.SetType(types.Types[types.TSTRING])
863 r = typecheck.Expr(r)
870 // Preserve the body of the map clear pattern so it can
871 // be detected during walk. The loop body will not be used
872 // when optimizing away the range loop to a runtime call.
877 // copy the map value in case it is a map literal.
878 // TODO(rsc): Make tmp = literal expressions reuse tmp.
879 // For maps tmp is just one word so it hardly matters.
883 // n.Prealloc is the temp for the iterator.
884 // MapIterType contains pointers and needs to be zeroed.
885 n.Prealloc = o.newTemp(reflectdata.MapIterType(xt), true)
887 n.Key = o.exprInPlace(n.Key)
888 n.Value = o.exprInPlace(n.Value)
890 orderBlock(&n.Body, o.free)
892 o.out = append(o.out, n)
896 n := n.(*ir.ReturnStmt)
897 o.exprList(n.Results)
898 o.out = append(o.out, n)
900 // Special: clean case temporaries in each block entry.
901 // Select must enter one of its blocks, so there is no
902 // need for a cleaning at the end.
903 // Doubly special: evaluation order for select is stricter
904 // than ordinary expressions. Even something like p.c
905 // has to be hoisted into a temporary, so that it cannot be
906 // reordered after the channel evaluation for a different
907 // case (if p were nil, then the timing of the fault would
910 n := n.(*ir.SelectStmt)
912 for _, ncas := range n.Cases {
916 // Append any new body prologue to ninit.
917 // The next loop will insert ninit into nbody.
918 if len(ncas.Init()) != 0 {
919 base.Fatalf("order select ninit")
926 ir.Dump("select case", r)
927 base.Fatalf("unknown op in select %v", r.Op())
931 r := r.(*ir.AssignListStmt)
932 recv := r.Rhs[0].(*ir.UnaryExpr)
933 recv.X = o.expr(recv.X, nil)
934 if !ir.IsAutoTmp(recv.X) {
935 recv.X = o.copyExpr(recv.X)
937 init := ir.TakeInit(r)
940 do := func(i int, t *types.Type) {
945 // If this is case x := <-ch or case x, y := <-ch, the case has
946 // the ODCL nodes to declare x and y. We want to delay that
947 // declaration (and possible allocation) until inside the case body.
948 // Delete the ODCL nodes here and recreate them inside the body below.
950 if len(init) > 0 && init[0].Op() == ir.ODCL && init[0].(*ir.Decl).X == n {
953 // iimport may have added a default initialization assignment,
954 // due to how it handles ODCL statements.
955 if len(init) > 0 && init[0].Op() == ir.OAS && init[0].(*ir.AssignStmt).X == n {
959 dcl := typecheck.Stmt(ir.NewDecl(base.Pos, ir.ODCL, n.(*ir.Name)))
960 ncas.PtrInit().Append(dcl)
962 tmp := o.newTemp(t, t.HasPointers())
963 as := typecheck.Stmt(ir.NewAssignStmt(base.Pos, n, typecheck.Conv(tmp, n.Type())))
964 ncas.PtrInit().Append(as)
967 do(0, recv.X.Type().Elem())
968 do(1, types.Types[types.TBOOL])
970 ir.DumpList("ninit", r.Init())
971 base.Fatalf("ninit on select recv")
973 orderBlock(ncas.PtrInit(), o.free)
976 r := r.(*ir.SendStmt)
977 if len(r.Init()) != 0 {
978 ir.DumpList("ninit", r.Init())
979 base.Fatalf("ninit on select send")
983 // r->left is c, r->right is x, both are always evaluated.
984 r.Chan = o.expr(r.Chan, nil)
986 if !ir.IsAutoTmp(r.Chan) {
987 r.Chan = o.copyExpr(r.Chan)
989 r.Value = o.expr(r.Value, nil)
990 if !ir.IsAutoTmp(r.Value) {
991 r.Value = o.copyExpr(r.Value)
995 // Now that we have accumulated all the temporaries, clean them.
996 // Also insert any ninit queued during the previous loop.
997 // (The temporary cleaning must follow that ninit work.)
998 for _, cas := range n.Cases {
999 orderBlock(&cas.Body, o.free)
1000 cas.Body.Prepend(o.cleanTempNoPop(t)...)
1002 // TODO(mdempsky): Is this actually necessary?
1003 // walkSelect appears to walk Ninit.
1004 cas.Body.Prepend(ir.TakeInit(cas)...)
1007 o.out = append(o.out, n)
1010 // Special: value being sent is passed as a pointer; make it addressable.
1012 n := n.(*ir.SendStmt)
1014 n.Chan = o.expr(n.Chan, nil)
1015 n.Value = o.expr(n.Value, nil)
1016 if base.Flag.Cfg.Instrumenting {
1017 // Force copying to the stack so that (chan T)(nil) <- x
1018 // is still instrumented as a read of x.
1019 n.Value = o.copyExpr(n.Value)
1021 n.Value = o.addrTemp(n.Value)
1023 o.out = append(o.out, n)
1026 // TODO(rsc): Clean temporaries more aggressively.
1027 // Note that because walkSwitch will rewrite some of the
1028 // switch into a binary search, this is not as easy as it looks.
1029 // (If we ran that code here we could invoke order.stmt on
1030 // the if-else chain instead.)
1031 // For now just clean all the temporaries at the end.
1032 // In practice that's fine.
1034 n := n.(*ir.SwitchStmt)
1035 if base.Debug.Libfuzzer != 0 && !hasDefaultCase(n) {
1036 // Add empty "default:" case for instrumentation.
1037 n.Cases = append(n.Cases, ir.NewCaseStmt(base.Pos, nil, nil))
1041 n.Tag = o.expr(n.Tag, nil)
1042 for _, ncas := range n.Cases {
1043 o.exprListInPlace(ncas.List)
1044 orderBlock(&ncas.Body, o.free)
1047 o.out = append(o.out, n)
1054 func hasDefaultCase(n *ir.SwitchStmt) bool {
1055 for _, ncas := range n.Cases {
1056 if len(ncas.List) == 0 {
1063 // exprList orders the expression list l into o.
1064 func (o *orderState) exprList(l ir.Nodes) {
1067 s[i] = o.expr(s[i], nil)
1071 // exprListInPlace orders the expression list l but saves
1072 // the side effects on the individual expression ninit lists.
1073 func (o *orderState) exprListInPlace(l ir.Nodes) {
1076 s[i] = o.exprInPlace(s[i])
1080 func (o *orderState) exprNoLHS(n ir.Node) ir.Node {
1081 return o.expr(n, nil)
1084 // expr orders a single expression, appending side
1085 // effects to o.out as needed.
1086 // If this is part of an assignment lhs = *np, lhs is given.
1087 // Otherwise lhs == nil. (When lhs != nil it may be possible
1088 // to avoid copying the result of the expression to a temporary.)
1089 // The result of expr MUST be assigned back to n, e.g.
1090 // n.Left = o.expr(n.Left, lhs)
1091 func (o *orderState) expr(n, lhs ir.Node) ir.Node {
1101 func (o *orderState) expr1(n, lhs ir.Node) ir.Node {
1107 o.edit = o.exprNoLHS // create closure once
1109 ir.EditChildren(n, o.edit)
1112 // Addition of strings turns into a function call.
1113 // Allocate a temporary to hold the strings.
1114 // Fewer than 5 strings use direct runtime helpers.
1116 n := n.(*ir.AddStringExpr)
1119 if len(n.List) > 5 {
1120 t := types.NewArray(types.Types[types.TSTRING], int64(len(n.List)))
1121 n.Prealloc = o.newTemp(t, false)
1124 // Mark string(byteSlice) arguments to reuse byteSlice backing
1125 // buffer during conversion. String concatenation does not
1126 // memorize the strings for later use, so it is safe.
1127 // However, we can do it only if there is at least one non-empty string literal.
1128 // Otherwise if all other arguments are empty strings,
1129 // concatstrings will return the reference to the temp string
1134 for _, n1 := range n.List {
1135 hasbyte = hasbyte || n1.Op() == ir.OBYTES2STR
1136 haslit = haslit || n1.Op() == ir.OLITERAL && len(ir.StringVal(n1)) != 0
1139 if haslit && hasbyte {
1140 for _, n2 := range n.List {
1141 if n2.Op() == ir.OBYTES2STR {
1142 n2 := n2.(*ir.ConvExpr)
1143 n2.SetOp(ir.OBYTES2STRTMP)
1150 n := n.(*ir.IndexExpr)
1151 n.X = o.expr(n.X, nil)
1152 n.Index = o.expr(n.Index, nil)
1156 // Enforce that any []byte slices we are not copying
1157 // can not be changed before the map index by forcing
1158 // the map index to happen immediately following the
1159 // conversions. See copyExpr a few lines below.
1160 needCopy = mapKeyReplaceStrConv(n.Index)
1162 if base.Flag.Cfg.Instrumenting {
1163 // Race detector needs the copy.
1168 // key must be addressable
1169 n.Index = o.mapKeyTemp(n.X.Type(), n.Index)
1171 return o.copyExpr(n)
1175 // concrete type (not interface) argument might need an addressable
1176 // temporary to pass to the runtime conversion routine.
1177 case ir.OCONVIFACE, ir.OCONVIDATA:
1178 n := n.(*ir.ConvExpr)
1179 n.X = o.expr(n.X, nil)
1180 if n.X.Type().IsInterface() {
1183 if _, _, needsaddr := dataWordFuncName(n.X.Type()); needsaddr || isStaticCompositeLiteral(n.X) {
1184 // Need a temp if we need to pass the address to the conversion function.
1185 // We also process static composite literal node here, making a named static global
1186 // whose address we can put directly in an interface (see OCONVIFACE/OCONVIDATA case in walk).
1187 n.X = o.addrTemp(n.X)
1192 n := n.(*ir.ConvExpr)
1193 if n.X.Op() == ir.OCALLMETH {
1194 base.FatalfAt(n.X.Pos(), "OCALLMETH missed by typecheck")
1196 if n.Type().IsKind(types.TUNSAFEPTR) && n.X.Type().IsKind(types.TUINTPTR) && (n.X.Op() == ir.OCALLFUNC || n.X.Op() == ir.OCALLINTER) {
1197 call := n.X.(*ir.CallExpr)
1198 // When reordering unsafe.Pointer(f()) into a separate
1199 // statement, the conversion and function call must stay
1200 // together. See golang.org/issue/15329.
1203 if lhs == nil || lhs.Op() != ir.ONAME || base.Flag.Cfg.Instrumenting {
1204 return o.copyExpr(n)
1207 n.X = o.expr(n.X, nil)
1211 case ir.OANDAND, ir.OOROR:
1216 // if r { // or !r, for OROR
1221 n := n.(*ir.LogicalExpr)
1222 r := o.newTemp(n.Type(), false)
1224 // Evaluate left-hand side.
1225 lhs := o.expr(n.X, nil)
1226 o.out = append(o.out, typecheck.Stmt(ir.NewAssignStmt(base.Pos, r, lhs)))
1228 // Evaluate right-hand side, save generated code.
1233 rhs := o.expr(n.Y, nil)
1234 o.out = append(o.out, typecheck.Stmt(ir.NewAssignStmt(base.Pos, r, rhs)))
1239 // If left-hand side doesn't cause a short-circuit, issue right-hand side.
1240 nif := ir.NewIfStmt(base.Pos, r, nil, nil)
1241 if n.Op() == ir.OANDAND {
1246 o.out = append(o.out, nif)
1250 base.FatalfAt(n.Pos(), "OCALLMETH missed by typecheck")
1251 panic("unreachable")
1272 // len([]rune(s)) is rewritten to runtime.countrunes(s) later.
1273 conv := n.(*ir.UnaryExpr).X.(*ir.ConvExpr)
1274 conv.X = o.expr(conv.X, nil)
1279 if lhs == nil || lhs.Op() != ir.ONAME || base.Flag.Cfg.Instrumenting {
1280 return o.copyExpr(n)
1285 n := n.(*ir.InlinedCallExpr)
1287 return n.SingleResult()
1290 // Check for append(x, make([]T, y)...) .
1291 n := n.(*ir.CallExpr)
1292 if isAppendOfMake(n) {
1293 n.Args[0] = o.expr(n.Args[0], nil) // order x
1294 mk := n.Args[1].(*ir.MakeExpr)
1295 mk.Len = o.expr(mk.Len, nil) // order y
1300 if lhs == nil || lhs.Op() != ir.ONAME && !ir.SameSafeExpr(lhs, n.Args[0]) {
1301 return o.copyExpr(n)
1305 case ir.OSLICE, ir.OSLICEARR, ir.OSLICESTR, ir.OSLICE3, ir.OSLICE3ARR:
1306 n := n.(*ir.SliceExpr)
1307 n.X = o.expr(n.X, nil)
1308 n.Low = o.cheapExpr(o.expr(n.Low, nil))
1309 n.High = o.cheapExpr(o.expr(n.High, nil))
1310 n.Max = o.cheapExpr(o.expr(n.Max, nil))
1311 if lhs == nil || lhs.Op() != ir.ONAME && !ir.SameSafeExpr(lhs, n.X) {
1312 return o.copyExpr(n)
1317 n := n.(*ir.ClosureExpr)
1318 if n.Transient() && len(n.Func.ClosureVars) > 0 {
1319 n.Prealloc = o.newTemp(typecheck.ClosureType(n), false)
1324 n := n.(*ir.SelectorExpr)
1325 n.X = o.expr(n.X, nil)
1327 t := typecheck.MethodValueType(n)
1328 n.Prealloc = o.newTemp(t, false)
1333 n := n.(*ir.CompLitExpr)
1336 t := types.NewArray(n.Type().Elem(), n.Len)
1337 n.Prealloc = o.newTemp(t, false)
1341 case ir.ODOTTYPE, ir.ODOTTYPE2:
1342 n := n.(*ir.TypeAssertExpr)
1343 n.X = o.expr(n.X, nil)
1344 if !types.IsDirectIface(n.Type()) || base.Flag.Cfg.Instrumenting {
1345 return o.copyExprClear(n)
1350 n := n.(*ir.UnaryExpr)
1351 n.X = o.expr(n.X, nil)
1352 return o.copyExprClear(n)
1354 case ir.OEQ, ir.ONE, ir.OLT, ir.OLE, ir.OGT, ir.OGE:
1355 n := n.(*ir.BinaryExpr)
1356 n.X = o.expr(n.X, nil)
1357 n.Y = o.expr(n.Y, nil)
1362 // Mark string(byteSlice) arguments to reuse byteSlice backing
1363 // buffer during conversion. String comparison does not
1364 // memorize the strings for later use, so it is safe.
1365 if n.X.Op() == ir.OBYTES2STR {
1366 n.X.(*ir.ConvExpr).SetOp(ir.OBYTES2STRTMP)
1368 if n.Y.Op() == ir.OBYTES2STR {
1369 n.Y.(*ir.ConvExpr).SetOp(ir.OBYTES2STRTMP)
1372 case t.IsStruct() || t.IsArray():
1373 // for complex comparisons, we need both args to be
1374 // addressable so we can pass them to the runtime.
1375 n.X = o.addrTemp(n.X)
1376 n.Y = o.addrTemp(n.Y)
1381 // Order map by converting:
1388 // m := map[int]int{}
1392 // Then order the result.
1393 // Without this special case, order would otherwise compute all
1394 // the keys and values before storing any of them to the map.
1396 n := n.(*ir.CompLitExpr)
1398 statics := entries[:0]
1399 var dynamics []*ir.KeyExpr
1400 for _, r := range entries {
1401 r := r.(*ir.KeyExpr)
1403 if !isStaticCompositeLiteral(r.Key) || !isStaticCompositeLiteral(r.Value) {
1404 dynamics = append(dynamics, r)
1408 // Recursively ordering some static entries can change them to dynamic;
1409 // e.g., OCONVIFACE nodes. See #31777.
1410 r = o.expr(r, nil).(*ir.KeyExpr)
1411 if !isStaticCompositeLiteral(r.Key) || !isStaticCompositeLiteral(r.Value) {
1412 dynamics = append(dynamics, r)
1416 statics = append(statics, r)
1420 if len(dynamics) == 0 {
1424 // Emit the creation of the map (with all its static entries).
1425 m := o.newTemp(n.Type(), false)
1426 as := ir.NewAssignStmt(base.Pos, m, n)
1430 // Emit eval+insert of dynamic entries, one at a time.
1431 for _, r := range dynamics {
1432 as := ir.NewAssignStmt(base.Pos, ir.NewIndexExpr(base.Pos, m, r.Key), r.Value)
1433 typecheck.Stmt(as) // Note: this converts the OINDEX to an OINDEXMAP
1439 // No return - type-assertions above. Each case must return for itself.
1442 // as2func orders OAS2FUNC nodes. It creates temporaries to ensure left-to-right assignment.
1443 // The caller should order the right-hand side of the assignment before calling order.as2func.
1447 // tmp1, tmp2, tmp3 = ...
1448 // a, b, a = tmp1, tmp2, tmp3
1449 // This is necessary to ensure left to right assignment order.
1450 func (o *orderState) as2func(n *ir.AssignListStmt) {
1451 results := n.Rhs[0].Type()
1452 as := ir.NewAssignListStmt(n.Pos(), ir.OAS2, nil, nil)
1453 for i, nl := range n.Lhs {
1454 if !ir.IsBlank(nl) {
1455 typ := results.Field(i).Type
1456 tmp := o.newTemp(typ, typ.HasPointers())
1458 as.Lhs = append(as.Lhs, nl)
1459 as.Rhs = append(as.Rhs, tmp)
1463 o.out = append(o.out, n)
1464 o.stmt(typecheck.Stmt(as))
1467 // as2ok orders OAS2XXX with ok.
1468 // Just like as2func, this also adds temporaries to ensure left-to-right assignment.
1469 func (o *orderState) as2ok(n *ir.AssignListStmt) {
1470 as := ir.NewAssignListStmt(n.Pos(), ir.OAS2, nil, nil)
1472 do := func(i int, typ *types.Type) {
1473 if nl := n.Lhs[i]; !ir.IsBlank(nl) {
1474 var tmp ir.Node = o.newTemp(typ, typ.HasPointers())
1476 as.Lhs = append(as.Lhs, nl)
1478 // The "ok" result is an untyped boolean according to the Go
1479 // spec. We need to explicitly convert it to the LHS type in
1480 // case the latter is a defined boolean type (#8475).
1481 tmp = typecheck.Conv(tmp, nl.Type())
1483 as.Rhs = append(as.Rhs, tmp)
1487 do(0, n.Rhs[0].Type())
1488 do(1, types.Types[types.TBOOL])
1490 o.out = append(o.out, n)
1491 o.stmt(typecheck.Stmt(as))
1494 // isFuncPCIntrinsic returns whether n is a direct call of internal/abi.FuncPCABIxxx functions.
1495 func isFuncPCIntrinsic(n *ir.CallExpr) bool {
1496 if n.Op() != ir.OCALLFUNC || n.X.Op() != ir.ONAME {
1499 fn := n.X.(*ir.Name).Sym()
1500 return (fn.Name == "FuncPCABI0" || fn.Name == "FuncPCABIInternal") &&
1501 (fn.Pkg.Path == "internal/abi" || fn.Pkg == types.LocalPkg && base.Ctxt.Pkgpath == "internal/abi")
1504 // isIfaceOfFunc returns whether n is an interface conversion from a direct reference of a func.
1505 func isIfaceOfFunc(n ir.Node) bool {
1506 return n.Op() == ir.OCONVIFACE && n.(*ir.ConvExpr).X.Op() == ir.ONAME && n.(*ir.ConvExpr).X.(*ir.Name).Class == ir.PFUNC