1 // Copyright 2013 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. 4 5 // Garbage collector liveness bitmap generation. 6 7 // The command line flag -live causes this code to print debug information. 8 // The levels are: 9 // 10 // -live (aka -live=1): print liveness lists as code warnings at safe points 11 // -live=2: print an assembly listing with liveness annotations 12 // 13 // Each level includes the earlier output as well. 14 15 package gc 16 17 import ( 18 "cmd/compile/internal/ssa" 19 "cmd/compile/internal/types" 20 "cmd/internal/obj" 21 "cmd/internal/objabi" 22 "cmd/internal/src" 23 "crypto/md5" 24 "crypto/sha1" 25 "fmt" 26 "os" 27 "strings" 28 ) 29 30 // TODO(mdempsky): Update to reference OpVar{Def,Kill,Live} instead. 31 32 // VARDEF is an annotation for the liveness analysis, marking a place 33 // where a complete initialization (definition) of a variable begins. 34 // Since the liveness analysis can see initialization of single-word 35 // variables quite easy, gvardef is usually only called for multi-word 36 // or 'fat' variables, those satisfying isfat(n->type). 37 // However, gvardef is also called when a non-fat variable is initialized 38 // via a block move; the only time this happens is when you have 39 // return f() 40 // for a function with multiple return values exactly matching the return 41 // types of the current function. 42 // 43 // A 'VARDEF x' annotation in the instruction stream tells the liveness 44 // analysis to behave as though the variable x is being initialized at that 45 // point in the instruction stream. The VARDEF must appear before the 46 // actual (multi-instruction) initialization, and it must also appear after 47 // any uses of the previous value, if any. For example, if compiling: 48 // 49 // x = x[1:] 50 // 51 // it is important to generate code like: 52 // 53 // base, len, cap = pieces of x[1:] 54 // VARDEF x 55 // x = {base, len, cap} 56 // 57 // If instead the generated code looked like: 58 // 59 // VARDEF x 60 // base, len, cap = pieces of x[1:] 61 // x = {base, len, cap} 62 // 63 // then the liveness analysis would decide the previous value of x was 64 // unnecessary even though it is about to be used by the x[1:] computation. 65 // Similarly, if the generated code looked like: 66 // 67 // base, len, cap = pieces of x[1:] 68 // x = {base, len, cap} 69 // VARDEF x 70 // 71 // then the liveness analysis will not preserve the new value of x, because 72 // the VARDEF appears to have "overwritten" it. 73 // 74 // VARDEF is a bit of a kludge to work around the fact that the instruction 75 // stream is working on single-word values but the liveness analysis 76 // wants to work on individual variables, which might be multi-word 77 // aggregates. It might make sense at some point to look into letting 78 // the liveness analysis work on single-word values as well, although 79 // there are complications around interface values, slices, and strings, 80 // all of which cannot be treated as individual words. 81 // 82 // VARKILL is the opposite of VARDEF: it marks a value as no longer needed, 83 // even if its address has been taken. That is, a VARKILL annotation asserts 84 // that its argument is certainly dead, for use when the liveness analysis 85 // would not otherwise be able to deduce that fact. 86 87 // BlockEffects summarizes the liveness effects on an SSA block. 88 type BlockEffects struct { 89 lastbitmapindex int // for livenessepilogue 90 91 // Computed during livenessprologue using only the content of 92 // individual blocks: 93 // 94 // uevar: upward exposed variables (used before set in block) 95 // varkill: killed variables (set in block) 96 // avarinit: addrtaken variables set or used (proof of initialization) 97 uevar bvec 98 varkill bvec 99 avarinit bvec 100 101 // Computed during livenesssolve using control flow information: 102 // 103 // livein: variables live at block entry 104 // liveout: variables live at block exit 105 // avarinitany: addrtaken variables possibly initialized at block exit 106 // (initialized in block or at exit from any predecessor block) 107 // avarinitall: addrtaken variables certainly initialized at block exit 108 // (initialized in block or at exit from all predecessor blocks) 109 livein bvec 110 liveout bvec 111 avarinitany bvec 112 avarinitall bvec 113 } 114 115 // A collection of global state used by liveness analysis. 116 type Liveness struct { 117 fn *Node 118 f *ssa.Func 119 vars []*Node 120 idx map[*Node]int32 121 stkptrsize int64 122 123 be []BlockEffects 124 125 // stackMapIndex maps from safe points (i.e., CALLs) to their 126 // index within the stack maps. 127 stackMapIndex map[*ssa.Value]int 128 129 // An array with a bit vector for each safe point tracking live variables. 130 livevars []bvec 131 132 cache progeffectscache 133 } 134 135 type progeffectscache struct { 136 textavarinit []int32 137 retuevar []int32 138 tailuevar []int32 139 initialized bool 140 } 141 142 // livenessShouldTrack reports whether the liveness analysis 143 // should track the variable n. 144 // We don't care about variables that have no pointers, 145 // nor do we care about non-local variables, 146 // nor do we care about empty structs (handled by the pointer check), 147 // nor do we care about the fake PAUTOHEAP variables. 148 func livenessShouldTrack(n *Node) bool { 149 return n.Op == ONAME && (n.Class() == PAUTO || n.Class() == PPARAM || n.Class() == PPARAMOUT) && types.Haspointers(n.Type) 150 } 151 152 // getvariables returns the list of on-stack variables that we need to track 153 // and a map for looking up indices by *Node. 154 func getvariables(fn *Node) ([]*Node, map[*Node]int32) { 155 var vars []*Node 156 for _, n := range fn.Func.Dcl { 157 if livenessShouldTrack(n) { 158 vars = append(vars, n) 159 } 160 } 161 idx := make(map[*Node]int32, len(vars)) 162 for i, n := range vars { 163 idx[n] = int32(i) 164 } 165 return vars, idx 166 } 167 168 func (lv *Liveness) initcache() { 169 if lv.cache.initialized { 170 Fatalf("liveness cache initialized twice") 171 return 172 } 173 lv.cache.initialized = true 174 175 for i, node := range lv.vars { 176 switch node.Class() { 177 case PPARAM: 178 // A return instruction with a p.to is a tail return, which brings 179 // the stack pointer back up (if it ever went down) and then jumps 180 // to a new function entirely. That form of instruction must read 181 // all the parameters for correctness, and similarly it must not 182 // read the out arguments - they won't be set until the new 183 // function runs. 184 185 lv.cache.tailuevar = append(lv.cache.tailuevar, int32(i)) 186 187 if node.Addrtaken() { 188 lv.cache.textavarinit = append(lv.cache.textavarinit, int32(i)) 189 } 190 191 case PPARAMOUT: 192 // If the result had its address taken, it is being tracked 193 // by the avarinit code, which does not use uevar. 194 // If we added it to uevar too, we'd not see any kill 195 // and decide that the variable was live entry, which it is not. 196 // So only use uevar in the non-addrtaken case. 197 // The p.to.type == obj.TYPE_NONE limits the bvset to 198 // non-tail-call return instructions; see note below for details. 199 if !node.Addrtaken() { 200 lv.cache.retuevar = append(lv.cache.retuevar, int32(i)) 201 } 202 } 203 } 204 } 205 206 // A liveEffect is a set of flags that describe an instruction's 207 // liveness effects on a variable. 208 // 209 // The possible flags are: 210 // uevar - used by the instruction 211 // varkill - killed by the instruction 212 // for variables without address taken, means variable was set 213 // for variables with address taken, means variable was marked dead 214 // avarinit - initialized or referred to by the instruction, 215 // only for variables with address taken but not escaping to heap 216 // 217 // The avarinit output serves as a signal that the data has been 218 // initialized, because any use of a variable must come after its 219 // initialization. 220 type liveEffect int 221 222 const ( 223 uevar liveEffect = 1 << iota 224 varkill 225 avarinit 226 ) 227 228 // valueEffects returns the index of a variable in lv.vars and the 229 // liveness effects v has on that variable. 230 // If v does not affect any tracked variables, it returns -1, 0. 231 func (lv *Liveness) valueEffects(v *ssa.Value) (int32, liveEffect) { 232 n, e := affectedNode(v) 233 if e == 0 || n == nil || n.Op != ONAME { // cheapest checks first 234 return -1, 0 235 } 236 237 // AllocFrame has dropped unused variables from 238 // lv.fn.Func.Dcl, but they might still be referenced by 239 // OpVarFoo pseudo-ops. Ignore them to prevent "lost track of 240 // variable" ICEs (issue 19632). 241 switch v.Op { 242 case ssa.OpVarDef, ssa.OpVarKill, ssa.OpVarLive, ssa.OpKeepAlive: 243 if !n.Name.Used() { 244 return -1, 0 245 } 246 } 247 248 var effect liveEffect 249 if n.Addrtaken() { 250 if v.Op != ssa.OpVarKill { 251 effect |= avarinit 252 } 253 if v.Op == ssa.OpVarDef || v.Op == ssa.OpVarKill { 254 effect |= varkill 255 } 256 } else { 257 // Read is a read, obviously. 258 // Addr by itself is also implicitly a read. 259 // 260 // Addr|Write means that the address is being taken 261 // but only so that the instruction can write to the value. 262 // It is not a read. 263 264 if e&ssa.SymRead != 0 || e&(ssa.SymAddr|ssa.SymWrite) == ssa.SymAddr { 265 effect |= uevar 266 } 267 if e&ssa.SymWrite != 0 && (!isfat(n.Type) || v.Op == ssa.OpVarDef) { 268 effect |= varkill 269 } 270 } 271 272 if effect == 0 { 273 return -1, 0 274 } 275 276 if pos, ok := lv.idx[n]; ok { 277 return pos, effect 278 } 279 return -1, 0 280 } 281 282 // affectedNode returns the *Node affected by v 283 func affectedNode(v *ssa.Value) (*Node, ssa.SymEffect) { 284 // Special cases. 285 switch v.Op { 286 case ssa.OpLoadReg: 287 n, _ := AutoVar(v.Args[0]) 288 return n, ssa.SymRead 289 case ssa.OpStoreReg: 290 n, _ := AutoVar(v) 291 return n, ssa.SymWrite 292 293 case ssa.OpVarLive: 294 return v.Aux.(*Node), ssa.SymRead 295 case ssa.OpVarDef, ssa.OpVarKill: 296 return v.Aux.(*Node), ssa.SymWrite 297 case ssa.OpKeepAlive: 298 n, _ := AutoVar(v.Args[0]) 299 return n, ssa.SymRead 300 } 301 302 e := v.Op.SymEffect() 303 if e == 0 { 304 return nil, 0 305 } 306 307 var n *Node 308 switch a := v.Aux.(type) { 309 case nil, *obj.LSym: 310 // ok, but no node 311 case *Node: 312 n = a 313 default: 314 Fatalf("weird aux: %s", v.LongString()) 315 } 316 317 return n, e 318 } 319 320 // Constructs a new liveness structure used to hold the global state of the 321 // liveness computation. The cfg argument is a slice of *BasicBlocks and the 322 // vars argument is a slice of *Nodes. 323 func newliveness(fn *Node, f *ssa.Func, vars []*Node, idx map[*Node]int32, stkptrsize int64) *Liveness { 324 lv := &Liveness{ 325 fn: fn, 326 f: f, 327 vars: vars, 328 idx: idx, 329 stkptrsize: stkptrsize, 330 be: make([]BlockEffects, f.NumBlocks()), 331 } 332 333 nblocks := int32(len(f.Blocks)) 334 nvars := int32(len(vars)) 335 bulk := bvbulkalloc(nvars, nblocks*7) 336 for _, b := range f.Blocks { 337 be := lv.blockEffects(b) 338 339 be.uevar = bulk.next() 340 be.varkill = bulk.next() 341 be.livein = bulk.next() 342 be.liveout = bulk.next() 343 be.avarinit = bulk.next() 344 be.avarinitany = bulk.next() 345 be.avarinitall = bulk.next() 346 } 347 return lv 348 } 349 350 func (lv *Liveness) blockEffects(b *ssa.Block) *BlockEffects { 351 return &lv.be[b.ID] 352 } 353 354 // NOTE: The bitmap for a specific type t could be cached in t after 355 // the first run and then simply copied into bv at the correct offset 356 // on future calls with the same type t. 357 func onebitwalktype1(t *types.Type, off int64, bv bvec) { 358 if t.Align > 0 && off&int64(t.Align-1) != 0 { 359 Fatalf("onebitwalktype1: invalid initial alignment, %v", t) 360 } 361 362 switch t.Etype { 363 case TINT8, TUINT8, TINT16, TUINT16, 364 TINT32, TUINT32, TINT64, TUINT64, 365 TINT, TUINT, TUINTPTR, TBOOL, 366 TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128: 367 368 case TPTR32, TPTR64, TUNSAFEPTR, TFUNC, TCHAN, TMAP: 369 if off&int64(Widthptr-1) != 0 { 370 Fatalf("onebitwalktype1: invalid alignment, %v", t) 371 } 372 bv.Set(int32(off / int64(Widthptr))) // pointer 373 374 case TSTRING: 375 // struct { byte *str; intgo len; } 376 if off&int64(Widthptr-1) != 0 { 377 Fatalf("onebitwalktype1: invalid alignment, %v", t) 378 } 379 bv.Set(int32(off / int64(Widthptr))) //pointer in first slot 380 381 case TINTER: 382 // struct { Itab *tab; void *data; } 383 // or, when isnilinter(t)==true: 384 // struct { Type *type; void *data; } 385 if off&int64(Widthptr-1) != 0 { 386 Fatalf("onebitwalktype1: invalid alignment, %v", t) 387 } 388 bv.Set(int32(off / int64(Widthptr))) // pointer in first slot 389 bv.Set(int32(off/int64(Widthptr) + 1)) // pointer in second slot 390 391 case TSLICE: 392 // struct { byte *array; uintgo len; uintgo cap; } 393 if off&int64(Widthptr-1) != 0 { 394 Fatalf("onebitwalktype1: invalid TARRAY alignment, %v", t) 395 } 396 bv.Set(int32(off / int64(Widthptr))) // pointer in first slot (BitsPointer) 397 398 case TARRAY: 399 elt := t.Elem() 400 if elt.Width == 0 { 401 // Short-circuit for #20739. 402 break 403 } 404 for i := int64(0); i < t.NumElem(); i++ { 405 onebitwalktype1(elt, off, bv) 406 off += elt.Width 407 } 408 409 case TSTRUCT: 410 for _, f := range t.Fields().Slice() { 411 onebitwalktype1(f.Type, off+f.Offset, bv) 412 } 413 414 default: 415 Fatalf("onebitwalktype1: unexpected type, %v", t) 416 } 417 } 418 419 // localWords returns the number of words of local variables. 420 func (lv *Liveness) localWords() int32 { 421 return int32(lv.stkptrsize / int64(Widthptr)) 422 } 423 424 // argWords returns the number of words of in and out arguments. 425 func (lv *Liveness) argWords() int32 { 426 return int32(lv.fn.Type.ArgWidth() / int64(Widthptr)) 427 } 428 429 // Generates live pointer value maps for arguments and local variables. The 430 // this argument and the in arguments are always assumed live. The vars 431 // argument is a slice of *Nodes. 432 func (lv *Liveness) pointerMap(liveout bvec, vars []*Node, args, locals bvec) { 433 for i := int32(0); ; i++ { 434 i = liveout.Next(i) 435 if i < 0 { 436 break 437 } 438 node := vars[i] 439 switch node.Class() { 440 case PAUTO: 441 onebitwalktype1(node.Type, node.Xoffset+lv.stkptrsize, locals) 442 443 case PPARAM, PPARAMOUT: 444 onebitwalktype1(node.Type, node.Xoffset, args) 445 } 446 } 447 } 448 449 // Returns true for instructions that are safe points that must be annotated 450 // with liveness information. 451 func issafepoint(v *ssa.Value) bool { 452 return v.Op.IsCall() 453 } 454 455 // Initializes the sets for solving the live variables. Visits all the 456 // instructions in each basic block to summarizes the information at each basic 457 // block 458 func (lv *Liveness) prologue() { 459 lv.initcache() 460 461 for _, b := range lv.f.Blocks { 462 be := lv.blockEffects(b) 463 464 // Walk the block instructions backward and update the block 465 // effects with the each prog effects. 466 for j := len(b.Values) - 1; j >= 0; j-- { 467 pos, e := lv.valueEffects(b.Values[j]) 468 if e&varkill != 0 { 469 be.varkill.Set(pos) 470 be.uevar.Unset(pos) 471 } 472 if e&uevar != 0 { 473 be.uevar.Set(pos) 474 } 475 } 476 477 // Walk the block instructions forward to update avarinit bits. 478 // avarinit describes the effect at the end of the block, not the beginning. 479 for j := 0; j < len(b.Values); j++ { 480 pos, e := lv.valueEffects(b.Values[j]) 481 if e&varkill != 0 { 482 be.avarinit.Unset(pos) 483 } 484 if e&avarinit != 0 { 485 be.avarinit.Set(pos) 486 } 487 } 488 } 489 } 490 491 // Solve the liveness dataflow equations. 492 func (lv *Liveness) solve() { 493 // These temporary bitvectors exist to avoid successive allocations and 494 // frees within the loop. 495 newlivein := bvalloc(int32(len(lv.vars))) 496 newliveout := bvalloc(int32(len(lv.vars))) 497 any := bvalloc(int32(len(lv.vars))) 498 all := bvalloc(int32(len(lv.vars))) 499 500 // Push avarinitall, avarinitany forward. 501 // avarinitall says the addressed var is initialized along all paths reaching the block exit. 502 // avarinitany says the addressed var is initialized along some path reaching the block exit. 503 for _, b := range lv.f.Blocks { 504 be := lv.blockEffects(b) 505 if b == lv.f.Entry { 506 be.avarinitall.Copy(be.avarinit) 507 } else { 508 be.avarinitall.Clear() 509 be.avarinitall.Not() 510 } 511 be.avarinitany.Copy(be.avarinit) 512 } 513 514 // Walk blocks in the general direction of propagation (RPO 515 // for avarinit{any,all}, and PO for live{in,out}). This 516 // improves convergence. 517 po := lv.f.Postorder() 518 519 for change := true; change; { 520 change = false 521 for i := len(po) - 1; i >= 0; i-- { 522 b := po[i] 523 be := lv.blockEffects(b) 524 lv.avarinitanyall(b, any, all) 525 526 any.AndNot(any, be.varkill) 527 all.AndNot(all, be.varkill) 528 any.Or(any, be.avarinit) 529 all.Or(all, be.avarinit) 530 if !any.Eq(be.avarinitany) { 531 change = true 532 be.avarinitany.Copy(any) 533 } 534 535 if !all.Eq(be.avarinitall) { 536 change = true 537 be.avarinitall.Copy(all) 538 } 539 } 540 } 541 542 // Iterate through the blocks in reverse round-robin fashion. A work 543 // queue might be slightly faster. As is, the number of iterations is 544 // so low that it hardly seems to be worth the complexity. 545 546 for change := true; change; { 547 change = false 548 for _, b := range po { 549 be := lv.blockEffects(b) 550 551 newliveout.Clear() 552 switch b.Kind { 553 case ssa.BlockRet: 554 for _, pos := range lv.cache.retuevar { 555 newliveout.Set(pos) 556 } 557 case ssa.BlockRetJmp: 558 for _, pos := range lv.cache.tailuevar { 559 newliveout.Set(pos) 560 } 561 case ssa.BlockExit: 562 // nothing to do 563 default: 564 // A variable is live on output from this block 565 // if it is live on input to some successor. 566 // 567 // out[b] = \bigcup_{s \in succ[b]} in[s] 568 newliveout.Copy(lv.blockEffects(b.Succs[0].Block()).livein) 569 for _, succ := range b.Succs[1:] { 570 newliveout.Or(newliveout, lv.blockEffects(succ.Block()).livein) 571 } 572 } 573 574 if !be.liveout.Eq(newliveout) { 575 change = true 576 be.liveout.Copy(newliveout) 577 } 578 579 // A variable is live on input to this block 580 // if it is live on output from this block and 581 // not set by the code in this block. 582 // 583 // in[b] = uevar[b] \cup (out[b] \setminus varkill[b]) 584 newlivein.AndNot(be.liveout, be.varkill) 585 be.livein.Or(newlivein, be.uevar) 586 } 587 } 588 } 589 590 // Visits all instructions in a basic block and computes a bit vector of live 591 // variables at each safe point locations. 592 func (lv *Liveness) epilogue() { 593 nvars := int32(len(lv.vars)) 594 liveout := bvalloc(nvars) 595 any := bvalloc(nvars) 596 all := bvalloc(nvars) 597 livedefer := bvalloc(nvars) // always-live variables 598 599 // If there is a defer (that could recover), then all output 600 // parameters are live all the time. In addition, any locals 601 // that are pointers to heap-allocated output parameters are 602 // also always live (post-deferreturn code needs these 603 // pointers to copy values back to the stack). 604 // TODO: if the output parameter is heap-allocated, then we 605 // don't need to keep the stack copy live? 606 if lv.fn.Func.HasDefer() { 607 for i, n := range lv.vars { 608 if n.Class() == PPARAMOUT { 609 if n.IsOutputParamHeapAddr() { 610 // Just to be paranoid. Heap addresses are PAUTOs. 611 Fatalf("variable %v both output param and heap output param", n) 612 } 613 if n.Name.Param.Heapaddr != nil { 614 // If this variable moved to the heap, then 615 // its stack copy is not live. 616 continue 617 } 618 // Note: zeroing is handled by zeroResults in walk.go. 619 livedefer.Set(int32(i)) 620 } 621 if n.IsOutputParamHeapAddr() { 622 n.Name.SetNeedzero(true) 623 livedefer.Set(int32(i)) 624 } 625 } 626 } 627 628 { 629 // Reserve an entry for function entry. 630 live := bvalloc(nvars) 631 for _, pos := range lv.cache.textavarinit { 632 live.Set(pos) 633 } 634 lv.livevars = append(lv.livevars, live) 635 } 636 637 for _, b := range lv.f.Blocks { 638 be := lv.blockEffects(b) 639 640 // Compute avarinitany and avarinitall for entry to block. 641 // This duplicates information known during livenesssolve 642 // but avoids storing two more vectors for each block. 643 lv.avarinitanyall(b, any, all) 644 645 // Walk forward through the basic block instructions and 646 // allocate liveness maps for those instructions that need them. 647 // Seed the maps with information about the addrtaken variables. 648 for _, v := range b.Values { 649 pos, e := lv.valueEffects(v) 650 if e&varkill != 0 { 651 any.Unset(pos) 652 all.Unset(pos) 653 } 654 if e&avarinit != 0 { 655 any.Set(pos) 656 all.Set(pos) 657 } 658 659 if !issafepoint(v) { 660 continue 661 } 662 663 // Annotate ambiguously live variables so that they can 664 // be zeroed at function entry and at VARKILL points. 665 // liveout is dead here and used as a temporary. 666 liveout.AndNot(any, all) 667 if !liveout.IsEmpty() { 668 for pos := int32(0); pos < liveout.n; pos++ { 669 if !liveout.Get(pos) { 670 continue 671 } 672 all.Set(pos) // silence future warnings in this block 673 n := lv.vars[pos] 674 if !n.Name.Needzero() { 675 n.Name.SetNeedzero(true) 676 if debuglive >= 1 { 677 Warnl(v.Pos, "%v: %L is ambiguously live", lv.fn.Func.Nname, n) 678 } 679 } 680 } 681 } 682 683 // Live stuff first. 684 live := bvalloc(nvars) 685 live.Copy(any) 686 lv.livevars = append(lv.livevars, live) 687 } 688 689 be.lastbitmapindex = len(lv.livevars) - 1 690 } 691 692 for _, b := range lv.f.Blocks { 693 be := lv.blockEffects(b) 694 695 // walk backward, construct maps at each safe point 696 index := int32(be.lastbitmapindex) 697 if index < 0 { 698 // the first block we encounter should have the ATEXT so 699 // at no point should pos ever be less than zero. 700 Fatalf("livenessepilogue") 701 } 702 703 liveout.Copy(be.liveout) 704 for i := len(b.Values) - 1; i >= 0; i-- { 705 v := b.Values[i] 706 707 if issafepoint(v) { 708 // Found an interesting instruction, record the 709 // corresponding liveness information. 710 711 live := lv.livevars[index] 712 live.Or(live, liveout) 713 live.Or(live, livedefer) // only for non-entry safe points 714 index-- 715 } 716 717 // Update liveness information. 718 pos, e := lv.valueEffects(v) 719 if e&varkill != 0 { 720 liveout.Unset(pos) 721 } 722 if e&uevar != 0 { 723 liveout.Set(pos) 724 } 725 } 726 727 if b == lv.f.Entry { 728 if index != 0 { 729 Fatalf("bad index for entry point: %v", index) 730 } 731 732 // Record live variables. 733 live := lv.livevars[index] 734 live.Or(live, liveout) 735 } 736 } 737 738 // Useful sanity check: on entry to the function, 739 // the only things that can possibly be live are the 740 // input parameters. 741 for j, n := range lv.vars { 742 if n.Class() != PPARAM && lv.livevars[0].Get(int32(j)) { 743 Fatalf("internal error: %v %L recorded as live on entry", lv.fn.Func.Nname, n) 744 } 745 } 746 } 747 748 func (lv *Liveness) clobber() { 749 // The clobberdead experiment inserts code to clobber all the dead variables (locals and args) 750 // before and after every safepoint. This experiment is useful for debugging the generation 751 // of live pointer bitmaps. 752 if objabi.Clobberdead_enabled == 0 { 753 return 754 } 755 var varSize int64 756 for _, n := range lv.vars { 757 varSize += n.Type.Size() 758 } 759 if len(lv.livevars) > 1000 || varSize > 10000 { 760 // Be careful to avoid doing too much work. 761 // Bail if >1000 safepoints or >10000 bytes of variables. 762 // Otherwise, giant functions make this experiment generate too much code. 763 return 764 } 765 if h := os.Getenv("GOCLOBBERDEADHASH"); h != "" { 766 // Clobber only functions where the hash of the function name matches a pattern. 767 // Useful for binary searching for a miscompiled function. 768 hstr := "" 769 for _, b := range sha1.Sum([]byte(lv.fn.funcname())) { 770 hstr += fmt.Sprintf("%08b", b) 771 } 772 if !strings.HasSuffix(hstr, h) { 773 return 774 } 775 fmt.Printf("\t\t\tCLOBBERDEAD %s\n", lv.fn.funcname()) 776 } 777 if lv.f.Name == "forkAndExecInChild" { 778 // forkAndExecInChild calls vfork (on linux/amd64, anyway). 779 // The code we add here clobbers parts of the stack in the child. 780 // When the parent resumes, it is using the same stack frame. But the 781 // child has clobbered stack variables that the parent needs. Boom! 782 // In particular, the sys argument gets clobbered. 783 // Note to self: GOCLOBBERDEADHASH=011100101110 784 return 785 } 786 787 var oldSched []*ssa.Value 788 for _, b := range lv.f.Blocks { 789 // Copy block's values to a temporary. 790 oldSched = append(oldSched[:0], b.Values...) 791 b.Values = b.Values[:0] 792 793 // Clobber all dead variables at entry. 794 if b == lv.f.Entry { 795 for len(oldSched) > 0 && len(oldSched[0].Args) == 0 { 796 // Skip argless ops. We need to skip at least 797 // the lowered ClosurePtr op, because it 798 // really wants to be first. This will also 799 // skip ops like InitMem and SP, which are ok. 800 b.Values = append(b.Values, oldSched[0]) 801 oldSched = oldSched[1:] 802 } 803 clobber(lv, b, lv.livevars[0]) 804 } 805 806 // Copy values into schedule, adding clobbering around safepoints. 807 for _, v := range oldSched { 808 if !issafepoint(v) { 809 b.Values = append(b.Values, v) 810 continue 811 } 812 before := true 813 if v.Op.IsCall() && v.Aux != nil && v.Aux.(*obj.LSym) == typedmemmove { 814 // Can't put clobber code before the call to typedmemmove. 815 // The variable to-be-copied is marked as dead 816 // at the callsite. That is ok, though, as typedmemmove 817 // is marked as nosplit, and the first thing it does 818 // is to call memmove (also nosplit), after which 819 // the source value is dead. 820 // See issue 16026. 821 before = false 822 } 823 if before { 824 clobber(lv, b, lv.livevars[lv.stackMapIndex[v]]) 825 } 826 b.Values = append(b.Values, v) 827 clobber(lv, b, lv.livevars[lv.stackMapIndex[v]]) 828 } 829 } 830 } 831 832 // clobber generates code to clobber all dead variables (those not marked in live). 833 // Clobbering instructions are added to the end of b.Values. 834 func clobber(lv *Liveness, b *ssa.Block, live bvec) { 835 for i, n := range lv.vars { 836 if !live.Get(int32(i)) { 837 clobberVar(b, n) 838 } 839 } 840 } 841 842 // clobberVar generates code to trash the pointers in v. 843 // Clobbering instructions are added to the end of b.Values. 844 func clobberVar(b *ssa.Block, v *Node) { 845 clobberWalk(b, v, 0, v.Type) 846 } 847 848 // b = block to which we append instructions 849 // v = variable 850 // offset = offset of (sub-portion of) variable to clobber (in bytes) 851 // t = type of sub-portion of v. 852 func clobberWalk(b *ssa.Block, v *Node, offset int64, t *types.Type) { 853 if !types.Haspointers(t) { 854 return 855 } 856 switch t.Etype { 857 case TPTR32, 858 TPTR64, 859 TUNSAFEPTR, 860 TFUNC, 861 TCHAN, 862 TMAP: 863 clobberPtr(b, v, offset) 864 865 case TSTRING: 866 // struct { byte *str; int len; } 867 clobberPtr(b, v, offset) 868 869 case TINTER: 870 // struct { Itab *tab; void *data; } 871 // or, when isnilinter(t)==true: 872 // struct { Type *type; void *data; } 873 clobberPtr(b, v, offset) 874 clobberPtr(b, v, offset+int64(Widthptr)) 875 876 case TSLICE: 877 // struct { byte *array; int len; int cap; } 878 clobberPtr(b, v, offset) 879 880 case TARRAY: 881 for i := int64(0); i < t.NumElem(); i++ { 882 clobberWalk(b, v, offset+i*t.Elem().Size(), t.Elem()) 883 } 884 885 case TSTRUCT: 886 for _, t1 := range t.Fields().Slice() { 887 clobberWalk(b, v, offset+t1.Offset, t1.Type) 888 } 889 890 default: 891 Fatalf("clobberWalk: unexpected type, %v", t) 892 } 893 } 894 895 // clobberPtr generates a clobber of the pointer at offset offset in v. 896 // The clobber instruction is added at the end of b. 897 func clobberPtr(b *ssa.Block, v *Node, offset int64) { 898 b.NewValue0IA(src.NoXPos, ssa.OpClobber, types.TypeVoid, offset, v) 899 } 900 901 func (lv *Liveness) avarinitanyall(b *ssa.Block, any, all bvec) { 902 if len(b.Preds) == 0 { 903 any.Clear() 904 all.Clear() 905 for _, pos := range lv.cache.textavarinit { 906 any.Set(pos) 907 all.Set(pos) 908 } 909 return 910 } 911 912 be := lv.blockEffects(b.Preds[0].Block()) 913 any.Copy(be.avarinitany) 914 all.Copy(be.avarinitall) 915 916 for _, pred := range b.Preds[1:] { 917 be := lv.blockEffects(pred.Block()) 918 any.Or(any, be.avarinitany) 919 all.And(all, be.avarinitall) 920 } 921 } 922 923 // FNV-1 hash function constants. 924 const ( 925 H0 = 2166136261 926 Hp = 16777619 927 ) 928 929 func hashbitmap(h uint32, bv bvec) uint32 { 930 n := int((bv.n + 31) / 32) 931 for i := 0; i < n; i++ { 932 w := bv.b[i] 933 h = (h * Hp) ^ (w & 0xff) 934 h = (h * Hp) ^ ((w >> 8) & 0xff) 935 h = (h * Hp) ^ ((w >> 16) & 0xff) 936 h = (h * Hp) ^ ((w >> 24) & 0xff) 937 } 938 939 return h 940 } 941 942 // Compact liveness information by coalescing identical per-call-site bitmaps. 943 // The merging only happens for a single function, not across the entire binary. 944 // 945 // There are actually two lists of bitmaps, one list for the local variables and one 946 // list for the function arguments. Both lists are indexed by the same PCDATA 947 // index, so the corresponding pairs must be considered together when 948 // merging duplicates. The argument bitmaps change much less often during 949 // function execution than the local variable bitmaps, so it is possible that 950 // we could introduce a separate PCDATA index for arguments vs locals and 951 // then compact the set of argument bitmaps separately from the set of 952 // local variable bitmaps. As of 2014-04-02, doing this to the godoc binary 953 // is actually a net loss: we save about 50k of argument bitmaps but the new 954 // PCDATA tables cost about 100k. So for now we keep using a single index for 955 // both bitmap lists. 956 func (lv *Liveness) compact() { 957 // Linear probing hash table of bitmaps seen so far. 958 // The hash table has 4n entries to keep the linear 959 // scan short. An entry of -1 indicates an empty slot. 960 n := len(lv.livevars) 961 962 tablesize := 4 * n 963 table := make([]int, tablesize) 964 for i := range table { 965 table[i] = -1 966 } 967 968 // remap[i] = the new index of the old bit vector #i. 969 remap := make([]int, n) 970 for i := range remap { 971 remap[i] = -1 972 } 973 uniq := 0 // unique tables found so far 974 975 // Consider bit vectors in turn. 976 // If new, assign next number using uniq, 977 // record in remap, record in lv.livevars 978 // under the new index, and add entry to hash table. 979 // If already seen, record earlier index in remap. 980 Outer: 981 for i, live := range lv.livevars { 982 h := hashbitmap(H0, live) % uint32(tablesize) 983 984 for { 985 j := table[h] 986 if j < 0 { 987 break 988 } 989 jlive := lv.livevars[j] 990 if live.Eq(jlive) { 991 remap[i] = j 992 continue Outer 993 } 994 995 h++ 996 if h == uint32(tablesize) { 997 h = 0 998 } 999 } 1000 1001 table[h] = uniq 1002 remap[i] = uniq 1003 lv.livevars[uniq] = live 1004 uniq++ 1005 } 1006 1007 // We've already reordered lv.livevars[0:uniq]. Clear the 1008 // pointers later in the array so they can be GC'd. 1009 tail := lv.livevars[uniq:] 1010 for i := range tail { // memclr loop pattern 1011 tail[i] = bvec{} 1012 } 1013 lv.livevars = lv.livevars[:uniq] 1014 1015 // Record compacted stack map indexes for each value. 1016 // These will later become PCDATA instructions. 1017 lv.showlive(nil, lv.livevars[0]) 1018 pos := 1 1019 lv.stackMapIndex = make(map[*ssa.Value]int) 1020 for _, b := range lv.f.Blocks { 1021 for _, v := range b.Values { 1022 if issafepoint(v) { 1023 lv.showlive(v, lv.livevars[remap[pos]]) 1024 lv.stackMapIndex[v] = int(remap[pos]) 1025 pos++ 1026 } 1027 } 1028 } 1029 } 1030 1031 func (lv *Liveness) showlive(v *ssa.Value, live bvec) { 1032 if debuglive == 0 || lv.fn.funcname() == "init" || strings.HasPrefix(lv.fn.funcname(), ".") { 1033 return 1034 } 1035 if live.IsEmpty() { 1036 return 1037 } 1038 1039 pos := lv.fn.Func.Nname.Pos 1040 if v != nil { 1041 pos = v.Pos 1042 } 1043 1044 s := "live at " 1045 if v == nil { 1046 s += fmt.Sprintf("entry to %s:", lv.fn.funcname()) 1047 } else if sym, ok := v.Aux.(*obj.LSym); ok { 1048 fn := sym.Name 1049 if pos := strings.Index(fn, "."); pos >= 0 { 1050 fn = fn[pos+1:] 1051 } 1052 s += fmt.Sprintf("call to %s:", fn) 1053 } else { 1054 s += "indirect call:" 1055 } 1056 1057 for j, n := range lv.vars { 1058 if live.Get(int32(j)) { 1059 s += fmt.Sprintf(" %v", n) 1060 } 1061 } 1062 1063 Warnl(pos, s) 1064 } 1065 1066 func (lv *Liveness) printbvec(printed bool, name string, live bvec) bool { 1067 started := false 1068 for i, n := range lv.vars { 1069 if !live.Get(int32(i)) { 1070 continue 1071 } 1072 if !started { 1073 if !printed { 1074 fmt.Printf("\t") 1075 } else { 1076 fmt.Printf(" ") 1077 } 1078 started = true 1079 printed = true 1080 fmt.Printf("%s=", name) 1081 } else { 1082 fmt.Printf(",") 1083 } 1084 1085 fmt.Printf("%s", n.Sym.Name) 1086 } 1087 return printed 1088 } 1089 1090 // printeffect is like printbvec, but for a single variable. 1091 func (lv *Liveness) printeffect(printed bool, name string, pos int32, x bool) bool { 1092 if !x { 1093 return printed 1094 } 1095 if !printed { 1096 fmt.Printf("\t") 1097 } else { 1098 fmt.Printf(" ") 1099 } 1100 fmt.Printf("%s=%s", name, lv.vars[pos].Sym.Name) 1101 return true 1102 } 1103 1104 // Prints the computed liveness information and inputs, for debugging. 1105 // This format synthesizes the information used during the multiple passes 1106 // into a single presentation. 1107 func (lv *Liveness) printDebug() { 1108 fmt.Printf("liveness: %s\n", lv.fn.funcname()) 1109 1110 pcdata := 0 1111 for i, b := range lv.f.Blocks { 1112 if i > 0 { 1113 fmt.Printf("\n") 1114 } 1115 1116 // bb#0 pred=1,2 succ=3,4 1117 fmt.Printf("bb#%d pred=", b.ID) 1118 for j, pred := range b.Preds { 1119 if j > 0 { 1120 fmt.Printf(",") 1121 } 1122 fmt.Printf("%d", pred.Block().ID) 1123 } 1124 fmt.Printf(" succ=") 1125 for j, succ := range b.Succs { 1126 if j > 0 { 1127 fmt.Printf(",") 1128 } 1129 fmt.Printf("%d", succ.Block().ID) 1130 } 1131 fmt.Printf("\n") 1132 1133 be := lv.blockEffects(b) 1134 1135 // initial settings 1136 printed := false 1137 printed = lv.printbvec(printed, "uevar", be.uevar) 1138 printed = lv.printbvec(printed, "livein", be.livein) 1139 if printed { 1140 fmt.Printf("\n") 1141 } 1142 1143 // program listing, with individual effects listed 1144 1145 if b == lv.f.Entry { 1146 live := lv.livevars[pcdata] 1147 fmt.Printf("(%s) function entry\n", linestr(lv.fn.Func.Nname.Pos)) 1148 fmt.Printf("\tlive=") 1149 printed = false 1150 for j, n := range lv.vars { 1151 if !live.Get(int32(j)) { 1152 continue 1153 } 1154 if printed { 1155 fmt.Printf(",") 1156 } 1157 fmt.Printf("%v", n) 1158 printed = true 1159 } 1160 fmt.Printf("\n") 1161 } 1162 1163 for _, v := range b.Values { 1164 fmt.Printf("(%s) %v\n", linestr(v.Pos), v.LongString()) 1165 1166 if pos, ok := lv.stackMapIndex[v]; ok { 1167 pcdata = pos 1168 } 1169 1170 pos, effect := lv.valueEffects(v) 1171 printed = false 1172 printed = lv.printeffect(printed, "uevar", pos, effect&uevar != 0) 1173 printed = lv.printeffect(printed, "varkill", pos, effect&varkill != 0) 1174 printed = lv.printeffect(printed, "avarinit", pos, effect&avarinit != 0) 1175 if printed { 1176 fmt.Printf("\n") 1177 } 1178 1179 if !issafepoint(v) { 1180 continue 1181 } 1182 1183 live := lv.livevars[pcdata] 1184 fmt.Printf("\tlive=") 1185 printed = false 1186 for j, n := range lv.vars { 1187 if !live.Get(int32(j)) { 1188 continue 1189 } 1190 if printed { 1191 fmt.Printf(",") 1192 } 1193 fmt.Printf("%v", n) 1194 printed = true 1195 } 1196 fmt.Printf("\n") 1197 } 1198 1199 // bb bitsets 1200 fmt.Printf("end\n") 1201 printed = false 1202 printed = lv.printbvec(printed, "varkill", be.varkill) 1203 printed = lv.printbvec(printed, "liveout", be.liveout) 1204 printed = lv.printbvec(printed, "avarinit", be.avarinit) 1205 printed = lv.printbvec(printed, "avarinitany", be.avarinitany) 1206 printed = lv.printbvec(printed, "avarinitall", be.avarinitall) 1207 if printed { 1208 fmt.Printf("\n") 1209 } 1210 } 1211 1212 fmt.Printf("\n") 1213 } 1214 1215 // Dumps a slice of bitmaps to a symbol as a sequence of uint32 values. The 1216 // first word dumped is the total number of bitmaps. The second word is the 1217 // length of the bitmaps. All bitmaps are assumed to be of equal length. The 1218 // remaining bytes are the raw bitmaps. 1219 func (lv *Liveness) emit(argssym, livesym *obj.LSym) { 1220 args := bvalloc(lv.argWords()) 1221 aoff := duint32(argssym, 0, uint32(len(lv.livevars))) // number of bitmaps 1222 aoff = duint32(argssym, aoff, uint32(args.n)) // number of bits in each bitmap 1223 1224 locals := bvalloc(lv.localWords()) 1225 loff := duint32(livesym, 0, uint32(len(lv.livevars))) // number of bitmaps 1226 loff = duint32(livesym, loff, uint32(locals.n)) // number of bits in each bitmap 1227 1228 for _, live := range lv.livevars { 1229 args.Clear() 1230 locals.Clear() 1231 1232 lv.pointerMap(live, lv.vars, args, locals) 1233 1234 aoff = dbvec(argssym, aoff, args) 1235 loff = dbvec(livesym, loff, locals) 1236 } 1237 1238 // Give these LSyms content-addressable names, 1239 // so that they can be de-duplicated. 1240 // This provides significant binary size savings. 1241 // It is safe to rename these LSyms because 1242 // they are tracked separately from ctxt.hash. 1243 argssym.Name = fmt.Sprintf("gclocals%x", md5.Sum(argssym.P)) 1244 livesym.Name = fmt.Sprintf("gclocals%x", md5.Sum(livesym.P)) 1245 } 1246 1247 // Entry pointer for liveness analysis. Solves for the liveness of 1248 // pointer variables in the function and emits a runtime data 1249 // structure read by the garbage collector. 1250 // Returns a map from GC safe points to their corresponding stack map index. 1251 func liveness(e *ssafn, f *ssa.Func) map[*ssa.Value]int { 1252 // Construct the global liveness state. 1253 vars, idx := getvariables(e.curfn) 1254 lv := newliveness(e.curfn, f, vars, idx, e.stkptrsize) 1255 1256 // Run the dataflow framework. 1257 lv.prologue() 1258 lv.solve() 1259 lv.epilogue() 1260 lv.compact() 1261 lv.clobber() 1262 if debuglive >= 2 { 1263 lv.printDebug() 1264 } 1265 1266 // Emit the live pointer map data structures 1267 if ls := e.curfn.Func.lsym; ls != nil { 1268 lv.emit(&ls.Func.GCArgs, &ls.Func.GCLocals) 1269 } 1270 return lv.stackMapIndex 1271 } 1272