OSDN Git Service

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