loop.h 16 KB
Newer Older
Richard Stallman committed
1
/* Loop optimization definitions for GNU C-Compiler
2
   Copyright (C) 1991, 1995, 1998, 1999, 2000, 2001, 2002
3
   Free Software Foundation, Inc.
Richard Stallman committed
4

5
This file is part of GCC.
Richard Stallman committed
6

7 8 9 10
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Richard Stallman committed
11

12 13 14 15
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
Richard Stallman committed
16 17

You should have received a copy of the GNU General Public License
18 19 20
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */
Richard Stallman committed
21

22
#include "bitmap.h"
23 24 25
#include "sbitmap.h"
#include "hard-reg-set.h"
#include "basic-block.h"
26

27 28 29
/* Flags passed to loop_optimize.  */
#define LOOP_UNROLL 1
#define LOOP_BCT 2
30
#define LOOP_PREFETCH 4
31
#define LOOP_AUTO_UNROLL 8
32

33
/* Get the loop info pointer of a loop.  */
Kazu Hirata committed
34
#define LOOP_INFO(LOOP) ((struct loop_info *) (LOOP)->aux)
35

36
/* Get a pointer to the loop movables structure.  */
37
#define LOOP_MOVABLES(LOOP) (&LOOP_INFO (LOOP)->movables)
38

39
/* Get a pointer to the loop registers structure.  */
40
#define LOOP_REGS(LOOP) (&LOOP_INFO (LOOP)->regs)
41

42
/* Get a pointer to the loop induction variables structure.  */
43
#define LOOP_IVS(LOOP) (&LOOP_INFO (LOOP)->ivs)
44

Richard Stallman committed
45 46 47 48 49 50 51
/* Get the luid of an insn.  Catch the error of trying to reference the LUID
   of an insn added during loop, since these don't have LUIDs.  */

#define INSN_LUID(INSN)			\
  (INSN_UID (INSN) < max_uid_for_loop ? uid_luid[INSN_UID (INSN)] \
   : (abort (), -1))

52 53 54 55
#define REGNO_FIRST_LUID(REGNO) uid_luid[REGNO_FIRST_UID (REGNO)]
#define REGNO_LAST_LUID(REGNO) uid_luid[REGNO_LAST_UID (REGNO)]


Richard Stallman committed
56 57 58 59 60 61
/* A "basic induction variable" or biv is a pseudo reg that is set
   (within this loop) only by incrementing or decrementing it.  */
/* A "general induction variable" or giv is a pseudo reg whose
   value is a linear function of a biv.  */

/* Bivs are recognized by `basic_induction_var';
Michael Hayes committed
62
   Givs by `general_induction_var'.  */
Richard Stallman committed
63 64 65

/* An enum for the two different types of givs, those that are used
   as memory addresses and those that are calculated into registers.  */
Kazu Hirata committed
66 67 68 69 70
enum g_types
{
  DEST_ADDR,
  DEST_REG
};
Richard Stallman committed
71

Michael Hayes committed
72

Richard Stallman committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/* A `struct induction' is created for every instruction that sets
   an induction variable (either a biv or a giv).  */

