comparison gcc/cfgloopanal.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* Natural loop analysis code for GNU compiler. 1 /* Natural loop analysis code for GNU compiler.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Free Software Foundation, Inc.
4 3
5 This file is part of GCC. 4 This file is part of GCC.
6 5
7 GCC is free software; you can redistribute it and/or modify it under 6 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free 7 the terms of the GNU General Public License as published by the Free
19 <http://www.gnu.org/licenses/>. */ 18 <http://www.gnu.org/licenses/>. */
20 19
21 #include "config.h" 20 #include "config.h"
22 #include "system.h" 21 #include "system.h"
23 #include "coretypes.h" 22 #include "coretypes.h"
24 #include "tm.h" 23 #include "backend.h"
25 #include "rtl.h" 24 #include "rtl.h"
26 #include "hard-reg-set.h" 25 #include "tree.h"
27 #include "obstack.h" 26 #include "predict.h"
28 #include "basic-block.h" 27 #include "memmodel.h"
28 #include "emit-rtl.h"
29 #include "cfgloop.h" 29 #include "cfgloop.h"
30 #include "explow.h"
30 #include "expr.h" 31 #include "expr.h"
31 #include "output.h"
32 #include "graphds.h" 32 #include "graphds.h"
33 #include "params.h" 33 #include "params.h"
34 34
35 struct target_cfgloop default_target_cfgloop; 35 struct target_cfgloop default_target_cfgloop;
36 #if SWITCHABLE_TARGET 36 #if SWITCHABLE_TARGET
64 each cycle, we want to mark blocks that belong directly to innermost 64 each cycle, we want to mark blocks that belong directly to innermost
65 loop containing the whole cycle. 65 loop containing the whole cycle.
66 66
67 LOOPS is the loop tree. */ 67 LOOPS is the loop tree. */
68 68
69 #define LOOP_REPR(LOOP) ((LOOP)->num + last_basic_block) 69 #define LOOP_REPR(LOOP) ((LOOP)->num + last_basic_block_for_fn (cfun))
70 #define BB_REPR(BB) ((BB)->index + 1) 70 #define BB_REPR(BB) ((BB)->index + 1)
71 71
72 bool 72 bool
73 mark_irreducible_loops (void) 73 mark_irreducible_loops (void)
74 { 74 {
77 edge e; 77 edge e;
78 edge_iterator ei; 78 edge_iterator ei;
79 int src, dest; 79 int src, dest;
80 unsigned depth; 80 unsigned depth;
81 struct graph *g; 81 struct graph *g;
82 int num = number_of_loops (); 82 int num = number_of_loops (cfun);
83 struct loop *cloop; 83 struct loop *cloop;
84 bool irred_loop_found = false; 84 bool irred_loop_found = false;
85 int i; 85 int i;
86 86
87 gcc_assert (current_loops != NULL); 87 gcc_assert (current_loops != NULL);
88 88
89 /* Reset the flags. */ 89 /* Reset the flags. */
90 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb) 90 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR_FOR_FN (cfun),
91 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
91 { 92 {
92 act->flags &= ~BB_IRREDUCIBLE_LOOP; 93 act->flags &= ~BB_IRREDUCIBLE_LOOP;
93 FOR_EACH_EDGE (e, ei, act->succs) 94 FOR_EACH_EDGE (e, ei, act->succs)
94 e->flags &= ~EDGE_IRREDUCIBLE_LOOP; 95 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
95 } 96 }
96 97
97 /* Create the edge lists. */ 98 /* Create the edge lists. */
98 g = new_graph (last_basic_block + num); 99 g = new_graph (last_basic_block_for_fn (cfun) + num);
99 100
100 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb) 101 FOR_BB_BETWEEN (act, ENTRY_BLOCK_PTR_FOR_FN (cfun),
102 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
101 FOR_EACH_EDGE (e, ei, act->succs) 103 FOR_EACH_EDGE (e, ei, act->succs)
102 { 104 {
103 /* Ignore edges to exit. */ 105 /* Ignore edges to exit. */
104 if (e->dest == EXIT_BLOCK_PTR) 106 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
105 continue; 107 continue;
106 108
107 src = BB_REPR (act); 109 src = BB_REPR (act);
108 dest = BB_REPR (e->dest); 110 dest = BB_REPR (e->dest);
109 111
128 depth = 1 + loop_depth (find_common_loop (act->loop_father, 130 depth = 1 + loop_depth (find_common_loop (act->loop_father,
129 e->dest->loop_father)); 131 e->dest->loop_father));
130 if (depth == loop_depth (act->loop_father)) 132 if (depth == loop_depth (act->loop_father))
131 cloop = act->loop_father; 133 cloop = act->loop_father;
132 else 134 else
133 cloop = VEC_index (loop_p, act->loop_father->superloops, depth); 135 cloop = (*act->loop_father->superloops)[depth];
134 136
135 src = LOOP_REPR (cloop); 137 src = LOOP_REPR (cloop);
136 } 138 }
137 139
138 add_edge (g, src, dest)->data = e; 140 add_edge (g, src, dest)->data = e;
172 int 174 int
173 num_loop_insns (const struct loop *loop) 175 num_loop_insns (const struct loop *loop)
174 { 176 {
175 basic_block *bbs, bb; 177 basic_block *bbs, bb;
176 unsigned i, ninsns = 0; 178 unsigned i, ninsns = 0;
177 rtx insn; 179 rtx_insn *insn;
178 180
179 bbs = get_loop_body (loop); 181 bbs = get_loop_body (loop);
180 for (i = 0; i < loop->num_nodes; i++) 182 for (i = 0; i < loop->num_nodes; i++)
181 { 183 {
182 bb = bbs[i]; 184 bb = bbs[i];
196 int 198 int
197 average_num_loop_insns (const struct loop *loop) 199 average_num_loop_insns (const struct loop *loop)
198 { 200 {
199 basic_block *bbs, bb; 201 basic_block *bbs, bb;
200 unsigned i, binsns, ninsns, ratio; 202 unsigned i, binsns, ninsns, ratio;
201 rtx insn; 203 rtx_insn *insn;
202 204
203 ninsns = 0; 205 ninsns = 0;
204 bbs = get_loop_body (loop); 206 bbs = get_loop_body (loop);
205 for (i = 0; i < loop->num_nodes; i++) 207 for (i = 0; i < loop->num_nodes; i++)
206 { 208 {
228 /* Returns expected number of iterations of LOOP, according to 230 /* Returns expected number of iterations of LOOP, according to
229 measured or guessed profile. No bounding is done on the 231 measured or guessed profile. No bounding is done on the
230 value. */ 232 value. */
231 233
232 gcov_type 234 gcov_type
233 expected_loop_iterations_unbounded (const struct loop *loop) 235 expected_loop_iterations_unbounded (const struct loop *loop,
236 bool *read_profile_p)
234 { 237 {
235 edge e; 238 edge e;
236 edge_iterator ei; 239 edge_iterator ei;
237 240 gcov_type expected = -1;
238 if (loop->latch->count || loop->header->count) 241
239 { 242 if (read_profile_p)
240 gcov_type count_in, count_latch, expected; 243 *read_profile_p = false;
241 244
242 count_in = 0; 245 /* If we have no profile at all, use AVG_LOOP_NITER. */
243 count_latch = 0; 246 if (profile_status_for_fn (cfun) == PROFILE_ABSENT)
247 expected = PARAM_VALUE (PARAM_AVG_LOOP_NITER);
248 else if (loop->latch && (loop->latch->count.reliable_p ()
249 || loop->header->count.reliable_p ()))
250 {
251 profile_count count_in = profile_count::zero (),
252 count_latch = profile_count::zero ();
244 253
245 FOR_EACH_EDGE (e, ei, loop->header->preds) 254 FOR_EACH_EDGE (e, ei, loop->header->preds)
246 if (e->src == loop->latch) 255 if (e->src == loop->latch)
247 count_latch = e->count; 256 count_latch = e->count ();
248 else 257 else
249 count_in += e->count; 258 count_in += e->count ();
250 259
251 if (count_in == 0) 260 if (!count_latch.initialized_p ())
252 expected = count_latch * 2; 261 ;
262 else if (!(count_in > profile_count::zero ()))
263 expected = count_latch.to_gcov_type () * 2;
253 else 264 else
254 expected = (count_latch + count_in - 1) / count_in; 265 {
255 266 expected = (count_latch.to_gcov_type () + count_in.to_gcov_type ()
256 return expected; 267 - 1) / count_in.to_gcov_type ();
257 } 268 if (read_profile_p)
258 else 269 *read_profile_p = true;
270 }
271 }
272 if (expected == -1)
259 { 273 {
260 int freq_in, freq_latch; 274 int freq_in, freq_latch;
261 275
262 freq_in = 0; 276 freq_in = 0;
263 freq_latch = 0; 277 freq_latch = 0;
264 278
265 FOR_EACH_EDGE (e, ei, loop->header->preds) 279 FOR_EACH_EDGE (e, ei, loop->header->preds)
266 if (e->src == loop->latch) 280 if (flow_bb_inside_loop_p (loop, e->src))
267 freq_latch = EDGE_FREQUENCY (e); 281 freq_latch += EDGE_FREQUENCY (e);
268 else 282 else
269 freq_in += EDGE_FREQUENCY (e); 283 freq_in += EDGE_FREQUENCY (e);
270 284
271 if (freq_in == 0) 285 if (freq_in == 0)
272 return freq_latch * 2; 286 {
273 287 /* If we have no profile at all, use AVG_LOOP_NITER iterations. */
274 return (freq_latch + freq_in - 1) / freq_in; 288 if (!freq_latch)
275 } 289 expected = PARAM_VALUE (PARAM_AVG_LOOP_NITER);
290 else
291 expected = freq_latch * 2;
292 }
293 else
294 expected = (freq_latch + freq_in - 1) / freq_in;
295 }
296
297 HOST_WIDE_INT max = get_max_loop_iterations_int (loop);
298 if (max != -1 && max < expected)
299 return max;
300 return expected;
276 } 301 }
277 302
278 /* Returns expected number of LOOP iterations. The returned value is bounded 303 /* Returns expected number of LOOP iterations. The returned value is bounded
279 by REG_BR_PROB_BASE. */ 304 by REG_BR_PROB_BASE. */
280 305
281 unsigned 306 unsigned
282 expected_loop_iterations (const struct loop *loop) 307 expected_loop_iterations (struct loop *loop)
283 { 308 {
284 gcov_type expected = expected_loop_iterations_unbounded (loop); 309 gcov_type expected = expected_loop_iterations_unbounded (loop);
285 return (expected > REG_BR_PROB_BASE ? REG_BR_PROB_BASE : expected); 310 return (expected > REG_BR_PROB_BASE ? REG_BR_PROB_BASE : expected);
286 } 311 }
287 312
300 mx = l + 1; 325 mx = l + 1;
301 } 326 }
302 return mx; 327 return mx;
303 } 328 }
304 329
305 /* Returns estimate on cost of computing SEQ. */
306
307 static unsigned
308 seq_cost (const_rtx seq, bool speed)
309 {
310 unsigned cost = 0;
311 rtx set;
312
313 for (; seq; seq = NEXT_INSN (seq))
314 {
315 set = single_set (seq);
316 if (set)
317 cost += rtx_cost (set, SET, speed);
318 else
319 cost++;
320 }
321
322 return cost;
323 }
324
325 /* Initialize the constants for computing set costs. */ 330 /* Initialize the constants for computing set costs. */
326 331
327 void 332 void
328 init_set_costs (void) 333 init_set_costs (void)
329 { 334 {
330 int speed; 335 int speed;
331 rtx seq; 336 rtx_insn *seq;
332 rtx reg1 = gen_raw_REG (SImode, FIRST_PSEUDO_REGISTER); 337 rtx reg1 = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 1);
333 rtx reg2 = gen_raw_REG (SImode, FIRST_PSEUDO_REGISTER + 1); 338 rtx reg2 = gen_raw_REG (SImode, LAST_VIRTUAL_REGISTER + 2);
334 rtx addr = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER + 2); 339 rtx addr = gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 3);
335 rtx mem = validize_mem (gen_rtx_MEM (SImode, addr)); 340 rtx mem = validize_mem (gen_rtx_MEM (SImode, addr));
336 unsigned i; 341 unsigned i;
337 342
338 target_avail_regs = 0; 343 target_avail_regs = 0;
339 target_clobbered_regs = 0; 344 target_clobbered_regs = 0;
409 one. */ 414 one. */
410 cost = target_spill_cost [speed] * n_new; 415 cost = target_spill_cost [speed] * n_new;
411 416
412 if (optimize && (flag_ira_region == IRA_REGION_ALL 417 if (optimize && (flag_ira_region == IRA_REGION_ALL
413 || flag_ira_region == IRA_REGION_MIXED) 418 || flag_ira_region == IRA_REGION_MIXED)
414 && number_of_loops () <= (unsigned) IRA_MAX_LOOPS_NUM) 419 && number_of_loops (cfun) <= (unsigned) IRA_MAX_LOOPS_NUM)
415 /* IRA regional allocation deals with high register pressure 420 /* IRA regional allocation deals with high register pressure
416 better. So decrease the cost (to do more accurate the cost 421 better. So decrease the cost (to do more accurate the cost
417 calculation for IRA, we need to know how many registers lives 422 calculation for IRA, we need to know how many registers lives
418 through the loop transparently). */ 423 through the loop transparently). */
419 cost /= 2; 424 cost /= 2;
427 mark_loop_exit_edges (void) 432 mark_loop_exit_edges (void)
428 { 433 {
429 basic_block bb; 434 basic_block bb;
430 edge e; 435 edge e;
431 436
432 if (number_of_loops () <= 1) 437 if (number_of_loops (cfun) <= 1)
433 return; 438 return;
434 439
435 FOR_EACH_BB (bb) 440 FOR_EACH_BB_FN (bb, cfun)
436 { 441 {
437 edge_iterator ei; 442 edge_iterator ei;
438 443
439 FOR_EACH_EDGE (e, ei, bb->succs) 444 FOR_EACH_EDGE (e, ei, bb->succs)
440 { 445 {
445 e->flags &= ~EDGE_LOOP_EXIT; 450 e->flags &= ~EDGE_LOOP_EXIT;
446 } 451 }
447 } 452 }
448 } 453 }
449 454
455 /* Return exit edge if loop has only one exit that is likely
456 to be executed on runtime (i.e. it is not EH or leading
457 to noreturn call. */
458
459 edge
460 single_likely_exit (struct loop *loop)
461 {
462 edge found = single_exit (loop);
463 vec<edge> exits;
464 unsigned i;
465 edge ex;
466
467 if (found)
468 return found;
469 exits = get_loop_exit_edges (loop);
470 FOR_EACH_VEC_ELT (exits, i, ex)
471 {
472 if (probably_never_executed_edge_p (cfun, ex)
473 /* We want to rule out paths to noreturns but not low probabilities
474 resulting from adjustments or combining.
475 FIXME: once we have better quality tracking, make this more
476 robust. */
477 || ex->probability <= profile_probability::very_unlikely ())
478 continue;
479 if (!found)
480 found = ex;
481 else
482 {
483 exits.release ();
484 return NULL;
485 }
486 }
487 exits.release ();
488 return found;
489 }
490
491
492 /* Gets basic blocks of a LOOP. Header is the 0-th block, rest is in dfs
493 order against direction of edges from latch. Specially, if
494 header != latch, latch is the 1-st block. */
495
496 vec<basic_block>
497 get_loop_hot_path (const struct loop *loop)
498 {
499 basic_block bb = loop->header;
500 vec<basic_block> path = vNULL;
501 bitmap visited = BITMAP_ALLOC (NULL);
502
503 while (true)
504 {
505 edge_iterator ei;
506 edge e;
507 edge best = NULL;
508
509 path.safe_push (bb);
510 bitmap_set_bit (visited, bb->index);
511 FOR_EACH_EDGE (e, ei, bb->succs)
512 if ((!best || e->probability > best->probability)
513 && !loop_exit_edge_p (loop, e)
514 && !bitmap_bit_p (visited, e->dest->index))
515 best = e;
516 if (!best || best->dest == loop->header)
517 break;
518 bb = best->dest;
519 }
520 BITMAP_FREE (visited);
521 return path;
522 }