* diagnostic.c (announce_function): Move to toplev.c.
[official-gcc.git] / gcc / loop.c
blob4a987d50337cfe65b15a19afd770682440b0702d
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "rtl.h"
42 #include "tm_p.h"
43 #include "function.h"
44 #include "expr.h"
45 #include "hard-reg-set.h"
46 #include "basic-block.h"
47 #include "insn-config.h"
48 #include "regs.h"
49 #include "recog.h"
50 #include "flags.h"
51 #include "real.h"
52 #include "loop.h"
53 #include "cselib.h"
54 #include "except.h"
55 #include "toplev.h"
56 #include "predict.h"
57 #include "insn-flags.h"
58 #include "optabs.h"
59 #include "cfgloop.h"
61 /* Not really meaningful values, but at least something. */
62 #ifndef SIMULTANEOUS_PREFETCHES
63 #define SIMULTANEOUS_PREFETCHES 3
64 #endif
65 #ifndef PREFETCH_BLOCK
66 #define PREFETCH_BLOCK 32
67 #endif
68 #ifndef HAVE_prefetch
69 #define HAVE_prefetch 0
70 #define CODE_FOR_prefetch 0
71 #define gen_prefetch(a,b,c) (abort(), NULL_RTX)
72 #endif
74 /* Give up the prefetch optimizations once we exceed a given threshold.
75 It is unlikely that we would be able to optimize something in a loop
76 with so many detected prefetches. */
77 #define MAX_PREFETCHES 100
78 /* The number of prefetch blocks that are beneficial to fetch at once before
79 a loop with a known (and low) iteration count. */
80 #define PREFETCH_BLOCKS_BEFORE_LOOP_MAX 6
81 /* For very tiny loops it is not worthwhile to prefetch even before the loop,
82 since it is likely that the data are already in the cache. */
83 #define PREFETCH_BLOCKS_BEFORE_LOOP_MIN 2
85 /* Parameterize some prefetch heuristics so they can be turned on and off
86 easily for performance testing on new architectures. These can be
87 defined in target-dependent files. */
89 /* Prefetch is worthwhile only when loads/stores are dense. */
90 #ifndef PREFETCH_ONLY_DENSE_MEM
91 #define PREFETCH_ONLY_DENSE_MEM 1
92 #endif
94 /* Define what we mean by "dense" loads and stores; This value divided by 256
95 is the minimum percentage of memory references that worth prefetching. */
96 #ifndef PREFETCH_DENSE_MEM
97 #define PREFETCH_DENSE_MEM 220
98 #endif
100 /* Do not prefetch for a loop whose iteration count is known to be low. */
101 #ifndef PREFETCH_NO_LOW_LOOPCNT
102 #define PREFETCH_NO_LOW_LOOPCNT 1
103 #endif
105 /* Define what we mean by a "low" iteration count. */
106 #ifndef PREFETCH_LOW_LOOPCNT
107 #define PREFETCH_LOW_LOOPCNT 32
108 #endif
110 /* Do not prefetch for a loop that contains a function call; such a loop is
111 probably not an internal loop. */
112 #ifndef PREFETCH_NO_CALL
113 #define PREFETCH_NO_CALL 1
114 #endif
116 /* Do not prefetch accesses with an extreme stride. */
117 #ifndef PREFETCH_NO_EXTREME_STRIDE
118 #define PREFETCH_NO_EXTREME_STRIDE 1
119 #endif
121 /* Define what we mean by an "extreme" stride. */
122 #ifndef PREFETCH_EXTREME_STRIDE
123 #define PREFETCH_EXTREME_STRIDE 4096
124 #endif
126 /* Define a limit to how far apart indices can be and still be merged
127 into a single prefetch. */
128 #ifndef PREFETCH_EXTREME_DIFFERENCE
129 #define PREFETCH_EXTREME_DIFFERENCE 4096
130 #endif
132 /* Issue prefetch instructions before the loop to fetch data to be used
133 in the first few loop iterations. */
134 #ifndef PREFETCH_BEFORE_LOOP
135 #define PREFETCH_BEFORE_LOOP 1
136 #endif
138 /* Do not handle reversed order prefetches (negative stride). */
139 #ifndef PREFETCH_NO_REVERSE_ORDER
140 #define PREFETCH_NO_REVERSE_ORDER 1
141 #endif
143 /* Prefetch even if the GIV is in conditional code. */
144 #ifndef PREFETCH_CONDITIONAL
145 #define PREFETCH_CONDITIONAL 1
146 #endif
148 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
149 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
151 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
152 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
153 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
155 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
156 ((REGNO) < FIRST_PSEUDO_REGISTER \
157 ? (int) HARD_REGNO_NREGS ((REGNO), GET_MODE (SET_DEST)) : 1)
160 /* Vector mapping INSN_UIDs to luids.
161 The luids are like uids but increase monotonically always.
162 We use them to see whether a jump comes from outside a given loop. */
164 int *uid_luid;
166 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
167 number the insn is contained in. */
169 struct loop **uid_loop;
171 /* 1 + largest uid of any insn. */
173 int max_uid_for_loop;
175 /* Number of loops detected in current function. Used as index to the
176 next few tables. */
178 static int max_loop_num;
180 /* Bound on pseudo register number before loop optimization.
181 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
182 unsigned int max_reg_before_loop;
184 /* The value to pass to the next call of reg_scan_update. */
185 static int loop_max_reg;
187 /* During the analysis of a loop, a chain of `struct movable's
188 is made to record all the movable insns found.
189 Then the entire chain can be scanned to decide which to move. */
191 struct movable
193 rtx insn; /* A movable insn */
194 rtx set_src; /* The expression this reg is set from. */
195 rtx set_dest; /* The destination of this SET. */
196 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
197 of any registers used within the LIBCALL. */
198 int consec; /* Number of consecutive following insns
199 that must be moved with this one. */
200 unsigned int regno; /* The register it sets */
201 short lifetime; /* lifetime of that register;
202 may be adjusted when matching movables
203 that load the same value are found. */
204 short savings; /* Number of insns we can move for this reg,
205 including other movables that force this
206 or match this one. */
207 ENUM_BITFIELD(machine_mode) savemode : 8; /* Nonzero means it is a mode for
208 a low part that we should avoid changing when
209 clearing the rest of the reg. */
210 unsigned int cond : 1; /* 1 if only conditionally movable */
211 unsigned int force : 1; /* 1 means MUST move this insn */
212 unsigned int global : 1; /* 1 means reg is live outside this loop */
213 /* If PARTIAL is 1, GLOBAL means something different:
214 that the reg is live outside the range from where it is set
215 to the following label. */
216 unsigned int done : 1; /* 1 inhibits further processing of this */
218 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
219 In particular, moving it does not make it
220 invariant. */
221 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
222 load SRC, rather than copying INSN. */
223 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
224 first insn of a consecutive sets group. */
225 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
226 unsigned int insert_temp : 1; /* 1 means we copy to a new pseudo and replace
227 the original insn with a copy from that
228 pseudo, rather than deleting it. */
229 struct movable *match; /* First entry for same value */
230 struct movable *forces; /* An insn that must be moved if this is */
231 struct movable *next;
235 FILE *loop_dump_stream;
237 /* Forward declarations. */
239 static void invalidate_loops_containing_label (rtx);
240 static void find_and_verify_loops (rtx, struct loops *);
241 static void mark_loop_jump (rtx, struct loop *);
242 static void prescan_loop (struct loop *);
243 static int reg_in_basic_block_p (rtx, rtx);
244 static int consec_sets_invariant_p (const struct loop *, rtx, int, rtx);
245 static int labels_in_range_p (rtx, int);
246 static void count_one_set (struct loop_regs *, rtx, rtx, rtx *);
247 static void note_addr_stored (rtx, rtx, void *);
248 static void note_set_pseudo_multiple_uses (rtx, rtx, void *);
249 static int loop_reg_used_before_p (const struct loop *, rtx, rtx);
250 static void scan_loop (struct loop*, int);
251 #if 0
252 static void replace_call_address (rtx, rtx, rtx);
253 #endif
254 static rtx skip_consec_insns (rtx, int);
255 static int libcall_benefit (rtx);
256 static void ignore_some_movables (struct loop_movables *);
257 static void force_movables (struct loop_movables *);
258 static void combine_movables (struct loop_movables *, struct loop_regs *);
259 static int num_unmoved_movables (const struct loop *);
260 static int regs_match_p (rtx, rtx, struct loop_movables *);
261 static int rtx_equal_for_loop_p (rtx, rtx, struct loop_movables *,
262 struct loop_regs *);
263 static void add_label_notes (rtx, rtx);
264 static void move_movables (struct loop *loop, struct loop_movables *, int,
265 int);
266 static void loop_movables_add (struct loop_movables *, struct movable *);
267 static void loop_movables_free (struct loop_movables *);
268 static int count_nonfixed_reads (const struct loop *, rtx);
269 static void loop_bivs_find (struct loop *);
270 static void loop_bivs_init_find (struct loop *);
271 static void loop_bivs_check (struct loop *);
272 static void loop_givs_find (struct loop *);
273 static void loop_givs_check (struct loop *);
274 static int loop_biv_eliminable_p (struct loop *, struct iv_class *, int, int);
275 static int loop_giv_reduce_benefit (struct loop *, struct iv_class *,
276 struct induction *, rtx);
277 static void loop_givs_dead_check (struct loop *, struct iv_class *);
278 static void loop_givs_reduce (struct loop *, struct iv_class *);
279 static void loop_givs_rescan (struct loop *, struct iv_class *, rtx *);
280 static void loop_ivs_free (struct loop *);
281 static void strength_reduce (struct loop *, int);
282 static void find_single_use_in_loop (struct loop_regs *, rtx, rtx);
283 static int valid_initial_value_p (rtx, rtx, int, rtx);
284 static void find_mem_givs (const struct loop *, rtx, rtx, int, int);
285 static void record_biv (struct loop *, struct induction *, rtx, rtx, rtx,
286 rtx, rtx *, int, int);
287 static void check_final_value (const struct loop *, struct induction *);
288 static void loop_ivs_dump (const struct loop *, FILE *, int);
289 static void loop_iv_class_dump (const struct iv_class *, FILE *, int);
290 static void loop_biv_dump (const struct induction *, FILE *, int);
291 static void loop_giv_dump (const struct induction *, FILE *, int);
292 static void record_giv (const struct loop *, struct induction *, rtx, rtx,
293 rtx, rtx, rtx, rtx, int, enum g_types, int, int,
294 rtx *);
295 static void update_giv_derive (const struct loop *, rtx);
296 static void check_ext_dependent_givs (const struct loop *, struct iv_class *);
297 static int basic_induction_var (const struct loop *, rtx, enum machine_mode,
298 rtx, rtx, rtx *, rtx *, rtx **);
299 static rtx simplify_giv_expr (const struct loop *, rtx, rtx *, int *);
300 static int general_induction_var (const struct loop *loop, rtx, rtx *, rtx *,
301 rtx *, rtx *, int, int *, enum machine_mode);
302 static int consec_sets_giv (const struct loop *, int, rtx, rtx, rtx, rtx *,
303 rtx *, rtx *, rtx *);
304 static int check_dbra_loop (struct loop *, int);
305 static rtx express_from_1 (rtx, rtx, rtx);
306 static rtx combine_givs_p (struct induction *, struct induction *);
307 static int cmp_combine_givs_stats (const void *, const void *);
308 static void combine_givs (struct loop_regs *, struct iv_class *);
309 static int product_cheap_p (rtx, rtx);
310 static int maybe_eliminate_biv (const struct loop *, struct iv_class *, int,
311 int, int);
312 static int maybe_eliminate_biv_1 (const struct loop *, rtx, rtx,
313 struct iv_class *, int, basic_block, rtx);
314 static int last_use_this_basic_block (rtx, rtx);
315 static void record_initial (rtx, rtx, void *);
316 static void update_reg_last_use (rtx, rtx);
317 static rtx next_insn_in_loop (const struct loop *, rtx);
318 static void loop_regs_scan (const struct loop *, int);
319 static int count_insns_in_loop (const struct loop *);
320 static int find_mem_in_note_1 (rtx *, void *);
321 static rtx find_mem_in_note (rtx);
322 static void load_mems (const struct loop *);
323 static int insert_loop_mem (rtx *, void *);
324 static int replace_loop_mem (rtx *, void *);
325 static void replace_loop_mems (rtx, rtx, rtx, int);
326 static int replace_loop_reg (rtx *, void *);
327 static void replace_loop_regs (rtx insn, rtx, rtx);
328 static void note_reg_stored (rtx, rtx, void *);
329 static void try_copy_prop (const struct loop *, rtx, unsigned int);
330 static void try_swap_copy_prop (const struct loop *, rtx, unsigned int);
331 static rtx check_insn_for_givs (struct loop *, rtx, int, int);
332 static rtx check_insn_for_bivs (struct loop *, rtx, int, int);
333 static rtx gen_add_mult (rtx, rtx, rtx, rtx);
334 static void loop_regs_update (const struct loop *, rtx);
335 static int iv_add_mult_cost (rtx, rtx, rtx, rtx);
337 static rtx loop_insn_emit_after (const struct loop *, basic_block, rtx, rtx);
338 static rtx loop_call_insn_emit_before (const struct loop *, basic_block,
339 rtx, rtx);
340 static rtx loop_call_insn_hoist (const struct loop *, rtx);
341 static rtx loop_insn_sink_or_swim (const struct loop *, rtx);
343 static void loop_dump_aux (const struct loop *, FILE *, int);
344 static void loop_delete_insns (rtx, rtx);
345 static HOST_WIDE_INT remove_constant_addition (rtx *);
346 static rtx gen_load_of_final_value (rtx, rtx);
347 void debug_ivs (const struct loop *);
348 void debug_iv_class (const struct iv_class *);
349 void debug_biv (const struct induction *);
350 void debug_giv (const struct induction *);
351 void debug_loop (const struct loop *);
352 void debug_loops (const struct loops *);
354 typedef struct loop_replace_args
356 rtx match;
357 rtx replacement;
358 rtx insn;
359 } loop_replace_args;
361 /* Nonzero iff INSN is between START and END, inclusive. */
362 #define INSN_IN_RANGE_P(INSN, START, END) \
363 (INSN_UID (INSN) < max_uid_for_loop \
364 && INSN_LUID (INSN) >= INSN_LUID (START) \
365 && INSN_LUID (INSN) <= INSN_LUID (END))
367 /* Indirect_jump_in_function is computed once per function. */
368 static int indirect_jump_in_function;
369 static int indirect_jump_in_function_p (rtx);
371 static int compute_luids (rtx, rtx, int);
373 static int biv_elimination_giv_has_0_offset (struct induction *,
374 struct induction *, rtx);
376 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
377 copy the value of the strength reduced giv to its original register. */
378 static int copy_cost;
380 /* Cost of using a register, to normalize the benefits of a giv. */
381 static int reg_address_cost;
383 void
384 init_loop (void)
386 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
388 reg_address_cost = address_cost (reg, SImode);
390 copy_cost = COSTS_N_INSNS (1);
393 /* Compute the mapping from uids to luids.
394 LUIDs are numbers assigned to insns, like uids,
395 except that luids increase monotonically through the code.
396 Start at insn START and stop just before END. Assign LUIDs
397 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
398 static int
399 compute_luids (rtx start, rtx end, int prev_luid)
401 int i;
402 rtx insn;
404 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
406 if (INSN_UID (insn) >= max_uid_for_loop)
407 continue;
408 /* Don't assign luids to line-number NOTEs, so that the distance in
409 luids between two insns is not affected by -g. */
410 if (GET_CODE (insn) != NOTE
411 || NOTE_LINE_NUMBER (insn) <= 0)
412 uid_luid[INSN_UID (insn)] = ++i;
413 else
414 /* Give a line number note the same luid as preceding insn. */
415 uid_luid[INSN_UID (insn)] = i;
417 return i + 1;
420 /* Entry point of this file. Perform loop optimization
421 on the current function. F is the first insn of the function
422 and DUMPFILE is a stream for output of a trace of actions taken
423 (or 0 if none should be output). */
425 void
426 loop_optimize (rtx f, FILE *dumpfile, int flags)
428 rtx insn;
429 int i;
430 struct loops loops_data;
431 struct loops *loops = &loops_data;
432 struct loop_info *loops_info;
434 loop_dump_stream = dumpfile;
436 init_recog_no_volatile ();
438 max_reg_before_loop = max_reg_num ();
439 loop_max_reg = max_reg_before_loop;
441 regs_may_share = 0;
443 /* Count the number of loops. */
445 max_loop_num = 0;
446 for (insn = f; insn; insn = NEXT_INSN (insn))
448 if (GET_CODE (insn) == NOTE
449 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
450 max_loop_num++;
453 /* Don't waste time if no loops. */
454 if (max_loop_num == 0)
455 return;
457 loops->num = max_loop_num;
459 /* Get size to use for tables indexed by uids.
460 Leave some space for labels allocated by find_and_verify_loops. */
461 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
463 uid_luid = xcalloc (max_uid_for_loop, sizeof (int));
464 uid_loop = xcalloc (max_uid_for_loop, sizeof (struct loop *));
466 /* Allocate storage for array of loops. */
467 loops->array = xcalloc (loops->num, sizeof (struct loop));
469 /* Find and process each loop.
470 First, find them, and record them in order of their beginnings. */
471 find_and_verify_loops (f, loops);
473 /* Allocate and initialize auxiliary loop information. */
474 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
475 for (i = 0; i < (int) loops->num; i++)
476 loops->array[i].aux = loops_info + i;
478 /* Now find all register lifetimes. This must be done after
479 find_and_verify_loops, because it might reorder the insns in the
480 function. */
481 reg_scan (f, max_reg_before_loop, 1);
483 /* This must occur after reg_scan so that registers created by gcse
484 will have entries in the register tables.
486 We could have added a call to reg_scan after gcse_main in toplev.c,
487 but moving this call to init_alias_analysis is more efficient. */
488 init_alias_analysis ();
490 /* See if we went too far. Note that get_max_uid already returns
491 one more that the maximum uid of all insn. */
492 if (get_max_uid () > max_uid_for_loop)
493 abort ();
494 /* Now reset it to the actual size we need. See above. */
495 max_uid_for_loop = get_max_uid ();
497 /* find_and_verify_loops has already called compute_luids, but it
498 might have rearranged code afterwards, so we need to recompute
499 the luids now. */
500 compute_luids (f, NULL_RTX, 0);
502 /* Don't leave gaps in uid_luid for insns that have been
503 deleted. It is possible that the first or last insn
504 using some register has been deleted by cross-jumping.
505 Make sure that uid_luid for that former insn's uid
506 points to the general area where that insn used to be. */
507 for (i = 0; i < max_uid_for_loop; i++)
509 uid_luid[0] = uid_luid[i];
510 if (uid_luid[0] != 0)
511 break;
513 for (i = 0; i < max_uid_for_loop; i++)
514 if (uid_luid[i] == 0)
515 uid_luid[i] = uid_luid[i - 1];
517 /* Determine if the function has indirect jump. On some systems
518 this prevents low overhead loop instructions from being used. */
519 indirect_jump_in_function = indirect_jump_in_function_p (f);
521 /* Now scan the loops, last ones first, since this means inner ones are done
522 before outer ones. */
523 for (i = max_loop_num - 1; i >= 0; i--)
525 struct loop *loop = &loops->array[i];
527 if (! loop->invalid && loop->end)
528 scan_loop (loop, flags);
531 end_alias_analysis ();
533 /* Clean up. */
534 free (uid_luid);
535 free (uid_loop);
536 free (loops_info);
537 free (loops->array);
540 /* Returns the next insn, in execution order, after INSN. START and
541 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
542 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
543 insn-stream; it is used with loops that are entered near the
544 bottom. */
546 static rtx
547 next_insn_in_loop (const struct loop *loop, rtx insn)
549 insn = NEXT_INSN (insn);
551 if (insn == loop->end)
553 if (loop->top)
554 /* Go to the top of the loop, and continue there. */
555 insn = loop->top;
556 else
557 /* We're done. */
558 insn = NULL_RTX;
561 if (insn == loop->scan_start)
562 /* We're done. */
563 insn = NULL_RTX;
565 return insn;
568 /* Optimize one loop described by LOOP. */
570 /* ??? Could also move memory writes out of loops if the destination address
571 is invariant, the source is invariant, the memory write is not volatile,
572 and if we can prove that no read inside the loop can read this address
573 before the write occurs. If there is a read of this address after the
574 write, then we can also mark the memory read as invariant. */
576 static void
577 scan_loop (struct loop *loop, int flags)
579 struct loop_info *loop_info = LOOP_INFO (loop);
580 struct loop_regs *regs = LOOP_REGS (loop);
581 int i;
582 rtx loop_start = loop->start;
583 rtx loop_end = loop->end;
584 rtx p;
585 /* 1 if we are scanning insns that could be executed zero times. */
586 int maybe_never = 0;
587 /* 1 if we are scanning insns that might never be executed
588 due to a subroutine call which might exit before they are reached. */
589 int call_passed = 0;
590 /* Number of insns in the loop. */
591 int insn_count;
592 int tem;
593 rtx temp, update_start, update_end;
594 /* The SET from an insn, if it is the only SET in the insn. */
595 rtx set, set1;
596 /* Chain describing insns movable in current loop. */
597 struct loop_movables *movables = LOOP_MOVABLES (loop);
598 /* Ratio of extra register life span we can justify
599 for saving an instruction. More if loop doesn't call subroutines
600 since in that case saving an insn makes more difference
601 and more registers are available. */
602 int threshold;
603 /* Nonzero if we are scanning instructions in a sub-loop. */
604 int loop_depth = 0;
605 int in_libcall;
607 loop->top = 0;
609 movables->head = 0;
610 movables->last = 0;
612 /* Determine whether this loop starts with a jump down to a test at
613 the end. This will occur for a small number of loops with a test
614 that is too complex to duplicate in front of the loop.
616 We search for the first insn or label in the loop, skipping NOTEs.
617 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
618 (because we might have a loop executed only once that contains a
619 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
620 (in case we have a degenerate loop).
622 Note that if we mistakenly think that a loop is entered at the top
623 when, in fact, it is entered at the exit test, the only effect will be
624 slightly poorer optimization. Making the opposite error can generate
625 incorrect code. Since very few loops now start with a jump to the
626 exit test, the code here to detect that case is very conservative. */
628 for (p = NEXT_INSN (loop_start);
629 p != loop_end
630 && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
631 && (GET_CODE (p) != NOTE
632 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
633 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
634 p = NEXT_INSN (p))
637 loop->scan_start = p;
639 /* If loop end is the end of the current function, then emit a
640 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
641 note insn. This is the position we use when sinking insns out of
642 the loop. */
643 if (NEXT_INSN (loop->end) != 0)
644 loop->sink = NEXT_INSN (loop->end);
645 else
646 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
648 /* Set up variables describing this loop. */
649 prescan_loop (loop);
650 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
652 /* If loop has a jump before the first label,
653 the true entry is the target of that jump.
654 Start scan from there.
655 But record in LOOP->TOP the place where the end-test jumps
656 back to so we can scan that after the end of the loop. */
657 if (GET_CODE (p) == JUMP_INSN
658 /* Loop entry must be unconditional jump (and not a RETURN) */
659 && any_uncondjump_p (p)
660 && JUMP_LABEL (p) != 0
661 /* Check to see whether the jump actually
662 jumps out of the loop (meaning it's no loop).
663 This case can happen for things like
664 do {..} while (0). If this label was generated previously
665 by loop, we can't tell anything about it and have to reject
666 the loop. */
667 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
669 loop->top = next_label (loop->scan_start);
670 loop->scan_start = JUMP_LABEL (p);
673 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
674 as required by loop_reg_used_before_p. So skip such loops. (This
675 test may never be true, but it's best to play it safe.)
677 Also, skip loops where we do not start scanning at a label. This
678 test also rejects loops starting with a JUMP_INSN that failed the
679 test above. */
681 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
682 || GET_CODE (loop->scan_start) != CODE_LABEL)
684 if (loop_dump_stream)
685 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
686 INSN_UID (loop_start), INSN_UID (loop_end));
687 return;
690 /* Allocate extra space for REGs that might be created by load_mems.
691 We allocate a little extra slop as well, in the hopes that we
692 won't have to reallocate the regs array. */
693 loop_regs_scan (loop, loop_info->mems_idx + 16);
694 insn_count = count_insns_in_loop (loop);
696 if (loop_dump_stream)
698 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
699 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
700 if (loop->cont)
701 fprintf (loop_dump_stream, "Continue at insn %d.\n",
702 INSN_UID (loop->cont));
705 /* Scan through the loop finding insns that are safe to move.
706 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
707 this reg will be considered invariant for subsequent insns.
708 We consider whether subsequent insns use the reg
709 in deciding whether it is worth actually moving.
711 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
712 and therefore it is possible that the insns we are scanning
713 would never be executed. At such times, we must make sure
714 that it is safe to execute the insn once instead of zero times.
715 When MAYBE_NEVER is 0, all insns will be executed at least once
716 so that is not a problem. */
718 for (in_libcall = 0, p = next_insn_in_loop (loop, loop->scan_start);
719 p != NULL_RTX;
720 p = next_insn_in_loop (loop, p))
722 if (in_libcall && INSN_P (p) && find_reg_note (p, REG_RETVAL, NULL_RTX))
723 in_libcall--;
724 if (GET_CODE (p) == INSN)
726 temp = find_reg_note (p, REG_LIBCALL, NULL_RTX);
727 if (temp)
728 in_libcall++;
729 if (! in_libcall
730 && (set = single_set (p))
731 && GET_CODE (SET_DEST (set)) == REG
732 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
733 && SET_DEST (set) != pic_offset_table_rtx
734 #endif
735 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
737 int tem1 = 0;
738 int tem2 = 0;
739 int move_insn = 0;
740 int insert_temp = 0;
741 rtx src = SET_SRC (set);
742 rtx dependencies = 0;
744 /* Figure out what to use as a source of this insn. If a
745 REG_EQUIV note is given or if a REG_EQUAL note with a
746 constant operand is specified, use it as the source and
747 mark that we should move this insn by calling
748 emit_move_insn rather that duplicating the insn.
750 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL
751 note is present. */
752 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
753 if (temp)
754 src = XEXP (temp, 0), move_insn = 1;
755 else
757 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
758 if (temp && CONSTANT_P (XEXP (temp, 0)))
759 src = XEXP (temp, 0), move_insn = 1;
760 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
762 src = XEXP (temp, 0);
763 /* A libcall block can use regs that don't appear in
764 the equivalent expression. To move the libcall,
765 we must move those regs too. */
766 dependencies = libcall_other_reg (p, src);
770 /* For parallels, add any possible uses to the dependencies, as
771 we can't move the insn without resolving them first. */
772 if (GET_CODE (PATTERN (p)) == PARALLEL)
774 for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
776 rtx x = XVECEXP (PATTERN (p), 0, i);
777 if (GET_CODE (x) == USE)
778 dependencies
779 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0),
780 dependencies);
784 if (/* The register is used in basic blocks other
785 than the one where it is set (meaning that
786 something after this point in the loop might
787 depend on its value before the set). */
788 ! reg_in_basic_block_p (p, SET_DEST (set))
789 /* And the set is not guaranteed to be executed once
790 the loop starts, or the value before the set is
791 needed before the set occurs...
793 ??? Note we have quadratic behavior here, mitigated
794 by the fact that the previous test will often fail for
795 large loops. Rather than re-scanning the entire loop
796 each time for register usage, we should build tables
797 of the register usage and use them here instead. */
798 && (maybe_never
799 || loop_reg_used_before_p (loop, set, p)))
800 /* It is unsafe to move the set. However, it may be OK to
801 move the source into a new pseudo, and substitute a
802 reg-to-reg copy for the original insn.
804 This code used to consider it OK to move a set of a variable
805 which was not created by the user and not used in an exit
806 test.
807 That behavior is incorrect and was removed. */
808 insert_temp = 1;
810 /* Don't try to optimize a MODE_CC set with a constant
811 source. It probably will be combined with a conditional
812 jump. */
813 if (GET_MODE_CLASS (GET_MODE (SET_DEST (set))) == MODE_CC
814 && CONSTANT_P (src))
816 /* Don't try to optimize a register that was made
817 by loop-optimization for an inner loop.
818 We don't know its life-span, so we can't compute
819 the benefit. */
820 else if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
822 /* Don't move the source and add a reg-to-reg copy:
823 - with -Os (this certainly increases size),
824 - if the mode doesn't support copy operations (obviously),
825 - if the source is already a reg (the motion will gain nothing),
826 - if the source is a legitimate constant (likewise). */
827 else if (insert_temp
828 && (optimize_size
829 || ! can_copy_p (GET_MODE (SET_SRC (set)))
830 || GET_CODE (SET_SRC (set)) == REG
831 || (CONSTANT_P (SET_SRC (set))
832 && LEGITIMATE_CONSTANT_P (SET_SRC (set)))))
834 else if ((tem = loop_invariant_p (loop, src))
835 && (dependencies == 0
836 || (tem2
837 = loop_invariant_p (loop, dependencies)) != 0)
838 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
839 || (tem1
840 = consec_sets_invariant_p
841 (loop, SET_DEST (set),
842 regs->array[REGNO (SET_DEST (set))].set_in_loop,
843 p)))
844 /* If the insn can cause a trap (such as divide by zero),
845 can't move it unless it's guaranteed to be executed
846 once loop is entered. Even a function call might
847 prevent the trap insn from being reached
848 (since it might exit!) */
849 && ! ((maybe_never || call_passed)
850 && may_trap_p (src)))
852 struct movable *m;
853 int regno = REGNO (SET_DEST (set));
855 /* A potential lossage is where we have a case where two insns
856 can be combined as long as they are both in the loop, but
857 we move one of them outside the loop. For large loops,
858 this can lose. The most common case of this is the address
859 of a function being called.
861 Therefore, if this register is marked as being used
862 exactly once if we are in a loop with calls
863 (a "large loop"), see if we can replace the usage of
864 this register with the source of this SET. If we can,
865 delete this insn.
867 Don't do this if P has a REG_RETVAL note or if we have
868 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
870 if (loop_info->has_call
871 && regs->array[regno].single_usage != 0
872 && regs->array[regno].single_usage != const0_rtx
873 && REGNO_FIRST_UID (regno) == INSN_UID (p)
874 && (REGNO_LAST_UID (regno)
875 == INSN_UID (regs->array[regno].single_usage))
876 && regs->array[regno].set_in_loop == 1
877 && GET_CODE (SET_SRC (set)) != ASM_OPERANDS
878 && ! side_effects_p (SET_SRC (set))
879 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
880 && (! SMALL_REGISTER_CLASSES
881 || (! (GET_CODE (SET_SRC (set)) == REG
882 && (REGNO (SET_SRC (set))
883 < FIRST_PSEUDO_REGISTER))))
884 /* This test is not redundant; SET_SRC (set) might be
885 a call-clobbered register and the life of REGNO
886 might span a call. */
887 && ! modified_between_p (SET_SRC (set), p,
888 regs->array[regno].single_usage)
889 && no_labels_between_p (p,
890 regs->array[regno].single_usage)
891 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
892 regs->array[regno].single_usage))
894 /* Replace any usage in a REG_EQUAL note. Must copy
895 the new source, so that we don't get rtx sharing
896 between the SET_SOURCE and REG_NOTES of insn p. */
897 REG_NOTES (regs->array[regno].single_usage)
898 = (replace_rtx
899 (REG_NOTES (regs->array[regno].single_usage),
900 SET_DEST (set), copy_rtx (SET_SRC (set))));
902 delete_insn (p);
903 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set));
904 i++)
905 regs->array[regno+i].set_in_loop = 0;
906 continue;
909 m = xmalloc (sizeof (struct movable));
910 m->next = 0;
911 m->insn = p;
912 m->set_src = src;
913 m->dependencies = dependencies;
914 m->set_dest = SET_DEST (set);
915 m->force = 0;
916 m->consec
917 = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
918 m->done = 0;
919 m->forces = 0;
920 m->partial = 0;
921 m->move_insn = move_insn;
922 m->move_insn_first = 0;
923 m->insert_temp = insert_temp;
924 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
925 m->savemode = VOIDmode;
926 m->regno = regno;
927 /* Set M->cond if either loop_invariant_p
928 or consec_sets_invariant_p returned 2
929 (only conditionally invariant). */
930 m->cond = ((tem | tem1 | tem2) > 1);
931 m->global = LOOP_REG_GLOBAL_P (loop, regno);
932 m->match = 0;
933 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
934 m->savings = regs->array[regno].n_times_set;
935 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
936 m->savings += libcall_benefit (p);
937 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
938 regs->array[regno+i].set_in_loop = move_insn ? -2 : -1;
939 /* Add M to the end of the chain MOVABLES. */
940 loop_movables_add (movables, m);
942 if (m->consec > 0)
944 /* It is possible for the first instruction to have a
945 REG_EQUAL note but a non-invariant SET_SRC, so we must
946 remember the status of the first instruction in case
947 the last instruction doesn't have a REG_EQUAL note. */
948 m->move_insn_first = m->move_insn;
950 /* Skip this insn, not checking REG_LIBCALL notes. */
951 p = next_nonnote_insn (p);
952 /* Skip the consecutive insns, if there are any. */
953 p = skip_consec_insns (p, m->consec);
954 /* Back up to the last insn of the consecutive group. */
955 p = prev_nonnote_insn (p);
957 /* We must now reset m->move_insn, m->is_equiv, and
958 possibly m->set_src to correspond to the effects of
959 all the insns. */
960 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
961 if (temp)
962 m->set_src = XEXP (temp, 0), m->move_insn = 1;
963 else
965 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
966 if (temp && CONSTANT_P (XEXP (temp, 0)))
967 m->set_src = XEXP (temp, 0), m->move_insn = 1;
968 else
969 m->move_insn = 0;
972 m->is_equiv
973 = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
976 /* If this register is always set within a STRICT_LOW_PART
977 or set to zero, then its high bytes are constant.
978 So clear them outside the loop and within the loop
979 just load the low bytes.
980 We must check that the machine has an instruction to do so.
981 Also, if the value loaded into the register
982 depends on the same register, this cannot be done. */
983 else if (SET_SRC (set) == const0_rtx
984 && GET_CODE (NEXT_INSN (p)) == INSN
985 && (set1 = single_set (NEXT_INSN (p)))
986 && GET_CODE (set1) == SET
987 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
988 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
989 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
990 == SET_DEST (set))
991 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
993 int regno = REGNO (SET_DEST (set));
994 if (regs->array[regno].set_in_loop == 2)
996 struct movable *m;
997 m = xmalloc (sizeof (struct movable));
998 m->next = 0;
999 m->insn = p;
1000 m->set_dest = SET_DEST (set);
1001 m->dependencies = 0;
1002 m->force = 0;
1003 m->consec = 0;
1004 m->done = 0;
1005 m->forces = 0;
1006 m->move_insn = 0;
1007 m->move_insn_first = 0;
1008 m->insert_temp = insert_temp;
1009 m->partial = 1;
1010 /* If the insn may not be executed on some cycles,
1011 we can't clear the whole reg; clear just high part.
1012 Not even if the reg is used only within this loop.
1013 Consider this:
1014 while (1)
1015 while (s != t) {
1016 if (foo ()) x = *s;
1017 use (x);
1019 Clearing x before the inner loop could clobber a value
1020 being saved from the last time around the outer loop.
1021 However, if the reg is not used outside this loop
1022 and all uses of the register are in the same
1023 basic block as the store, there is no problem.
1025 If this insn was made by loop, we don't know its
1026 INSN_LUID and hence must make a conservative
1027 assumption. */
1028 m->global = (INSN_UID (p) >= max_uid_for_loop
1029 || LOOP_REG_GLOBAL_P (loop, regno)
1030 || (labels_in_range_p
1031 (p, REGNO_FIRST_LUID (regno))));
1032 if (maybe_never && m->global)
1033 m->savemode = GET_MODE (SET_SRC (set1));
1034 else
1035 m->savemode = VOIDmode;
1036 m->regno = regno;
1037 m->cond = 0;
1038 m->match = 0;
1039 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
1040 m->savings = 1;
1041 for (i = 0;
1042 i < LOOP_REGNO_NREGS (regno, SET_DEST (set));
1043 i++)
1044 regs->array[regno+i].set_in_loop = -1;
1045 /* Add M to the end of the chain MOVABLES. */
1046 loop_movables_add (movables, m);
1051 /* Past a call insn, we get to insns which might not be executed
1052 because the call might exit. This matters for insns that trap.
1053 Constant and pure call insns always return, so they don't count. */
1054 else if (GET_CODE (p) == CALL_INSN && ! CONST_OR_PURE_CALL_P (p))
1055 call_passed = 1;
1056 /* Past a label or a jump, we get to insns for which we
1057 can't count on whether or how many times they will be
1058 executed during each iteration. Therefore, we can
1059 only move out sets of trivial variables
1060 (those not used after the loop). */
1061 /* Similar code appears twice in strength_reduce. */
1062 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1063 /* If we enter the loop in the middle, and scan around to the
1064 beginning, don't set maybe_never for that. This must be an
1065 unconditional jump, otherwise the code at the top of the
1066 loop might never be executed. Unconditional jumps are
1067 followed by a barrier then the loop_end. */
1068 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
1069 && NEXT_INSN (NEXT_INSN (p)) == loop_end
1070 && any_uncondjump_p (p)))
1071 maybe_never = 1;
1072 else if (GET_CODE (p) == NOTE)
1074 /* At the virtual top of a converted loop, insns are again known to
1075 be executed: logically, the loop begins here even though the exit
1076 code has been duplicated. */
1077 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1078 maybe_never = call_passed = 0;
1079 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1080 loop_depth++;
1081 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1082 loop_depth--;
1086 /* If one movable subsumes another, ignore that other. */
1088 ignore_some_movables (movables);
1090 /* For each movable insn, see if the reg that it loads
1091 leads when it dies right into another conditionally movable insn.
1092 If so, record that the second insn "forces" the first one,
1093 since the second can be moved only if the first is. */
1095 force_movables (movables);
1097 /* See if there are multiple movable insns that load the same value.
1098 If there are, make all but the first point at the first one
1099 through the `match' field, and add the priorities of them
1100 all together as the priority of the first. */
1102 combine_movables (movables, regs);
1104 /* Now consider each movable insn to decide whether it is worth moving.
1105 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1107 For machines with few registers this increases code size, so do not
1108 move moveables when optimizing for code size on such machines.
1109 (The 18 below is the value for i386.) */
1111 if (!optimize_size
1112 || (reg_class_size[GENERAL_REGS] > 18 && !loop_info->has_call))
1114 move_movables (loop, movables, threshold, insn_count);
1116 /* Recalculate regs->array if move_movables has created new
1117 registers. */
1118 if (max_reg_num () > regs->num)
1120 loop_regs_scan (loop, 0);
1121 for (update_start = loop_start;
1122 PREV_INSN (update_start)
1123 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1124 update_start = PREV_INSN (update_start))
1126 update_end = NEXT_INSN (loop_end);
1128 reg_scan_update (update_start, update_end, loop_max_reg);
1129 loop_max_reg = max_reg_num ();
1133 /* Now candidates that still are negative are those not moved.
1134 Change regs->array[I].set_in_loop to indicate that those are not actually
1135 invariant. */
1136 for (i = 0; i < regs->num; i++)
1137 if (regs->array[i].set_in_loop < 0)
1138 regs->array[i].set_in_loop = regs->array[i].n_times_set;
1140 /* Now that we've moved some things out of the loop, we might be able to
1141 hoist even more memory references. */
1142 load_mems (loop);
1144 /* Recalculate regs->array if load_mems has created new registers. */
1145 if (max_reg_num () > regs->num)
1146 loop_regs_scan (loop, 0);
1148 for (update_start = loop_start;
1149 PREV_INSN (update_start)
1150 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1151 update_start = PREV_INSN (update_start))
1153 update_end = NEXT_INSN (loop_end);
1155 reg_scan_update (update_start, update_end, loop_max_reg);
1156 loop_max_reg = max_reg_num ();
1158 if (flag_strength_reduce)
1160 if (update_end && GET_CODE (update_end) == CODE_LABEL)
1161 /* Ensure our label doesn't go away. */
1162 LABEL_NUSES (update_end)++;
1164 strength_reduce (loop, flags);
1166 reg_scan_update (update_start, update_end, loop_max_reg);
1167 loop_max_reg = max_reg_num ();
1169 if (update_end && GET_CODE (update_end) == CODE_LABEL
1170 && --LABEL_NUSES (update_end) == 0)
1171 delete_related_insns (update_end);
1175 /* The movable information is required for strength reduction. */
1176 loop_movables_free (movables);
1178 free (regs->array);
1179 regs->array = 0;
1180 regs->num = 0;
1183 /* Add elements to *OUTPUT to record all the pseudo-regs
1184 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1186 void
1187 record_excess_regs (rtx in_this, rtx not_in_this, rtx *output)
1189 enum rtx_code code;
1190 const char *fmt;
1191 int i;
1193 code = GET_CODE (in_this);
1195 switch (code)
1197 case PC:
1198 case CC0:
1199 case CONST_INT:
1200 case CONST_DOUBLE:
1201 case CONST:
1202 case SYMBOL_REF:
1203 case LABEL_REF:
1204 return;
1206 case REG:
1207 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1208 && ! reg_mentioned_p (in_this, not_in_this))
1209 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1210 return;
1212 default:
1213 break;
1216 fmt = GET_RTX_FORMAT (code);
1217 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1219 int j;
1221 switch (fmt[i])
1223 case 'E':
1224 for (j = 0; j < XVECLEN (in_this, i); j++)
1225 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1226 break;
1228 case 'e':
1229 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1230 break;
1235 /* Check what regs are referred to in the libcall block ending with INSN,
1236 aside from those mentioned in the equivalent value.
1237 If there are none, return 0.
1238 If there are one or more, return an EXPR_LIST containing all of them. */
1241 libcall_other_reg (rtx insn, rtx equiv)
1243 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1244 rtx p = XEXP (note, 0);
1245 rtx output = 0;
1247 /* First, find all the regs used in the libcall block
1248 that are not mentioned as inputs to the result. */
1250 while (p != insn)
1252 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1253 || GET_CODE (p) == CALL_INSN)
1254 record_excess_regs (PATTERN (p), equiv, &output);
1255 p = NEXT_INSN (p);
1258 return output;
1261 /* Return 1 if all uses of REG
1262 are between INSN and the end of the basic block. */
1264 static int
1265 reg_in_basic_block_p (rtx insn, rtx reg)
1267 int regno = REGNO (reg);
1268 rtx p;
1270 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1271 return 0;
1273 /* Search this basic block for the already recorded last use of the reg. */
1274 for (p = insn; p; p = NEXT_INSN (p))
1276 switch (GET_CODE (p))
1278 case NOTE:
1279 break;
1281 case INSN:
1282 case CALL_INSN:
1283 /* Ordinary insn: if this is the last use, we win. */
1284 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1285 return 1;
1286 break;
1288 case JUMP_INSN:
1289 /* Jump insn: if this is the last use, we win. */
1290 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1291 return 1;
1292 /* Otherwise, it's the end of the basic block, so we lose. */
1293 return 0;
1295 case CODE_LABEL:
1296 case BARRIER:
1297 /* It's the end of the basic block, so we lose. */
1298 return 0;
1300 default:
1301 break;
1305 /* The "last use" that was recorded can't be found after the first
1306 use. This can happen when the last use was deleted while
1307 processing an inner loop, this inner loop was then completely
1308 unrolled, and the outer loop is always exited after the inner loop,
1309 so that everything after the first use becomes a single basic block. */
1310 return 1;
1313 /* Compute the benefit of eliminating the insns in the block whose
1314 last insn is LAST. This may be a group of insns used to compute a
1315 value directly or can contain a library call. */
1317 static int
1318 libcall_benefit (rtx last)
1320 rtx insn;
1321 int benefit = 0;
1323 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1324 insn != last; insn = NEXT_INSN (insn))
1326 if (GET_CODE (insn) == CALL_INSN)
1327 benefit += 10; /* Assume at least this many insns in a library
1328 routine. */
1329 else if (GET_CODE (insn) == INSN
1330 && GET_CODE (PATTERN (insn)) != USE
1331 && GET_CODE (PATTERN (insn)) != CLOBBER)
1332 benefit++;
1335 return benefit;
1338 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1340 static rtx
1341 skip_consec_insns (rtx insn, int count)
1343 for (; count > 0; count--)
1345 rtx temp;
1347 /* If first insn of libcall sequence, skip to end. */
1348 /* Do this at start of loop, since INSN is guaranteed to
1349 be an insn here. */
1350 if (GET_CODE (insn) != NOTE
1351 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1352 insn = XEXP (temp, 0);
1355 insn = NEXT_INSN (insn);
1356 while (GET_CODE (insn) == NOTE);
1359 return insn;
1362 /* Ignore any movable whose insn falls within a libcall
1363 which is part of another movable.
1364 We make use of the fact that the movable for the libcall value
1365 was made later and so appears later on the chain. */
1367 static void
1368 ignore_some_movables (struct loop_movables *movables)
1370 struct movable *m, *m1;
1372 for (m = movables->head; m; m = m->next)
1374 /* Is this a movable for the value of a libcall? */
1375 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1376 if (note)
1378 rtx insn;
1379 /* Check for earlier movables inside that range,
1380 and mark them invalid. We cannot use LUIDs here because
1381 insns created by loop.c for prior loops don't have LUIDs.
1382 Rather than reject all such insns from movables, we just
1383 explicitly check each insn in the libcall (since invariant
1384 libcalls aren't that common). */
1385 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1386 for (m1 = movables->head; m1 != m; m1 = m1->next)
1387 if (m1->insn == insn)
1388 m1->done = 1;
1393 /* For each movable insn, see if the reg that it loads
1394 leads when it dies right into another conditionally movable insn.
1395 If so, record that the second insn "forces" the first one,
1396 since the second can be moved only if the first is. */
1398 static void
1399 force_movables (struct loop_movables *movables)
1401 struct movable *m, *m1;
1403 for (m1 = movables->head; m1; m1 = m1->next)
1404 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1405 if (!m1->partial && !m1->done)
1407 int regno = m1->regno;
1408 for (m = m1->next; m; m = m->next)
1409 /* ??? Could this be a bug? What if CSE caused the
1410 register of M1 to be used after this insn?
1411 Since CSE does not update regno_last_uid,
1412 this insn M->insn might not be where it dies.
1413 But very likely this doesn't matter; what matters is
1414 that M's reg is computed from M1's reg. */
1415 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1416 && !m->done)
1417 break;
1418 if (m != 0 && m->set_src == m1->set_dest
1419 /* If m->consec, m->set_src isn't valid. */
1420 && m->consec == 0)
1421 m = 0;
1423 /* Increase the priority of the moving the first insn
1424 since it permits the second to be moved as well. */
1425 if (m != 0)
1427 m->forces = m1;
1428 m1->lifetime += m->lifetime;
1429 m1->savings += m->savings;
1434 /* Find invariant expressions that are equal and can be combined into
1435 one register. */
1437 static void
1438 combine_movables (struct loop_movables *movables, struct loop_regs *regs)
1440 struct movable *m;
1441 char *matched_regs = xmalloc (regs->num);
1442 enum machine_mode mode;
1444 /* Regs that are set more than once are not allowed to match
1445 or be matched. I'm no longer sure why not. */
1446 /* Only pseudo registers are allowed to match or be matched,
1447 since move_movables does not validate the change. */
1448 /* Perhaps testing m->consec_sets would be more appropriate here? */
1450 for (m = movables->head; m; m = m->next)
1451 if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1452 && m->regno >= FIRST_PSEUDO_REGISTER
1453 && !m->insert_temp
1454 && !m->partial)
1456 struct movable *m1;
1457 int regno = m->regno;
1459 memset (matched_regs, 0, regs->num);
1460 matched_regs[regno] = 1;
1462 /* We want later insns to match the first one. Don't make the first
1463 one match any later ones. So start this loop at m->next. */
1464 for (m1 = m->next; m1; m1 = m1->next)
1465 if (m != m1 && m1->match == 0
1466 && !m1->insert_temp
1467 && regs->array[m1->regno].n_times_set == 1
1468 && m1->regno >= FIRST_PSEUDO_REGISTER
1469 /* A reg used outside the loop mustn't be eliminated. */
1470 && !m1->global
1471 /* A reg used for zero-extending mustn't be eliminated. */
1472 && !m1->partial
1473 && (matched_regs[m1->regno]
1476 /* Can combine regs with different modes loaded from the
1477 same constant only if the modes are the same or
1478 if both are integer modes with M wider or the same
1479 width as M1. The check for integer is redundant, but
1480 safe, since the only case of differing destination
1481 modes with equal sources is when both sources are
1482 VOIDmode, i.e., CONST_INT. */
1483 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1484 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1485 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1486 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1487 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1488 /* See if the source of M1 says it matches M. */
1489 && ((GET_CODE (m1->set_src) == REG
1490 && matched_regs[REGNO (m1->set_src)])
1491 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1492 movables, regs))))
1493 && ((m->dependencies == m1->dependencies)
1494 || rtx_equal_p (m->dependencies, m1->dependencies)))
1496 m->lifetime += m1->lifetime;
1497 m->savings += m1->savings;
1498 m1->done = 1;
1499 m1->match = m;
1500 matched_regs[m1->regno] = 1;
1504 /* Now combine the regs used for zero-extension.
1505 This can be done for those not marked `global'
1506 provided their lives don't overlap. */
1508 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1509 mode = GET_MODE_WIDER_MODE (mode))
1511 struct movable *m0 = 0;
1513 /* Combine all the registers for extension from mode MODE.
1514 Don't combine any that are used outside this loop. */
1515 for (m = movables->head; m; m = m->next)
1516 if (m->partial && ! m->global
1517 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1519 struct movable *m1;
1521 int first = REGNO_FIRST_LUID (m->regno);
1522 int last = REGNO_LAST_LUID (m->regno);
1524 if (m0 == 0)
1526 /* First one: don't check for overlap, just record it. */
1527 m0 = m;
1528 continue;
1531 /* Make sure they extend to the same mode.
1532 (Almost always true.) */
1533 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1534 continue;
1536 /* We already have one: check for overlap with those
1537 already combined together. */
1538 for (m1 = movables->head; m1 != m; m1 = m1->next)
1539 if (m1 == m0 || (m1->partial && m1->match == m0))
1540 if (! (REGNO_FIRST_LUID (m1->regno) > last
1541 || REGNO_LAST_LUID (m1->regno) < first))
1542 goto overlap;
1544 /* No overlap: we can combine this with the others. */
1545 m0->lifetime += m->lifetime;
1546 m0->savings += m->savings;
1547 m->done = 1;
1548 m->match = m0;
1550 overlap:
1555 /* Clean up. */
1556 free (matched_regs);
1559 /* Returns the number of movable instructions in LOOP that were not
1560 moved outside the loop. */
1562 static int
1563 num_unmoved_movables (const struct loop *loop)
1565 int num = 0;
1566 struct movable *m;
1568 for (m = LOOP_MOVABLES (loop)->head; m; m = m->next)
1569 if (!m->done)
1570 ++num;
1572 return num;
1576 /* Return 1 if regs X and Y will become the same if moved. */
1578 static int
1579 regs_match_p (rtx x, rtx y, struct loop_movables *movables)
1581 unsigned int xn = REGNO (x);
1582 unsigned int yn = REGNO (y);
1583 struct movable *mx, *my;
1585 for (mx = movables->head; mx; mx = mx->next)
1586 if (mx->regno == xn)
1587 break;
1589 for (my = movables->head; my; my = my->next)
1590 if (my->regno == yn)
1591 break;
1593 return (mx && my
1594 && ((mx->match == my->match && mx->match != 0)
1595 || mx->match == my
1596 || mx == my->match));
1599 /* Return 1 if X and Y are identical-looking rtx's.
1600 This is the Lisp function EQUAL for rtx arguments.
1602 If two registers are matching movables or a movable register and an
1603 equivalent constant, consider them equal. */
1605 static int
1606 rtx_equal_for_loop_p (rtx x, rtx y, struct loop_movables *movables,
1607 struct loop_regs *regs)
1609 int i;
1610 int j;
1611 struct movable *m;
1612 enum rtx_code code;
1613 const char *fmt;
1615 if (x == y)
1616 return 1;
1617 if (x == 0 || y == 0)
1618 return 0;
1620 code = GET_CODE (x);
1622 /* If we have a register and a constant, they may sometimes be
1623 equal. */
1624 if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
1625 && CONSTANT_P (y))
1627 for (m = movables->head; m; m = m->next)
1628 if (m->move_insn && m->regno == REGNO (x)
1629 && rtx_equal_p (m->set_src, y))
1630 return 1;
1632 else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
1633 && CONSTANT_P (x))
1635 for (m = movables->head; m; m = m->next)
1636 if (m->move_insn && m->regno == REGNO (y)
1637 && rtx_equal_p (m->set_src, x))
1638 return 1;
1641 /* Otherwise, rtx's of different codes cannot be equal. */
1642 if (code != GET_CODE (y))
1643 return 0;
1645 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1646 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1648 if (GET_MODE (x) != GET_MODE (y))
1649 return 0;
1651 /* These three types of rtx's can be compared nonrecursively. */
1652 if (code == REG)
1653 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1655 if (code == LABEL_REF)
1656 return XEXP (x, 0) == XEXP (y, 0);
1657 if (code == SYMBOL_REF)
1658 return XSTR (x, 0) == XSTR (y, 0);
1660 /* Compare the elements. If any pair of corresponding elements
1661 fail to match, return 0 for the whole things. */
1663 fmt = GET_RTX_FORMAT (code);
1664 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1666 switch (fmt[i])
1668 case 'w':
1669 if (XWINT (x, i) != XWINT (y, i))
1670 return 0;
1671 break;
1673 case 'i':
1674 if (XINT (x, i) != XINT (y, i))
1675 return 0;
1676 break;
1678 case 'E':
1679 /* Two vectors must have the same length. */
1680 if (XVECLEN (x, i) != XVECLEN (y, i))
1681 return 0;
1683 /* And the corresponding elements must match. */
1684 for (j = 0; j < XVECLEN (x, i); j++)
1685 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
1686 movables, regs) == 0)
1687 return 0;
1688 break;
1690 case 'e':
1691 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
1692 == 0)
1693 return 0;
1694 break;
1696 case 's':
1697 if (strcmp (XSTR (x, i), XSTR (y, i)))
1698 return 0;
1699 break;
1701 case 'u':
1702 /* These are just backpointers, so they don't matter. */
1703 break;
1705 case '0':
1706 break;
1708 /* It is believed that rtx's at this level will never
1709 contain anything but integers and other rtx's,
1710 except for within LABEL_REFs and SYMBOL_REFs. */
1711 default:
1712 abort ();
1715 return 1;
1718 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1719 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1720 references is incremented once for each added note. */
1722 static void
1723 add_label_notes (rtx x, rtx insns)
1725 enum rtx_code code = GET_CODE (x);
1726 int i, j;
1727 const char *fmt;
1728 rtx insn;
1730 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1732 /* This code used to ignore labels that referred to dispatch tables to
1733 avoid flow generating (slightly) worse code.
1735 We no longer ignore such label references (see LABEL_REF handling in
1736 mark_jump_label for additional information). */
1737 for (insn = insns; insn; insn = NEXT_INSN (insn))
1738 if (reg_mentioned_p (XEXP (x, 0), insn))
1740 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
1741 REG_NOTES (insn));
1742 if (LABEL_P (XEXP (x, 0)))
1743 LABEL_NUSES (XEXP (x, 0))++;
1747 fmt = GET_RTX_FORMAT (code);
1748 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1750 if (fmt[i] == 'e')
1751 add_label_notes (XEXP (x, i), insns);
1752 else if (fmt[i] == 'E')
1753 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1754 add_label_notes (XVECEXP (x, i, j), insns);
1758 /* Scan MOVABLES, and move the insns that deserve to be moved.
1759 If two matching movables are combined, replace one reg with the
1760 other throughout. */
1762 static void
1763 move_movables (struct loop *loop, struct loop_movables *movables,
1764 int threshold, int insn_count)
1766 struct loop_regs *regs = LOOP_REGS (loop);
1767 int nregs = regs->num;
1768 rtx new_start = 0;
1769 struct movable *m;
1770 rtx p;
1771 rtx loop_start = loop->start;
1772 rtx loop_end = loop->end;
1773 /* Map of pseudo-register replacements to handle combining
1774 when we move several insns that load the same value
1775 into different pseudo-registers. */
1776 rtx *reg_map = xcalloc (nregs, sizeof (rtx));
1777 char *already_moved = xcalloc (nregs, sizeof (char));
1779 for (m = movables->head; m; m = m->next)
1781 /* Describe this movable insn. */
1783 if (loop_dump_stream)
1785 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1786 INSN_UID (m->insn), m->regno, m->lifetime);
1787 if (m->consec > 0)
1788 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1789 if (m->cond)
1790 fprintf (loop_dump_stream, "cond ");
1791 if (m->force)
1792 fprintf (loop_dump_stream, "force ");
1793 if (m->global)
1794 fprintf (loop_dump_stream, "global ");
1795 if (m->done)
1796 fprintf (loop_dump_stream, "done ");
1797 if (m->move_insn)
1798 fprintf (loop_dump_stream, "move-insn ");
1799 if (m->match)
1800 fprintf (loop_dump_stream, "matches %d ",
1801 INSN_UID (m->match->insn));
1802 if (m->forces)
1803 fprintf (loop_dump_stream, "forces %d ",
1804 INSN_UID (m->forces->insn));
1807 /* Ignore the insn if it's already done (it matched something else).
1808 Otherwise, see if it is now safe to move. */
1810 if (!m->done
1811 && (! m->cond
1812 || (1 == loop_invariant_p (loop, m->set_src)
1813 && (m->dependencies == 0
1814 || 1 == loop_invariant_p (loop, m->dependencies))
1815 && (m->consec == 0
1816 || 1 == consec_sets_invariant_p (loop, m->set_dest,
1817 m->consec + 1,
1818 m->insn))))
1819 && (! m->forces || m->forces->done))
1821 int regno;
1822 rtx p;
1823 int savings = m->savings;
1825 /* We have an insn that is safe to move.
1826 Compute its desirability. */
1828 p = m->insn;
1829 regno = m->regno;
1831 if (loop_dump_stream)
1832 fprintf (loop_dump_stream, "savings %d ", savings);
1834 if (regs->array[regno].moved_once && loop_dump_stream)
1835 fprintf (loop_dump_stream, "halved since already moved ");
1837 /* An insn MUST be moved if we already moved something else
1838 which is safe only if this one is moved too: that is,
1839 if already_moved[REGNO] is nonzero. */
1841 /* An insn is desirable to move if the new lifetime of the
1842 register is no more than THRESHOLD times the old lifetime.
1843 If it's not desirable, it means the loop is so big
1844 that moving won't speed things up much,
1845 and it is liable to make register usage worse. */
1847 /* It is also desirable to move if it can be moved at no
1848 extra cost because something else was already moved. */
1850 if (already_moved[regno]
1851 || flag_move_all_movables
1852 || (threshold * savings * m->lifetime) >=
1853 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
1854 || (m->forces && m->forces->done
1855 && regs->array[m->forces->regno].n_times_set == 1))
1857 int count;
1858 struct movable *m1;
1859 rtx first = NULL_RTX;
1860 rtx newreg = NULL_RTX;
1862 if (m->insert_temp)
1863 newreg = gen_reg_rtx (GET_MODE (m->set_dest));
1865 /* Now move the insns that set the reg. */
1867 if (m->partial && m->match)
1869 rtx newpat, i1;
1870 rtx r1, r2;
1871 /* Find the end of this chain of matching regs.
1872 Thus, we load each reg in the chain from that one reg.
1873 And that reg is loaded with 0 directly,
1874 since it has ->match == 0. */
1875 for (m1 = m; m1->match; m1 = m1->match);
1876 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1877 SET_DEST (PATTERN (m1->insn)));
1878 i1 = loop_insn_hoist (loop, newpat);
1880 /* Mark the moved, invariant reg as being allowed to
1881 share a hard reg with the other matching invariant. */
1882 REG_NOTES (i1) = REG_NOTES (m->insn);
1883 r1 = SET_DEST (PATTERN (m->insn));
1884 r2 = SET_DEST (PATTERN (m1->insn));
1885 regs_may_share
1886 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1887 gen_rtx_EXPR_LIST (VOIDmode, r2,
1888 regs_may_share));
1889 delete_insn (m->insn);
1891 if (new_start == 0)
1892 new_start = i1;
1894 if (loop_dump_stream)
1895 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1897 /* If we are to re-generate the item being moved with a
1898 new move insn, first delete what we have and then emit
1899 the move insn before the loop. */
1900 else if (m->move_insn)
1902 rtx i1, temp, seq;
1904 for (count = m->consec; count >= 0; count--)
1906 /* If this is the first insn of a library call sequence,
1907 something is very wrong. */
1908 if (GET_CODE (p) != NOTE
1909 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1910 abort ();
1912 /* If this is the last insn of a libcall sequence, then
1913 delete every insn in the sequence except the last.
1914 The last insn is handled in the normal manner. */
1915 if (GET_CODE (p) != NOTE
1916 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1918 temp = XEXP (temp, 0);
1919 while (temp != p)
1920 temp = delete_insn (temp);
1923 temp = p;
1924 p = delete_insn (p);
1926 /* simplify_giv_expr expects that it can walk the insns
1927 at m->insn forwards and see this old sequence we are
1928 tossing here. delete_insn does preserve the next
1929 pointers, but when we skip over a NOTE we must fix
1930 it up. Otherwise that code walks into the non-deleted
1931 insn stream. */
1932 while (p && GET_CODE (p) == NOTE)
1933 p = NEXT_INSN (temp) = NEXT_INSN (p);
1935 if (m->insert_temp)
1937 /* Replace the original insn with a move from
1938 our newly created temp. */
1939 start_sequence ();
1940 emit_move_insn (m->set_dest, newreg);
1941 seq = get_insns ();
1942 end_sequence ();
1943 emit_insn_before (seq, p);
1947 start_sequence ();
1948 emit_move_insn (m->insert_temp ? newreg : m->set_dest,
1949 m->set_src);
1950 seq = get_insns ();
1951 end_sequence ();
1953 add_label_notes (m->set_src, seq);
1955 i1 = loop_insn_hoist (loop, seq);
1956 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1957 set_unique_reg_note (i1,
1958 m->is_equiv ? REG_EQUIV : REG_EQUAL,
1959 m->set_src);
1961 if (loop_dump_stream)
1962 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1964 /* The more regs we move, the less we like moving them. */
1965 threshold -= 3;
1967 else
1969 for (count = m->consec; count >= 0; count--)
1971 rtx i1, temp;
1973 /* If first insn of libcall sequence, skip to end. */
1974 /* Do this at start of loop, since p is guaranteed to
1975 be an insn here. */
1976 if (GET_CODE (p) != NOTE
1977 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1978 p = XEXP (temp, 0);
1980 /* If last insn of libcall sequence, move all
1981 insns except the last before the loop. The last
1982 insn is handled in the normal manner. */
1983 if (GET_CODE (p) != NOTE
1984 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1986 rtx fn_address = 0;
1987 rtx fn_reg = 0;
1988 rtx fn_address_insn = 0;
1990 first = 0;
1991 for (temp = XEXP (temp, 0); temp != p;
1992 temp = NEXT_INSN (temp))
1994 rtx body;
1995 rtx n;
1996 rtx next;
1998 if (GET_CODE (temp) == NOTE)
1999 continue;
2001 body = PATTERN (temp);
2003 /* Find the next insn after TEMP,
2004 not counting USE or NOTE insns. */
2005 for (next = NEXT_INSN (temp); next != p;
2006 next = NEXT_INSN (next))
2007 if (! (GET_CODE (next) == INSN
2008 && GET_CODE (PATTERN (next)) == USE)
2009 && GET_CODE (next) != NOTE)
2010 break;
2012 /* If that is the call, this may be the insn
2013 that loads the function address.
2015 Extract the function address from the insn
2016 that loads it into a register.
2017 If this insn was cse'd, we get incorrect code.
2019 So emit a new move insn that copies the
2020 function address into the register that the
2021 call insn will use. flow.c will delete any
2022 redundant stores that we have created. */
2023 if (GET_CODE (next) == CALL_INSN
2024 && GET_CODE (body) == SET
2025 && GET_CODE (SET_DEST (body)) == REG
2026 && (n = find_reg_note (temp, REG_EQUAL,
2027 NULL_RTX)))
2029 fn_reg = SET_SRC (body);
2030 if (GET_CODE (fn_reg) != REG)
2031 fn_reg = SET_DEST (body);
2032 fn_address = XEXP (n, 0);
2033 fn_address_insn = temp;
2035 /* We have the call insn.
2036 If it uses the register we suspect it might,
2037 load it with the correct address directly. */
2038 if (GET_CODE (temp) == CALL_INSN
2039 && fn_address != 0
2040 && reg_referenced_p (fn_reg, body))
2041 loop_insn_emit_after (loop, 0, fn_address_insn,
2042 gen_move_insn
2043 (fn_reg, fn_address));
2045 if (GET_CODE (temp) == CALL_INSN)
2047 i1 = loop_call_insn_hoist (loop, body);
2048 /* Because the USAGE information potentially
2049 contains objects other than hard registers
2050 we need to copy it. */
2051 if (CALL_INSN_FUNCTION_USAGE (temp))
2052 CALL_INSN_FUNCTION_USAGE (i1)
2053 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
2055 else
2056 i1 = loop_insn_hoist (loop, body);
2057 if (first == 0)
2058 first = i1;
2059 if (temp == fn_address_insn)
2060 fn_address_insn = i1;
2061 REG_NOTES (i1) = REG_NOTES (temp);
2062 REG_NOTES (temp) = NULL;
2063 delete_insn (temp);
2065 if (new_start == 0)
2066 new_start = first;
2068 if (m->savemode != VOIDmode)
2070 /* P sets REG to zero; but we should clear only
2071 the bits that are not covered by the mode
2072 m->savemode. */
2073 rtx reg = m->set_dest;
2074 rtx sequence;
2075 rtx tem;
2077 start_sequence ();
2078 tem = expand_simple_binop
2079 (GET_MODE (reg), AND, reg,
2080 GEN_INT ((((HOST_WIDE_INT) 1
2081 << GET_MODE_BITSIZE (m->savemode)))
2082 - 1),
2083 reg, 1, OPTAB_LIB_WIDEN);
2084 if (tem == 0)
2085 abort ();
2086 if (tem != reg)
2087 emit_move_insn (reg, tem);
2088 sequence = get_insns ();
2089 end_sequence ();
2090 i1 = loop_insn_hoist (loop, sequence);
2092 else if (GET_CODE (p) == CALL_INSN)
2094 i1 = loop_call_insn_hoist (loop, PATTERN (p));
2095 /* Because the USAGE information potentially
2096 contains objects other than hard registers
2097 we need to copy it. */
2098 if (CALL_INSN_FUNCTION_USAGE (p))
2099 CALL_INSN_FUNCTION_USAGE (i1)
2100 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2102 else if (count == m->consec && m->move_insn_first)
2104 rtx seq;
2105 /* The SET_SRC might not be invariant, so we must
2106 use the REG_EQUAL note. */
2107 start_sequence ();
2108 emit_move_insn (m->set_dest, m->set_src);
2109 seq = get_insns ();
2110 end_sequence ();
2112 add_label_notes (m->set_src, seq);
2114 i1 = loop_insn_hoist (loop, seq);
2115 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2116 set_unique_reg_note (i1, m->is_equiv ? REG_EQUIV
2117 : REG_EQUAL, m->set_src);
2119 else if (m->insert_temp)
2121 rtx *reg_map2 = xcalloc (REGNO (newreg),
2122 sizeof(rtx));
2123 reg_map2 [m->regno] = newreg;
2125 i1 = loop_insn_hoist (loop, copy_rtx (PATTERN (p)));
2126 replace_regs (i1, reg_map2, REGNO (newreg), 1);
2127 free (reg_map2);
2129 else
2130 i1 = loop_insn_hoist (loop, PATTERN (p));
2132 if (REG_NOTES (i1) == 0)
2134 REG_NOTES (i1) = REG_NOTES (p);
2135 REG_NOTES (p) = NULL;
2137 /* If there is a REG_EQUAL note present whose value
2138 is not loop invariant, then delete it, since it
2139 may cause problems with later optimization passes.
2140 It is possible for cse to create such notes
2141 like this as a result of record_jump_cond. */
2143 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2144 && ! loop_invariant_p (loop, XEXP (temp, 0)))
2145 remove_note (i1, temp);
2148 if (new_start == 0)
2149 new_start = i1;
2151 if (loop_dump_stream)
2152 fprintf (loop_dump_stream, " moved to %d",
2153 INSN_UID (i1));
2155 /* If library call, now fix the REG_NOTES that contain
2156 insn pointers, namely REG_LIBCALL on FIRST
2157 and REG_RETVAL on I1. */
2158 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2160 XEXP (temp, 0) = first;
2161 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2162 XEXP (temp, 0) = i1;
2165 temp = p;
2166 delete_insn (p);
2167 p = NEXT_INSN (p);
2169 /* simplify_giv_expr expects that it can walk the insns
2170 at m->insn forwards and see this old sequence we are
2171 tossing here. delete_insn does preserve the next
2172 pointers, but when we skip over a NOTE we must fix
2173 it up. Otherwise that code walks into the non-deleted
2174 insn stream. */
2175 while (p && GET_CODE (p) == NOTE)
2176 p = NEXT_INSN (temp) = NEXT_INSN (p);
2178 if (m->insert_temp)
2180 rtx seq;
2181 /* Replace the original insn with a move from
2182 our newly created temp. */
2183 start_sequence ();
2184 emit_move_insn (m->set_dest, newreg);
2185 seq = get_insns ();
2186 end_sequence ();
2187 emit_insn_before (seq, p);
2191 /* The more regs we move, the less we like moving them. */
2192 threshold -= 3;
2195 m->done = 1;
2197 if (!m->insert_temp)
2199 /* Any other movable that loads the same register
2200 MUST be moved. */
2201 already_moved[regno] = 1;
2203 /* This reg has been moved out of one loop. */
2204 regs->array[regno].moved_once = 1;
2206 /* The reg set here is now invariant. */
2207 if (! m->partial)
2209 int i;
2210 for (i = 0; i < LOOP_REGNO_NREGS (regno, m->set_dest); i++)
2211 regs->array[regno+i].set_in_loop = 0;
2214 /* Change the length-of-life info for the register
2215 to say it lives at least the full length of this loop.
2216 This will help guide optimizations in outer loops. */
2218 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2219 /* This is the old insn before all the moved insns.
2220 We can't use the moved insn because it is out of range
2221 in uid_luid. Only the old insns have luids. */
2222 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2223 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2224 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2227 /* Combine with this moved insn any other matching movables. */
2229 if (! m->partial)
2230 for (m1 = movables->head; m1; m1 = m1->next)
2231 if (m1->match == m)
2233 rtx temp;
2235 /* Schedule the reg loaded by M1
2236 for replacement so that shares the reg of M.
2237 If the modes differ (only possible in restricted
2238 circumstances, make a SUBREG.
2240 Note this assumes that the target dependent files
2241 treat REG and SUBREG equally, including within
2242 GO_IF_LEGITIMATE_ADDRESS and in all the
2243 predicates since we never verify that replacing the
2244 original register with a SUBREG results in a
2245 recognizable insn. */
2246 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2247 reg_map[m1->regno] = m->set_dest;
2248 else
2249 reg_map[m1->regno]
2250 = gen_lowpart_common (GET_MODE (m1->set_dest),
2251 m->set_dest);
2253 /* Get rid of the matching insn
2254 and prevent further processing of it. */
2255 m1->done = 1;
2257 /* If library call, delete all insns. */
2258 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2259 NULL_RTX)))
2260 delete_insn_chain (XEXP (temp, 0), m1->insn);
2261 else
2262 delete_insn (m1->insn);
2264 /* Any other movable that loads the same register
2265 MUST be moved. */
2266 already_moved[m1->regno] = 1;
2268 /* The reg merged here is now invariant,
2269 if the reg it matches is invariant. */
2270 if (! m->partial)
2272 int i;
2273 for (i = 0;
2274 i < LOOP_REGNO_NREGS (regno, m1->set_dest);
2275 i++)
2276 regs->array[m1->regno+i].set_in_loop = 0;
2280 else if (loop_dump_stream)
2281 fprintf (loop_dump_stream, "not desirable");
2283 else if (loop_dump_stream && !m->match)
2284 fprintf (loop_dump_stream, "not safe");
2286 if (loop_dump_stream)
2287 fprintf (loop_dump_stream, "\n");
2290 if (new_start == 0)
2291 new_start = loop_start;
2293 /* Go through all the instructions in the loop, making
2294 all the register substitutions scheduled in REG_MAP. */
2295 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2296 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2297 || GET_CODE (p) == CALL_INSN)
2299 replace_regs (PATTERN (p), reg_map, nregs, 0);
2300 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2301 INSN_CODE (p) = -1;
2304 /* Clean up. */
2305 free (reg_map);
2306 free (already_moved);
2310 static void
2311 loop_movables_add (struct loop_movables *movables, struct movable *m)
2313 if (movables->head == 0)
2314 movables->head = m;
2315 else
2316 movables->last->next = m;
2317 movables->last = m;
2321 static void
2322 loop_movables_free (struct loop_movables *movables)
2324 struct movable *m;
2325 struct movable *m_next;
2327 for (m = movables->head; m; m = m_next)
2329 m_next = m->next;
2330 free (m);
2334 #if 0
2335 /* Scan X and replace the address of any MEM in it with ADDR.
2336 REG is the address that MEM should have before the replacement. */
2338 static void
2339 replace_call_address (rtx x, rtx reg, rtx addr)
2341 enum rtx_code code;
2342 int i;
2343 const char *fmt;
2345 if (x == 0)
2346 return;
2347 code = GET_CODE (x);
2348 switch (code)
2350 case PC:
2351 case CC0:
2352 case CONST_INT:
2353 case CONST_DOUBLE:
2354 case CONST:
2355 case SYMBOL_REF:
2356 case LABEL_REF:
2357 case REG:
2358 return;
2360 case SET:
2361 /* Short cut for very common case. */
2362 replace_call_address (XEXP (x, 1), reg, addr);
2363 return;
2365 case CALL:
2366 /* Short cut for very common case. */
2367 replace_call_address (XEXP (x, 0), reg, addr);
2368 return;
2370 case MEM:
2371 /* If this MEM uses a reg other than the one we expected,
2372 something is wrong. */
2373 if (XEXP (x, 0) != reg)
2374 abort ();
2375 XEXP (x, 0) = addr;
2376 return;
2378 default:
2379 break;
2382 fmt = GET_RTX_FORMAT (code);
2383 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2385 if (fmt[i] == 'e')
2386 replace_call_address (XEXP (x, i), reg, addr);
2387 else if (fmt[i] == 'E')
2389 int j;
2390 for (j = 0; j < XVECLEN (x, i); j++)
2391 replace_call_address (XVECEXP (x, i, j), reg, addr);
2395 #endif
2397 /* Return the number of memory refs to addresses that vary
2398 in the rtx X. */
2400 static int
2401 count_nonfixed_reads (const struct loop *loop, rtx x)
2403 enum rtx_code code;
2404 int i;
2405 const char *fmt;
2406 int value;
2408 if (x == 0)
2409 return 0;
2411 code = GET_CODE (x);
2412 switch (code)
2414 case PC:
2415 case CC0:
2416 case CONST_INT:
2417 case CONST_DOUBLE:
2418 case CONST:
2419 case SYMBOL_REF:
2420 case LABEL_REF:
2421 case REG:
2422 return 0;
2424 case MEM:
2425 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2426 + count_nonfixed_reads (loop, XEXP (x, 0)));
2428 default:
2429 break;
2432 value = 0;
2433 fmt = GET_RTX_FORMAT (code);
2434 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2436 if (fmt[i] == 'e')
2437 value += count_nonfixed_reads (loop, XEXP (x, i));
2438 if (fmt[i] == 'E')
2440 int j;
2441 for (j = 0; j < XVECLEN (x, i); j++)
2442 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2445 return value;
2448 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2449 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2450 `unknown_address_altered', `unknown_constant_address_altered', and
2451 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2452 list `store_mems' in LOOP. */
2454 static void
2455 prescan_loop (struct loop *loop)
2457 int level = 1;
2458 rtx insn;
2459 struct loop_info *loop_info = LOOP_INFO (loop);
2460 rtx start = loop->start;
2461 rtx end = loop->end;
2462 /* The label after END. Jumping here is just like falling off the
2463 end of the loop. We use next_nonnote_insn instead of next_label
2464 as a hedge against the (pathological) case where some actual insn
2465 might end up between the two. */
2466 rtx exit_target = next_nonnote_insn (end);
2468 loop_info->has_indirect_jump = indirect_jump_in_function;
2469 loop_info->pre_header_has_call = 0;
2470 loop_info->has_call = 0;
2471 loop_info->has_nonconst_call = 0;
2472 loop_info->has_prefetch = 0;
2473 loop_info->has_volatile = 0;
2474 loop_info->has_tablejump = 0;
2475 loop_info->has_multiple_exit_targets = 0;
2476 loop->level = 1;
2478 loop_info->unknown_address_altered = 0;
2479 loop_info->unknown_constant_address_altered = 0;
2480 loop_info->store_mems = NULL_RTX;
2481 loop_info->first_loop_store_insn = NULL_RTX;
2482 loop_info->mems_idx = 0;
2483 loop_info->num_mem_sets = 0;
2484 /* If loop opts run twice, this was set on 1st pass for 2nd. */
2485 loop_info->preconditioned = NOTE_PRECONDITIONED (end);
2487 for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
2488 insn = PREV_INSN (insn))
2490 if (GET_CODE (insn) == CALL_INSN)
2492 loop_info->pre_header_has_call = 1;
2493 break;
2497 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2498 insn = NEXT_INSN (insn))
2500 switch (GET_CODE (insn))
2502 case NOTE:
2503 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2505 ++level;
2506 /* Count number of loops contained in this one. */
2507 loop->level++;
2509 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2510 --level;
2511 break;
2513 case CALL_INSN:
2514 if (! CONST_OR_PURE_CALL_P (insn))
2516 loop_info->unknown_address_altered = 1;
2517 loop_info->has_nonconst_call = 1;
2519 else if (pure_call_p (insn))
2520 loop_info->has_nonconst_call = 1;
2521 loop_info->has_call = 1;
2522 if (can_throw_internal (insn))
2523 loop_info->has_multiple_exit_targets = 1;
2525 /* Calls initializing constant objects have CLOBBER of MEM /u in the
2526 attached FUNCTION_USAGE expression list, not accounted for by the
2527 code above. We should note these to avoid missing dependencies in
2528 later references. */
2530 rtx fusage_entry;
2532 for (fusage_entry = CALL_INSN_FUNCTION_USAGE (insn);
2533 fusage_entry; fusage_entry = XEXP (fusage_entry, 1))
2535 rtx fusage = XEXP (fusage_entry, 0);
2537 if (GET_CODE (fusage) == CLOBBER
2538 && GET_CODE (XEXP (fusage, 0)) == MEM
2539 && RTX_UNCHANGING_P (XEXP (fusage, 0)))
2541 note_stores (fusage, note_addr_stored, loop_info);
2542 if (! loop_info->first_loop_store_insn
2543 && loop_info->store_mems)
2544 loop_info->first_loop_store_insn = insn;
2548 break;
2550 case JUMP_INSN:
2551 if (! loop_info->has_multiple_exit_targets)
2553 rtx set = pc_set (insn);
2555 if (set)
2557 rtx src = SET_SRC (set);
2558 rtx label1, label2;
2560 if (GET_CODE (src) == IF_THEN_ELSE)
2562 label1 = XEXP (src, 1);
2563 label2 = XEXP (src, 2);
2565 else
2567 label1 = src;
2568 label2 = NULL_RTX;
2573 if (label1 && label1 != pc_rtx)
2575 if (GET_CODE (label1) != LABEL_REF)
2577 /* Something tricky. */
2578 loop_info->has_multiple_exit_targets = 1;
2579 break;
2581 else if (XEXP (label1, 0) != exit_target
2582 && LABEL_OUTSIDE_LOOP_P (label1))
2584 /* A jump outside the current loop. */
2585 loop_info->has_multiple_exit_targets = 1;
2586 break;
2590 label1 = label2;
2591 label2 = NULL_RTX;
2593 while (label1);
2595 else
2597 /* A return, or something tricky. */
2598 loop_info->has_multiple_exit_targets = 1;
2601 /* FALLTHRU */
2603 case INSN:
2604 if (volatile_refs_p (PATTERN (insn)))
2605 loop_info->has_volatile = 1;
2607 if (GET_CODE (insn) == JUMP_INSN
2608 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2609 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2610 loop_info->has_tablejump = 1;
2612 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2613 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2614 loop_info->first_loop_store_insn = insn;
2616 if (flag_non_call_exceptions && can_throw_internal (insn))
2617 loop_info->has_multiple_exit_targets = 1;
2618 break;
2620 default:
2621 break;
2625 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2626 if (/* An exception thrown by a called function might land us
2627 anywhere. */
2628 ! loop_info->has_nonconst_call
2629 /* We don't want loads for MEMs moved to a location before the
2630 one at which their stack memory becomes allocated. (Note
2631 that this is not a problem for malloc, etc., since those
2632 require actual function calls. */
2633 && ! current_function_calls_alloca
2634 /* There are ways to leave the loop other than falling off the
2635 end. */
2636 && ! loop_info->has_multiple_exit_targets)
2637 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2638 insn = NEXT_INSN (insn))
2639 for_each_rtx (&insn, insert_loop_mem, loop_info);
2641 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2642 that loop_invariant_p and load_mems can use true_dependence
2643 to determine what is really clobbered. */
2644 if (loop_info->unknown_address_altered)
2646 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2648 loop_info->store_mems
2649 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2651 if (loop_info->unknown_constant_address_altered)
2653 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2655 RTX_UNCHANGING_P (mem) = 1;
2656 loop_info->store_mems
2657 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2661 /* Invalidate all loops containing LABEL. */
2663 static void
2664 invalidate_loops_containing_label (rtx label)
2666 struct loop *loop;
2667 for (loop = uid_loop[INSN_UID (label)]; loop; loop = loop->outer)
2668 loop->invalid = 1;
2671 /* Scan the function looking for loops. Record the start and end of each loop.
2672 Also mark as invalid loops any loops that contain a setjmp or are branched
2673 to from outside the loop. */
2675 static void
2676 find_and_verify_loops (rtx f, struct loops *loops)
2678 rtx insn;
2679 rtx label;
2680 int num_loops;
2681 struct loop *current_loop;
2682 struct loop *next_loop;
2683 struct loop *loop;
2685 num_loops = loops->num;
2687 compute_luids (f, NULL_RTX, 0);
2689 /* If there are jumps to undefined labels,
2690 treat them as jumps out of any/all loops.
2691 This also avoids writing past end of tables when there are no loops. */
2692 uid_loop[0] = NULL;
2694 /* Find boundaries of loops, mark which loops are contained within
2695 loops, and invalidate loops that have setjmp. */
2697 num_loops = 0;
2698 current_loop = NULL;
2699 for (insn = f; insn; insn = NEXT_INSN (insn))
2701 if (GET_CODE (insn) == NOTE)
2702 switch (NOTE_LINE_NUMBER (insn))
2704 case NOTE_INSN_LOOP_BEG:
2705 next_loop = loops->array + num_loops;
2706 next_loop->num = num_loops;
2707 num_loops++;
2708 next_loop->start = insn;
2709 next_loop->outer = current_loop;
2710 current_loop = next_loop;
2711 break;
2713 case NOTE_INSN_LOOP_CONT:
2714 current_loop->cont = insn;
2715 break;
2717 case NOTE_INSN_LOOP_VTOP:
2718 current_loop->vtop = insn;
2719 break;
2721 case NOTE_INSN_LOOP_END:
2722 if (! current_loop)
2723 abort ();
2725 current_loop->end = insn;
2726 current_loop = current_loop->outer;
2727 break;
2729 default:
2730 break;
2733 if (GET_CODE (insn) == CALL_INSN
2734 && find_reg_note (insn, REG_SETJMP, NULL))
2736 /* In this case, we must invalidate our current loop and any
2737 enclosing loop. */
2738 for (loop = current_loop; loop; loop = loop->outer)
2740 loop->invalid = 1;
2741 if (loop_dump_stream)
2742 fprintf (loop_dump_stream,
2743 "\nLoop at %d ignored due to setjmp.\n",
2744 INSN_UID (loop->start));
2748 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2749 enclosing loop, but this doesn't matter. */
2750 uid_loop[INSN_UID (insn)] = current_loop;
2753 /* Any loop containing a label used in an initializer must be invalidated,
2754 because it can be jumped into from anywhere. */
2755 for (label = forced_labels; label; label = XEXP (label, 1))
2756 invalidate_loops_containing_label (XEXP (label, 0));
2758 /* Any loop containing a label used for an exception handler must be
2759 invalidated, because it can be jumped into from anywhere. */
2760 for_each_eh_label (invalidate_loops_containing_label);
2762 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2763 loop that it is not contained within, that loop is marked invalid.
2764 If any INSN or CALL_INSN uses a label's address, then the loop containing
2765 that label is marked invalid, because it could be jumped into from
2766 anywhere.
2768 Also look for blocks of code ending in an unconditional branch that
2769 exits the loop. If such a block is surrounded by a conditional
2770 branch around the block, move the block elsewhere (see below) and
2771 invert the jump to point to the code block. This may eliminate a
2772 label in our loop and will simplify processing by both us and a
2773 possible second cse pass. */
2775 for (insn = f; insn; insn = NEXT_INSN (insn))
2776 if (INSN_P (insn))
2778 struct loop *this_loop = uid_loop[INSN_UID (insn)];
2780 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2782 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2783 if (note)
2784 invalidate_loops_containing_label (XEXP (note, 0));
2787 if (GET_CODE (insn) != JUMP_INSN)
2788 continue;
2790 mark_loop_jump (PATTERN (insn), this_loop);
2792 /* See if this is an unconditional branch outside the loop. */
2793 if (this_loop
2794 && (GET_CODE (PATTERN (insn)) == RETURN
2795 || (any_uncondjump_p (insn)
2796 && onlyjump_p (insn)
2797 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2798 != this_loop)))
2799 && get_max_uid () < max_uid_for_loop)
2801 rtx p;
2802 rtx our_next = next_real_insn (insn);
2803 rtx last_insn_to_move = NEXT_INSN (insn);
2804 struct loop *dest_loop;
2805 struct loop *outer_loop = NULL;
2807 /* Go backwards until we reach the start of the loop, a label,
2808 or a JUMP_INSN. */
2809 for (p = PREV_INSN (insn);
2810 GET_CODE (p) != CODE_LABEL
2811 && ! (GET_CODE (p) == NOTE
2812 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2813 && GET_CODE (p) != JUMP_INSN;
2814 p = PREV_INSN (p))
2817 /* Check for the case where we have a jump to an inner nested
2818 loop, and do not perform the optimization in that case. */
2820 if (JUMP_LABEL (insn))
2822 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2823 if (dest_loop)
2825 for (outer_loop = dest_loop; outer_loop;
2826 outer_loop = outer_loop->outer)
2827 if (outer_loop == this_loop)
2828 break;
2832 /* Make sure that the target of P is within the current loop. */
2834 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2835 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2836 outer_loop = this_loop;
2838 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2839 we have a block of code to try to move.
2841 We look backward and then forward from the target of INSN
2842 to find a BARRIER at the same loop depth as the target.
2843 If we find such a BARRIER, we make a new label for the start
2844 of the block, invert the jump in P and point it to that label,
2845 and move the block of code to the spot we found. */
2847 if (! outer_loop
2848 && GET_CODE (p) == JUMP_INSN
2849 && JUMP_LABEL (p) != 0
2850 /* Just ignore jumps to labels that were never emitted.
2851 These always indicate compilation errors. */
2852 && INSN_UID (JUMP_LABEL (p)) != 0
2853 && any_condjump_p (p) && onlyjump_p (p)
2854 && next_real_insn (JUMP_LABEL (p)) == our_next
2855 /* If it's not safe to move the sequence, then we
2856 mustn't try. */
2857 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2858 &last_insn_to_move))
2860 rtx target
2861 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2862 struct loop *target_loop = uid_loop[INSN_UID (target)];
2863 rtx loc, loc2;
2864 rtx tmp;
2866 /* Search for possible garbage past the conditional jumps
2867 and look for the last barrier. */
2868 for (tmp = last_insn_to_move;
2869 tmp && GET_CODE (tmp) != CODE_LABEL; tmp = NEXT_INSN (tmp))
2870 if (GET_CODE (tmp) == BARRIER)
2871 last_insn_to_move = tmp;
2873 for (loc = target; loc; loc = PREV_INSN (loc))
2874 if (GET_CODE (loc) == BARRIER
2875 /* Don't move things inside a tablejump. */
2876 && ((loc2 = next_nonnote_insn (loc)) == 0
2877 || GET_CODE (loc2) != CODE_LABEL
2878 || (loc2 = next_nonnote_insn (loc2)) == 0
2879 || GET_CODE (loc2) != JUMP_INSN
2880 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2881 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2882 && uid_loop[INSN_UID (loc)] == target_loop)
2883 break;
2885 if (loc == 0)
2886 for (loc = target; loc; loc = NEXT_INSN (loc))
2887 if (GET_CODE (loc) == BARRIER
2888 /* Don't move things inside a tablejump. */
2889 && ((loc2 = next_nonnote_insn (loc)) == 0
2890 || GET_CODE (loc2) != CODE_LABEL
2891 || (loc2 = next_nonnote_insn (loc2)) == 0
2892 || GET_CODE (loc2) != JUMP_INSN
2893 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2894 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2895 && uid_loop[INSN_UID (loc)] == target_loop)
2896 break;
2898 if (loc)
2900 rtx cond_label = JUMP_LABEL (p);
2901 rtx new_label = get_label_after (p);
2903 /* Ensure our label doesn't go away. */
2904 LABEL_NUSES (cond_label)++;
2906 /* Verify that uid_loop is large enough and that
2907 we can invert P. */
2908 if (invert_jump (p, new_label, 1))
2910 rtx q, r;
2912 /* If no suitable BARRIER was found, create a suitable
2913 one before TARGET. Since TARGET is a fall through
2914 path, we'll need to insert a jump around our block
2915 and add a BARRIER before TARGET.
2917 This creates an extra unconditional jump outside
2918 the loop. However, the benefits of removing rarely
2919 executed instructions from inside the loop usually
2920 outweighs the cost of the extra unconditional jump
2921 outside the loop. */
2922 if (loc == 0)
2924 rtx temp;
2926 temp = gen_jump (JUMP_LABEL (insn));
2927 temp = emit_jump_insn_before (temp, target);
2928 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2929 LABEL_NUSES (JUMP_LABEL (insn))++;
2930 loc = emit_barrier_before (target);
2933 /* Include the BARRIER after INSN and copy the
2934 block after LOC. */
2935 if (squeeze_notes (&new_label, &last_insn_to_move))
2936 abort ();
2937 reorder_insns (new_label, last_insn_to_move, loc);
2939 /* All those insns are now in TARGET_LOOP. */
2940 for (q = new_label;
2941 q != NEXT_INSN (last_insn_to_move);
2942 q = NEXT_INSN (q))
2943 uid_loop[INSN_UID (q)] = target_loop;
2945 /* The label jumped to by INSN is no longer a loop
2946 exit. Unless INSN does not have a label (e.g.,
2947 it is a RETURN insn), search loop->exit_labels
2948 to find its label_ref, and remove it. Also turn
2949 off LABEL_OUTSIDE_LOOP_P bit. */
2950 if (JUMP_LABEL (insn))
2952 for (q = 0, r = this_loop->exit_labels;
2954 q = r, r = LABEL_NEXTREF (r))
2955 if (XEXP (r, 0) == JUMP_LABEL (insn))
2957 LABEL_OUTSIDE_LOOP_P (r) = 0;
2958 if (q)
2959 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2960 else
2961 this_loop->exit_labels = LABEL_NEXTREF (r);
2962 break;
2965 for (loop = this_loop; loop && loop != target_loop;
2966 loop = loop->outer)
2967 loop->exit_count--;
2969 /* If we didn't find it, then something is
2970 wrong. */
2971 if (! r)
2972 abort ();
2975 /* P is now a jump outside the loop, so it must be put
2976 in loop->exit_labels, and marked as such.
2977 The easiest way to do this is to just call
2978 mark_loop_jump again for P. */
2979 mark_loop_jump (PATTERN (p), this_loop);
2981 /* If INSN now jumps to the insn after it,
2982 delete INSN. */
2983 if (JUMP_LABEL (insn) != 0
2984 && (next_real_insn (JUMP_LABEL (insn))
2985 == next_real_insn (insn)))
2986 delete_related_insns (insn);
2989 /* Continue the loop after where the conditional
2990 branch used to jump, since the only branch insn
2991 in the block (if it still remains) is an inter-loop
2992 branch and hence needs no processing. */
2993 insn = NEXT_INSN (cond_label);
2995 if (--LABEL_NUSES (cond_label) == 0)
2996 delete_related_insns (cond_label);
2998 /* This loop will be continued with NEXT_INSN (insn). */
2999 insn = PREV_INSN (insn);
3006 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
3007 loops it is contained in, mark the target loop invalid.
3009 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
3011 static void
3012 mark_loop_jump (rtx x, struct loop *loop)
3014 struct loop *dest_loop;
3015 struct loop *outer_loop;
3016 int i;
3018 switch (GET_CODE (x))
3020 case PC:
3021 case USE:
3022 case CLOBBER:
3023 case REG:
3024 case MEM:
3025 case CONST_INT:
3026 case CONST_DOUBLE:
3027 case RETURN:
3028 return;
3030 case CONST:
3031 /* There could be a label reference in here. */
3032 mark_loop_jump (XEXP (x, 0), loop);
3033 return;
3035 case PLUS:
3036 case MINUS:
3037 case MULT:
3038 mark_loop_jump (XEXP (x, 0), loop);
3039 mark_loop_jump (XEXP (x, 1), loop);
3040 return;
3042 case LO_SUM:
3043 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3044 mark_loop_jump (XEXP (x, 1), loop);
3045 return;
3047 case SIGN_EXTEND:
3048 case ZERO_EXTEND:
3049 mark_loop_jump (XEXP (x, 0), loop);
3050 return;
3052 case LABEL_REF:
3053 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3055 /* Link together all labels that branch outside the loop. This
3056 is used by final_[bg]iv_value and the loop unrolling code. Also
3057 mark this LABEL_REF so we know that this branch should predict
3058 false. */
3060 /* A check to make sure the label is not in an inner nested loop,
3061 since this does not count as a loop exit. */
3062 if (dest_loop)
3064 for (outer_loop = dest_loop; outer_loop;
3065 outer_loop = outer_loop->outer)
3066 if (outer_loop == loop)
3067 break;
3069 else
3070 outer_loop = NULL;
3072 if (loop && ! outer_loop)
3074 LABEL_OUTSIDE_LOOP_P (x) = 1;
3075 LABEL_NEXTREF (x) = loop->exit_labels;
3076 loop->exit_labels = x;
3078 for (outer_loop = loop;
3079 outer_loop && outer_loop != dest_loop;
3080 outer_loop = outer_loop->outer)
3081 outer_loop->exit_count++;
3084 /* If this is inside a loop, but not in the current loop or one enclosed
3085 by it, it invalidates at least one loop. */
3087 if (! dest_loop)
3088 return;
3090 /* We must invalidate every nested loop containing the target of this
3091 label, except those that also contain the jump insn. */
3093 for (; dest_loop; dest_loop = dest_loop->outer)
3095 /* Stop when we reach a loop that also contains the jump insn. */
3096 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3097 if (dest_loop == outer_loop)
3098 return;
3100 /* If we get here, we know we need to invalidate a loop. */
3101 if (loop_dump_stream && ! dest_loop->invalid)
3102 fprintf (loop_dump_stream,
3103 "\nLoop at %d ignored due to multiple entry points.\n",
3104 INSN_UID (dest_loop->start));
3106 dest_loop->invalid = 1;
3108 return;
3110 case SET:
3111 /* If this is not setting pc, ignore. */
3112 if (SET_DEST (x) == pc_rtx)
3113 mark_loop_jump (SET_SRC (x), loop);
3114 return;
3116 case IF_THEN_ELSE:
3117 mark_loop_jump (XEXP (x, 1), loop);
3118 mark_loop_jump (XEXP (x, 2), loop);
3119 return;
3121 case PARALLEL:
3122 case ADDR_VEC:
3123 for (i = 0; i < XVECLEN (x, 0); i++)
3124 mark_loop_jump (XVECEXP (x, 0, i), loop);
3125 return;
3127 case ADDR_DIFF_VEC:
3128 for (i = 0; i < XVECLEN (x, 1); i++)
3129 mark_loop_jump (XVECEXP (x, 1, i), loop);
3130 return;
3132 default:
3133 /* Strictly speaking this is not a jump into the loop, only a possible
3134 jump out of the loop. However, we have no way to link the destination
3135 of this jump onto the list of exit labels. To be safe we mark this
3136 loop and any containing loops as invalid. */
3137 if (loop)
3139 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3141 if (loop_dump_stream && ! outer_loop->invalid)
3142 fprintf (loop_dump_stream,
3143 "\nLoop at %d ignored due to unknown exit jump.\n",
3144 INSN_UID (outer_loop->start));
3145 outer_loop->invalid = 1;
3148 return;
3152 /* Return nonzero if there is a label in the range from
3153 insn INSN to and including the insn whose luid is END
3154 INSN must have an assigned luid (i.e., it must not have
3155 been previously created by loop.c). */
3157 static int
3158 labels_in_range_p (rtx insn, int end)
3160 while (insn && INSN_LUID (insn) <= end)
3162 if (GET_CODE (insn) == CODE_LABEL)
3163 return 1;
3164 insn = NEXT_INSN (insn);
3167 return 0;
3170 /* Record that a memory reference X is being set. */
3172 static void
3173 note_addr_stored (rtx x, rtx y ATTRIBUTE_UNUSED,
3174 void *data ATTRIBUTE_UNUSED)
3176 struct loop_info *loop_info = data;
3178 if (x == 0 || GET_CODE (x) != MEM)
3179 return;
3181 /* Count number of memory writes.
3182 This affects heuristics in strength_reduce. */
3183 loop_info->num_mem_sets++;
3185 /* BLKmode MEM means all memory is clobbered. */
3186 if (GET_MODE (x) == BLKmode)
3188 if (RTX_UNCHANGING_P (x))
3189 loop_info->unknown_constant_address_altered = 1;
3190 else
3191 loop_info->unknown_address_altered = 1;
3193 return;
3196 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
3197 loop_info->store_mems);
3200 /* X is a value modified by an INSN that references a biv inside a loop
3201 exit test (ie, X is somehow related to the value of the biv). If X
3202 is a pseudo that is used more than once, then the biv is (effectively)
3203 used more than once. DATA is a pointer to a loop_regs structure. */
3205 static void
3206 note_set_pseudo_multiple_uses (rtx x, rtx y ATTRIBUTE_UNUSED, void *data)
3208 struct loop_regs *regs = (struct loop_regs *) data;
3210 if (x == 0)
3211 return;
3213 while (GET_CODE (x) == STRICT_LOW_PART
3214 || GET_CODE (x) == SIGN_EXTRACT
3215 || GET_CODE (x) == ZERO_EXTRACT
3216 || GET_CODE (x) == SUBREG)
3217 x = XEXP (x, 0);
3219 if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3220 return;
3222 /* If we do not have usage information, or if we know the register
3223 is used more than once, note that fact for check_dbra_loop. */
3224 if (REGNO (x) >= max_reg_before_loop
3225 || ! regs->array[REGNO (x)].single_usage
3226 || regs->array[REGNO (x)].single_usage == const0_rtx)
3227 regs->multiple_uses = 1;
3230 /* Return nonzero if the rtx X is invariant over the current loop.
3232 The value is 2 if we refer to something only conditionally invariant.
3234 A memory ref is invariant if it is not volatile and does not conflict
3235 with anything stored in `loop_info->store_mems'. */
3238 loop_invariant_p (const struct loop *loop, rtx x)
3240 struct loop_info *loop_info = LOOP_INFO (loop);
3241 struct loop_regs *regs = LOOP_REGS (loop);
3242 int i;
3243 enum rtx_code code;
3244 const char *fmt;
3245 int conditional = 0;
3246 rtx mem_list_entry;
3248 if (x == 0)
3249 return 1;
3250 code = GET_CODE (x);
3251 switch (code)
3253 case CONST_INT:
3254 case CONST_DOUBLE:
3255 case SYMBOL_REF:
3256 case CONST:
3257 return 1;
3259 case LABEL_REF:
3260 /* A LABEL_REF is normally invariant, however, if we are unrolling
3261 loops, and this label is inside the loop, then it isn't invariant.
3262 This is because each unrolled copy of the loop body will have
3263 a copy of this label. If this was invariant, then an insn loading
3264 the address of this label into a register might get moved outside
3265 the loop, and then each loop body would end up using the same label.
3267 We don't know the loop bounds here though, so just fail for all
3268 labels. */
3269 if (flag_old_unroll_loops)
3270 return 0;
3271 else
3272 return 1;
3274 case PC:
3275 case CC0:
3276 case UNSPEC_VOLATILE:
3277 return 0;
3279 case REG:
3280 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3281 since the reg might be set by initialization within the loop. */
3283 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3284 || x == arg_pointer_rtx || x == pic_offset_table_rtx)
3285 && ! current_function_has_nonlocal_goto)
3286 return 1;
3288 if (LOOP_INFO (loop)->has_call
3289 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3290 return 0;
3292 /* Out-of-range regs can occur when we are called from unrolling.
3293 These have always been created by the unroller and are set in
3294 the loop, hence are never invariant. */
3296 if (REGNO (x) >= (unsigned) regs->num)
3297 return 0;
3299 if (regs->array[REGNO (x)].set_in_loop < 0)
3300 return 2;
3302 return regs->array[REGNO (x)].set_in_loop == 0;
3304 case MEM:
3305 /* Volatile memory references must be rejected. Do this before
3306 checking for read-only items, so that volatile read-only items
3307 will be rejected also. */
3308 if (MEM_VOLATILE_P (x))
3309 return 0;
3311 /* See if there is any dependence between a store and this load. */
3312 mem_list_entry = loop_info->store_mems;
3313 while (mem_list_entry)
3315 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3316 x, rtx_varies_p))
3317 return 0;
3319 mem_list_entry = XEXP (mem_list_entry, 1);
3322 /* It's not invalidated by a store in memory
3323 but we must still verify the address is invariant. */
3324 break;
3326 case ASM_OPERANDS:
3327 /* Don't mess with insns declared volatile. */
3328 if (MEM_VOLATILE_P (x))
3329 return 0;
3330 break;
3332 default:
3333 break;
3336 fmt = GET_RTX_FORMAT (code);
3337 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3339 if (fmt[i] == 'e')
3341 int tem = loop_invariant_p (loop, XEXP (x, i));
3342 if (tem == 0)
3343 return 0;
3344 if (tem == 2)
3345 conditional = 1;
3347 else if (fmt[i] == 'E')
3349 int j;
3350 for (j = 0; j < XVECLEN (x, i); j++)
3352 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3353 if (tem == 0)
3354 return 0;
3355 if (tem == 2)
3356 conditional = 1;
3362 return 1 + conditional;
3365 /* Return nonzero if all the insns in the loop that set REG
3366 are INSN and the immediately following insns,
3367 and if each of those insns sets REG in an invariant way
3368 (not counting uses of REG in them).
3370 The value is 2 if some of these insns are only conditionally invariant.
3372 We assume that INSN itself is the first set of REG
3373 and that its source is invariant. */
3375 static int
3376 consec_sets_invariant_p (const struct loop *loop, rtx reg, int n_sets,
3377 rtx insn)
3379 struct loop_regs *regs = LOOP_REGS (loop);
3380 rtx p = insn;
3381 unsigned int regno = REGNO (reg);
3382 rtx temp;
3383 /* Number of sets we have to insist on finding after INSN. */
3384 int count = n_sets - 1;
3385 int old = regs->array[regno].set_in_loop;
3386 int value = 0;
3387 int this;
3389 /* If N_SETS hit the limit, we can't rely on its value. */
3390 if (n_sets == 127)
3391 return 0;
3393 regs->array[regno].set_in_loop = 0;
3395 while (count > 0)
3397 enum rtx_code code;
3398 rtx set;
3400 p = NEXT_INSN (p);
3401 code = GET_CODE (p);
3403 /* If library call, skip to end of it. */
3404 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3405 p = XEXP (temp, 0);
3407 this = 0;
3408 if (code == INSN
3409 && (set = single_set (p))
3410 && GET_CODE (SET_DEST (set)) == REG
3411 && REGNO (SET_DEST (set)) == regno)
3413 this = loop_invariant_p (loop, SET_SRC (set));
3414 if (this != 0)
3415 value |= this;
3416 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3418 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3419 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3420 notes are OK. */
3421 this = (CONSTANT_P (XEXP (temp, 0))
3422 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3423 && loop_invariant_p (loop, XEXP (temp, 0))));
3424 if (this != 0)
3425 value |= this;
3428 if (this != 0)
3429 count--;
3430 else if (code != NOTE)
3432 regs->array[regno].set_in_loop = old;
3433 return 0;
3437 regs->array[regno].set_in_loop = old;
3438 /* If loop_invariant_p ever returned 2, we return 2. */
3439 return 1 + (value & 2);
3442 #if 0
3443 /* I don't think this condition is sufficient to allow INSN
3444 to be moved, so we no longer test it. */
3446 /* Return 1 if all insns in the basic block of INSN and following INSN
3447 that set REG are invariant according to TABLE. */
3449 static int
3450 all_sets_invariant_p (rtx reg, rtx insn, short *table)
3452 rtx p = insn;
3453 int regno = REGNO (reg);
3455 while (1)
3457 enum rtx_code code;
3458 p = NEXT_INSN (p);
3459 code = GET_CODE (p);
3460 if (code == CODE_LABEL || code == JUMP_INSN)
3461 return 1;
3462 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3463 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3464 && REGNO (SET_DEST (PATTERN (p))) == regno)
3466 if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3467 return 0;
3471 #endif /* 0 */
3473 /* Look at all uses (not sets) of registers in X. For each, if it is
3474 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3475 a different insn, set USAGE[REGNO] to const0_rtx. */
3477 static void
3478 find_single_use_in_loop (struct loop_regs *regs, rtx insn, rtx x)
3480 enum rtx_code code = GET_CODE (x);
3481 const char *fmt = GET_RTX_FORMAT (code);
3482 int i, j;
3484 if (code == REG)
3485 regs->array[REGNO (x)].single_usage
3486 = (regs->array[REGNO (x)].single_usage != 0
3487 && regs->array[REGNO (x)].single_usage != insn)
3488 ? const0_rtx : insn;
3490 else if (code == SET)
3492 /* Don't count SET_DEST if it is a REG; otherwise count things
3493 in SET_DEST because if a register is partially modified, it won't
3494 show up as a potential movable so we don't care how USAGE is set
3495 for it. */
3496 if (GET_CODE (SET_DEST (x)) != REG)
3497 find_single_use_in_loop (regs, insn, SET_DEST (x));
3498 find_single_use_in_loop (regs, insn, SET_SRC (x));
3500 else
3501 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3503 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3504 find_single_use_in_loop (regs, insn, XEXP (x, i));
3505 else if (fmt[i] == 'E')
3506 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3507 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3511 /* Count and record any set in X which is contained in INSN. Update
3512 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3513 in X. */
3515 static void
3516 count_one_set (struct loop_regs *regs, rtx insn, rtx x, rtx *last_set)
3518 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3519 /* Don't move a reg that has an explicit clobber.
3520 It's not worth the pain to try to do it correctly. */
3521 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3523 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3525 rtx dest = SET_DEST (x);
3526 while (GET_CODE (dest) == SUBREG
3527 || GET_CODE (dest) == ZERO_EXTRACT
3528 || GET_CODE (dest) == SIGN_EXTRACT
3529 || GET_CODE (dest) == STRICT_LOW_PART)
3530 dest = XEXP (dest, 0);
3531 if (GET_CODE (dest) == REG)
3533 int i;
3534 int regno = REGNO (dest);
3535 for (i = 0; i < LOOP_REGNO_NREGS (regno, dest); i++)
3537 /* If this is the first setting of this reg
3538 in current basic block, and it was set before,
3539 it must be set in two basic blocks, so it cannot
3540 be moved out of the loop. */
3541 if (regs->array[regno].set_in_loop > 0
3542 && last_set[regno] == 0)
3543 regs->array[regno+i].may_not_optimize = 1;
3544 /* If this is not first setting in current basic block,
3545 see if reg was used in between previous one and this.
3546 If so, neither one can be moved. */
3547 if (last_set[regno] != 0
3548 && reg_used_between_p (dest, last_set[regno], insn))
3549 regs->array[regno+i].may_not_optimize = 1;
3550 if (regs->array[regno+i].set_in_loop < 127)
3551 ++regs->array[regno+i].set_in_loop;
3552 last_set[regno+i] = insn;
3558 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3559 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3560 contained in insn INSN is used by any insn that precedes INSN in
3561 cyclic order starting from the loop entry point.
3563 We don't want to use INSN_LUID here because if we restrict INSN to those
3564 that have a valid INSN_LUID, it means we cannot move an invariant out
3565 from an inner loop past two loops. */
3567 static int
3568 loop_reg_used_before_p (const struct loop *loop, rtx set, rtx insn)
3570 rtx reg = SET_DEST (set);
3571 rtx p;
3573 /* Scan forward checking for register usage. If we hit INSN, we
3574 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3575 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3577 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3578 return 1;
3580 if (p == loop->end)
3581 p = loop->start;
3584 return 0;
3588 /* Information we collect about arrays that we might want to prefetch. */
3589 struct prefetch_info
3591 struct iv_class *class; /* Class this prefetch is based on. */
3592 struct induction *giv; /* GIV this prefetch is based on. */
3593 rtx base_address; /* Start prefetching from this address plus
3594 index. */
3595 HOST_WIDE_INT index;
3596 HOST_WIDE_INT stride; /* Prefetch stride in bytes in each
3597 iteration. */
3598 unsigned int bytes_accessed; /* Sum of sizes of all accesses to this
3599 prefetch area in one iteration. */
3600 unsigned int total_bytes; /* Total bytes loop will access in this block.
3601 This is set only for loops with known
3602 iteration counts and is 0xffffffff
3603 otherwise. */
3604 int prefetch_in_loop; /* Number of prefetch insns in loop. */
3605 int prefetch_before_loop; /* Number of prefetch insns before loop. */
3606 unsigned int write : 1; /* 1 for read/write prefetches. */
3609 /* Data used by check_store function. */
3610 struct check_store_data
3612 rtx mem_address;
3613 int mem_write;
3616 static void check_store (rtx, rtx, void *);
3617 static void emit_prefetch_instructions (struct loop *);
3618 static int rtx_equal_for_prefetch_p (rtx, rtx);
3620 /* Set mem_write when mem_address is found. Used as callback to
3621 note_stores. */
3622 static void
3623 check_store (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
3625 struct check_store_data *d = (struct check_store_data *) data;
3627 if ((GET_CODE (x) == MEM) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
3628 d->mem_write = 1;
3631 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3632 important to get some addresses combined. Later more sophisticated
3633 transformations can be added when necessary.
3635 ??? Same trick with swapping operand is done at several other places.
3636 It can be nice to develop some common way to handle this. */
3638 static int
3639 rtx_equal_for_prefetch_p (rtx x, rtx y)
3641 int i;
3642 int j;
3643 enum rtx_code code = GET_CODE (x);
3644 const char *fmt;
3646 if (x == y)
3647 return 1;
3648 if (code != GET_CODE (y))
3649 return 0;
3651 code = GET_CODE (x);
3653 if (GET_RTX_CLASS (code) == 'c')
3655 return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
3656 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
3657 || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
3658 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
3660 /* Compare the elements. If any pair of corresponding elements fails to
3661 match, return 0 for the whole thing. */
3663 fmt = GET_RTX_FORMAT (code);
3664 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3666 switch (fmt[i])
3668 case 'w':
3669 if (XWINT (x, i) != XWINT (y, i))
3670 return 0;
3671 break;
3673 case 'i':
3674 if (XINT (x, i) != XINT (y, i))
3675 return 0;
3676 break;
3678 case 'E':
3679 /* Two vectors must have the same length. */
3680 if (XVECLEN (x, i) != XVECLEN (y, i))
3681 return 0;
3683 /* And the corresponding elements must match. */
3684 for (j = 0; j < XVECLEN (x, i); j++)
3685 if (rtx_equal_for_prefetch_p (XVECEXP (x, i, j),
3686 XVECEXP (y, i, j)) == 0)
3687 return 0;
3688 break;
3690 case 'e':
3691 if (rtx_equal_for_prefetch_p (XEXP (x, i), XEXP (y, i)) == 0)
3692 return 0;
3693 break;
3695 case 's':
3696 if (strcmp (XSTR (x, i), XSTR (y, i)))
3697 return 0;
3698 break;
3700 case 'u':
3701 /* These are just backpointers, so they don't matter. */
3702 break;
3704 case '0':
3705 break;
3707 /* It is believed that rtx's at this level will never
3708 contain anything but integers and other rtx's,
3709 except for within LABEL_REFs and SYMBOL_REFs. */
3710 default:
3711 abort ();
3714 return 1;
3717 /* Remove constant addition value from the expression X (when present)
3718 and return it. */
3720 static HOST_WIDE_INT
3721 remove_constant_addition (rtx *x)
3723 HOST_WIDE_INT addval = 0;
3724 rtx exp = *x;
3726 /* Avoid clobbering a shared CONST expression. */
3727 if (GET_CODE (exp) == CONST)
3729 if (GET_CODE (XEXP (exp, 0)) == PLUS
3730 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == SYMBOL_REF
3731 && GET_CODE (XEXP (XEXP (exp, 0), 1)) == CONST_INT)
3733 *x = XEXP (XEXP (exp, 0), 0);
3734 return INTVAL (XEXP (XEXP (exp, 0), 1));
3736 return 0;
3739 if (GET_CODE (exp) == CONST_INT)
3741 addval = INTVAL (exp);
3742 *x = const0_rtx;
3745 /* For plus expression recurse on ourself. */
3746 else if (GET_CODE (exp) == PLUS)
3748 addval += remove_constant_addition (&XEXP (exp, 0));
3749 addval += remove_constant_addition (&XEXP (exp, 1));
3751 /* In case our parameter was constant, remove extra zero from the
3752 expression. */
3753 if (XEXP (exp, 0) == const0_rtx)
3754 *x = XEXP (exp, 1);
3755 else if (XEXP (exp, 1) == const0_rtx)
3756 *x = XEXP (exp, 0);
3759 return addval;
3762 /* Attempt to identify accesses to arrays that are most likely to cause cache
3763 misses, and emit prefetch instructions a few prefetch blocks forward.
3765 To detect the arrays we use the GIV information that was collected by the
3766 strength reduction pass.
3768 The prefetch instructions are generated after the GIV information is done
3769 and before the strength reduction process. The new GIVs are injected into
3770 the strength reduction tables, so the prefetch addresses are optimized as
3771 well.
3773 GIVs are split into base address, stride, and constant addition values.
3774 GIVs with the same address, stride and close addition values are combined
3775 into a single prefetch. Also writes to GIVs are detected, so that prefetch
3776 for write instructions can be used for the block we write to, on machines
3777 that support write prefetches.
3779 Several heuristics are used to determine when to prefetch. They are
3780 controlled by defined symbols that can be overridden for each target. */
3782 static void
3783 emit_prefetch_instructions (struct loop *loop)
3785 int num_prefetches = 0;
3786 int num_real_prefetches = 0;
3787 int num_real_write_prefetches = 0;
3788 int num_prefetches_before = 0;
3789 int num_write_prefetches_before = 0;
3790 int ahead = 0;
3791 int i;
3792 struct iv_class *bl;
3793 struct induction *iv;
3794 struct prefetch_info info[MAX_PREFETCHES];
3795 struct loop_ivs *ivs = LOOP_IVS (loop);
3797 if (!HAVE_prefetch)
3798 return;
3800 /* Consider only loops w/o calls. When a call is done, the loop is probably
3801 slow enough to read the memory. */
3802 if (PREFETCH_NO_CALL && LOOP_INFO (loop)->has_call)
3804 if (loop_dump_stream)
3805 fprintf (loop_dump_stream, "Prefetch: ignoring loop: has call.\n");
3807 return;
3810 /* Don't prefetch in loops known to have few iterations. */
3811 if (PREFETCH_NO_LOW_LOOPCNT
3812 && LOOP_INFO (loop)->n_iterations
3813 && LOOP_INFO (loop)->n_iterations <= PREFETCH_LOW_LOOPCNT)
3815 if (loop_dump_stream)
3816 fprintf (loop_dump_stream,
3817 "Prefetch: ignoring loop: not enough iterations.\n");
3818 return;
3821 /* Search all induction variables and pick those interesting for the prefetch
3822 machinery. */
3823 for (bl = ivs->list; bl; bl = bl->next)
3825 struct induction *biv = bl->biv, *biv1;
3826 int basestride = 0;
3828 biv1 = biv;
3830 /* Expect all BIVs to be executed in each iteration. This makes our
3831 analysis more conservative. */
3832 while (biv1)
3834 /* Discard non-constant additions that we can't handle well yet, and
3835 BIVs that are executed multiple times; such BIVs ought to be
3836 handled in the nested loop. We accept not_every_iteration BIVs,
3837 since these only result in larger strides and make our
3838 heuristics more conservative. */
3839 if (GET_CODE (biv->add_val) != CONST_INT)
3841 if (loop_dump_stream)
3843 fprintf (loop_dump_stream,
3844 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
3845 REGNO (biv->src_reg), INSN_UID (biv->insn));
3846 print_rtl (loop_dump_stream, biv->add_val);
3847 fprintf (loop_dump_stream, "\n");
3849 break;
3852 if (biv->maybe_multiple)
3854 if (loop_dump_stream)
3856 fprintf (loop_dump_stream,
3857 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
3858 REGNO (biv->src_reg), INSN_UID (biv->insn));
3859 print_rtl (loop_dump_stream, biv->add_val);
3860 fprintf (loop_dump_stream, "\n");
3862 break;
3865 basestride += INTVAL (biv1->add_val);
3866 biv1 = biv1->next_iv;
3869 if (biv1 || !basestride)
3870 continue;
3872 for (iv = bl->giv; iv; iv = iv->next_iv)
3874 rtx address;
3875 rtx temp;
3876 HOST_WIDE_INT index = 0;
3877 int add = 1;
3878 HOST_WIDE_INT stride = 0;
3879 int stride_sign = 1;
3880 struct check_store_data d;
3881 const char *ignore_reason = NULL;
3882 int size = GET_MODE_SIZE (GET_MODE (iv));
3884 /* See whether an induction variable is interesting to us and if
3885 not, report the reason. */
3886 if (iv->giv_type != DEST_ADDR)
3887 ignore_reason = "giv is not a destination address";
3889 /* We are interested only in constant stride memory references
3890 in order to be able to compute density easily. */
3891 else if (GET_CODE (iv->mult_val) != CONST_INT)
3892 ignore_reason = "stride is not constant";
3894 else
3896 stride = INTVAL (iv->mult_val) * basestride;
3897 if (stride < 0)
3899 stride = -stride;
3900 stride_sign = -1;
3903 /* On some targets, reversed order prefetches are not
3904 worthwhile. */
3905 if (PREFETCH_NO_REVERSE_ORDER && stride_sign < 0)
3906 ignore_reason = "reversed order stride";
3908 /* Prefetch of accesses with an extreme stride might not be
3909 worthwhile, either. */
3910 else if (PREFETCH_NO_EXTREME_STRIDE
3911 && stride > PREFETCH_EXTREME_STRIDE)
3912 ignore_reason = "extreme stride";
3914 /* Ignore GIVs with varying add values; we can't predict the
3915 value for the next iteration. */
3916 else if (!loop_invariant_p (loop, iv->add_val))
3917 ignore_reason = "giv has varying add value";
3919 /* Ignore GIVs in the nested loops; they ought to have been
3920 handled already. */
3921 else if (iv->maybe_multiple)
3922 ignore_reason = "giv is in nested loop";
3925 if (ignore_reason != NULL)
3927 if (loop_dump_stream)
3928 fprintf (loop_dump_stream,
3929 "Prefetch: ignoring giv at %d: %s.\n",
3930 INSN_UID (iv->insn), ignore_reason);
3931 continue;
3934 /* Determine the pointer to the basic array we are examining. It is
3935 the sum of the BIV's initial value and the GIV's add_val. */
3936 address = copy_rtx (iv->add_val);
3937 temp = copy_rtx (bl->initial_value);
3939 address = simplify_gen_binary (PLUS, Pmode, temp, address);
3940 index = remove_constant_addition (&address);
3942 d.mem_write = 0;
3943 d.mem_address = *iv->location;
3945 /* When the GIV is not always executed, we might be better off by
3946 not dirtying the cache pages. */
3947 if (PREFETCH_CONDITIONAL || iv->always_executed)
3948 note_stores (PATTERN (iv->insn), check_store, &d);
3949 else
3951 if (loop_dump_stream)
3952 fprintf (loop_dump_stream, "Prefetch: Ignoring giv at %d: %s\n",
3953 INSN_UID (iv->insn), "in conditional code.");
3954 continue;
3957 /* Attempt to find another prefetch to the same array and see if we
3958 can merge this one. */
3959 for (i = 0; i < num_prefetches; i++)
3960 if (rtx_equal_for_prefetch_p (address, info[i].base_address)
3961 && stride == info[i].stride)
3963 /* In case both access same array (same location
3964 just with small difference in constant indexes), merge
3965 the prefetches. Just do the later and the earlier will
3966 get prefetched from previous iteration.
3967 The artificial threshold should not be too small,
3968 but also not bigger than small portion of memory usually
3969 traversed by single loop. */
3970 if (index >= info[i].index
3971 && index - info[i].index < PREFETCH_EXTREME_DIFFERENCE)
3973 info[i].write |= d.mem_write;
3974 info[i].bytes_accessed += size;
3975 info[i].index = index;
3976 info[i].giv = iv;
3977 info[i].class = bl;
3978 info[num_prefetches].base_address = address;
3979 add = 0;
3980 break;
3983 if (index < info[i].index
3984 && info[i].index - index < PREFETCH_EXTREME_DIFFERENCE)
3986 info[i].write |= d.mem_write;
3987 info[i].bytes_accessed += size;
3988 add = 0;
3989 break;
3993 /* Merging failed. */
3994 if (add)
3996 info[num_prefetches].giv = iv;
3997 info[num_prefetches].class = bl;
3998 info[num_prefetches].index = index;
3999 info[num_prefetches].stride = stride;
4000 info[num_prefetches].base_address = address;
4001 info[num_prefetches].write = d.mem_write;
4002 info[num_prefetches].bytes_accessed = size;
4003 num_prefetches++;
4004 if (num_prefetches >= MAX_PREFETCHES)
4006 if (loop_dump_stream)
4007 fprintf (loop_dump_stream,
4008 "Maximal number of prefetches exceeded.\n");
4009 return;
4015 for (i = 0; i < num_prefetches; i++)
4017 int density;
4019 /* Attempt to calculate the total number of bytes fetched by all
4020 iterations of the loop. Avoid overflow. */
4021 if (LOOP_INFO (loop)->n_iterations
4022 && ((unsigned HOST_WIDE_INT) (0xffffffff / info[i].stride)
4023 >= LOOP_INFO (loop)->n_iterations))
4024 info[i].total_bytes = info[i].stride * LOOP_INFO (loop)->n_iterations;
4025 else
4026 info[i].total_bytes = 0xffffffff;
4028 density = info[i].bytes_accessed * 100 / info[i].stride;
4030 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4031 if (PREFETCH_ONLY_DENSE_MEM)
4032 if (density * 256 > PREFETCH_DENSE_MEM * 100
4033 && (info[i].total_bytes / PREFETCH_BLOCK
4034 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN))
4036 info[i].prefetch_before_loop = 1;
4037 info[i].prefetch_in_loop
4038 = (info[i].total_bytes / PREFETCH_BLOCK
4039 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX);
4041 else
4043 info[i].prefetch_in_loop = 0, info[i].prefetch_before_loop = 0;
4044 if (loop_dump_stream)
4045 fprintf (loop_dump_stream,
4046 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4047 INSN_UID (info[i].giv->insn), density);
4049 else
4050 info[i].prefetch_in_loop = 1, info[i].prefetch_before_loop = 1;
4052 /* Find how many prefetch instructions we'll use within the loop. */
4053 if (info[i].prefetch_in_loop != 0)
4055 info[i].prefetch_in_loop = ((info[i].stride + PREFETCH_BLOCK - 1)
4056 / PREFETCH_BLOCK);
4057 num_real_prefetches += info[i].prefetch_in_loop;
4058 if (info[i].write)
4059 num_real_write_prefetches += info[i].prefetch_in_loop;
4063 /* Determine how many iterations ahead to prefetch within the loop, based
4064 on how many prefetches we currently expect to do within the loop. */
4065 if (num_real_prefetches != 0)
4067 if ((ahead = SIMULTANEOUS_PREFETCHES / num_real_prefetches) == 0)
4069 if (loop_dump_stream)
4070 fprintf (loop_dump_stream,
4071 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4072 SIMULTANEOUS_PREFETCHES, num_real_prefetches);
4073 num_real_prefetches = 0, num_real_write_prefetches = 0;
4076 /* We'll also use AHEAD to determine how many prefetch instructions to
4077 emit before a loop, so don't leave it zero. */
4078 if (ahead == 0)
4079 ahead = PREFETCH_BLOCKS_BEFORE_LOOP_MAX;
4081 for (i = 0; i < num_prefetches; i++)
4083 /* Update if we've decided not to prefetch anything within the loop. */
4084 if (num_real_prefetches == 0)
4085 info[i].prefetch_in_loop = 0;
4087 /* Find how many prefetch instructions we'll use before the loop. */
4088 if (info[i].prefetch_before_loop != 0)
4090 int n = info[i].total_bytes / PREFETCH_BLOCK;
4091 if (n > ahead)
4092 n = ahead;
4093 info[i].prefetch_before_loop = n;
4094 num_prefetches_before += n;
4095 if (info[i].write)
4096 num_write_prefetches_before += n;
4099 if (loop_dump_stream)
4101 if (info[i].prefetch_in_loop == 0
4102 && info[i].prefetch_before_loop == 0)
4103 continue;
4104 fprintf (loop_dump_stream, "Prefetch insn: %d",
4105 INSN_UID (info[i].giv->insn));
4106 fprintf (loop_dump_stream,
4107 "; in loop: %d; before: %d; %s\n",
4108 info[i].prefetch_in_loop,
4109 info[i].prefetch_before_loop,
4110 info[i].write ? "read/write" : "read only");
4111 fprintf (loop_dump_stream,
4112 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4113 (int) (info[i].bytes_accessed * 100 / info[i].stride),
4114 info[i].bytes_accessed, info[i].total_bytes);
4115 fprintf (loop_dump_stream, " index: " HOST_WIDE_INT_PRINT_DEC
4116 "; stride: " HOST_WIDE_INT_PRINT_DEC "; address: ",
4117 info[i].index, info[i].stride);
4118 print_rtl (loop_dump_stream, info[i].base_address);
4119 fprintf (loop_dump_stream, "\n");
4123 if (num_real_prefetches + num_prefetches_before > 0)
4125 /* Record that this loop uses prefetch instructions. */
4126 LOOP_INFO (loop)->has_prefetch = 1;
4128 if (loop_dump_stream)
4130 fprintf (loop_dump_stream, "Real prefetches needed within loop: %d (write: %d)\n",
4131 num_real_prefetches, num_real_write_prefetches);
4132 fprintf (loop_dump_stream, "Real prefetches needed before loop: %d (write: %d)\n",
4133 num_prefetches_before, num_write_prefetches_before);
4137 for (i = 0; i < num_prefetches; i++)
4139 int y;
4141 for (y = 0; y < info[i].prefetch_in_loop; y++)
4143 rtx loc = copy_rtx (*info[i].giv->location);
4144 rtx insn;
4145 int bytes_ahead = PREFETCH_BLOCK * (ahead + y);
4146 rtx before_insn = info[i].giv->insn;
4147 rtx prev_insn = PREV_INSN (info[i].giv->insn);
4148 rtx seq;
4150 /* We can save some effort by offsetting the address on
4151 architectures with offsettable memory references. */
4152 if (offsettable_address_p (0, VOIDmode, loc))
4153 loc = plus_constant (loc, bytes_ahead);
4154 else
4156 rtx reg = gen_reg_rtx (Pmode);
4157 loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
4158 GEN_INT (bytes_ahead), reg,
4159 0, before_insn);
4160 loc = reg;
4163 start_sequence ();
4164 /* Make sure the address operand is valid for prefetch. */
4165 if (! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
4166 (loc, insn_data[(int)CODE_FOR_prefetch].operand[0].mode))
4167 loc = force_reg (Pmode, loc);
4168 emit_insn (gen_prefetch (loc, GEN_INT (info[i].write),
4169 GEN_INT (3)));
4170 seq = get_insns ();
4171 end_sequence ();
4172 emit_insn_before (seq, before_insn);
4174 /* Check all insns emitted and record the new GIV
4175 information. */
4176 insn = NEXT_INSN (prev_insn);
4177 while (insn != before_insn)
4179 insn = check_insn_for_givs (loop, insn,
4180 info[i].giv->always_executed,
4181 info[i].giv->maybe_multiple);
4182 insn = NEXT_INSN (insn);
4186 if (PREFETCH_BEFORE_LOOP)
4188 /* Emit insns before the loop to fetch the first cache lines or,
4189 if we're not prefetching within the loop, everything we expect
4190 to need. */
4191 for (y = 0; y < info[i].prefetch_before_loop; y++)
4193 rtx reg = gen_reg_rtx (Pmode);
4194 rtx loop_start = loop->start;
4195 rtx init_val = info[i].class->initial_value;
4196 rtx add_val = simplify_gen_binary (PLUS, Pmode,
4197 info[i].giv->add_val,
4198 GEN_INT (y * PREFETCH_BLOCK));
4200 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4201 non-constant INIT_VAL to have the same mode as REG, which
4202 in this case we know to be Pmode. */
4203 if (GET_MODE (init_val) != Pmode && !CONSTANT_P (init_val))
4205 rtx seq;
4207 start_sequence ();
4208 init_val = convert_to_mode (Pmode, init_val, 0);
4209 seq = get_insns ();
4210 end_sequence ();
4211 loop_insn_emit_before (loop, 0, loop_start, seq);
4213 loop_iv_add_mult_emit_before (loop, init_val,
4214 info[i].giv->mult_val,
4215 add_val, reg, 0, loop_start);
4216 emit_insn_before (gen_prefetch (reg, GEN_INT (info[i].write),
4217 GEN_INT (3)),
4218 loop_start);
4223 return;
4226 /* A "basic induction variable" or biv is a pseudo reg that is set
4227 (within this loop) only by incrementing or decrementing it. */
4228 /* A "general induction variable" or giv is a pseudo reg whose
4229 value is a linear function of a biv. */
4231 /* Bivs are recognized by `basic_induction_var';
4232 Givs by `general_induction_var'. */
4234 /* Communication with routines called via `note_stores'. */
4236 static rtx note_insn;
4238 /* Dummy register to have nonzero DEST_REG for DEST_ADDR type givs. */
4240 static rtx addr_placeholder;
4242 /* ??? Unfinished optimizations, and possible future optimizations,
4243 for the strength reduction code. */
4245 /* ??? The interaction of biv elimination, and recognition of 'constant'
4246 bivs, may cause problems. */
4248 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4249 performance problems.
4251 Perhaps don't eliminate things that can be combined with an addressing
4252 mode. Find all givs that have the same biv, mult_val, and add_val;
4253 then for each giv, check to see if its only use dies in a following
4254 memory address. If so, generate a new memory address and check to see
4255 if it is valid. If it is valid, then store the modified memory address,
4256 otherwise, mark the giv as not done so that it will get its own iv. */
4258 /* ??? Could try to optimize branches when it is known that a biv is always
4259 positive. */
4261 /* ??? When replace a biv in a compare insn, we should replace with closest
4262 giv so that an optimized branch can still be recognized by the combiner,
4263 e.g. the VAX acb insn. */
4265 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4266 was rerun in loop_optimize whenever a register was added or moved.
4267 Also, some of the optimizations could be a little less conservative. */
4269 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4270 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4271 callback.
4273 NOT_EVERY_ITERATION is 1 if current insn is not known to be executed at
4274 least once for every loop iteration except for the last one.
4276 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4277 loop iteration.
4279 void
4280 for_each_insn_in_loop (struct loop *loop, loop_insn_callback fncall)
4282 int not_every_iteration = 0;
4283 int maybe_multiple = 0;
4284 int past_loop_latch = 0;
4285 int loop_depth = 0;
4286 rtx p;
4288 /* If loop_scan_start points to the loop exit test, we have to be wary of
4289 subversive use of gotos inside expression statements. */
4290 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
4291 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
4293 /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE. */
4294 for (p = next_insn_in_loop (loop, loop->scan_start);
4295 p != NULL_RTX;
4296 p = next_insn_in_loop (loop, p))
4298 p = fncall (loop, p, not_every_iteration, maybe_multiple);
4300 /* Past CODE_LABEL, we get to insns that may be executed multiple
4301 times. The only way we can be sure that they can't is if every
4302 jump insn between here and the end of the loop either
4303 returns, exits the loop, is a jump to a location that is still
4304 behind the label, or is a jump to the loop start. */
4306 if (GET_CODE (p) == CODE_LABEL)
4308 rtx insn = p;
4310 maybe_multiple = 0;
4312 while (1)
4314 insn = NEXT_INSN (insn);
4315 if (insn == loop->scan_start)
4316 break;
4317 if (insn == loop->end)
4319 if (loop->top != 0)
4320 insn = loop->top;
4321 else
4322 break;
4323 if (insn == loop->scan_start)
4324 break;
4327 if (GET_CODE (insn) == JUMP_INSN
4328 && GET_CODE (PATTERN (insn)) != RETURN
4329 && (!any_condjump_p (insn)
4330 || (JUMP_LABEL (insn) != 0
4331 && JUMP_LABEL (insn) != loop->scan_start
4332 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
4334 maybe_multiple = 1;
4335 break;
4340 /* Past a jump, we get to insns for which we can't count
4341 on whether they will be executed during each iteration. */
4342 /* This code appears twice in strength_reduce. There is also similar
4343 code in scan_loop. */
4344 if (GET_CODE (p) == JUMP_INSN
4345 /* If we enter the loop in the middle, and scan around to the
4346 beginning, don't set not_every_iteration for that.
4347 This can be any kind of jump, since we want to know if insns
4348 will be executed if the loop is executed. */
4349 && !(JUMP_LABEL (p) == loop->top
4350 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
4351 && any_uncondjump_p (p))
4352 || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
4354 rtx label = 0;
4356 /* If this is a jump outside the loop, then it also doesn't
4357 matter. Check to see if the target of this branch is on the
4358 loop->exits_labels list. */
4360 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
4361 if (XEXP (label, 0) == JUMP_LABEL (p))
4362 break;
4364 if (!label)
4365 not_every_iteration = 1;
4368 else if (GET_CODE (p) == NOTE)
4370 /* At the virtual top of a converted loop, insns are again known to
4371 be executed each iteration: logically, the loop begins here
4372 even though the exit code has been duplicated.
4374 Insns are also again known to be executed each iteration at
4375 the LOOP_CONT note. */
4376 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
4377 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
4378 && loop_depth == 0)
4379 not_every_iteration = 0;
4380 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
4381 loop_depth++;
4382 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
4383 loop_depth--;
4386 /* Note if we pass a loop latch. If we do, then we can not clear
4387 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4388 a loop since a jump before the last CODE_LABEL may have started
4389 a new loop iteration.
4391 Note that LOOP_TOP is only set for rotated loops and we need
4392 this check for all loops, so compare against the CODE_LABEL
4393 which immediately follows LOOP_START. */
4394 if (GET_CODE (p) == JUMP_INSN
4395 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
4396 past_loop_latch = 1;
4398 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4399 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4400 or not an insn is known to be executed each iteration of the
4401 loop, whether or not any iterations are known to occur.
4403 Therefore, if we have just passed a label and have no more labels
4404 between here and the test insn of the loop, and we have not passed
4405 a jump to the top of the loop, then we know these insns will be
4406 executed each iteration. */
4408 if (not_every_iteration
4409 && !past_loop_latch
4410 && GET_CODE (p) == CODE_LABEL
4411 && no_labels_between_p (p, loop->end)
4412 && loop_insn_first_p (p, loop->cont))
4413 not_every_iteration = 0;
4417 static void
4418 loop_bivs_find (struct loop *loop)
4420 struct loop_regs *regs = LOOP_REGS (loop);
4421 struct loop_ivs *ivs = LOOP_IVS (loop);
4422 /* Temporary list pointers for traversing ivs->list. */
4423 struct iv_class *bl, **backbl;
4425 ivs->list = 0;
4427 for_each_insn_in_loop (loop, check_insn_for_bivs);
4429 /* Scan ivs->list to remove all regs that proved not to be bivs.
4430 Make a sanity check against regs->n_times_set. */
4431 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
4433 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4434 /* Above happens if register modified by subreg, etc. */
4435 /* Make sure it is not recognized as a basic induction var: */
4436 || regs->array[bl->regno].n_times_set != bl->biv_count
4437 /* If never incremented, it is invariant that we decided not to
4438 move. So leave it alone. */
4439 || ! bl->incremented)
4441 if (loop_dump_stream)
4442 fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
4443 bl->regno,
4444 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4445 ? "not induction variable"
4446 : (! bl->incremented ? "never incremented"
4447 : "count error")));
4449 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
4450 *backbl = bl->next;
4452 else
4454 backbl = &bl->next;
4456 if (loop_dump_stream)
4457 fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
4463 /* Determine how BIVS are initialized by looking through pre-header
4464 extended basic block. */
4465 static void
4466 loop_bivs_init_find (struct loop *loop)
4468 struct loop_ivs *ivs = LOOP_IVS (loop);
4469 /* Temporary list pointers for traversing ivs->list. */
4470 struct iv_class *bl;
4471 int call_seen;
4472 rtx p;
4474 /* Find initial value for each biv by searching backwards from loop_start,
4475 halting at first label. Also record any test condition. */
4477 call_seen = 0;
4478 for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
4480 rtx test;
4482 note_insn = p;
4484 if (GET_CODE (p) == CALL_INSN)
4485 call_seen = 1;
4487 if (INSN_P (p))
4488 note_stores (PATTERN (p), record_initial, ivs);
4490 /* Record any test of a biv that branches around the loop if no store
4491 between it and the start of loop. We only care about tests with
4492 constants and registers and only certain of those. */
4493 if (GET_CODE (p) == JUMP_INSN
4494 && JUMP_LABEL (p) != 0
4495 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
4496 && (test = get_condition_for_loop (loop, p)) != 0
4497 && GET_CODE (XEXP (test, 0)) == REG
4498 && REGNO (XEXP (test, 0)) < max_reg_before_loop
4499 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
4500 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
4501 && bl->init_insn == 0)
4503 /* If an NE test, we have an initial value! */
4504 if (GET_CODE (test) == NE)
4506 bl->init_insn = p;
4507 bl->init_set = gen_rtx_SET (VOIDmode,
4508 XEXP (test, 0), XEXP (test, 1));
4510 else
4511 bl->initial_test = test;
4517 /* Look at the each biv and see if we can say anything better about its
4518 initial value from any initializing insns set up above. (This is done
4519 in two passes to avoid missing SETs in a PARALLEL.) */
4520 static void
4521 loop_bivs_check (struct loop *loop)
4523 struct loop_ivs *ivs = LOOP_IVS (loop);
4524 /* Temporary list pointers for traversing ivs->list. */
4525 struct iv_class *bl;
4526 struct iv_class **backbl;
4528 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
4530 rtx src;
4531 rtx note;
4533 if (! bl->init_insn)
4534 continue;
4536 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4537 is a constant, use the value of that. */
4538 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4539 && CONSTANT_P (XEXP (note, 0)))
4540 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4541 && CONSTANT_P (XEXP (note, 0))))
4542 src = XEXP (note, 0);
4543 else
4544 src = SET_SRC (bl->init_set);
4546 if (loop_dump_stream)
4547 fprintf (loop_dump_stream,
4548 "Biv %d: initialized at insn %d: initial value ",
4549 bl->regno, INSN_UID (bl->init_insn));
4551 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4552 || GET_MODE (src) == VOIDmode)
4553 && valid_initial_value_p (src, bl->init_insn,
4554 LOOP_INFO (loop)->pre_header_has_call,
4555 loop->start))
4557 bl->initial_value = src;
4559 if (loop_dump_stream)
4561 print_simple_rtl (loop_dump_stream, src);
4562 fputc ('\n', loop_dump_stream);
4565 /* If we can't make it a giv,
4566 let biv keep initial value of "itself". */
4567 else if (loop_dump_stream)
4568 fprintf (loop_dump_stream, "is complex\n");
4573 /* Search the loop for general induction variables. */
4575 static void
4576 loop_givs_find (struct loop* loop)
4578 for_each_insn_in_loop (loop, check_insn_for_givs);
4582 /* For each giv for which we still don't know whether or not it is
4583 replaceable, check to see if it is replaceable because its final value
4584 can be calculated. */
4586 static void
4587 loop_givs_check (struct loop *loop)
4589 struct loop_ivs *ivs = LOOP_IVS (loop);
4590 struct iv_class *bl;
4592 for (bl = ivs->list; bl; bl = bl->next)
4594 struct induction *v;
4596 for (v = bl->giv; v; v = v->next_iv)
4597 if (! v->replaceable && ! v->not_replaceable)
4598 check_final_value (loop, v);
4603 /* Return nonzero if it is possible to eliminate the biv BL provided
4604 all givs are reduced. This is possible if either the reg is not
4605 used outside the loop, or we can compute what its final value will
4606 be. */
4608 static int
4609 loop_biv_eliminable_p (struct loop *loop, struct iv_class *bl,
4610 int threshold, int insn_count)
4612 /* For architectures with a decrement_and_branch_until_zero insn,
4613 don't do this if we put a REG_NONNEG note on the endtest for this
4614 biv. */
4616 #ifdef HAVE_decrement_and_branch_until_zero
4617 if (bl->nonneg)
4619 if (loop_dump_stream)
4620 fprintf (loop_dump_stream,
4621 "Cannot eliminate nonneg biv %d.\n", bl->regno);
4622 return 0;
4624 #endif
4626 /* Check that biv is used outside loop or if it has a final value.
4627 Compare against bl->init_insn rather than loop->start. We aren't
4628 concerned with any uses of the biv between init_insn and
4629 loop->start since these won't be affected by the value of the biv
4630 elsewhere in the function, so long as init_insn doesn't use the
4631 biv itself. */
4633 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
4634 && bl->init_insn
4635 && INSN_UID (bl->init_insn) < max_uid_for_loop
4636 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
4637 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4638 || (bl->final_value = final_biv_value (loop, bl)))
4639 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
4641 if (loop_dump_stream)
4643 fprintf (loop_dump_stream,
4644 "Cannot eliminate biv %d.\n",
4645 bl->regno);
4646 fprintf (loop_dump_stream,
4647 "First use: insn %d, last use: insn %d.\n",
4648 REGNO_FIRST_UID (bl->regno),
4649 REGNO_LAST_UID (bl->regno));
4651 return 0;
4655 /* Reduce each giv of BL that we have decided to reduce. */
4657 static void
4658 loop_givs_reduce (struct loop *loop, struct iv_class *bl)
4660 struct induction *v;
4662 for (v = bl->giv; v; v = v->next_iv)
4664 struct induction *tv;
4665 if (! v->ignore && v->same == 0)
4667 int auto_inc_opt = 0;
4669 /* If the code for derived givs immediately below has already
4670 allocated a new_reg, we must keep it. */
4671 if (! v->new_reg)
4672 v->new_reg = gen_reg_rtx (v->mode);
4674 #ifdef AUTO_INC_DEC
4675 /* If the target has auto-increment addressing modes, and
4676 this is an address giv, then try to put the increment
4677 immediately after its use, so that flow can create an
4678 auto-increment addressing mode. */
4679 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4680 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4681 /* We don't handle reversed biv's because bl->biv->insn
4682 does not have a valid INSN_LUID. */
4683 && ! bl->reversed
4684 && v->always_executed && ! v->maybe_multiple
4685 && INSN_UID (v->insn) < max_uid_for_loop)
4687 /* If other giv's have been combined with this one, then
4688 this will work only if all uses of the other giv's occur
4689 before this giv's insn. This is difficult to check.
4691 We simplify this by looking for the common case where
4692 there is one DEST_REG giv, and this giv's insn is the
4693 last use of the dest_reg of that DEST_REG giv. If the
4694 increment occurs after the address giv, then we can
4695 perform the optimization. (Otherwise, the increment
4696 would have to go before other_giv, and we would not be
4697 able to combine it with the address giv to get an
4698 auto-inc address.) */
4699 if (v->combined_with)
4701 struct induction *other_giv = 0;
4703 for (tv = bl->giv; tv; tv = tv->next_iv)
4704 if (tv->same == v)
4706 if (other_giv)
4707 break;
4708 else
4709 other_giv = tv;
4711 if (! tv && other_giv
4712 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4713 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4714 == INSN_UID (v->insn))
4715 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4716 auto_inc_opt = 1;
4718 /* Check for case where increment is before the address
4719 giv. Do this test in "loop order". */
4720 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4721 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4722 || (INSN_LUID (bl->biv->insn)
4723 > INSN_LUID (loop->scan_start))))
4724 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4725 && (INSN_LUID (loop->scan_start)
4726 < INSN_LUID (bl->biv->insn))))
4727 auto_inc_opt = -1;
4728 else
4729 auto_inc_opt = 1;
4731 #ifdef HAVE_cc0
4733 rtx prev;
4735 /* We can't put an insn immediately after one setting
4736 cc0, or immediately before one using cc0. */
4737 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4738 || (auto_inc_opt == -1
4739 && (prev = prev_nonnote_insn (v->insn)) != 0
4740 && INSN_P (prev)
4741 && sets_cc0_p (PATTERN (prev))))
4742 auto_inc_opt = 0;
4744 #endif
4746 if (auto_inc_opt)
4747 v->auto_inc_opt = 1;
4749 #endif
4751 /* For each place where the biv is incremented, add an insn
4752 to increment the new, reduced reg for the giv. */
4753 for (tv = bl->biv; tv; tv = tv->next_iv)
4755 rtx insert_before;
4757 /* Skip if location is the same as a previous one. */
4758 if (tv->same)
4759 continue;
4760 if (! auto_inc_opt)
4761 insert_before = NEXT_INSN (tv->insn);
4762 else if (auto_inc_opt == 1)
4763 insert_before = NEXT_INSN (v->insn);
4764 else
4765 insert_before = v->insn;
4767 if (tv->mult_val == const1_rtx)
4768 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4769 v->new_reg, v->new_reg,
4770 0, insert_before);
4771 else /* tv->mult_val == const0_rtx */
4772 /* A multiply is acceptable here
4773 since this is presumed to be seldom executed. */
4774 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4775 v->add_val, v->new_reg,
4776 0, insert_before);
4779 /* Add code at loop start to initialize giv's reduced reg. */
4781 loop_iv_add_mult_hoist (loop,
4782 extend_value_for_giv (v, bl->initial_value),
4783 v->mult_val, v->add_val, v->new_reg);
4789 /* Check for givs whose first use is their definition and whose
4790 last use is the definition of another giv. If so, it is likely
4791 dead and should not be used to derive another giv nor to
4792 eliminate a biv. */
4794 static void
4795 loop_givs_dead_check (struct loop *loop ATTRIBUTE_UNUSED, struct iv_class *bl)
4797 struct induction *v;
4799 for (v = bl->giv; v; v = v->next_iv)
4801 if (v->ignore
4802 || (v->same && v->same->ignore))
4803 continue;
4805 if (v->giv_type == DEST_REG
4806 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4808 struct induction *v1;
4810 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4811 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4812 v->maybe_dead = 1;
4818 static void
4819 loop_givs_rescan (struct loop *loop, struct iv_class *bl, rtx *reg_map)
4821 struct induction *v;
4823 for (v = bl->giv; v; v = v->next_iv)
4825 if (v->same && v->same->ignore)
4826 v->ignore = 1;
4828 if (v->ignore)
4829 continue;
4831 /* Update expression if this was combined, in case other giv was
4832 replaced. */
4833 if (v->same)
4834 v->new_reg = replace_rtx (v->new_reg,
4835 v->same->dest_reg, v->same->new_reg);
4837 /* See if this register is known to be a pointer to something. If
4838 so, see if we can find the alignment. First see if there is a
4839 destination register that is a pointer. If so, this shares the
4840 alignment too. Next see if we can deduce anything from the
4841 computational information. If not, and this is a DEST_ADDR
4842 giv, at least we know that it's a pointer, though we don't know
4843 the alignment. */
4844 if (GET_CODE (v->new_reg) == REG
4845 && v->giv_type == DEST_REG
4846 && REG_POINTER (v->dest_reg))
4847 mark_reg_pointer (v->new_reg,
4848 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
4849 else if (GET_CODE (v->new_reg) == REG
4850 && REG_POINTER (v->src_reg))
4852 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
4854 if (align == 0
4855 || GET_CODE (v->add_val) != CONST_INT
4856 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
4857 align = 0;
4859 mark_reg_pointer (v->new_reg, align);
4861 else if (GET_CODE (v->new_reg) == REG
4862 && GET_CODE (v->add_val) == REG
4863 && REG_POINTER (v->add_val))
4865 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
4867 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
4868 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
4869 align = 0;
4871 mark_reg_pointer (v->new_reg, align);
4873 else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
4874 mark_reg_pointer (v->new_reg, 0);
4876 if (v->giv_type == DEST_ADDR)
4877 /* Store reduced reg as the address in the memref where we found
4878 this giv. */
4879 validate_change (v->insn, v->location, v->new_reg, 0);
4880 else if (v->replaceable)
4882 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4884 else
4886 rtx original_insn = v->insn;
4887 rtx note;
4889 /* Not replaceable; emit an insn to set the original giv reg from
4890 the reduced giv, same as above. */
4891 v->insn = loop_insn_emit_after (loop, 0, original_insn,
4892 gen_move_insn (v->dest_reg,
4893 v->new_reg));
4895 /* The original insn may have a REG_EQUAL note. This note is
4896 now incorrect and may result in invalid substitutions later.
4897 The original insn is dead, but may be part of a libcall
4898 sequence, which doesn't seem worth the bother of handling. */
4899 note = find_reg_note (original_insn, REG_EQUAL, NULL_RTX);
4900 if (note)
4901 remove_note (original_insn, note);
4904 /* When a loop is reversed, givs which depend on the reversed
4905 biv, and which are live outside the loop, must be set to their
4906 correct final value. This insn is only needed if the giv is
4907 not replaceable. The correct final value is the same as the
4908 value that the giv starts the reversed loop with. */
4909 if (bl->reversed && ! v->replaceable)
4910 loop_iv_add_mult_sink (loop,
4911 extend_value_for_giv (v, bl->initial_value),
4912 v->mult_val, v->add_val, v->dest_reg);
4913 else if (v->final_value)
4914 loop_insn_sink_or_swim (loop,
4915 gen_load_of_final_value (v->dest_reg,
4916 v->final_value));
4918 if (loop_dump_stream)
4920 fprintf (loop_dump_stream, "giv at %d reduced to ",
4921 INSN_UID (v->insn));
4922 print_simple_rtl (loop_dump_stream, v->new_reg);
4923 fprintf (loop_dump_stream, "\n");
4929 static int
4930 loop_giv_reduce_benefit (struct loop *loop ATTRIBUTE_UNUSED,
4931 struct iv_class *bl, struct induction *v,
4932 rtx test_reg)
4934 int add_cost;
4935 int benefit;
4937 benefit = v->benefit;
4938 PUT_MODE (test_reg, v->mode);
4939 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
4940 test_reg, test_reg);
4942 /* Reduce benefit if not replaceable, since we will insert a
4943 move-insn to replace the insn that calculates this giv. Don't do
4944 this unless the giv is a user variable, since it will often be
4945 marked non-replaceable because of the duplication of the exit
4946 code outside the loop. In such a case, the copies we insert are
4947 dead and will be deleted. So they don't have a cost. Similar
4948 situations exist. */
4949 /* ??? The new final_[bg]iv_value code does a much better job of
4950 finding replaceable giv's, and hence this code may no longer be
4951 necessary. */
4952 if (! v->replaceable && ! bl->eliminable
4953 && REG_USERVAR_P (v->dest_reg))
4954 benefit -= copy_cost;
4956 /* Decrease the benefit to count the add-insns that we will insert
4957 to increment the reduced reg for the giv. ??? This can
4958 overestimate the run-time cost of the additional insns, e.g. if
4959 there are multiple basic blocks that increment the biv, but only
4960 one of these blocks is executed during each iteration. There is
4961 no good way to detect cases like this with the current structure
4962 of the loop optimizer. This code is more accurate for
4963 determining code size than run-time benefits. */
4964 benefit -= add_cost * bl->biv_count;
4966 /* Decide whether to strength-reduce this giv or to leave the code
4967 unchanged (recompute it from the biv each time it is used). This
4968 decision can be made independently for each giv. */
4970 #ifdef AUTO_INC_DEC
4971 /* Attempt to guess whether autoincrement will handle some of the
4972 new add insns; if so, increase BENEFIT (undo the subtraction of
4973 add_cost that was done above). */
4974 if (v->giv_type == DEST_ADDR
4975 /* Increasing the benefit is risky, since this is only a guess.
4976 Avoid increasing register pressure in cases where there would
4977 be no other benefit from reducing this giv. */
4978 && benefit > 0
4979 && GET_CODE (v->mult_val) == CONST_INT)
4981 int size = GET_MODE_SIZE (GET_MODE (v->mem));
4983 if (HAVE_POST_INCREMENT
4984 && INTVAL (v->mult_val) == size)
4985 benefit += add_cost * bl->biv_count;
4986 else if (HAVE_PRE_INCREMENT
4987 && INTVAL (v->mult_val) == size)
4988 benefit += add_cost * bl->biv_count;
4989 else if (HAVE_POST_DECREMENT
4990 && -INTVAL (v->mult_val) == size)
4991 benefit += add_cost * bl->biv_count;
4992 else if (HAVE_PRE_DECREMENT
4993 && -INTVAL (v->mult_val) == size)
4994 benefit += add_cost * bl->biv_count;
4996 #endif
4998 return benefit;
5002 /* Free IV structures for LOOP. */
5004 static void
5005 loop_ivs_free (struct loop *loop)
5007 struct loop_ivs *ivs = LOOP_IVS (loop);
5008 struct iv_class *iv = ivs->list;
5010 free (ivs->regs);
5012 while (iv)
5014 struct iv_class *next = iv->next;
5015 struct induction *induction;
5016 struct induction *next_induction;
5018 for (induction = iv->biv; induction; induction = next_induction)
5020 next_induction = induction->next_iv;
5021 free (induction);
5023 for (induction = iv->giv; induction; induction = next_induction)
5025 next_induction = induction->next_iv;
5026 free (induction);
5029 free (iv);
5030 iv = next;
5035 /* Perform strength reduction and induction variable elimination.
5037 Pseudo registers created during this function will be beyond the
5038 last valid index in several tables including
5039 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
5040 problem here, because the added registers cannot be givs outside of
5041 their loop, and hence will never be reconsidered. But scan_loop
5042 must check regnos to make sure they are in bounds. */
5044 static void
5045 strength_reduce (struct loop *loop, int flags)
5047 struct loop_info *loop_info = LOOP_INFO (loop);
5048 struct loop_regs *regs = LOOP_REGS (loop);
5049 struct loop_ivs *ivs = LOOP_IVS (loop);
5050 rtx p;
5051 /* Temporary list pointer for traversing ivs->list. */
5052 struct iv_class *bl;
5053 /* Ratio of extra register life span we can justify
5054 for saving an instruction. More if loop doesn't call subroutines
5055 since in that case saving an insn makes more difference
5056 and more registers are available. */
5057 /* ??? could set this to last value of threshold in move_movables */
5058 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
5059 /* Map of pseudo-register replacements. */
5060 rtx *reg_map = NULL;
5061 int reg_map_size;
5062 int unrolled_insn_copies = 0;
5063 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
5064 int insn_count = count_insns_in_loop (loop);
5066 addr_placeholder = gen_reg_rtx (Pmode);
5068 ivs->n_regs = max_reg_before_loop;
5069 ivs->regs = xcalloc (ivs->n_regs, sizeof (struct iv));
5071 /* Find all BIVs in loop. */
5072 loop_bivs_find (loop);
5074 /* Exit if there are no bivs. */
5075 if (! ivs->list)
5077 /* Can still unroll the loop anyways, but indicate that there is no
5078 strength reduction info available. */
5079 if (flags & LOOP_UNROLL)
5080 unroll_loop (loop, insn_count, 0);
5082 loop_ivs_free (loop);
5083 return;
5086 /* Determine how BIVS are initialized by looking through pre-header
5087 extended basic block. */
5088 loop_bivs_init_find (loop);
5090 /* Look at the each biv and see if we can say anything better about its
5091 initial value from any initializing insns set up above. */
5092 loop_bivs_check (loop);
5094 /* Search the loop for general induction variables. */
5095 loop_givs_find (loop);
5097 /* Try to calculate and save the number of loop iterations. This is
5098 set to zero if the actual number can not be calculated. This must
5099 be called after all giv's have been identified, since otherwise it may
5100 fail if the iteration variable is a giv. */
5101 loop_iterations (loop);
5103 #ifdef HAVE_prefetch
5104 if (flags & LOOP_PREFETCH)
5105 emit_prefetch_instructions (loop);
5106 #endif
5108 /* Now for each giv for which we still don't know whether or not it is
5109 replaceable, check to see if it is replaceable because its final value
5110 can be calculated. This must be done after loop_iterations is called,
5111 so that final_giv_value will work correctly. */
5112 loop_givs_check (loop);
5114 /* Try to prove that the loop counter variable (if any) is always
5115 nonnegative; if so, record that fact with a REG_NONNEG note
5116 so that "decrement and branch until zero" insn can be used. */
5117 check_dbra_loop (loop, insn_count);
5119 /* Create reg_map to hold substitutions for replaceable giv regs.
5120 Some givs might have been made from biv increments, so look at
5121 ivs->reg_iv_type for a suitable size. */
5122 reg_map_size = ivs->n_regs;
5123 reg_map = xcalloc (reg_map_size, sizeof (rtx));
5125 /* Examine each iv class for feasibility of strength reduction/induction
5126 variable elimination. */
5128 for (bl = ivs->list; bl; bl = bl->next)
5130 struct induction *v;
5131 int benefit;
5133 /* Test whether it will be possible to eliminate this biv
5134 provided all givs are reduced. */
5135 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
5137 /* This will be true at the end, if all givs which depend on this
5138 biv have been strength reduced.
5139 We can't (currently) eliminate the biv unless this is so. */
5140 bl->all_reduced = 1;
5142 /* Check each extension dependent giv in this class to see if its
5143 root biv is safe from wrapping in the interior mode. */
5144 check_ext_dependent_givs (loop, bl);
5146 /* Combine all giv's for this iv_class. */
5147 combine_givs (regs, bl);
5149 for (v = bl->giv; v; v = v->next_iv)
5151 struct induction *tv;
5153 if (v->ignore || v->same)
5154 continue;
5156 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
5158 /* If an insn is not to be strength reduced, then set its ignore
5159 flag, and clear bl->all_reduced. */
5161 /* A giv that depends on a reversed biv must be reduced if it is
5162 used after the loop exit, otherwise, it would have the wrong
5163 value after the loop exit. To make it simple, just reduce all
5164 of such giv's whether or not we know they are used after the loop
5165 exit. */
5167 if (! flag_reduce_all_givs
5168 && v->lifetime * threshold * benefit < insn_count
5169 && ! bl->reversed)
5171 if (loop_dump_stream)
5172 fprintf (loop_dump_stream,
5173 "giv of insn %d not worth while, %d vs %d.\n",
5174 INSN_UID (v->insn),
5175 v->lifetime * threshold * benefit, insn_count);
5176 v->ignore = 1;
5177 bl->all_reduced = 0;
5179 else
5181 /* Check that we can increment the reduced giv without a
5182 multiply insn. If not, reject it. */
5184 for (tv = bl->biv; tv; tv = tv->next_iv)
5185 if (tv->mult_val == const1_rtx
5186 && ! product_cheap_p (tv->add_val, v->mult_val))
5188 if (loop_dump_stream)
5189 fprintf (loop_dump_stream,
5190 "giv of insn %d: would need a multiply.\n",
5191 INSN_UID (v->insn));
5192 v->ignore = 1;
5193 bl->all_reduced = 0;
5194 break;
5199 /* Check for givs whose first use is their definition and whose
5200 last use is the definition of another giv. If so, it is likely
5201 dead and should not be used to derive another giv nor to
5202 eliminate a biv. */
5203 loop_givs_dead_check (loop, bl);
5205 /* Reduce each giv that we decided to reduce. */
5206 loop_givs_reduce (loop, bl);
5208 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
5209 as not reduced.
5211 For each giv register that can be reduced now: if replaceable,
5212 substitute reduced reg wherever the old giv occurs;
5213 else add new move insn "giv_reg = reduced_reg". */
5214 loop_givs_rescan (loop, bl, reg_map);
5216 /* All the givs based on the biv bl have been reduced if they
5217 merit it. */
5219 /* For each giv not marked as maybe dead that has been combined with a
5220 second giv, clear any "maybe dead" mark on that second giv.
5221 v->new_reg will either be or refer to the register of the giv it
5222 combined with.
5224 Doing this clearing avoids problems in biv elimination where
5225 a giv's new_reg is a complex value that can't be put in the
5226 insn but the giv combined with (with a reg as new_reg) is
5227 marked maybe_dead. Since the register will be used in either
5228 case, we'd prefer it be used from the simpler giv. */
5230 for (v = bl->giv; v; v = v->next_iv)
5231 if (! v->maybe_dead && v->same)
5232 v->same->maybe_dead = 0;
5234 /* Try to eliminate the biv, if it is a candidate.
5235 This won't work if ! bl->all_reduced,
5236 since the givs we planned to use might not have been reduced.
5238 We have to be careful that we didn't initially think we could
5239 eliminate this biv because of a giv that we now think may be
5240 dead and shouldn't be used as a biv replacement.
5242 Also, there is the possibility that we may have a giv that looks
5243 like it can be used to eliminate a biv, but the resulting insn
5244 isn't valid. This can happen, for example, on the 88k, where a
5245 JUMP_INSN can compare a register only with zero. Attempts to
5246 replace it with a compare with a constant will fail.
5248 Note that in cases where this call fails, we may have replaced some
5249 of the occurrences of the biv with a giv, but no harm was done in
5250 doing so in the rare cases where it can occur. */
5252 if (bl->all_reduced == 1 && bl->eliminable
5253 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
5255 /* ?? If we created a new test to bypass the loop entirely,
5256 or otherwise drop straight in, based on this test, then
5257 we might want to rewrite it also. This way some later
5258 pass has more hope of removing the initialization of this
5259 biv entirely. */
5261 /* If final_value != 0, then the biv may be used after loop end
5262 and we must emit an insn to set it just in case.
5264 Reversed bivs already have an insn after the loop setting their
5265 value, so we don't need another one. We can't calculate the
5266 proper final value for such a biv here anyways. */
5267 if (bl->final_value && ! bl->reversed)
5268 loop_insn_sink_or_swim (loop,
5269 gen_load_of_final_value (bl->biv->dest_reg,
5270 bl->final_value));
5272 if (loop_dump_stream)
5273 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
5274 bl->regno);
5276 /* See above note wrt final_value. But since we couldn't eliminate
5277 the biv, we must set the value after the loop instead of before. */
5278 else if (bl->final_value && ! bl->reversed)
5279 loop_insn_sink (loop, gen_load_of_final_value (bl->biv->dest_reg,
5280 bl->final_value));
5283 /* Go through all the instructions in the loop, making all the
5284 register substitutions scheduled in REG_MAP. */
5286 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
5287 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5288 || GET_CODE (p) == CALL_INSN)
5290 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
5291 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
5292 INSN_CODE (p) = -1;
5295 if (loop_info->n_iterations > 0)
5297 /* When we completely unroll a loop we will likely not need the increment
5298 of the loop BIV and we will not need the conditional branch at the
5299 end of the loop. */
5300 unrolled_insn_copies = insn_count - 2;
5302 #ifdef HAVE_cc0
5303 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5304 need the comparison before the conditional branch at the end of the
5305 loop. */
5306 unrolled_insn_copies -= 1;
5307 #endif
5309 /* We'll need one copy for each loop iteration. */
5310 unrolled_insn_copies *= loop_info->n_iterations;
5312 /* A little slop to account for the ability to remove initialization
5313 code, better CSE, and other secondary benefits of completely
5314 unrolling some loops. */
5315 unrolled_insn_copies -= 1;
5317 /* Clamp the value. */
5318 if (unrolled_insn_copies < 0)
5319 unrolled_insn_copies = 0;
5322 /* Unroll loops from within strength reduction so that we can use the
5323 induction variable information that strength_reduce has already
5324 collected. Always unroll loops that would be as small or smaller
5325 unrolled than when rolled. */
5326 if ((flags & LOOP_UNROLL)
5327 || ((flags & LOOP_AUTO_UNROLL)
5328 && loop_info->n_iterations > 0
5329 && unrolled_insn_copies <= insn_count))
5330 unroll_loop (loop, insn_count, 1);
5332 #ifdef HAVE_doloop_end
5333 if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
5334 doloop_optimize (loop);
5335 #endif /* HAVE_doloop_end */
5337 /* In case number of iterations is known, drop branch prediction note
5338 in the branch. Do that only in second loop pass, as loop unrolling
5339 may change the number of iterations performed. */
5340 if (flags & LOOP_BCT)
5342 unsigned HOST_WIDE_INT n
5343 = loop_info->n_iterations / loop_info->unroll_number;
5344 if (n > 1)
5345 predict_insn (prev_nonnote_insn (loop->end), PRED_LOOP_ITERATIONS,
5346 REG_BR_PROB_BASE - REG_BR_PROB_BASE / n);
5349 if (loop_dump_stream)
5350 fprintf (loop_dump_stream, "\n");
5352 loop_ivs_free (loop);
5353 if (reg_map)
5354 free (reg_map);
5357 /*Record all basic induction variables calculated in the insn. */
5358 static rtx
5359 check_insn_for_bivs (struct loop *loop, rtx p, int not_every_iteration,
5360 int maybe_multiple)
5362 struct loop_ivs *ivs = LOOP_IVS (loop);
5363 rtx set;
5364 rtx dest_reg;
5365 rtx inc_val;
5366 rtx mult_val;
5367 rtx *location;
5369 if (GET_CODE (p) == INSN
5370 && (set = single_set (p))
5371 && GET_CODE (SET_DEST (set)) == REG)
5373 dest_reg = SET_DEST (set);
5374 if (REGNO (dest_reg) < max_reg_before_loop
5375 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
5376 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
5378 if (basic_induction_var (loop, SET_SRC (set),
5379 GET_MODE (SET_SRC (set)),
5380 dest_reg, p, &inc_val, &mult_val,
5381 &location))
5383 /* It is a possible basic induction variable.
5384 Create and initialize an induction structure for it. */
5386 struct induction *v = xmalloc (sizeof (struct induction));
5388 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
5389 not_every_iteration, maybe_multiple);
5390 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
5392 else if (REGNO (dest_reg) < ivs->n_regs)
5393 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
5396 return p;
5399 /* Record all givs calculated in the insn.
5400 A register is a giv if: it is only set once, it is a function of a
5401 biv and a constant (or invariant), and it is not a biv. */
5402 static rtx
5403 check_insn_for_givs (struct loop *loop, rtx p, int not_every_iteration,
5404 int maybe_multiple)
5406 struct loop_regs *regs = LOOP_REGS (loop);
5408 rtx set;
5409 /* Look for a general induction variable in a register. */
5410 if (GET_CODE (p) == INSN
5411 && (set = single_set (p))
5412 && GET_CODE (SET_DEST (set)) == REG
5413 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
5415 rtx src_reg;
5416 rtx dest_reg;
5417 rtx add_val;
5418 rtx mult_val;
5419 rtx ext_val;
5420 int benefit;
5421 rtx regnote = 0;
5422 rtx last_consec_insn;
5424 dest_reg = SET_DEST (set);
5425 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
5426 return p;
5428 if (/* SET_SRC is a giv. */
5429 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
5430 &mult_val, &ext_val, 0, &benefit, VOIDmode)
5431 /* Equivalent expression is a giv. */
5432 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
5433 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
5434 &add_val, &mult_val, &ext_val, 0,
5435 &benefit, VOIDmode)))
5436 /* Don't try to handle any regs made by loop optimization.
5437 We have nothing on them in regno_first_uid, etc. */
5438 && REGNO (dest_reg) < max_reg_before_loop
5439 /* Don't recognize a BASIC_INDUCT_VAR here. */
5440 && dest_reg != src_reg
5441 /* This must be the only place where the register is set. */
5442 && (regs->array[REGNO (dest_reg)].n_times_set == 1
5443 /* or all sets must be consecutive and make a giv. */
5444 || (benefit = consec_sets_giv (loop, benefit, p,
5445 src_reg, dest_reg,
5446 &add_val, &mult_val, &ext_val,
5447 &last_consec_insn))))
5449 struct induction *v = xmalloc (sizeof (struct induction));
5451 /* If this is a library call, increase benefit. */
5452 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5453 benefit += libcall_benefit (p);
5455 /* Skip the consecutive insns, if there are any. */
5456 if (regs->array[REGNO (dest_reg)].n_times_set != 1)
5457 p = last_consec_insn;
5459 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
5460 ext_val, benefit, DEST_REG, not_every_iteration,
5461 maybe_multiple, (rtx*) 0);
5466 /* Look for givs which are memory addresses. */
5467 if (GET_CODE (p) == INSN)
5468 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
5469 maybe_multiple);
5471 /* Update the status of whether giv can derive other givs. This can
5472 change when we pass a label or an insn that updates a biv. */
5473 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5474 || GET_CODE (p) == CODE_LABEL)
5475 update_giv_derive (loop, p);
5476 return p;
5479 /* Return 1 if X is a valid source for an initial value (or as value being
5480 compared against in an initial test).
5482 X must be either a register or constant and must not be clobbered between
5483 the current insn and the start of the loop.
5485 INSN is the insn containing X. */
5487 static int
5488 valid_initial_value_p (rtx x, rtx insn, int call_seen, rtx loop_start)
5490 if (CONSTANT_P (x))
5491 return 1;
5493 /* Only consider pseudos we know about initialized in insns whose luids
5494 we know. */
5495 if (GET_CODE (x) != REG
5496 || REGNO (x) >= max_reg_before_loop)
5497 return 0;
5499 /* Don't use call-clobbered registers across a call which clobbers it. On
5500 some machines, don't use any hard registers at all. */
5501 if (REGNO (x) < FIRST_PSEUDO_REGISTER
5502 && (SMALL_REGISTER_CLASSES
5503 || (call_used_regs[REGNO (x)] && call_seen)))
5504 return 0;
5506 /* Don't use registers that have been clobbered before the start of the
5507 loop. */
5508 if (reg_set_between_p (x, insn, loop_start))
5509 return 0;
5511 return 1;
5514 /* Scan X for memory refs and check each memory address
5515 as a possible giv. INSN is the insn whose pattern X comes from.
5516 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5517 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
5518 more than once in each loop iteration. */
5520 static void
5521 find_mem_givs (const struct loop *loop, rtx x, rtx insn,
5522 int not_every_iteration, int maybe_multiple)
5524 int i, j;
5525 enum rtx_code code;
5526 const char *fmt;
5528 if (x == 0)
5529 return;
5531 code = GET_CODE (x);
5532 switch (code)
5534 case REG:
5535 case CONST_INT:
5536 case CONST:
5537 case CONST_DOUBLE:
5538 case SYMBOL_REF:
5539 case LABEL_REF:
5540 case PC:
5541 case CC0:
5542 case ADDR_VEC:
5543 case ADDR_DIFF_VEC:
5544 case USE:
5545 case CLOBBER:
5546 return;
5548 case MEM:
5550 rtx src_reg;
5551 rtx add_val;
5552 rtx mult_val;
5553 rtx ext_val;
5554 int benefit;
5556 /* This code used to disable creating GIVs with mult_val == 1 and
5557 add_val == 0. However, this leads to lost optimizations when
5558 it comes time to combine a set of related DEST_ADDR GIVs, since
5559 this one would not be seen. */
5561 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
5562 &mult_val, &ext_val, 1, &benefit,
5563 GET_MODE (x)))
5565 /* Found one; record it. */
5566 struct induction *v = xmalloc (sizeof (struct induction));
5568 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
5569 add_val, ext_val, benefit, DEST_ADDR,
5570 not_every_iteration, maybe_multiple, &XEXP (x, 0));
5572 v->mem = x;
5575 return;
5577 default:
5578 break;
5581 /* Recursively scan the subexpressions for other mem refs. */
5583 fmt = GET_RTX_FORMAT (code);
5584 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5585 if (fmt[i] == 'e')
5586 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
5587 maybe_multiple);
5588 else if (fmt[i] == 'E')
5589 for (j = 0; j < XVECLEN (x, i); j++)
5590 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
5591 maybe_multiple);
5594 /* Fill in the data about one biv update.
5595 V is the `struct induction' in which we record the biv. (It is
5596 allocated by the caller, with alloca.)
5597 INSN is the insn that sets it.
5598 DEST_REG is the biv's reg.
5600 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5601 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5602 being set to INC_VAL.
5604 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5605 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5606 can be executed more than once per iteration. If MAYBE_MULTIPLE
5607 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5608 executed exactly once per iteration. */
5610 static void
5611 record_biv (struct loop *loop, struct induction *v, rtx insn, rtx dest_reg,
5612 rtx inc_val, rtx mult_val, rtx *location,
5613 int not_every_iteration, int maybe_multiple)
5615 struct loop_ivs *ivs = LOOP_IVS (loop);
5616 struct iv_class *bl;
5618 v->insn = insn;
5619 v->src_reg = dest_reg;
5620 v->dest_reg = dest_reg;
5621 v->mult_val = mult_val;
5622 v->add_val = inc_val;
5623 v->ext_dependent = NULL_RTX;
5624 v->location = location;
5625 v->mode = GET_MODE (dest_reg);
5626 v->always_computable = ! not_every_iteration;
5627 v->always_executed = ! not_every_iteration;
5628 v->maybe_multiple = maybe_multiple;
5629 v->same = 0;
5631 /* Add this to the reg's iv_class, creating a class
5632 if this is the first incrementation of the reg. */
5634 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
5635 if (bl == 0)
5637 /* Create and initialize new iv_class. */
5639 bl = xmalloc (sizeof (struct iv_class));
5641 bl->regno = REGNO (dest_reg);
5642 bl->biv = 0;
5643 bl->giv = 0;
5644 bl->biv_count = 0;
5645 bl->giv_count = 0;
5647 /* Set initial value to the reg itself. */
5648 bl->initial_value = dest_reg;
5649 bl->final_value = 0;
5650 /* We haven't seen the initializing insn yet */
5651 bl->init_insn = 0;
5652 bl->init_set = 0;
5653 bl->initial_test = 0;
5654 bl->incremented = 0;
5655 bl->eliminable = 0;
5656 bl->nonneg = 0;
5657 bl->reversed = 0;
5658 bl->total_benefit = 0;
5660 /* Add this class to ivs->list. */
5661 bl->next = ivs->list;
5662 ivs->list = bl;
5664 /* Put it in the array of biv register classes. */
5665 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
5667 else
5669 /* Check if location is the same as a previous one. */
5670 struct induction *induction;
5671 for (induction = bl->biv; induction; induction = induction->next_iv)
5672 if (location == induction->location)
5674 v->same = induction;
5675 break;
5679 /* Update IV_CLASS entry for this biv. */
5680 v->next_iv = bl->biv;
5681 bl->biv = v;
5682 bl->biv_count++;
5683 if (mult_val == const1_rtx)
5684 bl->incremented = 1;
5686 if (loop_dump_stream)
5687 loop_biv_dump (v, loop_dump_stream, 0);
5690 /* Fill in the data about one giv.
5691 V is the `struct induction' in which we record the giv. (It is
5692 allocated by the caller, with alloca.)
5693 INSN is the insn that sets it.
5694 BENEFIT estimates the savings from deleting this insn.
5695 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5696 into a register or is used as a memory address.
5698 SRC_REG is the biv reg which the giv is computed from.
5699 DEST_REG is the giv's reg (if the giv is stored in a reg).
5700 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5701 LOCATION points to the place where this giv's value appears in INSN. */
5703 static void
5704 record_giv (const struct loop *loop, struct induction *v, rtx insn,
5705 rtx src_reg, rtx dest_reg, rtx mult_val, rtx add_val,
5706 rtx ext_val, int benefit, enum g_types type,
5707 int not_every_iteration, int maybe_multiple, rtx *location)
5709 struct loop_ivs *ivs = LOOP_IVS (loop);
5710 struct induction *b;
5711 struct iv_class *bl;
5712 rtx set = single_set (insn);
5713 rtx temp;
5715 /* Attempt to prove constantness of the values. Don't let simplify_rtx
5716 undo the MULT canonicalization that we performed earlier. */
5717 temp = simplify_rtx (add_val);
5718 if (temp
5719 && ! (GET_CODE (add_val) == MULT
5720 && GET_CODE (temp) == ASHIFT))
5721 add_val = temp;
5723 v->insn = insn;
5724 v->src_reg = src_reg;
5725 v->giv_type = type;
5726 v->dest_reg = dest_reg;
5727 v->mult_val = mult_val;
5728 v->add_val = add_val;
5729 v->ext_dependent = ext_val;
5730 v->benefit = benefit;
5731 v->location = location;
5732 v->cant_derive = 0;
5733 v->combined_with = 0;
5734 v->maybe_multiple = maybe_multiple;
5735 v->maybe_dead = 0;
5736 v->derive_adjustment = 0;
5737 v->same = 0;
5738 v->ignore = 0;
5739 v->new_reg = 0;
5740 v->final_value = 0;
5741 v->same_insn = 0;
5742 v->auto_inc_opt = 0;
5743 v->unrolled = 0;
5744 v->shared = 0;
5746 /* The v->always_computable field is used in update_giv_derive, to
5747 determine whether a giv can be used to derive another giv. For a
5748 DEST_REG giv, INSN computes a new value for the giv, so its value
5749 isn't computable if INSN insn't executed every iteration.
5750 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5751 it does not compute a new value. Hence the value is always computable
5752 regardless of whether INSN is executed each iteration. */
5754 if (type == DEST_ADDR)
5755 v->always_computable = 1;
5756 else
5757 v->always_computable = ! not_every_iteration;
5759 v->always_executed = ! not_every_iteration;
5761 if (type == DEST_ADDR)
5763 v->mode = GET_MODE (*location);
5764 v->lifetime = 1;
5766 else /* type == DEST_REG */
5768 v->mode = GET_MODE (SET_DEST (set));
5770 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
5772 /* If the lifetime is zero, it means that this register is
5773 really a dead store. So mark this as a giv that can be
5774 ignored. This will not prevent the biv from being eliminated. */
5775 if (v->lifetime == 0)
5776 v->ignore = 1;
5778 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
5779 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
5782 /* Add the giv to the class of givs computed from one biv. */
5784 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
5785 if (bl)
5787 v->next_iv = bl->giv;
5788 bl->giv = v;
5789 /* Don't count DEST_ADDR. This is supposed to count the number of
5790 insns that calculate givs. */
5791 if (type == DEST_REG)
5792 bl->giv_count++;
5793 bl->total_benefit += benefit;
5795 else
5796 /* Fatal error, biv missing for this giv? */
5797 abort ();
5799 if (type == DEST_ADDR)
5801 v->replaceable = 1;
5802 v->not_replaceable = 0;
5804 else
5806 /* The giv can be replaced outright by the reduced register only if all
5807 of the following conditions are true:
5808 - the insn that sets the giv is always executed on any iteration
5809 on which the giv is used at all
5810 (there are two ways to deduce this:
5811 either the insn is executed on every iteration,
5812 or all uses follow that insn in the same basic block),
5813 - the giv is not used outside the loop
5814 - no assignments to the biv occur during the giv's lifetime. */
5816 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
5817 /* Previous line always fails if INSN was moved by loop opt. */
5818 && REGNO_LAST_LUID (REGNO (dest_reg))
5819 < INSN_LUID (loop->end)
5820 && (! not_every_iteration
5821 || last_use_this_basic_block (dest_reg, insn)))
5823 /* Now check that there are no assignments to the biv within the
5824 giv's lifetime. This requires two separate checks. */
5826 /* Check each biv update, and fail if any are between the first
5827 and last use of the giv.
5829 If this loop contains an inner loop that was unrolled, then
5830 the insn modifying the biv may have been emitted by the loop
5831 unrolling code, and hence does not have a valid luid. Just
5832 mark the biv as not replaceable in this case. It is not very
5833 useful as a biv, because it is used in two different loops.
5834 It is very unlikely that we would be able to optimize the giv
5835 using this biv anyways. */
5837 v->replaceable = 1;
5838 v->not_replaceable = 0;
5839 for (b = bl->biv; b; b = b->next_iv)
5841 if (INSN_UID (b->insn) >= max_uid_for_loop
5842 || ((INSN_LUID (b->insn)
5843 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
5844 && (INSN_LUID (b->insn)
5845 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
5847 v->replaceable = 0;
5848 v->not_replaceable = 1;
5849 break;
5853 /* If there are any backwards branches that go from after the
5854 biv update to before it, then this giv is not replaceable. */
5855 if (v->replaceable)
5856 for (b = bl->biv; b; b = b->next_iv)
5857 if (back_branch_in_range_p (loop, b->insn))
5859 v->replaceable = 0;
5860 v->not_replaceable = 1;
5861 break;
5864 else
5866 /* May still be replaceable, we don't have enough info here to
5867 decide. */
5868 v->replaceable = 0;
5869 v->not_replaceable = 0;
5873 /* Record whether the add_val contains a const_int, for later use by
5874 combine_givs. */
5876 rtx tem = add_val;
5878 v->no_const_addval = 1;
5879 if (tem == const0_rtx)
5881 else if (CONSTANT_P (add_val))
5882 v->no_const_addval = 0;
5883 if (GET_CODE (tem) == PLUS)
5885 while (1)
5887 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5888 tem = XEXP (tem, 0);
5889 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5890 tem = XEXP (tem, 1);
5891 else
5892 break;
5894 if (CONSTANT_P (XEXP (tem, 1)))
5895 v->no_const_addval = 0;
5899 if (loop_dump_stream)
5900 loop_giv_dump (v, loop_dump_stream, 0);
5903 /* All this does is determine whether a giv can be made replaceable because
5904 its final value can be calculated. This code can not be part of record_giv
5905 above, because final_giv_value requires that the number of loop iterations
5906 be known, and that can not be accurately calculated until after all givs
5907 have been identified. */
5909 static void
5910 check_final_value (const struct loop *loop, struct induction *v)
5912 rtx final_value = 0;
5914 /* DEST_ADDR givs will never reach here, because they are always marked
5915 replaceable above in record_giv. */
5917 /* The giv can be replaced outright by the reduced register only if all
5918 of the following conditions are true:
5919 - the insn that sets the giv is always executed on any iteration
5920 on which the giv is used at all
5921 (there are two ways to deduce this:
5922 either the insn is executed on every iteration,
5923 or all uses follow that insn in the same basic block),
5924 - its final value can be calculated (this condition is different
5925 than the one above in record_giv)
5926 - it's not used before the it's set
5927 - no assignments to the biv occur during the giv's lifetime. */
5929 #if 0
5930 /* This is only called now when replaceable is known to be false. */
5931 /* Clear replaceable, so that it won't confuse final_giv_value. */
5932 v->replaceable = 0;
5933 #endif
5935 if ((final_value = final_giv_value (loop, v))
5936 && (v->always_executed
5937 || last_use_this_basic_block (v->dest_reg, v->insn)))
5939 int biv_increment_seen = 0, before_giv_insn = 0;
5940 rtx p = v->insn;
5941 rtx last_giv_use;
5943 v->replaceable = 1;
5944 v->not_replaceable = 0;
5946 /* When trying to determine whether or not a biv increment occurs
5947 during the lifetime of the giv, we can ignore uses of the variable
5948 outside the loop because final_value is true. Hence we can not
5949 use regno_last_uid and regno_first_uid as above in record_giv. */
5951 /* Search the loop to determine whether any assignments to the
5952 biv occur during the giv's lifetime. Start with the insn
5953 that sets the giv, and search around the loop until we come
5954 back to that insn again.
5956 Also fail if there is a jump within the giv's lifetime that jumps
5957 to somewhere outside the lifetime but still within the loop. This
5958 catches spaghetti code where the execution order is not linear, and
5959 hence the above test fails. Here we assume that the giv lifetime
5960 does not extend from one iteration of the loop to the next, so as
5961 to make the test easier. Since the lifetime isn't known yet,
5962 this requires two loops. See also record_giv above. */
5964 last_giv_use = v->insn;
5966 while (1)
5968 p = NEXT_INSN (p);
5969 if (p == loop->end)
5971 before_giv_insn = 1;
5972 p = NEXT_INSN (loop->start);
5974 if (p == v->insn)
5975 break;
5977 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5978 || GET_CODE (p) == CALL_INSN)
5980 /* It is possible for the BIV increment to use the GIV if we
5981 have a cycle. Thus we must be sure to check each insn for
5982 both BIV and GIV uses, and we must check for BIV uses
5983 first. */
5985 if (! biv_increment_seen
5986 && reg_set_p (v->src_reg, PATTERN (p)))
5987 biv_increment_seen = 1;
5989 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5991 if (biv_increment_seen || before_giv_insn)
5993 v->replaceable = 0;
5994 v->not_replaceable = 1;
5995 break;
5997 last_giv_use = p;
6002 /* Now that the lifetime of the giv is known, check for branches
6003 from within the lifetime to outside the lifetime if it is still
6004 replaceable. */
6006 if (v->replaceable)
6008 p = v->insn;
6009 while (1)
6011 p = NEXT_INSN (p);
6012 if (p == loop->end)
6013 p = NEXT_INSN (loop->start);
6014 if (p == last_giv_use)
6015 break;
6017 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
6018 && LABEL_NAME (JUMP_LABEL (p))
6019 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
6020 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
6021 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
6022 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
6024 v->replaceable = 0;
6025 v->not_replaceable = 1;
6027 if (loop_dump_stream)
6028 fprintf (loop_dump_stream,
6029 "Found branch outside giv lifetime.\n");
6031 break;
6036 /* If it is replaceable, then save the final value. */
6037 if (v->replaceable)
6038 v->final_value = final_value;
6041 if (loop_dump_stream && v->replaceable)
6042 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
6043 INSN_UID (v->insn), REGNO (v->dest_reg));
6046 /* Update the status of whether a giv can derive other givs.
6048 We need to do something special if there is or may be an update to the biv
6049 between the time the giv is defined and the time it is used to derive
6050 another giv.
6052 In addition, a giv that is only conditionally set is not allowed to
6053 derive another giv once a label has been passed.
6055 The cases we look at are when a label or an update to a biv is passed. */
6057 static void
6058 update_giv_derive (const struct loop *loop, rtx p)
6060 struct loop_ivs *ivs = LOOP_IVS (loop);
6061 struct iv_class *bl;
6062 struct induction *biv, *giv;
6063 rtx tem;
6064 int dummy;
6066 /* Search all IV classes, then all bivs, and finally all givs.
6068 There are three cases we are concerned with. First we have the situation
6069 of a giv that is only updated conditionally. In that case, it may not
6070 derive any givs after a label is passed.
6072 The second case is when a biv update occurs, or may occur, after the
6073 definition of a giv. For certain biv updates (see below) that are
6074 known to occur between the giv definition and use, we can adjust the
6075 giv definition. For others, or when the biv update is conditional,
6076 we must prevent the giv from deriving any other givs. There are two
6077 sub-cases within this case.
6079 If this is a label, we are concerned with any biv update that is done
6080 conditionally, since it may be done after the giv is defined followed by
6081 a branch here (actually, we need to pass both a jump and a label, but
6082 this extra tracking doesn't seem worth it).
6084 If this is a jump, we are concerned about any biv update that may be
6085 executed multiple times. We are actually only concerned about
6086 backward jumps, but it is probably not worth performing the test
6087 on the jump again here.
6089 If this is a biv update, we must adjust the giv status to show that a
6090 subsequent biv update was performed. If this adjustment cannot be done,
6091 the giv cannot derive further givs. */
6093 for (bl = ivs->list; bl; bl = bl->next)
6094 for (biv = bl->biv; biv; biv = biv->next_iv)
6095 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
6096 || biv->insn == p)
6098 for (giv = bl->giv; giv; giv = giv->next_iv)
6100 /* If cant_derive is already true, there is no point in
6101 checking all of these conditions again. */
6102 if (giv->cant_derive)
6103 continue;
6105 /* If this giv is conditionally set and we have passed a label,
6106 it cannot derive anything. */
6107 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
6108 giv->cant_derive = 1;
6110 /* Skip givs that have mult_val == 0, since
6111 they are really invariants. Also skip those that are
6112 replaceable, since we know their lifetime doesn't contain
6113 any biv update. */
6114 else if (giv->mult_val == const0_rtx || giv->replaceable)
6115 continue;
6117 /* The only way we can allow this giv to derive another
6118 is if this is a biv increment and we can form the product
6119 of biv->add_val and giv->mult_val. In this case, we will
6120 be able to compute a compensation. */
6121 else if (biv->insn == p)
6123 rtx ext_val_dummy;
6125 tem = 0;
6126 if (biv->mult_val == const1_rtx)
6127 tem = simplify_giv_expr (loop,
6128 gen_rtx_MULT (giv->mode,
6129 biv->add_val,
6130 giv->mult_val),
6131 &ext_val_dummy, &dummy);
6133 if (tem && giv->derive_adjustment)
6134 tem = simplify_giv_expr
6135 (loop,
6136 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
6137 &ext_val_dummy, &dummy);
6139 if (tem)
6140 giv->derive_adjustment = tem;
6141 else
6142 giv->cant_derive = 1;
6144 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
6145 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
6146 giv->cant_derive = 1;
6151 /* Check whether an insn is an increment legitimate for a basic induction var.
6152 X is the source of insn P, or a part of it.
6153 MODE is the mode in which X should be interpreted.
6155 DEST_REG is the putative biv, also the destination of the insn.
6156 We accept patterns of these forms:
6157 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
6158 REG = INVARIANT + REG
6160 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
6161 store the additive term into *INC_VAL, and store the place where
6162 we found the additive term into *LOCATION.
6164 If X is an assignment of an invariant into DEST_REG, we set
6165 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
6167 We also want to detect a BIV when it corresponds to a variable
6168 whose mode was promoted via PROMOTED_MODE. In that case, an increment
6169 of the variable may be a PLUS that adds a SUBREG of that variable to
6170 an invariant and then sign- or zero-extends the result of the PLUS
6171 into the variable.
6173 Most GIVs in such cases will be in the promoted mode, since that is the
6174 probably the natural computation mode (and almost certainly the mode
6175 used for addresses) on the machine. So we view the pseudo-reg containing
6176 the variable as the BIV, as if it were simply incremented.
6178 Note that treating the entire pseudo as a BIV will result in making
6179 simple increments to any GIVs based on it. However, if the variable
6180 overflows in its declared mode but not its promoted mode, the result will
6181 be incorrect. This is acceptable if the variable is signed, since
6182 overflows in such cases are undefined, but not if it is unsigned, since
6183 those overflows are defined. So we only check for SIGN_EXTEND and
6184 not ZERO_EXTEND.
6186 If we cannot find a biv, we return 0. */
6188 static int
6189 basic_induction_var (const struct loop *loop, rtx x, enum machine_mode mode,
6190 rtx dest_reg, rtx p, rtx *inc_val, rtx *mult_val,
6191 rtx **location)
6193 enum rtx_code code;
6194 rtx *argp, arg;
6195 rtx insn, set = 0, last, inc;
6197 code = GET_CODE (x);
6198 *location = NULL;
6199 switch (code)
6201 case PLUS:
6202 if (rtx_equal_p (XEXP (x, 0), dest_reg)
6203 || (GET_CODE (XEXP (x, 0)) == SUBREG
6204 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
6205 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
6207 argp = &XEXP (x, 1);
6209 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
6210 || (GET_CODE (XEXP (x, 1)) == SUBREG
6211 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
6212 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
6214 argp = &XEXP (x, 0);
6216 else
6217 return 0;
6219 arg = *argp;
6220 if (loop_invariant_p (loop, arg) != 1)
6221 return 0;
6223 /* convert_modes can emit new instructions, e.g. when arg is a loop
6224 invariant MEM and dest_reg has a different mode.
6225 These instructions would be emitted after the end of the function
6226 and then *inc_val would be an unitialized pseudo.
6227 Detect this and bail in this case.
6228 Other alternatives to solve this can be introducing a convert_modes
6229 variant which is allowed to fail but not allowed to emit new
6230 instructions, emit these instructions before loop start and let
6231 it be garbage collected if *inc_val is never used or saving the
6232 *inc_val initialization sequence generated here and when *inc_val
6233 is going to be actually used, emit it at some suitable place. */
6234 last = get_last_insn ();
6235 inc = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
6236 if (get_last_insn () != last)
6238 delete_insns_since (last);
6239 return 0;
6242 *inc_val = inc;
6243 *mult_val = const1_rtx;
6244 *location = argp;
6245 return 1;
6247 case SUBREG:
6248 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
6249 handle addition of promoted variables.
6250 ??? The comment at the start of this function is wrong: promoted
6251 variable increments don't look like it says they do. */
6252 return basic_induction_var (loop, SUBREG_REG (x),
6253 GET_MODE (SUBREG_REG (x)),
6254 dest_reg, p, inc_val, mult_val, location);
6256 case REG:
6257 /* If this register is assigned in a previous insn, look at its
6258 source, but don't go outside the loop or past a label. */
6260 /* If this sets a register to itself, we would repeat any previous
6261 biv increment if we applied this strategy blindly. */
6262 if (rtx_equal_p (dest_reg, x))
6263 return 0;
6265 insn = p;
6266 while (1)
6268 rtx dest;
6271 insn = PREV_INSN (insn);
6273 while (insn && GET_CODE (insn) == NOTE
6274 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6276 if (!insn)
6277 break;
6278 set = single_set (insn);
6279 if (set == 0)
6280 break;
6281 dest = SET_DEST (set);
6282 if (dest == x
6283 || (GET_CODE (dest) == SUBREG
6284 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
6285 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
6286 && SUBREG_REG (dest) == x))
6287 return basic_induction_var (loop, SET_SRC (set),
6288 (GET_MODE (SET_SRC (set)) == VOIDmode
6289 ? GET_MODE (x)
6290 : GET_MODE (SET_SRC (set))),
6291 dest_reg, insn,
6292 inc_val, mult_val, location);
6294 while (GET_CODE (dest) == SIGN_EXTRACT
6295 || GET_CODE (dest) == ZERO_EXTRACT
6296 || GET_CODE (dest) == SUBREG
6297 || GET_CODE (dest) == STRICT_LOW_PART)
6298 dest = XEXP (dest, 0);
6299 if (dest == x)
6300 break;
6302 /* Fall through. */
6304 /* Can accept constant setting of biv only when inside inner most loop.
6305 Otherwise, a biv of an inner loop may be incorrectly recognized
6306 as a biv of the outer loop,
6307 causing code to be moved INTO the inner loop. */
6308 case MEM:
6309 if (loop_invariant_p (loop, x) != 1)
6310 return 0;
6311 case CONST_INT:
6312 case SYMBOL_REF:
6313 case CONST:
6314 /* convert_modes aborts if we try to convert to or from CCmode, so just
6315 exclude that case. It is very unlikely that a condition code value
6316 would be a useful iterator anyways. convert_modes aborts if we try to
6317 convert a float mode to non-float or vice versa too. */
6318 if (loop->level == 1
6319 && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
6320 && GET_MODE_CLASS (mode) != MODE_CC)
6322 /* Possible bug here? Perhaps we don't know the mode of X. */
6323 last = get_last_insn ();
6324 inc = convert_modes (GET_MODE (dest_reg), mode, x, 0);
6325 if (get_last_insn () != last)
6327 delete_insns_since (last);
6328 return 0;
6331 *inc_val = inc;
6332 *mult_val = const0_rtx;
6333 return 1;
6335 else
6336 return 0;
6338 case SIGN_EXTEND:
6339 /* Ignore this BIV if signed arithmetic overflow is defined. */
6340 if (flag_wrapv)
6341 return 0;
6342 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6343 dest_reg, p, inc_val, mult_val, location);
6345 case ASHIFTRT:
6346 /* Similar, since this can be a sign extension. */
6347 for (insn = PREV_INSN (p);
6348 (insn && GET_CODE (insn) == NOTE
6349 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6350 insn = PREV_INSN (insn))
6353 if (insn)
6354 set = single_set (insn);
6356 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
6357 && set && SET_DEST (set) == XEXP (x, 0)
6358 && GET_CODE (XEXP (x, 1)) == CONST_INT
6359 && INTVAL (XEXP (x, 1)) >= 0
6360 && GET_CODE (SET_SRC (set)) == ASHIFT
6361 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
6362 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
6363 GET_MODE (XEXP (x, 0)),
6364 dest_reg, insn, inc_val, mult_val,
6365 location);
6366 return 0;
6368 default:
6369 return 0;
6373 /* A general induction variable (giv) is any quantity that is a linear
6374 function of a basic induction variable,
6375 i.e. giv = biv * mult_val + add_val.
6376 The coefficients can be any loop invariant quantity.
6377 A giv need not be computed directly from the biv;
6378 it can be computed by way of other givs. */
6380 /* Determine whether X computes a giv.
6381 If it does, return a nonzero value
6382 which is the benefit from eliminating the computation of X;
6383 set *SRC_REG to the register of the biv that it is computed from;
6384 set *ADD_VAL and *MULT_VAL to the coefficients,
6385 such that the value of X is biv * mult + add; */
6387 static int
6388 general_induction_var (const struct loop *loop, rtx x, rtx *src_reg,
6389 rtx *add_val, rtx *mult_val, rtx *ext_val,
6390 int is_addr, int *pbenefit,
6391 enum machine_mode addr_mode)
6393 struct loop_ivs *ivs = LOOP_IVS (loop);
6394 rtx orig_x = x;
6396 /* If this is an invariant, forget it, it isn't a giv. */
6397 if (loop_invariant_p (loop, x) == 1)
6398 return 0;
6400 *pbenefit = 0;
6401 *ext_val = NULL_RTX;
6402 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
6403 if (x == 0)
6404 return 0;
6406 switch (GET_CODE (x))
6408 case USE:
6409 case CONST_INT:
6410 /* Since this is now an invariant and wasn't before, it must be a giv
6411 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6412 with. */
6413 *src_reg = ivs->list->biv->dest_reg;
6414 *mult_val = const0_rtx;
6415 *add_val = x;
6416 break;
6418 case REG:
6419 /* This is equivalent to a BIV. */
6420 *src_reg = x;
6421 *mult_val = const1_rtx;
6422 *add_val = const0_rtx;
6423 break;
6425 case PLUS:
6426 /* Either (plus (biv) (invar)) or
6427 (plus (mult (biv) (invar_1)) (invar_2)). */
6428 if (GET_CODE (XEXP (x, 0)) == MULT)
6430 *src_reg = XEXP (XEXP (x, 0), 0);
6431 *mult_val = XEXP (XEXP (x, 0), 1);
6433 else
6435 *src_reg = XEXP (x, 0);
6436 *mult_val = const1_rtx;
6438 *add_val = XEXP (x, 1);
6439 break;
6441 case MULT:
6442 /* ADD_VAL is zero. */
6443 *src_reg = XEXP (x, 0);
6444 *mult_val = XEXP (x, 1);
6445 *add_val = const0_rtx;
6446 break;
6448 default:
6449 abort ();
6452 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6453 unless they are CONST_INT). */
6454 if (GET_CODE (*add_val) == USE)
6455 *add_val = XEXP (*add_val, 0);
6456 if (GET_CODE (*mult_val) == USE)
6457 *mult_val = XEXP (*mult_val, 0);
6459 if (is_addr)
6460 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
6461 else
6462 *pbenefit += rtx_cost (orig_x, SET);
6464 /* Always return true if this is a giv so it will be detected as such,
6465 even if the benefit is zero or negative. This allows elimination
6466 of bivs that might otherwise not be eliminated. */
6467 return 1;
6470 /* Given an expression, X, try to form it as a linear function of a biv.
6471 We will canonicalize it to be of the form
6472 (plus (mult (BIV) (invar_1))
6473 (invar_2))
6474 with possible degeneracies.
6476 The invariant expressions must each be of a form that can be used as a
6477 machine operand. We surround then with a USE rtx (a hack, but localized
6478 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6479 routine; it is the caller's responsibility to strip them.
6481 If no such canonicalization is possible (i.e., two biv's are used or an
6482 expression that is neither invariant nor a biv or giv), this routine
6483 returns 0.
6485 For a nonzero return, the result will have a code of CONST_INT, USE,
6486 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6488 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6490 static rtx sge_plus (enum machine_mode, rtx, rtx);
6491 static rtx sge_plus_constant (rtx, rtx);
6493 static rtx
6494 simplify_giv_expr (const struct loop *loop, rtx x, rtx *ext_val, int *benefit)
6496 struct loop_ivs *ivs = LOOP_IVS (loop);
6497 struct loop_regs *regs = LOOP_REGS (loop);
6498 enum machine_mode mode = GET_MODE (x);
6499 rtx arg0, arg1;
6500 rtx tem;
6502 /* If this is not an integer mode, or if we cannot do arithmetic in this
6503 mode, this can't be a giv. */
6504 if (mode != VOIDmode
6505 && (GET_MODE_CLASS (mode) != MODE_INT
6506 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
6507 return NULL_RTX;
6509 switch (GET_CODE (x))
6511 case PLUS:
6512 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6513 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6514 if (arg0 == 0 || arg1 == 0)
6515 return NULL_RTX;
6517 /* Put constant last, CONST_INT last if both constant. */
6518 if ((GET_CODE (arg0) == USE
6519 || GET_CODE (arg0) == CONST_INT)
6520 && ! ((GET_CODE (arg0) == USE
6521 && GET_CODE (arg1) == USE)
6522 || GET_CODE (arg1) == CONST_INT))
6523 tem = arg0, arg0 = arg1, arg1 = tem;
6525 /* Handle addition of zero, then addition of an invariant. */
6526 if (arg1 == const0_rtx)
6527 return arg0;
6528 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6529 switch (GET_CODE (arg0))
6531 case CONST_INT:
6532 case USE:
6533 /* Adding two invariants must result in an invariant, so enclose
6534 addition operation inside a USE and return it. */
6535 if (GET_CODE (arg0) == USE)
6536 arg0 = XEXP (arg0, 0);
6537 if (GET_CODE (arg1) == USE)
6538 arg1 = XEXP (arg1, 0);
6540 if (GET_CODE (arg0) == CONST_INT)
6541 tem = arg0, arg0 = arg1, arg1 = tem;
6542 if (GET_CODE (arg1) == CONST_INT)
6543 tem = sge_plus_constant (arg0, arg1);
6544 else
6545 tem = sge_plus (mode, arg0, arg1);
6547 if (GET_CODE (tem) != CONST_INT)
6548 tem = gen_rtx_USE (mode, tem);
6549 return tem;
6551 case REG:
6552 case MULT:
6553 /* biv + invar or mult + invar. Return sum. */
6554 return gen_rtx_PLUS (mode, arg0, arg1);
6556 case PLUS:
6557 /* (a + invar_1) + invar_2. Associate. */
6558 return
6559 simplify_giv_expr (loop,
6560 gen_rtx_PLUS (mode,
6561 XEXP (arg0, 0),
6562 gen_rtx_PLUS (mode,
6563 XEXP (arg0, 1),
6564 arg1)),
6565 ext_val, benefit);
6567 default:
6568 abort ();
6571 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6572 MULT to reduce cases. */
6573 if (GET_CODE (arg0) == REG)
6574 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
6575 if (GET_CODE (arg1) == REG)
6576 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
6578 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6579 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6580 Recurse to associate the second PLUS. */
6581 if (GET_CODE (arg1) == MULT)
6582 tem = arg0, arg0 = arg1, arg1 = tem;
6584 if (GET_CODE (arg1) == PLUS)
6585 return
6586 simplify_giv_expr (loop,
6587 gen_rtx_PLUS (mode,
6588 gen_rtx_PLUS (mode, arg0,
6589 XEXP (arg1, 0)),
6590 XEXP (arg1, 1)),
6591 ext_val, benefit);
6593 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6594 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
6595 return NULL_RTX;
6597 if (!rtx_equal_p (arg0, arg1))
6598 return NULL_RTX;
6600 return simplify_giv_expr (loop,
6601 gen_rtx_MULT (mode,
6602 XEXP (arg0, 0),
6603 gen_rtx_PLUS (mode,
6604 XEXP (arg0, 1),
6605 XEXP (arg1, 1))),
6606 ext_val, benefit);
6608 case MINUS:
6609 /* Handle "a - b" as "a + b * (-1)". */
6610 return simplify_giv_expr (loop,
6611 gen_rtx_PLUS (mode,
6612 XEXP (x, 0),
6613 gen_rtx_MULT (mode,
6614 XEXP (x, 1),
6615 constm1_rtx)),
6616 ext_val, benefit);
6618 case MULT:
6619 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6620 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6621 if (arg0 == 0 || arg1 == 0)
6622 return NULL_RTX;
6624 /* Put constant last, CONST_INT last if both constant. */
6625 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6626 && GET_CODE (arg1) != CONST_INT)
6627 tem = arg0, arg0 = arg1, arg1 = tem;
6629 /* If second argument is not now constant, not giv. */
6630 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
6631 return NULL_RTX;
6633 /* Handle multiply by 0 or 1. */
6634 if (arg1 == const0_rtx)
6635 return const0_rtx;
6637 else if (arg1 == const1_rtx)
6638 return arg0;
6640 switch (GET_CODE (arg0))
6642 case REG:
6643 /* biv * invar. Done. */
6644 return gen_rtx_MULT (mode, arg0, arg1);
6646 case CONST_INT:
6647 /* Product of two constants. */
6648 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
6650 case USE:
6651 /* invar * invar is a giv, but attempt to simplify it somehow. */
6652 if (GET_CODE (arg1) != CONST_INT)
6653 return NULL_RTX;
6655 arg0 = XEXP (arg0, 0);
6656 if (GET_CODE (arg0) == MULT)
6658 /* (invar_0 * invar_1) * invar_2. Associate. */
6659 return simplify_giv_expr (loop,
6660 gen_rtx_MULT (mode,
6661 XEXP (arg0, 0),
6662 gen_rtx_MULT (mode,
6663 XEXP (arg0,
6665 arg1)),
6666 ext_val, benefit);
6668 /* Propagate the MULT expressions to the intermost nodes. */
6669 else if (GET_CODE (arg0) == PLUS)
6671 /* (invar_0 + invar_1) * invar_2. Distribute. */
6672 return simplify_giv_expr (loop,
6673 gen_rtx_PLUS (mode,
6674 gen_rtx_MULT (mode,
6675 XEXP (arg0,
6677 arg1),
6678 gen_rtx_MULT (mode,
6679 XEXP (arg0,
6681 arg1)),
6682 ext_val, benefit);
6684 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
6686 case MULT:
6687 /* (a * invar_1) * invar_2. Associate. */
6688 return simplify_giv_expr (loop,
6689 gen_rtx_MULT (mode,
6690 XEXP (arg0, 0),
6691 gen_rtx_MULT (mode,
6692 XEXP (arg0, 1),
6693 arg1)),
6694 ext_val, benefit);
6696 case PLUS:
6697 /* (a + invar_1) * invar_2. Distribute. */
6698 return simplify_giv_expr (loop,
6699 gen_rtx_PLUS (mode,
6700 gen_rtx_MULT (mode,
6701 XEXP (arg0, 0),
6702 arg1),
6703 gen_rtx_MULT (mode,
6704 XEXP (arg0, 1),
6705 arg1)),
6706 ext_val, benefit);
6708 default:
6709 abort ();
6712 case ASHIFT:
6713 /* Shift by constant is multiply by power of two. */
6714 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6715 return 0;
6717 return
6718 simplify_giv_expr (loop,
6719 gen_rtx_MULT (mode,
6720 XEXP (x, 0),
6721 GEN_INT ((HOST_WIDE_INT) 1
6722 << INTVAL (XEXP (x, 1)))),
6723 ext_val, benefit);
6725 case NEG:
6726 /* "-a" is "a * (-1)" */
6727 return simplify_giv_expr (loop,
6728 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
6729 ext_val, benefit);
6731 case NOT:
6732 /* "~a" is "-a - 1". Silly, but easy. */
6733 return simplify_giv_expr (loop,
6734 gen_rtx_MINUS (mode,
6735 gen_rtx_NEG (mode, XEXP (x, 0)),
6736 const1_rtx),
6737 ext_val, benefit);
6739 case USE:
6740 /* Already in proper form for invariant. */
6741 return x;
6743 case SIGN_EXTEND:
6744 case ZERO_EXTEND:
6745 case TRUNCATE:
6746 /* Conditionally recognize extensions of simple IVs. After we've
6747 computed loop traversal counts and verified the range of the
6748 source IV, we'll reevaluate this as a GIV. */
6749 if (*ext_val == NULL_RTX)
6751 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6752 if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
6754 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
6755 return arg0;
6758 goto do_default;
6760 case REG:
6761 /* If this is a new register, we can't deal with it. */
6762 if (REGNO (x) >= max_reg_before_loop)
6763 return 0;
6765 /* Check for biv or giv. */
6766 switch (REG_IV_TYPE (ivs, REGNO (x)))
6768 case BASIC_INDUCT:
6769 return x;
6770 case GENERAL_INDUCT:
6772 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
6774 /* Form expression from giv and add benefit. Ensure this giv
6775 can derive another and subtract any needed adjustment if so. */
6777 /* Increasing the benefit here is risky. The only case in which it
6778 is arguably correct is if this is the only use of V. In other
6779 cases, this will artificially inflate the benefit of the current
6780 giv, and lead to suboptimal code. Thus, it is disabled, since
6781 potentially not reducing an only marginally beneficial giv is
6782 less harmful than reducing many givs that are not really
6783 beneficial. */
6785 rtx single_use = regs->array[REGNO (x)].single_usage;
6786 if (single_use && single_use != const0_rtx)
6787 *benefit += v->benefit;
6790 if (v->cant_derive)
6791 return 0;
6793 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
6794 v->src_reg, v->mult_val),
6795 v->add_val);
6797 if (v->derive_adjustment)
6798 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
6799 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
6800 if (*ext_val)
6802 if (!v->ext_dependent)
6803 return arg0;
6805 else
6807 *ext_val = v->ext_dependent;
6808 return arg0;
6810 return 0;
6813 default:
6814 do_default:
6815 /* If it isn't an induction variable, and it is invariant, we
6816 may be able to simplify things further by looking through
6817 the bits we just moved outside the loop. */
6818 if (loop_invariant_p (loop, x) == 1)
6820 struct movable *m;
6821 struct loop_movables *movables = LOOP_MOVABLES (loop);
6823 for (m = movables->head; m; m = m->next)
6824 if (rtx_equal_p (x, m->set_dest))
6826 /* Ok, we found a match. Substitute and simplify. */
6828 /* If we match another movable, we must use that, as
6829 this one is going away. */
6830 if (m->match)
6831 return simplify_giv_expr (loop, m->match->set_dest,
6832 ext_val, benefit);
6834 /* If consec is nonzero, this is a member of a group of
6835 instructions that were moved together. We handle this
6836 case only to the point of seeking to the last insn and
6837 looking for a REG_EQUAL. Fail if we don't find one. */
6838 if (m->consec != 0)
6840 int i = m->consec;
6841 tem = m->insn;
6844 tem = NEXT_INSN (tem);
6846 while (--i > 0);
6848 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6849 if (tem)
6850 tem = XEXP (tem, 0);
6852 else
6854 tem = single_set (m->insn);
6855 if (tem)
6856 tem = SET_SRC (tem);
6859 if (tem)
6861 /* What we are most interested in is pointer
6862 arithmetic on invariants -- only take
6863 patterns we may be able to do something with. */
6864 if (GET_CODE (tem) == PLUS
6865 || GET_CODE (tem) == MULT
6866 || GET_CODE (tem) == ASHIFT
6867 || GET_CODE (tem) == CONST_INT
6868 || GET_CODE (tem) == SYMBOL_REF)
6870 tem = simplify_giv_expr (loop, tem, ext_val,
6871 benefit);
6872 if (tem)
6873 return tem;
6875 else if (GET_CODE (tem) == CONST
6876 && GET_CODE (XEXP (tem, 0)) == PLUS
6877 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6878 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6880 tem = simplify_giv_expr (loop, XEXP (tem, 0),
6881 ext_val, benefit);
6882 if (tem)
6883 return tem;
6886 break;
6889 break;
6892 /* Fall through to general case. */
6893 default:
6894 /* If invariant, return as USE (unless CONST_INT).
6895 Otherwise, not giv. */
6896 if (GET_CODE (x) == USE)
6897 x = XEXP (x, 0);
6899 if (loop_invariant_p (loop, x) == 1)
6901 if (GET_CODE (x) == CONST_INT)
6902 return x;
6903 if (GET_CODE (x) == CONST
6904 && GET_CODE (XEXP (x, 0)) == PLUS
6905 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6906 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6907 x = XEXP (x, 0);
6908 return gen_rtx_USE (mode, x);
6910 else
6911 return 0;
6915 /* This routine folds invariants such that there is only ever one
6916 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6918 static rtx
6919 sge_plus_constant (rtx x, rtx c)
6921 if (GET_CODE (x) == CONST_INT)
6922 return GEN_INT (INTVAL (x) + INTVAL (c));
6923 else if (GET_CODE (x) != PLUS)
6924 return gen_rtx_PLUS (GET_MODE (x), x, c);
6925 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6927 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6928 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6930 else if (GET_CODE (XEXP (x, 0)) == PLUS
6931 || GET_CODE (XEXP (x, 1)) != PLUS)
6933 return gen_rtx_PLUS (GET_MODE (x),
6934 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6936 else
6938 return gen_rtx_PLUS (GET_MODE (x),
6939 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6943 static rtx
6944 sge_plus (enum machine_mode mode, rtx x, rtx y)
6946 while (GET_CODE (y) == PLUS)
6948 rtx a = XEXP (y, 0);
6949 if (GET_CODE (a) == CONST_INT)
6950 x = sge_plus_constant (x, a);
6951 else
6952 x = gen_rtx_PLUS (mode, x, a);
6953 y = XEXP (y, 1);
6955 if (GET_CODE (y) == CONST_INT)
6956 x = sge_plus_constant (x, y);
6957 else
6958 x = gen_rtx_PLUS (mode, x, y);
6959 return x;
6962 /* Help detect a giv that is calculated by several consecutive insns;
6963 for example,
6964 giv = biv * M
6965 giv = giv + A
6966 The caller has already identified the first insn P as having a giv as dest;
6967 we check that all other insns that set the same register follow
6968 immediately after P, that they alter nothing else,
6969 and that the result of the last is still a giv.
6971 The value is 0 if the reg set in P is not really a giv.
6972 Otherwise, the value is the amount gained by eliminating
6973 all the consecutive insns that compute the value.
6975 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6976 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6978 The coefficients of the ultimate giv value are stored in
6979 *MULT_VAL and *ADD_VAL. */
6981 static int
6982 consec_sets_giv (const struct loop *loop, int first_benefit, rtx p,
6983 rtx src_reg, rtx dest_reg, rtx *add_val, rtx *mult_val,
6984 rtx *ext_val, rtx *last_consec_insn)
6986 struct loop_ivs *ivs = LOOP_IVS (loop);
6987 struct loop_regs *regs = LOOP_REGS (loop);
6988 int count;
6989 enum rtx_code code;
6990 int benefit;
6991 rtx temp;
6992 rtx set;
6994 /* Indicate that this is a giv so that we can update the value produced in
6995 each insn of the multi-insn sequence.
6997 This induction structure will be used only by the call to
6998 general_induction_var below, so we can allocate it on our stack.
6999 If this is a giv, our caller will replace the induct var entry with
7000 a new induction structure. */
7001 struct induction *v;
7003 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
7004 return 0;
7006 v = alloca (sizeof (struct induction));
7007 v->src_reg = src_reg;
7008 v->mult_val = *mult_val;
7009 v->add_val = *add_val;
7010 v->benefit = first_benefit;
7011 v->cant_derive = 0;
7012 v->derive_adjustment = 0;
7013 v->ext_dependent = NULL_RTX;
7015 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
7016 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
7018 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
7020 while (count > 0)
7022 p = NEXT_INSN (p);
7023 code = GET_CODE (p);
7025 /* If libcall, skip to end of call sequence. */
7026 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
7027 p = XEXP (temp, 0);
7029 if (code == INSN
7030 && (set = single_set (p))
7031 && GET_CODE (SET_DEST (set)) == REG
7032 && SET_DEST (set) == dest_reg
7033 && (general_induction_var (loop, SET_SRC (set), &src_reg,
7034 add_val, mult_val, ext_val, 0,
7035 &benefit, VOIDmode)
7036 /* Giv created by equivalent expression. */
7037 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
7038 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
7039 add_val, mult_val, ext_val, 0,
7040 &benefit, VOIDmode)))
7041 && src_reg == v->src_reg)
7043 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
7044 benefit += libcall_benefit (p);
7046 count--;
7047 v->mult_val = *mult_val;
7048 v->add_val = *add_val;
7049 v->benefit += benefit;
7051 else if (code != NOTE)
7053 /* Allow insns that set something other than this giv to a
7054 constant. Such insns are needed on machines which cannot
7055 include long constants and should not disqualify a giv. */
7056 if (code == INSN
7057 && (set = single_set (p))
7058 && SET_DEST (set) != dest_reg
7059 && CONSTANT_P (SET_SRC (set)))
7060 continue;
7062 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7063 return 0;
7067 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7068 *last_consec_insn = p;
7069 return v->benefit;
7072 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7073 represented by G1. If no such expression can be found, or it is clear that
7074 it cannot possibly be a valid address, 0 is returned.
7076 To perform the computation, we note that
7077 G1 = x * v + a and
7078 G2 = y * v + b
7079 where `v' is the biv.
7081 So G2 = (y/b) * G1 + (b - a*y/x).
7083 Note that MULT = y/x.
7085 Update: A and B are now allowed to be additive expressions such that
7086 B contains all variables in A. That is, computing B-A will not require
7087 subtracting variables. */
7089 static rtx
7090 express_from_1 (rtx a, rtx b, rtx mult)
7092 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
7094 if (mult == const0_rtx)
7095 return b;
7097 /* If MULT is not 1, we cannot handle A with non-constants, since we
7098 would then be required to subtract multiples of the registers in A.
7099 This is theoretically possible, and may even apply to some Fortran
7100 constructs, but it is a lot of work and we do not attempt it here. */
7102 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
7103 return NULL_RTX;
7105 /* In general these structures are sorted top to bottom (down the PLUS
7106 chain), but not left to right across the PLUS. If B is a higher
7107 order giv than A, we can strip one level and recurse. If A is higher
7108 order, we'll eventually bail out, but won't know that until the end.
7109 If they are the same, we'll strip one level around this loop. */
7111 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
7113 rtx ra, rb, oa, ob, tmp;
7115 ra = XEXP (a, 0), oa = XEXP (a, 1);
7116 if (GET_CODE (ra) == PLUS)
7117 tmp = ra, ra = oa, oa = tmp;
7119 rb = XEXP (b, 0), ob = XEXP (b, 1);
7120 if (GET_CODE (rb) == PLUS)
7121 tmp = rb, rb = ob, ob = tmp;
7123 if (rtx_equal_p (ra, rb))
7124 /* We matched: remove one reg completely. */
7125 a = oa, b = ob;
7126 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
7127 /* An alternate match. */
7128 a = oa, b = rb;
7129 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
7130 /* An alternate match. */
7131 a = ra, b = ob;
7132 else
7134 /* Indicates an extra register in B. Strip one level from B and
7135 recurse, hoping B was the higher order expression. */
7136 ob = express_from_1 (a, ob, mult);
7137 if (ob == NULL_RTX)
7138 return NULL_RTX;
7139 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
7143 /* Here we are at the last level of A, go through the cases hoping to
7144 get rid of everything but a constant. */
7146 if (GET_CODE (a) == PLUS)
7148 rtx ra, oa;
7150 ra = XEXP (a, 0), oa = XEXP (a, 1);
7151 if (rtx_equal_p (oa, b))
7152 oa = ra;
7153 else if (!rtx_equal_p (ra, b))
7154 return NULL_RTX;
7156 if (GET_CODE (oa) != CONST_INT)
7157 return NULL_RTX;
7159 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
7161 else if (GET_CODE (a) == CONST_INT)
7163 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
7165 else if (CONSTANT_P (a))
7167 enum machine_mode mode_a = GET_MODE (a);
7168 enum machine_mode mode_b = GET_MODE (b);
7169 enum machine_mode mode = mode_b == VOIDmode ? mode_a : mode_b;
7170 return simplify_gen_binary (MINUS, mode, b, a);
7172 else if (GET_CODE (b) == PLUS)
7174 if (rtx_equal_p (a, XEXP (b, 0)))
7175 return XEXP (b, 1);
7176 else if (rtx_equal_p (a, XEXP (b, 1)))
7177 return XEXP (b, 0);
7178 else
7179 return NULL_RTX;
7181 else if (rtx_equal_p (a, b))
7182 return const0_rtx;
7184 return NULL_RTX;
7188 express_from (struct induction *g1, struct induction *g2)
7190 rtx mult, add;
7192 /* The value that G1 will be multiplied by must be a constant integer. Also,
7193 the only chance we have of getting a valid address is if b*c/a (see above
7194 for notation) is also an integer. */
7195 if (GET_CODE (g1->mult_val) == CONST_INT
7196 && GET_CODE (g2->mult_val) == CONST_INT)
7198 if (g1->mult_val == const0_rtx
7199 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
7200 return NULL_RTX;
7201 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
7203 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
7204 mult = const1_rtx;
7205 else
7207 /* ??? Find out if the one is a multiple of the other? */
7208 return NULL_RTX;
7211 add = express_from_1 (g1->add_val, g2->add_val, mult);
7212 if (add == NULL_RTX)
7214 /* Failed. If we've got a multiplication factor between G1 and G2,
7215 scale G1's addend and try again. */
7216 if (INTVAL (mult) > 1)
7218 rtx g1_add_val = g1->add_val;
7219 if (GET_CODE (g1_add_val) == MULT
7220 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
7222 HOST_WIDE_INT m;
7223 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
7224 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
7225 XEXP (g1_add_val, 0), GEN_INT (m));
7227 else
7229 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
7230 mult);
7233 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
7236 if (add == NULL_RTX)
7237 return NULL_RTX;
7239 /* Form simplified final result. */
7240 if (mult == const0_rtx)
7241 return add;
7242 else if (mult == const1_rtx)
7243 mult = g1->dest_reg;
7244 else
7245 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
7247 if (add == const0_rtx)
7248 return mult;
7249 else
7251 if (GET_CODE (add) == PLUS
7252 && CONSTANT_P (XEXP (add, 1)))
7254 rtx tem = XEXP (add, 1);
7255 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
7256 add = tem;
7259 return gen_rtx_PLUS (g2->mode, mult, add);
7263 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7264 represented by G1. This indicates that G2 should be combined with G1 and
7265 that G2 can use (either directly or via an address expression) a register
7266 used to represent G1. */
7268 static rtx
7269 combine_givs_p (struct induction *g1, struct induction *g2)
7271 rtx comb, ret;
7273 /* With the introduction of ext dependent givs, we must care for modes.
7274 G2 must not use a wider mode than G1. */
7275 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
7276 return NULL_RTX;
7278 ret = comb = express_from (g1, g2);
7279 if (comb == NULL_RTX)
7280 return NULL_RTX;
7281 if (g1->mode != g2->mode)
7282 ret = gen_lowpart (g2->mode, comb);
7284 /* If these givs are identical, they can be combined. We use the results
7285 of express_from because the addends are not in a canonical form, so
7286 rtx_equal_p is a weaker test. */
7287 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
7288 combination to be the other way round. */
7289 if (comb == g1->dest_reg
7290 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
7292 return ret;
7295 /* If G2 can be expressed as a function of G1 and that function is valid
7296 as an address and no more expensive than using a register for G2,
7297 the expression of G2 in terms of G1 can be used. */
7298 if (ret != NULL_RTX
7299 && g2->giv_type == DEST_ADDR
7300 && memory_address_p (GET_MODE (g2->mem), ret))
7301 return ret;
7303 return NULL_RTX;
7306 /* Check each extension dependent giv in this class to see if its
7307 root biv is safe from wrapping in the interior mode, which would
7308 make the giv illegal. */
7310 static void
7311 check_ext_dependent_givs (const struct loop *loop, struct iv_class *bl)
7313 struct loop_info *loop_info = LOOP_INFO (loop);
7314 int ze_ok = 0, se_ok = 0, info_ok = 0;
7315 enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
7316 HOST_WIDE_INT start_val;
7317 unsigned HOST_WIDE_INT u_end_val = 0;
7318 unsigned HOST_WIDE_INT u_start_val = 0;
7319 rtx incr = pc_rtx;
7320 struct induction *v;
7322 /* Make sure the iteration data is available. We must have
7323 constants in order to be certain of no overflow. */
7324 if (loop_info->n_iterations > 0
7325 && bl->initial_value
7326 && GET_CODE (bl->initial_value) == CONST_INT
7327 && (incr = biv_total_increment (bl))
7328 && GET_CODE (incr) == CONST_INT
7329 /* Make sure the host can represent the arithmetic. */
7330 && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
7332 unsigned HOST_WIDE_INT abs_incr, total_incr;
7333 HOST_WIDE_INT s_end_val;
7334 int neg_incr;
7336 info_ok = 1;
7337 start_val = INTVAL (bl->initial_value);
7338 u_start_val = start_val;
7340 neg_incr = 0, abs_incr = INTVAL (incr);
7341 if (INTVAL (incr) < 0)
7342 neg_incr = 1, abs_incr = -abs_incr;
7343 total_incr = abs_incr * loop_info->n_iterations;
7345 /* Check for host arithmetic overflow. */
7346 if (total_incr / loop_info->n_iterations == abs_incr)
7348 unsigned HOST_WIDE_INT u_max;
7349 HOST_WIDE_INT s_max;
7351 u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
7352 s_end_val = u_end_val;
7353 u_max = GET_MODE_MASK (biv_mode);
7354 s_max = u_max >> 1;
7356 /* Check zero extension of biv ok. */
7357 if (start_val >= 0
7358 /* Check for host arithmetic overflow. */
7359 && (neg_incr
7360 ? u_end_val < u_start_val
7361 : u_end_val > u_start_val)
7362 /* Check for target arithmetic overflow. */
7363 && (neg_incr
7364 ? 1 /* taken care of with host overflow */
7365 : u_end_val <= u_max))
7367 ze_ok = 1;
7370 /* Check sign extension of biv ok. */
7371 /* ??? While it is true that overflow with signed and pointer
7372 arithmetic is undefined, I fear too many programmers don't
7373 keep this fact in mind -- myself included on occasion.
7374 So leave alone with the signed overflow optimizations. */
7375 if (start_val >= -s_max - 1
7376 /* Check for host arithmetic overflow. */
7377 && (neg_incr
7378 ? s_end_val < start_val
7379 : s_end_val > start_val)
7380 /* Check for target arithmetic overflow. */
7381 && (neg_incr
7382 ? s_end_val >= -s_max - 1
7383 : s_end_val <= s_max))
7385 se_ok = 1;
7390 /* If we know the BIV is compared at run-time against an
7391 invariant value, and the increment is +/- 1, we may also
7392 be able to prove that the BIV cannot overflow. */
7393 else if (bl->biv->src_reg == loop_info->iteration_var
7394 && loop_info->comparison_value
7395 && loop_invariant_p (loop, loop_info->comparison_value)
7396 && (incr = biv_total_increment (bl))
7397 && GET_CODE (incr) == CONST_INT)
7399 /* If the increment is +1, and the exit test is a <,
7400 the BIV cannot overflow. (For <=, we have the
7401 problematic case that the comparison value might
7402 be the maximum value of the range.) */
7403 if (INTVAL (incr) == 1)
7405 if (loop_info->comparison_code == LT)
7406 se_ok = ze_ok = 1;
7407 else if (loop_info->comparison_code == LTU)
7408 ze_ok = 1;
7411 /* Likewise for increment -1 and exit test >. */
7412 if (INTVAL (incr) == -1)
7414 if (loop_info->comparison_code == GT)
7415 se_ok = ze_ok = 1;
7416 else if (loop_info->comparison_code == GTU)
7417 ze_ok = 1;
7421 /* Invalidate givs that fail the tests. */
7422 for (v = bl->giv; v; v = v->next_iv)
7423 if (v->ext_dependent)
7425 enum rtx_code code = GET_CODE (v->ext_dependent);
7426 int ok = 0;
7428 switch (code)
7430 case SIGN_EXTEND:
7431 ok = se_ok;
7432 break;
7433 case ZERO_EXTEND:
7434 ok = ze_ok;
7435 break;
7437 case TRUNCATE:
7438 /* We don't know whether this value is being used as either
7439 signed or unsigned, so to safely truncate we must satisfy
7440 both. The initial check here verifies the BIV itself;
7441 once that is successful we may check its range wrt the
7442 derived GIV. This works only if we were able to determine
7443 constant start and end values above. */
7444 if (se_ok && ze_ok && info_ok)
7446 enum machine_mode outer_mode = GET_MODE (v->ext_dependent);
7447 unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
7449 /* We know from the above that both endpoints are nonnegative,
7450 and that there is no wrapping. Verify that both endpoints
7451 are within the (signed) range of the outer mode. */
7452 if (u_start_val <= max && u_end_val <= max)
7453 ok = 1;
7455 break;
7457 default:
7458 abort ();
7461 if (ok)
7463 if (loop_dump_stream)
7465 fprintf (loop_dump_stream,
7466 "Verified ext dependent giv at %d of reg %d\n",
7467 INSN_UID (v->insn), bl->regno);
7470 else
7472 if (loop_dump_stream)
7474 const char *why;
7476 if (info_ok)
7477 why = "biv iteration values overflowed";
7478 else
7480 if (incr == pc_rtx)
7481 incr = biv_total_increment (bl);
7482 if (incr == const1_rtx)
7483 why = "biv iteration info incomplete; incr by 1";
7484 else
7485 why = "biv iteration info incomplete";
7488 fprintf (loop_dump_stream,
7489 "Failed ext dependent giv at %d, %s\n",
7490 INSN_UID (v->insn), why);
7492 v->ignore = 1;
7493 bl->all_reduced = 0;
7498 /* Generate a version of VALUE in a mode appropriate for initializing V. */
7501 extend_value_for_giv (struct induction *v, rtx value)
7503 rtx ext_dep = v->ext_dependent;
7505 if (! ext_dep)
7506 return value;
7508 /* Recall that check_ext_dependent_givs verified that the known bounds
7509 of a biv did not overflow or wrap with respect to the extension for
7510 the giv. Therefore, constants need no additional adjustment. */
7511 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
7512 return value;
7514 /* Otherwise, we must adjust the value to compensate for the
7515 differing modes of the biv and the giv. */
7516 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
7519 struct combine_givs_stats
7521 int giv_number;
7522 int total_benefit;
7525 static int
7526 cmp_combine_givs_stats (const void *xp, const void *yp)
7528 const struct combine_givs_stats * const x =
7529 (const struct combine_givs_stats *) xp;
7530 const struct combine_givs_stats * const y =
7531 (const struct combine_givs_stats *) yp;
7532 int d;
7533 d = y->total_benefit - x->total_benefit;
7534 /* Stabilize the sort. */
7535 if (!d)
7536 d = x->giv_number - y->giv_number;
7537 return d;
7540 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7541 any other. If so, point SAME to the giv combined with and set NEW_REG to
7542 be an expression (in terms of the other giv's DEST_REG) equivalent to the
7543 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
7545 static void
7546 combine_givs (struct loop_regs *regs, struct iv_class *bl)
7548 /* Additional benefit to add for being combined multiple times. */
7549 const int extra_benefit = 3;
7551 struct induction *g1, *g2, **giv_array;
7552 int i, j, k, giv_count;
7553 struct combine_givs_stats *stats;
7554 rtx *can_combine;
7556 /* Count givs, because bl->giv_count is incorrect here. */
7557 giv_count = 0;
7558 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7559 if (!g1->ignore)
7560 giv_count++;
7562 giv_array = alloca (giv_count * sizeof (struct induction *));
7563 i = 0;
7564 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7565 if (!g1->ignore)
7566 giv_array[i++] = g1;
7568 stats = xcalloc (giv_count, sizeof (*stats));
7569 can_combine = xcalloc (giv_count, giv_count * sizeof (rtx));
7571 for (i = 0; i < giv_count; i++)
7573 int this_benefit;
7574 rtx single_use;
7576 g1 = giv_array[i];
7577 stats[i].giv_number = i;
7579 /* If a DEST_REG GIV is used only once, do not allow it to combine
7580 with anything, for in doing so we will gain nothing that cannot
7581 be had by simply letting the GIV with which we would have combined
7582 to be reduced on its own. The losage shows up in particular with
7583 DEST_ADDR targets on hosts with reg+reg addressing, though it can
7584 be seen elsewhere as well. */
7585 if (g1->giv_type == DEST_REG
7586 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
7587 && single_use != const0_rtx)
7588 continue;
7590 this_benefit = g1->benefit;
7591 /* Add an additional weight for zero addends. */
7592 if (g1->no_const_addval)
7593 this_benefit += 1;
7595 for (j = 0; j < giv_count; j++)
7597 rtx this_combine;
7599 g2 = giv_array[j];
7600 if (g1 != g2
7601 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
7603 can_combine[i * giv_count + j] = this_combine;
7604 this_benefit += g2->benefit + extra_benefit;
7607 stats[i].total_benefit = this_benefit;
7610 /* Iterate, combining until we can't. */
7611 restart:
7612 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
7614 if (loop_dump_stream)
7616 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
7617 for (k = 0; k < giv_count; k++)
7619 g1 = giv_array[stats[k].giv_number];
7620 if (!g1->combined_with && !g1->same)
7621 fprintf (loop_dump_stream, " {%d, %d}",
7622 INSN_UID (giv_array[stats[k].giv_number]->insn),
7623 stats[k].total_benefit);
7625 putc ('\n', loop_dump_stream);
7628 for (k = 0; k < giv_count; k++)
7630 int g1_add_benefit = 0;
7632 i = stats[k].giv_number;
7633 g1 = giv_array[i];
7635 /* If it has already been combined, skip. */
7636 if (g1->combined_with || g1->same)
7637 continue;
7639 for (j = 0; j < giv_count; j++)
7641 g2 = giv_array[j];
7642 if (g1 != g2 && can_combine[i * giv_count + j]
7643 /* If it has already been combined, skip. */
7644 && ! g2->same && ! g2->combined_with)
7646 int l;
7648 g2->new_reg = can_combine[i * giv_count + j];
7649 g2->same = g1;
7650 /* For destination, we now may replace by mem expression instead
7651 of register. This changes the costs considerably, so add the
7652 compensation. */
7653 if (g2->giv_type == DEST_ADDR)
7654 g2->benefit = (g2->benefit + reg_address_cost
7655 - address_cost (g2->new_reg,
7656 GET_MODE (g2->mem)));
7657 g1->combined_with++;
7658 g1->lifetime += g2->lifetime;
7660 g1_add_benefit += g2->benefit;
7662 /* ??? The new final_[bg]iv_value code does a much better job
7663 of finding replaceable giv's, and hence this code may no
7664 longer be necessary. */
7665 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
7666 g1_add_benefit -= copy_cost;
7668 /* To help optimize the next set of combinations, remove
7669 this giv from the benefits of other potential mates. */
7670 for (l = 0; l < giv_count; ++l)
7672 int m = stats[l].giv_number;
7673 if (can_combine[m * giv_count + j])
7674 stats[l].total_benefit -= g2->benefit + extra_benefit;
7677 if (loop_dump_stream)
7678 fprintf (loop_dump_stream,
7679 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
7680 INSN_UID (g2->insn), INSN_UID (g1->insn),
7681 g1->benefit, g1_add_benefit, g1->lifetime);
7685 /* To help optimize the next set of combinations, remove
7686 this giv from the benefits of other potential mates. */
7687 if (g1->combined_with)
7689 for (j = 0; j < giv_count; ++j)
7691 int m = stats[j].giv_number;
7692 if (can_combine[m * giv_count + i])
7693 stats[j].total_benefit -= g1->benefit + extra_benefit;
7696 g1->benefit += g1_add_benefit;
7698 /* We've finished with this giv, and everything it touched.
7699 Restart the combination so that proper weights for the
7700 rest of the givs are properly taken into account. */
7701 /* ??? Ideally we would compact the arrays at this point, so
7702 as to not cover old ground. But sanely compacting
7703 can_combine is tricky. */
7704 goto restart;
7708 /* Clean up. */
7709 free (stats);
7710 free (can_combine);
7713 /* Generate sequence for REG = B * M + A. B is the initial value of
7714 the basic induction variable, M a multiplicative constant, A an
7715 additive constant and REG the destination register. */
7717 static rtx
7718 gen_add_mult (rtx b, rtx m, rtx a, rtx reg)
7720 rtx seq;
7721 rtx result;
7723 start_sequence ();
7724 /* Use unsigned arithmetic. */
7725 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7726 if (reg != result)
7727 emit_move_insn (reg, result);
7728 seq = get_insns ();
7729 end_sequence ();
7731 return seq;
7735 /* Update registers created in insn sequence SEQ. */
7737 static void
7738 loop_regs_update (const struct loop *loop ATTRIBUTE_UNUSED, rtx seq)
7740 rtx insn;
7742 /* Update register info for alias analysis. */
7744 if (seq == NULL_RTX)
7745 return;
7747 if (INSN_P (seq))
7749 insn = seq;
7750 while (insn != NULL_RTX)
7752 rtx set = single_set (insn);
7754 if (set && GET_CODE (SET_DEST (set)) == REG)
7755 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7757 insn = NEXT_INSN (insn);
7760 else if (GET_CODE (seq) == SET
7761 && GET_CODE (SET_DEST (seq)) == REG)
7762 record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
7766 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. B
7767 is the initial value of the basic induction variable, M a
7768 multiplicative constant, A an additive constant and REG the
7769 destination register. */
7771 void
7772 loop_iv_add_mult_emit_before (const struct loop *loop, rtx b, rtx m, rtx a,
7773 rtx reg, basic_block before_bb, rtx before_insn)
7775 rtx seq;
7777 if (! before_insn)
7779 loop_iv_add_mult_hoist (loop, b, m, a, reg);
7780 return;
7783 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7784 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7786 /* Increase the lifetime of any invariants moved further in code. */
7787 update_reg_last_use (a, before_insn);
7788 update_reg_last_use (b, before_insn);
7789 update_reg_last_use (m, before_insn);
7791 /* It is possible that the expansion created lots of new registers.
7792 Iterate over the sequence we just created and record them all. We
7793 must do this before inserting the sequence. */
7794 loop_regs_update (loop, seq);
7796 loop_insn_emit_before (loop, before_bb, before_insn, seq);
7800 /* Emit insns in loop pre-header to set REG = B * M + A. B is the
7801 initial value of the basic induction variable, M a multiplicative
7802 constant, A an additive constant and REG the destination
7803 register. */
7805 void
7806 loop_iv_add_mult_sink (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
7808 rtx seq;
7810 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7811 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7813 /* Increase the lifetime of any invariants moved further in code.
7814 ???? Is this really necessary? */
7815 update_reg_last_use (a, loop->sink);
7816 update_reg_last_use (b, loop->sink);
7817 update_reg_last_use (m, loop->sink);
7819 /* It is possible that the expansion created lots of new registers.
7820 Iterate over the sequence we just created and record them all. We
7821 must do this before inserting the sequence. */
7822 loop_regs_update (loop, seq);
7824 loop_insn_sink (loop, seq);
7828 /* Emit insns after loop to set REG = B * M + A. B is the initial
7829 value of the basic induction variable, M a multiplicative constant,
7830 A an additive constant and REG the destination register. */
7832 void
7833 loop_iv_add_mult_hoist (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
7835 rtx seq;
7837 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7838 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7840 /* It is possible that the expansion created lots of new registers.
7841 Iterate over the sequence we just created and record them all. We
7842 must do this before inserting the sequence. */
7843 loop_regs_update (loop, seq);
7845 loop_insn_hoist (loop, seq);
7850 /* Similar to gen_add_mult, but compute cost rather than generating
7851 sequence. */
7853 static int
7854 iv_add_mult_cost (rtx b, rtx m, rtx a, rtx reg)
7856 int cost = 0;
7857 rtx last, result;
7859 start_sequence ();
7860 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7861 if (reg != result)
7862 emit_move_insn (reg, result);
7863 last = get_last_insn ();
7864 while (last)
7866 rtx t = single_set (last);
7867 if (t)
7868 cost += rtx_cost (SET_SRC (t), SET);
7869 last = PREV_INSN (last);
7871 end_sequence ();
7872 return cost;
7875 /* Test whether A * B can be computed without
7876 an actual multiply insn. Value is 1 if so.
7878 ??? This function stinks because it generates a ton of wasted RTL
7879 ??? and as a result fragments GC memory to no end. There are other
7880 ??? places in the compiler which are invoked a lot and do the same
7881 ??? thing, generate wasted RTL just to see if something is possible. */
7883 static int
7884 product_cheap_p (rtx a, rtx b)
7886 rtx tmp;
7887 int win, n_insns;
7889 /* If only one is constant, make it B. */
7890 if (GET_CODE (a) == CONST_INT)
7891 tmp = a, a = b, b = tmp;
7893 /* If first constant, both constant, so don't need multiply. */
7894 if (GET_CODE (a) == CONST_INT)
7895 return 1;
7897 /* If second not constant, neither is constant, so would need multiply. */
7898 if (GET_CODE (b) != CONST_INT)
7899 return 0;
7901 /* One operand is constant, so might not need multiply insn. Generate the
7902 code for the multiply and see if a call or multiply, or long sequence
7903 of insns is generated. */
7905 start_sequence ();
7906 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
7907 tmp = get_insns ();
7908 end_sequence ();
7910 win = 1;
7911 if (INSN_P (tmp))
7913 n_insns = 0;
7914 while (tmp != NULL_RTX)
7916 rtx next = NEXT_INSN (tmp);
7918 if (++n_insns > 3
7919 || GET_CODE (tmp) != INSN
7920 || (GET_CODE (PATTERN (tmp)) == SET
7921 && GET_CODE (SET_SRC (PATTERN (tmp))) == MULT)
7922 || (GET_CODE (PATTERN (tmp)) == PARALLEL
7923 && GET_CODE (XVECEXP (PATTERN (tmp), 0, 0)) == SET
7924 && GET_CODE (SET_SRC (XVECEXP (PATTERN (tmp), 0, 0))) == MULT))
7926 win = 0;
7927 break;
7930 tmp = next;
7933 else if (GET_CODE (tmp) == SET
7934 && GET_CODE (SET_SRC (tmp)) == MULT)
7935 win = 0;
7936 else if (GET_CODE (tmp) == PARALLEL
7937 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7938 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7939 win = 0;
7941 return win;
7944 /* Check to see if loop can be terminated by a "decrement and branch until
7945 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7946 Also try reversing an increment loop to a decrement loop
7947 to see if the optimization can be performed.
7948 Value is nonzero if optimization was performed. */
7950 /* This is useful even if the architecture doesn't have such an insn,
7951 because it might change a loops which increments from 0 to n to a loop
7952 which decrements from n to 0. A loop that decrements to zero is usually
7953 faster than one that increments from zero. */
7955 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7956 such as approx_final_value, biv_total_increment, loop_iterations, and
7957 final_[bg]iv_value. */
7959 static int
7960 check_dbra_loop (struct loop *loop, int insn_count)
7962 struct loop_info *loop_info = LOOP_INFO (loop);
7963 struct loop_regs *regs = LOOP_REGS (loop);
7964 struct loop_ivs *ivs = LOOP_IVS (loop);
7965 struct iv_class *bl;
7966 rtx reg;
7967 rtx jump_label;
7968 rtx final_value;
7969 rtx start_value;
7970 rtx new_add_val;
7971 rtx comparison;
7972 rtx before_comparison;
7973 rtx p;
7974 rtx jump;
7975 rtx first_compare;
7976 int compare_and_branch;
7977 rtx loop_start = loop->start;
7978 rtx loop_end = loop->end;
7980 /* If last insn is a conditional branch, and the insn before tests a
7981 register value, try to optimize it. Otherwise, we can't do anything. */
7983 jump = PREV_INSN (loop_end);
7984 comparison = get_condition_for_loop (loop, jump);
7985 if (comparison == 0)
7986 return 0;
7987 if (!onlyjump_p (jump))
7988 return 0;
7990 /* Try to compute whether the compare/branch at the loop end is one or
7991 two instructions. */
7992 get_condition (jump, &first_compare);
7993 if (first_compare == jump)
7994 compare_and_branch = 1;
7995 else if (first_compare == prev_nonnote_insn (jump))
7996 compare_and_branch = 2;
7997 else
7998 return 0;
8001 /* If more than one condition is present to control the loop, then
8002 do not proceed, as this function does not know how to rewrite
8003 loop tests with more than one condition.
8005 Look backwards from the first insn in the last comparison
8006 sequence and see if we've got another comparison sequence. */
8008 rtx jump1;
8009 if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
8010 if (GET_CODE (jump1) == JUMP_INSN)
8011 return 0;
8014 /* Check all of the bivs to see if the compare uses one of them.
8015 Skip biv's set more than once because we can't guarantee that
8016 it will be zero on the last iteration. Also skip if the biv is
8017 used between its update and the test insn. */
8019 for (bl = ivs->list; bl; bl = bl->next)
8021 if (bl->biv_count == 1
8022 && ! bl->biv->maybe_multiple
8023 && bl->biv->dest_reg == XEXP (comparison, 0)
8024 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
8025 first_compare))
8026 break;
8029 if (! bl)
8030 return 0;
8032 /* Look for the case where the basic induction variable is always
8033 nonnegative, and equals zero on the last iteration.
8034 In this case, add a reg_note REG_NONNEG, which allows the
8035 m68k DBRA instruction to be used. */
8037 if (((GET_CODE (comparison) == GT && XEXP (comparison, 1) == constm1_rtx)
8038 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
8039 && GET_CODE (bl->biv->add_val) == CONST_INT
8040 && INTVAL (bl->biv->add_val) < 0)
8042 /* Initial value must be greater than 0,
8043 init_val % -dec_value == 0 to ensure that it equals zero on
8044 the last iteration */
8046 if (GET_CODE (bl->initial_value) == CONST_INT
8047 && INTVAL (bl->initial_value) > 0
8048 && (INTVAL (bl->initial_value)
8049 % (-INTVAL (bl->biv->add_val))) == 0)
8051 /* register always nonnegative, add REG_NOTE to branch */
8052 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8053 REG_NOTES (jump)
8054 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8055 REG_NOTES (jump));
8056 bl->nonneg = 1;
8058 return 1;
8061 /* If the decrement is 1 and the value was tested as >= 0 before
8062 the loop, then we can safely optimize. */
8063 for (p = loop_start; p; p = PREV_INSN (p))
8065 if (GET_CODE (p) == CODE_LABEL)
8066 break;
8067 if (GET_CODE (p) != JUMP_INSN)
8068 continue;
8070 before_comparison = get_condition_for_loop (loop, p);
8071 if (before_comparison
8072 && XEXP (before_comparison, 0) == bl->biv->dest_reg
8073 && (GET_CODE (before_comparison) == LT
8074 || GET_CODE (before_comparison) == LTU)
8075 && XEXP (before_comparison, 1) == const0_rtx
8076 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
8077 && INTVAL (bl->biv->add_val) == -1)
8079 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8080 REG_NOTES (jump)
8081 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8082 REG_NOTES (jump));
8083 bl->nonneg = 1;
8085 return 1;
8089 else if (GET_CODE (bl->biv->add_val) == CONST_INT
8090 && INTVAL (bl->biv->add_val) > 0)
8092 /* Try to change inc to dec, so can apply above optimization. */
8093 /* Can do this if:
8094 all registers modified are induction variables or invariant,
8095 all memory references have non-overlapping addresses
8096 (obviously true if only one write)
8097 allow 2 insns for the compare/jump at the end of the loop. */
8098 /* Also, we must avoid any instructions which use both the reversed
8099 biv and another biv. Such instructions will fail if the loop is
8100 reversed. We meet this condition by requiring that either
8101 no_use_except_counting is true, or else that there is only
8102 one biv. */
8103 int num_nonfixed_reads = 0;
8104 /* 1 if the iteration var is used only to count iterations. */
8105 int no_use_except_counting = 0;
8106 /* 1 if the loop has no memory store, or it has a single memory store
8107 which is reversible. */
8108 int reversible_mem_store = 1;
8110 if (bl->giv_count == 0
8111 && !loop->exit_count
8112 && !loop_info->has_multiple_exit_targets)
8114 rtx bivreg = regno_reg_rtx[bl->regno];
8115 struct iv_class *blt;
8117 /* If there are no givs for this biv, and the only exit is the
8118 fall through at the end of the loop, then
8119 see if perhaps there are no uses except to count. */
8120 no_use_except_counting = 1;
8121 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8122 if (INSN_P (p))
8124 rtx set = single_set (p);
8126 if (set && GET_CODE (SET_DEST (set)) == REG
8127 && REGNO (SET_DEST (set)) == bl->regno)
8128 /* An insn that sets the biv is okay. */
8130 else if (!reg_mentioned_p (bivreg, PATTERN (p)))
8131 /* An insn that doesn't mention the biv is okay. */
8133 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
8134 || p == prev_nonnote_insn (loop_end))
8136 /* If either of these insns uses the biv and sets a pseudo
8137 that has more than one usage, then the biv has uses
8138 other than counting since it's used to derive a value
8139 that is used more than one time. */
8140 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
8141 regs);
8142 if (regs->multiple_uses)
8144 no_use_except_counting = 0;
8145 break;
8148 else
8150 no_use_except_counting = 0;
8151 break;
8155 /* A biv has uses besides counting if it is used to set
8156 another biv. */
8157 for (blt = ivs->list; blt; blt = blt->next)
8158 if (blt->init_set
8159 && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
8161 no_use_except_counting = 0;
8162 break;
8166 if (no_use_except_counting)
8167 /* No need to worry about MEMs. */
8169 else if (loop_info->num_mem_sets <= 1)
8171 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8172 if (INSN_P (p))
8173 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
8175 /* If the loop has a single store, and the destination address is
8176 invariant, then we can't reverse the loop, because this address
8177 might then have the wrong value at loop exit.
8178 This would work if the source was invariant also, however, in that
8179 case, the insn should have been moved out of the loop. */
8181 if (loop_info->num_mem_sets == 1)
8183 struct induction *v;
8185 /* If we could prove that each of the memory locations
8186 written to was different, then we could reverse the
8187 store -- but we don't presently have any way of
8188 knowing that. */
8189 reversible_mem_store = 0;
8191 /* If the store depends on a register that is set after the
8192 store, it depends on the initial value, and is thus not
8193 reversible. */
8194 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
8196 if (v->giv_type == DEST_REG
8197 && reg_mentioned_p (v->dest_reg,
8198 PATTERN (loop_info->first_loop_store_insn))
8199 && loop_insn_first_p (loop_info->first_loop_store_insn,
8200 v->insn))
8201 reversible_mem_store = 0;
8205 else
8206 return 0;
8208 /* This code only acts for innermost loops. Also it simplifies
8209 the memory address check by only reversing loops with
8210 zero or one memory access.
8211 Two memory accesses could involve parts of the same array,
8212 and that can't be reversed.
8213 If the biv is used only for counting, than we don't need to worry
8214 about all these things. */
8216 if ((num_nonfixed_reads <= 1
8217 && ! loop_info->has_nonconst_call
8218 && ! loop_info->has_prefetch
8219 && ! loop_info->has_volatile
8220 && reversible_mem_store
8221 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
8222 + num_unmoved_movables (loop) + compare_and_branch == insn_count)
8223 && (bl == ivs->list && bl->next == 0))
8224 || (no_use_except_counting && ! loop_info->has_prefetch))
8226 rtx tem;
8228 /* Loop can be reversed. */
8229 if (loop_dump_stream)
8230 fprintf (loop_dump_stream, "Can reverse loop\n");
8232 /* Now check other conditions:
8234 The increment must be a constant, as must the initial value,
8235 and the comparison code must be LT.
8237 This test can probably be improved since +/- 1 in the constant
8238 can be obtained by changing LT to LE and vice versa; this is
8239 confusing. */
8241 if (comparison
8242 /* for constants, LE gets turned into LT */
8243 && (GET_CODE (comparison) == LT
8244 || (GET_CODE (comparison) == LE
8245 && no_use_except_counting)
8246 || GET_CODE (comparison) == LTU))
8248 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
8249 rtx initial_value, comparison_value;
8250 int nonneg = 0;
8251 enum rtx_code cmp_code;
8252 int comparison_const_width;
8253 unsigned HOST_WIDE_INT comparison_sign_mask;
8255 add_val = INTVAL (bl->biv->add_val);
8256 comparison_value = XEXP (comparison, 1);
8257 if (GET_MODE (comparison_value) == VOIDmode)
8258 comparison_const_width
8259 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
8260 else
8261 comparison_const_width
8262 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
8263 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
8264 comparison_const_width = HOST_BITS_PER_WIDE_INT;
8265 comparison_sign_mask
8266 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
8268 /* If the comparison value is not a loop invariant, then we
8269 can not reverse this loop.
8271 ??? If the insns which initialize the comparison value as
8272 a whole compute an invariant result, then we could move
8273 them out of the loop and proceed with loop reversal. */
8274 if (! loop_invariant_p (loop, comparison_value))
8275 return 0;
8277 if (GET_CODE (comparison_value) == CONST_INT)
8278 comparison_val = INTVAL (comparison_value);
8279 initial_value = bl->initial_value;
8281 /* Normalize the initial value if it is an integer and
8282 has no other use except as a counter. This will allow
8283 a few more loops to be reversed. */
8284 if (no_use_except_counting
8285 && GET_CODE (comparison_value) == CONST_INT
8286 && GET_CODE (initial_value) == CONST_INT)
8288 comparison_val = comparison_val - INTVAL (bl->initial_value);
8289 /* The code below requires comparison_val to be a multiple
8290 of add_val in order to do the loop reversal, so
8291 round up comparison_val to a multiple of add_val.
8292 Since comparison_value is constant, we know that the
8293 current comparison code is LT. */
8294 comparison_val = comparison_val + add_val - 1;
8295 comparison_val
8296 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
8297 /* We postpone overflow checks for COMPARISON_VAL here;
8298 even if there is an overflow, we might still be able to
8299 reverse the loop, if converting the loop exit test to
8300 NE is possible. */
8301 initial_value = const0_rtx;
8304 /* First check if we can do a vanilla loop reversal. */
8305 if (initial_value == const0_rtx
8306 /* If we have a decrement_and_branch_on_count,
8307 prefer the NE test, since this will allow that
8308 instruction to be generated. Note that we must
8309 use a vanilla loop reversal if the biv is used to
8310 calculate a giv or has a non-counting use. */
8311 #if ! defined (HAVE_decrement_and_branch_until_zero) \
8312 && defined (HAVE_decrement_and_branch_on_count)
8313 && (! (add_val == 1 && loop->vtop
8314 && (bl->biv_count == 0
8315 || no_use_except_counting)))
8316 #endif
8317 && GET_CODE (comparison_value) == CONST_INT
8318 /* Now do postponed overflow checks on COMPARISON_VAL. */
8319 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
8320 & comparison_sign_mask))
8322 /* Register will always be nonnegative, with value
8323 0 on last iteration */
8324 add_adjust = add_val;
8325 nonneg = 1;
8326 cmp_code = GE;
8328 else if (add_val == 1 && loop->vtop
8329 && (bl->biv_count == 0
8330 || no_use_except_counting))
8332 add_adjust = 0;
8333 cmp_code = NE;
8335 else
8336 return 0;
8338 if (GET_CODE (comparison) == LE)
8339 add_adjust -= add_val;
8341 /* If the initial value is not zero, or if the comparison
8342 value is not an exact multiple of the increment, then we
8343 can not reverse this loop. */
8344 if (initial_value == const0_rtx
8345 && GET_CODE (comparison_value) == CONST_INT)
8347 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
8348 return 0;
8350 else
8352 if (! no_use_except_counting || add_val != 1)
8353 return 0;
8356 final_value = comparison_value;
8358 /* Reset these in case we normalized the initial value
8359 and comparison value above. */
8360 if (GET_CODE (comparison_value) == CONST_INT
8361 && GET_CODE (initial_value) == CONST_INT)
8363 comparison_value = GEN_INT (comparison_val);
8364 final_value
8365 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
8367 bl->initial_value = initial_value;
8369 /* Save some info needed to produce the new insns. */
8370 reg = bl->biv->dest_reg;
8371 jump_label = condjump_label (PREV_INSN (loop_end));
8372 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
8374 /* Set start_value; if this is not a CONST_INT, we need
8375 to generate a SUB.
8376 Initialize biv to start_value before loop start.
8377 The old initializing insn will be deleted as a
8378 dead store by flow.c. */
8379 if (initial_value == const0_rtx
8380 && GET_CODE (comparison_value) == CONST_INT)
8382 start_value = GEN_INT (comparison_val - add_adjust);
8383 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
8385 else if (GET_CODE (initial_value) == CONST_INT)
8387 enum machine_mode mode = GET_MODE (reg);
8388 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
8389 rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
8391 if (add_insn == 0)
8392 return 0;
8394 start_value
8395 = gen_rtx_PLUS (mode, comparison_value, offset);
8396 loop_insn_hoist (loop, add_insn);
8397 if (GET_CODE (comparison) == LE)
8398 final_value = gen_rtx_PLUS (mode, comparison_value,
8399 GEN_INT (add_val));
8401 else if (! add_adjust)
8403 enum machine_mode mode = GET_MODE (reg);
8404 rtx sub_insn = gen_sub3_insn (reg, comparison_value,
8405 initial_value);
8407 if (sub_insn == 0)
8408 return 0;
8409 start_value
8410 = gen_rtx_MINUS (mode, comparison_value, initial_value);
8411 loop_insn_hoist (loop, sub_insn);
8413 else
8414 /* We could handle the other cases too, but it'll be
8415 better to have a testcase first. */
8416 return 0;
8418 /* We may not have a single insn which can increment a reg, so
8419 create a sequence to hold all the insns from expand_inc. */
8420 start_sequence ();
8421 expand_inc (reg, new_add_val);
8422 tem = get_insns ();
8423 end_sequence ();
8425 p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
8426 delete_insn (bl->biv->insn);
8428 /* Update biv info to reflect its new status. */
8429 bl->biv->insn = p;
8430 bl->initial_value = start_value;
8431 bl->biv->add_val = new_add_val;
8433 /* Update loop info. */
8434 loop_info->initial_value = reg;
8435 loop_info->initial_equiv_value = reg;
8436 loop_info->final_value = const0_rtx;
8437 loop_info->final_equiv_value = const0_rtx;
8438 loop_info->comparison_value = const0_rtx;
8439 loop_info->comparison_code = cmp_code;
8440 loop_info->increment = new_add_val;
8442 /* Inc LABEL_NUSES so that delete_insn will
8443 not delete the label. */
8444 LABEL_NUSES (XEXP (jump_label, 0))++;
8446 /* Emit an insn after the end of the loop to set the biv's
8447 proper exit value if it is used anywhere outside the loop. */
8448 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
8449 || ! bl->init_insn
8450 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
8451 loop_insn_sink (loop, gen_load_of_final_value (reg, final_value));
8453 /* Delete compare/branch at end of loop. */
8454 delete_related_insns (PREV_INSN (loop_end));
8455 if (compare_and_branch == 2)
8456 delete_related_insns (first_compare);
8458 /* Add new compare/branch insn at end of loop. */
8459 start_sequence ();
8460 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8461 GET_MODE (reg), 0,
8462 XEXP (jump_label, 0));
8463 tem = get_insns ();
8464 end_sequence ();
8465 emit_jump_insn_before (tem, loop_end);
8467 for (tem = PREV_INSN (loop_end);
8468 tem && GET_CODE (tem) != JUMP_INSN;
8469 tem = PREV_INSN (tem))
8472 if (tem)
8473 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8475 if (nonneg)
8477 if (tem)
8479 /* Increment of LABEL_NUSES done above. */
8480 /* Register is now always nonnegative,
8481 so add REG_NONNEG note to the branch. */
8482 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
8483 REG_NOTES (tem));
8485 bl->nonneg = 1;
8488 /* No insn may reference both the reversed and another biv or it
8489 will fail (see comment near the top of the loop reversal
8490 code).
8491 Earlier on, we have verified that the biv has no use except
8492 counting, or it is the only biv in this function.
8493 However, the code that computes no_use_except_counting does
8494 not verify reg notes. It's possible to have an insn that
8495 references another biv, and has a REG_EQUAL note with an
8496 expression based on the reversed biv. To avoid this case,
8497 remove all REG_EQUAL notes based on the reversed biv
8498 here. */
8499 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8500 if (INSN_P (p))
8502 rtx *pnote;
8503 rtx set = single_set (p);
8504 /* If this is a set of a GIV based on the reversed biv, any
8505 REG_EQUAL notes should still be correct. */
8506 if (! set
8507 || GET_CODE (SET_DEST (set)) != REG
8508 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
8509 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
8510 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
8511 for (pnote = &REG_NOTES (p); *pnote;)
8513 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
8514 && reg_mentioned_p (regno_reg_rtx[bl->regno],
8515 XEXP (*pnote, 0)))
8516 *pnote = XEXP (*pnote, 1);
8517 else
8518 pnote = &XEXP (*pnote, 1);
8522 /* Mark that this biv has been reversed. Each giv which depends
8523 on this biv, and which is also live past the end of the loop
8524 will have to be fixed up. */
8526 bl->reversed = 1;
8528 if (loop_dump_stream)
8530 fprintf (loop_dump_stream, "Reversed loop");
8531 if (bl->nonneg)
8532 fprintf (loop_dump_stream, " and added reg_nonneg\n");
8533 else
8534 fprintf (loop_dump_stream, "\n");
8537 return 1;
8542 return 0;
8545 /* Verify whether the biv BL appears to be eliminable,
8546 based on the insns in the loop that refer to it.
8548 If ELIMINATE_P is nonzero, actually do the elimination.
8550 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8551 determine whether invariant insns should be placed inside or at the
8552 start of the loop. */
8554 static int
8555 maybe_eliminate_biv (const struct loop *loop, struct iv_class *bl,
8556 int eliminate_p, int threshold, int insn_count)
8558 struct loop_ivs *ivs = LOOP_IVS (loop);
8559 rtx reg = bl->biv->dest_reg;
8560 rtx p;
8562 /* Scan all insns in the loop, stopping if we find one that uses the
8563 biv in a way that we cannot eliminate. */
8565 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
8567 enum rtx_code code = GET_CODE (p);
8568 basic_block where_bb = 0;
8569 rtx where_insn = threshold >= insn_count ? 0 : p;
8570 rtx note;
8572 /* If this is a libcall that sets a giv, skip ahead to its end. */
8573 if (GET_RTX_CLASS (code) == 'i')
8575 note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8577 if (note)
8579 rtx last = XEXP (note, 0);
8580 rtx set = single_set (last);
8582 if (set && GET_CODE (SET_DEST (set)) == REG)
8584 unsigned int regno = REGNO (SET_DEST (set));
8586 if (regno < ivs->n_regs
8587 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
8588 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
8589 p = last;
8594 /* Closely examine the insn if the biv is mentioned. */
8595 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8596 && reg_mentioned_p (reg, PATTERN (p))
8597 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
8598 eliminate_p, where_bb, where_insn))
8600 if (loop_dump_stream)
8601 fprintf (loop_dump_stream,
8602 "Cannot eliminate biv %d: biv used in insn %d.\n",
8603 bl->regno, INSN_UID (p));
8604 break;
8607 /* If we are eliminating, kill REG_EQUAL notes mentioning the biv. */
8608 if (eliminate_p
8609 && (note = find_reg_note (p, REG_EQUAL, NULL_RTX)) != NULL_RTX
8610 && reg_mentioned_p (reg, XEXP (note, 0)))
8611 remove_note (p, note);
8614 if (p == loop->end)
8616 if (loop_dump_stream)
8617 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8618 bl->regno, eliminate_p ? "was" : "can be");
8619 return 1;
8622 return 0;
8625 /* INSN and REFERENCE are instructions in the same insn chain.
8626 Return nonzero if INSN is first. */
8629 loop_insn_first_p (rtx insn, rtx reference)
8631 rtx p, q;
8633 for (p = insn, q = reference;;)
8635 /* Start with test for not first so that INSN == REFERENCE yields not
8636 first. */
8637 if (q == insn || ! p)
8638 return 0;
8639 if (p == reference || ! q)
8640 return 1;
8642 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8643 previous insn, hence the <= comparison below does not work if
8644 P is a note. */
8645 if (INSN_UID (p) < max_uid_for_loop
8646 && INSN_UID (q) < max_uid_for_loop
8647 && GET_CODE (p) != NOTE)
8648 return INSN_LUID (p) <= INSN_LUID (q);
8650 if (INSN_UID (p) >= max_uid_for_loop
8651 || GET_CODE (p) == NOTE)
8652 p = NEXT_INSN (p);
8653 if (INSN_UID (q) >= max_uid_for_loop)
8654 q = NEXT_INSN (q);
8658 /* We are trying to eliminate BIV in INSN using GIV. Return nonzero if
8659 the offset that we have to take into account due to auto-increment /
8660 div derivation is zero. */
8661 static int
8662 biv_elimination_giv_has_0_offset (struct induction *biv,
8663 struct induction *giv, rtx insn)
8665 /* If the giv V had the auto-inc address optimization applied
8666 to it, and INSN occurs between the giv insn and the biv
8667 insn, then we'd have to adjust the value used here.
8668 This is rare, so we don't bother to make this possible. */
8669 if (giv->auto_inc_opt
8670 && ((loop_insn_first_p (giv->insn, insn)
8671 && loop_insn_first_p (insn, biv->insn))
8672 || (loop_insn_first_p (biv->insn, insn)
8673 && loop_insn_first_p (insn, giv->insn))))
8674 return 0;
8676 return 1;
8679 /* If BL appears in X (part of the pattern of INSN), see if we can
8680 eliminate its use. If so, return 1. If not, return 0.
8682 If BIV does not appear in X, return 1.
8684 If ELIMINATE_P is nonzero, actually do the elimination.
8685 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
8686 Depending on how many items have been moved out of the loop, it
8687 will either be before INSN (when WHERE_INSN is nonzero) or at the
8688 start of the loop (when WHERE_INSN is zero). */
8690 static int
8691 maybe_eliminate_biv_1 (const struct loop *loop, rtx x, rtx insn,
8692 struct iv_class *bl, int eliminate_p,
8693 basic_block where_bb, rtx where_insn)
8695 enum rtx_code code = GET_CODE (x);
8696 rtx reg = bl->biv->dest_reg;
8697 enum machine_mode mode = GET_MODE (reg);
8698 struct induction *v;
8699 rtx arg, tem;
8700 #ifdef HAVE_cc0
8701 rtx new;
8702 #endif
8703 int arg_operand;
8704 const char *fmt;
8705 int i, j;
8707 switch (code)
8709 case REG:
8710 /* If we haven't already been able to do something with this BIV,
8711 we can't eliminate it. */
8712 if (x == reg)
8713 return 0;
8714 return 1;
8716 case SET:
8717 /* If this sets the BIV, it is not a problem. */
8718 if (SET_DEST (x) == reg)
8719 return 1;
8721 /* If this is an insn that defines a giv, it is also ok because
8722 it will go away when the giv is reduced. */
8723 for (v = bl->giv; v; v = v->next_iv)
8724 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8725 return 1;
8727 #ifdef HAVE_cc0
8728 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8730 /* Can replace with any giv that was reduced and
8731 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8732 Require a constant for MULT_VAL, so we know it's nonzero.
8733 ??? We disable this optimization to avoid potential
8734 overflows. */
8736 for (v = bl->giv; v; v = v->next_iv)
8737 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
8738 && v->add_val == const0_rtx
8739 && ! v->ignore && ! v->maybe_dead && v->always_computable
8740 && v->mode == mode
8741 && 0)
8743 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8744 continue;
8746 if (! eliminate_p)
8747 return 1;
8749 /* If the giv has the opposite direction of change,
8750 then reverse the comparison. */
8751 if (INTVAL (v->mult_val) < 0)
8752 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8753 const0_rtx, v->new_reg);
8754 else
8755 new = v->new_reg;
8757 /* We can probably test that giv's reduced reg. */
8758 if (validate_change (insn, &SET_SRC (x), new, 0))
8759 return 1;
8762 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8763 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8764 Require a constant for MULT_VAL, so we know it's nonzero.
8765 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8766 overflow problem. */
8768 for (v = bl->giv; v; v = v->next_iv)
8769 if (GET_CODE (v->mult_val) == CONST_INT
8770 && v->mult_val != const0_rtx
8771 && ! v->ignore && ! v->maybe_dead && v->always_computable
8772 && v->mode == mode
8773 && (GET_CODE (v->add_val) == SYMBOL_REF
8774 || GET_CODE (v->add_val) == LABEL_REF
8775 || GET_CODE (v->add_val) == CONST
8776 || (GET_CODE (v->add_val) == REG
8777 && REG_POINTER (v->add_val))))
8779 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8780 continue;
8782 if (! eliminate_p)
8783 return 1;
8785 /* If the giv has the opposite direction of change,
8786 then reverse the comparison. */
8787 if (INTVAL (v->mult_val) < 0)
8788 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8789 v->new_reg);
8790 else
8791 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8792 copy_rtx (v->add_val));
8794 /* Replace biv with the giv's reduced register. */
8795 update_reg_last_use (v->add_val, insn);
8796 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8797 return 1;
8799 /* Insn doesn't support that constant or invariant. Copy it
8800 into a register (it will be a loop invariant.) */
8801 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8803 loop_insn_emit_before (loop, 0, where_insn,
8804 gen_move_insn (tem,
8805 copy_rtx (v->add_val)));
8807 /* Substitute the new register for its invariant value in
8808 the compare expression. */
8809 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8810 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8811 return 1;
8814 #endif
8815 break;
8817 case COMPARE:
8818 case EQ: case NE:
8819 case GT: case GE: case GTU: case GEU:
8820 case LT: case LE: case LTU: case LEU:
8821 /* See if either argument is the biv. */
8822 if (XEXP (x, 0) == reg)
8823 arg = XEXP (x, 1), arg_operand = 1;
8824 else if (XEXP (x, 1) == reg)
8825 arg = XEXP (x, 0), arg_operand = 0;
8826 else
8827 break;
8829 if (CONSTANT_P (arg))
8831 /* First try to replace with any giv that has constant positive
8832 mult_val and constant add_val. We might be able to support
8833 negative mult_val, but it seems complex to do it in general. */
8835 for (v = bl->giv; v; v = v->next_iv)
8836 if (GET_CODE (v->mult_val) == CONST_INT
8837 && INTVAL (v->mult_val) > 0
8838 && (GET_CODE (v->add_val) == SYMBOL_REF
8839 || GET_CODE (v->add_val) == LABEL_REF
8840 || GET_CODE (v->add_val) == CONST
8841 || (GET_CODE (v->add_val) == REG
8842 && REG_POINTER (v->add_val)))
8843 && ! v->ignore && ! v->maybe_dead && v->always_computable
8844 && v->mode == mode)
8846 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8847 continue;
8849 /* Don't eliminate if the linear combination that makes up
8850 the giv overflows when it is applied to ARG. */
8851 if (GET_CODE (arg) == CONST_INT)
8853 rtx add_val;
8855 if (GET_CODE (v->add_val) == CONST_INT)
8856 add_val = v->add_val;
8857 else
8858 add_val = const0_rtx;
8860 if (const_mult_add_overflow_p (arg, v->mult_val,
8861 add_val, mode, 1))
8862 continue;
8865 if (! eliminate_p)
8866 return 1;
8868 /* Replace biv with the giv's reduced reg. */
8869 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
8871 /* If all constants are actually constant integers and
8872 the derived constant can be directly placed in the COMPARE,
8873 do so. */
8874 if (GET_CODE (arg) == CONST_INT
8875 && GET_CODE (v->add_val) == CONST_INT)
8877 tem = expand_mult_add (arg, NULL_RTX, v->mult_val,
8878 v->add_val, mode, 1);
8880 else
8882 /* Otherwise, load it into a register. */
8883 tem = gen_reg_rtx (mode);
8884 loop_iv_add_mult_emit_before (loop, arg,
8885 v->mult_val, v->add_val,
8886 tem, where_bb, where_insn);
8889 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8891 if (apply_change_group ())
8892 return 1;
8895 /* Look for giv with positive constant mult_val and nonconst add_val.
8896 Insert insns to calculate new compare value.
8897 ??? Turn this off due to possible overflow. */
8899 for (v = bl->giv; v; v = v->next_iv)
8900 if (GET_CODE (v->mult_val) == CONST_INT
8901 && INTVAL (v->mult_val) > 0
8902 && ! v->ignore && ! v->maybe_dead && v->always_computable
8903 && v->mode == mode
8904 && 0)
8906 rtx tem;
8908 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8909 continue;
8911 if (! eliminate_p)
8912 return 1;
8914 tem = gen_reg_rtx (mode);
8916 /* Replace biv with giv's reduced register. */
8917 validate_change (insn, &XEXP (x, 1 - arg_operand),
8918 v->new_reg, 1);
8920 /* Compute value to compare against. */
8921 loop_iv_add_mult_emit_before (loop, arg,
8922 v->mult_val, v->add_val,
8923 tem, where_bb, where_insn);
8924 /* Use it in this insn. */
8925 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8926 if (apply_change_group ())
8927 return 1;
8930 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8932 if (loop_invariant_p (loop, arg) == 1)
8934 /* Look for giv with constant positive mult_val and nonconst
8935 add_val. Insert insns to compute new compare value.
8936 ??? Turn this off due to possible overflow. */
8938 for (v = bl->giv; v; v = v->next_iv)
8939 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8940 && ! v->ignore && ! v->maybe_dead && v->always_computable
8941 && v->mode == mode
8942 && 0)
8944 rtx tem;
8946 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8947 continue;
8949 if (! eliminate_p)
8950 return 1;
8952 tem = gen_reg_rtx (mode);
8954 /* Replace biv with giv's reduced register. */
8955 validate_change (insn, &XEXP (x, 1 - arg_operand),
8956 v->new_reg, 1);
8958 /* Compute value to compare against. */
8959 loop_iv_add_mult_emit_before (loop, arg,
8960 v->mult_val, v->add_val,
8961 tem, where_bb, where_insn);
8962 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8963 if (apply_change_group ())
8964 return 1;
8968 /* This code has problems. Basically, you can't know when
8969 seeing if we will eliminate BL, whether a particular giv
8970 of ARG will be reduced. If it isn't going to be reduced,
8971 we can't eliminate BL. We can try forcing it to be reduced,
8972 but that can generate poor code.
8974 The problem is that the benefit of reducing TV, below should
8975 be increased if BL can actually be eliminated, but this means
8976 we might have to do a topological sort of the order in which
8977 we try to process biv. It doesn't seem worthwhile to do
8978 this sort of thing now. */
8980 #if 0
8981 /* Otherwise the reg compared with had better be a biv. */
8982 if (GET_CODE (arg) != REG
8983 || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
8984 return 0;
8986 /* Look for a pair of givs, one for each biv,
8987 with identical coefficients. */
8988 for (v = bl->giv; v; v = v->next_iv)
8990 struct induction *tv;
8992 if (v->ignore || v->maybe_dead || v->mode != mode)
8993 continue;
8995 for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
8996 tv = tv->next_iv)
8997 if (! tv->ignore && ! tv->maybe_dead
8998 && rtx_equal_p (tv->mult_val, v->mult_val)
8999 && rtx_equal_p (tv->add_val, v->add_val)
9000 && tv->mode == mode)
9002 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
9003 continue;
9005 if (! eliminate_p)
9006 return 1;
9008 /* Replace biv with its giv's reduced reg. */
9009 XEXP (x, 1 - arg_operand) = v->new_reg;
9010 /* Replace other operand with the other giv's
9011 reduced reg. */
9012 XEXP (x, arg_operand) = tv->new_reg;
9013 return 1;
9016 #endif
9019 /* If we get here, the biv can't be eliminated. */
9020 return 0;
9022 case MEM:
9023 /* If this address is a DEST_ADDR giv, it doesn't matter if the
9024 biv is used in it, since it will be replaced. */
9025 for (v = bl->giv; v; v = v->next_iv)
9026 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
9027 return 1;
9028 break;
9030 default:
9031 break;
9034 /* See if any subexpression fails elimination. */
9035 fmt = GET_RTX_FORMAT (code);
9036 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9038 switch (fmt[i])
9040 case 'e':
9041 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
9042 eliminate_p, where_bb, where_insn))
9043 return 0;
9044 break;
9046 case 'E':
9047 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9048 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
9049 eliminate_p, where_bb, where_insn))
9050 return 0;
9051 break;
9055 return 1;
9058 /* Return nonzero if the last use of REG
9059 is in an insn following INSN in the same basic block. */
9061 static int
9062 last_use_this_basic_block (rtx reg, rtx insn)
9064 rtx n;
9065 for (n = insn;
9066 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
9067 n = NEXT_INSN (n))
9069 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
9070 return 1;
9072 return 0;
9075 /* Called via `note_stores' to record the initial value of a biv. Here we
9076 just record the location of the set and process it later. */
9078 static void
9079 record_initial (rtx dest, rtx set, void *data ATTRIBUTE_UNUSED)
9081 struct loop_ivs *ivs = (struct loop_ivs *) data;
9082 struct iv_class *bl;
9084 if (GET_CODE (dest) != REG
9085 || REGNO (dest) >= ivs->n_regs
9086 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
9087 return;
9089 bl = REG_IV_CLASS (ivs, REGNO (dest));
9091 /* If this is the first set found, record it. */
9092 if (bl->init_insn == 0)
9094 bl->init_insn = note_insn;
9095 bl->init_set = set;
9099 /* If any of the registers in X are "old" and currently have a last use earlier
9100 than INSN, update them to have a last use of INSN. Their actual last use
9101 will be the previous insn but it will not have a valid uid_luid so we can't
9102 use it. X must be a source expression only. */
9104 static void
9105 update_reg_last_use (rtx x, rtx insn)
9107 /* Check for the case where INSN does not have a valid luid. In this case,
9108 there is no need to modify the regno_last_uid, as this can only happen
9109 when code is inserted after the loop_end to set a pseudo's final value,
9110 and hence this insn will never be the last use of x.
9111 ???? This comment is not correct. See for example loop_givs_reduce.
9112 This may insert an insn before another new insn. */
9113 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
9114 && INSN_UID (insn) < max_uid_for_loop
9115 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
9117 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
9119 else
9121 int i, j;
9122 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
9123 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9125 if (fmt[i] == 'e')
9126 update_reg_last_use (XEXP (x, i), insn);
9127 else if (fmt[i] == 'E')
9128 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9129 update_reg_last_use (XVECEXP (x, i, j), insn);
9134 /* Given an insn INSN and condition COND, return the condition in a
9135 canonical form to simplify testing by callers. Specifically:
9137 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
9138 (2) Both operands will be machine operands; (cc0) will have been replaced.
9139 (3) If an operand is a constant, it will be the second operand.
9140 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
9141 for GE, GEU, and LEU.
9143 If the condition cannot be understood, or is an inequality floating-point
9144 comparison which needs to be reversed, 0 will be returned.
9146 If REVERSE is nonzero, then reverse the condition prior to canonizing it.
9148 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9149 insn used in locating the condition was found. If a replacement test
9150 of the condition is desired, it should be placed in front of that
9151 insn and we will be sure that the inputs are still valid.
9153 If WANT_REG is nonzero, we wish the condition to be relative to that
9154 register, if possible. Therefore, do not canonicalize the condition
9155 further. */
9158 canonicalize_condition (rtx insn, rtx cond, int reverse, rtx *earliest,
9159 rtx want_reg)
9161 enum rtx_code code;
9162 rtx prev = insn;
9163 rtx set;
9164 rtx tem;
9165 rtx op0, op1;
9166 int reverse_code = 0;
9167 enum machine_mode mode;
9169 code = GET_CODE (cond);
9170 mode = GET_MODE (cond);
9171 op0 = XEXP (cond, 0);
9172 op1 = XEXP (cond, 1);
9174 if (reverse)
9175 code = reversed_comparison_code (cond, insn);
9176 if (code == UNKNOWN)
9177 return 0;
9179 if (earliest)
9180 *earliest = insn;
9182 /* If we are comparing a register with zero, see if the register is set
9183 in the previous insn to a COMPARE or a comparison operation. Perform
9184 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
9185 in cse.c */
9187 while (GET_RTX_CLASS (code) == '<'
9188 && op1 == CONST0_RTX (GET_MODE (op0))
9189 && op0 != want_reg)
9191 /* Set nonzero when we find something of interest. */
9192 rtx x = 0;
9194 #ifdef HAVE_cc0
9195 /* If comparison with cc0, import actual comparison from compare
9196 insn. */
9197 if (op0 == cc0_rtx)
9199 if ((prev = prev_nonnote_insn (prev)) == 0
9200 || GET_CODE (prev) != INSN
9201 || (set = single_set (prev)) == 0
9202 || SET_DEST (set) != cc0_rtx)
9203 return 0;
9205 op0 = SET_SRC (set);
9206 op1 = CONST0_RTX (GET_MODE (op0));
9207 if (earliest)
9208 *earliest = prev;
9210 #endif
9212 /* If this is a COMPARE, pick up the two things being compared. */
9213 if (GET_CODE (op0) == COMPARE)
9215 op1 = XEXP (op0, 1);
9216 op0 = XEXP (op0, 0);
9217 continue;
9219 else if (GET_CODE (op0) != REG)
9220 break;
9222 /* Go back to the previous insn. Stop if it is not an INSN. We also
9223 stop if it isn't a single set or if it has a REG_INC note because
9224 we don't want to bother dealing with it. */
9226 if ((prev = prev_nonnote_insn (prev)) == 0
9227 || GET_CODE (prev) != INSN
9228 || FIND_REG_INC_NOTE (prev, NULL_RTX))
9229 break;
9231 set = set_of (op0, prev);
9233 if (set
9234 && (GET_CODE (set) != SET
9235 || !rtx_equal_p (SET_DEST (set), op0)))
9236 break;
9238 /* If this is setting OP0, get what it sets it to if it looks
9239 relevant. */
9240 if (set)
9242 enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
9243 #ifdef FLOAT_STORE_FLAG_VALUE
9244 REAL_VALUE_TYPE fsfv;
9245 #endif
9247 /* ??? We may not combine comparisons done in a CCmode with
9248 comparisons not done in a CCmode. This is to aid targets
9249 like Alpha that have an IEEE compliant EQ instruction, and
9250 a non-IEEE compliant BEQ instruction. The use of CCmode is
9251 actually artificial, simply to prevent the combination, but
9252 should not affect other platforms.
9254 However, we must allow VOIDmode comparisons to match either
9255 CCmode or non-CCmode comparison, because some ports have
9256 modeless comparisons inside branch patterns.
9258 ??? This mode check should perhaps look more like the mode check
9259 in simplify_comparison in combine. */
9261 if ((GET_CODE (SET_SRC (set)) == COMPARE
9262 || (((code == NE
9263 || (code == LT
9264 && GET_MODE_CLASS (inner_mode) == MODE_INT
9265 && (GET_MODE_BITSIZE (inner_mode)
9266 <= HOST_BITS_PER_WIDE_INT)
9267 && (STORE_FLAG_VALUE
9268 & ((HOST_WIDE_INT) 1
9269 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9270 #ifdef FLOAT_STORE_FLAG_VALUE
9271 || (code == LT
9272 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9273 && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
9274 REAL_VALUE_NEGATIVE (fsfv)))
9275 #endif
9277 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
9278 && (((GET_MODE_CLASS (mode) == MODE_CC)
9279 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9280 || mode == VOIDmode || inner_mode == VOIDmode))
9281 x = SET_SRC (set);
9282 else if (((code == EQ
9283 || (code == GE
9284 && (GET_MODE_BITSIZE (inner_mode)
9285 <= HOST_BITS_PER_WIDE_INT)
9286 && GET_MODE_CLASS (inner_mode) == MODE_INT
9287 && (STORE_FLAG_VALUE
9288 & ((HOST_WIDE_INT) 1
9289 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9290 #ifdef FLOAT_STORE_FLAG_VALUE
9291 || (code == GE
9292 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9293 && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
9294 REAL_VALUE_NEGATIVE (fsfv)))
9295 #endif
9297 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
9298 && (((GET_MODE_CLASS (mode) == MODE_CC)
9299 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9300 || mode == VOIDmode || inner_mode == VOIDmode))
9303 reverse_code = 1;
9304 x = SET_SRC (set);
9306 else
9307 break;
9310 else if (reg_set_p (op0, prev))
9311 /* If this sets OP0, but not directly, we have to give up. */
9312 break;
9314 if (x)
9316 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9317 code = GET_CODE (x);
9318 if (reverse_code)
9320 code = reversed_comparison_code (x, prev);
9321 if (code == UNKNOWN)
9322 return 0;
9323 reverse_code = 0;
9326 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
9327 if (earliest)
9328 *earliest = prev;
9332 /* If constant is first, put it last. */
9333 if (CONSTANT_P (op0))
9334 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
9336 /* If OP0 is the result of a comparison, we weren't able to find what
9337 was really being compared, so fail. */
9338 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
9339 return 0;
9341 /* Canonicalize any ordered comparison with integers involving equality
9342 if we can do computations in the relevant mode and we do not
9343 overflow. */
9345 if (GET_CODE (op1) == CONST_INT
9346 && GET_MODE (op0) != VOIDmode
9347 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
9349 HOST_WIDE_INT const_val = INTVAL (op1);
9350 unsigned HOST_WIDE_INT uconst_val = const_val;
9351 unsigned HOST_WIDE_INT max_val
9352 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
9354 switch (code)
9356 case LE:
9357 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
9358 code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
9359 break;
9361 /* When cross-compiling, const_val might be sign-extended from
9362 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9363 case GE:
9364 if ((HOST_WIDE_INT) (const_val & max_val)
9365 != (((HOST_WIDE_INT) 1
9366 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9367 code = GT, op1 = gen_int_mode (const_val - 1, GET_MODE (op0));
9368 break;
9370 case LEU:
9371 if (uconst_val < max_val)
9372 code = LTU, op1 = gen_int_mode (uconst_val + 1, GET_MODE (op0));
9373 break;
9375 case GEU:
9376 if (uconst_val != 0)
9377 code = GTU, op1 = gen_int_mode (uconst_val - 1, GET_MODE (op0));
9378 break;
9380 default:
9381 break;
9385 /* Never return CC0; return zero instead. */
9386 if (CC0_P (op0))
9387 return 0;
9389 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
9392 /* Given a jump insn JUMP, return the condition that will cause it to branch
9393 to its JUMP_LABEL. If the condition cannot be understood, or is an
9394 inequality floating-point comparison which needs to be reversed, 0 will
9395 be returned.
9397 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9398 insn used in locating the condition was found. If a replacement test
9399 of the condition is desired, it should be placed in front of that
9400 insn and we will be sure that the inputs are still valid. */
9403 get_condition (rtx jump, rtx *earliest)
9405 rtx cond;
9406 int reverse;
9407 rtx set;
9409 /* If this is not a standard conditional jump, we can't parse it. */
9410 if (GET_CODE (jump) != JUMP_INSN
9411 || ! any_condjump_p (jump))
9412 return 0;
9413 set = pc_set (jump);
9415 cond = XEXP (SET_SRC (set), 0);
9417 /* If this branches to JUMP_LABEL when the condition is false, reverse
9418 the condition. */
9419 reverse
9420 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
9421 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
9423 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
9426 /* Similar to above routine, except that we also put an invariant last
9427 unless both operands are invariants. */
9430 get_condition_for_loop (const struct loop *loop, rtx x)
9432 rtx comparison = get_condition (x, (rtx*) 0);
9434 if (comparison == 0
9435 || ! loop_invariant_p (loop, XEXP (comparison, 0))
9436 || loop_invariant_p (loop, XEXP (comparison, 1)))
9437 return comparison;
9439 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
9440 XEXP (comparison, 1), XEXP (comparison, 0));
9443 /* Scan the function and determine whether it has indirect (computed) jumps.
9445 This is taken mostly from flow.c; similar code exists elsewhere
9446 in the compiler. It may be useful to put this into rtlanal.c. */
9447 static int
9448 indirect_jump_in_function_p (rtx start)
9450 rtx insn;
9452 for (insn = start; insn; insn = NEXT_INSN (insn))
9453 if (computed_jump_p (insn))
9454 return 1;
9456 return 0;
9459 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9460 documentation for LOOP_MEMS for the definition of `appropriate'.
9461 This function is called from prescan_loop via for_each_rtx. */
9463 static int
9464 insert_loop_mem (rtx *mem, void *data ATTRIBUTE_UNUSED)
9466 struct loop_info *loop_info = data;
9467 int i;
9468 rtx m = *mem;
9470 if (m == NULL_RTX)
9471 return 0;
9473 switch (GET_CODE (m))
9475 case MEM:
9476 break;
9478 case CLOBBER:
9479 /* We're not interested in MEMs that are only clobbered. */
9480 return -1;
9482 case CONST_DOUBLE:
9483 /* We're not interested in the MEM associated with a
9484 CONST_DOUBLE, so there's no need to traverse into this. */
9485 return -1;
9487 case EXPR_LIST:
9488 /* We're not interested in any MEMs that only appear in notes. */
9489 return -1;
9491 default:
9492 /* This is not a MEM. */
9493 return 0;
9496 /* See if we've already seen this MEM. */
9497 for (i = 0; i < loop_info->mems_idx; ++i)
9498 if (rtx_equal_p (m, loop_info->mems[i].mem))
9500 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
9501 /* The modes of the two memory accesses are different. If
9502 this happens, something tricky is going on, and we just
9503 don't optimize accesses to this MEM. */
9504 loop_info->mems[i].optimize = 0;
9506 return 0;
9509 /* Resize the array, if necessary. */
9510 if (loop_info->mems_idx == loop_info->mems_allocated)
9512 if (loop_info->mems_allocated != 0)
9513 loop_info->mems_allocated *= 2;
9514 else
9515 loop_info->mems_allocated = 32;
9517 loop_info->mems = xrealloc (loop_info->mems,
9518 loop_info->mems_allocated * sizeof (loop_mem_info));
9521 /* Actually insert the MEM. */
9522 loop_info->mems[loop_info->mems_idx].mem = m;
9523 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9524 because we can't put it in a register. We still store it in the
9525 table, though, so that if we see the same address later, but in a
9526 non-BLK mode, we'll not think we can optimize it at that point. */
9527 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
9528 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
9529 ++loop_info->mems_idx;
9531 return 0;
9535 /* Allocate REGS->ARRAY or reallocate it if it is too small.
9537 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
9538 register that is modified by an insn between FROM and TO. If the
9539 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
9540 more, stop incrementing it, to avoid overflow.
9542 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
9543 register I is used, if it is only used once. Otherwise, it is set
9544 to 0 (for no uses) or const0_rtx for more than one use. This
9545 parameter may be zero, in which case this processing is not done.
9547 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
9548 optimize register I. */
9550 static void
9551 loop_regs_scan (const struct loop *loop, int extra_size)
9553 struct loop_regs *regs = LOOP_REGS (loop);
9554 int old_nregs;
9555 /* last_set[n] is nonzero iff reg n has been set in the current
9556 basic block. In that case, it is the insn that last set reg n. */
9557 rtx *last_set;
9558 rtx insn;
9559 int i;
9561 old_nregs = regs->num;
9562 regs->num = max_reg_num ();
9564 /* Grow the regs array if not allocated or too small. */
9565 if (regs->num >= regs->size)
9567 regs->size = regs->num + extra_size;
9569 regs->array = xrealloc (regs->array, regs->size * sizeof (*regs->array));
9571 /* Zero the new elements. */
9572 memset (regs->array + old_nregs, 0,
9573 (regs->size - old_nregs) * sizeof (*regs->array));
9576 /* Clear previously scanned fields but do not clear n_times_set. */
9577 for (i = 0; i < old_nregs; i++)
9579 regs->array[i].set_in_loop = 0;
9580 regs->array[i].may_not_optimize = 0;
9581 regs->array[i].single_usage = NULL_RTX;
9584 last_set = xcalloc (regs->num, sizeof (rtx));
9586 /* Scan the loop, recording register usage. */
9587 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9588 insn = NEXT_INSN (insn))
9590 if (INSN_P (insn))
9592 /* Record registers that have exactly one use. */
9593 find_single_use_in_loop (regs, insn, PATTERN (insn));
9595 /* Include uses in REG_EQUAL notes. */
9596 if (REG_NOTES (insn))
9597 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
9599 if (GET_CODE (PATTERN (insn)) == SET
9600 || GET_CODE (PATTERN (insn)) == CLOBBER)
9601 count_one_set (regs, insn, PATTERN (insn), last_set);
9602 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
9604 int i;
9605 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
9606 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
9607 last_set);
9611 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
9612 memset (last_set, 0, regs->num * sizeof (rtx));
9614 /* Invalidate all registers used for function argument passing.
9615 We check rtx_varies_p for the same reason as below, to allow
9616 optimizing PIC calculations. */
9617 if (GET_CODE (insn) == CALL_INSN)
9619 rtx link;
9620 for (link = CALL_INSN_FUNCTION_USAGE (insn);
9621 link;
9622 link = XEXP (link, 1))
9624 rtx op, reg;
9626 if (GET_CODE (op = XEXP (link, 0)) == USE
9627 && GET_CODE (reg = XEXP (op, 0)) == REG
9628 && rtx_varies_p (reg, 1))
9629 regs->array[REGNO (reg)].may_not_optimize = 1;
9634 /* Invalidate all hard registers clobbered by calls. With one exception:
9635 a call-clobbered PIC register is still function-invariant for our
9636 purposes, since we can hoist any PIC calculations out of the loop.
9637 Thus the call to rtx_varies_p. */
9638 if (LOOP_INFO (loop)->has_call)
9639 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9640 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
9641 && rtx_varies_p (regno_reg_rtx[i], 1))
9643 regs->array[i].may_not_optimize = 1;
9644 regs->array[i].set_in_loop = 1;
9647 #ifdef AVOID_CCMODE_COPIES
9648 /* Don't try to move insns which set CC registers if we should not
9649 create CCmode register copies. */
9650 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
9651 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
9652 regs->array[i].may_not_optimize = 1;
9653 #endif
9655 /* Set regs->array[I].n_times_set for the new registers. */
9656 for (i = old_nregs; i < regs->num; i++)
9657 regs->array[i].n_times_set = regs->array[i].set_in_loop;
9659 free (last_set);
9662 /* Returns the number of real INSNs in the LOOP. */
9664 static int
9665 count_insns_in_loop (const struct loop *loop)
9667 int count = 0;
9668 rtx insn;
9670 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9671 insn = NEXT_INSN (insn))
9672 if (INSN_P (insn))
9673 ++count;
9675 return count;
9678 /* Move MEMs into registers for the duration of the loop. */
9680 static void
9681 load_mems (const struct loop *loop)
9683 struct loop_info *loop_info = LOOP_INFO (loop);
9684 struct loop_regs *regs = LOOP_REGS (loop);
9685 int maybe_never = 0;
9686 int i;
9687 rtx p, prev_ebb_head;
9688 rtx label = NULL_RTX;
9689 rtx end_label;
9690 /* Nonzero if the next instruction may never be executed. */
9691 int next_maybe_never = 0;
9692 unsigned int last_max_reg = max_reg_num ();
9694 if (loop_info->mems_idx == 0)
9695 return;
9697 /* We cannot use next_label here because it skips over normal insns. */
9698 end_label = next_nonnote_insn (loop->end);
9699 if (end_label && GET_CODE (end_label) != CODE_LABEL)
9700 end_label = NULL_RTX;
9702 /* Check to see if it's possible that some instructions in the loop are
9703 never executed. Also check if there is a goto out of the loop other
9704 than right after the end of the loop. */
9705 for (p = next_insn_in_loop (loop, loop->scan_start);
9706 p != NULL_RTX;
9707 p = next_insn_in_loop (loop, p))
9709 if (GET_CODE (p) == CODE_LABEL)
9710 maybe_never = 1;
9711 else if (GET_CODE (p) == JUMP_INSN
9712 /* If we enter the loop in the middle, and scan
9713 around to the beginning, don't set maybe_never
9714 for that. This must be an unconditional jump,
9715 otherwise the code at the top of the loop might
9716 never be executed. Unconditional jumps are
9717 followed a by barrier then loop end. */
9718 && ! (GET_CODE (p) == JUMP_INSN
9719 && JUMP_LABEL (p) == loop->top
9720 && NEXT_INSN (NEXT_INSN (p)) == loop->end
9721 && any_uncondjump_p (p)))
9723 /* If this is a jump outside of the loop but not right
9724 after the end of the loop, we would have to emit new fixup
9725 sequences for each such label. */
9726 if (/* If we can't tell where control might go when this
9727 JUMP_INSN is executed, we must be conservative. */
9728 !JUMP_LABEL (p)
9729 || (JUMP_LABEL (p) != end_label
9730 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
9731 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
9732 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end))))
9733 return;
9735 if (!any_condjump_p (p))
9736 /* Something complicated. */
9737 maybe_never = 1;
9738 else
9739 /* If there are any more instructions in the loop, they
9740 might not be reached. */
9741 next_maybe_never = 1;
9743 else if (next_maybe_never)
9744 maybe_never = 1;
9747 /* Find start of the extended basic block that enters the loop. */
9748 for (p = loop->start;
9749 PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
9750 p = PREV_INSN (p))
9752 prev_ebb_head = p;
9754 cselib_init ();
9756 /* Build table of mems that get set to constant values before the
9757 loop. */
9758 for (; p != loop->start; p = NEXT_INSN (p))
9759 cselib_process_insn (p);
9761 /* Actually move the MEMs. */
9762 for (i = 0; i < loop_info->mems_idx; ++i)
9764 regset_head load_copies;
9765 regset_head store_copies;
9766 int written = 0;
9767 rtx reg;
9768 rtx mem = loop_info->mems[i].mem;
9769 rtx mem_list_entry;
9771 if (MEM_VOLATILE_P (mem)
9772 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
9773 /* There's no telling whether or not MEM is modified. */
9774 loop_info->mems[i].optimize = 0;
9776 /* Go through the MEMs written to in the loop to see if this
9777 one is aliased by one of them. */
9778 mem_list_entry = loop_info->store_mems;
9779 while (mem_list_entry)
9781 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
9782 written = 1;
9783 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
9784 mem, rtx_varies_p))
9786 /* MEM is indeed aliased by this store. */
9787 loop_info->mems[i].optimize = 0;
9788 break;
9790 mem_list_entry = XEXP (mem_list_entry, 1);
9793 if (flag_float_store && written
9794 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
9795 loop_info->mems[i].optimize = 0;
9797 /* If this MEM is written to, we must be sure that there
9798 are no reads from another MEM that aliases this one. */
9799 if (loop_info->mems[i].optimize && written)
9801 int j;
9803 for (j = 0; j < loop_info->mems_idx; ++j)
9805 if (j == i)
9806 continue;
9807 else if (true_dependence (mem,
9808 VOIDmode,
9809 loop_info->mems[j].mem,
9810 rtx_varies_p))
9812 /* It's not safe to hoist loop_info->mems[i] out of
9813 the loop because writes to it might not be
9814 seen by reads from loop_info->mems[j]. */
9815 loop_info->mems[i].optimize = 0;
9816 break;
9821 if (maybe_never && may_trap_p (mem))
9822 /* We can't access the MEM outside the loop; it might
9823 cause a trap that wouldn't have happened otherwise. */
9824 loop_info->mems[i].optimize = 0;
9826 if (!loop_info->mems[i].optimize)
9827 /* We thought we were going to lift this MEM out of the
9828 loop, but later discovered that we could not. */
9829 continue;
9831 INIT_REG_SET (&load_copies);
9832 INIT_REG_SET (&store_copies);
9834 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9835 order to keep scan_loop from moving stores to this MEM
9836 out of the loop just because this REG is neither a
9837 user-variable nor used in the loop test. */
9838 reg = gen_reg_rtx (GET_MODE (mem));
9839 REG_USERVAR_P (reg) = 1;
9840 loop_info->mems[i].reg = reg;
9842 /* Now, replace all references to the MEM with the
9843 corresponding pseudos. */
9844 maybe_never = 0;
9845 for (p = next_insn_in_loop (loop, loop->scan_start);
9846 p != NULL_RTX;
9847 p = next_insn_in_loop (loop, p))
9849 if (INSN_P (p))
9851 rtx set;
9853 set = single_set (p);
9855 /* See if this copies the mem into a register that isn't
9856 modified afterwards. We'll try to do copy propagation
9857 a little further on. */
9858 if (set
9859 /* @@@ This test is _way_ too conservative. */
9860 && ! maybe_never
9861 && GET_CODE (SET_DEST (set)) == REG
9862 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
9863 && REGNO (SET_DEST (set)) < last_max_reg
9864 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
9865 && rtx_equal_p (SET_SRC (set), mem))
9866 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
9868 /* See if this copies the mem from a register that isn't
9869 modified afterwards. We'll try to remove the
9870 redundant copy later on by doing a little register
9871 renaming and copy propagation. This will help
9872 to untangle things for the BIV detection code. */
9873 if (set
9874 && ! maybe_never
9875 && GET_CODE (SET_SRC (set)) == REG
9876 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
9877 && REGNO (SET_SRC (set)) < last_max_reg
9878 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
9879 && rtx_equal_p (SET_DEST (set), mem))
9880 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
9882 /* If this is a call which uses / clobbers this memory
9883 location, we must not change the interface here. */
9884 if (GET_CODE (p) == CALL_INSN
9885 && reg_mentioned_p (loop_info->mems[i].mem,
9886 CALL_INSN_FUNCTION_USAGE (p)))
9888 cancel_changes (0);
9889 loop_info->mems[i].optimize = 0;
9890 break;
9892 else
9893 /* Replace the memory reference with the shadow register. */
9894 replace_loop_mems (p, loop_info->mems[i].mem,
9895 loop_info->mems[i].reg, written);
9898 if (GET_CODE (p) == CODE_LABEL
9899 || GET_CODE (p) == JUMP_INSN)
9900 maybe_never = 1;
9903 if (! loop_info->mems[i].optimize)
9904 ; /* We found we couldn't do the replacement, so do nothing. */
9905 else if (! apply_change_group ())
9906 /* We couldn't replace all occurrences of the MEM. */
9907 loop_info->mems[i].optimize = 0;
9908 else
9910 /* Load the memory immediately before LOOP->START, which is
9911 the NOTE_LOOP_BEG. */
9912 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9913 rtx set;
9914 rtx best = mem;
9915 int j;
9916 struct elt_loc_list *const_equiv = 0;
9918 if (e)
9920 struct elt_loc_list *equiv;
9921 struct elt_loc_list *best_equiv = 0;
9922 for (equiv = e->locs; equiv; equiv = equiv->next)
9924 if (CONSTANT_P (equiv->loc))
9925 const_equiv = equiv;
9926 else if (GET_CODE (equiv->loc) == REG
9927 /* Extending hard register lifetimes causes crash
9928 on SRC targets. Doing so on non-SRC is
9929 probably also not good idea, since we most
9930 probably have pseudoregister equivalence as
9931 well. */
9932 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
9933 best_equiv = equiv;
9935 /* Use the constant equivalence if that is cheap enough. */
9936 if (! best_equiv)
9937 best_equiv = const_equiv;
9938 else if (const_equiv
9939 && (rtx_cost (const_equiv->loc, SET)
9940 <= rtx_cost (best_equiv->loc, SET)))
9942 best_equiv = const_equiv;
9943 const_equiv = 0;
9946 /* If best_equiv is nonzero, we know that MEM is set to a
9947 constant or register before the loop. We will use this
9948 knowledge to initialize the shadow register with that
9949 constant or reg rather than by loading from MEM. */
9950 if (best_equiv)
9951 best = copy_rtx (best_equiv->loc);
9954 set = gen_move_insn (reg, best);
9955 set = loop_insn_hoist (loop, set);
9956 if (REG_P (best))
9958 for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
9959 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
9961 REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
9962 break;
9966 if (const_equiv)
9967 set_unique_reg_note (set, REG_EQUAL, copy_rtx (const_equiv->loc));
9969 if (written)
9971 if (label == NULL_RTX)
9973 label = gen_label_rtx ();
9974 emit_label_after (label, loop->end);
9977 /* Store the memory immediately after END, which is
9978 the NOTE_LOOP_END. */
9979 set = gen_move_insn (copy_rtx (mem), reg);
9980 loop_insn_emit_after (loop, 0, label, set);
9983 if (loop_dump_stream)
9985 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9986 REGNO (reg), (written ? "r/w" : "r/o"));
9987 print_rtl (loop_dump_stream, mem);
9988 fputc ('\n', loop_dump_stream);
9991 /* Attempt a bit of copy propagation. This helps untangle the
9992 data flow, and enables {basic,general}_induction_var to find
9993 more bivs/givs. */
9994 EXECUTE_IF_SET_IN_REG_SET
9995 (&load_copies, FIRST_PSEUDO_REGISTER, j,
9997 try_copy_prop (loop, reg, j);
9999 CLEAR_REG_SET (&load_copies);
10001 EXECUTE_IF_SET_IN_REG_SET
10002 (&store_copies, FIRST_PSEUDO_REGISTER, j,
10004 try_swap_copy_prop (loop, reg, j);
10006 CLEAR_REG_SET (&store_copies);
10010 if (label != NULL_RTX && end_label != NULL_RTX)
10012 /* Now, we need to replace all references to the previous exit
10013 label with the new one. */
10014 replace_label_data rr;
10015 rr.r1 = end_label;
10016 rr.r2 = label;
10017 rr.update_label_nuses = true;
10019 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
10021 for_each_rtx (&p, replace_label, &rr);
10025 cselib_finish ();
10028 /* For communication between note_reg_stored and its caller. */
10029 struct note_reg_stored_arg
10031 int set_seen;
10032 rtx reg;
10035 /* Called via note_stores, record in SET_SEEN whether X, which is written,
10036 is equal to ARG. */
10037 static void
10038 note_reg_stored (rtx x, rtx setter ATTRIBUTE_UNUSED, void *arg)
10040 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
10041 if (t->reg == x)
10042 t->set_seen = 1;
10045 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
10046 There must be exactly one insn that sets this pseudo; it will be
10047 deleted if all replacements succeed and we can prove that the register
10048 is not used after the loop. */
10050 static void
10051 try_copy_prop (const struct loop *loop, rtx replacement, unsigned int regno)
10053 /* This is the reg that we are copying from. */
10054 rtx reg_rtx = regno_reg_rtx[regno];
10055 rtx init_insn = 0;
10056 rtx insn;
10057 /* These help keep track of whether we replaced all uses of the reg. */
10058 int replaced_last = 0;
10059 int store_is_first = 0;
10061 for (insn = next_insn_in_loop (loop, loop->scan_start);
10062 insn != NULL_RTX;
10063 insn = next_insn_in_loop (loop, insn))
10065 rtx set;
10067 /* Only substitute within one extended basic block from the initializing
10068 insn. */
10069 if (GET_CODE (insn) == CODE_LABEL && init_insn)
10070 break;
10072 if (! INSN_P (insn))
10073 continue;
10075 /* Is this the initializing insn? */
10076 set = single_set (insn);
10077 if (set
10078 && GET_CODE (SET_DEST (set)) == REG
10079 && REGNO (SET_DEST (set)) == regno)
10081 if (init_insn)
10082 abort ();
10084 init_insn = insn;
10085 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
10086 store_is_first = 1;
10089 /* Only substitute after seeing the initializing insn. */
10090 if (init_insn && insn != init_insn)
10092 struct note_reg_stored_arg arg;
10094 replace_loop_regs (insn, reg_rtx, replacement);
10095 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
10096 replaced_last = 1;
10098 /* Stop replacing when REPLACEMENT is modified. */
10099 arg.reg = replacement;
10100 arg.set_seen = 0;
10101 note_stores (PATTERN (insn), note_reg_stored, &arg);
10102 if (arg.set_seen)
10104 rtx note = find_reg_note (insn, REG_EQUAL, NULL);
10106 /* It is possible that we've turned previously valid REG_EQUAL to
10107 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
10108 REPLACEMENT is modified, we get different meaning. */
10109 if (note && reg_mentioned_p (replacement, XEXP (note, 0)))
10110 remove_note (insn, note);
10111 break;
10115 if (! init_insn)
10116 abort ();
10117 if (apply_change_group ())
10119 if (loop_dump_stream)
10120 fprintf (loop_dump_stream, " Replaced reg %d", regno);
10121 if (store_is_first && replaced_last)
10123 rtx first;
10124 rtx retval_note;
10126 /* Assume we're just deleting INIT_INSN. */
10127 first = init_insn;
10128 /* Look for REG_RETVAL note. If we're deleting the end of
10129 the libcall sequence, the whole sequence can go. */
10130 retval_note = find_reg_note (init_insn, REG_RETVAL, NULL_RTX);
10131 /* If we found a REG_RETVAL note, find the first instruction
10132 in the sequence. */
10133 if (retval_note)
10134 first = XEXP (retval_note, 0);
10136 /* Delete the instructions. */
10137 loop_delete_insns (first, init_insn);
10139 if (loop_dump_stream)
10140 fprintf (loop_dump_stream, ".\n");
10144 /* Replace all the instructions from FIRST up to and including LAST
10145 with NOTE_INSN_DELETED notes. */
10147 static void
10148 loop_delete_insns (rtx first, rtx last)
10150 while (1)
10152 if (loop_dump_stream)
10153 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
10154 INSN_UID (first));
10155 delete_insn (first);
10157 /* If this was the LAST instructions we're supposed to delete,
10158 we're done. */
10159 if (first == last)
10160 break;
10162 first = NEXT_INSN (first);
10166 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
10167 loop LOOP if the order of the sets of these registers can be
10168 swapped. There must be exactly one insn within the loop that sets
10169 this pseudo followed immediately by a move insn that sets
10170 REPLACEMENT with REGNO. */
10171 static void
10172 try_swap_copy_prop (const struct loop *loop, rtx replacement,
10173 unsigned int regno)
10175 rtx insn;
10176 rtx set = NULL_RTX;
10177 unsigned int new_regno;
10179 new_regno = REGNO (replacement);
10181 for (insn = next_insn_in_loop (loop, loop->scan_start);
10182 insn != NULL_RTX;
10183 insn = next_insn_in_loop (loop, insn))
10185 /* Search for the insn that copies REGNO to NEW_REGNO? */
10186 if (INSN_P (insn)
10187 && (set = single_set (insn))
10188 && GET_CODE (SET_DEST (set)) == REG
10189 && REGNO (SET_DEST (set)) == new_regno
10190 && GET_CODE (SET_SRC (set)) == REG
10191 && REGNO (SET_SRC (set)) == regno)
10192 break;
10195 if (insn != NULL_RTX)
10197 rtx prev_insn;
10198 rtx prev_set;
10200 /* Some DEF-USE info would come in handy here to make this
10201 function more general. For now, just check the previous insn
10202 which is the most likely candidate for setting REGNO. */
10204 prev_insn = PREV_INSN (insn);
10206 if (INSN_P (insn)
10207 && (prev_set = single_set (prev_insn))
10208 && GET_CODE (SET_DEST (prev_set)) == REG
10209 && REGNO (SET_DEST (prev_set)) == regno)
10211 /* We have:
10212 (set (reg regno) (expr))
10213 (set (reg new_regno) (reg regno))
10215 so try converting this to:
10216 (set (reg new_regno) (expr))
10217 (set (reg regno) (reg new_regno))
10219 The former construct is often generated when a global
10220 variable used for an induction variable is shadowed by a
10221 register (NEW_REGNO). The latter construct improves the
10222 chances of GIV replacement and BIV elimination. */
10224 validate_change (prev_insn, &SET_DEST (prev_set),
10225 replacement, 1);
10226 validate_change (insn, &SET_DEST (set),
10227 SET_SRC (set), 1);
10228 validate_change (insn, &SET_SRC (set),
10229 replacement, 1);
10231 if (apply_change_group ())
10233 if (loop_dump_stream)
10234 fprintf (loop_dump_stream,
10235 " Swapped set of reg %d at %d with reg %d at %d.\n",
10236 regno, INSN_UID (insn),
10237 new_regno, INSN_UID (prev_insn));
10239 /* Update first use of REGNO. */
10240 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
10241 REGNO_FIRST_UID (regno) = INSN_UID (insn);
10243 /* Now perform copy propagation to hopefully
10244 remove all uses of REGNO within the loop. */
10245 try_copy_prop (loop, replacement, regno);
10251 /* Worker function for find_mem_in_note, called via for_each_rtx. */
10253 static int
10254 find_mem_in_note_1 (rtx *x, void *data)
10256 if (*x != NULL_RTX && GET_CODE (*x) == MEM)
10258 rtx *res = (rtx *) data;
10259 *res = *x;
10260 return 1;
10262 return 0;
10265 /* Returns the first MEM found in NOTE by depth-first search. */
10267 static rtx
10268 find_mem_in_note (rtx note)
10270 if (note && for_each_rtx (&note, find_mem_in_note_1, &note))
10271 return note;
10272 return NULL_RTX;
10275 /* Replace MEM with its associated pseudo register. This function is
10276 called from load_mems via for_each_rtx. DATA is actually a pointer
10277 to a structure describing the instruction currently being scanned
10278 and the MEM we are currently replacing. */
10280 static int
10281 replace_loop_mem (rtx *mem, void *data)
10283 loop_replace_args *args = (loop_replace_args *) data;
10284 rtx m = *mem;
10286 if (m == NULL_RTX)
10287 return 0;
10289 switch (GET_CODE (m))
10291 case MEM:
10292 break;
10294 case CONST_DOUBLE:
10295 /* We're not interested in the MEM associated with a
10296 CONST_DOUBLE, so there's no need to traverse into one. */
10297 return -1;
10299 default:
10300 /* This is not a MEM. */
10301 return 0;
10304 if (!rtx_equal_p (args->match, m))
10305 /* This is not the MEM we are currently replacing. */
10306 return 0;
10308 /* Actually replace the MEM. */
10309 validate_change (args->insn, mem, args->replacement, 1);
10311 return 0;
10314 static void
10315 replace_loop_mems (rtx insn, rtx mem, rtx reg, int written)
10317 loop_replace_args args;
10319 args.insn = insn;
10320 args.match = mem;
10321 args.replacement = reg;
10323 for_each_rtx (&insn, replace_loop_mem, &args);
10325 /* If we hoist a mem write out of the loop, then REG_EQUAL
10326 notes referring to the mem are no longer valid. */
10327 if (written)
10329 rtx note, sub;
10330 rtx *link;
10332 for (link = &REG_NOTES (insn); (note = *link); link = &XEXP (note, 1))
10334 if (REG_NOTE_KIND (note) == REG_EQUAL
10335 && (sub = find_mem_in_note (note))
10336 && true_dependence (mem, VOIDmode, sub, rtx_varies_p))
10338 /* Remove the note. */
10339 validate_change (NULL_RTX, link, XEXP (note, 1), 1);
10340 break;
10346 /* Replace one register with another. Called through for_each_rtx; PX points
10347 to the rtx being scanned. DATA is actually a pointer to
10348 a structure of arguments. */
10350 static int
10351 replace_loop_reg (rtx *px, void *data)
10353 rtx x = *px;
10354 loop_replace_args *args = (loop_replace_args *) data;
10356 if (x == NULL_RTX)
10357 return 0;
10359 if (x == args->match)
10360 validate_change (args->insn, px, args->replacement, 1);
10362 return 0;
10365 static void
10366 replace_loop_regs (rtx insn, rtx reg, rtx replacement)
10368 loop_replace_args args;
10370 args.insn = insn;
10371 args.match = reg;
10372 args.replacement = replacement;
10374 for_each_rtx (&insn, replace_loop_reg, &args);
10377 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
10378 (ignored in the interim). */
10380 static rtx
10381 loop_insn_emit_after (const struct loop *loop ATTRIBUTE_UNUSED,
10382 basic_block where_bb ATTRIBUTE_UNUSED, rtx where_insn,
10383 rtx pattern)
10385 return emit_insn_after (pattern, where_insn);
10389 /* If WHERE_INSN is nonzero emit insn for PATTERN before WHERE_INSN
10390 in basic block WHERE_BB (ignored in the interim) within the loop
10391 otherwise hoist PATTERN into the loop pre-header. */
10394 loop_insn_emit_before (const struct loop *loop,
10395 basic_block where_bb ATTRIBUTE_UNUSED,
10396 rtx where_insn, rtx pattern)
10398 if (! where_insn)
10399 return loop_insn_hoist (loop, pattern);
10400 return emit_insn_before (pattern, where_insn);
10404 /* Emit call insn for PATTERN before WHERE_INSN in basic block
10405 WHERE_BB (ignored in the interim) within the loop. */
10407 static rtx
10408 loop_call_insn_emit_before (const struct loop *loop ATTRIBUTE_UNUSED,
10409 basic_block where_bb ATTRIBUTE_UNUSED,
10410 rtx where_insn, rtx pattern)
10412 return emit_call_insn_before (pattern, where_insn);
10416 /* Hoist insn for PATTERN into the loop pre-header. */
10419 loop_insn_hoist (const struct loop *loop, rtx pattern)
10421 return loop_insn_emit_before (loop, 0, loop->start, pattern);
10425 /* Hoist call insn for PATTERN into the loop pre-header. */
10427 static rtx
10428 loop_call_insn_hoist (const struct loop *loop, rtx pattern)
10430 return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
10434 /* Sink insn for PATTERN after the loop end. */
10437 loop_insn_sink (const struct loop *loop, rtx pattern)
10439 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
10442 /* bl->final_value can be either general_operand or PLUS of general_operand
10443 and constant. Emit sequence of instructions to load it into REG. */
10444 static rtx
10445 gen_load_of_final_value (rtx reg, rtx final_value)
10447 rtx seq;
10448 start_sequence ();
10449 final_value = force_operand (final_value, reg);
10450 if (final_value != reg)
10451 emit_move_insn (reg, final_value);
10452 seq = get_insns ();
10453 end_sequence ();
10454 return seq;
10457 /* If the loop has multiple exits, emit insn for PATTERN before the
10458 loop to ensure that it will always be executed no matter how the
10459 loop exits. Otherwise, emit the insn for PATTERN after the loop,
10460 since this is slightly more efficient. */
10462 static rtx
10463 loop_insn_sink_or_swim (const struct loop *loop, rtx pattern)
10465 if (loop->exit_count)
10466 return loop_insn_hoist (loop, pattern);
10467 else
10468 return loop_insn_sink (loop, pattern);
10471 static void
10472 loop_ivs_dump (const struct loop *loop, FILE *file, int verbose)
10474 struct iv_class *bl;
10475 int iv_num = 0;
10477 if (! loop || ! file)
10478 return;
10480 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10481 iv_num++;
10483 fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
10485 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10487 loop_iv_class_dump (bl, file, verbose);
10488 fputc ('\n', file);
10493 static void
10494 loop_iv_class_dump (const struct iv_class *bl, FILE *file,
10495 int verbose ATTRIBUTE_UNUSED)
10497 struct induction *v;
10498 rtx incr;
10499 int i;
10501 if (! bl || ! file)
10502 return;
10504 fprintf (file, "IV class for reg %d, benefit %d\n",
10505 bl->regno, bl->total_benefit);
10507 fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
10508 if (bl->initial_value)
10510 fprintf (file, ", init val: ");
10511 print_simple_rtl (file, bl->initial_value);
10513 if (bl->initial_test)
10515 fprintf (file, ", init test: ");
10516 print_simple_rtl (file, bl->initial_test);
10518 fputc ('\n', file);
10520 if (bl->final_value)
10522 fprintf (file, " Final val: ");
10523 print_simple_rtl (file, bl->final_value);
10524 fputc ('\n', file);
10527 if ((incr = biv_total_increment (bl)))
10529 fprintf (file, " Total increment: ");
10530 print_simple_rtl (file, incr);
10531 fputc ('\n', file);
10534 /* List the increments. */
10535 for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
10537 fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
10538 print_simple_rtl (file, v->add_val);
10539 fputc ('\n', file);
10542 /* List the givs. */
10543 for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
10545 fprintf (file, " Giv%d: insn %d, benefit %d, ",
10546 i, INSN_UID (v->insn), v->benefit);
10547 if (v->giv_type == DEST_ADDR)
10548 print_simple_rtl (file, v->mem);
10549 else
10550 print_simple_rtl (file, single_set (v->insn));
10551 fputc ('\n', file);
10556 static void
10557 loop_biv_dump (const struct induction *v, FILE *file, int verbose)
10559 if (! v || ! file)
10560 return;
10562 fprintf (file,
10563 "Biv %d: insn %d",
10564 REGNO (v->dest_reg), INSN_UID (v->insn));
10565 fprintf (file, " const ");
10566 print_simple_rtl (file, v->add_val);
10568 if (verbose && v->final_value)
10570 fputc ('\n', file);
10571 fprintf (file, " final ");
10572 print_simple_rtl (file, v->final_value);
10575 fputc ('\n', file);
10579 static void
10580 loop_giv_dump (const struct induction *v, FILE *file, int verbose)
10582 if (! v || ! file)
10583 return;
10585 if (v->giv_type == DEST_REG)
10586 fprintf (file, "Giv %d: insn %d",
10587 REGNO (v->dest_reg), INSN_UID (v->insn));
10588 else
10589 fprintf (file, "Dest address: insn %d",
10590 INSN_UID (v->insn));
10592 fprintf (file, " src reg %d benefit %d",
10593 REGNO (v->src_reg), v->benefit);
10594 fprintf (file, " lifetime %d",
10595 v->lifetime);
10597 if (v->replaceable)
10598 fprintf (file, " replaceable");
10600 if (v->no_const_addval)
10601 fprintf (file, " ncav");
10603 if (v->ext_dependent)
10605 switch (GET_CODE (v->ext_dependent))
10607 case SIGN_EXTEND:
10608 fprintf (file, " ext se");
10609 break;
10610 case ZERO_EXTEND:
10611 fprintf (file, " ext ze");
10612 break;
10613 case TRUNCATE:
10614 fprintf (file, " ext tr");
10615 break;
10616 default:
10617 abort ();
10621 fputc ('\n', file);
10622 fprintf (file, " mult ");
10623 print_simple_rtl (file, v->mult_val);
10625 fputc ('\n', file);
10626 fprintf (file, " add ");
10627 print_simple_rtl (file, v->add_val);
10629 if (verbose && v->final_value)
10631 fputc ('\n', file);
10632 fprintf (file, " final ");
10633 print_simple_rtl (file, v->final_value);
10636 fputc ('\n', file);
10640 void
10641 debug_ivs (const struct loop *loop)
10643 loop_ivs_dump (loop, stderr, 1);
10647 void
10648 debug_iv_class (const struct iv_class *bl)
10650 loop_iv_class_dump (bl, stderr, 1);
10654 void
10655 debug_biv (const struct induction *v)
10657 loop_biv_dump (v, stderr, 1);
10661 void
10662 debug_giv (const struct induction *v)
10664 loop_giv_dump (v, stderr, 1);
10668 #define LOOP_BLOCK_NUM_1(INSN) \
10669 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
10671 /* The notes do not have an assigned block, so look at the next insn. */
10672 #define LOOP_BLOCK_NUM(INSN) \
10673 ((INSN) ? (GET_CODE (INSN) == NOTE \
10674 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
10675 : LOOP_BLOCK_NUM_1 (INSN)) \
10676 : -1)
10678 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
10680 static void
10681 loop_dump_aux (const struct loop *loop, FILE *file,
10682 int verbose ATTRIBUTE_UNUSED)
10684 rtx label;
10686 if (! loop || ! file)
10687 return;
10689 /* Print diagnostics to compare our concept of a loop with
10690 what the loop notes say. */
10691 if (! PREV_INSN (loop->first->head)
10692 || GET_CODE (PREV_INSN (loop->first->head)) != NOTE
10693 || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
10694 != NOTE_INSN_LOOP_BEG)
10695 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
10696 INSN_UID (PREV_INSN (loop->first->head)));
10697 if (! NEXT_INSN (loop->last->end)
10698 || GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
10699 || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
10700 != NOTE_INSN_LOOP_END)
10701 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
10702 INSN_UID (NEXT_INSN (loop->last->end)));
10704 if (loop->start)
10706 fprintf (file,
10707 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
10708 LOOP_BLOCK_NUM (loop->start),
10709 LOOP_INSN_UID (loop->start),
10710 LOOP_BLOCK_NUM (loop->cont),
10711 LOOP_INSN_UID (loop->cont),
10712 LOOP_BLOCK_NUM (loop->cont),
10713 LOOP_INSN_UID (loop->cont),
10714 LOOP_BLOCK_NUM (loop->vtop),
10715 LOOP_INSN_UID (loop->vtop),
10716 LOOP_BLOCK_NUM (loop->end),
10717 LOOP_INSN_UID (loop->end));
10718 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
10719 LOOP_BLOCK_NUM (loop->top),
10720 LOOP_INSN_UID (loop->top),
10721 LOOP_BLOCK_NUM (loop->scan_start),
10722 LOOP_INSN_UID (loop->scan_start));
10723 fprintf (file, ";; exit_count %d", loop->exit_count);
10724 if (loop->exit_count)
10726 fputs (", labels:", file);
10727 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
10729 fprintf (file, " %d ",
10730 LOOP_INSN_UID (XEXP (label, 0)));
10733 fputs ("\n", file);
10735 /* This can happen when a marked loop appears as two nested loops,
10736 say from while (a || b) {}. The inner loop won't match
10737 the loop markers but the outer one will. */
10738 if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
10739 fprintf (file, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
10743 /* Call this function from the debugger to dump LOOP. */
10745 void
10746 debug_loop (const struct loop *loop)
10748 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
10751 /* Call this function from the debugger to dump LOOPS. */
10753 void
10754 debug_loops (const struct loops *loops)
10756 flow_loops_dump (loops, stderr, loop_dump_aux, 1);