OSDN Git Service

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