1
0
mirror of https://github.com/golang/go synced 2024-11-18 20:04:52 -07:00

cmd/compile: use CTNIL for pointer-typed OLITERALs

We used to be more aggressive about constant folding in the frontend,
handling expressions that the Go spec does not consider constant;
e.g., "(*int)(unsafe.Pointer(uintptr(200)))". However, that led to a
lot of subtle Go spec conformance issues, so we've since abandoned
that effort (CL 151320), leaving SSA to handle these cases instead.

As such, the only time we now end up with pointer-typed OLITERALs is
when "nil" is implicitly converted to a pointer-typed variable.
Instead of representing these OLITERALs with an CTINT of 0, we can
just use CTNIL.

Saves a few bytes of memory and lines of code.

Change-Id: Ibc5c756b992fdc89c3bdaf4fda3aa352e8e2b101
Reviewed-on: https://go-review.googlesource.com/c/go/+/193437
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2019-09-04 17:45:50 -07:00
parent e6ba19f913
commit ad1f2c9618
3 changed files with 7 additions and 29 deletions

View File

@ -349,10 +349,7 @@ func convlit1(n *Node, t *types.Type, explicit bool, reuse canReuseNode) *Node {
case TARRAY:
goto bad
case TPTR, TUNSAFEPTR:
n.SetVal(Val{new(Mpint)})
case TCHAN, TFUNC, TINTER, TMAP, TSLICE:
case TCHAN, TFUNC, TINTER, TMAP, TPTR, TSLICE, TUNSAFEPTR:
break
}
@ -602,16 +599,7 @@ func evconst(n *Node) {
case OEQ, ONE, OLT, OLE, OGT, OGE:
if nl.Op == OLITERAL && nr.Op == OLITERAL {
if nl.Type.IsInterface() != nr.Type.IsInterface() {
// Mixed interface/non-interface
// constant comparison means comparing
// nil interface with some typed
// constant, which is always unequal.
// E.g., interface{}(nil) == (*int)(nil).
setboolconst(n, op == ONE)
} else {
setboolconst(n, compareOp(nl.Val(), op, nr.Val()))
}
setboolconst(n, compareOp(nl.Val(), op, nr.Val()))
}
case OLSH, ORSH:
@ -732,15 +720,6 @@ func compareOp(x Val, op Op, y Val) bool {
x, y = match(x, y)
switch x.Ctype() {
case CTNIL:
_, _ = x.U.(*NilVal), y.U.(*NilVal) // assert dynamic types match
switch op {
case OEQ:
return true
case ONE:
return false
}
case CTBOOL:
x, y := x.U.(bool), y.U.(bool)
switch op {

View File

@ -734,15 +734,14 @@ func constTypeOf(typ *types.Type) Ctype {
}
switch typ.Etype {
case TCHAN, TFUNC, TMAP, TNIL, TINTER, TSLICE:
case TCHAN, TFUNC, TMAP, TNIL, TINTER, TPTR, TSLICE, TUNSAFEPTR:
return CTNIL
case TBOOL:
return CTBOOL
case TSTRING:
return CTSTR
case TINT, TINT8, TINT16, TINT32, TINT64,
TUINT, TUINT8, TUINT16, TUINT32, TUINT64, TUINTPTR,
TPTR, TUNSAFEPTR:
TUINT, TUINT8, TUINT16, TUINT32, TUINT64, TUINTPTR:
return CTINT
case TFLOAT32, TFLOAT64:
return CTFLT

View File

@ -509,8 +509,8 @@ func IsGlobalAddr(v *Value) bool {
if v.Op == OpAddr && v.Args[0].Op == OpSB {
return true // address of a global
}
if v.Op == OpConst64 || v.Op == OpConst32 {
return true // nil, the only possible pointer constant
if v.Op == OpConstNil {
return true
}
return false
}
@ -520,7 +520,7 @@ func IsReadOnlyGlobalAddr(v *Value) bool {
if !IsGlobalAddr(v) {
return false
}
if v.Op == OpConst64 || v.Op == OpConst32 {
if v.Op == OpConstNil {
// Nil pointers are read only. See issue 33438.
return true
}