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