]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/ir: export 'reassigned', handle OASOP
authorThan McIntosh <thanm@google.com>
Fri, 30 Jun 2023 20:06:10 +0000 (16:06 -0400)
committerThan McIntosh <thanm@google.com>
Thu, 10 Aug 2023 18:53:18 +0000 (18:53 +0000)
Rename the ir-local function "reassigned" to "Reassigned" so that it
can be used as part of inline heuristic analysis. Fix up the header
comment along that way, which had some stale material. Add support for
detecting reassignments via OASOP (as opposed to just simple
assignments).

Updates #61502.

Change-Id: I50f40f81263c0d7f61f30fcf0258f0b0f93acdca
Reviewed-on: https://go-review.googlesource.com/c/go/+/511560
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Than McIntosh <thanm@google.com>

src/cmd/compile/internal/ir/expr.go

index 5355edc17691cdc38e1753a788328239233c0a42..02b1733f0402940a930b10a41305d540a2da0ab5 100644 (file)
@@ -906,20 +906,20 @@ FindRHS:
                base.Fatalf("RHS is nil: %v", defn)
        }
 
-       if reassigned(n) {
+       if Reassigned(n) {
                return nil
        }
 
        return rhs
 }
 
-// reassigned takes an ONAME node, walks the function in which it is defined, and returns a boolean
-// indicating whether the name has any assignments other than its declaration.
-// The second return value is the first such assignment encountered in the walk, if any. It is mostly
-// useful for -m output documenting the reason for inhibited optimizations.
+// Reassigned takes an ONAME node, walks the function in which it is
+// defined, and returns a boolean indicating whether the name has any
+// assignments other than its declaration.
 // NB: global variables are always considered to be re-assigned.
-// TODO: handle initial declaration not including an assignment and followed by a single assignment?
-func reassigned(name *Name) bool {
+// TODO: handle initial declaration not including an assignment and
+// followed by a single assignment?
+func Reassigned(name *Name) bool {
        if name.Op() != ONAME {
                base.Fatalf("reassigned %v", name)
        }
@@ -934,7 +934,10 @@ func reassigned(name *Name) bool {
 
        // isName reports whether n is a reference to name.
        isName := func(x Node) bool {
-               n, ok := x.(*Name)
+               if x == nil {
+                       return false
+               }
+               n, ok := OuterValue(x).(*Name)
                return ok && n.Canonical() == name
        }
 
@@ -953,9 +956,14 @@ func reassigned(name *Name) bool {
                                        return true
                                }
                        }
+               case OASOP:
+                       n := n.(*AssignOpStmt)
+                       if isName(n.X) {
+                               return true
+                       }
                case OADDR:
                        n := n.(*AddrExpr)
-                       if isName(OuterValue(n.X)) {
+                       if isName(n.X) {
                                return true
                        }
                case ORANGE: