]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cfgloopanal.c
trans-decl.c (gfc_finish_decl): Remove unused second argument 'init'.
[thirdparty/gcc.git] / gcc / cfgloopanal.c
CommitLineData
3d436d2a 1/* Natural loop analysis code for GNU compiler.
83f676b3 2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3d436d2a
ZD
3
4This file is part of GCC.
5
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
8Software Foundation; either version 2, or (at your option) any later
9version.
10
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.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
366ccddb
KC
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA. */
3d436d2a
ZD
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "rtl.h"
26#include "hard-reg-set.h"
7932a3db 27#include "obstack.h"
3d436d2a
ZD
28#include "basic-block.h"
29#include "cfgloop.h"
30#include "expr.h"
31#include "output.h"
32
3d436d2a 33/* Checks whether BB is executed exactly once in each LOOP iteration. */
f2dca510 34
3d436d2a 35bool
6c878b23 36just_once_each_iteration_p (const struct loop *loop, basic_block bb)
3d436d2a
ZD
37{
38 /* It must be executed at least once each iteration. */
d47cc544 39 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
3d436d2a
ZD
40 return false;
41
42 /* And just once. */
43 if (bb->loop_father != loop)
44 return false;
45
46 /* But this was not enough. We might have some irreducible loop here. */
47 if (bb->flags & BB_IRREDUCIBLE_LOOP)
48 return false;
49
50 return true;
51}
52
cfbe3efe
ZD
53/* Structure representing edge of a graph. */
54
55struct edge
56{
57 int src, dest; /* Source and destination. */
58 struct edge *pred_next, *succ_next;
59 /* Next edge in predecessor and successor lists. */
60 void *data; /* Data attached to the edge. */
61};
62
63/* Structure representing vertex of a graph. */
64
65struct vertex
66{
67 struct edge *pred, *succ;
68 /* Lists of predecessors and successors. */
69 int component; /* Number of dfs restarts before reaching the
70 vertex. */
71 int post; /* Postorder number. */
72};
73
74/* Structure representing a graph. */
75
76struct graph
77{
78 int n_vertices; /* Number of vertices. */
79 struct vertex *vertices;
80 /* The vertices. */
81};
82
83/* Dumps graph G into F. */
84
85extern void dump_graph (FILE *, struct graph *);
83f676b3
RS
86
87void
88dump_graph (FILE *f, struct graph *g)
cfbe3efe
ZD
89{
90 int i;
91 struct edge *e;
92
93 for (i = 0; i < g->n_vertices; i++)
94 {
95 if (!g->vertices[i].pred
96 && !g->vertices[i].succ)
97 continue;
98
99 fprintf (f, "%d (%d)\t<-", i, g->vertices[i].component);
100 for (e = g->vertices[i].pred; e; e = e->pred_next)
101 fprintf (f, " %d", e->src);
102 fprintf (f, "\n");
103
104 fprintf (f, "\t->");
105 for (e = g->vertices[i].succ; e; e = e->succ_next)
106 fprintf (f, " %d", e->dest);
107 fprintf (f, "\n");
108 }
109}
110
111/* Creates a new graph with N_VERTICES vertices. */
112
113static struct graph *
114new_graph (int n_vertices)
115{
5ed6ace5 116 struct graph *g = XNEW (struct graph);
cfbe3efe
ZD
117
118 g->n_vertices = n_vertices;
5ed6ace5 119 g->vertices = XCNEWVEC (struct vertex, n_vertices);
cfbe3efe
ZD
120
121 return g;
122}
123
124/* Adds an edge from F to T to graph G, with DATA attached. */
125
126static void
127add_edge (struct graph *g, int f, int t, void *data)
128{
129 struct edge *e = xmalloc (sizeof (struct edge));
130
131 e->src = f;
132 e->dest = t;
133 e->data = data;
134
135 e->pred_next = g->vertices[t].pred;
136 g->vertices[t].pred = e;
137
138 e->succ_next = g->vertices[f].succ;
139 g->vertices[f].succ = e;
140}
141
142/* Runs dfs search over vertices of G, from NQ vertices in queue QS.
143 The vertices in postorder are stored into QT. If FORWARD is false,
144 backward dfs is run. */
145
146static void
147dfs (struct graph *g, int *qs, int nq, int *qt, bool forward)
148{
149 int i, tick = 0, v, comp = 0, top;
150 struct edge *e;
151 struct edge **stack = xmalloc (sizeof (struct edge *) * g->n_vertices);
152
153 for (i = 0; i < g->n_vertices; i++)
154 {
155 g->vertices[i].component = -1;
156 g->vertices[i].post = -1;
157 }
158
159#define FST_EDGE(V) (forward ? g->vertices[(V)].succ : g->vertices[(V)].pred)
160#define NEXT_EDGE(E) (forward ? (E)->succ_next : (E)->pred_next)
161#define EDGE_SRC(E) (forward ? (E)->src : (E)->dest)
162#define EDGE_DEST(E) (forward ? (E)->dest : (E)->src)
163
164 for (i = 0; i < nq; i++)
165 {
166 v = qs[i];
167 if (g->vertices[v].post != -1)
168 continue;
169
170 g->vertices[v].component = comp++;
171 e = FST_EDGE (v);
172 top = 0;
173
174 while (1)
175 {
176 while (e && g->vertices[EDGE_DEST (e)].component != -1)
177 e = NEXT_EDGE (e);
178
179 if (!e)
180 {
181 if (qt)
182 qt[tick] = v;
c22cacf3 183 g->vertices[v].post = tick++;
cfbe3efe
ZD
184
185 if (!top)
186 break;
187
188 e = stack[--top];
189 v = EDGE_SRC (e);
190 e = NEXT_EDGE (e);
191 continue;
192 }
193
194 stack[top++] = e;
195 v = EDGE_DEST (e);
196 e = FST_EDGE (v);
197 g->vertices[v].component = comp - 1;
198 }
199 }
200
201 free (stack);
202}
203
204/* Marks the edge E in graph G irreducible if it connects two vertices in the
205 same scc. */
206
207static void
208check_irred (struct graph *g, struct edge *e)
209{
210 edge real = e->data;
211
212 /* All edges should lead from a component with higher number to the
213 one with lower one. */
341c100f 214 gcc_assert (g->vertices[e->src].component >= g->vertices[e->dest].component);
cfbe3efe
ZD
215
216 if (g->vertices[e->src].component != g->vertices[e->dest].component)
217 return;
218
219 real->flags |= EDGE_IRREDUCIBLE_LOOP;
220 if (flow_bb_inside_loop_p (real->src->loop_father, real->dest))
221 real->src->flags |= BB_IRREDUCIBLE_LOOP;
222}
223
224/* Runs CALLBACK for all edges in G. */
225
226static void
227for_each_edge (struct graph *g,
228 void (callback) (struct graph *, struct edge *))
229{
230 struct edge *e;
231 int i;
232
233 for (i = 0; i < g->n_vertices; i++)
234 for (e = g->vertices[i].succ; e; e = e->succ_next)
235 callback (g, e);
236}
237
238/* Releases the memory occupied by G. */
239
240static void
241free_graph (struct graph *g)
242{
243 struct edge *e, *n;
244 int i;
245
246 for (i = 0; i < g->n_vertices; i++)
247 for (e = g->vertices[i].succ; e; e = n)
248 {
249 n = e->succ_next;
250 free (e);
251 }
252 free (g->vertices);
253 free (g);
254}
255
35b07080
ZD
256/* Marks blocks and edges that are part of non-recognized loops; i.e. we
257 throw away all latch edges and mark blocks inside any remaining cycle.
258 Everything is a bit complicated due to fact we do not want to do this
259 for parts of cycles that only "pass" through some loop -- i.e. for
260 each cycle, we want to mark blocks that belong directly to innermost
cfbe3efe 261 loop containing the whole cycle.
c22cacf3 262
cfbe3efe
ZD
263 LOOPS is the loop tree. */
264
265#define LOOP_REPR(LOOP) ((LOOP)->num + last_basic_block)
266#define BB_REPR(BB) ((BB)->index + 1)
267
3d436d2a 268void
d73be268 269mark_irreducible_loops (void)
3d436d2a 270{
3d436d2a 271 basic_block act;
cfbe3efe 272 edge e;
628f6a4e 273 edge_iterator ei;
cfbe3efe
ZD
274 int i, src, dest;
275 struct graph *g;
42fd6772 276 int num = current_loops ? number_of_loops () : 1;
598ec7bd
ZD
277 int *queue1 = XNEWVEC (int, last_basic_block + num);
278 int *queue2 = XNEWVEC (int, last_basic_block + num);
cfbe3efe 279 int nq, depth;
42fd6772
ZD
280 struct loop *cloop, *loop;
281 loop_iterator li;
3d436d2a 282
35b07080
ZD
283 /* Reset the flags. */
284 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
285 {
286 act->flags &= ~BB_IRREDUCIBLE_LOOP;
628f6a4e 287 FOR_EACH_EDGE (e, ei, act->succs)
35b07080
ZD
288 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
289 }
290
3d436d2a 291 /* Create the edge lists. */
598ec7bd 292 g = new_graph (last_basic_block + num);
cfbe3efe 293
3d436d2a 294 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
628f6a4e 295 FOR_EACH_EDGE (e, ei, act->succs)
3d436d2a 296 {
c22cacf3
MS
297 /* Ignore edges to exit. */
298 if (e->dest == EXIT_BLOCK_PTR)
3d436d2a 299 continue;
cfbe3efe 300
598ec7bd
ZD
301 src = BB_REPR (act);
302 dest = BB_REPR (e->dest);
cfbe3efe 303
d73be268 304 if (current_loops)
598ec7bd
ZD
305 {
306 /* Ignore latch edges. */
307 if (e->dest->loop_father->header == e->dest
308 && e->dest->loop_father->latch == act)
309 continue;
cfbe3efe 310
598ec7bd
ZD
311 /* Edges inside a single loop should be left where they are. Edges
312 to subloop headers should lead to representative of the subloop,
313 but from the same place.
3d436d2a 314
598ec7bd
ZD
315 Edges exiting loops should lead from representative
316 of the son of nearest common ancestor of the loops in that
317 act lays. */
3d436d2a 318
598ec7bd
ZD
319 if (e->dest->loop_father->header == e->dest)
320 dest = LOOP_REPR (e->dest->loop_father);
cfbe3efe 321
598ec7bd
ZD
322 if (!flow_bb_inside_loop_p (act->loop_father, e->dest))
323 {
324 depth = find_common_loop (act->loop_father,
325 e->dest->loop_father)->depth + 1;
326 if (depth == act->loop_father->depth)
327 cloop = act->loop_father;
328 else
329 cloop = act->loop_father->pred[depth];
330
331 src = LOOP_REPR (cloop);
332 }
3d436d2a 333 }
cfbe3efe
ZD
334
335 add_edge (g, src, dest, e);
3d436d2a
ZD
336 }
337
cfbe3efe
ZD
338 /* Find the strongly connected components. Use the algorithm of Tarjan --
339 first determine the postorder dfs numbering in reversed graph, then
340 run the dfs on the original graph in the order given by decreasing
341 numbers assigned by the previous pass. */
342 nq = 0;
343 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3d436d2a 344 {
cfbe3efe 345 queue1[nq++] = BB_REPR (act);
3d436d2a 346 }
42fd6772
ZD
347
348 if (current_loops)
349 {
350 FOR_EACH_LOOP (li, loop, 0)
351 {
352 queue1[nq++] = LOOP_REPR (loop);
353 }
354 }
cfbe3efe
ZD
355 dfs (g, queue1, nq, queue2, false);
356 for (i = 0; i < nq; i++)
357 queue1[i] = queue2[nq - i - 1];
358 dfs (g, queue1, nq, NULL, true);
3d436d2a 359
cfbe3efe
ZD
360 /* Mark the irreducible loops. */
361 for_each_edge (g, check_irred);
3d436d2a 362
cfbe3efe
ZD
363 free_graph (g);
364 free (queue1);
365 free (queue2);
3d436d2a 366
d73be268
ZD
367 if (current_loops)
368 current_loops->state |= LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS;
3d436d2a
ZD
369}
370
371/* Counts number of insns inside LOOP. */
372int
d329e058 373num_loop_insns (struct loop *loop)
3d436d2a
ZD
374{
375 basic_block *bbs, bb;
376 unsigned i, ninsns = 0;
377 rtx insn;
378
379 bbs = get_loop_body (loop);
380 for (i = 0; i < loop->num_nodes; i++)
381 {
382 bb = bbs[i];
383 ninsns++;
a813c111 384 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
91f4cfe3
ZD
385 if (INSN_P (insn))
386 ninsns++;
3d436d2a
ZD
387 }
388 free(bbs);
d329e058 389
3d436d2a
ZD
390 return ninsns;
391}
392
393/* Counts number of insns executed on average per iteration LOOP. */
394int
d329e058 395average_num_loop_insns (struct loop *loop)
3d436d2a
ZD
396{
397 basic_block *bbs, bb;
398 unsigned i, binsns, ninsns, ratio;
399 rtx insn;
400
401 ninsns = 0;
402 bbs = get_loop_body (loop);
403 for (i = 0; i < loop->num_nodes; i++)
404 {
405 bb = bbs[i];
406
407 binsns = 1;
a813c111 408 for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
91f4cfe3
ZD
409 if (INSN_P (insn))
410 binsns++;
3d436d2a
ZD
411
412 ratio = loop->header->frequency == 0
413 ? BB_FREQ_MAX
414 : (bb->frequency * BB_FREQ_MAX) / loop->header->frequency;
415 ninsns += binsns * ratio;
416 }
417 free(bbs);
d329e058 418
3d436d2a
ZD
419 ninsns /= BB_FREQ_MAX;
420 if (!ninsns)
421 ninsns = 1; /* To avoid division by zero. */
422
423 return ninsns;
424}
425
ac84e05e
ZD
426/* Returns expected number of iterations of LOOP, according to
427 measured or guessed profile. No bounding is done on the
428 value. */
429
430gcov_type
431expected_loop_iterations_unbounded (const struct loop *loop)
3d436d2a
ZD
432{
433 edge e;
628f6a4e 434 edge_iterator ei;
3d436d2a 435
997de8ed 436 if (loop->latch->count || loop->header->count)
3d436d2a
ZD
437 {
438 gcov_type count_in, count_latch, expected;
439
440 count_in = 0;
441 count_latch = 0;
442
628f6a4e 443 FOR_EACH_EDGE (e, ei, loop->header->preds)
3d436d2a
ZD
444 if (e->src == loop->latch)
445 count_latch = e->count;
446 else
447 count_in += e->count;
448
449 if (count_in == 0)
c22cacf3 450 expected = count_latch * 2;
bade3a00 451 else
c22cacf3 452 expected = (count_latch + count_in - 1) / count_in;
3d436d2a 453
ac84e05e 454 return expected;
3d436d2a
ZD
455 }
456 else
457 {
458 int freq_in, freq_latch;
459
460 freq_in = 0;
461 freq_latch = 0;
462
628f6a4e 463 FOR_EACH_EDGE (e, ei, loop->header->preds)
3d436d2a
ZD
464 if (e->src == loop->latch)
465 freq_latch = EDGE_FREQUENCY (e);
466 else
467 freq_in += EDGE_FREQUENCY (e);
468
469 if (freq_in == 0)
bade3a00 470 return freq_latch * 2;
3d436d2a
ZD
471
472 return (freq_latch + freq_in - 1) / freq_in;
473 }
474}
689ba89d 475
ac84e05e
ZD
476/* Returns expected number of LOOP iterations. The returned value is bounded
477 by REG_BR_PROB_BASE. */
478
479unsigned
480expected_loop_iterations (const struct loop *loop)
481{
482 gcov_type expected = expected_loop_iterations_unbounded (loop);
483 return (expected > REG_BR_PROB_BASE ? REG_BR_PROB_BASE : expected);
484}
485
689ba89d
ZD
486/* Returns the maximum level of nesting of subloops of LOOP. */
487
488unsigned
489get_loop_level (const struct loop *loop)
490{
491 const struct loop *ploop;
492 unsigned mx = 0, l;
493
494 for (ploop = loop->inner; ploop; ploop = ploop->next)
495 {
496 l = get_loop_level (ploop);
497 if (l >= mx)
498 mx = l + 1;
499 }
500 return mx;
501}
5e962776
ZD
502
503/* Returns estimate on cost of computing SEQ. */
504
505static unsigned
506seq_cost (rtx seq)
507{
508 unsigned cost = 0;
509 rtx set;
510
511 for (; seq; seq = NEXT_INSN (seq))
512 {
513 set = single_set (seq);
514 if (set)
515 cost += rtx_cost (set, SET);
516 else
517 cost++;
518 }
519
520 return cost;
521}
522
523/* The properties of the target. */
524
8b11a64c
ZD
525unsigned target_avail_regs; /* Number of available registers. */
526unsigned target_res_regs; /* Number of reserved registers. */
527unsigned target_small_cost; /* The cost for register when there is a free one. */
528unsigned target_pres_cost; /* The cost for register when there are not too many
5e962776 529 free ones. */
8b11a64c 530unsigned target_spill_cost; /* The cost for register when we need to spill. */
5e962776
ZD
531
532/* Initialize the constants for computing set costs. */
533
534void
535init_set_costs (void)
536{
537 rtx seq;
538 rtx reg1 = gen_raw_REG (SImode, FIRST_PSEUDO_REGISTER);
539 rtx reg2 = gen_raw_REG (SImode, FIRST_PSEUDO_REGISTER + 1);
540 rtx addr = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER + 2);
541 rtx mem = validize_mem (gen_rtx_MEM (SImode, addr));
542 unsigned i;
543
544 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
545 if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i)
546 && !fixed_regs[i])
8b11a64c 547 target_avail_regs++;
5e962776 548
8b11a64c 549 target_res_regs = 3;
5e962776
ZD
550
551 /* These are really just heuristic values. */
c22cacf3 552
5e962776
ZD
553 start_sequence ();
554 emit_move_insn (reg1, reg2);
555 seq = get_insns ();
556 end_sequence ();
8b11a64c
ZD
557 target_small_cost = seq_cost (seq);
558 target_pres_cost = 2 * target_small_cost;
5e962776
ZD
559
560 start_sequence ();
561 emit_move_insn (mem, reg1);
562 emit_move_insn (reg2, mem);
563 seq = get_insns ();
564 end_sequence ();
8b11a64c 565 target_spill_cost = seq_cost (seq);
5e962776
ZD
566}
567
568/* Calculates cost for having SIZE new loop global variables. REGS_USED is the
569 number of global registers used in loop. N_USES is the number of relevant
570 variable uses. */
571
572unsigned
573global_cost_for_size (unsigned size, unsigned regs_used, unsigned n_uses)
574{
575 unsigned regs_needed = regs_used + size;
576 unsigned cost = 0;
577
8b11a64c
ZD
578 if (regs_needed + target_res_regs <= target_avail_regs)
579 cost += target_small_cost * size;
580 else if (regs_needed <= target_avail_regs)
581 cost += target_pres_cost * size;
5e962776
ZD
582 else
583 {
8b11a64c
ZD
584 cost += target_pres_cost * size;
585 cost += target_spill_cost * n_uses * (regs_needed - target_avail_regs) / regs_needed;
5e962776
ZD
586 }
587
588 return cost;
589}
590
d73be268 591/* Sets EDGE_LOOP_EXIT flag for all loop exits. */
70388d94
ZD
592
593void
d73be268 594mark_loop_exit_edges (void)
70388d94
ZD
595{
596 basic_block bb;
597 edge e;
c22cacf3 598
d73be268 599 if (!current_loops)
70388d94
ZD
600 return;
601
602 FOR_EACH_BB (bb)
603 {
604 edge_iterator ei;
605
70388d94
ZD
606 FOR_EACH_EDGE (e, ei, bb->succs)
607 {
2ff3e325
ZD
608 if (bb->loop_father->outer
609 && loop_exit_edge_p (bb->loop_father, e))
70388d94
ZD
610 e->flags |= EDGE_LOOP_EXIT;
611 else
612 e->flags &= ~EDGE_LOOP_EXIT;
613 }
614 }
615}
616