]>
The Tcpdump Group git mirrors - libpcap/blob - optimize.c
2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Optimization module for tcpdump intermediate representation.
29 #include <pcap-stdinc.h>
36 #ifdef HAVE_SYS_BITYPES_H
37 #include <sys/bitypes.h>
39 #include <sys/types.h>
53 #ifdef HAVE_OS_PROTO_H
61 #if defined(MSDOS) && !defined(__DJGPP__)
62 extern int _w32_ffs (int mask
);
66 #if defined(WIN32) && defined (_MSC_VER)
71 * Represents a deleted instruction.
76 * Register numbers for use-def values.
77 * 0 through BPF_MEMWORDS-1 represent the corresponding scratch memory
78 * location. A_ATOM is the accumulator and X_ATOM is the index
81 #define A_ATOM BPF_MEMWORDS
82 #define X_ATOM (BPF_MEMWORDS+1)
85 * This define is used to represent *both* the accumulator and
86 * x register in use-def computations.
87 * Currently, the use-def code assumes only one definition per instruction.
89 #define AX_ATOM N_ATOMS
92 * A flag to indicate that further optimization is needed.
93 * Iterative passes are continued until a given pass yields no
99 * A block is marked if only if its mark equals the current mark.
100 * Rather than traverse the code array, marking each item, 'cur_mark' is
101 * incremented. This automatically makes each element unmarked.
104 #define isMarked(p) ((p)->mark == cur_mark)
105 #define unMarkAll() cur_mark += 1
106 #define Mark(p) ((p)->mark = cur_mark)
108 static void opt_init(struct block
*);
109 static void opt_cleanup(void);
111 static void intern_blocks(struct block
*);
113 static void find_inedges(struct block
*);
115 static void opt_dump(struct block
*);
119 struct block
**blocks
;
124 * A bit vector set representation of the dominators.
125 * We round up the set size to the next power of two.
127 static int nodewords
;
128 static int edgewords
;
129 struct block
**levels
;
131 #define BITS_PER_WORD (8*sizeof(bpf_u_int32))
133 * True if a is in uset {p}
135 #define SET_MEMBER(p, a) \
136 ((p)[(unsigned)(a) / BITS_PER_WORD] & (1 << ((unsigned)(a) % BITS_PER_WORD)))
141 #define SET_INSERT(p, a) \
142 (p)[(unsigned)(a) / BITS_PER_WORD] |= (1 << ((unsigned)(a) % BITS_PER_WORD))
145 * Delete 'a' from uset p.
147 #define SET_DELETE(p, a) \
148 (p)[(unsigned)(a) / BITS_PER_WORD] &= ~(1 << ((unsigned)(a) % BITS_PER_WORD))
153 #define SET_INTERSECT(a, b, n)\
155 register bpf_u_int32 *_x = a, *_y = b;\
156 register int _n = n;\
157 while (--_n >= 0) *_x++ &= *_y++;\
163 #define SET_SUBTRACT(a, b, n)\
165 register bpf_u_int32 *_x = a, *_y = b;\
166 register int _n = n;\
167 while (--_n >= 0) *_x++ &=~ *_y++;\
173 #define SET_UNION(a, b, n)\
175 register bpf_u_int32 *_x = a, *_y = b;\
176 register int _n = n;\
177 while (--_n >= 0) *_x++ |= *_y++;\
180 static uset all_dom_sets
;
181 static uset all_closure_sets
;
182 static uset all_edge_sets
;
185 #define MAX(a,b) ((a)>(b)?(a):(b))
189 find_levels_r(struct block
*b
)
200 find_levels_r(JT(b
));
201 find_levels_r(JF(b
));
202 level
= MAX(JT(b
)->level
, JF(b
)->level
) + 1;
206 b
->link
= levels
[level
];
211 * Level graph. The levels go from 0 at the leaves to
212 * N_LEVELS at the root. The levels[] array points to the
213 * first node of the level list, whose elements are linked
214 * with the 'link' field of the struct block.
217 find_levels(struct block
*root
)
219 memset((char *)levels
, 0, n_blocks
* sizeof(*levels
));
225 * Find dominator relationships.
226 * Assumes graph has been leveled.
229 find_dom(struct block
*root
)
236 * Initialize sets to contain all nodes.
239 i
= n_blocks
* nodewords
;
242 /* Root starts off empty. */
243 for (i
= nodewords
; --i
>= 0;)
246 /* root->level is the highest level no found. */
247 for (i
= root
->level
; i
>= 0; --i
) {
248 for (b
= levels
[i
]; b
; b
= b
->link
) {
249 SET_INSERT(b
->dom
, b
->id
);
252 SET_INTERSECT(JT(b
)->dom
, b
->dom
, nodewords
);
253 SET_INTERSECT(JF(b
)->dom
, b
->dom
, nodewords
);
259 propedom(struct edge
*ep
)
261 SET_INSERT(ep
->edom
, ep
->id
);
263 SET_INTERSECT(ep
->succ
->et
.edom
, ep
->edom
, edgewords
);
264 SET_INTERSECT(ep
->succ
->ef
.edom
, ep
->edom
, edgewords
);
269 * Compute edge dominators.
270 * Assumes graph has been leveled and predecessors established.
273 find_edom(struct block
*root
)
280 for (i
= n_edges
* edgewords
; --i
>= 0; )
283 /* root->level is the highest level no found. */
284 memset(root
->et
.edom
, 0, edgewords
* sizeof(*(uset
)0));
285 memset(root
->ef
.edom
, 0, edgewords
* sizeof(*(uset
)0));
286 for (i
= root
->level
; i
>= 0; --i
) {
287 for (b
= levels
[i
]; b
!= 0; b
= b
->link
) {
295 * Find the backwards transitive closure of the flow graph. These sets
296 * are backwards in the sense that we find the set of nodes that reach
297 * a given node, not the set of nodes that can be reached by a node.
299 * Assumes graph has been leveled.
302 find_closure(struct block
*root
)
308 * Initialize sets to contain no nodes.
310 memset((char *)all_closure_sets
, 0,
311 n_blocks
* nodewords
* sizeof(*all_closure_sets
));
313 /* root->level is the highest level no found. */
314 for (i
= root
->level
; i
>= 0; --i
) {
315 for (b
= levels
[i
]; b
; b
= b
->link
) {
316 SET_INSERT(b
->closure
, b
->id
);
319 SET_UNION(JT(b
)->closure
, b
->closure
, nodewords
);
320 SET_UNION(JF(b
)->closure
, b
->closure
, nodewords
);
326 * Return the register number that is used by s. If A and X are both
327 * used, return AX_ATOM. If no register is used, return -1.
329 * The implementation should probably change to an array access.
332 atomuse(struct stmt
*s
)
334 register int c
= s
->code
;
339 switch (BPF_CLASS(c
)) {
342 return (BPF_RVAL(c
) == BPF_A
) ? A_ATOM
:
343 (BPF_RVAL(c
) == BPF_X
) ? X_ATOM
: -1;
347 return (BPF_MODE(c
) == BPF_IND
) ? X_ATOM
:
348 (BPF_MODE(c
) == BPF_MEM
) ? s
->k
: -1;
358 if (BPF_SRC(c
) == BPF_X
)
363 return BPF_MISCOP(c
) == BPF_TXA
? X_ATOM
: A_ATOM
;
370 * Return the register number that is defined by 's'. We assume that
371 * a single stmt cannot define more than one register. If no register
372 * is defined, return -1.
374 * The implementation should probably change to an array access.
377 atomdef(struct stmt
*s
)
382 switch (BPF_CLASS(s
->code
)) {
396 return BPF_MISCOP(s
->code
) == BPF_TAX
? X_ATOM
: A_ATOM
;
402 * Compute the sets of registers used, defined, and killed by 'b'.
404 * "Used" means that a statement in 'b' uses the register before any
405 * statement in 'b' defines it, i.e. it uses the value left in
406 * that register by a predecessor block of this block.
407 * "Defined" means that a statement in 'b' defines it.
408 * "Killed" means that a statement in 'b' defines it before any
409 * statement in 'b' uses it, i.e. it kills the value left in that
410 * register by a predecessor block of this block.
413 compute_local_ud(struct block
*b
)
416 atomset def
= 0, use
= 0, kill
= 0;
419 for (s
= b
->stmts
; s
; s
= s
->next
) {
420 if (s
->s
.code
== NOP
)
422 atom
= atomuse(&s
->s
);
424 if (atom
== AX_ATOM
) {
425 if (!ATOMELEM(def
, X_ATOM
))
426 use
|= ATOMMASK(X_ATOM
);
427 if (!ATOMELEM(def
, A_ATOM
))
428 use
|= ATOMMASK(A_ATOM
);
430 else if (atom
< N_ATOMS
) {
431 if (!ATOMELEM(def
, atom
))
432 use
|= ATOMMASK(atom
);
437 atom
= atomdef(&s
->s
);
439 if (!ATOMELEM(use
, atom
))
440 kill
|= ATOMMASK(atom
);
441 def
|= ATOMMASK(atom
);
444 if (BPF_CLASS(b
->s
.code
) == BPF_JMP
) {
446 * XXX - what about RET?
448 atom
= atomuse(&b
->s
);
450 if (atom
== AX_ATOM
) {
451 if (!ATOMELEM(def
, X_ATOM
))
452 use
|= ATOMMASK(X_ATOM
);
453 if (!ATOMELEM(def
, A_ATOM
))
454 use
|= ATOMMASK(A_ATOM
);
456 else if (atom
< N_ATOMS
) {
457 if (!ATOMELEM(def
, atom
))
458 use
|= ATOMMASK(atom
);
471 * Assume graph is already leveled.
474 find_ud(struct block
*root
)
480 * root->level is the highest level no found;
481 * count down from there.
483 maxlevel
= root
->level
;
484 for (i
= maxlevel
; i
>= 0; --i
)
485 for (p
= levels
[i
]; p
; p
= p
->link
) {
490 for (i
= 1; i
<= maxlevel
; ++i
) {
491 for (p
= levels
[i
]; p
; p
= p
->link
) {
492 p
->out_use
|= JT(p
)->in_use
| JF(p
)->in_use
;
493 p
->in_use
|= p
->out_use
&~ p
->kill
;
499 * These data structures are used in a Cocke and Shwarz style
500 * value numbering scheme. Since the flowgraph is acyclic,
501 * exit values can be propagated from a node's predecessors
502 * provided it is uniquely defined.
508 struct valnode
*next
;
512 static struct valnode
*hashtbl
[MODULUS
];
516 /* Integer constants mapped with the load immediate opcode. */
517 #define K(i) F(BPF_LD|BPF_IMM|BPF_W, i, 0L)
524 struct vmapinfo
*vmap
;
525 struct valnode
*vnode_base
;
526 struct valnode
*next_vnode
;
532 next_vnode
= vnode_base
;
533 memset((char *)vmap
, 0, maxval
* sizeof(*vmap
));
534 memset((char *)hashtbl
, 0, sizeof hashtbl
);
537 /* Because we really don't have an IR, this stuff is a little messy. */
539 F(int code
, int v0
, int v1
)
545 hash
= (u_int
)code
^ (v0
<< 4) ^ (v1
<< 8);
548 for (p
= hashtbl
[hash
]; p
; p
= p
->next
)
549 if (p
->code
== code
&& p
->v0
== v0
&& p
->v1
== v1
)
553 if (BPF_MODE(code
) == BPF_IMM
&&
554 (BPF_CLASS(code
) == BPF_LD
|| BPF_CLASS(code
) == BPF_LDX
)) {
555 vmap
[val
].const_val
= v0
;
556 vmap
[val
].is_const
= 1;
563 p
->next
= hashtbl
[hash
];
570 vstore(struct stmt
*s
, int *valp
, int newval
, int alter
)
572 if (alter
&& *valp
== newval
)
579 * Do constant-folding on binary operators.
580 * (Unary operators are handled elsewhere.)
583 fold_op(struct stmt
*s
, int v0
, int v1
)
587 a
= vmap
[v0
].const_val
;
588 b
= vmap
[v1
].const_val
;
590 switch (BPF_OP(s
->code
)) {
605 bpf_error("division by zero");
629 s
->code
= BPF_LD
|BPF_IMM
;
633 static inline struct slist
*
634 this_op(struct slist
*s
)
636 while (s
!= 0 && s
->s
.code
== NOP
)
642 opt_not(struct block
*b
)
644 struct block
*tmp
= JT(b
);
651 opt_peep(struct block
*b
)
654 struct slist
*next
, *last
;
662 for (/*empty*/; /*empty*/; s
= next
) {
668 break; /* nothing left in the block */
671 * Find the next real instruction after that one
674 next
= this_op(s
->next
);
676 break; /* no next instruction */
680 * st M[k] --> st M[k]
683 if (s
->s
.code
== BPF_ST
&&
684 next
->s
.code
== (BPF_LDX
|BPF_MEM
) &&
685 s
->s
.k
== next
->s
.k
) {
687 next
->s
.code
= BPF_MISC
|BPF_TAX
;
693 if (s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
694 next
->s
.code
== (BPF_MISC
|BPF_TAX
)) {
695 s
->s
.code
= BPF_LDX
|BPF_IMM
;
696 next
->s
.code
= BPF_MISC
|BPF_TXA
;
700 * This is an ugly special case, but it happens
701 * when you say tcp[k] or udp[k] where k is a constant.
703 if (s
->s
.code
== (BPF_LD
|BPF_IMM
)) {
704 struct slist
*add
, *tax
, *ild
;
707 * Check that X isn't used on exit from this
708 * block (which the optimizer might cause).
709 * We know the code generator won't generate
710 * any local dependencies.
712 if (ATOMELEM(b
->out_use
, X_ATOM
))
716 * Check that the instruction following the ldi
717 * is an addx, or it's an ldxms with an addx
718 * following it (with 0 or more nops between the
721 if (next
->s
.code
!= (BPF_LDX
|BPF_MSH
|BPF_B
))
724 add
= this_op(next
->next
);
725 if (add
== 0 || add
->s
.code
!= (BPF_ALU
|BPF_ADD
|BPF_X
))
729 * Check that a tax follows that (with 0 or more
730 * nops between them).
732 tax
= this_op(add
->next
);
733 if (tax
== 0 || tax
->s
.code
!= (BPF_MISC
|BPF_TAX
))
737 * Check that an ild follows that (with 0 or more
738 * nops between them).
740 ild
= this_op(tax
->next
);
741 if (ild
== 0 || BPF_CLASS(ild
->s
.code
) != BPF_LD
||
742 BPF_MODE(ild
->s
.code
) != BPF_IND
)
745 * We want to turn this sequence:
748 * (005) ldxms [14] {next} -- optional
751 * (008) ild [x+0] {ild}
753 * into this sequence:
761 * XXX We need to check that X is not
762 * subsequently used, because we want to change
763 * what'll be in it after this sequence.
765 * We know we can eliminate the accumulator
766 * modifications earlier in the sequence since
767 * it is defined by the last stmt of this sequence
768 * (i.e., the last statement of the sequence loads
769 * a value into the accumulator, so we can eliminate
770 * earlier operations on the accumulator).
780 * If the comparison at the end of a block is an equality
781 * comparison against a constant, and nobody uses the value
782 * we leave in the A register at the end of a block, and
783 * the operation preceding the comparison is an arithmetic
784 * operation, we can sometime optimize it away.
786 if (b
->s
.code
== (BPF_JMP
|BPF_JEQ
|BPF_K
) &&
787 !ATOMELEM(b
->out_use
, A_ATOM
)) {
789 * We can optimize away certain subtractions of the
792 if (last
->s
.code
== (BPF_ALU
|BPF_SUB
|BPF_X
)) {
793 val
= b
->val
[X_ATOM
];
794 if (vmap
[val
].is_const
) {
796 * If we have a subtract to do a comparison,
797 * and the X register is a known constant,
798 * we can merge this value into the
804 b
->s
.k
+= vmap
[val
].const_val
;
807 } else if (b
->s
.k
== 0) {
809 * If the X register isn't a constant,
810 * and the comparison in the test is
811 * against 0, we can compare with the
812 * X register, instead:
818 b
->s
.code
= BPF_JMP
|BPF_JEQ
|BPF_X
;
823 * Likewise, a constant subtract can be simplified:
826 * jeq #y -> jeq #(x+y)
828 else if (last
->s
.code
== (BPF_ALU
|BPF_SUB
|BPF_K
)) {
834 * And, similarly, a constant AND can be simplified
835 * if we're testing against 0, i.e.:
840 else if (last
->s
.code
== (BPF_ALU
|BPF_AND
|BPF_K
) &&
843 b
->s
.code
= BPF_JMP
|BPF_K
|BPF_JSET
;
851 * jset #ffffffff -> always
853 if (b
->s
.code
== (BPF_JMP
|BPF_K
|BPF_JSET
)) {
856 if (b
->s
.k
== 0xffffffff)
860 * If we're comparing against the index register, and the index
861 * register is a known constant, we can just compare against that
864 val
= b
->val
[X_ATOM
];
865 if (vmap
[val
].is_const
&& BPF_SRC(b
->s
.code
) == BPF_X
) {
866 bpf_int32 v
= vmap
[val
].const_val
;
871 * If the accumulator is a known constant, we can compute the
874 val
= b
->val
[A_ATOM
];
875 if (vmap
[val
].is_const
&& BPF_SRC(b
->s
.code
) == BPF_K
) {
876 bpf_int32 v
= vmap
[val
].const_val
;
877 switch (BPF_OP(b
->s
.code
)) {
884 v
= (unsigned)v
> b
->s
.k
;
888 v
= (unsigned)v
>= b
->s
.k
;
908 * Compute the symbolic value of expression of 's', and update
909 * anything it defines in the value table 'val'. If 'alter' is true,
910 * do various optimizations. This code would be cleaner if symbolic
911 * evaluation and code transformations weren't folded together.
914 opt_stmt(struct stmt
*s
, int val
[], int alter
)
921 case BPF_LD
|BPF_ABS
|BPF_W
:
922 case BPF_LD
|BPF_ABS
|BPF_H
:
923 case BPF_LD
|BPF_ABS
|BPF_B
:
924 v
= F(s
->code
, s
->k
, 0L);
925 vstore(s
, &val
[A_ATOM
], v
, alter
);
928 case BPF_LD
|BPF_IND
|BPF_W
:
929 case BPF_LD
|BPF_IND
|BPF_H
:
930 case BPF_LD
|BPF_IND
|BPF_B
:
932 if (alter
&& vmap
[v
].is_const
) {
933 s
->code
= BPF_LD
|BPF_ABS
|BPF_SIZE(s
->code
);
934 s
->k
+= vmap
[v
].const_val
;
935 v
= F(s
->code
, s
->k
, 0L);
939 v
= F(s
->code
, s
->k
, v
);
940 vstore(s
, &val
[A_ATOM
], v
, alter
);
944 v
= F(s
->code
, 0L, 0L);
945 vstore(s
, &val
[A_ATOM
], v
, alter
);
950 vstore(s
, &val
[A_ATOM
], v
, alter
);
953 case BPF_LDX
|BPF_IMM
:
955 vstore(s
, &val
[X_ATOM
], v
, alter
);
958 case BPF_LDX
|BPF_MSH
|BPF_B
:
959 v
= F(s
->code
, s
->k
, 0L);
960 vstore(s
, &val
[X_ATOM
], v
, alter
);
963 case BPF_ALU
|BPF_NEG
:
964 if (alter
&& vmap
[val
[A_ATOM
]].is_const
) {
965 s
->code
= BPF_LD
|BPF_IMM
;
966 s
->k
= -vmap
[val
[A_ATOM
]].const_val
;
967 val
[A_ATOM
] = K(s
->k
);
970 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], 0L);
973 case BPF_ALU
|BPF_ADD
|BPF_K
:
974 case BPF_ALU
|BPF_SUB
|BPF_K
:
975 case BPF_ALU
|BPF_MUL
|BPF_K
:
976 case BPF_ALU
|BPF_DIV
|BPF_K
:
977 case BPF_ALU
|BPF_AND
|BPF_K
:
978 case BPF_ALU
|BPF_OR
|BPF_K
:
979 case BPF_ALU
|BPF_LSH
|BPF_K
:
980 case BPF_ALU
|BPF_RSH
|BPF_K
:
981 op
= BPF_OP(s
->code
);
984 /* don't optimize away "sub #0"
985 * as it may be needed later to
986 * fixup the generated math code */
988 op
== BPF_LSH
|| op
== BPF_RSH
||
993 if (op
== BPF_MUL
|| op
== BPF_AND
) {
994 s
->code
= BPF_LD
|BPF_IMM
;
995 val
[A_ATOM
] = K(s
->k
);
999 if (vmap
[val
[A_ATOM
]].is_const
) {
1000 fold_op(s
, val
[A_ATOM
], K(s
->k
));
1001 val
[A_ATOM
] = K(s
->k
);
1005 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], K(s
->k
));
1008 case BPF_ALU
|BPF_ADD
|BPF_X
:
1009 case BPF_ALU
|BPF_SUB
|BPF_X
:
1010 case BPF_ALU
|BPF_MUL
|BPF_X
:
1011 case BPF_ALU
|BPF_DIV
|BPF_X
:
1012 case BPF_ALU
|BPF_AND
|BPF_X
:
1013 case BPF_ALU
|BPF_OR
|BPF_X
:
1014 case BPF_ALU
|BPF_LSH
|BPF_X
:
1015 case BPF_ALU
|BPF_RSH
|BPF_X
:
1016 op
= BPF_OP(s
->code
);
1017 if (alter
&& vmap
[val
[X_ATOM
]].is_const
) {
1018 if (vmap
[val
[A_ATOM
]].is_const
) {
1019 fold_op(s
, val
[A_ATOM
], val
[X_ATOM
]);
1020 val
[A_ATOM
] = K(s
->k
);
1023 s
->code
= BPF_ALU
|BPF_K
|op
;
1024 s
->k
= vmap
[val
[X_ATOM
]].const_val
;
1027 F(s
->code
, val
[A_ATOM
], K(s
->k
));
1032 * Check if we're doing something to an accumulator
1033 * that is 0, and simplify. This may not seem like
1034 * much of a simplification but it could open up further
1036 * XXX We could also check for mul by 1, etc.
1038 if (alter
&& vmap
[val
[A_ATOM
]].is_const
1039 && vmap
[val
[A_ATOM
]].const_val
== 0) {
1040 if (op
== BPF_ADD
|| op
== BPF_OR
) {
1041 s
->code
= BPF_MISC
|BPF_TXA
;
1042 vstore(s
, &val
[A_ATOM
], val
[X_ATOM
], alter
);
1045 else if (op
== BPF_MUL
|| op
== BPF_DIV
||
1046 op
== BPF_AND
|| op
== BPF_LSH
|| op
== BPF_RSH
) {
1047 s
->code
= BPF_LD
|BPF_IMM
;
1049 vstore(s
, &val
[A_ATOM
], K(s
->k
), alter
);
1052 else if (op
== BPF_NEG
) {
1057 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], val
[X_ATOM
]);
1060 case BPF_MISC
|BPF_TXA
:
1061 vstore(s
, &val
[A_ATOM
], val
[X_ATOM
], alter
);
1064 case BPF_LD
|BPF_MEM
:
1066 if (alter
&& vmap
[v
].is_const
) {
1067 s
->code
= BPF_LD
|BPF_IMM
;
1068 s
->k
= vmap
[v
].const_val
;
1071 vstore(s
, &val
[A_ATOM
], v
, alter
);
1074 case BPF_MISC
|BPF_TAX
:
1075 vstore(s
, &val
[X_ATOM
], val
[A_ATOM
], alter
);
1078 case BPF_LDX
|BPF_MEM
:
1080 if (alter
&& vmap
[v
].is_const
) {
1081 s
->code
= BPF_LDX
|BPF_IMM
;
1082 s
->k
= vmap
[v
].const_val
;
1085 vstore(s
, &val
[X_ATOM
], v
, alter
);
1089 vstore(s
, &val
[s
->k
], val
[A_ATOM
], alter
);
1093 vstore(s
, &val
[s
->k
], val
[X_ATOM
], alter
);
1099 deadstmt(register struct stmt
*s
, register struct stmt
*last
[])
1105 if (atom
== AX_ATOM
) {
1116 last
[atom
]->code
= NOP
;
1123 opt_deadstores(register struct block
*b
)
1125 register struct slist
*s
;
1127 struct stmt
*last
[N_ATOMS
];
1129 memset((char *)last
, 0, sizeof last
);
1131 for (s
= b
->stmts
; s
!= 0; s
= s
->next
)
1132 deadstmt(&s
->s
, last
);
1133 deadstmt(&b
->s
, last
);
1135 for (atom
= 0; atom
< N_ATOMS
; ++atom
)
1136 if (last
[atom
] && !ATOMELEM(b
->out_use
, atom
)) {
1137 last
[atom
]->code
= NOP
;
1143 opt_blk(struct block
*b
, int do_stmts
)
1148 bpf_int32 aval
, xval
;
1151 for (s
= b
->stmts
; s
&& s
->next
; s
= s
->next
)
1152 if (BPF_CLASS(s
->s
.code
) == BPF_JMP
) {
1159 * Initialize the atom values.
1164 * We have no predecessors, so everything is undefined
1165 * upon entry to this block.
1167 memset((char *)b
->val
, 0, sizeof(b
->val
));
1170 * Inherit values from our predecessors.
1172 * First, get the values from the predecessor along the
1173 * first edge leading to this node.
1175 memcpy((char *)b
->val
, (char *)p
->pred
->val
, sizeof(b
->val
));
1177 * Now look at all the other nodes leading to this node.
1178 * If, for the predecessor along that edge, a register
1179 * has a different value from the one we have (i.e.,
1180 * control paths are merging, and the merging paths
1181 * assign different values to that register), give the
1182 * register the undefined value of 0.
1184 while ((p
= p
->next
) != NULL
) {
1185 for (i
= 0; i
< N_ATOMS
; ++i
)
1186 if (b
->val
[i
] != p
->pred
->val
[i
])
1190 aval
= b
->val
[A_ATOM
];
1191 xval
= b
->val
[X_ATOM
];
1192 for (s
= b
->stmts
; s
; s
= s
->next
)
1193 opt_stmt(&s
->s
, b
->val
, do_stmts
);
1196 * This is a special case: if we don't use anything from this
1197 * block, and we load the accumulator or index register with a
1198 * value that is already there, or if this block is a return,
1199 * eliminate all the statements.
1201 * XXX - what if it does a store?
1203 * XXX - why does it matter whether we use anything from this
1204 * block? If the accumulator or index register doesn't change
1205 * its value, isn't that OK even if we use that value?
1207 * XXX - if we load the accumulator with a different value,
1208 * and the block ends with a conditional branch, we obviously
1209 * can't eliminate it, as the branch depends on that value.
1210 * For the index register, the conditional branch only depends
1211 * on the index register value if the test is against the index
1212 * register value rather than a constant; if nothing uses the
1213 * value we put into the index register, and we're not testing
1214 * against the index register's value, and there aren't any
1215 * other problems that would keep us from eliminating this
1216 * block, can we eliminate it?
1219 ((b
->out_use
== 0 && aval
!= 0 && b
->val
[A_ATOM
] == aval
&&
1220 xval
!= 0 && b
->val
[X_ATOM
] == xval
) ||
1221 BPF_CLASS(b
->s
.code
) == BPF_RET
)) {
1222 if (b
->stmts
!= 0) {
1231 * Set up values for branch optimizer.
1233 if (BPF_SRC(b
->s
.code
) == BPF_K
)
1234 b
->oval
= K(b
->s
.k
);
1236 b
->oval
= b
->val
[X_ATOM
];
1237 b
->et
.code
= b
->s
.code
;
1238 b
->ef
.code
= -b
->s
.code
;
1242 * Return true if any register that is used on exit from 'succ', has
1243 * an exit value that is different from the corresponding exit value
1247 use_conflict(struct block
*b
, struct block
*succ
)
1250 atomset use
= succ
->out_use
;
1255 for (atom
= 0; atom
< N_ATOMS
; ++atom
)
1256 if (ATOMELEM(use
, atom
))
1257 if (b
->val
[atom
] != succ
->val
[atom
])
1262 static struct block
*
1263 fold_edge(struct block
*child
, struct edge
*ep
)
1266 int aval0
, aval1
, oval0
, oval1
;
1267 int code
= ep
->code
;
1275 if (child
->s
.code
!= code
)
1278 aval0
= child
->val
[A_ATOM
];
1279 oval0
= child
->oval
;
1280 aval1
= ep
->pred
->val
[A_ATOM
];
1281 oval1
= ep
->pred
->oval
;
1288 * The operands of the branch instructions are
1289 * identical, so the result is true if a true
1290 * branch was taken to get here, otherwise false.
1292 return sense
? JT(child
) : JF(child
);
1294 if (sense
&& code
== (BPF_JMP
|BPF_JEQ
|BPF_K
))
1296 * At this point, we only know the comparison if we
1297 * came down the true branch, and it was an equality
1298 * comparison with a constant.
1300 * I.e., if we came down the true branch, and the branch
1301 * was an equality comparison with a constant, we know the
1302 * accumulator contains that constant. If we came down
1303 * the false branch, or the comparison wasn't with a
1304 * constant, we don't know what was in the accumulator.
1306 * We rely on the fact that distinct constants have distinct
1315 opt_j(struct edge
*ep
)
1318 register struct block
*target
;
1320 if (JT(ep
->succ
) == 0)
1323 if (JT(ep
->succ
) == JF(ep
->succ
)) {
1325 * Common branch targets can be eliminated, provided
1326 * there is no data dependency.
1328 if (!use_conflict(ep
->pred
, ep
->succ
->et
.succ
)) {
1330 ep
->succ
= JT(ep
->succ
);
1334 * For each edge dominator that matches the successor of this
1335 * edge, promote the edge successor to the its grandchild.
1337 * XXX We violate the set abstraction here in favor a reasonably
1341 for (i
= 0; i
< edgewords
; ++i
) {
1342 register bpf_u_int32 x
= ep
->edom
[i
];
1347 k
+= i
* BITS_PER_WORD
;
1349 target
= fold_edge(ep
->succ
, edges
[k
]);
1351 * Check that there is no data dependency between
1352 * nodes that will be violated if we move the edge.
1354 if (target
!= 0 && !use_conflict(ep
->pred
, target
)) {
1357 if (JT(target
) != 0)
1359 * Start over unless we hit a leaf.
1370 or_pullup(struct block
*b
)
1374 struct block
**diffp
, **samep
;
1382 * Make sure each predecessor loads the same value.
1385 val
= ep
->pred
->val
[A_ATOM
];
1386 for (ep
= ep
->next
; ep
!= 0; ep
= ep
->next
)
1387 if (val
!= ep
->pred
->val
[A_ATOM
])
1390 if (JT(b
->in_edges
->pred
) == b
)
1391 diffp
= &JT(b
->in_edges
->pred
);
1393 diffp
= &JF(b
->in_edges
->pred
);
1400 if (JT(*diffp
) != JT(b
))
1403 if (!SET_MEMBER((*diffp
)->dom
, b
->id
))
1406 if ((*diffp
)->val
[A_ATOM
] != val
)
1409 diffp
= &JF(*diffp
);
1412 samep
= &JF(*diffp
);
1417 if (JT(*samep
) != JT(b
))
1420 if (!SET_MEMBER((*samep
)->dom
, b
->id
))
1423 if ((*samep
)->val
[A_ATOM
] == val
)
1426 /* XXX Need to check that there are no data dependencies
1427 between dp0 and dp1. Currently, the code generator
1428 will not produce such dependencies. */
1429 samep
= &JF(*samep
);
1432 /* XXX This doesn't cover everything. */
1433 for (i
= 0; i
< N_ATOMS
; ++i
)
1434 if ((*samep
)->val
[i
] != pred
->val
[i
])
1437 /* Pull up the node. */
1443 * At the top of the chain, each predecessor needs to point at the
1444 * pulled up node. Inside the chain, there is only one predecessor
1448 for (ep
= b
->in_edges
; ep
!= 0; ep
= ep
->next
) {
1449 if (JT(ep
->pred
) == b
)
1450 JT(ep
->pred
) = pull
;
1452 JF(ep
->pred
) = pull
;
1462 and_pullup(struct block
*b
)
1466 struct block
**diffp
, **samep
;
1474 * Make sure each predecessor loads the same value.
1476 val
= ep
->pred
->val
[A_ATOM
];
1477 for (ep
= ep
->next
; ep
!= 0; ep
= ep
->next
)
1478 if (val
!= ep
->pred
->val
[A_ATOM
])
1481 if (JT(b
->in_edges
->pred
) == b
)
1482 diffp
= &JT(b
->in_edges
->pred
);
1484 diffp
= &JF(b
->in_edges
->pred
);
1491 if (JF(*diffp
) != JF(b
))
1494 if (!SET_MEMBER((*diffp
)->dom
, b
->id
))
1497 if ((*diffp
)->val
[A_ATOM
] != val
)
1500 diffp
= &JT(*diffp
);
1503 samep
= &JT(*diffp
);
1508 if (JF(*samep
) != JF(b
))
1511 if (!SET_MEMBER((*samep
)->dom
, b
->id
))
1514 if ((*samep
)->val
[A_ATOM
] == val
)
1517 /* XXX Need to check that there are no data dependencies
1518 between diffp and samep. Currently, the code generator
1519 will not produce such dependencies. */
1520 samep
= &JT(*samep
);
1523 /* XXX This doesn't cover everything. */
1524 for (i
= 0; i
< N_ATOMS
; ++i
)
1525 if ((*samep
)->val
[i
] != pred
->val
[i
])
1528 /* Pull up the node. */
1534 * At the top of the chain, each predecessor needs to point at the
1535 * pulled up node. Inside the chain, there is only one predecessor
1539 for (ep
= b
->in_edges
; ep
!= 0; ep
= ep
->next
) {
1540 if (JT(ep
->pred
) == b
)
1541 JT(ep
->pred
) = pull
;
1543 JF(ep
->pred
) = pull
;
1553 opt_blks(struct block
*root
, int do_stmts
)
1559 maxlevel
= root
->level
;
1562 for (i
= maxlevel
; i
>= 0; --i
)
1563 for (p
= levels
[i
]; p
; p
= p
->link
)
1564 opt_blk(p
, do_stmts
);
1568 * No point trying to move branches; it can't possibly
1569 * make a difference at this point.
1573 for (i
= 1; i
<= maxlevel
; ++i
) {
1574 for (p
= levels
[i
]; p
; p
= p
->link
) {
1581 for (i
= 1; i
<= maxlevel
; ++i
) {
1582 for (p
= levels
[i
]; p
; p
= p
->link
) {
1590 link_inedge(struct edge
*parent
, struct block
*child
)
1592 parent
->next
= child
->in_edges
;
1593 child
->in_edges
= parent
;
1597 find_inedges(struct block
*root
)
1602 for (i
= 0; i
< n_blocks
; ++i
)
1603 blocks
[i
]->in_edges
= 0;
1606 * Traverse the graph, adding each edge to the predecessor
1607 * list of its successors. Skip the leaves (i.e. level 0).
1609 for (i
= root
->level
; i
> 0; --i
) {
1610 for (b
= levels
[i
]; b
!= 0; b
= b
->link
) {
1611 link_inedge(&b
->et
, JT(b
));
1612 link_inedge(&b
->ef
, JF(b
));
1618 opt_root(struct block
**b
)
1620 struct slist
*tmp
, *s
;
1624 while (BPF_CLASS((*b
)->s
.code
) == BPF_JMP
&& JT(*b
) == JF(*b
))
1633 * If the root node is a return, then there is no
1634 * point executing any statements (since the bpf machine
1635 * has no side effects).
1637 if (BPF_CLASS((*b
)->s
.code
) == BPF_RET
)
1642 opt_loop(struct block
*root
, int do_stmts
)
1647 printf("opt_loop(root, %d) begin\n", do_stmts
);
1658 opt_blks(root
, do_stmts
);
1661 printf("opt_loop(root, %d) bottom, done=%d\n", do_stmts
, done
);
1669 * Optimize the filter code in its dag representation.
1672 bpf_optimize(struct block
**rootp
)
1681 intern_blocks(root
);
1684 printf("after intern_blocks()\n");
1691 printf("after opt_root()\n");
1699 make_marks(struct block
*p
)
1703 if (BPF_CLASS(p
->s
.code
) != BPF_RET
) {
1711 * Mark code array such that isMarked(i) is true
1712 * only for nodes that are alive.
1715 mark_code(struct block
*p
)
1722 * True iff the two stmt lists load the same value from the packet into
1726 eq_slist(struct slist
*x
, struct slist
*y
)
1729 while (x
&& x
->s
.code
== NOP
)
1731 while (y
&& y
->s
.code
== NOP
)
1737 if (x
->s
.code
!= y
->s
.code
|| x
->s
.k
!= y
->s
.k
)
1745 eq_blk(struct block
*b0
, struct block
*b1
)
1747 if (b0
->s
.code
== b1
->s
.code
&&
1748 b0
->s
.k
== b1
->s
.k
&&
1749 b0
->et
.succ
== b1
->et
.succ
&&
1750 b0
->ef
.succ
== b1
->ef
.succ
)
1751 return eq_slist(b0
->stmts
, b1
->stmts
);
1756 intern_blocks(struct block
*root
)
1760 int done1
; /* don't shadow global */
1763 for (i
= 0; i
< n_blocks
; ++i
)
1764 blocks
[i
]->link
= 0;
1768 for (i
= n_blocks
- 1; --i
>= 0; ) {
1769 if (!isMarked(blocks
[i
]))
1771 for (j
= i
+ 1; j
< n_blocks
; ++j
) {
1772 if (!isMarked(blocks
[j
]))
1774 if (eq_blk(blocks
[i
], blocks
[j
])) {
1775 blocks
[i
]->link
= blocks
[j
]->link
?
1776 blocks
[j
]->link
: blocks
[j
];
1781 for (i
= 0; i
< n_blocks
; ++i
) {
1787 JT(p
) = JT(p
)->link
;
1791 JF(p
) = JF(p
)->link
;
1801 free((void *)vnode_base
);
1803 free((void *)edges
);
1804 free((void *)space
);
1805 free((void *)levels
);
1806 free((void *)blocks
);
1810 * Return the number of stmts in 's'.
1813 slength(struct slist
*s
)
1817 for (; s
; s
= s
->next
)
1818 if (s
->s
.code
!= NOP
)
1824 * Return the number of nodes reachable by 'p'.
1825 * All nodes should be initially unmarked.
1828 count_blocks(struct block
*p
)
1830 if (p
== 0 || isMarked(p
))
1833 return count_blocks(JT(p
)) + count_blocks(JF(p
)) + 1;
1837 * Do a depth first search on the flow graph, numbering the
1838 * the basic blocks, and entering them into the 'blocks' array.`
1841 number_blks_r(struct block
*p
)
1845 if (p
== 0 || isMarked(p
))
1853 number_blks_r(JT(p
));
1854 number_blks_r(JF(p
));
1858 * Return the number of stmts in the flowgraph reachable by 'p'.
1859 * The nodes should be unmarked before calling.
1861 * Note that "stmts" means "instructions", and that this includes
1863 * side-effect statements in 'p' (slength(p->stmts));
1865 * statements in the true branch from 'p' (count_stmts(JT(p)));
1867 * statements in the false branch from 'p' (count_stmts(JF(p)));
1869 * the conditional jump itself (1);
1871 * an extra long jump if the true branch requires it (p->longjt);
1873 * an extra long jump if the false branch requires it (p->longjf).
1876 count_stmts(struct block
*p
)
1880 if (p
== 0 || isMarked(p
))
1883 n
= count_stmts(JT(p
)) + count_stmts(JF(p
));
1884 return slength(p
->stmts
) + n
+ 1 + p
->longjt
+ p
->longjf
;
1888 * Allocate memory. All allocation is done before optimization
1889 * is begun. A linear bound on the size of all data structures is computed
1890 * from the total number of blocks and/or statements.
1893 opt_init(struct block
*root
)
1896 int i
, n
, max_stmts
;
1899 * First, count the blocks, so we can malloc an array to map
1900 * block number to block. Then, put the blocks into the array.
1903 n
= count_blocks(root
);
1904 blocks
= (struct block
**)calloc(n
, sizeof(*blocks
));
1906 bpf_error("malloc");
1909 number_blks_r(root
);
1911 n_edges
= 2 * n_blocks
;
1912 edges
= (struct edge
**)calloc(n_edges
, sizeof(*edges
));
1914 bpf_error("malloc");
1917 * The number of levels is bounded by the number of nodes.
1919 levels
= (struct block
**)calloc(n_blocks
, sizeof(*levels
));
1921 bpf_error("malloc");
1923 edgewords
= n_edges
/ (8 * sizeof(bpf_u_int32
)) + 1;
1924 nodewords
= n_blocks
/ (8 * sizeof(bpf_u_int32
)) + 1;
1927 space
= (bpf_u_int32
*)malloc(2 * n_blocks
* nodewords
* sizeof(*space
)
1928 + n_edges
* edgewords
* sizeof(*space
));
1930 bpf_error("malloc");
1933 for (i
= 0; i
< n
; ++i
) {
1937 all_closure_sets
= p
;
1938 for (i
= 0; i
< n
; ++i
) {
1939 blocks
[i
]->closure
= p
;
1943 for (i
= 0; i
< n
; ++i
) {
1944 register struct block
*b
= blocks
[i
];
1952 b
->ef
.id
= n_blocks
+ i
;
1953 edges
[n_blocks
+ i
] = &b
->ef
;
1958 for (i
= 0; i
< n
; ++i
)
1959 max_stmts
+= slength(blocks
[i
]->stmts
) + 1;
1961 * We allocate at most 3 value numbers per statement,
1962 * so this is an upper bound on the number of valnodes
1965 maxval
= 3 * max_stmts
;
1966 vmap
= (struct vmapinfo
*)calloc(maxval
, sizeof(*vmap
));
1967 vnode_base
= (struct valnode
*)calloc(maxval
, sizeof(*vnode_base
));
1968 if (vmap
== NULL
|| vnode_base
== NULL
)
1969 bpf_error("malloc");
1973 * Some pointers used to convert the basic block form of the code,
1974 * into the array form that BPF requires. 'fstart' will point to
1975 * the malloc'd array while 'ftail' is used during the recursive traversal.
1977 static struct bpf_insn
*fstart
;
1978 static struct bpf_insn
*ftail
;
1985 * Returns true if successful. Returns false if a branch has
1986 * an offset that is too large. If so, we have marked that
1987 * branch so that on a subsequent iteration, it will be treated
1991 convert_code_r(struct block
*p
)
1993 struct bpf_insn
*dst
;
1997 int extrajmps
; /* number of extra jumps inserted */
1998 struct slist
**offset
= NULL
;
2000 if (p
== 0 || isMarked(p
))
2004 if (convert_code_r(JF(p
)) == 0)
2006 if (convert_code_r(JT(p
)) == 0)
2009 slen
= slength(p
->stmts
);
2010 dst
= ftail
-= (slen
+ 1 + p
->longjt
+ p
->longjf
);
2011 /* inflate length by any extra jumps */
2013 p
->offset
= dst
- fstart
;
2015 /* generate offset[] for convenience */
2017 offset
= (struct slist
**)calloc(slen
, sizeof(struct slist
*));
2019 bpf_error("not enough core");
2024 for (off
= 0; off
< slen
&& src
; off
++) {
2026 printf("off=%d src=%x\n", off
, src
);
2033 for (src
= p
->stmts
; src
; src
= src
->next
) {
2034 if (src
->s
.code
== NOP
)
2036 dst
->code
= (u_short
)src
->s
.code
;
2039 /* fill block-local relative jump */
2040 if (BPF_CLASS(src
->s
.code
) != BPF_JMP
|| src
->s
.code
== (BPF_JMP
|BPF_JA
)) {
2042 if (src
->s
.jt
|| src
->s
.jf
) {
2043 bpf_error("illegal jmp destination");
2049 if (off
== slen
- 2) /*???*/
2055 const char *ljerr
= "%s for block-local relative jump: off=%d";
2058 printf("code=%x off=%d %x %x\n", src
->s
.code
,
2059 off
, src
->s
.jt
, src
->s
.jf
);
2062 if (!src
->s
.jt
|| !src
->s
.jf
) {
2063 bpf_error(ljerr
, "no jmp destination", off
);
2068 for (i
= 0; i
< slen
; i
++) {
2069 if (offset
[i
] == src
->s
.jt
) {
2071 bpf_error(ljerr
, "multiple matches", off
);
2075 dst
->jt
= i
- off
- 1;
2078 if (offset
[i
] == src
->s
.jf
) {
2080 bpf_error(ljerr
, "multiple matches", off
);
2083 dst
->jf
= i
- off
- 1;
2088 bpf_error(ljerr
, "no destination found", off
);
2100 bids
[dst
- fstart
] = p
->id
+ 1;
2102 dst
->code
= (u_short
)p
->s
.code
;
2106 off
= JT(p
)->offset
- (p
->offset
+ slen
) - 1;
2108 /* offset too large for branch, must add a jump */
2109 if (p
->longjt
== 0) {
2110 /* mark this instruction and retry */
2114 /* branch if T to following jump */
2115 dst
->jt
= extrajmps
;
2117 dst
[extrajmps
].code
= BPF_JMP
|BPF_JA
;
2118 dst
[extrajmps
].k
= off
- extrajmps
;
2122 off
= JF(p
)->offset
- (p
->offset
+ slen
) - 1;
2124 /* offset too large for branch, must add a jump */
2125 if (p
->longjf
== 0) {
2126 /* mark this instruction and retry */
2130 /* branch if F to following jump */
2131 /* if two jumps are inserted, F goes to second one */
2132 dst
->jf
= extrajmps
;
2134 dst
[extrajmps
].code
= BPF_JMP
|BPF_JA
;
2135 dst
[extrajmps
].k
= off
- extrajmps
;
2145 * Convert flowgraph intermediate representation to the
2146 * BPF array representation. Set *lenp to the number of instructions.
2148 * This routine does *NOT* leak the memory pointed to by fp. It *must
2149 * not* do free(fp) before returning fp; doing so would make no sense,
2150 * as the BPF array pointed to by the return value of icode_to_fcode()
2151 * must be valid - it's being returned for use in a bpf_program structure.
2153 * If it appears that icode_to_fcode() is leaking, the problem is that
2154 * the program using pcap_compile() is failing to free the memory in
2155 * the BPF program when it's done - the leak is in the program, not in
2156 * the routine that happens to be allocating the memory. (By analogy, if
2157 * a program calls fopen() without ever calling fclose() on the FILE *,
2158 * it will leak the FILE structure; the leak is not in fopen(), it's in
2159 * the program.) Change the program to use pcap_freecode() when it's
2160 * done with the filter program. See the pcap man page.
2163 icode_to_fcode(struct block
*root
, u_int
*lenp
)
2166 struct bpf_insn
*fp
;
2169 * Loop doing convert_code_r() until no branches remain
2170 * with too-large offsets.
2174 n
= *lenp
= count_stmts(root
);
2176 fp
= (struct bpf_insn
*)malloc(sizeof(*fp
) * n
);
2178 bpf_error("malloc");
2179 memset((char *)fp
, 0, sizeof(*fp
) * n
);
2184 if (convert_code_r(root
))
2193 * Make a copy of a BPF program and put it in the "fcode" member of
2196 * If we fail to allocate memory for the copy, fill in the "errbuf"
2197 * member of the "pcap_t" with an error message, and return -1;
2198 * otherwise, return 0.
2201 install_bpf_program(pcap_t
*p
, struct bpf_program
*fp
)
2206 * Validate the program.
2208 if (!bpf_validate(fp
->bf_insns
, fp
->bf_len
)) {
2209 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2210 "BPF program is not valid");
2215 * Free up any already installed program.
2217 pcap_freecode(&p
->fcode
);
2219 prog_size
= sizeof(*fp
->bf_insns
) * fp
->bf_len
;
2220 p
->fcode
.bf_len
= fp
->bf_len
;
2221 p
->fcode
.bf_insns
= (struct bpf_insn
*)malloc(prog_size
);
2222 if (p
->fcode
.bf_insns
== NULL
) {
2223 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
2224 "malloc: %s", pcap_strerror(errno
));
2227 memcpy(p
->fcode
.bf_insns
, fp
->bf_insns
, prog_size
);
2233 opt_dump(struct block
*root
)
2235 struct bpf_program f
;
2237 memset(bids
, 0, sizeof bids
);
2238 f
.bf_insns
= icode_to_fcode(root
, &f
.bf_len
);
2241 free((char *)f
.bf_insns
);