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