2015-07-02 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / lcm.c
blobaf5e78e0fa47ab7797582f42517dba7d69d3bd84
1 /* Generic partial redundancy elimination with lazy code motion support.
2 Copyright (C) 1998-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* These routines are meant to be used by various optimization
21 passes which can be modeled as lazy code motion problems.
22 Including, but not limited to:
24 * Traditional partial redundancy elimination.
26 * Placement of caller/caller register save/restores.
28 * Load/store motion.
30 * Copy motion.
32 * Conversion of flat register files to a stacked register
33 model.
35 * Dead load/store elimination.
37 These routines accept as input:
39 * Basic block information (number of blocks, lists of
40 predecessors and successors). Note the granularity
41 does not need to be basic block, they could be statements
42 or functions.
44 * Bitmaps of local properties (computed, transparent and
45 anticipatable expressions).
47 The output of these routines is bitmap of redundant computations
48 and a bitmap of optimal placement points. */
51 #include "config.h"
52 #include "system.h"
53 #include "coretypes.h"
54 #include "tm.h"
55 #include "rtl.h"
56 #include "regs.h"
57 #include "hard-reg-set.h"
58 #include "flags.h"
59 #include "insn-config.h"
60 #include "recog.h"
61 #include "predict.h"
62 #include "function.h"
63 #include "dominance.h"
64 #include "cfg.h"
65 #include "cfganal.h"
66 #include "lcm.h"
67 #include "basic-block.h"
68 #include "tm_p.h"
69 #include "sbitmap.h"
70 #include "dumpfile.h"
72 /* Edge based LCM routines. */
73 static void compute_antinout_edge (sbitmap *, sbitmap *, sbitmap *, sbitmap *);
74 static void compute_earliest (struct edge_list *, int, sbitmap *, sbitmap *,
75 sbitmap *, sbitmap *, sbitmap *);
76 static void compute_laterin (struct edge_list *, sbitmap *, sbitmap *,
77 sbitmap *, sbitmap *);
78 static void compute_insert_delete (struct edge_list *edge_list, sbitmap *,
79 sbitmap *, sbitmap *, sbitmap *, sbitmap *);
81 /* Edge based LCM routines on a reverse flowgraph. */
82 static void compute_farthest (struct edge_list *, int, sbitmap *, sbitmap *,
83 sbitmap*, sbitmap *, sbitmap *);
84 static void compute_nearerout (struct edge_list *, sbitmap *, sbitmap *,
85 sbitmap *, sbitmap *);
86 static void compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *,
87 sbitmap *, sbitmap *, sbitmap *,
88 sbitmap *);
90 /* Edge based lcm routines. */
92 /* Compute expression anticipatability at entrance and exit of each block.
93 This is done based on the flow graph, and not on the pred-succ lists.
94 Other than that, its pretty much identical to compute_antinout. */
96 static void
97 compute_antinout_edge (sbitmap *antloc, sbitmap *transp, sbitmap *antin,
98 sbitmap *antout)
100 basic_block bb;
101 edge e;
102 basic_block *worklist, *qin, *qout, *qend;
103 unsigned int qlen;
104 edge_iterator ei;
106 /* Allocate a worklist array/queue. Entries are only added to the
107 list if they were not already on the list. So the size is
108 bounded by the number of basic blocks. */
109 qin = qout = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
111 /* We want a maximal solution, so make an optimistic initialization of
112 ANTIN. */
113 bitmap_vector_ones (antin, last_basic_block_for_fn (cfun));
115 /* Put every block on the worklist; this is necessary because of the
116 optimistic initialization of ANTIN above. */
117 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
118 int postorder_num = post_order_compute (postorder, false, false);
119 for (int i = 0; i < postorder_num; ++i)
121 bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
122 *qin++ = bb;
123 bb->aux = bb;
125 free (postorder);
127 qin = worklist;
128 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
129 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
131 /* Mark blocks which are predecessors of the exit block so that we
132 can easily identify them below. */
133 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
134 e->src->aux = EXIT_BLOCK_PTR_FOR_FN (cfun);
136 /* Iterate until the worklist is empty. */
137 while (qlen)
139 /* Take the first entry off the worklist. */
140 bb = *qout++;
141 qlen--;
143 if (qout >= qend)
144 qout = worklist;
146 if (bb->aux == EXIT_BLOCK_PTR_FOR_FN (cfun))
147 /* Do not clear the aux field for blocks which are predecessors of
148 the EXIT block. That way we never add then to the worklist
149 again. */
150 bitmap_clear (antout[bb->index]);
151 else
153 /* Clear the aux field of this block so that it can be added to
154 the worklist again if necessary. */
155 bb->aux = NULL;
156 bitmap_intersection_of_succs (antout[bb->index], antin, bb);
159 if (bitmap_or_and (antin[bb->index], antloc[bb->index],
160 transp[bb->index], antout[bb->index]))
161 /* If the in state of this block changed, then we need
162 to add the predecessors of this block to the worklist
163 if they are not already on the worklist. */
164 FOR_EACH_EDGE (e, ei, bb->preds)
165 if (!e->src->aux && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
167 *qin++ = e->src;
168 e->src->aux = e;
169 qlen++;
170 if (qin >= qend)
171 qin = worklist;
175 clear_aux_for_edges ();
176 clear_aux_for_blocks ();
177 free (worklist);
180 /* Compute the earliest vector for edge based lcm. */
182 static void
183 compute_earliest (struct edge_list *edge_list, int n_exprs, sbitmap *antin,
184 sbitmap *antout, sbitmap *avout, sbitmap *kill,
185 sbitmap *earliest)
187 sbitmap difference, temp_bitmap;
188 int x, num_edges;
189 basic_block pred, succ;
191 num_edges = NUM_EDGES (edge_list);
193 difference = sbitmap_alloc (n_exprs);
194 temp_bitmap = sbitmap_alloc (n_exprs);
196 for (x = 0; x < num_edges; x++)
198 pred = INDEX_EDGE_PRED_BB (edge_list, x);
199 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
200 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
201 bitmap_copy (earliest[x], antin[succ->index]);
202 else
204 if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
205 bitmap_clear (earliest[x]);
206 else
208 bitmap_and_compl (difference, antin[succ->index],
209 avout[pred->index]);
210 bitmap_not (temp_bitmap, antout[pred->index]);
211 bitmap_and_or (earliest[x], difference,
212 kill[pred->index], temp_bitmap);
217 sbitmap_free (temp_bitmap);
218 sbitmap_free (difference);
221 /* later(p,s) is dependent on the calculation of laterin(p).
222 laterin(p) is dependent on the calculation of later(p2,p).
224 laterin(ENTRY) is defined as all 0's
225 later(ENTRY, succs(ENTRY)) are defined using laterin(ENTRY)
226 laterin(succs(ENTRY)) is defined by later(ENTRY, succs(ENTRY)).
228 If we progress in this manner, starting with all basic blocks
229 in the work list, anytime we change later(bb), we need to add
230 succs(bb) to the worklist if they are not already on the worklist.
232 Boundary conditions:
234 We prime the worklist all the normal basic blocks. The ENTRY block can
235 never be added to the worklist since it is never the successor of any
236 block. We explicitly prevent the EXIT block from being added to the
237 worklist.
239 We optimistically initialize LATER. That is the only time this routine
240 will compute LATER for an edge out of the entry block since the entry
241 block is never on the worklist. Thus, LATERIN is neither used nor
242 computed for the ENTRY block.
244 Since the EXIT block is never added to the worklist, we will neither
245 use nor compute LATERIN for the exit block. Edges which reach the
246 EXIT block are handled in the normal fashion inside the loop. However,
247 the insertion/deletion computation needs LATERIN(EXIT), so we have
248 to compute it. */
250 static void
251 compute_laterin (struct edge_list *edge_list, sbitmap *earliest,
252 sbitmap *antloc, sbitmap *later, sbitmap *laterin)
254 int num_edges, i;
255 edge e;
256 basic_block *worklist, *qin, *qout, *qend, bb;
257 unsigned int qlen;
258 edge_iterator ei;
260 num_edges = NUM_EDGES (edge_list);
262 /* Allocate a worklist array/queue. Entries are only added to the
263 list if they were not already on the list. So the size is
264 bounded by the number of basic blocks. */
265 qin = qout = worklist
266 = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
268 /* Initialize a mapping from each edge to its index. */
269 for (i = 0; i < num_edges; i++)
270 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
272 /* We want a maximal solution, so initially consider LATER true for
273 all edges. This allows propagation through a loop since the incoming
274 loop edge will have LATER set, so if all the other incoming edges
275 to the loop are set, then LATERIN will be set for the head of the
276 loop.
278 If the optimistic setting of LATER on that edge was incorrect (for
279 example the expression is ANTLOC in a block within the loop) then
280 this algorithm will detect it when we process the block at the head
281 of the optimistic edge. That will requeue the affected blocks. */
282 bitmap_vector_ones (later, num_edges);
284 /* Note that even though we want an optimistic setting of LATER, we
285 do not want to be overly optimistic. Consider an outgoing edge from
286 the entry block. That edge should always have a LATER value the
287 same as EARLIEST for that edge. */
288 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
289 bitmap_copy (later[(size_t) e->aux], earliest[(size_t) e->aux]);
291 /* Add all the blocks to the worklist. This prevents an early exit from
292 the loop given our optimistic initialization of LATER above. */
293 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
294 int postorder_num = inverted_post_order_compute (postorder);
295 for (int i = 0; i < postorder_num; ++i)
297 bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
298 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
299 || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
300 continue;
301 *qin++ = bb;
302 bb->aux = bb;
304 free (postorder);
306 /* Note that we do not use the last allocated element for our queue,
307 as EXIT_BLOCK is never inserted into it. */
308 qin = worklist;
309 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
310 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
312 /* Iterate until the worklist is empty. */
313 while (qlen)
315 /* Take the first entry off the worklist. */
316 bb = *qout++;
317 bb->aux = NULL;
318 qlen--;
319 if (qout >= qend)
320 qout = worklist;
322 /* Compute the intersection of LATERIN for each incoming edge to B. */
323 bitmap_ones (laterin[bb->index]);
324 FOR_EACH_EDGE (e, ei, bb->preds)
325 bitmap_and (laterin[bb->index], laterin[bb->index],
326 later[(size_t)e->aux]);
328 /* Calculate LATER for all outgoing edges. */
329 FOR_EACH_EDGE (e, ei, bb->succs)
330 if (bitmap_ior_and_compl (later[(size_t) e->aux],
331 earliest[(size_t) e->aux],
332 laterin[bb->index],
333 antloc[bb->index])
334 /* If LATER for an outgoing edge was changed, then we need
335 to add the target of the outgoing edge to the worklist. */
336 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest->aux == 0)
338 *qin++ = e->dest;
339 e->dest->aux = e;
340 qlen++;
341 if (qin >= qend)
342 qin = worklist;
346 /* Computation of insertion and deletion points requires computing LATERIN
347 for the EXIT block. We allocated an extra entry in the LATERIN array
348 for just this purpose. */
349 bitmap_ones (laterin[last_basic_block_for_fn (cfun)]);
350 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
351 bitmap_and (laterin[last_basic_block_for_fn (cfun)],
352 laterin[last_basic_block_for_fn (cfun)],
353 later[(size_t) e->aux]);
355 clear_aux_for_edges ();
356 free (worklist);
359 /* Compute the insertion and deletion points for edge based LCM. */
361 static void
362 compute_insert_delete (struct edge_list *edge_list, sbitmap *antloc,
363 sbitmap *later, sbitmap *laterin, sbitmap *insert,
364 sbitmap *del)
366 int x;
367 basic_block bb;
369 FOR_EACH_BB_FN (bb, cfun)
370 bitmap_and_compl (del[bb->index], antloc[bb->index],
371 laterin[bb->index]);
373 for (x = 0; x < NUM_EDGES (edge_list); x++)
375 basic_block b = INDEX_EDGE_SUCC_BB (edge_list, x);
377 if (b == EXIT_BLOCK_PTR_FOR_FN (cfun))
378 bitmap_and_compl (insert[x], later[x],
379 laterin[last_basic_block_for_fn (cfun)]);
380 else
381 bitmap_and_compl (insert[x], later[x], laterin[b->index]);
385 /* Given local properties TRANSP, ANTLOC, AVLOC, KILL return the insert and
386 delete vectors for edge based LCM and return the AVIN, AVOUT bitmap.
387 map the insert vector to what edge an expression should be inserted on. */
389 struct edge_list *
390 pre_edge_lcm_avs (int n_exprs, sbitmap *transp,
391 sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
392 sbitmap *avin, sbitmap *avout,
393 sbitmap **insert, sbitmap **del)
395 sbitmap *antin, *antout, *earliest;
396 sbitmap *later, *laterin;
397 struct edge_list *edge_list;
398 int num_edges;
400 edge_list = create_edge_list ();
401 num_edges = NUM_EDGES (edge_list);
403 #ifdef LCM_DEBUG_INFO
404 if (dump_file)
406 fprintf (dump_file, "Edge List:\n");
407 verify_edge_list (dump_file, edge_list);
408 print_edge_list (dump_file, edge_list);
409 dump_bitmap_vector (dump_file, "transp", "", transp,
410 last_basic_block_for_fn (cfun));
411 dump_bitmap_vector (dump_file, "antloc", "", antloc,
412 last_basic_block_for_fn (cfun));
413 dump_bitmap_vector (dump_file, "avloc", "", avloc,
414 last_basic_block_for_fn (cfun));
415 dump_bitmap_vector (dump_file, "kill", "", kill,
416 last_basic_block_for_fn (cfun));
418 #endif
420 /* Compute global availability. */
421 compute_available (avloc, kill, avout, avin);
423 /* Compute global anticipatability. */
424 antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
425 antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
426 compute_antinout_edge (antloc, transp, antin, antout);
428 #ifdef LCM_DEBUG_INFO
429 if (dump_file)
431 dump_bitmap_vector (dump_file, "antin", "", antin,
432 last_basic_block_for_fn (cfun));
433 dump_bitmap_vector (dump_file, "antout", "", antout,
434 last_basic_block_for_fn (cfun));
436 #endif
438 /* Compute earliestness. */
439 earliest = sbitmap_vector_alloc (num_edges, n_exprs);
440 compute_earliest (edge_list, n_exprs, antin, antout, avout, kill, earliest);
442 #ifdef LCM_DEBUG_INFO
443 if (dump_file)
444 dump_bitmap_vector (dump_file, "earliest", "", earliest, num_edges);
445 #endif
447 sbitmap_vector_free (antout);
448 sbitmap_vector_free (antin);
450 later = sbitmap_vector_alloc (num_edges, n_exprs);
452 /* Allocate an extra element for the exit block in the laterin vector. */
453 laterin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
454 n_exprs);
455 compute_laterin (edge_list, earliest, antloc, later, laterin);
457 #ifdef LCM_DEBUG_INFO
458 if (dump_file)
460 dump_bitmap_vector (dump_file, "laterin", "", laterin,
461 last_basic_block_for_fn (cfun) + 1);
462 dump_bitmap_vector (dump_file, "later", "", later, num_edges);
464 #endif
466 sbitmap_vector_free (earliest);
468 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
469 *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
470 bitmap_vector_clear (*insert, num_edges);
471 bitmap_vector_clear (*del, last_basic_block_for_fn (cfun));
472 compute_insert_delete (edge_list, antloc, later, laterin, *insert, *del);
474 sbitmap_vector_free (laterin);
475 sbitmap_vector_free (later);
477 #ifdef LCM_DEBUG_INFO
478 if (dump_file)
480 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
481 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
482 last_basic_block_for_fn (cfun));
484 #endif
486 return edge_list;
489 /* Wrapper to allocate avin/avout and call pre_edge_lcm_avs. */
491 struct edge_list *
492 pre_edge_lcm (int n_exprs, sbitmap *transp,
493 sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
494 sbitmap **insert, sbitmap **del)
496 struct edge_list *edge_list;
497 sbitmap *avin, *avout;
499 avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
500 avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
502 edge_list = pre_edge_lcm_avs (n_exprs, transp, avloc, antloc, kill,
503 avin, avout, insert, del);
505 sbitmap_vector_free (avout);
506 sbitmap_vector_free (avin);
508 return edge_list;
511 /* Compute the AVIN and AVOUT vectors from the AVLOC and KILL vectors.
512 Return the number of passes we performed to iterate to a solution. */
514 void
515 compute_available (sbitmap *avloc, sbitmap *kill, sbitmap *avout,
516 sbitmap *avin)
518 edge e;
519 basic_block *worklist, *qin, *qout, *qend, bb;
520 unsigned int qlen;
521 edge_iterator ei;
523 /* Allocate a worklist array/queue. Entries are only added to the
524 list if they were not already on the list. So the size is
525 bounded by the number of basic blocks. */
526 qin = qout = worklist =
527 XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
529 /* We want a maximal solution. */
530 bitmap_vector_ones (avout, last_basic_block_for_fn (cfun));
532 /* Put every block on the worklist; this is necessary because of the
533 optimistic initialization of AVOUT above. Use inverted postorder
534 to make the dataflow problem require less iterations. */
535 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
536 int postorder_num = inverted_post_order_compute (postorder);
537 for (int i = 0; i < postorder_num; ++i)
539 bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
540 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
541 || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
542 continue;
543 *qin++ = bb;
544 bb->aux = bb;
546 free (postorder);
548 qin = worklist;
549 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
550 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
552 /* Mark blocks which are successors of the entry block so that we
553 can easily identify them below. */
554 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
555 e->dest->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun);
557 /* Iterate until the worklist is empty. */
558 while (qlen)
560 /* Take the first entry off the worklist. */
561 bb = *qout++;
562 qlen--;
564 if (qout >= qend)
565 qout = worklist;
567 /* If one of the predecessor blocks is the ENTRY block, then the
568 intersection of avouts is the null set. We can identify such blocks
569 by the special value in the AUX field in the block structure. */
570 if (bb->aux == ENTRY_BLOCK_PTR_FOR_FN (cfun))
571 /* Do not clear the aux field for blocks which are successors of the
572 ENTRY block. That way we never add then to the worklist again. */
573 bitmap_clear (avin[bb->index]);
574 else
576 /* Clear the aux field of this block so that it can be added to
577 the worklist again if necessary. */
578 bb->aux = NULL;
579 bitmap_intersection_of_preds (avin[bb->index], avout, bb);
582 if (bitmap_ior_and_compl (avout[bb->index], avloc[bb->index],
583 avin[bb->index], kill[bb->index]))
584 /* If the out state of this block changed, then we need
585 to add the successors of this block to the worklist
586 if they are not already on the worklist. */
587 FOR_EACH_EDGE (e, ei, bb->succs)
588 if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
590 *qin++ = e->dest;
591 e->dest->aux = e;
592 qlen++;
594 if (qin >= qend)
595 qin = worklist;
599 clear_aux_for_edges ();
600 clear_aux_for_blocks ();
601 free (worklist);
604 /* Compute the farthest vector for edge based lcm. */
606 static void
607 compute_farthest (struct edge_list *edge_list, int n_exprs,
608 sbitmap *st_avout, sbitmap *st_avin, sbitmap *st_antin,
609 sbitmap *kill, sbitmap *farthest)
611 sbitmap difference, temp_bitmap;
612 int x, num_edges;
613 basic_block pred, succ;
615 num_edges = NUM_EDGES (edge_list);
617 difference = sbitmap_alloc (n_exprs);
618 temp_bitmap = sbitmap_alloc (n_exprs);
620 for (x = 0; x < num_edges; x++)
622 pred = INDEX_EDGE_PRED_BB (edge_list, x);
623 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
624 if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
625 bitmap_copy (farthest[x], st_avout[pred->index]);
626 else
628 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
629 bitmap_clear (farthest[x]);
630 else
632 bitmap_and_compl (difference, st_avout[pred->index],
633 st_antin[succ->index]);
634 bitmap_not (temp_bitmap, st_avin[succ->index]);
635 bitmap_and_or (farthest[x], difference,
636 kill[succ->index], temp_bitmap);
641 sbitmap_free (temp_bitmap);
642 sbitmap_free (difference);
645 /* Compute nearer and nearerout vectors for edge based lcm.
647 This is the mirror of compute_laterin, additional comments on the
648 implementation can be found before compute_laterin. */
650 static void
651 compute_nearerout (struct edge_list *edge_list, sbitmap *farthest,
652 sbitmap *st_avloc, sbitmap *nearer, sbitmap *nearerout)
654 int num_edges, i;
655 edge e;
656 basic_block *worklist, *tos, bb;
657 edge_iterator ei;
659 num_edges = NUM_EDGES (edge_list);
661 /* Allocate a worklist array/queue. Entries are only added to the
662 list if they were not already on the list. So the size is
663 bounded by the number of basic blocks. */
664 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
666 /* Initialize NEARER for each edge and build a mapping from an edge to
667 its index. */
668 for (i = 0; i < num_edges; i++)
669 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
671 /* We want a maximal solution. */
672 bitmap_vector_ones (nearer, num_edges);
674 /* Note that even though we want an optimistic setting of NEARER, we
675 do not want to be overly optimistic. Consider an incoming edge to
676 the exit block. That edge should always have a NEARER value the
677 same as FARTHEST for that edge. */
678 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
679 bitmap_copy (nearer[(size_t)e->aux], farthest[(size_t)e->aux]);
681 /* Add all the blocks to the worklist. This prevents an early exit
682 from the loop given our optimistic initialization of NEARER. */
683 FOR_EACH_BB_FN (bb, cfun)
685 *tos++ = bb;
686 bb->aux = bb;
689 /* Iterate until the worklist is empty. */
690 while (tos != worklist)
692 /* Take the first entry off the worklist. */
693 bb = *--tos;
694 bb->aux = NULL;
696 /* Compute the intersection of NEARER for each outgoing edge from B. */
697 bitmap_ones (nearerout[bb->index]);
698 FOR_EACH_EDGE (e, ei, bb->succs)
699 bitmap_and (nearerout[bb->index], nearerout[bb->index],
700 nearer[(size_t) e->aux]);
702 /* Calculate NEARER for all incoming edges. */
703 FOR_EACH_EDGE (e, ei, bb->preds)
704 if (bitmap_ior_and_compl (nearer[(size_t) e->aux],
705 farthest[(size_t) e->aux],
706 nearerout[e->dest->index],
707 st_avloc[e->dest->index])
708 /* If NEARER for an incoming edge was changed, then we need
709 to add the source of the incoming edge to the worklist. */
710 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) && e->src->aux == 0)
712 *tos++ = e->src;
713 e->src->aux = e;
717 /* Computation of insertion and deletion points requires computing NEAREROUT
718 for the ENTRY block. We allocated an extra entry in the NEAREROUT array
719 for just this purpose. */
720 bitmap_ones (nearerout[last_basic_block_for_fn (cfun)]);
721 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
722 bitmap_and (nearerout[last_basic_block_for_fn (cfun)],
723 nearerout[last_basic_block_for_fn (cfun)],
724 nearer[(size_t) e->aux]);
726 clear_aux_for_edges ();
727 free (tos);
730 /* Compute the insertion and deletion points for edge based LCM. */
732 static void
733 compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *st_avloc,
734 sbitmap *nearer, sbitmap *nearerout,
735 sbitmap *insert, sbitmap *del)
737 int x;
738 basic_block bb;
740 FOR_EACH_BB_FN (bb, cfun)
741 bitmap_and_compl (del[bb->index], st_avloc[bb->index],
742 nearerout[bb->index]);
744 for (x = 0; x < NUM_EDGES (edge_list); x++)
746 basic_block b = INDEX_EDGE_PRED_BB (edge_list, x);
747 if (b == ENTRY_BLOCK_PTR_FOR_FN (cfun))
748 bitmap_and_compl (insert[x], nearer[x],
749 nearerout[last_basic_block_for_fn (cfun)]);
750 else
751 bitmap_and_compl (insert[x], nearer[x], nearerout[b->index]);
755 /* Given local properties TRANSP, ST_AVLOC, ST_ANTLOC, KILL return the
756 insert and delete vectors for edge based reverse LCM. Returns an
757 edgelist which is used to map the insert vector to what edge
758 an expression should be inserted on. */
760 struct edge_list *
761 pre_edge_rev_lcm (int n_exprs, sbitmap *transp,
762 sbitmap *st_avloc, sbitmap *st_antloc, sbitmap *kill,
763 sbitmap **insert, sbitmap **del)
765 sbitmap *st_antin, *st_antout;
766 sbitmap *st_avout, *st_avin, *farthest;
767 sbitmap *nearer, *nearerout;
768 struct edge_list *edge_list;
769 int num_edges;
771 edge_list = create_edge_list ();
772 num_edges = NUM_EDGES (edge_list);
774 st_antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
775 st_antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
776 bitmap_vector_clear (st_antin, last_basic_block_for_fn (cfun));
777 bitmap_vector_clear (st_antout, last_basic_block_for_fn (cfun));
778 compute_antinout_edge (st_antloc, transp, st_antin, st_antout);
780 /* Compute global anticipatability. */
781 st_avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
782 st_avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
783 compute_available (st_avloc, kill, st_avout, st_avin);
785 #ifdef LCM_DEBUG_INFO
786 if (dump_file)
788 fprintf (dump_file, "Edge List:\n");
789 verify_edge_list (dump_file, edge_list);
790 print_edge_list (dump_file, edge_list);
791 dump_bitmap_vector (dump_file, "transp", "", transp,
792 last_basic_block_for_fn (cfun));
793 dump_bitmap_vector (dump_file, "st_avloc", "", st_avloc,
794 last_basic_block_for_fn (cfun));
795 dump_bitmap_vector (dump_file, "st_antloc", "", st_antloc,
796 last_basic_block_for_fn (cfun));
797 dump_bitmap_vector (dump_file, "st_antin", "", st_antin,
798 last_basic_block_for_fn (cfun));
799 dump_bitmap_vector (dump_file, "st_antout", "", st_antout,
800 last_basic_block_for_fn (cfun));
801 dump_bitmap_vector (dump_file, "st_kill", "", kill,
802 last_basic_block_for_fn (cfun));
804 #endif
806 #ifdef LCM_DEBUG_INFO
807 if (dump_file)
809 dump_bitmap_vector (dump_file, "st_avout", "", st_avout, last_basic_block_for_fn (cfun));
810 dump_bitmap_vector (dump_file, "st_avin", "", st_avin, last_basic_block_for_fn (cfun));
812 #endif
814 /* Compute farthestness. */
815 farthest = sbitmap_vector_alloc (num_edges, n_exprs);
816 compute_farthest (edge_list, n_exprs, st_avout, st_avin, st_antin,
817 kill, farthest);
819 #ifdef LCM_DEBUG_INFO
820 if (dump_file)
821 dump_bitmap_vector (dump_file, "farthest", "", farthest, num_edges);
822 #endif
824 sbitmap_vector_free (st_antin);
825 sbitmap_vector_free (st_antout);
827 sbitmap_vector_free (st_avin);
828 sbitmap_vector_free (st_avout);
830 nearer = sbitmap_vector_alloc (num_edges, n_exprs);
832 /* Allocate an extra element for the entry block. */
833 nearerout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
834 n_exprs);
835 compute_nearerout (edge_list, farthest, st_avloc, nearer, nearerout);
837 #ifdef LCM_DEBUG_INFO
838 if (dump_file)
840 dump_bitmap_vector (dump_file, "nearerout", "", nearerout,
841 last_basic_block_for_fn (cfun) + 1);
842 dump_bitmap_vector (dump_file, "nearer", "", nearer, num_edges);
844 #endif
846 sbitmap_vector_free (farthest);
848 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
849 *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
850 compute_rev_insert_delete (edge_list, st_avloc, nearer, nearerout,
851 *insert, *del);
853 sbitmap_vector_free (nearerout);
854 sbitmap_vector_free (nearer);
856 #ifdef LCM_DEBUG_INFO
857 if (dump_file)
859 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
860 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
861 last_basic_block_for_fn (cfun));
863 #endif
864 return edge_list;