]>
git.saurik.com Git - apple/network_cmds.git/blob - pcap/optimize.c
e78ce1c0c191125549443cadc3276c7c6414596b
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: optimize.c,v 1.5 1996/09/16 02:33:07 tholo Exp $ */
27 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that: (1) source code distributions
32 * retain the above copyright notice and this paragraph in its entirety, (2)
33 * distributions including binary code include the above copyright notice and
34 * this paragraph in its entirety in the documentation or other materials
35 * provided with the distribution, and (3) all advertising materials mentioning
36 * features or use of this software display the following acknowledgement:
37 * ``This product includes software developed by the University of California,
38 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
39 * the University nor the names of its contributors may be used to endorse
40 * or promote products derived from this software without specific prior
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
43 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 * Optimization module for tcpdump intermediate representation.
50 "@(#) Header: optimize.c,v 1.58 96/06/16 22:36:59 leres Exp (LBL)";
53 #include <sys/types.h>
62 #ifdef HAVE_OS_PROTO_H
73 #define A_ATOM BPF_MEMWORDS
74 #define X_ATOM (BPF_MEMWORDS+1)
79 * This define is used to represent *both* the accumulator and
80 * x register in use-def computations.
81 * Currently, the use-def code assumes only one definition per instruction.
83 #define AX_ATOM N_ATOMS
86 * A flag to indicate that further optimization is needed.
87 * Iterative passes are continued until a given pass yields no
93 * A block is marked if only if its mark equals the current mark.
94 * Rather than traverse the code array, marking each item, 'cur_mark' is
95 * incremented. This automatically makes each element unmarked.
98 #define isMarked(p) ((p)->mark == cur_mark)
99 #define unMarkAll() cur_mark += 1
100 #define Mark(p) ((p)->mark = cur_mark)
102 static void opt_init(struct block
*);
103 static void opt_cleanup(void);
105 static void make_marks(struct block
*);
106 static void mark_code(struct block
*);
108 static void intern_blocks(struct block
*);
110 static int eq_slist(struct slist
*, struct slist
*);
112 static void find_levels_r(struct block
*);
114 static void find_levels(struct block
*);
115 static void find_dom(struct block
*);
116 static void propedom(struct edge
*);
117 static void find_edom(struct block
*);
118 static void find_closure(struct block
*);
119 static int atomuse(struct stmt
*);
120 static int atomdef(struct stmt
*);
121 static void compute_local_ud(struct block
*);
122 static void find_ud(struct block
*);
123 static void init_val(void);
124 static int F(int, int, int);
125 static __inline
void vstore(struct stmt
*, int *, int, int);
126 static void opt_blk(struct block
*, int);
127 static int use_conflict(struct block
*, struct block
*);
128 static void opt_j(struct edge
*);
129 static void or_pullup(struct block
*);
130 static void and_pullup(struct block
*);
131 static void opt_blks(struct block
*, int);
132 static __inline
void link_inedge(struct edge
*, struct block
*);
133 static void find_inedges(struct block
*);
134 static void opt_root(struct block
**);
135 static void opt_loop(struct block
*, int);
136 static void fold_op(struct stmt
*, int, int);
137 static __inline
struct slist
*this_op(struct slist
*);
138 static void opt_not(struct block
*);
139 static void opt_peep(struct block
*);
140 static void opt_stmt(struct stmt
*, int[], int);
141 static void deadstmt(struct stmt
*, struct stmt
*[]);
142 static void opt_deadstores(struct block
*);
143 static void opt_blk(struct block
*, int);
144 static int use_conflict(struct block
*, struct block
*);
145 static void opt_j(struct edge
*);
146 static struct block
*fold_edge(struct block
*, struct edge
*);
147 static __inline
int eq_blk(struct block
*, struct block
*);
148 static int slength(struct slist
*);
149 static int count_blocks(struct block
*);
150 static void number_blks_r(struct block
*);
151 static int count_stmts(struct block
*);
152 static int convert_code_r(struct block
*);
154 static void opt_dump(struct block
*);
158 struct block
**blocks
;
163 * A bit vector set representation of the dominators.
164 * We round up the set size to the next power of two.
166 static int nodewords
;
167 static int edgewords
;
168 struct block
**levels
;
170 #define BITS_PER_WORD (8*sizeof(bpf_u_int32))
172 * True if a is in uset {p}
174 #define SET_MEMBER(p, a) \
175 ((p)[(unsigned)(a) / BITS_PER_WORD] & (1 << ((unsigned)(a) % BITS_PER_WORD)))
180 #define SET_INSERT(p, a) \
181 (p)[(unsigned)(a) / BITS_PER_WORD] |= (1 << ((unsigned)(a) % BITS_PER_WORD))
184 * Delete 'a' from uset p.
186 #define SET_DELETE(p, a) \
187 (p)[(unsigned)(a) / BITS_PER_WORD] &= ~(1 << ((unsigned)(a) % BITS_PER_WORD))
192 #define SET_INTERSECT(a, b, n)\
194 register bpf_u_int32 *_x = a, *_y = b;\
195 register int _n = n;\
196 while (--_n >= 0) *_x++ &= *_y++;\
202 #define SET_SUBTRACT(a, b, n)\
204 register bpf_u_int32 *_x = a, *_y = b;\
205 register int _n = n;\
206 while (--_n >= 0) *_x++ &=~ *_y++;\
212 #define SET_UNION(a, b, n)\
214 register bpf_u_int32 *_x = a, *_y = b;\
215 register int _n = n;\
216 while (--_n >= 0) *_x++ |= *_y++;\
219 static uset all_dom_sets
;
220 static uset all_closure_sets
;
221 static uset all_edge_sets
;
224 #define MAX(a,b) ((a)>(b)?(a):(b))
240 find_levels_r(JT(b
));
241 find_levels_r(JF(b
));
242 level
= MAX(JT(b
)->level
, JF(b
)->level
) + 1;
246 b
->link
= levels
[level
];
251 * Level graph. The levels go from 0 at the leaves to
252 * N_LEVELS at the root. The levels[] array points to the
253 * first node of the level list, whose elements are linked
254 * with the 'link' field of the struct block.
260 memset((char *)levels
, 0, n_blocks
* sizeof(*levels
));
266 * Find dominator relationships.
267 * Assumes graph has been leveled.
278 * Initialize sets to contain all nodes.
281 i
= n_blocks
* nodewords
;
284 /* Root starts off empty. */
285 for (i
= nodewords
; --i
>= 0;)
288 /* root->level is the highest level no found. */
289 for (i
= root
->level
; i
>= 0; --i
) {
290 for (b
= levels
[i
]; b
; b
= b
->link
) {
291 SET_INSERT(b
->dom
, b
->id
);
294 SET_INTERSECT(JT(b
)->dom
, b
->dom
, nodewords
);
295 SET_INTERSECT(JF(b
)->dom
, b
->dom
, nodewords
);
304 SET_INSERT(ep
->edom
, ep
->id
);
306 SET_INTERSECT(ep
->succ
->et
.edom
, ep
->edom
, edgewords
);
307 SET_INTERSECT(ep
->succ
->ef
.edom
, ep
->edom
, edgewords
);
312 * Compute edge dominators.
313 * Assumes graph has been leveled and predecessors established.
324 for (i
= n_edges
* edgewords
; --i
>= 0; )
327 /* root->level is the highest level no found. */
328 memset(root
->et
.edom
, 0, edgewords
* sizeof(*(uset
)0));
329 memset(root
->ef
.edom
, 0, edgewords
* sizeof(*(uset
)0));
330 for (i
= root
->level
; i
>= 0; --i
) {
331 for (b
= levels
[i
]; b
!= 0; b
= b
->link
) {
339 * Find the backwards transitive closure of the flow graph. These sets
340 * are backwards in the sense that we find the set of nodes that reach
341 * a given node, not the set of nodes that can be reached by a node.
343 * Assumes graph has been leveled.
353 * Initialize sets to contain no nodes.
355 memset((char *)all_closure_sets
, 0,
356 n_blocks
* nodewords
* sizeof(*all_closure_sets
));
358 /* root->level is the highest level no found. */
359 for (i
= root
->level
; i
>= 0; --i
) {
360 for (b
= levels
[i
]; b
; b
= b
->link
) {
361 SET_INSERT(b
->closure
, b
->id
);
364 SET_UNION(JT(b
)->closure
, b
->closure
, nodewords
);
365 SET_UNION(JF(b
)->closure
, b
->closure
, nodewords
);
371 * Return the register number that is used by s. If A and X are both
372 * used, return AX_ATOM. If no register is used, return -1.
374 * The implementation should probably change to an array access.
380 register int c
= s
->code
;
385 switch (BPF_CLASS(c
)) {
388 return (BPF_RVAL(c
) == BPF_A
) ? A_ATOM
:
389 (BPF_RVAL(c
) == BPF_X
) ? X_ATOM
: -1;
393 return (BPF_MODE(c
) == BPF_IND
) ? X_ATOM
:
394 (BPF_MODE(c
) == BPF_MEM
) ? s
->k
: -1;
404 if (BPF_SRC(c
) == BPF_X
)
409 return BPF_MISCOP(c
) == BPF_TXA
? X_ATOM
: A_ATOM
;
416 * Return the register number that is defined by 's'. We assume that
417 * a single stmt cannot define more than one register. If no register
418 * is defined, return -1.
420 * The implementation should probably change to an array access.
429 switch (BPF_CLASS(s
->code
)) {
443 return BPF_MISCOP(s
->code
) == BPF_TAX
? X_ATOM
: A_ATOM
;
453 atomset def
= 0, use
= 0, kill
= 0;
456 for (s
= b
->stmts
; s
; s
= s
->next
) {
457 if (s
->s
.code
== NOP
)
459 atom
= atomuse(&s
->s
);
461 if (atom
== AX_ATOM
) {
462 if (!ATOMELEM(def
, X_ATOM
))
463 use
|= ATOMMASK(X_ATOM
);
464 if (!ATOMELEM(def
, A_ATOM
))
465 use
|= ATOMMASK(A_ATOM
);
467 else if (atom
< N_ATOMS
) {
468 if (!ATOMELEM(def
, atom
))
469 use
|= ATOMMASK(atom
);
474 atom
= atomdef(&s
->s
);
476 if (!ATOMELEM(use
, atom
))
477 kill
|= ATOMMASK(atom
);
478 def
|= ATOMMASK(atom
);
481 if (!ATOMELEM(def
, A_ATOM
) && BPF_CLASS(b
->s
.code
) == BPF_JMP
)
482 use
|= ATOMMASK(A_ATOM
);
490 * Assume graph is already leveled.
500 * root->level is the highest level no found;
501 * count down from there.
503 maxlevel
= root
->level
;
504 for (i
= maxlevel
; i
>= 0; --i
)
505 for (p
= levels
[i
]; p
; p
= p
->link
) {
510 for (i
= 1; i
<= maxlevel
; ++i
) {
511 for (p
= levels
[i
]; p
; p
= p
->link
) {
512 p
->out_use
|= JT(p
)->in_use
| JF(p
)->in_use
;
513 p
->in_use
|= p
->out_use
&~ p
->kill
;
519 * These data structures are used in a Cocke and Shwarz style
520 * value numbering scheme. Since the flowgraph is acyclic,
521 * exit values can be propagated from a node's predecessors
522 * provided it is uniquely defined.
528 struct valnode
*next
;
532 static struct valnode
*hashtbl
[MODULUS
];
536 /* Integer constants mapped with the load immediate opcode. */
537 #define K(i) F(BPF_LD|BPF_IMM|BPF_W, i, 0L)
544 struct vmapinfo
*vmap
;
545 struct valnode
*vnode_base
;
546 struct valnode
*next_vnode
;
552 next_vnode
= vnode_base
;
553 memset((char *)vmap
, 0, maxval
* sizeof(*vmap
));
554 memset((char *)hashtbl
, 0, sizeof hashtbl
);
557 /* Because we really don't have an IR, this stuff is a little messy. */
567 hash
= (u_int
)code
^ (v0
<< 4) ^ (v1
<< 8);
570 for (p
= hashtbl
[hash
]; p
; p
= p
->next
)
571 if (p
->code
== code
&& p
->v0
== v0
&& p
->v1
== v1
)
575 if (BPF_MODE(code
) == BPF_IMM
&&
576 (BPF_CLASS(code
) == BPF_LD
|| BPF_CLASS(code
) == BPF_LDX
)) {
577 vmap
[val
].const_val
= v0
;
578 vmap
[val
].is_const
= 1;
585 p
->next
= hashtbl
[hash
];
592 vstore(s
, valp
, newval
, alter
)
598 if (alter
&& *valp
== newval
)
611 a
= vmap
[v0
].const_val
;
612 b
= vmap
[v1
].const_val
;
614 switch (BPF_OP(s
->code
)) {
629 bpf_error("division by zero");
657 s
->code
= BPF_LD
|BPF_IMM
;
661 static __inline
struct slist
*
665 while (s
!= 0 && s
->s
.code
== NOP
)
674 struct block
*tmp
= JT(b
);
685 struct slist
*next
, *last
;
697 next
= this_op(s
->next
);
703 * st M[k] --> st M[k]
706 if (s
->s
.code
== BPF_ST
&&
707 next
->s
.code
== (BPF_LDX
|BPF_MEM
) &&
708 s
->s
.k
== next
->s
.k
) {
710 next
->s
.code
= BPF_MISC
|BPF_TAX
;
716 if (s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
717 next
->s
.code
== (BPF_MISC
|BPF_TAX
)) {
718 s
->s
.code
= BPF_LDX
|BPF_IMM
;
719 next
->s
.code
= BPF_MISC
|BPF_TXA
;
723 * This is an ugly special case, but it happens
724 * when you say tcp[k] or udp[k] where k is a constant.
726 if (s
->s
.code
== (BPF_LD
|BPF_IMM
)) {
727 struct slist
*add
, *tax
, *ild
;
730 * Check that X isn't used on exit from this
731 * block (which the optimizer might cause).
732 * We know the code generator won't generate
733 * any local dependencies.
735 if (ATOMELEM(b
->out_use
, X_ATOM
))
738 if (next
->s
.code
!= (BPF_LDX
|BPF_MSH
|BPF_B
))
741 add
= this_op(next
->next
);
742 if (add
== 0 || add
->s
.code
!= (BPF_ALU
|BPF_ADD
|BPF_X
))
745 tax
= this_op(add
->next
);
746 if (tax
== 0 || tax
->s
.code
!= (BPF_MISC
|BPF_TAX
))
749 ild
= this_op(tax
->next
);
750 if (ild
== 0 || BPF_CLASS(ild
->s
.code
) != BPF_LD
||
751 BPF_MODE(ild
->s
.code
) != BPF_IND
)
754 * XXX We need to check that X is not
755 * subsequently used. We know we can eliminate the
756 * accumulator modifications since it is defined
757 * by the last stmt of this sequence.
759 * We want to turn this sequence:
762 * (005) ldxms [14] {next} -- optional
765 * (008) ild [x+0] {ild}
767 * into this sequence:
785 * If we have a subtract to do a comparison, and the X register
786 * is a known constant, we can merge this value into the
789 if (last
->s
.code
== (BPF_ALU
|BPF_SUB
|BPF_X
) &&
790 !ATOMELEM(b
->out_use
, A_ATOM
)) {
791 val
= b
->val
[X_ATOM
];
792 if (vmap
[val
].is_const
) {
795 b
->s
.k
+= vmap
[val
].const_val
;
796 op
= BPF_OP(b
->s
.code
);
797 if (op
== BPF_JGT
|| op
== BPF_JGE
) {
798 struct block
*t
= JT(b
);
801 b
->s
.k
+= 0x80000000;
805 } else if (b
->s
.k
== 0) {
811 b
->s
.code
= BPF_CLASS(b
->s
.code
) | BPF_OP(b
->s
.code
) |
817 * Likewise, a constant subtract can be simplified.
819 else if (last
->s
.code
== (BPF_ALU
|BPF_SUB
|BPF_K
) &&
820 !ATOMELEM(b
->out_use
, A_ATOM
)) {
825 op
= BPF_OP(b
->s
.code
);
826 if (op
== BPF_JGT
|| op
== BPF_JGE
) {
827 struct block
*t
= JT(b
);
830 b
->s
.k
+= 0x80000000;
838 if (last
->s
.code
== (BPF_ALU
|BPF_AND
|BPF_K
) &&
839 !ATOMELEM(b
->out_use
, A_ATOM
) && b
->s
.k
== 0) {
841 b
->s
.code
= BPF_JMP
|BPF_K
|BPF_JSET
;
847 * If the accumulator is a known constant, we can compute the
850 val
= b
->val
[A_ATOM
];
851 if (vmap
[val
].is_const
&& BPF_SRC(b
->s
.code
) == BPF_K
) {
852 bpf_int32 v
= vmap
[val
].const_val
;
853 switch (BPF_OP(b
->s
.code
)) {
860 v
= (unsigned)v
> b
->s
.k
;
864 v
= (unsigned)v
>= b
->s
.k
;
884 * Compute the symbolic value of expression of 's', and update
885 * anything it defines in the value table 'val'. If 'alter' is true,
886 * do various optimizations. This code would be cleaner if symbolic
887 * evaluation and code transformations weren't folded together.
890 opt_stmt(s
, val
, alter
)
900 case BPF_LD
|BPF_ABS
|BPF_W
:
901 case BPF_LD
|BPF_ABS
|BPF_H
:
902 case BPF_LD
|BPF_ABS
|BPF_B
:
903 v
= F(s
->code
, s
->k
, 0L);
904 vstore(s
, &val
[A_ATOM
], v
, alter
);
907 case BPF_LD
|BPF_IND
|BPF_W
:
908 case BPF_LD
|BPF_IND
|BPF_H
:
909 case BPF_LD
|BPF_IND
|BPF_B
:
911 if (alter
&& vmap
[v
].is_const
) {
912 s
->code
= BPF_LD
|BPF_ABS
|BPF_SIZE(s
->code
);
913 s
->k
+= vmap
[v
].const_val
;
914 v
= F(s
->code
, s
->k
, 0L);
918 v
= F(s
->code
, s
->k
, v
);
919 vstore(s
, &val
[A_ATOM
], v
, alter
);
923 v
= F(s
->code
, 0L, 0L);
924 vstore(s
, &val
[A_ATOM
], v
, alter
);
929 vstore(s
, &val
[A_ATOM
], v
, alter
);
932 case BPF_LDX
|BPF_IMM
:
934 vstore(s
, &val
[X_ATOM
], v
, alter
);
937 case BPF_LDX
|BPF_MSH
|BPF_B
:
938 v
= F(s
->code
, s
->k
, 0L);
939 vstore(s
, &val
[X_ATOM
], v
, alter
);
942 case BPF_ALU
|BPF_NEG
:
943 if (alter
&& vmap
[val
[A_ATOM
]].is_const
) {
944 s
->code
= BPF_LD
|BPF_IMM
;
945 s
->k
= -vmap
[val
[A_ATOM
]].const_val
;
946 val
[A_ATOM
] = K(s
->k
);
949 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], 0L);
952 case BPF_ALU
|BPF_ADD
|BPF_K
:
953 case BPF_ALU
|BPF_SUB
|BPF_K
:
954 case BPF_ALU
|BPF_MUL
|BPF_K
:
955 case BPF_ALU
|BPF_DIV
|BPF_K
:
956 case BPF_ALU
|BPF_AND
|BPF_K
:
957 case BPF_ALU
|BPF_OR
|BPF_K
:
958 case BPF_ALU
|BPF_LSH
|BPF_K
:
959 case BPF_ALU
|BPF_RSH
|BPF_K
:
960 op
= BPF_OP(s
->code
);
963 if (op
== BPF_ADD
|| op
== BPF_SUB
||
964 op
== BPF_LSH
|| op
== BPF_RSH
||
969 if (op
== BPF_MUL
|| op
== BPF_AND
) {
970 s
->code
= BPF_LD
|BPF_IMM
;
971 val
[A_ATOM
] = K(s
->k
);
975 if (vmap
[val
[A_ATOM
]].is_const
) {
976 fold_op(s
, val
[A_ATOM
], K(s
->k
));
977 val
[A_ATOM
] = K(s
->k
);
981 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], K(s
->k
));
984 case BPF_ALU
|BPF_ADD
|BPF_X
:
985 case BPF_ALU
|BPF_SUB
|BPF_X
:
986 case BPF_ALU
|BPF_MUL
|BPF_X
:
987 case BPF_ALU
|BPF_DIV
|BPF_X
:
988 case BPF_ALU
|BPF_AND
|BPF_X
:
989 case BPF_ALU
|BPF_OR
|BPF_X
:
990 case BPF_ALU
|BPF_LSH
|BPF_X
:
991 case BPF_ALU
|BPF_RSH
|BPF_X
:
992 op
= BPF_OP(s
->code
);
993 if (alter
&& vmap
[val
[X_ATOM
]].is_const
) {
994 if (vmap
[val
[A_ATOM
]].is_const
) {
995 fold_op(s
, val
[A_ATOM
], val
[X_ATOM
]);
996 val
[A_ATOM
] = K(s
->k
);
999 s
->code
= BPF_ALU
|BPF_K
|op
;
1000 s
->k
= vmap
[val
[X_ATOM
]].const_val
;
1003 F(s
->code
, val
[A_ATOM
], K(s
->k
));
1008 * Check if we're doing something to an accumulator
1009 * that is 0, and simplify. This may not seem like
1010 * much of a simplification but it could open up further
1012 * XXX We could also check for mul by 1, and -1, etc.
1014 if (alter
&& vmap
[val
[A_ATOM
]].is_const
1015 && vmap
[val
[A_ATOM
]].const_val
== 0) {
1016 if (op
== BPF_ADD
|| op
== BPF_OR
||
1017 op
== BPF_LSH
|| op
== BPF_RSH
|| op
== BPF_SUB
) {
1018 s
->code
= BPF_MISC
|BPF_TXA
;
1019 vstore(s
, &val
[A_ATOM
], val
[X_ATOM
], alter
);
1022 else if (op
== BPF_MUL
|| op
== BPF_DIV
||
1024 s
->code
= BPF_LD
|BPF_IMM
;
1026 vstore(s
, &val
[A_ATOM
], K(s
->k
), alter
);
1029 else if (op
== BPF_NEG
) {
1034 val
[A_ATOM
] = F(s
->code
, val
[A_ATOM
], val
[X_ATOM
]);
1037 case BPF_MISC
|BPF_TXA
:
1038 vstore(s
, &val
[A_ATOM
], val
[X_ATOM
], alter
);
1041 case BPF_LD
|BPF_MEM
:
1043 if (alter
&& vmap
[v
].is_const
) {
1044 s
->code
= BPF_LD
|BPF_IMM
;
1045 s
->k
= vmap
[v
].const_val
;
1048 vstore(s
, &val
[A_ATOM
], v
, alter
);
1051 case BPF_MISC
|BPF_TAX
:
1052 vstore(s
, &val
[X_ATOM
], val
[A_ATOM
], alter
);
1055 case BPF_LDX
|BPF_MEM
:
1057 if (alter
&& vmap
[v
].is_const
) {
1058 s
->code
= BPF_LDX
|BPF_IMM
;
1059 s
->k
= vmap
[v
].const_val
;
1062 vstore(s
, &val
[X_ATOM
], v
, alter
);
1066 vstore(s
, &val
[s
->k
], val
[A_ATOM
], alter
);
1070 vstore(s
, &val
[s
->k
], val
[X_ATOM
], alter
);
1077 register struct stmt
*s
;
1078 register struct stmt
*last
[];
1084 if (atom
== AX_ATOM
) {
1095 last
[atom
]->code
= NOP
;
1103 register struct block
*b
;
1105 register struct slist
*s
;
1107 struct stmt
*last
[N_ATOMS
];
1109 memset((char *)last
, 0, sizeof last
);
1111 for (s
= b
->stmts
; s
!= 0; s
= s
->next
)
1112 deadstmt(&s
->s
, last
);
1113 deadstmt(&b
->s
, last
);
1115 for (atom
= 0; atom
< N_ATOMS
; ++atom
)
1116 if (last
[atom
] && !ATOMELEM(b
->out_use
, atom
)) {
1117 last
[atom
]->code
= NOP
;
1123 opt_blk(b
, do_stmts
)
1133 * Initialize the atom values.
1134 * If we have no predecessors, everything is undefined.
1135 * Otherwise, we inherent our values from our predecessors.
1136 * If any register has an ambiguous value (i.e. control paths are
1137 * merging) give it the undefined value of 0.
1141 memset((char *)b
->val
, 0, sizeof(b
->val
));
1143 memcpy((char *)b
->val
, (char *)p
->pred
->val
, sizeof(b
->val
));
1144 while ((p
= p
->next
) != NULL
) {
1145 for (i
= 0; i
< N_ATOMS
; ++i
)
1146 if (b
->val
[i
] != p
->pred
->val
[i
])
1150 aval
= b
->val
[A_ATOM
];
1151 for (s
= b
->stmts
; s
; s
= s
->next
)
1152 opt_stmt(&s
->s
, b
->val
, do_stmts
);
1155 * This is a special case: if we don't use anything from this
1156 * block, and we load the accumulator with value that is
1157 * already there, or if this block is a return,
1158 * eliminate all the statements.
1161 ((b
->out_use
== 0 && aval
!= 0 &&b
->val
[A_ATOM
] == aval
) ||
1162 BPF_CLASS(b
->s
.code
) == BPF_RET
)) {
1163 if (b
->stmts
!= 0) {
1172 * Set up values for branch optimizer.
1174 if (BPF_SRC(b
->s
.code
) == BPF_K
)
1175 b
->oval
= K(b
->s
.k
);
1177 b
->oval
= b
->val
[X_ATOM
];
1178 b
->et
.code
= b
->s
.code
;
1179 b
->ef
.code
= -b
->s
.code
;
1183 * Return true if any register that is used on exit from 'succ', has
1184 * an exit value that is different from the corresponding exit value
1188 use_conflict(b
, succ
)
1189 struct block
*b
, *succ
;
1192 atomset use
= succ
->out_use
;
1197 for (atom
= 0; atom
< N_ATOMS
; ++atom
)
1198 if (ATOMELEM(use
, atom
))
1199 if (b
->val
[atom
] != succ
->val
[atom
])
1204 static struct block
*
1205 fold_edge(child
, ep
)
1206 struct block
*child
;
1210 int aval0
, aval1
, oval0
, oval1
;
1211 int code
= ep
->code
;
1219 if (child
->s
.code
!= code
)
1222 aval0
= child
->val
[A_ATOM
];
1223 oval0
= child
->oval
;
1224 aval1
= ep
->pred
->val
[A_ATOM
];
1225 oval1
= ep
->pred
->oval
;
1232 * The operands are identical, so the
1233 * result is true if a true branch was
1234 * taken to get here, otherwise false.
1236 return sense
? JT(child
) : JF(child
);
1238 if (sense
&& code
== (BPF_JMP
|BPF_JEQ
|BPF_K
))
1240 * At this point, we only know the comparison if we
1241 * came down the true branch, and it was an equality
1242 * comparison with a constant. We rely on the fact that
1243 * distinct constants have distinct value numbers.
1255 register struct block
*target
;
1257 if (JT(ep
->succ
) == 0)
1260 if (JT(ep
->succ
) == JF(ep
->succ
)) {
1262 * Common branch targets can be eliminated, provided
1263 * there is no data dependency.
1265 if (!use_conflict(ep
->pred
, ep
->succ
->et
.succ
)) {
1267 ep
->succ
= JT(ep
->succ
);
1271 * For each edge dominator that matches the successor of this
1272 * edge, promote the edge successor to the its grandchild.
1274 * XXX We violate the set abstraction here in favor a reasonably
1278 for (i
= 0; i
< edgewords
; ++i
) {
1279 register bpf_u_int32 x
= ep
->edom
[i
];
1284 k
+= i
* BITS_PER_WORD
;
1286 target
= fold_edge(ep
->succ
, edges
[k
]);
1288 * Check that there is no data dependency between
1289 * nodes that will be violated if we move the edge.
1291 if (target
!= 0 && !use_conflict(ep
->pred
, target
)) {
1294 if (JT(target
) != 0)
1296 * Start over unless we hit a leaf.
1312 struct block
**diffp
, **samep
;
1320 * Make sure each predecessor loads the same value.
1323 val
= ep
->pred
->val
[A_ATOM
];
1324 for (ep
= ep
->next
; ep
!= 0; ep
= ep
->next
)
1325 if (val
!= ep
->pred
->val
[A_ATOM
])
1328 if (JT(b
->in_edges
->pred
) == b
)
1329 diffp
= &JT(b
->in_edges
->pred
);
1331 diffp
= &JF(b
->in_edges
->pred
);
1338 if (JT(*diffp
) != JT(b
))
1341 if (!SET_MEMBER((*diffp
)->dom
, b
->id
))
1344 if ((*diffp
)->val
[A_ATOM
] != val
)
1347 diffp
= &JF(*diffp
);
1350 samep
= &JF(*diffp
);
1355 if (JT(*samep
) != JT(b
))
1358 if (!SET_MEMBER((*samep
)->dom
, b
->id
))
1361 if ((*samep
)->val
[A_ATOM
] == val
)
1364 /* XXX Need to check that there are no data dependencies
1365 between dp0 and dp1. Currently, the code generator
1366 will not produce such dependencies. */
1367 samep
= &JF(*samep
);
1370 /* XXX This doesn't cover everything. */
1371 for (i
= 0; i
< N_ATOMS
; ++i
)
1372 if ((*samep
)->val
[i
] != pred
->val
[i
])
1375 /* Pull up the node. */
1381 * At the top of the chain, each predecessor needs to point at the
1382 * pulled up node. Inside the chain, there is only one predecessor
1386 for (ep
= b
->in_edges
; ep
!= 0; ep
= ep
->next
) {
1387 if (JT(ep
->pred
) == b
)
1388 JT(ep
->pred
) = pull
;
1390 JF(ep
->pred
) = pull
;
1405 struct block
**diffp
, **samep
;
1413 * Make sure each predecessor loads the same value.
1415 val
= ep
->pred
->val
[A_ATOM
];
1416 for (ep
= ep
->next
; ep
!= 0; ep
= ep
->next
)
1417 if (val
!= ep
->pred
->val
[A_ATOM
])
1420 if (JT(b
->in_edges
->pred
) == b
)
1421 diffp
= &JT(b
->in_edges
->pred
);
1423 diffp
= &JF(b
->in_edges
->pred
);
1430 if (JF(*diffp
) != JF(b
))
1433 if (!SET_MEMBER((*diffp
)->dom
, b
->id
))
1436 if ((*diffp
)->val
[A_ATOM
] != val
)
1439 diffp
= &JT(*diffp
);
1442 samep
= &JT(*diffp
);
1447 if (JF(*samep
) != JF(b
))
1450 if (!SET_MEMBER((*samep
)->dom
, b
->id
))
1453 if ((*samep
)->val
[A_ATOM
] == val
)
1456 /* XXX Need to check that there are no data dependencies
1457 between diffp and samep. Currently, the code generator
1458 will not produce such dependencies. */
1459 samep
= &JT(*samep
);
1462 /* XXX This doesn't cover everything. */
1463 for (i
= 0; i
< N_ATOMS
; ++i
)
1464 if ((*samep
)->val
[i
] != pred
->val
[i
])
1467 /* Pull up the node. */
1473 * At the top of the chain, each predecessor needs to point at the
1474 * pulled up node. Inside the chain, there is only one predecessor
1478 for (ep
= b
->in_edges
; ep
!= 0; ep
= ep
->next
) {
1479 if (JT(ep
->pred
) == b
)
1480 JT(ep
->pred
) = pull
;
1482 JF(ep
->pred
) = pull
;
1492 opt_blks(root
, do_stmts
)
1500 maxlevel
= root
->level
;
1501 for (i
= maxlevel
; i
>= 0; --i
)
1502 for (p
= levels
[i
]; p
; p
= p
->link
)
1503 opt_blk(p
, do_stmts
);
1507 * No point trying to move branches; it can't possibly
1508 * make a difference at this point.
1512 for (i
= 1; i
<= maxlevel
; ++i
) {
1513 for (p
= levels
[i
]; p
; p
= p
->link
) {
1518 for (i
= 1; i
<= maxlevel
; ++i
) {
1519 for (p
= levels
[i
]; p
; p
= p
->link
) {
1526 static __inline
void
1527 link_inedge(parent
, child
)
1528 struct edge
*parent
;
1529 struct block
*child
;
1531 parent
->next
= child
->in_edges
;
1532 child
->in_edges
= parent
;
1542 for (i
= 0; i
< n_blocks
; ++i
)
1543 blocks
[i
]->in_edges
= 0;
1546 * Traverse the graph, adding each edge to the predecessor
1547 * list of its successors. Skip the leaves (i.e. level 0).
1549 for (i
= root
->level
; i
> 0; --i
) {
1550 for (b
= levels
[i
]; b
!= 0; b
= b
->link
) {
1551 link_inedge(&b
->et
, JT(b
));
1552 link_inedge(&b
->ef
, JF(b
));
1561 struct slist
*tmp
, *s
;
1565 while (BPF_CLASS((*b
)->s
.code
) == BPF_JMP
&& JT(*b
) == JF(*b
))
1574 * If the root node is a return, then there is no
1575 * point executing any statements (since the bpf machine
1576 * has no side effects).
1578 if (BPF_CLASS((*b
)->s
.code
) == BPF_RET
)
1583 opt_loop(root
, do_stmts
)
1600 opt_blks(root
, do_stmts
);
1609 * Optimize the filter code in its dag representation.
1613 struct block
**rootp
;
1622 intern_blocks(root
);
1633 if (BPF_CLASS(p
->s
.code
) != BPF_RET
) {
1641 * Mark code array such that isMarked(i) is true
1642 * only for nodes that are alive.
1653 * True iff the two stmt lists load the same value from the packet into
1658 struct slist
*x
, *y
;
1661 while (x
&& x
->s
.code
== NOP
)
1663 while (y
&& y
->s
.code
== NOP
)
1669 if (x
->s
.code
!= y
->s
.code
|| x
->s
.k
!= y
->s
.k
)
1678 struct block
*b0
, *b1
;
1680 if (b0
->s
.code
== b1
->s
.code
&&
1681 b0
->s
.k
== b1
->s
.k
&&
1682 b0
->et
.succ
== b1
->et
.succ
&&
1683 b0
->ef
.succ
== b1
->ef
.succ
)
1684 return eq_slist(b0
->stmts
, b1
->stmts
);
1697 for (i
= 0; i
< n_blocks
; ++i
)
1698 blocks
[i
]->link
= 0;
1702 for (i
= n_blocks
- 1; --i
>= 0; ) {
1703 if (!isMarked(blocks
[i
]))
1705 for (j
= i
+ 1; j
< n_blocks
; ++j
) {
1706 if (!isMarked(blocks
[j
]))
1708 if (eq_blk(blocks
[i
], blocks
[j
])) {
1709 blocks
[i
]->link
= blocks
[j
]->link
?
1710 blocks
[j
]->link
: blocks
[j
];
1715 for (i
= 0; i
< n_blocks
; ++i
) {
1721 JT(p
) = JT(p
)->link
;
1725 JF(p
) = JF(p
)->link
;
1735 free((void *)vnode_base
);
1737 free((void *)edges
);
1738 free((void *)space
);
1739 free((void *)levels
);
1740 free((void *)blocks
);
1744 * Return the number of stmts in 's'.
1752 for (; s
; s
= s
->next
)
1753 if (s
->s
.code
!= NOP
)
1759 * Return the number of nodes reachable by 'p'.
1760 * All nodes should be initially unmarked.
1766 if (p
== 0 || isMarked(p
))
1769 return count_blocks(JT(p
)) + count_blocks(JF(p
)) + 1;
1773 * Do a depth first search on the flow graph, numbering the
1774 * the basic blocks, and entering them into the 'blocks' array.`
1782 if (p
== 0 || isMarked(p
))
1790 number_blks_r(JT(p
));
1791 number_blks_r(JF(p
));
1795 * Return the number of stmts in the flowgraph reachable by 'p'.
1796 * The nodes should be unmarked before calling.
1804 if (p
== 0 || isMarked(p
))
1807 n
= count_stmts(JT(p
)) + count_stmts(JF(p
));
1808 return slength(p
->stmts
) + n
+ 1;
1812 * Allocate memory. All allocation is done before optimization
1813 * is begun. A linear bound on the size of all data structures is computed
1814 * from the total number of blocks and/or statements.
1821 int i
, n
, max_stmts
;
1824 * First, count the blocks, so we can malloc an array to map
1825 * block number to block. Then, put the blocks into the array.
1828 n
= count_blocks(root
);
1829 blocks
= (struct block
**)malloc(n
* sizeof(*blocks
));
1832 number_blks_r(root
);
1834 n_edges
= 2 * n_blocks
;
1835 edges
= (struct edge
**)malloc(n_edges
* sizeof(*edges
));
1838 * The number of levels is bounded by the number of nodes.
1840 levels
= (struct block
**)malloc(n_blocks
* sizeof(*levels
));
1842 edgewords
= n_edges
/ (8 * sizeof(bpf_u_int32
)) + 1;
1843 nodewords
= n_blocks
/ (8 * sizeof(bpf_u_int32
)) + 1;
1846 space
= (bpf_u_int32
*)malloc(2 * n_blocks
* nodewords
* sizeof(*space
)
1847 + n_edges
* edgewords
* sizeof(*space
));
1850 for (i
= 0; i
< n
; ++i
) {
1854 all_closure_sets
= p
;
1855 for (i
= 0; i
< n
; ++i
) {
1856 blocks
[i
]->closure
= p
;
1860 for (i
= 0; i
< n
; ++i
) {
1861 register struct block
*b
= blocks
[i
];
1869 b
->ef
.id
= n_blocks
+ i
;
1870 edges
[n_blocks
+ i
] = &b
->ef
;
1875 for (i
= 0; i
< n
; ++i
)
1876 max_stmts
+= slength(blocks
[i
]->stmts
) + 1;
1878 * We allocate at most 3 value numbers per statement,
1879 * so this is an upper bound on the number of valnodes
1882 maxval
= 3 * max_stmts
;
1883 vmap
= (struct vmapinfo
*)malloc(maxval
* sizeof(*vmap
));
1884 vnode_base
= (struct valnode
*)malloc(maxval
* sizeof(*vmap
));
1888 * Some pointers used to convert the basic block form of the code,
1889 * into the array form that BPF requires. 'fstart' will point to
1890 * the malloc'd array while 'ftail' is used during the recursive traversal.
1892 static struct bpf_insn
*fstart
;
1893 static struct bpf_insn
*ftail
;
1900 * Returns true if successful. Returns false if a branch has
1901 * an offset that is too large. If so, we have marked that
1902 * branch so that on a subsequent iteration, it will be treated
1909 struct bpf_insn
*dst
;
1913 int extrajmps
; /* number of extra jumps inserted */
1915 if (p
== 0 || isMarked(p
))
1919 if (convert_code_r(JF(p
)) == 0)
1921 if (convert_code_r(JT(p
)) == 0)
1924 slen
= slength(p
->stmts
);
1925 dst
= ftail
-= (slen
+ 1 + p
->longjt
+ p
->longjf
);
1926 /* inflate length by any extra jumps */
1928 p
->offset
= dst
- fstart
;
1930 for (src
= p
->stmts
; src
; src
= src
->next
) {
1931 if (src
->s
.code
== NOP
)
1933 dst
->code
= (u_short
)src
->s
.code
;
1938 bids
[dst
- fstart
] = p
->id
+ 1;
1940 dst
->code
= (u_short
)p
->s
.code
;
1944 off
= JT(p
)->offset
- (p
->offset
+ slen
) - 1;
1946 /* offset too large for branch, must add a jump */
1947 if (p
->longjt
== 0) {
1948 /* mark this instruction and retry */
1952 /* branch if T to following jump */
1953 dst
->jt
= extrajmps
;
1955 dst
[extrajmps
].code
= BPF_JMP
|BPF_JA
;
1956 dst
[extrajmps
].k
= off
- extrajmps
;
1960 off
= JF(p
)->offset
- (p
->offset
+ slen
) - 1;
1962 /* offset too large for branch, must add a jump */
1963 if (p
->longjf
== 0) {
1964 /* mark this instruction and retry */
1968 /* branch if F to following jump */
1969 /* if two jumps are inserted, F goes to second one */
1970 dst
->jf
= extrajmps
;
1972 dst
[extrajmps
].code
= BPF_JMP
|BPF_JA
;
1973 dst
[extrajmps
].k
= off
- extrajmps
;
1983 * Convert flowgraph intermediate representation to the
1984 * BPF array representation. Set *lenp to the number of instructions.
1987 icode_to_fcode(root
, lenp
)
1992 struct bpf_insn
*fp
;
1995 * Loop doing convert_codr_r() until no branches remain
1996 * with too-large offsets.
2000 n
= *lenp
= count_stmts(root
);
2002 fp
= (struct bpf_insn
*)malloc(sizeof(*fp
) * n
);
2003 memset((char *)fp
, 0, sizeof(*fp
) * n
);
2008 if (convert_code_r(root
))
2021 struct bpf_program f
;
2023 memset(bids
, 0, sizeof bids
);
2024 f
.bf_insns
= icode_to_fcode(root
, &f
.bf_len
);
2027 free((char *)f
.bf_insns
);