github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/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
  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
  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
  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) + 0);
  1127  		bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 1); // 3 = multiword
  1128  		// next word contains 2 = Iface, 3 = Eface
  1129  		if(isnilinter(t)) {
  1130  			bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 2);
  1131  			bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 3);
  1132  		} else {
  1133  			bvset(bv, ((*xoffset / widthptr) * BitsPerPointer) + 3);
  1134  		}
  1135  		*xoffset += t->width;
  1136  		break;
  1137  
  1138  	case TARRAY:
  1139  		// The value of t->bound is -1 for slices types and >0 for
  1140  		// for fixed array types.  All other values are invalid.
  1141  		if(t->bound < -1)
  1142  			fatal("twobitwalktype1: invalid bound, %T", t);
  1143  		if(isslice(t)) {
  1144  			// struct { byte *array; uintgo len; uintgo cap; }
  1145  			if((*xoffset & (widthptr-1)) != 0)
  1146  				fatal("twobitwalktype1: invalid TARRAY alignment, %T", t);
  1147  			bvset(bv, (*xoffset / widthptr) * BitsPerPointer + 1); // 2 = live ptr in first slot
  1148  			*xoffset += t->width;
  1149  		} else
  1150  			for(i = 0; i < t->bound; i++)
  1151  				twobitwalktype1(t->type, xoffset, bv);
  1152  		break;
  1153  
  1154  	case TSTRUCT:
  1155  		o = 0;
  1156  		for(t1 = t->type; t1 != T; t1 = t1->down) {
  1157  			fieldoffset = t1->width;
  1158  			*xoffset += fieldoffset - o;
  1159  			twobitwalktype1(t1->type, xoffset, bv);
  1160  			o = fieldoffset + t1->type->width;
  1161  		}
  1162  		*xoffset += t->width - o;
  1163  		break;
  1164  
  1165  	default:
  1166  		fatal("twobitwalktype1: unexpected type, %T", t);
  1167  	}
  1168  }
  1169  
  1170  // Returns the number of words of local variables.
  1171  static int32
  1172  localswords(void)
  1173  {
  1174  	return stkptrsize / widthptr;
  1175  }
  1176  
  1177  // Returns the number of words of in and out arguments.
  1178  static int32
  1179  argswords(void)
  1180  {
  1181  	return curfn->type->argwid / widthptr;
  1182  }
  1183  
  1184  // Generates live pointer value maps for arguments and local variables.  The
  1185  // this argument and the in arguments are always assumed live.  The vars
  1186  // argument is an array of Node*s.
  1187  static void
  1188  twobitlivepointermap(Liveness *lv, Bvec *liveout, Array *vars, Bvec *args, Bvec *locals)
  1189  {
  1190  	Node *node;
  1191  	Type *thisargtype;
  1192  	Type *inargtype;
  1193  	vlong xoffset;
  1194  	int32 i;
  1195  
  1196  	for(i = 0; (i = bvnext(liveout, i)) >= 0; i++) {
  1197  		node = *(Node**)arrayget(vars, i);
  1198  		switch(node->class) {
  1199  		case PAUTO:
  1200  			xoffset = node->xoffset + stkptrsize;
  1201  			twobitwalktype1(node->type, &xoffset, locals);
  1202  			break;
  1203  		case PPARAM:
  1204  		case PPARAMOUT:
  1205  			xoffset = node->xoffset;
  1206  			twobitwalktype1(node->type, &xoffset, args);
  1207  			break;
  1208  		}
  1209  	}
  1210  	
  1211  	// The node list only contains declared names.
  1212  	// If the receiver or arguments are unnamed, they will be omitted
  1213  	// from the list above. Preserve those values - even though they are unused -
  1214  	// in order to keep their addresses live for use in stack traces.
  1215  	thisargtype = getthisx(lv->fn->type);
  1216  	if(thisargtype != nil) {
  1217  		xoffset = 0;
  1218  		twobitwalktype1(thisargtype, &xoffset, args);
  1219  	}
  1220  	inargtype = getinargx(lv->fn->type);
  1221  	if(inargtype != nil) {
  1222  		xoffset = 0;
  1223  		twobitwalktype1(inargtype, &xoffset, args);
  1224  	}
  1225  }
  1226  
  1227  // Construct a disembodied instruction.
  1228  static Prog*
  1229  unlinkedprog(int as)
  1230  {
  1231  	Prog *p;
  1232  
  1233  	p = mal(sizeof(*p));
  1234  	clearp(p);
  1235  	p->as = as;
  1236  	return p;
  1237  }
  1238  
  1239  // Construct a new PCDATA instruction associated with and for the purposes of
  1240  // covering an existing instruction.
  1241  static Prog*
  1242  newpcdataprog(Prog *prog, int32 index)
  1243  {
  1244  	Node from, to;
  1245  	Prog *pcdata;
  1246  
  1247  	nodconst(&from, types[TINT32], PCDATA_StackMapIndex);
  1248  	nodconst(&to, types[TINT32], index);
  1249  	pcdata = unlinkedprog(APCDATA);
  1250  	pcdata->lineno = prog->lineno;
  1251  	naddr(&from, &pcdata->from, 0);
  1252  	naddr(&to, &pcdata->to, 0);
  1253  	return pcdata;
  1254  }
  1255  
  1256  // Returns true for instructions that are safe points that must be annotated
  1257  // with liveness information.
  1258  static int
  1259  issafepoint(Prog *prog)
  1260  {
  1261  	return prog->as == ATEXT || prog->as == ACALL;
  1262  }
  1263  
  1264  // Initializes the sets for solving the live variables.  Visits all the
  1265  // instructions in each basic block to summarizes the information at each basic
  1266  // block
  1267  static void
  1268  livenessprologue(Liveness *lv)
  1269  {
  1270  	BasicBlock *bb;
  1271  	Bvec *uevar, *varkill, *avarinit;
  1272  	Prog *p;
  1273  	int32 i;
  1274  	int32 nvars;
  1275  
  1276  	nvars = arraylength(lv->vars);
  1277  	uevar = bvalloc(nvars);
  1278  	varkill = bvalloc(nvars);
  1279  	avarinit = bvalloc(nvars);
  1280  	for(i = 0; i < arraylength(lv->cfg); i++) {
  1281  		bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1282  		// Walk the block instructions backward and update the block
  1283  		// effects with the each prog effects.
  1284  		for(p = bb->last; p != nil; p = p->opt) {
  1285  			progeffects(p, lv->vars, uevar, varkill, avarinit);
  1286  			if(debuglive >= 3)
  1287  				printeffects(p, uevar, varkill, avarinit);
  1288  			bvor(lv->varkill[i], lv->varkill[i], varkill);
  1289  			bvandnot(lv->uevar[i], lv->uevar[i], varkill);
  1290  			bvor(lv->uevar[i], lv->uevar[i], uevar);			
  1291  		}
  1292  		// Walk the block instructions forward to update avarinit bits.
  1293  		// avarinit describes the effect at the end of the block, not the beginning.
  1294  		bvresetall(varkill);
  1295  		for(p = bb->first;; p = p->link) {
  1296  			progeffects(p, lv->vars, uevar, varkill, avarinit);
  1297  			if(debuglive >= 3)
  1298  				printeffects(p, uevar, varkill, avarinit);
  1299  			bvandnot(lv->avarinit[i], lv->avarinit[i], varkill);
  1300  			bvor(lv->avarinit[i], lv->avarinit[i], avarinit);
  1301  			if(p == bb->last)
  1302  				break;
  1303  		}
  1304  	}
  1305  	free(uevar);
  1306  	free(varkill);
  1307  	free(avarinit);
  1308  }
  1309  
  1310  // Solve the liveness dataflow equations.
  1311  static void
  1312  livenesssolve(Liveness *lv)
  1313  {
  1314  	BasicBlock *bb, *succ, *pred;
  1315  	Bvec *newlivein, *newliveout, *any, *all;
  1316  	int32 rpo, i, j, change;
  1317  
  1318  	// These temporary bitvectors exist to avoid successive allocations and
  1319  	// frees within the loop.
  1320  	newlivein = bvalloc(arraylength(lv->vars));
  1321  	newliveout = bvalloc(arraylength(lv->vars));
  1322  	any = bvalloc(arraylength(lv->vars));
  1323  	all = bvalloc(arraylength(lv->vars));
  1324  
  1325  	// Push avarinitall, avarinitany forward.
  1326  	// avarinitall says the addressed var is initialized along all paths reaching the block exit.
  1327  	// avarinitany says the addressed var is initialized along some path reaching the block exit.
  1328  	for(i = 0; i < arraylength(lv->cfg); i++) {
  1329  		bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1330  		rpo = bb->rpo;
  1331  		if(i == 0)
  1332  			bvcopy(lv->avarinitall[rpo], lv->avarinit[rpo]);
  1333  		else {
  1334  			bvresetall(lv->avarinitall[rpo]);
  1335  			bvnot(lv->avarinitall[rpo]);
  1336  		}
  1337  		bvcopy(lv->avarinitany[rpo], lv->avarinit[rpo]);
  1338  	}
  1339  
  1340  	change = 1;
  1341  	while(change != 0) {
  1342  		change = 0;
  1343  		for(i = 0; i < arraylength(lv->cfg); i++) {
  1344  			bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1345  			rpo = bb->rpo;
  1346  			bvresetall(any);
  1347  			bvresetall(all);
  1348  			for(j = 0; j < arraylength(bb->pred); j++) {
  1349  				pred = *(BasicBlock**)arrayget(bb->pred, j);
  1350  				if(j == 0) {
  1351  					bvcopy(any, lv->avarinitany[pred->rpo]);
  1352  					bvcopy(all, lv->avarinitall[pred->rpo]);
  1353  				} else {
  1354  					bvor(any, any, lv->avarinitany[pred->rpo]);
  1355  					bvand(all, all, lv->avarinitall[pred->rpo]);
  1356  				}
  1357  			}
  1358  			bvandnot(any, any, lv->varkill[rpo]);
  1359  			bvandnot(all, all, lv->varkill[rpo]);
  1360  			bvor(any, any, lv->avarinit[rpo]);
  1361  			bvor(all, all, lv->avarinit[rpo]);
  1362  			if(bvcmp(any, lv->avarinitany[rpo])) {
  1363  				change = 1;
  1364  				bvcopy(lv->avarinitany[rpo], any);
  1365  			}
  1366  			if(bvcmp(all, lv->avarinitall[rpo])) {
  1367  				change = 1;
  1368  				bvcopy(lv->avarinitall[rpo], all);
  1369  			}
  1370  		}
  1371  	}
  1372  
  1373  	// Iterate through the blocks in reverse round-robin fashion.  A work
  1374  	// queue might be slightly faster.  As is, the number of iterations is
  1375  	// so low that it hardly seems to be worth the complexity.
  1376  	change = 1;
  1377  	while(change != 0) {
  1378  		change = 0;
  1379  		// Walk blocks in the general direction of propagation.  This
  1380  		// improves convergence.
  1381  		for(i = arraylength(lv->cfg) - 1; i >= 0; i--) {
  1382  			// A variable is live on output from this block
  1383  			// if it is live on input to some successor.
  1384  			//
  1385  			// out[b] = \bigcup_{s \in succ[b]} in[s]
  1386  			bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1387  			rpo = bb->rpo;
  1388  			bvresetall(newliveout);
  1389  			for(j = 0; j < arraylength(bb->succ); j++) {
  1390  				succ = *(BasicBlock**)arrayget(bb->succ, j);
  1391  				bvor(newliveout, newliveout, lv->livein[succ->rpo]);
  1392  			}
  1393  			if(bvcmp(lv->liveout[rpo], newliveout)) {
  1394  				change = 1;
  1395  				bvcopy(lv->liveout[rpo], newliveout);
  1396  			}
  1397  
  1398  			// A variable is live on input to this block
  1399  			// if it is live on output from this block and
  1400  			// not set by the code in this block.
  1401  			//
  1402  			// in[b] = uevar[b] \cup (out[b] \setminus varkill[b])
  1403  			bvandnot(newlivein, lv->liveout[rpo], lv->varkill[rpo]);
  1404  			bvor(lv->livein[rpo], newlivein, lv->uevar[rpo]);
  1405  		}
  1406  	}
  1407  
  1408  	free(newlivein);
  1409  	free(newliveout);
  1410  	free(any);
  1411  	free(all);
  1412  }
  1413  
  1414  // This function is slow but it is only used for generating debug prints.
  1415  // Check whether n is marked live in args/locals.
  1416  static int
  1417  islive(Node *n, Bvec *args, Bvec *locals)
  1418  {
  1419  	int i;
  1420  
  1421  	switch(n->class) {
  1422  	case PPARAM:
  1423  	case PPARAMOUT:
  1424  		for(i = 0; i < n->type->width/widthptr*BitsPerPointer; i++)
  1425  			if(bvget(args, n->xoffset/widthptr*BitsPerPointer + i))
  1426  				return 1;
  1427  		break;
  1428  	case PAUTO:
  1429  		for(i = 0; i < n->type->width/widthptr*BitsPerPointer; i++)
  1430  			if(bvget(locals, (n->xoffset + stkptrsize)/widthptr*BitsPerPointer + i))
  1431  				return 1;
  1432  		break;
  1433  	}
  1434  	return 0;
  1435  }
  1436  
  1437  // Visits all instructions in a basic block and computes a bit vector of live
  1438  // variables at each safe point locations.
  1439  static void
  1440  livenessepilogue(Liveness *lv)
  1441  {
  1442  	BasicBlock *bb, *pred;
  1443  	Bvec *ambig, *livein, *liveout, *uevar, *varkill, *args, *locals, *avarinit, *any, *all;
  1444  	Node *n;
  1445  	Prog *p, *next;
  1446  	int32 i, j, numlive, startmsg, nmsg, nvars, pos;
  1447  	vlong xoffset;
  1448  	char **msg;
  1449  	Fmt fmt;
  1450  
  1451  	nvars = arraylength(lv->vars);
  1452  	livein = bvalloc(nvars);
  1453  	liveout = bvalloc(nvars);
  1454  	uevar = bvalloc(nvars);
  1455  	varkill = bvalloc(nvars);
  1456  	avarinit = bvalloc(nvars);
  1457  	any = bvalloc(nvars);
  1458  	all = bvalloc(nvars);
  1459  	ambig = bvalloc(localswords() * BitsPerPointer);
  1460  	msg = nil;
  1461  	nmsg = 0;
  1462  	startmsg = 0;
  1463  
  1464  	for(i = 0; i < arraylength(lv->cfg); i++) {
  1465  		bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1466  		
  1467  		// Compute avarinitany and avarinitall for entry to block.
  1468  		// This duplicates information known during livenesssolve
  1469  		// but avoids storing two more vectors for each block.
  1470  		bvresetall(any);
  1471  		bvresetall(all);
  1472  		for(j = 0; j < arraylength(bb->pred); j++) {
  1473  			pred = *(BasicBlock**)arrayget(bb->pred, j);
  1474  			if(j == 0) {
  1475  				bvcopy(any, lv->avarinitany[pred->rpo]);
  1476  				bvcopy(all, lv->avarinitall[pred->rpo]);
  1477  			} else {
  1478  				bvor(any, any, lv->avarinitany[pred->rpo]);
  1479  				bvand(all, all, lv->avarinitall[pred->rpo]);
  1480  			}
  1481  		}
  1482  
  1483  		// Walk forward through the basic block instructions and
  1484  		// allocate liveness maps for those instructions that need them.
  1485  		// Seed the maps with information about the addrtaken variables.
  1486  		for(p = bb->first;; p = p->link) {
  1487  			progeffects(p, lv->vars, uevar, varkill, avarinit);
  1488  			bvandnot(any, any, varkill);
  1489  			bvandnot(all, all, varkill);
  1490  			bvor(any, any, avarinit);
  1491  			bvor(all, all, avarinit);
  1492  
  1493  			if(issafepoint(p)) {
  1494  				// Annotate ambiguously live variables so that they can
  1495  				// be zeroed at function entry.
  1496  				// livein and liveout are dead here and used as temporaries.
  1497  				// For now, only enabled when using GOEXPERIMENT=precisestack
  1498  				// during make.bash / all.bash.
  1499  				if(precisestack_enabled) {
  1500  					bvresetall(livein);
  1501  					bvandnot(liveout, any, all);
  1502  					if(!bvisempty(liveout)) {
  1503  						for(pos = 0; pos < liveout->n; pos++) {
  1504  							if(!bvget(liveout, pos))
  1505  								continue;
  1506  							bvset(all, pos); // silence future warnings in this block
  1507  							n = *(Node**)arrayget(lv->vars, pos);
  1508  							if(!n->needzero) {
  1509  								n->needzero = 1;
  1510  								if(debuglive >= 1)
  1511  									warnl(p->lineno, "%N: %lN is ambiguously live", curfn->nname, n);
  1512  								// Record in 'ambiguous' bitmap.
  1513  								xoffset = n->xoffset + stkptrsize;
  1514  								twobitwalktype1(n->type, &xoffset, ambig);
  1515  							}
  1516  						}
  1517  					}
  1518  				}
  1519  	
  1520  				// Allocate a bit vector for each class and facet of
  1521  				// value we are tracking.
  1522  	
  1523  				// Live stuff first.
  1524  				args = bvalloc(argswords() * BitsPerPointer);
  1525  				arrayadd(lv->argslivepointers, &args);
  1526  				locals = bvalloc(localswords() * BitsPerPointer);
  1527  				arrayadd(lv->livepointers, &locals);
  1528  
  1529  				if(debuglive >= 3) {
  1530  					print("%P\n", p);
  1531  					printvars("avarinitany", any, lv->vars);
  1532  				}
  1533  
  1534  				// Record any values with an "address taken" reaching
  1535  				// this code position as live. Must do now instead of below
  1536  				// because the any/all calculation requires walking forward
  1537  				// over the block (as this loop does), while the liveout
  1538  				// requires walking backward (as the next loop does).
  1539  				twobitlivepointermap(lv, any, lv->vars, args, locals);
  1540  			}
  1541  			
  1542  			if(p == bb->last)
  1543  				break;
  1544  		}
  1545  		bb->lastbitmapindex = arraylength(lv->livepointers) - 1;
  1546  	}
  1547  	
  1548  	for(i = 0; i < arraylength(lv->cfg); i++) {
  1549  		bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1550  		
  1551  		if(debuglive >= 1 && strcmp(curfn->nname->sym->name, "init") != 0 && curfn->nname->sym->name[0] != '.') {
  1552  			nmsg = arraylength(lv->livepointers);
  1553  			startmsg = nmsg;
  1554  			msg = xmalloc(nmsg*sizeof msg[0]);
  1555  			for(j=0; j<nmsg; j++)
  1556  				msg[j] = nil;
  1557  		}
  1558  
  1559  		// walk backward, emit pcdata and populate the maps
  1560  		pos = bb->lastbitmapindex;
  1561  		if(pos < 0) {
  1562  			// the first block we encounter should have the ATEXT so
  1563  			// at no point should pos ever be less than zero.
  1564  			fatal("livenessepilogue");
  1565  		}
  1566  
  1567  		bvcopy(livein, lv->liveout[bb->rpo]);
  1568  		for(p = bb->last; p != nil; p = next) {
  1569  			next = p->opt; // splicebefore modifies p->opt
  1570  			// Propagate liveness information
  1571  			progeffects(p, lv->vars, uevar, varkill, avarinit);
  1572  			bvcopy(liveout, livein);
  1573  			bvandnot(livein, liveout, varkill);
  1574  			bvor(livein, livein, uevar);
  1575  			if(debuglive >= 3 && issafepoint(p)){
  1576  				print("%P\n", p);
  1577  				printvars("uevar", uevar, lv->vars);
  1578  				printvars("varkill", varkill, lv->vars);
  1579  				printvars("livein", livein, lv->vars);
  1580  				printvars("liveout", liveout, lv->vars);
  1581  			}
  1582  			if(issafepoint(p)) {
  1583  				// Found an interesting instruction, record the
  1584  				// corresponding liveness information.  
  1585  				
  1586  				// Useful sanity check: on entry to the function,
  1587  				// the only things that can possibly be live are the
  1588  				// input parameters.
  1589  				if(p->as == ATEXT) {
  1590  					for(j = 0; j < liveout->n; j++) {
  1591  						if(!bvget(liveout, j))
  1592  							continue;
  1593  						n = *(Node**)arrayget(lv->vars, j);
  1594  						if(n->class != PPARAM)
  1595  							yyerrorl(p->lineno, "internal error: %N %lN recorded as live on entry", curfn->nname, n);
  1596  					}
  1597  				}
  1598  
  1599  				// Record live pointers.
  1600  				args = *(Bvec**)arrayget(lv->argslivepointers, pos);
  1601  				locals = *(Bvec**)arrayget(lv->livepointers, pos);
  1602  				twobitlivepointermap(lv, liveout, lv->vars, args, locals);
  1603  				
  1604  				// Ambiguously live variables are zeroed immediately after
  1605  				// function entry. Mark them live for all the non-entry bitmaps
  1606  				// so that GODEBUG=gcdead=1 mode does not poison them.
  1607  				if(p->as == ACALL)
  1608  					bvor(locals, locals, ambig);
  1609  
  1610  				// Show live pointer bitmaps.
  1611  				// We're interpreting the args and locals bitmap instead of liveout so that we
  1612  				// include the bits added by the avarinit logic in the
  1613  				// previous loop.
  1614  				if(msg != nil) {
  1615  					fmtstrinit(&fmt);
  1616  					fmtprint(&fmt, "%L: live at ", p->lineno);
  1617  					if(p->as == ACALL && p->to.node)
  1618  						fmtprint(&fmt, "call to %s:", p->to.node->sym->name);
  1619  					else if(p->as == ACALL)
  1620  						fmtprint(&fmt, "indirect call:");
  1621  					else
  1622  						fmtprint(&fmt, "entry to %s:", p->from.node->sym->name);
  1623  					numlive = 0;
  1624  					for(j = 0; j < arraylength(lv->vars); j++) {
  1625  						n = *(Node**)arrayget(lv->vars, j);
  1626  						if(islive(n, args, locals)) {
  1627  							fmtprint(&fmt, " %N", n);
  1628  							numlive++;
  1629  						}
  1630  					}
  1631  					fmtprint(&fmt, "\n");
  1632  					if(numlive == 0) // squelch message
  1633  						free(fmtstrflush(&fmt));
  1634  					else
  1635  						msg[--startmsg] = fmtstrflush(&fmt);
  1636  				}
  1637  
  1638  				// Only CALL instructions need a PCDATA annotation.
  1639  				// The TEXT instruction annotation is implicit.
  1640  				if(p->as == ACALL) {
  1641  					if(isdeferreturn(p)) {
  1642  						// runtime.deferreturn modifies its return address to return
  1643  						// back to the CALL, not to the subsequent instruction.
  1644  						// Because the return comes back one instruction early,
  1645  						// the PCDATA must begin one instruction early too.
  1646  						// The instruction before a call to deferreturn is always a
  1647  						// no-op, to keep PC-specific data unambiguous.
  1648  						splicebefore(lv, bb, newpcdataprog(p->opt, pos), p->opt);
  1649  					} else {
  1650  						splicebefore(lv, bb, newpcdataprog(p, pos), p);
  1651  					}
  1652  				}
  1653  
  1654  				pos--;
  1655  			}
  1656  		}
  1657  		if(msg != nil) {
  1658  			for(j=startmsg; j<nmsg; j++) 
  1659  				if(msg[j] != nil)
  1660  					print("%s", msg[j]);
  1661  			free(msg);
  1662  			msg = nil;
  1663  			nmsg = 0;
  1664  			startmsg = 0;
  1665  		}
  1666  	}
  1667  
  1668  	free(livein);
  1669  	free(liveout);
  1670  	free(uevar);
  1671  	free(varkill);
  1672  	free(avarinit);
  1673  	free(any);
  1674  	free(all);
  1675  	free(ambig);
  1676  	
  1677  	flusherrors();
  1678  }
  1679  
  1680  // FNV-1 hash function constants.
  1681  #define H0 2166136261UL
  1682  #define Hp 16777619UL
  1683  /*c2go
  1684  enum
  1685  {
  1686  	H0 = 2166136261,
  1687  	Hp = 16777619,
  1688  };
  1689  */
  1690  
  1691  static uint32
  1692  hashbitmap(uint32 h, Bvec *bv)
  1693  {
  1694  	uchar *p, *ep;
  1695  	
  1696  	p = (uchar*)bv->b;
  1697  	ep = p + 4*((bv->n+31)/32);
  1698  	while(p < ep)
  1699  		h = (h*Hp) ^ *p++;
  1700  	return h;
  1701  }
  1702  
  1703  // Compact liveness information by coalescing identical per-call-site bitmaps.
  1704  // The merging only happens for a single function, not across the entire binary.
  1705  //
  1706  // There are actually two lists of bitmaps, one list for the local variables and one
  1707  // list for the function arguments. Both lists are indexed by the same PCDATA
  1708  // index, so the corresponding pairs must be considered together when
  1709  // merging duplicates. The argument bitmaps change much less often during
  1710  // function execution than the local variable bitmaps, so it is possible that
  1711  // we could introduce a separate PCDATA index for arguments vs locals and
  1712  // then compact the set of argument bitmaps separately from the set of
  1713  // local variable bitmaps. As of 2014-04-02, doing this to the godoc binary
  1714  // is actually a net loss: we save about 50k of argument bitmaps but the new
  1715  // PCDATA tables cost about 100k. So for now we keep using a single index for
  1716  // both bitmap lists.
  1717  static void
  1718  livenesscompact(Liveness *lv)
  1719  {
  1720  	int *table, *remap, i, j, n, tablesize, uniq;
  1721  	uint32 h;
  1722  	Bvec *local, *arg, *jlocal, *jarg;
  1723  	Prog *p;
  1724  
  1725  	// Linear probing hash table of bitmaps seen so far.
  1726  	// The hash table has 4n entries to keep the linear
  1727  	// scan short. An entry of -1 indicates an empty slot.
  1728  	n = arraylength(lv->livepointers);
  1729  	tablesize = 4*n;
  1730  	table = xmalloc(tablesize*sizeof table[0]);
  1731  	memset(table, 0xff, tablesize*sizeof table[0]);
  1732  	
  1733  	// remap[i] = the new index of the old bit vector #i.
  1734  	remap = xmalloc(n*sizeof remap[0]);
  1735  	memset(remap, 0xff, n*sizeof remap[0]);
  1736  	uniq = 0; // unique tables found so far
  1737  
  1738  	// Consider bit vectors in turn.
  1739  	// If new, assign next number using uniq,
  1740  	// record in remap, record in lv->livepointers and lv->argslivepointers
  1741  	// under the new index, and add entry to hash table.
  1742  	// If already seen, record earlier index in remap and free bitmaps.
  1743  	for(i=0; i<n; i++) {
  1744  		local = *(Bvec**)arrayget(lv->livepointers, i);
  1745  		arg = *(Bvec**)arrayget(lv->argslivepointers, i);
  1746  		h = hashbitmap(hashbitmap(H0, local), arg) % tablesize;
  1747  
  1748  		for(;;) {
  1749  			j = table[h];
  1750  			if(j < 0)
  1751  				break;
  1752  			jlocal = *(Bvec**)arrayget(lv->livepointers, j);
  1753  			jarg = *(Bvec**)arrayget(lv->argslivepointers, j);
  1754  			if(bvcmp(local, jlocal) == 0 && bvcmp(arg, jarg) == 0) {
  1755  				free(local);
  1756  				free(arg);
  1757  				remap[i] = j;
  1758  				goto Next;
  1759  			}
  1760  			if(++h == tablesize)
  1761  				h = 0;
  1762  		}
  1763  		table[h] = uniq;
  1764  		remap[i] = uniq;
  1765  		*(Bvec**)arrayget(lv->livepointers, uniq) = local;
  1766  		*(Bvec**)arrayget(lv->argslivepointers, uniq) = arg;
  1767  		uniq++;
  1768  	Next:;
  1769  	}
  1770  
  1771  	// We've already reordered lv->livepointers[0:uniq]
  1772  	// and lv->argslivepointers[0:uniq] and freed the bitmaps
  1773  	// we don't need anymore. Clear the pointers later in the
  1774  	// array so that we can tell where the coalesced bitmaps stop
  1775  	// and so that we don't double-free when cleaning up.
  1776  	for(j=uniq; j<n; j++) {
  1777  		*(Bvec**)arrayget(lv->livepointers, j) = nil;
  1778  		*(Bvec**)arrayget(lv->argslivepointers, j) = nil;
  1779  	}
  1780  	
  1781  	// Rewrite PCDATA instructions to use new numbering.
  1782  	for(p=lv->ptxt; p != P; p=p->link) {
  1783  		if(p->as == APCDATA && p->from.offset == PCDATA_StackMapIndex) {
  1784  			i = p->to.offset;
  1785  			if(i >= 0)
  1786  				p->to.offset = remap[i];
  1787  		}
  1788  	}
  1789  
  1790  	free(table);
  1791  	free(remap);
  1792  }
  1793  
  1794  static int
  1795  printbitset(int printed, char *name, Array *vars, Bvec *bits)
  1796  {
  1797  	int i, started;
  1798  	Node *n;
  1799  
  1800  	started = 0;	
  1801  	for(i=0; i<arraylength(vars); i++) {
  1802  		if(!bvget(bits, i))
  1803  			continue;
  1804  		if(!started) {
  1805  			if(!printed)
  1806  				print("\t");
  1807  			else
  1808  				print(" ");
  1809  			started = 1;
  1810  			printed = 1;
  1811  			print("%s=", name);
  1812  		} else {
  1813  			print(",");
  1814  		}
  1815  		n = *(Node**)arrayget(vars, i);
  1816  		print("%s", n->sym->name);
  1817  	}
  1818  	return printed;
  1819  }
  1820  
  1821  // Prints the computed liveness information and inputs, for debugging.
  1822  // This format synthesizes the information used during the multiple passes
  1823  // into a single presentation.
  1824  static void
  1825  livenessprintdebug(Liveness *lv)
  1826  {
  1827  	int i, j, pcdata, printed;
  1828  	BasicBlock *bb;
  1829  	Prog *p;
  1830  	Bvec *uevar, *varkill, *avarinit, *args, *locals;
  1831  	Node *n;
  1832  
  1833  	print("liveness: %s\n", curfn->nname->sym->name);
  1834  
  1835  	uevar = bvalloc(arraylength(lv->vars));
  1836  	varkill = bvalloc(arraylength(lv->vars));
  1837  	avarinit = bvalloc(arraylength(lv->vars));
  1838  
  1839  	pcdata = 0;
  1840  	for(i = 0; i < arraylength(lv->cfg); i++) {
  1841  		if(i > 0)
  1842  			print("\n");
  1843  		bb = *(BasicBlock**)arrayget(lv->cfg, i);
  1844  
  1845  		// bb#0 pred=1,2 succ=3,4
  1846  		print("bb#%d pred=", i);
  1847  		for(j = 0; j < arraylength(bb->pred); j++) {
  1848  			if(j > 0)
  1849  				print(",");
  1850  			print("%d", (*(BasicBlock**)arrayget(bb->pred, j))->rpo);
  1851  		}
  1852  		print(" succ=");
  1853  		for(j = 0; j < arraylength(bb->succ); j++) {
  1854  			if(j > 0)
  1855  				print(",");
  1856  			print("%d", (*(BasicBlock**)arrayget(bb->succ, j))->rpo);
  1857  		}
  1858  		print("\n");
  1859  		
  1860  		// initial settings
  1861  		printed = 0;
  1862  		printed = printbitset(printed, "uevar", lv->vars, lv->uevar[bb->rpo]);
  1863  		printed = printbitset(printed, "livein", lv->vars, lv->livein[bb->rpo]);
  1864  		if(printed)
  1865  			print("\n");
  1866  		
  1867  		// program listing, with individual effects listed
  1868  		for(p = bb->first;; p = p->link) {
  1869  			print("%P\n", p);
  1870  			if(p->as == APCDATA && p->from.offset == PCDATA_StackMapIndex)
  1871  				pcdata = p->to.offset;
  1872  			progeffects(p, lv->vars, uevar, varkill, avarinit);
  1873  			printed = 0;
  1874  			printed = printbitset(printed, "uevar", lv->vars, uevar);
  1875  			printed = printbitset(printed, "varkill", lv->vars, varkill);
  1876  			printed = printbitset(printed, "avarinit", lv->vars, avarinit);
  1877  			if(printed)
  1878  				print("\n");
  1879  			if(issafepoint(p)) {
  1880  				args = *(Bvec**)arrayget(lv->argslivepointers, pcdata);
  1881  				locals = *(Bvec**)arrayget(lv->livepointers, pcdata);
  1882  				print("\tlive=");
  1883  				printed = 0;
  1884  				for(j = 0; j < arraylength(lv->vars); j++) {
  1885  					n = *(Node**)arrayget(lv->vars, j);
  1886  					if(islive(n, args, locals)) {
  1887  						if(printed++)
  1888  							print(",");
  1889  						print("%N", n);
  1890  					}
  1891  				}
  1892  				print("\n");
  1893  			}
  1894  			if(p == bb->last)
  1895  				break;
  1896  		}
  1897  		
  1898  		// bb bitsets
  1899  		print("end\n");
  1900  		printed = printbitset(printed, "varkill", lv->vars, lv->varkill[bb->rpo]);
  1901  		printed = printbitset(printed, "liveout", lv->vars, lv->liveout[bb->rpo]);
  1902  		printed = printbitset(printed, "avarinit", lv->vars, lv->avarinit[bb->rpo]);
  1903  		printed = printbitset(printed, "avarinitany", lv->vars, lv->avarinitany[bb->rpo]);
  1904  		printed = printbitset(printed, "avarinitall", lv->vars, lv->avarinitall[bb->rpo]);
  1905  		if(printed)
  1906  			print("\n");
  1907  	}
  1908  	print("\n");
  1909  
  1910  	free(uevar);
  1911  	free(varkill);
  1912  	free(avarinit);
  1913  }
  1914  
  1915  // Dumps an array of bitmaps to a symbol as a sequence of uint32 values.  The
  1916  // first word dumped is the total number of bitmaps.  The second word is the
  1917  // length of the bitmaps.  All bitmaps are assumed to be of equal length.  The
  1918  // words that are followed are the raw bitmap words.  The arr argument is an
  1919  // array of Node*s.
  1920  static void
  1921  twobitwritesymbol(Array *arr, Sym *sym)
  1922  {
  1923  	Bvec *bv;
  1924  	int off, i, j, len;
  1925  	uint32 word;
  1926  
  1927  	len = arraylength(arr);
  1928  	off = 0;
  1929  	off += 4; // number of bitmaps, to fill in later
  1930  	bv = *(Bvec**)arrayget(arr, 0);
  1931  	off = duint32(sym, off, bv->n); // number of bits in each bitmap
  1932  	for(i = 0; i < len; i++) {
  1933  		// bitmap words
  1934  		bv = *(Bvec**)arrayget(arr, i);
  1935  		if(bv == nil)
  1936  			break;
  1937  		for(j = 0; j < bv->n; j += 32) {
  1938  			word = bv->b[j/32];
  1939  			// Runtime reads the bitmaps as byte arrays. Oblige.
  1940  			off = duint8(sym, off, word);
  1941  			off = duint8(sym, off, word>>8);
  1942  			off = duint8(sym, off, word>>16);
  1943  			off = duint8(sym, off, word>>24);
  1944  		}
  1945  	}
  1946  	duint32(sym, 0, i); // number of bitmaps
  1947  	ggloblsym(sym, off, RODATA);
  1948  }
  1949  
  1950  static void
  1951  printprog(Prog *p)
  1952  {
  1953  	while(p != nil) {
  1954  		print("%P\n", p);
  1955  		p = p->link;
  1956  	}
  1957  }
  1958  
  1959  // Entry pointer for liveness analysis.  Constructs a complete CFG, solves for
  1960  // the liveness of pointer variables in the function, and emits a runtime data
  1961  // structure read by the garbage collector.
  1962  void
  1963  liveness(Node *fn, Prog *firstp, Sym *argssym, Sym *livesym)
  1964  {
  1965  	Array *cfg, *vars;
  1966  	Liveness *lv;
  1967  	int debugdelta;
  1968  	NodeList *l;
  1969  
  1970  	// Change name to dump debugging information only for a specific function.
  1971  	debugdelta = 0;
  1972  	if(strcmp(curfn->nname->sym->name, "!") == 0)
  1973  		debugdelta = 2;
  1974  	
  1975  	debuglive += debugdelta;
  1976  	if(debuglive >= 3) {
  1977  		print("liveness: %s\n", curfn->nname->sym->name);
  1978  		printprog(firstp);
  1979  	}
  1980  	checkptxt(fn, firstp);
  1981  
  1982  	// Construct the global liveness state.
  1983  	cfg = newcfg(firstp);
  1984  	if(debuglive >= 3)
  1985  		printcfg(cfg);
  1986  	vars = getvariables(fn);
  1987  	lv = newliveness(fn, firstp, cfg, vars);
  1988  
  1989  	// Run the dataflow framework.
  1990  	livenessprologue(lv);
  1991  	if(debuglive >= 3)
  1992  		livenessprintcfg(lv);
  1993  	livenesssolve(lv);
  1994  	if(debuglive >= 3)
  1995  		livenessprintcfg(lv);
  1996  	livenessepilogue(lv);
  1997  	if(debuglive >= 3)
  1998  		livenessprintcfg(lv);
  1999  	livenesscompact(lv);
  2000  
  2001  	if(debuglive >= 2)
  2002  		livenessprintdebug(lv);
  2003  
  2004  	// Emit the live pointer map data structures
  2005  	twobitwritesymbol(lv->livepointers, livesym);
  2006  	twobitwritesymbol(lv->argslivepointers, argssym);
  2007  
  2008  	// Free everything.
  2009  	for(l=fn->dcl; l != nil; l = l->next)
  2010  		if(l->n != N)
  2011  			l->n->opt = nil;
  2012  	freeliveness(lv);
  2013  	arrayfree(vars);
  2014  	freecfg(cfg);
  2015  	
  2016  	debuglive -= debugdelta;
  2017  }