File size: 13,567 Bytes
fc11197 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | // Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssa
import (
"cmd/compile/internal/types"
"cmp"
"slices"
)
// decompose converts phi ops on compound builtin types into phi
// ops on simple types, then invokes rewrite rules to decompose
// other ops on those types.
func decomposeBuiltin(f *Func) {
// Decompose phis
for _, b := range f.Blocks {
for _, v := range b.Values {
if v.Op != OpPhi {
continue
}
decomposeBuiltinPhi(v)
}
}
// Decompose other values
// Note: Leave dead values because we need to keep the original
// values around so the name component resolution below can still work.
applyRewrite(f, rewriteBlockdec, rewriteValuedec, leaveDeadValues)
if f.Config.RegSize == 4 {
applyRewrite(f, rewriteBlockdec64, rewriteValuedec64, leaveDeadValues)
}
// Split up named values into their components.
// accumulate old names for aggregates (that are decomposed) in toDelete for efficient bulk deletion,
// accumulate new LocalSlots in newNames for addition after the iteration. This decomposition is for
// builtin types with leaf components, and thus there is no need to reprocess the newly create LocalSlots.
var toDelete []namedVal
var newNames []*LocalSlot
for i, name := range f.Names {
t := name.Type
switch {
case t.IsInteger() && t.Size() > f.Config.RegSize:
hiName, loName := f.SplitInt64(name)
newNames = maybeAppend2(f, newNames, hiName, loName)
for j, v := range f.NamedValues[*name] {
if v.Op != OpInt64Make {
continue
}
f.NamedValues[*hiName] = append(f.NamedValues[*hiName], v.Args[0])
f.NamedValues[*loName] = append(f.NamedValues[*loName], v.Args[1])
toDelete = append(toDelete, namedVal{i, j})
}
case t.IsComplex():
rName, iName := f.SplitComplex(name)
newNames = maybeAppend2(f, newNames, rName, iName)
for j, v := range f.NamedValues[*name] {
if v.Op != OpComplexMake {
continue
}
f.NamedValues[*rName] = append(f.NamedValues[*rName], v.Args[0])
f.NamedValues[*iName] = append(f.NamedValues[*iName], v.Args[1])
toDelete = append(toDelete, namedVal{i, j})
}
case t.IsString():
ptrName, lenName := f.SplitString(name)
newNames = maybeAppend2(f, newNames, ptrName, lenName)
for j, v := range f.NamedValues[*name] {
if v.Op != OpStringMake {
continue
}
f.NamedValues[*ptrName] = append(f.NamedValues[*ptrName], v.Args[0])
f.NamedValues[*lenName] = append(f.NamedValues[*lenName], v.Args[1])
toDelete = append(toDelete, namedVal{i, j})
}
case t.IsSlice():
ptrName, lenName, capName := f.SplitSlice(name)
newNames = maybeAppend2(f, newNames, ptrName, lenName)
newNames = maybeAppend(f, newNames, capName)
for j, v := range f.NamedValues[*name] {
if v.Op != OpSliceMake {
continue
}
f.NamedValues[*ptrName] = append(f.NamedValues[*ptrName], v.Args[0])
f.NamedValues[*lenName] = append(f.NamedValues[*lenName], v.Args[1])
f.NamedValues[*capName] = append(f.NamedValues[*capName], v.Args[2])
toDelete = append(toDelete, namedVal{i, j})
}
case t.IsInterface():
typeName, dataName := f.SplitInterface(name)
newNames = maybeAppend2(f, newNames, typeName, dataName)
for j, v := range f.NamedValues[*name] {
if v.Op != OpIMake {
continue
}
f.NamedValues[*typeName] = append(f.NamedValues[*typeName], v.Args[0])
f.NamedValues[*dataName] = append(f.NamedValues[*dataName], v.Args[1])
toDelete = append(toDelete, namedVal{i, j})
}
case t.IsFloat():
// floats are never decomposed, even ones bigger than RegSize
case t.Size() > f.Config.RegSize && !t.IsSIMD():
f.Fatalf("undecomposed named type %s %v", name, t)
}
}
deleteNamedVals(f, toDelete)
f.Names = append(f.Names, newNames...)
}
func maybeAppend(f *Func, ss []*LocalSlot, s *LocalSlot) []*LocalSlot {
if _, ok := f.NamedValues[*s]; !ok {
f.NamedValues[*s] = nil
return append(ss, s)
}
return ss
}
func maybeAppend2(f *Func, ss []*LocalSlot, s1, s2 *LocalSlot) []*LocalSlot {
return maybeAppend(f, maybeAppend(f, ss, s1), s2)
}
func decomposeBuiltinPhi(v *Value) {
switch {
case v.Type.IsInteger() && v.Type.Size() > v.Block.Func.Config.RegSize:
decomposeInt64Phi(v)
case v.Type.IsComplex():
decomposeComplexPhi(v)
case v.Type.IsString():
decomposeStringPhi(v)
case v.Type.IsSlice():
decomposeSlicePhi(v)
case v.Type.IsInterface():
decomposeInterfacePhi(v)
case v.Type.IsFloat():
// floats are never decomposed, even ones bigger than RegSize
case v.Type.Size() > v.Block.Func.Config.RegSize && !v.Type.IsSIMD():
v.Fatalf("%v undecomposed type %v", v, v.Type)
}
}
func decomposeStringPhi(v *Value) {
types := &v.Block.Func.Config.Types
ptrType := types.BytePtr
lenType := types.Int
ptr := v.Block.NewValue0(v.Pos, OpPhi, ptrType)
len := v.Block.NewValue0(v.Pos, OpPhi, lenType)
for _, a := range v.Args {
ptr.AddArg(a.Block.NewValue1(v.Pos, OpStringPtr, ptrType, a))
len.AddArg(a.Block.NewValue1(v.Pos, OpStringLen, lenType, a))
}
v.reset(OpStringMake)
v.AddArg(ptr)
v.AddArg(len)
}
func decomposeSlicePhi(v *Value) {
types := &v.Block.Func.Config.Types
ptrType := v.Type.Elem().PtrTo()
lenType := types.Int
ptr := v.Block.NewValue0(v.Pos, OpPhi, ptrType)
len := v.Block.NewValue0(v.Pos, OpPhi, lenType)
cap := v.Block.NewValue0(v.Pos, OpPhi, lenType)
for _, a := range v.Args {
ptr.AddArg(a.Block.NewValue1(v.Pos, OpSlicePtr, ptrType, a))
len.AddArg(a.Block.NewValue1(v.Pos, OpSliceLen, lenType, a))
cap.AddArg(a.Block.NewValue1(v.Pos, OpSliceCap, lenType, a))
}
v.reset(OpSliceMake)
v.AddArg(ptr)
v.AddArg(len)
v.AddArg(cap)
}
func decomposeInt64Phi(v *Value) {
cfgtypes := &v.Block.Func.Config.Types
var partType *types.Type
if v.Type.IsSigned() {
partType = cfgtypes.Int32
} else {
partType = cfgtypes.UInt32
}
hi := v.Block.NewValue0(v.Pos, OpPhi, partType)
lo := v.Block.NewValue0(v.Pos, OpPhi, cfgtypes.UInt32)
for _, a := range v.Args {
hi.AddArg(a.Block.NewValue1(v.Pos, OpInt64Hi, partType, a))
lo.AddArg(a.Block.NewValue1(v.Pos, OpInt64Lo, cfgtypes.UInt32, a))
}
v.reset(OpInt64Make)
v.AddArg(hi)
v.AddArg(lo)
}
func decomposeComplexPhi(v *Value) {
cfgtypes := &v.Block.Func.Config.Types
var partType *types.Type
switch z := v.Type.Size(); z {
case 8:
partType = cfgtypes.Float32
case 16:
partType = cfgtypes.Float64
default:
v.Fatalf("decomposeComplexPhi: bad complex size %d", z)
}
real := v.Block.NewValue0(v.Pos, OpPhi, partType)
imag := v.Block.NewValue0(v.Pos, OpPhi, partType)
for _, a := range v.Args {
real.AddArg(a.Block.NewValue1(v.Pos, OpComplexReal, partType, a))
imag.AddArg(a.Block.NewValue1(v.Pos, OpComplexImag, partType, a))
}
v.reset(OpComplexMake)
v.AddArg(real)
v.AddArg(imag)
}
func decomposeInterfacePhi(v *Value) {
uintptrType := v.Block.Func.Config.Types.Uintptr
ptrType := v.Block.Func.Config.Types.BytePtr
itab := v.Block.NewValue0(v.Pos, OpPhi, uintptrType)
data := v.Block.NewValue0(v.Pos, OpPhi, ptrType)
for _, a := range v.Args {
itab.AddArg(a.Block.NewValue1(v.Pos, OpITab, uintptrType, a))
data.AddArg(a.Block.NewValue1(v.Pos, OpIData, ptrType, a))
}
v.reset(OpIMake)
v.AddArg(itab)
v.AddArg(data)
}
func decomposeUser(f *Func) {
for _, b := range f.Blocks {
for _, v := range b.Values {
if v.Op != OpPhi {
continue
}
decomposeUserPhi(v)
}
}
// Split up named values into their components.
i := 0
var newNames []*LocalSlot
for _, name := range f.Names {
t := name.Type
switch {
case isStructNotSIMD(t):
newNames = decomposeUserStructInto(f, name, newNames)
case t.IsArray():
newNames = decomposeUserArrayInto(f, name, newNames)
default:
f.Names[i] = name
i++
}
}
f.Names = f.Names[:i]
f.Names = append(f.Names, newNames...)
}
// decomposeUserArrayInto creates names for the element(s) of arrays referenced
// by name where possible, and appends those new names to slots, which is then
// returned.
func decomposeUserArrayInto(f *Func, name *LocalSlot, slots []*LocalSlot) []*LocalSlot {
t := name.Type
if t.Size() == 0 {
// TODO(khr): Not sure what to do here. Probably nothing.
// Names for empty arrays aren't important.
return slots
}
if t.NumElem() != 1 {
// shouldn't get here due to CanSSA
f.Fatalf("array not of size 1")
}
elemName := f.SplitArray(name)
var keep []*Value
for _, v := range f.NamedValues[*name] {
if v.Op != OpArrayMake1 {
keep = append(keep, v)
continue
}
f.NamedValues[*elemName] = append(f.NamedValues[*elemName], v.Args[0])
}
if len(keep) == 0 {
// delete the name for the array as a whole
delete(f.NamedValues, *name)
} else {
f.NamedValues[*name] = keep
}
if t.Elem().IsArray() {
return decomposeUserArrayInto(f, elemName, slots)
} else if isStructNotSIMD(t.Elem()) {
return decomposeUserStructInto(f, elemName, slots)
}
return append(slots, elemName)
}
// decomposeUserStructInto creates names for the fields(s) of structs referenced
// by name where possible, and appends those new names to slots, which is then
// returned.
func decomposeUserStructInto(f *Func, name *LocalSlot, slots []*LocalSlot) []*LocalSlot {
fnames := []*LocalSlot{} // slots for struct in name
t := name.Type
n := t.NumFields()
for i := 0; i < n; i++ {
fs := f.SplitStruct(name, i)
fnames = append(fnames, fs)
// arrays and structs will be decomposed further, so
// there's no need to record a name
if !fs.Type.IsArray() && !isStructNotSIMD(fs.Type) {
slots = maybeAppend(f, slots, fs)
}
}
var keep []*Value
// create named values for each struct field
for _, v := range f.NamedValues[*name] {
if v.Op != OpStructMake || len(v.Args) != n {
keep = append(keep, v)
continue
}
for i := 0; i < len(fnames); i++ {
f.NamedValues[*fnames[i]] = append(f.NamedValues[*fnames[i]], v.Args[i])
}
}
if len(keep) == 0 {
// delete the name for the struct as a whole
delete(f.NamedValues, *name)
} else {
f.NamedValues[*name] = keep
}
// now that this f.NamedValues contains values for the struct
// fields, recurse into nested structs
for i := 0; i < n; i++ {
if isStructNotSIMD(name.Type.FieldType(i)) {
slots = decomposeUserStructInto(f, fnames[i], slots)
delete(f.NamedValues, *fnames[i])
} else if name.Type.FieldType(i).IsArray() {
slots = decomposeUserArrayInto(f, fnames[i], slots)
delete(f.NamedValues, *fnames[i])
}
}
return slots
}
func decomposeUserPhi(v *Value) {
switch {
case isStructNotSIMD(v.Type):
decomposeStructPhi(v)
case v.Type.IsArray():
decomposeArrayPhi(v)
}
}
// decomposeStructPhi replaces phi-of-struct with structmake(phi-for-each-field),
// and then recursively decomposes the phis for each field.
func decomposeStructPhi(v *Value) {
t := v.Type
if t.Size() == 0 {
v.reset(OpEmpty)
return
}
n := t.NumFields()
fields := make([]*Value, 0, MaxStruct)
for i := 0; i < n; i++ {
fields = append(fields, v.Block.NewValue0(v.Pos, OpPhi, t.FieldType(i)))
}
for _, a := range v.Args {
for i := 0; i < n; i++ {
fields[i].AddArg(a.Block.NewValue1I(v.Pos, OpStructSelect, t.FieldType(i), int64(i), a))
}
}
v.reset(OpStructMake)
v.AddArgs(fields...)
// Recursively decompose phis for each field.
for _, f := range fields {
decomposeUserPhi(f)
}
}
// decomposeArrayPhi replaces phi-of-array with arraymake(phi-of-array-element),
// and then recursively decomposes the element phi.
func decomposeArrayPhi(v *Value) {
t := v.Type
if t.Size() == 0 {
v.reset(OpEmpty)
return
}
if t.NumElem() != 1 {
v.Fatalf("SSAable array must have no more than 1 element")
}
elem := v.Block.NewValue0(v.Pos, OpPhi, t.Elem())
for _, a := range v.Args {
elem.AddArg(a.Block.NewValue1I(v.Pos, OpArraySelect, t.Elem(), 0, a))
}
v.reset(OpArrayMake1)
v.AddArg(elem)
// Recursively decompose elem phi.
decomposeUserPhi(elem)
}
// MaxStruct is the maximum number of fields a struct
// can have and still be SSAable.
const MaxStruct = 4
type namedVal struct {
locIndex, valIndex int // f.NamedValues[f.Names[locIndex]][valIndex] = key
}
// deleteNamedVals removes particular values with debugger names from f's naming data structures,
// removes all values with OpInvalid, and re-sorts the list of Names.
func deleteNamedVals(f *Func, toDelete []namedVal) {
// Arrange to delete from larger indices to smaller, to ensure swap-with-end deletion does not invalidate pending indices.
slices.SortFunc(toDelete, func(a, b namedVal) int {
if a.locIndex != b.locIndex {
return cmp.Compare(b.locIndex, a.locIndex)
}
return cmp.Compare(b.valIndex, a.valIndex)
})
// Get rid of obsolete names
for _, d := range toDelete {
loc := f.Names[d.locIndex]
vals := f.NamedValues[*loc]
l := len(vals) - 1
if l > 0 {
vals[d.valIndex] = vals[l]
}
vals[l] = nil
f.NamedValues[*loc] = vals[:l]
}
// Delete locations with no values attached.
end := len(f.Names)
for i := len(f.Names) - 1; i >= 0; i-- {
loc := f.Names[i]
vals := f.NamedValues[*loc]
last := len(vals)
for j := len(vals) - 1; j >= 0; j-- {
if vals[j].Op == OpInvalid {
last--
vals[j] = vals[last]
vals[last] = nil
}
}
if last < len(vals) {
f.NamedValues[*loc] = vals[:last]
}
if len(vals) == 0 {
delete(f.NamedValues, *loc)
end--
f.Names[i] = f.Names[end]
f.Names[end] = nil
}
}
f.Names = f.Names[:end]
}
func isStructNotSIMD(t *types.Type) bool {
return t.IsStruct() && !t.IsSIMD()
}
|