Update change log
[platform/upstream/gcc48.git] / gcc / cfgbuild.c
1 /* Control flow graph building code for GNU compiler.
2    Copyright (C) 1987-2013 Free Software Foundation, Inc.
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
8 Software Foundation; either version 3, or (at your option) any later
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
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 \f
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "function.h"
32 #include "except.h"
33 #include "expr.h"
34 #include "diagnostic-core.h"
35 #include "timevar.h"
36 #include "sbitmap.h"
37
38 static void make_edges (basic_block, basic_block, int);
39 static void make_label_edge (sbitmap, basic_block, rtx, int);
40 static void find_bb_boundaries (basic_block);
41 static void compute_outgoing_frequencies (basic_block);
42 \f
43 /* Return true if insn is something that should be contained inside basic
44    block.  */
45
46 bool
47 inside_basic_block_p (const_rtx insn)
48 {
49   switch (GET_CODE (insn))
50     {
51     case CODE_LABEL:
52       /* Avoid creating of basic block for jumptables.  */
53       return (NEXT_INSN (insn) == 0
54               || !JUMP_P (NEXT_INSN (insn))
55               || (GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_VEC
56                   && GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_DIFF_VEC));
57
58     case JUMP_INSN:
59       return (GET_CODE (PATTERN (insn)) != ADDR_VEC
60               && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
61
62     case CALL_INSN:
63     case INSN:
64     case DEBUG_INSN:
65       return true;
66
67     case BARRIER:
68     case NOTE:
69       return false;
70
71     default:
72       gcc_unreachable ();
73     }
74 }
75
76 /* Return true if INSN may cause control flow transfer, so it should be last in
77    the basic block.  */
78
79 bool
80 control_flow_insn_p (const_rtx insn)
81 {
82   switch (GET_CODE (insn))
83     {
84     case NOTE:
85     case CODE_LABEL:
86     case DEBUG_INSN:
87       return false;
88
89     case JUMP_INSN:
90       /* Jump insn always causes control transfer except for tablejumps.  */
91       return (GET_CODE (PATTERN (insn)) != ADDR_VEC
92               && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
93
94     case CALL_INSN:
95       /* Noreturn and sibling call instructions terminate the basic blocks
96          (but only if they happen unconditionally).  */
97       if ((SIBLING_CALL_P (insn)
98            || find_reg_note (insn, REG_NORETURN, 0))
99           && GET_CODE (PATTERN (insn)) != COND_EXEC)
100         return true;
101
102       /* Call insn may return to the nonlocal goto handler.  */
103       if (can_nonlocal_goto (insn))
104         return true;
105       break;
106
107     case INSN:
108       /* Treat trap instructions like noreturn calls (same provision).  */
109       if (GET_CODE (PATTERN (insn)) == TRAP_IF
110           && XEXP (PATTERN (insn), 0) == const1_rtx)
111         return true;
112       if (!cfun->can_throw_non_call_exceptions)
113         return false;
114       break;
115
116     case BARRIER:
117       /* It is nonsense to reach barrier when looking for the
118          end of basic block, but before dead code is eliminated
119          this may happen.  */
120       return false;
121
122     default:
123       gcc_unreachable ();
124     }
125
126   return can_throw_internal (insn);
127 }
128
129 \f
130 /* Create an edge between two basic blocks.  FLAGS are auxiliary information
131    about the edge that is accumulated between calls.  */
132
133 /* Create an edge from a basic block to a label.  */
134
135 static void
136 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
137 {
138   gcc_assert (LABEL_P (label));
139
140   /* If the label was never emitted, this insn is junk, but avoid a
141      crash trying to refer to BLOCK_FOR_INSN (label).  This can happen
142      as a result of a syntax error and a diagnostic has already been
143      printed.  */
144
145   if (INSN_UID (label) == 0)
146     return;
147
148   cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
149 }
150
151 /* Create the edges generated by INSN in REGION.  */
152
153 void
154 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
155 {
156   eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
157
158   if (lp)
159     {
160       rtx label = lp->landing_pad;
161
162       /* During initial rtl generation, use the post_landing_pad.  */
163       if (label == NULL)
164         {
165           gcc_assert (lp->post_landing_pad);
166           label = label_rtx (lp->post_landing_pad);
167         }
168
169       make_label_edge (edge_cache, src, label,
170                        EDGE_ABNORMAL | EDGE_EH
171                        | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
172     }
173 }
174
175 /* States of basic block as seen by find_many_sub_basic_blocks.  */
176 enum state {
177   /* Basic blocks created via split_block belong to this state.
178      make_edges will examine these basic blocks to see if we need to
179      create edges going out of them.  */
180   BLOCK_NEW = 0,
181
182   /* Basic blocks that do not need examining belong to this state.
183      These blocks will be left intact.  In particular, make_edges will
184      not create edges going out of these basic blocks.  */
185   BLOCK_ORIGINAL,
186
187   /* Basic blocks that may need splitting (due to a label appearing in
188      the middle, etc) belong to this state.  After splitting them,
189      make_edges will create edges going out of them as needed.  */
190   BLOCK_TO_SPLIT
191 };
192
193 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
194 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
195
196 /* Used internally by purge_dead_tablejump_edges, ORed into state.  */
197 #define BLOCK_USED_BY_TABLEJUMP         32
198 #define FULL_STATE(BB) ((size_t) (BB)->aux)
199
200 /* Identify the edges going out of basic blocks between MIN and MAX,
201    inclusive, that have their states set to BLOCK_NEW or
202    BLOCK_TO_SPLIT.
203
204    UPDATE_P should be nonzero if we are updating CFG and zero if we
205    are building CFG from scratch.  */
206
207 static void
208 make_edges (basic_block min, basic_block max, int update_p)
209 {
210   basic_block bb;
211   sbitmap edge_cache = NULL;
212
213   /* Heavy use of computed goto in machine-generated code can lead to
214      nearly fully-connected CFGs.  In that case we spend a significant
215      amount of time searching the edge lists for duplicates.  */
216   if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
217     edge_cache = sbitmap_alloc (last_basic_block);
218
219   /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
220      is always the entry.  */
221   if (min == ENTRY_BLOCK_PTR->next_bb)
222     make_edge (ENTRY_BLOCK_PTR, min, EDGE_FALLTHRU);
223
224   FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
225     {
226       rtx insn, x;
227       enum rtx_code code;
228       edge e;
229       edge_iterator ei;
230
231       if (STATE (bb) == BLOCK_ORIGINAL)
232         continue;
233
234       /* If we have an edge cache, cache edges going out of BB.  */
235       if (edge_cache)
236         {
237           bitmap_clear (edge_cache);
238           if (update_p)
239             {
240               FOR_EACH_EDGE (e, ei, bb->succs)
241                 if (e->dest != EXIT_BLOCK_PTR)
242                   bitmap_set_bit (edge_cache, e->dest->index);
243             }
244         }
245
246       if (LABEL_P (BB_HEAD (bb))
247           && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
248         cached_make_edge (NULL, ENTRY_BLOCK_PTR, bb, 0);
249
250       /* Examine the last instruction of the block, and discover the
251          ways we can leave the block.  */
252
253       insn = BB_END (bb);
254       code = GET_CODE (insn);
255
256       /* A branch.  */
257       if (code == JUMP_INSN)
258         {
259           rtx tmp;
260
261           /* Recognize a non-local goto as a branch outside the
262              current function.  */
263           if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
264             ;
265
266           /* Recognize a tablejump and do the right thing.  */
267           else if (tablejump_p (insn, NULL, &tmp))
268             {
269               rtvec vec;
270               int j;
271
272               if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
273                 vec = XVEC (PATTERN (tmp), 0);
274               else
275                 vec = XVEC (PATTERN (tmp), 1);
276
277               for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
278                 make_label_edge (edge_cache, bb,
279                                  XEXP (RTVEC_ELT (vec, j), 0), 0);
280
281               /* Some targets (eg, ARM) emit a conditional jump that also
282                  contains the out-of-range target.  Scan for these and
283                  add an edge if necessary.  */
284               if ((tmp = single_set (insn)) != NULL
285                   && SET_DEST (tmp) == pc_rtx
286                   && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
287                   && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
288                 make_label_edge (edge_cache, bb,
289                                  XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
290             }
291
292           /* If this is a computed jump, then mark it as reaching
293              everything on the forced_labels list.  */
294           else if (computed_jump_p (insn))
295             {
296               for (x = forced_labels; x; x = XEXP (x, 1))
297                 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
298             }
299
300           /* Returns create an exit out.  */
301           else if (returnjump_p (insn))
302             cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
303
304           /* Recognize asm goto and do the right thing.  */
305           else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
306             {
307               int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
308               for (i = 0; i < n; ++i)
309                 make_label_edge (edge_cache, bb,
310                                  XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
311             }
312
313           /* Otherwise, we have a plain conditional or unconditional jump.  */
314           else
315             {
316               gcc_assert (JUMP_LABEL (insn));
317               make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
318             }
319         }
320
321       /* If this is a sibling call insn, then this is in effect a combined call
322          and return, and so we need an edge to the exit block.  No need to
323          worry about EH edges, since we wouldn't have created the sibling call
324          in the first place.  */
325       if (code == CALL_INSN && SIBLING_CALL_P (insn))
326         cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
327                           EDGE_SIBCALL | EDGE_ABNORMAL);
328
329       /* If this is a CALL_INSN, then mark it as reaching the active EH
330          handler for this CALL_INSN.  If we're handling non-call
331          exceptions then any insn can reach any of the active handlers.
332          Also mark the CALL_INSN as reaching any nonlocal goto handler.  */
333       else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
334         {
335           /* Add any appropriate EH edges.  */
336           rtl_make_eh_edge (edge_cache, bb, insn);
337
338           if (code == CALL_INSN)
339             {
340               if (can_nonlocal_goto (insn))
341                 {
342                   /* ??? This could be made smarter: in some cases it's
343                      possible to tell that certain calls will not do a
344                      nonlocal goto.  For example, if the nested functions
345                      that do the nonlocal gotos do not have their addresses
346                      taken, then only calls to those functions or to other
347                      nested functions that use them could possibly do
348                      nonlocal gotos.  */
349                   for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
350                     make_label_edge (edge_cache, bb, XEXP (x, 0),
351                                      EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
352                 }
353
354               if (flag_tm)
355                 {
356                   rtx note;
357                   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
358                     if (REG_NOTE_KIND (note) == REG_TM)
359                       make_label_edge (edge_cache, bb, XEXP (note, 0),
360                                        EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
361                 }
362             }
363         }
364
365       /* Find out if we can drop through to the next block.  */
366       insn = NEXT_INSN (insn);
367       e = find_edge (bb, EXIT_BLOCK_PTR);
368       if (e && e->flags & EDGE_FALLTHRU)
369         insn = NULL;
370
371       while (insn
372              && NOTE_P (insn)
373              && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
374         insn = NEXT_INSN (insn);
375
376       if (!insn)
377         cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
378       else if (bb->next_bb != EXIT_BLOCK_PTR)
379         {
380           if (insn == BB_HEAD (bb->next_bb))
381             cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
382         }
383     }
384
385   if (edge_cache)
386     sbitmap_free (edge_cache);
387 }
388 \f
389 static void
390 mark_tablejump_edge (rtx label)
391 {
392   basic_block bb;
393
394   gcc_assert (LABEL_P (label));
395   /* See comment in make_label_edge.  */
396   if (INSN_UID (label) == 0)
397     return;
398   bb = BLOCK_FOR_INSN (label);
399   SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
400 }
401
402 static void
403 purge_dead_tablejump_edges (basic_block bb, rtx table)
404 {
405   rtx insn = BB_END (bb), tmp;
406   rtvec vec;
407   int j;
408   edge_iterator ei;
409   edge e;
410
411   if (GET_CODE (PATTERN (table)) == ADDR_VEC)
412     vec = XVEC (PATTERN (table), 0);
413   else
414     vec = XVEC (PATTERN (table), 1);
415
416   for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
417     mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
418
419   /* Some targets (eg, ARM) emit a conditional jump that also
420      contains the out-of-range target.  Scan for these and
421      add an edge if necessary.  */
422   if ((tmp = single_set (insn)) != NULL
423        && SET_DEST (tmp) == pc_rtx
424        && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
425        && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
426     mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
427
428   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
429     {
430       if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
431         SET_STATE (e->dest, FULL_STATE (e->dest)
432                             & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
433       else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
434         {
435           remove_edge (e);
436           continue;
437         }
438       ei_next (&ei);
439     }
440 }
441
442 /* Scan basic block BB for possible BB boundaries inside the block
443    and create new basic blocks in the progress.  */
444
445 static void
446 find_bb_boundaries (basic_block bb)
447 {
448   basic_block orig_bb = bb;
449   rtx insn = BB_HEAD (bb);
450   rtx end = BB_END (bb), x;
451   rtx table;
452   rtx flow_transfer_insn = NULL_RTX;
453   edge fallthru = NULL;
454
455   if (insn == BB_END (bb))
456     return;
457
458   if (LABEL_P (insn))
459     insn = NEXT_INSN (insn);
460
461   /* Scan insn chain and try to find new basic block boundaries.  */
462   while (1)
463     {
464       enum rtx_code code = GET_CODE (insn);
465
466       /* In case we've previously seen an insn that effects a control
467          flow transfer, split the block.  */
468       if ((flow_transfer_insn || code == CODE_LABEL)
469           && inside_basic_block_p (insn))
470         {
471           fallthru = split_block (bb, PREV_INSN (insn));
472           if (flow_transfer_insn)
473             {
474               BB_END (bb) = flow_transfer_insn;
475
476               /* Clean up the bb field for the insns between the blocks.  */
477               for (x = NEXT_INSN (flow_transfer_insn);
478                    x != BB_HEAD (fallthru->dest);
479                    x = NEXT_INSN (x))
480                 if (!BARRIER_P (x))
481                   set_block_for_insn (x, NULL);
482             }
483
484           bb = fallthru->dest;
485           remove_edge (fallthru);
486           flow_transfer_insn = NULL_RTX;
487           if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
488             make_edge (ENTRY_BLOCK_PTR, bb, 0);
489         }
490       else if (code == BARRIER)
491         {
492           /* __builtin_unreachable () may cause a barrier to be emitted in
493              the middle of a BB.  We need to split it in the same manner as
494              if the barrier were preceded by a control_flow_insn_p insn.  */
495           if (!flow_transfer_insn)
496             flow_transfer_insn = prev_nonnote_insn_bb (insn);
497         }
498
499       if (control_flow_insn_p (insn))
500         flow_transfer_insn = insn;
501       if (insn == end)
502         break;
503       insn = NEXT_INSN (insn);
504     }
505
506   /* In case expander replaced normal insn by sequence terminating by
507      return and barrier, or possibly other sequence not behaving like
508      ordinary jump, we need to take care and move basic block boundary.  */
509   if (flow_transfer_insn)
510     {
511       BB_END (bb) = flow_transfer_insn;
512
513       /* Clean up the bb field for the insns that do not belong to BB.  */
514       x = flow_transfer_insn;
515       while (x != end)
516         {
517           x = NEXT_INSN (x);
518           if (!BARRIER_P (x))
519             set_block_for_insn (x, NULL);
520         }
521     }
522
523   /* We've possibly replaced the conditional jump by conditional jump
524      followed by cleanup at fallthru edge, so the outgoing edges may
525      be dead.  */
526   purge_dead_edges (bb);
527
528   /* purge_dead_edges doesn't handle tablejump's, but if we have split the
529      basic block, we might need to kill some edges.  */
530   if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
531     purge_dead_tablejump_edges (bb, table);
532 }
533
534 /*  Assume that frequency of basic block B is known.  Compute frequencies
535     and probabilities of outgoing edges.  */
536
537 static void
538 compute_outgoing_frequencies (basic_block b)
539 {
540   edge e, f;
541   edge_iterator ei;
542
543   if (EDGE_COUNT (b->succs) == 2)
544     {
545       rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
546       int probability;
547
548       if (note)
549         {
550           probability = INTVAL (XEXP (note, 0));
551           e = BRANCH_EDGE (b);
552           e->probability = probability;
553           e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
554                       / REG_BR_PROB_BASE);
555           f = FALLTHRU_EDGE (b);
556           f->probability = REG_BR_PROB_BASE - probability;
557           f->count = b->count - e->count;
558           return;
559         }
560       else
561         {
562           guess_outgoing_edge_probabilities (b);
563         }
564     }
565   else if (single_succ_p (b))
566     {
567       e = single_succ_edge (b);
568       e->probability = REG_BR_PROB_BASE;
569       e->count = b->count;
570       return;
571     }
572   else
573     {
574       /* We rely on BBs with more than two successors to have sane probabilities
575          and do not guess them here. For BBs terminated by switch statements
576          expanded to jump-table jump, we have done the right thing during
577          expansion. For EH edges, we still guess the probabilities here.  */
578       bool complex_edge = false;
579       FOR_EACH_EDGE (e, ei, b->succs)
580         if (e->flags & EDGE_COMPLEX)
581           {
582             complex_edge = true;
583             break;
584           }
585       if (complex_edge)
586         guess_outgoing_edge_probabilities (b);
587     }
588
589   if (b->count)
590     FOR_EACH_EDGE (e, ei, b->succs)
591       e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2)
592                   / REG_BR_PROB_BASE);
593 }
594
595 /* Assume that some pass has inserted labels or control flow
596    instructions within a basic block.  Split basic blocks as needed
597    and create edges.  */
598
599 void
600 find_many_sub_basic_blocks (sbitmap blocks)
601 {
602   basic_block bb, min, max;
603
604   FOR_EACH_BB (bb)
605     SET_STATE (bb,
606                bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
607
608   FOR_EACH_BB (bb)
609     if (STATE (bb) == BLOCK_TO_SPLIT)
610       find_bb_boundaries (bb);
611
612   FOR_EACH_BB (bb)
613     if (STATE (bb) != BLOCK_ORIGINAL)
614       break;
615
616   min = max = bb;
617   for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
618     if (STATE (bb) != BLOCK_ORIGINAL)
619       max = bb;
620
621   /* Now re-scan and wire in all edges.  This expect simple (conditional)
622      jumps at the end of each new basic blocks.  */
623   make_edges (min, max, 1);
624
625   /* Update branch probabilities.  Expect only (un)conditional jumps
626      to be created with only the forward edges.  */
627   if (profile_status != PROFILE_ABSENT)
628     FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
629       {
630         edge e;
631         edge_iterator ei;
632
633         if (STATE (bb) == BLOCK_ORIGINAL)
634           continue;
635         if (STATE (bb) == BLOCK_NEW)
636           {
637             bb->count = 0;
638             bb->frequency = 0;
639             FOR_EACH_EDGE (e, ei, bb->preds)
640               {
641                 bb->count += e->count;
642                 bb->frequency += EDGE_FREQUENCY (e);
643               }
644           }
645
646         compute_outgoing_frequencies (bb);
647       }
648
649   FOR_EACH_BB (bb)
650     SET_STATE (bb, 0);
651 }