struct induction
{
  rtx insn;			/* The insn that sets a biv or giv */
  rtx new_reg;			/* New register, containing strength reduced
				   version of this giv.  */
  rtx src_reg;			/* Biv from which this giv is computed.
				   (If this is a biv, then this is the biv.) */
  enum g_types giv_type;	/* Indicate whether DEST_ADDR or DEST_REG */
  rtx dest_reg;			/* Destination register for insn: this is the
				   register which was the biv or giv.
				   For a biv, this equals src_reg.
				   For a DEST_ADDR type giv, this is 0.  */
  rtx *location;		/* Place in the insn where this giv occurs.
				   If GIV_TYPE is DEST_REG, this is 0.  */
90 91
				/* For a biv, this is the place where add_val
				   was found.  */
Richard Stallman committed
92
  enum machine_mode mode;	/* The mode of this biv or giv */
93
  rtx mem;			/* For DEST_ADDR, the memory object.  */
Richard Stallman committed
94 95 96 97 98 99 100
  rtx mult_val;			/* Multiplicative factor for src_reg.  */
  rtx add_val;			/* Additive constant for that product.  */
  int benefit;			/* Gain from eliminating this insn.  */
  rtx final_value;		/* If the giv is used outside the loop, and its
				   final value could be calculated, it is put
				   here, and the giv is made replaceable.  Set
				   the giv to this value before the loop.  */
101 102 103
  unsigned combined_with;	/* The number of givs this giv has been
				   combined with.  If nonzero, this giv
				   cannot combine with any other giv.  */
Richard Stallman committed
104 105 106 107 108 109 110 111 112 113 114 115
  unsigned replaceable : 1;	/* 1 if we can substitute the strength-reduced
				   variable for the original variable.
				   0 means they must be kept separate and the
				   new one must be copied into the old pseudo
				   reg each time the old one is set.  */
  unsigned not_replaceable : 1;	/* Used to prevent duplicating work.  This is
				   1 if we know that the giv definitely can
				   not be made replaceable, in which case we
				   don't bother checking the variable again
				   even if further info is available.
				   Both this and the above can be zero.  */
  unsigned ignore : 1;		/* 1 prohibits further processing of giv */
116 117 118
  unsigned always_computable : 1;/* 1 if this value is computable every
				    iteration.  */
  unsigned always_executed : 1; /* 1 if this set occurs each iteration.  */
119 120
  unsigned maybe_multiple : 1;	/* Only used for a biv and  1 if this biv
				   update may be done multiple times per
Kazu Hirata committed
121
				   iteration.  */
Richard Stallman committed
122 123 124
  unsigned cant_derive : 1;	/* For giv's, 1 if this giv cannot derive
				   another giv.  This occurs in many cases
				   where a giv's lifetime spans an update to
Kazu Hirata committed
125
				   a biv.  */
Richard Stallman committed
126 127
  unsigned maybe_dead : 1;	/* 1 if this giv might be dead.  In that case,
				   we won't use it to eliminate a biv, it
Kazu Hirata committed
128
				   would probably lose.  */
129
  unsigned auto_inc_opt : 1;	/* 1 if this giv had its increment output next
Kazu Hirata committed
130
				   to it to try to form an auto-inc address.  */
131 132
  unsigned unrolled : 1;	/* 1 if new register has been allocated and
				   initialized in unrolled loop.  */
133
  unsigned shared : 1;
Kazu Hirata committed
134
  unsigned no_const_addval : 1; /* 1 if add_val does not contain a const.  */
Richard Stallman committed
135 136 137 138
  int lifetime;			/* Length of life of this giv */
  rtx derive_adjustment;	/* If nonzero, is an adjustment to be
				   subtracted from add_val when this giv
				   derives another.  This occurs when the
Kazu Hirata committed
139
				   giv spans a biv update by incrementation.  */
140
  rtx ext_dependent;		/* If nonzero, is a sign or zero extension
141
				   if a biv on which this giv is dependent.  */
Richard Stallman committed
142 143 144 145 146 147
  struct induction *next_iv;	/* For givs, links together all givs that are
				   based on the same biv.  For bivs, links
				   together all biv entries that refer to the
				   same biv register.  */
  struct induction *same;	/* If this giv has been combined with another
				   giv, this points to the base giv.  The base
148
				   giv will have COMBINED_WITH nonzero.  */
Charles Hannum committed
149
  HOST_WIDE_INT const_adjust;	/* Used by loop unrolling, when an address giv
Richard Stallman committed
150 151
				   is split, and a constant is eliminated from
				   the address, the -constant is stored here
Kazu Hirata committed
152
				   for later use.  */
153 154 155 156
  struct induction *same_insn;	/* If there are multiple identical givs in
				   the same insn, then all but one have this
				   field set, and they all point to the giv
				   that doesn't have this field set.  */
157
  rtx last_use;			/* For a giv made from a biv increment, this is
Kazu Hirata committed
158
				   a substitute for the lifetime information.  */
Richard Stallman committed
159 160
};

