]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lcm.c
2013-11-19 Catherine Moore <clm@codesourcery.com>
[thirdparty/gcc.git] / gcc / lcm.c
CommitLineData
9ca8d59e 1/* Generic partial redundancy elimination with lazy code motion support.
711789cc 2 Copyright (C) 1998-2013 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. */
53c5d9d4 108 bitmap_vector_ones (antin, last_basic_block);
e48ba7af 109
5d6931e2 110 /* Put every block on the worklist; this is necessary because of the
111 optimistic initialization of ANTIN above. */
4c26117a 112 FOR_EACH_BB_REVERSE (bb)
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. */
cd665a06 124 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5d6931e2 125 e->src->aux = EXIT_BLOCK_PTR;
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
4c26117a 137 if (bb->aux == EXIT_BLOCK_PTR)
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)
9ca8d59e 156 if (!e->src->aux && e->src != ENTRY_BLOCK_PTR)
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);
191 if (pred == ENTRY_BLOCK_PTR)
53c5d9d4 192 bitmap_copy (earliest[x], antin[succ->index]);
7bcd381b 193 else
8851e806 194 {
742ee135 195 if (succ == EXIT_BLOCK_PTR)
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. */
cd665a06 279 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->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. */
4c26117a 284 FOR_EACH_BB (bb)
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. */
320 && e->dest != EXIT_BLOCK_PTR && e->dest->aux == 0)
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. */
53c5d9d4 333 bitmap_ones (laterin[last_basic_block]);
cd665a06 334 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
53c5d9d4 335 bitmap_and (laterin[last_basic_block],
f20183e6 336 laterin[last_basic_block],
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
4c26117a 353 FOR_EACH_BB (bb)
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
7bcd381b 361 if (b == EXIT_BLOCK_PTR)
53c5d9d4 362 bitmap_and_compl (insert[x], later[x], laterin[last_basic_block]);
7bcd381b 363 else
53c5d9d4 364 bitmap_and_compl (insert[x], later[x], laterin[b->index]);
7bcd381b 365 }
366}
e48ba7af 367
9ca8d59e 368/* Given local properties TRANSP, ANTLOC, AVOUT, KILL return the insert and
369 delete vectors for edge based LCM. Returns an edgelist which is used to
370 map the insert vector to what edge an expression should be inserted on. */
e48ba7af 371
7bcd381b 372struct edge_list *
3f5be5f4 373pre_edge_lcm (int n_exprs, sbitmap *transp,
3ad4992f 374 sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
9ce37fa7 375 sbitmap **insert, sbitmap **del)
e48ba7af 376{
7bcd381b 377 sbitmap *antin, *antout, *earliest;
378 sbitmap *avin, *avout;
379 sbitmap *later, *laterin;
380 struct edge_list *edge_list;
381 int num_edges;
e48ba7af 382
7bcd381b 383 edge_list = create_edge_list ();
384 num_edges = NUM_EDGES (edge_list);
e48ba7af 385
7bcd381b 386#ifdef LCM_DEBUG_INFO
3f5be5f4 387 if (dump_file)
e48ba7af 388 {
3f5be5f4 389 fprintf (dump_file, "Edge List:\n");
390 verify_edge_list (dump_file, edge_list);
391 print_edge_list (dump_file, edge_list);
53c5d9d4 392 dump_bitmap_vector (dump_file, "transp", "", transp, last_basic_block);
393 dump_bitmap_vector (dump_file, "antloc", "", antloc, last_basic_block);
394 dump_bitmap_vector (dump_file, "avloc", "", avloc, last_basic_block);
395 dump_bitmap_vector (dump_file, "kill", "", kill, last_basic_block);
e48ba7af 396 }
7bcd381b 397#endif
e48ba7af 398
7bcd381b 399 /* Compute global availability. */
f20183e6 400 avin = sbitmap_vector_alloc (last_basic_block, n_exprs);
401 avout = sbitmap_vector_alloc (last_basic_block, n_exprs);
7bcd381b 402 compute_available (avloc, kill, avout, avin);
cca23eb2 403 sbitmap_vector_free (avin);
e48ba7af 404
7bcd381b 405 /* Compute global anticipatability. */
f20183e6 406 antin = sbitmap_vector_alloc (last_basic_block, n_exprs);
407 antout = sbitmap_vector_alloc (last_basic_block, n_exprs);
7bcd381b 408 compute_antinout_edge (antloc, transp, antin, antout);
e48ba7af 409
7bcd381b 410#ifdef LCM_DEBUG_INFO
3f5be5f4 411 if (dump_file)
e48ba7af 412 {
53c5d9d4 413 dump_bitmap_vector (dump_file, "antin", "", antin, last_basic_block);
414 dump_bitmap_vector (dump_file, "antout", "", antout, last_basic_block);
e48ba7af 415 }
7bcd381b 416#endif
e48ba7af 417
7bcd381b 418 /* Compute earliestness. */
419 earliest = sbitmap_vector_alloc (num_edges, n_exprs);
420 compute_earliest (edge_list, n_exprs, antin, antout, avout, kill, earliest);
e48ba7af 421
7bcd381b 422#ifdef LCM_DEBUG_INFO
3f5be5f4 423 if (dump_file)
53c5d9d4 424 dump_bitmap_vector (dump_file, "earliest", "", earliest, num_edges);
7bcd381b 425#endif
e48ba7af 426
cca23eb2 427 sbitmap_vector_free (antout);
428 sbitmap_vector_free (antin);
429 sbitmap_vector_free (avout);
e48ba7af 430
7bcd381b 431 later = sbitmap_vector_alloc (num_edges, n_exprs);
9ca8d59e 432
7bcd381b 433 /* Allocate an extra element for the exit block in the laterin vector. */
f20183e6 434 laterin = sbitmap_vector_alloc (last_basic_block + 1, n_exprs);
2325f0e2 435 compute_laterin (edge_list, earliest, antloc, later, laterin);
436
7bcd381b 437#ifdef LCM_DEBUG_INFO
3f5be5f4 438 if (dump_file)
7bcd381b 439 {
53c5d9d4 440 dump_bitmap_vector (dump_file, "laterin", "", laterin, last_basic_block + 1);
441 dump_bitmap_vector (dump_file, "later", "", later, num_edges);
7bcd381b 442 }
443#endif
e48ba7af 444
cca23eb2 445 sbitmap_vector_free (earliest);
7bcd381b 446
447 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
9ce37fa7 448 *del = sbitmap_vector_alloc (last_basic_block, n_exprs);
53c5d9d4 449 bitmap_vector_clear (*insert, num_edges);
450 bitmap_vector_clear (*del, last_basic_block);
9ce37fa7 451 compute_insert_delete (edge_list, antloc, later, laterin, *insert, *del);
e48ba7af 452
cca23eb2 453 sbitmap_vector_free (laterin);
454 sbitmap_vector_free (later);
7bcd381b 455
456#ifdef LCM_DEBUG_INFO
3f5be5f4 457 if (dump_file)
e48ba7af 458 {
53c5d9d4 459 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
460 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
f20183e6 461 last_basic_block);
e48ba7af 462 }
7bcd381b 463#endif
e48ba7af 464
7bcd381b 465 return edge_list;
466}
3b7e1f27 467
468/* Compute the AVIN and AVOUT vectors from the AVLOC and KILL vectors.
469 Return the number of passes we performed to iterate to a solution. */
470
2325f0e2 471void
3ad4992f 472compute_available (sbitmap *avloc, sbitmap *kill, sbitmap *avout,
473 sbitmap *avin)
e48ba7af 474{
3b7e1f27 475 edge e;
4c26117a 476 basic_block *worklist, *qin, *qout, *qend, bb;
3b7e1f27 477 unsigned int qlen;
cd665a06 478 edge_iterator ei;
3b7e1f27 479
480 /* Allocate a worklist array/queue. Entries are only added to the
481 list if they were not already on the list. So the size is
482 bounded by the number of basic blocks. */
48e1416a 483 qin = qout = worklist =
a28770e1 484 XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
3b7e1f27 485
486 /* We want a maximal solution. */
53c5d9d4 487 bitmap_vector_ones (avout, last_basic_block);
3b7e1f27 488
489 /* Put every block on the worklist; this is necessary because of the
490 optimistic initialization of AVOUT above. */
4c26117a 491 FOR_EACH_BB (bb)
3b7e1f27 492 {
4c26117a 493 *qin++ = bb;
494 bb->aux = bb;
3b7e1f27 495 }
496
497 qin = worklist;
a28770e1 498 qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
499 qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
3b7e1f27 500
501 /* Mark blocks which are successors of the entry block so that we
502 can easily identify them below. */
cd665a06 503 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3b7e1f27 504 e->dest->aux = ENTRY_BLOCK_PTR;
505
506 /* Iterate until the worklist is empty. */
507 while (qlen)
508 {
509 /* Take the first entry off the worklist. */
4c26117a 510 bb = *qout++;
3b7e1f27 511 qlen--;
512
513 if (qout >= qend)
8851e806 514 qout = worklist;
3b7e1f27 515
516 /* If one of the predecessor blocks is the ENTRY block, then the
517 intersection of avouts is the null set. We can identify such blocks
518 by the special value in the AUX field in the block structure. */
4c26117a 519 if (bb->aux == ENTRY_BLOCK_PTR)
3b7e1f27 520 /* Do not clear the aux field for blocks which are successors of the
521 ENTRY block. That way we never add then to the worklist again. */
53c5d9d4 522 bitmap_clear (avin[bb->index]);
3b7e1f27 523 else
524 {
525 /* Clear the aux field of this block so that it can be added to
526 the worklist again if necessary. */
4c26117a 527 bb->aux = NULL;
08b7917c 528 bitmap_intersection_of_preds (avin[bb->index], avout, bb);
3b7e1f27 529 }
530
53c5d9d4 531 if (bitmap_ior_and_compl (avout[bb->index], avloc[bb->index],
d4d93ea0 532 avin[bb->index], kill[bb->index]))
3b7e1f27 533 /* If the out state of this block changed, then we need
534 to add the successors of this block to the worklist
535 if they are not already on the worklist. */
cd665a06 536 FOR_EACH_EDGE (e, ei, bb->succs)
3b7e1f27 537 if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR)
538 {
539 *qin++ = e->dest;
540 e->dest->aux = e;
541 qlen++;
542
543 if (qin >= qend)
8851e806 544 qin = worklist;
3b7e1f27 545 }
546 }
547
548 clear_aux_for_edges ();
549 clear_aux_for_blocks ();
550 free (worklist);
e48ba7af 551}
552
7bcd381b 553/* Compute the farthest vector for edge based lcm. */
9ca8d59e 554
e48ba7af 555static void
3ad4992f 556compute_farthest (struct edge_list *edge_list, int n_exprs,
557 sbitmap *st_avout, sbitmap *st_avin, sbitmap *st_antin,
558 sbitmap *kill, sbitmap *farthest)
e48ba7af 559{
7bcd381b 560 sbitmap difference, temp_bitmap;
5ab08585 561 int x, num_edges;
7bcd381b 562 basic_block pred, succ;
e48ba7af 563
7bcd381b 564 num_edges = NUM_EDGES (edge_list);
e48ba7af 565
7bcd381b 566 difference = sbitmap_alloc (n_exprs);
567 temp_bitmap = sbitmap_alloc (n_exprs);
e48ba7af 568
7bcd381b 569 for (x = 0; x < num_edges; x++)
e48ba7af 570 {
7bcd381b 571 pred = INDEX_EDGE_PRED_BB (edge_list, x);
572 succ = INDEX_EDGE_SUCC_BB (edge_list, x);
573 if (succ == EXIT_BLOCK_PTR)
53c5d9d4 574 bitmap_copy (farthest[x], st_avout[pred->index]);
7bcd381b 575 else
e48ba7af 576 {
7bcd381b 577 if (pred == ENTRY_BLOCK_PTR)
53c5d9d4 578 bitmap_clear (farthest[x]);
7bcd381b 579 else
580 {
53c5d9d4 581 bitmap_and_compl (difference, st_avout[pred->index],
b3d6de89 582 st_antin[succ->index]);
53c5d9d4 583 bitmap_not (temp_bitmap, st_avin[succ->index]);
584 bitmap_and_or (farthest[x], difference,
b3d6de89 585 kill[succ->index], temp_bitmap);
7bcd381b 586 }
e48ba7af 587 }
e48ba7af 588 }
9ca8d59e 589
f5123ed5 590 sbitmap_free (temp_bitmap);
591 sbitmap_free (difference);
e48ba7af 592}
593
2325f0e2 594/* Compute nearer and nearerout vectors for edge based lcm.
595
596 This is the mirror of compute_laterin, additional comments on the
597 implementation can be found before compute_laterin. */
598
e48ba7af 599static void
3ad4992f 600compute_nearerout (struct edge_list *edge_list, sbitmap *farthest,
601 sbitmap *st_avloc, sbitmap *nearer, sbitmap *nearerout)
e48ba7af 602{
4c26117a 603 int num_edges, i;
2325f0e2 604 edge e;
4c26117a 605 basic_block *worklist, *tos, bb;
cd665a06 606 edge_iterator ei;
e48ba7af 607
7bcd381b 608 num_edges = NUM_EDGES (edge_list);
e48ba7af 609
2325f0e2 610 /* Allocate a worklist array/queue. Entries are only added to the
611 list if they were not already on the list. So the size is
612 bounded by the number of basic blocks. */
a28770e1 613 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
e48ba7af 614
2325f0e2 615 /* Initialize NEARER for each edge and build a mapping from an edge to
616 its index. */
617 for (i = 0; i < num_edges; i++)
9ffd5d6d 618 INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
7bcd381b 619
2325f0e2 620 /* We want a maximal solution. */
53c5d9d4 621 bitmap_vector_ones (nearer, num_edges);
2325f0e2 622
048599b9 623 /* Note that even though we want an optimistic setting of NEARER, we
624 do not want to be overly optimistic. Consider an incoming edge to
625 the exit block. That edge should always have a NEARER value the
626 same as FARTHEST for that edge. */
cd665a06 627 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
53c5d9d4 628 bitmap_copy (nearer[(size_t)e->aux], farthest[(size_t)e->aux]);
048599b9 629
2325f0e2 630 /* Add all the blocks to the worklist. This prevents an early exit
631 from the loop given our optimistic initialization of NEARER. */
4c26117a 632 FOR_EACH_BB (bb)
e48ba7af 633 {
4c26117a 634 *tos++ = bb;
635 bb->aux = bb;
7bcd381b 636 }
5ab08585 637
2325f0e2 638 /* Iterate until the worklist is empty. */
639 while (tos != worklist)
7bcd381b 640 {
2325f0e2 641 /* Take the first entry off the worklist. */
4c26117a 642 bb = *--tos;
643 bb->aux = NULL;
2325f0e2 644
645 /* Compute the intersection of NEARER for each outgoing edge from B. */
53c5d9d4 646 bitmap_ones (nearerout[bb->index]);
cd665a06 647 FOR_EACH_EDGE (e, ei, bb->succs)
53c5d9d4 648 bitmap_and (nearerout[bb->index], nearerout[bb->index],
9ffd5d6d 649 nearer[(size_t) e->aux]);
2325f0e2 650
651 /* Calculate NEARER for all incoming edges. */
cd665a06 652 FOR_EACH_EDGE (e, ei, bb->preds)
53c5d9d4 653 if (bitmap_ior_and_compl (nearer[(size_t) e->aux],
b3d6de89 654 farthest[(size_t) e->aux],
655 nearerout[e->dest->index],
656 st_avloc[e->dest->index])
9ca8d59e 657 /* If NEARER for an incoming edge was changed, then we need
658 to add the source of the incoming edge to the worklist. */
659 && e->src != ENTRY_BLOCK_PTR && e->src->aux == 0)
660 {
661 *tos++ = e->src;
662 e->src->aux = e;
663 }
7bcd381b 664 }
e48ba7af 665
2325f0e2 666 /* Computation of insertion and deletion points requires computing NEAREROUT
667 for the ENTRY block. We allocated an extra entry in the NEAREROUT array
668 for just this purpose. */
53c5d9d4 669 bitmap_ones (nearerout[last_basic_block]);
cd665a06 670 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
53c5d9d4 671 bitmap_and (nearerout[last_basic_block],
f20183e6 672 nearerout[last_basic_block],
9ffd5d6d 673 nearer[(size_t) e->aux]);
2325f0e2 674
82f7392b 675 clear_aux_for_edges ();
2325f0e2 676 free (tos);
7bcd381b 677}
e48ba7af 678
7bcd381b 679/* Compute the insertion and deletion points for edge based LCM. */
9ca8d59e 680
e48ba7af 681static void
3ad4992f 682compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *st_avloc,
683 sbitmap *nearer, sbitmap *nearerout,
9ce37fa7 684 sbitmap *insert, sbitmap *del)
e48ba7af 685{
7bcd381b 686 int x;
4c26117a 687 basic_block bb;
e48ba7af 688
4c26117a 689 FOR_EACH_BB (bb)
53c5d9d4 690 bitmap_and_compl (del[bb->index], st_avloc[bb->index],
d4d93ea0 691 nearerout[bb->index]);
5ab08585 692
7bcd381b 693 for (x = 0; x < NUM_EDGES (edge_list); x++)
e48ba7af 694 {
7bcd381b 695 basic_block b = INDEX_EDGE_PRED_BB (edge_list, x);
696 if (b == ENTRY_BLOCK_PTR)
53c5d9d4 697 bitmap_and_compl (insert[x], nearer[x], nearerout[last_basic_block]);
e48ba7af 698 else
53c5d9d4 699 bitmap_and_compl (insert[x], nearer[x], nearerout[b->index]);
e48ba7af 700 }
e48ba7af 701}
702
5ab08585 703/* Given local properties TRANSP, ST_AVLOC, ST_ANTLOC, KILL return the
7bcd381b 704 insert and delete vectors for edge based reverse LCM. Returns an
705 edgelist which is used to map the insert vector to what edge
706 an expression should be inserted on. */
e48ba7af 707
7bcd381b 708struct edge_list *
3f5be5f4 709pre_edge_rev_lcm (int n_exprs, sbitmap *transp,
3ad4992f 710 sbitmap *st_avloc, sbitmap *st_antloc, sbitmap *kill,
9ce37fa7 711 sbitmap **insert, sbitmap **del)
e48ba7af 712{
7bcd381b 713 sbitmap *st_antin, *st_antout;
714 sbitmap *st_avout, *st_avin, *farthest;
715 sbitmap *nearer, *nearerout;
716 struct edge_list *edge_list;
27548a74 717 int num_edges;
7bcd381b 718
719 edge_list = create_edge_list ();
720 num_edges = NUM_EDGES (edge_list);
721
f0af5a88 722 st_antin = sbitmap_vector_alloc (last_basic_block, n_exprs);
723 st_antout = sbitmap_vector_alloc (last_basic_block, n_exprs);
53c5d9d4 724 bitmap_vector_clear (st_antin, last_basic_block);
725 bitmap_vector_clear (st_antout, last_basic_block);
7bcd381b 726 compute_antinout_edge (st_antloc, transp, st_antin, st_antout);
727
728 /* Compute global anticipatability. */
f20183e6 729 st_avout = sbitmap_vector_alloc (last_basic_block, n_exprs);
730 st_avin = sbitmap_vector_alloc (last_basic_block, n_exprs);
7bcd381b 731 compute_available (st_avloc, kill, st_avout, st_avin);
732
733#ifdef LCM_DEBUG_INFO
3f5be5f4 734 if (dump_file)
7bcd381b 735 {
3f5be5f4 736 fprintf (dump_file, "Edge List:\n");
737 verify_edge_list (dump_file, edge_list);
738 print_edge_list (dump_file, edge_list);
53c5d9d4 739 dump_bitmap_vector (dump_file, "transp", "", transp, last_basic_block);
740 dump_bitmap_vector (dump_file, "st_avloc", "", st_avloc, last_basic_block);
741 dump_bitmap_vector (dump_file, "st_antloc", "", st_antloc, last_basic_block);
742 dump_bitmap_vector (dump_file, "st_antin", "", st_antin, last_basic_block);
743 dump_bitmap_vector (dump_file, "st_antout", "", st_antout, last_basic_block);
744 dump_bitmap_vector (dump_file, "st_kill", "", kill, last_basic_block);
7bcd381b 745 }
746#endif
e48ba7af 747
7bcd381b 748#ifdef LCM_DEBUG_INFO
3f5be5f4 749 if (dump_file)
7bcd381b 750 {
53c5d9d4 751 dump_bitmap_vector (dump_file, "st_avout", "", st_avout, last_basic_block);
752 dump_bitmap_vector (dump_file, "st_avin", "", st_avin, last_basic_block);
7bcd381b 753 }
754#endif
e48ba7af 755
7bcd381b 756 /* Compute farthestness. */
757 farthest = sbitmap_vector_alloc (num_edges, n_exprs);
5ab08585 758 compute_farthest (edge_list, n_exprs, st_avout, st_avin, st_antin,
7bcd381b 759 kill, farthest);
760
761#ifdef LCM_DEBUG_INFO
3f5be5f4 762 if (dump_file)
53c5d9d4 763 dump_bitmap_vector (dump_file, "farthest", "", farthest, num_edges);
7bcd381b 764#endif
765
cca23eb2 766 sbitmap_vector_free (st_antin);
767 sbitmap_vector_free (st_antout);
768
769 sbitmap_vector_free (st_avin);
770 sbitmap_vector_free (st_avout);
7bcd381b 771
772 nearer = sbitmap_vector_alloc (num_edges, n_exprs);
9ca8d59e 773
7bcd381b 774 /* Allocate an extra element for the entry block. */
f20183e6 775 nearerout = sbitmap_vector_alloc (last_basic_block + 1, n_exprs);
2325f0e2 776 compute_nearerout (edge_list, farthest, st_avloc, nearer, nearerout);
7bcd381b 777
778#ifdef LCM_DEBUG_INFO
3f5be5f4 779 if (dump_file)
e48ba7af 780 {
53c5d9d4 781 dump_bitmap_vector (dump_file, "nearerout", "", nearerout,
f20183e6 782 last_basic_block + 1);
53c5d9d4 783 dump_bitmap_vector (dump_file, "nearer", "", nearer, num_edges);
e48ba7af 784 }
7bcd381b 785#endif
786
cca23eb2 787 sbitmap_vector_free (farthest);
7bcd381b 788
789 *insert = sbitmap_vector_alloc (num_edges, n_exprs);
9ce37fa7 790 *del = sbitmap_vector_alloc (last_basic_block, n_exprs);
9ca8d59e 791 compute_rev_insert_delete (edge_list, st_avloc, nearer, nearerout,
9ce37fa7 792 *insert, *del);
7bcd381b 793
cca23eb2 794 sbitmap_vector_free (nearerout);
795 sbitmap_vector_free (nearer);
7bcd381b 796
797#ifdef LCM_DEBUG_INFO
3f5be5f4 798 if (dump_file)
7bcd381b 799 {
53c5d9d4 800 dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
801 dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
f20183e6 802 last_basic_block);
7bcd381b 803 }
804#endif
7bcd381b 805 return edge_list;
e48ba7af 806}
18862b5a 807