2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / lcm.c
blobac27ec51a8065d64b177aab77efde02a352d1c36
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 "input.h"
63 #include "function.h"
64 #include "dominance.h"
65 #include "cfg.h"
66 #include "cfganal.h"
67 #include "lcm.h"
68 #include "basic-block.h"
69 #include "tm_p.h"
70 #include "sbitmap.h"
71 #include "dumpfile.h"
73 /* Edge based LCM routines. */
74 static void compute_antinout_edge (sbitmap *, sbitmap *, sbitmap *, sbitmap *);
75 static void compute_earliest (struct edge_list *, int, sbitmap *, sbitmap *,
76 sbitmap *, sbitmap *, sbitmap *);
77 static void compute_laterin (struct edge_list *, sbitmap *, sbitmap *,
78 sbitmap *, sbitmap *);
79 static void compute_insert_delete (struct edge_list *edge_list, sbitmap *,
80 sbitmap *, sbitmap *, sbitmap *, sbitmap *);
82 /* Edge based LCM routines on a reverse flowgraph. */
83 static void compute_farthest (struct edge_list *, int, sbitmap *, sbitmap *,
84 sbitmap*, sbitmap *, sbitmap *);
85 static void compute_nearerout (struct edge_list *, sbitmap *, sbitmap *,
86 sbitmap *, sbitmap *);
87 static void compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *,
88 sbitmap *, sbitmap *, sbitmap *,
89 sbitmap *);
91 /* Edge based lcm routines. */
93 /* Compute expression anticipatability at entrance and exit of each block.
94 This is done based on the flow graph, and not on the pred-succ lists.
95 Other than that, its pretty much identical to compute_antinout. */
97 static void
98 compute_antinout_edge (sbitmap *antloc, sbitmap *transp, sbitmap *antin,
99 sbitmap *antout)
101 basic_block bb;
102 edge e;
103 basic_block *worklist, *qin, *qout, *qend;
104 unsigned int qlen;
105 edge_iterator ei;
107 /* Allocate a worklist array/queue. Entries are only added to the
108 list if they were not already on the list. So the size is
109 bounded by the number of basic blocks. */
110 qin = qout = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
112 /* We want a maximal solution, so make an optimistic initialization of
113 ANTIN. */
114 bitmap_vector_ones (antin, last_basic_block_for_fn (cfun));
116 /* Put every block on the worklist; this is necessary because of the
117 optimistic initialization of ANTIN above. */
118 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
119 int postorder_num = post_order_compute (postorder, false, false);
120 for (int i = 0; i < postorder_num; ++i)
122 bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
123 *qin++ = bb;
124 bb->aux = bb;
126 free (postorder);
128 qin = worklist;
129 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
130 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
132 /* Mark blocks which are predecessors of the exit block so that we
133 can easily identify them below. */
134 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
135 e->src->aux = EXIT_BLOCK_PTR_FOR_FN (cfun);
137 /* Iterate until the worklist is empty. */
138 while (qlen)
140 /* Take the first entry off the worklist. */
141 bb = *qout++;
142 qlen--;
144 if (qout >= qend)
145 qout = worklist;
147 if (bb->aux == EXIT_BLOCK_PTR_FOR_FN (cfun))
148 /* Do not clear the aux field for blocks which are predecessors of
149 the EXIT block. That way we never add then to the worklist
150 again. */
151 bitmap_clear (antout[bb->index]);
152 else
154 /* Clear the aux field of this block so that it can be added to
155 the worklist again if necessary. */
156 bb->aux = NULL;
157 bitmap_intersection_of_succs (antout[bb->index], antin, bb);
160 if (bitmap_or_and (antin[bb->index], antloc[bb->index],
161 transp[bb->index], antout[bb->index]))
162 /* If the in state of this block changed, then we need
163 to add the predecessors of this block to the worklist
164 if they are not already on the worklist. */
165 FOR_EACH_EDGE (e, ei, bb->preds)
166 if (!e->src->aux && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
168 *qin++ = e->src;
169 e->src->aux = e;
170 qlen++;
171 if (qin >= qend)
172 qin = worklist;
176 clear_aux_for_edges ();
177 clear_aux_for_blocks ();
178 free (worklist);
181 /* Compute the earliest vector for edge based lcm. */
183 static void
184 compute_earliest (struct edge_list *edge_list, int n_exprs, sbitmap *antin,
185 sbitmap *antout, sbitmap *avout, sbitmap *kill,
186 sbitmap *earliest)
188 sbitmap difference, temp_bitmap;
189 int x, num_edges;
190 basic_block pred, succ;
192 num_edges = NUM_EDGES (edge_list);
194 difference = sbitmap_alloc (n_exprs);
195 temp_bitmap = sbitmap_alloc (n_exprs);
197 for (x = 0; x < num_edges; x++)
199 pred = INDEX_EDGE_PRED_BB (edge_list, x);
200 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
201 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
202 bitmap_copy (earliest[x], antin[succ->index]);
203 else
205 if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
206 bitmap_clear (earliest[x]);
207 else
209 bitmap_and_compl (difference, antin[succ->index],
210 avout[pred->index]);
211 bitmap_not (temp_bitmap, antout[pred->index]);
212 bitmap_and_or (earliest[x], difference,
213 kill[pred->index], temp_bitmap);
218 sbitmap_free (temp_bitmap);
219 sbitmap_free (difference);
222 /* later(p,s) is dependent on the calculation of laterin(p).
223 laterin(p) is dependent on the calculation of later(p2,p).
225 laterin(ENTRY) is defined as all 0's
226 later(ENTRY, succs(ENTRY)) are defined using laterin(ENTRY)
227 laterin(succs(ENTRY)) is defined by later(ENTRY, succs(ENTRY)).
229 If we progress in this manner, starting with all basic blocks
230 in the work list, anytime we change later(bb), we need to add
231 succs(bb) to the worklist if they are not already on the worklist.
233 Boundary conditions:
235 We prime the worklist all the normal basic blocks. The ENTRY block can
236 never be added to the worklist since it is never the successor of any
237 block. We explicitly prevent the EXIT block from being added to the
238 worklist.
240 We optimistically initialize LATER. That is the only time this routine
241 will compute LATER for an edge out of the entry block since the entry
242 block is never on the worklist. Thus, LATERIN is neither used nor
243 computed for the ENTRY block.
245 Since the EXIT block is never added to the worklist, we will neither
246 use nor compute LATERIN for the exit block. Edges which reach the
247 EXIT block are handled in the normal fashion inside the loop. However,
248 the insertion/deletion computation needs LATERIN(EXIT), so we have
249 to compute it. */
251 static void
252 compute_laterin (struct edge_list *edge_list, sbitmap *earliest,
253 sbitmap *antloc, sbitmap *later, sbitmap *laterin)
255 int num_edges, i;
256 edge e;
257 basic_block *worklist, *qin, *qout, *qend, bb;
258 unsigned int qlen;
259 edge_iterator ei;
261 num_edges = NUM_EDGES (edge_list);
263 /* Allocate a worklist array/queue. Entries are only added to the
264 list if they were not already on the list. So the size is
265 bounded by the number of basic blocks. */
266 qin = qout = worklist
267 = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
269 /* Initialize a mapping from each edge to its index. */
270 for (i = 0; i < num_edges; i++)
271 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
273 /* We want a maximal solution, so initially consider LATER true for
274 all edges. This allows propagation through a loop since the incoming
275 loop edge will have LATER set, so if all the other incoming edges
276 to the loop are set, then LATERIN will be set for the head of the
277 loop.
279 If the optimistic setting of LATER on that edge was incorrect (for
280 example the expression is ANTLOC in a block within the loop) then
281 this algorithm will detect it when we process the block at the head
282 of the optimistic edge. That will requeue the affected blocks. */
283 bitmap_vector_ones (later, num_edges);
285 /* Note that even though we want an optimistic setting of LATER, we
286 do not want to be overly optimistic. Consider an outgoing edge from
287 the entry block. That edge should always have a LATER value the
288 same as EARLIEST for that edge. */
289 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
290 bitmap_copy (later[(size_t) e->aux], earliest[(size_t) e->aux]);
292 /* Add all the blocks to the worklist. This prevents an early exit from
293 the loop given our optimistic initialization of LATER above. */
294 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
295 int postorder_num = inverted_post_order_compute (postorder);
296 for (int i = 0; i < postorder_num; ++i)
298 bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
299 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
300 || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
301 continue;
302 *qin++ = bb;
303 bb->aux = bb;
305 free (postorder);
307 /* Note that we do not use the last allocated element for our queue,
308 as EXIT_BLOCK is never inserted into it. */
309 qin = worklist;
310 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
311 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
313 /* Iterate until the worklist is empty. */
314 while (qlen)
316 /* Take the first entry off the worklist. */
317 bb = *qout++;
318 bb->aux = NULL;
319 qlen--;
320 if (qout >= qend)
321 qout = worklist;
323 /* Compute the intersection of LATERIN for each incoming edge to B. */
324 bitmap_ones (laterin[bb->index]);
325 FOR_EACH_EDGE (e, ei, bb->preds)
326 bitmap_and (laterin[bb->index], laterin[bb->index],
327 later[(size_t)e->aux]);
329 /* Calculate LATER for all outgoing edges. */
330 FOR_EACH_EDGE (e, ei, bb->succs)
331 if (bitmap_ior_and_compl (later[(size_t) e->aux],
332 earliest[(size_t) e->aux],
333 laterin[bb->index],
334 antloc[bb->index])
335 /* If LATER for an outgoing edge was changed, then we need
336 to add the target of the outgoing edge to the worklist. */
337 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest->aux == 0)
339 *qin++ = e->dest;
340 e->dest->aux = e;
341 qlen++;
342 if (qin >= qend)
343 qin = worklist;
347 /* Computation of insertion and deletion points requires computing LATERIN
348 for the EXIT block. We allocated an extra entry in the LATERIN array
349 for just this purpose. */
350 bitmap_ones (laterin[last_basic_block_for_fn (cfun)]);
351 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
352 bitmap_and (laterin[last_basic_block_for_fn (cfun)],
353 laterin[last_basic_block_for_fn (cfun)],
354 later[(size_t) e->aux]);
356 clear_aux_for_edges ();
357 free (worklist);
360 /* Compute the insertion and deletion points for edge based LCM. */
362 static void
363 compute_insert_delete (struct edge_list *edge_list, sbitmap *antloc,
364 sbitmap *later, sbitmap *laterin, sbitmap *insert,
365 sbitmap *del)
367 int x;
368 basic_block bb;
370 FOR_EACH_BB_FN (bb, cfun)
371 bitmap_and_compl (del[bb->index], antloc[bb->index],
372 laterin[bb->index]);
374 for (x = 0; x < NUM_EDGES (edge_list); x++)
376 basic_block b = INDEX_EDGE_SUCC_BB (edge_list, x);
378 if (b == EXIT_BLOCK_PTR_FOR_FN (cfun))
379 bitmap_and_compl (insert[x], later[x],
380 laterin[last_basic_block_for_fn (cfun)]);
381 else
382 bitmap_and_compl (insert[x], later[x], laterin[b->index]);
386 /* Given local properties TRANSP, ANTLOC, AVLOC, KILL return the insert and
387 delete vectors for edge based LCM and return the AVIN, AVOUT bitmap.
388 map the insert vector to what edge an expression should be inserted on. */
390 struct edge_list *
391 pre_edge_lcm_avs (int n_exprs, sbitmap *transp,
392 sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
393 sbitmap *avin, sbitmap *avout,
394 sbitmap **insert, sbitmap **del)
396 sbitmap *antin, *antout, *earliest;
397 sbitmap *later, *laterin;
398 struct edge_list *edge_list;
399 int num_edges;
401 edge_list = create_edge_list ();
402 num_edges = NUM_EDGES (edge_list);
404 #ifdef LCM_DEBUG_INFO
405 if (dump_file)
407 fprintf (dump_file, "Edge List:\n");
408 verify_edge_list (dump_file, edge_list);
409 print_edge_list (dump_file, edge_list);
410 dump_bitmap_vector (dump_file, "transp", "", transp,
411 last_basic_block_for_fn (cfun));
412 dump_bitmap_vector (dump_file, "antloc", "", antloc,
413 last_basic_block_for_fn (cfun));
414 dump_bitmap_vector (dump_file, "avloc", "", avloc,
415 last_basic_block_for_fn (cfun));
416 dump_bitmap_vector (dump_file, "kill", "", kill,
417 last_basic_block_for_fn (cfun));
419 #endif
421 /* Compute global availability. */
422 compute_available (avloc, kill, avout, avin);
424 /* Compute global anticipatability. */
425 antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
426 antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
427 compute_antinout_edge (antloc, transp, antin, antout);
429 #ifdef LCM_DEBUG_INFO
430 if (dump_file)
432 dump_bitmap_vector (dump_file, "antin", "", antin,
433 last_basic_block_for_fn (cfun));
434 dump_bitmap_vector (dump_file, "antout", "", antout,
435 last_basic_block_for_fn (cfun));
437 #endif
439 /* Compute earliestness. */
440 earliest = sbitmap_vector_alloc (num_edges, n_exprs);
441 compute_earliest (edge_list, n_exprs, antin, antout, avout, kill, earliest);
443 #ifdef LCM_DEBUG_INFO
444 if (dump_file)
445 dump_bitmap_vector (dump_file, "earliest", "", earliest, num_edges);
446 #endif
448 sbitmap_vector_free (antout);
449 sbitmap_vector_free (antin);
451 later = sbitmap_vector_alloc (num_edges, n_exprs);
453 /* Allocate an extra element for the exit block in the laterin vector. */
454 laterin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
455 n_exprs);
456 compute_laterin (edge_list, earliest, antloc, later, laterin);
458 #ifdef LCM_DEBUG_INFO
459 if (dump_file)
461 dump_bitmap_vector (dump_file, "laterin", "", laterin,
462 last_basic_block_for_fn (cfun) + 1);
463 dump_bitmap_vector (dump_file, "later", "", later, num_edges);
465 #endif
467 sbitmap_vector_free (earliest);
469 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
470 *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
471 bitmap_vector_clear (*insert, num_edges);
472 bitmap_vector_clear (*del, last_basic_block_for_fn (cfun));
473 compute_insert_delete (edge_list, antloc, later, laterin, *insert, *del);
475 sbitmap_vector_free (laterin);
476 sbitmap_vector_free (later);
478 #ifdef LCM_DEBUG_INFO
479 if (dump_file)
481 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
482 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
483 last_basic_block_for_fn (cfun));
485 #endif
487 return edge_list;
490 /* Wrapper to allocate avin/avout and call pre_edge_lcm_avs. */
492 struct edge_list *
493 pre_edge_lcm (int n_exprs, sbitmap *transp,
494 sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
495 sbitmap **insert, sbitmap **del)
497 struct edge_list *edge_list;
498 sbitmap *avin, *avout;
500 avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
501 avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
503 edge_list = pre_edge_lcm_avs (n_exprs, transp, avloc, antloc, kill,
504 avin, avout, insert, del);
506 sbitmap_vector_free (avout);
507 sbitmap_vector_free (avin);
509 return edge_list;
512 /* Compute the AVIN and AVOUT vectors from the AVLOC and KILL vectors.
513 Return the number of passes we performed to iterate to a solution. */
515 void
516 compute_available (sbitmap *avloc, sbitmap *kill, sbitmap *avout,
517 sbitmap *avin)
519 edge e;
520 basic_block *worklist, *qin, *qout, *qend, bb;
521 unsigned int qlen;
522 edge_iterator ei;
524 /* Allocate a worklist array/queue. Entries are only added to the
525 list if they were not already on the list. So the size is
526 bounded by the number of basic blocks. */
527 qin = qout = worklist =
528 XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
530 /* We want a maximal solution. */
531 bitmap_vector_ones (avout, last_basic_block_for_fn (cfun));
533 /* Put every block on the worklist; this is necessary because of the
534 optimistic initialization of AVOUT above. Use inverted postorder
535 to make the dataflow problem require less iterations. */
536 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
537 int postorder_num = inverted_post_order_compute (postorder);
538 for (int i = 0; i < postorder_num; ++i)
540 bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
541 if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
542 || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
543 continue;
544 *qin++ = bb;
545 bb->aux = bb;
547 free (postorder);
549 qin = worklist;
550 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
551 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
553 /* Mark blocks which are successors of the entry block so that we
554 can easily identify them below. */
555 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
556 e->dest->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun);
558 /* Iterate until the worklist is empty. */
559 while (qlen)
561 /* Take the first entry off the worklist. */
562 bb = *qout++;
563 qlen--;
565 if (qout >= qend)
566 qout = worklist;
568 /* If one of the predecessor blocks is the ENTRY block, then the
569 intersection of avouts is the null set. We can identify such blocks
570 by the special value in the AUX field in the block structure. */
571 if (bb->aux == ENTRY_BLOCK_PTR_FOR_FN (cfun))
572 /* Do not clear the aux field for blocks which are successors of the
573 ENTRY block. That way we never add then to the worklist again. */
574 bitmap_clear (avin[bb->index]);
575 else
577 /* Clear the aux field of this block so that it can be added to
578 the worklist again if necessary. */
579 bb->aux = NULL;
580 bitmap_intersection_of_preds (avin[bb->index], avout, bb);
583 if (bitmap_ior_and_compl (avout[bb->index], avloc[bb->index],
584 avin[bb->index], kill[bb->index]))
585 /* If the out state of this block changed, then we need
586 to add the successors of this block to the worklist
587 if they are not already on the worklist. */
588 FOR_EACH_EDGE (e, ei, bb->succs)
589 if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
591 *qin++ = e->dest;
592 e->dest->aux = e;
593 qlen++;
595 if (qin >= qend)
596 qin = worklist;
600 clear_aux_for_edges ();
601 clear_aux_for_blocks ();
602 free (worklist);
605 /* Compute the farthest vector for edge based lcm. */
607 static void
608 compute_farthest (struct edge_list *edge_list, int n_exprs,
609 sbitmap *st_avout, sbitmap *st_avin, sbitmap *st_antin,
610 sbitmap *kill, sbitmap *farthest)
612 sbitmap difference, temp_bitmap;
613 int x, num_edges;
614 basic_block pred, succ;
616 num_edges = NUM_EDGES (edge_list);
618 difference = sbitmap_alloc (n_exprs);
619 temp_bitmap = sbitmap_alloc (n_exprs);
621 for (x = 0; x < num_edges; x++)
623 pred = INDEX_EDGE_PRED_BB (edge_list, x);
624 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
625 if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
626 bitmap_copy (farthest[x], st_avout[pred->index]);
627 else
629 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
630 bitmap_clear (farthest[x]);
631 else
633 bitmap_and_compl (difference, st_avout[pred->index],
634 st_antin[succ->index]);
635 bitmap_not (temp_bitmap, st_avin[succ->index]);
636 bitmap_and_or (farthest[x], difference,
637 kill[succ->index], temp_bitmap);
642 sbitmap_free (temp_bitmap);
643 sbitmap_free (difference);
646 /* Compute nearer and nearerout vectors for edge based lcm.
648 This is the mirror of compute_laterin, additional comments on the
649 implementation can be found before compute_laterin. */
651 static void
652 compute_nearerout (struct edge_list *edge_list, sbitmap *farthest,
653 sbitmap *st_avloc, sbitmap *nearer, sbitmap *nearerout)
655 int num_edges, i;
656 edge e;
657 basic_block *worklist, *tos, bb;
658 edge_iterator ei;
660 num_edges = NUM_EDGES (edge_list);
662 /* Allocate a worklist array/queue. Entries are only added to the
663 list if they were not already on the list. So the size is
664 bounded by the number of basic blocks. */
665 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
667 /* Initialize NEARER for each edge and build a mapping from an edge to
668 its index. */
669 for (i = 0; i < num_edges; i++)
670 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
672 /* We want a maximal solution. */
673 bitmap_vector_ones (nearer, num_edges);
675 /* Note that even though we want an optimistic setting of NEARER, we
676 do not want to be overly optimistic. Consider an incoming edge to
677 the exit block. That edge should always have a NEARER value the
678 same as FARTHEST for that edge. */
679 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
680 bitmap_copy (nearer[(size_t)e->aux], farthest[(size_t)e->aux]);
682 /* Add all the blocks to the worklist. This prevents an early exit
683 from the loop given our optimistic initialization of NEARER. */
684 FOR_EACH_BB_FN (bb, cfun)
686 *tos++ = bb;
687 bb->aux = bb;
690 /* Iterate until the worklist is empty. */
691 while (tos != worklist)
693 /* Take the first entry off the worklist. */
694 bb = *--tos;
695 bb->aux = NULL;
697 /* Compute the intersection of NEARER for each outgoing edge from B. */
698 bitmap_ones (nearerout[bb->index]);
699 FOR_EACH_EDGE (e, ei, bb->succs)
700 bitmap_and (nearerout[bb->index], nearerout[bb->index],
701 nearer[(size_t) e->aux]);
703 /* Calculate NEARER for all incoming edges. */
704 FOR_EACH_EDGE (e, ei, bb->preds)
705 if (bitmap_ior_and_compl (nearer[(size_t) e->aux],
706 farthest[(size_t) e->aux],
707 nearerout[e->dest->index],
708 st_avloc[e->dest->index])
709 /* If NEARER for an incoming edge was changed, then we need
710 to add the source of the incoming edge to the worklist. */
711 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) && e->src->aux == 0)
713 *tos++ = e->src;
714 e->src->aux = e;
718 /* Computation of insertion and deletion points requires computing NEAREROUT
719 for the ENTRY block. We allocated an extra entry in the NEAREROUT array
720 for just this purpose. */
721 bitmap_ones (nearerout[last_basic_block_for_fn (cfun)]);
722 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
723 bitmap_and (nearerout[last_basic_block_for_fn (cfun)],
724 nearerout[last_basic_block_for_fn (cfun)],
725 nearer[(size_t) e->aux]);
727 clear_aux_for_edges ();
728 free (tos);
731 /* Compute the insertion and deletion points for edge based LCM. */
733 static void
734 compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *st_avloc,
735 sbitmap *nearer, sbitmap *nearerout,
736 sbitmap *insert, sbitmap *del)
738 int x;
739 basic_block bb;
741 FOR_EACH_BB_FN (bb, cfun)
742 bitmap_and_compl (del[bb->index], st_avloc[bb->index],
743 nearerout[bb->index]);
745 for (x = 0; x < NUM_EDGES (edge_list); x++)
747 basic_block b = INDEX_EDGE_PRED_BB (edge_list, x);
748 if (b == ENTRY_BLOCK_PTR_FOR_FN (cfun))
749 bitmap_and_compl (insert[x], nearer[x],
750 nearerout[last_basic_block_for_fn (cfun)]);
751 else
752 bitmap_and_compl (insert[x], nearer[x], nearerout[b->index]);
756 /* Given local properties TRANSP, ST_AVLOC, ST_ANTLOC, KILL return the
757 insert and delete vectors for edge based reverse LCM. Returns an
758 edgelist which is used to map the insert vector to what edge
759 an expression should be inserted on. */
761 struct edge_list *
762 pre_edge_rev_lcm (int n_exprs, sbitmap *transp,
763 sbitmap *st_avloc, sbitmap *st_antloc, sbitmap *kill,
764 sbitmap **insert, sbitmap **del)
766 sbitmap *st_antin, *st_antout;
767 sbitmap *st_avout, *st_avin, *farthest;
768 sbitmap *nearer, *nearerout;
769 struct edge_list *edge_list;
770 int num_edges;
772 edge_list = create_edge_list ();
773 num_edges = NUM_EDGES (edge_list);
775 st_antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
776 st_antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
777 bitmap_vector_clear (st_antin, last_basic_block_for_fn (cfun));
778 bitmap_vector_clear (st_antout, last_basic_block_for_fn (cfun));
779 compute_antinout_edge (st_antloc, transp, st_antin, st_antout);
781 /* Compute global anticipatability. */
782 st_avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
783 st_avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
784 compute_available (st_avloc, kill, st_avout, st_avin);
786 #ifdef LCM_DEBUG_INFO
787 if (dump_file)
789 fprintf (dump_file, "Edge List:\n");
790 verify_edge_list (dump_file, edge_list);
791 print_edge_list (dump_file, edge_list);
792 dump_bitmap_vector (dump_file, "transp", "", transp,
793 last_basic_block_for_fn (cfun));
794 dump_bitmap_vector (dump_file, "st_avloc", "", st_avloc,
795 last_basic_block_for_fn (cfun));
796 dump_bitmap_vector (dump_file, "st_antloc", "", st_antloc,
797 last_basic_block_for_fn (cfun));
798 dump_bitmap_vector (dump_file, "st_antin", "", st_antin,
799 last_basic_block_for_fn (cfun));
800 dump_bitmap_vector (dump_file, "st_antout", "", st_antout,
801 last_basic_block_for_fn (cfun));
802 dump_bitmap_vector (dump_file, "st_kill", "", kill,
803 last_basic_block_for_fn (cfun));
805 #endif
807 #ifdef LCM_DEBUG_INFO
808 if (dump_file)
810 dump_bitmap_vector (dump_file, "st_avout", "", st_avout, last_basic_block_for_fn (cfun));
811 dump_bitmap_vector (dump_file, "st_avin", "", st_avin, last_basic_block_for_fn (cfun));
813 #endif
815 /* Compute farthestness. */
816 farthest = sbitmap_vector_alloc (num_edges, n_exprs);
817 compute_farthest (edge_list, n_exprs, st_avout, st_avin, st_antin,
818 kill, farthest);
820 #ifdef LCM_DEBUG_INFO
821 if (dump_file)
822 dump_bitmap_vector (dump_file, "farthest", "", farthest, num_edges);
823 #endif
825 sbitmap_vector_free (st_antin);
826 sbitmap_vector_free (st_antout);
828 sbitmap_vector_free (st_avin);
829 sbitmap_vector_free (st_avout);
831 nearer = sbitmap_vector_alloc (num_edges, n_exprs);
833 /* Allocate an extra element for the entry block. */
834 nearerout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
835 n_exprs);
836 compute_nearerout (edge_list, farthest, st_avloc, nearer, nearerout);
838 #ifdef LCM_DEBUG_INFO
839 if (dump_file)
841 dump_bitmap_vector (dump_file, "nearerout", "", nearerout,
842 last_basic_block_for_fn (cfun) + 1);
843 dump_bitmap_vector (dump_file, "nearer", "", nearer, num_edges);
845 #endif
847 sbitmap_vector_free (farthest);
849 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
850 *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
851 compute_rev_insert_delete (edge_list, st_avloc, nearer, nearerout,
852 *insert, *del);
854 sbitmap_vector_free (nearerout);
855 sbitmap_vector_free (nearer);
857 #ifdef LCM_DEBUG_INFO
858 if (dump_file)
860 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
861 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
862 last_basic_block_for_fn (cfun));
864 #endif
865 return edge_list;