Michael Hayes committed
161

Richard Stallman committed
162 163
/* A `struct iv_class' is created for each biv.  */

Kazu Hirata committed
164 165
struct iv_class
{
166
  unsigned int regno;		/* Pseudo reg which is the biv.  */
Richard Stallman committed
167 168 169 170 171 172 173
  int biv_count;		/* Number of insns setting this reg.  */
  struct induction *biv;	/* List of all insns that set this reg.  */
  int giv_count;		/* Number of DEST_REG givs computed from this
				   biv.  The resulting count is only used in
				   check_dbra_loop.  */
  struct induction *giv;	/* List of all insns that compute a giv
				   from this reg.  */
174 175 176 177 178
  int total_benefit;		/* Sum of BENEFITs of all those givs.  */
  rtx initial_value;		/* Value of reg at loop start.  */
  rtx initial_test;		/* Test performed on BIV before loop.  */
  rtx final_value;		/* Value of reg at loop end, if known.  */
  struct iv_class *next;	/* Links all class structures together.  */
Kazu Hirata committed
179 180
  rtx init_insn;		/* insn which initializes biv, 0 if none.  */
  rtx init_set;			/* SET of INIT_INSN, if any.  */
Richard Stallman committed
181
  unsigned incremented : 1;	/* 1 if somewhere incremented/decremented */
182 183 184 185
  unsigned eliminable : 1;	/* 1 if plausible candidate for
                                   elimination.  */
  unsigned nonneg : 1;		/* 1 if we added a REG_NONNEG note for
                                   this.  */
Richard Stallman committed
186
  unsigned reversed : 1;	/* 1 if we reversed the loop that this
Kazu Hirata committed
187
				   biv controls.  */
188
  unsigned all_reduced : 1;	/* 1 if all givs using this biv have
189
                                   been reduced.  */
Richard Stallman committed
190 191
};

Michael Hayes committed
192 193 194

/* Definitions used by the basic induction variable discovery code.  */
enum iv_mode
195
{
Michael Hayes committed
196 197 198 199 200
  UNKNOWN_INDUCT,
  BASIC_INDUCT,
  NOT_BASIC_INDUCT,
  GENERAL_INDUCT
};
201

Michael Hayes committed
202 203 204 205

/* A `struct iv' is created for every register.  */

struct iv
206
{
Michael Hayes committed
207
  enum iv_mode type;
208
  union
Michael Hayes committed
209 210 211 212 213 214 215 216 217 218
  {
    struct iv_class *class;
    struct induction *info;
  } iv;
};


#define REG_IV_TYPE(ivs, n) ivs->regs[n].type
#define REG_IV_INFO(ivs, n) ivs->regs[n].iv.info
#define REG_IV_CLASS(ivs, n) ivs->regs[n].iv.class
Kazu Hirata committed
219

Michael Hayes committed
220 221 222

struct loop_ivs
{
223
  /* Indexed by register number, contains pointer to `struct
Michael Hayes committed
224 225
     iv' if register is an induction variable.  */
  struct iv *regs;
226

Michael Hayes committed
227 228
  /* Size of regs array.  */
  unsigned int n_regs;
Kazu Hirata committed
229

230 231
  /* The head of a list which links together (via the next field)
     every iv class for the current loop.  */
Michael Hayes committed
232
  struct iv_class *list;
233 234
};

Michael Hayes committed
235 236 237 238 239 240 241 242 243

typedef struct loop_mem_info
{
  rtx mem;      /* The MEM itself.  */
  rtx reg;      /* Corresponding pseudo, if any.  */
  int optimize; /* Nonzero if we can optimize access to this MEM.  */
} loop_mem_info;


244

