]>
Commit | Line | Data |
---|---|---|
862be747 | 1 | /* Natural loop analysis code for GNU compiler. |
d353bf18 | 2 | Copyright (C) 2002-2015 Free Software Foundation, Inc. |
862be747 | 3 | |
4 | This file is part of GCC. | |
5 | ||
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 | |
8c4c00c1 | 8 | Software Foundation; either version 3, or (at your option) any later |
862be747 | 9 | version. |
10 | ||
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 | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
862be747 | 19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
9ef16211 | 23 | #include "backend.h" |
862be747 | 24 | #include "rtl.h" |
9ef16211 | 25 | #include "tree.h" |
7c29e30e | 26 | #include "predict.h" |
27 | #include "expmed.h" | |
28 | #include "insn-config.h" | |
29 | #include "emit-rtl.h" | |
30 | #include "cfgloop.h" | |
d53441c8 | 31 | #include "flags.h" |
d53441c8 | 32 | #include "alias.h" |
d53441c8 | 33 | #include "dojump.h" |
34 | #include "explow.h" | |
35 | #include "calls.h" | |
d53441c8 | 36 | #include "varasm.h" |
37 | #include "stmt.h" | |
862be747 | 38 | #include "expr.h" |
3f9439d7 | 39 | #include "graphds.h" |
47dd2e78 | 40 | #include "params.h" |
862be747 | 41 | |
e8aa5a28 | 42 | struct target_cfgloop default_target_cfgloop; |
43 | #if SWITCHABLE_TARGET | |
44 | struct target_cfgloop *this_target_cfgloop = &default_target_cfgloop; | |
45 | #endif | |
46 | ||
862be747 | 47 | /* Checks whether BB is executed exactly once in each LOOP iteration. */ |
9c1ccc0f | 48 | |
862be747 | 49 | bool |
7ecb5bb2 | 50 | just_once_each_iteration_p (const struct loop *loop, const_basic_block bb) |
862be747 | 51 | { |
52 | /* It must be executed at least once each iteration. */ | |
0051c76a | 53 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb)) |
862be747 | 54 | return false; |
55 | ||
56 | /* And just once. */ | |
57 | if (bb->loop_father != loop) | |
58 | return false; | |
59 | ||
60 | /* But this was not enough. We might have some irreducible loop here. */ | |
61 | if (bb->flags & BB_IRREDUCIBLE_LOOP) | |
62 | return false; | |
63 | ||
64 | return true; | |
65 | } | |
66 | ||
a5414ff5 | 67 | /* Marks blocks and edges that are part of non-recognized loops; i.e. we |
68 | throw away all latch edges and mark blocks inside any remaining cycle. | |
69 | Everything is a bit complicated due to fact we do not want to do this | |
70 | for parts of cycles that only "pass" through some loop -- i.e. for | |
71 | each cycle, we want to mark blocks that belong directly to innermost | |
69b23c5d | 72 | loop containing the whole cycle. |
a0c938f0 | 73 | |
69b23c5d | 74 | LOOPS is the loop tree. */ |
75 | ||
fe672ac0 | 76 | #define LOOP_REPR(LOOP) ((LOOP)->num + last_basic_block_for_fn (cfun)) |
69b23c5d | 77 | #define BB_REPR(BB) ((BB)->index + 1) |
78 | ||
c9263b6a | 79 | bool |
7194de72 | 80 | mark_irreducible_loops (void) |
862be747 | 81 | { |
862be747 | 82 | basic_block act; |
c9263b6a | 83 | struct graph_edge *ge; |
69b23c5d | 84 | edge e; |
cd665a06 | 85 | edge_iterator ei; |
3f9439d7 | 86 | int src, dest; |
87 | unsigned depth; | |
69b23c5d | 88 | struct graph *g; |
41f75a99 | 89 | int num = number_of_loops (cfun); |
3f9439d7 | 90 | struct loop *cloop; |
c9263b6a | 91 | bool irred_loop_found = false; |
92 | int i; | |
862be747 | 93 | |
7a3bf727 | 94 | gcc_assert (current_loops != NULL); |
95 | ||
a5414ff5 | 96 | /* Reset the flags. */ |
34154e27 | 97 | FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR_FOR_FN (cfun), |
98 | EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) | |
a5414ff5 | 99 | { |
100 | act->flags &= ~BB_IRREDUCIBLE_LOOP; | |
cd665a06 | 101 | FOR_EACH_EDGE (e, ei, act->succs) |
a5414ff5 | 102 | e->flags &= ~EDGE_IRREDUCIBLE_LOOP; |
103 | } | |
104 | ||
862be747 | 105 | /* Create the edge lists. */ |
fe672ac0 | 106 | g = new_graph (last_basic_block_for_fn (cfun) + num); |
69b23c5d | 107 | |
34154e27 | 108 | FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR_FOR_FN (cfun), |
109 | EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) | |
cd665a06 | 110 | FOR_EACH_EDGE (e, ei, act->succs) |
862be747 | 111 | { |
a0c938f0 | 112 | /* Ignore edges to exit. */ |
34154e27 | 113 | if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
862be747 | 114 | continue; |
69b23c5d | 115 | |
88e6f696 | 116 | src = BB_REPR (act); |
117 | dest = BB_REPR (e->dest); | |
69b23c5d | 118 | |
7a3bf727 | 119 | /* Ignore latch edges. */ |
120 | if (e->dest->loop_father->header == e->dest | |
121 | && e->dest->loop_father->latch == act) | |
122 | continue; | |
123 | ||
124 | /* Edges inside a single loop should be left where they are. Edges | |
125 | to subloop headers should lead to representative of the subloop, | |
126 | but from the same place. | |
69b23c5d | 127 | |
7a3bf727 | 128 | Edges exiting loops should lead from representative |
129 | of the son of nearest common ancestor of the loops in that | |
130 | act lays. */ | |
131 | ||
132 | if (e->dest->loop_father->header == e->dest) | |
133 | dest = LOOP_REPR (e->dest->loop_father); | |
134 | ||
135 | if (!flow_bb_inside_loop_p (act->loop_father, e->dest)) | |
136 | { | |
137 | depth = 1 + loop_depth (find_common_loop (act->loop_father, | |
138 | e->dest->loop_father)); | |
139 | if (depth == loop_depth (act->loop_father)) | |
140 | cloop = act->loop_father; | |
141 | else | |
f1f41a6c | 142 | cloop = (*act->loop_father->superloops)[depth]; |
7a3bf727 | 143 | |
144 | src = LOOP_REPR (cloop); | |
862be747 | 145 | } |
69b23c5d | 146 | |
3f9439d7 | 147 | add_edge (g, src, dest)->data = e; |
862be747 | 148 | } |
149 | ||
3f9439d7 | 150 | /* Find the strongly connected components. */ |
151 | graphds_scc (g, NULL); | |
862be747 | 152 | |
69b23c5d | 153 | /* Mark the irreducible loops. */ |
c9263b6a | 154 | for (i = 0; i < g->n_vertices; i++) |
155 | for (ge = g->vertices[i].succ; ge; ge = ge->succ_next) | |
156 | { | |
157 | edge real = (edge) ge->data; | |
158 | /* edge E in graph G is irreducible if it connects two vertices in the | |
159 | same scc. */ | |
160 | ||
161 | /* All edges should lead from a component with higher number to the | |
162 | one with lower one. */ | |
163 | gcc_assert (g->vertices[ge->src].component >= g->vertices[ge->dest].component); | |
164 | ||
165 | if (g->vertices[ge->src].component != g->vertices[ge->dest].component) | |
166 | continue; | |
167 | ||
168 | real->flags |= EDGE_IRREDUCIBLE_LOOP; | |
169 | irred_loop_found = true; | |
170 | if (flow_bb_inside_loop_p (real->src->loop_father, real->dest)) | |
171 | real->src->flags |= BB_IRREDUCIBLE_LOOP; | |
172 | } | |
862be747 | 173 | |
69b23c5d | 174 | free_graph (g); |
862be747 | 175 | |
f24ec26f | 176 | loops_state_set (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS); |
c9263b6a | 177 | return irred_loop_found; |
862be747 | 178 | } |
179 | ||
180 | /* Counts number of insns inside LOOP. */ | |
181 | int | |
7ecb5bb2 | 182 | num_loop_insns (const struct loop *loop) |
862be747 | 183 | { |
184 | basic_block *bbs, bb; | |
185 | unsigned i, ninsns = 0; | |
2b422a4f | 186 | rtx_insn *insn; |
862be747 | 187 | |
188 | bbs = get_loop_body (loop); | |
189 | for (i = 0; i < loop->num_nodes; i++) | |
190 | { | |
191 | bb = bbs[i]; | |
9845d120 | 192 | FOR_BB_INSNS (bb, insn) |
193 | if (NONDEBUG_INSN_P (insn)) | |
c87a3eff | 194 | ninsns++; |
862be747 | 195 | } |
47c3d424 | 196 | free (bbs); |
197 | ||
198 | if (!ninsns) | |
199 | ninsns = 1; /* To avoid division by zero. */ | |
4c9e08a4 | 200 | |
862be747 | 201 | return ninsns; |
202 | } | |
203 | ||
204 | /* Counts number of insns executed on average per iteration LOOP. */ | |
205 | int | |
7ecb5bb2 | 206 | average_num_loop_insns (const struct loop *loop) |
862be747 | 207 | { |
208 | basic_block *bbs, bb; | |
209 | unsigned i, binsns, ninsns, ratio; | |
2b422a4f | 210 | rtx_insn *insn; |
862be747 | 211 | |
212 | ninsns = 0; | |
213 | bbs = get_loop_body (loop); | |
214 | for (i = 0; i < loop->num_nodes; i++) | |
215 | { | |
216 | bb = bbs[i]; | |
217 | ||
9845d120 | 218 | binsns = 0; |
219 | FOR_BB_INSNS (bb, insn) | |
220 | if (NONDEBUG_INSN_P (insn)) | |
c87a3eff | 221 | binsns++; |
862be747 | 222 | |
223 | ratio = loop->header->frequency == 0 | |
224 | ? BB_FREQ_MAX | |
225 | : (bb->frequency * BB_FREQ_MAX) / loop->header->frequency; | |
226 | ninsns += binsns * ratio; | |
227 | } | |
47c3d424 | 228 | free (bbs); |
4c9e08a4 | 229 | |
862be747 | 230 | ninsns /= BB_FREQ_MAX; |
231 | if (!ninsns) | |
232 | ninsns = 1; /* To avoid division by zero. */ | |
233 | ||
234 | return ninsns; | |
235 | } | |
236 | ||
d97e22fb | 237 | /* Returns expected number of iterations of LOOP, according to |
238 | measured or guessed profile. No bounding is done on the | |
239 | value. */ | |
240 | ||
241 | gcov_type | |
242 | expected_loop_iterations_unbounded (const struct loop *loop) | |
862be747 | 243 | { |
244 | edge e; | |
cd665a06 | 245 | edge_iterator ei; |
862be747 | 246 | |
7a22afab | 247 | if (loop->latch->count || loop->header->count) |
862be747 | 248 | { |
249 | gcov_type count_in, count_latch, expected; | |
250 | ||
251 | count_in = 0; | |
252 | count_latch = 0; | |
253 | ||
cd665a06 | 254 | FOR_EACH_EDGE (e, ei, loop->header->preds) |
862be747 | 255 | if (e->src == loop->latch) |
256 | count_latch = e->count; | |
257 | else | |
258 | count_in += e->count; | |
259 | ||
260 | if (count_in == 0) | |
a0c938f0 | 261 | expected = count_latch * 2; |
d04f7eb9 | 262 | else |
a0c938f0 | 263 | expected = (count_latch + count_in - 1) / count_in; |
862be747 | 264 | |
d97e22fb | 265 | return expected; |
862be747 | 266 | } |
267 | else | |
268 | { | |
269 | int freq_in, freq_latch; | |
270 | ||
271 | freq_in = 0; | |
272 | freq_latch = 0; | |
273 | ||
cd665a06 | 274 | FOR_EACH_EDGE (e, ei, loop->header->preds) |
862be747 | 275 | if (e->src == loop->latch) |
276 | freq_latch = EDGE_FREQUENCY (e); | |
277 | else | |
278 | freq_in += EDGE_FREQUENCY (e); | |
279 | ||
280 | if (freq_in == 0) | |
d04f7eb9 | 281 | return freq_latch * 2; |
862be747 | 282 | |
283 | return (freq_latch + freq_in - 1) / freq_in; | |
284 | } | |
285 | } | |
2d49f824 | 286 | |
d97e22fb | 287 | /* Returns expected number of LOOP iterations. The returned value is bounded |
288 | by REG_BR_PROB_BASE. */ | |
289 | ||
290 | unsigned | |
291 | expected_loop_iterations (const struct loop *loop) | |
292 | { | |
293 | gcov_type expected = expected_loop_iterations_unbounded (loop); | |
294 | return (expected > REG_BR_PROB_BASE ? REG_BR_PROB_BASE : expected); | |
295 | } | |
296 | ||
2d49f824 | 297 | /* Returns the maximum level of nesting of subloops of LOOP. */ |
298 | ||
299 | unsigned | |
300 | get_loop_level (const struct loop *loop) | |
301 | { | |
302 | const struct loop *ploop; | |
303 | unsigned mx = 0, l; | |
304 | ||
305 | for (ploop = loop->inner; ploop; ploop = ploop->next) | |
306 | { | |
307 | l = get_loop_level (ploop); | |
308 | if (l >= mx) | |
309 | mx = l + 1; | |
310 | } | |
311 | return mx; | |
312 | } | |
3a0ecac2 | 313 | |
3a0ecac2 | 314 | /* Initialize the constants for computing set costs. */ |
315 | ||
316 | void | |
317 | init_set_costs (void) | |
318 | { | |
f529eb25 | 319 | int speed; |
2b422a4f | 320 | rtx_insn *seq; |
dcd6d0f4 | 321 | rtx reg1 = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 1); |
322 | rtx reg2 = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 2); | |
323 | rtx addr = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 3); | |
3a0ecac2 | 324 | rtx mem = validize_mem (gen_rtx_MEM (SImode, addr)); |
325 | unsigned i; | |
326 | ||
6d8b68a3 | 327 | target_avail_regs = 0; |
a6b74a67 | 328 | target_clobbered_regs = 0; |
3a0ecac2 | 329 | for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) |
330 | if (TEST_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], i) | |
331 | && !fixed_regs[i]) | |
a6b74a67 | 332 | { |
333 | target_avail_regs++; | |
334 | if (call_used_regs[i]) | |
335 | target_clobbered_regs++; | |
336 | } | |
3a0ecac2 | 337 | |
dec41e98 | 338 | target_res_regs = 3; |
3a0ecac2 | 339 | |
f529eb25 | 340 | for (speed = 0; speed < 2; speed++) |
341 | { | |
342 | crtl->maybe_hot_insn_p = speed; | |
343 | /* Set up the costs for using extra registers: | |
344 | ||
345 | 1) If not many free registers remain, we should prefer having an | |
346 | additional move to decreasing the number of available registers. | |
347 | (TARGET_REG_COST). | |
348 | 2) If no registers are available, we need to spill, which may require | |
349 | storing the old value to memory and loading it back | |
350 | (TARGET_SPILL_COST). */ | |
351 | ||
352 | start_sequence (); | |
353 | emit_move_insn (reg1, reg2); | |
354 | seq = get_insns (); | |
355 | end_sequence (); | |
356 | target_reg_cost [speed] = seq_cost (seq, speed); | |
357 | ||
358 | start_sequence (); | |
359 | emit_move_insn (mem, reg1); | |
360 | emit_move_insn (reg2, mem); | |
361 | seq = get_insns (); | |
362 | end_sequence (); | |
363 | target_spill_cost [speed] = seq_cost (seq, speed); | |
364 | } | |
365 | default_rtl_profile (); | |
3a0ecac2 | 366 | } |
367 | ||
25153338 | 368 | /* Estimates cost of increased register pressure caused by making N_NEW new |
369 | registers live around the loop. N_OLD is the number of registers live | |
a6b74a67 | 370 | around the loop. If CALL_P is true, also take into account that |
371 | call-used registers may be clobbered in the loop body, reducing the | |
372 | number of available registers before we spill. */ | |
3a0ecac2 | 373 | |
374 | unsigned | |
a6b74a67 | 375 | estimate_reg_pressure_cost (unsigned n_new, unsigned n_old, bool speed, |
376 | bool call_p) | |
3a0ecac2 | 377 | { |
47dd2e78 | 378 | unsigned cost; |
25153338 | 379 | unsigned regs_needed = n_new + n_old; |
a6b74a67 | 380 | unsigned available_regs = target_avail_regs; |
381 | ||
382 | /* If there is a call in the loop body, the call-clobbered registers | |
383 | are not available for loop invariants. */ | |
384 | if (call_p) | |
385 | available_regs = available_regs - target_clobbered_regs; | |
3a0ecac2 | 386 | |
25153338 | 387 | /* If we have enough registers, we should use them and not restrict |
388 | the transformations unnecessarily. */ | |
a6b74a67 | 389 | if (regs_needed + target_res_regs <= available_regs) |
25153338 | 390 | return 0; |
391 | ||
a6b74a67 | 392 | if (regs_needed <= available_regs) |
47dd2e78 | 393 | /* If we are close to running out of registers, try to preserve |
394 | them. */ | |
f529eb25 | 395 | cost = target_reg_cost [speed] * n_new; |
47dd2e78 | 396 | else |
397 | /* If we run out of registers, it is very expensive to add another | |
398 | one. */ | |
f529eb25 | 399 | cost = target_spill_cost [speed] * n_new; |
47dd2e78 | 400 | |
cf709bf6 | 401 | if (optimize && (flag_ira_region == IRA_REGION_ALL |
402 | || flag_ira_region == IRA_REGION_MIXED) | |
41f75a99 | 403 | && number_of_loops (cfun) <= (unsigned) IRA_MAX_LOOPS_NUM) |
47dd2e78 | 404 | /* IRA regional allocation deals with high register pressure |
405 | better. So decrease the cost (to do more accurate the cost | |
406 | calculation for IRA, we need to know how many registers lives | |
407 | through the loop transparently). */ | |
408 | cost /= 2; | |
409 | ||
410 | return cost; | |
3a0ecac2 | 411 | } |
412 | ||
7194de72 | 413 | /* Sets EDGE_LOOP_EXIT flag for all loop exits. */ |
ffc6b5d5 | 414 | |
415 | void | |
7194de72 | 416 | mark_loop_exit_edges (void) |
ffc6b5d5 | 417 | { |
418 | basic_block bb; | |
419 | edge e; | |
a0c938f0 | 420 | |
41f75a99 | 421 | if (number_of_loops (cfun) <= 1) |
ffc6b5d5 | 422 | return; |
423 | ||
fc00614f | 424 | FOR_EACH_BB_FN (bb, cfun) |
ffc6b5d5 | 425 | { |
426 | edge_iterator ei; | |
427 | ||
ffc6b5d5 | 428 | FOR_EACH_EDGE (e, ei, bb->succs) |
429 | { | |
9e3536f4 | 430 | if (loop_outer (bb->loop_father) |
c088dce6 | 431 | && loop_exit_edge_p (bb->loop_father, e)) |
ffc6b5d5 | 432 | e->flags |= EDGE_LOOP_EXIT; |
433 | else | |
434 | e->flags &= ~EDGE_LOOP_EXIT; | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
3681186e | 439 | /* Return exit edge if loop has only one exit that is likely |
440 | to be executed on runtime (i.e. it is not EH or leading | |
441 | to noreturn call. */ | |
442 | ||
443 | edge | |
444 | single_likely_exit (struct loop *loop) | |
445 | { | |
446 | edge found = single_exit (loop); | |
f1f41a6c | 447 | vec<edge> exits; |
3681186e | 448 | unsigned i; |
449 | edge ex; | |
450 | ||
451 | if (found) | |
452 | return found; | |
453 | exits = get_loop_exit_edges (loop); | |
f1f41a6c | 454 | FOR_EACH_VEC_ELT (exits, i, ex) |
3681186e | 455 | { |
456 | if (ex->flags & (EDGE_EH | EDGE_ABNORMAL_CALL)) | |
457 | continue; | |
458 | /* The constant of 5 is set in a way so noreturn calls are | |
459 | ruled out by this test. The static branch prediction algorithm | |
460 | will not assign such a low probability to conditionals for usual | |
461 | reasons. */ | |
f26d8580 | 462 | if (profile_status_for_fn (cfun) != PROFILE_ABSENT |
3681186e | 463 | && ex->probability < 5 && !ex->count) |
464 | continue; | |
465 | if (!found) | |
466 | found = ex; | |
467 | else | |
468 | { | |
f1f41a6c | 469 | exits.release (); |
3681186e | 470 | return NULL; |
471 | } | |
472 | } | |
f1f41a6c | 473 | exits.release (); |
3681186e | 474 | return found; |
475 | } | |
d583c979 | 476 | |
477 | ||
478 | /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs | |
479 | order against direction of edges from latch. Specially, if | |
480 | header != latch, latch is the 1-st block. */ | |
481 | ||
f1f41a6c | 482 | vec<basic_block> |
d583c979 | 483 | get_loop_hot_path (const struct loop *loop) |
484 | { | |
485 | basic_block bb = loop->header; | |
1e094109 | 486 | vec<basic_block> path = vNULL; |
d583c979 | 487 | bitmap visited = BITMAP_ALLOC (NULL); |
488 | ||
489 | while (true) | |
490 | { | |
491 | edge_iterator ei; | |
492 | edge e; | |
493 | edge best = NULL; | |
494 | ||
f1f41a6c | 495 | path.safe_push (bb); |
d583c979 | 496 | bitmap_set_bit (visited, bb->index); |
497 | FOR_EACH_EDGE (e, ei, bb->succs) | |
498 | if ((!best || e->probability > best->probability) | |
499 | && !loop_exit_edge_p (loop, e) | |
500 | && !bitmap_bit_p (visited, e->dest->index)) | |
501 | best = e; | |
502 | if (!best || best->dest == loop->header) | |
503 | break; | |
504 | bb = best->dest; | |
505 | } | |
506 | BITMAP_FREE (visited); | |
507 | return path; | |
508 | } |