]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cfghooks.c
sse.md (VI): New mode iterator.
[thirdparty/gcc.git] / gcc / cfghooks.c
CommitLineData
9ee634e3 1/* Hooks for cfg representation specific functions.
d652f226
JJ
2 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2010
3 Free Software Foundation, Inc.
9ee634e3
JH
4 Contributed by Sebastian Pop <s.pop@laposte.net>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
9dcd6f09 10the Free Software Foundation; either version 3, or (at your option)
9ee634e3
JH
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
9ee634e3
JH
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "tree.h"
27#include "rtl.h"
28#include "basic-block.h"
6de9cd9a 29#include "tree-flow.h"
f470c378 30#include "timevar.h"
718f9c0f 31#include "diagnostic-core.h"
598ec7bd 32#include "cfgloop.h"
9ee634e3
JH
33
34/* A pointer to one of the hooks containers. */
f470c378 35static struct cfg_hooks *cfg_hooks;
9ee634e3
JH
36
37/* Initialization of functions specific to the rtl IR. */
d329e058
AJ
38void
39rtl_register_cfg_hooks (void)
9ee634e3
JH
40{
41 cfg_hooks = &rtl_cfg_hooks;
42}
43
44/* Initialization of functions specific to the rtl IR. */
d329e058
AJ
45void
46cfg_layout_rtl_register_cfg_hooks (void)
9ee634e3
JH
47{
48 cfg_hooks = &cfg_layout_rtl_cfg_hooks;
49}
f470c378 50
6de9cd9a
DN
51/* Initialization of functions specific to the tree IR. */
52
53void
726a989a 54gimple_register_cfg_hooks (void)
6de9cd9a 55{
726a989a 56 cfg_hooks = &gimple_cfg_hooks;
6de9cd9a
DN
57}
58
e855c69d
AB
59struct cfg_hooks
60get_cfg_hooks (void)
61{
62 return *cfg_hooks;
63}
64
65void
66set_cfg_hooks (struct cfg_hooks new_cfg_hooks)
67{
68 *cfg_hooks = new_cfg_hooks;
69}
70
52bca999 71/* Returns current ir type. */
6de9cd9a 72
52bca999
SB
73enum ir_type
74current_ir_type (void)
6de9cd9a 75{
726a989a 76 if (cfg_hooks == &gimple_cfg_hooks)
52bca999
SB
77 return IR_GIMPLE;
78 else if (cfg_hooks == &rtl_cfg_hooks)
79 return IR_RTL_CFGRTL;
80 else if (cfg_hooks == &cfg_layout_rtl_cfg_hooks)
81 return IR_RTL_CFGLAYOUT;
82 else
83 gcc_unreachable ();
6de9cd9a
DN
84}
85
f470c378
ZD
86/* Verify the CFG consistency.
87
88 Currently it does following: checks edge and basic block list correctness
89 and calls into IL dependent checking then. */
90
24e47c76 91DEBUG_FUNCTION void
f470c378
ZD
92verify_flow_info (void)
93{
94 size_t *edge_checksum;
50f63b1a 95 int err = 0;
f470c378
ZD
96 basic_block bb, last_bb_seen;
97 basic_block *last_visited;
98
99 timevar_push (TV_CFG_VERIFY);
5ed6ace5
MD
100 last_visited = XCNEWVEC (basic_block, last_basic_block);
101 edge_checksum = XCNEWVEC (size_t, last_basic_block);
f470c378
ZD
102
103 /* Check bb chain & numbers. */
104 last_bb_seen = ENTRY_BLOCK_PTR;
105 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, NULL, next_bb)
106 {
107 if (bb != EXIT_BLOCK_PTR
108 && bb != BASIC_BLOCK (bb->index))
109 {
110 error ("bb %d on wrong place", bb->index);
111 err = 1;
112 }
113
114 if (bb->prev_bb != last_bb_seen)
115 {
116 error ("prev_bb of %d should be %d, not %d",
117 bb->index, last_bb_seen->index, bb->prev_bb->index);
118 err = 1;
119 }
120
121 last_bb_seen = bb;
122 }
123
124 /* Now check the basic blocks (boundaries etc.) */
125 FOR_EACH_BB_REVERSE (bb)
126 {
127 int n_fallthru = 0;
128 edge e;
628f6a4e 129 edge_iterator ei;
f470c378 130
598ec7bd
ZD
131 if (bb->loop_father != NULL && current_loops == NULL)
132 {
133 error ("verify_flow_info: Block %i has loop_father, but there are no loops",
134 bb->index);
135 err = 1;
136 }
137 if (bb->loop_father == NULL && current_loops != NULL)
138 {
139 error ("verify_flow_info: Block %i lacks loop_father", bb->index);
140 err = 1;
141 }
142
f470c378
ZD
143 if (bb->count < 0)
144 {
145 error ("verify_flow_info: Wrong count of block %i %i",
c22cacf3 146 bb->index, (int)bb->count);
f470c378
ZD
147 err = 1;
148 }
149 if (bb->frequency < 0)
150 {
151 error ("verify_flow_info: Wrong frequency of block %i %i",
c22cacf3 152 bb->index, bb->frequency);
f470c378
ZD
153 err = 1;
154 }
628f6a4e 155 FOR_EACH_EDGE (e, ei, bb->succs)
f470c378 156 {
24bd1a0b 157 if (last_visited [e->dest->index] == bb)
f470c378
ZD
158 {
159 error ("verify_flow_info: Duplicate edge %i->%i",
160 e->src->index, e->dest->index);
161 err = 1;
162 }
163 if (e->probability < 0 || e->probability > REG_BR_PROB_BASE)
164 {
165 error ("verify_flow_info: Wrong probability of edge %i->%i %i",
166 e->src->index, e->dest->index, e->probability);
167 err = 1;
168 }
169 if (e->count < 0)
170 {
171 error ("verify_flow_info: Wrong count of edge %i->%i %i",
172 e->src->index, e->dest->index, (int)e->count);
173 err = 1;
174 }
175
24bd1a0b 176 last_visited [e->dest->index] = bb;
f470c378
ZD
177
178 if (e->flags & EDGE_FALLTHRU)
179 n_fallthru++;
180
181 if (e->src != bb)
182 {
183 error ("verify_flow_info: Basic block %d succ edge is corrupted",
184 bb->index);
185 fprintf (stderr, "Predecessor: ");
186 dump_edge_info (stderr, e, 0);
187 fprintf (stderr, "\nSuccessor: ");
188 dump_edge_info (stderr, e, 1);
189 fprintf (stderr, "\n");
190 err = 1;
191 }
192
24bd1a0b 193 edge_checksum[e->dest->index] += (size_t) e;
f470c378
ZD
194 }
195 if (n_fallthru > 1)
196 {
ab532386 197 error ("wrong amount of branch edges after unconditional jump %i", bb->index);
f470c378
ZD
198 err = 1;
199 }
200
628f6a4e 201 FOR_EACH_EDGE (e, ei, bb->preds)
f470c378
ZD
202 {
203 if (e->dest != bb)
204 {
205 error ("basic block %d pred edge is corrupted", bb->index);
206 fputs ("Predecessor: ", stderr);
207 dump_edge_info (stderr, e, 0);
208 fputs ("\nSuccessor: ", stderr);
209 dump_edge_info (stderr, e, 1);
210 fputc ('\n', stderr);
211 err = 1;
212 }
73553871
KH
213
214 if (ei.index != e->dest_idx)
215 {
216 error ("basic block %d pred edge is corrupted", bb->index);
217 error ("its dest_idx should be %d, not %d",
218 ei.index, e->dest_idx);
219 fputs ("Predecessor: ", stderr);
220 dump_edge_info (stderr, e, 0);
221 fputs ("\nSuccessor: ", stderr);
222 dump_edge_info (stderr, e, 1);
223 fputc ('\n', stderr);
224 err = 1;
225 }
226
24bd1a0b 227 edge_checksum[e->dest->index] -= (size_t) e;
f470c378
ZD
228 }
229 }
230
231 /* Complete edge checksumming for ENTRY and EXIT. */
232 {
233 edge e;
628f6a4e 234 edge_iterator ei;
f470c378 235
628f6a4e 236 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
24bd1a0b 237 edge_checksum[e->dest->index] += (size_t) e;
f470c378 238
628f6a4e 239 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
24bd1a0b 240 edge_checksum[e->dest->index] -= (size_t) e;
f470c378
ZD
241 }
242
243 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
24bd1a0b 244 if (edge_checksum[bb->index])
f470c378
ZD
245 {
246 error ("basic block %i edge lists are corrupted", bb->index);
247 err = 1;
248 }
249
f470c378
ZD
250 last_bb_seen = ENTRY_BLOCK_PTR;
251
252 /* Clean up. */
253 free (last_visited);
254 free (edge_checksum);
255
256 if (cfg_hooks->verify_flow_info)
257 err |= cfg_hooks->verify_flow_info ();
258 if (err)
259 internal_error ("verify_flow_info failed");
260 timevar_pop (TV_CFG_VERIFY);
261}
262
263/* Print out one basic block. This function takes care of the purely
264 graph related information. The cfg hook for the active representation
265 should dump representation-specific information. */
266
267void
268dump_bb (basic_block bb, FILE *outf, int indent)
269{
270 edge e;
628f6a4e 271 edge_iterator ei;
f470c378 272 char *s_indent;
c22cacf3 273
ae50c0cb 274 s_indent = (char *) alloca ((size_t) indent + 1);
400e39e3 275 memset (s_indent, ' ', (size_t) indent);
f470c378
ZD
276 s_indent[indent] = '\0';
277
278 fprintf (outf, ";;%s basic block %d, loop depth %d, count ",
279 s_indent, bb->index, bb->loop_depth);
280 fprintf (outf, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) bb->count);
281 putc ('\n', outf);
282
283 fprintf (outf, ";;%s prev block ", s_indent);
284 if (bb->prev_bb)
285 fprintf (outf, "%d, ", bb->prev_bb->index);
286 else
287 fprintf (outf, "(nil), ");
288 fprintf (outf, "next block ");
289 if (bb->next_bb)
290 fprintf (outf, "%d", bb->next_bb->index);
291 else
292 fprintf (outf, "(nil)");
293 putc ('\n', outf);
294
295 fprintf (outf, ";;%s pred: ", s_indent);
628f6a4e 296 FOR_EACH_EDGE (e, ei, bb->preds)
f470c378
ZD
297 dump_edge_info (outf, e, 0);
298 putc ('\n', outf);
299
300 fprintf (outf, ";;%s succ: ", s_indent);
628f6a4e 301 FOR_EACH_EDGE (e, ei, bb->succs)
f470c378
ZD
302 dump_edge_info (outf, e, 1);
303 putc ('\n', outf);
304
305 if (cfg_hooks->dump_bb)
726a989a 306 cfg_hooks->dump_bb (bb, outf, indent, 0);
f470c378
ZD
307}
308
309/* Redirect edge E to the given basic block DEST and update underlying program
310 representation. Returns edge representing redirected branch (that may not
311 be equivalent to E in the case of duplicate edges being removed) or NULL
312 if edge is not easily redirectable for whatever reason. */
313
6de9cd9a 314edge
f470c378
ZD
315redirect_edge_and_branch (edge e, basic_block dest)
316{
6de9cd9a 317 edge ret;
f470c378
ZD
318
319 if (!cfg_hooks->redirect_edge_and_branch)
ab532386 320 internal_error ("%s does not support redirect_edge_and_branch",
f470c378
ZD
321 cfg_hooks->name);
322
323 ret = cfg_hooks->redirect_edge_and_branch (e, dest);
324
452ba14d
ZD
325 /* If RET != E, then either the redirection failed, or the edge E
326 was removed since RET already lead to the same destination. */
327 if (current_loops != NULL && ret == e)
328 rescan_loop_exit (e, false, false);
6270df4c 329
f470c378
ZD
330 return ret;
331}
332
14fa2cc0
ZD
333/* Returns true if it is possible to remove the edge E by redirecting it
334 to the destination of the other edge going from its source. */
335
336bool
9678086d 337can_remove_branch_p (const_edge e)
14fa2cc0
ZD
338{
339 if (!cfg_hooks->can_remove_branch_p)
340 internal_error ("%s does not support can_remove_branch_p",
341 cfg_hooks->name);
342
343 if (EDGE_COUNT (e->src->succs) != 2)
344 return false;
345
346 return cfg_hooks->can_remove_branch_p (e);
347}
348
349/* Removes E, by redirecting it to the destination of the other edge going
350 from its source. Can_remove_branch_p must be true for E, hence this
351 operation cannot fail. */
352
353void
354remove_branch (edge e)
355{
356 edge other;
357 basic_block src = e->src;
358 int irr;
359
360 gcc_assert (EDGE_COUNT (e->src->succs) == 2);
361
362 other = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
363 irr = other->flags & EDGE_IRREDUCIBLE_LOOP;
364
14fa2cc0
ZD
365 e = redirect_edge_and_branch (e, other->dest);
366 gcc_assert (e != NULL);
367
368 e->flags &= ~EDGE_IRREDUCIBLE_LOOP;
369 e->flags |= irr;
370}
371
452ba14d
ZD
372/* Removes edge E from cfg. Unlike remove_branch, it does not update IL. */
373
374void
375remove_edge (edge e)
376{
377 if (current_loops != NULL)
378 rescan_loop_exit (e, false, true);
379
380 remove_edge_raw (e);
381}
382
f470c378
ZD
383/* Redirect the edge E to basic block DEST even if it requires creating
384 of a new basic block; then it returns the newly created basic block.
385 Aborts when redirection is impossible. */
386
387basic_block
388redirect_edge_and_branch_force (edge e, basic_block dest)
389{
6270df4c 390 basic_block ret, src = e->src;
598ec7bd 391 struct loop *loop;
f470c378
ZD
392
393 if (!cfg_hooks->redirect_edge_and_branch_force)
ab532386 394 internal_error ("%s does not support redirect_edge_and_branch_force",
f470c378
ZD
395 cfg_hooks->name);
396
6270df4c
ZD
397 if (current_loops != NULL)
398 rescan_loop_exit (e, false, true);
399
f470c378 400 ret = cfg_hooks->redirect_edge_and_branch_force (e, dest);
cf103ca4 401 if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
89f8f30f
ZD
402 set_immediate_dominator (CDI_DOMINATORS, ret, src);
403
6270df4c 404 if (current_loops != NULL)
598ec7bd 405 {
6270df4c
ZD
406 if (ret != NULL)
407 {
408 loop = find_common_loop (single_pred (ret)->loop_father,
409 single_succ (ret)->loop_father);
410 add_bb_to_loop (ret, loop);
411 }
412 else if (find_edge (src, dest) == e)
413 rescan_loop_exit (e, true, false);
598ec7bd 414 }
f470c378
ZD
415
416 return ret;
417}
418
419/* Splits basic block BB after the specified instruction I (but at least after
420 the labels). If I is NULL, splits just after labels. The newly created edge
421 is returned. The new basic block is created just after the old one. */
422
423edge
424split_block (basic_block bb, void *i)
425{
426 basic_block new_bb;
ede7cba0 427 edge res;
f470c378
ZD
428
429 if (!cfg_hooks->split_block)
ab532386 430 internal_error ("%s does not support split_block", cfg_hooks->name);
f470c378
ZD
431
432 new_bb = cfg_hooks->split_block (bb, i);
433 if (!new_bb)
434 return NULL;
435
436 new_bb->count = bb->count;
437 new_bb->frequency = bb->frequency;
438 new_bb->loop_depth = bb->loop_depth;
cbea518e 439 new_bb->discriminator = bb->discriminator;
f470c378 440
fce22de5 441 if (dom_info_available_p (CDI_DOMINATORS))
f470c378
ZD
442 {
443 redirect_immediate_dominators (CDI_DOMINATORS, bb, new_bb);
444 set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
445 }
446
598ec7bd 447 if (current_loops != NULL)
dbdc7f97
ZD
448 {
449 add_bb_to_loop (new_bb, bb->loop_father);
450 if (bb->loop_father->latch == bb)
451 bb->loop_father->latch = new_bb;
452 }
598ec7bd 453
ede7cba0
SP
454 res = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
455
456 if (bb->flags & BB_IRREDUCIBLE_LOOP)
457 {
458 new_bb->flags |= BB_IRREDUCIBLE_LOOP;
459 res->flags |= EDGE_IRREDUCIBLE_LOOP;
460 }
461
462 return res;
f470c378
ZD
463}
464
465/* Splits block BB just after labels. The newly created edge is returned. */
466
467edge
468split_block_after_labels (basic_block bb)
469{
470 return split_block (bb, NULL);
471}
472
321440fd 473/* Moves block BB immediately after block AFTER. Returns false if the
f470c378
ZD
474 movement was impossible. */
475
476bool
477move_block_after (basic_block bb, basic_block after)
478{
479 bool ret;
480
481 if (!cfg_hooks->move_block_after)
ab532386 482 internal_error ("%s does not support move_block_after", cfg_hooks->name);
f470c378
ZD
483
484 ret = cfg_hooks->move_block_after (bb, after);
485
486 return ret;
487}
488
489/* Deletes the basic block BB. */
490
491void
492delete_basic_block (basic_block bb)
493{
494 if (!cfg_hooks->delete_basic_block)
ab532386 495 internal_error ("%s does not support delete_basic_block", cfg_hooks->name);
f470c378
ZD
496
497 cfg_hooks->delete_basic_block (bb);
498
598ec7bd
ZD
499 if (current_loops != NULL)
500 {
501 struct loop *loop = bb->loop_father;
502
503 /* If we remove the header or the latch of a loop, mark the loop for
504 removal by setting its header and latch to NULL. */
505 if (loop->latch == bb
506 || loop->header == bb)
507 {
508 loop->header = NULL;
509 loop->latch = NULL;
510 }
511
512 remove_bb_from_loops (bb);
513 }
514
f470c378
ZD
515 /* Remove the edges into and out of this block. Note that there may
516 indeed be edges in, if we are removing an unreachable loop. */
628f6a4e
BE
517 while (EDGE_COUNT (bb->preds) != 0)
518 remove_edge (EDGE_PRED (bb, 0));
519 while (EDGE_COUNT (bb->succs) != 0)
520 remove_edge (EDGE_SUCC (bb, 0));
f470c378 521
2b28c07a 522 if (dom_info_available_p (CDI_DOMINATORS))
f470c378 523 delete_from_dominance_info (CDI_DOMINATORS, bb);
2b28c07a 524 if (dom_info_available_p (CDI_POST_DOMINATORS))
f470c378
ZD
525 delete_from_dominance_info (CDI_POST_DOMINATORS, bb);
526
527 /* Remove the basic block from the array. */
528 expunge_block (bb);
529}
530
531/* Splits edge E and returns the newly created basic block. */
532
533basic_block
534split_edge (edge e)
535{
536 basic_block ret;
537 gcov_type count = e->count;
538 int freq = EDGE_FREQUENCY (e);
017b3258 539 edge f;
a746fd8c 540 bool irr = (e->flags & EDGE_IRREDUCIBLE_LOOP) != 0;
598ec7bd
ZD
541 struct loop *loop;
542 basic_block src = e->src, dest = e->dest;
f470c378
ZD
543
544 if (!cfg_hooks->split_edge)
ab532386 545 internal_error ("%s does not support split_edge", cfg_hooks->name);
f470c378 546
6270df4c
ZD
547 if (current_loops != NULL)
548 rescan_loop_exit (e, false, true);
549
f470c378
ZD
550 ret = cfg_hooks->split_edge (e);
551 ret->count = count;
552 ret->frequency = freq;
c5cbcccf
ZD
553 single_succ_edge (ret)->probability = REG_BR_PROB_BASE;
554 single_succ_edge (ret)->count = count;
f470c378 555
a746fd8c
ZD
556 if (irr)
557 {
558 ret->flags |= BB_IRREDUCIBLE_LOOP;
c5cbcccf
ZD
559 single_pred_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
560 single_succ_edge (ret)->flags |= EDGE_IRREDUCIBLE_LOOP;
a746fd8c
ZD
561 }
562
2b28c07a 563 if (dom_info_available_p (CDI_DOMINATORS))
c5cbcccf 564 set_immediate_dominator (CDI_DOMINATORS, ret, single_pred (ret));
f470c378 565
2b28c07a 566 if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
017b3258
ZD
567 {
568 /* There are two cases:
569
570 If the immediate dominator of e->dest is not e->src, it
571 remains unchanged.
572
573 If immediate dominator of e->dest is e->src, it may become
574 ret, provided that all other predecessors of e->dest are
575 dominated by e->dest. */
576
c5cbcccf
ZD
577 if (get_immediate_dominator (CDI_DOMINATORS, single_succ (ret))
578 == single_pred (ret))
017b3258 579 {
628f6a4e 580 edge_iterator ei;
c5cbcccf 581 FOR_EACH_EDGE (f, ei, single_succ (ret)->preds)
017b3258 582 {
c5cbcccf 583 if (f == single_succ_edge (ret))
017b3258
ZD
584 continue;
585
586 if (!dominated_by_p (CDI_DOMINATORS, f->src,
c5cbcccf 587 single_succ (ret)))
017b3258
ZD
588 break;
589 }
590
591 if (!f)
c5cbcccf 592 set_immediate_dominator (CDI_DOMINATORS, single_succ (ret), ret);
017b3258 593 }
598ec7bd
ZD
594 }
595
596 if (current_loops != NULL)
597 {
598 loop = find_common_loop (src->loop_father, dest->loop_father);
599 add_bb_to_loop (ret, loop);
600
601 if (loop->latch == src)
602 loop->latch = ret;
603 }
f470c378
ZD
604
605 return ret;
606}
607
608/* Creates a new basic block just after the basic block AFTER.
609 HEAD and END are the first and the last statement belonging
610 to the block. If both are NULL, an empty block is created. */
611
612basic_block
613create_basic_block (void *head, void *end, basic_block after)
614{
615 basic_block ret;
616
617 if (!cfg_hooks->create_basic_block)
ab532386 618 internal_error ("%s does not support create_basic_block", cfg_hooks->name);
f470c378
ZD
619
620 ret = cfg_hooks->create_basic_block (head, end, after);
621
2b28c07a 622 if (dom_info_available_p (CDI_DOMINATORS))
f470c378 623 add_to_dominance_info (CDI_DOMINATORS, ret);
2b28c07a 624 if (dom_info_available_p (CDI_POST_DOMINATORS))
f470c378
ZD
625 add_to_dominance_info (CDI_POST_DOMINATORS, ret);
626
627 return ret;
628}
629
630/* Creates an empty basic block just after basic block AFTER. */
631
632basic_block
633create_empty_bb (basic_block after)
634{
635 return create_basic_block (NULL, NULL, after);
636}
637
638/* Checks whether we may merge blocks BB1 and BB2. */
639
640bool
b48d0358 641can_merge_blocks_p (basic_block bb1, basic_block bb2)
f470c378
ZD
642{
643 bool ret;
644
645 if (!cfg_hooks->can_merge_blocks_p)
ab532386 646 internal_error ("%s does not support can_merge_blocks_p", cfg_hooks->name);
f470c378
ZD
647
648 ret = cfg_hooks->can_merge_blocks_p (bb1, bb2);
649
650 return ret;
651}
652
6de9cd9a
DN
653void
654predict_edge (edge e, enum br_predictor predictor, int probability)
655{
656 if (!cfg_hooks->predict_edge)
ab532386 657 internal_error ("%s does not support predict_edge", cfg_hooks->name);
6de9cd9a
DN
658
659 cfg_hooks->predict_edge (e, predictor, probability);
660}
661
662bool
9678086d 663predicted_by_p (const_basic_block bb, enum br_predictor predictor)
6de9cd9a
DN
664{
665 if (!cfg_hooks->predict_edge)
ab532386 666 internal_error ("%s does not support predicted_by_p", cfg_hooks->name);
6de9cd9a
DN
667
668 return cfg_hooks->predicted_by_p (bb, predictor);
669}
670
f470c378
ZD
671/* Merges basic block B into basic block A. */
672
673void
674merge_blocks (basic_block a, basic_block b)
675{
676 edge e;
628f6a4e 677 edge_iterator ei;
f470c378
ZD
678
679 if (!cfg_hooks->merge_blocks)
ab532386 680 internal_error ("%s does not support merge_blocks", cfg_hooks->name);
f470c378 681
9e824336
ZD
682 cfg_hooks->merge_blocks (a, b);
683
598ec7bd
ZD
684 if (current_loops != NULL)
685 remove_bb_from_loops (b);
686
f470c378
ZD
687 /* Normally there should only be one successor of A and that is B, but
688 partway though the merge of blocks for conditional_execution we'll
689 be merging a TEST block with THEN and ELSE successors. Free the
690 whole lot of them and hope the caller knows what they're doing. */
628f6a4e
BE
691
692 while (EDGE_COUNT (a->succs) != 0)
452ba14d 693 remove_edge (EDGE_SUCC (a, 0));
f470c378
ZD
694
695 /* Adjust the edges out of B for the new owner. */
628f6a4e 696 FOR_EACH_EDGE (e, ei, b->succs)
6270df4c
ZD
697 {
698 e->src = a;
699 if (current_loops != NULL)
700 rescan_loop_exit (e, true, false);
701 }
628f6a4e 702 a->succs = b->succs;
f470c378
ZD
703 a->flags |= b->flags;
704
705 /* B hasn't quite yet ceased to exist. Attempt to prevent mishap. */
628f6a4e 706 b->preds = b->succs = NULL;
f470c378 707
2b28c07a 708 if (dom_info_available_p (CDI_DOMINATORS))
f470c378
ZD
709 redirect_immediate_dominators (CDI_DOMINATORS, b, a);
710
2b28c07a 711 if (dom_info_available_p (CDI_DOMINATORS))
f470c378 712 delete_from_dominance_info (CDI_DOMINATORS, b);
2b28c07a 713 if (dom_info_available_p (CDI_POST_DOMINATORS))
f470c378
ZD
714 delete_from_dominance_info (CDI_POST_DOMINATORS, b);
715
716 expunge_block (b);
717}
718
719/* Split BB into entry part and the rest (the rest is the newly created block).
720 Redirect those edges for that REDIRECT_EDGE_P returns true to the entry
721 part. Returns the edge connecting the entry part to the rest. */
722
723edge
724make_forwarder_block (basic_block bb, bool (*redirect_edge_p) (edge),
725 void (*new_bb_cbk) (basic_block))
726{
628f6a4e
BE
727 edge e, fallthru;
728 edge_iterator ei;
f470c378 729 basic_block dummy, jump;
598ec7bd 730 struct loop *loop, *ploop, *cloop;
f470c378
ZD
731
732 if (!cfg_hooks->make_forwarder_block)
ab532386 733 internal_error ("%s does not support make_forwarder_block",
f470c378
ZD
734 cfg_hooks->name);
735
736 fallthru = split_block_after_labels (bb);
737 dummy = fallthru->src;
738 bb = fallthru->dest;
739
740 /* Redirect back edges we want to keep. */
628f6a4e 741 for (ei = ei_start (dummy->preds); (e = ei_safe_edge (ei)); )
f470c378 742 {
e855c69d
AB
743 basic_block e_src;
744
f470c378 745 if (redirect_edge_p (e))
628f6a4e
BE
746 {
747 ei_next (&ei);
748 continue;
749 }
f470c378
ZD
750
751 dummy->frequency -= EDGE_FREQUENCY (e);
752 dummy->count -= e->count;
753 if (dummy->frequency < 0)
754 dummy->frequency = 0;
755 if (dummy->count < 0)
756 dummy->count = 0;
649b2789
PH
757 fallthru->count -= e->count;
758 if (fallthru->count < 0)
759 fallthru->count = 0;
f470c378 760
e855c69d 761 e_src = e->src;
f470c378 762 jump = redirect_edge_and_branch_force (e, bb);
e855c69d
AB
763 if (jump != NULL)
764 {
765 /* If we redirected the loop latch edge, the JUMP block now acts like
766 the new latch of the loop. */
767 if (current_loops != NULL
768 && dummy->loop_father != NULL
769 && dummy->loop_father->header == dummy
770 && dummy->loop_father->latch == e_src)
771 dummy->loop_father->latch = jump;
b8698a0f 772
e855c69d
AB
773 if (new_bb_cbk != NULL)
774 new_bb_cbk (jump);
775 }
f470c378
ZD
776 }
777
fce22de5 778 if (dom_info_available_p (CDI_DOMINATORS))
f470c378 779 {
66f97d31
ZD
780 VEC (basic_block, heap) *doms_to_fix = VEC_alloc (basic_block, heap, 2);
781 VEC_quick_push (basic_block, doms_to_fix, dummy);
782 VEC_quick_push (basic_block, doms_to_fix, bb);
783 iterate_fix_dominators (CDI_DOMINATORS, doms_to_fix, false);
784 VEC_free (basic_block, heap, doms_to_fix);
f470c378
ZD
785 }
786
598ec7bd
ZD
787 if (current_loops != NULL)
788 {
789 /* If we do not split a loop header, then both blocks belong to the
790 same loop. In case we split loop header and do not redirect the
791 latch edge to DUMMY, then DUMMY belongs to the outer loop, and
89f8f30f
ZD
792 BB becomes the new header. If latch is not recorded for the loop,
793 we leave this updating on the caller (this may only happen during
794 loop analysis). */
598ec7bd
ZD
795 loop = dummy->loop_father;
796 if (loop->header == dummy
89f8f30f 797 && loop->latch != NULL
598ec7bd
ZD
798 && find_edge (loop->latch, dummy) == NULL)
799 {
800 remove_bb_from_loops (dummy);
801 loop->header = bb;
802
803 cloop = loop;
804 FOR_EACH_EDGE (e, ei, dummy->preds)
805 {
806 cloop = find_common_loop (cloop, e->src->loop_father);
807 }
808 add_bb_to_loop (dummy, cloop);
809 }
810
811 /* In case we split loop latch, update it. */
9ba025a2 812 for (ploop = loop; ploop; ploop = loop_outer (ploop))
598ec7bd
ZD
813 if (ploop->latch == dummy)
814 ploop->latch = bb;
815 }
816
f470c378
ZD
817 cfg_hooks->make_forwarder_block (fallthru);
818
819 return fallthru;
820}
821
cf103ca4
EB
822/* Try to make the edge fallthru. */
823
f470c378
ZD
824void
825tidy_fallthru_edge (edge e)
826{
827 if (cfg_hooks->tidy_fallthru_edge)
828 cfg_hooks->tidy_fallthru_edge (e);
829}
830
831/* Fix up edges that now fall through, or rather should now fall through
832 but previously required a jump around now deleted blocks. Simplify
833 the search by only examining blocks numerically adjacent, since this
8effe856
EB
834 is how they were created.
835
836 ??? This routine is currently RTL specific. */
f470c378
ZD
837
838void
839tidy_fallthru_edges (void)
840{
841 basic_block b, c;
842
843 if (!cfg_hooks->tidy_fallthru_edge)
844 return;
845
846 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
847 return;
848
849 FOR_BB_BETWEEN (b, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, next_bb)
850 {
851 edge s;
852
853 c = b->next_bb;
854
855 /* We care about simple conditional or unconditional jumps with
856 a single successor.
857
858 If we had a conditional branch to the next instruction when
3cabd6d1
LB
859 CFG was built, then there will only be one out edge for the
860 block which ended with the conditional branch (since we do
861 not create duplicate edges).
f470c378
ZD
862
863 Furthermore, the edge will be marked as a fallthru because we
864 merge the flags for the duplicate edges. So we do not want to
865 check that the edge is not a FALLTHRU edge. */
866
c5cbcccf 867 if (single_succ_p (b))
628f6a4e 868 {
c5cbcccf 869 s = single_succ_edge (b);
628f6a4e
BE
870 if (! (s->flags & EDGE_COMPLEX)
871 && s->dest == c
872 && !find_reg_note (BB_END (b), REG_CROSSING_JUMP, NULL_RTX))
873 tidy_fallthru_edge (s);
874 }
f470c378
ZD
875 }
876}
6de9cd9a 877
cf103ca4
EB
878/* Edge E is assumed to be fallthru edge. Emit needed jump instruction
879 (and possibly create new basic block) to make edge non-fallthru.
880 Return newly created BB or NULL if none. */
881
882basic_block
883force_nonfallthru (edge e)
884{
885 basic_block ret, src = e->src, dest = e->dest;
886 struct loop *loop;
887
888 if (!cfg_hooks->force_nonfallthru)
889 internal_error ("%s does not support force_nonfallthru",
890 cfg_hooks->name);
891
892 if (current_loops != NULL)
893 rescan_loop_exit (e, false, true);
894
895 ret = cfg_hooks->force_nonfallthru (e);
896 if (ret != NULL && dom_info_available_p (CDI_DOMINATORS))
897 set_immediate_dominator (CDI_DOMINATORS, ret, src);
898
899 if (current_loops != NULL)
900 {
901 if (ret != NULL)
902 {
903 loop = find_common_loop (single_pred (ret)->loop_father,
904 single_succ (ret)->loop_father);
905 add_bb_to_loop (ret, loop);
906 }
907 else if (find_edge (src, dest) == e)
908 rescan_loop_exit (e, true, false);
909 }
910
911 return ret;
912}
913
6de9cd9a
DN
914/* Returns true if we can duplicate basic block BB. */
915
916bool
9678086d 917can_duplicate_block_p (const_basic_block bb)
6de9cd9a 918{
6de9cd9a 919 if (!cfg_hooks->can_duplicate_block_p)
ab532386 920 internal_error ("%s does not support can_duplicate_block_p",
6de9cd9a
DN
921 cfg_hooks->name);
922
923 if (bb == EXIT_BLOCK_PTR || bb == ENTRY_BLOCK_PTR)
924 return false;
925
6de9cd9a
DN
926 return cfg_hooks->can_duplicate_block_p (bb);
927}
928
929/* Duplicates basic block BB and redirects edge E to it. Returns the
b9a66240
ZD
930 new basic block. The new basic block is placed after the basic block
931 AFTER. */
6de9cd9a
DN
932
933basic_block
b9a66240 934duplicate_block (basic_block bb, edge e, basic_block after)
6de9cd9a
DN
935{
936 edge s, n;
937 basic_block new_bb;
938 gcov_type new_count = e ? e->count : 0;
628f6a4e 939 edge_iterator ei;
6de9cd9a
DN
940
941 if (!cfg_hooks->duplicate_block)
ab532386 942 internal_error ("%s does not support duplicate_block",
6de9cd9a
DN
943 cfg_hooks->name);
944
945 if (bb->count < new_count)
946 new_count = bb->count;
e376fe58 947
77a74ed7 948 gcc_checking_assert (can_duplicate_block_p (bb));
6de9cd9a
DN
949
950 new_bb = cfg_hooks->duplicate_block (bb);
b9a66240
ZD
951 if (after)
952 move_block_after (new_bb, after);
6de9cd9a
DN
953
954 new_bb->loop_depth = bb->loop_depth;
955 new_bb->flags = bb->flags;
628f6a4e 956 FOR_EACH_EDGE (s, ei, bb->succs)
6de9cd9a
DN
957 {
958 /* Since we are creating edges from a new block to successors
959 of another block (which therefore are known to be disjoint), there
960 is no need to actually check for duplicated edges. */
961 n = unchecked_make_edge (new_bb, s->dest, s->flags);
962 n->probability = s->probability;
963 if (e && bb->count)
964 {
965 /* Take care for overflows! */
966 n->count = s->count * (new_count * 10000 / bb->count) / 10000;
967 s->count -= n->count;
968 }
969 else
970 n->count = s->count;
971 n->aux = s->aux;
972 }
973
974 if (e)
975 {
976 new_bb->count = new_count;
977 bb->count -= new_count;
978
979 new_bb->frequency = EDGE_FREQUENCY (e);
980 bb->frequency -= EDGE_FREQUENCY (e);
981
982 redirect_edge_and_branch_force (e, new_bb);
983
984 if (bb->count < 0)
985 bb->count = 0;
986 if (bb->frequency < 0)
987 bb->frequency = 0;
988 }
989 else
990 {
991 new_bb->count = bb->count;
992 new_bb->frequency = bb->frequency;
993 }
994
6580ee77
JH
995 set_bb_original (new_bb, bb);
996 set_bb_copy (bb, new_bb);
6de9cd9a 997
b02b9b53
ZD
998 /* Add the new block to the copy of the loop of BB, or directly to the loop
999 of BB if the loop is not being copied. */
598ec7bd 1000 if (current_loops != NULL)
b02b9b53
ZD
1001 {
1002 struct loop *cloop = bb->loop_father;
561e8a90
ZD
1003 struct loop *copy = get_loop_copy (cloop);
1004 add_bb_to_loop (new_bb, copy ? copy : cloop);
b02b9b53 1005 }
598ec7bd 1006
6de9cd9a
DN
1007 return new_bb;
1008}
1009
1010/* Return 1 if BB ends with a call, possibly followed by some
1011 instructions that must stay with the call, 0 otherwise. */
1012
c22cacf3 1013bool
b48d0358 1014block_ends_with_call_p (basic_block bb)
6de9cd9a
DN
1015{
1016 if (!cfg_hooks->block_ends_with_call_p)
1017 internal_error ("%s does not support block_ends_with_call_p", cfg_hooks->name);
1018
1019 return (cfg_hooks->block_ends_with_call_p) (bb);
1020}
1021
1022/* Return 1 if BB ends with a conditional branch, 0 otherwise. */
1023
c22cacf3 1024bool
9678086d 1025block_ends_with_condjump_p (const_basic_block bb)
6de9cd9a
DN
1026{
1027 if (!cfg_hooks->block_ends_with_condjump_p)
1028 internal_error ("%s does not support block_ends_with_condjump_p",
1029 cfg_hooks->name);
1030
1031 return (cfg_hooks->block_ends_with_condjump_p) (bb);
1032}
1033
1034/* Add fake edges to the function exit for any non constant and non noreturn
1035 calls, volatile inline assembly in the bitmap of blocks specified by
1036 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
1037 that were split.
1038
1039 The goal is to expose cases in which entering a basic block does not imply
1040 that all subsequent instructions must be executed. */
1041
1042int
1043flow_call_edges_add (sbitmap blocks)
1044{
1045 if (!cfg_hooks->flow_call_edges_add)
c22cacf3 1046 internal_error ("%s does not support flow_call_edges_add",
6de9cd9a
DN
1047 cfg_hooks->name);
1048
1049 return (cfg_hooks->flow_call_edges_add) (blocks);
1050}
d9d4706f
KH
1051
1052/* This function is called immediately after edge E is added to the
1053 edge vector E->dest->preds. */
1054
1055void
1056execute_on_growing_pred (edge e)
1057{
1058 if (cfg_hooks->execute_on_growing_pred)
1059 cfg_hooks->execute_on_growing_pred (e);
1060}
1061
1062/* This function is called immediately before edge E is removed from
1063 the edge vector E->dest->preds. */
1064
1065void
1066execute_on_shrinking_pred (edge e)
1067{
1068 if (cfg_hooks->execute_on_shrinking_pred)
1069 cfg_hooks->execute_on_shrinking_pred (e);
1070}
1cb7dfc3 1071
c22cacf3
MS
1072/* This is used inside loop versioning when we want to insert
1073 stmts/insns on the edges, which have a different behavior
1cb7dfc3
MH
1074 in tree's and in RTL, so we made a CFG hook. */
1075void
1076lv_flush_pending_stmts (edge e)
1077{
1078 if (cfg_hooks->flush_pending_stmts)
1079 cfg_hooks->flush_pending_stmts (e);
1080}
1081
1082/* Loop versioning uses the duplicate_loop_to_header_edge to create
1083 a new version of the loop basic-blocks, the parameters here are
1084 exactly the same as in duplicate_loop_to_header_edge or
1085 tree_duplicate_loop_to_header_edge; while in tree-ssa there is
1086 additional work to maintain ssa information that's why there is
1087 a need to call the tree_duplicate_loop_to_header_edge rather
1088 than duplicate_loop_to_header_edge when we are in tree mode. */
1089bool
1090cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge e,
d73be268 1091 unsigned int ndupl,
1cb7dfc3 1092 sbitmap wont_exit, edge orig,
ee8c1b05
ZD
1093 VEC (edge, heap) **to_remove,
1094 int flags)
1cb7dfc3
MH
1095{
1096 gcc_assert (cfg_hooks->cfg_hook_duplicate_loop_to_header_edge);
d73be268 1097 return cfg_hooks->cfg_hook_duplicate_loop_to_header_edge (loop, e,
1cb7dfc3
MH
1098 ndupl, wont_exit,
1099 orig, to_remove,
ee8c1b05 1100 flags);
1cb7dfc3
MH
1101}
1102
1103/* Conditional jumps are represented differently in trees and RTL,
1104 this hook takes a basic block that is known to have a cond jump
fa10beec 1105 at its end and extracts the taken and not taken edges out of it
1cb7dfc3
MH
1106 and store it in E1 and E2 respectively. */
1107void
1108extract_cond_bb_edges (basic_block b, edge *e1, edge *e2)
1109{
1110 gcc_assert (cfg_hooks->extract_cond_bb_edges);
1111 cfg_hooks->extract_cond_bb_edges (b, e1, e2);
1112}
1113
1114/* Responsible for updating the ssa info (PHI nodes) on the
315682fb 1115 new condition basic block that guards the versioned loop. */
1cb7dfc3
MH
1116void
1117lv_adjust_loop_header_phi (basic_block first, basic_block second,
ae50c0cb 1118 basic_block new_block, edge e)
1cb7dfc3
MH
1119{
1120 if (cfg_hooks->lv_adjust_loop_header_phi)
ae50c0cb 1121 cfg_hooks->lv_adjust_loop_header_phi (first, second, new_block, e);
1cb7dfc3
MH
1122}
1123
1124/* Conditions in trees and RTL are different so we need
1125 a different handling when we add the condition to the
1126 versioning code. */
1127void
1128lv_add_condition_to_bb (basic_block first, basic_block second,
ae50c0cb 1129 basic_block new_block, void *cond)
1cb7dfc3
MH
1130{
1131 gcc_assert (cfg_hooks->lv_add_condition_to_bb);
ae50c0cb 1132 cfg_hooks->lv_add_condition_to_bb (first, second, new_block, cond);
c22cacf3 1133}