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