245 246 247 248 249 250
struct loop_reg
{
  /* Number of times the reg is set during the loop being scanned.
     During code motion, a negative value indicates a reg that has
     been made a candidate; in particular -2 means that it is an
     candidate that we know is equal to a constant and -1 means that
251
     it is a candidate not known equal to a constant.  After code
252 253
     motion, regs moved have 0 (which is accurate now) while the
     failed candidates have the original number of times set.
Kazu Hirata committed
254

255 256
     Therefore, at all times, == 0 indicates an invariant register;
     < 0 a conditionally invariant one.  */
257
  int set_in_loop;
258 259 260 261

  /* Original value of set_in_loop; same except that this value
     is not set negative for a reg whose sets have been made candidates
     and not set to 0 for a reg that is moved.  */
262
  int n_times_set;
Kazu Hirata committed
263

264 265
  /* Contains the insn in which a register was used if it was used
     exactly once; contains const0_rtx if it was used more than once.  */
266 267 268 269 270
  rtx single_usage;

  /* Nonzero indicates that the register cannot be moved or strength
     reduced.  */
  char may_not_optimize;
Kazu Hirata committed
271

272 273
  /* Nonzero means reg N has already been moved out of one loop.
     This reduces the desire to move it out of another.  */
274 275 276
  char moved_once;
};

277

278 279 280 281 282 283
struct loop_regs
{
  int num;			/* Number of regs used in table.  */
  int size;			/* Size of table.  */
  struct loop_reg *array;	/* Register usage info. array.  */
  int multiple_uses;		/* Nonzero if a reg has multiple uses.  */
284 285
};

286

287

288 289 290 291 292 293 294 295 296
struct loop_movables
{
  /* Head of movable chain.  */
  struct movable *head;
  /* Last movable in chain.  */
  struct movable *last;
};


297
/* Information pertaining to a loop.  */
298 299 300

struct loop_info
{
301 302
  /* Nonzero if there is a subroutine call in the current loop.  */
  int has_call;
303 304 305 306
  /* Nonzero if there is a libcall in the current loop.  */
  int has_libcall;
  /* Nonzero if there is a non constant call in the current loop.  */
  int has_nonconst_call;
307 308
  /* Nonzero if there is a prefetch instruction in the current loop.  */
  int has_prefetch;
309 310 311 312 313 314 315 316 317 318
  /* Nonzero if there is a volatile memory reference in the current
     loop.  */
  int has_volatile;
  /* Nonzero if there is a tablejump in the current loop.  */
  int has_tablejump;
  /* Nonzero if there are ways to leave the loop other than falling
     off the end.  */
  int has_multiple_exit_targets;
  /* Nonzero if there is an indirect jump in the current function.  */
  int has_indirect_jump;
319 320 321
  /* Whether loop unrolling has emitted copies of the loop body so
     that the main loop needs no exit tests.  */
  int preconditioned;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  /* Register or constant initial loop value.  */
  rtx initial_value;
  /* Register or constant value used for comparison test.  */
  rtx comparison_value;
  /* Register or constant approximate final value.  */
  rtx final_value;
  /* Register or constant initial loop value with term common to
     final_value removed.  */
  rtx initial_equiv_value;
  /* Register or constant final loop value with term common to
     initial_value removed.  */
  rtx final_equiv_value;
  /* Register corresponding to iteration variable.  */
  rtx iteration_var;
  /* Constant loop increment.  */
  rtx increment;
  enum rtx_code comparison_code;
  /* Holds the number of loop iterations.  It is zero if the number
     could not be calculated.  Must be unsigned since the number of
     iterations can be as high as 2^wordsize - 1.  For loops with a
     wider iterator, this number will be zero if the number of loop
     iterations is too large for an unsigned integer to hold.  */
  unsigned HOST_WIDE_INT n_iterations;
345
  /* The number of times the loop body was unrolled.  */
346
  unsigned int unroll_number;
347
  int used_count_register;
348 349
  /* The loop iterator induction variable.  */
  struct iv_class *iv;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  /* List of MEMs that are stored in this loop.  */
  rtx store_mems;
  /* Array of MEMs that are used (read or written) in this loop, but
     cannot be aliased by anything in this loop, except perhaps
     themselves.  In other words, if mems[i] is altered during
     the loop, it is altered by an expression that is rtx_equal_p to
     it.  */
  loop_mem_info *mems;
  /* The index of the next available slot in MEMS.  */
  int mems_idx;
  /* The number of elements allocated in MEMS.  */
  int mems_allocated;
  /* Nonzero if we don't know what MEMs were changed in the current
     loop.  This happens if the loop contains a call (in which case
     `has_call' will also be set) or if we store into more than
     NUM_STORES MEMs.  */
  int unknown_address_altered;
  /* The above doesn't count any readonly memory locations that are
     stored.  This does.  */
  int unknown_constant_address_altered;
  /* Count of memory write instructions discovered in the loop.  */
  int num_mem_sets;
  /* The insn where the first of these was found.  */
  rtx first_loop_store_insn;
374 375
  /* The chain of movable insns in loop.  */
  struct loop_movables movables;
376 377
  /* The registers used the in loop.  */
  struct loop_regs regs;
378 379
  /* The induction variable information in loop.  */
  struct loop_ivs ivs;
380
  /* Nonzero if call is in pre_header extended basic block.  */
381
  int pre_header_has_call;
382 383
};

