OSDN Git Service

PR tree-optimization/46009
[pf3gnuchains/gcc-fork.git] / gcc / sched-ebb.c
1 /* Instruction scheduling pass.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4    Free Software Foundation, Inc.
5    Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
6    and currently maintained by, Jim Wilson (wilson@cygnus.com)
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23 \f
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "diagnostic-core.h"
29 #include "toplev.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "hard-reg-set.h"
33 #include "regs.h"
34 #include "function.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
38 #include "except.h"
39 #include "toplev.h"
40 #include "recog.h"
41 #include "cfglayout.h"
42 #include "params.h"
43 #include "sched-int.h"
44 #include "target.h"
45 #include "output.h"
46
47 \f
48 #ifdef INSN_SCHEDULING
49
50 /* The number of insns to be scheduled in total.  */
51 static int rgn_n_insns;
52
53 /* The number of insns scheduled so far.  */
54 static int sched_rgn_n_insns;
55
56 /* Set of blocks, that already have their dependencies calculated.  */
57 static bitmap_head dont_calc_deps;
58
59 /* Last basic block in current ebb.  */
60 static basic_block last_bb;
61
62 /* Implementations of the sched_info functions for region scheduling.  */
63 static void init_ready_list (void);
64 static void begin_schedule_ready (rtx, rtx);
65 static int schedule_more_p (void);
66 static const char *ebb_print_insn (const_rtx, int);
67 static int rank (rtx, rtx);
68 static int ebb_contributes_to_priority (rtx, rtx);
69 static basic_block earliest_block_with_similiar_load (basic_block, rtx);
70 static void add_deps_for_risky_insns (rtx, rtx);
71 static basic_block schedule_ebb (rtx, rtx);
72 static void debug_ebb_dependencies (rtx, rtx);
73
74 static void ebb_add_remove_insn (rtx, int);
75 static void ebb_add_block (basic_block, basic_block);
76 static basic_block advance_target_bb (basic_block, rtx);
77 static void ebb_fix_recovery_cfg (int, int, int);
78
79 /* Return nonzero if there are more insns that should be scheduled.  */
80
81 static int
82 schedule_more_p (void)
83 {
84   return sched_rgn_n_insns < rgn_n_insns;
85 }
86
87 /* Print dependency information about ebb between HEAD and TAIL.  */
88 static void
89 debug_ebb_dependencies (rtx head, rtx tail)
90 {
91   fprintf (sched_dump,
92            ";;   --------------- forward dependences: ------------ \n");
93
94   fprintf (sched_dump, "\n;;   --- EBB Dependences --- from bb%d to bb%d \n",
95            BLOCK_NUM (head), BLOCK_NUM (tail));
96
97   debug_dependencies (head, tail);
98 }
99
100 /* Add all insns that are initially ready to the ready list READY.  Called
101    once before scheduling a set of insns.  */
102
103 static void
104 init_ready_list (void)
105 {
106   int n = 0;
107   rtx prev_head = current_sched_info->prev_head;
108   rtx next_tail = current_sched_info->next_tail;
109   rtx insn;
110
111   sched_rgn_n_insns = 0;
112
113   /* Print debugging information.  */
114   if (sched_verbose >= 5)
115     debug_ebb_dependencies (NEXT_INSN (prev_head), PREV_INSN (next_tail));
116
117   /* Initialize ready list with all 'ready' insns in target block.
118      Count number of insns in the target block being scheduled.  */
119   for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
120     {
121       try_ready (insn);
122       n++;
123     }
124
125   gcc_assert (n == rgn_n_insns);
126 }
127
128 /* INSN is being scheduled after LAST.  Update counters.  */
129 static void
130 begin_schedule_ready (rtx insn, rtx last)
131 {
132   sched_rgn_n_insns++;
133
134   if (BLOCK_FOR_INSN (insn) == last_bb
135       /* INSN is a jump in the last block, ...  */
136       && control_flow_insn_p (insn)
137       /* that is going to be moved over some instructions.  */
138       && last != PREV_INSN (insn))
139     {
140       edge e;
141       basic_block bb;
142
143       /* An obscure special case, where we do have partially dead
144          instruction scheduled after last control flow instruction.
145          In this case we can create new basic block.  It is
146          always exactly one basic block last in the sequence.  */
147
148       e = find_fallthru_edge (last_bb->succs);
149
150       gcc_checking_assert (!e || !(e->flags & EDGE_COMPLEX));
151
152       gcc_checking_assert (BLOCK_FOR_INSN (insn) == last_bb
153                            && !IS_SPECULATION_CHECK_P (insn)
154                            && BB_HEAD (last_bb) != insn
155                            && BB_END (last_bb) == insn);
156
157       {
158         rtx x;
159
160         x = NEXT_INSN (insn);
161         if (e)
162           gcc_checking_assert (NOTE_P (x) || LABEL_P (x));
163         else
164           gcc_checking_assert (BARRIER_P (x));
165       }
166
167       if (e)
168         {
169           bb = split_edge (e);
170           gcc_assert (NOTE_INSN_BASIC_BLOCK_P (BB_END (bb)));
171         }
172       else
173         /* Create an empty unreachable block after the INSN.  */
174         bb = create_basic_block (NEXT_INSN (insn), NULL_RTX, last_bb);
175
176       /* split_edge () creates BB before E->DEST.  Keep in mind, that
177          this operation extends scheduling region till the end of BB.
178          Hence, we need to shift NEXT_TAIL, so haifa-sched.c won't go out
179          of the scheduling region.  */
180       current_sched_info->next_tail = NEXT_INSN (BB_END (bb));
181       gcc_assert (current_sched_info->next_tail);
182
183       /* Append new basic block to the end of the ebb.  */
184       sched_init_only_bb (bb, last_bb);
185       gcc_assert (last_bb == bb);
186     }
187 }
188
189 /* Return a string that contains the insn uid and optionally anything else
190    necessary to identify this insn in an output.  It's valid to use a
191    static buffer for this.  The ALIGNED parameter should cause the string
192    to be formatted so that multiple output lines will line up nicely.  */
193
194 static const char *
195 ebb_print_insn (const_rtx insn, int aligned ATTRIBUTE_UNUSED)
196 {
197   static char tmp[80];
198
199   /* '+' before insn means it is a new cycle start.  */
200   if (GET_MODE (insn) == TImode)
201     sprintf (tmp, "+ %4d", INSN_UID (insn));
202   else
203     sprintf (tmp, "  %4d", INSN_UID (insn));
204
205   return tmp;
206 }
207
208 /* Compare priority of two insns.  Return a positive number if the second
209    insn is to be preferred for scheduling, and a negative one if the first
210    is to be preferred.  Zero if they are equally good.  */
211
212 static int
213 rank (rtx insn1, rtx insn2)
214 {
215   basic_block bb1 = BLOCK_FOR_INSN (insn1);
216   basic_block bb2 = BLOCK_FOR_INSN (insn2);
217
218   if (bb1->count > bb2->count
219       || bb1->frequency > bb2->frequency)
220     return -1;
221   if (bb1->count < bb2->count
222       || bb1->frequency < bb2->frequency)
223     return 1;
224   return 0;
225 }
226
227 /* NEXT is an instruction that depends on INSN (a backward dependence);
228    return nonzero if we should include this dependence in priority
229    calculations.  */
230
231 static int
232 ebb_contributes_to_priority (rtx next ATTRIBUTE_UNUSED,
233                              rtx insn ATTRIBUTE_UNUSED)
234 {
235   return 1;
236 }
237
238  /* INSN is a JUMP_INSN, COND_SET is the set of registers that are
239     conditionally set before INSN.  Store the set of registers that
240     must be considered as used by this jump in USED and that of
241     registers that must be considered as set in SET.  */
242
243 void
244 ebb_compute_jump_reg_dependencies (rtx insn, regset cond_set, regset used,
245                                    regset set)
246 {
247   basic_block b = BLOCK_FOR_INSN (insn);
248   edge e;
249   edge_iterator ei;
250
251   FOR_EACH_EDGE (e, ei, b->succs)
252     if (e->flags & EDGE_FALLTHRU)
253       /* The jump may be a by-product of a branch that has been merged
254          in the main codepath after being conditionalized.  Therefore
255          it may guard the fallthrough block from using a value that has
256          conditionally overwritten that of the main codepath.  So we
257          consider that it restores the value of the main codepath.  */
258       bitmap_and (set, df_get_live_in (e->dest), cond_set);
259     else
260       bitmap_ior_into (used, df_get_live_in (e->dest));
261 }
262
263 /* Used in schedule_insns to initialize current_sched_info for scheduling
264    regions (or single basic blocks).  */
265
266 static struct common_sched_info_def ebb_common_sched_info;
267
268 static struct sched_deps_info_def ebb_sched_deps_info =
269   {
270     ebb_compute_jump_reg_dependencies,
271     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
272     NULL,
273     1, 0, 0
274   };
275
276 static struct haifa_sched_info ebb_sched_info =
277 {
278   init_ready_list,
279   NULL,
280   schedule_more_p,
281   NULL,
282   rank,
283   ebb_print_insn,
284   ebb_contributes_to_priority,
285   NULL, /* insn_finishes_block_p */
286
287   NULL, NULL,
288   NULL, NULL,
289   1, 0,
290
291   ebb_add_remove_insn,
292   begin_schedule_ready,
293   advance_target_bb,
294   SCHED_EBB
295   /* We can create new blocks in begin_schedule_ready ().  */
296   | NEW_BBS
297 };
298 \f
299 /* Returns the earliest block in EBB currently being processed where a
300    "similar load" 'insn2' is found, and hence LOAD_INSN can move
301    speculatively into the found block.  All the following must hold:
302
303    (1) both loads have 1 base register (PFREE_CANDIDATEs).
304    (2) load_insn and load2 have a def-use dependence upon
305    the same insn 'insn1'.
306
307    From all these we can conclude that the two loads access memory
308    addresses that differ at most by a constant, and hence if moving
309    load_insn would cause an exception, it would have been caused by
310    load2 anyhow.
311
312    The function uses list (given by LAST_BLOCK) of already processed
313    blocks in EBB.  The list is formed in `add_deps_for_risky_insns'.  */
314
315 static basic_block
316 earliest_block_with_similiar_load (basic_block last_block, rtx load_insn)
317 {
318   sd_iterator_def back_sd_it;
319   dep_t back_dep;
320   basic_block bb, earliest_block = NULL;
321
322   FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
323     {
324       rtx insn1 = DEP_PRO (back_dep);
325
326       if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
327         /* Found a DEF-USE dependence (insn1, load_insn).  */
328         {
329           sd_iterator_def fore_sd_it;
330           dep_t fore_dep;
331
332           FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
333             {
334               rtx insn2 = DEP_CON (fore_dep);
335               basic_block insn2_block = BLOCK_FOR_INSN (insn2);
336
337               if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
338                 {
339                   if (earliest_block != NULL
340                       && earliest_block->index < insn2_block->index)
341                     continue;
342
343                   /* Found a DEF-USE dependence (insn1, insn2).  */
344                   if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
345                     /* insn2 not guaranteed to be a 1 base reg load.  */
346                     continue;
347
348                   for (bb = last_block; bb; bb = (basic_block) bb->aux)
349                     if (insn2_block == bb)
350                       break;
351
352                   if (!bb)
353                     /* insn2 is the similar load.  */
354                     earliest_block = insn2_block;
355                 }
356             }
357         }
358     }
359
360   return earliest_block;
361 }
362
363 /* The following function adds dependencies between jumps and risky
364    insns in given ebb.  */
365
366 static void
367 add_deps_for_risky_insns (rtx head, rtx tail)
368 {
369   rtx insn, prev;
370   int classification;
371   rtx last_jump = NULL_RTX;
372   rtx next_tail = NEXT_INSN (tail);
373   basic_block last_block = NULL, bb;
374
375   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
376     if (control_flow_insn_p (insn))
377       {
378         bb = BLOCK_FOR_INSN (insn);
379         bb->aux = last_block;
380         last_block = bb;
381         last_jump = insn;
382       }
383     else if (INSN_P (insn) && last_jump != NULL_RTX)
384       {
385         classification = haifa_classify_insn (insn);
386         prev = last_jump;
387         switch (classification)
388           {
389           case PFREE_CANDIDATE:
390             if (flag_schedule_speculative_load)
391               {
392                 bb = earliest_block_with_similiar_load (last_block, insn);
393                 if (bb)
394                   {
395                     bb = (basic_block) bb->aux;
396                     if (!bb)
397                       break;
398                     prev = BB_END (bb);
399                   }
400               }
401             /* Fall through.  */
402           case TRAP_RISKY:
403           case IRISKY:
404           case PRISKY_CANDIDATE:
405             /* ??? We could implement better checking PRISKY_CANDIDATEs
406                analogous to sched-rgn.c.  */
407             /* We can not change the mode of the backward
408                dependency because REG_DEP_ANTI has the lowest
409                rank.  */
410             if (! sched_insns_conditions_mutex_p (insn, prev))
411               {
412                 dep_def _dep, *dep = &_dep;
413
414                 init_dep (dep, prev, insn, REG_DEP_ANTI);
415
416                 if (!(current_sched_info->flags & USE_DEPS_LIST))
417                   {
418                     enum DEPS_ADJUST_RESULT res;
419
420                     res = sd_add_or_update_dep (dep, false);
421
422                     /* We can't change an existing dependency with
423                        DEP_ANTI.  */
424                     gcc_assert (res != DEP_CHANGED);
425                   }
426                 else
427                   {
428                     if ((current_sched_info->flags & DO_SPECULATION)
429                         && (spec_info->mask & BEGIN_CONTROL))
430                       DEP_STATUS (dep) = set_dep_weak (DEP_ANTI, BEGIN_CONTROL,
431                                                        MAX_DEP_WEAK);
432
433                     sd_add_or_update_dep (dep, false);
434
435                     /* Dep_status could have been changed.
436                        No assertion here.  */
437                   }
438               }
439
440             break;
441
442           default:
443             break;
444           }
445       }
446   /* Maintain the invariant that bb->aux is clear after use.  */
447   while (last_block)
448     {
449       bb = (basic_block) last_block->aux;
450       last_block->aux = NULL;
451       last_block = bb;
452     }
453 }
454
455 /* Schedule a single extended basic block, defined by the boundaries HEAD
456    and TAIL.  */
457
458 static basic_block
459 schedule_ebb (rtx head, rtx tail)
460 {
461   basic_block first_bb, target_bb;
462   struct deps_desc tmp_deps;
463
464   first_bb = BLOCK_FOR_INSN (head);
465   last_bb = BLOCK_FOR_INSN (tail);
466
467   if (no_real_insns_p (head, tail))
468     return BLOCK_FOR_INSN (tail);
469
470   gcc_assert (INSN_P (head) && INSN_P (tail));
471
472   if (!bitmap_bit_p (&dont_calc_deps, first_bb->index))
473     {
474       init_deps_global ();
475
476       /* Compute dependencies.  */
477       init_deps (&tmp_deps, false);
478       sched_analyze (&tmp_deps, head, tail);
479       free_deps (&tmp_deps);
480
481       add_deps_for_risky_insns (head, tail);
482
483       if (targetm.sched.dependencies_evaluation_hook)
484         targetm.sched.dependencies_evaluation_hook (head, tail);
485
486       finish_deps_global ();
487     }
488   else
489     /* Only recovery blocks can have their dependencies already calculated,
490        and they always are single block ebbs.  */
491     gcc_assert (first_bb == last_bb);
492
493   /* Set priorities.  */
494   current_sched_info->sched_max_insns_priority = 0;
495   rgn_n_insns = set_priorities (head, tail);
496   current_sched_info->sched_max_insns_priority++;
497
498   current_sched_info->prev_head = PREV_INSN (head);
499   current_sched_info->next_tail = NEXT_INSN (tail);
500
501   remove_notes (head, tail);
502
503   unlink_bb_notes (first_bb, last_bb);
504
505   target_bb = first_bb;
506
507   /* Make ready list big enough to hold all the instructions from the ebb.  */
508   sched_extend_ready_list (rgn_n_insns);
509   schedule_block (&target_bb);
510   /* Free ready list.  */
511   sched_finish_ready_list ();
512
513   /* We might pack all instructions into fewer blocks,
514      so we may made some of them empty.  Can't assert (b == last_bb).  */
515
516   /* Sanity check: verify that all region insns were scheduled.  */
517   gcc_assert (sched_rgn_n_insns == rgn_n_insns);
518
519   /* Free dependencies.  */
520   sched_free_deps (current_sched_info->head, current_sched_info->tail, true);
521
522   gcc_assert (haifa_recovery_bb_ever_added_p
523               || deps_pools_are_empty_p ());
524
525   if (EDGE_COUNT (last_bb->preds) == 0)
526     /* LAST_BB is unreachable.  */
527     {
528       gcc_assert (first_bb != last_bb
529                   && EDGE_COUNT (last_bb->succs) == 0);
530       last_bb = last_bb->prev_bb;
531       delete_basic_block (last_bb->next_bb);
532     }
533
534   return last_bb;
535 }
536
537 /* The one entry point in this file.  */
538
539 void
540 schedule_ebbs (void)
541 {
542   basic_block bb;
543   int probability_cutoff;
544   rtx tail;
545
546   if (profile_info && flag_branch_probabilities)
547     probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
548   else
549     probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
550   probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
551
552   /* Taking care of this degenerate case makes the rest of
553      this code simpler.  */
554   if (n_basic_blocks == NUM_FIXED_BLOCKS)
555     return;
556
557   /* Setup infos.  */
558   {
559     memcpy (&ebb_common_sched_info, &haifa_common_sched_info,
560             sizeof (ebb_common_sched_info));
561
562     ebb_common_sched_info.fix_recovery_cfg = ebb_fix_recovery_cfg;
563     ebb_common_sched_info.add_block = ebb_add_block;
564     ebb_common_sched_info.sched_pass_id = SCHED_EBB_PASS;
565
566     common_sched_info = &ebb_common_sched_info;
567     sched_deps_info = &ebb_sched_deps_info;
568     current_sched_info = &ebb_sched_info;
569   }
570
571   haifa_sched_init ();
572
573   compute_bb_for_insn ();
574
575   /* Initialize DONT_CALC_DEPS and ebb-{start, end} markers.  */
576   bitmap_initialize (&dont_calc_deps, 0);
577   bitmap_clear (&dont_calc_deps);
578
579   /* Schedule every region in the subroutine.  */
580   FOR_EACH_BB (bb)
581     {
582       rtx head = BB_HEAD (bb);
583
584       for (;;)
585         {
586           edge e;
587           tail = BB_END (bb);
588           if (bb->next_bb == EXIT_BLOCK_PTR
589               || LABEL_P (BB_HEAD (bb->next_bb)))
590             break;
591           e = find_fallthru_edge (bb->succs);
592           if (! e)
593             break;
594           if (e->probability <= probability_cutoff)
595             break;
596           bb = bb->next_bb;
597         }
598
599       /* Blah.  We should fix the rest of the code not to get confused by
600          a note or two.  */
601       while (head != tail)
602         {
603           if (NOTE_P (head) || BOUNDARY_DEBUG_INSN_P (head))
604             head = NEXT_INSN (head);
605           else if (NOTE_P (tail) || BOUNDARY_DEBUG_INSN_P (tail))
606             tail = PREV_INSN (tail);
607           else if (LABEL_P (head))
608             head = NEXT_INSN (head);
609           else
610             break;
611         }
612
613       bb = schedule_ebb (head, tail);
614     }
615   bitmap_clear (&dont_calc_deps);
616
617   /* Reposition the prologue and epilogue notes in case we moved the
618      prologue/epilogue insns.  */
619   if (reload_completed)
620     reposition_prologue_and_epilogue_notes ();
621
622   haifa_sched_finish ();
623 }
624
625 /* INSN has been added to/removed from current ebb.  */
626 static void
627 ebb_add_remove_insn (rtx insn ATTRIBUTE_UNUSED, int remove_p)
628 {
629   if (!remove_p)
630     rgn_n_insns++;
631   else
632     rgn_n_insns--;
633 }
634
635 /* BB was added to ebb after AFTER.  */
636 static void
637 ebb_add_block (basic_block bb, basic_block after)
638 {
639   /* Recovery blocks are always bounded by BARRIERS,
640      therefore, they always form single block EBB,
641      therefore, we can use rec->index to identify such EBBs.  */
642   if (after == EXIT_BLOCK_PTR)
643     bitmap_set_bit (&dont_calc_deps, bb->index);
644   else if (after == last_bb)
645     last_bb = bb;
646 }
647
648 /* Return next block in ebb chain.  For parameter meaning please refer to
649    sched-int.h: struct sched_info: advance_target_bb.  */
650 static basic_block
651 advance_target_bb (basic_block bb, rtx insn)
652 {
653   if (insn)
654     {
655       if (BLOCK_FOR_INSN (insn) != bb
656           && control_flow_insn_p (insn)
657           /* We handle interblock movement of the speculation check
658              or over a speculation check in
659              haifa-sched.c: move_block_after_check ().  */
660           && !IS_SPECULATION_BRANCHY_CHECK_P (insn)
661           && !IS_SPECULATION_BRANCHY_CHECK_P (BB_END (bb)))
662         {
663           /* Assert that we don't move jumps across blocks.  */
664           gcc_assert (!control_flow_insn_p (BB_END (bb))
665                       && NOTE_INSN_BASIC_BLOCK_P (BB_HEAD (bb->next_bb)));
666           return bb;
667         }
668       else
669         return 0;
670     }
671   else
672     /* Return next non empty block.  */
673     {
674       do
675         {
676           gcc_assert (bb != last_bb);
677
678           bb = bb->next_bb;
679         }
680       while (bb_note (bb) == BB_END (bb));
681
682       return bb;
683     }
684 }
685
686 /* Fix internal data after interblock movement of jump instruction.
687    For parameter meaning please refer to
688    sched-int.h: struct sched_info: fix_recovery_cfg.  */
689 static void
690 ebb_fix_recovery_cfg (int bbi ATTRIBUTE_UNUSED, int jump_bbi,
691                       int jump_bb_nexti)
692 {
693   gcc_assert (last_bb->index != bbi);
694
695   if (jump_bb_nexti == last_bb->index)
696     last_bb = BASIC_BLOCK (jump_bbi);
697 }
698
699 #endif /* INSN_SCHEDULING */