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