1 /* Natural loop analysis code for GNU compiler.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
32 /* Checks whether BB is executed exactly once in each LOOP iteration. */
35 just_once_each_iteration_p (struct loop
*loop
, basic_block bb
)
37 /* It must be executed at least once each iteration. */
38 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, bb
))
42 if (bb
->loop_father
!= loop
)
45 /* But this was not enough. We might have some irreducible loop here. */
46 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
52 /* Structure representing edge of a graph. */
56 int src
, dest
; /* Source and destination. */
57 struct edge
*pred_next
, *succ_next
;
58 /* Next edge in predecessor and successor lists. */
59 void *data
; /* Data attached to the edge. */
62 /* Structure representing vertex of a graph. */
66 struct edge
*pred
, *succ
;
67 /* Lists of predecessors and successors. */
68 int component
; /* Number of dfs restarts before reaching the
70 int post
; /* Postorder number. */
73 /* Structure representing a graph. */
77 int n_vertices
; /* Number of vertices. */
78 struct vertex
*vertices
;
82 /* Dumps graph G into F. */
84 extern void dump_graph (FILE *, struct graph
*);
85 void dump_graph (FILE *f
, struct graph
*g
)
90 for (i
= 0; i
< g
->n_vertices
; i
++)
92 if (!g
->vertices
[i
].pred
93 && !g
->vertices
[i
].succ
)
96 fprintf (f
, "%d (%d)\t<-", i
, g
->vertices
[i
].component
);
97 for (e
= g
->vertices
[i
].pred
; e
; e
= e
->pred_next
)
98 fprintf (f
, " %d", e
->src
);
102 for (e
= g
->vertices
[i
].succ
; e
; e
= e
->succ_next
)
103 fprintf (f
, " %d", e
->dest
);
108 /* Creates a new graph with N_VERTICES vertices. */
110 static struct graph
*
111 new_graph (int n_vertices
)
113 struct graph
*g
= xmalloc (sizeof (struct graph
));
115 g
->n_vertices
= n_vertices
;
116 g
->vertices
= xcalloc (n_vertices
, sizeof (struct vertex
));
121 /* Adds an edge from F to T to graph G, with DATA attached. */
124 add_edge (struct graph
*g
, int f
, int t
, void *data
)
126 struct edge
*e
= xmalloc (sizeof (struct edge
));
132 e
->pred_next
= g
->vertices
[t
].pred
;
133 g
->vertices
[t
].pred
= e
;
135 e
->succ_next
= g
->vertices
[f
].succ
;
136 g
->vertices
[f
].succ
= e
;
139 /* Runs dfs search over vertices of G, from NQ vertices in queue QS.
140 The vertices in postorder are stored into QT. If FORWARD is false,
141 backward dfs is run. */
144 dfs (struct graph
*g
, int *qs
, int nq
, int *qt
, bool forward
)
146 int i
, tick
= 0, v
, comp
= 0, top
;
148 struct edge
**stack
= xmalloc (sizeof (struct edge
*) * g
->n_vertices
);
150 for (i
= 0; i
< g
->n_vertices
; i
++)
152 g
->vertices
[i
].component
= -1;
153 g
->vertices
[i
].post
= -1;
156 #define FST_EDGE(V) (forward ? g->vertices[(V)].succ : g->vertices[(V)].pred)
157 #define NEXT_EDGE(E) (forward ? (E)->succ_next : (E)->pred_next)
158 #define EDGE_SRC(E) (forward ? (E)->src : (E)->dest)
159 #define EDGE_DEST(E) (forward ? (E)->dest : (E)->src)
161 for (i
= 0; i
< nq
; i
++)
164 if (g
->vertices
[v
].post
!= -1)
167 g
->vertices
[v
].component
= comp
++;
173 while (e
&& g
->vertices
[EDGE_DEST (e
)].component
!= -1)
180 g
->vertices
[v
].post
= tick
++;
194 g
->vertices
[v
].component
= comp
- 1;
201 /* Marks the edge E in graph G irreducible if it connects two vertices in the
205 check_irred (struct graph
*g
, struct edge
*e
)
209 /* All edges should lead from a component with higher number to the
210 one with lower one. */
211 if (g
->vertices
[e
->src
].component
< g
->vertices
[e
->dest
].component
)
214 if (g
->vertices
[e
->src
].component
!= g
->vertices
[e
->dest
].component
)
217 real
->flags
|= EDGE_IRREDUCIBLE_LOOP
;
218 if (flow_bb_inside_loop_p (real
->src
->loop_father
, real
->dest
))
219 real
->src
->flags
|= BB_IRREDUCIBLE_LOOP
;
222 /* Runs CALLBACK for all edges in G. */
225 for_each_edge (struct graph
*g
,
226 void (callback
) (struct graph
*, struct edge
*))
231 for (i
= 0; i
< g
->n_vertices
; i
++)
232 for (e
= g
->vertices
[i
].succ
; e
; e
= e
->succ_next
)
236 /* Releases the memory occupied by G. */
239 free_graph (struct graph
*g
)
244 for (i
= 0; i
< g
->n_vertices
; i
++)
245 for (e
= g
->vertices
[i
].succ
; e
; e
= n
)
254 /* Marks blocks and edges that are part of non-recognized loops; i.e. we
255 throw away all latch edges and mark blocks inside any remaining cycle.
256 Everything is a bit complicated due to fact we do not want to do this
257 for parts of cycles that only "pass" through some loop -- i.e. for
258 each cycle, we want to mark blocks that belong directly to innermost
259 loop containing the whole cycle.
261 LOOPS is the loop tree. */
263 #define LOOP_REPR(LOOP) ((LOOP)->num + last_basic_block)
264 #define BB_REPR(BB) ((BB)->index + 1)
267 mark_irreducible_loops (struct loops
*loops
)
273 int *queue1
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
274 int *queue2
= xmalloc ((last_basic_block
+ loops
->num
) * sizeof (int));
278 /* Reset the flags. */
279 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
281 act
->flags
&= ~BB_IRREDUCIBLE_LOOP
;
282 for (e
= act
->succ
; e
; e
= e
->succ_next
)
283 e
->flags
&= ~EDGE_IRREDUCIBLE_LOOP
;
286 /* Create the edge lists. */
287 g
= new_graph (last_basic_block
+ loops
->num
);
289 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
290 for (e
= act
->succ
; e
; e
= e
->succ_next
)
292 /* Ignore edges to exit. */
293 if (e
->dest
== EXIT_BLOCK_PTR
)
296 /* And latch edges. */
297 if (e
->dest
->loop_father
->header
== e
->dest
298 && e
->dest
->loop_father
->latch
== act
)
301 /* Edges inside a single loop should be left where they are. Edges
302 to subloop headers should lead to representative of the subloop,
303 but from the same place.
305 Edges exiting loops should lead from representative
306 of the son of nearest common ancestor of the loops in that
310 dest
= BB_REPR (e
->dest
);
312 if (e
->dest
->loop_father
->header
== e
->dest
)
313 dest
= LOOP_REPR (e
->dest
->loop_father
);
315 if (!flow_bb_inside_loop_p (act
->loop_father
, e
->dest
))
317 depth
= find_common_loop (act
->loop_father
,
318 e
->dest
->loop_father
)->depth
+ 1;
319 if (depth
== act
->loop_father
->depth
)
320 cloop
= act
->loop_father
;
322 cloop
= act
->loop_father
->pred
[depth
];
324 src
= LOOP_REPR (cloop
);
327 add_edge (g
, src
, dest
, e
);
330 /* Find the strongly connected components. Use the algorithm of Tarjan --
331 first determine the postorder dfs numbering in reversed graph, then
332 run the dfs on the original graph in the order given by decreasing
333 numbers assigned by the previous pass. */
335 FOR_BB_BETWEEN (act
, ENTRY_BLOCK_PTR
, EXIT_BLOCK_PTR
, next_bb
)
337 queue1
[nq
++] = BB_REPR (act
);
339 for (i
= 1; i
< (int) loops
->num
; i
++)
340 if (loops
->parray
[i
])
341 queue1
[nq
++] = LOOP_REPR (loops
->parray
[i
]);
342 dfs (g
, queue1
, nq
, queue2
, false);
343 for (i
= 0; i
< nq
; i
++)
344 queue1
[i
] = queue2
[nq
- i
- 1];
345 dfs (g
, queue1
, nq
, NULL
, true);
347 /* Mark the irreducible loops. */
348 for_each_edge (g
, check_irred
);
354 loops
->state
|= LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
;
357 /* Counts number of insns inside LOOP. */
359 num_loop_insns (struct loop
*loop
)
361 basic_block
*bbs
, bb
;
362 unsigned i
, ninsns
= 0;
365 bbs
= get_loop_body (loop
);
366 for (i
= 0; i
< loop
->num_nodes
; i
++)
370 for (insn
= BB_HEAD (bb
); insn
!= BB_END (bb
); insn
= NEXT_INSN (insn
))
379 /* Counts number of insns executed on average per iteration LOOP. */
381 average_num_loop_insns (struct loop
*loop
)
383 basic_block
*bbs
, bb
;
384 unsigned i
, binsns
, ninsns
, ratio
;
388 bbs
= get_loop_body (loop
);
389 for (i
= 0; i
< loop
->num_nodes
; i
++)
394 for (insn
= BB_HEAD (bb
); insn
!= BB_END (bb
); insn
= NEXT_INSN (insn
))
398 ratio
= loop
->header
->frequency
== 0
400 : (bb
->frequency
* BB_FREQ_MAX
) / loop
->header
->frequency
;
401 ninsns
+= binsns
* ratio
;
405 ninsns
/= BB_FREQ_MAX
;
407 ninsns
= 1; /* To avoid division by zero. */
412 /* Returns expected number of LOOP iterations.
413 Compute upper bound on number of iterations in case they do not fit integer
414 to help loop peeling heuristics. Use exact counts if at all possible. */
416 expected_loop_iterations (const struct loop
*loop
)
420 if (loop
->header
->count
)
422 gcov_type count_in
, count_latch
, expected
;
427 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
428 if (e
->src
== loop
->latch
)
429 count_latch
= e
->count
;
431 count_in
+= e
->count
;
434 expected
= count_latch
* 2;
436 expected
= (count_latch
+ count_in
- 1) / count_in
;
438 /* Avoid overflows. */
439 return (expected
> REG_BR_PROB_BASE
? REG_BR_PROB_BASE
: expected
);
443 int freq_in
, freq_latch
;
448 for (e
= loop
->header
->pred
; e
; e
= e
->pred_next
)
449 if (e
->src
== loop
->latch
)
450 freq_latch
= EDGE_FREQUENCY (e
);
452 freq_in
+= EDGE_FREQUENCY (e
);
455 return freq_latch
* 2;
457 return (freq_latch
+ freq_in
- 1) / freq_in
;
461 /* Returns the maximum level of nesting of subloops of LOOP. */
464 get_loop_level (const struct loop
*loop
)
466 const struct loop
*ploop
;
469 for (ploop
= loop
->inner
; ploop
; ploop
= ploop
->next
)
471 l
= get_loop_level (ploop
);