]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lcm.c
Delete SEQUENCE rtl usage outside of reorg and ssa passes.
[thirdparty/gcc.git] / gcc / lcm.c
CommitLineData
f4e72d6e 1/* Generic partial redundancy elimination with lazy code motion support.
272cdf58 2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
d2ecda27 3
1322177d 4This file is part of GCC.
d2ecda27 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
d2ecda27 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
d2ecda27
JL
15
16You should have received a copy of the GNU General Public License
1322177d
LB
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
d2ecda27
JL
20
21/* These routines are meant to be used by various optimization
b3bb6456 22 passes which can be modeled as lazy code motion problems.
d2ecda27
JL
23 Including, but not limited to:
24
25 * Traditional partial redundancy elimination.
26
27 * Placement of caller/caller register save/restores.
28
29 * Load/store motion.
30
31 * Copy motion.
32
33 * Conversion of flat register files to a stacked register
34 model.
35
36 * Dead load/store elimination.
37
38 These routines accept as input:
39
40 * Basic block information (number of blocks, lists of
41 predecessors and successors). Note the granularity
42 does not need to be basic block, they could be statements
43 or functions.
44
45 * Bitmaps of local properties (computed, transparent and
46 anticipatable expressions).
47
48 The output of these routines is bitmap of redundant computations
49 and a bitmap of optimal placement points. */
50
51
52#include "config.h"
53#include "system.h"
d2ecda27
JL
54#include "rtl.h"
55#include "regs.h"
56#include "hard-reg-set.h"
57#include "flags.h"
58#include "real.h"
59#include "insn-config.h"
60#include "recog.h"
61#include "basic-block.h"
81b40b72 62#include "output.h"
9f09b1f2 63#include "tm_p.h"
f4e72d6e 64
9f09b1f2
R
65/* We want target macros for the mode switching code to be able to refer
66 to instruction attribute values. */
67#include "insn-attr.h"
d2ecda27 68
a42cd965 69/* Edge based LCM routines. */
f4e72d6e
RK
70static void compute_antinout_edge PARAMS ((sbitmap *, sbitmap *,
71 sbitmap *, sbitmap *));
72static void compute_earliest PARAMS ((struct edge_list *, int,
73 sbitmap *, sbitmap *,
74 sbitmap *, sbitmap *,
75 sbitmap *));
76static void compute_laterin PARAMS ((struct edge_list *, sbitmap *,
77 sbitmap *, sbitmap *,
78 sbitmap *));
79static void compute_insert_delete PARAMS ((struct edge_list *edge_list,
80 sbitmap *, sbitmap *,
81 sbitmap *, sbitmap *,
82 sbitmap *));
a42cd965
AM
83
84/* Edge based LCM routines on a reverse flowgraph. */
f4e72d6e
RK
85static void compute_farthest PARAMS ((struct edge_list *, int,
86 sbitmap *, sbitmap *,
87 sbitmap*, sbitmap *,
88 sbitmap *));
89static void compute_nearerout PARAMS ((struct edge_list *, sbitmap *,
90 sbitmap *, sbitmap *,
91 sbitmap *));
92static void compute_rev_insert_delete PARAMS ((struct edge_list *edge_list,
93 sbitmap *, sbitmap *,
94 sbitmap *, sbitmap *,
95 sbitmap *));
a42cd965
AM
96\f
97/* Edge based lcm routines. */
9ca88d5a 98
b3bb6456
AJ
99/* Compute expression anticipatability at entrance and exit of each block.
100 This is done based on the flow graph, and not on the pred-succ lists.
a42cd965 101 Other than that, its pretty much identical to compute_antinout. */
d2ecda27
JL
102
103static void
a42cd965 104compute_antinout_edge (antloc, transp, antin, antout)
d2ecda27
JL
105 sbitmap *antloc;
106 sbitmap *transp;
107 sbitmap *antin;
108 sbitmap *antout;
109{
e0082a72 110 basic_block bb;
a42cd965 111 edge e;
274969ea
MM
112 basic_block *worklist, *qin, *qout, *qend;
113 unsigned int qlen;
9ca88d5a 114
bd0eaec2
JL
115 /* Allocate a worklist array/queue. Entries are only added to the
116 list if they were not already on the list. So the size is
117 bounded by the number of basic blocks. */
274969ea 118 qin = qout = worklist
0b17ab2f 119 = (basic_block *) xmalloc (sizeof (basic_block) * n_basic_blocks);
d2ecda27 120
bd0eaec2
JL
121 /* We want a maximal solution, so make an optimistic initialization of
122 ANTIN. */
d55bc081 123 sbitmap_vector_ones (antin, last_basic_block);
d2ecda27 124
ce724250
JL
125 /* Put every block on the worklist; this is necessary because of the
126 optimistic initialization of ANTIN above. */
e0082a72 127 FOR_EACH_BB_REVERSE (bb)
d2ecda27 128 {
e0082a72
ZD
129 *qin++ =bb;
130 bb->aux = bb;
bd0eaec2 131 }
b3bb6456 132
274969ea 133 qin = worklist;
0b17ab2f
RH
134 qend = &worklist[n_basic_blocks];
135 qlen = n_basic_blocks;
d2ecda27 136
ce724250
JL
137 /* Mark blocks which are predecessors of the exit block so that we
138 can easily identify them below. */
139 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
140 e->src->aux = EXIT_BLOCK_PTR;
141
bd0eaec2 142 /* Iterate until the worklist is empty. */
274969ea 143 while (qlen)
bd0eaec2
JL
144 {
145 /* Take the first entry off the worklist. */
e0082a72 146 bb = *qout++;
274969ea 147 qlen--;
9ca88d5a 148
274969ea 149 if (qout >= qend)
e11e816e 150 qout = worklist;
d2ecda27 151
e0082a72 152 if (bb->aux == EXIT_BLOCK_PTR)
f4e72d6e
RK
153 /* Do not clear the aux field for blocks which are predecessors of
154 the EXIT block. That way we never add then to the worklist
155 again. */
e0082a72 156 sbitmap_zero (antout[bb->index]);
bd0eaec2
JL
157 else
158 {
159 /* Clear the aux field of this block so that it can be added to
160 the worklist again if necessary. */
e0082a72
ZD
161 bb->aux = NULL;
162 sbitmap_intersection_of_succs (antout[bb->index], antin, bb->index);
bd0eaec2 163 }
a42cd965 164
e0082a72
ZD
165 if (sbitmap_a_or_b_and_c_cg (antin[bb->index], antloc[bb->index],
166 transp[bb->index], antout[bb->index]))
f4e72d6e
RK
167 /* If the in state of this block changed, then we need
168 to add the predecessors of this block to the worklist
169 if they are not already on the worklist. */
e0082a72 170 for (e = bb->pred; e; e = e->pred_next)
f4e72d6e 171 if (!e->src->aux && e->src != ENTRY_BLOCK_PTR)
d2ecda27 172 {
274969ea 173 *qin++ = e->src;
f4e72d6e 174 e->src->aux = e;
274969ea
MM
175 qlen++;
176 if (qin >= qend)
e11e816e 177 qin = worklist;
d2ecda27 178 }
d2ecda27 179 }
f4e72d6e 180
108c1afc
RH
181 clear_aux_for_edges ();
182 clear_aux_for_blocks ();
274969ea 183 free (worklist);
d2ecda27
JL
184}
185
a42cd965 186/* Compute the earliest vector for edge based lcm. */
f4e72d6e 187
d2ecda27 188static void
a42cd965
AM
189compute_earliest (edge_list, n_exprs, antin, antout, avout, kill, earliest)
190 struct edge_list *edge_list;
d2ecda27 191 int n_exprs;
a42cd965 192 sbitmap *antin, *antout, *avout, *kill, *earliest;
d2ecda27 193{
a42cd965 194 sbitmap difference, temp_bitmap;
b3bb6456 195 int x, num_edges;
a42cd965 196 basic_block pred, succ;
d2ecda27 197
a42cd965 198 num_edges = NUM_EDGES (edge_list);
d2ecda27 199
a42cd965
AM
200 difference = sbitmap_alloc (n_exprs);
201 temp_bitmap = sbitmap_alloc (n_exprs);
d2ecda27 202
a42cd965 203 for (x = 0; x < num_edges; x++)
d2ecda27 204 {
a42cd965
AM
205 pred = INDEX_EDGE_PRED_BB (edge_list, x);
206 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
207 if (pred == ENTRY_BLOCK_PTR)
0b17ab2f 208 sbitmap_copy (earliest[x], antin[succ->index]);
a42cd965 209 else
e11e816e 210 {
81b40b72 211 if (succ == EXIT_BLOCK_PTR)
f4e72d6e 212 sbitmap_zero (earliest[x]);
a42cd965 213 else
d2ecda27 214 {
0b17ab2f
RH
215 sbitmap_difference (difference, antin[succ->index],
216 avout[pred->index]);
217 sbitmap_not (temp_bitmap, antout[pred->index]);
f4e72d6e 218 sbitmap_a_and_b_or_c (earliest[x], difference,
0b17ab2f 219 kill[pred->index], temp_bitmap);
d2ecda27
JL
220 }
221 }
d2ecda27 222 }
f4e72d6e 223
76ac938b
MH
224 sbitmap_free (temp_bitmap);
225 sbitmap_free (difference);
d2ecda27
JL
226}
227
bd0eaec2
JL
228/* later(p,s) is dependent on the calculation of laterin(p).
229 laterin(p) is dependent on the calculation of later(p2,p).
230
231 laterin(ENTRY) is defined as all 0's
232 later(ENTRY, succs(ENTRY)) are defined using laterin(ENTRY)
233 laterin(succs(ENTRY)) is defined by later(ENTRY, succs(ENTRY)).
234
235 If we progress in this manner, starting with all basic blocks
236 in the work list, anytime we change later(bb), we need to add
237 succs(bb) to the worklist if they are not already on the worklist.
238
239 Boundary conditions:
240
241 We prime the worklist all the normal basic blocks. The ENTRY block can
242 never be added to the worklist since it is never the successor of any
243 block. We explicitly prevent the EXIT block from being added to the
244 worklist.
245
246 We optimistically initialize LATER. That is the only time this routine
247 will compute LATER for an edge out of the entry block since the entry
248 block is never on the worklist. Thus, LATERIN is neither used nor
249 computed for the ENTRY block.
250
251 Since the EXIT block is never added to the worklist, we will neither
252 use nor compute LATERIN for the exit block. Edges which reach the
253 EXIT block are handled in the normal fashion inside the loop. However,
254 the insertion/deletion computation needs LATERIN(EXIT), so we have
255 to compute it. */
b3bb6456 256
d2ecda27 257static void
bd0eaec2 258compute_laterin (edge_list, earliest, antloc, later, laterin)
a42cd965 259 struct edge_list *edge_list;
a42cd965 260 sbitmap *earliest, *antloc, *later, *laterin;
d2ecda27 261{
e0082a72 262 int num_edges, i;
bd0eaec2 263 edge e;
e0082a72 264 basic_block *worklist, *qin, *qout, *qend, bb;
274969ea 265 unsigned int qlen;
d2ecda27 266
a42cd965 267 num_edges = NUM_EDGES (edge_list);
d2ecda27 268
bd0eaec2
JL
269 /* Allocate a worklist array/queue. Entries are only added to the
270 list if they were not already on the list. So the size is
271 bounded by the number of basic blocks. */
274969ea 272 qin = qout = worklist
0b17ab2f 273 = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
bd0eaec2
JL
274
275 /* Initialize a mapping from each edge to its index. */
276 for (i = 0; i < num_edges; i++)
63408827 277 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
bd0eaec2
JL
278
279 /* We want a maximal solution, so initially consider LATER true for
280 all edges. This allows propagation through a loop since the incoming
281 loop edge will have LATER set, so if all the other incoming edges
282 to the loop are set, then LATERIN will be set for the head of the
283 loop.
284
285 If the optimistic setting of LATER on that edge was incorrect (for
286 example the expression is ANTLOC in a block within the loop) then
287 this algorithm will detect it when we process the block at the head
288 of the optimistic edge. That will requeue the affected blocks. */
289 sbitmap_vector_ones (later, num_edges);
290
89e606c9
JL
291 /* Note that even though we want an optimistic setting of LATER, we
292 do not want to be overly optimistic. Consider an outgoing edge from
293 the entry block. That edge should always have a LATER value the
294 same as EARLIEST for that edge. */
295 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
f4e72d6e 296 sbitmap_copy (later[(size_t) e->aux], earliest[(size_t) e->aux]);
89e606c9 297
bd0eaec2
JL
298 /* Add all the blocks to the worklist. This prevents an early exit from
299 the loop given our optimistic initialization of LATER above. */
e0082a72 300 FOR_EACH_BB (bb)
d2ecda27 301 {
e0082a72
ZD
302 *qin++ = bb;
303 bb->aux = bb;
a42cd965 304 }
274969ea
MM
305 qin = worklist;
306 /* Note that we do not use the last allocated element for our queue,
307 as EXIT_BLOCK is never inserted into it. In fact the above allocation
0b17ab2f
RH
308 of n_basic_blocks + 1 elements is not encessary. */
309 qend = &worklist[n_basic_blocks];
310 qlen = n_basic_blocks;
a42cd965 311
bd0eaec2 312 /* Iterate until the worklist is empty. */
274969ea 313 while (qlen)
a42cd965 314 {
bd0eaec2 315 /* Take the first entry off the worklist. */
e0082a72
ZD
316 bb = *qout++;
317 bb->aux = NULL;
274969ea
MM
318 qlen--;
319 if (qout >= qend)
e11e816e 320 qout = worklist;
bd0eaec2
JL
321
322 /* Compute the intersection of LATERIN for each incoming edge to B. */
e0082a72
ZD
323 sbitmap_ones (laterin[bb->index]);
324 for (e = bb->pred; e != NULL; e = e->pred_next)
325 sbitmap_a_and_b (laterin[bb->index], laterin[bb->index], later[(size_t)e->aux]);
bd0eaec2
JL
326
327 /* Calculate LATER for all outgoing edges. */
e0082a72 328 for (e = bb->succ; e != NULL; e = e->succ_next)
b47374fa 329 if (sbitmap_union_of_diff_cg (later[(size_t) e->aux],
0b17ab2f
RH
330 earliest[(size_t) e->aux],
331 laterin[e->src->index],
332 antloc[e->src->index])
f4e72d6e
RK
333 /* If LATER for an outgoing edge was changed, then we need
334 to add the target of the outgoing edge to the worklist. */
335 && e->dest != EXIT_BLOCK_PTR && e->dest->aux == 0)
336 {
274969ea 337 *qin++ = e->dest;
f4e72d6e 338 e->dest->aux = e;
274969ea
MM
339 qlen++;
340 if (qin >= qend)
341 qin = worklist;
f4e72d6e 342 }
d2ecda27
JL
343 }
344
bd0eaec2
JL
345 /* Computation of insertion and deletion points requires computing LATERIN
346 for the EXIT block. We allocated an extra entry in the LATERIN array
347 for just this purpose. */
d55bc081 348 sbitmap_ones (laterin[last_basic_block]);
bd0eaec2 349 for (e = EXIT_BLOCK_PTR->pred; e != NULL; e = e->pred_next)
d55bc081
ZD
350 sbitmap_a_and_b (laterin[last_basic_block],
351 laterin[last_basic_block],
63408827 352 later[(size_t) e->aux]);
bd0eaec2 353
108c1afc 354 clear_aux_for_edges ();
274969ea 355 free (worklist);
d2ecda27
JL
356}
357
a42cd965 358/* Compute the insertion and deletion points for edge based LCM. */
f4e72d6e 359
a42cd965
AM
360static void
361compute_insert_delete (edge_list, antloc, later, laterin,
362 insert, delete)
363 struct edge_list *edge_list;
364 sbitmap *antloc, *later, *laterin, *insert, *delete;
365{
366 int x;
e0082a72 367 basic_block bb;
d2ecda27 368
e0082a72
ZD
369 FOR_EACH_BB (bb)
370 sbitmap_difference (delete[bb->index], antloc[bb->index], laterin[bb->index]);
b3bb6456 371
a42cd965
AM
372 for (x = 0; x < NUM_EDGES (edge_list); x++)
373 {
374 basic_block b = INDEX_EDGE_SUCC_BB (edge_list, x);
f4e72d6e 375
a42cd965 376 if (b == EXIT_BLOCK_PTR)
d55bc081 377 sbitmap_difference (insert[x], later[x], laterin[last_basic_block]);
a42cd965 378 else
0b17ab2f 379 sbitmap_difference (insert[x], later[x], laterin[b->index]);
a42cd965
AM
380 }
381}
d2ecda27 382
f4e72d6e
RK
383/* Given local properties TRANSP, ANTLOC, AVOUT, KILL return the insert and
384 delete vectors for edge based LCM. Returns an edgelist which is used to
385 map the insert vector to what edge an expression should be inserted on. */
d2ecda27 386
a42cd965
AM
387struct edge_list *
388pre_edge_lcm (file, n_exprs, transp, avloc, antloc, kill, insert, delete)
4b66e1c0 389 FILE *file ATTRIBUTE_UNUSED;
d2ecda27 390 int n_exprs;
a42cd965
AM
391 sbitmap *transp;
392 sbitmap *avloc;
d2ecda27 393 sbitmap *antloc;
a42cd965
AM
394 sbitmap *kill;
395 sbitmap **insert;
396 sbitmap **delete;
d2ecda27 397{
a42cd965
AM
398 sbitmap *antin, *antout, *earliest;
399 sbitmap *avin, *avout;
400 sbitmap *later, *laterin;
401 struct edge_list *edge_list;
402 int num_edges;
d2ecda27 403
a42cd965
AM
404 edge_list = create_edge_list ();
405 num_edges = NUM_EDGES (edge_list);
d2ecda27 406
a42cd965
AM
407#ifdef LCM_DEBUG_INFO
408 if (file)
d2ecda27 409 {
a42cd965
AM
410 fprintf (file, "Edge List:\n");
411 verify_edge_list (file, edge_list);
412 print_edge_list (file, edge_list);
d55bc081
ZD
413 dump_sbitmap_vector (file, "transp", "", transp, last_basic_block);
414 dump_sbitmap_vector (file, "antloc", "", antloc, last_basic_block);
415 dump_sbitmap_vector (file, "avloc", "", avloc, last_basic_block);
416 dump_sbitmap_vector (file, "kill", "", kill, last_basic_block);
d2ecda27 417 }
a42cd965 418#endif
d2ecda27 419
a42cd965 420 /* Compute global availability. */
d55bc081
ZD
421 avin = sbitmap_vector_alloc (last_basic_block, n_exprs);
422 avout = sbitmap_vector_alloc (last_basic_block, n_exprs);
a42cd965 423 compute_available (avloc, kill, avout, avin);
5a660bff 424 sbitmap_vector_free (avin);
d2ecda27 425
a42cd965 426 /* Compute global anticipatability. */
d55bc081
ZD
427 antin = sbitmap_vector_alloc (last_basic_block, n_exprs);
428 antout = sbitmap_vector_alloc (last_basic_block, n_exprs);
a42cd965 429 compute_antinout_edge (antloc, transp, antin, antout);
d2ecda27 430
a42cd965
AM
431#ifdef LCM_DEBUG_INFO
432 if (file)
d2ecda27 433 {
d55bc081
ZD
434 dump_sbitmap_vector (file, "antin", "", antin, last_basic_block);
435 dump_sbitmap_vector (file, "antout", "", antout, last_basic_block);
d2ecda27 436 }
a42cd965 437#endif
d2ecda27 438
a42cd965
AM
439 /* Compute earliestness. */
440 earliest = sbitmap_vector_alloc (num_edges, n_exprs);
441 compute_earliest (edge_list, n_exprs, antin, antout, avout, kill, earliest);
d2ecda27 442
a42cd965
AM
443#ifdef LCM_DEBUG_INFO
444 if (file)
445 dump_sbitmap_vector (file, "earliest", "", earliest, num_edges);
446#endif
d2ecda27 447
5a660bff
DB
448 sbitmap_vector_free (antout);
449 sbitmap_vector_free (antin);
450 sbitmap_vector_free (avout);
d2ecda27 451
a42cd965 452 later = sbitmap_vector_alloc (num_edges, n_exprs);
f4e72d6e 453
a42cd965 454 /* Allocate an extra element for the exit block in the laterin vector. */
d55bc081 455 laterin = sbitmap_vector_alloc (last_basic_block + 1, n_exprs);
bd0eaec2
JL
456 compute_laterin (edge_list, earliest, antloc, later, laterin);
457
a42cd965
AM
458#ifdef LCM_DEBUG_INFO
459 if (file)
460 {
d55bc081 461 dump_sbitmap_vector (file, "laterin", "", laterin, last_basic_block + 1);
a42cd965
AM
462 dump_sbitmap_vector (file, "later", "", later, num_edges);
463 }
464#endif
d2ecda27 465
5a660bff 466 sbitmap_vector_free (earliest);
a42cd965
AM
467
468 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
d55bc081 469 *delete = sbitmap_vector_alloc (last_basic_block, n_exprs);
a42cd965 470 compute_insert_delete (edge_list, antloc, later, laterin, *insert, *delete);
d2ecda27 471
5a660bff
DB
472 sbitmap_vector_free (laterin);
473 sbitmap_vector_free (later);
a42cd965
AM
474
475#ifdef LCM_DEBUG_INFO
476 if (file)
d2ecda27 477 {
a42cd965 478 dump_sbitmap_vector (file, "pre_insert_map", "", *insert, num_edges);
f4e72d6e 479 dump_sbitmap_vector (file, "pre_delete_map", "", *delete,
d55bc081 480 last_basic_block);
d2ecda27 481 }
a42cd965 482#endif
d2ecda27 483
a42cd965
AM
484 return edge_list;
485}
9ca88d5a
DB
486
487/* Compute the AVIN and AVOUT vectors from the AVLOC and KILL vectors.
488 Return the number of passes we performed to iterate to a solution. */
489
bd0eaec2 490void
a42cd965 491compute_available (avloc, kill, avout, avin)
9ca88d5a 492 sbitmap *avloc, *kill, *avout, *avin;
d2ecda27 493{
9ca88d5a 494 edge e;
e0082a72 495 basic_block *worklist, *qin, *qout, *qend, bb;
9ca88d5a
DB
496 unsigned int qlen;
497
498 /* Allocate a worklist array/queue. Entries are only added to the
499 list if they were not already on the list. So the size is
500 bounded by the number of basic blocks. */
501 qin = qout = worklist
0b17ab2f 502 = (basic_block *) xmalloc (sizeof (basic_block) * n_basic_blocks);
9ca88d5a
DB
503
504 /* We want a maximal solution. */
d55bc081 505 sbitmap_vector_ones (avout, last_basic_block);
9ca88d5a
DB
506
507 /* Put every block on the worklist; this is necessary because of the
508 optimistic initialization of AVOUT above. */
e0082a72 509 FOR_EACH_BB (bb)
9ca88d5a 510 {
e0082a72
ZD
511 *qin++ = bb;
512 bb->aux = bb;
9ca88d5a
DB
513 }
514
515 qin = worklist;
0b17ab2f
RH
516 qend = &worklist[n_basic_blocks];
517 qlen = n_basic_blocks;
9ca88d5a
DB
518
519 /* Mark blocks which are successors of the entry block so that we
520 can easily identify them below. */
521 for (e = ENTRY_BLOCK_PTR->succ; e; e = e->succ_next)
522 e->dest->aux = ENTRY_BLOCK_PTR;
523
524 /* Iterate until the worklist is empty. */
525 while (qlen)
526 {
527 /* Take the first entry off the worklist. */
e0082a72 528 bb = *qout++;
9ca88d5a
DB
529 qlen--;
530
531 if (qout >= qend)
e11e816e 532 qout = worklist;
9ca88d5a
DB
533
534 /* If one of the predecessor blocks is the ENTRY block, then the
535 intersection of avouts is the null set. We can identify such blocks
536 by the special value in the AUX field in the block structure. */
e0082a72 537 if (bb->aux == ENTRY_BLOCK_PTR)
9ca88d5a
DB
538 /* Do not clear the aux field for blocks which are successors of the
539 ENTRY block. That way we never add then to the worklist again. */
e0082a72 540 sbitmap_zero (avin[bb->index]);
9ca88d5a
DB
541 else
542 {
543 /* Clear the aux field of this block so that it can be added to
544 the worklist again if necessary. */
e0082a72
ZD
545 bb->aux = NULL;
546 sbitmap_intersection_of_preds (avin[bb->index], avout, bb->index);
9ca88d5a
DB
547 }
548
e0082a72 549 if (sbitmap_union_of_diff_cg (avout[bb->index], avloc[bb->index], avin[bb->index], kill[bb->index]))
9ca88d5a
DB
550 /* If the out state of this block changed, then we need
551 to add the successors of this block to the worklist
552 if they are not already on the worklist. */
e0082a72 553 for (e = bb->succ; e; e = e->succ_next)
9ca88d5a
DB
554 if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR)
555 {
556 *qin++ = e->dest;
557 e->dest->aux = e;
558 qlen++;
559
560 if (qin >= qend)
e11e816e 561 qin = worklist;
9ca88d5a
DB
562 }
563 }
564
565 clear_aux_for_edges ();
566 clear_aux_for_blocks ();
567 free (worklist);
d2ecda27
JL
568}
569
a42cd965 570/* Compute the farthest vector for edge based lcm. */
f4e72d6e 571
d2ecda27 572static void
b3bb6456 573compute_farthest (edge_list, n_exprs, st_avout, st_avin, st_antin,
a42cd965
AM
574 kill, farthest)
575 struct edge_list *edge_list;
d2ecda27 576 int n_exprs;
a42cd965 577 sbitmap *st_avout, *st_avin, *st_antin, *kill, *farthest;
d2ecda27 578{
a42cd965 579 sbitmap difference, temp_bitmap;
b3bb6456 580 int x, num_edges;
a42cd965 581 basic_block pred, succ;
d2ecda27 582
a42cd965 583 num_edges = NUM_EDGES (edge_list);
d2ecda27 584
a42cd965
AM
585 difference = sbitmap_alloc (n_exprs);
586 temp_bitmap = sbitmap_alloc (n_exprs);
d2ecda27 587
a42cd965 588 for (x = 0; x < num_edges; x++)
d2ecda27 589 {
a42cd965
AM
590 pred = INDEX_EDGE_PRED_BB (edge_list, x);
591 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
592 if (succ == EXIT_BLOCK_PTR)
0b17ab2f 593 sbitmap_copy (farthest[x], st_avout[pred->index]);
a42cd965 594 else
d2ecda27 595 {
a42cd965 596 if (pred == ENTRY_BLOCK_PTR)
f4e72d6e 597 sbitmap_zero (farthest[x]);
a42cd965
AM
598 else
599 {
0b17ab2f
RH
600 sbitmap_difference (difference, st_avout[pred->index],
601 st_antin[succ->index]);
602 sbitmap_not (temp_bitmap, st_avin[succ->index]);
b3bb6456 603 sbitmap_a_and_b_or_c (farthest[x], difference,
0b17ab2f 604 kill[succ->index], temp_bitmap);
a42cd965 605 }
d2ecda27 606 }
d2ecda27 607 }
f4e72d6e 608
76ac938b
MH
609 sbitmap_free (temp_bitmap);
610 sbitmap_free (difference);
d2ecda27
JL
611}
612
bd0eaec2
JL
613/* Compute nearer and nearerout vectors for edge based lcm.
614
615 This is the mirror of compute_laterin, additional comments on the
616 implementation can be found before compute_laterin. */
617
d2ecda27 618static void
bd0eaec2 619compute_nearerout (edge_list, farthest, st_avloc, nearer, nearerout)
a42cd965 620 struct edge_list *edge_list;
a42cd965 621 sbitmap *farthest, *st_avloc, *nearer, *nearerout;
d2ecda27 622{
e0082a72 623 int num_edges, i;
bd0eaec2 624 edge e;
e0082a72 625 basic_block *worklist, *tos, bb;
d2ecda27 626
a42cd965 627 num_edges = NUM_EDGES (edge_list);
d2ecda27 628
bd0eaec2
JL
629 /* Allocate a worklist array/queue. Entries are only added to the
630 list if they were not already on the list. So the size is
631 bounded by the number of basic blocks. */
f4e72d6e 632 tos = worklist
0b17ab2f 633 = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
d2ecda27 634
bd0eaec2
JL
635 /* Initialize NEARER for each edge and build a mapping from an edge to
636 its index. */
637 for (i = 0; i < num_edges; i++)
63408827 638 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
a42cd965 639
bd0eaec2
JL
640 /* We want a maximal solution. */
641 sbitmap_vector_ones (nearer, num_edges);
642
89e606c9
JL
643 /* Note that even though we want an optimistic setting of NEARER, we
644 do not want to be overly optimistic. Consider an incoming edge to
645 the exit block. That edge should always have a NEARER value the
646 same as FARTHEST for that edge. */
647 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
e5b7ca32 648 sbitmap_copy (nearer[(size_t)e->aux], farthest[(size_t)e->aux]);
89e606c9 649
bd0eaec2
JL
650 /* Add all the blocks to the worklist. This prevents an early exit
651 from the loop given our optimistic initialization of NEARER. */
e0082a72 652 FOR_EACH_BB (bb)
d2ecda27 653 {
e0082a72
ZD
654 *tos++ = bb;
655 bb->aux = bb;
a42cd965 656 }
b3bb6456 657
bd0eaec2
JL
658 /* Iterate until the worklist is empty. */
659 while (tos != worklist)
a42cd965 660 {
bd0eaec2 661 /* Take the first entry off the worklist. */
e0082a72
ZD
662 bb = *--tos;
663 bb->aux = NULL;
bd0eaec2
JL
664
665 /* Compute the intersection of NEARER for each outgoing edge from B. */
e0082a72
ZD
666 sbitmap_ones (nearerout[bb->index]);
667 for (e = bb->succ; e != NULL; e = e->succ_next)
668 sbitmap_a_and_b (nearerout[bb->index], nearerout[bb->index],
63408827 669 nearer[(size_t) e->aux]);
bd0eaec2
JL
670
671 /* Calculate NEARER for all incoming edges. */
e0082a72 672 for (e = bb->pred; e != NULL; e = e->pred_next)
b47374fa 673 if (sbitmap_union_of_diff_cg (nearer[(size_t) e->aux],
0b17ab2f
RH
674 farthest[(size_t) e->aux],
675 nearerout[e->dest->index],
676 st_avloc[e->dest->index])
f4e72d6e
RK
677 /* If NEARER for an incoming edge was changed, then we need
678 to add the source of the incoming edge to the worklist. */
679 && e->src != ENTRY_BLOCK_PTR && e->src->aux == 0)
680 {
681 *tos++ = e->src;
682 e->src->aux = e;
683 }
a42cd965 684 }
d2ecda27 685
bd0eaec2
JL
686 /* Computation of insertion and deletion points requires computing NEAREROUT
687 for the ENTRY block. We allocated an extra entry in the NEAREROUT array
688 for just this purpose. */
d55bc081 689 sbitmap_ones (nearerout[last_basic_block]);
bd0eaec2 690 for (e = ENTRY_BLOCK_PTR->succ; e != NULL; e = e->succ_next)
d55bc081
ZD
691 sbitmap_a_and_b (nearerout[last_basic_block],
692 nearerout[last_basic_block],
63408827 693 nearer[(size_t) e->aux]);
bd0eaec2 694
108c1afc 695 clear_aux_for_edges ();
bd0eaec2 696 free (tos);
a42cd965 697}
d2ecda27 698
a42cd965 699/* Compute the insertion and deletion points for edge based LCM. */
f4e72d6e 700
d2ecda27 701static void
a42cd965
AM
702compute_rev_insert_delete (edge_list, st_avloc, nearer, nearerout,
703 insert, delete)
704 struct edge_list *edge_list;
705 sbitmap *st_avloc, *nearer, *nearerout, *insert, *delete;
d2ecda27 706{
a42cd965 707 int x;
e0082a72 708 basic_block bb;
d2ecda27 709
e0082a72
ZD
710 FOR_EACH_BB (bb)
711 sbitmap_difference (delete[bb->index], st_avloc[bb->index], nearerout[bb->index]);
b3bb6456 712
a42cd965 713 for (x = 0; x < NUM_EDGES (edge_list); x++)
d2ecda27 714 {
a42cd965
AM
715 basic_block b = INDEX_EDGE_PRED_BB (edge_list, x);
716 if (b == ENTRY_BLOCK_PTR)
d55bc081 717 sbitmap_difference (insert[x], nearer[x], nearerout[last_basic_block]);
d2ecda27 718 else
0b17ab2f 719 sbitmap_difference (insert[x], nearer[x], nearerout[b->index]);
d2ecda27 720 }
d2ecda27
JL
721}
722
b3bb6456 723/* Given local properties TRANSP, ST_AVLOC, ST_ANTLOC, KILL return the
a42cd965
AM
724 insert and delete vectors for edge based reverse LCM. Returns an
725 edgelist which is used to map the insert vector to what edge
726 an expression should be inserted on. */
d2ecda27 727
a42cd965 728struct edge_list *
b3bb6456 729pre_edge_rev_lcm (file, n_exprs, transp, st_avloc, st_antloc, kill,
a42cd965 730 insert, delete)
4b66e1c0 731 FILE *file ATTRIBUTE_UNUSED;
a42cd965
AM
732 int n_exprs;
733 sbitmap *transp;
734 sbitmap *st_avloc;
735 sbitmap *st_antloc;
736 sbitmap *kill;
737 sbitmap **insert;
738 sbitmap **delete;
d2ecda27 739{
a42cd965
AM
740 sbitmap *st_antin, *st_antout;
741 sbitmap *st_avout, *st_avin, *farthest;
742 sbitmap *nearer, *nearerout;
743 struct edge_list *edge_list;
4b66e1c0 744 int num_edges;
a42cd965
AM
745
746 edge_list = create_edge_list ();
747 num_edges = NUM_EDGES (edge_list);
748
d55bc081
ZD
749 st_antin = (sbitmap *) sbitmap_vector_alloc (last_basic_block, n_exprs);
750 st_antout = (sbitmap *) sbitmap_vector_alloc (last_basic_block, n_exprs);
751 sbitmap_vector_zero (st_antin, last_basic_block);
752 sbitmap_vector_zero (st_antout, last_basic_block);
a42cd965
AM
753 compute_antinout_edge (st_antloc, transp, st_antin, st_antout);
754
755 /* Compute global anticipatability. */
d55bc081
ZD
756 st_avout = sbitmap_vector_alloc (last_basic_block, n_exprs);
757 st_avin = sbitmap_vector_alloc (last_basic_block, n_exprs);
a42cd965
AM
758 compute_available (st_avloc, kill, st_avout, st_avin);
759
760#ifdef LCM_DEBUG_INFO
761 if (file)
762 {
763 fprintf (file, "Edge List:\n");
764 verify_edge_list (file, edge_list);
765 print_edge_list (file, edge_list);
d55bc081
ZD
766 dump_sbitmap_vector (file, "transp", "", transp, last_basic_block);
767 dump_sbitmap_vector (file, "st_avloc", "", st_avloc, last_basic_block);
768 dump_sbitmap_vector (file, "st_antloc", "", st_antloc, last_basic_block);
769 dump_sbitmap_vector (file, "st_antin", "", st_antin, last_basic_block);
770 dump_sbitmap_vector (file, "st_antout", "", st_antout, last_basic_block);
771 dump_sbitmap_vector (file, "st_kill", "", kill, last_basic_block);
a42cd965
AM
772 }
773#endif
d2ecda27 774
a42cd965
AM
775#ifdef LCM_DEBUG_INFO
776 if (file)
777 {
d55bc081
ZD
778 dump_sbitmap_vector (file, "st_avout", "", st_avout, last_basic_block);
779 dump_sbitmap_vector (file, "st_avin", "", st_avin, last_basic_block);
a42cd965
AM
780 }
781#endif
d2ecda27 782
a42cd965
AM
783 /* Compute farthestness. */
784 farthest = sbitmap_vector_alloc (num_edges, n_exprs);
b3bb6456 785 compute_farthest (edge_list, n_exprs, st_avout, st_avin, st_antin,
a42cd965
AM
786 kill, farthest);
787
788#ifdef LCM_DEBUG_INFO
789 if (file)
790 dump_sbitmap_vector (file, "farthest", "", farthest, num_edges);
791#endif
792
5a660bff
DB
793 sbitmap_vector_free (st_antin);
794 sbitmap_vector_free (st_antout);
795
796 sbitmap_vector_free (st_avin);
797 sbitmap_vector_free (st_avout);
a42cd965
AM
798
799 nearer = sbitmap_vector_alloc (num_edges, n_exprs);
f4e72d6e 800
a42cd965 801 /* Allocate an extra element for the entry block. */
d55bc081 802 nearerout = sbitmap_vector_alloc (last_basic_block + 1, n_exprs);
bd0eaec2 803 compute_nearerout (edge_list, farthest, st_avloc, nearer, nearerout);
a42cd965
AM
804
805#ifdef LCM_DEBUG_INFO
806 if (file)
d2ecda27 807 {
b3bb6456 808 dump_sbitmap_vector (file, "nearerout", "", nearerout,
d55bc081 809 last_basic_block + 1);
a42cd965 810 dump_sbitmap_vector (file, "nearer", "", nearer, num_edges);
d2ecda27 811 }
a42cd965
AM
812#endif
813
5a660bff 814 sbitmap_vector_free (farthest);
a42cd965
AM
815
816 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
d55bc081 817 *delete = sbitmap_vector_alloc (last_basic_block, n_exprs);
f4e72d6e
RK
818 compute_rev_insert_delete (edge_list, st_avloc, nearer, nearerout,
819 *insert, *delete);
a42cd965 820
5a660bff
DB
821 sbitmap_vector_free (nearerout);
822 sbitmap_vector_free (nearer);
a42cd965
AM
823
824#ifdef LCM_DEBUG_INFO
825 if (file)
826 {
827 dump_sbitmap_vector (file, "pre_insert_map", "", *insert, num_edges);
f4e72d6e 828 dump_sbitmap_vector (file, "pre_delete_map", "", *delete,
d55bc081 829 last_basic_block);
a42cd965
AM
830 }
831#endif
a42cd965 832 return edge_list;
d2ecda27 833}
9f09b1f2 834
f4e72d6e
RK
835/* Mode switching:
836
837 The algorithm for setting the modes consists of scanning the insn list
9f09b1f2
R
838 and finding all the insns which require a specific mode. Each insn gets
839 a unique struct seginfo element. These structures are inserted into a list
840 for each basic block. For each entity, there is an array of bb_info over
841 the flow graph basic blocks (local var 'bb_info'), and contains a list
842 of all insns within that basic block, in the order they are encountered.
843
844 For each entity, any basic block WITHOUT any insns requiring a specific
845 mode are given a single entry, without a mode. (Each basic block
846 in the flow graph must have at least one entry in the segment table.)
847
848 The LCM algorithm is then run over the flow graph to determine where to
849 place the sets to the highest-priority value in respect of first the first
850 insn in any one block. Any adjustments required to the transparancy
851 vectors are made, then the next iteration starts for the next-lower
852 priority mode, till for each entity all modes are exhasted.
853
854 More details are located in the code for optimize_mode_switching(). */
855
856/* This structure contains the information for each insn which requires
b3bb6456 857 either single or double mode to be set.
9f09b1f2 858 MODE is the mode this insn must be executed in.
1cca43ea
AO
859 INSN_PTR is the insn to be executed (may be the note that marks the
860 beginning of a basic block).
9f09b1f2
R
861 BBNUM is the flow graph basic block this insn occurs in.
862 NEXT is the next insn in the same basic block. */
b3bb6456 863struct seginfo
9f09b1f2
R
864{
865 int mode;
866 rtx insn_ptr;
867 int bbnum;
868 struct seginfo *next;
869 HARD_REG_SET regs_live;
870};
871
9ca88d5a 872struct bb_info
9f09b1f2
R
873{
874 struct seginfo *seginfo;
875 int computing;
876};
877
878/* These bitmaps are used for the LCM algorithm. */
879
c8d8ed65 880#ifdef OPTIMIZE_MODE_SWITCHING
9f09b1f2
R
881static sbitmap *antic;
882static sbitmap *transp;
883static sbitmap *comp;
884static sbitmap *delete;
885static sbitmap *insert;
886
1270c255 887static struct seginfo * new_seginfo PARAMS ((int, rtx, int, HARD_REG_SET));
9ca88d5a 888static void add_seginfo PARAMS ((struct bb_info *, struct seginfo *));
9f09b1f2
R
889static void reg_dies PARAMS ((rtx, HARD_REG_SET));
890static void reg_becomes_live PARAMS ((rtx, rtx, void *));
c8d8ed65
RK
891static void make_preds_opaque PARAMS ((basic_block, int));
892#endif
893\f
894#ifdef OPTIMIZE_MODE_SWITCHING
9f09b1f2
R
895
896/* This function will allocate a new BBINFO structure, initialized
1270c255 897 with the MODE, INSN, and basic block BB parameters. */
c8d8ed65 898
9f09b1f2
R
899static struct seginfo *
900new_seginfo (mode, insn, bb, regs_live)
901 int mode;
902 rtx insn;
903 int bb;
904 HARD_REG_SET regs_live;
905{
906 struct seginfo *ptr;
907 ptr = xmalloc (sizeof (struct seginfo));
908 ptr->mode = mode;
909 ptr->insn_ptr = insn;
910 ptr->bbnum = bb;
911 ptr->next = NULL;
912 COPY_HARD_REG_SET (ptr->regs_live, regs_live);
913 return ptr;
914}
915
b3bb6456 916/* Add a seginfo element to the end of a list.
9f09b1f2
R
917 HEAD is a pointer to the list beginning.
918 INFO is the structure to be linked in. */
c8d8ed65 919
9f09b1f2
R
920static void
921add_seginfo (head, info)
9ca88d5a 922 struct bb_info *head;
9f09b1f2
R
923 struct seginfo *info;
924{
925 struct seginfo *ptr;
926
927 if (head->seginfo == NULL)
928 head->seginfo = info;
929 else
930 {
931 ptr = head->seginfo;
932 while (ptr->next != NULL)
e11e816e 933 ptr = ptr->next;
9f09b1f2
R
934 ptr->next = info;
935 }
936}
937
938/* Make all predecessors of basic block B opaque, recursively, till we hit
939 some that are already non-transparent, or an edge where aux is set; that
940 denotes that a mode set is to be done on that edge.
941 J is the bit number in the bitmaps that corresponds to the entity that
942 we are currently handling mode-switching for. */
c8d8ed65 943
9f09b1f2
R
944static void
945make_preds_opaque (b, j)
946 basic_block b;
947 int j;
948{
949 edge e;
950
951 for (e = b->pred; e; e = e->pred_next)
952 {
953 basic_block pb = e->src;
f4e72d6e 954
0b17ab2f 955 if (e->aux || ! TEST_BIT (transp[pb->index], j))
9f09b1f2 956 continue;
f4e72d6e 957
0b17ab2f 958 RESET_BIT (transp[pb->index], j);
9f09b1f2
R
959 make_preds_opaque (pb, j);
960 }
961}
962
963/* Record in LIVE that register REG died. */
c8d8ed65 964
9f09b1f2
R
965static void
966reg_dies (reg, live)
967 rtx reg;
968 HARD_REG_SET live;
969{
f4e72d6e 970 int regno, nregs;
9f09b1f2
R
971
972 if (GET_CODE (reg) != REG)
973 return;
f4e72d6e 974
9f09b1f2
R
975 regno = REGNO (reg);
976 if (regno < FIRST_PSEUDO_REGISTER)
f4e72d6e
RK
977 for (nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1; nregs >= 0;
978 nregs--)
979 CLEAR_HARD_REG_BIT (live, regno + nregs);
9f09b1f2
R
980}
981
982/* Record in LIVE that register REG became live.
983 This is called via note_stores. */
c8d8ed65 984
9f09b1f2
R
985static void
986reg_becomes_live (reg, setter, live)
987 rtx reg;
988 rtx setter ATTRIBUTE_UNUSED;
989 void *live;
990{
f4e72d6e 991 int regno, nregs;
9f09b1f2
R
992
993 if (GET_CODE (reg) == SUBREG)
994 reg = SUBREG_REG (reg);
995
996 if (GET_CODE (reg) != REG)
997 return;
998
999 regno = REGNO (reg);
1000 if (regno < FIRST_PSEUDO_REGISTER)
f4e72d6e
RK
1001 for (nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg)) - 1; nregs >= 0;
1002 nregs--)
1003 SET_HARD_REG_BIT (* (HARD_REG_SET *) live, regno + nregs);
9f09b1f2
R
1004}
1005
97d36f45
RH
1006/* Find all insns that need a particular mode setting, and insert the
1007 necessary mode switches. Return true if we did work. */
f4e72d6e 1008
97d36f45 1009int
9f09b1f2 1010optimize_mode_switching (file)
97d36f45 1011 FILE *file;
9f09b1f2 1012{
9f09b1f2 1013 rtx insn;
e0082a72
ZD
1014 int e;
1015 basic_block bb;
9f09b1f2
R
1016 int need_commit = 0;
1017 sbitmap *kill;
1018 struct edge_list *edge_list;
8b60264b 1019 static const int num_modes[] = NUM_MODES_FOR_MODE_SWITCHING;
ca7558fc 1020#define N_ENTITIES ARRAY_SIZE (num_modes)
9f09b1f2 1021 int entity_map[N_ENTITIES];
9ca88d5a 1022 struct bb_info *bb_info[N_ENTITIES];
9f09b1f2
R
1023 int i, j;
1024 int n_entities;
1025 int max_num_modes = 0;
73991d6a 1026 bool emited = false;
272cdf58 1027 basic_block post_entry ATTRIBUTE_UNUSED, pre_exit ATTRIBUTE_UNUSED;
9f09b1f2 1028
38c1593d 1029 clear_bb_flags ();
e8eacc3f 1030
9f09b1f2 1031 for (e = N_ENTITIES - 1, n_entities = 0; e >= 0; e--)
f4e72d6e
RK
1032 if (OPTIMIZE_MODE_SWITCHING (e))
1033 {
81b40b72
R
1034 int entry_exit_extra = 0;
1035
1036 /* Create the list of segments within each basic block.
1037 If NORMAL_MODE is defined, allow for two extra
e11e816e 1038 blocks split from the entry and exit block. */
81b40b72
R
1039#ifdef NORMAL_MODE
1040 entry_exit_extra = 2;
1041#endif
f4e72d6e 1042 bb_info[n_entities]
81b40b72
R
1043 = (struct bb_info *) xcalloc (last_basic_block + entry_exit_extra,
1044 sizeof **bb_info);
f4e72d6e
RK
1045 entity_map[n_entities++] = e;
1046 if (num_modes[e] > max_num_modes)
1047 max_num_modes = num_modes[e];
1048 }
1049
9f09b1f2 1050 if (! n_entities)
97d36f45 1051 return 0;
9f09b1f2 1052
e8eacc3f 1053#ifdef NORMAL_MODE
81b40b72
R
1054 {
1055 /* Split the edge from the entry block and the fallthrough edge to the
1056 exit block, so that we can note that there NORMAL_MODE is supplied /
1057 required. */
1058 edge eg;
1059 post_entry = split_edge (ENTRY_BLOCK_PTR->succ);
1060 /* The only non-call predecessor at this stage is a block with a
1061 fallthrough edge; there can be at most one, but there could be
1062 none at all, e.g. when exit is called. */
1063 for (pre_exit = 0, eg = EXIT_BLOCK_PTR->pred; eg; eg = eg->pred_next)
1064 if (eg->flags & EDGE_FALLTHRU)
1065 {
1066 regset live_at_end = eg->src->global_live_at_end;
1067
1068 if (pre_exit)
1069 abort ();
1070 pre_exit = split_edge (eg);
1071 COPY_REG_SET (pre_exit->global_live_at_start, live_at_end);
1072 COPY_REG_SET (pre_exit->global_live_at_end, live_at_end);
1073 }
1074 }
e8eacc3f
AO
1075#endif
1076
9f09b1f2
R
1077 /* Create the bitmap vectors. */
1078
d55bc081
ZD
1079 antic = sbitmap_vector_alloc (last_basic_block, n_entities);
1080 transp = sbitmap_vector_alloc (last_basic_block, n_entities);
1081 comp = sbitmap_vector_alloc (last_basic_block, n_entities);
9f09b1f2 1082
d55bc081 1083 sbitmap_vector_ones (transp, last_basic_block);
9f09b1f2
R
1084
1085 for (j = n_entities - 1; j >= 0; j--)
1086 {
1087 int e = entity_map[j];
1088 int no_mode = num_modes[e];
9ca88d5a 1089 struct bb_info *info = bb_info[j];
9f09b1f2
R
1090
1091 /* Determine what the first use (if any) need for a mode of entity E is.
97d36f45 1092 This will be the mode that is anticipatable for this block.
9f09b1f2 1093 Also compute the initial transparency settings. */
e0082a72 1094 FOR_EACH_BB (bb)
9f09b1f2
R
1095 {
1096 struct seginfo *ptr;
1097 int last_mode = no_mode;
1098 HARD_REG_SET live_now;
1099
1100 REG_SET_TO_HARD_REG_SET (live_now,
e0082a72
ZD
1101 bb->global_live_at_start);
1102 for (insn = bb->head;
1103 insn != NULL && insn != NEXT_INSN (bb->end);
9f09b1f2
R
1104 insn = NEXT_INSN (insn))
1105 {
2c3c49de 1106 if (INSN_P (insn))
9f09b1f2
R
1107 {
1108 int mode = MODE_NEEDED (e, insn);
1109 rtx link;
1110
1111 if (mode != no_mode && mode != last_mode)
1112 {
1113 last_mode = mode;
e0082a72
ZD
1114 ptr = new_seginfo (mode, insn, bb->index, live_now);
1115 add_seginfo (info + bb->index, ptr);
1116 RESET_BIT (transp[bb->index], j);
9f09b1f2
R
1117 }
1118
1119 /* Update LIVE_NOW. */
1120 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1121 if (REG_NOTE_KIND (link) == REG_DEAD)
1122 reg_dies (XEXP (link, 0), live_now);
f4e72d6e 1123
9f09b1f2
R
1124 note_stores (PATTERN (insn), reg_becomes_live, &live_now);
1125 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1126 if (REG_NOTE_KIND (link) == REG_UNUSED)
1127 reg_dies (XEXP (link, 0), live_now);
1128 }
1129 }
f4e72d6e 1130
e0082a72 1131 info[bb->index].computing = last_mode;
9f09b1f2
R
1132 /* Check for blocks without ANY mode requirements. */
1133 if (last_mode == no_mode)
1134 {
81b40b72 1135 ptr = new_seginfo (no_mode, bb->end, bb->index, live_now);
e0082a72 1136 add_seginfo (info + bb->index, ptr);
9f09b1f2
R
1137 }
1138 }
1270c255 1139#ifdef NORMAL_MODE
9f09b1f2 1140 {
1270c255 1141 int mode = NORMAL_MODE (e);
f4e72d6e 1142
9f09b1f2
R
1143 if (mode != no_mode)
1144 {
81b40b72 1145 bb = post_entry;
b3bb6456 1146
81b40b72
R
1147 /* By always making this nontransparent, we save
1148 an extra check in make_preds_opaque. We also
1149 need this to avoid confusing pre_edge_lcm when
1150 antic is cleared but transp and comp are set. */
1151 RESET_BIT (transp[bb->index], j);
e8eacc3f 1152
81b40b72
R
1153 /* Insert a fake computing definition of MODE into entry
1154 blocks which compute no mode. This represents the mode on
1155 entry. */
1156 info[bb->index].computing = mode;
1157
1158 if (pre_exit)
1159 info[pre_exit->index].seginfo->mode = mode;
9f09b1f2
R
1160 }
1161 }
1270c255 1162#endif /* NORMAL_MODE */
9f09b1f2
R
1163 }
1164
d55bc081 1165 kill = sbitmap_vector_alloc (last_basic_block, n_entities);
9f09b1f2
R
1166 for (i = 0; i < max_num_modes; i++)
1167 {
1168 int current_mode[N_ENTITIES];
1169
1170 /* Set the anticipatable and computing arrays. */
d55bc081
ZD
1171 sbitmap_vector_zero (antic, last_basic_block);
1172 sbitmap_vector_zero (comp, last_basic_block);
9f09b1f2
R
1173 for (j = n_entities - 1; j >= 0; j--)
1174 {
1175 int m = current_mode[j] = MODE_PRIORITY_TO_MODE (entity_map[j], i);
9ca88d5a 1176 struct bb_info *info = bb_info[j];
b3bb6456 1177
e0082a72 1178 FOR_EACH_BB (bb)
9f09b1f2 1179 {
e0082a72
ZD
1180 if (info[bb->index].seginfo->mode == m)
1181 SET_BIT (antic[bb->index], j);
9f09b1f2 1182
e0082a72
ZD
1183 if (info[bb->index].computing == m)
1184 SET_BIT (comp[bb->index], j);
9f09b1f2
R
1185 }
1186 }
1187
1188 /* Calculate the optimal locations for the
1189 placement mode switches to modes with priority I. */
1190
e0082a72
ZD
1191 FOR_EACH_BB (bb)
1192 sbitmap_not (kill[bb->index], transp[bb->index]);
9f09b1f2
R
1193 edge_list = pre_edge_lcm (file, 1, transp, comp, antic,
1194 kill, &insert, &delete);
1195
f4e72d6e 1196 for (j = n_entities - 1; j >= 0; j--)
9f09b1f2
R
1197 {
1198 /* Insert all mode sets that have been inserted by lcm. */
1199 int no_mode = num_modes[entity_map[j]];
f4e72d6e 1200
9f09b1f2
R
1201 /* Wherever we have moved a mode setting upwards in the flow graph,
1202 the blocks between the new setting site and the now redundant
1203 computation ceases to be transparent for any lower-priority
1204 mode of the same entity. First set the aux field of each
1205 insertion site edge non-transparent, then propagate the new
1206 non-transparency from the redundant computation upwards till
1207 we hit an insertion site or an already non-transparent block. */
1208 for (e = NUM_EDGES (edge_list) - 1; e >= 0; e--)
1209 {
1210 edge eg = INDEX_EDGE (edge_list, e);
1211 int mode;
1212 basic_block src_bb;
1213 HARD_REG_SET live_at_edge;
1214 rtx mode_set;
1215
1216 eg->aux = 0;
1217
1218 if (! TEST_BIT (insert[e], j))
1219 continue;
1220
1221 eg->aux = (void *)1;
1222
1223 mode = current_mode[j];
1224 src_bb = eg->src;
1225
f4e72d6e
RK
1226 REG_SET_TO_HARD_REG_SET (live_at_edge,
1227 src_bb->global_live_at_end);
1228
9f09b1f2
R
1229 start_sequence ();
1230 EMIT_MODE_SET (entity_map[j], mode, live_at_edge);
2f937369 1231 mode_set = get_insns ();
9f09b1f2
R
1232 end_sequence ();
1233
73991d6a 1234 /* Do not bother to insert empty sequence. */
2f937369 1235 if (mode_set == NULL_RTX)
73991d6a
JH
1236 continue;
1237
e8eacc3f
AO
1238 /* If this is an abnormal edge, we'll insert at the end
1239 of the previous block. */
9f09b1f2
R
1240 if (eg->flags & EDGE_ABNORMAL)
1241 {
73991d6a 1242 emited = true;
09d84e04
AO
1243 if (GET_CODE (src_bb->end) == JUMP_INSN)
1244 emit_insn_before (mode_set, src_bb->end);
e8eacc3f
AO
1245 /* It doesn't make sense to switch to normal mode
1246 after a CALL_INSN, so we're going to abort if we
1247 find one. The cases in which a CALL_INSN may
1248 have an abnormal edge are sibcalls and EH edges.
1249 In the case of sibcalls, the dest basic-block is
1250 the EXIT_BLOCK, that runs in normal mode; it is
1251 assumed that a sibcall insn requires normal mode
1252 itself, so no mode switch would be required after
1253 the call (it wouldn't make sense, anyway). In
1254 the case of EH edges, EH entry points also start
1255 in normal mode, so a similar reasoning applies. */
1256 else if (GET_CODE (src_bb->end) == INSN)
3c030e88 1257 emit_insn_after (mode_set, src_bb->end);
e8eacc3f
AO
1258 else
1259 abort ();
0b17ab2f
RH
1260 bb_info[j][src_bb->index].computing = mode;
1261 RESET_BIT (transp[src_bb->index], j);
9f09b1f2
R
1262 }
1263 else
1264 {
1265 need_commit = 1;
1266 insert_insn_on_edge (mode_set, eg);
1267 }
9f09b1f2
R
1268 }
1269
e0082a72
ZD
1270 FOR_EACH_BB_REVERSE (bb)
1271 if (TEST_BIT (delete[bb->index], j))
f4e72d6e 1272 {
e0082a72 1273 make_preds_opaque (bb, j);
f4e72d6e 1274 /* Cancel the 'deleted' mode set. */
e0082a72 1275 bb_info[j][bb->index].seginfo->mode = no_mode;
f4e72d6e 1276 }
9f09b1f2 1277 }
f4e72d6e 1278
108c1afc 1279 clear_aux_for_edges ();
9f09b1f2
R
1280 free_edge_list (edge_list);
1281 }
1282
1283 /* Now output the remaining mode sets in all the segments. */
1284 for (j = n_entities - 1; j >= 0; j--)
1285 {
1270c255
CP
1286 int no_mode = num_modes[entity_map[j]];
1287
e0082a72 1288 FOR_EACH_BB_REVERSE (bb)
9f09b1f2
R
1289 {
1290 struct seginfo *ptr, *next;
e0082a72 1291 for (ptr = bb_info[j][bb->index].seginfo; ptr; ptr = next)
9f09b1f2
R
1292 {
1293 next = ptr->next;
1270c255 1294 if (ptr->mode != no_mode)
9f09b1f2
R
1295 {
1296 rtx mode_set;
1297
1298 start_sequence ();
1299 EMIT_MODE_SET (entity_map[j], ptr->mode, ptr->regs_live);
2f937369 1300 mode_set = get_insns ();
9f09b1f2
R
1301 end_sequence ();
1302
73991d6a 1303 /* Do not bother to insert empty sequence. */
2f937369 1304 if (mode_set == NULL_RTX)
73991d6a
JH
1305 continue;
1306
1307 emited = true;
25fa8bdc
AO
1308 if (GET_CODE (ptr->insn_ptr) == NOTE
1309 && (NOTE_LINE_NUMBER (ptr->insn_ptr)
1310 == NOTE_INSN_BASIC_BLOCK))
3c030e88 1311 emit_insn_after (mode_set, ptr->insn_ptr);
1270c255 1312 else
3c030e88 1313 emit_insn_before (mode_set, ptr->insn_ptr);
9f09b1f2 1314 }
f4e72d6e 1315
9f09b1f2
R
1316 free (ptr);
1317 }
1318 }
f4e72d6e 1319
9f09b1f2
R
1320 free (bb_info[j]);
1321 }
1322
1323 /* Finished. Free up all the things we've allocated. */
b3bb6456 1324
9f09b1f2
R
1325 sbitmap_vector_free (kill);
1326 sbitmap_vector_free (antic);
1327 sbitmap_vector_free (transp);
1328 sbitmap_vector_free (comp);
1329 sbitmap_vector_free (delete);
1330 sbitmap_vector_free (insert);
1331
1332 if (need_commit)
1333 commit_edge_insertions ();
97d36f45 1334
81b40b72
R
1335#ifdef NORMAL_MODE
1336 cleanup_cfg (CLEANUP_NO_INSN_DEL);
1337#else
73991d6a
JH
1338 if (!need_commit && !emited)
1339 return 0;
81b40b72 1340#endif
73991d6a 1341
38c1593d
JH
1342 max_regno = max_reg_num ();
1343 allocate_reg_info (max_regno, FALSE, FALSE);
1344 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
1345 (PROP_DEATH_NOTES | PROP_KILL_DEAD_CODE
1346 | PROP_SCAN_DEAD_CODE));
97d36f45
RH
1347
1348 return 1;
9f09b1f2 1349}
97d36f45 1350#endif /* OPTIMIZE_MODE_SWITCHING */