Home | History | Annotate | Line # | Download | only in dist
optimize.c revision 1.9
      1  1.9  christos /*	$NetBSD: optimize.c,v 1.9 2017/01/24 22:29:28 christos Exp $	*/
      2  1.5  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      9  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
     10  1.1  christos  * distributions including binary code include the above copyright notice and
     11  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     12  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     13  1.1  christos  * features or use of this software display the following acknowledgement:
     14  1.1  christos  * ``This product includes software developed by the University of California,
     15  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     16  1.1  christos  * the University nor the names of its contributors may be used to endorse
     17  1.1  christos  * or promote products derived from this software without specific prior
     18  1.1  christos  * written permission.
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     20  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     21  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  1.1  christos  *
     23  1.1  christos  *  Optimization module for tcpdump intermediate representation.
     24  1.1  christos  */
     25  1.7  christos 
     26  1.7  christos #include <sys/cdefs.h>
     27  1.9  christos __RCSID("$NetBSD: optimize.c,v 1.9 2017/01/24 22:29:28 christos Exp $");
     28  1.1  christos 
     29  1.1  christos #ifdef HAVE_CONFIG_H
     30  1.1  christos #include "config.h"
     31  1.1  christos #endif
     32  1.1  christos 
     33  1.9  christos #ifdef _WIN32
     34  1.1  christos #include <pcap-stdinc.h>
     35  1.9  christos #else /* _WIN32 */
     36  1.1  christos #if HAVE_INTTYPES_H
     37  1.1  christos #include <inttypes.h>
     38  1.1  christos #elif HAVE_STDINT_H
     39  1.1  christos #include <stdint.h>
     40  1.1  christos #endif
     41  1.1  christos #ifdef HAVE_SYS_BITYPES_H
     42  1.1  christos #include <sys/bitypes.h>
     43  1.1  christos #endif
     44  1.1  christos #include <sys/types.h>
     45  1.9  christos #endif /* _WIN32 */
     46  1.1  christos 
     47  1.1  christos #include <stdio.h>
     48  1.1  christos #include <stdlib.h>
     49  1.1  christos #include <memory.h>
     50  1.1  christos #include <string.h>
     51  1.1  christos 
     52  1.1  christos #include <errno.h>
     53  1.1  christos 
     54  1.1  christos #include "pcap-int.h"
     55  1.1  christos 
     56  1.1  christos #include "gencode.h"
     57  1.1  christos 
     58  1.1  christos #ifdef HAVE_OS_PROTO_H
     59  1.1  christos #include "os-proto.h"
     60  1.1  christos #endif
     61  1.1  christos 
     62  1.1  christos #ifdef BDEBUG
     63  1.9  christos int pcap_optimizer_debug;
     64  1.1  christos #endif
     65  1.1  christos 
     66  1.1  christos #if defined(MSDOS) && !defined(__DJGPP__)
     67  1.1  christos extern int _w32_ffs (int mask);
     68  1.1  christos #define ffs _w32_ffs
     69  1.1  christos #endif
     70  1.1  christos 
     71  1.9  christos /*
     72  1.9  christos  * So is the check for _MSC_VER done because MinGW has this?
     73  1.9  christos  */
     74  1.9  christos #if defined(_WIN32) && defined (_MSC_VER)
     75  1.9  christos /*
     76  1.9  christos  * ffs -- vax ffs instruction
     77  1.9  christos  *
     78  1.9  christos  * XXX - with versions of VS that have it, use _BitScanForward()?
     79  1.9  christos  */
     80  1.9  christos static int
     81  1.9  christos ffs(int mask)
     82  1.9  christos {
     83  1.9  christos 	int bit;
     84  1.9  christos 
     85  1.9  christos 	if (mask == 0)
     86  1.9  christos 		return(0);
     87  1.9  christos 	for (bit = 1; !(mask & 1); bit++)
     88  1.9  christos 		mask >>= 1;
     89  1.9  christos 	return(bit);
     90  1.9  christos }
     91  1.1  christos #endif
     92  1.1  christos 
     93  1.1  christos /*
     94  1.1  christos  * Represents a deleted instruction.
     95  1.1  christos  */
     96  1.1  christos #define NOP -1
     97  1.1  christos 
     98  1.1  christos /*
     99  1.1  christos  * Register numbers for use-def values.
    100  1.1  christos  * 0 through BPF_MEMWORDS-1 represent the corresponding scratch memory
    101  1.1  christos  * location.  A_ATOM is the accumulator and X_ATOM is the index
    102  1.1  christos  * register.
    103  1.1  christos  */
    104  1.1  christos #define A_ATOM BPF_MEMWORDS
    105  1.1  christos #define X_ATOM (BPF_MEMWORDS+1)
    106  1.1  christos 
    107  1.1  christos /*
    108  1.1  christos  * This define is used to represent *both* the accumulator and
    109  1.1  christos  * x register in use-def computations.
    110  1.1  christos  * Currently, the use-def code assumes only one definition per instruction.
    111  1.1  christos  */
    112  1.1  christos #define AX_ATOM N_ATOMS
    113  1.1  christos 
    114  1.1  christos /*
    115  1.9  christos  * These data structures are used in a Cocke and Shwarz style
    116  1.9  christos  * value numbering scheme.  Since the flowgraph is acyclic,
    117  1.9  christos  * exit values can be propagated from a node's predecessors
    118  1.9  christos  * provided it is uniquely defined.
    119  1.1  christos  */
    120  1.9  christos struct valnode {
    121  1.9  christos 	int code;
    122  1.9  christos 	int v0, v1;
    123  1.9  christos 	int val;
    124  1.9  christos 	struct valnode *next;
    125  1.9  christos };
    126  1.1  christos 
    127  1.9  christos /* Integer constants mapped with the load immediate opcode. */
    128  1.9  christos #define K(i) F(opt_state, BPF_LD|BPF_IMM|BPF_W, i, 0L)
    129  1.1  christos 
    130  1.9  christos struct vmapinfo {
    131  1.9  christos 	int is_const;
    132  1.9  christos 	bpf_int32 const_val;
    133  1.9  christos };
    134  1.1  christos 
    135  1.9  christos struct _opt_state {
    136  1.9  christos 	/*
    137  1.9  christos 	 * A flag to indicate that further optimization is needed.
    138  1.9  christos 	 * Iterative passes are continued until a given pass yields no
    139  1.9  christos 	 * branch movement.
    140  1.9  christos 	 */
    141  1.9  christos 	int done;
    142  1.1  christos 
    143  1.9  christos 	int n_blocks;
    144  1.9  christos 	struct block **blocks;
    145  1.9  christos 	int n_edges;
    146  1.9  christos 	struct edge **edges;
    147  1.1  christos 
    148  1.9  christos 	/*
    149  1.9  christos 	 * A bit vector set representation of the dominators.
    150  1.9  christos 	 * We round up the set size to the next power of two.
    151  1.9  christos 	 */
    152  1.9  christos 	int nodewords;
    153  1.9  christos 	int edgewords;
    154  1.9  christos 	struct block **levels;
    155  1.9  christos 	bpf_u_int32 *space;
    156  1.1  christos 
    157  1.1  christos #define BITS_PER_WORD (8*sizeof(bpf_u_int32))
    158  1.1  christos /*
    159  1.1  christos  * True if a is in uset {p}
    160  1.1  christos  */
    161  1.1  christos #define SET_MEMBER(p, a) \
    162  1.1  christos ((p)[(unsigned)(a) / BITS_PER_WORD] & (1 << ((unsigned)(a) % BITS_PER_WORD)))
    163  1.1  christos 
    164  1.1  christos /*
    165  1.1  christos  * Add 'a' to uset p.
    166  1.1  christos  */
    167  1.1  christos #define SET_INSERT(p, a) \
    168  1.1  christos (p)[(unsigned)(a) / BITS_PER_WORD] |= (1 << ((unsigned)(a) % BITS_PER_WORD))
    169  1.1  christos 
    170  1.1  christos /*
    171  1.1  christos  * Delete 'a' from uset p.
    172  1.1  christos  */
    173  1.1  christos #define SET_DELETE(p, a) \
    174  1.1  christos (p)[(unsigned)(a) / BITS_PER_WORD] &= ~(1 << ((unsigned)(a) % BITS_PER_WORD))
    175  1.1  christos 
    176  1.1  christos /*
    177  1.1  christos  * a := a intersect b
    178  1.1  christos  */
    179  1.1  christos #define SET_INTERSECT(a, b, n)\
    180  1.1  christos {\
    181  1.1  christos 	register bpf_u_int32 *_x = a, *_y = b;\
    182  1.1  christos 	register int _n = n;\
    183  1.1  christos 	while (--_n >= 0) *_x++ &= *_y++;\
    184  1.1  christos }
    185  1.1  christos 
    186  1.1  christos /*
    187  1.1  christos  * a := a - b
    188  1.1  christos  */
    189  1.1  christos #define SET_SUBTRACT(a, b, n)\
    190  1.1  christos {\
    191  1.1  christos 	register bpf_u_int32 *_x = a, *_y = b;\
    192  1.1  christos 	register int _n = n;\
    193  1.1  christos 	while (--_n >= 0) *_x++ &=~ *_y++;\
    194  1.1  christos }
    195  1.1  christos 
    196  1.1  christos /*
    197  1.1  christos  * a := a union b
    198  1.1  christos  */
    199  1.1  christos #define SET_UNION(a, b, n)\
    200  1.1  christos {\
    201  1.1  christos 	register bpf_u_int32 *_x = a, *_y = b;\
    202  1.1  christos 	register int _n = n;\
    203  1.1  christos 	while (--_n >= 0) *_x++ |= *_y++;\
    204  1.1  christos }
    205  1.1  christos 
    206  1.9  christos 	uset all_dom_sets;
    207  1.9  christos 	uset all_closure_sets;
    208  1.9  christos 	uset all_edge_sets;
    209  1.9  christos 
    210  1.9  christos #define MODULUS 213
    211  1.9  christos 	struct valnode *hashtbl[MODULUS];
    212  1.9  christos 	int curval;
    213  1.9  christos 	int maxval;
    214  1.9  christos 
    215  1.9  christos 	struct vmapinfo *vmap;
    216  1.9  christos 	struct valnode *vnode_base;
    217  1.9  christos 	struct valnode *next_vnode;
    218  1.9  christos };
    219  1.9  christos 
    220  1.9  christos typedef struct {
    221  1.9  christos 	/*
    222  1.9  christos 	 * Some pointers used to convert the basic block form of the code,
    223  1.9  christos 	 * into the array form that BPF requires.  'fstart' will point to
    224  1.9  christos 	 * the malloc'd array while 'ftail' is used during the recursive
    225  1.9  christos 	 * traversal.
    226  1.9  christos 	 */
    227  1.9  christos 	struct bpf_insn *fstart;
    228  1.9  christos 	struct bpf_insn *ftail;
    229  1.9  christos } conv_state_t;
    230  1.9  christos 
    231  1.9  christos static void opt_init(compiler_state_t *, opt_state_t *, struct icode *);
    232  1.9  christos static void opt_cleanup(opt_state_t *);
    233  1.9  christos 
    234  1.9  christos static void intern_blocks(opt_state_t *, struct icode *);
    235  1.9  christos 
    236  1.9  christos static void find_inedges(opt_state_t *, struct block *);
    237  1.9  christos #ifdef BDEBUG
    238  1.9  christos static void opt_dump(compiler_state_t *, struct icode *);
    239  1.9  christos #endif
    240  1.1  christos 
    241  1.1  christos #ifndef MAX
    242  1.1  christos #define MAX(a,b) ((a)>(b)?(a):(b))
    243  1.1  christos #endif
    244  1.1  christos 
    245  1.1  christos static void
    246  1.9  christos find_levels_r(opt_state_t *opt_state, struct icode *ic, struct block *b)
    247  1.1  christos {
    248  1.1  christos 	int level;
    249  1.1  christos 
    250  1.9  christos 	if (isMarked(ic, b))
    251  1.1  christos 		return;
    252  1.1  christos 
    253  1.9  christos 	Mark(ic, b);
    254  1.1  christos 	b->link = 0;
    255  1.1  christos 
    256  1.1  christos 	if (JT(b)) {
    257  1.9  christos 		find_levels_r(opt_state, ic, JT(b));
    258  1.9  christos 		find_levels_r(opt_state, ic, JF(b));
    259  1.1  christos 		level = MAX(JT(b)->level, JF(b)->level) + 1;
    260  1.1  christos 	} else
    261  1.1  christos 		level = 0;
    262  1.1  christos 	b->level = level;
    263  1.9  christos 	b->link = opt_state->levels[level];
    264  1.9  christos 	opt_state->levels[level] = b;
    265  1.1  christos }
    266  1.1  christos 
    267  1.1  christos /*
    268  1.1  christos  * Level graph.  The levels go from 0 at the leaves to
    269  1.9  christos  * N_LEVELS at the root.  The opt_state->levels[] array points to the
    270  1.1  christos  * first node of the level list, whose elements are linked
    271  1.1  christos  * with the 'link' field of the struct block.
    272  1.1  christos  */
    273  1.1  christos static void
    274  1.9  christos find_levels(opt_state_t *opt_state, struct icode *ic)
    275  1.1  christos {
    276  1.9  christos 	memset((char *)opt_state->levels, 0, opt_state->n_blocks * sizeof(*opt_state->levels));
    277  1.9  christos 	unMarkAll(ic);
    278  1.9  christos 	find_levels_r(opt_state, ic, ic->root);
    279  1.1  christos }
    280  1.1  christos 
    281  1.1  christos /*
    282  1.1  christos  * Find dominator relationships.
    283  1.1  christos  * Assumes graph has been leveled.
    284  1.1  christos  */
    285  1.1  christos static void
    286  1.9  christos find_dom(opt_state_t *opt_state, struct block *root)
    287  1.1  christos {
    288  1.1  christos 	int i;
    289  1.1  christos 	struct block *b;
    290  1.1  christos 	bpf_u_int32 *x;
    291  1.1  christos 
    292  1.1  christos 	/*
    293  1.1  christos 	 * Initialize sets to contain all nodes.
    294  1.1  christos 	 */
    295  1.9  christos 	x = opt_state->all_dom_sets;
    296  1.9  christos 	i = opt_state->n_blocks * opt_state->nodewords;
    297  1.1  christos 	while (--i >= 0)
    298  1.1  christos 		*x++ = ~0;
    299  1.1  christos 	/* Root starts off empty. */
    300  1.9  christos 	for (i = opt_state->nodewords; --i >= 0;)
    301  1.1  christos 		root->dom[i] = 0;
    302  1.1  christos 
    303  1.1  christos 	/* root->level is the highest level no found. */
    304  1.1  christos 	for (i = root->level; i >= 0; --i) {
    305  1.9  christos 		for (b = opt_state->levels[i]; b; b = b->link) {
    306  1.1  christos 			SET_INSERT(b->dom, b->id);
    307  1.1  christos 			if (JT(b) == 0)
    308  1.1  christos 				continue;
    309  1.9  christos 			SET_INTERSECT(JT(b)->dom, b->dom, opt_state->nodewords);
    310  1.9  christos 			SET_INTERSECT(JF(b)->dom, b->dom, opt_state->nodewords);
    311  1.1  christos 		}
    312  1.1  christos 	}
    313  1.1  christos }
    314  1.1  christos 
    315  1.1  christos static void
    316  1.9  christos propedom(opt_state_t *opt_state, struct edge *ep)
    317  1.1  christos {
    318  1.1  christos 	SET_INSERT(ep->edom, ep->id);
    319  1.1  christos 	if (ep->succ) {
    320  1.9  christos 		SET_INTERSECT(ep->succ->et.edom, ep->edom, opt_state->edgewords);
    321  1.9  christos 		SET_INTERSECT(ep->succ->ef.edom, ep->edom, opt_state->edgewords);
    322  1.1  christos 	}
    323  1.1  christos }
    324  1.1  christos 
    325  1.1  christos /*
    326  1.1  christos  * Compute edge dominators.
    327  1.1  christos  * Assumes graph has been leveled and predecessors established.
    328  1.1  christos  */
    329  1.1  christos static void
    330  1.9  christos find_edom(opt_state_t *opt_state, struct block *root)
    331  1.1  christos {
    332  1.1  christos 	int i;
    333  1.1  christos 	uset x;
    334  1.1  christos 	struct block *b;
    335  1.1  christos 
    336  1.9  christos 	x = opt_state->all_edge_sets;
    337  1.9  christos 	for (i = opt_state->n_edges * opt_state->edgewords; --i >= 0; )
    338  1.1  christos 		x[i] = ~0;
    339  1.1  christos 
    340  1.1  christos 	/* root->level is the highest level no found. */
    341  1.9  christos 	memset(root->et.edom, 0, opt_state->edgewords * sizeof(*(uset)0));
    342  1.9  christos 	memset(root->ef.edom, 0, opt_state->edgewords * sizeof(*(uset)0));
    343  1.1  christos 	for (i = root->level; i >= 0; --i) {
    344  1.9  christos 		for (b = opt_state->levels[i]; b != 0; b = b->link) {
    345  1.9  christos 			propedom(opt_state, &b->et);
    346  1.9  christos 			propedom(opt_state, &b->ef);
    347  1.1  christos 		}
    348  1.1  christos 	}
    349  1.1  christos }
    350  1.1  christos 
    351  1.1  christos /*
    352  1.1  christos  * Find the backwards transitive closure of the flow graph.  These sets
    353  1.1  christos  * are backwards in the sense that we find the set of nodes that reach
    354  1.1  christos  * a given node, not the set of nodes that can be reached by a node.
    355  1.1  christos  *
    356  1.1  christos  * Assumes graph has been leveled.
    357  1.1  christos  */
    358  1.1  christos static void
    359  1.9  christos find_closure(opt_state_t *opt_state, struct block *root)
    360  1.1  christos {
    361  1.1  christos 	int i;
    362  1.1  christos 	struct block *b;
    363  1.1  christos 
    364  1.1  christos 	/*
    365  1.1  christos 	 * Initialize sets to contain no nodes.
    366  1.1  christos 	 */
    367  1.9  christos 	memset((char *)opt_state->all_closure_sets, 0,
    368  1.9  christos 	      opt_state->n_blocks * opt_state->nodewords * sizeof(*opt_state->all_closure_sets));
    369  1.1  christos 
    370  1.1  christos 	/* root->level is the highest level no found. */
    371  1.1  christos 	for (i = root->level; i >= 0; --i) {
    372  1.9  christos 		for (b = opt_state->levels[i]; b; b = b->link) {
    373  1.1  christos 			SET_INSERT(b->closure, b->id);
    374  1.1  christos 			if (JT(b) == 0)
    375  1.1  christos 				continue;
    376  1.9  christos 			SET_UNION(JT(b)->closure, b->closure, opt_state->nodewords);
    377  1.9  christos 			SET_UNION(JF(b)->closure, b->closure, opt_state->nodewords);
    378  1.1  christos 		}
    379  1.1  christos 	}
    380  1.1  christos }
    381  1.1  christos 
    382  1.1  christos /*
    383  1.1  christos  * Return the register number that is used by s.  If A and X are both
    384  1.1  christos  * used, return AX_ATOM.  If no register is used, return -1.
    385  1.1  christos  *
    386  1.1  christos  * The implementation should probably change to an array access.
    387  1.1  christos  */
    388  1.1  christos static int
    389  1.6  christos atomuse(struct stmt *s)
    390  1.1  christos {
    391  1.1  christos 	register int c = s->code;
    392  1.1  christos 
    393  1.1  christos 	if (c == NOP)
    394  1.1  christos 		return -1;
    395  1.1  christos 
    396  1.1  christos 	switch (BPF_CLASS(c)) {
    397  1.1  christos 
    398  1.1  christos 	case BPF_RET:
    399  1.1  christos 		return (BPF_RVAL(c) == BPF_A) ? A_ATOM :
    400  1.1  christos 			(BPF_RVAL(c) == BPF_X) ? X_ATOM : -1;
    401  1.1  christos 
    402  1.1  christos 	case BPF_LD:
    403  1.1  christos 	case BPF_LDX:
    404  1.1  christos 		return (BPF_MODE(c) == BPF_IND) ? X_ATOM :
    405  1.1  christos 			(BPF_MODE(c) == BPF_MEM) ? s->k : -1;
    406  1.1  christos 
    407  1.1  christos 	case BPF_ST:
    408  1.1  christos 		return A_ATOM;
    409  1.1  christos 
    410  1.1  christos 	case BPF_STX:
    411  1.1  christos 		return X_ATOM;
    412  1.1  christos 
    413  1.1  christos 	case BPF_JMP:
    414  1.1  christos 	case BPF_ALU:
    415  1.1  christos 		if (BPF_SRC(c) == BPF_X)
    416  1.1  christos 			return AX_ATOM;
    417  1.1  christos 		return A_ATOM;
    418  1.1  christos 
    419  1.1  christos 	case BPF_MISC:
    420  1.1  christos 		return BPF_MISCOP(c) == BPF_TXA ? X_ATOM : A_ATOM;
    421  1.1  christos 	}
    422  1.1  christos 	abort();
    423  1.1  christos 	/* NOTREACHED */
    424  1.1  christos }
    425  1.1  christos 
    426  1.1  christos /*
    427  1.1  christos  * Return the register number that is defined by 's'.  We assume that
    428  1.1  christos  * a single stmt cannot define more than one register.  If no register
    429  1.1  christos  * is defined, return -1.
    430  1.1  christos  *
    431  1.1  christos  * The implementation should probably change to an array access.
    432  1.1  christos  */
    433  1.1  christos static int
    434  1.6  christos atomdef(struct stmt *s)
    435  1.1  christos {
    436  1.1  christos 	if (s->code == NOP)
    437  1.1  christos 		return -1;
    438  1.1  christos 
    439  1.1  christos 	switch (BPF_CLASS(s->code)) {
    440  1.1  christos 
    441  1.1  christos 	case BPF_LD:
    442  1.1  christos 	case BPF_ALU:
    443  1.1  christos 		return A_ATOM;
    444  1.1  christos 
    445  1.1  christos 	case BPF_LDX:
    446  1.1  christos 		return X_ATOM;
    447  1.1  christos 
    448  1.1  christos 	case BPF_ST:
    449  1.1  christos 	case BPF_STX:
    450  1.1  christos 		return s->k;
    451  1.1  christos 
    452  1.1  christos 	case BPF_MISC:
    453  1.1  christos 		return BPF_MISCOP(s->code) == BPF_TAX ? X_ATOM : A_ATOM;
    454  1.1  christos 	}
    455  1.1  christos 	return -1;
    456  1.1  christos }
    457  1.1  christos 
    458  1.1  christos /*
    459  1.1  christos  * Compute the sets of registers used, defined, and killed by 'b'.
    460  1.1  christos  *
    461  1.1  christos  * "Used" means that a statement in 'b' uses the register before any
    462  1.1  christos  * statement in 'b' defines it, i.e. it uses the value left in
    463  1.1  christos  * that register by a predecessor block of this block.
    464  1.1  christos  * "Defined" means that a statement in 'b' defines it.
    465  1.1  christos  * "Killed" means that a statement in 'b' defines it before any
    466  1.1  christos  * statement in 'b' uses it, i.e. it kills the value left in that
    467  1.1  christos  * register by a predecessor block of this block.
    468  1.1  christos  */
    469  1.1  christos static void
    470  1.6  christos compute_local_ud(struct block *b)
    471  1.1  christos {
    472  1.1  christos 	struct slist *s;
    473  1.9  christos 	atomset def = 0, use = 0, killed = 0;
    474  1.1  christos 	int atom;
    475  1.1  christos 
    476  1.1  christos 	for (s = b->stmts; s; s = s->next) {
    477  1.1  christos 		if (s->s.code == NOP)
    478  1.1  christos 			continue;
    479  1.1  christos 		atom = atomuse(&s->s);
    480  1.1  christos 		if (atom >= 0) {
    481  1.1  christos 			if (atom == AX_ATOM) {
    482  1.1  christos 				if (!ATOMELEM(def, X_ATOM))
    483  1.1  christos 					use |= ATOMMASK(X_ATOM);
    484  1.1  christos 				if (!ATOMELEM(def, A_ATOM))
    485  1.1  christos 					use |= ATOMMASK(A_ATOM);
    486  1.1  christos 			}
    487  1.1  christos 			else if (atom < N_ATOMS) {
    488  1.1  christos 				if (!ATOMELEM(def, atom))
    489  1.1  christos 					use |= ATOMMASK(atom);
    490  1.1  christos 			}
    491  1.1  christos 			else
    492  1.1  christos 				abort();
    493  1.1  christos 		}
    494  1.1  christos 		atom = atomdef(&s->s);
    495  1.1  christos 		if (atom >= 0) {
    496  1.1  christos 			if (!ATOMELEM(use, atom))
    497  1.9  christos 				killed |= ATOMMASK(atom);
    498  1.1  christos 			def |= ATOMMASK(atom);
    499  1.1  christos 		}
    500  1.1  christos 	}
    501  1.1  christos 	if (BPF_CLASS(b->s.code) == BPF_JMP) {
    502  1.1  christos 		/*
    503  1.1  christos 		 * XXX - what about RET?
    504  1.1  christos 		 */
    505  1.1  christos 		atom = atomuse(&b->s);
    506  1.1  christos 		if (atom >= 0) {
    507  1.1  christos 			if (atom == AX_ATOM) {
    508  1.1  christos 				if (!ATOMELEM(def, X_ATOM))
    509  1.1  christos 					use |= ATOMMASK(X_ATOM);
    510  1.1  christos 				if (!ATOMELEM(def, A_ATOM))
    511  1.1  christos 					use |= ATOMMASK(A_ATOM);
    512  1.1  christos 			}
    513  1.1  christos 			else if (atom < N_ATOMS) {
    514  1.1  christos 				if (!ATOMELEM(def, atom))
    515  1.1  christos 					use |= ATOMMASK(atom);
    516  1.1  christos 			}
    517  1.1  christos 			else
    518  1.1  christos 				abort();
    519  1.1  christos 		}
    520  1.1  christos 	}
    521  1.1  christos 
    522  1.1  christos 	b->def = def;
    523  1.9  christos 	b->kill = killed;
    524  1.1  christos 	b->in_use = use;
    525  1.1  christos }
    526  1.1  christos 
    527  1.1  christos /*
    528  1.1  christos  * Assume graph is already leveled.
    529  1.1  christos  */
    530  1.1  christos static void
    531  1.9  christos find_ud(opt_state_t *opt_state, struct block *root)
    532  1.1  christos {
    533  1.1  christos 	int i, maxlevel;
    534  1.1  christos 	struct block *p;
    535  1.1  christos 
    536  1.1  christos 	/*
    537  1.1  christos 	 * root->level is the highest level no found;
    538  1.1  christos 	 * count down from there.
    539  1.1  christos 	 */
    540  1.1  christos 	maxlevel = root->level;
    541  1.1  christos 	for (i = maxlevel; i >= 0; --i)
    542  1.9  christos 		for (p = opt_state->levels[i]; p; p = p->link) {
    543  1.1  christos 			compute_local_ud(p);
    544  1.1  christos 			p->out_use = 0;
    545  1.1  christos 		}
    546  1.1  christos 
    547  1.1  christos 	for (i = 1; i <= maxlevel; ++i) {
    548  1.9  christos 		for (p = opt_state->levels[i]; p; p = p->link) {
    549  1.1  christos 			p->out_use |= JT(p)->in_use | JF(p)->in_use;
    550  1.1  christos 			p->in_use |= p->out_use &~ p->kill;
    551  1.1  christos 		}
    552  1.1  christos 	}
    553  1.1  christos }
    554  1.1  christos static void
    555  1.9  christos init_val(opt_state_t *opt_state)
    556  1.1  christos {
    557  1.9  christos 	opt_state->curval = 0;
    558  1.9  christos 	opt_state->next_vnode = opt_state->vnode_base;
    559  1.9  christos 	memset((char *)opt_state->vmap, 0, opt_state->maxval * sizeof(*opt_state->vmap));
    560  1.9  christos 	memset((char *)opt_state->hashtbl, 0, sizeof opt_state->hashtbl);
    561  1.1  christos }
    562  1.1  christos 
    563  1.1  christos /* Because we really don't have an IR, this stuff is a little messy. */
    564  1.1  christos static int
    565  1.9  christos F(opt_state_t *opt_state, int code, int v0, int v1)
    566  1.1  christos {
    567  1.1  christos 	u_int hash;
    568  1.1  christos 	int val;
    569  1.1  christos 	struct valnode *p;
    570  1.1  christos 
    571  1.1  christos 	hash = (u_int)code ^ (v0 << 4) ^ (v1 << 8);
    572  1.1  christos 	hash %= MODULUS;
    573  1.1  christos 
    574  1.9  christos 	for (p = opt_state->hashtbl[hash]; p; p = p->next)
    575  1.1  christos 		if (p->code == code && p->v0 == v0 && p->v1 == v1)
    576  1.1  christos 			return p->val;
    577  1.1  christos 
    578  1.9  christos 	val = ++opt_state->curval;
    579  1.1  christos 	if (BPF_MODE(code) == BPF_IMM &&
    580  1.1  christos 	    (BPF_CLASS(code) == BPF_LD || BPF_CLASS(code) == BPF_LDX)) {
    581  1.9  christos 		opt_state->vmap[val].const_val = v0;
    582  1.9  christos 		opt_state->vmap[val].is_const = 1;
    583  1.1  christos 	}
    584  1.9  christos 	p = opt_state->next_vnode++;
    585  1.1  christos 	p->val = val;
    586  1.1  christos 	p->code = code;
    587  1.1  christos 	p->v0 = v0;
    588  1.1  christos 	p->v1 = v1;
    589  1.9  christos 	p->next = opt_state->hashtbl[hash];
    590  1.9  christos 	opt_state->hashtbl[hash] = p;
    591  1.1  christos 
    592  1.1  christos 	return val;
    593  1.1  christos }
    594  1.1  christos 
    595  1.1  christos static inline void
    596  1.6  christos vstore(struct stmt *s, int *valp, int newval, int alter)
    597  1.1  christos {
    598  1.1  christos 	if (alter && *valp == newval)
    599  1.1  christos 		s->code = NOP;
    600  1.1  christos 	else
    601  1.1  christos 		*valp = newval;
    602  1.1  christos }
    603  1.1  christos 
    604  1.6  christos /*
    605  1.6  christos  * Do constant-folding on binary operators.
    606  1.6  christos  * (Unary operators are handled elsewhere.)
    607  1.6  christos  */
    608  1.1  christos static void
    609  1.9  christos fold_op(compiler_state_t *cstate, struct icode *ic, opt_state_t *opt_state,
    610  1.9  christos     struct stmt *s, int v0, int v1)
    611  1.1  christos {
    612  1.1  christos 	bpf_u_int32 a, b;
    613  1.1  christos 
    614  1.9  christos 	a = opt_state->vmap[v0].const_val;
    615  1.9  christos 	b = opt_state->vmap[v1].const_val;
    616  1.1  christos 
    617  1.1  christos 	switch (BPF_OP(s->code)) {
    618  1.1  christos 	case BPF_ADD:
    619  1.1  christos 		a += b;
    620  1.1  christos 		break;
    621  1.1  christos 
    622  1.1  christos 	case BPF_SUB:
    623  1.1  christos 		a -= b;
    624  1.1  christos 		break;
    625  1.1  christos 
    626  1.1  christos 	case BPF_MUL:
    627  1.1  christos 		a *= b;
    628  1.1  christos 		break;
    629  1.1  christos 
    630  1.1  christos 	case BPF_DIV:
    631  1.1  christos 		if (b == 0)
    632  1.9  christos 			bpf_error(cstate, "division by zero");
    633  1.1  christos 		a /= b;
    634  1.1  christos 		break;
    635  1.1  christos 
    636  1.7  christos 	case BPF_MOD:
    637  1.7  christos 		if (b == 0)
    638  1.9  christos 			bpf_error(cstate, "modulus by zero");
    639  1.7  christos 		a %= b;
    640  1.7  christos 		break;
    641  1.7  christos 
    642  1.1  christos 	case BPF_AND:
    643  1.1  christos 		a &= b;
    644  1.1  christos 		break;
    645  1.1  christos 
    646  1.1  christos 	case BPF_OR:
    647  1.1  christos 		a |= b;
    648  1.1  christos 		break;
    649  1.1  christos 
    650  1.7  christos 	case BPF_XOR:
    651  1.7  christos 		a ^= b;
    652  1.7  christos 		break;
    653  1.7  christos 
    654  1.1  christos 	case BPF_LSH:
    655  1.1  christos 		a <<= b;
    656  1.1  christos 		break;
    657  1.1  christos 
    658  1.1  christos 	case BPF_RSH:
    659  1.1  christos 		a >>= b;
    660  1.1  christos 		break;
    661  1.1  christos 
    662  1.1  christos 	default:
    663  1.1  christos 		abort();
    664  1.1  christos 	}
    665  1.1  christos 	s->k = a;
    666  1.1  christos 	s->code = BPF_LD|BPF_IMM;
    667  1.9  christos 	opt_state->done = 0;
    668  1.1  christos }
    669  1.1  christos 
    670  1.1  christos static inline struct slist *
    671  1.6  christos this_op(struct slist *s)
    672  1.1  christos {
    673  1.1  christos 	while (s != 0 && s->s.code == NOP)
    674  1.1  christos 		s = s->next;
    675  1.1  christos 	return s;
    676  1.1  christos }
    677  1.1  christos 
    678  1.1  christos static void
    679  1.6  christos opt_not(struct block *b)
    680  1.1  christos {
    681  1.1  christos 	struct block *tmp = JT(b);
    682  1.1  christos 
    683  1.1  christos 	JT(b) = JF(b);
    684  1.1  christos 	JF(b) = tmp;
    685  1.1  christos }
    686  1.1  christos 
    687  1.1  christos static void
    688  1.9  christos opt_peep(opt_state_t *opt_state, struct block *b)
    689  1.1  christos {
    690  1.1  christos 	struct slist *s;
    691  1.1  christos 	struct slist *next, *last;
    692  1.1  christos 	int val;
    693  1.1  christos 
    694  1.1  christos 	s = b->stmts;
    695  1.1  christos 	if (s == 0)
    696  1.1  christos 		return;
    697  1.1  christos 
    698  1.1  christos 	last = s;
    699  1.1  christos 	for (/*empty*/; /*empty*/; s = next) {
    700  1.1  christos 		/*
    701  1.1  christos 		 * Skip over nops.
    702  1.1  christos 		 */
    703  1.1  christos 		s = this_op(s);
    704  1.1  christos 		if (s == 0)
    705  1.1  christos 			break;	/* nothing left in the block */
    706  1.1  christos 
    707  1.1  christos 		/*
    708  1.1  christos 		 * Find the next real instruction after that one
    709  1.1  christos 		 * (skipping nops).
    710  1.1  christos 		 */
    711  1.1  christos 		next = this_op(s->next);
    712  1.1  christos 		if (next == 0)
    713  1.1  christos 			break;	/* no next instruction */
    714  1.1  christos 		last = next;
    715  1.1  christos 
    716  1.1  christos 		/*
    717  1.1  christos 		 * st  M[k]	-->	st  M[k]
    718  1.1  christos 		 * ldx M[k]		tax
    719  1.1  christos 		 */
    720  1.1  christos 		if (s->s.code == BPF_ST &&
    721  1.1  christos 		    next->s.code == (BPF_LDX|BPF_MEM) &&
    722  1.1  christos 		    s->s.k == next->s.k) {
    723  1.9  christos 			opt_state->done = 0;
    724  1.1  christos 			next->s.code = BPF_MISC|BPF_TAX;
    725  1.1  christos 		}
    726  1.1  christos 		/*
    727  1.1  christos 		 * ld  #k	-->	ldx  #k
    728  1.1  christos 		 * tax			txa
    729  1.1  christos 		 */
    730  1.1  christos 		if (s->s.code == (BPF_LD|BPF_IMM) &&
    731  1.1  christos 		    next->s.code == (BPF_MISC|BPF_TAX)) {
    732  1.1  christos 			s->s.code = BPF_LDX|BPF_IMM;
    733  1.1  christos 			next->s.code = BPF_MISC|BPF_TXA;
    734  1.9  christos 			opt_state->done = 0;
    735  1.1  christos 		}
    736  1.1  christos 		/*
    737  1.1  christos 		 * This is an ugly special case, but it happens
    738  1.1  christos 		 * when you say tcp[k] or udp[k] where k is a constant.
    739  1.1  christos 		 */
    740  1.1  christos 		if (s->s.code == (BPF_LD|BPF_IMM)) {
    741  1.1  christos 			struct slist *add, *tax, *ild;
    742  1.1  christos 
    743  1.1  christos 			/*
    744  1.1  christos 			 * Check that X isn't used on exit from this
    745  1.1  christos 			 * block (which the optimizer might cause).
    746  1.1  christos 			 * We know the code generator won't generate
    747  1.1  christos 			 * any local dependencies.
    748  1.1  christos 			 */
    749  1.1  christos 			if (ATOMELEM(b->out_use, X_ATOM))
    750  1.1  christos 				continue;
    751  1.1  christos 
    752  1.1  christos 			/*
    753  1.1  christos 			 * Check that the instruction following the ldi
    754  1.1  christos 			 * is an addx, or it's an ldxms with an addx
    755  1.1  christos 			 * following it (with 0 or more nops between the
    756  1.1  christos 			 * ldxms and addx).
    757  1.1  christos 			 */
    758  1.1  christos 			if (next->s.code != (BPF_LDX|BPF_MSH|BPF_B))
    759  1.1  christos 				add = next;
    760  1.1  christos 			else
    761  1.1  christos 				add = this_op(next->next);
    762  1.1  christos 			if (add == 0 || add->s.code != (BPF_ALU|BPF_ADD|BPF_X))
    763  1.1  christos 				continue;
    764  1.1  christos 
    765  1.1  christos 			/*
    766  1.1  christos 			 * Check that a tax follows that (with 0 or more
    767  1.1  christos 			 * nops between them).
    768  1.1  christos 			 */
    769  1.1  christos 			tax = this_op(add->next);
    770  1.1  christos 			if (tax == 0 || tax->s.code != (BPF_MISC|BPF_TAX))
    771  1.1  christos 				continue;
    772  1.1  christos 
    773  1.1  christos 			/*
    774  1.1  christos 			 * Check that an ild follows that (with 0 or more
    775  1.1  christos 			 * nops between them).
    776  1.1  christos 			 */
    777  1.1  christos 			ild = this_op(tax->next);
    778  1.1  christos 			if (ild == 0 || BPF_CLASS(ild->s.code) != BPF_LD ||
    779  1.1  christos 			    BPF_MODE(ild->s.code) != BPF_IND)
    780  1.1  christos 				continue;
    781  1.1  christos 			/*
    782  1.1  christos 			 * We want to turn this sequence:
    783  1.1  christos 			 *
    784  1.1  christos 			 * (004) ldi     #0x2		{s}
    785  1.1  christos 			 * (005) ldxms   [14]		{next}  -- optional
    786  1.1  christos 			 * (006) addx			{add}
    787  1.1  christos 			 * (007) tax			{tax}
    788  1.1  christos 			 * (008) ild     [x+0]		{ild}
    789  1.1  christos 			 *
    790  1.1  christos 			 * into this sequence:
    791  1.1  christos 			 *
    792  1.1  christos 			 * (004) nop
    793  1.1  christos 			 * (005) ldxms   [14]
    794  1.1  christos 			 * (006) nop
    795  1.1  christos 			 * (007) nop
    796  1.1  christos 			 * (008) ild     [x+2]
    797  1.1  christos 			 *
    798  1.1  christos 			 * XXX We need to check that X is not
    799  1.1  christos 			 * subsequently used, because we want to change
    800  1.1  christos 			 * what'll be in it after this sequence.
    801  1.1  christos 			 *
    802  1.1  christos 			 * We know we can eliminate the accumulator
    803  1.1  christos 			 * modifications earlier in the sequence since
    804  1.1  christos 			 * it is defined by the last stmt of this sequence
    805  1.1  christos 			 * (i.e., the last statement of the sequence loads
    806  1.1  christos 			 * a value into the accumulator, so we can eliminate
    807  1.1  christos 			 * earlier operations on the accumulator).
    808  1.1  christos 			 */
    809  1.1  christos 			ild->s.k += s->s.k;
    810  1.1  christos 			s->s.code = NOP;
    811  1.1  christos 			add->s.code = NOP;
    812  1.1  christos 			tax->s.code = NOP;
    813  1.9  christos 			opt_state->done = 0;
    814  1.1  christos 		}
    815  1.1  christos 	}
    816  1.1  christos 	/*
    817  1.1  christos 	 * If the comparison at the end of a block is an equality
    818  1.1  christos 	 * comparison against a constant, and nobody uses the value
    819  1.1  christos 	 * we leave in the A register at the end of a block, and
    820  1.1  christos 	 * the operation preceding the comparison is an arithmetic
    821  1.1  christos 	 * operation, we can sometime optimize it away.
    822  1.1  christos 	 */
    823  1.1  christos 	if (b->s.code == (BPF_JMP|BPF_JEQ|BPF_K) &&
    824  1.1  christos 	    !ATOMELEM(b->out_use, A_ATOM)) {
    825  1.1  christos 	    	/*
    826  1.1  christos 	    	 * We can optimize away certain subtractions of the
    827  1.1  christos 	    	 * X register.
    828  1.1  christos 	    	 */
    829  1.1  christos 		if (last->s.code == (BPF_ALU|BPF_SUB|BPF_X)) {
    830  1.1  christos 			val = b->val[X_ATOM];
    831  1.9  christos 			if (opt_state->vmap[val].is_const) {
    832  1.1  christos 				/*
    833  1.1  christos 				 * If we have a subtract to do a comparison,
    834  1.1  christos 				 * and the X register is a known constant,
    835  1.1  christos 				 * we can merge this value into the
    836  1.1  christos 				 * comparison:
    837  1.1  christos 				 *
    838  1.1  christos 				 * sub x  ->	nop
    839  1.1  christos 				 * jeq #y	jeq #(x+y)
    840  1.1  christos 				 */
    841  1.9  christos 				b->s.k += opt_state->vmap[val].const_val;
    842  1.1  christos 				last->s.code = NOP;
    843  1.9  christos 				opt_state->done = 0;
    844  1.1  christos 			} else if (b->s.k == 0) {
    845  1.1  christos 				/*
    846  1.1  christos 				 * If the X register isn't a constant,
    847  1.1  christos 				 * and the comparison in the test is
    848  1.1  christos 				 * against 0, we can compare with the
    849  1.1  christos 				 * X register, instead:
    850  1.1  christos 				 *
    851  1.1  christos 				 * sub x  ->	nop
    852  1.1  christos 				 * jeq #0	jeq x
    853  1.1  christos 				 */
    854  1.1  christos 				last->s.code = NOP;
    855  1.1  christos 				b->s.code = BPF_JMP|BPF_JEQ|BPF_X;
    856  1.9  christos 				opt_state->done = 0;
    857  1.1  christos 			}
    858  1.1  christos 		}
    859  1.1  christos 		/*
    860  1.1  christos 		 * Likewise, a constant subtract can be simplified:
    861  1.1  christos 		 *
    862  1.1  christos 		 * sub #x ->	nop
    863  1.1  christos 		 * jeq #y ->	jeq #(x+y)
    864  1.1  christos 		 */
    865  1.1  christos 		else if (last->s.code == (BPF_ALU|BPF_SUB|BPF_K)) {
    866  1.1  christos 			last->s.code = NOP;
    867  1.1  christos 			b->s.k += last->s.k;
    868  1.9  christos 			opt_state->done = 0;
    869  1.1  christos 		}
    870  1.1  christos 		/*
    871  1.1  christos 		 * And, similarly, a constant AND can be simplified
    872  1.1  christos 		 * if we're testing against 0, i.e.:
    873  1.1  christos 		 *
    874  1.1  christos 		 * and #k	nop
    875  1.1  christos 		 * jeq #0  ->	jset #k
    876  1.1  christos 		 */
    877  1.1  christos 		else if (last->s.code == (BPF_ALU|BPF_AND|BPF_K) &&
    878  1.1  christos 		    b->s.k == 0) {
    879  1.1  christos 			b->s.k = last->s.k;
    880  1.1  christos 			b->s.code = BPF_JMP|BPF_K|BPF_JSET;
    881  1.1  christos 			last->s.code = NOP;
    882  1.9  christos 			opt_state->done = 0;
    883  1.1  christos 			opt_not(b);
    884  1.1  christos 		}
    885  1.1  christos 	}
    886  1.1  christos 	/*
    887  1.1  christos 	 * jset #0        ->   never
    888  1.1  christos 	 * jset #ffffffff ->   always
    889  1.1  christos 	 */
    890  1.1  christos 	if (b->s.code == (BPF_JMP|BPF_K|BPF_JSET)) {
    891  1.1  christos 		if (b->s.k == 0)
    892  1.1  christos 			JT(b) = JF(b);
    893  1.9  christos 		if ((u_int)b->s.k == 0xffffffffU)
    894  1.1  christos 			JF(b) = JT(b);
    895  1.1  christos 	}
    896  1.1  christos 	/*
    897  1.1  christos 	 * If we're comparing against the index register, and the index
    898  1.1  christos 	 * register is a known constant, we can just compare against that
    899  1.1  christos 	 * constant.
    900  1.1  christos 	 */
    901  1.1  christos 	val = b->val[X_ATOM];
    902  1.9  christos 	if (opt_state->vmap[val].is_const && BPF_SRC(b->s.code) == BPF_X) {
    903  1.9  christos 		bpf_int32 v = opt_state->vmap[val].const_val;
    904  1.1  christos 		b->s.code &= ~BPF_X;
    905  1.1  christos 		b->s.k = v;
    906  1.1  christos 	}
    907  1.1  christos 	/*
    908  1.1  christos 	 * If the accumulator is a known constant, we can compute the
    909  1.1  christos 	 * comparison result.
    910  1.1  christos 	 */
    911  1.1  christos 	val = b->val[A_ATOM];
    912  1.9  christos 	if (opt_state->vmap[val].is_const && BPF_SRC(b->s.code) == BPF_K) {
    913  1.9  christos 		bpf_int32 v = opt_state->vmap[val].const_val;
    914  1.1  christos 		switch (BPF_OP(b->s.code)) {
    915  1.1  christos 
    916  1.1  christos 		case BPF_JEQ:
    917  1.1  christos 			v = v == b->s.k;
    918  1.1  christos 			break;
    919  1.1  christos 
    920  1.1  christos 		case BPF_JGT:
    921  1.2  christos 			v = (unsigned)v > (unsigned)b->s.k;
    922  1.1  christos 			break;
    923  1.1  christos 
    924  1.1  christos 		case BPF_JGE:
    925  1.2  christos 			v = (unsigned)v >= (unsigned)b->s.k;
    926  1.1  christos 			break;
    927  1.1  christos 
    928  1.1  christos 		case BPF_JSET:
    929  1.1  christos 			v &= b->s.k;
    930  1.1  christos 			break;
    931  1.1  christos 
    932  1.1  christos 		default:
    933  1.1  christos 			abort();
    934  1.1  christos 		}
    935  1.1  christos 		if (JF(b) != JT(b))
    936  1.9  christos 			opt_state->done = 0;
    937  1.1  christos 		if (v)
    938  1.1  christos 			JF(b) = JT(b);
    939  1.1  christos 		else
    940  1.1  christos 			JT(b) = JF(b);
    941  1.1  christos 	}
    942  1.1  christos }
    943  1.1  christos 
    944  1.1  christos /*
    945  1.1  christos  * Compute the symbolic value of expression of 's', and update
    946  1.1  christos  * anything it defines in the value table 'val'.  If 'alter' is true,
    947  1.1  christos  * do various optimizations.  This code would be cleaner if symbolic
    948  1.1  christos  * evaluation and code transformations weren't folded together.
    949  1.1  christos  */
    950  1.1  christos static void
    951  1.9  christos opt_stmt(compiler_state_t *cstate, struct icode *ic, opt_state_t *opt_state,
    952  1.9  christos     struct stmt *s, int val[], int alter)
    953  1.1  christos {
    954  1.1  christos 	int op;
    955  1.1  christos 	int v;
    956  1.1  christos 
    957  1.1  christos 	switch (s->code) {
    958  1.1  christos 
    959  1.1  christos 	case BPF_LD|BPF_ABS|BPF_W:
    960  1.1  christos 	case BPF_LD|BPF_ABS|BPF_H:
    961  1.1  christos 	case BPF_LD|BPF_ABS|BPF_B:
    962  1.9  christos 		v = F(opt_state, s->code, s->k, 0L);
    963  1.1  christos 		vstore(s, &val[A_ATOM], v, alter);
    964  1.1  christos 		break;
    965  1.1  christos 
    966  1.1  christos 	case BPF_LD|BPF_IND|BPF_W:
    967  1.1  christos 	case BPF_LD|BPF_IND|BPF_H:
    968  1.1  christos 	case BPF_LD|BPF_IND|BPF_B:
    969  1.1  christos 		v = val[X_ATOM];
    970  1.9  christos 		if (alter && opt_state->vmap[v].is_const) {
    971  1.1  christos 			s->code = BPF_LD|BPF_ABS|BPF_SIZE(s->code);
    972  1.9  christos 			s->k += opt_state->vmap[v].const_val;
    973  1.9  christos 			v = F(opt_state, s->code, s->k, 0L);
    974  1.9  christos 			opt_state->done = 0;
    975  1.1  christos 		}
    976  1.1  christos 		else
    977  1.9  christos 			v = F(opt_state, s->code, s->k, v);
    978  1.1  christos 		vstore(s, &val[A_ATOM], v, alter);
    979  1.1  christos 		break;
    980  1.1  christos 
    981  1.1  christos 	case BPF_LD|BPF_LEN:
    982  1.9  christos 		v = F(opt_state, s->code, 0L, 0L);
    983  1.1  christos 		vstore(s, &val[A_ATOM], v, alter);
    984  1.1  christos 		break;
    985  1.1  christos 
    986  1.1  christos 	case BPF_LD|BPF_IMM:
    987  1.1  christos 		v = K(s->k);
    988  1.1  christos 		vstore(s, &val[A_ATOM], v, alter);
    989  1.1  christos 		break;
    990  1.1  christos 
    991  1.1  christos 	case BPF_LDX|BPF_IMM:
    992  1.1  christos 		v = K(s->k);
    993  1.1  christos 		vstore(s, &val[X_ATOM], v, alter);
    994  1.1  christos 		break;
    995  1.1  christos 
    996  1.1  christos 	case BPF_LDX|BPF_MSH|BPF_B:
    997  1.9  christos 		v = F(opt_state, s->code, s->k, 0L);
    998  1.1  christos 		vstore(s, &val[X_ATOM], v, alter);
    999  1.1  christos 		break;
   1000  1.1  christos 
   1001  1.1  christos 	case BPF_ALU|BPF_NEG:
   1002  1.9  christos 		if (alter && opt_state->vmap[val[A_ATOM]].is_const) {
   1003  1.1  christos 			s->code = BPF_LD|BPF_IMM;
   1004  1.9  christos 			s->k = -opt_state->vmap[val[A_ATOM]].const_val;
   1005  1.1  christos 			val[A_ATOM] = K(s->k);
   1006  1.1  christos 		}
   1007  1.1  christos 		else
   1008  1.9  christos 			val[A_ATOM] = F(opt_state, s->code, val[A_ATOM], 0L);
   1009  1.1  christos 		break;
   1010  1.1  christos 
   1011  1.1  christos 	case BPF_ALU|BPF_ADD|BPF_K:
   1012  1.1  christos 	case BPF_ALU|BPF_SUB|BPF_K:
   1013  1.1  christos 	case BPF_ALU|BPF_MUL|BPF_K:
   1014  1.1  christos 	case BPF_ALU|BPF_DIV|BPF_K:
   1015  1.7  christos 	case BPF_ALU|BPF_MOD|BPF_K:
   1016  1.1  christos 	case BPF_ALU|BPF_AND|BPF_K:
   1017  1.1  christos 	case BPF_ALU|BPF_OR|BPF_K:
   1018  1.7  christos 	case BPF_ALU|BPF_XOR|BPF_K:
   1019  1.1  christos 	case BPF_ALU|BPF_LSH|BPF_K:
   1020  1.1  christos 	case BPF_ALU|BPF_RSH|BPF_K:
   1021  1.1  christos 		op = BPF_OP(s->code);
   1022  1.1  christos 		if (alter) {
   1023  1.1  christos 			if (s->k == 0) {
   1024  1.1  christos 				/* don't optimize away "sub #0"
   1025  1.1  christos 				 * as it may be needed later to
   1026  1.1  christos 				 * fixup the generated math code */
   1027  1.1  christos 				if (op == BPF_ADD ||
   1028  1.1  christos 				    op == BPF_LSH || op == BPF_RSH ||
   1029  1.7  christos 				    op == BPF_OR || op == BPF_XOR) {
   1030  1.1  christos 					s->code = NOP;
   1031  1.1  christos 					break;
   1032  1.1  christos 				}
   1033  1.1  christos 				if (op == BPF_MUL || op == BPF_AND) {
   1034  1.1  christos 					s->code = BPF_LD|BPF_IMM;
   1035  1.1  christos 					val[A_ATOM] = K(s->k);
   1036  1.1  christos 					break;
   1037  1.1  christos 				}
   1038  1.1  christos 			}
   1039  1.9  christos 			if (opt_state->vmap[val[A_ATOM]].is_const) {
   1040  1.9  christos 				fold_op(cstate, ic, opt_state, s, val[A_ATOM], K(s->k));
   1041  1.1  christos 				val[A_ATOM] = K(s->k);
   1042  1.1  christos 				break;
   1043  1.1  christos 			}
   1044  1.1  christos 		}
   1045  1.9  christos 		val[A_ATOM] = F(opt_state, s->code, val[A_ATOM], K(s->k));
   1046  1.1  christos 		break;
   1047  1.1  christos 
   1048  1.1  christos 	case BPF_ALU|BPF_ADD|BPF_X:
   1049  1.1  christos 	case BPF_ALU|BPF_SUB|BPF_X:
   1050  1.1  christos 	case BPF_ALU|BPF_MUL|BPF_X:
   1051  1.1  christos 	case BPF_ALU|BPF_DIV|BPF_X:
   1052  1.7  christos 	case BPF_ALU|BPF_MOD|BPF_X:
   1053  1.1  christos 	case BPF_ALU|BPF_AND|BPF_X:
   1054  1.1  christos 	case BPF_ALU|BPF_OR|BPF_X:
   1055  1.7  christos 	case BPF_ALU|BPF_XOR|BPF_X:
   1056  1.1  christos 	case BPF_ALU|BPF_LSH|BPF_X:
   1057  1.1  christos 	case BPF_ALU|BPF_RSH|BPF_X:
   1058  1.1  christos 		op = BPF_OP(s->code);
   1059  1.9  christos 		if (alter && opt_state->vmap[val[X_ATOM]].is_const) {
   1060  1.9  christos 			if (opt_state->vmap[val[A_ATOM]].is_const) {
   1061  1.9  christos 				fold_op(cstate, ic, opt_state, s, val[A_ATOM], val[X_ATOM]);
   1062  1.1  christos 				val[A_ATOM] = K(s->k);
   1063  1.1  christos 			}
   1064  1.1  christos 			else {
   1065  1.1  christos 				s->code = BPF_ALU|BPF_K|op;
   1066  1.9  christos 				s->k = opt_state->vmap[val[X_ATOM]].const_val;
   1067  1.9  christos 				opt_state->done = 0;
   1068  1.1  christos 				val[A_ATOM] =
   1069  1.9  christos 					F(opt_state, s->code, val[A_ATOM], K(s->k));
   1070  1.1  christos 			}
   1071  1.1  christos 			break;
   1072  1.1  christos 		}
   1073  1.1  christos 		/*
   1074  1.1  christos 		 * Check if we're doing something to an accumulator
   1075  1.1  christos 		 * that is 0, and simplify.  This may not seem like
   1076  1.1  christos 		 * much of a simplification but it could open up further
   1077  1.1  christos 		 * optimizations.
   1078  1.1  christos 		 * XXX We could also check for mul by 1, etc.
   1079  1.1  christos 		 */
   1080  1.9  christos 		if (alter && opt_state->vmap[val[A_ATOM]].is_const
   1081  1.9  christos 		    && opt_state->vmap[val[A_ATOM]].const_val == 0) {
   1082  1.7  christos 			if (op == BPF_ADD || op == BPF_OR || op == BPF_XOR) {
   1083  1.1  christos 				s->code = BPF_MISC|BPF_TXA;
   1084  1.1  christos 				vstore(s, &val[A_ATOM], val[X_ATOM], alter);
   1085  1.1  christos 				break;
   1086  1.1  christos 			}
   1087  1.7  christos 			else if (op == BPF_MUL || op == BPF_DIV || op == BPF_MOD ||
   1088  1.1  christos 				 op == BPF_AND || op == BPF_LSH || op == BPF_RSH) {
   1089  1.1  christos 				s->code = BPF_LD|BPF_IMM;
   1090  1.1  christos 				s->k = 0;
   1091  1.1  christos 				vstore(s, &val[A_ATOM], K(s->k), alter);
   1092  1.1  christos 				break;
   1093  1.1  christos 			}
   1094  1.1  christos 			else if (op == BPF_NEG) {
   1095  1.1  christos 				s->code = NOP;
   1096  1.1  christos 				break;
   1097  1.1  christos 			}
   1098  1.1  christos 		}
   1099  1.9  christos 		val[A_ATOM] = F(opt_state, s->code, val[A_ATOM], val[X_ATOM]);
   1100  1.1  christos 		break;
   1101  1.1  christos 
   1102  1.1  christos 	case BPF_MISC|BPF_TXA:
   1103  1.1  christos 		vstore(s, &val[A_ATOM], val[X_ATOM], alter);
   1104  1.1  christos 		break;
   1105  1.1  christos 
   1106  1.1  christos 	case BPF_LD|BPF_MEM:
   1107  1.1  christos 		v = val[s->k];
   1108  1.9  christos 		if (alter && opt_state->vmap[v].is_const) {
   1109  1.1  christos 			s->code = BPF_LD|BPF_IMM;
   1110  1.9  christos 			s->k = opt_state->vmap[v].const_val;
   1111  1.9  christos 			opt_state->done = 0;
   1112  1.1  christos 		}
   1113  1.1  christos 		vstore(s, &val[A_ATOM], v, alter);
   1114  1.1  christos 		break;
   1115  1.1  christos 
   1116  1.1  christos 	case BPF_MISC|BPF_TAX:
   1117  1.1  christos 		vstore(s, &val[X_ATOM], val[A_ATOM], alter);
   1118  1.1  christos 		break;
   1119  1.1  christos 
   1120  1.1  christos 	case BPF_LDX|BPF_MEM:
   1121  1.1  christos 		v = val[s->k];
   1122  1.9  christos 		if (alter && opt_state->vmap[v].is_const) {
   1123  1.1  christos 			s->code = BPF_LDX|BPF_IMM;
   1124  1.9  christos 			s->k = opt_state->vmap[v].const_val;
   1125  1.9  christos 			opt_state->done = 0;
   1126  1.1  christos 		}
   1127  1.1  christos 		vstore(s, &val[X_ATOM], v, alter);
   1128  1.1  christos 		break;
   1129  1.1  christos 
   1130  1.1  christos 	case BPF_ST:
   1131  1.1  christos 		vstore(s, &val[s->k], val[A_ATOM], alter);
   1132  1.1  christos 		break;
   1133  1.1  christos 
   1134  1.1  christos 	case BPF_STX:
   1135  1.1  christos 		vstore(s, &val[s->k], val[X_ATOM], alter);
   1136  1.1  christos 		break;
   1137  1.1  christos 	}
   1138  1.1  christos }
   1139  1.1  christos 
   1140  1.1  christos static void
   1141  1.9  christos deadstmt(opt_state_t *opt_state, register struct stmt *s, register struct stmt *last[])
   1142  1.1  christos {
   1143  1.1  christos 	register int atom;
   1144  1.1  christos 
   1145  1.1  christos 	atom = atomuse(s);
   1146  1.1  christos 	if (atom >= 0) {
   1147  1.1  christos 		if (atom == AX_ATOM) {
   1148  1.1  christos 			last[X_ATOM] = 0;
   1149  1.1  christos 			last[A_ATOM] = 0;
   1150  1.1  christos 		}
   1151  1.1  christos 		else
   1152  1.1  christos 			last[atom] = 0;
   1153  1.1  christos 	}
   1154  1.1  christos 	atom = atomdef(s);
   1155  1.1  christos 	if (atom >= 0) {
   1156  1.1  christos 		if (last[atom]) {
   1157  1.9  christos 			opt_state->done = 0;
   1158  1.1  christos 			last[atom]->code = NOP;
   1159  1.1  christos 		}
   1160  1.1  christos 		last[atom] = s;
   1161  1.1  christos 	}
   1162  1.1  christos }
   1163  1.1  christos 
   1164  1.1  christos static void
   1165  1.9  christos opt_deadstores(opt_state_t *opt_state, register struct block *b)
   1166  1.1  christos {
   1167  1.1  christos 	register struct slist *s;
   1168  1.1  christos 	register int atom;
   1169  1.1  christos 	struct stmt *last[N_ATOMS];
   1170  1.1  christos 
   1171  1.1  christos 	memset((char *)last, 0, sizeof last);
   1172  1.1  christos 
   1173  1.1  christos 	for (s = b->stmts; s != 0; s = s->next)
   1174  1.9  christos 		deadstmt(opt_state, &s->s, last);
   1175  1.9  christos 	deadstmt(opt_state, &b->s, last);
   1176  1.1  christos 
   1177  1.1  christos 	for (atom = 0; atom < N_ATOMS; ++atom)
   1178  1.1  christos 		if (last[atom] && !ATOMELEM(b->out_use, atom)) {
   1179  1.1  christos 			last[atom]->code = NOP;
   1180  1.9  christos 			opt_state->done = 0;
   1181  1.1  christos 		}
   1182  1.1  christos }
   1183  1.1  christos 
   1184  1.1  christos static void
   1185  1.9  christos opt_blk(compiler_state_t *cstate, struct icode *ic, opt_state_t *opt_state,
   1186  1.9  christos     struct block *b, int do_stmts)
   1187  1.1  christos {
   1188  1.1  christos 	struct slist *s;
   1189  1.1  christos 	struct edge *p;
   1190  1.1  christos 	int i;
   1191  1.1  christos 	bpf_int32 aval, xval;
   1192  1.1  christos 
   1193  1.1  christos #if 0
   1194  1.1  christos 	for (s = b->stmts; s && s->next; s = s->next)
   1195  1.1  christos 		if (BPF_CLASS(s->s.code) == BPF_JMP) {
   1196  1.1  christos 			do_stmts = 0;
   1197  1.1  christos 			break;
   1198  1.1  christos 		}
   1199  1.1  christos #endif
   1200  1.1  christos 
   1201  1.1  christos 	/*
   1202  1.1  christos 	 * Initialize the atom values.
   1203  1.1  christos 	 */
   1204  1.1  christos 	p = b->in_edges;
   1205  1.1  christos 	if (p == 0) {
   1206  1.1  christos 		/*
   1207  1.1  christos 		 * We have no predecessors, so everything is undefined
   1208  1.1  christos 		 * upon entry to this block.
   1209  1.1  christos 		 */
   1210  1.1  christos 		memset((char *)b->val, 0, sizeof(b->val));
   1211  1.1  christos 	} else {
   1212  1.1  christos 		/*
   1213  1.1  christos 		 * Inherit values from our predecessors.
   1214  1.1  christos 		 *
   1215  1.1  christos 		 * First, get the values from the predecessor along the
   1216  1.1  christos 		 * first edge leading to this node.
   1217  1.1  christos 		 */
   1218  1.1  christos 		memcpy((char *)b->val, (char *)p->pred->val, sizeof(b->val));
   1219  1.1  christos 		/*
   1220  1.1  christos 		 * Now look at all the other nodes leading to this node.
   1221  1.1  christos 		 * If, for the predecessor along that edge, a register
   1222  1.1  christos 		 * has a different value from the one we have (i.e.,
   1223  1.1  christos 		 * control paths are merging, and the merging paths
   1224  1.1  christos 		 * assign different values to that register), give the
   1225  1.1  christos 		 * register the undefined value of 0.
   1226  1.1  christos 		 */
   1227  1.1  christos 		while ((p = p->next) != NULL) {
   1228  1.1  christos 			for (i = 0; i < N_ATOMS; ++i)
   1229  1.1  christos 				if (b->val[i] != p->pred->val[i])
   1230  1.1  christos 					b->val[i] = 0;
   1231  1.1  christos 		}
   1232  1.1  christos 	}
   1233  1.1  christos 	aval = b->val[A_ATOM];
   1234  1.1  christos 	xval = b->val[X_ATOM];
   1235  1.1  christos 	for (s = b->stmts; s; s = s->next)
   1236  1.9  christos 		opt_stmt(cstate, ic, opt_state, &s->s, b->val, do_stmts);
   1237  1.1  christos 
   1238  1.1  christos 	/*
   1239  1.1  christos 	 * This is a special case: if we don't use anything from this
   1240  1.1  christos 	 * block, and we load the accumulator or index register with a
   1241  1.1  christos 	 * value that is already there, or if this block is a return,
   1242  1.1  christos 	 * eliminate all the statements.
   1243  1.1  christos 	 *
   1244  1.1  christos 	 * XXX - what if it does a store?
   1245  1.1  christos 	 *
   1246  1.1  christos 	 * XXX - why does it matter whether we use anything from this
   1247  1.1  christos 	 * block?  If the accumulator or index register doesn't change
   1248  1.1  christos 	 * its value, isn't that OK even if we use that value?
   1249  1.1  christos 	 *
   1250  1.1  christos 	 * XXX - if we load the accumulator with a different value,
   1251  1.1  christos 	 * and the block ends with a conditional branch, we obviously
   1252  1.1  christos 	 * can't eliminate it, as the branch depends on that value.
   1253  1.1  christos 	 * For the index register, the conditional branch only depends
   1254  1.1  christos 	 * on the index register value if the test is against the index
   1255  1.1  christos 	 * register value rather than a constant; if nothing uses the
   1256  1.1  christos 	 * value we put into the index register, and we're not testing
   1257  1.1  christos 	 * against the index register's value, and there aren't any
   1258  1.1  christos 	 * other problems that would keep us from eliminating this
   1259  1.1  christos 	 * block, can we eliminate it?
   1260  1.1  christos 	 */
   1261  1.1  christos 	if (do_stmts &&
   1262  1.1  christos 	    ((b->out_use == 0 && aval != 0 && b->val[A_ATOM] == aval &&
   1263  1.1  christos 	      xval != 0 && b->val[X_ATOM] == xval) ||
   1264  1.1  christos 	     BPF_CLASS(b->s.code) == BPF_RET)) {
   1265  1.1  christos 		if (b->stmts != 0) {
   1266  1.1  christos 			b->stmts = 0;
   1267  1.9  christos 			opt_state->done = 0;
   1268  1.1  christos 		}
   1269  1.1  christos 	} else {
   1270  1.9  christos 		opt_peep(opt_state, b);
   1271  1.9  christos 		opt_deadstores(opt_state, b);
   1272  1.1  christos 	}
   1273  1.1  christos 	/*
   1274  1.1  christos 	 * Set up values for branch optimizer.
   1275  1.1  christos 	 */
   1276  1.1  christos 	if (BPF_SRC(b->s.code) == BPF_K)
   1277  1.1  christos 		b->oval = K(b->s.k);
   1278  1.1  christos 	else
   1279  1.1  christos 		b->oval = b->val[X_ATOM];
   1280  1.1  christos 	b->et.code = b->s.code;
   1281  1.1  christos 	b->ef.code = -b->s.code;
   1282  1.1  christos }
   1283  1.1  christos 
   1284  1.1  christos /*
   1285  1.1  christos  * Return true if any register that is used on exit from 'succ', has
   1286  1.1  christos  * an exit value that is different from the corresponding exit value
   1287  1.1  christos  * from 'b'.
   1288  1.1  christos  */
   1289  1.1  christos static int
   1290  1.6  christos use_conflict(struct block *b, struct block *succ)
   1291  1.1  christos {
   1292  1.1  christos 	int atom;
   1293  1.1  christos 	atomset use = succ->out_use;
   1294  1.1  christos 
   1295  1.1  christos 	if (use == 0)
   1296  1.1  christos 		return 0;
   1297  1.1  christos 
   1298  1.1  christos 	for (atom = 0; atom < N_ATOMS; ++atom)
   1299  1.1  christos 		if (ATOMELEM(use, atom))
   1300  1.1  christos 			if (b->val[atom] != succ->val[atom])
   1301  1.1  christos 				return 1;
   1302  1.1  christos 	return 0;
   1303  1.1  christos }
   1304  1.1  christos 
   1305  1.1  christos static struct block *
   1306  1.6  christos fold_edge(struct block *child, struct edge *ep)
   1307  1.1  christos {
   1308  1.1  christos 	int sense;
   1309  1.1  christos 	int aval0, aval1, oval0, oval1;
   1310  1.1  christos 	int code = ep->code;
   1311  1.1  christos 
   1312  1.1  christos 	if (code < 0) {
   1313  1.1  christos 		code = -code;
   1314  1.1  christos 		sense = 0;
   1315  1.1  christos 	} else
   1316  1.1  christos 		sense = 1;
   1317  1.1  christos 
   1318  1.1  christos 	if (child->s.code != code)
   1319  1.1  christos 		return 0;
   1320  1.1  christos 
   1321  1.1  christos 	aval0 = child->val[A_ATOM];
   1322  1.1  christos 	oval0 = child->oval;
   1323  1.1  christos 	aval1 = ep->pred->val[A_ATOM];
   1324  1.1  christos 	oval1 = ep->pred->oval;
   1325  1.1  christos 
   1326  1.1  christos 	if (aval0 != aval1)
   1327  1.1  christos 		return 0;
   1328  1.1  christos 
   1329  1.1  christos 	if (oval0 == oval1)
   1330  1.1  christos 		/*
   1331  1.1  christos 		 * The operands of the branch instructions are
   1332  1.1  christos 		 * identical, so the result is true if a true
   1333  1.1  christos 		 * branch was taken to get here, otherwise false.
   1334  1.1  christos 		 */
   1335  1.1  christos 		return sense ? JT(child) : JF(child);
   1336  1.1  christos 
   1337  1.1  christos 	if (sense && code == (BPF_JMP|BPF_JEQ|BPF_K))
   1338  1.1  christos 		/*
   1339  1.1  christos 		 * At this point, we only know the comparison if we
   1340  1.1  christos 		 * came down the true branch, and it was an equality
   1341  1.1  christos 		 * comparison with a constant.
   1342  1.1  christos 		 *
   1343  1.1  christos 		 * I.e., if we came down the true branch, and the branch
   1344  1.1  christos 		 * was an equality comparison with a constant, we know the
   1345  1.1  christos 		 * accumulator contains that constant.  If we came down
   1346  1.1  christos 		 * the false branch, or the comparison wasn't with a
   1347  1.1  christos 		 * constant, we don't know what was in the accumulator.
   1348  1.1  christos 		 *
   1349  1.1  christos 		 * We rely on the fact that distinct constants have distinct
   1350  1.1  christos 		 * value numbers.
   1351  1.1  christos 		 */
   1352  1.1  christos 		return JF(child);
   1353  1.1  christos 
   1354  1.1  christos 	return 0;
   1355  1.1  christos }
   1356  1.1  christos 
   1357  1.1  christos static void
   1358  1.9  christos opt_j(opt_state_t *opt_state, struct edge *ep)
   1359  1.1  christos {
   1360  1.1  christos 	register int i, k;
   1361  1.1  christos 	register struct block *target;
   1362  1.1  christos 
   1363  1.1  christos 	if (JT(ep->succ) == 0)
   1364  1.1  christos 		return;
   1365  1.1  christos 
   1366  1.1  christos 	if (JT(ep->succ) == JF(ep->succ)) {
   1367  1.1  christos 		/*
   1368  1.1  christos 		 * Common branch targets can be eliminated, provided
   1369  1.1  christos 		 * there is no data dependency.
   1370  1.1  christos 		 */
   1371  1.1  christos 		if (!use_conflict(ep->pred, ep->succ->et.succ)) {
   1372  1.9  christos 			opt_state->done = 0;
   1373  1.1  christos 			ep->succ = JT(ep->succ);
   1374  1.1  christos 		}
   1375  1.1  christos 	}
   1376  1.1  christos 	/*
   1377  1.1  christos 	 * For each edge dominator that matches the successor of this
   1378  1.1  christos 	 * edge, promote the edge successor to the its grandchild.
   1379  1.1  christos 	 *
   1380  1.1  christos 	 * XXX We violate the set abstraction here in favor a reasonably
   1381  1.1  christos 	 * efficient loop.
   1382  1.1  christos 	 */
   1383  1.1  christos  top:
   1384  1.9  christos 	for (i = 0; i < opt_state->edgewords; ++i) {
   1385  1.1  christos 		register bpf_u_int32 x = ep->edom[i];
   1386  1.1  christos 
   1387  1.1  christos 		while (x != 0) {
   1388  1.1  christos 			k = ffs(x) - 1;
   1389  1.1  christos 			x &=~ (1 << k);
   1390  1.1  christos 			k += i * BITS_PER_WORD;
   1391  1.1  christos 
   1392  1.9  christos 			target = fold_edge(ep->succ, opt_state->edges[k]);
   1393  1.1  christos 			/*
   1394  1.1  christos 			 * Check that there is no data dependency between
   1395  1.1  christos 			 * nodes that will be violated if we move the edge.
   1396  1.1  christos 			 */
   1397  1.1  christos 			if (target != 0 && !use_conflict(ep->pred, target)) {
   1398  1.9  christos 				opt_state->done = 0;
   1399  1.1  christos 				ep->succ = target;
   1400  1.1  christos 				if (JT(target) != 0)
   1401  1.1  christos 					/*
   1402  1.1  christos 					 * Start over unless we hit a leaf.
   1403  1.1  christos 					 */
   1404  1.1  christos 					goto top;
   1405  1.1  christos 				return;
   1406  1.1  christos 			}
   1407  1.1  christos 		}
   1408  1.1  christos 	}
   1409  1.1  christos }
   1410  1.1  christos 
   1411  1.1  christos 
   1412  1.1  christos static void
   1413  1.9  christos or_pullup(opt_state_t *opt_state, struct block *b)
   1414  1.1  christos {
   1415  1.1  christos 	int val, at_top;
   1416  1.1  christos 	struct block *pull;
   1417  1.1  christos 	struct block **diffp, **samep;
   1418  1.1  christos 	struct edge *ep;
   1419  1.1  christos 
   1420  1.1  christos 	ep = b->in_edges;
   1421  1.1  christos 	if (ep == 0)
   1422  1.1  christos 		return;
   1423  1.1  christos 
   1424  1.1  christos 	/*
   1425  1.1  christos 	 * Make sure each predecessor loads the same value.
   1426  1.1  christos 	 * XXX why?
   1427  1.1  christos 	 */
   1428  1.1  christos 	val = ep->pred->val[A_ATOM];
   1429  1.1  christos 	for (ep = ep->next; ep != 0; ep = ep->next)
   1430  1.1  christos 		if (val != ep->pred->val[A_ATOM])
   1431  1.1  christos 			return;
   1432  1.1  christos 
   1433  1.1  christos 	if (JT(b->in_edges->pred) == b)
   1434  1.1  christos 		diffp = &JT(b->in_edges->pred);
   1435  1.1  christos 	else
   1436  1.1  christos 		diffp = &JF(b->in_edges->pred);
   1437  1.1  christos 
   1438  1.1  christos 	at_top = 1;
   1439  1.1  christos 	while (1) {
   1440  1.1  christos 		if (*diffp == 0)
   1441  1.1  christos 			return;
   1442  1.1  christos 
   1443  1.1  christos 		if (JT(*diffp) != JT(b))
   1444  1.1  christos 			return;
   1445  1.1  christos 
   1446  1.1  christos 		if (!SET_MEMBER((*diffp)->dom, b->id))
   1447  1.1  christos 			return;
   1448  1.1  christos 
   1449  1.1  christos 		if ((*diffp)->val[A_ATOM] != val)
   1450  1.1  christos 			break;
   1451  1.1  christos 
   1452  1.1  christos 		diffp = &JF(*diffp);
   1453  1.1  christos 		at_top = 0;
   1454  1.1  christos 	}
   1455  1.1  christos 	samep = &JF(*diffp);
   1456  1.1  christos 	while (1) {
   1457  1.1  christos 		if (*samep == 0)
   1458  1.1  christos 			return;
   1459  1.1  christos 
   1460  1.1  christos 		if (JT(*samep) != JT(b))
   1461  1.1  christos 			return;
   1462  1.1  christos 
   1463  1.1  christos 		if (!SET_MEMBER((*samep)->dom, b->id))
   1464  1.1  christos 			return;
   1465  1.1  christos 
   1466  1.1  christos 		if ((*samep)->val[A_ATOM] == val)
   1467  1.1  christos 			break;
   1468  1.1  christos 
   1469  1.1  christos 		/* XXX Need to check that there are no data dependencies
   1470  1.1  christos 		   between dp0 and dp1.  Currently, the code generator
   1471  1.1  christos 		   will not produce such dependencies. */
   1472  1.1  christos 		samep = &JF(*samep);
   1473  1.1  christos 	}
   1474  1.1  christos #ifdef notdef
   1475  1.1  christos 	/* XXX This doesn't cover everything. */
   1476  1.1  christos 	for (i = 0; i < N_ATOMS; ++i)
   1477  1.1  christos 		if ((*samep)->val[i] != pred->val[i])
   1478  1.1  christos 			return;
   1479  1.1  christos #endif
   1480  1.1  christos 	/* Pull up the node. */
   1481  1.1  christos 	pull = *samep;
   1482  1.1  christos 	*samep = JF(pull);
   1483  1.1  christos 	JF(pull) = *diffp;
   1484  1.1  christos 
   1485  1.1  christos 	/*
   1486  1.1  christos 	 * At the top of the chain, each predecessor needs to point at the
   1487  1.1  christos 	 * pulled up node.  Inside the chain, there is only one predecessor
   1488  1.1  christos 	 * to worry about.
   1489  1.1  christos 	 */
   1490  1.1  christos 	if (at_top) {
   1491  1.1  christos 		for (ep = b->in_edges; ep != 0; ep = ep->next) {
   1492  1.1  christos 			if (JT(ep->pred) == b)
   1493  1.1  christos 				JT(ep->pred) = pull;
   1494  1.1  christos 			else
   1495  1.1  christos 				JF(ep->pred) = pull;
   1496  1.1  christos 		}
   1497  1.1  christos 	}
   1498  1.1  christos 	else
   1499  1.1  christos 		*diffp = pull;
   1500  1.1  christos 
   1501  1.9  christos 	opt_state->done = 0;
   1502  1.1  christos }
   1503  1.1  christos 
   1504  1.1  christos static void
   1505  1.9  christos and_pullup(opt_state_t *opt_state, struct block *b)
   1506  1.1  christos {
   1507  1.1  christos 	int val, at_top;
   1508  1.1  christos 	struct block *pull;
   1509  1.1  christos 	struct block **diffp, **samep;
   1510  1.1  christos 	struct edge *ep;
   1511  1.1  christos 
   1512  1.1  christos 	ep = b->in_edges;
   1513  1.1  christos 	if (ep == 0)
   1514  1.1  christos 		return;
   1515  1.1  christos 
   1516  1.1  christos 	/*
   1517  1.1  christos 	 * Make sure each predecessor loads the same value.
   1518  1.1  christos 	 */
   1519  1.1  christos 	val = ep->pred->val[A_ATOM];
   1520  1.1  christos 	for (ep = ep->next; ep != 0; ep = ep->next)
   1521  1.1  christos 		if (val != ep->pred->val[A_ATOM])
   1522  1.1  christos 			return;
   1523  1.1  christos 
   1524  1.1  christos 	if (JT(b->in_edges->pred) == b)
   1525  1.1  christos 		diffp = &JT(b->in_edges->pred);
   1526  1.1  christos 	else
   1527  1.1  christos 		diffp = &JF(b->in_edges->pred);
   1528  1.1  christos 
   1529  1.1  christos 	at_top = 1;
   1530  1.1  christos 	while (1) {
   1531  1.1  christos 		if (*diffp == 0)
   1532  1.1  christos 			return;
   1533  1.1  christos 
   1534  1.1  christos 		if (JF(*diffp) != JF(b))
   1535  1.1  christos 			return;
   1536  1.1  christos 
   1537  1.1  christos 		if (!SET_MEMBER((*diffp)->dom, b->id))
   1538  1.1  christos 			return;
   1539  1.1  christos 
   1540  1.1  christos 		if ((*diffp)->val[A_ATOM] != val)
   1541  1.1  christos 			break;
   1542  1.1  christos 
   1543  1.1  christos 		diffp = &JT(*diffp);
   1544  1.1  christos 		at_top = 0;
   1545  1.1  christos 	}
   1546  1.1  christos 	samep = &JT(*diffp);
   1547  1.1  christos 	while (1) {
   1548  1.1  christos 		if (*samep == 0)
   1549  1.1  christos 			return;
   1550  1.1  christos 
   1551  1.1  christos 		if (JF(*samep) != JF(b))
   1552  1.1  christos 			return;
   1553  1.1  christos 
   1554  1.1  christos 		if (!SET_MEMBER((*samep)->dom, b->id))
   1555  1.1  christos 			return;
   1556  1.1  christos 
   1557  1.1  christos 		if ((*samep)->val[A_ATOM] == val)
   1558  1.1  christos 			break;
   1559  1.1  christos 
   1560  1.1  christos 		/* XXX Need to check that there are no data dependencies
   1561  1.1  christos 		   between diffp and samep.  Currently, the code generator
   1562  1.1  christos 		   will not produce such dependencies. */
   1563  1.1  christos 		samep = &JT(*samep);
   1564  1.1  christos 	}
   1565  1.1  christos #ifdef notdef
   1566  1.1  christos 	/* XXX This doesn't cover everything. */
   1567  1.1  christos 	for (i = 0; i < N_ATOMS; ++i)
   1568  1.1  christos 		if ((*samep)->val[i] != pred->val[i])
   1569  1.1  christos 			return;
   1570  1.1  christos #endif
   1571  1.1  christos 	/* Pull up the node. */
   1572  1.1  christos 	pull = *samep;
   1573  1.1  christos 	*samep = JT(pull);
   1574  1.1  christos 	JT(pull) = *diffp;
   1575  1.1  christos 
   1576  1.1  christos 	/*
   1577  1.1  christos 	 * At the top of the chain, each predecessor needs to point at the
   1578  1.1  christos 	 * pulled up node.  Inside the chain, there is only one predecessor
   1579  1.1  christos 	 * to worry about.
   1580  1.1  christos 	 */
   1581  1.1  christos 	if (at_top) {
   1582  1.1  christos 		for (ep = b->in_edges; ep != 0; ep = ep->next) {
   1583  1.1  christos 			if (JT(ep->pred) == b)
   1584  1.1  christos 				JT(ep->pred) = pull;
   1585  1.1  christos 			else
   1586  1.1  christos 				JF(ep->pred) = pull;
   1587  1.1  christos 		}
   1588  1.1  christos 	}
   1589  1.1  christos 	else
   1590  1.1  christos 		*diffp = pull;
   1591  1.1  christos 
   1592  1.9  christos 	opt_state->done = 0;
   1593  1.1  christos }
   1594  1.1  christos 
   1595  1.1  christos static void
   1596  1.9  christos opt_blks(compiler_state_t *cstate, opt_state_t *opt_state, struct icode *ic,
   1597  1.9  christos     int do_stmts)
   1598  1.1  christos {
   1599  1.1  christos 	int i, maxlevel;
   1600  1.1  christos 	struct block *p;
   1601  1.1  christos 
   1602  1.9  christos 	init_val(opt_state);
   1603  1.9  christos 	maxlevel = ic->root->level;
   1604  1.1  christos 
   1605  1.9  christos 	find_inedges(opt_state, ic->root);
   1606  1.1  christos 	for (i = maxlevel; i >= 0; --i)
   1607  1.9  christos 		for (p = opt_state->levels[i]; p; p = p->link)
   1608  1.9  christos 			opt_blk(cstate, ic, opt_state, p, do_stmts);
   1609  1.1  christos 
   1610  1.1  christos 	if (do_stmts)
   1611  1.1  christos 		/*
   1612  1.1  christos 		 * No point trying to move branches; it can't possibly
   1613  1.1  christos 		 * make a difference at this point.
   1614  1.1  christos 		 */
   1615  1.1  christos 		return;
   1616  1.1  christos 
   1617  1.1  christos 	for (i = 1; i <= maxlevel; ++i) {
   1618  1.9  christos 		for (p = opt_state->levels[i]; p; p = p->link) {
   1619  1.9  christos 			opt_j(opt_state, &p->et);
   1620  1.9  christos 			opt_j(opt_state, &p->ef);
   1621  1.1  christos 		}
   1622  1.1  christos 	}
   1623  1.1  christos 
   1624  1.9  christos 	find_inedges(opt_state, ic->root);
   1625  1.1  christos 	for (i = 1; i <= maxlevel; ++i) {
   1626  1.9  christos 		for (p = opt_state->levels[i]; p; p = p->link) {
   1627  1.9  christos 			or_pullup(opt_state, p);
   1628  1.9  christos 			and_pullup(opt_state, p);
   1629  1.1  christos 		}
   1630  1.1  christos 	}
   1631  1.1  christos }
   1632  1.1  christos 
   1633  1.1  christos static inline void
   1634  1.6  christos link_inedge(struct edge *parent, struct block *child)
   1635  1.1  christos {
   1636  1.1  christos 	parent->next = child->in_edges;
   1637  1.1  christos 	child->in_edges = parent;
   1638  1.1  christos }
   1639  1.1  christos 
   1640  1.1  christos static void
   1641  1.9  christos find_inedges(opt_state_t *opt_state, struct block *root)
   1642  1.1  christos {
   1643  1.1  christos 	int i;
   1644  1.1  christos 	struct block *b;
   1645  1.1  christos 
   1646  1.9  christos 	for (i = 0; i < opt_state->n_blocks; ++i)
   1647  1.9  christos 		opt_state->blocks[i]->in_edges = 0;
   1648  1.1  christos 
   1649  1.1  christos 	/*
   1650  1.1  christos 	 * Traverse the graph, adding each edge to the predecessor
   1651  1.1  christos 	 * list of its successors.  Skip the leaves (i.e. level 0).
   1652  1.1  christos 	 */
   1653  1.1  christos 	for (i = root->level; i > 0; --i) {
   1654  1.9  christos 		for (b = opt_state->levels[i]; b != 0; b = b->link) {
   1655  1.1  christos 			link_inedge(&b->et, JT(b));
   1656  1.1  christos 			link_inedge(&b->ef, JF(b));
   1657  1.1  christos 		}
   1658  1.1  christos 	}
   1659  1.1  christos }
   1660  1.1  christos 
   1661  1.1  christos static void
   1662  1.6  christos opt_root(struct block **b)
   1663  1.1  christos {
   1664  1.1  christos 	struct slist *tmp, *s;
   1665  1.1  christos 
   1666  1.1  christos 	s = (*b)->stmts;
   1667  1.1  christos 	(*b)->stmts = 0;
   1668  1.1  christos 	while (BPF_CLASS((*b)->s.code) == BPF_JMP && JT(*b) == JF(*b))
   1669  1.1  christos 		*b = JT(*b);
   1670  1.1  christos 
   1671  1.1  christos 	tmp = (*b)->stmts;
   1672  1.1  christos 	if (tmp != 0)
   1673  1.1  christos 		sappend(s, tmp);
   1674  1.1  christos 	(*b)->stmts = s;
   1675  1.1  christos 
   1676  1.1  christos 	/*
   1677  1.1  christos 	 * If the root node is a return, then there is no
   1678  1.1  christos 	 * point executing any statements (since the bpf machine
   1679  1.1  christos 	 * has no side effects).
   1680  1.1  christos 	 */
   1681  1.1  christos 	if (BPF_CLASS((*b)->s.code) == BPF_RET)
   1682  1.1  christos 		(*b)->stmts = 0;
   1683  1.1  christos }
   1684  1.1  christos 
   1685  1.1  christos static void
   1686  1.9  christos opt_loop(compiler_state_t *cstate, opt_state_t *opt_state, struct icode *ic,
   1687  1.9  christos     int do_stmts)
   1688  1.1  christos {
   1689  1.1  christos 
   1690  1.1  christos #ifdef BDEBUG
   1691  1.9  christos 	if (pcap_optimizer_debug > 1) {
   1692  1.1  christos 		printf("opt_loop(root, %d) begin\n", do_stmts);
   1693  1.9  christos 		opt_dump(cstate, ic);
   1694  1.1  christos 	}
   1695  1.1  christos #endif
   1696  1.1  christos 	do {
   1697  1.9  christos 		opt_state->done = 1;
   1698  1.9  christos 		find_levels(opt_state, ic);
   1699  1.9  christos 		find_dom(opt_state, ic->root);
   1700  1.9  christos 		find_closure(opt_state, ic->root);
   1701  1.9  christos 		find_ud(opt_state, ic->root);
   1702  1.9  christos 		find_edom(opt_state, ic->root);
   1703  1.9  christos 		opt_blks(cstate, opt_state, ic, do_stmts);
   1704  1.1  christos #ifdef BDEBUG
   1705  1.9  christos 		if (pcap_optimizer_debug > 1) {
   1706  1.9  christos 			printf("opt_loop(root, %d) bottom, done=%d\n", do_stmts, opt_state->done);
   1707  1.9  christos 			opt_dump(cstate, ic);
   1708  1.1  christos 		}
   1709  1.1  christos #endif
   1710  1.9  christos 	} while (!opt_state->done);
   1711  1.1  christos }
   1712  1.1  christos 
   1713  1.1  christos /*
   1714  1.1  christos  * Optimize the filter code in its dag representation.
   1715  1.1  christos  */
   1716  1.1  christos void
   1717  1.9  christos bpf_optimize(compiler_state_t *cstate, struct icode *ic)
   1718  1.1  christos {
   1719  1.9  christos 	opt_state_t opt_state;
   1720  1.1  christos 
   1721  1.9  christos 	opt_init(cstate, &opt_state, ic);
   1722  1.9  christos 	opt_loop(cstate, &opt_state, ic, 0);
   1723  1.9  christos 	opt_loop(cstate, &opt_state, ic, 1);
   1724  1.9  christos 	intern_blocks(&opt_state, ic);
   1725  1.1  christos #ifdef BDEBUG
   1726  1.9  christos 	if (pcap_optimizer_debug > 1) {
   1727  1.1  christos 		printf("after intern_blocks()\n");
   1728  1.9  christos 		opt_dump(cstate, ic);
   1729  1.1  christos 	}
   1730  1.1  christos #endif
   1731  1.9  christos 	opt_root(&ic->root);
   1732  1.1  christos #ifdef BDEBUG
   1733  1.9  christos 	if (pcap_optimizer_debug > 1) {
   1734  1.1  christos 		printf("after opt_root()\n");
   1735  1.9  christos 		opt_dump(cstate, ic);
   1736  1.1  christos 	}
   1737  1.1  christos #endif
   1738  1.9  christos 	opt_cleanup(&opt_state);
   1739  1.1  christos }
   1740  1.1  christos 
   1741  1.1  christos static void
   1742  1.9  christos make_marks(struct icode *ic, struct block *p)
   1743  1.1  christos {
   1744  1.9  christos 	if (!isMarked(ic, p)) {
   1745  1.9  christos 		Mark(ic, p);
   1746  1.1  christos 		if (BPF_CLASS(p->s.code) != BPF_RET) {
   1747  1.9  christos 			make_marks(ic, JT(p));
   1748  1.9  christos 			make_marks(ic, JF(p));
   1749  1.1  christos 		}
   1750  1.1  christos 	}
   1751  1.1  christos }
   1752  1.1  christos 
   1753  1.1  christos /*
   1754  1.9  christos  * Mark code array such that isMarked(ic->cur_mark, i) is true
   1755  1.1  christos  * only for nodes that are alive.
   1756  1.1  christos  */
   1757  1.1  christos static void
   1758  1.9  christos mark_code(struct icode *ic)
   1759  1.1  christos {
   1760  1.9  christos 	ic->cur_mark += 1;
   1761  1.9  christos 	make_marks(ic, ic->root);
   1762  1.1  christos }
   1763  1.1  christos 
   1764  1.1  christos /*
   1765  1.1  christos  * True iff the two stmt lists load the same value from the packet into
   1766  1.1  christos  * the accumulator.
   1767  1.1  christos  */
   1768  1.1  christos static int
   1769  1.6  christos eq_slist(struct slist *x, struct slist *y)
   1770  1.1  christos {
   1771  1.1  christos 	while (1) {
   1772  1.1  christos 		while (x && x->s.code == NOP)
   1773  1.1  christos 			x = x->next;
   1774  1.1  christos 		while (y && y->s.code == NOP)
   1775  1.1  christos 			y = y->next;
   1776  1.1  christos 		if (x == 0)
   1777  1.1  christos 			return y == 0;
   1778  1.1  christos 		if (y == 0)
   1779  1.1  christos 			return x == 0;
   1780  1.1  christos 		if (x->s.code != y->s.code || x->s.k != y->s.k)
   1781  1.1  christos 			return 0;
   1782  1.1  christos 		x = x->next;
   1783  1.1  christos 		y = y->next;
   1784  1.1  christos 	}
   1785  1.1  christos }
   1786  1.1  christos 
   1787  1.1  christos static inline int
   1788  1.6  christos eq_blk(struct block *b0, struct block *b1)
   1789  1.1  christos {
   1790  1.1  christos 	if (b0->s.code == b1->s.code &&
   1791  1.1  christos 	    b0->s.k == b1->s.k &&
   1792  1.1  christos 	    b0->et.succ == b1->et.succ &&
   1793  1.1  christos 	    b0->ef.succ == b1->ef.succ)
   1794  1.1  christos 		return eq_slist(b0->stmts, b1->stmts);
   1795  1.1  christos 	return 0;
   1796  1.1  christos }
   1797  1.1  christos 
   1798  1.1  christos static void
   1799  1.9  christos intern_blocks(opt_state_t *opt_state, struct icode *ic)
   1800  1.1  christos {
   1801  1.1  christos 	struct block *p;
   1802  1.1  christos 	int i, j;
   1803  1.1  christos 	int done1; /* don't shadow global */
   1804  1.1  christos  top:
   1805  1.1  christos 	done1 = 1;
   1806  1.9  christos 	for (i = 0; i < opt_state->n_blocks; ++i)
   1807  1.9  christos 		opt_state->blocks[i]->link = 0;
   1808  1.1  christos 
   1809  1.9  christos 	mark_code(ic);
   1810  1.1  christos 
   1811  1.9  christos 	for (i = opt_state->n_blocks - 1; --i >= 0; ) {
   1812  1.9  christos 		if (!isMarked(ic, opt_state->blocks[i]))
   1813  1.1  christos 			continue;
   1814  1.9  christos 		for (j = i + 1; j < opt_state->n_blocks; ++j) {
   1815  1.9  christos 			if (!isMarked(ic, opt_state->blocks[j]))
   1816  1.1  christos 				continue;
   1817  1.9  christos 			if (eq_blk(opt_state->blocks[i], opt_state->blocks[j])) {
   1818  1.9  christos 				opt_state->blocks[i]->link = opt_state->blocks[j]->link ?
   1819  1.9  christos 					opt_state->blocks[j]->link : opt_state->blocks[j];
   1820  1.1  christos 				break;
   1821  1.1  christos 			}
   1822  1.1  christos 		}
   1823  1.1  christos 	}
   1824  1.9  christos 	for (i = 0; i < opt_state->n_blocks; ++i) {
   1825  1.9  christos 		p = opt_state->blocks[i];
   1826  1.1  christos 		if (JT(p) == 0)
   1827  1.1  christos 			continue;
   1828  1.1  christos 		if (JT(p)->link) {
   1829  1.1  christos 			done1 = 0;
   1830  1.1  christos 			JT(p) = JT(p)->link;
   1831  1.1  christos 		}
   1832  1.1  christos 		if (JF(p)->link) {
   1833  1.1  christos 			done1 = 0;
   1834  1.1  christos 			JF(p) = JF(p)->link;
   1835  1.1  christos 		}
   1836  1.1  christos 	}
   1837  1.1  christos 	if (!done1)
   1838  1.1  christos 		goto top;
   1839  1.1  christos }
   1840  1.1  christos 
   1841  1.1  christos static void
   1842  1.9  christos opt_cleanup(opt_state_t *opt_state)
   1843  1.1  christos {
   1844  1.9  christos 	free((void *)opt_state->vnode_base);
   1845  1.9  christos 	free((void *)opt_state->vmap);
   1846  1.9  christos 	free((void *)opt_state->edges);
   1847  1.9  christos 	free((void *)opt_state->space);
   1848  1.9  christos 	free((void *)opt_state->levels);
   1849  1.9  christos 	free((void *)opt_state->blocks);
   1850  1.1  christos }
   1851  1.1  christos 
   1852  1.1  christos /*
   1853  1.1  christos  * Return the number of stmts in 's'.
   1854  1.1  christos  */
   1855  1.5  christos static u_int
   1856  1.6  christos slength(struct slist *s)
   1857  1.1  christos {
   1858  1.5  christos 	u_int n = 0;
   1859  1.1  christos 
   1860  1.1  christos 	for (; s; s = s->next)
   1861  1.1  christos 		if (s->s.code != NOP)
   1862  1.1  christos 			++n;
   1863  1.1  christos 	return n;
   1864  1.1  christos }
   1865  1.1  christos 
   1866  1.1  christos /*
   1867  1.1  christos  * Return the number of nodes reachable by 'p'.
   1868  1.1  christos  * All nodes should be initially unmarked.
   1869  1.1  christos  */
   1870  1.1  christos static int
   1871  1.9  christos count_blocks(struct icode *ic, struct block *p)
   1872  1.1  christos {
   1873  1.9  christos 	if (p == 0 || isMarked(ic, p))
   1874  1.1  christos 		return 0;
   1875  1.9  christos 	Mark(ic, p);
   1876  1.9  christos 	return count_blocks(ic, JT(p)) + count_blocks(ic, JF(p)) + 1;
   1877  1.1  christos }
   1878  1.1  christos 
   1879  1.1  christos /*
   1880  1.1  christos  * Do a depth first search on the flow graph, numbering the
   1881  1.1  christos  * the basic blocks, and entering them into the 'blocks' array.`
   1882  1.1  christos  */
   1883  1.1  christos static void
   1884  1.9  christos number_blks_r(opt_state_t *opt_state, struct icode *ic, struct block *p)
   1885  1.1  christos {
   1886  1.1  christos 	int n;
   1887  1.1  christos 
   1888  1.9  christos 	if (p == 0 || isMarked(ic, p))
   1889  1.1  christos 		return;
   1890  1.1  christos 
   1891  1.9  christos 	Mark(ic, p);
   1892  1.9  christos 	n = opt_state->n_blocks++;
   1893  1.1  christos 	p->id = n;
   1894  1.9  christos 	opt_state->blocks[n] = p;
   1895  1.1  christos 
   1896  1.9  christos 	number_blks_r(opt_state, ic, JT(p));
   1897  1.9  christos 	number_blks_r(opt_state, ic, JF(p));
   1898  1.1  christos }
   1899  1.1  christos 
   1900  1.1  christos /*
   1901  1.1  christos  * Return the number of stmts in the flowgraph reachable by 'p'.
   1902  1.1  christos  * The nodes should be unmarked before calling.
   1903  1.1  christos  *
   1904  1.1  christos  * Note that "stmts" means "instructions", and that this includes
   1905  1.1  christos  *
   1906  1.1  christos  *	side-effect statements in 'p' (slength(p->stmts));
   1907  1.1  christos  *
   1908  1.1  christos  *	statements in the true branch from 'p' (count_stmts(JT(p)));
   1909  1.1  christos  *
   1910  1.1  christos  *	statements in the false branch from 'p' (count_stmts(JF(p)));
   1911  1.1  christos  *
   1912  1.1  christos  *	the conditional jump itself (1);
   1913  1.1  christos  *
   1914  1.1  christos  *	an extra long jump if the true branch requires it (p->longjt);
   1915  1.1  christos  *
   1916  1.1  christos  *	an extra long jump if the false branch requires it (p->longjf).
   1917  1.1  christos  */
   1918  1.5  christos static u_int
   1919  1.9  christos count_stmts(struct icode *ic, struct block *p)
   1920  1.1  christos {
   1921  1.5  christos 	u_int n;
   1922  1.1  christos 
   1923  1.9  christos 	if (p == 0 || isMarked(ic, p))
   1924  1.1  christos 		return 0;
   1925  1.9  christos 	Mark(ic, p);
   1926  1.9  christos 	n = count_stmts(ic, JT(p)) + count_stmts(ic, JF(p));
   1927  1.1  christos 	return slength(p->stmts) + n + 1 + p->longjt + p->longjf;
   1928  1.1  christos }
   1929  1.1  christos 
   1930  1.1  christos /*
   1931  1.1  christos  * Allocate memory.  All allocation is done before optimization
   1932  1.1  christos  * is begun.  A linear bound on the size of all data structures is computed
   1933  1.1  christos  * from the total number of blocks and/or statements.
   1934  1.1  christos  */
   1935  1.1  christos static void
   1936  1.9  christos opt_init(compiler_state_t *cstate, opt_state_t *opt_state, struct icode *ic)
   1937  1.1  christos {
   1938  1.1  christos 	bpf_u_int32 *p;
   1939  1.1  christos 	int i, n, max_stmts;
   1940  1.1  christos 
   1941  1.1  christos 	/*
   1942  1.1  christos 	 * First, count the blocks, so we can malloc an array to map
   1943  1.1  christos 	 * block number to block.  Then, put the blocks into the array.
   1944  1.1  christos 	 */
   1945  1.9  christos 	unMarkAll(ic);
   1946  1.9  christos 	n = count_blocks(ic, ic->root);
   1947  1.9  christos 	opt_state->blocks = (struct block **)calloc(n, sizeof(*opt_state->blocks));
   1948  1.9  christos 	if (opt_state->blocks == NULL)
   1949  1.9  christos 		bpf_error(cstate, "malloc");
   1950  1.9  christos 	unMarkAll(ic);
   1951  1.9  christos 	opt_state->n_blocks = 0;
   1952  1.9  christos 	number_blks_r(opt_state, ic, ic->root);
   1953  1.9  christos 
   1954  1.9  christos 	opt_state->n_edges = 2 * opt_state->n_blocks;
   1955  1.9  christos 	opt_state->edges = (struct edge **)calloc(opt_state->n_edges, sizeof(*opt_state->edges));
   1956  1.9  christos 	if (opt_state->edges == NULL)
   1957  1.9  christos 		bpf_error(cstate, "malloc");
   1958  1.1  christos 
   1959  1.1  christos 	/*
   1960  1.1  christos 	 * The number of levels is bounded by the number of nodes.
   1961  1.1  christos 	 */
   1962  1.9  christos 	opt_state->levels = (struct block **)calloc(opt_state->n_blocks, sizeof(*opt_state->levels));
   1963  1.9  christos 	if (opt_state->levels == NULL)
   1964  1.9  christos 		bpf_error(cstate, "malloc");
   1965  1.1  christos 
   1966  1.9  christos 	opt_state->edgewords = opt_state->n_edges / (8 * sizeof(bpf_u_int32)) + 1;
   1967  1.9  christos 	opt_state->nodewords = opt_state->n_blocks / (8 * sizeof(bpf_u_int32)) + 1;
   1968  1.1  christos 
   1969  1.1  christos 	/* XXX */
   1970  1.9  christos 	opt_state->space = (bpf_u_int32 *)malloc(2 * opt_state->n_blocks * opt_state->nodewords * sizeof(*opt_state->space)
   1971  1.9  christos 				 + opt_state->n_edges * opt_state->edgewords * sizeof(*opt_state->space));
   1972  1.9  christos 	if (opt_state->space == NULL)
   1973  1.9  christos 		bpf_error(cstate, "malloc");
   1974  1.9  christos 	p = opt_state->space;
   1975  1.9  christos 	opt_state->all_dom_sets = p;
   1976  1.1  christos 	for (i = 0; i < n; ++i) {
   1977  1.9  christos 		opt_state->blocks[i]->dom = p;
   1978  1.9  christos 		p += opt_state->nodewords;
   1979  1.1  christos 	}
   1980  1.9  christos 	opt_state->all_closure_sets = p;
   1981  1.1  christos 	for (i = 0; i < n; ++i) {
   1982  1.9  christos 		opt_state->blocks[i]->closure = p;
   1983  1.9  christos 		p += opt_state->nodewords;
   1984  1.1  christos 	}
   1985  1.9  christos 	opt_state->all_edge_sets = p;
   1986  1.1  christos 	for (i = 0; i < n; ++i) {
   1987  1.9  christos 		register struct block *b = opt_state->blocks[i];
   1988  1.1  christos 
   1989  1.1  christos 		b->et.edom = p;
   1990  1.9  christos 		p += opt_state->edgewords;
   1991  1.1  christos 		b->ef.edom = p;
   1992  1.9  christos 		p += opt_state->edgewords;
   1993  1.1  christos 		b->et.id = i;
   1994  1.9  christos 		opt_state->edges[i] = &b->et;
   1995  1.9  christos 		b->ef.id = opt_state->n_blocks + i;
   1996  1.9  christos 		opt_state->edges[opt_state->n_blocks + i] = &b->ef;
   1997  1.1  christos 		b->et.pred = b;
   1998  1.1  christos 		b->ef.pred = b;
   1999  1.1  christos 	}
   2000  1.1  christos 	max_stmts = 0;
   2001  1.1  christos 	for (i = 0; i < n; ++i)
   2002  1.9  christos 		max_stmts += slength(opt_state->blocks[i]->stmts) + 1;
   2003  1.1  christos 	/*
   2004  1.1  christos 	 * We allocate at most 3 value numbers per statement,
   2005  1.1  christos 	 * so this is an upper bound on the number of valnodes
   2006  1.1  christos 	 * we'll need.
   2007  1.1  christos 	 */
   2008  1.9  christos 	opt_state->maxval = 3 * max_stmts;
   2009  1.9  christos 	opt_state->vmap = (struct vmapinfo *)calloc(opt_state->maxval, sizeof(*opt_state->vmap));
   2010  1.9  christos 	opt_state->vnode_base = (struct valnode *)calloc(opt_state->maxval, sizeof(*opt_state->vnode_base));
   2011  1.9  christos 	if (opt_state->vmap == NULL || opt_state->vnode_base == NULL)
   2012  1.9  christos 		bpf_error(cstate, "malloc");
   2013  1.1  christos }
   2014  1.1  christos 
   2015  1.1  christos /*
   2016  1.9  christos  * This is only used when supporting optimizer debugging.  It is
   2017  1.9  christos  * global state, so do *not* do more than one compile in parallel
   2018  1.9  christos  * and expect it to provide meaningful information.
   2019  1.1  christos  */
   2020  1.1  christos #ifdef BDEBUG
   2021  1.1  christos int bids[1000];
   2022  1.1  christos #endif
   2023  1.1  christos 
   2024  1.1  christos /*
   2025  1.1  christos  * Returns true if successful.  Returns false if a branch has
   2026  1.1  christos  * an offset that is too large.  If so, we have marked that
   2027  1.1  christos  * branch so that on a subsequent iteration, it will be treated
   2028  1.1  christos  * properly.
   2029  1.1  christos  */
   2030  1.1  christos static int
   2031  1.9  christos convert_code_r(compiler_state_t *cstate, conv_state_t *conv_state,
   2032  1.9  christos     struct icode *ic, struct block *p)
   2033  1.1  christos {
   2034  1.1  christos 	struct bpf_insn *dst;
   2035  1.1  christos 	struct slist *src;
   2036  1.2  christos 	u_int slen;
   2037  1.1  christos 	u_int off;
   2038  1.1  christos 	int extrajmps;		/* number of extra jumps inserted */
   2039  1.1  christos 	struct slist **offset = NULL;
   2040  1.1  christos 
   2041  1.9  christos 	if (p == 0 || isMarked(ic, p))
   2042  1.1  christos 		return (1);
   2043  1.9  christos 	Mark(ic, p);
   2044  1.1  christos 
   2045  1.9  christos 	if (convert_code_r(cstate, conv_state, ic, JF(p)) == 0)
   2046  1.1  christos 		return (0);
   2047  1.9  christos 	if (convert_code_r(cstate, conv_state, ic, JT(p)) == 0)
   2048  1.1  christos 		return (0);
   2049  1.1  christos 
   2050  1.1  christos 	slen = slength(p->stmts);
   2051  1.9  christos 	dst = conv_state->ftail -= (slen + 1 + p->longjt + p->longjf);
   2052  1.1  christos 		/* inflate length by any extra jumps */
   2053  1.1  christos 
   2054  1.9  christos 	p->offset = (int)(dst - conv_state->fstart);
   2055  1.1  christos 
   2056  1.1  christos 	/* generate offset[] for convenience  */
   2057  1.1  christos 	if (slen) {
   2058  1.1  christos 		offset = (struct slist **)calloc(slen, sizeof(struct slist *));
   2059  1.1  christos 		if (!offset) {
   2060  1.9  christos 			bpf_error(cstate, "not enough core");
   2061  1.1  christos 			/*NOTREACHED*/
   2062  1.1  christos 		}
   2063  1.1  christos 	}
   2064  1.1  christos 	src = p->stmts;
   2065  1.1  christos 	for (off = 0; off < slen && src; off++) {
   2066  1.1  christos #if 0
   2067  1.1  christos 		printf("off=%d src=%x\n", off, src);
   2068  1.1  christos #endif
   2069  1.1  christos 		offset[off] = src;
   2070  1.1  christos 		src = src->next;
   2071  1.1  christos 	}
   2072  1.1  christos 
   2073  1.1  christos 	off = 0;
   2074  1.1  christos 	for (src = p->stmts; src; src = src->next) {
   2075  1.1  christos 		if (src->s.code == NOP)
   2076  1.1  christos 			continue;
   2077  1.1  christos 		dst->code = (u_short)src->s.code;
   2078  1.1  christos 		dst->k = src->s.k;
   2079  1.1  christos 
   2080  1.1  christos 		/* fill block-local relative jump */
   2081  1.1  christos 		if (BPF_CLASS(src->s.code) != BPF_JMP || src->s.code == (BPF_JMP|BPF_JA)) {
   2082  1.1  christos #if 0
   2083  1.1  christos 			if (src->s.jt || src->s.jf) {
   2084  1.9  christos 				bpf_error(cstate, "illegal jmp destination");
   2085  1.1  christos 				/*NOTREACHED*/
   2086  1.1  christos 			}
   2087  1.1  christos #endif
   2088  1.1  christos 			goto filled;
   2089  1.1  christos 		}
   2090  1.1  christos 		if (off == slen - 2)	/*???*/
   2091  1.1  christos 			goto filled;
   2092  1.1  christos 
   2093  1.1  christos 	    {
   2094  1.2  christos 		u_int i;
   2095  1.1  christos 		int jt, jf;
   2096  1.4  christos static const char ljerr[] = "%s for block-local relative jump: off=%d";
   2097  1.1  christos 
   2098  1.1  christos #if 0
   2099  1.1  christos 		printf("code=%x off=%d %x %x\n", src->s.code,
   2100  1.1  christos 			off, src->s.jt, src->s.jf);
   2101  1.1  christos #endif
   2102  1.1  christos 
   2103  1.1  christos 		if (!src->s.jt || !src->s.jf) {
   2104  1.9  christos 			bpf_error(cstate, ljerr, "no jmp destination", off);
   2105  1.1  christos 			/*NOTREACHED*/
   2106  1.1  christos 		}
   2107  1.1  christos 
   2108  1.1  christos 		jt = jf = 0;
   2109  1.1  christos 		for (i = 0; i < slen; i++) {
   2110  1.1  christos 			if (offset[i] == src->s.jt) {
   2111  1.1  christos 				if (jt) {
   2112  1.9  christos 					bpf_error(cstate, ljerr, "multiple matches", off);
   2113  1.1  christos 					/*NOTREACHED*/
   2114  1.1  christos 				}
   2115  1.1  christos 
   2116  1.1  christos 				dst->jt = i - off - 1;
   2117  1.1  christos 				jt++;
   2118  1.1  christos 			}
   2119  1.1  christos 			if (offset[i] == src->s.jf) {
   2120  1.1  christos 				if (jf) {
   2121  1.9  christos 					bpf_error(cstate, ljerr, "multiple matches", off);
   2122  1.1  christos 					/*NOTREACHED*/
   2123  1.1  christos 				}
   2124  1.1  christos 				dst->jf = i - off - 1;
   2125  1.1  christos 				jf++;
   2126  1.1  christos 			}
   2127  1.1  christos 		}
   2128  1.1  christos 		if (!jt || !jf) {
   2129  1.9  christos 			bpf_error(cstate, ljerr, "no destination found", off);
   2130  1.1  christos 			/*NOTREACHED*/
   2131  1.1  christos 		}
   2132  1.1  christos 	    }
   2133  1.1  christos filled:
   2134  1.1  christos 		++dst;
   2135  1.1  christos 		++off;
   2136  1.1  christos 	}
   2137  1.1  christos 	if (offset)
   2138  1.1  christos 		free(offset);
   2139  1.1  christos 
   2140  1.1  christos #ifdef BDEBUG
   2141  1.9  christos 	bids[dst - conv_state->fstart] = p->id + 1;
   2142  1.1  christos #endif
   2143  1.1  christos 	dst->code = (u_short)p->s.code;
   2144  1.1  christos 	dst->k = p->s.k;
   2145  1.1  christos 	if (JT(p)) {
   2146  1.1  christos 		extrajmps = 0;
   2147  1.1  christos 		off = JT(p)->offset - (p->offset + slen) - 1;
   2148  1.1  christos 		if (off >= 256) {
   2149  1.1  christos 		    /* offset too large for branch, must add a jump */
   2150  1.1  christos 		    if (p->longjt == 0) {
   2151  1.1  christos 		    	/* mark this instruction and retry */
   2152  1.1  christos 			p->longjt++;
   2153  1.1  christos 			return(0);
   2154  1.1  christos 		    }
   2155  1.1  christos 		    /* branch if T to following jump */
   2156  1.1  christos 		    dst->jt = extrajmps;
   2157  1.1  christos 		    extrajmps++;
   2158  1.1  christos 		    dst[extrajmps].code = BPF_JMP|BPF_JA;
   2159  1.1  christos 		    dst[extrajmps].k = off - extrajmps;
   2160  1.1  christos 		}
   2161  1.1  christos 		else
   2162  1.1  christos 		    dst->jt = off;
   2163  1.1  christos 		off = JF(p)->offset - (p->offset + slen) - 1;
   2164  1.1  christos 		if (off >= 256) {
   2165  1.1  christos 		    /* offset too large for branch, must add a jump */
   2166  1.1  christos 		    if (p->longjf == 0) {
   2167  1.1  christos 		    	/* mark this instruction and retry */
   2168  1.1  christos 			p->longjf++;
   2169  1.1  christos 			return(0);
   2170  1.1  christos 		    }
   2171  1.1  christos 		    /* branch if F to following jump */
   2172  1.1  christos 		    /* if two jumps are inserted, F goes to second one */
   2173  1.1  christos 		    dst->jf = extrajmps;
   2174  1.1  christos 		    extrajmps++;
   2175  1.1  christos 		    dst[extrajmps].code = BPF_JMP|BPF_JA;
   2176  1.1  christos 		    dst[extrajmps].k = off - extrajmps;
   2177  1.1  christos 		}
   2178  1.1  christos 		else
   2179  1.1  christos 		    dst->jf = off;
   2180  1.1  christos 	}
   2181  1.1  christos 	return (1);
   2182  1.1  christos }
   2183  1.1  christos 
   2184  1.1  christos 
   2185  1.1  christos /*
   2186  1.1  christos  * Convert flowgraph intermediate representation to the
   2187  1.1  christos  * BPF array representation.  Set *lenp to the number of instructions.
   2188  1.1  christos  *
   2189  1.1  christos  * This routine does *NOT* leak the memory pointed to by fp.  It *must
   2190  1.1  christos  * not* do free(fp) before returning fp; doing so would make no sense,
   2191  1.1  christos  * as the BPF array pointed to by the return value of icode_to_fcode()
   2192  1.1  christos  * must be valid - it's being returned for use in a bpf_program structure.
   2193  1.1  christos  *
   2194  1.1  christos  * If it appears that icode_to_fcode() is leaking, the problem is that
   2195  1.1  christos  * the program using pcap_compile() is failing to free the memory in
   2196  1.1  christos  * the BPF program when it's done - the leak is in the program, not in
   2197  1.1  christos  * the routine that happens to be allocating the memory.  (By analogy, if
   2198  1.1  christos  * a program calls fopen() without ever calling fclose() on the FILE *,
   2199  1.1  christos  * it will leak the FILE structure; the leak is not in fopen(), it's in
   2200  1.1  christos  * the program.)  Change the program to use pcap_freecode() when it's
   2201  1.1  christos  * done with the filter program.  See the pcap man page.
   2202  1.1  christos  */
   2203  1.1  christos struct bpf_insn *
   2204  1.9  christos icode_to_fcode(compiler_state_t *cstate, struct icode *ic,
   2205  1.9  christos     struct block *root, u_int *lenp)
   2206  1.1  christos {
   2207  1.5  christos 	u_int n;
   2208  1.1  christos 	struct bpf_insn *fp;
   2209  1.9  christos 	conv_state_t conv_state;
   2210  1.1  christos 
   2211  1.1  christos 	/*
   2212  1.1  christos 	 * Loop doing convert_code_r() until no branches remain
   2213  1.1  christos 	 * with too-large offsets.
   2214  1.1  christos 	 */
   2215  1.1  christos 	while (1) {
   2216  1.9  christos 	    unMarkAll(ic);
   2217  1.9  christos 	    n = *lenp = count_stmts(ic, root);
   2218  1.1  christos 
   2219  1.1  christos 	    fp = (struct bpf_insn *)malloc(sizeof(*fp) * n);
   2220  1.1  christos 	    if (fp == NULL)
   2221  1.9  christos 		    bpf_error(cstate, "malloc");
   2222  1.1  christos 	    memset((char *)fp, 0, sizeof(*fp) * n);
   2223  1.9  christos 	    conv_state.fstart = fp;
   2224  1.9  christos 	    conv_state.ftail = fp + n;
   2225  1.1  christos 
   2226  1.9  christos 	    unMarkAll(ic);
   2227  1.9  christos 	    if (convert_code_r(cstate, &conv_state, ic, root))
   2228  1.1  christos 		break;
   2229  1.1  christos 	    free(fp);
   2230  1.1  christos 	}
   2231  1.1  christos 
   2232  1.1  christos 	return fp;
   2233  1.1  christos }
   2234  1.1  christos 
   2235  1.1  christos /*
   2236  1.1  christos  * Make a copy of a BPF program and put it in the "fcode" member of
   2237  1.1  christos  * a "pcap_t".
   2238  1.1  christos  *
   2239  1.1  christos  * If we fail to allocate memory for the copy, fill in the "errbuf"
   2240  1.1  christos  * member of the "pcap_t" with an error message, and return -1;
   2241  1.1  christos  * otherwise, return 0.
   2242  1.1  christos  */
   2243  1.1  christos int
   2244  1.1  christos install_bpf_program(pcap_t *p, struct bpf_program *fp)
   2245  1.1  christos {
   2246  1.1  christos 	size_t prog_size;
   2247  1.1  christos 
   2248  1.1  christos 	/*
   2249  1.1  christos 	 * Validate the program.
   2250  1.1  christos 	 */
   2251  1.1  christos 	if (!bpf_validate(fp->bf_insns, fp->bf_len)) {
   2252  1.9  christos 		pcap_snprintf(p->errbuf, sizeof(p->errbuf),
   2253  1.1  christos 			"BPF program is not valid");
   2254  1.1  christos 		return (-1);
   2255  1.1  christos 	}
   2256  1.1  christos 
   2257  1.1  christos 	/*
   2258  1.1  christos 	 * Free up any already installed program.
   2259  1.1  christos 	 */
   2260  1.1  christos 	pcap_freecode(&p->fcode);
   2261  1.1  christos 
   2262  1.1  christos 	prog_size = sizeof(*fp->bf_insns) * fp->bf_len;
   2263  1.1  christos 	p->fcode.bf_len = fp->bf_len;
   2264  1.1  christos 	p->fcode.bf_insns = (struct bpf_insn *)malloc(prog_size);
   2265  1.1  christos 	if (p->fcode.bf_insns == NULL) {
   2266  1.9  christos 		pcap_snprintf(p->errbuf, sizeof(p->errbuf),
   2267  1.1  christos 			 "malloc: %s", pcap_strerror(errno));
   2268  1.1  christos 		return (-1);
   2269  1.1  christos 	}
   2270  1.1  christos 	memcpy(p->fcode.bf_insns, fp->bf_insns, prog_size);
   2271  1.1  christos 	return (0);
   2272  1.1  christos }
   2273  1.1  christos 
   2274  1.1  christos #ifdef BDEBUG
   2275  1.1  christos static void
   2276  1.9  christos dot_dump_node(struct icode *ic, struct block *block, struct bpf_program *prog,
   2277  1.9  christos     FILE *out)
   2278  1.8  christos {
   2279  1.8  christos 	int icount, noffset;
   2280  1.8  christos 	int i;
   2281  1.8  christos 
   2282  1.9  christos 	if (block == NULL || isMarked(ic, block))
   2283  1.8  christos 		return;
   2284  1.9  christos 	Mark(ic, block);
   2285  1.8  christos 
   2286  1.8  christos 	icount = slength(block->stmts) + 1 + block->longjt + block->longjf;
   2287  1.8  christos 	noffset = min(block->offset + icount, (int)prog->bf_len);
   2288  1.8  christos 
   2289  1.8  christos 	fprintf(out, "\tblock%d [shape=ellipse, id=\"block-%d\" label=\"BLOCK%d\\n", block->id, block->id, block->id);
   2290  1.8  christos 	for (i = block->offset; i < noffset; i++) {
   2291  1.8  christos 		fprintf(out, "\\n%s", bpf_image(prog->bf_insns + i, i));
   2292  1.8  christos 	}
   2293  1.8  christos 	fprintf(out, "\" tooltip=\"");
   2294  1.8  christos 	for (i = 0; i < BPF_MEMWORDS; i++)
   2295  1.8  christos 		if (block->val[i] != 0)
   2296  1.8  christos 			fprintf(out, "val[%d]=%d ", i, block->val[i]);
   2297  1.8  christos 	fprintf(out, "val[A]=%d ", block->val[A_ATOM]);
   2298  1.8  christos 	fprintf(out, "val[X]=%d", block->val[X_ATOM]);
   2299  1.8  christos 	fprintf(out, "\"");
   2300  1.8  christos 	if (JT(block) == NULL)
   2301  1.8  christos 		fprintf(out, ", peripheries=2");
   2302  1.8  christos 	fprintf(out, "];\n");
   2303  1.8  christos 
   2304  1.9  christos 	dot_dump_node(ic, JT(block), prog, out);
   2305  1.9  christos 	dot_dump_node(ic, JF(block), prog, out);
   2306  1.8  christos }
   2307  1.9  christos 
   2308  1.8  christos static void
   2309  1.9  christos dot_dump_edge(struct icode *ic, struct block *block, FILE *out)
   2310  1.8  christos {
   2311  1.9  christos 	if (block == NULL || isMarked(ic, block))
   2312  1.8  christos 		return;
   2313  1.9  christos 	Mark(ic, block);
   2314  1.8  christos 
   2315  1.8  christos 	if (JT(block)) {
   2316  1.8  christos 		fprintf(out, "\t\"block%d\":se -> \"block%d\":n [label=\"T\"]; \n",
   2317  1.8  christos 				block->id, JT(block)->id);
   2318  1.8  christos 		fprintf(out, "\t\"block%d\":sw -> \"block%d\":n [label=\"F\"]; \n",
   2319  1.8  christos 			   block->id, JF(block)->id);
   2320  1.8  christos 	}
   2321  1.9  christos 	dot_dump_edge(ic, JT(block), out);
   2322  1.9  christos 	dot_dump_edge(ic, JF(block), out);
   2323  1.8  christos }
   2324  1.9  christos 
   2325  1.8  christos /* Output the block CFG using graphviz/DOT language
   2326  1.8  christos  * In the CFG, block's code, value index for each registers at EXIT,
   2327  1.8  christos  * and the jump relationship is show.
   2328  1.8  christos  *
   2329  1.8  christos  * example DOT for BPF `ip src host 1.1.1.1' is:
   2330  1.8  christos     digraph BPF {
   2331  1.8  christos     	block0 [shape=ellipse, id="block-0" label="BLOCK0\n\n(000) ldh      [12]\n(001) jeq      #0x800           jt 2	jf 5" tooltip="val[A]=0 val[X]=0"];
   2332  1.8  christos     	block1 [shape=ellipse, id="block-1" label="BLOCK1\n\n(002) ld       [26]\n(003) jeq      #0x1010101       jt 4	jf 5" tooltip="val[A]=0 val[X]=0"];
   2333  1.8  christos     	block2 [shape=ellipse, id="block-2" label="BLOCK2\n\n(004) ret      #68" tooltip="val[A]=0 val[X]=0", peripheries=2];
   2334  1.8  christos     	block3 [shape=ellipse, id="block-3" label="BLOCK3\n\n(005) ret      #0" tooltip="val[A]=0 val[X]=0", peripheries=2];
   2335  1.8  christos     	"block0":se -> "block1":n [label="T"];
   2336  1.8  christos     	"block0":sw -> "block3":n [label="F"];
   2337  1.8  christos     	"block1":se -> "block2":n [label="T"];
   2338  1.8  christos     	"block1":sw -> "block3":n [label="F"];
   2339  1.8  christos     }
   2340  1.8  christos  *
   2341  1.8  christos  *  After install graphviz on http://www.graphviz.org/, save it as bpf.dot
   2342  1.8  christos  *  and run `dot -Tpng -O bpf.dot' to draw the graph.
   2343  1.8  christos  */
   2344  1.8  christos static void
   2345  1.9  christos dot_dump(compiler_state_t *cstate, struct icode *ic)
   2346  1.8  christos {
   2347  1.8  christos 	struct bpf_program f;
   2348  1.8  christos 	FILE *out = stdout;
   2349  1.8  christos 
   2350  1.8  christos 	memset(bids, 0, sizeof bids);
   2351  1.9  christos 	f.bf_insns = icode_to_fcode(cstate, ic, ic->root, &f.bf_len);
   2352  1.8  christos 
   2353  1.8  christos 	fprintf(out, "digraph BPF {\n");
   2354  1.9  christos 	ic->cur_mark = 0;
   2355  1.9  christos 	unMarkAll(ic);
   2356  1.9  christos 	dot_dump_node(ic, ic->root, &f, out);
   2357  1.9  christos 	ic->cur_mark = 0;
   2358  1.9  christos 	unMarkAll(ic);
   2359  1.9  christos 	dot_dump_edge(ic, ic->root, out);
   2360  1.8  christos 	fprintf(out, "}\n");
   2361  1.8  christos 
   2362  1.8  christos 	free((char *)f.bf_insns);
   2363  1.8  christos }
   2364  1.8  christos 
   2365  1.8  christos static void
   2366  1.9  christos plain_dump(compiler_state_t *cstate, struct icode *ic)
   2367  1.1  christos {
   2368  1.1  christos 	struct bpf_program f;
   2369  1.1  christos 
   2370  1.1  christos 	memset(bids, 0, sizeof bids);
   2371  1.9  christos 	f.bf_insns = icode_to_fcode(cstate, ic, ic->root, &f.bf_len);
   2372  1.1  christos 	bpf_dump(&f, 1);
   2373  1.1  christos 	putchar('\n');
   2374  1.1  christos 	free((char *)f.bf_insns);
   2375  1.1  christos }
   2376  1.9  christos 
   2377  1.8  christos static void
   2378  1.9  christos opt_dump(compiler_state_t *cstate, struct icode *ic)
   2379  1.8  christos {
   2380  1.8  christos 	/* if optimizer debugging is enabled, output DOT graph
   2381  1.9  christos 	 * `pcap_optimizer_debug=4' is equivalent to -dddd to follow -d/-dd/-ddd
   2382  1.9  christos 	 * convention in tcpdump command line
   2383  1.8  christos 	 */
   2384  1.9  christos 	if (pcap_optimizer_debug > 3)
   2385  1.9  christos 		dot_dump(cstate, ic);
   2386  1.8  christos 	else
   2387  1.9  christos 		plain_dump(cstate, ic);
   2388  1.8  christos }
   2389  1.1  christos #endif
   2390