Richard Stallman committed
384 385 386 387 388

/* Variables declared in loop.c, but also needed in unroll.c.  */

extern int *uid_luid;
extern int max_uid_for_loop;
389
extern unsigned int max_reg_before_loop;
390
extern struct loop **uid_loop;
Richard Stallman committed
391 392
extern FILE *loop_dump_stream;

393

Richard Stallman committed
394 395
/* Forward declarations for non-static functions declared in loop.c and
   unroll.c.  */
396 397
int loop_invariant_p PARAMS ((const struct loop *, rtx));
rtx get_condition_for_loop PARAMS ((const struct loop *, rtx));
398 399
void loop_iv_add_mult_hoist PARAMS ((const struct loop *, rtx, rtx, rtx, rtx));
void loop_iv_add_mult_sink PARAMS ((const struct loop *, rtx, rtx, rtx, rtx));
400
void loop_iv_add_mult_emit_before PARAMS ((const struct loop *, rtx,
401 402
					   rtx, rtx, rtx,
					   basic_block, rtx));
403
rtx express_from PARAMS ((struct induction *, struct induction *));
404
rtx extend_value_for_giv PARAMS ((struct induction *, rtx));
405

406
void unroll_loop PARAMS ((struct loop *, int, int));
407
rtx biv_total_increment PARAMS ((const struct iv_class *));
408
unsigned HOST_WIDE_INT loop_iterations PARAMS ((struct loop *));
409
int precondition_loop_p PARAMS ((const struct loop *,
Kazu Hirata committed
410 411
				 rtx *, rtx *, rtx *,
				 enum machine_mode *mode));
412 413
rtx final_biv_value PARAMS ((const struct loop *, struct iv_class *));
rtx final_giv_value PARAMS ((const struct loop *, struct induction *));
414
void emit_unrolled_add PARAMS ((rtx, rtx, rtx));
415
int back_branch_in_range_p PARAMS ((const struct loop *, rtx));
416

417
int loop_insn_first_p PARAMS ((rtx, rtx));
418
typedef rtx (*loop_insn_callback) PARAMS ((struct loop *, rtx, int, int));
419
void for_each_insn_in_loop PARAMS ((struct loop *, loop_insn_callback));
420
rtx loop_insn_emit_before PARAMS((const struct loop *, basic_block,
421
				  rtx, rtx));
422
rtx loop_insn_sink PARAMS((const struct loop *, rtx));
423
rtx loop_insn_hoist PARAMS((const struct loop *, rtx));
424

425 426
/* Forward declarations for non-static functions declared in doloop.c.  */
int doloop_optimize PARAMS ((const struct loop *));