sched-int.h revision 119256
190075Sobrien/* Instruction scheduling pass.  This file contains definitions used
290075Sobrien   internally in the scheduler.
390075Sobrien   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
490075Sobrien   1999, 2000, 2001 Free Software Foundation, Inc.
590075Sobrien
690075SobrienThis file is part of GCC.
790075Sobrien
890075SobrienGCC is free software; you can redistribute it and/or modify it under
990075Sobrienthe terms of the GNU General Public License as published by the Free
1090075SobrienSoftware Foundation; either version 2, or (at your option) any later
1190075Sobrienversion.
1290075Sobrien
1390075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1490075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1590075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1690075Sobrienfor more details.
1790075Sobrien
1890075SobrienYou should have received a copy of the GNU General Public License
1990075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
2090075SobrienSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA
2190075Sobrien02111-1307, USA.  */
2290075Sobrien
23117395Skan/* Pointer to data describing the current DFA state.  */
24117395Skanextern state_t curr_state;
25117395Skan
2690075Sobrien/* Forward declaration.  */
2790075Sobrienstruct ready_list;
2890075Sobrien
2990075Sobrien/* Describe state of dependencies used during sched_analyze phase.  */
3090075Sobrienstruct deps
3190075Sobrien{
3290075Sobrien  /* The *_insns and *_mems are paired lists.  Each pending memory operation
3390075Sobrien     will have a pointer to the MEM rtx on one list and a pointer to the
3490075Sobrien     containing insn on the other list in the same place in the list.  */
3590075Sobrien
3690075Sobrien  /* We can't use add_dependence like the old code did, because a single insn
3790075Sobrien     may have multiple memory accesses, and hence needs to be on the list
3890075Sobrien     once for each memory access.  Add_dependence won't let you add an insn
3990075Sobrien     to a list more than once.  */
4090075Sobrien
4190075Sobrien  /* An INSN_LIST containing all insns with pending read operations.  */
4290075Sobrien  rtx pending_read_insns;
4390075Sobrien
4490075Sobrien  /* An EXPR_LIST containing all MEM rtx's which are pending reads.  */
4590075Sobrien  rtx pending_read_mems;
4690075Sobrien
4790075Sobrien  /* An INSN_LIST containing all insns with pending write operations.  */
4890075Sobrien  rtx pending_write_insns;
4990075Sobrien
5090075Sobrien  /* An EXPR_LIST containing all MEM rtx's which are pending writes.  */
5190075Sobrien  rtx pending_write_mems;
5290075Sobrien
5390075Sobrien  /* Indicates the combined length of the two pending lists.  We must prevent
5490075Sobrien     these lists from ever growing too large since the number of dependencies
5590075Sobrien     produced is at least O(N*N), and execution time is at least O(4*N*N), as
5690075Sobrien     a function of the length of these pending lists.  */
5790075Sobrien  int pending_lists_length;
5890075Sobrien
5990075Sobrien  /* Length of the pending memory flush list. Large functions with no
6090075Sobrien     calls may build up extremely large lists.  */
6190075Sobrien  int pending_flush_length;
6290075Sobrien
6390075Sobrien  /* The last insn upon which all memory references must depend.
6490075Sobrien     This is an insn which flushed the pending lists, creating a dependency
6590075Sobrien     between it and all previously pending memory references.  This creates
6690075Sobrien     a barrier (or a checkpoint) which no memory reference is allowed to cross.
6790075Sobrien
6890075Sobrien     This includes all non constant CALL_INSNs.  When we do interprocedural
6990075Sobrien     alias analysis, this restriction can be relaxed.
7090075Sobrien     This may also be an INSN that writes memory if the pending lists grow
7190075Sobrien     too large.  */
7290075Sobrien  rtx last_pending_memory_flush;
7390075Sobrien
7490075Sobrien  /* A list of the last function calls we have seen.  We use a list to
7590075Sobrien     represent last function calls from multiple predecessor blocks.
7690075Sobrien     Used to prevent register lifetimes from expanding unnecessarily.  */
7790075Sobrien  rtx last_function_call;
7890075Sobrien
7990075Sobrien  /* A list of insns which use a pseudo register that does not already
8090075Sobrien     cross a call.  We create dependencies between each of those insn
8190075Sobrien     and the next call insn, to ensure that they won't cross a call after
8290075Sobrien     scheduling is done.  */
8390075Sobrien  rtx sched_before_next_call;
8490075Sobrien
8590075Sobrien  /* Used to keep post-call psuedo/hard reg movements together with
8690075Sobrien     the call.  */
8790075Sobrien  bool in_post_call_group_p;
8890075Sobrien
89102780Skan  /* Set to the tail insn of the outermost libcall block.
90102780Skan
91102780Skan     When nonzero, we will mark each insn processed by sched_analyze_insn
92102780Skan     with SCHED_GROUP_P to ensure libcalls are scheduled as a unit.  */
93102780Skan  rtx libcall_block_tail_insn;
94102780Skan
9590075Sobrien  /* The maximum register number for the following arrays.  Before reload
9690075Sobrien     this is max_reg_num; after reload it is FIRST_PSEUDO_REGISTER.  */
9790075Sobrien  int max_reg;
9890075Sobrien
9990075Sobrien  /* Element N is the next insn that sets (hard or pseudo) register
10090075Sobrien     N within the current basic block; or zero, if there is no
10190075Sobrien     such insn.  Needed for new registers which may be introduced
10290075Sobrien     by splitting insns.  */
10390075Sobrien  struct deps_reg
10490075Sobrien    {
10590075Sobrien      rtx uses;
10690075Sobrien      rtx sets;
10790075Sobrien      rtx clobbers;
10890075Sobrien      int uses_length;
10990075Sobrien      int clobbers_length;
11090075Sobrien    } *reg_last;
11190075Sobrien
112117395Skan  /* Element N is set for each register that has any nonzero element
11390075Sobrien     in reg_last[N].{uses,sets,clobbers}.  */
11490075Sobrien  regset_head reg_last_in_use;
115119256Skan
116119256Skan  /* Element N is set for each register that is conditionally set.  */
117119256Skan  regset_head reg_conditional_sets;
11890075Sobrien};
11990075Sobrien
12090075Sobrien/* This structure holds some state of the current scheduling pass, and
12190075Sobrien   contains some function pointers that abstract out some of the non-generic
12290075Sobrien   functionality from functions such as schedule_block or schedule_insn.
12390075Sobrien   There is one global variable, current_sched_info, which points to the
12490075Sobrien   sched_info structure currently in use.  */
12590075Sobrienstruct sched_info
12690075Sobrien{
12790075Sobrien  /* Add all insns that are initially ready to the ready list.  Called once
12890075Sobrien     before scheduling a set of insns.  */
12990075Sobrien  void (*init_ready_list) PARAMS ((struct ready_list *));
13090075Sobrien  /* Called after taking an insn from the ready list.  Returns nonzero if
13190075Sobrien     this insn can be scheduled, nonzero if we should silently discard it.  */
13290075Sobrien  int (*can_schedule_ready_p) PARAMS ((rtx));
13390075Sobrien  /* Return nonzero if there are more insns that should be scheduled.  */
13490075Sobrien  int (*schedule_more_p) PARAMS ((void));
13590075Sobrien  /* Called after an insn has all its dependencies resolved.  Return nonzero
13690075Sobrien     if it should be moved to the ready list or the queue, or zero if we
13790075Sobrien     should silently discard it.  */
13890075Sobrien  int (*new_ready) PARAMS ((rtx));
13990075Sobrien  /* Compare priority of two insns.  Return a positive number if the second
14090075Sobrien     insn is to be preferred for scheduling, and a negative one if the first
14190075Sobrien     is to be preferred.  Zero if they are equally good.  */
14290075Sobrien  int (*rank) PARAMS ((rtx, rtx));
14390075Sobrien  /* Return a string that contains the insn uid and optionally anything else
14490075Sobrien     necessary to identify this insn in an output.  It's valid to use a
14590075Sobrien     static buffer for this.  The ALIGNED parameter should cause the string
14690075Sobrien     to be formatted so that multiple output lines will line up nicely.  */
14790075Sobrien  const char *(*print_insn) PARAMS ((rtx, int));
14890075Sobrien  /* Return nonzero if an insn should be included in priority
14990075Sobrien     calculations.  */
15090075Sobrien  int (*contributes_to_priority) PARAMS ((rtx, rtx));
15190075Sobrien  /* Called when computing dependencies for a JUMP_INSN.  This function
152119256Skan     should store the set of registers that must be considered as used
153119256Skan     and the set of registers that must be considered as set by the jump.  */
154119256Skan  void (*compute_jump_reg_dependencies) PARAMS ((rtx, regset, regset, regset));
15590075Sobrien
15690075Sobrien  /* The boundaries of the set of insns to be scheduled.  */
15790075Sobrien  rtx prev_head, next_tail;
15890075Sobrien
15990075Sobrien  /* Filled in after the schedule is finished; the first and last scheduled
16090075Sobrien     insns.  */
16190075Sobrien  rtx head, tail;
16290075Sobrien
16390075Sobrien  /* If nonzero, enables an additional sanity check in schedule_block.  */
16490075Sobrien  unsigned int queue_must_finish_empty:1;
16590075Sobrien  /* Nonzero if we should use cselib for better alias analysis.  This
16690075Sobrien     must be 0 if the dependency information is used after sched_analyze
16790075Sobrien     has completed, e.g. if we're using it to initialize state for successor
16890075Sobrien     blocks in region scheduling.  */
16990075Sobrien  unsigned int use_cselib:1;
17090075Sobrien};
17190075Sobrien
17290075Sobrienextern struct sched_info *current_sched_info;
17390075Sobrien
17490075Sobrien/* Indexed by INSN_UID, the collection of all data associated with
17590075Sobrien   a single instruction.  */
17690075Sobrien
17790075Sobrienstruct haifa_insn_data
17890075Sobrien{
17990075Sobrien  /* A list of insns which depend on the instruction.  Unlike LOG_LINKS,
18090075Sobrien     it represents forward dependencies.  */
18190075Sobrien  rtx depend;
18290075Sobrien
18390075Sobrien  /* The line number note in effect for each insn.  For line number
18490075Sobrien     notes, this indicates whether the note may be reused.  */
18590075Sobrien  rtx line_note;
18690075Sobrien
18790075Sobrien  /* Logical uid gives the original ordering of the insns.  */
18890075Sobrien  int luid;
18990075Sobrien
19090075Sobrien  /* A priority for each insn.  */
19190075Sobrien  int priority;
19290075Sobrien
19390075Sobrien  /* The number of incoming edges in the forward dependency graph.
19490075Sobrien     As scheduling proceds, counts are decreased.  An insn moves to
19590075Sobrien     the ready queue when its counter reaches zero.  */
19690075Sobrien  int dep_count;
19790075Sobrien
19890075Sobrien  /* An encoding of the blockage range function.  Both unit and range
199117395Skan     are coded.  This member is used only for old pipeline interface.  */
20090075Sobrien  unsigned int blockage;
20190075Sobrien
20290075Sobrien  /* Number of instructions referring to this insn.  */
20390075Sobrien  int ref_count;
20490075Sobrien
20590075Sobrien  /* The minimum clock tick at which the insn becomes ready.  This is
20690075Sobrien     used to note timing constraints for the insns in the pending list.  */
20790075Sobrien  int tick;
20890075Sobrien
20990075Sobrien  short cost;
21090075Sobrien
211117395Skan  /* An encoding of the function units used.  This member is used only
212117395Skan     for old pipeline interface.  */
21390075Sobrien  short units;
21490075Sobrien
21590075Sobrien  /* This weight is an estimation of the insn's contribution to
21690075Sobrien     register pressure.  */
21790075Sobrien  short reg_weight;
21890075Sobrien
21990075Sobrien  /* Some insns (e.g. call) are not allowed to move across blocks.  */
22090075Sobrien  unsigned int cant_move : 1;
22190075Sobrien
22290075Sobrien  /* Set if there's DEF-USE dependence between some speculatively
22390075Sobrien     moved load insn and this one.  */
22490075Sobrien  unsigned int fed_by_spec_load : 1;
22590075Sobrien  unsigned int is_load_insn : 1;
22690075Sobrien
22790075Sobrien  /* Nonzero if priority has been computed already.  */
22890075Sobrien  unsigned int priority_known : 1;
22990075Sobrien};
23090075Sobrien
23190075Sobrienextern struct haifa_insn_data *h_i_d;
23290075Sobrien
23390075Sobrien/* Accessor macros for h_i_d.  There are more in haifa-sched.c and
23490075Sobrien   sched-rgn.c.  */
23590075Sobrien#define INSN_DEPEND(INSN)	(h_i_d[INSN_UID (INSN)].depend)
23690075Sobrien#define INSN_LUID(INSN)		(h_i_d[INSN_UID (INSN)].luid)
23790075Sobrien#define CANT_MOVE(insn)		(h_i_d[INSN_UID (insn)].cant_move)
23890075Sobrien#define INSN_DEP_COUNT(INSN)	(h_i_d[INSN_UID (INSN)].dep_count)
23990075Sobrien#define INSN_PRIORITY(INSN)	(h_i_d[INSN_UID (INSN)].priority)
24090075Sobrien#define INSN_PRIORITY_KNOWN(INSN) (h_i_d[INSN_UID (INSN)].priority_known)
24190075Sobrien#define INSN_COST(INSN)		(h_i_d[INSN_UID (INSN)].cost)
24290075Sobrien#define INSN_UNIT(INSN)		(h_i_d[INSN_UID (INSN)].units)
24390075Sobrien#define INSN_REG_WEIGHT(INSN)	(h_i_d[INSN_UID (INSN)].reg_weight)
24490075Sobrien
24590075Sobrien#define INSN_BLOCKAGE(INSN)	(h_i_d[INSN_UID (INSN)].blockage)
24690075Sobrien#define UNIT_BITS		5
24790075Sobrien#define BLOCKAGE_MASK		((1 << BLOCKAGE_BITS) - 1)
24890075Sobrien#define ENCODE_BLOCKAGE(U, R)			\
24990075Sobrien  (((U) << BLOCKAGE_BITS			\
25090075Sobrien    | MIN_BLOCKAGE_COST (R)) << BLOCKAGE_BITS	\
25190075Sobrien   | MAX_BLOCKAGE_COST (R))
25290075Sobrien#define UNIT_BLOCKED(B)		((B) >> (2 * BLOCKAGE_BITS))
25390075Sobrien#define BLOCKAGE_RANGE(B)                                                \
25490075Sobrien  (((((B) >> BLOCKAGE_BITS) & BLOCKAGE_MASK) << (HOST_BITS_PER_INT / 2)) \
25590075Sobrien   | ((B) & BLOCKAGE_MASK))
25690075Sobrien
25790075Sobrien/* Encodings of the `<name>_unit_blockage_range' function.  */
25890075Sobrien#define MIN_BLOCKAGE_COST(R) ((R) >> (HOST_BITS_PER_INT / 2))
25990075Sobrien#define MAX_BLOCKAGE_COST(R) ((R) & ((1 << (HOST_BITS_PER_INT / 2)) - 1))
26090075Sobrien
26190075Sobrienextern FILE *sched_dump;
26290075Sobrienextern int sched_verbose;
26390075Sobrien
26490075Sobrien#ifndef __GNUC__
26590075Sobrien#define __inline
26690075Sobrien#endif
26790075Sobrien
26890075Sobrien#ifndef HAIFA_INLINE
26990075Sobrien#define HAIFA_INLINE __inline
27090075Sobrien#endif
27190075Sobrien
27290075Sobrien/* Functions in sched-vis.c.  */
27390075Sobrienextern void init_target_units PARAMS ((void));
27490075Sobrienextern void insn_print_units PARAMS ((rtx));
27590075Sobrienextern void init_block_visualization PARAMS ((void));
27690075Sobrienextern void print_block_visualization PARAMS ((const char *));
27790075Sobrienextern void visualize_scheduled_insns PARAMS ((int));
27890075Sobrienextern void visualize_no_unit PARAMS ((rtx));
27990075Sobrienextern void visualize_stall_cycles PARAMS ((int));
28090075Sobrienextern void visualize_alloc PARAMS ((void));
28190075Sobrienextern void visualize_free PARAMS ((void));
28290075Sobrien
28390075Sobrien/* Functions in sched-deps.c.  */
28490075Sobrienextern void add_dependence PARAMS ((rtx, rtx, enum reg_note));
28590075Sobrienextern void add_insn_mem_dependence PARAMS ((struct deps *, rtx *, rtx *, rtx,
28690075Sobrien					     rtx));
28790075Sobrienextern void sched_analyze PARAMS ((struct deps *, rtx, rtx));
28890075Sobrienextern void init_deps PARAMS ((struct deps *));
28990075Sobrienextern void free_deps PARAMS ((struct deps *));
29090075Sobrienextern void init_deps_global PARAMS ((void));
29190075Sobrienextern void finish_deps_global PARAMS ((void));
29290075Sobrienextern void compute_forward_dependences PARAMS ((rtx, rtx));
29390075Sobrienextern rtx find_insn_list PARAMS ((rtx, rtx));
29490075Sobrienextern void init_dependency_caches PARAMS ((int));
29590075Sobrienextern void free_dependency_caches PARAMS ((void));
29690075Sobrien
29790075Sobrien/* Functions in haifa-sched.c.  */
29890075Sobrienextern void get_block_head_tail PARAMS ((int, rtx *, rtx *));
29990075Sobrienextern int no_real_insns_p PARAMS ((rtx, rtx));
30090075Sobrien
30190075Sobrienextern void rm_line_notes PARAMS ((rtx, rtx));
30290075Sobrienextern void save_line_notes PARAMS ((int, rtx, rtx));
30390075Sobrienextern void restore_line_notes PARAMS ((rtx, rtx));
30490075Sobrienextern void rm_redundant_line_notes PARAMS ((void));
30590075Sobrienextern void rm_other_notes PARAMS ((rtx, rtx));
30690075Sobrien
30790075Sobrienextern int insn_issue_delay PARAMS ((rtx));
30890075Sobrienextern int set_priorities PARAMS ((rtx, rtx));
30990075Sobrien
31096263Sobrienextern rtx sched_emit_insn PARAMS ((rtx));
31190075Sobrienextern void schedule_block PARAMS ((int, int));
31290075Sobrienextern void sched_init PARAMS ((FILE *));
31390075Sobrienextern void sched_finish PARAMS ((void));
31490075Sobrien
31590075Sobrienextern void ready_add PARAMS ((struct ready_list *, rtx));
31690075Sobrien
31790075Sobrien/* The following are exported for the benefit of debugging functions.  It
31890075Sobrien   would be nicer to keep them private to haifa-sched.c.  */
31990075Sobrienextern int insn_unit PARAMS ((rtx));
32090075Sobrienextern int insn_cost PARAMS ((rtx, rtx, rtx));
32190075Sobrienextern rtx get_unit_last_insn PARAMS ((int));
32290075Sobrienextern int actual_hazard_this_instance PARAMS ((int, int, rtx, int, int));
323117395Skanextern void print_insn PARAMS ((char *, rtx, int));
324