]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lcm.c
2014-01-15 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[thirdparty/gcc.git] / gcc / lcm.c
CommitLineData
9ca8d59e 1/* Generic partial redundancy elimination with lazy code motion support.
3aea1f79 2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
e48ba7af 3
f12b58b3 4This file is part of GCC.
e48ba7af 5
f12b58b3 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
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
e48ba7af 10
f12b58b3 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.
e48ba7af 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
e48ba7af 19
20/* These routines are meant to be used by various optimization
5ab08585 21 passes which can be modeled as lazy code motion problems.
e48ba7af 22 Including, but not limited to:
23
24 * Traditional partial redundancy elimination.
25
26 * Placement of caller/caller register save/restores.
27
28 * Load/store motion.
29
30 * Copy motion.
31
32 * Conversion of flat register files to a stacked register
33 model.
34
35 * Dead load/store elimination.
36
37 These routines accept as input:
38
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.
43
44 * Bitmaps of local properties (computed, transparent and
45 anticipatable expressions).
46
47 The output of these routines is bitmap of redundant computations
48 and a bitmap of optimal placement points. */
49
50
51#include "config.h"
52#include "system.h"
805e22b2 53#include "coretypes.h"
54#include "tm.h"
e48ba7af 55#include "rtl.h"
56#include "regs.h"
57#include "hard-reg-set.h"
58#include "flags.h"
e48ba7af 59#include "insn-config.h"
60#include "recog.h"
61#include "basic-block.h"
18862b5a 62#include "tm_p.h"
6ce54148 63#include "function.h"
0f71a633 64#include "sbitmap.h"
b9ed1410 65#include "dumpfile.h"
e48ba7af 66
7bcd381b 67/* Edge based LCM routines. */
3ad4992f 68static void compute_antinout_edge (sbitmap *, sbitmap *, sbitmap *, sbitmap *);
69static void compute_earliest (struct edge_list *, int, sbitmap *, sbitmap *,
70 sbitmap *, sbitmap *, sbitmap *);
71static void compute_laterin (struct edge_list *, sbitmap *, sbitmap *,
72 sbitmap *, sbitmap *);
73static void compute_insert_delete (struct edge_list *edge_list, sbitmap *,
74 sbitmap *, sbitmap *, sbitmap *, sbitmap *);
7bcd381b 75
76/* Edge based LCM routines on a reverse flowgraph. */
3ad4992f 77static void compute_farthest (struct edge_list *, int, sbitmap *, sbitmap *,
78 sbitmap*, sbitmap *, sbitmap *);
79static void compute_nearerout (struct edge_list *, sbitmap *, sbitmap *,
80 sbitmap *, sbitmap *);
81static void compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *,
82 sbitmap *, sbitmap *, sbitmap *,
83 sbitmap *);
7bcd381b 84\f
85/* Edge based lcm routines. */
3b7e1f27 86
5ab08585 87/* Compute expression anticipatability at entrance and exit of each block.
88 This is done based on the flow graph, and not on the pred-succ lists.
7bcd381b 89 Other than that, its pretty much identical to compute_antinout. */
e48ba7af 90
91static void
3ad4992f 92compute_antinout_edge (sbitmap *antloc, sbitmap *transp, sbitmap *antin,
93 sbitmap *antout)
e48ba7af 94{
4c26117a 95 basic_block bb;
7bcd381b 96 edge e;
2c59145b 97 basic_block *worklist, *qin, *qout, *qend;
98 unsigned int qlen;
cd665a06 99 edge_iterator ei;
3b7e1f27 100
2325f0e2 101 /* Allocate a worklist array/queue. Entries are only added to the
102 list if they were not already on the list. So the size is
103 bounded by the number of basic blocks. */
a28770e1 104 qin = qout = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
e48ba7af 105
2325f0e2 106 /* We want a maximal solution, so make an optimistic initialization of
107 ANTIN. */
fe672ac0 108 bitmap_vector_ones (antin, last_basic_block_for_fn (cfun));
e48ba7af 109
5d6931e2 110 /* Put every block on the worklist; this is necessary because of the
111 optimistic initialization of ANTIN above. */
7a46197b 112 FOR_EACH_BB_REVERSE_FN (bb, cfun)
e48ba7af 113 {
ea0041f4 114 *qin++ = bb;
4c26117a 115 bb->aux = bb;
2325f0e2 116 }
5ab08585 117
2c59145b 118 qin = worklist;
a28770e1 119 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
120 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
e48ba7af 121
5d6931e2 122 /* Mark blocks which are predecessors of the exit block so that we
123 can easily identify them below. */
34154e27 124 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
125 e->src->aux = EXIT_BLOCK_PTR_FOR_FN (cfun);
5d6931e2 126
2325f0e2 127 /* Iterate until the worklist is empty. */
2c59145b 128 while (qlen)
2325f0e2 129 {
130 /* Take the first entry off the worklist. */
4c26117a 131 bb = *qout++;
2c59145b 132 qlen--;
3b7e1f27 133
2c59145b 134 if (qout >= qend)
8851e806 135 qout = worklist;
e48ba7af 136
34154e27 137 if (bb->aux == EXIT_BLOCK_PTR_FOR_FN (cfun))
9ca8d59e 138 /* Do not clear the aux field for blocks which are predecessors of
139 the EXIT block. That way we never add then to the worklist
140 again. */
53c5d9d4 141 bitmap_clear (antout[bb->index]);
2325f0e2 142 else
143 {
144 /* Clear the aux field of this block so that it can be added to
145 the worklist again if necessary. */
4c26117a 146 bb->aux = NULL;
08b7917c 147 bitmap_intersection_of_succs (antout[bb->index], antin, bb);
2325f0e2 148 }
7bcd381b 149
53c5d9d4 150 if (bitmap_or_and (antin[bb->index], antloc[bb->index],
4c26117a 151 transp[bb->index], antout[bb->index]))
9ca8d59e 152 /* If the in state of this block changed, then we need
153 to add the predecessors of this block to the worklist
154 if they are not already on the worklist. */
cd665a06 155 FOR_EACH_EDGE (e, ei, bb->preds)
34154e27 156 if (!e->src->aux && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
e48ba7af 157 {
2c59145b 158 *qin++ = e->src;
9ca8d59e 159 e->src->aux = e;
2c59145b 160 qlen++;
161 if (qin >= qend)
8851e806 162 qin = worklist;
e48ba7af 163 }
e48ba7af 164 }
9ca8d59e 165
82f7392b 166 clear_aux_for_edges ();
167 clear_aux_for_blocks ();
2c59145b 168 free (worklist);
e48ba7af 169}
170
7bcd381b 171/* Compute the earliest vector for edge based lcm. */
9ca8d59e 172
e48ba7af 173static void
3ad4992f 174compute_earliest (struct edge_list *edge_list, int n_exprs, sbitmap *antin,
175 sbitmap *antout, sbitmap *avout, sbitmap *kill,
176 sbitmap *earliest)
e48ba7af 177{
7bcd381b 178 sbitmap difference, temp_bitmap;
5ab08585 179 int x, num_edges;
7bcd381b 180 basic_block pred, succ;
e48ba7af 181
7bcd381b 182 num_edges = NUM_EDGES (edge_list);
e48ba7af 183
7bcd381b 184 difference = sbitmap_alloc (n_exprs);
185 temp_bitmap = sbitmap_alloc (n_exprs);
e48ba7af 186
7bcd381b 187 for (x = 0; x < num_edges; x++)
e48ba7af 188 {
7bcd381b 189 pred = INDEX_EDGE_PRED_BB (edge_list, x);
190 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
34154e27 191 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
53c5d9d4 192 bitmap_copy (earliest[x], antin[succ->index]);
7bcd381b 193 else
8851e806 194 {
34154e27 195 if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
53c5d9d4 196 bitmap_clear (earliest[x]);
7bcd381b 197 else
e48ba7af 198 {
53c5d9d4 199 bitmap_and_compl (difference, antin[succ->index],
b3d6de89 200 avout[pred->index]);
53c5d9d4 201 bitmap_not (temp_bitmap, antout[pred->index]);
202 bitmap_and_or (earliest[x], difference,
b3d6de89 203 kill[pred->index], temp_bitmap);
e48ba7af 204 }
205 }
e48ba7af 206 }
9ca8d59e 207
f5123ed5 208 sbitmap_free (temp_bitmap);
209 sbitmap_free (difference);
e48ba7af 210}
211
2325f0e2 212/* later(p,s) is dependent on the calculation of laterin(p).
213 laterin(p) is dependent on the calculation of later(p2,p).
214
215 laterin(ENTRY) is defined as all 0's
216 later(ENTRY, succs(ENTRY)) are defined using laterin(ENTRY)
217 laterin(succs(ENTRY)) is defined by later(ENTRY, succs(ENTRY)).
218
219 If we progress in this manner, starting with all basic blocks
220 in the work list, anytime we change later(bb), we need to add
221 succs(bb) to the worklist if they are not already on the worklist.
222
223 Boundary conditions:
224
225 We prime the worklist all the normal basic blocks. The ENTRY block can
226 never be added to the worklist since it is never the successor of any
227 block. We explicitly prevent the EXIT block from being added to the
228 worklist.
229
230 We optimistically initialize LATER. That is the only time this routine
231 will compute LATER for an edge out of the entry block since the entry
232 block is never on the worklist. Thus, LATERIN is neither used nor
233 computed for the ENTRY block.
234
235 Since the EXIT block is never added to the worklist, we will neither
236 use nor compute LATERIN for the exit block. Edges which reach the
237 EXIT block are handled in the normal fashion inside the loop. However,
238 the insertion/deletion computation needs LATERIN(EXIT), so we have
239 to compute it. */
5ab08585 240
e48ba7af 241static void
3ad4992f 242compute_laterin (struct edge_list *edge_list, sbitmap *earliest,
243 sbitmap *antloc, sbitmap *later, sbitmap *laterin)
e48ba7af 244{
4c26117a 245 int num_edges, i;
2325f0e2 246 edge e;
4c26117a 247 basic_block *worklist, *qin, *qout, *qend, bb;
2c59145b 248 unsigned int qlen;
cd665a06 249 edge_iterator ei;
e48ba7af 250
7bcd381b 251 num_edges = NUM_EDGES (edge_list);
e48ba7af 252
2325f0e2 253 /* Allocate a worklist array/queue. Entries are only added to the
254 list if they were not already on the list. So the size is
255 bounded by the number of basic blocks. */
2c59145b 256 qin = qout = worklist
a28770e1 257 = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
2325f0e2 258
259 /* Initialize a mapping from each edge to its index. */
260 for (i = 0; i < num_edges; i++)
9ffd5d6d 261 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
2325f0e2 262
263 /* We want a maximal solution, so initially consider LATER true for
264 all edges. This allows propagation through a loop since the incoming
265 loop edge will have LATER set, so if all the other incoming edges
266 to the loop are set, then LATERIN will be set for the head of the
267 loop.
268
269 If the optimistic setting of LATER on that edge was incorrect (for
270 example the expression is ANTLOC in a block within the loop) then
271 this algorithm will detect it when we process the block at the head
272 of the optimistic edge. That will requeue the affected blocks. */
53c5d9d4 273 bitmap_vector_ones (later, num_edges);
2325f0e2 274
048599b9 275 /* Note that even though we want an optimistic setting of LATER, we
276 do not want to be overly optimistic. Consider an outgoing edge from
277 the entry block. That edge should always have a LATER value the
278 same as EARLIEST for that edge. */
34154e27 279 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
53c5d9d4 280 bitmap_copy (later[(size_t) e->aux], earliest[(size_t) e->aux]);
048599b9 281
2325f0e2 282 /* Add all the blocks to the worklist. This prevents an early exit from
283 the loop given our optimistic initialization of LATER above. */
fc00614f 284 FOR_EACH_BB_FN (bb, cfun)
e48ba7af 285 {
4c26117a 286 *qin++ = bb;
287 bb->aux = bb;
7bcd381b 288 }
d4d93ea0 289
2c59145b 290 /* Note that we do not use the last allocated element for our queue,
4d2e5d52 291 as EXIT_BLOCK is never inserted into it. */
d4d93ea0 292 qin = worklist;
a28770e1 293 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
294 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
7bcd381b 295
2325f0e2 296 /* Iterate until the worklist is empty. */
2c59145b 297 while (qlen)
7bcd381b 298 {
2325f0e2 299 /* Take the first entry off the worklist. */
4c26117a 300 bb = *qout++;
301 bb->aux = NULL;
2c59145b 302 qlen--;
303 if (qout >= qend)
8851e806 304 qout = worklist;
2325f0e2 305
306 /* Compute the intersection of LATERIN for each incoming edge to B. */
53c5d9d4 307 bitmap_ones (laterin[bb->index]);
cd665a06 308 FOR_EACH_EDGE (e, ei, bb->preds)
53c5d9d4 309 bitmap_and (laterin[bb->index], laterin[bb->index],
d4d93ea0 310 later[(size_t)e->aux]);
2325f0e2 311
312 /* Calculate LATER for all outgoing edges. */
cd665a06 313 FOR_EACH_EDGE (e, ei, bb->succs)
53c5d9d4 314 if (bitmap_ior_and_compl (later[(size_t) e->aux],
b3d6de89 315 earliest[(size_t) e->aux],
316 laterin[e->src->index],
317 antloc[e->src->index])
9ca8d59e 318 /* If LATER for an outgoing edge was changed, then we need
319 to add the target of the outgoing edge to the worklist. */
34154e27 320 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest->aux == 0)
9ca8d59e 321 {
2c59145b 322 *qin++ = e->dest;
9ca8d59e 323 e->dest->aux = e;
2c59145b 324 qlen++;
325 if (qin >= qend)
326 qin = worklist;
9ca8d59e 327 }
e48ba7af 328 }
329
2325f0e2 330 /* Computation of insertion and deletion points requires computing LATERIN
331 for the EXIT block. We allocated an extra entry in the LATERIN array
332 for just this purpose. */
fe672ac0 333 bitmap_ones (laterin[last_basic_block_for_fn (cfun)]);
34154e27 334 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
fe672ac0 335 bitmap_and (laterin[last_basic_block_for_fn (cfun)],
336 laterin[last_basic_block_for_fn (cfun)],
9ffd5d6d 337 later[(size_t) e->aux]);
2325f0e2 338
82f7392b 339 clear_aux_for_edges ();
2c59145b 340 free (worklist);
e48ba7af 341}
342
7bcd381b 343/* Compute the insertion and deletion points for edge based LCM. */
9ca8d59e 344
7bcd381b 345static void
3ad4992f 346compute_insert_delete (struct edge_list *edge_list, sbitmap *antloc,
347 sbitmap *later, sbitmap *laterin, sbitmap *insert,
9ce37fa7 348 sbitmap *del)
7bcd381b 349{
350 int x;
4c26117a 351 basic_block bb;
e48ba7af 352
fc00614f 353 FOR_EACH_BB_FN (bb, cfun)
53c5d9d4 354 bitmap_and_compl (del[bb->index], antloc[bb->index],
d4d93ea0 355 laterin[bb->index]);
5ab08585 356
7bcd381b 357 for (x = 0; x < NUM_EDGES (edge_list); x++)
358 {
359 basic_block b = INDEX_EDGE_SUCC_BB (edge_list, x);
9ca8d59e 360
34154e27 361 if (b == EXIT_BLOCK_PTR_FOR_FN (cfun))
fe672ac0 362 bitmap_and_compl (insert[x], later[x],
363 laterin[last_basic_block_for_fn (cfun)]);
7bcd381b 364 else
53c5d9d4 365 bitmap_and_compl (insert[x], later[x], laterin[b->index]);
7bcd381b 366 }
367}
e48ba7af 368
9ca8d59e 369/* Given local properties TRANSP, ANTLOC, AVOUT, KILL return the insert and
370 delete vectors for edge based LCM. Returns an edgelist which is used to
371 map the insert vector to what edge an expression should be inserted on. */
e48ba7af 372
7bcd381b 373struct edge_list *
3f5be5f4 374pre_edge_lcm (int n_exprs, sbitmap *transp,
3ad4992f 375 sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
9ce37fa7 376 sbitmap **insert, sbitmap **del)
e48ba7af 377{
7bcd381b 378 sbitmap *antin, *antout, *earliest;
379 sbitmap *avin, *avout;
380 sbitmap *later, *laterin;
381 struct edge_list *edge_list;
382 int num_edges;
e48ba7af 383
7bcd381b 384 edge_list = create_edge_list ();
385 num_edges = NUM_EDGES (edge_list);
e48ba7af 386
7bcd381b 387#ifdef LCM_DEBUG_INFO
3f5be5f4 388 if (dump_file)
e48ba7af 389 {
3f5be5f4 390 fprintf (dump_file, "Edge List:\n");
391 verify_edge_list (dump_file, edge_list);
392 print_edge_list (dump_file, edge_list);
fe672ac0 393 dump_bitmap_vector (dump_file, "transp", "", transp,
394 last_basic_block_for_fn (cfun));
395 dump_bitmap_vector (dump_file, "antloc", "", antloc,
396 last_basic_block_for_fn (cfun));
397 dump_bitmap_vector (dump_file, "avloc", "", avloc,
398 last_basic_block_for_fn (cfun));
399 dump_bitmap_vector (dump_file, "kill", "", kill,
400 last_basic_block_for_fn (cfun));
e48ba7af 401 }
7bcd381b 402#endif
e48ba7af 403
7bcd381b 404 /* Compute global availability. */
fe672ac0 405 avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
406 avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
7bcd381b 407 compute_available (avloc, kill, avout, avin);
cca23eb2 408 sbitmap_vector_free (avin);
e48ba7af 409
7bcd381b 410 /* Compute global anticipatability. */
fe672ac0 411 antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
412 antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
7bcd381b 413 compute_antinout_edge (antloc, transp, antin, antout);
e48ba7af 414
7bcd381b 415#ifdef LCM_DEBUG_INFO
3f5be5f4 416 if (dump_file)
e48ba7af 417 {
fe672ac0 418 dump_bitmap_vector (dump_file, "antin", "", antin,
419 last_basic_block_for_fn (cfun));
420 dump_bitmap_vector (dump_file, "antout", "", antout,
421 last_basic_block_for_fn (cfun));
e48ba7af 422 }
7bcd381b 423#endif
e48ba7af 424
7bcd381b 425 /* Compute earliestness. */
426 earliest = sbitmap_vector_alloc (num_edges, n_exprs);
427 compute_earliest (edge_list, n_exprs, antin, antout, avout, kill, earliest);
e48ba7af 428
7bcd381b 429#ifdef LCM_DEBUG_INFO
3f5be5f4 430 if (dump_file)
53c5d9d4 431 dump_bitmap_vector (dump_file, "earliest", "", earliest, num_edges);
7bcd381b 432#endif
e48ba7af 433
cca23eb2 434 sbitmap_vector_free (antout);
435 sbitmap_vector_free (antin);
436 sbitmap_vector_free (avout);
e48ba7af 437
7bcd381b 438 later = sbitmap_vector_alloc (num_edges, n_exprs);
9ca8d59e 439
7bcd381b 440 /* Allocate an extra element for the exit block in the laterin vector. */
fe672ac0 441 laterin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
442 n_exprs);
2325f0e2 443 compute_laterin (edge_list, earliest, antloc, later, laterin);
444
7bcd381b 445#ifdef LCM_DEBUG_INFO
3f5be5f4 446 if (dump_file)
7bcd381b 447 {
fe672ac0 448 dump_bitmap_vector (dump_file, "laterin", "", laterin,
449 last_basic_block_for_fn (cfun) + 1);
53c5d9d4 450 dump_bitmap_vector (dump_file, "later", "", later, num_edges);
7bcd381b 451 }
452#endif
e48ba7af 453
cca23eb2 454 sbitmap_vector_free (earliest);
7bcd381b 455
456 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
fe672ac0 457 *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
53c5d9d4 458 bitmap_vector_clear (*insert, num_edges);
fe672ac0 459 bitmap_vector_clear (*del, last_basic_block_for_fn (cfun));
9ce37fa7 460 compute_insert_delete (edge_list, antloc, later, laterin, *insert, *del);
e48ba7af 461
cca23eb2 462 sbitmap_vector_free (laterin);
463 sbitmap_vector_free (later);
7bcd381b 464
465#ifdef LCM_DEBUG_INFO
3f5be5f4 466 if (dump_file)
e48ba7af 467 {
53c5d9d4 468 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
469 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
fe672ac0 470 last_basic_block_for_fn (cfun));
e48ba7af 471 }
7bcd381b 472#endif
e48ba7af 473
7bcd381b 474 return edge_list;
475}
3b7e1f27 476
477/* Compute the AVIN and AVOUT vectors from the AVLOC and KILL vectors.
478 Return the number of passes we performed to iterate to a solution. */
479
2325f0e2 480void
3ad4992f 481compute_available (sbitmap *avloc, sbitmap *kill, sbitmap *avout,
482 sbitmap *avin)
e48ba7af 483{
3b7e1f27 484 edge e;
4c26117a 485 basic_block *worklist, *qin, *qout, *qend, bb;
3b7e1f27 486 unsigned int qlen;
cd665a06 487 edge_iterator ei;
3b7e1f27 488
489 /* Allocate a worklist array/queue. Entries are only added to the
490 list if they were not already on the list. So the size is
491 bounded by the number of basic blocks. */
48e1416a 492 qin = qout = worklist =
a28770e1 493 XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
3b7e1f27 494
495 /* We want a maximal solution. */
fe672ac0 496 bitmap_vector_ones (avout, last_basic_block_for_fn (cfun));
3b7e1f27 497
498 /* Put every block on the worklist; this is necessary because of the
499 optimistic initialization of AVOUT above. */
fc00614f 500 FOR_EACH_BB_FN (bb, cfun)
3b7e1f27 501 {
4c26117a 502 *qin++ = bb;
503 bb->aux = bb;
3b7e1f27 504 }
505
506 qin = worklist;
a28770e1 507 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
508 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
3b7e1f27 509
510 /* Mark blocks which are successors of the entry block so that we
511 can easily identify them below. */
34154e27 512 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
513 e->dest->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun);
3b7e1f27 514
515 /* Iterate until the worklist is empty. */
516 while (qlen)
517 {
518 /* Take the first entry off the worklist. */
4c26117a 519 bb = *qout++;
3b7e1f27 520 qlen--;
521
522 if (qout >= qend)
8851e806 523 qout = worklist;
3b7e1f27 524
525 /* If one of the predecessor blocks is the ENTRY block, then the
526 intersection of avouts is the null set. We can identify such blocks
527 by the special value in the AUX field in the block structure. */
34154e27 528 if (bb->aux == ENTRY_BLOCK_PTR_FOR_FN (cfun))
3b7e1f27 529 /* Do not clear the aux field for blocks which are successors of the
530 ENTRY block. That way we never add then to the worklist again. */
53c5d9d4 531 bitmap_clear (avin[bb->index]);
3b7e1f27 532 else
533 {
534 /* Clear the aux field of this block so that it can be added to
535 the worklist again if necessary. */
4c26117a 536 bb->aux = NULL;
08b7917c 537 bitmap_intersection_of_preds (avin[bb->index], avout, bb);
3b7e1f27 538 }
539
53c5d9d4 540 if (bitmap_ior_and_compl (avout[bb->index], avloc[bb->index],
d4d93ea0 541 avin[bb->index], kill[bb->index]))
3b7e1f27 542 /* If the out state of this block changed, then we need
543 to add the successors of this block to the worklist
544 if they are not already on the worklist. */
cd665a06 545 FOR_EACH_EDGE (e, ei, bb->succs)
34154e27 546 if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
3b7e1f27 547 {
548 *qin++ = e->dest;
549 e->dest->aux = e;
550 qlen++;
551
552 if (qin >= qend)
8851e806 553 qin = worklist;
3b7e1f27 554 }
555 }
556
557 clear_aux_for_edges ();
558 clear_aux_for_blocks ();
559 free (worklist);
e48ba7af 560}
561
7bcd381b 562/* Compute the farthest vector for edge based lcm. */
9ca8d59e 563
e48ba7af 564static void
3ad4992f 565compute_farthest (struct edge_list *edge_list, int n_exprs,
566 sbitmap *st_avout, sbitmap *st_avin, sbitmap *st_antin,
567 sbitmap *kill, sbitmap *farthest)
e48ba7af 568{
7bcd381b 569 sbitmap difference, temp_bitmap;
5ab08585 570 int x, num_edges;
7bcd381b 571 basic_block pred, succ;
e48ba7af 572
7bcd381b 573 num_edges = NUM_EDGES (edge_list);
e48ba7af 574
7bcd381b 575 difference = sbitmap_alloc (n_exprs);
576 temp_bitmap = sbitmap_alloc (n_exprs);
e48ba7af 577
7bcd381b 578 for (x = 0; x < num_edges; x++)
e48ba7af 579 {
7bcd381b 580 pred = INDEX_EDGE_PRED_BB (edge_list, x);
581 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
34154e27 582 if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
53c5d9d4 583 bitmap_copy (farthest[x], st_avout[pred->index]);
7bcd381b 584 else
e48ba7af 585 {
34154e27 586 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
53c5d9d4 587 bitmap_clear (farthest[x]);
7bcd381b 588 else
589 {
53c5d9d4 590 bitmap_and_compl (difference, st_avout[pred->index],
b3d6de89 591 st_antin[succ->index]);
53c5d9d4 592 bitmap_not (temp_bitmap, st_avin[succ->index]);
593 bitmap_and_or (farthest[x], difference,
b3d6de89 594 kill[succ->index], temp_bitmap);
7bcd381b 595 }
e48ba7af 596 }
e48ba7af 597 }
9ca8d59e 598
f5123ed5 599 sbitmap_free (temp_bitmap);
600 sbitmap_free (difference);
e48ba7af 601}
602
2325f0e2 603/* Compute nearer and nearerout vectors for edge based lcm.
604
605 This is the mirror of compute_laterin, additional comments on the
606 implementation can be found before compute_laterin. */
607
e48ba7af 608static void
3ad4992f 609compute_nearerout (struct edge_list *edge_list, sbitmap *farthest,
610 sbitmap *st_avloc, sbitmap *nearer, sbitmap *nearerout)
e48ba7af 611{
4c26117a 612 int num_edges, i;
2325f0e2 613 edge e;
4c26117a 614 basic_block *worklist, *tos, bb;
cd665a06 615 edge_iterator ei;
e48ba7af 616
7bcd381b 617 num_edges = NUM_EDGES (edge_list);
e48ba7af 618
2325f0e2 619 /* Allocate a worklist array/queue. Entries are only added to the
620 list if they were not already on the list. So the size is
621 bounded by the number of basic blocks. */
a28770e1 622 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
e48ba7af 623
2325f0e2 624 /* Initialize NEARER for each edge and build a mapping from an edge to
625 its index. */
626 for (i = 0; i < num_edges; i++)
9ffd5d6d 627 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
7bcd381b 628
2325f0e2 629 /* We want a maximal solution. */
53c5d9d4 630 bitmap_vector_ones (nearer, num_edges);
2325f0e2 631
048599b9 632 /* Note that even though we want an optimistic setting of NEARER, we
633 do not want to be overly optimistic. Consider an incoming edge to
634 the exit block. That edge should always have a NEARER value the
635 same as FARTHEST for that edge. */
34154e27 636 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
53c5d9d4 637 bitmap_copy (nearer[(size_t)e->aux], farthest[(size_t)e->aux]);
048599b9 638
2325f0e2 639 /* Add all the blocks to the worklist. This prevents an early exit
640 from the loop given our optimistic initialization of NEARER. */
fc00614f 641 FOR_EACH_BB_FN (bb, cfun)
e48ba7af 642 {
4c26117a 643 *tos++ = bb;
644 bb->aux = bb;
7bcd381b 645 }
5ab08585 646
2325f0e2 647 /* Iterate until the worklist is empty. */
648 while (tos != worklist)
7bcd381b 649 {
2325f0e2 650 /* Take the first entry off the worklist. */
4c26117a 651 bb = *--tos;
652 bb->aux = NULL;
2325f0e2 653
654 /* Compute the intersection of NEARER for each outgoing edge from B. */
53c5d9d4 655 bitmap_ones (nearerout[bb->index]);
cd665a06 656 FOR_EACH_EDGE (e, ei, bb->succs)
53c5d9d4 657 bitmap_and (nearerout[bb->index], nearerout[bb->index],
9ffd5d6d 658 nearer[(size_t) e->aux]);
2325f0e2 659
660 /* Calculate NEARER for all incoming edges. */
cd665a06 661 FOR_EACH_EDGE (e, ei, bb->preds)
53c5d9d4 662 if (bitmap_ior_and_compl (nearer[(size_t) e->aux],
b3d6de89 663 farthest[(size_t) e->aux],
664 nearerout[e->dest->index],
665 st_avloc[e->dest->index])
9ca8d59e 666 /* If NEARER for an incoming edge was changed, then we need
667 to add the source of the incoming edge to the worklist. */
34154e27 668 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) && e->src->aux == 0)
9ca8d59e 669 {
670 *tos++ = e->src;
671 e->src->aux = e;
672 }
7bcd381b 673 }
e48ba7af 674
2325f0e2 675 /* Computation of insertion and deletion points requires computing NEAREROUT
676 for the ENTRY block. We allocated an extra entry in the NEAREROUT array
677 for just this purpose. */
fe672ac0 678 bitmap_ones (nearerout[last_basic_block_for_fn (cfun)]);
34154e27 679 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
fe672ac0 680 bitmap_and (nearerout[last_basic_block_for_fn (cfun)],
681 nearerout[last_basic_block_for_fn (cfun)],
9ffd5d6d 682 nearer[(size_t) e->aux]);
2325f0e2 683
82f7392b 684 clear_aux_for_edges ();
2325f0e2 685 free (tos);
7bcd381b 686}
e48ba7af 687
7bcd381b 688/* Compute the insertion and deletion points for edge based LCM. */
9ca8d59e 689
e48ba7af 690static void
3ad4992f 691compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *st_avloc,
692 sbitmap *nearer, sbitmap *nearerout,
9ce37fa7 693 sbitmap *insert, sbitmap *del)
e48ba7af 694{
7bcd381b 695 int x;
4c26117a 696 basic_block bb;
e48ba7af 697
fc00614f 698 FOR_EACH_BB_FN (bb, cfun)
53c5d9d4 699 bitmap_and_compl (del[bb->index], st_avloc[bb->index],
d4d93ea0 700 nearerout[bb->index]);
5ab08585 701
7bcd381b 702 for (x = 0; x < NUM_EDGES (edge_list); x++)
e48ba7af 703 {
7bcd381b 704 basic_block b = INDEX_EDGE_PRED_BB (edge_list, x);
34154e27 705 if (b == ENTRY_BLOCK_PTR_FOR_FN (cfun))
fe672ac0 706 bitmap_and_compl (insert[x], nearer[x],
707 nearerout[last_basic_block_for_fn (cfun)]);
e48ba7af 708 else
53c5d9d4 709 bitmap_and_compl (insert[x], nearer[x], nearerout[b->index]);
e48ba7af 710 }
e48ba7af 711}
712
5ab08585 713/* Given local properties TRANSP, ST_AVLOC, ST_ANTLOC, KILL return the
7bcd381b 714 insert and delete vectors for edge based reverse LCM. Returns an
715 edgelist which is used to map the insert vector to what edge
716 an expression should be inserted on. */
e48ba7af 717
7bcd381b 718struct edge_list *
3f5be5f4 719pre_edge_rev_lcm (int n_exprs, sbitmap *transp,
3ad4992f 720 sbitmap *st_avloc, sbitmap *st_antloc, sbitmap *kill,
9ce37fa7 721 sbitmap **insert, sbitmap **del)
e48ba7af 722{
7bcd381b 723 sbitmap *st_antin, *st_antout;
724 sbitmap *st_avout, *st_avin, *farthest;
725 sbitmap *nearer, *nearerout;
726 struct edge_list *edge_list;
27548a74 727 int num_edges;
7bcd381b 728
729 edge_list = create_edge_list ();
730 num_edges = NUM_EDGES (edge_list);
731
fe672ac0 732 st_antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
733 st_antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
734 bitmap_vector_clear (st_antin, last_basic_block_for_fn (cfun));
735 bitmap_vector_clear (st_antout, last_basic_block_for_fn (cfun));
7bcd381b 736 compute_antinout_edge (st_antloc, transp, st_antin, st_antout);
737
738 /* Compute global anticipatability. */
fe672ac0 739 st_avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
740 st_avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
7bcd381b 741 compute_available (st_avloc, kill, st_avout, st_avin);
742
743#ifdef LCM_DEBUG_INFO
3f5be5f4 744 if (dump_file)
7bcd381b 745 {
3f5be5f4 746 fprintf (dump_file, "Edge List:\n");
747 verify_edge_list (dump_file, edge_list);
748 print_edge_list (dump_file, edge_list);
fe672ac0 749 dump_bitmap_vector (dump_file, "transp", "", transp,
750 last_basic_block_for_fn (cfun));
751 dump_bitmap_vector (dump_file, "st_avloc", "", st_avloc,
752 last_basic_block_for_fn (cfun));
753 dump_bitmap_vector (dump_file, "st_antloc", "", st_antloc,
754 last_basic_block_for_fn (cfun));
755 dump_bitmap_vector (dump_file, "st_antin", "", st_antin,
756 last_basic_block_for_fn (cfun));
757 dump_bitmap_vector (dump_file, "st_antout", "", st_antout,
758 last_basic_block_for_fn (cfun));
759 dump_bitmap_vector (dump_file, "st_kill", "", kill,
760 last_basic_block_for_fn (cfun));
7bcd381b 761 }
762#endif
e48ba7af 763
7bcd381b 764#ifdef LCM_DEBUG_INFO
3f5be5f4 765 if (dump_file)
7bcd381b 766 {
fe672ac0 767 dump_bitmap_vector (dump_file, "st_avout", "", st_avout, last_basic_block_for_fn (cfun));
768 dump_bitmap_vector (dump_file, "st_avin", "", st_avin, last_basic_block_for_fn (cfun));
7bcd381b 769 }
770#endif
e48ba7af 771
7bcd381b 772 /* Compute farthestness. */
773 farthest = sbitmap_vector_alloc (num_edges, n_exprs);
5ab08585 774 compute_farthest (edge_list, n_exprs, st_avout, st_avin, st_antin,
7bcd381b 775 kill, farthest);
776
777#ifdef LCM_DEBUG_INFO
3f5be5f4 778 if (dump_file)
53c5d9d4 779 dump_bitmap_vector (dump_file, "farthest", "", farthest, num_edges);
7bcd381b 780#endif
781
cca23eb2 782 sbitmap_vector_free (st_antin);
783 sbitmap_vector_free (st_antout);
784
785 sbitmap_vector_free (st_avin);
786 sbitmap_vector_free (st_avout);
7bcd381b 787
788 nearer = sbitmap_vector_alloc (num_edges, n_exprs);
9ca8d59e 789
7bcd381b 790 /* Allocate an extra element for the entry block. */
fe672ac0 791 nearerout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
792 n_exprs);
2325f0e2 793 compute_nearerout (edge_list, farthest, st_avloc, nearer, nearerout);
7bcd381b 794
795#ifdef LCM_DEBUG_INFO
3f5be5f4 796 if (dump_file)
e48ba7af 797 {
53c5d9d4 798 dump_bitmap_vector (dump_file, "nearerout", "", nearerout,
fe672ac0 799 last_basic_block_for_fn (cfun) + 1);
53c5d9d4 800 dump_bitmap_vector (dump_file, "nearer", "", nearer, num_edges);
e48ba7af 801 }
7bcd381b 802#endif
803
cca23eb2 804 sbitmap_vector_free (farthest);
7bcd381b 805
806 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
fe672ac0 807 *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
9ca8d59e 808 compute_rev_insert_delete (edge_list, st_avloc, nearer, nearerout,
9ce37fa7 809 *insert, *del);
7bcd381b 810
cca23eb2 811 sbitmap_vector_free (nearerout);
812 sbitmap_vector_free (nearer);
7bcd381b 813
814#ifdef LCM_DEBUG_INFO
3f5be5f4 815 if (dump_file)
7bcd381b 816 {
53c5d9d4 817 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
818 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
fe672ac0 819 last_basic_block_for_fn (cfun));
7bcd381b 820 }
821#endif
7bcd381b 822 return edge_list;
e48ba7af 823}
18862b5a 824