github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/cmd/gc/plive.c (about) 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 // -live=3: print information during each computation phase (much chattier) 13 // 14 // Each level includes the earlier output as well. 15 16 #include <u.h> 17 #include <libc.h> 18 #include "gg.h" 19 #include "opt.h" 20 #include "../../pkg/runtime/funcdata.h" 21 22 enum { BitsPerPointer = 2 }; 23 24 enum { 25 UNVISITED = 0, 26 VISITED = 1, 27 }; 28 29 // An ordinary basic block. 30 // 31 // Instructions are threaded together in a doubly-linked list. To iterate in 32 // program order follow the link pointer from the first node and stop after the 33 // last node has been visited 34 // 35 // for(p = bb->first;; p = p->link) { 36 // ... 37 // if(p == bb->last) 38 // break; 39 // } 40 // 41 // To iterate in reverse program order by following the opt pointer from the 42 // last node 43 // 44 // for(p = bb->last; p != nil; p = p->opt) { 45 // ... 46 // } 47 typedef struct BasicBlock BasicBlock; 48 struct BasicBlock { 49 // An array of preceding blocks. If the length of this array is 0 the 50 // block is probably the start block of the CFG. 51 Array *pred; 52 53 // An array out succeeding blocks. If the length of this array is zero, 54 // the block probably ends in a return instruction. 55 Array *succ; 56 57 // First instruction in the block. When part of a fully initialized 58 // control flow graph, the opt member will be nil. 59 Prog *first; 60 61 // Last instruction in the basic block. 62 Prog *last; 63 64 // The reverse post order number. This value is initialized to -1 and 65 // will be replaced by a non-negative value when the CFG is constructed. 66 // After CFG construction, if rpo is -1 this block is unreachable. 67 int rpo; 68 69 // State to denote whether the block has been visited during a 70 // traversal. 71 int mark; 72 73 // For use during livenessepilogue. 74 int lastbitmapindex; 75 }; 76 77 // A collection of global state used by liveness analysis. 78 typedef struct Liveness Liveness; 79 struct Liveness { 80 // A pointer to the node corresponding to the function being analyzed. 81 Node *fn; 82 83 // A linked list of instructions for this function. 84 Prog *ptxt; 85 86 // A list of arguments and local variables in this function. 87 Array *vars; 88 89 // A list of basic blocks that are overlayed on the instruction list. 90 // The blocks are roughly in the same order as the instructions 91 // in the function (first block has TEXT instruction, and so on). 92 Array *cfg; 93 94 // Summary sets of block effects. 95 // The Bvec** is indexed by bb->rpo to yield a single Bvec*. 96 // That bit vector is indexed by variable number (same as lv->vars). 97 // 98 // Computed during livenessprologue using only the content of 99 // individual blocks: 100 // 101 // uevar: upward exposed variables (used before set in block) 102 // varkill: killed variables (set in block) 103 // avarinit: addrtaken variables set or used (proof of initialization) 104 // 105 // Computed during livenesssolve using control flow information: 106 // 107 // livein: variables live at block entry 108 // liveout: variables live at block exit 109 // avarinitany: addrtaken variables possibly initialized at block exit 110 // (initialized in block or at exit from any predecessor block) 111 // avarinitall: addrtaken variables certainly initialized at block exit 112 // (initialized in block or at exit from all predecessor blocks) 113 Bvec **uevar; 114 Bvec **varkill; 115 Bvec **livein; 116 Bvec **liveout; 117 Bvec **avarinit; 118 Bvec **avarinitany; 119 Bvec **avarinitall; 120 121 // An array with a bit vector for each safe point tracking live pointers 122 // in the arguments and locals area, indexed by bb->rpo. 123 Array *argslivepointers; 124 Array *livepointers; 125 }; 126 127 static void* 128 xmalloc(uintptr size) 129 { 130 void *result; 131 132 result = malloc(size); 133 if(result == nil) 134 fatal("malloc failed"); 135 return result; 136 } 137 138 // Constructs a new basic block containing a single instruction. 139 static BasicBlock* 140 newblock(Prog *prog) 141 { 142 BasicBlock *result; 143 144 if(prog == nil) 145 fatal("newblock: prog cannot be nil"); 146 result = xmalloc(sizeof(*result)); 147 result->rpo = -1; 148 result->mark = UNVISITED; 149 result->first = prog; 150 result->last = prog; 151 result->pred = arraynew(2, sizeof(BasicBlock*)); 152 result->succ = arraynew(2, sizeof(BasicBlock*)); 153 return result; 154 } 155 156 // Frees a basic block and all of its leaf data structures. 157 static void 158 freeblock(BasicBlock *bb) 159 { 160 if(bb == nil) 161 fatal("freeblock: cannot free nil"); 162 arrayfree(bb->pred); 163 arrayfree(bb->succ); 164 free(bb); 165 } 166 167 // Adds an edge between two basic blocks by making from a predecessor of to and 168 // to a successor of from. 169 static void 170 addedge(BasicBlock *from, BasicBlock *to) 171 { 172 if(from == nil) 173 fatal("addedge: from is nil"); 174 if(to == nil) 175 fatal("addedge: to is nil"); 176 arrayadd(from->succ, &to); 177 arrayadd(to->pred, &from); 178 } 179 180 // Inserts prev before curr in the instruction 181 // stream. Any control flow, such as branches or fall throughs, that target the 182 // existing instruction are adjusted to target the new instruction. 183 static void 184 splicebefore(Liveness *lv, BasicBlock *bb, Prog *prev, Prog *curr) 185 { 186 Prog *next, tmp; 187 188 USED(lv); 189 190 // There may be other instructions pointing at curr, 191 // and we want them to now point at prev. Instead of 192 // trying to find all such instructions, swap the contents 193 // so that the problem becomes inserting next after curr. 194 // The "opt" field is the backward link in the linked list. 195 196 // Overwrite curr's data with prev, but keep the list links. 197 tmp = *curr; 198 *curr = *prev; 199 curr->opt = tmp.opt; 200 curr->link = tmp.link; 201 202 // Overwrite prev (now next) with curr's old data. 203 next = prev; 204 *next = tmp; 205 next->opt = nil; 206 next->link = nil; 207 208 // Now insert next after curr. 209 next->link = curr->link; 210 next->opt = curr; 211 curr->link = next; 212 if(next->link && next->link->opt == curr) 213 next->link->opt = next; 214 215 if(bb->last == curr) 216 bb->last = next; 217 } 218 219 // A pretty printer for basic blocks. 220 static void 221 printblock(BasicBlock *bb) 222 { 223 BasicBlock *pred; 224 BasicBlock *succ; 225 Prog *prog; 226 int i; 227 228 print("basic block %d\n", bb->rpo); 229 print("\tpred:"); 230 for(i = 0; i < arraylength(bb->pred); i++) { 231 pred = *(BasicBlock**)arrayget(bb->pred, i); 232 print(" %d", pred->rpo); 233 } 234 print("\n"); 235 print("\tsucc:"); 236 for(i = 0; i < arraylength(bb->succ); i++) { 237 succ = *(BasicBlock**)arrayget(bb->succ, i); 238 print(" %d", succ->rpo); 239 } 240 print("\n"); 241 print("\tprog:\n"); 242 for(prog = bb->first;; prog=prog->link) { 243 print("\t\t%P\n", prog); 244 if(prog == bb->last) 245 break; 246 } 247 } 248 249 250 // Iterates over a basic block applying a callback to each instruction. There 251 // are two criteria for termination. If the end of basic block is reached a 252 // value of zero is returned. If the callback returns a non-zero value, the 253 // iteration is stopped and the value of the callback is returned. 254 static int 255 blockany(BasicBlock *bb, int (*callback)(Prog*)) 256 { 257 Prog *p; 258 int result; 259 260 for(p = bb->last; p != nil; p = p->opt) { 261 result = (*callback)(p); 262 if(result != 0) 263 return result; 264 } 265 return 0; 266 } 267 268 // Collects and returns and array of Node*s for functions arguments and local 269 // variables. 270 static Array* 271 getvariables(Node *fn) 272 { 273 Array *result; 274 NodeList *ll; 275 276 result = arraynew(0, sizeof(Node*)); 277 for(ll = fn->dcl; ll != nil; ll = ll->next) { 278 if(ll->n->op == ONAME) { 279 // In order for GODEBUG=gcdead=1 to work, each bitmap needs 280 // to contain information about all variables covered by the bitmap. 281 // For local variables, the bitmap only covers the stkptrsize 282 // bytes in the frame where variables containing pointers live. 283 // For arguments and results, the bitmap covers all variables, 284 // so we must include all the variables, even the ones without 285 // pointers. 286 switch(ll->n->class) { 287 case PAUTO: 288 if(haspointers(ll->n->type)) 289 arrayadd(result, &ll->n); 290 break; 291 case PPARAM: 292 case PPARAMOUT: 293 arrayadd(result, &ll->n); 294 break; 295 } 296 } 297 } 298 return result; 299 } 300 301 // A pretty printer for control flow graphs. Takes an array of BasicBlock*s. 302 static void 303 printcfg(Array *cfg) 304 { 305 BasicBlock *bb; 306 int32 i; 307 308 for(i = 0; i < arraylength(cfg); i++) { 309 bb = *(BasicBlock**)arrayget(cfg, i); 310 printblock(bb); 311 } 312 } 313 314 // Assigns a reverse post order number to each connected basic block using the 315 // standard algorithm. Unconnected blocks will not be affected. 316 static void 317 reversepostorder(BasicBlock *root, int32 *rpo) 318 { 319 BasicBlock *bb; 320 int i; 321 322 root->mark = VISITED; 323 for(i = 0; i < arraylength(root->succ); i++) { 324 bb = *(BasicBlock**)arrayget(root->succ, i); 325 if(bb->mark == UNVISITED) 326 reversepostorder(bb, rpo); 327 } 328 *rpo -= 1; 329 root->rpo = *rpo; 330 } 331 332 // Comparison predicate used for sorting basic blocks by their rpo in ascending 333 // order. 334 static int 335 blockrpocmp(const void *p1, const void *p2) 336 { 337 BasicBlock *bb1; 338 BasicBlock *bb2; 339 340 bb1 = *(BasicBlock**)p1; 341 bb2 = *(BasicBlock**)p2; 342 if(bb1->rpo < bb2->rpo) 343 return -1; 344 if(bb1->rpo > bb2->rpo) 345 return 1; 346 return 0; 347 } 348 349 // A pattern matcher for call instructions. Returns true when the instruction 350 // is a call to a specific package qualified function name. 351 static int 352 iscall(Prog *prog, LSym *name) 353 { 354 if(prog == nil) 355 fatal("iscall: prog is nil"); 356 if(name == nil) 357 fatal("iscall: function name is nil"); 358 if(prog->as != ACALL) 359 return 0; 360 return name == prog->to.sym; 361 } 362 363 // Returns true for instructions that call a runtime function implementing a 364 // select communication clause. 365 static int 366 isselectcommcasecall(Prog *prog) 367 { 368 static LSym* names[5]; 369 int32 i; 370 371 if(names[0] == nil) { 372 names[0] = linksym(pkglookup("selectsend", runtimepkg)); 373 names[1] = linksym(pkglookup("selectrecv", runtimepkg)); 374 names[2] = linksym(pkglookup("selectrecv2", runtimepkg)); 375 names[3] = linksym(pkglookup("selectdefault", runtimepkg)); 376 } 377 for(i = 0; names[i] != nil; i++) 378 if(iscall(prog, names[i])) 379 return 1; 380 return 0; 381 } 382 383 // Returns true for call instructions that target runtime·newselect. 384 static int 385 isnewselect(Prog *prog) 386 { 387 static LSym *sym; 388 389 if(sym == nil) 390 sym = linksym(pkglookup("newselect", runtimepkg)); 391 return iscall(prog, sym); 392 } 393 394 // Returns true for call instructions that target runtime·selectgo. 395 static int 396 isselectgocall(Prog *prog) 397 { 398 static LSym *sym; 399 400 if(sym == nil) 401 sym = linksym(pkglookup("selectgo", runtimepkg)); 402 return iscall(prog, sym); 403 } 404 405 static int 406 isdeferreturn(Prog *prog) 407 { 408 static LSym *sym; 409 410 if(sym == nil) 411 sym = linksym(pkglookup("deferreturn", runtimepkg)); 412 return iscall(prog, sym); 413 } 414 415 // Walk backwards from a runtime·selectgo call up to its immediately dominating 416 // runtime·newselect call. Any successor nodes of communication clause nodes 417 // are implicit successors of the runtime·selectgo call node. The goal of this 418 // analysis is to add these missing edges to complete the control flow graph. 419 static void 420 addselectgosucc(BasicBlock *selectgo) 421 { 422 BasicBlock *pred; 423 BasicBlock *succ; 424 425 pred = selectgo; 426 for(;;) { 427 if(arraylength(pred->pred) == 0) 428 fatal("selectgo does not have a newselect"); 429 pred = *(BasicBlock**)arrayget(pred->pred, 0); 430 if(blockany(pred, isselectcommcasecall)) { 431 // A select comm case block should have exactly one 432 // successor. 433 if(arraylength(pred->succ) != 1) 434 fatal("select comm case has too many successors"); 435 succ = *(BasicBlock**)arrayget(pred->succ, 0); 436 // Its successor should have exactly two successors. 437 // The drop through should flow to the selectgo block 438 // and the branch should lead to the select case 439 // statements block. 440 if(arraylength(succ->succ) != 2) 441 fatal("select comm case successor has too many successors"); 442 // Add the block as a successor of the selectgo block. 443 addedge(selectgo, succ); 444 } 445 if(blockany(pred, isnewselect)) { 446 // Reached the matching newselect. 447 break; 448 } 449 } 450 } 451 452 // The entry point for the missing selectgo control flow algorithm. Takes an 453 // array of BasicBlock*s containing selectgo calls. 454 static void 455 fixselectgo(Array *selectgo) 456 { 457 BasicBlock *bb; 458 int32 i; 459 460 for(i = 0; i < arraylength(selectgo); i++) { 461 bb = *(BasicBlock**)arrayget(selectgo, i); 462 addselectgosucc(bb); 463 } 464 } 465 466 // Constructs a control flow graph from a sequence of instructions. This 467 // procedure is complicated by various sources of implicit control flow that are 468 // not accounted for using the standard cfg construction algorithm. Returns an 469 // array of BasicBlock*s in control flow graph form (basic blocks ordered by 470 // their RPO number). 471 static Array* 472 newcfg(Prog *firstp) 473 { 474 Prog *p; 475 Prog *prev; 476 BasicBlock *bb; 477 Array *cfg; 478 Array *selectgo; 479 int32 i; 480 int32 rpo; 481 482 // Reset the opt field of each prog to nil. In the first and second 483 // passes, instructions that are labels temporarily use the opt field to 484 // point to their basic block. In the third pass, the opt field reset 485 // to point to the predecessor of an instruction in its basic block. 486 for(p = firstp; p != P; p = p->link) 487 p->opt = nil; 488 489 // Allocate an array to remember where we have seen selectgo calls. 490 // These blocks will be revisited to add successor control flow edges. 491 selectgo = arraynew(0, sizeof(BasicBlock*)); 492 493 // Loop through all instructions identifying branch targets 494 // and fall-throughs and allocate basic blocks. 495 cfg = arraynew(0, sizeof(BasicBlock*)); 496 bb = newblock(firstp); 497 arrayadd(cfg, &bb); 498 for(p = firstp; p != P; p = p->link) { 499 if(p->to.type == D_BRANCH) { 500 if(p->to.u.branch == nil) 501 fatal("prog branch to nil"); 502 if(p->to.u.branch->opt == nil) { 503 p->to.u.branch->opt = newblock(p->to.u.branch); 504 arrayadd(cfg, &p->to.u.branch->opt); 505 } 506 if(p->as != AJMP && p->link != nil && p->link->opt == nil) { 507 p->link->opt = newblock(p->link); 508 arrayadd(cfg, &p->link->opt); 509 } 510 } else if(isselectcommcasecall(p) || isselectgocall(p)) { 511 // Accommodate implicit selectgo control flow. 512 if(p->link->opt == nil) { 513 p->link->opt = newblock(p->link); 514 arrayadd(cfg, &p->link->opt); 515 } 516 } 517 } 518 519 // Loop through all basic blocks maximally growing the list of 520 // contained instructions until a label is reached. Add edges 521 // for branches and fall-through instructions. 522 for(i = 0; i < arraylength(cfg); i++) { 523 bb = *(BasicBlock**)arrayget(cfg, i); 524 for(p = bb->last; p != nil; p = p->link) { 525 if(p->opt != nil && p != bb->last) 526 break; 527 bb->last = p; 528 529 // Stop before an unreachable RET, to avoid creating 530 // unreachable control flow nodes. 531 if(p->link != nil && p->link->as == ARET && p->link->mode == 1) 532 break; 533 534 // Collect basic blocks with selectgo calls. 535 if(isselectgocall(p)) 536 arrayadd(selectgo, &bb); 537 } 538 if(bb->last->to.type == D_BRANCH) 539 addedge(bb, bb->last->to.u.branch->opt); 540 if(bb->last->link != nil) { 541 // Add a fall-through when the instruction is 542 // not an unconditional control transfer. 543 switch(bb->last->as) { 544 case AJMP: 545 case ARET: 546 case AUNDEF: 547 break; 548 default: 549 addedge(bb, bb->last->link->opt); 550 } 551 } 552 } 553 554 // Add back links so the instructions in a basic block can be traversed 555 // backward. This is the final state of the instruction opt field. 556 for(i = 0; i < arraylength(cfg); i++) { 557 bb = *(BasicBlock**)arrayget(cfg, i); 558 p = bb->first; 559 prev = nil; 560 for(;;) { 561 p->opt = prev; 562 if(p == bb->last) 563 break; 564 prev = p; 565 p = p->link; 566 } 567 } 568 569 // Add missing successor edges to the selectgo blocks. 570 if(arraylength(selectgo)) 571 fixselectgo(selectgo); 572 arrayfree(selectgo); 573 574 // Find a depth-first order and assign a depth-first number to 575 // all basic blocks. 576 for(i = 0; i < arraylength(cfg); i++) { 577 bb = *(BasicBlock**)arrayget(cfg, i); 578 bb->mark = UNVISITED; 579 } 580 bb = *(BasicBlock**)arrayget(cfg, 0); 581 rpo = arraylength(cfg); 582 reversepostorder(bb, &rpo); 583 584 // Sort the basic blocks by their depth first number. The 585 // array is now a depth-first spanning tree with the first 586 // node being the root. 587 arraysort(cfg, blockrpocmp); 588 bb = *(BasicBlock**)arrayget(cfg, 0); 589 590 // Unreachable control flow nodes are indicated by a -1 in the rpo 591 // field. If we see these nodes something must have gone wrong in an 592 // upstream compilation phase. 593 if(bb->rpo == -1) { 594 print("newcfg: unreachable basic block for %P\n", bb->last); 595 printcfg(cfg); 596 fatal("newcfg: invalid control flow graph"); 597 } 598 599 return cfg; 600 } 601 602 // Frees a control flow graph (an array of BasicBlock*s) and all of its leaf 603 // data structures. 604 static void 605 freecfg(Array *cfg) 606 { 607 BasicBlock *bb; 608 BasicBlock *bb0; 609 Prog *p; 610 int32 i; 611 int32 len; 612 613 len = arraylength(cfg); 614 if(len > 0) { 615 bb0 = *(BasicBlock**)arrayget(cfg, 0); 616 for(p = bb0->first; p != P; p = p->link) { 617 p->opt = nil; 618 } 619 for(i = 0; i < len; i++) { 620 bb = *(BasicBlock**)arrayget(cfg, i); 621 freeblock(bb); 622 } 623 } 624 arrayfree(cfg); 625 } 626 627 // Returns true if the node names a variable that is otherwise uninteresting to 628 // the liveness computation. 629 static int 630 isfunny(Node *node) 631 { 632 char *names[] = { ".fp", ".args", nil }; 633 int i; 634 635 if(node->sym != nil && node->sym->name != nil) 636 for(i = 0; names[i] != nil; i++) 637 if(strcmp(node->sym->name, names[i]) == 0) 638 return 1; 639 return 0; 640 } 641 642 // Computes the effects of an instruction on a set of 643 // variables. The vars argument is an array of Node*s. 644 // 645 // The output vectors give bits for variables: 646 // uevar - used by this instruction 647 // varkill - killed by this instruction 648 // for variables without address taken, means variable was set 649 // for variables with address taken, means variable was marked dead 650 // avarinit - initialized or referred to by this instruction, 651 // only for variables with address taken but not escaping to heap 652 // 653 // The avarinit output serves as a signal that the data has been 654 // initialized, because any use of a variable must come after its 655 // initialization. 656 static void 657 progeffects(Prog *prog, Array *vars, Bvec *uevar, Bvec *varkill, Bvec *avarinit) 658 { 659 ProgInfo info; 660 Adr *from; 661 Adr *to; 662 Node *node; 663 int32 i; 664 int32 pos; 665 666 bvresetall(uevar); 667 bvresetall(varkill); 668 bvresetall(avarinit); 669 670 proginfo(&info, prog); 671 if(prog->as == ARET) { 672 // Return instructions implicitly read all the arguments. For 673 // the sake of correctness, out arguments must be read. For the 674 // sake of backtrace quality, we read in arguments as well. 675 // 676 // A return instruction with a p->to is a tail return, which brings 677 // the stack pointer back up (if it ever went down) and then jumps 678 // to a new function entirely. That form of instruction must read 679 // all the parameters for correctness, and similarly it must not 680 // read the out arguments - they won't be set until the new 681 // function runs. 682 for(i = 0; i < arraylength(vars); i++) { 683 node = *(Node**)arrayget(vars, i); 684 switch(node->class & ~PHEAP) { 685 case PPARAM: 686 bvset(uevar, i); 687 break; 688 case PPARAMOUT: 689 // If the result had its address taken, it is being tracked 690 // by the avarinit code, which does not use uevar. 691 // If we added it to uevar too, we'd not see any kill 692 // and decide that the varible was live entry, which it is not. 693 // So only use uevar in the non-addrtaken case. 694 // The p->to.type == D_NONE limits the bvset to 695 // non-tail-call return instructions; see note above 696 // the for loop for details. 697 if(!node->addrtaken && prog->to.type == D_NONE) 698 bvset(uevar, i); 699 break; 700 } 701 } 702 return; 703 } 704 if(prog->as == ATEXT) { 705 // A text instruction marks the entry point to a function and 706 // the definition point of all in arguments. 707 for(i = 0; i < arraylength(vars); i++) { 708 node = *(Node**)arrayget(vars, i); 709 switch(node->class & ~PHEAP) { 710 case PPARAM: 711 if(node->addrtaken) 712 bvset(avarinit, i); 713 bvset(varkill, i); 714 break; 715 } 716 } 717 return; 718 } 719 if(info.flags & (LeftRead | LeftWrite | LeftAddr)) { 720 from = &prog->from; 721 if (from->node != nil && from->sym != nil) { 722 switch(from->node->class & ~PHEAP) { 723 case PAUTO: 724 case PPARAM: 725 case PPARAMOUT: 726 pos = arrayindexof(vars, from->node); 727 if(pos == -1) 728 goto Next; 729 if(from->node->addrtaken) { 730 bvset(avarinit, pos); 731 } else { 732 if(info.flags & (LeftRead | LeftAddr)) 733 bvset(uevar, pos); 734 if(info.flags & LeftWrite) 735 if(from->node != nil && !isfat(from->node->type)) 736 bvset(varkill, pos); 737 } 738 } 739 } 740 } 741 Next: 742 if(info.flags & (RightRead | RightWrite | RightAddr)) { 743 to = &prog->to; 744 if (to->node != nil && to->sym != nil) { 745 switch(to->node->class & ~PHEAP) { 746 case PAUTO: 747 case PPARAM: 748 case PPARAMOUT: 749 pos = arrayindexof(vars, to->node); 750 if(pos == -1) 751 goto Next1; 752 if(to->node->addrtaken) { 753 if(prog->as != AVARKILL) 754 bvset(avarinit, pos); 755 if(prog->as == AVARDEF || prog->as == AVARKILL) 756 bvset(varkill, pos); 757 } else { 758 // RightRead is a read, obviously. 759 // RightAddr by itself is also implicitly a read. 760 // 761 // RightAddr|RightWrite means that the address is being taken 762 // but only so that the instruction can write to the value. 763 // It is not a read. It is equivalent to RightWrite except that 764 // having the RightAddr bit set keeps the registerizer from 765 // trying to substitute a register for the memory location. 766 if((info.flags & RightRead) || (info.flags & (RightAddr|RightWrite)) == RightAddr) 767 bvset(uevar, pos); 768 if(info.flags & RightWrite) 769 if(to->node != nil && (!isfat(to->node->type) || prog->as == AVARDEF)) 770 bvset(varkill, pos); 771 } 772 } 773 } 774 } 775 Next1:; 776 } 777 778 // Constructs a new liveness structure used to hold the global state of the 779 // liveness computation. The cfg argument is an array of BasicBlock*s and the 780 // vars argument is an array of Node*s. 781 static Liveness* 782 newliveness(Node *fn, Prog *ptxt, Array *cfg, Array *vars) 783 { 784 Liveness *result; 785 int32 i; 786 int32 nblocks; 787 int32 nvars; 788 789 result = xmalloc(sizeof(*result)); 790 result->fn = fn; 791 result->ptxt = ptxt; 792 result->cfg = cfg; 793 result->vars = vars; 794 795 nblocks = arraylength(cfg); 796 result->uevar = xmalloc(sizeof(Bvec*) * nblocks); 797 result->varkill = xmalloc(sizeof(Bvec*) * nblocks); 798 result->livein = xmalloc(sizeof(Bvec*) * nblocks); 799 result->liveout = xmalloc(sizeof(Bvec*) * nblocks); 800 result->avarinit = xmalloc(sizeof(Bvec*) * nblocks); 801 result->avarinitany = xmalloc(sizeof(Bvec*) * nblocks); 802 result->avarinitall = xmalloc(sizeof(Bvec*) * nblocks); 803 804 nvars = arraylength(vars); 805 for(i = 0; i < nblocks; i++) { 806 result->uevar[i] = bvalloc(nvars); 807 result->varkill[i] = bvalloc(nvars); 808 result->livein[i] = bvalloc(nvars); 809 result->liveout[i] = bvalloc(nvars); 810 result->avarinit[i] = bvalloc(nvars); 811 result->avarinitany[i] = bvalloc(nvars); 812 result->avarinitall[i] = bvalloc(nvars); 813 } 814 815 result->livepointers = arraynew(0, sizeof(Bvec*)); 816 result->argslivepointers = arraynew(0, sizeof(Bvec*)); 817 return result; 818 } 819 820 // Frees the liveness structure and all of its leaf data structures. 821 static void 822 freeliveness(Liveness *lv) 823 { 824 int32 i; 825 826 if(lv == nil) 827 fatal("freeliveness: cannot free nil"); 828 829 for(i = 0; i < arraylength(lv->livepointers); i++) 830 free(*(Bvec**)arrayget(lv->livepointers, i)); 831 arrayfree(lv->livepointers); 832 833 for(i = 0; i < arraylength(lv->argslivepointers); i++) 834 free(*(Bvec**)arrayget(lv->argslivepointers, i)); 835 arrayfree(lv->argslivepointers); 836 837 for(i = 0; i < arraylength(lv->cfg); i++) { 838 free(lv->uevar[i]); 839 free(lv->varkill[i]); 840 free(lv->livein[i]); 841 free(lv->liveout[i]); 842 free(lv->avarinit[i]); 843 free(lv->avarinitany[i]); 844 free(lv->avarinitall[i]); 845 } 846 847 free(lv->uevar); 848 free(lv->varkill); 849 free(lv->livein); 850 free(lv->liveout); 851 free(lv->avarinit); 852 free(lv->avarinitany); 853 free(lv->avarinitall); 854 855 free(lv); 856 } 857 858 static void 859 printeffects(Prog *p, Bvec *uevar, Bvec *varkill, Bvec *avarinit) 860 { 861 print("effects of %P", p); 862 print("\nuevar: "); 863 bvprint(uevar); 864 print("\nvarkill: "); 865 bvprint(varkill); 866 print("\navarinit: "); 867 bvprint(avarinit); 868 print("\n"); 869 } 870 871 // Pretty print a variable node. Uses Pascal like conventions for pointers and 872 // addresses to avoid confusing the C like conventions used in the node variable 873 // names. 874 static void 875 printnode(Node *node) 876 { 877 char *p; 878 char *a; 879 880 p = haspointers(node->type) ? "^" : ""; 881 a = node->addrtaken ? "@" : ""; 882 print(" %N%s%s", node, p, a); 883 } 884 885 // Pretty print a list of variables. The vars argument is an array of Node*s. 886 static void 887 printvars(char *name, Bvec *bv, Array *vars) 888 { 889 int32 i; 890 891 print("%s:", name); 892 for(i = 0; i < arraylength(vars); i++) 893 if(bvget(bv, i)) 894 printnode(*(Node**)arrayget(vars, i)); 895 print("\n"); 896 } 897 898 // Prints a basic block annotated with the information computed by liveness 899 // analysis. 900 static void 901 livenessprintblock(Liveness *lv, BasicBlock *bb) 902 { 903 BasicBlock *pred; 904 BasicBlock *succ; 905 Prog *prog; 906 Bvec *live; 907 int i; 908 int32 pos; 909 910 print("basic block %d\n", bb->rpo); 911 912 print("\tpred:"); 913 for(i = 0; i < arraylength(bb->pred); i++) { 914 pred = *(BasicBlock**)arrayget(bb->pred, i); 915 print(" %d", pred->rpo); 916 } 917 print("\n"); 918 919 print("\tsucc:"); 920 for(i = 0; i < arraylength(bb->succ); i++) { 921 succ = *(BasicBlock**)arrayget(bb->succ, i); 922 print(" %d", succ->rpo); 923 } 924 print("\n"); 925 926 printvars("\tuevar", lv->uevar[bb->rpo], lv->vars); 927 printvars("\tvarkill", lv->varkill[bb->rpo], lv->vars); 928 printvars("\tlivein", lv->livein[bb->rpo], lv->vars); 929 printvars("\tliveout", lv->liveout[bb->rpo], lv->vars); 930 printvars("\tavarinit", lv->avarinit[bb->rpo], lv->vars); 931 printvars("\tavarinitany", lv->avarinitany[bb->rpo], lv->vars); 932 printvars("\tavarinitall", lv->avarinitall[bb->rpo], lv->vars); 933 934 print("\tprog:\n"); 935 for(prog = bb->first;; prog = prog->link) { 936 print("\t\t%P", prog); 937 if(prog->as == APCDATA && prog->from.offset == PCDATA_StackMapIndex) { 938 pos = prog->to.offset; 939 live = *(Bvec**)arrayget(lv->livepointers, pos); 940 print(" "); 941 bvprint(live); 942 } 943 print("\n"); 944 if(prog == bb->last) 945 break; 946 } 947 } 948 949 // Prints a control flow graph annotated with any information computed by 950 // liveness analysis. 951 static void 952 livenessprintcfg(Liveness *lv) 953 { 954 BasicBlock *bb; 955 int32 i; 956 957 for(i = 0; i < arraylength(lv->cfg); i++) { 958 bb = *(BasicBlock**)arrayget(lv->cfg, i); 959 livenessprintblock(lv, bb); 960 } 961 } 962 963 static void 964 checkauto(Node *fn, Prog *p, Node *n) 965 { 966 NodeList *l; 967 968 for(l = fn->dcl; l != nil; l = l->next) 969 if(l->n->op == ONAME && l->n->class == PAUTO && l->n == n) 970 return; 971 972 print("checkauto %N: %N (%p; class=%d) not found in %P\n", curfn, n, n, n->class, p); 973 for(l = fn->dcl; l != nil; l = l->next) 974 print("\t%N (%p; class=%d)\n", l->n, l->n, l->n->class); 975 yyerror("checkauto: invariant lost"); 976 } 977 978 static void 979 checkparam(Node *fn, Prog *p, Node *n) 980 { 981 NodeList *l; 982 Node *a; 983 int class; 984 985 if(isfunny(n)) 986 return; 987 for(l = fn->dcl; l != nil; l = l->next) { 988 a = l->n; 989 class = a->class & ~PHEAP; 990 if(a->op == ONAME && (class == PPARAM || class == PPARAMOUT) && a == n) 991 return; 992 } 993 994 print("checkparam %N: %N (%p; class=%d) not found in %P\n", curfn, n, n, n->class, p); 995 for(l = fn->dcl; l != nil; l = l->next) 996 print("\t%N (%p; class=%d)\n", l->n, l->n, l->n->class); 997 yyerror("checkparam: invariant lost"); 998 } 999 1000 static void 1001 checkprog(Node *fn, Prog *p) 1002 { 1003 if(p->from.type == D_AUTO) 1004 checkauto(fn, p, p->from.node); 1005 if(p->from.type == D_PARAM) 1006 checkparam(fn, p, p->from.node); 1007 if(p->to.type == D_AUTO) 1008 checkauto(fn, p, p->to.node); 1009 if(p->to.type == D_PARAM) 1010 checkparam(fn, p, p->to.node); 1011 } 1012 1013 // Check instruction invariants. We assume that the nodes corresponding to the 1014 // sources and destinations of memory operations will be declared in the 1015 // function. This is not strictly true, as is the case for the so-called funny 1016 // nodes and there are special cases to skip over that stuff. The analysis will 1017 // fail if this invariant blindly changes. 1018 static void 1019 checkptxt(Node *fn, Prog *firstp) 1020 { 1021 Prog *p; 1022 1023 for(p = firstp; p != P; p = p->link) { 1024 if(0) 1025 print("analyzing '%P'\n", p); 1026 switch(p->as) { 1027 case ADATA: 1028 case AGLOBL: 1029 case ANAME: 1030 case ASIGNAME: 1031 case ATYPE: 1032 continue; 1033 } 1034 checkprog(fn, p); 1035 } 1036 } 1037 1038 // NOTE: The bitmap for a specific type t should be cached in t after the first run 1039 // and then simply copied into bv at the correct offset on future calls with 1040 // the same type t. On https://rsc.googlecode.com/hg/testdata/slow.go, twobitwalktype1 1041 // accounts for 40% of the 6g execution time. 1042 static void 1043 twobitwalktype1(Type *t, vlong *xoffset, Bvec *bv) 1044 { 1045 vlong fieldoffset; 1046 vlong i; 1047 vlong o; 1048 Type *t1; 1049 1050 if(t->align > 0 && (*xoffset & (t->align - 1)) != 0) 1051 fatal("twobitwalktype1: invalid initial alignment, %T", t); 1052 1053 switch(t->etype) { 1054 case TINT8: 1055 case TUINT8: 1056 case TINT16: 1057 case TUINT16: 1058 case TINT32: 1059 case TUINT32: 1060 case TINT64: 1061 case TUINT64: 1062 case TINT: 1063 case TUINT: 1064 case TUINTPTR: 1065 case TBOOL: 1066 case TFLOAT32: 1067 case TFLOAT64: 1068 case TCOMPLEX64: 1069 case TCOMPLEX128: 1070 for(i = 0; i < t->width; i++) { 1071 bvset(bv, ((*xoffset + i) / widthptr) * BitsPerPointer); // 1 = live scalar 1072 } 1073 *xoffset += t->width; 1074 break; 1075 1076 case TPTR32: 1077 case TPTR64: 1078 case TUNSAFEPTR: 1079 case TFUNC: 1080 case TCHAN: 1081 case TMAP: 1082 if((*xoffset & (widthptr-1)) != 0) 1083 fatal("twobitwalktype1: invalid alignment, %T", t); 1084 bvset(bv, (*xoffset / widthptr) * BitsPerPointer + 1); // 2 = live ptr 1085 *xoffset += t->width; 1086 break; 1087 1088 case TSTRING: 1089 // struct { byte *str; intgo len; } 1090 if((*xoffset & (widthptr-1)) != 0) 1091 fatal("twobitwalktype1: invalid alignment, %T", t); 1092 bvset(bv, (*xoffset / widthptr) * BitsPerPointer + 0); 1093 bvset(bv, (*xoffset / widthptr) * BitsPerPointer + 1); // 3:0 = multiword:string 1094 *xoffset += t->width; 1095 break; 1096 1097 case TINTER: 1098 // struct { Itab *tab; union { void *ptr, uintptr val } data; } 1099 // or, when isnilinter(t)==true: 1100 // struct { Type *type; union { void *ptr, uintptr val } data; } 1101 if((*xoffset & (widthptr-1)) != 0) 1102 fatal("twobitwalktype1: invalid alignment, %T", t); 1103 bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 0); 1104 bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 1); // 3 = multiword 1105 // next word contains 2 = Iface, 3 = Eface 1106 if(isnilinter(t)) { 1107 bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 2); 1108 bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 3); 1109 } else { 1110 bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 3); 1111 } 1112 *xoffset += t->width; 1113 break; 1114 1115 case TARRAY: 1116 // The value of t->bound is -1 for slices types and >0 for 1117 // for fixed array types. All other values are invalid. 1118 if(t->bound < -1) 1119 fatal("twobitwalktype1: invalid bound, %T", t); 1120 if(isslice(t)) { 1121 // struct { byte *array; uintgo len; uintgo cap; } 1122 if((*xoffset & (widthptr-1)) != 0) 1123 fatal("twobitwalktype1: invalid TARRAY alignment, %T", t); 1124 bvset(bv, (*xoffset / widthptr) * BitsPerPointer + 0); 1125 bvset(bv, (*xoffset / widthptr) * BitsPerPointer + 1); 1126 bvset(bv, (*xoffset / widthptr) * BitsPerPointer + 2); // 3:1 = multiword/slice 1127 *xoffset += t->width; 1128 } else 1129 for(i = 0; i < t->bound; i++) 1130 twobitwalktype1(t->type, xoffset, bv); 1131 break; 1132 1133 case TSTRUCT: 1134 o = 0; 1135 for(t1 = t->type; t1 != T; t1 = t1->down) { 1136 fieldoffset = t1->width; 1137 *xoffset += fieldoffset - o; 1138 twobitwalktype1(t1->type, xoffset, bv); 1139 o = fieldoffset + t1->type->width; 1140 } 1141 *xoffset += t->width - o; 1142 break; 1143 1144 default: 1145 fatal("twobitwalktype1: unexpected type, %T", t); 1146 } 1147 } 1148 1149 // Returns the number of words of local variables. 1150 static int32 1151 localswords(void) 1152 { 1153 return stkptrsize / widthptr; 1154 } 1155 1156 // Returns the number of words of in and out arguments. 1157 static int32 1158 argswords(void) 1159 { 1160 return curfn->type->argwid / widthptr; 1161 } 1162 1163 // Generates live pointer value maps for arguments and local variables. The 1164 // this argument and the in arguments are always assumed live. The vars 1165 // argument is an array of Node*s. 1166 static void 1167 twobitlivepointermap(Liveness *lv, Bvec *liveout, Array *vars, Bvec *args, Bvec *locals) 1168 { 1169 Node *node; 1170 Type *thisargtype; 1171 Type *inargtype; 1172 vlong xoffset; 1173 int32 i; 1174 1175 for(i = 0; i < arraylength(vars); i++) { 1176 node = *(Node**)arrayget(vars, i); 1177 switch(node->class) { 1178 case PAUTO: 1179 if(bvget(liveout, i)) { 1180 xoffset = node->xoffset + stkptrsize; 1181 twobitwalktype1(node->type, &xoffset, locals); 1182 } 1183 break; 1184 case PPARAM: 1185 case PPARAMOUT: 1186 if(bvget(liveout, i)) { 1187 xoffset = node->xoffset; 1188 twobitwalktype1(node->type, &xoffset, args); 1189 } 1190 break; 1191 } 1192 } 1193 1194 // The node list only contains declared names. 1195 // If the receiver or arguments are unnamed, they will be omitted 1196 // from the list above. Preserve those values - even though they are unused - 1197 // in order to keep their addresses live for use in stack traces. 1198 thisargtype = getthisx(lv->fn->type); 1199 if(thisargtype != nil) { 1200 xoffset = 0; 1201 twobitwalktype1(thisargtype, &xoffset, args); 1202 } 1203 inargtype = getinargx(lv->fn->type); 1204 if(inargtype != nil) { 1205 xoffset = 0; 1206 twobitwalktype1(inargtype, &xoffset, args); 1207 } 1208 } 1209 1210 // Construct a disembodied instruction. 1211 static Prog* 1212 unlinkedprog(int as) 1213 { 1214 Prog *p; 1215 1216 p = mal(sizeof(*p)); 1217 clearp(p); 1218 p->as = as; 1219 return p; 1220 } 1221 1222 // Construct a new PCDATA instruction associated with and for the purposes of 1223 // covering an existing instruction. 1224 static Prog* 1225 newpcdataprog(Prog *prog, int32 index) 1226 { 1227 Node from, to; 1228 Prog *pcdata; 1229 1230 nodconst(&from, types[TINT32], PCDATA_StackMapIndex); 1231 nodconst(&to, types[TINT32], index); 1232 pcdata = unlinkedprog(APCDATA); 1233 pcdata->lineno = prog->lineno; 1234 naddr(&from, &pcdata->from, 0); 1235 naddr(&to, &pcdata->to, 0); 1236 return pcdata; 1237 } 1238 1239 // Returns true for instructions that are safe points that must be annotated 1240 // with liveness information. 1241 static int 1242 issafepoint(Prog *prog) 1243 { 1244 return prog->as == ATEXT || prog->as == ACALL; 1245 } 1246 1247 // Initializes the sets for solving the live variables. Visits all the 1248 // instructions in each basic block to summarizes the information at each basic 1249 // block 1250 static void 1251 livenessprologue(Liveness *lv) 1252 { 1253 BasicBlock *bb; 1254 Bvec *uevar, *varkill, *avarinit; 1255 Prog *p; 1256 int32 i; 1257 int32 nvars; 1258 1259 nvars = arraylength(lv->vars); 1260 uevar = bvalloc(nvars); 1261 varkill = bvalloc(nvars); 1262 avarinit = bvalloc(nvars); 1263 for(i = 0; i < arraylength(lv->cfg); i++) { 1264 bb = *(BasicBlock**)arrayget(lv->cfg, i); 1265 // Walk the block instructions backward and update the block 1266 // effects with the each prog effects. 1267 for(p = bb->last; p != nil; p = p->opt) { 1268 progeffects(p, lv->vars, uevar, varkill, avarinit); 1269 if(debuglive >= 3) 1270 printeffects(p, uevar, varkill, avarinit); 1271 bvor(lv->varkill[i], lv->varkill[i], varkill); 1272 bvandnot(lv->uevar[i], lv->uevar[i], varkill); 1273 bvor(lv->uevar[i], lv->uevar[i], uevar); 1274 } 1275 // Walk the block instructions forward to update avarinit bits. 1276 // avarinit describes the effect at the end of the block, not the beginning. 1277 bvresetall(varkill); 1278 for(p = bb->first;; p = p->link) { 1279 progeffects(p, lv->vars, uevar, varkill, avarinit); 1280 if(debuglive >= 3) 1281 printeffects(p, uevar, varkill, avarinit); 1282 bvandnot(lv->avarinit[i], lv->avarinit[i], varkill); 1283 bvor(lv->avarinit[i], lv->avarinit[i], avarinit); 1284 if(p == bb->last) 1285 break; 1286 } 1287 } 1288 free(uevar); 1289 free(varkill); 1290 free(avarinit); 1291 } 1292 1293 // Solve the liveness dataflow equations. 1294 static void 1295 livenesssolve(Liveness *lv) 1296 { 1297 BasicBlock *bb, *succ, *pred; 1298 Bvec *newlivein, *newliveout, *any, *all; 1299 int32 rpo, i, j, change; 1300 1301 // These temporary bitvectors exist to avoid successive allocations and 1302 // frees within the loop. 1303 newlivein = bvalloc(arraylength(lv->vars)); 1304 newliveout = bvalloc(arraylength(lv->vars)); 1305 any = bvalloc(arraylength(lv->vars)); 1306 all = bvalloc(arraylength(lv->vars)); 1307 1308 // Push avarinitall, avarinitany forward. 1309 // avarinitall says the addressed var is initialized along all paths reaching the block exit. 1310 // avarinitany says the addressed var is initialized along some path reaching the block exit. 1311 for(i = 0; i < arraylength(lv->cfg); i++) { 1312 bb = *(BasicBlock**)arrayget(lv->cfg, i); 1313 rpo = bb->rpo; 1314 if(i == 0) 1315 bvcopy(lv->avarinitall[rpo], lv->avarinit[rpo]); 1316 else { 1317 bvresetall(lv->avarinitall[rpo]); 1318 bvnot(lv->avarinitall[rpo]); 1319 } 1320 bvcopy(lv->avarinitany[rpo], lv->avarinit[rpo]); 1321 } 1322 1323 change = 1; 1324 while(change != 0) { 1325 change = 0; 1326 for(i = 0; i < arraylength(lv->cfg); i++) { 1327 bb = *(BasicBlock**)arrayget(lv->cfg, i); 1328 rpo = bb->rpo; 1329 bvresetall(any); 1330 bvresetall(all); 1331 for(j = 0; j < arraylength(bb->pred); j++) { 1332 pred = *(BasicBlock**)arrayget(bb->pred, j); 1333 if(j == 0) { 1334 bvcopy(any, lv->avarinitany[pred->rpo]); 1335 bvcopy(all, lv->avarinitall[pred->rpo]); 1336 } else { 1337 bvor(any, any, lv->avarinitany[pred->rpo]); 1338 bvand(all, all, lv->avarinitall[pred->rpo]); 1339 } 1340 } 1341 bvandnot(any, any, lv->varkill[rpo]); 1342 bvandnot(all, all, lv->varkill[rpo]); 1343 bvor(any, any, lv->avarinit[rpo]); 1344 bvor(all, all, lv->avarinit[rpo]); 1345 if(bvcmp(any, lv->avarinitany[rpo])) { 1346 change = 1; 1347 bvcopy(lv->avarinitany[rpo], any); 1348 } 1349 if(bvcmp(all, lv->avarinitall[rpo])) { 1350 change = 1; 1351 bvcopy(lv->avarinitall[rpo], all); 1352 } 1353 } 1354 } 1355 1356 // Iterate through the blocks in reverse round-robin fashion. A work 1357 // queue might be slightly faster. As is, the number of iterations is 1358 // so low that it hardly seems to be worth the complexity. 1359 change = 1; 1360 while(change != 0) { 1361 change = 0; 1362 // Walk blocks in the general direction of propagation. This 1363 // improves convergence. 1364 for(i = arraylength(lv->cfg) - 1; i >= 0; i--) { 1365 // A variable is live on output from this block 1366 // if it is live on input to some successor. 1367 // 1368 // out[b] = \bigcup_{s \in succ[b]} in[s] 1369 bb = *(BasicBlock**)arrayget(lv->cfg, i); 1370 rpo = bb->rpo; 1371 bvresetall(newliveout); 1372 for(j = 0; j < arraylength(bb->succ); j++) { 1373 succ = *(BasicBlock**)arrayget(bb->succ, j); 1374 bvor(newliveout, newliveout, lv->livein[succ->rpo]); 1375 } 1376 if(bvcmp(lv->liveout[rpo], newliveout)) { 1377 change = 1; 1378 bvcopy(lv->liveout[rpo], newliveout); 1379 } 1380 1381 // A variable is live on input to this block 1382 // if it is live on output from this block and 1383 // not set by the code in this block. 1384 // 1385 // in[b] = uevar[b] \cup (out[b] \setminus varkill[b]) 1386 bvandnot(newlivein, lv->liveout[rpo], lv->varkill[rpo]); 1387 bvor(lv->livein[rpo], newlivein, lv->uevar[rpo]); 1388 } 1389 } 1390 1391 free(newlivein); 1392 free(newliveout); 1393 free(any); 1394 free(all); 1395 } 1396 1397 // This function is slow but it is only used for generating debug prints. 1398 // Check whether n is marked live in args/locals. 1399 static int 1400 islive(Node *n, Bvec *args, Bvec *locals) 1401 { 1402 int i; 1403 1404 switch(n->class) { 1405 case PPARAM: 1406 case PPARAMOUT: 1407 for(i = 0; i < n->type->width/widthptr*BitsPerPointer; i++) 1408 if(bvget(args, n->xoffset/widthptr*BitsPerPointer + i)) 1409 return 1; 1410 break; 1411 case PAUTO: 1412 for(i = 0; i < n->type->width/widthptr*BitsPerPointer; i++) 1413 if(bvget(locals, (n->xoffset + stkptrsize)/widthptr*BitsPerPointer + i)) 1414 return 1; 1415 break; 1416 } 1417 return 0; 1418 } 1419 1420 // Visits all instructions in a basic block and computes a bit vector of live 1421 // variables at each safe point locations. 1422 static void 1423 livenessepilogue(Liveness *lv) 1424 { 1425 BasicBlock *bb, *pred; 1426 Bvec *ambig, *livein, *liveout, *uevar, *varkill, *args, *locals, *avarinit, *any, *all; 1427 Node *n; 1428 Prog *p, *next; 1429 int32 i, j, numlive, startmsg, nmsg, nvars, pos; 1430 vlong xoffset; 1431 char **msg; 1432 Fmt fmt; 1433 1434 nvars = arraylength(lv->vars); 1435 livein = bvalloc(nvars); 1436 liveout = bvalloc(nvars); 1437 uevar = bvalloc(nvars); 1438 varkill = bvalloc(nvars); 1439 avarinit = bvalloc(nvars); 1440 any = bvalloc(nvars); 1441 all = bvalloc(nvars); 1442 ambig = bvalloc(localswords() * BitsPerPointer); 1443 msg = nil; 1444 nmsg = 0; 1445 startmsg = 0; 1446 1447 for(i = 0; i < arraylength(lv->cfg); i++) { 1448 bb = *(BasicBlock**)arrayget(lv->cfg, i); 1449 1450 // Compute avarinitany and avarinitall for entry to block. 1451 // This duplicates information known during livenesssolve 1452 // but avoids storing two more vectors for each block. 1453 bvresetall(any); 1454 bvresetall(all); 1455 for(j = 0; j < arraylength(bb->pred); j++) { 1456 pred = *(BasicBlock**)arrayget(bb->pred, j); 1457 if(j == 0) { 1458 bvcopy(any, lv->avarinitany[pred->rpo]); 1459 bvcopy(all, lv->avarinitall[pred->rpo]); 1460 } else { 1461 bvor(any, any, lv->avarinitany[pred->rpo]); 1462 bvand(all, all, lv->avarinitall[pred->rpo]); 1463 } 1464 } 1465 1466 // Walk forward through the basic block instructions and 1467 // allocate liveness maps for those instructions that need them. 1468 // Seed the maps with information about the addrtaken variables. 1469 for(p = bb->first;; p = p->link) { 1470 progeffects(p, lv->vars, uevar, varkill, avarinit); 1471 bvandnot(any, any, varkill); 1472 bvandnot(all, all, varkill); 1473 bvor(any, any, avarinit); 1474 bvor(all, all, avarinit); 1475 1476 if(issafepoint(p)) { 1477 // Annotate ambiguously live variables so that they can 1478 // be zeroed at function entry. 1479 // livein and liveout are dead here and used as temporaries. 1480 // For now, only enabled when using GOEXPERIMENT=precisestack 1481 // during make.bash / all.bash. 1482 if(precisestack_enabled) { 1483 bvresetall(livein); 1484 bvandnot(liveout, any, all); 1485 if(!bvisempty(liveout)) { 1486 for(pos = 0; pos < liveout->n; pos++) { 1487 if(!bvget(liveout, pos)) 1488 continue; 1489 bvset(all, pos); // silence future warnings in this block 1490 n = *(Node**)arrayget(lv->vars, pos); 1491 if(!n->needzero) { 1492 n->needzero = 1; 1493 if(debuglive >= 1) 1494 warnl(p->lineno, "%N: %lN is ambiguously live", curfn->nname, n); 1495 // Record in 'ambiguous' bitmap. 1496 xoffset = n->xoffset + stkptrsize; 1497 twobitwalktype1(n->type, &xoffset, ambig); 1498 } 1499 } 1500 } 1501 } 1502 1503 // Allocate a bit vector for each class and facet of 1504 // value we are tracking. 1505 1506 // Live stuff first. 1507 args = bvalloc(argswords() * BitsPerPointer); 1508 arrayadd(lv->argslivepointers, &args); 1509 locals = bvalloc(localswords() * BitsPerPointer); 1510 arrayadd(lv->livepointers, &locals); 1511 1512 if(debuglive >= 3) { 1513 print("%P\n", p); 1514 printvars("avarinitany", any, lv->vars); 1515 } 1516 1517 // Record any values with an "address taken" reaching 1518 // this code position as live. Must do now instead of below 1519 // because the any/all calculation requires walking forward 1520 // over the block (as this loop does), while the liveout 1521 // requires walking backward (as the next loop does). 1522 twobitlivepointermap(lv, any, lv->vars, args, locals); 1523 } 1524 1525 if(p == bb->last) 1526 break; 1527 } 1528 bb->lastbitmapindex = arraylength(lv->livepointers) - 1; 1529 } 1530 1531 for(i = 0; i < arraylength(lv->cfg); i++) { 1532 bb = *(BasicBlock**)arrayget(lv->cfg, i); 1533 1534 if(debuglive >= 1 && strcmp(curfn->nname->sym->name, "init") != 0 && curfn->nname->sym->name[0] != '.') { 1535 nmsg = arraylength(lv->livepointers); 1536 startmsg = nmsg; 1537 msg = xmalloc(nmsg*sizeof msg[0]); 1538 for(j=0; j<nmsg; j++) 1539 msg[j] = nil; 1540 } 1541 1542 // walk backward, emit pcdata and populate the maps 1543 pos = bb->lastbitmapindex; 1544 if(pos < 0) { 1545 // the first block we encounter should have the ATEXT so 1546 // at no point should pos ever be less than zero. 1547 fatal("livenessepilogue"); 1548 } 1549 1550 bvcopy(livein, lv->liveout[bb->rpo]); 1551 for(p = bb->last; p != nil; p = next) { 1552 next = p->opt; // splicebefore modifies p->opt 1553 // Propagate liveness information 1554 progeffects(p, lv->vars, uevar, varkill, avarinit); 1555 bvcopy(liveout, livein); 1556 bvandnot(livein, liveout, varkill); 1557 bvor(livein, livein, uevar); 1558 if(debuglive >= 3 && issafepoint(p)){ 1559 print("%P\n", p); 1560 printvars("uevar", uevar, lv->vars); 1561 printvars("varkill", varkill, lv->vars); 1562 printvars("livein", livein, lv->vars); 1563 printvars("liveout", liveout, lv->vars); 1564 } 1565 if(issafepoint(p)) { 1566 // Found an interesting instruction, record the 1567 // corresponding liveness information. 1568 1569 // Useful sanity check: on entry to the function, 1570 // the only things that can possibly be live are the 1571 // input parameters. 1572 if(p->as == ATEXT) { 1573 for(j = 0; j < liveout->n; j++) { 1574 if(!bvget(liveout, j)) 1575 continue; 1576 n = *(Node**)arrayget(lv->vars, j); 1577 if(n->class != PPARAM) 1578 yyerrorl(p->lineno, "internal error: %N %lN recorded as live on entry", curfn->nname, n); 1579 } 1580 } 1581 1582 // Record live pointers. 1583 args = *(Bvec**)arrayget(lv->argslivepointers, pos); 1584 locals = *(Bvec**)arrayget(lv->livepointers, pos); 1585 twobitlivepointermap(lv, liveout, lv->vars, args, locals); 1586 1587 // Ambiguously live variables are zeroed immediately after 1588 // function entry. Mark them live for all the non-entry bitmaps 1589 // so that GODEBUG=gcdead=1 mode does not poison them. 1590 if(p->as == ACALL) 1591 bvor(locals, locals, ambig); 1592 1593 // Show live pointer bitmaps. 1594 // We're interpreting the args and locals bitmap instead of liveout so that we 1595 // include the bits added by the avarinit logic in the 1596 // previous loop. 1597 if(msg != nil) { 1598 fmtstrinit(&fmt); 1599 fmtprint(&fmt, "%L: live at ", p->lineno); 1600 if(p->as == ACALL && p->to.node) 1601 fmtprint(&fmt, "call to %s:", p->to.node->sym->name); 1602 else if(p->as == ACALL) 1603 fmtprint(&fmt, "indirect call:"); 1604 else 1605 fmtprint(&fmt, "entry to %s:", p->from.node->sym->name); 1606 numlive = 0; 1607 for(j = 0; j < arraylength(lv->vars); j++) { 1608 n = *(Node**)arrayget(lv->vars, j); 1609 if(islive(n, args, locals)) { 1610 fmtprint(&fmt, " %N", n); 1611 numlive++; 1612 } 1613 } 1614 fmtprint(&fmt, "\n"); 1615 if(numlive == 0) // squelch message 1616 free(fmtstrflush(&fmt)); 1617 else 1618 msg[--startmsg] = fmtstrflush(&fmt); 1619 } 1620 1621 // Only CALL instructions need a PCDATA annotation. 1622 // The TEXT instruction annotation is implicit. 1623 if(p->as == ACALL) { 1624 if(isdeferreturn(p)) { 1625 // runtime.deferreturn modifies its return address to return 1626 // back to the CALL, not to the subsequent instruction. 1627 // Because the return comes back one instruction early, 1628 // the PCDATA must begin one instruction early too. 1629 // The instruction before a call to deferreturn is always a 1630 // no-op, to keep PC-specific data unambiguous. 1631 splicebefore(lv, bb, newpcdataprog(p->opt, pos), p->opt); 1632 } else { 1633 splicebefore(lv, bb, newpcdataprog(p, pos), p); 1634 } 1635 } 1636 1637 pos--; 1638 } 1639 } 1640 if(msg != nil) { 1641 for(j=startmsg; j<nmsg; j++) 1642 if(msg[j] != nil) 1643 print("%s", msg[j]); 1644 free(msg); 1645 msg = nil; 1646 nmsg = 0; 1647 startmsg = 0; 1648 } 1649 } 1650 1651 free(livein); 1652 free(liveout); 1653 free(uevar); 1654 free(varkill); 1655 free(avarinit); 1656 free(any); 1657 free(all); 1658 free(ambig); 1659 1660 flusherrors(); 1661 } 1662 1663 // FNV-1 hash function constants. 1664 #define H0 2166136261UL 1665 #define Hp 16777619UL 1666 1667 static uint32 1668 hashbitmap(uint32 h, Bvec *bv) 1669 { 1670 uchar *p, *ep; 1671 1672 p = (uchar*)bv->b; 1673 ep = p + 4*((bv->n+31)/32); 1674 while(p < ep) 1675 h = (h*Hp) ^ *p++; 1676 return h; 1677 } 1678 1679 // Compact liveness information by coalescing identical per-call-site bitmaps. 1680 // The merging only happens for a single function, not across the entire binary. 1681 // 1682 // There are actually two lists of bitmaps, one list for the local variables and one 1683 // list for the function arguments. Both lists are indexed by the same PCDATA 1684 // index, so the corresponding pairs must be considered together when 1685 // merging duplicates. The argument bitmaps change much less often during 1686 // function execution than the local variable bitmaps, so it is possible that 1687 // we could introduce a separate PCDATA index for arguments vs locals and 1688 // then compact the set of argument bitmaps separately from the set of 1689 // local variable bitmaps. As of 2014-04-02, doing this to the godoc binary 1690 // is actually a net loss: we save about 50k of argument bitmaps but the new 1691 // PCDATA tables cost about 100k. So for now we keep using a single index for 1692 // both bitmap lists. 1693 static void 1694 livenesscompact(Liveness *lv) 1695 { 1696 int *table, *remap, i, j, n, tablesize, uniq; 1697 uint32 h; 1698 Bvec *local, *arg, *jlocal, *jarg; 1699 Prog *p; 1700 1701 // Linear probing hash table of bitmaps seen so far. 1702 // The hash table has 4n entries to keep the linear 1703 // scan short. An entry of -1 indicates an empty slot. 1704 n = arraylength(lv->livepointers); 1705 tablesize = 4*n; 1706 table = xmalloc(tablesize*sizeof table[0]); 1707 memset(table, 0xff, tablesize*sizeof table[0]); 1708 1709 // remap[i] = the new index of the old bit vector #i. 1710 remap = xmalloc(n*sizeof remap[0]); 1711 memset(remap, 0xff, n*sizeof remap[0]); 1712 uniq = 0; // unique tables found so far 1713 1714 // Consider bit vectors in turn. 1715 // If new, assign next number using uniq, 1716 // record in remap, record in lv->livepointers and lv->argslivepointers 1717 // under the new index, and add entry to hash table. 1718 // If already seen, record earlier index in remap and free bitmaps. 1719 for(i=0; i<n; i++) { 1720 local = *(Bvec**)arrayget(lv->livepointers, i); 1721 arg = *(Bvec**)arrayget(lv->argslivepointers, i); 1722 h = hashbitmap(hashbitmap(H0, local), arg) % tablesize; 1723 1724 for(;;) { 1725 j = table[h]; 1726 if(j < 0) 1727 break; 1728 jlocal = *(Bvec**)arrayget(lv->livepointers, j); 1729 jarg = *(Bvec**)arrayget(lv->argslivepointers, j); 1730 if(bvcmp(local, jlocal) == 0 && bvcmp(arg, jarg) == 0) { 1731 free(local); 1732 free(arg); 1733 remap[i] = j; 1734 goto Next; 1735 } 1736 if(++h == tablesize) 1737 h = 0; 1738 } 1739 table[h] = uniq; 1740 remap[i] = uniq; 1741 *(Bvec**)arrayget(lv->livepointers, uniq) = local; 1742 *(Bvec**)arrayget(lv->argslivepointers, uniq) = arg; 1743 uniq++; 1744 Next:; 1745 } 1746 1747 // We've already reordered lv->livepointers[0:uniq] 1748 // and lv->argslivepointers[0:uniq] and freed the bitmaps 1749 // we don't need anymore. Clear the pointers later in the 1750 // array so that we can tell where the coalesced bitmaps stop 1751 // and so that we don't double-free when cleaning up. 1752 for(j=uniq; j<n; j++) { 1753 *(Bvec**)arrayget(lv->livepointers, j) = nil; 1754 *(Bvec**)arrayget(lv->argslivepointers, j) = nil; 1755 } 1756 1757 // Rewrite PCDATA instructions to use new numbering. 1758 for(p=lv->ptxt; p != P; p=p->link) { 1759 if(p->as == APCDATA && p->from.offset == PCDATA_StackMapIndex) { 1760 i = p->to.offset; 1761 if(i >= 0) 1762 p->to.offset = remap[i]; 1763 } 1764 } 1765 1766 free(table); 1767 free(remap); 1768 } 1769 1770 static int 1771 printbitset(int printed, char *name, Array *vars, Bvec *bits) 1772 { 1773 int i, started; 1774 Node *n; 1775 1776 started = 0; 1777 for(i=0; i<arraylength(vars); i++) { 1778 if(!bvget(bits, i)) 1779 continue; 1780 if(!started) { 1781 if(!printed) 1782 print("\t"); 1783 else 1784 print(" "); 1785 started = 1; 1786 printed = 1; 1787 print("%s=", name); 1788 } else { 1789 print(","); 1790 } 1791 n = *(Node**)arrayget(vars, i); 1792 print("%s", n->sym->name); 1793 } 1794 return printed; 1795 } 1796 1797 // Prints the computed liveness information and inputs, for debugging. 1798 // This format synthesizes the information used during the multiple passes 1799 // into a single presentation. 1800 static void 1801 livenessprintdebug(Liveness *lv) 1802 { 1803 int i, j, pcdata, printed; 1804 BasicBlock *bb; 1805 Prog *p; 1806 Bvec *uevar, *varkill, *avarinit, *args, *locals; 1807 Node *n; 1808 1809 print("liveness: %s\n", curfn->nname->sym->name); 1810 1811 uevar = bvalloc(arraylength(lv->vars)); 1812 varkill = bvalloc(arraylength(lv->vars)); 1813 avarinit = bvalloc(arraylength(lv->vars)); 1814 1815 pcdata = 0; 1816 for(i = 0; i < arraylength(lv->cfg); i++) { 1817 if(i > 0) 1818 print("\n"); 1819 bb = *(BasicBlock**)arrayget(lv->cfg, i); 1820 1821 // bb#0 pred=1,2 succ=3,4 1822 print("bb#%d pred=", i); 1823 for(j = 0; j < arraylength(bb->pred); j++) { 1824 if(j > 0) 1825 print(","); 1826 print("%d", (*(BasicBlock**)arrayget(bb->pred, j))->rpo); 1827 } 1828 print(" succ="); 1829 for(j = 0; j < arraylength(bb->succ); j++) { 1830 if(j > 0) 1831 print(","); 1832 print("%d", (*(BasicBlock**)arrayget(bb->succ, j))->rpo); 1833 } 1834 print("\n"); 1835 1836 // initial settings 1837 printed = 0; 1838 printed = printbitset(printed, "uevar", lv->vars, lv->uevar[bb->rpo]); 1839 printed = printbitset(printed, "livein", lv->vars, lv->livein[bb->rpo]); 1840 if(printed) 1841 print("\n"); 1842 1843 // program listing, with individual effects listed 1844 for(p = bb->first;; p = p->link) { 1845 print("%P\n", p); 1846 if(p->as == APCDATA && p->from.offset == PCDATA_StackMapIndex) 1847 pcdata = p->to.offset; 1848 progeffects(p, lv->vars, uevar, varkill, avarinit); 1849 printed = 0; 1850 printed = printbitset(printed, "uevar", lv->vars, uevar); 1851 printed = printbitset(printed, "varkill", lv->vars, varkill); 1852 printed = printbitset(printed, "avarinit", lv->vars, avarinit); 1853 if(printed) 1854 print("\n"); 1855 if(issafepoint(p)) { 1856 args = *(Bvec**)arrayget(lv->argslivepointers, pcdata); 1857 locals = *(Bvec**)arrayget(lv->livepointers, pcdata); 1858 print("\tlive="); 1859 printed = 0; 1860 for(j = 0; j < arraylength(lv->vars); j++) { 1861 n = *(Node**)arrayget(lv->vars, j); 1862 if(islive(n, args, locals)) { 1863 if(printed++) 1864 print(","); 1865 print("%N", n); 1866 } 1867 } 1868 print("\n"); 1869 } 1870 if(p == bb->last) 1871 break; 1872 } 1873 1874 // bb bitsets 1875 print("end\n"); 1876 printed = printbitset(printed, "varkill", lv->vars, lv->varkill[bb->rpo]); 1877 printed = printbitset(printed, "liveout", lv->vars, lv->liveout[bb->rpo]); 1878 printed = printbitset(printed, "avarinit", lv->vars, lv->avarinit[bb->rpo]); 1879 printed = printbitset(printed, "avarinitany", lv->vars, lv->avarinitany[bb->rpo]); 1880 printed = printbitset(printed, "avarinitall", lv->vars, lv->avarinitall[bb->rpo]); 1881 if(printed) 1882 print("\n"); 1883 } 1884 print("\n"); 1885 1886 free(uevar); 1887 free(varkill); 1888 free(avarinit); 1889 } 1890 1891 // Dumps an array of bitmaps to a symbol as a sequence of uint32 values. The 1892 // first word dumped is the total number of bitmaps. The second word is the 1893 // length of the bitmaps. All bitmaps are assumed to be of equal length. The 1894 // words that are followed are the raw bitmap words. The arr argument is an 1895 // array of Node*s. 1896 static void 1897 twobitwritesymbol(Array *arr, Sym *sym) 1898 { 1899 Bvec *bv; 1900 int off, i, j, len; 1901 uint32 word; 1902 1903 len = arraylength(arr); 1904 off = 0; 1905 off += 4; // number of bitmaps, to fill in later 1906 bv = *(Bvec**)arrayget(arr, 0); 1907 off = duint32(sym, off, bv->n); // number of bits in each bitmap 1908 for(i = 0; i < len; i++) { 1909 // bitmap words 1910 bv = *(Bvec**)arrayget(arr, i); 1911 if(bv == nil) 1912 break; 1913 for(j = 0; j < bv->n; j += 32) { 1914 word = bv->b[j/32]; 1915 off = duint32(sym, off, word); 1916 } 1917 } 1918 duint32(sym, 0, i); // number of bitmaps 1919 ggloblsym(sym, off, 0, 1); 1920 } 1921 1922 static void 1923 printprog(Prog *p) 1924 { 1925 while(p != nil) { 1926 print("%P\n", p); 1927 p = p->link; 1928 } 1929 } 1930 1931 // Entry pointer for liveness analysis. Constructs a complete CFG, solves for 1932 // the liveness of pointer variables in the function, and emits a runtime data 1933 // structure read by the garbage collector. 1934 void 1935 liveness(Node *fn, Prog *firstp, Sym *argssym, Sym *livesym) 1936 { 1937 Array *cfg, *vars; 1938 Liveness *lv; 1939 int debugdelta; 1940 1941 // Change name to dump debugging information only for a specific function. 1942 debugdelta = 0; 1943 if(strcmp(curfn->nname->sym->name, "!") == 0) 1944 debugdelta = 2; 1945 1946 debuglive += debugdelta; 1947 if(debuglive >= 3) { 1948 print("liveness: %s\n", curfn->nname->sym->name); 1949 printprog(firstp); 1950 } 1951 checkptxt(fn, firstp); 1952 1953 // Construct the global liveness state. 1954 cfg = newcfg(firstp); 1955 if(debuglive >= 3) 1956 printcfg(cfg); 1957 vars = getvariables(fn); 1958 lv = newliveness(fn, firstp, cfg, vars); 1959 1960 // Run the dataflow framework. 1961 livenessprologue(lv); 1962 if(debuglive >= 3) 1963 livenessprintcfg(lv); 1964 livenesssolve(lv); 1965 if(debuglive >= 3) 1966 livenessprintcfg(lv); 1967 livenessepilogue(lv); 1968 if(debuglive >= 3) 1969 livenessprintcfg(lv); 1970 livenesscompact(lv); 1971 1972 if(debuglive >= 2) 1973 livenessprintdebug(lv); 1974 1975 // Emit the live pointer map data structures 1976 twobitwritesymbol(lv->livepointers, livesym); 1977 twobitwritesymbol(lv->argslivepointers, argssym); 1978 1979 // Free everything. 1980 freeliveness(lv); 1981 arrayfree(vars); 1982 freecfg(cfg); 1983 1984 debuglive -= debugdelta; 1985 }