1 /* $Id: cj.c,v 1.10 1994/06/24 10:20:47 ceriel Exp $ */
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
6 /* C R O S S J U M P I N G
15 #include "../share/types.h"
16 #include "../share/debug.h"
17 #include "../share/global.h"
18 #include "../share/files.h"
19 #include "../share/get.h"
20 #include "../share/put.h"
21 #include "../share/lset.h"
22 #include "../share/map.h"
23 #include "../share/alloc.h"
24 #include "../share/aux.h"
25 #include "../share/def.h"
26 #include "../share/stack_chg.h"
27 #include "../share/go.h"
30 /* Cross jumping performs optimzations like:
32 * if cond then goto L1; if cond then goto L1
41 * CJ looks for two basic blocks b1 and b2 with the following properties:
42 * - there exists a basic block S such that SUCC(b1) = SUCC(b2) = {S}
43 * (so both have only 1 successor)
44 * - the last N (N > 0) instructions of b1 and b2, not counting a possible
45 * BRAnch instruction, are the same.
46 * As a result of the first condition, at least of the two blocks must end
47 * on an (unconditional) BRAnch instruction. If both end on a BRA, one block
48 * is chosen at random. Assume this block is b1. A new label L is put just
49 * before the N common instructions of block b2 (so this block is split
50 * into two). The BRA of b1 is changed into a BRA L. So dynamically the same
51 * instructions are executed in a slightly different order; yet the size of
52 * the code has become smaller.
56 STATIC int Scj; /* number of optimizations found */
62 #define DLINK(l1,l2) l1->l_next=l2; l2->l_prev=l1
65 STATIC bool same_instr(l1,l2)
68 /* See if l1 and l2 are the same instruction */
70 if (l1 == 0 || l2 == 0 || TYPE(l1) != TYPE(l2)) return FALSE;
71 if (INSTR(l1) != INSTR(l2)) return FALSE;
73 case OPSHORT: return SHORT(l1) == SHORT(l2);
74 case OPOFFSET: return OFFSET(l1) == OFFSET(l2);
75 case OPPROC: return PROC(l1) == PROC(l2);
76 case OPOBJECT: return OBJ(l1) == OBJ(l2);
77 case OPINSTRLAB: return INSTRLAB(l1) == INSTRLAB(l2);
78 case OPNO: return TRUE;
79 default: return FALSE;
85 STATIC line_p last_mnem(b)
88 /* Determine the last line of a list */
92 for (l = b->b_start; l->l_next != (line_p) 0; l = l->l_next);
93 while (l != (line_p) 0 && (INSTR(l) < sp_fmnem || INSTR(l) > sp_lmnem)) {
100 STATIC bool is_desirable(text)
103 /* We avoid to generate a BRAnch in the middle of some expression,
104 * as the code generator will write the contents of the fakestack
105 * to the real stack if it encounters a BRA. We do not avoid to
106 * split the parameter-pushing code of a subroutine call into two,
107 * as the parameters are pushed on the real stack anyway.
108 * So e.g. "LOL a ; LOL b; ADI" will not be split, but
109 * "LOL a; LOL b; CAL f" may be split.
114 int stack_diff,pop,push;
117 for (l = text; l != (line_p) 0; l = l->l_next) {
124 line_change(l,&ok,&pop,&push);
125 /* printf("instr %d, pop %d, push %d, ok %d\n",INSTR(l),pop,push,ok); */
126 if (!ok || (stack_diff -= pop) < 0) {
136 STATIC cp_loops(b1,b2)
139 /* Copy the loopset of b2 to b1 */
143 for (i = Lfirst(b2->b_loops); i != (Lindex) 0;
144 i = Lnext(i,b2->b_loops)) {
145 lp = (loop_p) Lelem(i);
146 Ladd(lp,&b1->b_loops);
151 STATIC jump_cross(l1,l2,b1,b2)
155 /* A cross-jump from block b2 to block b1 is found; the code in
156 * block b2 from line l2 up to the BRAnch is removed; block b1 is
157 * split into two; the second part consists of a new label
158 * followed by the code from l1 till the end of the block.
165 /* First adjust the control flow graph */
166 b = freshblock(); /* create a new basic block */
167 b->b_succ = b1->b_succ;
169 b1->b_succ = Lempty_set(); Ladd(b,&b1->b_succ);
171 Ldeleteset(b2->b_succ); b2->b_succ = Lempty_set(); Ladd(b,&b2->b_succ);
172 /* PRED(b) = {b1,b2} */
173 b->b_pred = Lempty_set(); Ladd(b1,&b->b_pred); Ladd(b2,&b->b_pred);
174 /* PRED(SUCC(b)) := PRED(SUCC(b)) - {b1,b2} + {b} */
175 assert(Lnrelems(b->b_succ) == 1);
176 s = (bblock_p) Lelem(Lfirst(b->b_succ));
177 Lremove(b1,&s->b_pred); Lremove(b2,&s->b_pred); Ladd(b,&s->b_pred);
179 b->b_idom = common_dom(b1,b2);
180 b->b_flags = b1->b_flags;
181 b->b_next = b1->b_next;
184 /* Now adjust the EM text */
186 while (l && INSTR(l) == op_lab) {
190 if (l == (line_p) 0) {
191 b1->b_start = (line_p) 0;
193 l->l_next = (line_p) 0;
195 if (INSTR(l1) == op_lab) {
199 l = newline(OPINSTRLAB);
201 INSTRLAB(l) = freshlabel();
205 for (l = l2; INSTR(l) != op_bra;) {
206 line_p next = l->l_next;
208 assert (l != (line_p) 0);
212 INSTRLAB(l) = INSTRLAB(b->b_start);
216 STATIC bool try_tail(b1,b2)
219 /* See if b1 and b2 end on the same sequence of instructions */
222 bblock_p b = (bblock_p) 0;
224 /* printf("try block %d and %d\n",b1->b_id,b2->b_id); */
226 if (b1->b_start == (line_p) 0 || b2->b_start == (line_p) 0) return FALSE;
229 if (l1 == (line_p) 0 || l2 == (line_p) 0) return FALSE;
230 /* printf("consider:\n"); showinstr(l1); showinstr(l2); */
231 if (INSTR(l1) == op_bra) {
235 if (INSTR(l2) == op_bra) {
239 assert(b != (bblock_p) 0);
240 while(same_instr(l1,l2)) {
244 /* printf("consider:\n"); showinstr(l1); showinstr(l2); */
247 l1 = (l1 == 0 ? b1->b_start : l1->l_next);
248 l2 = (l2 == 0 ? b2->b_start : l2->l_next);
249 if (is_desirable(l1)) {
251 jump_cross(l2,l1,b2,b1);
254 jump_cross(l1,l2,b1,b2);
265 STATIC bool try_pred(b)
268 /* See if there is any pair (b1,b2), both in PRED(b) for
269 * which we can perform cross jumping.
272 register bblock_p b1,b2;
276 for (i = Lfirst(s); i != (Lindex) 0; i = Lnext(i,s)) {
277 b1 = (bblock_p) Lelem(i);
278 if (Lnrelems(b1->b_succ) != 1) continue;
279 for (j = Lfirst(s); j != (Lindex) 0; j = Lnext(j,s)) {
280 b2 = (bblock_p) Lelem(j);
281 if (b1 != b2 && Lnrelems(b2->b_succ) == 1) {
282 if (try_tail(b1,b2)) return TRUE;
294 /* Perform cross jumping for procedure p.
295 * In case cases a cross-jumping optimization which give
296 * new opportunities for further cross-jumping optimizations.
297 * Hence we repeat the whole process for the entire procedure,
298 * untill we find no further optimizations.
304 if (IS_ENTERED_WITH_GTO(p)) return;
308 while (b != (bblock_p) 0) {
323 go(argc,argv,no_action,cj_optimize,no_action,no_action);
324 report("cross jumps",Scj);
334 extern char em_mnem[]; /* The mnemonics of the EM instructions. */
336 STATIC showinstr(lnp) line_p lnp; {
338 /* Makes the instruction in `lnp' human readable. Only lines that
339 * can occur in expressions that are going to be eliminated are
342 if (lnp == 0) return;
343 if (INSTR(lnp) < sp_fmnem || INSTR(lnp) > sp_lmnem) {
348 printf("\t%s", &em_mnem[4 * (INSTR(lnp)-sp_fmnem)]);
353 printf(" %d", SHORT(lnp)); break;
355 printf(" %d", OBJ(lnp)->o_id); break;
357 printf(" %ld", OFFSET(lnp)); break;