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