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