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