github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/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  				// For now, only enabled when using GOEXPERIMENT=precisestack
  1491  				// during make.bash / all.bash.
  1492  				if(precisestack_enabled) {
  1493  					bvresetall(livein);
  1494  					bvandnot(liveout, any, all);
  1495  					if(!bvisempty(liveout)) {
  1496  						for(pos = 0; pos < liveout->n; pos++) {
  1497  							if(!bvget(liveout, pos))
  1498  								continue;
  1499  							bvset(all, pos); // silence future warnings in this block
  1500  							n = *(Node**)arrayget(lv->vars, pos);
  1501  							if(!n->needzero) {
  1502  								n->needzero = 1;
  1503  								if(debuglive >= 1)
  1504  									warnl(p->lineno, "%N: %lN is ambiguously live", curfn->nname, n);
  1505  								// Record in 'ambiguous' bitmap.
  1506  								xoffset = n->xoffset + stkptrsize;
  1507  								twobitwalktype1(n->type, &xoffset, ambig);
  1508  							}
  1509  						}
  1510  					}
  1511  				}
  1512  	
  1513  				// Allocate a bit vector for each class and facet of
  1514  				// value we are tracking.
  1515  	
  1516  				// Live stuff first.
  1517  				args = bvalloc(argswords() * BitsPerPointer);
  1518  				arrayadd(lv->argslivepointers, &args);
  1519  				locals = bvalloc(localswords() * BitsPerPointer);
  1520  				arrayadd(lv->livepointers, &locals);
  1521  
  1522  				if(debuglive >= 3) {
  1523  					print("%P\n", p);
  1524  					printvars("avarinitany", any, lv->vars);
  1525  				}
  1526  
  1527  				// Record any values with an "address taken" reaching
  1528  				// this code position as live. Must do now instead of below
  1529  				// because the any/all calculation requires walking forward
  1530  				// over the block (as this loop does), while the liveout
  1531  				// requires walking backward (as the next loop does).
  1532  				twobitlivepointermap(lv, any, lv->vars, args, locals);
  1533  			}
  1534  			
  1535  			if(p == bb->last)
  1536  				break;
  1537  		}
  1538  		bb->lastbitmapindex = arraylength(lv->livepointers) - 1;
  1539  	}
  1540  	
  1541  	for(i = 0; i < arraylength(lv->cfg); i++) {
  1542  		bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1543  		
  1544  		if(debuglive >= 1 && strcmp(curfn->nname->sym->name, "init") != 0 && curfn->nname->sym->name[0] != '.') {
  1545  			nmsg = arraylength(lv->livepointers);
  1546  			startmsg = nmsg;
  1547  			msg = xmalloc(nmsg*sizeof msg[0]);
  1548  			for(j=0; j<nmsg; j++)
  1549  				msg[j] = nil;
  1550  		}
  1551  
  1552  		// walk backward, emit pcdata and populate the maps
  1553  		pos = bb->lastbitmapindex;
  1554  		if(pos < 0) {
  1555  			// the first block we encounter should have the ATEXT so
  1556  			// at no point should pos ever be less than zero.
  1557  			fatal("livenessepilogue");
  1558  		}
  1559  
  1560  		bvcopy(livein, lv->liveout[bb->rpo]);
  1561  		for(p = bb->last; p != nil; p = next) {
  1562  			next = p->opt; // splicebefore modifies p->opt
  1563  			// Propagate liveness information
  1564  			progeffects(p, lv->vars, uevar, varkill, avarinit);
  1565  			bvcopy(liveout, livein);
  1566  			bvandnot(livein, liveout, varkill);
  1567  			bvor(livein, livein, uevar);
  1568  			if(debuglive >= 3 && issafepoint(p)){
  1569  				print("%P\n", p);
  1570  				printvars("uevar", uevar, lv->vars);
  1571  				printvars("varkill", varkill, lv->vars);
  1572  				printvars("livein", livein, lv->vars);
  1573  				printvars("liveout", liveout, lv->vars);
  1574  			}
  1575  			if(issafepoint(p)) {
  1576  				// Found an interesting instruction, record the
  1577  				// corresponding liveness information.  
  1578  				
  1579  				// Useful sanity check: on entry to the function,
  1580  				// the only things that can possibly be live are the
  1581  				// input parameters.
  1582  				if(p->as == ATEXT) {
  1583  					for(j = 0; j < liveout->n; j++) {
  1584  						if(!bvget(liveout, j))
  1585  							continue;
  1586  						n = *(Node**)arrayget(lv->vars, j);
  1587  						if(n->class != PPARAM)
  1588  							yyerrorl(p->lineno, "internal error: %N %lN recorded as live on entry", curfn->nname, n);
  1589  					}
  1590  				}
  1591  
  1592  				// Record live pointers.
  1593  				args = *(Bvec**)arrayget(lv->argslivepointers, pos);
  1594  				locals = *(Bvec**)arrayget(lv->livepointers, pos);
  1595  				twobitlivepointermap(lv, liveout, lv->vars, args, locals);
  1596  				
  1597  				// Ambiguously live variables are zeroed immediately after
  1598  				// function entry. Mark them live for all the non-entry bitmaps
  1599  				// so that GODEBUG=gcdead=1 mode does not poison them.
  1600  				if(p->as == ACALL)
  1601  					bvor(locals, locals, ambig);
  1602  
  1603  				// Show live pointer bitmaps.
  1604  				// We're interpreting the args and locals bitmap instead of liveout so that we
  1605  				// include the bits added by the avarinit logic in the
  1606  				// previous loop.
  1607  				if(msg != nil) {
  1608  					fmtstrinit(&fmt);
  1609  					fmtprint(&fmt, "%L: live at ", p->lineno);
  1610  					if(p->as == ACALL && p->to.node)
  1611  						fmtprint(&fmt, "call to %s:", p->to.node->sym->name);
  1612  					else if(p->as == ACALL)
  1613  						fmtprint(&fmt, "indirect call:");
  1614  					else
  1615  						fmtprint(&fmt, "entry to %s:", p->from.node->sym->name);
  1616  					numlive = 0;
  1617  					for(j = 0; j < arraylength(lv->vars); j++) {
  1618  						n = *(Node**)arrayget(lv->vars, j);
  1619  						if(islive(n, args, locals)) {
  1620  							fmtprint(&fmt, " %N", n);
  1621  							numlive++;
  1622  						}
  1623  					}
  1624  					fmtprint(&fmt, "\n");
  1625  					if(numlive == 0) // squelch message
  1626  						free(fmtstrflush(&fmt));
  1627  					else
  1628  						msg[--startmsg] = fmtstrflush(&fmt);
  1629  				}
  1630  
  1631  				// Only CALL instructions need a PCDATA annotation.
  1632  				// The TEXT instruction annotation is implicit.
  1633  				if(p->as == ACALL) {
  1634  					if(isdeferreturn(p)) {
  1635  						// runtime.deferreturn modifies its return address to return
  1636  						// back to the CALL, not to the subsequent instruction.
  1637  						// Because the return comes back one instruction early,
  1638  						// the PCDATA must begin one instruction early too.
  1639  						// The instruction before a call to deferreturn is always a
  1640  						// no-op, to keep PC-specific data unambiguous.
  1641  						splicebefore(lv, bb, newpcdataprog(p->opt, pos), p->opt);
  1642  					} else {
  1643  						splicebefore(lv, bb, newpcdataprog(p, pos), p);
  1644  					}
  1645  				}
  1646  
  1647  				pos--;
  1648  			}
  1649  		}
  1650  		if(msg != nil) {
  1651  			for(j=startmsg; j<nmsg; j++) 
  1652  				if(msg[j] != nil)
  1653  					print("%s", msg[j]);
  1654  			free(msg);
  1655  			msg = nil;
  1656  			nmsg = 0;
  1657  			startmsg = 0;
  1658  		}
  1659  	}
  1660  
  1661  	free(livein);
  1662  	free(liveout);
  1663  	free(uevar);
  1664  	free(varkill);
  1665  	free(avarinit);
  1666  	free(any);
  1667  	free(all);
  1668  	free(ambig);
  1669  	
  1670  	flusherrors();
  1671  }
  1672  
  1673  // FNV-1 hash function constants.
  1674  #define H0 2166136261UL
  1675  #define Hp 16777619UL
  1676  /*c2go
  1677  enum
  1678  {
  1679  	H0 = 2166136261,
  1680  	Hp = 16777619,
  1681  };
  1682  */
  1683  
  1684  static uint32
  1685  hashbitmap(uint32 h, Bvec *bv)
  1686  {
  1687  	uchar *p, *ep;
  1688  	
  1689  	p = (uchar*)bv->b;
  1690  	ep = p + 4*((bv->n+31)/32);
  1691  	while(p < ep)
  1692  		h = (h*Hp) ^ *p++;
  1693  	return h;
  1694  }
  1695  
  1696  // Compact liveness information by coalescing identical per-call-site bitmaps.
  1697  // The merging only happens for a single function, not across the entire binary.
  1698  //
  1699  // There are actually two lists of bitmaps, one list for the local variables and one
  1700  // list for the function arguments. Both lists are indexed by the same PCDATA
  1701  // index, so the corresponding pairs must be considered together when
  1702  // merging duplicates. The argument bitmaps change much less often during
  1703  // function execution than the local variable bitmaps, so it is possible that
  1704  // we could introduce a separate PCDATA index for arguments vs locals and
  1705  // then compact the set of argument bitmaps separately from the set of
  1706  // local variable bitmaps. As of 2014-04-02, doing this to the godoc binary
  1707  // is actually a net loss: we save about 50k of argument bitmaps but the new
  1708  // PCDATA tables cost about 100k. So for now we keep using a single index for
  1709  // both bitmap lists.
  1710  static void
  1711  livenesscompact(Liveness *lv)
  1712  {
  1713  	int *table, *remap, i, j, n, tablesize, uniq;
  1714  	uint32 h;
  1715  	Bvec *local, *arg, *jlocal, *jarg;
  1716  	Prog *p;
  1717  
  1718  	// Linear probing hash table of bitmaps seen so far.
  1719  	// The hash table has 4n entries to keep the linear
  1720  	// scan short. An entry of -1 indicates an empty slot.
  1721  	n = arraylength(lv->livepointers);
  1722  	tablesize = 4*n;
  1723  	table = xmalloc(tablesize*sizeof table[0]);
  1724  	memset(table, 0xff, tablesize*sizeof table[0]);
  1725  	
  1726  	// remap[i] = the new index of the old bit vector #i.
  1727  	remap = xmalloc(n*sizeof remap[0]);
  1728  	memset(remap, 0xff, n*sizeof remap[0]);
  1729  	uniq = 0; // unique tables found so far
  1730  
  1731  	// Consider bit vectors in turn.
  1732  	// If new, assign next number using uniq,
  1733  	// record in remap, record in lv->livepointers and lv->argslivepointers
  1734  	// under the new index, and add entry to hash table.
  1735  	// If already seen, record earlier index in remap and free bitmaps.
  1736  	for(i=0; i<n; i++) {
  1737  		local = *(Bvec**)arrayget(lv->livepointers, i);
  1738  		arg = *(Bvec**)arrayget(lv->argslivepointers, i);
  1739  		h = hashbitmap(hashbitmap(H0, local), arg) % tablesize;
  1740  
  1741  		for(;;) {
  1742  			j = table[h];
  1743  			if(j < 0)
  1744  				break;
  1745  			jlocal = *(Bvec**)arrayget(lv->livepointers, j);
  1746  			jarg = *(Bvec**)arrayget(lv->argslivepointers, j);
  1747  			if(bvcmp(local, jlocal) == 0 && bvcmp(arg, jarg) == 0) {
  1748  				free(local);
  1749  				free(arg);
  1750  				remap[i] = j;
  1751  				goto Next;
  1752  			}
  1753  			if(++h == tablesize)
  1754  				h = 0;
  1755  		}
  1756  		table[h] = uniq;
  1757  		remap[i] = uniq;
  1758  		*(Bvec**)arrayget(lv->livepointers, uniq) = local;
  1759  		*(Bvec**)arrayget(lv->argslivepointers, uniq) = arg;
  1760  		uniq++;
  1761  	Next:;
  1762  	}
  1763  
  1764  	// We've already reordered lv->livepointers[0:uniq]
  1765  	// and lv->argslivepointers[0:uniq] and freed the bitmaps
  1766  	// we don't need anymore. Clear the pointers later in the
  1767  	// array so that we can tell where the coalesced bitmaps stop
  1768  	// and so that we don't double-free when cleaning up.
  1769  	for(j=uniq; j<n; j++) {
  1770  		*(Bvec**)arrayget(lv->livepointers, j) = nil;
  1771  		*(Bvec**)arrayget(lv->argslivepointers, j) = nil;
  1772  	}
  1773  	
  1774  	// Rewrite PCDATA instructions to use new numbering.
  1775  	for(p=lv->ptxt; p != P; p=p->link) {
  1776  		if(p->as == APCDATA && p->from.offset == PCDATA_StackMapIndex) {
  1777  			i = p->to.offset;
  1778  			if(i >= 0)
  1779  				p->to.offset = remap[i];
  1780  		}
  1781  	}
  1782  
  1783  	free(table);
  1784  	free(remap);
  1785  }
  1786  
  1787  static int
  1788  printbitset(int printed, char *name, Array *vars, Bvec *bits)
  1789  {
  1790  	int i, started;
  1791  	Node *n;
  1792  
  1793  	started = 0;	
  1794  	for(i=0; i<arraylength(vars); i++) {
  1795  		if(!bvget(bits, i))
  1796  			continue;
  1797  		if(!started) {
  1798  			if(!printed)
  1799  				print("\t");
  1800  			else
  1801  				print(" ");
  1802  			started = 1;
  1803  			printed = 1;
  1804  			print("%s=", name);
  1805  		} else {
  1806  			print(",");
  1807  		}
  1808  		n = *(Node**)arrayget(vars, i);
  1809  		print("%s", n->sym->name);
  1810  	}
  1811  	return printed;
  1812  }
  1813  
  1814  // Prints the computed liveness information and inputs, for debugging.
  1815  // This format synthesizes the information used during the multiple passes
  1816  // into a single presentation.
  1817  static void
  1818  livenessprintdebug(Liveness *lv)
  1819  {
  1820  	int i, j, pcdata, printed;
  1821  	BasicBlock *bb;
  1822  	Prog *p;
  1823  	Bvec *uevar, *varkill, *avarinit, *args, *locals;
  1824  	Node *n;
  1825  
  1826  	print("liveness: %s\n", curfn->nname->sym->name);
  1827  
  1828  	uevar = bvalloc(arraylength(lv->vars));
  1829  	varkill = bvalloc(arraylength(lv->vars));
  1830  	avarinit = bvalloc(arraylength(lv->vars));
  1831  
  1832  	pcdata = 0;
  1833  	for(i = 0; i < arraylength(lv->cfg); i++) {
  1834  		if(i > 0)
  1835  			print("\n");
  1836  		bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1837  
  1838  		// bb#0 pred=1,2 succ=3,4
  1839  		print("bb#%d pred=", i);
  1840  		for(j = 0; j < arraylength(bb->pred); j++) {
  1841  			if(j > 0)
  1842  				print(",");
  1843  			print("%d", (*(BasicBlock**)arrayget(bb->pred, j))->rpo);
  1844  		}
  1845  		print(" succ=");
  1846  		for(j = 0; j < arraylength(bb->succ); j++) {
  1847  			if(j > 0)
  1848  				print(",");
  1849  			print("%d", (*(BasicBlock**)arrayget(bb->succ, j))->rpo);
  1850  		}
  1851  		print("\n");
  1852  		
  1853  		// initial settings
  1854  		printed = 0;
  1855  		printed = printbitset(printed, "uevar", lv->vars, lv->uevar[bb->rpo]);
  1856  		printed = printbitset(printed, "livein", lv->vars, lv->livein[bb->rpo]);
  1857  		if(printed)
  1858  			print("\n");
  1859  		
  1860  		// program listing, with individual effects listed
  1861  		for(p = bb->first;; p = p->link) {
  1862  			print("%P\n", p);
  1863  			if(p->as == APCDATA && p->from.offset == PCDATA_StackMapIndex)
  1864  				pcdata = p->to.offset;
  1865  			progeffects(p, lv->vars, uevar, varkill, avarinit);
  1866  			printed = 0;
  1867  			printed = printbitset(printed, "uevar", lv->vars, uevar);
  1868  			printed = printbitset(printed, "varkill", lv->vars, varkill);
  1869  			printed = printbitset(printed, "avarinit", lv->vars, avarinit);
  1870  			if(printed)
  1871  				print("\n");
  1872  			if(issafepoint(p)) {
  1873  				args = *(Bvec**)arrayget(lv->argslivepointers, pcdata);
  1874  				locals = *(Bvec**)arrayget(lv->livepointers, pcdata);
  1875  				print("\tlive=");
  1876  				printed = 0;
  1877  				for(j = 0; j < arraylength(lv->vars); j++) {
  1878  					n = *(Node**)arrayget(lv->vars, j);
  1879  					if(islive(n, args, locals)) {
  1880  						if(printed++)
  1881  							print(",");
  1882  						print("%N", n);
  1883  					}
  1884  				}
  1885  				print("\n");
  1886  			}
  1887  			if(p == bb->last)
  1888  				break;
  1889  		}
  1890  		
  1891  		// bb bitsets
  1892  		print("end\n");
  1893  		printed = printbitset(printed, "varkill", lv->vars, lv->varkill[bb->rpo]);
  1894  		printed = printbitset(printed, "liveout", lv->vars, lv->liveout[bb->rpo]);
  1895  		printed = printbitset(printed, "avarinit", lv->vars, lv->avarinit[bb->rpo]);
  1896  		printed = printbitset(printed, "avarinitany", lv->vars, lv->avarinitany[bb->rpo]);
  1897  		printed = printbitset(printed, "avarinitall", lv->vars, lv->avarinitall[bb->rpo]);
  1898  		if(printed)
  1899  			print("\n");
  1900  	}
  1901  	print("\n");
  1902  
  1903  	free(uevar);
  1904  	free(varkill);
  1905  	free(avarinit);
  1906  }
  1907  
  1908  // Dumps an array of bitmaps to a symbol as a sequence of uint32 values.  The
  1909  // first word dumped is the total number of bitmaps.  The second word is the
  1910  // length of the bitmaps.  All bitmaps are assumed to be of equal length.  The
  1911  // words that are followed are the raw bitmap words.  The arr argument is an
  1912  // array of Node*s.
  1913  static void
  1914  twobitwritesymbol(Array *arr, Sym *sym)
  1915  {
  1916  	Bvec *bv;
  1917  	int off, i, j, len;
  1918  	uint32 word;
  1919  
  1920  	len = arraylength(arr);
  1921  	off = 0;
  1922  	off += 4; // number of bitmaps, to fill in later
  1923  	bv = *(Bvec**)arrayget(arr, 0);
  1924  	off = duint32(sym, off, bv->n); // number of bits in each bitmap
  1925  	for(i = 0; i < len; i++) {
  1926  		// bitmap words
  1927  		bv = *(Bvec**)arrayget(arr, i);
  1928  		if(bv == nil)
  1929  			break;
  1930  		for(j = 0; j < bv->n; j += 32) {
  1931  			word = bv->b[j/32];
  1932  			// Runtime reads the bitmaps as byte arrays. Oblige.
  1933  			off = duint8(sym, off, word);
  1934  			off = duint8(sym, off, word>>8);
  1935  			off = duint8(sym, off, word>>16);
  1936  			off = duint8(sym, off, word>>24);
  1937  		}
  1938  	}
  1939  	duint32(sym, 0, i); // number of bitmaps
  1940  	ggloblsym(sym, off, RODATA);
  1941  }
  1942  
  1943  static void
  1944  printprog(Prog *p)
  1945  {
  1946  	while(p != nil) {
  1947  		print("%P\n", p);
  1948  		p = p->link;
  1949  	}
  1950  }
  1951  
  1952  // Entry pointer for liveness analysis.  Constructs a complete CFG, solves for
  1953  // the liveness of pointer variables in the function, and emits a runtime data
  1954  // structure read by the garbage collector.
  1955  void
  1956  liveness(Node *fn, Prog *firstp, Sym *argssym, Sym *livesym)
  1957  {
  1958  	Array *cfg, *vars;
  1959  	Liveness *lv;
  1960  	int debugdelta;
  1961  	NodeList *l;
  1962  
  1963  	// Change name to dump debugging information only for a specific function.
  1964  	debugdelta = 0;
  1965  	if(strcmp(curfn->nname->sym->name, "!") == 0)
  1966  		debugdelta = 2;
  1967  	
  1968  	debuglive += debugdelta;
  1969  	if(debuglive >= 3) {
  1970  		print("liveness: %s\n", curfn->nname->sym->name);
  1971  		printprog(firstp);
  1972  	}
  1973  	checkptxt(fn, firstp);
  1974  
  1975  	// Construct the global liveness state.
  1976  	cfg = newcfg(firstp);
  1977  	if(debuglive >= 3)
  1978  		printcfg(cfg);
  1979  	vars = getvariables(fn);
  1980  	lv = newliveness(fn, firstp, cfg, vars);
  1981  
  1982  	// Run the dataflow framework.
  1983  	livenessprologue(lv);
  1984  	if(debuglive >= 3)
  1985  		livenessprintcfg(lv);
  1986  	livenesssolve(lv);
  1987  	if(debuglive >= 3)
  1988  		livenessprintcfg(lv);
  1989  	livenessepilogue(lv);
  1990  	if(debuglive >= 3)
  1991  		livenessprintcfg(lv);
  1992  	livenesscompact(lv);
  1993  
  1994  	if(debuglive >= 2)
  1995  		livenessprintdebug(lv);
  1996  
  1997  	// Emit the live pointer map data structures
  1998  	twobitwritesymbol(lv->livepointers, livesym);
  1999  	twobitwritesymbol(lv->argslivepointers, argssym);
  2000  
  2001  	// Free everything.
  2002  	for(l=fn->dcl; l != nil; l = l->next)
  2003  		if(l->n != N)
  2004  			l->n->opt = nil;
  2005  	freeliveness(lv);
  2006  	arrayfree(vars);
  2007  	freecfg(cfg);
  2008  	
  2009  	debuglive -= debugdelta;
  2010  }