* config/arm/netbsd.h (TARGET_OS_CPP_BUILTINS): Use
[official-gcc.git] / gcc / loop.c
blob7d3c5b2eebe622a0b89d1cf592f9a9ee51c6227d
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 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 "rtl.h"
40 #include "tm_p.h"
41 #include "obstack.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "hard-reg-set.h"
45 #include "basic-block.h"
46 #include "insn-config.h"
47 #include "regs.h"
48 #include "recog.h"
49 #include "flags.h"
50 #include "real.h"
51 #include "loop.h"
52 #include "cselib.h"
53 #include "except.h"
54 #include "toplev.h"
55 #include "predict.h"
56 #include "insn-flags.h"
57 #include "optabs.h"
59 /* Not really meaningful values, but at least something. */
60 #ifndef SIMULTANEOUS_PREFETCHES
61 #define SIMULTANEOUS_PREFETCHES 3
62 #endif
63 #ifndef PREFETCH_BLOCK
64 #define PREFETCH_BLOCK 32
65 #endif
66 #ifndef HAVE_prefetch
67 #define HAVE_prefetch 0
68 #define CODE_FOR_prefetch 0
69 #define gen_prefetch(a,b,c) (abort(), NULL_RTX)
70 #endif
72 /* Give up the prefetch optimizations once we exceed a given threshhold.
73 It is unlikely that we would be able to optimize something in a loop
74 with so many detected prefetches. */
75 #define MAX_PREFETCHES 100
76 /* The number of prefetch blocks that are beneficial to fetch at once before
77 a loop with a known (and low) iteration count. */
78 #define PREFETCH_BLOCKS_BEFORE_LOOP_MAX 6
79 /* For very tiny loops it is not worthwhile to prefetch even before the loop,
80 since it is likely that the data are already in the cache. */
81 #define PREFETCH_BLOCKS_BEFORE_LOOP_MIN 2
82 /* The minimal number of prefetch blocks that a loop must consume to make
83 the emitting of prefetch instruction in the body of loop worthwhile. */
84 #define PREFETCH_BLOCKS_IN_LOOP_MIN 6
86 /* Parameterize some prefetch heuristics so they can be turned on and off
87 easily for performance testing on new architecures. These can be
88 defined in target-dependent files. */
90 /* Prefetch is worthwhile only when loads/stores are dense. */
91 #ifndef PREFETCH_ONLY_DENSE_MEM
92 #define PREFETCH_ONLY_DENSE_MEM 1
93 #endif
95 /* Define what we mean by "dense" loads and stores; This value divided by 256
96 is the minimum percentage of memory references that worth prefetching. */
97 #ifndef PREFETCH_DENSE_MEM
98 #define PREFETCH_DENSE_MEM 220
99 #endif
101 /* Do not prefetch for a loop whose iteration count is known to be low. */
102 #ifndef PREFETCH_NO_LOW_LOOPCNT
103 #define PREFETCH_NO_LOW_LOOPCNT 1
104 #endif
106 /* Define what we mean by a "low" iteration count. */
107 #ifndef PREFETCH_LOW_LOOPCNT
108 #define PREFETCH_LOW_LOOPCNT 32
109 #endif
111 /* Do not prefetch for a loop that contains a function call; such a loop is
112 probably not an internal loop. */
113 #ifndef PREFETCH_NO_CALL
114 #define PREFETCH_NO_CALL 1
115 #endif
117 /* Do not prefetch accesses with an extreme stride. */
118 #ifndef PREFETCH_NO_EXTREME_STRIDE
119 #define PREFETCH_NO_EXTREME_STRIDE 1
120 #endif
122 /* Define what we mean by an "extreme" stride. */
123 #ifndef PREFETCH_EXTREME_STRIDE
124 #define PREFETCH_EXTREME_STRIDE 4096
125 #endif
127 /* Define a limit to how far apart indices can be and still be merged
128 into a single prefetch. */
129 #ifndef PREFETCH_EXTREME_DIFFERENCE
130 #define PREFETCH_EXTREME_DIFFERENCE 4096
131 #endif
133 /* Issue prefetch instructions before the loop to fetch data to be used
134 in the first few loop iterations. */
135 #ifndef PREFETCH_BEFORE_LOOP
136 #define PREFETCH_BEFORE_LOOP 1
137 #endif
139 /* Do not handle reversed order prefetches (negative stride). */
140 #ifndef PREFETCH_NO_REVERSE_ORDER
141 #define PREFETCH_NO_REVERSE_ORDER 1
142 #endif
144 /* Prefetch even if the GIV is in conditional code. */
145 #ifndef PREFETCH_CONDITIONAL
146 #define PREFETCH_CONDITIONAL 1
147 #endif
149 /* If the loop requires more prefetches than the target can process in
150 parallel then don't prefetch anything in that loop. */
151 #ifndef PREFETCH_LIMIT_TO_SIMULTANEOUS
152 #define PREFETCH_LIMIT_TO_SIMULTANEOUS 1
153 #endif
155 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
156 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
158 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
159 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
160 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
162 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
163 ((REGNO) < FIRST_PSEUDO_REGISTER \
164 ? HARD_REGNO_NREGS ((REGNO), GET_MODE (SET_DEST)) : 1)
167 /* Vector mapping INSN_UIDs to luids.
168 The luids are like uids but increase monotonically always.
169 We use them to see whether a jump comes from outside a given loop. */
171 int *uid_luid;
173 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
174 number the insn is contained in. */
176 struct loop **uid_loop;
178 /* 1 + largest uid of any insn. */
180 int max_uid_for_loop;
182 /* 1 + luid of last insn. */
184 static int max_luid;
186 /* Number of loops detected in current function. Used as index to the
187 next few tables. */
189 static int max_loop_num;
191 /* Bound on pseudo register number before loop optimization.
192 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
193 unsigned int max_reg_before_loop;
195 /* The value to pass to the next call of reg_scan_update. */
196 static int loop_max_reg;
198 #define obstack_chunk_alloc xmalloc
199 #define obstack_chunk_free free
201 /* During the analysis of a loop, a chain of `struct movable's
202 is made to record all the movable insns found.
203 Then the entire chain can be scanned to decide which to move. */
205 struct movable
207 rtx insn; /* A movable insn */
208 rtx set_src; /* The expression this reg is set from. */
209 rtx set_dest; /* The destination of this SET. */
210 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
211 of any registers used within the LIBCALL. */
212 int consec; /* Number of consecutive following insns
213 that must be moved with this one. */
214 unsigned int regno; /* The register it sets */
215 short lifetime; /* lifetime of that register;
216 may be adjusted when matching movables
217 that load the same value are found. */
218 short savings; /* Number of insns we can move for this reg,
219 including other movables that force this
220 or match this one. */
221 unsigned int cond : 1; /* 1 if only conditionally movable */
222 unsigned int force : 1; /* 1 means MUST move this insn */
223 unsigned int global : 1; /* 1 means reg is live outside this loop */
224 /* If PARTIAL is 1, GLOBAL means something different:
225 that the reg is live outside the range from where it is set
226 to the following label. */
227 unsigned int done : 1; /* 1 inhibits further processing of this */
229 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
230 In particular, moving it does not make it
231 invariant. */
232 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
233 load SRC, rather than copying INSN. */
234 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
235 first insn of a consecutive sets group. */
236 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
237 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
238 that we should avoid changing when clearing
239 the rest of the reg. */
240 struct movable *match; /* First entry for same value */
241 struct movable *forces; /* An insn that must be moved if this is */
242 struct movable *next;
246 FILE *loop_dump_stream;
248 /* Forward declarations. */
250 static void invalidate_loops_containing_label PARAMS ((rtx));
251 static void find_and_verify_loops PARAMS ((rtx, struct loops *));
252 static void mark_loop_jump PARAMS ((rtx, struct loop *));
253 static void prescan_loop PARAMS ((struct loop *));
254 static int reg_in_basic_block_p PARAMS ((rtx, rtx));
255 static int consec_sets_invariant_p PARAMS ((const struct loop *,
256 rtx, int, rtx));
257 static int labels_in_range_p PARAMS ((rtx, int));
258 static void count_one_set PARAMS ((struct loop_regs *, rtx, rtx, rtx *));
259 static void note_addr_stored PARAMS ((rtx, rtx, void *));
260 static void note_set_pseudo_multiple_uses PARAMS ((rtx, rtx, void *));
261 static int loop_reg_used_before_p PARAMS ((const struct loop *, rtx, rtx));
262 static void scan_loop PARAMS ((struct loop*, int));
263 #if 0
264 static void replace_call_address PARAMS ((rtx, rtx, rtx));
265 #endif
266 static rtx skip_consec_insns PARAMS ((rtx, int));
267 static int libcall_benefit PARAMS ((rtx));
268 static void ignore_some_movables PARAMS ((struct loop_movables *));
269 static void force_movables PARAMS ((struct loop_movables *));
270 static void combine_movables PARAMS ((struct loop_movables *,
271 struct loop_regs *));
272 static int num_unmoved_movables PARAMS ((const struct loop *));
273 static int regs_match_p PARAMS ((rtx, rtx, struct loop_movables *));
274 static int rtx_equal_for_loop_p PARAMS ((rtx, rtx, struct loop_movables *,
275 struct loop_regs *));
276 static void add_label_notes PARAMS ((rtx, rtx));
277 static void move_movables PARAMS ((struct loop *loop, struct loop_movables *,
278 int, int));
279 static void loop_movables_add PARAMS((struct loop_movables *,
280 struct movable *));
281 static void loop_movables_free PARAMS((struct loop_movables *));
282 static int count_nonfixed_reads PARAMS ((const struct loop *, rtx));
283 static void loop_bivs_find PARAMS((struct loop *));
284 static void loop_bivs_init_find PARAMS((struct loop *));
285 static void loop_bivs_check PARAMS((struct loop *));
286 static void loop_givs_find PARAMS((struct loop *));
287 static void loop_givs_check PARAMS((struct loop *));
288 static int loop_biv_eliminable_p PARAMS((struct loop *, struct iv_class *,
289 int, int));
290 static int loop_giv_reduce_benefit PARAMS((struct loop *, struct iv_class *,
291 struct induction *, rtx));
292 static void loop_givs_dead_check PARAMS((struct loop *, struct iv_class *));
293 static void loop_givs_reduce PARAMS((struct loop *, struct iv_class *));
294 static void loop_givs_rescan PARAMS((struct loop *, struct iv_class *,
295 rtx *));
296 static void loop_ivs_free PARAMS((struct loop *));
297 static void strength_reduce PARAMS ((struct loop *, int));
298 static void find_single_use_in_loop PARAMS ((struct loop_regs *, rtx, rtx));
299 static int valid_initial_value_p PARAMS ((rtx, rtx, int, rtx));
300 static void find_mem_givs PARAMS ((const struct loop *, rtx, rtx, int, int));
301 static void record_biv PARAMS ((struct loop *, struct induction *,
302 rtx, rtx, rtx, rtx, rtx *,
303 int, int));
304 static void check_final_value PARAMS ((const struct loop *,
305 struct induction *));
306 static void loop_ivs_dump PARAMS((const struct loop *, FILE *, int));
307 static void loop_iv_class_dump PARAMS((const struct iv_class *, FILE *, int));
308 static void loop_biv_dump PARAMS((const struct induction *, FILE *, int));
309 static void loop_giv_dump PARAMS((const struct induction *, FILE *, int));
310 static void record_giv PARAMS ((const struct loop *, struct induction *,
311 rtx, rtx, rtx, rtx, rtx, rtx, int,
312 enum g_types, int, int, rtx *));
313 static void update_giv_derive PARAMS ((const struct loop *, rtx));
314 static void check_ext_dependent_givs PARAMS ((struct iv_class *,
315 struct loop_info *));
316 static int basic_induction_var PARAMS ((const struct loop *, rtx,
317 enum machine_mode, rtx, rtx,
318 rtx *, rtx *, rtx **));
319 static rtx simplify_giv_expr PARAMS ((const struct loop *, rtx, rtx *, int *));
320 static int general_induction_var PARAMS ((const struct loop *loop, rtx, rtx *,
321 rtx *, rtx *, rtx *, int, int *,
322 enum machine_mode));
323 static int consec_sets_giv PARAMS ((const struct loop *, int, rtx,
324 rtx, rtx, rtx *, rtx *, rtx *, rtx *));
325 static int check_dbra_loop PARAMS ((struct loop *, int));
326 static rtx express_from_1 PARAMS ((rtx, rtx, rtx));
327 static rtx combine_givs_p PARAMS ((struct induction *, struct induction *));
328 static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
329 static void combine_givs PARAMS ((struct loop_regs *, struct iv_class *));
330 static int product_cheap_p PARAMS ((rtx, rtx));
331 static int maybe_eliminate_biv PARAMS ((const struct loop *, struct iv_class *,
332 int, int, int));
333 static int maybe_eliminate_biv_1 PARAMS ((const struct loop *, rtx, rtx,
334 struct iv_class *, int,
335 basic_block, rtx));
336 static int last_use_this_basic_block PARAMS ((rtx, rtx));
337 static void record_initial PARAMS ((rtx, rtx, void *));
338 static void update_reg_last_use PARAMS ((rtx, rtx));
339 static rtx next_insn_in_loop PARAMS ((const struct loop *, rtx));
340 static void loop_regs_scan PARAMS ((const struct loop *, int));
341 static int count_insns_in_loop PARAMS ((const struct loop *));
342 static void load_mems PARAMS ((const struct loop *));
343 static int insert_loop_mem PARAMS ((rtx *, void *));
344 static int replace_loop_mem PARAMS ((rtx *, void *));
345 static void replace_loop_mems PARAMS ((rtx, rtx, rtx));
346 static int replace_loop_reg PARAMS ((rtx *, void *));
347 static void replace_loop_regs PARAMS ((rtx insn, rtx, rtx));
348 static void note_reg_stored PARAMS ((rtx, rtx, void *));
349 static void try_copy_prop PARAMS ((const struct loop *, rtx, unsigned int));
350 static void try_swap_copy_prop PARAMS ((const struct loop *, rtx,
351 unsigned int));
352 static int replace_label PARAMS ((rtx *, void *));
353 static rtx check_insn_for_givs PARAMS((struct loop *, rtx, int, int));
354 static rtx check_insn_for_bivs PARAMS((struct loop *, rtx, int, int));
355 static rtx gen_add_mult PARAMS ((rtx, rtx, rtx, rtx));
356 static void loop_regs_update PARAMS ((const struct loop *, rtx));
357 static int iv_add_mult_cost PARAMS ((rtx, rtx, rtx, rtx));
359 static rtx loop_insn_emit_after PARAMS((const struct loop *, basic_block,
360 rtx, rtx));
361 static rtx loop_call_insn_emit_before PARAMS((const struct loop *,
362 basic_block, rtx, rtx));
363 static rtx loop_call_insn_hoist PARAMS((const struct loop *, rtx));
364 static rtx loop_insn_sink_or_swim PARAMS((const struct loop *, rtx));
366 static void loop_dump_aux PARAMS ((const struct loop *, FILE *, int));
367 static void loop_delete_insns PARAMS ((rtx, rtx));
368 static HOST_WIDE_INT remove_constant_addition PARAMS ((rtx *));
369 static rtx gen_load_of_final_value PARAMS ((rtx, rtx));
370 void debug_ivs PARAMS ((const struct loop *));
371 void debug_iv_class PARAMS ((const struct iv_class *));
372 void debug_biv PARAMS ((const struct induction *));
373 void debug_giv PARAMS ((const struct induction *));
374 void debug_loop PARAMS ((const struct loop *));
375 void debug_loops PARAMS ((const struct loops *));
377 typedef struct rtx_pair
379 rtx r1;
380 rtx r2;
381 } rtx_pair;
383 typedef struct loop_replace_args
385 rtx match;
386 rtx replacement;
387 rtx insn;
388 } loop_replace_args;
390 /* Nonzero iff INSN is between START and END, inclusive. */
391 #define INSN_IN_RANGE_P(INSN, START, END) \
392 (INSN_UID (INSN) < max_uid_for_loop \
393 && INSN_LUID (INSN) >= INSN_LUID (START) \
394 && INSN_LUID (INSN) <= INSN_LUID (END))
396 /* Indirect_jump_in_function is computed once per function. */
397 static int indirect_jump_in_function;
398 static int indirect_jump_in_function_p PARAMS ((rtx));
400 static int compute_luids PARAMS ((rtx, rtx, int));
402 static int biv_elimination_giv_has_0_offset PARAMS ((struct induction *,
403 struct induction *,
404 rtx));
406 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
407 copy the value of the strength reduced giv to its original register. */
408 static int copy_cost;
410 /* Cost of using a register, to normalize the benefits of a giv. */
411 static int reg_address_cost;
413 void
414 init_loop ()
416 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
418 reg_address_cost = address_cost (reg, SImode);
420 copy_cost = COSTS_N_INSNS (1);
423 /* Compute the mapping from uids to luids.
424 LUIDs are numbers assigned to insns, like uids,
425 except that luids increase monotonically through the code.
426 Start at insn START and stop just before END. Assign LUIDs
427 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
428 static int
429 compute_luids (start, end, prev_luid)
430 rtx start, end;
431 int prev_luid;
433 int i;
434 rtx insn;
436 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
438 if (INSN_UID (insn) >= max_uid_for_loop)
439 continue;
440 /* Don't assign luids to line-number NOTEs, so that the distance in
441 luids between two insns is not affected by -g. */
442 if (GET_CODE (insn) != NOTE
443 || NOTE_LINE_NUMBER (insn) <= 0)
444 uid_luid[INSN_UID (insn)] = ++i;
445 else
446 /* Give a line number note the same luid as preceding insn. */
447 uid_luid[INSN_UID (insn)] = i;
449 return i + 1;
452 /* Entry point of this file. Perform loop optimization
453 on the current function. F is the first insn of the function
454 and DUMPFILE is a stream for output of a trace of actions taken
455 (or 0 if none should be output). */
457 void
458 loop_optimize (f, dumpfile, flags)
459 /* f is the first instruction of a chain of insns for one function */
460 rtx f;
461 FILE *dumpfile;
462 int flags;
464 rtx insn;
465 int i;
466 struct loops loops_data;
467 struct loops *loops = &loops_data;
468 struct loop_info *loops_info;
470 loop_dump_stream = dumpfile;
472 init_recog_no_volatile ();
474 max_reg_before_loop = max_reg_num ();
475 loop_max_reg = max_reg_before_loop;
477 regs_may_share = 0;
479 /* Count the number of loops. */
481 max_loop_num = 0;
482 for (insn = f; insn; insn = NEXT_INSN (insn))
484 if (GET_CODE (insn) == NOTE
485 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
486 max_loop_num++;
489 /* Don't waste time if no loops. */
490 if (max_loop_num == 0)
491 return;
493 loops->num = max_loop_num;
495 /* Get size to use for tables indexed by uids.
496 Leave some space for labels allocated by find_and_verify_loops. */
497 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
499 uid_luid = (int *) xcalloc (max_uid_for_loop, sizeof (int));
500 uid_loop = (struct loop **) xcalloc (max_uid_for_loop,
501 sizeof (struct loop *));
503 /* Allocate storage for array of loops. */
504 loops->array = (struct loop *)
505 xcalloc (loops->num, sizeof (struct loop));
507 /* Find and process each loop.
508 First, find them, and record them in order of their beginnings. */
509 find_and_verify_loops (f, loops);
511 /* Allocate and initialize auxiliary loop information. */
512 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
513 for (i = 0; i < loops->num; i++)
514 loops->array[i].aux = loops_info + i;
516 /* Now find all register lifetimes. This must be done after
517 find_and_verify_loops, because it might reorder the insns in the
518 function. */
519 reg_scan (f, max_reg_before_loop, 1);
521 /* This must occur after reg_scan so that registers created by gcse
522 will have entries in the register tables.
524 We could have added a call to reg_scan after gcse_main in toplev.c,
525 but moving this call to init_alias_analysis is more efficient. */
526 init_alias_analysis ();
528 /* See if we went too far. Note that get_max_uid already returns
529 one more that the maximum uid of all insn. */
530 if (get_max_uid () > max_uid_for_loop)
531 abort ();
532 /* Now reset it to the actual size we need. See above. */
533 max_uid_for_loop = get_max_uid ();
535 /* find_and_verify_loops has already called compute_luids, but it
536 might have rearranged code afterwards, so we need to recompute
537 the luids now. */
538 max_luid = compute_luids (f, NULL_RTX, 0);
540 /* Don't leave gaps in uid_luid for insns that have been
541 deleted. It is possible that the first or last insn
542 using some register has been deleted by cross-jumping.
543 Make sure that uid_luid for that former insn's uid
544 points to the general area where that insn used to be. */
545 for (i = 0; i < max_uid_for_loop; i++)
547 uid_luid[0] = uid_luid[i];
548 if (uid_luid[0] != 0)
549 break;
551 for (i = 0; i < max_uid_for_loop; i++)
552 if (uid_luid[i] == 0)
553 uid_luid[i] = uid_luid[i - 1];
555 /* Determine if the function has indirect jump. On some systems
556 this prevents low overhead loop instructions from being used. */
557 indirect_jump_in_function = indirect_jump_in_function_p (f);
559 /* Now scan the loops, last ones first, since this means inner ones are done
560 before outer ones. */
561 for (i = max_loop_num - 1; i >= 0; i--)
563 struct loop *loop = &loops->array[i];
565 if (! loop->invalid && loop->end)
566 scan_loop (loop, flags);
569 /* If there were lexical blocks inside the loop, they have been
570 replicated. We will now have more than one NOTE_INSN_BLOCK_BEG
571 and NOTE_INSN_BLOCK_END for each such block. We must duplicate
572 the BLOCKs as well. */
573 if (write_symbols != NO_DEBUG)
574 reorder_blocks ();
576 end_alias_analysis ();
578 /* Clean up. */
579 free (uid_luid);
580 free (uid_loop);
581 free (loops_info);
582 free (loops->array);
585 /* Returns the next insn, in execution order, after INSN. START and
586 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
587 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
588 insn-stream; it is used with loops that are entered near the
589 bottom. */
591 static rtx
592 next_insn_in_loop (loop, insn)
593 const struct loop *loop;
594 rtx insn;
596 insn = NEXT_INSN (insn);
598 if (insn == loop->end)
600 if (loop->top)
601 /* Go to the top of the loop, and continue there. */
602 insn = loop->top;
603 else
604 /* We're done. */
605 insn = NULL_RTX;
608 if (insn == loop->scan_start)
609 /* We're done. */
610 insn = NULL_RTX;
612 return insn;
615 /* Optimize one loop described by LOOP. */
617 /* ??? Could also move memory writes out of loops if the destination address
618 is invariant, the source is invariant, the memory write is not volatile,
619 and if we can prove that no read inside the loop can read this address
620 before the write occurs. If there is a read of this address after the
621 write, then we can also mark the memory read as invariant. */
623 static void
624 scan_loop (loop, flags)
625 struct loop *loop;
626 int flags;
628 struct loop_info *loop_info = LOOP_INFO (loop);
629 struct loop_regs *regs = LOOP_REGS (loop);
630 int i;
631 rtx loop_start = loop->start;
632 rtx loop_end = loop->end;
633 rtx p;
634 /* 1 if we are scanning insns that could be executed zero times. */
635 int maybe_never = 0;
636 /* 1 if we are scanning insns that might never be executed
637 due to a subroutine call which might exit before they are reached. */
638 int call_passed = 0;
639 /* Jump insn that enters the loop, or 0 if control drops in. */
640 rtx loop_entry_jump = 0;
641 /* Number of insns in the loop. */
642 int insn_count;
643 int tem;
644 rtx temp, update_start, update_end;
645 /* The SET from an insn, if it is the only SET in the insn. */
646 rtx set, set1;
647 /* Chain describing insns movable in current loop. */
648 struct loop_movables *movables = LOOP_MOVABLES (loop);
649 /* Ratio of extra register life span we can justify
650 for saving an instruction. More if loop doesn't call subroutines
651 since in that case saving an insn makes more difference
652 and more registers are available. */
653 int threshold;
654 /* Nonzero if we are scanning instructions in a sub-loop. */
655 int loop_depth = 0;
657 loop->top = 0;
659 movables->head = 0;
660 movables->last = 0;
662 /* Determine whether this loop starts with a jump down to a test at
663 the end. This will occur for a small number of loops with a test
664 that is too complex to duplicate in front of the loop.
666 We search for the first insn or label in the loop, skipping NOTEs.
667 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
668 (because we might have a loop executed only once that contains a
669 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
670 (in case we have a degenerate loop).
672 Note that if we mistakenly think that a loop is entered at the top
673 when, in fact, it is entered at the exit test, the only effect will be
674 slightly poorer optimization. Making the opposite error can generate
675 incorrect code. Since very few loops now start with a jump to the
676 exit test, the code here to detect that case is very conservative. */
678 for (p = NEXT_INSN (loop_start);
679 p != loop_end
680 && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
681 && (GET_CODE (p) != NOTE
682 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
683 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
684 p = NEXT_INSN (p))
687 loop->scan_start = p;
689 /* If loop end is the end of the current function, then emit a
690 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
691 note insn. This is the position we use when sinking insns out of
692 the loop. */
693 if (NEXT_INSN (loop->end) != 0)
694 loop->sink = NEXT_INSN (loop->end);
695 else
696 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
698 /* Set up variables describing this loop. */
699 prescan_loop (loop);
700 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
702 /* If loop has a jump before the first label,
703 the true entry is the target of that jump.
704 Start scan from there.
705 But record in LOOP->TOP the place where the end-test jumps
706 back to so we can scan that after the end of the loop. */
707 if (GET_CODE (p) == JUMP_INSN)
709 loop_entry_jump = p;
711 /* Loop entry must be unconditional jump (and not a RETURN) */
712 if (any_uncondjump_p (p)
713 && JUMP_LABEL (p) != 0
714 /* Check to see whether the jump actually
715 jumps out of the loop (meaning it's no loop).
716 This case can happen for things like
717 do {..} while (0). If this label was generated previously
718 by loop, we can't tell anything about it and have to reject
719 the loop. */
720 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
722 loop->top = next_label (loop->scan_start);
723 loop->scan_start = JUMP_LABEL (p);
727 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
728 as required by loop_reg_used_before_p. So skip such loops. (This
729 test may never be true, but it's best to play it safe.)
731 Also, skip loops where we do not start scanning at a label. This
732 test also rejects loops starting with a JUMP_INSN that failed the
733 test above. */
735 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
736 || GET_CODE (loop->scan_start) != CODE_LABEL)
738 if (loop_dump_stream)
739 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
740 INSN_UID (loop_start), INSN_UID (loop_end));
741 return;
744 /* Allocate extra space for REGs that might be created by load_mems.
745 We allocate a little extra slop as well, in the hopes that we
746 won't have to reallocate the regs array. */
747 loop_regs_scan (loop, loop_info->mems_idx + 16);
748 insn_count = count_insns_in_loop (loop);
750 if (loop_dump_stream)
752 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
753 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
754 if (loop->cont)
755 fprintf (loop_dump_stream, "Continue at insn %d.\n",
756 INSN_UID (loop->cont));
759 /* Scan through the loop finding insns that are safe to move.
760 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
761 this reg will be considered invariant for subsequent insns.
762 We consider whether subsequent insns use the reg
763 in deciding whether it is worth actually moving.
765 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
766 and therefore it is possible that the insns we are scanning
767 would never be executed. At such times, we must make sure
768 that it is safe to execute the insn once instead of zero times.
769 When MAYBE_NEVER is 0, all insns will be executed at least once
770 so that is not a problem. */
772 for (p = next_insn_in_loop (loop, loop->scan_start);
773 p != NULL_RTX;
774 p = next_insn_in_loop (loop, p))
776 if (GET_CODE (p) == INSN
777 && (set = single_set (p))
778 && GET_CODE (SET_DEST (set)) == REG
779 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
780 && SET_DEST (set) != pic_offset_table_rtx
781 #endif
782 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
784 int tem1 = 0;
785 int tem2 = 0;
786 int move_insn = 0;
787 rtx src = SET_SRC (set);
788 rtx dependencies = 0;
790 /* Figure out what to use as a source of this insn. If a REG_EQUIV
791 note is given or if a REG_EQUAL note with a constant operand is
792 specified, use it as the source and mark that we should move
793 this insn by calling emit_move_insn rather that duplicating the
794 insn.
796 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
797 is present. */
798 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
799 if (temp)
800 src = XEXP (temp, 0), move_insn = 1;
801 else
803 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
804 if (temp && CONSTANT_P (XEXP (temp, 0)))
805 src = XEXP (temp, 0), move_insn = 1;
806 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
808 src = XEXP (temp, 0);
809 /* A libcall block can use regs that don't appear in
810 the equivalent expression. To move the libcall,
811 we must move those regs too. */
812 dependencies = libcall_other_reg (p, src);
816 /* For parallels, add any possible uses to the depencies, as we can't move
817 the insn without resolving them first. */
818 if (GET_CODE (PATTERN (p)) == PARALLEL)
820 for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
822 rtx x = XVECEXP (PATTERN (p), 0, i);
823 if (GET_CODE (x) == USE)
824 dependencies = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0), dependencies);
828 /* Don't try to optimize a register that was made
829 by loop-optimization for an inner loop.
830 We don't know its life-span, so we can't compute the benefit. */
831 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
833 else if (/* The register is used in basic blocks other
834 than the one where it is set (meaning that
835 something after this point in the loop might
836 depend on its value before the set). */
837 ! reg_in_basic_block_p (p, SET_DEST (set))
838 /* And the set is not guaranteed to be executed once
839 the loop starts, or the value before the set is
840 needed before the set occurs...
842 ??? Note we have quadratic behaviour here, mitigated
843 by the fact that the previous test will often fail for
844 large loops. Rather than re-scanning the entire loop
845 each time for register usage, we should build tables
846 of the register usage and use them here instead. */
847 && (maybe_never
848 || loop_reg_used_before_p (loop, set, p)))
849 /* It is unsafe to move the set.
851 This code used to consider it OK to move a set of a variable
852 which was not created by the user and not used in an exit test.
853 That behavior is incorrect and was removed. */
855 else if ((tem = loop_invariant_p (loop, src))
856 && (dependencies == 0
857 || (tem2 = loop_invariant_p (loop, dependencies)) != 0)
858 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
859 || (tem1
860 = consec_sets_invariant_p
861 (loop, SET_DEST (set),
862 regs->array[REGNO (SET_DEST (set))].set_in_loop,
863 p)))
864 /* If the insn can cause a trap (such as divide by zero),
865 can't move it unless it's guaranteed to be executed
866 once loop is entered. Even a function call might
867 prevent the trap insn from being reached
868 (since it might exit!) */
869 && ! ((maybe_never || call_passed)
870 && may_trap_p (src)))
872 struct movable *m;
873 int regno = REGNO (SET_DEST (set));
875 /* A potential lossage is where we have a case where two insns
876 can be combined as long as they are both in the loop, but
877 we move one of them outside the loop. For large loops,
878 this can lose. The most common case of this is the address
879 of a function being called.
881 Therefore, if this register is marked as being used exactly
882 once if we are in a loop with calls (a "large loop"), see if
883 we can replace the usage of this register with the source
884 of this SET. If we can, delete this insn.
886 Don't do this if P has a REG_RETVAL note or if we have
887 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
889 if (loop_info->has_call
890 && regs->array[regno].single_usage != 0
891 && regs->array[regno].single_usage != const0_rtx
892 && REGNO_FIRST_UID (regno) == INSN_UID (p)
893 && (REGNO_LAST_UID (regno)
894 == INSN_UID (regs->array[regno].single_usage))
895 && regs->array[regno].set_in_loop == 1
896 && GET_CODE (SET_SRC (set)) != ASM_OPERANDS
897 && ! side_effects_p (SET_SRC (set))
898 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
899 && (! SMALL_REGISTER_CLASSES
900 || (! (GET_CODE (SET_SRC (set)) == REG
901 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
902 /* This test is not redundant; SET_SRC (set) might be
903 a call-clobbered register and the life of REGNO
904 might span a call. */
905 && ! modified_between_p (SET_SRC (set), p,
906 regs->array[regno].single_usage)
907 && no_labels_between_p (p, regs->array[regno].single_usage)
908 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
909 regs->array[regno].single_usage))
911 /* Replace any usage in a REG_EQUAL note. Must copy the
912 new source, so that we don't get rtx sharing between the
913 SET_SOURCE and REG_NOTES of insn p. */
914 REG_NOTES (regs->array[regno].single_usage)
915 = replace_rtx (REG_NOTES (regs->array[regno].single_usage),
916 SET_DEST (set), copy_rtx (SET_SRC (set)));
918 delete_insn (p);
919 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
920 regs->array[regno+i].set_in_loop = 0;
921 continue;
924 m = (struct movable *) xmalloc (sizeof (struct movable));
925 m->next = 0;
926 m->insn = p;
927 m->set_src = src;
928 m->dependencies = dependencies;
929 m->set_dest = SET_DEST (set);
930 m->force = 0;
931 m->consec = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
932 m->done = 0;
933 m->forces = 0;
934 m->partial = 0;
935 m->move_insn = move_insn;
936 m->move_insn_first = 0;
937 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
938 m->savemode = VOIDmode;
939 m->regno = regno;
940 /* Set M->cond if either loop_invariant_p
941 or consec_sets_invariant_p returned 2
942 (only conditionally invariant). */
943 m->cond = ((tem | tem1 | tem2) > 1);
944 m->global = LOOP_REG_GLOBAL_P (loop, regno);
945 m->match = 0;
946 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
947 m->savings = regs->array[regno].n_times_set;
948 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
949 m->savings += libcall_benefit (p);
950 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
951 regs->array[regno+i].set_in_loop = move_insn ? -2 : -1;
952 /* Add M to the end of the chain MOVABLES. */
953 loop_movables_add (movables, m);
955 if (m->consec > 0)
957 /* It is possible for the first instruction to have a
958 REG_EQUAL note but a non-invariant SET_SRC, so we must
959 remember the status of the first instruction in case
960 the last instruction doesn't have a REG_EQUAL note. */
961 m->move_insn_first = m->move_insn;
963 /* Skip this insn, not checking REG_LIBCALL notes. */
964 p = next_nonnote_insn (p);
965 /* Skip the consecutive insns, if there are any. */
966 p = skip_consec_insns (p, m->consec);
967 /* Back up to the last insn of the consecutive group. */
968 p = prev_nonnote_insn (p);
970 /* We must now reset m->move_insn, m->is_equiv, and possibly
971 m->set_src to correspond to the effects of all the
972 insns. */
973 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
974 if (temp)
975 m->set_src = XEXP (temp, 0), m->move_insn = 1;
976 else
978 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
979 if (temp && CONSTANT_P (XEXP (temp, 0)))
980 m->set_src = XEXP (temp, 0), m->move_insn = 1;
981 else
982 m->move_insn = 0;
985 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
988 /* If this register is always set within a STRICT_LOW_PART
989 or set to zero, then its high bytes are constant.
990 So clear them outside the loop and within the loop
991 just load the low bytes.
992 We must check that the machine has an instruction to do so.
993 Also, if the value loaded into the register
994 depends on the same register, this cannot be done. */
995 else if (SET_SRC (set) == const0_rtx
996 && GET_CODE (NEXT_INSN (p)) == INSN
997 && (set1 = single_set (NEXT_INSN (p)))
998 && GET_CODE (set1) == SET
999 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
1000 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
1001 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
1002 == SET_DEST (set))
1003 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
1005 int regno = REGNO (SET_DEST (set));
1006 if (regs->array[regno].set_in_loop == 2)
1008 struct movable *m;
1009 m = (struct movable *) xmalloc (sizeof (struct movable));
1010 m->next = 0;
1011 m->insn = p;
1012 m->set_dest = SET_DEST (set);
1013 m->dependencies = 0;
1014 m->force = 0;
1015 m->consec = 0;
1016 m->done = 0;
1017 m->forces = 0;
1018 m->move_insn = 0;
1019 m->move_insn_first = 0;
1020 m->partial = 1;
1021 /* If the insn may not be executed on some cycles,
1022 we can't clear the whole reg; clear just high part.
1023 Not even if the reg is used only within this loop.
1024 Consider this:
1025 while (1)
1026 while (s != t) {
1027 if (foo ()) x = *s;
1028 use (x);
1030 Clearing x before the inner loop could clobber a value
1031 being saved from the last time around the outer loop.
1032 However, if the reg is not used outside this loop
1033 and all uses of the register are in the same
1034 basic block as the store, there is no problem.
1036 If this insn was made by loop, we don't know its
1037 INSN_LUID and hence must make a conservative
1038 assumption. */
1039 m->global = (INSN_UID (p) >= max_uid_for_loop
1040 || LOOP_REG_GLOBAL_P (loop, regno)
1041 || (labels_in_range_p
1042 (p, REGNO_FIRST_LUID (regno))));
1043 if (maybe_never && m->global)
1044 m->savemode = GET_MODE (SET_SRC (set1));
1045 else
1046 m->savemode = VOIDmode;
1047 m->regno = regno;
1048 m->cond = 0;
1049 m->match = 0;
1050 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
1051 m->savings = 1;
1052 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
1053 regs->array[regno+i].set_in_loop = -1;
1054 /* Add M to the end of the chain MOVABLES. */
1055 loop_movables_add (movables, m);
1059 /* Past a call insn, we get to insns which might not be executed
1060 because the call might exit. This matters for insns that trap.
1061 Constant and pure call insns always return, so they don't count. */
1062 else if (GET_CODE (p) == CALL_INSN && ! CONST_OR_PURE_CALL_P (p))
1063 call_passed = 1;
1064 /* Past a label or a jump, we get to insns for which we
1065 can't count on whether or how many times they will be
1066 executed during each iteration. Therefore, we can
1067 only move out sets of trivial variables
1068 (those not used after the loop). */
1069 /* Similar code appears twice in strength_reduce. */
1070 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1071 /* If we enter the loop in the middle, and scan around to the
1072 beginning, don't set maybe_never for that. This must be an
1073 unconditional jump, otherwise the code at the top of the
1074 loop might never be executed. Unconditional jumps are
1075 followed by a barrier then the loop_end. */
1076 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
1077 && NEXT_INSN (NEXT_INSN (p)) == loop_end
1078 && any_uncondjump_p (p)))
1079 maybe_never = 1;
1080 else if (GET_CODE (p) == NOTE)
1082 /* At the virtual top of a converted loop, insns are again known to
1083 be executed: logically, the loop begins here even though the exit
1084 code has been duplicated. */
1085 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1086 maybe_never = call_passed = 0;
1087 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1088 loop_depth++;
1089 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1090 loop_depth--;
1094 /* If one movable subsumes another, ignore that other. */
1096 ignore_some_movables (movables);
1098 /* For each movable insn, see if the reg that it loads
1099 leads when it dies right into another conditionally movable insn.
1100 If so, record that the second insn "forces" the first one,
1101 since the second can be moved only if the first is. */
1103 force_movables (movables);
1105 /* See if there are multiple movable insns that load the same value.
1106 If there are, make all but the first point at the first one
1107 through the `match' field, and add the priorities of them
1108 all together as the priority of the first. */
1110 combine_movables (movables, regs);
1112 /* Now consider each movable insn to decide whether it is worth moving.
1113 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1115 Generally this increases code size, so do not move moveables when
1116 optimizing for code size. */
1118 if (! optimize_size)
1120 move_movables (loop, movables, threshold, insn_count);
1122 /* Recalculate regs->array if move_movables has created new
1123 registers. */
1124 if (max_reg_num () > regs->num)
1126 loop_regs_scan (loop, 0);
1127 for (update_start = loop_start;
1128 PREV_INSN (update_start)
1129 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1130 update_start = PREV_INSN (update_start))
1132 update_end = NEXT_INSN (loop_end);
1134 reg_scan_update (update_start, update_end, loop_max_reg);
1135 loop_max_reg = max_reg_num ();
1139 /* Now candidates that still are negative are those not moved.
1140 Change regs->array[I].set_in_loop to indicate that those are not actually
1141 invariant. */
1142 for (i = 0; i < regs->num; i++)
1143 if (regs->array[i].set_in_loop < 0)
1144 regs->array[i].set_in_loop = regs->array[i].n_times_set;
1146 /* Now that we've moved some things out of the loop, we might be able to
1147 hoist even more memory references. */
1148 load_mems (loop);
1150 /* Recalculate regs->array if load_mems has created new registers. */
1151 if (max_reg_num () > regs->num)
1152 loop_regs_scan (loop, 0);
1154 for (update_start = loop_start;
1155 PREV_INSN (update_start)
1156 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1157 update_start = PREV_INSN (update_start))
1159 update_end = NEXT_INSN (loop_end);
1161 reg_scan_update (update_start, update_end, loop_max_reg);
1162 loop_max_reg = max_reg_num ();
1164 if (flag_strength_reduce)
1166 if (update_end && GET_CODE (update_end) == CODE_LABEL)
1167 /* Ensure our label doesn't go away. */
1168 LABEL_NUSES (update_end)++;
1170 strength_reduce (loop, flags);
1172 reg_scan_update (update_start, update_end, loop_max_reg);
1173 loop_max_reg = max_reg_num ();
1175 if (update_end && GET_CODE (update_end) == CODE_LABEL
1176 && --LABEL_NUSES (update_end) == 0)
1177 delete_related_insns (update_end);
1181 /* The movable information is required for strength reduction. */
1182 loop_movables_free (movables);
1184 free (regs->array);
1185 regs->array = 0;
1186 regs->num = 0;
1189 /* Add elements to *OUTPUT to record all the pseudo-regs
1190 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1192 void
1193 record_excess_regs (in_this, not_in_this, output)
1194 rtx in_this, not_in_this;
1195 rtx *output;
1197 enum rtx_code code;
1198 const char *fmt;
1199 int i;
1201 code = GET_CODE (in_this);
1203 switch (code)
1205 case PC:
1206 case CC0:
1207 case CONST_INT:
1208 case CONST_DOUBLE:
1209 case CONST:
1210 case SYMBOL_REF:
1211 case LABEL_REF:
1212 return;
1214 case REG:
1215 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1216 && ! reg_mentioned_p (in_this, not_in_this))
1217 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1218 return;
1220 default:
1221 break;
1224 fmt = GET_RTX_FORMAT (code);
1225 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1227 int j;
1229 switch (fmt[i])
1231 case 'E':
1232 for (j = 0; j < XVECLEN (in_this, i); j++)
1233 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1234 break;
1236 case 'e':
1237 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1238 break;
1243 /* Check what regs are referred to in the libcall block ending with INSN,
1244 aside from those mentioned in the equivalent value.
1245 If there are none, return 0.
1246 If there are one or more, return an EXPR_LIST containing all of them. */
1249 libcall_other_reg (insn, equiv)
1250 rtx insn, equiv;
1252 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1253 rtx p = XEXP (note, 0);
1254 rtx output = 0;
1256 /* First, find all the regs used in the libcall block
1257 that are not mentioned as inputs to the result. */
1259 while (p != insn)
1261 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1262 || GET_CODE (p) == CALL_INSN)
1263 record_excess_regs (PATTERN (p), equiv, &output);
1264 p = NEXT_INSN (p);
1267 return output;
1270 /* Return 1 if all uses of REG
1271 are between INSN and the end of the basic block. */
1273 static int
1274 reg_in_basic_block_p (insn, reg)
1275 rtx insn, reg;
1277 int regno = REGNO (reg);
1278 rtx p;
1280 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1281 return 0;
1283 /* Search this basic block for the already recorded last use of the reg. */
1284 for (p = insn; p; p = NEXT_INSN (p))
1286 switch (GET_CODE (p))
1288 case NOTE:
1289 break;
1291 case INSN:
1292 case CALL_INSN:
1293 /* Ordinary insn: if this is the last use, we win. */
1294 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1295 return 1;
1296 break;
1298 case JUMP_INSN:
1299 /* Jump insn: if this is the last use, we win. */
1300 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1301 return 1;
1302 /* Otherwise, it's the end of the basic block, so we lose. */
1303 return 0;
1305 case CODE_LABEL:
1306 case BARRIER:
1307 /* It's the end of the basic block, so we lose. */
1308 return 0;
1310 default:
1311 break;
1315 /* The "last use" that was recorded can't be found after the first
1316 use. This can happen when the last use was deleted while
1317 processing an inner loop, this inner loop was then completely
1318 unrolled, and the outer loop is always exited after the inner loop,
1319 so that everything after the first use becomes a single basic block. */
1320 return 1;
1323 /* Compute the benefit of eliminating the insns in the block whose
1324 last insn is LAST. This may be a group of insns used to compute a
1325 value directly or can contain a library call. */
1327 static int
1328 libcall_benefit (last)
1329 rtx last;
1331 rtx insn;
1332 int benefit = 0;
1334 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1335 insn != last; insn = NEXT_INSN (insn))
1337 if (GET_CODE (insn) == CALL_INSN)
1338 benefit += 10; /* Assume at least this many insns in a library
1339 routine. */
1340 else if (GET_CODE (insn) == INSN
1341 && GET_CODE (PATTERN (insn)) != USE
1342 && GET_CODE (PATTERN (insn)) != CLOBBER)
1343 benefit++;
1346 return benefit;
1349 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1351 static rtx
1352 skip_consec_insns (insn, count)
1353 rtx insn;
1354 int count;
1356 for (; count > 0; count--)
1358 rtx temp;
1360 /* If first insn of libcall sequence, skip to end. */
1361 /* Do this at start of loop, since INSN is guaranteed to
1362 be an insn here. */
1363 if (GET_CODE (insn) != NOTE
1364 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1365 insn = XEXP (temp, 0);
1368 insn = NEXT_INSN (insn);
1369 while (GET_CODE (insn) == NOTE);
1372 return insn;
1375 /* Ignore any movable whose insn falls within a libcall
1376 which is part of another movable.
1377 We make use of the fact that the movable for the libcall value
1378 was made later and so appears later on the chain. */
1380 static void
1381 ignore_some_movables (movables)
1382 struct loop_movables *movables;
1384 struct movable *m, *m1;
1386 for (m = movables->head; m; m = m->next)
1388 /* Is this a movable for the value of a libcall? */
1389 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1390 if (note)
1392 rtx insn;
1393 /* Check for earlier movables inside that range,
1394 and mark them invalid. We cannot use LUIDs here because
1395 insns created by loop.c for prior loops don't have LUIDs.
1396 Rather than reject all such insns from movables, we just
1397 explicitly check each insn in the libcall (since invariant
1398 libcalls aren't that common). */
1399 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1400 for (m1 = movables->head; m1 != m; m1 = m1->next)
1401 if (m1->insn == insn)
1402 m1->done = 1;
1407 /* For each movable insn, see if the reg that it loads
1408 leads when it dies right into another conditionally movable insn.
1409 If so, record that the second insn "forces" the first one,
1410 since the second can be moved only if the first is. */
1412 static void
1413 force_movables (movables)
1414 struct loop_movables *movables;
1416 struct movable *m, *m1;
1418 for (m1 = movables->head; m1; m1 = m1->next)
1419 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1420 if (!m1->partial && !m1->done)
1422 int regno = m1->regno;
1423 for (m = m1->next; m; m = m->next)
1424 /* ??? Could this be a bug? What if CSE caused the
1425 register of M1 to be used after this insn?
1426 Since CSE does not update regno_last_uid,
1427 this insn M->insn might not be where it dies.
1428 But very likely this doesn't matter; what matters is
1429 that M's reg is computed from M1's reg. */
1430 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1431 && !m->done)
1432 break;
1433 if (m != 0 && m->set_src == m1->set_dest
1434 /* If m->consec, m->set_src isn't valid. */
1435 && m->consec == 0)
1436 m = 0;
1438 /* Increase the priority of the moving the first insn
1439 since it permits the second to be moved as well. */
1440 if (m != 0)
1442 m->forces = m1;
1443 m1->lifetime += m->lifetime;
1444 m1->savings += m->savings;
1449 /* Find invariant expressions that are equal and can be combined into
1450 one register. */
1452 static void
1453 combine_movables (movables, regs)
1454 struct loop_movables *movables;
1455 struct loop_regs *regs;
1457 struct movable *m;
1458 char *matched_regs = (char *) xmalloc (regs->num);
1459 enum machine_mode mode;
1461 /* Regs that are set more than once are not allowed to match
1462 or be matched. I'm no longer sure why not. */
1463 /* Only pseudo registers are allowed to match or be matched,
1464 since move_movables does not validate the change. */
1465 /* Perhaps testing m->consec_sets would be more appropriate here? */
1467 for (m = movables->head; m; m = m->next)
1468 if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1469 && m->regno >= FIRST_PSEUDO_REGISTER
1470 && !m->partial)
1472 struct movable *m1;
1473 int regno = m->regno;
1475 memset (matched_regs, 0, regs->num);
1476 matched_regs[regno] = 1;
1478 /* We want later insns to match the first one. Don't make the first
1479 one match any later ones. So start this loop at m->next. */
1480 for (m1 = m->next; m1; m1 = m1->next)
1481 if (m != m1 && m1->match == 0
1482 && regs->array[m1->regno].n_times_set == 1
1483 && m1->regno >= FIRST_PSEUDO_REGISTER
1484 /* A reg used outside the loop mustn't be eliminated. */
1485 && !m1->global
1486 /* A reg used for zero-extending mustn't be eliminated. */
1487 && !m1->partial
1488 && (matched_regs[m1->regno]
1491 /* Can combine regs with different modes loaded from the
1492 same constant only if the modes are the same or
1493 if both are integer modes with M wider or the same
1494 width as M1. The check for integer is redundant, but
1495 safe, since the only case of differing destination
1496 modes with equal sources is when both sources are
1497 VOIDmode, i.e., CONST_INT. */
1498 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1499 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1500 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1501 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1502 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1503 /* See if the source of M1 says it matches M. */
1504 && ((GET_CODE (m1->set_src) == REG
1505 && matched_regs[REGNO (m1->set_src)])
1506 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1507 movables, regs))))
1508 && ((m->dependencies == m1->dependencies)
1509 || rtx_equal_p (m->dependencies, m1->dependencies)))
1511 m->lifetime += m1->lifetime;
1512 m->savings += m1->savings;
1513 m1->done = 1;
1514 m1->match = m;
1515 matched_regs[m1->regno] = 1;
1519 /* Now combine the regs used for zero-extension.
1520 This can be done for those not marked `global'
1521 provided their lives don't overlap. */
1523 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1524 mode = GET_MODE_WIDER_MODE (mode))
1526 struct movable *m0 = 0;
1528 /* Combine all the registers for extension from mode MODE.
1529 Don't combine any that are used outside this loop. */
1530 for (m = movables->head; m; m = m->next)
1531 if (m->partial && ! m->global
1532 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1534 struct movable *m1;
1536 int first = REGNO_FIRST_LUID (m->regno);
1537 int last = REGNO_LAST_LUID (m->regno);
1539 if (m0 == 0)
1541 /* First one: don't check for overlap, just record it. */
1542 m0 = m;
1543 continue;
1546 /* Make sure they extend to the same mode.
1547 (Almost always true.) */
1548 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1549 continue;
1551 /* We already have one: check for overlap with those
1552 already combined together. */
1553 for (m1 = movables->head; m1 != m; m1 = m1->next)
1554 if (m1 == m0 || (m1->partial && m1->match == m0))
1555 if (! (REGNO_FIRST_LUID (m1->regno) > last
1556 || REGNO_LAST_LUID (m1->regno) < first))
1557 goto overlap;
1559 /* No overlap: we can combine this with the others. */
1560 m0->lifetime += m->lifetime;
1561 m0->savings += m->savings;
1562 m->done = 1;
1563 m->match = m0;
1565 overlap:
1570 /* Clean up. */
1571 free (matched_regs);
1574 /* Returns the number of movable instructions in LOOP that were not
1575 moved outside the loop. */
1577 static int
1578 num_unmoved_movables (loop)
1579 const struct loop *loop;
1581 int num = 0;
1582 struct movable *m;
1584 for (m = LOOP_MOVABLES (loop)->head; m; m = m->next)
1585 if (!m->done)
1586 ++num;
1588 return num;
1592 /* Return 1 if regs X and Y will become the same if moved. */
1594 static int
1595 regs_match_p (x, y, movables)
1596 rtx x, y;
1597 struct loop_movables *movables;
1599 unsigned int xn = REGNO (x);
1600 unsigned int yn = REGNO (y);
1601 struct movable *mx, *my;
1603 for (mx = movables->head; mx; mx = mx->next)
1604 if (mx->regno == xn)
1605 break;
1607 for (my = movables->head; my; my = my->next)
1608 if (my->regno == yn)
1609 break;
1611 return (mx && my
1612 && ((mx->match == my->match && mx->match != 0)
1613 || mx->match == my
1614 || mx == my->match));
1617 /* Return 1 if X and Y are identical-looking rtx's.
1618 This is the Lisp function EQUAL for rtx arguments.
1620 If two registers are matching movables or a movable register and an
1621 equivalent constant, consider them equal. */
1623 static int
1624 rtx_equal_for_loop_p (x, y, movables, regs)
1625 rtx x, y;
1626 struct loop_movables *movables;
1627 struct loop_regs *regs;
1629 int i;
1630 int j;
1631 struct movable *m;
1632 enum rtx_code code;
1633 const char *fmt;
1635 if (x == y)
1636 return 1;
1637 if (x == 0 || y == 0)
1638 return 0;
1640 code = GET_CODE (x);
1642 /* If we have a register and a constant, they may sometimes be
1643 equal. */
1644 if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
1645 && CONSTANT_P (y))
1647 for (m = movables->head; m; m = m->next)
1648 if (m->move_insn && m->regno == REGNO (x)
1649 && rtx_equal_p (m->set_src, y))
1650 return 1;
1652 else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
1653 && CONSTANT_P (x))
1655 for (m = movables->head; m; m = m->next)
1656 if (m->move_insn && m->regno == REGNO (y)
1657 && rtx_equal_p (m->set_src, x))
1658 return 1;
1661 /* Otherwise, rtx's of different codes cannot be equal. */
1662 if (code != GET_CODE (y))
1663 return 0;
1665 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1666 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1668 if (GET_MODE (x) != GET_MODE (y))
1669 return 0;
1671 /* These three types of rtx's can be compared nonrecursively. */
1672 if (code == REG)
1673 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1675 if (code == LABEL_REF)
1676 return XEXP (x, 0) == XEXP (y, 0);
1677 if (code == SYMBOL_REF)
1678 return XSTR (x, 0) == XSTR (y, 0);
1680 /* Compare the elements. If any pair of corresponding elements
1681 fail to match, return 0 for the whole things. */
1683 fmt = GET_RTX_FORMAT (code);
1684 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1686 switch (fmt[i])
1688 case 'w':
1689 if (XWINT (x, i) != XWINT (y, i))
1690 return 0;
1691 break;
1693 case 'i':
1694 if (XINT (x, i) != XINT (y, i))
1695 return 0;
1696 break;
1698 case 'E':
1699 /* Two vectors must have the same length. */
1700 if (XVECLEN (x, i) != XVECLEN (y, i))
1701 return 0;
1703 /* And the corresponding elements must match. */
1704 for (j = 0; j < XVECLEN (x, i); j++)
1705 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
1706 movables, regs) == 0)
1707 return 0;
1708 break;
1710 case 'e':
1711 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
1712 == 0)
1713 return 0;
1714 break;
1716 case 's':
1717 if (strcmp (XSTR (x, i), XSTR (y, i)))
1718 return 0;
1719 break;
1721 case 'u':
1722 /* These are just backpointers, so they don't matter. */
1723 break;
1725 case '0':
1726 break;
1728 /* It is believed that rtx's at this level will never
1729 contain anything but integers and other rtx's,
1730 except for within LABEL_REFs and SYMBOL_REFs. */
1731 default:
1732 abort ();
1735 return 1;
1738 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1739 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1740 references is incremented once for each added note. */
1742 static void
1743 add_label_notes (x, insns)
1744 rtx x;
1745 rtx insns;
1747 enum rtx_code code = GET_CODE (x);
1748 int i, j;
1749 const char *fmt;
1750 rtx insn;
1752 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1754 /* This code used to ignore labels that referred to dispatch tables to
1755 avoid flow generating (slighly) worse code.
1757 We no longer ignore such label references (see LABEL_REF handling in
1758 mark_jump_label for additional information). */
1759 for (insn = insns; insn; insn = NEXT_INSN (insn))
1760 if (reg_mentioned_p (XEXP (x, 0), insn))
1762 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
1763 REG_NOTES (insn));
1764 if (LABEL_P (XEXP (x, 0)))
1765 LABEL_NUSES (XEXP (x, 0))++;
1769 fmt = GET_RTX_FORMAT (code);
1770 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1772 if (fmt[i] == 'e')
1773 add_label_notes (XEXP (x, i), insns);
1774 else if (fmt[i] == 'E')
1775 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1776 add_label_notes (XVECEXP (x, i, j), insns);
1780 /* Scan MOVABLES, and move the insns that deserve to be moved.
1781 If two matching movables are combined, replace one reg with the
1782 other throughout. */
1784 static void
1785 move_movables (loop, movables, threshold, insn_count)
1786 struct loop *loop;
1787 struct loop_movables *movables;
1788 int threshold;
1789 int insn_count;
1791 struct loop_regs *regs = LOOP_REGS (loop);
1792 int nregs = regs->num;
1793 rtx new_start = 0;
1794 struct movable *m;
1795 rtx p;
1796 rtx loop_start = loop->start;
1797 rtx loop_end = loop->end;
1798 /* Map of pseudo-register replacements to handle combining
1799 when we move several insns that load the same value
1800 into different pseudo-registers. */
1801 rtx *reg_map = (rtx *) xcalloc (nregs, sizeof (rtx));
1802 char *already_moved = (char *) xcalloc (nregs, sizeof (char));
1804 for (m = movables->head; m; m = m->next)
1806 /* Describe this movable insn. */
1808 if (loop_dump_stream)
1810 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1811 INSN_UID (m->insn), m->regno, m->lifetime);
1812 if (m->consec > 0)
1813 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1814 if (m->cond)
1815 fprintf (loop_dump_stream, "cond ");
1816 if (m->force)
1817 fprintf (loop_dump_stream, "force ");
1818 if (m->global)
1819 fprintf (loop_dump_stream, "global ");
1820 if (m->done)
1821 fprintf (loop_dump_stream, "done ");
1822 if (m->move_insn)
1823 fprintf (loop_dump_stream, "move-insn ");
1824 if (m->match)
1825 fprintf (loop_dump_stream, "matches %d ",
1826 INSN_UID (m->match->insn));
1827 if (m->forces)
1828 fprintf (loop_dump_stream, "forces %d ",
1829 INSN_UID (m->forces->insn));
1832 /* Ignore the insn if it's already done (it matched something else).
1833 Otherwise, see if it is now safe to move. */
1835 if (!m->done
1836 && (! m->cond
1837 || (1 == loop_invariant_p (loop, m->set_src)
1838 && (m->dependencies == 0
1839 || 1 == loop_invariant_p (loop, m->dependencies))
1840 && (m->consec == 0
1841 || 1 == consec_sets_invariant_p (loop, m->set_dest,
1842 m->consec + 1,
1843 m->insn))))
1844 && (! m->forces || m->forces->done))
1846 int regno;
1847 rtx p;
1848 int savings = m->savings;
1850 /* We have an insn that is safe to move.
1851 Compute its desirability. */
1853 p = m->insn;
1854 regno = m->regno;
1856 if (loop_dump_stream)
1857 fprintf (loop_dump_stream, "savings %d ", savings);
1859 if (regs->array[regno].moved_once && loop_dump_stream)
1860 fprintf (loop_dump_stream, "halved since already moved ");
1862 /* An insn MUST be moved if we already moved something else
1863 which is safe only if this one is moved too: that is,
1864 if already_moved[REGNO] is nonzero. */
1866 /* An insn is desirable to move if the new lifetime of the
1867 register is no more than THRESHOLD times the old lifetime.
1868 If it's not desirable, it means the loop is so big
1869 that moving won't speed things up much,
1870 and it is liable to make register usage worse. */
1872 /* It is also desirable to move if it can be moved at no
1873 extra cost because something else was already moved. */
1875 if (already_moved[regno]
1876 || flag_move_all_movables
1877 || (threshold * savings * m->lifetime) >=
1878 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
1879 || (m->forces && m->forces->done
1880 && regs->array[m->forces->regno].n_times_set == 1))
1882 int count;
1883 struct movable *m1;
1884 rtx first = NULL_RTX;
1886 /* Now move the insns that set the reg. */
1888 if (m->partial && m->match)
1890 rtx newpat, i1;
1891 rtx r1, r2;
1892 /* Find the end of this chain of matching regs.
1893 Thus, we load each reg in the chain from that one reg.
1894 And that reg is loaded with 0 directly,
1895 since it has ->match == 0. */
1896 for (m1 = m; m1->match; m1 = m1->match);
1897 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1898 SET_DEST (PATTERN (m1->insn)));
1899 i1 = loop_insn_hoist (loop, newpat);
1901 /* Mark the moved, invariant reg as being allowed to
1902 share a hard reg with the other matching invariant. */
1903 REG_NOTES (i1) = REG_NOTES (m->insn);
1904 r1 = SET_DEST (PATTERN (m->insn));
1905 r2 = SET_DEST (PATTERN (m1->insn));
1906 regs_may_share
1907 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1908 gen_rtx_EXPR_LIST (VOIDmode, r2,
1909 regs_may_share));
1910 delete_insn (m->insn);
1912 if (new_start == 0)
1913 new_start = i1;
1915 if (loop_dump_stream)
1916 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1918 /* If we are to re-generate the item being moved with a
1919 new move insn, first delete what we have and then emit
1920 the move insn before the loop. */
1921 else if (m->move_insn)
1923 rtx i1, temp, seq;
1925 for (count = m->consec; count >= 0; count--)
1927 /* If this is the first insn of a library call sequence,
1928 skip to the end. */
1929 if (GET_CODE (p) != NOTE
1930 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1931 p = XEXP (temp, 0);
1933 /* If this is the last insn of a libcall sequence, then
1934 delete every insn in the sequence except the last.
1935 The last insn is handled in the normal manner. */
1936 if (GET_CODE (p) != NOTE
1937 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1939 temp = XEXP (temp, 0);
1940 while (temp != p)
1941 temp = delete_insn (temp);
1944 temp = p;
1945 p = delete_insn (p);
1947 /* simplify_giv_expr expects that it can walk the insns
1948 at m->insn forwards and see this old sequence we are
1949 tossing here. delete_insn does preserve the next
1950 pointers, but when we skip over a NOTE we must fix
1951 it up. Otherwise that code walks into the non-deleted
1952 insn stream. */
1953 while (p && GET_CODE (p) == NOTE)
1954 p = NEXT_INSN (temp) = NEXT_INSN (p);
1957 start_sequence ();
1958 emit_move_insn (m->set_dest, m->set_src);
1959 temp = get_insns ();
1960 seq = gen_sequence ();
1961 end_sequence ();
1963 add_label_notes (m->set_src, temp);
1965 i1 = loop_insn_hoist (loop, seq);
1966 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1967 set_unique_reg_note (i1,
1968 m->is_equiv ? REG_EQUIV : REG_EQUAL,
1969 m->set_src);
1971 if (loop_dump_stream)
1972 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1974 /* The more regs we move, the less we like moving them. */
1975 threshold -= 3;
1977 else
1979 for (count = m->consec; count >= 0; count--)
1981 rtx i1, temp;
1983 /* If first insn of libcall sequence, skip to end. */
1984 /* Do this at start of loop, since p is guaranteed to
1985 be an insn here. */
1986 if (GET_CODE (p) != NOTE
1987 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1988 p = XEXP (temp, 0);
1990 /* If last insn of libcall sequence, move all
1991 insns except the last before the loop. The last
1992 insn is handled in the normal manner. */
1993 if (GET_CODE (p) != NOTE
1994 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1996 rtx fn_address = 0;
1997 rtx fn_reg = 0;
1998 rtx fn_address_insn = 0;
2000 first = 0;
2001 for (temp = XEXP (temp, 0); temp != p;
2002 temp = NEXT_INSN (temp))
2004 rtx body;
2005 rtx n;
2006 rtx next;
2008 if (GET_CODE (temp) == NOTE)
2009 continue;
2011 body = PATTERN (temp);
2013 /* Find the next insn after TEMP,
2014 not counting USE or NOTE insns. */
2015 for (next = NEXT_INSN (temp); next != p;
2016 next = NEXT_INSN (next))
2017 if (! (GET_CODE (next) == INSN
2018 && GET_CODE (PATTERN (next)) == USE)
2019 && GET_CODE (next) != NOTE)
2020 break;
2022 /* If that is the call, this may be the insn
2023 that loads the function address.
2025 Extract the function address from the insn
2026 that loads it into a register.
2027 If this insn was cse'd, we get incorrect code.
2029 So emit a new move insn that copies the
2030 function address into the register that the
2031 call insn will use. flow.c will delete any
2032 redundant stores that we have created. */
2033 if (GET_CODE (next) == CALL_INSN
2034 && GET_CODE (body) == SET
2035 && GET_CODE (SET_DEST (body)) == REG
2036 && (n = find_reg_note (temp, REG_EQUAL,
2037 NULL_RTX)))
2039 fn_reg = SET_SRC (body);
2040 if (GET_CODE (fn_reg) != REG)
2041 fn_reg = SET_DEST (body);
2042 fn_address = XEXP (n, 0);
2043 fn_address_insn = temp;
2045 /* We have the call insn.
2046 If it uses the register we suspect it might,
2047 load it with the correct address directly. */
2048 if (GET_CODE (temp) == CALL_INSN
2049 && fn_address != 0
2050 && reg_referenced_p (fn_reg, body))
2051 loop_insn_emit_after (loop, 0, fn_address_insn,
2052 gen_move_insn
2053 (fn_reg, fn_address));
2055 if (GET_CODE (temp) == CALL_INSN)
2057 i1 = loop_call_insn_hoist (loop, body);
2058 /* Because the USAGE information potentially
2059 contains objects other than hard registers
2060 we need to copy it. */
2061 if (CALL_INSN_FUNCTION_USAGE (temp))
2062 CALL_INSN_FUNCTION_USAGE (i1)
2063 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
2065 else
2066 i1 = loop_insn_hoist (loop, body);
2067 if (first == 0)
2068 first = i1;
2069 if (temp == fn_address_insn)
2070 fn_address_insn = i1;
2071 REG_NOTES (i1) = REG_NOTES (temp);
2072 REG_NOTES (temp) = NULL;
2073 delete_insn (temp);
2075 if (new_start == 0)
2076 new_start = first;
2078 if (m->savemode != VOIDmode)
2080 /* P sets REG to zero; but we should clear only
2081 the bits that are not covered by the mode
2082 m->savemode. */
2083 rtx reg = m->set_dest;
2084 rtx sequence;
2085 rtx tem;
2087 start_sequence ();
2088 tem = expand_simple_binop
2089 (GET_MODE (reg), AND, reg,
2090 GEN_INT ((((HOST_WIDE_INT) 1
2091 << GET_MODE_BITSIZE (m->savemode)))
2092 - 1),
2093 reg, 1, OPTAB_LIB_WIDEN);
2094 if (tem == 0)
2095 abort ();
2096 if (tem != reg)
2097 emit_move_insn (reg, tem);
2098 sequence = gen_sequence ();
2099 end_sequence ();
2100 i1 = loop_insn_hoist (loop, sequence);
2102 else if (GET_CODE (p) == CALL_INSN)
2104 i1 = loop_call_insn_hoist (loop, PATTERN (p));
2105 /* Because the USAGE information potentially
2106 contains objects other than hard registers
2107 we need to copy it. */
2108 if (CALL_INSN_FUNCTION_USAGE (p))
2109 CALL_INSN_FUNCTION_USAGE (i1)
2110 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2112 else if (count == m->consec && m->move_insn_first)
2114 rtx seq;
2115 /* The SET_SRC might not be invariant, so we must
2116 use the REG_EQUAL note. */
2117 start_sequence ();
2118 emit_move_insn (m->set_dest, m->set_src);
2119 temp = get_insns ();
2120 seq = gen_sequence ();
2121 end_sequence ();
2123 add_label_notes (m->set_src, temp);
2125 i1 = loop_insn_hoist (loop, seq);
2126 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2127 set_unique_reg_note (i1, m->is_equiv ? REG_EQUIV
2128 : REG_EQUAL, m->set_src);
2130 else
2131 i1 = loop_insn_hoist (loop, PATTERN (p));
2133 if (REG_NOTES (i1) == 0)
2135 REG_NOTES (i1) = REG_NOTES (p);
2136 REG_NOTES (p) = NULL;
2138 /* If there is a REG_EQUAL note present whose value
2139 is not loop invariant, then delete it, since it
2140 may cause problems with later optimization passes.
2141 It is possible for cse to create such notes
2142 like this as a result of record_jump_cond. */
2144 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2145 && ! loop_invariant_p (loop, XEXP (temp, 0)))
2146 remove_note (i1, temp);
2149 if (new_start == 0)
2150 new_start = i1;
2152 if (loop_dump_stream)
2153 fprintf (loop_dump_stream, " moved to %d",
2154 INSN_UID (i1));
2156 /* If library call, now fix the REG_NOTES that contain
2157 insn pointers, namely REG_LIBCALL on FIRST
2158 and REG_RETVAL on I1. */
2159 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2161 XEXP (temp, 0) = first;
2162 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2163 XEXP (temp, 0) = i1;
2166 temp = p;
2167 delete_insn (p);
2168 p = NEXT_INSN (p);
2170 /* simplify_giv_expr expects that it can walk the insns
2171 at m->insn forwards and see this old sequence we are
2172 tossing here. delete_insn does preserve the next
2173 pointers, but when we skip over a NOTE we must fix
2174 it up. Otherwise that code walks into the non-deleted
2175 insn stream. */
2176 while (p && GET_CODE (p) == NOTE)
2177 p = NEXT_INSN (temp) = NEXT_INSN (p);
2180 /* The more regs we move, the less we like moving them. */
2181 threshold -= 3;
2184 /* Any other movable that loads the same register
2185 MUST be moved. */
2186 already_moved[regno] = 1;
2188 /* This reg has been moved out of one loop. */
2189 regs->array[regno].moved_once = 1;
2191 /* The reg set here is now invariant. */
2192 if (! m->partial)
2194 int i;
2195 for (i = 0; i < LOOP_REGNO_NREGS (regno, m->set_dest); i++)
2196 regs->array[regno+i].set_in_loop = 0;
2199 m->done = 1;
2201 /* Change the length-of-life info for the register
2202 to say it lives at least the full length of this loop.
2203 This will help guide optimizations in outer loops. */
2205 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2206 /* This is the old insn before all the moved insns.
2207 We can't use the moved insn because it is out of range
2208 in uid_luid. Only the old insns have luids. */
2209 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2210 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2211 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2213 /* Combine with this moved insn any other matching movables. */
2215 if (! m->partial)
2216 for (m1 = movables->head; m1; m1 = m1->next)
2217 if (m1->match == m)
2219 rtx temp;
2221 /* Schedule the reg loaded by M1
2222 for replacement so that shares the reg of M.
2223 If the modes differ (only possible in restricted
2224 circumstances, make a SUBREG.
2226 Note this assumes that the target dependent files
2227 treat REG and SUBREG equally, including within
2228 GO_IF_LEGITIMATE_ADDRESS and in all the
2229 predicates since we never verify that replacing the
2230 original register with a SUBREG results in a
2231 recognizable insn. */
2232 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2233 reg_map[m1->regno] = m->set_dest;
2234 else
2235 reg_map[m1->regno]
2236 = gen_lowpart_common (GET_MODE (m1->set_dest),
2237 m->set_dest);
2239 /* Get rid of the matching insn
2240 and prevent further processing of it. */
2241 m1->done = 1;
2243 /* if library call, delete all insns. */
2244 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2245 NULL_RTX)))
2246 delete_insn_chain (XEXP (temp, 0), m1->insn);
2247 else
2248 delete_insn (m1->insn);
2250 /* Any other movable that loads the same register
2251 MUST be moved. */
2252 already_moved[m1->regno] = 1;
2254 /* The reg merged here is now invariant,
2255 if the reg it matches is invariant. */
2256 if (! m->partial)
2258 int i;
2259 for (i = 0;
2260 i < LOOP_REGNO_NREGS (regno, m1->set_dest);
2261 i++)
2262 regs->array[m1->regno+i].set_in_loop = 0;
2266 else if (loop_dump_stream)
2267 fprintf (loop_dump_stream, "not desirable");
2269 else if (loop_dump_stream && !m->match)
2270 fprintf (loop_dump_stream, "not safe");
2272 if (loop_dump_stream)
2273 fprintf (loop_dump_stream, "\n");
2276 if (new_start == 0)
2277 new_start = loop_start;
2279 /* Go through all the instructions in the loop, making
2280 all the register substitutions scheduled in REG_MAP. */
2281 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2282 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2283 || GET_CODE (p) == CALL_INSN)
2285 replace_regs (PATTERN (p), reg_map, nregs, 0);
2286 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2287 INSN_CODE (p) = -1;
2290 /* Clean up. */
2291 free (reg_map);
2292 free (already_moved);
2296 static void
2297 loop_movables_add (movables, m)
2298 struct loop_movables *movables;
2299 struct movable *m;
2301 if (movables->head == 0)
2302 movables->head = m;
2303 else
2304 movables->last->next = m;
2305 movables->last = m;
2309 static void
2310 loop_movables_free (movables)
2311 struct loop_movables *movables;
2313 struct movable *m;
2314 struct movable *m_next;
2316 for (m = movables->head; m; m = m_next)
2318 m_next = m->next;
2319 free (m);
2323 #if 0
2324 /* Scan X and replace the address of any MEM in it with ADDR.
2325 REG is the address that MEM should have before the replacement. */
2327 static void
2328 replace_call_address (x, reg, addr)
2329 rtx x, reg, addr;
2331 enum rtx_code code;
2332 int i;
2333 const char *fmt;
2335 if (x == 0)
2336 return;
2337 code = GET_CODE (x);
2338 switch (code)
2340 case PC:
2341 case CC0:
2342 case CONST_INT:
2343 case CONST_DOUBLE:
2344 case CONST:
2345 case SYMBOL_REF:
2346 case LABEL_REF:
2347 case REG:
2348 return;
2350 case SET:
2351 /* Short cut for very common case. */
2352 replace_call_address (XEXP (x, 1), reg, addr);
2353 return;
2355 case CALL:
2356 /* Short cut for very common case. */
2357 replace_call_address (XEXP (x, 0), reg, addr);
2358 return;
2360 case MEM:
2361 /* If this MEM uses a reg other than the one we expected,
2362 something is wrong. */
2363 if (XEXP (x, 0) != reg)
2364 abort ();
2365 XEXP (x, 0) = addr;
2366 return;
2368 default:
2369 break;
2372 fmt = GET_RTX_FORMAT (code);
2373 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2375 if (fmt[i] == 'e')
2376 replace_call_address (XEXP (x, i), reg, addr);
2377 else if (fmt[i] == 'E')
2379 int j;
2380 for (j = 0; j < XVECLEN (x, i); j++)
2381 replace_call_address (XVECEXP (x, i, j), reg, addr);
2385 #endif
2387 /* Return the number of memory refs to addresses that vary
2388 in the rtx X. */
2390 static int
2391 count_nonfixed_reads (loop, x)
2392 const struct loop *loop;
2393 rtx x;
2395 enum rtx_code code;
2396 int i;
2397 const char *fmt;
2398 int value;
2400 if (x == 0)
2401 return 0;
2403 code = GET_CODE (x);
2404 switch (code)
2406 case PC:
2407 case CC0:
2408 case CONST_INT:
2409 case CONST_DOUBLE:
2410 case CONST:
2411 case SYMBOL_REF:
2412 case LABEL_REF:
2413 case REG:
2414 return 0;
2416 case MEM:
2417 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2418 + count_nonfixed_reads (loop, XEXP (x, 0)));
2420 default:
2421 break;
2424 value = 0;
2425 fmt = GET_RTX_FORMAT (code);
2426 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2428 if (fmt[i] == 'e')
2429 value += count_nonfixed_reads (loop, XEXP (x, i));
2430 if (fmt[i] == 'E')
2432 int j;
2433 for (j = 0; j < XVECLEN (x, i); j++)
2434 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2437 return value;
2440 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2441 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2442 `unknown_address_altered', `unknown_constant_address_altered', and
2443 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2444 list `store_mems' in LOOP. */
2446 static void
2447 prescan_loop (loop)
2448 struct loop *loop;
2450 int level = 1;
2451 rtx insn;
2452 struct loop_info *loop_info = LOOP_INFO (loop);
2453 rtx start = loop->start;
2454 rtx end = loop->end;
2455 /* The label after END. Jumping here is just like falling off the
2456 end of the loop. We use next_nonnote_insn instead of next_label
2457 as a hedge against the (pathological) case where some actual insn
2458 might end up between the two. */
2459 rtx exit_target = next_nonnote_insn (end);
2461 loop_info->has_indirect_jump = indirect_jump_in_function;
2462 loop_info->pre_header_has_call = 0;
2463 loop_info->has_call = 0;
2464 loop_info->has_nonconst_call = 0;
2465 loop_info->has_prefetch = 0;
2466 loop_info->has_volatile = 0;
2467 loop_info->has_tablejump = 0;
2468 loop_info->has_multiple_exit_targets = 0;
2469 loop->level = 1;
2471 loop_info->unknown_address_altered = 0;
2472 loop_info->unknown_constant_address_altered = 0;
2473 loop_info->store_mems = NULL_RTX;
2474 loop_info->first_loop_store_insn = NULL_RTX;
2475 loop_info->mems_idx = 0;
2476 loop_info->num_mem_sets = 0;
2479 for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
2480 insn = PREV_INSN (insn))
2482 if (GET_CODE (insn) == CALL_INSN)
2484 loop_info->pre_header_has_call = 1;
2485 break;
2489 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2490 insn = NEXT_INSN (insn))
2492 switch (GET_CODE (insn))
2494 case NOTE:
2495 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2497 ++level;
2498 /* Count number of loops contained in this one. */
2499 loop->level++;
2501 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2502 --level;
2503 break;
2505 case CALL_INSN:
2506 if (! CONST_OR_PURE_CALL_P (insn))
2508 loop_info->unknown_address_altered = 1;
2509 loop_info->has_nonconst_call = 1;
2511 else if (pure_call_p (insn))
2512 loop_info->has_nonconst_call = 1;
2513 loop_info->has_call = 1;
2514 if (can_throw_internal (insn))
2515 loop_info->has_multiple_exit_targets = 1;
2516 break;
2518 case JUMP_INSN:
2519 if (! loop_info->has_multiple_exit_targets)
2521 rtx set = pc_set (insn);
2523 if (set)
2525 rtx src = SET_SRC (set);
2526 rtx label1, label2;
2528 if (GET_CODE (src) == IF_THEN_ELSE)
2530 label1 = XEXP (src, 1);
2531 label2 = XEXP (src, 2);
2533 else
2535 label1 = src;
2536 label2 = NULL_RTX;
2541 if (label1 && label1 != pc_rtx)
2543 if (GET_CODE (label1) != LABEL_REF)
2545 /* Something tricky. */
2546 loop_info->has_multiple_exit_targets = 1;
2547 break;
2549 else if (XEXP (label1, 0) != exit_target
2550 && LABEL_OUTSIDE_LOOP_P (label1))
2552 /* A jump outside the current loop. */
2553 loop_info->has_multiple_exit_targets = 1;
2554 break;
2558 label1 = label2;
2559 label2 = NULL_RTX;
2561 while (label1);
2563 else
2565 /* A return, or something tricky. */
2566 loop_info->has_multiple_exit_targets = 1;
2569 /* FALLTHRU */
2571 case INSN:
2572 if (volatile_refs_p (PATTERN (insn)))
2573 loop_info->has_volatile = 1;
2575 if (GET_CODE (insn) == JUMP_INSN
2576 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2577 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2578 loop_info->has_tablejump = 1;
2580 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2581 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2582 loop_info->first_loop_store_insn = insn;
2584 if (flag_non_call_exceptions && can_throw_internal (insn))
2585 loop_info->has_multiple_exit_targets = 1;
2586 break;
2588 default:
2589 break;
2593 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2594 if (/* An exception thrown by a called function might land us
2595 anywhere. */
2596 ! loop_info->has_nonconst_call
2597 /* We don't want loads for MEMs moved to a location before the
2598 one at which their stack memory becomes allocated. (Note
2599 that this is not a problem for malloc, etc., since those
2600 require actual function calls. */
2601 && ! current_function_calls_alloca
2602 /* There are ways to leave the loop other than falling off the
2603 end. */
2604 && ! loop_info->has_multiple_exit_targets)
2605 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2606 insn = NEXT_INSN (insn))
2607 for_each_rtx (&insn, insert_loop_mem, loop_info);
2609 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2610 that loop_invariant_p and load_mems can use true_dependence
2611 to determine what is really clobbered. */
2612 if (loop_info->unknown_address_altered)
2614 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2616 loop_info->store_mems
2617 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2619 if (loop_info->unknown_constant_address_altered)
2621 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2623 RTX_UNCHANGING_P (mem) = 1;
2624 loop_info->store_mems
2625 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2629 /* Invalidate all loops containing LABEL. */
2631 static void
2632 invalidate_loops_containing_label (label)
2633 rtx label;
2635 struct loop *loop;
2636 for (loop = uid_loop[INSN_UID (label)]; loop; loop = loop->outer)
2637 loop->invalid = 1;
2640 /* Scan the function looking for loops. Record the start and end of each loop.
2641 Also mark as invalid loops any loops that contain a setjmp or are branched
2642 to from outside the loop. */
2644 static void
2645 find_and_verify_loops (f, loops)
2646 rtx f;
2647 struct loops *loops;
2649 rtx insn;
2650 rtx label;
2651 int num_loops;
2652 struct loop *current_loop;
2653 struct loop *next_loop;
2654 struct loop *loop;
2656 num_loops = loops->num;
2658 compute_luids (f, NULL_RTX, 0);
2660 /* If there are jumps to undefined labels,
2661 treat them as jumps out of any/all loops.
2662 This also avoids writing past end of tables when there are no loops. */
2663 uid_loop[0] = NULL;
2665 /* Find boundaries of loops, mark which loops are contained within
2666 loops, and invalidate loops that have setjmp. */
2668 num_loops = 0;
2669 current_loop = NULL;
2670 for (insn = f; insn; insn = NEXT_INSN (insn))
2672 if (GET_CODE (insn) == NOTE)
2673 switch (NOTE_LINE_NUMBER (insn))
2675 case NOTE_INSN_LOOP_BEG:
2676 next_loop = loops->array + num_loops;
2677 next_loop->num = num_loops;
2678 num_loops++;
2679 next_loop->start = insn;
2680 next_loop->outer = current_loop;
2681 current_loop = next_loop;
2682 break;
2684 case NOTE_INSN_LOOP_CONT:
2685 current_loop->cont = insn;
2686 break;
2688 case NOTE_INSN_LOOP_VTOP:
2689 current_loop->vtop = insn;
2690 break;
2692 case NOTE_INSN_LOOP_END:
2693 if (! current_loop)
2694 abort ();
2696 current_loop->end = insn;
2697 current_loop = current_loop->outer;
2698 break;
2700 default:
2701 break;
2704 if (GET_CODE (insn) == CALL_INSN
2705 && find_reg_note (insn, REG_SETJMP, NULL))
2707 /* In this case, we must invalidate our current loop and any
2708 enclosing loop. */
2709 for (loop = current_loop; loop; loop = loop->outer)
2711 loop->invalid = 1;
2712 if (loop_dump_stream)
2713 fprintf (loop_dump_stream,
2714 "\nLoop at %d ignored due to setjmp.\n",
2715 INSN_UID (loop->start));
2719 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2720 enclosing loop, but this doesn't matter. */
2721 uid_loop[INSN_UID (insn)] = current_loop;
2724 /* Any loop containing a label used in an initializer must be invalidated,
2725 because it can be jumped into from anywhere. */
2726 for (label = forced_labels; label; label = XEXP (label, 1))
2727 invalidate_loops_containing_label (XEXP (label, 0));
2729 /* Any loop containing a label used for an exception handler must be
2730 invalidated, because it can be jumped into from anywhere. */
2731 for_each_eh_label (invalidate_loops_containing_label);
2733 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2734 loop that it is not contained within, that loop is marked invalid.
2735 If any INSN or CALL_INSN uses a label's address, then the loop containing
2736 that label is marked invalid, because it could be jumped into from
2737 anywhere.
2739 Also look for blocks of code ending in an unconditional branch that
2740 exits the loop. If such a block is surrounded by a conditional
2741 branch around the block, move the block elsewhere (see below) and
2742 invert the jump to point to the code block. This may eliminate a
2743 label in our loop and will simplify processing by both us and a
2744 possible second cse pass. */
2746 for (insn = f; insn; insn = NEXT_INSN (insn))
2747 if (INSN_P (insn))
2749 struct loop *this_loop = uid_loop[INSN_UID (insn)];
2751 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2753 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2754 if (note)
2755 invalidate_loops_containing_label (XEXP (note, 0));
2758 if (GET_CODE (insn) != JUMP_INSN)
2759 continue;
2761 mark_loop_jump (PATTERN (insn), this_loop);
2763 /* See if this is an unconditional branch outside the loop. */
2764 if (this_loop
2765 && (GET_CODE (PATTERN (insn)) == RETURN
2766 || (any_uncondjump_p (insn)
2767 && onlyjump_p (insn)
2768 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2769 != this_loop)))
2770 && get_max_uid () < max_uid_for_loop)
2772 rtx p;
2773 rtx our_next = next_real_insn (insn);
2774 rtx last_insn_to_move = NEXT_INSN (insn);
2775 struct loop *dest_loop;
2776 struct loop *outer_loop = NULL;
2778 /* Go backwards until we reach the start of the loop, a label,
2779 or a JUMP_INSN. */
2780 for (p = PREV_INSN (insn);
2781 GET_CODE (p) != CODE_LABEL
2782 && ! (GET_CODE (p) == NOTE
2783 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2784 && GET_CODE (p) != JUMP_INSN;
2785 p = PREV_INSN (p))
2788 /* Check for the case where we have a jump to an inner nested
2789 loop, and do not perform the optimization in that case. */
2791 if (JUMP_LABEL (insn))
2793 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2794 if (dest_loop)
2796 for (outer_loop = dest_loop; outer_loop;
2797 outer_loop = outer_loop->outer)
2798 if (outer_loop == this_loop)
2799 break;
2803 /* Make sure that the target of P is within the current loop. */
2805 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2806 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2807 outer_loop = this_loop;
2809 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2810 we have a block of code to try to move.
2812 We look backward and then forward from the target of INSN
2813 to find a BARRIER at the same loop depth as the target.
2814 If we find such a BARRIER, we make a new label for the start
2815 of the block, invert the jump in P and point it to that label,
2816 and move the block of code to the spot we found. */
2818 if (! outer_loop
2819 && GET_CODE (p) == JUMP_INSN
2820 && JUMP_LABEL (p) != 0
2821 /* Just ignore jumps to labels that were never emitted.
2822 These always indicate compilation errors. */
2823 && INSN_UID (JUMP_LABEL (p)) != 0
2824 && any_condjump_p (p) && onlyjump_p (p)
2825 && next_real_insn (JUMP_LABEL (p)) == our_next
2826 /* If it's not safe to move the sequence, then we
2827 mustn't try. */
2828 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2829 &last_insn_to_move))
2831 rtx target
2832 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2833 struct loop *target_loop = uid_loop[INSN_UID (target)];
2834 rtx loc, loc2;
2835 rtx tmp;
2837 /* Search for possible garbage past the conditional jumps
2838 and look for the last barrier. */
2839 for (tmp = last_insn_to_move;
2840 tmp && GET_CODE (tmp) != CODE_LABEL; tmp = NEXT_INSN (tmp))
2841 if (GET_CODE (tmp) == BARRIER)
2842 last_insn_to_move = tmp;
2844 for (loc = target; loc; loc = PREV_INSN (loc))
2845 if (GET_CODE (loc) == BARRIER
2846 /* Don't move things inside a tablejump. */
2847 && ((loc2 = next_nonnote_insn (loc)) == 0
2848 || GET_CODE (loc2) != CODE_LABEL
2849 || (loc2 = next_nonnote_insn (loc2)) == 0
2850 || GET_CODE (loc2) != JUMP_INSN
2851 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2852 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2853 && uid_loop[INSN_UID (loc)] == target_loop)
2854 break;
2856 if (loc == 0)
2857 for (loc = target; loc; loc = NEXT_INSN (loc))
2858 if (GET_CODE (loc) == BARRIER
2859 /* Don't move things inside a tablejump. */
2860 && ((loc2 = next_nonnote_insn (loc)) == 0
2861 || GET_CODE (loc2) != CODE_LABEL
2862 || (loc2 = next_nonnote_insn (loc2)) == 0
2863 || GET_CODE (loc2) != JUMP_INSN
2864 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2865 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2866 && uid_loop[INSN_UID (loc)] == target_loop)
2867 break;
2869 if (loc)
2871 rtx cond_label = JUMP_LABEL (p);
2872 rtx new_label = get_label_after (p);
2874 /* Ensure our label doesn't go away. */
2875 LABEL_NUSES (cond_label)++;
2877 /* Verify that uid_loop is large enough and that
2878 we can invert P. */
2879 if (invert_jump (p, new_label, 1))
2881 rtx q, r;
2883 /* If no suitable BARRIER was found, create a suitable
2884 one before TARGET. Since TARGET is a fall through
2885 path, we'll need to insert an jump around our block
2886 and add a BARRIER before TARGET.
2888 This creates an extra unconditional jump outside
2889 the loop. However, the benefits of removing rarely
2890 executed instructions from inside the loop usually
2891 outweighs the cost of the extra unconditional jump
2892 outside the loop. */
2893 if (loc == 0)
2895 rtx temp;
2897 temp = gen_jump (JUMP_LABEL (insn));
2898 temp = emit_jump_insn_before (temp, target);
2899 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2900 LABEL_NUSES (JUMP_LABEL (insn))++;
2901 loc = emit_barrier_before (target);
2904 /* Include the BARRIER after INSN and copy the
2905 block after LOC. */
2906 if (squeeze_notes (&new_label, &last_insn_to_move))
2907 abort ();
2908 reorder_insns (new_label, last_insn_to_move, loc);
2910 /* All those insns are now in TARGET_LOOP. */
2911 for (q = new_label;
2912 q != NEXT_INSN (last_insn_to_move);
2913 q = NEXT_INSN (q))
2914 uid_loop[INSN_UID (q)] = target_loop;
2916 /* The label jumped to by INSN is no longer a loop
2917 exit. Unless INSN does not have a label (e.g.,
2918 it is a RETURN insn), search loop->exit_labels
2919 to find its label_ref, and remove it. Also turn
2920 off LABEL_OUTSIDE_LOOP_P bit. */
2921 if (JUMP_LABEL (insn))
2923 for (q = 0, r = this_loop->exit_labels;
2925 q = r, r = LABEL_NEXTREF (r))
2926 if (XEXP (r, 0) == JUMP_LABEL (insn))
2928 LABEL_OUTSIDE_LOOP_P (r) = 0;
2929 if (q)
2930 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2931 else
2932 this_loop->exit_labels = LABEL_NEXTREF (r);
2933 break;
2936 for (loop = this_loop; loop && loop != target_loop;
2937 loop = loop->outer)
2938 loop->exit_count--;
2940 /* If we didn't find it, then something is
2941 wrong. */
2942 if (! r)
2943 abort ();
2946 /* P is now a jump outside the loop, so it must be put
2947 in loop->exit_labels, and marked as such.
2948 The easiest way to do this is to just call
2949 mark_loop_jump again for P. */
2950 mark_loop_jump (PATTERN (p), this_loop);
2952 /* If INSN now jumps to the insn after it,
2953 delete INSN. */
2954 if (JUMP_LABEL (insn) != 0
2955 && (next_real_insn (JUMP_LABEL (insn))
2956 == next_real_insn (insn)))
2957 delete_related_insns (insn);
2960 /* Continue the loop after where the conditional
2961 branch used to jump, since the only branch insn
2962 in the block (if it still remains) is an inter-loop
2963 branch and hence needs no processing. */
2964 insn = NEXT_INSN (cond_label);
2966 if (--LABEL_NUSES (cond_label) == 0)
2967 delete_related_insns (cond_label);
2969 /* This loop will be continued with NEXT_INSN (insn). */
2970 insn = PREV_INSN (insn);
2977 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2978 loops it is contained in, mark the target loop invalid.
2980 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2982 static void
2983 mark_loop_jump (x, loop)
2984 rtx x;
2985 struct loop *loop;
2987 struct loop *dest_loop;
2988 struct loop *outer_loop;
2989 int i;
2991 switch (GET_CODE (x))
2993 case PC:
2994 case USE:
2995 case CLOBBER:
2996 case REG:
2997 case MEM:
2998 case CONST_INT:
2999 case CONST_DOUBLE:
3000 case RETURN:
3001 return;
3003 case CONST:
3004 /* There could be a label reference in here. */
3005 mark_loop_jump (XEXP (x, 0), loop);
3006 return;
3008 case PLUS:
3009 case MINUS:
3010 case MULT:
3011 mark_loop_jump (XEXP (x, 0), loop);
3012 mark_loop_jump (XEXP (x, 1), loop);
3013 return;
3015 case LO_SUM:
3016 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3017 mark_loop_jump (XEXP (x, 1), loop);
3018 return;
3020 case SIGN_EXTEND:
3021 case ZERO_EXTEND:
3022 mark_loop_jump (XEXP (x, 0), loop);
3023 return;
3025 case LABEL_REF:
3026 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3028 /* Link together all labels that branch outside the loop. This
3029 is used by final_[bg]iv_value and the loop unrolling code. Also
3030 mark this LABEL_REF so we know that this branch should predict
3031 false. */
3033 /* A check to make sure the label is not in an inner nested loop,
3034 since this does not count as a loop exit. */
3035 if (dest_loop)
3037 for (outer_loop = dest_loop; outer_loop;
3038 outer_loop = outer_loop->outer)
3039 if (outer_loop == loop)
3040 break;
3042 else
3043 outer_loop = NULL;
3045 if (loop && ! outer_loop)
3047 LABEL_OUTSIDE_LOOP_P (x) = 1;
3048 LABEL_NEXTREF (x) = loop->exit_labels;
3049 loop->exit_labels = x;
3051 for (outer_loop = loop;
3052 outer_loop && outer_loop != dest_loop;
3053 outer_loop = outer_loop->outer)
3054 outer_loop->exit_count++;
3057 /* If this is inside a loop, but not in the current loop or one enclosed
3058 by it, it invalidates at least one loop. */
3060 if (! dest_loop)
3061 return;
3063 /* We must invalidate every nested loop containing the target of this
3064 label, except those that also contain the jump insn. */
3066 for (; dest_loop; dest_loop = dest_loop->outer)
3068 /* Stop when we reach a loop that also contains the jump insn. */
3069 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3070 if (dest_loop == outer_loop)
3071 return;
3073 /* If we get here, we know we need to invalidate a loop. */
3074 if (loop_dump_stream && ! dest_loop->invalid)
3075 fprintf (loop_dump_stream,
3076 "\nLoop at %d ignored due to multiple entry points.\n",
3077 INSN_UID (dest_loop->start));
3079 dest_loop->invalid = 1;
3081 return;
3083 case SET:
3084 /* If this is not setting pc, ignore. */
3085 if (SET_DEST (x) == pc_rtx)
3086 mark_loop_jump (SET_SRC (x), loop);
3087 return;
3089 case IF_THEN_ELSE:
3090 mark_loop_jump (XEXP (x, 1), loop);
3091 mark_loop_jump (XEXP (x, 2), loop);
3092 return;
3094 case PARALLEL:
3095 case ADDR_VEC:
3096 for (i = 0; i < XVECLEN (x, 0); i++)
3097 mark_loop_jump (XVECEXP (x, 0, i), loop);
3098 return;
3100 case ADDR_DIFF_VEC:
3101 for (i = 0; i < XVECLEN (x, 1); i++)
3102 mark_loop_jump (XVECEXP (x, 1, i), loop);
3103 return;
3105 default:
3106 /* Strictly speaking this is not a jump into the loop, only a possible
3107 jump out of the loop. However, we have no way to link the destination
3108 of this jump onto the list of exit labels. To be safe we mark this
3109 loop and any containing loops as invalid. */
3110 if (loop)
3112 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3114 if (loop_dump_stream && ! outer_loop->invalid)
3115 fprintf (loop_dump_stream,
3116 "\nLoop at %d ignored due to unknown exit jump.\n",
3117 INSN_UID (outer_loop->start));
3118 outer_loop->invalid = 1;
3121 return;
3125 /* Return nonzero if there is a label in the range from
3126 insn INSN to and including the insn whose luid is END
3127 INSN must have an assigned luid (i.e., it must not have
3128 been previously created by loop.c). */
3130 static int
3131 labels_in_range_p (insn, end)
3132 rtx insn;
3133 int end;
3135 while (insn && INSN_LUID (insn) <= end)
3137 if (GET_CODE (insn) == CODE_LABEL)
3138 return 1;
3139 insn = NEXT_INSN (insn);
3142 return 0;
3145 /* Record that a memory reference X is being set. */
3147 static void
3148 note_addr_stored (x, y, data)
3149 rtx x;
3150 rtx y ATTRIBUTE_UNUSED;
3151 void *data ATTRIBUTE_UNUSED;
3153 struct loop_info *loop_info = data;
3155 if (x == 0 || GET_CODE (x) != MEM)
3156 return;
3158 /* Count number of memory writes.
3159 This affects heuristics in strength_reduce. */
3160 loop_info->num_mem_sets++;
3162 /* BLKmode MEM means all memory is clobbered. */
3163 if (GET_MODE (x) == BLKmode)
3165 if (RTX_UNCHANGING_P (x))
3166 loop_info->unknown_constant_address_altered = 1;
3167 else
3168 loop_info->unknown_address_altered = 1;
3170 return;
3173 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
3174 loop_info->store_mems);
3177 /* X is a value modified by an INSN that references a biv inside a loop
3178 exit test (ie, X is somehow related to the value of the biv). If X
3179 is a pseudo that is used more than once, then the biv is (effectively)
3180 used more than once. DATA is a pointer to a loop_regs structure. */
3182 static void
3183 note_set_pseudo_multiple_uses (x, y, data)
3184 rtx x;
3185 rtx y ATTRIBUTE_UNUSED;
3186 void *data;
3188 struct loop_regs *regs = (struct loop_regs *) data;
3190 if (x == 0)
3191 return;
3193 while (GET_CODE (x) == STRICT_LOW_PART
3194 || GET_CODE (x) == SIGN_EXTRACT
3195 || GET_CODE (x) == ZERO_EXTRACT
3196 || GET_CODE (x) == SUBREG)
3197 x = XEXP (x, 0);
3199 if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3200 return;
3202 /* If we do not have usage information, or if we know the register
3203 is used more than once, note that fact for check_dbra_loop. */
3204 if (REGNO (x) >= max_reg_before_loop
3205 || ! regs->array[REGNO (x)].single_usage
3206 || regs->array[REGNO (x)].single_usage == const0_rtx)
3207 regs->multiple_uses = 1;
3210 /* Return nonzero if the rtx X is invariant over the current loop.
3212 The value is 2 if we refer to something only conditionally invariant.
3214 A memory ref is invariant if it is not volatile and does not conflict
3215 with anything stored in `loop_info->store_mems'. */
3218 loop_invariant_p (loop, x)
3219 const struct loop *loop;
3220 rtx x;
3222 struct loop_info *loop_info = LOOP_INFO (loop);
3223 struct loop_regs *regs = LOOP_REGS (loop);
3224 int i;
3225 enum rtx_code code;
3226 const char *fmt;
3227 int conditional = 0;
3228 rtx mem_list_entry;
3230 if (x == 0)
3231 return 1;
3232 code = GET_CODE (x);
3233 switch (code)
3235 case CONST_INT:
3236 case CONST_DOUBLE:
3237 case SYMBOL_REF:
3238 case CONST:
3239 return 1;
3241 case LABEL_REF:
3242 /* A LABEL_REF is normally invariant, however, if we are unrolling
3243 loops, and this label is inside the loop, then it isn't invariant.
3244 This is because each unrolled copy of the loop body will have
3245 a copy of this label. If this was invariant, then an insn loading
3246 the address of this label into a register might get moved outside
3247 the loop, and then each loop body would end up using the same label.
3249 We don't know the loop bounds here though, so just fail for all
3250 labels. */
3251 if (flag_unroll_loops)
3252 return 0;
3253 else
3254 return 1;
3256 case PC:
3257 case CC0:
3258 case UNSPEC_VOLATILE:
3259 return 0;
3261 case REG:
3262 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3263 since the reg might be set by initialization within the loop. */
3265 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3266 || x == arg_pointer_rtx || x == pic_offset_table_rtx)
3267 && ! current_function_has_nonlocal_goto)
3268 return 1;
3270 if (LOOP_INFO (loop)->has_call
3271 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3272 return 0;
3274 if (regs->array[REGNO (x)].set_in_loop < 0)
3275 return 2;
3277 return regs->array[REGNO (x)].set_in_loop == 0;
3279 case MEM:
3280 /* Volatile memory references must be rejected. Do this before
3281 checking for read-only items, so that volatile read-only items
3282 will be rejected also. */
3283 if (MEM_VOLATILE_P (x))
3284 return 0;
3286 /* See if there is any dependence between a store and this load. */
3287 mem_list_entry = loop_info->store_mems;
3288 while (mem_list_entry)
3290 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3291 x, rtx_varies_p))
3292 return 0;
3294 mem_list_entry = XEXP (mem_list_entry, 1);
3297 /* It's not invalidated by a store in memory
3298 but we must still verify the address is invariant. */
3299 break;
3301 case ASM_OPERANDS:
3302 /* Don't mess with insns declared volatile. */
3303 if (MEM_VOLATILE_P (x))
3304 return 0;
3305 break;
3307 default:
3308 break;
3311 fmt = GET_RTX_FORMAT (code);
3312 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3314 if (fmt[i] == 'e')
3316 int tem = loop_invariant_p (loop, XEXP (x, i));
3317 if (tem == 0)
3318 return 0;
3319 if (tem == 2)
3320 conditional = 1;
3322 else if (fmt[i] == 'E')
3324 int j;
3325 for (j = 0; j < XVECLEN (x, i); j++)
3327 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3328 if (tem == 0)
3329 return 0;
3330 if (tem == 2)
3331 conditional = 1;
3337 return 1 + conditional;
3340 /* Return nonzero if all the insns in the loop that set REG
3341 are INSN and the immediately following insns,
3342 and if each of those insns sets REG in an invariant way
3343 (not counting uses of REG in them).
3345 The value is 2 if some of these insns are only conditionally invariant.
3347 We assume that INSN itself is the first set of REG
3348 and that its source is invariant. */
3350 static int
3351 consec_sets_invariant_p (loop, reg, n_sets, insn)
3352 const struct loop *loop;
3353 int n_sets;
3354 rtx reg, insn;
3356 struct loop_regs *regs = LOOP_REGS (loop);
3357 rtx p = insn;
3358 unsigned int regno = REGNO (reg);
3359 rtx temp;
3360 /* Number of sets we have to insist on finding after INSN. */
3361 int count = n_sets - 1;
3362 int old = regs->array[regno].set_in_loop;
3363 int value = 0;
3364 int this;
3366 /* If N_SETS hit the limit, we can't rely on its value. */
3367 if (n_sets == 127)
3368 return 0;
3370 regs->array[regno].set_in_loop = 0;
3372 while (count > 0)
3374 enum rtx_code code;
3375 rtx set;
3377 p = NEXT_INSN (p);
3378 code = GET_CODE (p);
3380 /* If library call, skip to end of it. */
3381 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3382 p = XEXP (temp, 0);
3384 this = 0;
3385 if (code == INSN
3386 && (set = single_set (p))
3387 && GET_CODE (SET_DEST (set)) == REG
3388 && REGNO (SET_DEST (set)) == regno)
3390 this = loop_invariant_p (loop, SET_SRC (set));
3391 if (this != 0)
3392 value |= this;
3393 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3395 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3396 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3397 notes are OK. */
3398 this = (CONSTANT_P (XEXP (temp, 0))
3399 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3400 && loop_invariant_p (loop, XEXP (temp, 0))));
3401 if (this != 0)
3402 value |= this;
3405 if (this != 0)
3406 count--;
3407 else if (code != NOTE)
3409 regs->array[regno].set_in_loop = old;
3410 return 0;
3414 regs->array[regno].set_in_loop = old;
3415 /* If loop_invariant_p ever returned 2, we return 2. */
3416 return 1 + (value & 2);
3419 #if 0
3420 /* I don't think this condition is sufficient to allow INSN
3421 to be moved, so we no longer test it. */
3423 /* Return 1 if all insns in the basic block of INSN and following INSN
3424 that set REG are invariant according to TABLE. */
3426 static int
3427 all_sets_invariant_p (reg, insn, table)
3428 rtx reg, insn;
3429 short *table;
3431 rtx p = insn;
3432 int regno = REGNO (reg);
3434 while (1)
3436 enum rtx_code code;
3437 p = NEXT_INSN (p);
3438 code = GET_CODE (p);
3439 if (code == CODE_LABEL || code == JUMP_INSN)
3440 return 1;
3441 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3442 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3443 && REGNO (SET_DEST (PATTERN (p))) == regno)
3445 if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3446 return 0;
3450 #endif /* 0 */
3452 /* Look at all uses (not sets) of registers in X. For each, if it is
3453 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3454 a different insn, set USAGE[REGNO] to const0_rtx. */
3456 static void
3457 find_single_use_in_loop (regs, insn, x)
3458 struct loop_regs *regs;
3459 rtx insn;
3460 rtx x;
3462 enum rtx_code code = GET_CODE (x);
3463 const char *fmt = GET_RTX_FORMAT (code);
3464 int i, j;
3466 if (code == REG)
3467 regs->array[REGNO (x)].single_usage
3468 = (regs->array[REGNO (x)].single_usage != 0
3469 && regs->array[REGNO (x)].single_usage != insn)
3470 ? const0_rtx : insn;
3472 else if (code == SET)
3474 /* Don't count SET_DEST if it is a REG; otherwise count things
3475 in SET_DEST because if a register is partially modified, it won't
3476 show up as a potential movable so we don't care how USAGE is set
3477 for it. */
3478 if (GET_CODE (SET_DEST (x)) != REG)
3479 find_single_use_in_loop (regs, insn, SET_DEST (x));
3480 find_single_use_in_loop (regs, insn, SET_SRC (x));
3482 else
3483 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3485 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3486 find_single_use_in_loop (regs, insn, XEXP (x, i));
3487 else if (fmt[i] == 'E')
3488 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3489 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3493 /* Count and record any set in X which is contained in INSN. Update
3494 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3495 in X. */
3497 static void
3498 count_one_set (regs, insn, x, last_set)
3499 struct loop_regs *regs;
3500 rtx insn, x;
3501 rtx *last_set;
3503 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3504 /* Don't move a reg that has an explicit clobber.
3505 It's not worth the pain to try to do it correctly. */
3506 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3508 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3510 rtx dest = SET_DEST (x);
3511 while (GET_CODE (dest) == SUBREG
3512 || GET_CODE (dest) == ZERO_EXTRACT
3513 || GET_CODE (dest) == SIGN_EXTRACT
3514 || GET_CODE (dest) == STRICT_LOW_PART)
3515 dest = XEXP (dest, 0);
3516 if (GET_CODE (dest) == REG)
3518 int i;
3519 int regno = REGNO (dest);
3520 for (i = 0; i < LOOP_REGNO_NREGS (regno, dest); i++)
3522 /* If this is the first setting of this reg
3523 in current basic block, and it was set before,
3524 it must be set in two basic blocks, so it cannot
3525 be moved out of the loop. */
3526 if (regs->array[regno].set_in_loop > 0
3527 && last_set == 0)
3528 regs->array[regno+i].may_not_optimize = 1;
3529 /* If this is not first setting in current basic block,
3530 see if reg was used in between previous one and this.
3531 If so, neither one can be moved. */
3532 if (last_set[regno] != 0
3533 && reg_used_between_p (dest, last_set[regno], insn))
3534 regs->array[regno+i].may_not_optimize = 1;
3535 if (regs->array[regno+i].set_in_loop < 127)
3536 ++regs->array[regno+i].set_in_loop;
3537 last_set[regno+i] = insn;
3543 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3544 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3545 contained in insn INSN is used by any insn that precedes INSN in
3546 cyclic order starting from the loop entry point.
3548 We don't want to use INSN_LUID here because if we restrict INSN to those
3549 that have a valid INSN_LUID, it means we cannot move an invariant out
3550 from an inner loop past two loops. */
3552 static int
3553 loop_reg_used_before_p (loop, set, insn)
3554 const struct loop *loop;
3555 rtx set, insn;
3557 rtx reg = SET_DEST (set);
3558 rtx p;
3560 /* Scan forward checking for register usage. If we hit INSN, we
3561 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3562 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3564 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3565 return 1;
3567 if (p == loop->end)
3568 p = loop->start;
3571 return 0;
3575 /* Information we collect about arrays that we might want to prefetch. */
3576 struct prefetch_info
3578 struct iv_class *class; /* Class this prefetch is based on. */
3579 struct induction *giv; /* GIV this prefetch is based on. */
3580 rtx base_address; /* Start prefetching from this address plus
3581 index. */
3582 HOST_WIDE_INT index;
3583 HOST_WIDE_INT stride; /* Prefetch stride in bytes in each
3584 iteration. */
3585 unsigned int bytes_accessed; /* Sum of sizes of all acceses to this
3586 prefetch area in one iteration. */
3587 unsigned int total_bytes; /* Total bytes loop will access in this block.
3588 This is set only for loops with known
3589 iteration counts and is 0xffffffff
3590 otherwise. */
3591 int prefetch_in_loop; /* Number of prefetch insns in loop. */
3592 int prefetch_before_loop; /* Number of prefetch insns before loop. */
3593 unsigned int write : 1; /* 1 for read/write prefetches. */
3596 /* Data used by check_store function. */
3597 struct check_store_data
3599 rtx mem_address;
3600 int mem_write;
3603 static void check_store PARAMS ((rtx, rtx, void *));
3604 static void emit_prefetch_instructions PARAMS ((struct loop *));
3605 static int rtx_equal_for_prefetch_p PARAMS ((rtx, rtx));
3607 /* Set mem_write when mem_address is found. Used as callback to
3608 note_stores. */
3609 static void
3610 check_store (x, pat, data)
3611 rtx x, pat ATTRIBUTE_UNUSED;
3612 void *data;
3614 struct check_store_data *d = (struct check_store_data *) data;
3616 if ((GET_CODE (x) == MEM) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
3617 d->mem_write = 1;
3620 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3621 important to get some addresses combined. Later more sophisticated
3622 transformations can be added when necesary.
3624 ??? Same trick with swapping operand is done at several other places.
3625 It can be nice to develop some common way to handle this. */
3627 static int
3628 rtx_equal_for_prefetch_p (x, y)
3629 rtx x, y;
3631 int i;
3632 int j;
3633 enum rtx_code code = GET_CODE (x);
3634 const char *fmt;
3636 if (x == y)
3637 return 1;
3638 if (code != GET_CODE (y))
3639 return 0;
3641 code = GET_CODE (x);
3643 if (GET_RTX_CLASS (code) == 'c')
3645 return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
3646 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
3647 || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
3648 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
3650 /* Compare the elements. If any pair of corresponding elements fails to
3651 match, return 0 for the whole thing. */
3653 fmt = GET_RTX_FORMAT (code);
3654 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3656 switch (fmt[i])
3658 case 'w':
3659 if (XWINT (x, i) != XWINT (y, i))
3660 return 0;
3661 break;
3663 case 'i':
3664 if (XINT (x, i) != XINT (y, i))
3665 return 0;
3666 break;
3668 case 'E':
3669 /* Two vectors must have the same length. */
3670 if (XVECLEN (x, i) != XVECLEN (y, i))
3671 return 0;
3673 /* And the corresponding elements must match. */
3674 for (j = 0; j < XVECLEN (x, i); j++)
3675 if (rtx_equal_for_prefetch_p (XVECEXP (x, i, j),
3676 XVECEXP (y, i, j)) == 0)
3677 return 0;
3678 break;
3680 case 'e':
3681 if (rtx_equal_for_prefetch_p (XEXP (x, i), XEXP (y, i)) == 0)
3682 return 0;
3683 break;
3685 case 's':
3686 if (strcmp (XSTR (x, i), XSTR (y, i)))
3687 return 0;
3688 break;
3690 case 'u':
3691 /* These are just backpointers, so they don't matter. */
3692 break;
3694 case '0':
3695 break;
3697 /* It is believed that rtx's at this level will never
3698 contain anything but integers and other rtx's,
3699 except for within LABEL_REFs and SYMBOL_REFs. */
3700 default:
3701 abort ();
3704 return 1;
3707 /* Remove constant addition value from the expression X (when present)
3708 and return it. */
3710 static HOST_WIDE_INT
3711 remove_constant_addition (x)
3712 rtx *x;
3714 HOST_WIDE_INT addval = 0;
3715 rtx exp = *x;
3717 /* Avoid clobbering a shared CONST expression. */
3718 if (GET_CODE (exp) == CONST)
3720 if (GET_CODE (XEXP (exp, 0)) == PLUS
3721 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == SYMBOL_REF
3722 && GET_CODE (XEXP (XEXP (exp, 0), 1)) == CONST_INT)
3724 *x = XEXP (XEXP (exp, 0), 0);
3725 return INTVAL (XEXP (XEXP (exp, 0), 1));
3727 return 0;
3730 if (GET_CODE (exp) == CONST_INT)
3732 addval = INTVAL (exp);
3733 *x = const0_rtx;
3736 /* For plus expression recurse on ourself. */
3737 else if (GET_CODE (exp) == PLUS)
3739 addval += remove_constant_addition (&XEXP (exp, 0));
3740 addval += remove_constant_addition (&XEXP (exp, 1));
3742 /* In case our parameter was constant, remove extra zero from the
3743 expression. */
3744 if (XEXP (exp, 0) == const0_rtx)
3745 *x = XEXP (exp, 1);
3746 else if (XEXP (exp, 1) == const0_rtx)
3747 *x = XEXP (exp, 0);
3750 return addval;
3753 /* Attempt to identify accesses to arrays that are most likely to cause cache
3754 misses, and emit prefetch instructions a few prefetch blocks forward.
3756 To detect the arrays we use the GIV information that was collected by the
3757 strength reduction pass.
3759 The prefetch instructions are generated after the GIV information is done
3760 and before the strength reduction process. The new GIVs are injected into
3761 the strength reduction tables, so the prefetch addresses are optimized as
3762 well.
3764 GIVs are split into base address, stride, and constant addition values.
3765 GIVs with the same address, stride and close addition values are combined
3766 into a single prefetch. Also writes to GIVs are detected, so that prefetch
3767 for write instructions can be used for the block we write to, on machines
3768 that support write prefetches.
3770 Several heuristics are used to determine when to prefetch. They are
3771 controlled by defined symbols that can be overridden for each target. */
3773 static void
3774 emit_prefetch_instructions (loop)
3775 struct loop *loop;
3777 int num_prefetches = 0;
3778 int num_real_prefetches = 0;
3779 int num_real_write_prefetches = 0;
3780 int num_prefetches_before = 0;
3781 int num_write_prefetches_before = 0;
3782 int ahead = 0;
3783 int i;
3784 struct iv_class *bl;
3785 struct induction *iv;
3786 struct prefetch_info info[MAX_PREFETCHES];
3787 struct loop_ivs *ivs = LOOP_IVS (loop);
3789 if (!HAVE_prefetch)
3790 return;
3792 /* Consider only loops w/o calls. When a call is done, the loop is probably
3793 slow enough to read the memory. */
3794 if (PREFETCH_NO_CALL && LOOP_INFO (loop)->has_call)
3796 if (loop_dump_stream)
3797 fprintf (loop_dump_stream, "Prefetch: ignoring loop: has call.\n");
3799 return;
3802 /* Don't prefetch in loops known to have few iterations. */
3803 if (PREFETCH_NO_LOW_LOOPCNT
3804 && LOOP_INFO (loop)->n_iterations
3805 && LOOP_INFO (loop)->n_iterations <= PREFETCH_LOW_LOOPCNT)
3807 if (loop_dump_stream)
3808 fprintf (loop_dump_stream,
3809 "Prefetch: ignoring loop: not enough iterations.\n");
3810 return;
3813 /* Search all induction variables and pick those interesting for the prefetch
3814 machinery. */
3815 for (bl = ivs->list; bl; bl = bl->next)
3817 struct induction *biv = bl->biv, *biv1;
3818 int basestride = 0;
3820 biv1 = biv;
3822 /* Expect all BIVs to be executed in each iteration. This makes our
3823 analysis more conservative. */
3824 while (biv1)
3826 /* Discard non-constant additions that we can't handle well yet, and
3827 BIVs that are executed multiple times; such BIVs ought to be
3828 handled in the nested loop. We accept not_every_iteration BIVs,
3829 since these only result in larger strides and make our
3830 heuristics more conservative. */
3831 if (GET_CODE (biv->add_val) != CONST_INT)
3833 if (loop_dump_stream)
3835 fprintf (loop_dump_stream,
3836 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
3837 REGNO (biv->src_reg), INSN_UID (biv->insn));
3838 print_rtl (loop_dump_stream, biv->add_val);
3839 fprintf (loop_dump_stream, "\n");
3841 break;
3844 if (biv->maybe_multiple)
3846 if (loop_dump_stream)
3848 fprintf (loop_dump_stream,
3849 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
3850 REGNO (biv->src_reg), INSN_UID (biv->insn));
3851 print_rtl (loop_dump_stream, biv->add_val);
3852 fprintf (loop_dump_stream, "\n");
3854 break;
3857 basestride += INTVAL (biv1->add_val);
3858 biv1 = biv1->next_iv;
3861 if (biv1 || !basestride)
3862 continue;
3864 for (iv = bl->giv; iv; iv = iv->next_iv)
3866 rtx address;
3867 rtx temp;
3868 HOST_WIDE_INT index = 0;
3869 int add = 1;
3870 HOST_WIDE_INT stride = 0;
3871 int stride_sign = 1;
3872 struct check_store_data d;
3873 const char *ignore_reason = NULL;
3874 int size = GET_MODE_SIZE (GET_MODE (iv));
3876 /* See whether an induction variable is interesting to us and if
3877 not, report the reason. */
3878 if (iv->giv_type != DEST_ADDR)
3879 ignore_reason = "giv is not a destination address";
3881 /* We are interested only in constant stride memory references
3882 in order to be able to compute density easily. */
3883 else if (GET_CODE (iv->mult_val) != CONST_INT)
3884 ignore_reason = "stride is not constant";
3886 else
3888 stride = INTVAL (iv->mult_val) * basestride;
3889 if (stride < 0)
3891 stride = -stride;
3892 stride_sign = -1;
3895 /* On some targets, reversed order prefetches are not
3896 worthwhile. */
3897 if (PREFETCH_NO_REVERSE_ORDER && stride_sign < 0)
3898 ignore_reason = "reversed order stride";
3900 /* Prefetch of accesses with an extreme stride might not be
3901 worthwhile, either. */
3902 else if (PREFETCH_NO_EXTREME_STRIDE
3903 && stride > PREFETCH_EXTREME_STRIDE)
3904 ignore_reason = "extreme stride";
3906 /* Ignore GIVs with varying add values; we can't predict the
3907 value for the next iteration. */
3908 else if (!loop_invariant_p (loop, iv->add_val))
3909 ignore_reason = "giv has varying add value";
3911 /* Ignore GIVs in the nested loops; they ought to have been
3912 handled already. */
3913 else if (iv->maybe_multiple)
3914 ignore_reason = "giv is in nested loop";
3917 if (ignore_reason != NULL)
3919 if (loop_dump_stream)
3920 fprintf (loop_dump_stream,
3921 "Prefetch: ignoring giv at %d: %s.\n",
3922 INSN_UID (iv->insn), ignore_reason);
3923 continue;
3926 /* Determine the pointer to the basic array we are examining. It is
3927 the sum of the BIV's initial value and the GIV's add_val. */
3928 address = copy_rtx (iv->add_val);
3929 temp = copy_rtx (bl->initial_value);
3931 address = simplify_gen_binary (PLUS, Pmode, temp, address);
3932 index = remove_constant_addition (&address);
3934 d.mem_write = 0;
3935 d.mem_address = *iv->location;
3937 /* When the GIV is not always executed, we might be better off by
3938 not dirtying the cache pages. */
3939 if (PREFETCH_CONDITIONAL || iv->always_executed)
3940 note_stores (PATTERN (iv->insn), check_store, &d);
3941 else
3943 if (loop_dump_stream)
3944 fprintf (loop_dump_stream, "Prefetch: Ignoring giv at %d: %s\n",
3945 INSN_UID (iv->insn), "in conditional code.");
3946 continue;
3949 /* Attempt to find another prefetch to the same array and see if we
3950 can merge this one. */
3951 for (i = 0; i < num_prefetches; i++)
3952 if (rtx_equal_for_prefetch_p (address, info[i].base_address)
3953 && stride == info[i].stride)
3955 /* In case both access same array (same location
3956 just with small difference in constant indexes), merge
3957 the prefetches. Just do the later and the earlier will
3958 get prefetched from previous iteration.
3959 The artificial threshold should not be too small,
3960 but also not bigger than small portion of memory usually
3961 traversed by single loop. */
3962 if (index >= info[i].index
3963 && index - info[i].index < PREFETCH_EXTREME_DIFFERENCE)
3965 info[i].write |= d.mem_write;
3966 info[i].bytes_accessed += size;
3967 info[i].index = index;
3968 info[i].giv = iv;
3969 info[i].class = bl;
3970 info[num_prefetches].base_address = address;
3971 add = 0;
3972 break;
3975 if (index < info[i].index
3976 && info[i].index - index < PREFETCH_EXTREME_DIFFERENCE)
3978 info[i].write |= d.mem_write;
3979 info[i].bytes_accessed += size;
3980 add = 0;
3981 break;
3985 /* Merging failed. */
3986 if (add)
3988 info[num_prefetches].giv = iv;
3989 info[num_prefetches].class = bl;
3990 info[num_prefetches].index = index;
3991 info[num_prefetches].stride = stride;
3992 info[num_prefetches].base_address = address;
3993 info[num_prefetches].write = d.mem_write;
3994 info[num_prefetches].bytes_accessed = size;
3995 num_prefetches++;
3996 if (num_prefetches >= MAX_PREFETCHES)
3998 if (loop_dump_stream)
3999 fprintf (loop_dump_stream,
4000 "Maximal number of prefetches exceeded.\n");
4001 return;
4007 for (i = 0; i < num_prefetches; i++)
4009 int density;
4011 /* Attempt to calculate the total number of bytes fetched by all
4012 iterations of the loop. Avoid overflow. */
4013 if (LOOP_INFO (loop)->n_iterations
4014 && ((unsigned HOST_WIDE_INT) (0xffffffff / info[i].stride)
4015 >= LOOP_INFO (loop)->n_iterations))
4016 info[i].total_bytes = info[i].stride * LOOP_INFO (loop)->n_iterations;
4017 else
4018 info[i].total_bytes = 0xffffffff;
4020 density = info[i].bytes_accessed * 100 / info[i].stride;
4022 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4023 if (PREFETCH_ONLY_DENSE_MEM)
4024 if (density * 256 > PREFETCH_DENSE_MEM * 100
4025 && (info[i].total_bytes / PREFETCH_BLOCK
4026 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN))
4028 info[i].prefetch_before_loop = 1;
4029 info[i].prefetch_in_loop
4030 = (info[i].total_bytes / PREFETCH_BLOCK
4031 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX);
4033 else
4035 info[i].prefetch_in_loop = 0, info[i].prefetch_before_loop = 0;
4036 if (loop_dump_stream)
4037 fprintf (loop_dump_stream,
4038 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4039 INSN_UID (info[i].giv->insn), density);
4041 else
4042 info[i].prefetch_in_loop = 1, info[i].prefetch_before_loop = 1;
4044 /* Find how many prefetch instructions we'll use within the loop. */
4045 if (info[i].prefetch_in_loop != 0)
4047 info[i].prefetch_in_loop = ((info[i].stride + PREFETCH_BLOCK - 1)
4048 / PREFETCH_BLOCK);
4049 num_real_prefetches += info[i].prefetch_in_loop;
4050 if (info[i].write)
4051 num_real_write_prefetches += info[i].prefetch_in_loop;
4055 /* Determine how many iterations ahead to prefetch within the loop, based
4056 on how many prefetches we currently expect to do within the loop. */
4057 if (num_real_prefetches != 0)
4059 if ((ahead = SIMULTANEOUS_PREFETCHES / num_real_prefetches) == 0)
4061 if (loop_dump_stream)
4062 fprintf (loop_dump_stream,
4063 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4064 SIMULTANEOUS_PREFETCHES, num_real_prefetches);
4065 num_real_prefetches = 0, num_real_write_prefetches = 0;
4068 /* We'll also use AHEAD to determine how many prefetch instructions to
4069 emit before a loop, so don't leave it zero. */
4070 if (ahead == 0)
4071 ahead = PREFETCH_BLOCKS_BEFORE_LOOP_MAX;
4073 for (i = 0; i < num_prefetches; i++)
4075 /* Update if we've decided not to prefetch anything within the loop. */
4076 if (num_real_prefetches == 0)
4077 info[i].prefetch_in_loop = 0;
4079 /* Find how many prefetch instructions we'll use before the loop. */
4080 if (info[i].prefetch_before_loop != 0)
4082 int n = info[i].total_bytes / PREFETCH_BLOCK;
4083 if (n > ahead)
4084 n = ahead;
4085 info[i].prefetch_before_loop = n;
4086 num_prefetches_before += n;
4087 if (info[i].write)
4088 num_write_prefetches_before += n;
4091 if (loop_dump_stream)
4093 if (info[i].prefetch_in_loop == 0
4094 && info[i].prefetch_before_loop == 0)
4095 continue;
4096 fprintf (loop_dump_stream, "Prefetch insn: %d",
4097 INSN_UID (info[i].giv->insn));
4098 fprintf (loop_dump_stream,
4099 "; in loop: %d; before: %d; %s\n",
4100 info[i].prefetch_in_loop,
4101 info[i].prefetch_before_loop,
4102 info[i].write ? "read/write" : "read only");
4103 fprintf (loop_dump_stream,
4104 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4105 (int) (info[i].bytes_accessed * 100 / info[i].stride),
4106 info[i].bytes_accessed, info[i].total_bytes);
4107 fprintf (loop_dump_stream, " index: ");
4108 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].index);
4109 fprintf (loop_dump_stream, "; stride: ");
4110 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].stride);
4111 fprintf (loop_dump_stream, "; address: ");
4112 print_rtl (loop_dump_stream, info[i].base_address);
4113 fprintf (loop_dump_stream, "\n");
4117 if (num_real_prefetches + num_prefetches_before > 0)
4119 /* Record that this loop uses prefetch instructions. */
4120 LOOP_INFO (loop)->has_prefetch = 1;
4122 if (loop_dump_stream)
4124 fprintf (loop_dump_stream, "Real prefetches needed within loop: %d (write: %d)\n",
4125 num_real_prefetches, num_real_write_prefetches);
4126 fprintf (loop_dump_stream, "Real prefetches needed before loop: %d (write: %d)\n",
4127 num_prefetches_before, num_write_prefetches_before);
4131 for (i = 0; i < num_prefetches; i++)
4133 int y;
4135 for (y = 0; y < info[i].prefetch_in_loop; y++)
4137 rtx loc = copy_rtx (*info[i].giv->location);
4138 rtx insn;
4139 int bytes_ahead = PREFETCH_BLOCK * (ahead + y);
4140 rtx before_insn = info[i].giv->insn;
4141 rtx prev_insn = PREV_INSN (info[i].giv->insn);
4143 /* We can save some effort by offsetting the address on
4144 architectures with offsettable memory references. */
4145 if (offsettable_address_p (0, VOIDmode, loc))
4146 loc = plus_constant (loc, bytes_ahead);
4147 else
4149 rtx reg = gen_reg_rtx (Pmode);
4150 loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
4151 GEN_INT (bytes_ahead), reg,
4152 0, before_insn);
4153 loc = reg;
4156 /* Make sure the address operand is valid for prefetch. */
4157 if (! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
4158 (loc, insn_data[(int)CODE_FOR_prefetch].operand[0].mode))
4159 loc = force_reg (Pmode, loc);
4160 emit_insn_before (gen_prefetch (loc, GEN_INT (info[i].write),
4161 GEN_INT (3)),
4162 before_insn);
4164 /* Check all insns emitted and record the new GIV
4165 information. */
4166 insn = NEXT_INSN (prev_insn);
4167 while (insn != before_insn)
4169 insn = check_insn_for_givs (loop, insn,
4170 info[i].giv->always_executed,
4171 info[i].giv->maybe_multiple);
4172 insn = NEXT_INSN (insn);
4176 if (PREFETCH_BEFORE_LOOP)
4178 /* Emit insns before the loop to fetch the first cache lines or,
4179 if we're not prefetching within the loop, everything we expect
4180 to need. */
4181 for (y = 0; y < info[i].prefetch_before_loop; y++)
4183 rtx reg = gen_reg_rtx (Pmode);
4184 rtx loop_start = loop->start;
4185 rtx init_val = info[i].class->initial_value;
4186 rtx add_val = simplify_gen_binary (PLUS, Pmode,
4187 info[i].giv->add_val,
4188 GEN_INT (y * PREFETCH_BLOCK));
4190 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4191 non-constant INIT_VAL to have the same mode as REG, which
4192 in this case we know to be Pmode. */
4193 if (GET_MODE (init_val) != Pmode && !CONSTANT_P (init_val))
4194 init_val = convert_to_mode (Pmode, init_val, 0);
4195 loop_iv_add_mult_emit_before (loop, init_val,
4196 info[i].giv->mult_val,
4197 add_val, reg, 0, loop_start);
4198 emit_insn_before (gen_prefetch (reg, GEN_INT (info[i].write),
4199 GEN_INT (3)),
4200 loop_start);
4205 return;
4208 /* A "basic induction variable" or biv is a pseudo reg that is set
4209 (within this loop) only by incrementing or decrementing it. */
4210 /* A "general induction variable" or giv is a pseudo reg whose
4211 value is a linear function of a biv. */
4213 /* Bivs are recognized by `basic_induction_var';
4214 Givs by `general_induction_var'. */
4216 /* Communication with routines called via `note_stores'. */
4218 static rtx note_insn;
4220 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
4222 static rtx addr_placeholder;
4224 /* ??? Unfinished optimizations, and possible future optimizations,
4225 for the strength reduction code. */
4227 /* ??? The interaction of biv elimination, and recognition of 'constant'
4228 bivs, may cause problems. */
4230 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4231 performance problems.
4233 Perhaps don't eliminate things that can be combined with an addressing
4234 mode. Find all givs that have the same biv, mult_val, and add_val;
4235 then for each giv, check to see if its only use dies in a following
4236 memory address. If so, generate a new memory address and check to see
4237 if it is valid. If it is valid, then store the modified memory address,
4238 otherwise, mark the giv as not done so that it will get its own iv. */
4240 /* ??? Could try to optimize branches when it is known that a biv is always
4241 positive. */
4243 /* ??? When replace a biv in a compare insn, we should replace with closest
4244 giv so that an optimized branch can still be recognized by the combiner,
4245 e.g. the VAX acb insn. */
4247 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4248 was rerun in loop_optimize whenever a register was added or moved.
4249 Also, some of the optimizations could be a little less conservative. */
4251 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4252 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4253 callback.
4255 NOT_EVERY_ITERATION if current insn is not executed at least once for every
4256 loop iteration except for the last one.
4258 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4259 loop iteration.
4261 void
4262 for_each_insn_in_loop (loop, fncall)
4263 struct loop *loop;
4264 loop_insn_callback fncall;
4266 /* This is 1 if current insn is not executed at least once for every loop
4267 iteration. */
4268 int not_every_iteration = 0;
4269 int maybe_multiple = 0;
4270 int past_loop_latch = 0;
4271 int loop_depth = 0;
4272 rtx p;
4274 /* If loop_scan_start points to the loop exit test, we have to be wary of
4275 subversive use of gotos inside expression statements. */
4276 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
4277 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
4279 /* Scan through loop to find all possible bivs. */
4281 for (p = next_insn_in_loop (loop, loop->scan_start);
4282 p != NULL_RTX;
4283 p = next_insn_in_loop (loop, p))
4285 p = fncall (loop, p, not_every_iteration, maybe_multiple);
4287 /* Past CODE_LABEL, we get to insns that may be executed multiple
4288 times. The only way we can be sure that they can't is if every
4289 jump insn between here and the end of the loop either
4290 returns, exits the loop, is a jump to a location that is still
4291 behind the label, or is a jump to the loop start. */
4293 if (GET_CODE (p) == CODE_LABEL)
4295 rtx insn = p;
4297 maybe_multiple = 0;
4299 while (1)
4301 insn = NEXT_INSN (insn);
4302 if (insn == loop->scan_start)
4303 break;
4304 if (insn == loop->end)
4306 if (loop->top != 0)
4307 insn = loop->top;
4308 else
4309 break;
4310 if (insn == loop->scan_start)
4311 break;
4314 if (GET_CODE (insn) == JUMP_INSN
4315 && GET_CODE (PATTERN (insn)) != RETURN
4316 && (!any_condjump_p (insn)
4317 || (JUMP_LABEL (insn) != 0
4318 && JUMP_LABEL (insn) != loop->scan_start
4319 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
4321 maybe_multiple = 1;
4322 break;
4327 /* Past a jump, we get to insns for which we can't count
4328 on whether they will be executed during each iteration. */
4329 /* This code appears twice in strength_reduce. There is also similar
4330 code in scan_loop. */
4331 if (GET_CODE (p) == JUMP_INSN
4332 /* If we enter the loop in the middle, and scan around to the
4333 beginning, don't set not_every_iteration for that.
4334 This can be any kind of jump, since we want to know if insns
4335 will be executed if the loop is executed. */
4336 && !(JUMP_LABEL (p) == loop->top
4337 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
4338 && any_uncondjump_p (p))
4339 || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
4341 rtx label = 0;
4343 /* If this is a jump outside the loop, then it also doesn't
4344 matter. Check to see if the target of this branch is on the
4345 loop->exits_labels list. */
4347 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
4348 if (XEXP (label, 0) == JUMP_LABEL (p))
4349 break;
4351 if (!label)
4352 not_every_iteration = 1;
4355 else if (GET_CODE (p) == NOTE)
4357 /* At the virtual top of a converted loop, insns are again known to
4358 be executed each iteration: logically, the loop begins here
4359 even though the exit code has been duplicated.
4361 Insns are also again known to be executed each iteration at
4362 the LOOP_CONT note. */
4363 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
4364 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
4365 && loop_depth == 0)
4366 not_every_iteration = 0;
4367 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
4368 loop_depth++;
4369 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
4370 loop_depth--;
4373 /* Note if we pass a loop latch. If we do, then we can not clear
4374 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4375 a loop since a jump before the last CODE_LABEL may have started
4376 a new loop iteration.
4378 Note that LOOP_TOP is only set for rotated loops and we need
4379 this check for all loops, so compare against the CODE_LABEL
4380 which immediately follows LOOP_START. */
4381 if (GET_CODE (p) == JUMP_INSN
4382 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
4383 past_loop_latch = 1;
4385 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4386 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4387 or not an insn is known to be executed each iteration of the
4388 loop, whether or not any iterations are known to occur.
4390 Therefore, if we have just passed a label and have no more labels
4391 between here and the test insn of the loop, and we have not passed
4392 a jump to the top of the loop, then we know these insns will be
4393 executed each iteration. */
4395 if (not_every_iteration
4396 && !past_loop_latch
4397 && GET_CODE (p) == CODE_LABEL
4398 && no_labels_between_p (p, loop->end)
4399 && loop_insn_first_p (p, loop->cont))
4400 not_every_iteration = 0;
4404 static void
4405 loop_bivs_find (loop)
4406 struct loop *loop;
4408 struct loop_regs *regs = LOOP_REGS (loop);
4409 struct loop_ivs *ivs = LOOP_IVS (loop);
4410 /* Temporary list pointers for traversing ivs->list. */
4411 struct iv_class *bl, **backbl;
4413 ivs->list = 0;
4415 for_each_insn_in_loop (loop, check_insn_for_bivs);
4417 /* Scan ivs->list to remove all regs that proved not to be bivs.
4418 Make a sanity check against regs->n_times_set. */
4419 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
4421 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4422 /* Above happens if register modified by subreg, etc. */
4423 /* Make sure it is not recognized as a basic induction var: */
4424 || regs->array[bl->regno].n_times_set != bl->biv_count
4425 /* If never incremented, it is invariant that we decided not to
4426 move. So leave it alone. */
4427 || ! bl->incremented)
4429 if (loop_dump_stream)
4430 fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
4431 bl->regno,
4432 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4433 ? "not induction variable"
4434 : (! bl->incremented ? "never incremented"
4435 : "count error")));
4437 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
4438 *backbl = bl->next;
4440 else
4442 backbl = &bl->next;
4444 if (loop_dump_stream)
4445 fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
4451 /* Determine how BIVS are initialised by looking through pre-header
4452 extended basic block. */
4453 static void
4454 loop_bivs_init_find (loop)
4455 struct loop *loop;
4457 struct loop_ivs *ivs = LOOP_IVS (loop);
4458 /* Temporary list pointers for traversing ivs->list. */
4459 struct iv_class *bl;
4460 int call_seen;
4461 rtx p;
4463 /* Find initial value for each biv by searching backwards from loop_start,
4464 halting at first label. Also record any test condition. */
4466 call_seen = 0;
4467 for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
4469 rtx test;
4471 note_insn = p;
4473 if (GET_CODE (p) == CALL_INSN)
4474 call_seen = 1;
4476 if (INSN_P (p))
4477 note_stores (PATTERN (p), record_initial, ivs);
4479 /* Record any test of a biv that branches around the loop if no store
4480 between it and the start of loop. We only care about tests with
4481 constants and registers and only certain of those. */
4482 if (GET_CODE (p) == JUMP_INSN
4483 && JUMP_LABEL (p) != 0
4484 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
4485 && (test = get_condition_for_loop (loop, p)) != 0
4486 && GET_CODE (XEXP (test, 0)) == REG
4487 && REGNO (XEXP (test, 0)) < max_reg_before_loop
4488 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
4489 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
4490 && bl->init_insn == 0)
4492 /* If an NE test, we have an initial value! */
4493 if (GET_CODE (test) == NE)
4495 bl->init_insn = p;
4496 bl->init_set = gen_rtx_SET (VOIDmode,
4497 XEXP (test, 0), XEXP (test, 1));
4499 else
4500 bl->initial_test = test;
4506 /* Look at the each biv and see if we can say anything better about its
4507 initial value from any initializing insns set up above. (This is done
4508 in two passes to avoid missing SETs in a PARALLEL.) */
4509 static void
4510 loop_bivs_check (loop)
4511 struct loop *loop;
4513 struct loop_ivs *ivs = LOOP_IVS (loop);
4514 /* Temporary list pointers for traversing ivs->list. */
4515 struct iv_class *bl;
4516 struct iv_class **backbl;
4518 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
4520 rtx src;
4521 rtx note;
4523 if (! bl->init_insn)
4524 continue;
4526 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4527 is a constant, use the value of that. */
4528 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4529 && CONSTANT_P (XEXP (note, 0)))
4530 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4531 && CONSTANT_P (XEXP (note, 0))))
4532 src = XEXP (note, 0);
4533 else
4534 src = SET_SRC (bl->init_set);
4536 if (loop_dump_stream)
4537 fprintf (loop_dump_stream,
4538 "Biv %d: initialized at insn %d: initial value ",
4539 bl->regno, INSN_UID (bl->init_insn));
4541 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4542 || GET_MODE (src) == VOIDmode)
4543 && valid_initial_value_p (src, bl->init_insn,
4544 LOOP_INFO (loop)->pre_header_has_call,
4545 loop->start))
4547 bl->initial_value = src;
4549 if (loop_dump_stream)
4551 print_simple_rtl (loop_dump_stream, src);
4552 fputc ('\n', loop_dump_stream);
4555 /* If we can't make it a giv,
4556 let biv keep initial value of "itself". */
4557 else if (loop_dump_stream)
4558 fprintf (loop_dump_stream, "is complex\n");
4563 /* Search the loop for general induction variables. */
4565 static void
4566 loop_givs_find (loop)
4567 struct loop* loop;
4569 for_each_insn_in_loop (loop, check_insn_for_givs);
4573 /* For each giv for which we still don't know whether or not it is
4574 replaceable, check to see if it is replaceable because its final value
4575 can be calculated. */
4577 static void
4578 loop_givs_check (loop)
4579 struct loop *loop;
4581 struct loop_ivs *ivs = LOOP_IVS (loop);
4582 struct iv_class *bl;
4584 for (bl = ivs->list; bl; bl = bl->next)
4586 struct induction *v;
4588 for (v = bl->giv; v; v = v->next_iv)
4589 if (! v->replaceable && ! v->not_replaceable)
4590 check_final_value (loop, v);
4595 /* Return non-zero if it is possible to eliminate the biv BL provided
4596 all givs are reduced. This is possible if either the reg is not
4597 used outside the loop, or we can compute what its final value will
4598 be. */
4600 static int
4601 loop_biv_eliminable_p (loop, bl, threshold, insn_count)
4602 struct loop *loop;
4603 struct iv_class *bl;
4604 int threshold;
4605 int insn_count;
4607 /* For architectures with a decrement_and_branch_until_zero insn,
4608 don't do this if we put a REG_NONNEG note on the endtest for this
4609 biv. */
4611 #ifdef HAVE_decrement_and_branch_until_zero
4612 if (bl->nonneg)
4614 if (loop_dump_stream)
4615 fprintf (loop_dump_stream,
4616 "Cannot eliminate nonneg biv %d.\n", bl->regno);
4617 return 0;
4619 #endif
4621 /* Check that biv is used outside loop or if it has a final value.
4622 Compare against bl->init_insn rather than loop->start. We aren't
4623 concerned with any uses of the biv between init_insn and
4624 loop->start since these won't be affected by the value of the biv
4625 elsewhere in the function, so long as init_insn doesn't use the
4626 biv itself. */
4628 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
4629 && bl->init_insn
4630 && INSN_UID (bl->init_insn) < max_uid_for_loop
4631 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
4632 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4633 || (bl->final_value = final_biv_value (loop, bl)))
4634 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
4636 if (loop_dump_stream)
4638 fprintf (loop_dump_stream,
4639 "Cannot eliminate biv %d.\n",
4640 bl->regno);
4641 fprintf (loop_dump_stream,
4642 "First use: insn %d, last use: insn %d.\n",
4643 REGNO_FIRST_UID (bl->regno),
4644 REGNO_LAST_UID (bl->regno));
4646 return 0;
4650 /* Reduce each giv of BL that we have decided to reduce. */
4652 static void
4653 loop_givs_reduce (loop, bl)
4654 struct loop *loop;
4655 struct iv_class *bl;
4657 struct induction *v;
4659 for (v = bl->giv; v; v = v->next_iv)
4661 struct induction *tv;
4662 if (! v->ignore && v->same == 0)
4664 int auto_inc_opt = 0;
4666 /* If the code for derived givs immediately below has already
4667 allocated a new_reg, we must keep it. */
4668 if (! v->new_reg)
4669 v->new_reg = gen_reg_rtx (v->mode);
4671 #ifdef AUTO_INC_DEC
4672 /* If the target has auto-increment addressing modes, and
4673 this is an address giv, then try to put the increment
4674 immediately after its use, so that flow can create an
4675 auto-increment addressing mode. */
4676 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4677 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4678 /* We don't handle reversed biv's because bl->biv->insn
4679 does not have a valid INSN_LUID. */
4680 && ! bl->reversed
4681 && v->always_executed && ! v->maybe_multiple
4682 && INSN_UID (v->insn) < max_uid_for_loop)
4684 /* If other giv's have been combined with this one, then
4685 this will work only if all uses of the other giv's occur
4686 before this giv's insn. This is difficult to check.
4688 We simplify this by looking for the common case where
4689 there is one DEST_REG giv, and this giv's insn is the
4690 last use of the dest_reg of that DEST_REG giv. If the
4691 increment occurs after the address giv, then we can
4692 perform the optimization. (Otherwise, the increment
4693 would have to go before other_giv, and we would not be
4694 able to combine it with the address giv to get an
4695 auto-inc address.) */
4696 if (v->combined_with)
4698 struct induction *other_giv = 0;
4700 for (tv = bl->giv; tv; tv = tv->next_iv)
4701 if (tv->same == v)
4703 if (other_giv)
4704 break;
4705 else
4706 other_giv = tv;
4708 if (! tv && other_giv
4709 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4710 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4711 == INSN_UID (v->insn))
4712 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4713 auto_inc_opt = 1;
4715 /* Check for case where increment is before the address
4716 giv. Do this test in "loop order". */
4717 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4718 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4719 || (INSN_LUID (bl->biv->insn)
4720 > INSN_LUID (loop->scan_start))))
4721 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4722 && (INSN_LUID (loop->scan_start)
4723 < INSN_LUID (bl->biv->insn))))
4724 auto_inc_opt = -1;
4725 else
4726 auto_inc_opt = 1;
4728 #ifdef HAVE_cc0
4730 rtx prev;
4732 /* We can't put an insn immediately after one setting
4733 cc0, or immediately before one using cc0. */
4734 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4735 || (auto_inc_opt == -1
4736 && (prev = prev_nonnote_insn (v->insn)) != 0
4737 && INSN_P (prev)
4738 && sets_cc0_p (PATTERN (prev))))
4739 auto_inc_opt = 0;
4741 #endif
4743 if (auto_inc_opt)
4744 v->auto_inc_opt = 1;
4746 #endif
4748 /* For each place where the biv is incremented, add an insn
4749 to increment the new, reduced reg for the giv. */
4750 for (tv = bl->biv; tv; tv = tv->next_iv)
4752 rtx insert_before;
4754 if (! auto_inc_opt)
4755 insert_before = tv->insn;
4756 else if (auto_inc_opt == 1)
4757 insert_before = NEXT_INSN (v->insn);
4758 else
4759 insert_before = v->insn;
4761 if (tv->mult_val == const1_rtx)
4762 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4763 v->new_reg, v->new_reg,
4764 0, insert_before);
4765 else /* tv->mult_val == const0_rtx */
4766 /* A multiply is acceptable here
4767 since this is presumed to be seldom executed. */
4768 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4769 v->add_val, v->new_reg,
4770 0, insert_before);
4773 /* Add code at loop start to initialize giv's reduced reg. */
4775 loop_iv_add_mult_hoist (loop,
4776 extend_value_for_giv (v, bl->initial_value),
4777 v->mult_val, v->add_val, v->new_reg);
4783 /* Check for givs whose first use is their definition and whose
4784 last use is the definition of another giv. If so, it is likely
4785 dead and should not be used to derive another giv nor to
4786 eliminate a biv. */
4788 static void
4789 loop_givs_dead_check (loop, bl)
4790 struct loop *loop ATTRIBUTE_UNUSED;
4791 struct iv_class *bl;
4793 struct induction *v;
4795 for (v = bl->giv; v; v = v->next_iv)
4797 if (v->ignore
4798 || (v->same && v->same->ignore))
4799 continue;
4801 if (v->giv_type == DEST_REG
4802 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4804 struct induction *v1;
4806 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4807 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4808 v->maybe_dead = 1;
4814 static void
4815 loop_givs_rescan (loop, bl, reg_map)
4816 struct loop *loop;
4817 struct iv_class *bl;
4818 rtx *reg_map;
4820 struct induction *v;
4822 for (v = bl->giv; v; v = v->next_iv)
4824 if (v->same && v->same->ignore)
4825 v->ignore = 1;
4827 if (v->ignore)
4828 continue;
4830 /* Update expression if this was combined, in case other giv was
4831 replaced. */
4832 if (v->same)
4833 v->new_reg = replace_rtx (v->new_reg,
4834 v->same->dest_reg, v->same->new_reg);
4836 /* See if this register is known to be a pointer to something. If
4837 so, see if we can find the alignment. First see if there is a
4838 destination register that is a pointer. If so, this shares the
4839 alignment too. Next see if we can deduce anything from the
4840 computational information. If not, and this is a DEST_ADDR
4841 giv, at least we know that it's a pointer, though we don't know
4842 the alignment. */
4843 if (GET_CODE (v->new_reg) == REG
4844 && v->giv_type == DEST_REG
4845 && REG_POINTER (v->dest_reg))
4846 mark_reg_pointer (v->new_reg,
4847 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
4848 else if (GET_CODE (v->new_reg) == REG
4849 && REG_POINTER (v->src_reg))
4851 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
4853 if (align == 0
4854 || GET_CODE (v->add_val) != CONST_INT
4855 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
4856 align = 0;
4858 mark_reg_pointer (v->new_reg, align);
4860 else if (GET_CODE (v->new_reg) == REG
4861 && GET_CODE (v->add_val) == REG
4862 && REG_POINTER (v->add_val))
4864 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
4866 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
4867 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
4868 align = 0;
4870 mark_reg_pointer (v->new_reg, align);
4872 else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
4873 mark_reg_pointer (v->new_reg, 0);
4875 if (v->giv_type == DEST_ADDR)
4876 /* Store reduced reg as the address in the memref where we found
4877 this giv. */
4878 validate_change (v->insn, v->location, v->new_reg, 0);
4879 else if (v->replaceable)
4881 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4883 else
4885 /* Not replaceable; emit an insn to set the original giv reg from
4886 the reduced giv, same as above. */
4887 loop_insn_emit_after (loop, 0, v->insn,
4888 gen_move_insn (v->dest_reg, v->new_reg));
4891 /* When a loop is reversed, givs which depend on the reversed
4892 biv, and which are live outside the loop, must be set to their
4893 correct final value. This insn is only needed if the giv is
4894 not replaceable. The correct final value is the same as the
4895 value that the giv starts the reversed loop with. */
4896 if (bl->reversed && ! v->replaceable)
4897 loop_iv_add_mult_sink (loop,
4898 extend_value_for_giv (v, bl->initial_value),
4899 v->mult_val, v->add_val, v->dest_reg);
4900 else if (v->final_value)
4901 loop_insn_sink_or_swim (loop,
4902 gen_load_of_final_value (v->dest_reg,
4903 v->final_value));
4905 if (loop_dump_stream)
4907 fprintf (loop_dump_stream, "giv at %d reduced to ",
4908 INSN_UID (v->insn));
4909 print_simple_rtl (loop_dump_stream, v->new_reg);
4910 fprintf (loop_dump_stream, "\n");
4916 static int
4917 loop_giv_reduce_benefit (loop, bl, v, test_reg)
4918 struct loop *loop ATTRIBUTE_UNUSED;
4919 struct iv_class *bl;
4920 struct induction *v;
4921 rtx test_reg;
4923 int add_cost;
4924 int benefit;
4926 benefit = v->benefit;
4927 PUT_MODE (test_reg, v->mode);
4928 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
4929 test_reg, test_reg);
4931 /* Reduce benefit if not replaceable, since we will insert a
4932 move-insn to replace the insn that calculates this giv. Don't do
4933 this unless the giv is a user variable, since it will often be
4934 marked non-replaceable because of the duplication of the exit
4935 code outside the loop. In such a case, the copies we insert are
4936 dead and will be deleted. So they don't have a cost. Similar
4937 situations exist. */
4938 /* ??? The new final_[bg]iv_value code does a much better job of
4939 finding replaceable giv's, and hence this code may no longer be
4940 necessary. */
4941 if (! v->replaceable && ! bl->eliminable
4942 && REG_USERVAR_P (v->dest_reg))
4943 benefit -= copy_cost;
4945 /* Decrease the benefit to count the add-insns that we will insert
4946 to increment the reduced reg for the giv. ??? This can
4947 overestimate the run-time cost of the additional insns, e.g. if
4948 there are multiple basic blocks that increment the biv, but only
4949 one of these blocks is executed during each iteration. There is
4950 no good way to detect cases like this with the current structure
4951 of the loop optimizer. This code is more accurate for
4952 determining code size than run-time benefits. */
4953 benefit -= add_cost * bl->biv_count;
4955 /* Decide whether to strength-reduce this giv or to leave the code
4956 unchanged (recompute it from the biv each time it is used). This
4957 decision can be made independently for each giv. */
4959 #ifdef AUTO_INC_DEC
4960 /* Attempt to guess whether autoincrement will handle some of the
4961 new add insns; if so, increase BENEFIT (undo the subtraction of
4962 add_cost that was done above). */
4963 if (v->giv_type == DEST_ADDR
4964 /* Increasing the benefit is risky, since this is only a guess.
4965 Avoid increasing register pressure in cases where there would
4966 be no other benefit from reducing this giv. */
4967 && benefit > 0
4968 && GET_CODE (v->mult_val) == CONST_INT)
4970 int size = GET_MODE_SIZE (GET_MODE (v->mem));
4972 if (HAVE_POST_INCREMENT
4973 && INTVAL (v->mult_val) == size)
4974 benefit += add_cost * bl->biv_count;
4975 else if (HAVE_PRE_INCREMENT
4976 && INTVAL (v->mult_val) == size)
4977 benefit += add_cost * bl->biv_count;
4978 else if (HAVE_POST_DECREMENT
4979 && -INTVAL (v->mult_val) == size)
4980 benefit += add_cost * bl->biv_count;
4981 else if (HAVE_PRE_DECREMENT
4982 && -INTVAL (v->mult_val) == size)
4983 benefit += add_cost * bl->biv_count;
4985 #endif
4987 return benefit;
4991 /* Free IV structures for LOOP. */
4993 static void
4994 loop_ivs_free (loop)
4995 struct loop *loop;
4997 struct loop_ivs *ivs = LOOP_IVS (loop);
4998 struct iv_class *iv = ivs->list;
5000 free (ivs->regs);
5002 while (iv)
5004 struct iv_class *next = iv->next;
5005 struct induction *induction;
5006 struct induction *next_induction;
5008 for (induction = iv->biv; induction; induction = next_induction)
5010 next_induction = induction->next_iv;
5011 free (induction);
5013 for (induction = iv->giv; induction; induction = next_induction)
5015 next_induction = induction->next_iv;
5016 free (induction);
5019 free (iv);
5020 iv = next;
5025 /* Perform strength reduction and induction variable elimination.
5027 Pseudo registers created during this function will be beyond the
5028 last valid index in several tables including
5029 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
5030 problem here, because the added registers cannot be givs outside of
5031 their loop, and hence will never be reconsidered. But scan_loop
5032 must check regnos to make sure they are in bounds. */
5034 static void
5035 strength_reduce (loop, flags)
5036 struct loop *loop;
5037 int flags;
5039 struct loop_info *loop_info = LOOP_INFO (loop);
5040 struct loop_regs *regs = LOOP_REGS (loop);
5041 struct loop_ivs *ivs = LOOP_IVS (loop);
5042 rtx p;
5043 /* Temporary list pointer for traversing ivs->list. */
5044 struct iv_class *bl;
5045 /* Ratio of extra register life span we can justify
5046 for saving an instruction. More if loop doesn't call subroutines
5047 since in that case saving an insn makes more difference
5048 and more registers are available. */
5049 /* ??? could set this to last value of threshold in move_movables */
5050 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
5051 /* Map of pseudo-register replacements. */
5052 rtx *reg_map = NULL;
5053 int reg_map_size;
5054 int unrolled_insn_copies = 0;
5055 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
5056 int insn_count = count_insns_in_loop (loop);
5058 addr_placeholder = gen_reg_rtx (Pmode);
5060 ivs->n_regs = max_reg_before_loop;
5061 ivs->regs = (struct iv *) xcalloc (ivs->n_regs, sizeof (struct iv));
5063 /* Find all BIVs in loop. */
5064 loop_bivs_find (loop);
5066 /* Exit if there are no bivs. */
5067 if (! ivs->list)
5069 /* Can still unroll the loop anyways, but indicate that there is no
5070 strength reduction info available. */
5071 if (flags & LOOP_UNROLL)
5072 unroll_loop (loop, insn_count, 0);
5074 loop_ivs_free (loop);
5075 return;
5078 /* Determine how BIVS are initialised by looking through pre-header
5079 extended basic block. */
5080 loop_bivs_init_find (loop);
5082 /* Look at the each biv and see if we can say anything better about its
5083 initial value from any initializing insns set up above. */
5084 loop_bivs_check (loop);
5086 /* Search the loop for general induction variables. */
5087 loop_givs_find (loop);
5089 /* Try to calculate and save the number of loop iterations. This is
5090 set to zero if the actual number can not be calculated. This must
5091 be called after all giv's have been identified, since otherwise it may
5092 fail if the iteration variable is a giv. */
5093 loop_iterations (loop);
5095 #ifdef HAVE_prefetch
5096 if (flags & LOOP_PREFETCH)
5097 emit_prefetch_instructions (loop);
5098 #endif
5100 /* Now for each giv for which we still don't know whether or not it is
5101 replaceable, check to see if it is replaceable because its final value
5102 can be calculated. This must be done after loop_iterations is called,
5103 so that final_giv_value will work correctly. */
5104 loop_givs_check (loop);
5106 /* Try to prove that the loop counter variable (if any) is always
5107 nonnegative; if so, record that fact with a REG_NONNEG note
5108 so that "decrement and branch until zero" insn can be used. */
5109 check_dbra_loop (loop, insn_count);
5111 /* Create reg_map to hold substitutions for replaceable giv regs.
5112 Some givs might have been made from biv increments, so look at
5113 ivs->reg_iv_type for a suitable size. */
5114 reg_map_size = ivs->n_regs;
5115 reg_map = (rtx *) xcalloc (reg_map_size, sizeof (rtx));
5117 /* Examine each iv class for feasibility of strength reduction/induction
5118 variable elimination. */
5120 for (bl = ivs->list; bl; bl = bl->next)
5122 struct induction *v;
5123 int benefit;
5125 /* Test whether it will be possible to eliminate this biv
5126 provided all givs are reduced. */
5127 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
5129 /* This will be true at the end, if all givs which depend on this
5130 biv have been strength reduced.
5131 We can't (currently) eliminate the biv unless this is so. */
5132 bl->all_reduced = 1;
5134 /* Check each extension dependent giv in this class to see if its
5135 root biv is safe from wrapping in the interior mode. */
5136 check_ext_dependent_givs (bl, loop_info);
5138 /* Combine all giv's for this iv_class. */
5139 combine_givs (regs, bl);
5141 for (v = bl->giv; v; v = v->next_iv)
5143 struct induction *tv;
5145 if (v->ignore || v->same)
5146 continue;
5148 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
5150 /* If an insn is not to be strength reduced, then set its ignore
5151 flag, and clear bl->all_reduced. */
5153 /* A giv that depends on a reversed biv must be reduced if it is
5154 used after the loop exit, otherwise, it would have the wrong
5155 value after the loop exit. To make it simple, just reduce all
5156 of such giv's whether or not we know they are used after the loop
5157 exit. */
5159 if (! flag_reduce_all_givs
5160 && v->lifetime * threshold * benefit < insn_count
5161 && ! bl->reversed)
5163 if (loop_dump_stream)
5164 fprintf (loop_dump_stream,
5165 "giv of insn %d not worth while, %d vs %d.\n",
5166 INSN_UID (v->insn),
5167 v->lifetime * threshold * benefit, insn_count);
5168 v->ignore = 1;
5169 bl->all_reduced = 0;
5171 else
5173 /* Check that we can increment the reduced giv without a
5174 multiply insn. If not, reject it. */
5176 for (tv = bl->biv; tv; tv = tv->next_iv)
5177 if (tv->mult_val == const1_rtx
5178 && ! product_cheap_p (tv->add_val, v->mult_val))
5180 if (loop_dump_stream)
5181 fprintf (loop_dump_stream,
5182 "giv of insn %d: would need a multiply.\n",
5183 INSN_UID (v->insn));
5184 v->ignore = 1;
5185 bl->all_reduced = 0;
5186 break;
5191 /* Check for givs whose first use is their definition and whose
5192 last use is the definition of another giv. If so, it is likely
5193 dead and should not be used to derive another giv nor to
5194 eliminate a biv. */
5195 loop_givs_dead_check (loop, bl);
5197 /* Reduce each giv that we decided to reduce. */
5198 loop_givs_reduce (loop, bl);
5200 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
5201 as not reduced.
5203 For each giv register that can be reduced now: if replaceable,
5204 substitute reduced reg wherever the old giv occurs;
5205 else add new move insn "giv_reg = reduced_reg". */
5206 loop_givs_rescan (loop, bl, reg_map);
5208 /* All the givs based on the biv bl have been reduced if they
5209 merit it. */
5211 /* For each giv not marked as maybe dead that has been combined with a
5212 second giv, clear any "maybe dead" mark on that second giv.
5213 v->new_reg will either be or refer to the register of the giv it
5214 combined with.
5216 Doing this clearing avoids problems in biv elimination where
5217 a giv's new_reg is a complex value that can't be put in the
5218 insn but the giv combined with (with a reg as new_reg) is
5219 marked maybe_dead. Since the register will be used in either
5220 case, we'd prefer it be used from the simpler giv. */
5222 for (v = bl->giv; v; v = v->next_iv)
5223 if (! v->maybe_dead && v->same)
5224 v->same->maybe_dead = 0;
5226 /* Try to eliminate the biv, if it is a candidate.
5227 This won't work if ! bl->all_reduced,
5228 since the givs we planned to use might not have been reduced.
5230 We have to be careful that we didn't initially think we could
5231 eliminate this biv because of a giv that we now think may be
5232 dead and shouldn't be used as a biv replacement.
5234 Also, there is the possibility that we may have a giv that looks
5235 like it can be used to eliminate a biv, but the resulting insn
5236 isn't valid. This can happen, for example, on the 88k, where a
5237 JUMP_INSN can compare a register only with zero. Attempts to
5238 replace it with a compare with a constant will fail.
5240 Note that in cases where this call fails, we may have replaced some
5241 of the occurrences of the biv with a giv, but no harm was done in
5242 doing so in the rare cases where it can occur. */
5244 if (bl->all_reduced == 1 && bl->eliminable
5245 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
5247 /* ?? If we created a new test to bypass the loop entirely,
5248 or otherwise drop straight in, based on this test, then
5249 we might want to rewrite it also. This way some later
5250 pass has more hope of removing the initialization of this
5251 biv entirely. */
5253 /* If final_value != 0, then the biv may be used after loop end
5254 and we must emit an insn to set it just in case.
5256 Reversed bivs already have an insn after the loop setting their
5257 value, so we don't need another one. We can't calculate the
5258 proper final value for such a biv here anyways. */
5259 if (bl->final_value && ! bl->reversed)
5260 loop_insn_sink_or_swim (loop,
5261 gen_load_of_final_value (bl->biv->dest_reg,
5262 bl->final_value));
5264 if (loop_dump_stream)
5265 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
5266 bl->regno);
5268 /* See above note wrt final_value. But since we couldn't eliminate
5269 the biv, we must set the value after the loop instead of before. */
5270 else if (bl->final_value && ! bl->reversed)
5271 loop_insn_sink (loop, gen_load_of_final_value (bl->biv->dest_reg,
5272 bl->final_value));
5275 /* Go through all the instructions in the loop, making all the
5276 register substitutions scheduled in REG_MAP. */
5278 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
5279 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5280 || GET_CODE (p) == CALL_INSN)
5282 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
5283 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
5284 INSN_CODE (p) = -1;
5287 if (loop_info->n_iterations > 0)
5289 /* When we completely unroll a loop we will likely not need the increment
5290 of the loop BIV and we will not need the conditional branch at the
5291 end of the loop. */
5292 unrolled_insn_copies = insn_count - 2;
5294 #ifdef HAVE_cc0
5295 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5296 need the comparison before the conditional branch at the end of the
5297 loop. */
5298 unrolled_insn_copies -= 1;
5299 #endif
5301 /* We'll need one copy for each loop iteration. */
5302 unrolled_insn_copies *= loop_info->n_iterations;
5304 /* A little slop to account for the ability to remove initialization
5305 code, better CSE, and other secondary benefits of completely
5306 unrolling some loops. */
5307 unrolled_insn_copies -= 1;
5309 /* Clamp the value. */
5310 if (unrolled_insn_copies < 0)
5311 unrolled_insn_copies = 0;
5314 /* Unroll loops from within strength reduction so that we can use the
5315 induction variable information that strength_reduce has already
5316 collected. Always unroll loops that would be as small or smaller
5317 unrolled than when rolled. */
5318 if ((flags & LOOP_UNROLL)
5319 || (!(flags & LOOP_FIRST_PASS)
5320 && loop_info->n_iterations > 0
5321 && unrolled_insn_copies <= insn_count))
5322 unroll_loop (loop, insn_count, 1);
5324 #ifdef HAVE_doloop_end
5325 if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
5326 doloop_optimize (loop);
5327 #endif /* HAVE_doloop_end */
5329 /* In case number of iterations is known, drop branch prediction note
5330 in the branch. Do that only in second loop pass, as loop unrolling
5331 may change the number of iterations performed. */
5332 if (flags & LOOP_BCT)
5334 unsigned HOST_WIDE_INT n
5335 = loop_info->n_iterations / loop_info->unroll_number;
5336 if (n > 1)
5337 predict_insn (PREV_INSN (loop->end), PRED_LOOP_ITERATIONS,
5338 REG_BR_PROB_BASE - REG_BR_PROB_BASE / n);
5341 if (loop_dump_stream)
5342 fprintf (loop_dump_stream, "\n");
5344 loop_ivs_free (loop);
5345 if (reg_map)
5346 free (reg_map);
5349 /*Record all basic induction variables calculated in the insn. */
5350 static rtx
5351 check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
5352 struct loop *loop;
5353 rtx p;
5354 int not_every_iteration;
5355 int maybe_multiple;
5357 struct loop_ivs *ivs = LOOP_IVS (loop);
5358 rtx set;
5359 rtx dest_reg;
5360 rtx inc_val;
5361 rtx mult_val;
5362 rtx *location;
5364 if (GET_CODE (p) == INSN
5365 && (set = single_set (p))
5366 && GET_CODE (SET_DEST (set)) == REG)
5368 dest_reg = SET_DEST (set);
5369 if (REGNO (dest_reg) < max_reg_before_loop
5370 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
5371 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
5373 if (basic_induction_var (loop, SET_SRC (set),
5374 GET_MODE (SET_SRC (set)),
5375 dest_reg, p, &inc_val, &mult_val,
5376 &location))
5378 /* It is a possible basic induction variable.
5379 Create and initialize an induction structure for it. */
5381 struct induction *v
5382 = (struct induction *) xmalloc (sizeof (struct induction));
5384 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
5385 not_every_iteration, maybe_multiple);
5386 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
5388 else if (REGNO (dest_reg) < ivs->n_regs)
5389 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
5392 return p;
5395 /* Record all givs calculated in the insn.
5396 A register is a giv if: it is only set once, it is a function of a
5397 biv and a constant (or invariant), and it is not a biv. */
5398 static rtx
5399 check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
5400 struct loop *loop;
5401 rtx p;
5402 int not_every_iteration;
5403 int maybe_multiple;
5405 struct loop_regs *regs = LOOP_REGS (loop);
5407 rtx set;
5408 /* Look for a general induction variable in a register. */
5409 if (GET_CODE (p) == INSN
5410 && (set = single_set (p))
5411 && GET_CODE (SET_DEST (set)) == REG
5412 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
5414 rtx src_reg;
5415 rtx dest_reg;
5416 rtx add_val;
5417 rtx mult_val;
5418 rtx ext_val;
5419 int benefit;
5420 rtx regnote = 0;
5421 rtx last_consec_insn;
5423 dest_reg = SET_DEST (set);
5424 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
5425 return p;
5427 if (/* SET_SRC is a giv. */
5428 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
5429 &mult_val, &ext_val, 0, &benefit, VOIDmode)
5430 /* Equivalent expression is a giv. */
5431 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
5432 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
5433 &add_val, &mult_val, &ext_val, 0,
5434 &benefit, VOIDmode)))
5435 /* Don't try to handle any regs made by loop optimization.
5436 We have nothing on them in regno_first_uid, etc. */
5437 && REGNO (dest_reg) < max_reg_before_loop
5438 /* Don't recognize a BASIC_INDUCT_VAR here. */
5439 && dest_reg != src_reg
5440 /* This must be the only place where the register is set. */
5441 && (regs->array[REGNO (dest_reg)].n_times_set == 1
5442 /* or all sets must be consecutive and make a giv. */
5443 || (benefit = consec_sets_giv (loop, benefit, p,
5444 src_reg, dest_reg,
5445 &add_val, &mult_val, &ext_val,
5446 &last_consec_insn))))
5448 struct induction *v
5449 = (struct induction *) 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 #ifndef DONT_REDUCE_ADDR
5467 /* Look for givs which are memory addresses. */
5468 /* This resulted in worse code on a VAX 8600. I wonder if it
5469 still does. */
5470 if (GET_CODE (p) == INSN)
5471 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
5472 maybe_multiple);
5473 #endif
5475 /* Update the status of whether giv can derive other givs. This can
5476 change when we pass a label or an insn that updates a biv. */
5477 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5478 || GET_CODE (p) == CODE_LABEL)
5479 update_giv_derive (loop, p);
5480 return p;
5483 /* Return 1 if X is a valid source for an initial value (or as value being
5484 compared against in an initial test).
5486 X must be either a register or constant and must not be clobbered between
5487 the current insn and the start of the loop.
5489 INSN is the insn containing X. */
5491 static int
5492 valid_initial_value_p (x, insn, call_seen, loop_start)
5493 rtx x;
5494 rtx insn;
5495 int call_seen;
5496 rtx loop_start;
5498 if (CONSTANT_P (x))
5499 return 1;
5501 /* Only consider pseudos we know about initialized in insns whose luids
5502 we know. */
5503 if (GET_CODE (x) != REG
5504 || REGNO (x) >= max_reg_before_loop)
5505 return 0;
5507 /* Don't use call-clobbered registers across a call which clobbers it. On
5508 some machines, don't use any hard registers at all. */
5509 if (REGNO (x) < FIRST_PSEUDO_REGISTER
5510 && (SMALL_REGISTER_CLASSES
5511 || (call_used_regs[REGNO (x)] && call_seen)))
5512 return 0;
5514 /* Don't use registers that have been clobbered before the start of the
5515 loop. */
5516 if (reg_set_between_p (x, insn, loop_start))
5517 return 0;
5519 return 1;
5522 /* Scan X for memory refs and check each memory address
5523 as a possible giv. INSN is the insn whose pattern X comes from.
5524 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5525 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
5526 more thanonce in each loop iteration. */
5528 static void
5529 find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
5530 const struct loop *loop;
5531 rtx x;
5532 rtx insn;
5533 int not_every_iteration, maybe_multiple;
5535 int i, j;
5536 enum rtx_code code;
5537 const char *fmt;
5539 if (x == 0)
5540 return;
5542 code = GET_CODE (x);
5543 switch (code)
5545 case REG:
5546 case CONST_INT:
5547 case CONST:
5548 case CONST_DOUBLE:
5549 case SYMBOL_REF:
5550 case LABEL_REF:
5551 case PC:
5552 case CC0:
5553 case ADDR_VEC:
5554 case ADDR_DIFF_VEC:
5555 case USE:
5556 case CLOBBER:
5557 return;
5559 case MEM:
5561 rtx src_reg;
5562 rtx add_val;
5563 rtx mult_val;
5564 rtx ext_val;
5565 int benefit;
5567 /* This code used to disable creating GIVs with mult_val == 1 and
5568 add_val == 0. However, this leads to lost optimizations when
5569 it comes time to combine a set of related DEST_ADDR GIVs, since
5570 this one would not be seen. */
5572 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
5573 &mult_val, &ext_val, 1, &benefit,
5574 GET_MODE (x)))
5576 /* Found one; record it. */
5577 struct induction *v
5578 = (struct induction *) xmalloc (sizeof (struct induction));
5580 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
5581 add_val, ext_val, benefit, DEST_ADDR,
5582 not_every_iteration, maybe_multiple, &XEXP (x, 0));
5584 v->mem = x;
5587 return;
5589 default:
5590 break;
5593 /* Recursively scan the subexpressions for other mem refs. */
5595 fmt = GET_RTX_FORMAT (code);
5596 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5597 if (fmt[i] == 'e')
5598 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
5599 maybe_multiple);
5600 else if (fmt[i] == 'E')
5601 for (j = 0; j < XVECLEN (x, i); j++)
5602 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
5603 maybe_multiple);
5606 /* Fill in the data about one biv update.
5607 V is the `struct induction' in which we record the biv. (It is
5608 allocated by the caller, with alloca.)
5609 INSN is the insn that sets it.
5610 DEST_REG is the biv's reg.
5612 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5613 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5614 being set to INC_VAL.
5616 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5617 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5618 can be executed more than once per iteration. If MAYBE_MULTIPLE
5619 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5620 executed exactly once per iteration. */
5622 static void
5623 record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
5624 not_every_iteration, maybe_multiple)
5625 struct loop *loop;
5626 struct induction *v;
5627 rtx insn;
5628 rtx dest_reg;
5629 rtx inc_val;
5630 rtx mult_val;
5631 rtx *location;
5632 int not_every_iteration;
5633 int maybe_multiple;
5635 struct loop_ivs *ivs = LOOP_IVS (loop);
5636 struct iv_class *bl;
5638 v->insn = insn;
5639 v->src_reg = dest_reg;
5640 v->dest_reg = dest_reg;
5641 v->mult_val = mult_val;
5642 v->add_val = inc_val;
5643 v->ext_dependent = NULL_RTX;
5644 v->location = location;
5645 v->mode = GET_MODE (dest_reg);
5646 v->always_computable = ! not_every_iteration;
5647 v->always_executed = ! not_every_iteration;
5648 v->maybe_multiple = maybe_multiple;
5650 /* Add this to the reg's iv_class, creating a class
5651 if this is the first incrementation of the reg. */
5653 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
5654 if (bl == 0)
5656 /* Create and initialize new iv_class. */
5658 bl = (struct iv_class *) xmalloc (sizeof (struct iv_class));
5660 bl->regno = REGNO (dest_reg);
5661 bl->biv = 0;
5662 bl->giv = 0;
5663 bl->biv_count = 0;
5664 bl->giv_count = 0;
5666 /* Set initial value to the reg itself. */
5667 bl->initial_value = dest_reg;
5668 bl->final_value = 0;
5669 /* We haven't seen the initializing insn yet */
5670 bl->init_insn = 0;
5671 bl->init_set = 0;
5672 bl->initial_test = 0;
5673 bl->incremented = 0;
5674 bl->eliminable = 0;
5675 bl->nonneg = 0;
5676 bl->reversed = 0;
5677 bl->total_benefit = 0;
5679 /* Add this class to ivs->list. */
5680 bl->next = ivs->list;
5681 ivs->list = bl;
5683 /* Put it in the array of biv register classes. */
5684 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
5687 /* Update IV_CLASS entry for this biv. */
5688 v->next_iv = bl->biv;
5689 bl->biv = v;
5690 bl->biv_count++;
5691 if (mult_val == const1_rtx)
5692 bl->incremented = 1;
5694 if (loop_dump_stream)
5695 loop_biv_dump (v, loop_dump_stream, 0);
5698 /* Fill in the data about one giv.
5699 V is the `struct induction' in which we record the giv. (It is
5700 allocated by the caller, with alloca.)
5701 INSN is the insn that sets it.
5702 BENEFIT estimates the savings from deleting this insn.
5703 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5704 into a register or is used as a memory address.
5706 SRC_REG is the biv reg which the giv is computed from.
5707 DEST_REG is the giv's reg (if the giv is stored in a reg).
5708 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5709 LOCATION points to the place where this giv's value appears in INSN. */
5711 static void
5712 record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
5713 benefit, type, not_every_iteration, maybe_multiple, location)
5714 const struct loop *loop;
5715 struct induction *v;
5716 rtx insn;
5717 rtx src_reg;
5718 rtx dest_reg;
5719 rtx mult_val, add_val, ext_val;
5720 int benefit;
5721 enum g_types type;
5722 int not_every_iteration, maybe_multiple;
5723 rtx *location;
5725 struct loop_ivs *ivs = LOOP_IVS (loop);
5726 struct induction *b;
5727 struct iv_class *bl;
5728 rtx set = single_set (insn);
5729 rtx temp;
5731 /* Attempt to prove constantness of the values. Don't let simplity_rtx
5732 undo the MULT canonicalization that we performed earlier. */
5733 temp = simplify_rtx (add_val);
5734 if (temp
5735 && ! (GET_CODE (add_val) == MULT
5736 && GET_CODE (temp) == ASHIFT))
5737 add_val = temp;
5739 v->insn = insn;
5740 v->src_reg = src_reg;
5741 v->giv_type = type;
5742 v->dest_reg = dest_reg;
5743 v->mult_val = mult_val;
5744 v->add_val = add_val;
5745 v->ext_dependent = ext_val;
5746 v->benefit = benefit;
5747 v->location = location;
5748 v->cant_derive = 0;
5749 v->combined_with = 0;
5750 v->maybe_multiple = maybe_multiple;
5751 v->maybe_dead = 0;
5752 v->derive_adjustment = 0;
5753 v->same = 0;
5754 v->ignore = 0;
5755 v->new_reg = 0;
5756 v->final_value = 0;
5757 v->same_insn = 0;
5758 v->auto_inc_opt = 0;
5759 v->unrolled = 0;
5760 v->shared = 0;
5762 /* The v->always_computable field is used in update_giv_derive, to
5763 determine whether a giv can be used to derive another giv. For a
5764 DEST_REG giv, INSN computes a new value for the giv, so its value
5765 isn't computable if INSN insn't executed every iteration.
5766 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5767 it does not compute a new value. Hence the value is always computable
5768 regardless of whether INSN is executed each iteration. */
5770 if (type == DEST_ADDR)
5771 v->always_computable = 1;
5772 else
5773 v->always_computable = ! not_every_iteration;
5775 v->always_executed = ! not_every_iteration;
5777 if (type == DEST_ADDR)
5779 v->mode = GET_MODE (*location);
5780 v->lifetime = 1;
5782 else /* type == DEST_REG */
5784 v->mode = GET_MODE (SET_DEST (set));
5786 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
5788 /* If the lifetime is zero, it means that this register is
5789 really a dead store. So mark this as a giv that can be
5790 ignored. This will not prevent the biv from being eliminated. */
5791 if (v->lifetime == 0)
5792 v->ignore = 1;
5794 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
5795 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
5798 /* Add the giv to the class of givs computed from one biv. */
5800 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
5801 if (bl)
5803 v->next_iv = bl->giv;
5804 bl->giv = v;
5805 /* Don't count DEST_ADDR. This is supposed to count the number of
5806 insns that calculate givs. */
5807 if (type == DEST_REG)
5808 bl->giv_count++;
5809 bl->total_benefit += benefit;
5811 else
5812 /* Fatal error, biv missing for this giv? */
5813 abort ();
5815 if (type == DEST_ADDR)
5816 v->replaceable = 1;
5817 else
5819 /* The giv can be replaced outright by the reduced register only if all
5820 of the following conditions are true:
5821 - the insn that sets the giv is always executed on any iteration
5822 on which the giv is used at all
5823 (there are two ways to deduce this:
5824 either the insn is executed on every iteration,
5825 or all uses follow that insn in the same basic block),
5826 - the giv is not used outside the loop
5827 - no assignments to the biv occur during the giv's lifetime. */
5829 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
5830 /* Previous line always fails if INSN was moved by loop opt. */
5831 && REGNO_LAST_LUID (REGNO (dest_reg))
5832 < INSN_LUID (loop->end)
5833 && (! not_every_iteration
5834 || last_use_this_basic_block (dest_reg, insn)))
5836 /* Now check that there are no assignments to the biv within the
5837 giv's lifetime. This requires two separate checks. */
5839 /* Check each biv update, and fail if any are between the first
5840 and last use of the giv.
5842 If this loop contains an inner loop that was unrolled, then
5843 the insn modifying the biv may have been emitted by the loop
5844 unrolling code, and hence does not have a valid luid. Just
5845 mark the biv as not replaceable in this case. It is not very
5846 useful as a biv, because it is used in two different loops.
5847 It is very unlikely that we would be able to optimize the giv
5848 using this biv anyways. */
5850 v->replaceable = 1;
5851 for (b = bl->biv; b; b = b->next_iv)
5853 if (INSN_UID (b->insn) >= max_uid_for_loop
5854 || ((INSN_LUID (b->insn)
5855 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
5856 && (INSN_LUID (b->insn)
5857 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
5859 v->replaceable = 0;
5860 v->not_replaceable = 1;
5861 break;
5865 /* If there are any backwards branches that go from after the
5866 biv update to before it, then this giv is not replaceable. */
5867 if (v->replaceable)
5868 for (b = bl->biv; b; b = b->next_iv)
5869 if (back_branch_in_range_p (loop, b->insn))
5871 v->replaceable = 0;
5872 v->not_replaceable = 1;
5873 break;
5876 else
5878 /* May still be replaceable, we don't have enough info here to
5879 decide. */
5880 v->replaceable = 0;
5881 v->not_replaceable = 0;
5885 /* Record whether the add_val contains a const_int, for later use by
5886 combine_givs. */
5888 rtx tem = add_val;
5890 v->no_const_addval = 1;
5891 if (tem == const0_rtx)
5893 else if (CONSTANT_P (add_val))
5894 v->no_const_addval = 0;
5895 if (GET_CODE (tem) == PLUS)
5897 while (1)
5899 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5900 tem = XEXP (tem, 0);
5901 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5902 tem = XEXP (tem, 1);
5903 else
5904 break;
5906 if (CONSTANT_P (XEXP (tem, 1)))
5907 v->no_const_addval = 0;
5911 if (loop_dump_stream)
5912 loop_giv_dump (v, loop_dump_stream, 0);
5915 /* All this does is determine whether a giv can be made replaceable because
5916 its final value can be calculated. This code can not be part of record_giv
5917 above, because final_giv_value requires that the number of loop iterations
5918 be known, and that can not be accurately calculated until after all givs
5919 have been identified. */
5921 static void
5922 check_final_value (loop, v)
5923 const struct loop *loop;
5924 struct induction *v;
5926 struct loop_ivs *ivs = LOOP_IVS (loop);
5927 struct iv_class *bl;
5928 rtx final_value = 0;
5930 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
5932 /* DEST_ADDR givs will never reach here, because they are always marked
5933 replaceable above in record_giv. */
5935 /* The giv can be replaced outright by the reduced register only if all
5936 of the following conditions are true:
5937 - the insn that sets the giv is always executed on any iteration
5938 on which the giv is used at all
5939 (there are two ways to deduce this:
5940 either the insn is executed on every iteration,
5941 or all uses follow that insn in the same basic block),
5942 - its final value can be calculated (this condition is different
5943 than the one above in record_giv)
5944 - it's not used before the it's set
5945 - no assignments to the biv occur during the giv's lifetime. */
5947 #if 0
5948 /* This is only called now when replaceable is known to be false. */
5949 /* Clear replaceable, so that it won't confuse final_giv_value. */
5950 v->replaceable = 0;
5951 #endif
5953 if ((final_value = final_giv_value (loop, v))
5954 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5956 int biv_increment_seen = 0, before_giv_insn = 0;
5957 rtx p = v->insn;
5958 rtx last_giv_use;
5960 v->replaceable = 1;
5962 /* When trying to determine whether or not a biv increment occurs
5963 during the lifetime of the giv, we can ignore uses of the variable
5964 outside the loop because final_value is true. Hence we can not
5965 use regno_last_uid and regno_first_uid as above in record_giv. */
5967 /* Search the loop to determine whether any assignments to the
5968 biv occur during the giv's lifetime. Start with the insn
5969 that sets the giv, and search around the loop until we come
5970 back to that insn again.
5972 Also fail if there is a jump within the giv's lifetime that jumps
5973 to somewhere outside the lifetime but still within the loop. This
5974 catches spaghetti code where the execution order is not linear, and
5975 hence the above test fails. Here we assume that the giv lifetime
5976 does not extend from one iteration of the loop to the next, so as
5977 to make the test easier. Since the lifetime isn't known yet,
5978 this requires two loops. See also record_giv above. */
5980 last_giv_use = v->insn;
5982 while (1)
5984 p = NEXT_INSN (p);
5985 if (p == loop->end)
5987 before_giv_insn = 1;
5988 p = NEXT_INSN (loop->start);
5990 if (p == v->insn)
5991 break;
5993 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5994 || GET_CODE (p) == CALL_INSN)
5996 /* It is possible for the BIV increment to use the GIV if we
5997 have a cycle. Thus we must be sure to check each insn for
5998 both BIV and GIV uses, and we must check for BIV uses
5999 first. */
6001 if (! biv_increment_seen
6002 && reg_set_p (v->src_reg, PATTERN (p)))
6003 biv_increment_seen = 1;
6005 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
6007 if (biv_increment_seen || before_giv_insn)
6009 v->replaceable = 0;
6010 v->not_replaceable = 1;
6011 break;
6013 last_giv_use = p;
6018 /* Now that the lifetime of the giv is known, check for branches
6019 from within the lifetime to outside the lifetime if it is still
6020 replaceable. */
6022 if (v->replaceable)
6024 p = v->insn;
6025 while (1)
6027 p = NEXT_INSN (p);
6028 if (p == loop->end)
6029 p = NEXT_INSN (loop->start);
6030 if (p == last_giv_use)
6031 break;
6033 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
6034 && LABEL_NAME (JUMP_LABEL (p))
6035 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
6036 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
6037 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
6038 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
6040 v->replaceable = 0;
6041 v->not_replaceable = 1;
6043 if (loop_dump_stream)
6044 fprintf (loop_dump_stream,
6045 "Found branch outside giv lifetime.\n");
6047 break;
6052 /* If it is replaceable, then save the final value. */
6053 if (v->replaceable)
6054 v->final_value = final_value;
6057 if (loop_dump_stream && v->replaceable)
6058 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
6059 INSN_UID (v->insn), REGNO (v->dest_reg));
6062 /* Update the status of whether a giv can derive other givs.
6064 We need to do something special if there is or may be an update to the biv
6065 between the time the giv is defined and the time it is used to derive
6066 another giv.
6068 In addition, a giv that is only conditionally set is not allowed to
6069 derive another giv once a label has been passed.
6071 The cases we look at are when a label or an update to a biv is passed. */
6073 static void
6074 update_giv_derive (loop, p)
6075 const struct loop *loop;
6076 rtx p;
6078 struct loop_ivs *ivs = LOOP_IVS (loop);
6079 struct iv_class *bl;
6080 struct induction *biv, *giv;
6081 rtx tem;
6082 int dummy;
6084 /* Search all IV classes, then all bivs, and finally all givs.
6086 There are three cases we are concerned with. First we have the situation
6087 of a giv that is only updated conditionally. In that case, it may not
6088 derive any givs after a label is passed.
6090 The second case is when a biv update occurs, or may occur, after the
6091 definition of a giv. For certain biv updates (see below) that are
6092 known to occur between the giv definition and use, we can adjust the
6093 giv definition. For others, or when the biv update is conditional,
6094 we must prevent the giv from deriving any other givs. There are two
6095 sub-cases within this case.
6097 If this is a label, we are concerned with any biv update that is done
6098 conditionally, since it may be done after the giv is defined followed by
6099 a branch here (actually, we need to pass both a jump and a label, but
6100 this extra tracking doesn't seem worth it).
6102 If this is a jump, we are concerned about any biv update that may be
6103 executed multiple times. We are actually only concerned about
6104 backward jumps, but it is probably not worth performing the test
6105 on the jump again here.
6107 If this is a biv update, we must adjust the giv status to show that a
6108 subsequent biv update was performed. If this adjustment cannot be done,
6109 the giv cannot derive further givs. */
6111 for (bl = ivs->list; bl; bl = bl->next)
6112 for (biv = bl->biv; biv; biv = biv->next_iv)
6113 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
6114 || biv->insn == p)
6116 for (giv = bl->giv; giv; giv = giv->next_iv)
6118 /* If cant_derive is already true, there is no point in
6119 checking all of these conditions again. */
6120 if (giv->cant_derive)
6121 continue;
6123 /* If this giv is conditionally set and we have passed a label,
6124 it cannot derive anything. */
6125 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
6126 giv->cant_derive = 1;
6128 /* Skip givs that have mult_val == 0, since
6129 they are really invariants. Also skip those that are
6130 replaceable, since we know their lifetime doesn't contain
6131 any biv update. */
6132 else if (giv->mult_val == const0_rtx || giv->replaceable)
6133 continue;
6135 /* The only way we can allow this giv to derive another
6136 is if this is a biv increment and we can form the product
6137 of biv->add_val and giv->mult_val. In this case, we will
6138 be able to compute a compensation. */
6139 else if (biv->insn == p)
6141 rtx ext_val_dummy;
6143 tem = 0;
6144 if (biv->mult_val == const1_rtx)
6145 tem = simplify_giv_expr (loop,
6146 gen_rtx_MULT (giv->mode,
6147 biv->add_val,
6148 giv->mult_val),
6149 &ext_val_dummy, &dummy);
6151 if (tem && giv->derive_adjustment)
6152 tem = simplify_giv_expr
6153 (loop,
6154 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
6155 &ext_val_dummy, &dummy);
6157 if (tem)
6158 giv->derive_adjustment = tem;
6159 else
6160 giv->cant_derive = 1;
6162 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
6163 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
6164 giv->cant_derive = 1;
6169 /* Check whether an insn is an increment legitimate for a basic induction var.
6170 X is the source of insn P, or a part of it.
6171 MODE is the mode in which X should be interpreted.
6173 DEST_REG is the putative biv, also the destination of the insn.
6174 We accept patterns of these forms:
6175 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
6176 REG = INVARIANT + REG
6178 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
6179 store the additive term into *INC_VAL, and store the place where
6180 we found the additive term into *LOCATION.
6182 If X is an assignment of an invariant into DEST_REG, we set
6183 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
6185 We also want to detect a BIV when it corresponds to a variable
6186 whose mode was promoted via PROMOTED_MODE. In that case, an increment
6187 of the variable may be a PLUS that adds a SUBREG of that variable to
6188 an invariant and then sign- or zero-extends the result of the PLUS
6189 into the variable.
6191 Most GIVs in such cases will be in the promoted mode, since that is the
6192 probably the natural computation mode (and almost certainly the mode
6193 used for addresses) on the machine. So we view the pseudo-reg containing
6194 the variable as the BIV, as if it were simply incremented.
6196 Note that treating the entire pseudo as a BIV will result in making
6197 simple increments to any GIVs based on it. However, if the variable
6198 overflows in its declared mode but not its promoted mode, the result will
6199 be incorrect. This is acceptable if the variable is signed, since
6200 overflows in such cases are undefined, but not if it is unsigned, since
6201 those overflows are defined. So we only check for SIGN_EXTEND and
6202 not ZERO_EXTEND.
6204 If we cannot find a biv, we return 0. */
6206 static int
6207 basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
6208 const struct loop *loop;
6209 rtx x;
6210 enum machine_mode mode;
6211 rtx dest_reg;
6212 rtx p;
6213 rtx *inc_val;
6214 rtx *mult_val;
6215 rtx **location;
6217 enum rtx_code code;
6218 rtx *argp, arg;
6219 rtx insn, set = 0;
6221 code = GET_CODE (x);
6222 *location = NULL;
6223 switch (code)
6225 case PLUS:
6226 if (rtx_equal_p (XEXP (x, 0), dest_reg)
6227 || (GET_CODE (XEXP (x, 0)) == SUBREG
6228 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
6229 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
6231 argp = &XEXP (x, 1);
6233 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
6234 || (GET_CODE (XEXP (x, 1)) == SUBREG
6235 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
6236 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
6238 argp = &XEXP (x, 0);
6240 else
6241 return 0;
6243 arg = *argp;
6244 if (loop_invariant_p (loop, arg) != 1)
6245 return 0;
6247 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
6248 *mult_val = const1_rtx;
6249 *location = argp;
6250 return 1;
6252 case SUBREG:
6253 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
6254 handle addition of promoted variables.
6255 ??? The comment at the start of this function is wrong: promoted
6256 variable increments don't look like it says they do. */
6257 return basic_induction_var (loop, SUBREG_REG (x),
6258 GET_MODE (SUBREG_REG (x)),
6259 dest_reg, p, inc_val, mult_val, location);
6261 case REG:
6262 /* If this register is assigned in a previous insn, look at its
6263 source, but don't go outside the loop or past a label. */
6265 /* If this sets a register to itself, we would repeat any previous
6266 biv increment if we applied this strategy blindly. */
6267 if (rtx_equal_p (dest_reg, x))
6268 return 0;
6270 insn = p;
6271 while (1)
6273 rtx dest;
6276 insn = PREV_INSN (insn);
6278 while (insn && GET_CODE (insn) == NOTE
6279 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6281 if (!insn)
6282 break;
6283 set = single_set (insn);
6284 if (set == 0)
6285 break;
6286 dest = SET_DEST (set);
6287 if (dest == x
6288 || (GET_CODE (dest) == SUBREG
6289 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
6290 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
6291 && SUBREG_REG (dest) == x))
6292 return basic_induction_var (loop, SET_SRC (set),
6293 (GET_MODE (SET_SRC (set)) == VOIDmode
6294 ? GET_MODE (x)
6295 : GET_MODE (SET_SRC (set))),
6296 dest_reg, insn,
6297 inc_val, mult_val, location);
6299 while (GET_CODE (dest) == SIGN_EXTRACT
6300 || GET_CODE (dest) == ZERO_EXTRACT
6301 || GET_CODE (dest) == SUBREG
6302 || GET_CODE (dest) == STRICT_LOW_PART)
6303 dest = XEXP (dest, 0);
6304 if (dest == x)
6305 break;
6307 /* Fall through. */
6309 /* Can accept constant setting of biv only when inside inner most loop.
6310 Otherwise, a biv of an inner loop may be incorrectly recognized
6311 as a biv of the outer loop,
6312 causing code to be moved INTO the inner loop. */
6313 case MEM:
6314 if (loop_invariant_p (loop, x) != 1)
6315 return 0;
6316 case CONST_INT:
6317 case SYMBOL_REF:
6318 case CONST:
6319 /* convert_modes aborts if we try to convert to or from CCmode, so just
6320 exclude that case. It is very unlikely that a condition code value
6321 would be a useful iterator anyways. convert_modes aborts if we try to
6322 convert a float mode to non-float or vice versa too. */
6323 if (loop->level == 1
6324 && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
6325 && GET_MODE_CLASS (mode) != MODE_CC)
6327 /* Possible bug here? Perhaps we don't know the mode of X. */
6328 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
6329 *mult_val = const0_rtx;
6330 return 1;
6332 else
6333 return 0;
6335 case SIGN_EXTEND:
6336 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6337 dest_reg, p, inc_val, mult_val, location);
6339 case ASHIFTRT:
6340 /* Similar, since this can be a sign extension. */
6341 for (insn = PREV_INSN (p);
6342 (insn && GET_CODE (insn) == NOTE
6343 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6344 insn = PREV_INSN (insn))
6347 if (insn)
6348 set = single_set (insn);
6350 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
6351 && set && SET_DEST (set) == XEXP (x, 0)
6352 && GET_CODE (XEXP (x, 1)) == CONST_INT
6353 && INTVAL (XEXP (x, 1)) >= 0
6354 && GET_CODE (SET_SRC (set)) == ASHIFT
6355 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
6356 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
6357 GET_MODE (XEXP (x, 0)),
6358 dest_reg, insn, inc_val, mult_val,
6359 location);
6360 return 0;
6362 default:
6363 return 0;
6367 /* A general induction variable (giv) is any quantity that is a linear
6368 function of a basic induction variable,
6369 i.e. giv = biv * mult_val + add_val.
6370 The coefficients can be any loop invariant quantity.
6371 A giv need not be computed directly from the biv;
6372 it can be computed by way of other givs. */
6374 /* Determine whether X computes a giv.
6375 If it does, return a nonzero value
6376 which is the benefit from eliminating the computation of X;
6377 set *SRC_REG to the register of the biv that it is computed from;
6378 set *ADD_VAL and *MULT_VAL to the coefficients,
6379 such that the value of X is biv * mult + add; */
6381 static int
6382 general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
6383 is_addr, pbenefit, addr_mode)
6384 const struct loop *loop;
6385 rtx x;
6386 rtx *src_reg;
6387 rtx *add_val;
6388 rtx *mult_val;
6389 rtx *ext_val;
6390 int is_addr;
6391 int *pbenefit;
6392 enum machine_mode addr_mode;
6394 struct loop_ivs *ivs = LOOP_IVS (loop);
6395 rtx orig_x = x;
6397 /* If this is an invariant, forget it, it isn't a giv. */
6398 if (loop_invariant_p (loop, x) == 1)
6399 return 0;
6401 *pbenefit = 0;
6402 *ext_val = NULL_RTX;
6403 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
6404 if (x == 0)
6405 return 0;
6407 switch (GET_CODE (x))
6409 case USE:
6410 case CONST_INT:
6411 /* Since this is now an invariant and wasn't before, it must be a giv
6412 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6413 with. */
6414 *src_reg = ivs->list->biv->dest_reg;
6415 *mult_val = const0_rtx;
6416 *add_val = x;
6417 break;
6419 case REG:
6420 /* This is equivalent to a BIV. */
6421 *src_reg = x;
6422 *mult_val = const1_rtx;
6423 *add_val = const0_rtx;
6424 break;
6426 case PLUS:
6427 /* Either (plus (biv) (invar)) or
6428 (plus (mult (biv) (invar_1)) (invar_2)). */
6429 if (GET_CODE (XEXP (x, 0)) == MULT)
6431 *src_reg = XEXP (XEXP (x, 0), 0);
6432 *mult_val = XEXP (XEXP (x, 0), 1);
6434 else
6436 *src_reg = XEXP (x, 0);
6437 *mult_val = const1_rtx;
6439 *add_val = XEXP (x, 1);
6440 break;
6442 case MULT:
6443 /* ADD_VAL is zero. */
6444 *src_reg = XEXP (x, 0);
6445 *mult_val = XEXP (x, 1);
6446 *add_val = const0_rtx;
6447 break;
6449 default:
6450 abort ();
6453 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6454 unless they are CONST_INT). */
6455 if (GET_CODE (*add_val) == USE)
6456 *add_val = XEXP (*add_val, 0);
6457 if (GET_CODE (*mult_val) == USE)
6458 *mult_val = XEXP (*mult_val, 0);
6460 if (is_addr)
6461 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
6462 else
6463 *pbenefit += rtx_cost (orig_x, SET);
6465 /* Always return true if this is a giv so it will be detected as such,
6466 even if the benefit is zero or negative. This allows elimination
6467 of bivs that might otherwise not be eliminated. */
6468 return 1;
6471 /* Given an expression, X, try to form it as a linear function of a biv.
6472 We will canonicalize it to be of the form
6473 (plus (mult (BIV) (invar_1))
6474 (invar_2))
6475 with possible degeneracies.
6477 The invariant expressions must each be of a form that can be used as a
6478 machine operand. We surround then with a USE rtx (a hack, but localized
6479 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6480 routine; it is the caller's responsibility to strip them.
6482 If no such canonicalization is possible (i.e., two biv's are used or an
6483 expression that is neither invariant nor a biv or giv), this routine
6484 returns 0.
6486 For a non-zero return, the result will have a code of CONST_INT, USE,
6487 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6489 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6491 static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
6492 static rtx sge_plus_constant PARAMS ((rtx, rtx));
6494 static rtx
6495 simplify_giv_expr (loop, x, ext_val, benefit)
6496 const struct loop *loop;
6497 rtx x;
6498 rtx *ext_val;
6499 int *benefit;
6501 struct loop_ivs *ivs = LOOP_IVS (loop);
6502 struct loop_regs *regs = LOOP_REGS (loop);
6503 enum machine_mode mode = GET_MODE (x);
6504 rtx arg0, arg1;
6505 rtx tem;
6507 /* If this is not an integer mode, or if we cannot do arithmetic in this
6508 mode, this can't be a giv. */
6509 if (mode != VOIDmode
6510 && (GET_MODE_CLASS (mode) != MODE_INT
6511 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
6512 return NULL_RTX;
6514 switch (GET_CODE (x))
6516 case PLUS:
6517 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6518 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6519 if (arg0 == 0 || arg1 == 0)
6520 return NULL_RTX;
6522 /* Put constant last, CONST_INT last if both constant. */
6523 if ((GET_CODE (arg0) == USE
6524 || GET_CODE (arg0) == CONST_INT)
6525 && ! ((GET_CODE (arg0) == USE
6526 && GET_CODE (arg1) == USE)
6527 || GET_CODE (arg1) == CONST_INT))
6528 tem = arg0, arg0 = arg1, arg1 = tem;
6530 /* Handle addition of zero, then addition of an invariant. */
6531 if (arg1 == const0_rtx)
6532 return arg0;
6533 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6534 switch (GET_CODE (arg0))
6536 case CONST_INT:
6537 case USE:
6538 /* Adding two invariants must result in an invariant, so enclose
6539 addition operation inside a USE and return it. */
6540 if (GET_CODE (arg0) == USE)
6541 arg0 = XEXP (arg0, 0);
6542 if (GET_CODE (arg1) == USE)
6543 arg1 = XEXP (arg1, 0);
6545 if (GET_CODE (arg0) == CONST_INT)
6546 tem = arg0, arg0 = arg1, arg1 = tem;
6547 if (GET_CODE (arg1) == CONST_INT)
6548 tem = sge_plus_constant (arg0, arg1);
6549 else
6550 tem = sge_plus (mode, arg0, arg1);
6552 if (GET_CODE (tem) != CONST_INT)
6553 tem = gen_rtx_USE (mode, tem);
6554 return tem;
6556 case REG:
6557 case MULT:
6558 /* biv + invar or mult + invar. Return sum. */
6559 return gen_rtx_PLUS (mode, arg0, arg1);
6561 case PLUS:
6562 /* (a + invar_1) + invar_2. Associate. */
6563 return
6564 simplify_giv_expr (loop,
6565 gen_rtx_PLUS (mode,
6566 XEXP (arg0, 0),
6567 gen_rtx_PLUS (mode,
6568 XEXP (arg0, 1),
6569 arg1)),
6570 ext_val, benefit);
6572 default:
6573 abort ();
6576 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6577 MULT to reduce cases. */
6578 if (GET_CODE (arg0) == REG)
6579 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
6580 if (GET_CODE (arg1) == REG)
6581 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
6583 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6584 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6585 Recurse to associate the second PLUS. */
6586 if (GET_CODE (arg1) == MULT)
6587 tem = arg0, arg0 = arg1, arg1 = tem;
6589 if (GET_CODE (arg1) == PLUS)
6590 return
6591 simplify_giv_expr (loop,
6592 gen_rtx_PLUS (mode,
6593 gen_rtx_PLUS (mode, arg0,
6594 XEXP (arg1, 0)),
6595 XEXP (arg1, 1)),
6596 ext_val, benefit);
6598 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6599 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
6600 return NULL_RTX;
6602 if (!rtx_equal_p (arg0, arg1))
6603 return NULL_RTX;
6605 return simplify_giv_expr (loop,
6606 gen_rtx_MULT (mode,
6607 XEXP (arg0, 0),
6608 gen_rtx_PLUS (mode,
6609 XEXP (arg0, 1),
6610 XEXP (arg1, 1))),
6611 ext_val, benefit);
6613 case MINUS:
6614 /* Handle "a - b" as "a + b * (-1)". */
6615 return simplify_giv_expr (loop,
6616 gen_rtx_PLUS (mode,
6617 XEXP (x, 0),
6618 gen_rtx_MULT (mode,
6619 XEXP (x, 1),
6620 constm1_rtx)),
6621 ext_val, benefit);
6623 case MULT:
6624 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6625 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6626 if (arg0 == 0 || arg1 == 0)
6627 return NULL_RTX;
6629 /* Put constant last, CONST_INT last if both constant. */
6630 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6631 && GET_CODE (arg1) != CONST_INT)
6632 tem = arg0, arg0 = arg1, arg1 = tem;
6634 /* If second argument is not now constant, not giv. */
6635 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
6636 return NULL_RTX;
6638 /* Handle multiply by 0 or 1. */
6639 if (arg1 == const0_rtx)
6640 return const0_rtx;
6642 else if (arg1 == const1_rtx)
6643 return arg0;
6645 switch (GET_CODE (arg0))
6647 case REG:
6648 /* biv * invar. Done. */
6649 return gen_rtx_MULT (mode, arg0, arg1);
6651 case CONST_INT:
6652 /* Product of two constants. */
6653 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
6655 case USE:
6656 /* invar * invar is a giv, but attempt to simplify it somehow. */
6657 if (GET_CODE (arg1) != CONST_INT)
6658 return NULL_RTX;
6660 arg0 = XEXP (arg0, 0);
6661 if (GET_CODE (arg0) == MULT)
6663 /* (invar_0 * invar_1) * invar_2. Associate. */
6664 return simplify_giv_expr (loop,
6665 gen_rtx_MULT (mode,
6666 XEXP (arg0, 0),
6667 gen_rtx_MULT (mode,
6668 XEXP (arg0,
6670 arg1)),
6671 ext_val, benefit);
6673 /* Porpagate the MULT expressions to the intermost nodes. */
6674 else if (GET_CODE (arg0) == PLUS)
6676 /* (invar_0 + invar_1) * invar_2. Distribute. */
6677 return simplify_giv_expr (loop,
6678 gen_rtx_PLUS (mode,
6679 gen_rtx_MULT (mode,
6680 XEXP (arg0,
6682 arg1),
6683 gen_rtx_MULT (mode,
6684 XEXP (arg0,
6686 arg1)),
6687 ext_val, benefit);
6689 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
6691 case MULT:
6692 /* (a * invar_1) * invar_2. Associate. */
6693 return simplify_giv_expr (loop,
6694 gen_rtx_MULT (mode,
6695 XEXP (arg0, 0),
6696 gen_rtx_MULT (mode,
6697 XEXP (arg0, 1),
6698 arg1)),
6699 ext_val, benefit);
6701 case PLUS:
6702 /* (a + invar_1) * invar_2. Distribute. */
6703 return simplify_giv_expr (loop,
6704 gen_rtx_PLUS (mode,
6705 gen_rtx_MULT (mode,
6706 XEXP (arg0, 0),
6707 arg1),
6708 gen_rtx_MULT (mode,
6709 XEXP (arg0, 1),
6710 arg1)),
6711 ext_val, benefit);
6713 default:
6714 abort ();
6717 case ASHIFT:
6718 /* Shift by constant is multiply by power of two. */
6719 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6720 return 0;
6722 return
6723 simplify_giv_expr (loop,
6724 gen_rtx_MULT (mode,
6725 XEXP (x, 0),
6726 GEN_INT ((HOST_WIDE_INT) 1
6727 << INTVAL (XEXP (x, 1)))),
6728 ext_val, benefit);
6730 case NEG:
6731 /* "-a" is "a * (-1)" */
6732 return simplify_giv_expr (loop,
6733 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
6734 ext_val, benefit);
6736 case NOT:
6737 /* "~a" is "-a - 1". Silly, but easy. */
6738 return simplify_giv_expr (loop,
6739 gen_rtx_MINUS (mode,
6740 gen_rtx_NEG (mode, XEXP (x, 0)),
6741 const1_rtx),
6742 ext_val, benefit);
6744 case USE:
6745 /* Already in proper form for invariant. */
6746 return x;
6748 case SIGN_EXTEND:
6749 case ZERO_EXTEND:
6750 case TRUNCATE:
6751 /* Conditionally recognize extensions of simple IVs. After we've
6752 computed loop traversal counts and verified the range of the
6753 source IV, we'll reevaluate this as a GIV. */
6754 if (*ext_val == NULL_RTX)
6756 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6757 if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
6759 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
6760 return arg0;
6763 goto do_default;
6765 case REG:
6766 /* If this is a new register, we can't deal with it. */
6767 if (REGNO (x) >= max_reg_before_loop)
6768 return 0;
6770 /* Check for biv or giv. */
6771 switch (REG_IV_TYPE (ivs, REGNO (x)))
6773 case BASIC_INDUCT:
6774 return x;
6775 case GENERAL_INDUCT:
6777 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
6779 /* Form expression from giv and add benefit. Ensure this giv
6780 can derive another and subtract any needed adjustment if so. */
6782 /* Increasing the benefit here is risky. The only case in which it
6783 is arguably correct is if this is the only use of V. In other
6784 cases, this will artificially inflate the benefit of the current
6785 giv, and lead to suboptimal code. Thus, it is disabled, since
6786 potentially not reducing an only marginally beneficial giv is
6787 less harmful than reducing many givs that are not really
6788 beneficial. */
6790 rtx single_use = regs->array[REGNO (x)].single_usage;
6791 if (single_use && single_use != const0_rtx)
6792 *benefit += v->benefit;
6795 if (v->cant_derive)
6796 return 0;
6798 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
6799 v->src_reg, v->mult_val),
6800 v->add_val);
6802 if (v->derive_adjustment)
6803 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
6804 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
6805 if (*ext_val)
6807 if (!v->ext_dependent)
6808 return arg0;
6810 else
6812 *ext_val = v->ext_dependent;
6813 return arg0;
6815 return 0;
6818 default:
6819 do_default:
6820 /* If it isn't an induction variable, and it is invariant, we
6821 may be able to simplify things further by looking through
6822 the bits we just moved outside the loop. */
6823 if (loop_invariant_p (loop, x) == 1)
6825 struct movable *m;
6826 struct loop_movables *movables = LOOP_MOVABLES (loop);
6828 for (m = movables->head; m; m = m->next)
6829 if (rtx_equal_p (x, m->set_dest))
6831 /* Ok, we found a match. Substitute and simplify. */
6833 /* If we match another movable, we must use that, as
6834 this one is going away. */
6835 if (m->match)
6836 return simplify_giv_expr (loop, m->match->set_dest,
6837 ext_val, benefit);
6839 /* If consec is non-zero, this is a member of a group of
6840 instructions that were moved together. We handle this
6841 case only to the point of seeking to the last insn and
6842 looking for a REG_EQUAL. Fail if we don't find one. */
6843 if (m->consec != 0)
6845 int i = m->consec;
6846 tem = m->insn;
6849 tem = NEXT_INSN (tem);
6851 while (--i > 0);
6853 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6854 if (tem)
6855 tem = XEXP (tem, 0);
6857 else
6859 tem = single_set (m->insn);
6860 if (tem)
6861 tem = SET_SRC (tem);
6864 if (tem)
6866 /* What we are most interested in is pointer
6867 arithmetic on invariants -- only take
6868 patterns we may be able to do something with. */
6869 if (GET_CODE (tem) == PLUS
6870 || GET_CODE (tem) == MULT
6871 || GET_CODE (tem) == ASHIFT
6872 || GET_CODE (tem) == CONST_INT
6873 || GET_CODE (tem) == SYMBOL_REF)
6875 tem = simplify_giv_expr (loop, tem, ext_val,
6876 benefit);
6877 if (tem)
6878 return tem;
6880 else if (GET_CODE (tem) == CONST
6881 && GET_CODE (XEXP (tem, 0)) == PLUS
6882 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6883 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6885 tem = simplify_giv_expr (loop, XEXP (tem, 0),
6886 ext_val, benefit);
6887 if (tem)
6888 return tem;
6891 break;
6894 break;
6897 /* Fall through to general case. */
6898 default:
6899 /* If invariant, return as USE (unless CONST_INT).
6900 Otherwise, not giv. */
6901 if (GET_CODE (x) == USE)
6902 x = XEXP (x, 0);
6904 if (loop_invariant_p (loop, x) == 1)
6906 if (GET_CODE (x) == CONST_INT)
6907 return x;
6908 if (GET_CODE (x) == CONST
6909 && GET_CODE (XEXP (x, 0)) == PLUS
6910 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6911 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6912 x = XEXP (x, 0);
6913 return gen_rtx_USE (mode, x);
6915 else
6916 return 0;
6920 /* This routine folds invariants such that there is only ever one
6921 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6923 static rtx
6924 sge_plus_constant (x, c)
6925 rtx x, c;
6927 if (GET_CODE (x) == CONST_INT)
6928 return GEN_INT (INTVAL (x) + INTVAL (c));
6929 else if (GET_CODE (x) != PLUS)
6930 return gen_rtx_PLUS (GET_MODE (x), x, c);
6931 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6933 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6934 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6936 else if (GET_CODE (XEXP (x, 0)) == PLUS
6937 || GET_CODE (XEXP (x, 1)) != PLUS)
6939 return gen_rtx_PLUS (GET_MODE (x),
6940 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6942 else
6944 return gen_rtx_PLUS (GET_MODE (x),
6945 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6949 static rtx
6950 sge_plus (mode, x, y)
6951 enum machine_mode mode;
6952 rtx x, y;
6954 while (GET_CODE (y) == PLUS)
6956 rtx a = XEXP (y, 0);
6957 if (GET_CODE (a) == CONST_INT)
6958 x = sge_plus_constant (x, a);
6959 else
6960 x = gen_rtx_PLUS (mode, x, a);
6961 y = XEXP (y, 1);
6963 if (GET_CODE (y) == CONST_INT)
6964 x = sge_plus_constant (x, y);
6965 else
6966 x = gen_rtx_PLUS (mode, x, y);
6967 return x;
6970 /* Help detect a giv that is calculated by several consecutive insns;
6971 for example,
6972 giv = biv * M
6973 giv = giv + A
6974 The caller has already identified the first insn P as having a giv as dest;
6975 we check that all other insns that set the same register follow
6976 immediately after P, that they alter nothing else,
6977 and that the result of the last is still a giv.
6979 The value is 0 if the reg set in P is not really a giv.
6980 Otherwise, the value is the amount gained by eliminating
6981 all the consecutive insns that compute the value.
6983 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6984 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6986 The coefficients of the ultimate giv value are stored in
6987 *MULT_VAL and *ADD_VAL. */
6989 static int
6990 consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
6991 add_val, mult_val, ext_val, last_consec_insn)
6992 const struct loop *loop;
6993 int first_benefit;
6994 rtx p;
6995 rtx src_reg;
6996 rtx dest_reg;
6997 rtx *add_val;
6998 rtx *mult_val;
6999 rtx *ext_val;
7000 rtx *last_consec_insn;
7002 struct loop_ivs *ivs = LOOP_IVS (loop);
7003 struct loop_regs *regs = LOOP_REGS (loop);
7004 int count;
7005 enum rtx_code code;
7006 int benefit;
7007 rtx temp;
7008 rtx set;
7010 /* Indicate that this is a giv so that we can update the value produced in
7011 each insn of the multi-insn sequence.
7013 This induction structure will be used only by the call to
7014 general_induction_var below, so we can allocate it on our stack.
7015 If this is a giv, our caller will replace the induct var entry with
7016 a new induction structure. */
7017 struct induction *v;
7019 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
7020 return 0;
7022 v = (struct induction *) alloca (sizeof (struct induction));
7023 v->src_reg = src_reg;
7024 v->mult_val = *mult_val;
7025 v->add_val = *add_val;
7026 v->benefit = first_benefit;
7027 v->cant_derive = 0;
7028 v->derive_adjustment = 0;
7029 v->ext_dependent = NULL_RTX;
7031 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
7032 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
7034 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
7036 while (count > 0)
7038 p = NEXT_INSN (p);
7039 code = GET_CODE (p);
7041 /* If libcall, skip to end of call sequence. */
7042 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
7043 p = XEXP (temp, 0);
7045 if (code == INSN
7046 && (set = single_set (p))
7047 && GET_CODE (SET_DEST (set)) == REG
7048 && SET_DEST (set) == dest_reg
7049 && (general_induction_var (loop, SET_SRC (set), &src_reg,
7050 add_val, mult_val, ext_val, 0,
7051 &benefit, VOIDmode)
7052 /* Giv created by equivalent expression. */
7053 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
7054 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
7055 add_val, mult_val, ext_val, 0,
7056 &benefit, VOIDmode)))
7057 && src_reg == v->src_reg)
7059 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
7060 benefit += libcall_benefit (p);
7062 count--;
7063 v->mult_val = *mult_val;
7064 v->add_val = *add_val;
7065 v->benefit += benefit;
7067 else if (code != NOTE)
7069 /* Allow insns that set something other than this giv to a
7070 constant. Such insns are needed on machines which cannot
7071 include long constants and should not disqualify a giv. */
7072 if (code == INSN
7073 && (set = single_set (p))
7074 && SET_DEST (set) != dest_reg
7075 && CONSTANT_P (SET_SRC (set)))
7076 continue;
7078 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7079 return 0;
7083 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7084 *last_consec_insn = p;
7085 return v->benefit;
7088 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7089 represented by G1. If no such expression can be found, or it is clear that
7090 it cannot possibly be a valid address, 0 is returned.
7092 To perform the computation, we note that
7093 G1 = x * v + a and
7094 G2 = y * v + b
7095 where `v' is the biv.
7097 So G2 = (y/b) * G1 + (b - a*y/x).
7099 Note that MULT = y/x.
7101 Update: A and B are now allowed to be additive expressions such that
7102 B contains all variables in A. That is, computing B-A will not require
7103 subtracting variables. */
7105 static rtx
7106 express_from_1 (a, b, mult)
7107 rtx a, b, mult;
7109 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
7111 if (mult == const0_rtx)
7112 return b;
7114 /* If MULT is not 1, we cannot handle A with non-constants, since we
7115 would then be required to subtract multiples of the registers in A.
7116 This is theoretically possible, and may even apply to some Fortran
7117 constructs, but it is a lot of work and we do not attempt it here. */
7119 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
7120 return NULL_RTX;
7122 /* In general these structures are sorted top to bottom (down the PLUS
7123 chain), but not left to right across the PLUS. If B is a higher
7124 order giv than A, we can strip one level and recurse. If A is higher
7125 order, we'll eventually bail out, but won't know that until the end.
7126 If they are the same, we'll strip one level around this loop. */
7128 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
7130 rtx ra, rb, oa, ob, tmp;
7132 ra = XEXP (a, 0), oa = XEXP (a, 1);
7133 if (GET_CODE (ra) == PLUS)
7134 tmp = ra, ra = oa, oa = tmp;
7136 rb = XEXP (b, 0), ob = XEXP (b, 1);
7137 if (GET_CODE (rb) == PLUS)
7138 tmp = rb, rb = ob, ob = tmp;
7140 if (rtx_equal_p (ra, rb))
7141 /* We matched: remove one reg completely. */
7142 a = oa, b = ob;
7143 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
7144 /* An alternate match. */
7145 a = oa, b = rb;
7146 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
7147 /* An alternate match. */
7148 a = ra, b = ob;
7149 else
7151 /* Indicates an extra register in B. Strip one level from B and
7152 recurse, hoping B was the higher order expression. */
7153 ob = express_from_1 (a, ob, mult);
7154 if (ob == NULL_RTX)
7155 return NULL_RTX;
7156 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
7160 /* Here we are at the last level of A, go through the cases hoping to
7161 get rid of everything but a constant. */
7163 if (GET_CODE (a) == PLUS)
7165 rtx ra, oa;
7167 ra = XEXP (a, 0), oa = XEXP (a, 1);
7168 if (rtx_equal_p (oa, b))
7169 oa = ra;
7170 else if (!rtx_equal_p (ra, b))
7171 return NULL_RTX;
7173 if (GET_CODE (oa) != CONST_INT)
7174 return NULL_RTX;
7176 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
7178 else if (GET_CODE (a) == CONST_INT)
7180 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
7182 else if (CONSTANT_P (a))
7184 enum machine_mode mode_a = GET_MODE (a);
7185 enum machine_mode mode_b = GET_MODE (b);
7186 enum machine_mode mode = mode_b == VOIDmode ? mode_a : mode_b;
7187 return simplify_gen_binary (MINUS, mode, b, a);
7189 else if (GET_CODE (b) == PLUS)
7191 if (rtx_equal_p (a, XEXP (b, 0)))
7192 return XEXP (b, 1);
7193 else if (rtx_equal_p (a, XEXP (b, 1)))
7194 return XEXP (b, 0);
7195 else
7196 return NULL_RTX;
7198 else if (rtx_equal_p (a, b))
7199 return const0_rtx;
7201 return NULL_RTX;
7205 express_from (g1, g2)
7206 struct induction *g1, *g2;
7208 rtx mult, add;
7210 /* The value that G1 will be multiplied by must be a constant integer. Also,
7211 the only chance we have of getting a valid address is if b*c/a (see above
7212 for notation) is also an integer. */
7213 if (GET_CODE (g1->mult_val) == CONST_INT
7214 && GET_CODE (g2->mult_val) == CONST_INT)
7216 if (g1->mult_val == const0_rtx
7217 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
7218 return NULL_RTX;
7219 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
7221 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
7222 mult = const1_rtx;
7223 else
7225 /* ??? Find out if the one is a multiple of the other? */
7226 return NULL_RTX;
7229 add = express_from_1 (g1->add_val, g2->add_val, mult);
7230 if (add == NULL_RTX)
7232 /* Failed. If we've got a multiplication factor between G1 and G2,
7233 scale G1's addend and try again. */
7234 if (INTVAL (mult) > 1)
7236 rtx g1_add_val = g1->add_val;
7237 if (GET_CODE (g1_add_val) == MULT
7238 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
7240 HOST_WIDE_INT m;
7241 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
7242 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
7243 XEXP (g1_add_val, 0), GEN_INT (m));
7245 else
7247 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
7248 mult);
7251 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
7254 if (add == NULL_RTX)
7255 return NULL_RTX;
7257 /* Form simplified final result. */
7258 if (mult == const0_rtx)
7259 return add;
7260 else if (mult == const1_rtx)
7261 mult = g1->dest_reg;
7262 else
7263 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
7265 if (add == const0_rtx)
7266 return mult;
7267 else
7269 if (GET_CODE (add) == PLUS
7270 && CONSTANT_P (XEXP (add, 1)))
7272 rtx tem = XEXP (add, 1);
7273 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
7274 add = tem;
7277 return gen_rtx_PLUS (g2->mode, mult, add);
7281 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7282 represented by G1. This indicates that G2 should be combined with G1 and
7283 that G2 can use (either directly or via an address expression) a register
7284 used to represent G1. */
7286 static rtx
7287 combine_givs_p (g1, g2)
7288 struct induction *g1, *g2;
7290 rtx comb, ret;
7292 /* With the introduction of ext dependent givs, we must care for modes.
7293 G2 must not use a wider mode than G1. */
7294 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
7295 return NULL_RTX;
7297 ret = comb = express_from (g1, g2);
7298 if (comb == NULL_RTX)
7299 return NULL_RTX;
7300 if (g1->mode != g2->mode)
7301 ret = gen_lowpart (g2->mode, comb);
7303 /* If these givs are identical, they can be combined. We use the results
7304 of express_from because the addends are not in a canonical form, so
7305 rtx_equal_p is a weaker test. */
7306 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
7307 combination to be the other way round. */
7308 if (comb == g1->dest_reg
7309 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
7311 return ret;
7314 /* If G2 can be expressed as a function of G1 and that function is valid
7315 as an address and no more expensive than using a register for G2,
7316 the expression of G2 in terms of G1 can be used. */
7317 if (ret != NULL_RTX
7318 && g2->giv_type == DEST_ADDR
7319 && memory_address_p (GET_MODE (g2->mem), ret)
7320 /* ??? Looses, especially with -fforce-addr, where *g2->location
7321 will always be a register, and so anything more complicated
7322 gets discarded. */
7323 #if 0
7324 #ifdef ADDRESS_COST
7325 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
7326 #else
7327 && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
7328 #endif
7329 #endif
7332 return ret;
7335 return NULL_RTX;
7338 /* Check each extension dependent giv in this class to see if its
7339 root biv is safe from wrapping in the interior mode, which would
7340 make the giv illegal. */
7342 static void
7343 check_ext_dependent_givs (bl, loop_info)
7344 struct iv_class *bl;
7345 struct loop_info *loop_info;
7347 int ze_ok = 0, se_ok = 0, info_ok = 0;
7348 enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
7349 HOST_WIDE_INT start_val;
7350 unsigned HOST_WIDE_INT u_end_val = 0;
7351 unsigned HOST_WIDE_INT u_start_val = 0;
7352 rtx incr = pc_rtx;
7353 struct induction *v;
7355 /* Make sure the iteration data is available. We must have
7356 constants in order to be certain of no overflow. */
7357 /* ??? An unknown iteration count with an increment of +-1
7358 combined with friendly exit tests of against an invariant
7359 value is also ameanable to optimization. Not implemented. */
7360 if (loop_info->n_iterations > 0
7361 && bl->initial_value
7362 && GET_CODE (bl->initial_value) == CONST_INT
7363 && (incr = biv_total_increment (bl))
7364 && GET_CODE (incr) == CONST_INT
7365 /* Make sure the host can represent the arithmetic. */
7366 && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
7368 unsigned HOST_WIDE_INT abs_incr, total_incr;
7369 HOST_WIDE_INT s_end_val;
7370 int neg_incr;
7372 info_ok = 1;
7373 start_val = INTVAL (bl->initial_value);
7374 u_start_val = start_val;
7376 neg_incr = 0, abs_incr = INTVAL (incr);
7377 if (INTVAL (incr) < 0)
7378 neg_incr = 1, abs_incr = -abs_incr;
7379 total_incr = abs_incr * loop_info->n_iterations;
7381 /* Check for host arithmatic overflow. */
7382 if (total_incr / loop_info->n_iterations == abs_incr)
7384 unsigned HOST_WIDE_INT u_max;
7385 HOST_WIDE_INT s_max;
7387 u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
7388 s_end_val = u_end_val;
7389 u_max = GET_MODE_MASK (biv_mode);
7390 s_max = u_max >> 1;
7392 /* Check zero extension of biv ok. */
7393 if (start_val >= 0
7394 /* Check for host arithmatic overflow. */
7395 && (neg_incr
7396 ? u_end_val < u_start_val
7397 : u_end_val > u_start_val)
7398 /* Check for target arithmetic overflow. */
7399 && (neg_incr
7400 ? 1 /* taken care of with host overflow */
7401 : u_end_val <= u_max))
7403 ze_ok = 1;
7406 /* Check sign extension of biv ok. */
7407 /* ??? While it is true that overflow with signed and pointer
7408 arithmetic is undefined, I fear too many programmers don't
7409 keep this fact in mind -- myself included on occasion.
7410 So leave alone with the signed overflow optimizations. */
7411 if (start_val >= -s_max - 1
7412 /* Check for host arithmatic overflow. */
7413 && (neg_incr
7414 ? s_end_val < start_val
7415 : s_end_val > start_val)
7416 /* Check for target arithmetic overflow. */
7417 && (neg_incr
7418 ? s_end_val >= -s_max - 1
7419 : s_end_val <= s_max))
7421 se_ok = 1;
7426 /* Invalidate givs that fail the tests. */
7427 for (v = bl->giv; v; v = v->next_iv)
7428 if (v->ext_dependent)
7430 enum rtx_code code = GET_CODE (v->ext_dependent);
7431 int ok = 0;
7433 switch (code)
7435 case SIGN_EXTEND:
7436 ok = se_ok;
7437 break;
7438 case ZERO_EXTEND:
7439 ok = ze_ok;
7440 break;
7442 case TRUNCATE:
7443 /* We don't know whether this value is being used as either
7444 signed or unsigned, so to safely truncate we must satisfy
7445 both. The initial check here verifies the BIV itself;
7446 once that is successful we may check its range wrt the
7447 derived GIV. */
7448 if (se_ok && ze_ok)
7450 enum machine_mode outer_mode = GET_MODE (v->ext_dependent);
7451 unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
7453 /* We know from the above that both endpoints are nonnegative,
7454 and that there is no wrapping. Verify that both endpoints
7455 are within the (signed) range of the outer mode. */
7456 if (u_start_val <= max && u_end_val <= max)
7457 ok = 1;
7459 break;
7461 default:
7462 abort ();
7465 if (ok)
7467 if (loop_dump_stream)
7469 fprintf (loop_dump_stream,
7470 "Verified ext dependent giv at %d of reg %d\n",
7471 INSN_UID (v->insn), bl->regno);
7474 else
7476 if (loop_dump_stream)
7478 const char *why;
7480 if (info_ok)
7481 why = "biv iteration values overflowed";
7482 else
7484 if (incr == pc_rtx)
7485 incr = biv_total_increment (bl);
7486 if (incr == const1_rtx)
7487 why = "biv iteration info incomplete; incr by 1";
7488 else
7489 why = "biv iteration info incomplete";
7492 fprintf (loop_dump_stream,
7493 "Failed ext dependent giv at %d, %s\n",
7494 INSN_UID (v->insn), why);
7496 v->ignore = 1;
7497 bl->all_reduced = 0;
7502 /* Generate a version of VALUE in a mode appropriate for initializing V. */
7505 extend_value_for_giv (v, value)
7506 struct induction *v;
7507 rtx value;
7509 rtx ext_dep = v->ext_dependent;
7511 if (! ext_dep)
7512 return value;
7514 /* Recall that check_ext_dependent_givs verified that the known bounds
7515 of a biv did not overflow or wrap with respect to the extension for
7516 the giv. Therefore, constants need no additional adjustment. */
7517 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
7518 return value;
7520 /* Otherwise, we must adjust the value to compensate for the
7521 differing modes of the biv and the giv. */
7522 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
7525 struct combine_givs_stats
7527 int giv_number;
7528 int total_benefit;
7531 static int
7532 cmp_combine_givs_stats (xp, yp)
7533 const PTR xp;
7534 const PTR yp;
7536 const struct combine_givs_stats * const x =
7537 (const struct combine_givs_stats *) xp;
7538 const struct combine_givs_stats * const y =
7539 (const struct combine_givs_stats *) yp;
7540 int d;
7541 d = y->total_benefit - x->total_benefit;
7542 /* Stabilize the sort. */
7543 if (!d)
7544 d = x->giv_number - y->giv_number;
7545 return d;
7548 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7549 any other. If so, point SAME to the giv combined with and set NEW_REG to
7550 be an expression (in terms of the other giv's DEST_REG) equivalent to the
7551 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
7553 static void
7554 combine_givs (regs, bl)
7555 struct loop_regs *regs;
7556 struct iv_class *bl;
7558 /* Additional benefit to add for being combined multiple times. */
7559 const int extra_benefit = 3;
7561 struct induction *g1, *g2, **giv_array;
7562 int i, j, k, giv_count;
7563 struct combine_givs_stats *stats;
7564 rtx *can_combine;
7566 /* Count givs, because bl->giv_count is incorrect here. */
7567 giv_count = 0;
7568 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7569 if (!g1->ignore)
7570 giv_count++;
7572 giv_array
7573 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
7574 i = 0;
7575 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7576 if (!g1->ignore)
7577 giv_array[i++] = g1;
7579 stats = (struct combine_givs_stats *) xcalloc (giv_count, sizeof (*stats));
7580 can_combine = (rtx *) xcalloc (giv_count, giv_count * sizeof (rtx));
7582 for (i = 0; i < giv_count; i++)
7584 int this_benefit;
7585 rtx single_use;
7587 g1 = giv_array[i];
7588 stats[i].giv_number = i;
7590 /* If a DEST_REG GIV is used only once, do not allow it to combine
7591 with anything, for in doing so we will gain nothing that cannot
7592 be had by simply letting the GIV with which we would have combined
7593 to be reduced on its own. The losage shows up in particular with
7594 DEST_ADDR targets on hosts with reg+reg addressing, though it can
7595 be seen elsewhere as well. */
7596 if (g1->giv_type == DEST_REG
7597 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
7598 && single_use != const0_rtx)
7599 continue;
7601 this_benefit = g1->benefit;
7602 /* Add an additional weight for zero addends. */
7603 if (g1->no_const_addval)
7604 this_benefit += 1;
7606 for (j = 0; j < giv_count; j++)
7608 rtx this_combine;
7610 g2 = giv_array[j];
7611 if (g1 != g2
7612 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
7614 can_combine[i * giv_count + j] = this_combine;
7615 this_benefit += g2->benefit + extra_benefit;
7618 stats[i].total_benefit = this_benefit;
7621 /* Iterate, combining until we can't. */
7622 restart:
7623 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
7625 if (loop_dump_stream)
7627 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
7628 for (k = 0; k < giv_count; k++)
7630 g1 = giv_array[stats[k].giv_number];
7631 if (!g1->combined_with && !g1->same)
7632 fprintf (loop_dump_stream, " {%d, %d}",
7633 INSN_UID (giv_array[stats[k].giv_number]->insn),
7634 stats[k].total_benefit);
7636 putc ('\n', loop_dump_stream);
7639 for (k = 0; k < giv_count; k++)
7641 int g1_add_benefit = 0;
7643 i = stats[k].giv_number;
7644 g1 = giv_array[i];
7646 /* If it has already been combined, skip. */
7647 if (g1->combined_with || g1->same)
7648 continue;
7650 for (j = 0; j < giv_count; j++)
7652 g2 = giv_array[j];
7653 if (g1 != g2 && can_combine[i * giv_count + j]
7654 /* If it has already been combined, skip. */
7655 && ! g2->same && ! g2->combined_with)
7657 int l;
7659 g2->new_reg = can_combine[i * giv_count + j];
7660 g2->same = g1;
7661 /* For destination, we now may replace by mem expression instead
7662 of register. This changes the costs considerably, so add the
7663 compensation. */
7664 if (g2->giv_type == DEST_ADDR)
7665 g2->benefit = (g2->benefit + reg_address_cost
7666 - address_cost (g2->new_reg,
7667 GET_MODE (g2->mem)));
7668 g1->combined_with++;
7669 g1->lifetime += g2->lifetime;
7671 g1_add_benefit += g2->benefit;
7673 /* ??? The new final_[bg]iv_value code does a much better job
7674 of finding replaceable giv's, and hence this code may no
7675 longer be necessary. */
7676 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
7677 g1_add_benefit -= copy_cost;
7679 /* To help optimize the next set of combinations, remove
7680 this giv from the benefits of other potential mates. */
7681 for (l = 0; l < giv_count; ++l)
7683 int m = stats[l].giv_number;
7684 if (can_combine[m * giv_count + j])
7685 stats[l].total_benefit -= g2->benefit + extra_benefit;
7688 if (loop_dump_stream)
7689 fprintf (loop_dump_stream,
7690 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
7691 INSN_UID (g2->insn), INSN_UID (g1->insn),
7692 g1->benefit, g1_add_benefit, g1->lifetime);
7696 /* To help optimize the next set of combinations, remove
7697 this giv from the benefits of other potential mates. */
7698 if (g1->combined_with)
7700 for (j = 0; j < giv_count; ++j)
7702 int m = stats[j].giv_number;
7703 if (can_combine[m * giv_count + i])
7704 stats[j].total_benefit -= g1->benefit + extra_benefit;
7707 g1->benefit += g1_add_benefit;
7709 /* We've finished with this giv, and everything it touched.
7710 Restart the combination so that proper weights for the
7711 rest of the givs are properly taken into account. */
7712 /* ??? Ideally we would compact the arrays at this point, so
7713 as to not cover old ground. But sanely compacting
7714 can_combine is tricky. */
7715 goto restart;
7719 /* Clean up. */
7720 free (stats);
7721 free (can_combine);
7724 /* Generate sequence for REG = B * M + A. */
7726 static rtx
7727 gen_add_mult (b, m, a, reg)
7728 rtx b; /* initial value of basic induction variable */
7729 rtx m; /* multiplicative constant */
7730 rtx a; /* additive constant */
7731 rtx reg; /* destination register */
7733 rtx seq;
7734 rtx result;
7736 start_sequence ();
7737 /* Use unsigned arithmetic. */
7738 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7739 if (reg != result)
7740 emit_move_insn (reg, result);
7741 seq = gen_sequence ();
7742 end_sequence ();
7744 return seq;
7748 /* Update registers created in insn sequence SEQ. */
7750 static void
7751 loop_regs_update (loop, seq)
7752 const struct loop *loop ATTRIBUTE_UNUSED;
7753 rtx seq;
7755 /* Update register info for alias analysis. */
7757 if (GET_CODE (seq) == SEQUENCE)
7759 int i;
7760 for (i = 0; i < XVECLEN (seq, 0); ++i)
7762 rtx set = single_set (XVECEXP (seq, 0, i));
7763 if (set && GET_CODE (SET_DEST (set)) == REG)
7764 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7767 else
7769 if (GET_CODE (seq) == SET
7770 && GET_CODE (SET_DEST (seq)) == REG)
7771 record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
7776 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. */
7778 void
7779 loop_iv_add_mult_emit_before (loop, b, m, a, reg, before_bb, before_insn)
7780 const struct loop *loop;
7781 rtx b; /* initial value of basic induction variable */
7782 rtx m; /* multiplicative constant */
7783 rtx a; /* additive constant */
7784 rtx reg; /* destination register */
7785 basic_block before_bb;
7786 rtx before_insn;
7788 rtx seq;
7790 if (! before_insn)
7792 loop_iv_add_mult_hoist (loop, b, m, a, reg);
7793 return;
7796 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7797 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7799 /* Increase the lifetime of any invariants moved further in code. */
7800 update_reg_last_use (a, before_insn);
7801 update_reg_last_use (b, before_insn);
7802 update_reg_last_use (m, before_insn);
7804 loop_insn_emit_before (loop, before_bb, before_insn, seq);
7806 /* It is possible that the expansion created lots of new registers.
7807 Iterate over the sequence we just created and record them all. */
7808 loop_regs_update (loop, seq);
7812 /* Emit insns in loop pre-header to set REG = B * M + A. */
7814 void
7815 loop_iv_add_mult_sink (loop, b, m, a, reg)
7816 const struct loop *loop;
7817 rtx b; /* initial value of basic induction variable */
7818 rtx m; /* multiplicative constant */
7819 rtx a; /* additive constant */
7820 rtx reg; /* destination register */
7822 rtx seq;
7824 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7825 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7827 /* Increase the lifetime of any invariants moved further in code.
7828 ???? Is this really necessary? */
7829 update_reg_last_use (a, loop->sink);
7830 update_reg_last_use (b, loop->sink);
7831 update_reg_last_use (m, loop->sink);
7833 loop_insn_sink (loop, seq);
7835 /* It is possible that the expansion created lots of new registers.
7836 Iterate over the sequence we just created and record them all. */
7837 loop_regs_update (loop, seq);
7841 /* Emit insns after loop to set REG = B * M + A. */
7843 void
7844 loop_iv_add_mult_hoist (loop, b, m, a, reg)
7845 const struct loop *loop;
7846 rtx b; /* initial value of basic induction variable */
7847 rtx m; /* multiplicative constant */
7848 rtx a; /* additive constant */
7849 rtx reg; /* destination register */
7851 rtx seq;
7853 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7854 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7856 loop_insn_hoist (loop, seq);
7858 /* It is possible that the expansion created lots of new registers.
7859 Iterate over the sequence we just created and record them all. */
7860 loop_regs_update (loop, seq);
7865 /* Similar to gen_add_mult, but compute cost rather than generating
7866 sequence. */
7868 static int
7869 iv_add_mult_cost (b, m, a, reg)
7870 rtx b; /* initial value of basic induction variable */
7871 rtx m; /* multiplicative constant */
7872 rtx a; /* additive constant */
7873 rtx reg; /* destination register */
7875 int cost = 0;
7876 rtx last, result;
7878 start_sequence ();
7879 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7880 if (reg != result)
7881 emit_move_insn (reg, result);
7882 last = get_last_insn ();
7883 while (last)
7885 rtx t = single_set (last);
7886 if (t)
7887 cost += rtx_cost (SET_SRC (t), SET);
7888 last = PREV_INSN (last);
7890 end_sequence ();
7891 return cost;
7894 /* Test whether A * B can be computed without
7895 an actual multiply insn. Value is 1 if so. */
7897 static int
7898 product_cheap_p (a, b)
7899 rtx a;
7900 rtx b;
7902 int i;
7903 rtx tmp;
7904 int win = 1;
7906 /* If only one is constant, make it B. */
7907 if (GET_CODE (a) == CONST_INT)
7908 tmp = a, a = b, b = tmp;
7910 /* If first constant, both constant, so don't need multiply. */
7911 if (GET_CODE (a) == CONST_INT)
7912 return 1;
7914 /* If second not constant, neither is constant, so would need multiply. */
7915 if (GET_CODE (b) != CONST_INT)
7916 return 0;
7918 /* One operand is constant, so might not need multiply insn. Generate the
7919 code for the multiply and see if a call or multiply, or long sequence
7920 of insns is generated. */
7922 start_sequence ();
7923 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
7924 tmp = gen_sequence ();
7925 end_sequence ();
7927 if (GET_CODE (tmp) == SEQUENCE)
7929 if (XVEC (tmp, 0) == 0)
7930 win = 1;
7931 else if (XVECLEN (tmp, 0) > 3)
7932 win = 0;
7933 else
7934 for (i = 0; i < XVECLEN (tmp, 0); i++)
7936 rtx insn = XVECEXP (tmp, 0, i);
7938 if (GET_CODE (insn) != INSN
7939 || (GET_CODE (PATTERN (insn)) == SET
7940 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7941 || (GET_CODE (PATTERN (insn)) == PARALLEL
7942 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7943 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7945 win = 0;
7946 break;
7950 else if (GET_CODE (tmp) == SET
7951 && GET_CODE (SET_SRC (tmp)) == MULT)
7952 win = 0;
7953 else if (GET_CODE (tmp) == PARALLEL
7954 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7955 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7956 win = 0;
7958 return win;
7961 /* Check to see if loop can be terminated by a "decrement and branch until
7962 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7963 Also try reversing an increment loop to a decrement loop
7964 to see if the optimization can be performed.
7965 Value is nonzero if optimization was performed. */
7967 /* This is useful even if the architecture doesn't have such an insn,
7968 because it might change a loops which increments from 0 to n to a loop
7969 which decrements from n to 0. A loop that decrements to zero is usually
7970 faster than one that increments from zero. */
7972 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7973 such as approx_final_value, biv_total_increment, loop_iterations, and
7974 final_[bg]iv_value. */
7976 static int
7977 check_dbra_loop (loop, insn_count)
7978 struct loop *loop;
7979 int insn_count;
7981 struct loop_info *loop_info = LOOP_INFO (loop);
7982 struct loop_regs *regs = LOOP_REGS (loop);
7983 struct loop_ivs *ivs = LOOP_IVS (loop);
7984 struct iv_class *bl;
7985 rtx reg;
7986 rtx jump_label;
7987 rtx final_value;
7988 rtx start_value;
7989 rtx new_add_val;
7990 rtx comparison;
7991 rtx before_comparison;
7992 rtx p;
7993 rtx jump;
7994 rtx first_compare;
7995 int compare_and_branch;
7996 rtx loop_start = loop->start;
7997 rtx loop_end = loop->end;
7999 /* If last insn is a conditional branch, and the insn before tests a
8000 register value, try to optimize it. Otherwise, we can't do anything. */
8002 jump = PREV_INSN (loop_end);
8003 comparison = get_condition_for_loop (loop, jump);
8004 if (comparison == 0)
8005 return 0;
8006 if (!onlyjump_p (jump))
8007 return 0;
8009 /* Try to compute whether the compare/branch at the loop end is one or
8010 two instructions. */
8011 get_condition (jump, &first_compare);
8012 if (first_compare == jump)
8013 compare_and_branch = 1;
8014 else if (first_compare == prev_nonnote_insn (jump))
8015 compare_and_branch = 2;
8016 else
8017 return 0;
8020 /* If more than one condition is present to control the loop, then
8021 do not proceed, as this function does not know how to rewrite
8022 loop tests with more than one condition.
8024 Look backwards from the first insn in the last comparison
8025 sequence and see if we've got another comparison sequence. */
8027 rtx jump1;
8028 if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
8029 if (GET_CODE (jump1) == JUMP_INSN)
8030 return 0;
8033 /* Check all of the bivs to see if the compare uses one of them.
8034 Skip biv's set more than once because we can't guarantee that
8035 it will be zero on the last iteration. Also skip if the biv is
8036 used between its update and the test insn. */
8038 for (bl = ivs->list; bl; bl = bl->next)
8040 if (bl->biv_count == 1
8041 && ! bl->biv->maybe_multiple
8042 && bl->biv->dest_reg == XEXP (comparison, 0)
8043 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
8044 first_compare))
8045 break;
8048 if (! bl)
8049 return 0;
8051 /* Look for the case where the basic induction variable is always
8052 nonnegative, and equals zero on the last iteration.
8053 In this case, add a reg_note REG_NONNEG, which allows the
8054 m68k DBRA instruction to be used. */
8056 if (((GET_CODE (comparison) == GT
8057 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
8058 && INTVAL (XEXP (comparison, 1)) == -1)
8059 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
8060 && GET_CODE (bl->biv->add_val) == CONST_INT
8061 && INTVAL (bl->biv->add_val) < 0)
8063 /* Initial value must be greater than 0,
8064 init_val % -dec_value == 0 to ensure that it equals zero on
8065 the last iteration */
8067 if (GET_CODE (bl->initial_value) == CONST_INT
8068 && INTVAL (bl->initial_value) > 0
8069 && (INTVAL (bl->initial_value)
8070 % (-INTVAL (bl->biv->add_val))) == 0)
8072 /* register always nonnegative, add REG_NOTE to branch */
8073 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8074 REG_NOTES (jump)
8075 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8076 REG_NOTES (jump));
8077 bl->nonneg = 1;
8079 return 1;
8082 /* If the decrement is 1 and the value was tested as >= 0 before
8083 the loop, then we can safely optimize. */
8084 for (p = loop_start; p; p = PREV_INSN (p))
8086 if (GET_CODE (p) == CODE_LABEL)
8087 break;
8088 if (GET_CODE (p) != JUMP_INSN)
8089 continue;
8091 before_comparison = get_condition_for_loop (loop, p);
8092 if (before_comparison
8093 && XEXP (before_comparison, 0) == bl->biv->dest_reg
8094 && GET_CODE (before_comparison) == LT
8095 && XEXP (before_comparison, 1) == const0_rtx
8096 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
8097 && INTVAL (bl->biv->add_val) == -1)
8099 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8100 REG_NOTES (jump)
8101 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8102 REG_NOTES (jump));
8103 bl->nonneg = 1;
8105 return 1;
8109 else if (GET_CODE (bl->biv->add_val) == CONST_INT
8110 && INTVAL (bl->biv->add_val) > 0)
8112 /* Try to change inc to dec, so can apply above optimization. */
8113 /* Can do this if:
8114 all registers modified are induction variables or invariant,
8115 all memory references have non-overlapping addresses
8116 (obviously true if only one write)
8117 allow 2 insns for the compare/jump at the end of the loop. */
8118 /* Also, we must avoid any instructions which use both the reversed
8119 biv and another biv. Such instructions will fail if the loop is
8120 reversed. We meet this condition by requiring that either
8121 no_use_except_counting is true, or else that there is only
8122 one biv. */
8123 int num_nonfixed_reads = 0;
8124 /* 1 if the iteration var is used only to count iterations. */
8125 int no_use_except_counting = 0;
8126 /* 1 if the loop has no memory store, or it has a single memory store
8127 which is reversible. */
8128 int reversible_mem_store = 1;
8130 if (bl->giv_count == 0
8131 && !loop->exit_count
8132 && !loop_info->has_multiple_exit_targets)
8134 rtx bivreg = regno_reg_rtx[bl->regno];
8135 struct iv_class *blt;
8137 /* If there are no givs for this biv, and the only exit is the
8138 fall through at the end of the loop, then
8139 see if perhaps there are no uses except to count. */
8140 no_use_except_counting = 1;
8141 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8142 if (INSN_P (p))
8144 rtx set = single_set (p);
8146 if (set && GET_CODE (SET_DEST (set)) == REG
8147 && REGNO (SET_DEST (set)) == bl->regno)
8148 /* An insn that sets the biv is okay. */
8150 else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
8151 || p == prev_nonnote_insn (loop_end))
8152 && reg_mentioned_p (bivreg, PATTERN (p)))
8154 /* If either of these insns uses the biv and sets a pseudo
8155 that has more than one usage, then the biv has uses
8156 other than counting since it's used to derive a value
8157 that is used more than one time. */
8158 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
8159 regs);
8160 if (regs->multiple_uses)
8162 no_use_except_counting = 0;
8163 break;
8166 else if (reg_mentioned_p (bivreg, PATTERN (p)))
8168 no_use_except_counting = 0;
8169 break;
8173 /* A biv has uses besides counting if it is used to set
8174 another biv. */
8175 for (blt = ivs->list; blt; blt = blt->next)
8176 if (blt->init_set
8177 && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
8179 no_use_except_counting = 0;
8180 break;
8184 if (no_use_except_counting)
8185 /* No need to worry about MEMs. */
8187 else if (loop_info->num_mem_sets <= 1)
8189 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8190 if (INSN_P (p))
8191 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
8193 /* If the loop has a single store, and the destination address is
8194 invariant, then we can't reverse the loop, because this address
8195 might then have the wrong value at loop exit.
8196 This would work if the source was invariant also, however, in that
8197 case, the insn should have been moved out of the loop. */
8199 if (loop_info->num_mem_sets == 1)
8201 struct induction *v;
8203 /* If we could prove that each of the memory locations
8204 written to was different, then we could reverse the
8205 store -- but we don't presently have any way of
8206 knowing that. */
8207 reversible_mem_store = 0;
8209 /* If the store depends on a register that is set after the
8210 store, it depends on the initial value, and is thus not
8211 reversible. */
8212 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
8214 if (v->giv_type == DEST_REG
8215 && reg_mentioned_p (v->dest_reg,
8216 PATTERN (loop_info->first_loop_store_insn))
8217 && loop_insn_first_p (loop_info->first_loop_store_insn,
8218 v->insn))
8219 reversible_mem_store = 0;
8223 else
8224 return 0;
8226 /* This code only acts for innermost loops. Also it simplifies
8227 the memory address check by only reversing loops with
8228 zero or one memory access.
8229 Two memory accesses could involve parts of the same array,
8230 and that can't be reversed.
8231 If the biv is used only for counting, than we don't need to worry
8232 about all these things. */
8234 if ((num_nonfixed_reads <= 1
8235 && ! loop_info->has_nonconst_call
8236 && ! loop_info->has_prefetch
8237 && ! loop_info->has_volatile
8238 && reversible_mem_store
8239 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
8240 + num_unmoved_movables (loop) + compare_and_branch == insn_count)
8241 && (bl == ivs->list && bl->next == 0))
8242 || (no_use_except_counting && ! loop_info->has_prefetch))
8244 rtx tem;
8246 /* Loop can be reversed. */
8247 if (loop_dump_stream)
8248 fprintf (loop_dump_stream, "Can reverse loop\n");
8250 /* Now check other conditions:
8252 The increment must be a constant, as must the initial value,
8253 and the comparison code must be LT.
8255 This test can probably be improved since +/- 1 in the constant
8256 can be obtained by changing LT to LE and vice versa; this is
8257 confusing. */
8259 if (comparison
8260 /* for constants, LE gets turned into LT */
8261 && (GET_CODE (comparison) == LT
8262 || (GET_CODE (comparison) == LE
8263 && no_use_except_counting)))
8265 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
8266 rtx initial_value, comparison_value;
8267 int nonneg = 0;
8268 enum rtx_code cmp_code;
8269 int comparison_const_width;
8270 unsigned HOST_WIDE_INT comparison_sign_mask;
8272 add_val = INTVAL (bl->biv->add_val);
8273 comparison_value = XEXP (comparison, 1);
8274 if (GET_MODE (comparison_value) == VOIDmode)
8275 comparison_const_width
8276 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
8277 else
8278 comparison_const_width
8279 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
8280 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
8281 comparison_const_width = HOST_BITS_PER_WIDE_INT;
8282 comparison_sign_mask
8283 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
8285 /* If the comparison value is not a loop invariant, then we
8286 can not reverse this loop.
8288 ??? If the insns which initialize the comparison value as
8289 a whole compute an invariant result, then we could move
8290 them out of the loop and proceed with loop reversal. */
8291 if (! loop_invariant_p (loop, comparison_value))
8292 return 0;
8294 if (GET_CODE (comparison_value) == CONST_INT)
8295 comparison_val = INTVAL (comparison_value);
8296 initial_value = bl->initial_value;
8298 /* Normalize the initial value if it is an integer and
8299 has no other use except as a counter. This will allow
8300 a few more loops to be reversed. */
8301 if (no_use_except_counting
8302 && GET_CODE (comparison_value) == CONST_INT
8303 && GET_CODE (initial_value) == CONST_INT)
8305 comparison_val = comparison_val - INTVAL (bl->initial_value);
8306 /* The code below requires comparison_val to be a multiple
8307 of add_val in order to do the loop reversal, so
8308 round up comparison_val to a multiple of add_val.
8309 Since comparison_value is constant, we know that the
8310 current comparison code is LT. */
8311 comparison_val = comparison_val + add_val - 1;
8312 comparison_val
8313 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
8314 /* We postpone overflow checks for COMPARISON_VAL here;
8315 even if there is an overflow, we might still be able to
8316 reverse the loop, if converting the loop exit test to
8317 NE is possible. */
8318 initial_value = const0_rtx;
8321 /* First check if we can do a vanilla loop reversal. */
8322 if (initial_value == const0_rtx
8323 /* If we have a decrement_and_branch_on_count,
8324 prefer the NE test, since this will allow that
8325 instruction to be generated. Note that we must
8326 use a vanilla loop reversal if the biv is used to
8327 calculate a giv or has a non-counting use. */
8328 #if ! defined (HAVE_decrement_and_branch_until_zero) \
8329 && defined (HAVE_decrement_and_branch_on_count)
8330 && (! (add_val == 1 && loop->vtop
8331 && (bl->biv_count == 0
8332 || no_use_except_counting)))
8333 #endif
8334 && GET_CODE (comparison_value) == CONST_INT
8335 /* Now do postponed overflow checks on COMPARISON_VAL. */
8336 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
8337 & comparison_sign_mask))
8339 /* Register will always be nonnegative, with value
8340 0 on last iteration */
8341 add_adjust = add_val;
8342 nonneg = 1;
8343 cmp_code = GE;
8345 else if (add_val == 1 && loop->vtop
8346 && (bl->biv_count == 0
8347 || no_use_except_counting))
8349 add_adjust = 0;
8350 cmp_code = NE;
8352 else
8353 return 0;
8355 if (GET_CODE (comparison) == LE)
8356 add_adjust -= add_val;
8358 /* If the initial value is not zero, or if the comparison
8359 value is not an exact multiple of the increment, then we
8360 can not reverse this loop. */
8361 if (initial_value == const0_rtx
8362 && GET_CODE (comparison_value) == CONST_INT)
8364 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
8365 return 0;
8367 else
8369 if (! no_use_except_counting || add_val != 1)
8370 return 0;
8373 final_value = comparison_value;
8375 /* Reset these in case we normalized the initial value
8376 and comparison value above. */
8377 if (GET_CODE (comparison_value) == CONST_INT
8378 && GET_CODE (initial_value) == CONST_INT)
8380 comparison_value = GEN_INT (comparison_val);
8381 final_value
8382 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
8384 bl->initial_value = initial_value;
8386 /* Save some info needed to produce the new insns. */
8387 reg = bl->biv->dest_reg;
8388 jump_label = condjump_label (PREV_INSN (loop_end));
8389 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
8391 /* Set start_value; if this is not a CONST_INT, we need
8392 to generate a SUB.
8393 Initialize biv to start_value before loop start.
8394 The old initializing insn will be deleted as a
8395 dead store by flow.c. */
8396 if (initial_value == const0_rtx
8397 && GET_CODE (comparison_value) == CONST_INT)
8399 start_value = GEN_INT (comparison_val - add_adjust);
8400 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
8402 else if (GET_CODE (initial_value) == CONST_INT)
8404 enum machine_mode mode = GET_MODE (reg);
8405 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
8406 rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
8408 if (add_insn == 0)
8409 return 0;
8411 start_value
8412 = gen_rtx_PLUS (mode, comparison_value, offset);
8413 loop_insn_hoist (loop, add_insn);
8414 if (GET_CODE (comparison) == LE)
8415 final_value = gen_rtx_PLUS (mode, comparison_value,
8416 GEN_INT (add_val));
8418 else if (! add_adjust)
8420 enum machine_mode mode = GET_MODE (reg);
8421 rtx sub_insn = gen_sub3_insn (reg, comparison_value,
8422 initial_value);
8424 if (sub_insn == 0)
8425 return 0;
8426 start_value
8427 = gen_rtx_MINUS (mode, comparison_value, initial_value);
8428 loop_insn_hoist (loop, sub_insn);
8430 else
8431 /* We could handle the other cases too, but it'll be
8432 better to have a testcase first. */
8433 return 0;
8435 /* We may not have a single insn which can increment a reg, so
8436 create a sequence to hold all the insns from expand_inc. */
8437 start_sequence ();
8438 expand_inc (reg, new_add_val);
8439 tem = gen_sequence ();
8440 end_sequence ();
8442 p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
8443 delete_insn (bl->biv->insn);
8445 /* Update biv info to reflect its new status. */
8446 bl->biv->insn = p;
8447 bl->initial_value = start_value;
8448 bl->biv->add_val = new_add_val;
8450 /* Update loop info. */
8451 loop_info->initial_value = reg;
8452 loop_info->initial_equiv_value = reg;
8453 loop_info->final_value = const0_rtx;
8454 loop_info->final_equiv_value = const0_rtx;
8455 loop_info->comparison_value = const0_rtx;
8456 loop_info->comparison_code = cmp_code;
8457 loop_info->increment = new_add_val;
8459 /* Inc LABEL_NUSES so that delete_insn will
8460 not delete the label. */
8461 LABEL_NUSES (XEXP (jump_label, 0))++;
8463 /* Emit an insn after the end of the loop to set the biv's
8464 proper exit value if it is used anywhere outside the loop. */
8465 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
8466 || ! bl->init_insn
8467 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
8468 loop_insn_sink (loop, gen_load_of_final_value (reg, final_value));
8470 /* Delete compare/branch at end of loop. */
8471 delete_related_insns (PREV_INSN (loop_end));
8472 if (compare_and_branch == 2)
8473 delete_related_insns (first_compare);
8475 /* Add new compare/branch insn at end of loop. */
8476 start_sequence ();
8477 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8478 GET_MODE (reg), 0,
8479 XEXP (jump_label, 0));
8480 tem = gen_sequence ();
8481 end_sequence ();
8482 emit_jump_insn_before (tem, loop_end);
8484 for (tem = PREV_INSN (loop_end);
8485 tem && GET_CODE (tem) != JUMP_INSN;
8486 tem = PREV_INSN (tem))
8489 if (tem)
8490 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8492 if (nonneg)
8494 if (tem)
8496 /* Increment of LABEL_NUSES done above. */
8497 /* Register is now always nonnegative,
8498 so add REG_NONNEG note to the branch. */
8499 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
8500 REG_NOTES (tem));
8502 bl->nonneg = 1;
8505 /* No insn may reference both the reversed and another biv or it
8506 will fail (see comment near the top of the loop reversal
8507 code).
8508 Earlier on, we have verified that the biv has no use except
8509 counting, or it is the only biv in this function.
8510 However, the code that computes no_use_except_counting does
8511 not verify reg notes. It's possible to have an insn that
8512 references another biv, and has a REG_EQUAL note with an
8513 expression based on the reversed biv. To avoid this case,
8514 remove all REG_EQUAL notes based on the reversed biv
8515 here. */
8516 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8517 if (INSN_P (p))
8519 rtx *pnote;
8520 rtx set = single_set (p);
8521 /* If this is a set of a GIV based on the reversed biv, any
8522 REG_EQUAL notes should still be correct. */
8523 if (! set
8524 || GET_CODE (SET_DEST (set)) != REG
8525 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
8526 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
8527 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
8528 for (pnote = &REG_NOTES (p); *pnote;)
8530 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
8531 && reg_mentioned_p (regno_reg_rtx[bl->regno],
8532 XEXP (*pnote, 0)))
8533 *pnote = XEXP (*pnote, 1);
8534 else
8535 pnote = &XEXP (*pnote, 1);
8539 /* Mark that this biv has been reversed. Each giv which depends
8540 on this biv, and which is also live past the end of the loop
8541 will have to be fixed up. */
8543 bl->reversed = 1;
8545 if (loop_dump_stream)
8547 fprintf (loop_dump_stream, "Reversed loop");
8548 if (bl->nonneg)
8549 fprintf (loop_dump_stream, " and added reg_nonneg\n");
8550 else
8551 fprintf (loop_dump_stream, "\n");
8554 return 1;
8559 return 0;
8562 /* Verify whether the biv BL appears to be eliminable,
8563 based on the insns in the loop that refer to it.
8565 If ELIMINATE_P is non-zero, actually do the elimination.
8567 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8568 determine whether invariant insns should be placed inside or at the
8569 start of the loop. */
8571 static int
8572 maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
8573 const struct loop *loop;
8574 struct iv_class *bl;
8575 int eliminate_p;
8576 int threshold, insn_count;
8578 struct loop_ivs *ivs = LOOP_IVS (loop);
8579 rtx reg = bl->biv->dest_reg;
8580 rtx p;
8582 /* Scan all insns in the loop, stopping if we find one that uses the
8583 biv in a way that we cannot eliminate. */
8585 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
8587 enum rtx_code code = GET_CODE (p);
8588 basic_block where_bb = 0;
8589 rtx where_insn = threshold >= insn_count ? 0 : p;
8591 /* If this is a libcall that sets a giv, skip ahead to its end. */
8592 if (GET_RTX_CLASS (code) == 'i')
8594 rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8596 if (note)
8598 rtx last = XEXP (note, 0);
8599 rtx set = single_set (last);
8601 if (set && GET_CODE (SET_DEST (set)) == REG)
8603 unsigned int regno = REGNO (SET_DEST (set));
8605 if (regno < ivs->n_regs
8606 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
8607 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
8608 p = last;
8612 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8613 && reg_mentioned_p (reg, PATTERN (p))
8614 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
8615 eliminate_p, where_bb, where_insn))
8617 if (loop_dump_stream)
8618 fprintf (loop_dump_stream,
8619 "Cannot eliminate biv %d: biv used in insn %d.\n",
8620 bl->regno, INSN_UID (p));
8621 break;
8625 if (p == loop->end)
8627 if (loop_dump_stream)
8628 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8629 bl->regno, eliminate_p ? "was" : "can be");
8630 return 1;
8633 return 0;
8636 /* INSN and REFERENCE are instructions in the same insn chain.
8637 Return non-zero if INSN is first. */
8640 loop_insn_first_p (insn, reference)
8641 rtx insn, reference;
8643 rtx p, q;
8645 for (p = insn, q = reference;;)
8647 /* Start with test for not first so that INSN == REFERENCE yields not
8648 first. */
8649 if (q == insn || ! p)
8650 return 0;
8651 if (p == reference || ! q)
8652 return 1;
8654 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8655 previous insn, hence the <= comparison below does not work if
8656 P is a note. */
8657 if (INSN_UID (p) < max_uid_for_loop
8658 && INSN_UID (q) < max_uid_for_loop
8659 && GET_CODE (p) != NOTE)
8660 return INSN_LUID (p) <= INSN_LUID (q);
8662 if (INSN_UID (p) >= max_uid_for_loop
8663 || GET_CODE (p) == NOTE)
8664 p = NEXT_INSN (p);
8665 if (INSN_UID (q) >= max_uid_for_loop)
8666 q = NEXT_INSN (q);
8670 /* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
8671 the offset that we have to take into account due to auto-increment /
8672 div derivation is zero. */
8673 static int
8674 biv_elimination_giv_has_0_offset (biv, giv, insn)
8675 struct induction *biv, *giv;
8676 rtx insn;
8678 /* If the giv V had the auto-inc address optimization applied
8679 to it, and INSN occurs between the giv insn and the biv
8680 insn, then we'd have to adjust the value used here.
8681 This is rare, so we don't bother to make this possible. */
8682 if (giv->auto_inc_opt
8683 && ((loop_insn_first_p (giv->insn, insn)
8684 && loop_insn_first_p (insn, biv->insn))
8685 || (loop_insn_first_p (biv->insn, insn)
8686 && loop_insn_first_p (insn, giv->insn))))
8687 return 0;
8689 return 1;
8692 /* If BL appears in X (part of the pattern of INSN), see if we can
8693 eliminate its use. If so, return 1. If not, return 0.
8695 If BIV does not appear in X, return 1.
8697 If ELIMINATE_P is non-zero, actually do the elimination.
8698 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
8699 Depending on how many items have been moved out of the loop, it
8700 will either be before INSN (when WHERE_INSN is non-zero) or at the
8701 start of the loop (when WHERE_INSN is zero). */
8703 static int
8704 maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
8705 const struct loop *loop;
8706 rtx x, insn;
8707 struct iv_class *bl;
8708 int eliminate_p;
8709 basic_block where_bb;
8710 rtx where_insn;
8712 enum rtx_code code = GET_CODE (x);
8713 rtx reg = bl->biv->dest_reg;
8714 enum machine_mode mode = GET_MODE (reg);
8715 struct induction *v;
8716 rtx arg, tem;
8717 #ifdef HAVE_cc0
8718 rtx new;
8719 #endif
8720 int arg_operand;
8721 const char *fmt;
8722 int i, j;
8724 switch (code)
8726 case REG:
8727 /* If we haven't already been able to do something with this BIV,
8728 we can't eliminate it. */
8729 if (x == reg)
8730 return 0;
8731 return 1;
8733 case SET:
8734 /* If this sets the BIV, it is not a problem. */
8735 if (SET_DEST (x) == reg)
8736 return 1;
8738 /* If this is an insn that defines a giv, it is also ok because
8739 it will go away when the giv is reduced. */
8740 for (v = bl->giv; v; v = v->next_iv)
8741 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8742 return 1;
8744 #ifdef HAVE_cc0
8745 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8747 /* Can replace with any giv that was reduced and
8748 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8749 Require a constant for MULT_VAL, so we know it's nonzero.
8750 ??? We disable this optimization to avoid potential
8751 overflows. */
8753 for (v = bl->giv; v; v = v->next_iv)
8754 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
8755 && v->add_val == const0_rtx
8756 && ! v->ignore && ! v->maybe_dead && v->always_computable
8757 && v->mode == mode
8758 && 0)
8760 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8761 continue;
8763 if (! eliminate_p)
8764 return 1;
8766 /* If the giv has the opposite direction of change,
8767 then reverse the comparison. */
8768 if (INTVAL (v->mult_val) < 0)
8769 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8770 const0_rtx, v->new_reg);
8771 else
8772 new = v->new_reg;
8774 /* We can probably test that giv's reduced reg. */
8775 if (validate_change (insn, &SET_SRC (x), new, 0))
8776 return 1;
8779 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8780 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8781 Require a constant for MULT_VAL, so we know it's nonzero.
8782 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8783 overflow problem. */
8785 for (v = bl->giv; v; v = v->next_iv)
8786 if (GET_CODE (v->mult_val) == CONST_INT
8787 && v->mult_val != const0_rtx
8788 && ! v->ignore && ! v->maybe_dead && v->always_computable
8789 && v->mode == mode
8790 && (GET_CODE (v->add_val) == SYMBOL_REF
8791 || GET_CODE (v->add_val) == LABEL_REF
8792 || GET_CODE (v->add_val) == CONST
8793 || (GET_CODE (v->add_val) == REG
8794 && REG_POINTER (v->add_val))))
8796 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8797 continue;
8799 if (! eliminate_p)
8800 return 1;
8802 /* If the giv has the opposite direction of change,
8803 then reverse the comparison. */
8804 if (INTVAL (v->mult_val) < 0)
8805 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8806 v->new_reg);
8807 else
8808 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8809 copy_rtx (v->add_val));
8811 /* Replace biv with the giv's reduced register. */
8812 update_reg_last_use (v->add_val, insn);
8813 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8814 return 1;
8816 /* Insn doesn't support that constant or invariant. Copy it
8817 into a register (it will be a loop invariant.) */
8818 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8820 loop_insn_emit_before (loop, 0, where_insn,
8821 gen_move_insn (tem,
8822 copy_rtx (v->add_val)));
8824 /* Substitute the new register for its invariant value in
8825 the compare expression. */
8826 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8827 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8828 return 1;
8831 #endif
8832 break;
8834 case COMPARE:
8835 case EQ: case NE:
8836 case GT: case GE: case GTU: case GEU:
8837 case LT: case LE: case LTU: case LEU:
8838 /* See if either argument is the biv. */
8839 if (XEXP (x, 0) == reg)
8840 arg = XEXP (x, 1), arg_operand = 1;
8841 else if (XEXP (x, 1) == reg)
8842 arg = XEXP (x, 0), arg_operand = 0;
8843 else
8844 break;
8846 if (CONSTANT_P (arg))
8848 /* First try to replace with any giv that has constant positive
8849 mult_val and constant add_val. We might be able to support
8850 negative mult_val, but it seems complex to do it in general. */
8852 for (v = bl->giv; v; v = v->next_iv)
8853 if (GET_CODE (v->mult_val) == CONST_INT
8854 && INTVAL (v->mult_val) > 0
8855 && (GET_CODE (v->add_val) == SYMBOL_REF
8856 || GET_CODE (v->add_val) == LABEL_REF
8857 || GET_CODE (v->add_val) == CONST
8858 || (GET_CODE (v->add_val) == REG
8859 && REG_POINTER (v->add_val)))
8860 && ! v->ignore && ! v->maybe_dead && v->always_computable
8861 && v->mode == mode)
8863 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8864 continue;
8866 if (! eliminate_p)
8867 return 1;
8869 /* Replace biv with the giv's reduced reg. */
8870 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
8872 /* If all constants are actually constant integers and
8873 the derived constant can be directly placed in the COMPARE,
8874 do so. */
8875 if (GET_CODE (arg) == CONST_INT
8876 && GET_CODE (v->mult_val) == CONST_INT
8877 && GET_CODE (v->add_val) == CONST_INT)
8879 validate_change (insn, &XEXP (x, arg_operand),
8880 GEN_INT (INTVAL (arg)
8881 * INTVAL (v->mult_val)
8882 + INTVAL (v->add_val)), 1);
8884 else
8886 /* Otherwise, load it into a register. */
8887 tem = gen_reg_rtx (mode);
8888 loop_iv_add_mult_emit_before (loop, arg,
8889 v->mult_val, v->add_val,
8890 tem, where_bb, where_insn);
8891 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8893 if (apply_change_group ())
8894 return 1;
8897 /* Look for giv with positive constant mult_val and nonconst add_val.
8898 Insert insns to calculate new compare value.
8899 ??? Turn this off due to possible overflow. */
8901 for (v = bl->giv; v; v = v->next_iv)
8902 if (GET_CODE (v->mult_val) == CONST_INT
8903 && INTVAL (v->mult_val) > 0
8904 && ! v->ignore && ! v->maybe_dead && v->always_computable
8905 && v->mode == mode
8906 && 0)
8908 rtx tem;
8910 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8911 continue;
8913 if (! eliminate_p)
8914 return 1;
8916 tem = gen_reg_rtx (mode);
8918 /* Replace biv with giv's reduced register. */
8919 validate_change (insn, &XEXP (x, 1 - arg_operand),
8920 v->new_reg, 1);
8922 /* Compute value to compare against. */
8923 loop_iv_add_mult_emit_before (loop, arg,
8924 v->mult_val, v->add_val,
8925 tem, where_bb, where_insn);
8926 /* Use it in this insn. */
8927 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8928 if (apply_change_group ())
8929 return 1;
8932 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8934 if (loop_invariant_p (loop, arg) == 1)
8936 /* Look for giv with constant positive mult_val and nonconst
8937 add_val. Insert insns to compute new compare value.
8938 ??? Turn this off due to possible overflow. */
8940 for (v = bl->giv; v; v = v->next_iv)
8941 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8942 && ! v->ignore && ! v->maybe_dead && v->always_computable
8943 && v->mode == mode
8944 && 0)
8946 rtx tem;
8948 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8949 continue;
8951 if (! eliminate_p)
8952 return 1;
8954 tem = gen_reg_rtx (mode);
8956 /* Replace biv with giv's reduced register. */
8957 validate_change (insn, &XEXP (x, 1 - arg_operand),
8958 v->new_reg, 1);
8960 /* Compute value to compare against. */
8961 loop_iv_add_mult_emit_before (loop, arg,
8962 v->mult_val, v->add_val,
8963 tem, where_bb, where_insn);
8964 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8965 if (apply_change_group ())
8966 return 1;
8970 /* This code has problems. Basically, you can't know when
8971 seeing if we will eliminate BL, whether a particular giv
8972 of ARG will be reduced. If it isn't going to be reduced,
8973 we can't eliminate BL. We can try forcing it to be reduced,
8974 but that can generate poor code.
8976 The problem is that the benefit of reducing TV, below should
8977 be increased if BL can actually be eliminated, but this means
8978 we might have to do a topological sort of the order in which
8979 we try to process biv. It doesn't seem worthwhile to do
8980 this sort of thing now. */
8982 #if 0
8983 /* Otherwise the reg compared with had better be a biv. */
8984 if (GET_CODE (arg) != REG
8985 || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
8986 return 0;
8988 /* Look for a pair of givs, one for each biv,
8989 with identical coefficients. */
8990 for (v = bl->giv; v; v = v->next_iv)
8992 struct induction *tv;
8994 if (v->ignore || v->maybe_dead || v->mode != mode)
8995 continue;
8997 for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
8998 tv = tv->next_iv)
8999 if (! tv->ignore && ! tv->maybe_dead
9000 && rtx_equal_p (tv->mult_val, v->mult_val)
9001 && rtx_equal_p (tv->add_val, v->add_val)
9002 && tv->mode == mode)
9004 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
9005 continue;
9007 if (! eliminate_p)
9008 return 1;
9010 /* Replace biv with its giv's reduced reg. */
9011 XEXP (x, 1 - arg_operand) = v->new_reg;
9012 /* Replace other operand with the other giv's
9013 reduced reg. */
9014 XEXP (x, arg_operand) = tv->new_reg;
9015 return 1;
9018 #endif
9021 /* If we get here, the biv can't be eliminated. */
9022 return 0;
9024 case MEM:
9025 /* If this address is a DEST_ADDR giv, it doesn't matter if the
9026 biv is used in it, since it will be replaced. */
9027 for (v = bl->giv; v; v = v->next_iv)
9028 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
9029 return 1;
9030 break;
9032 default:
9033 break;
9036 /* See if any subexpression fails elimination. */
9037 fmt = GET_RTX_FORMAT (code);
9038 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9040 switch (fmt[i])
9042 case 'e':
9043 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
9044 eliminate_p, where_bb, where_insn))
9045 return 0;
9046 break;
9048 case 'E':
9049 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9050 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
9051 eliminate_p, where_bb, where_insn))
9052 return 0;
9053 break;
9057 return 1;
9060 /* Return nonzero if the last use of REG
9061 is in an insn following INSN in the same basic block. */
9063 static int
9064 last_use_this_basic_block (reg, insn)
9065 rtx reg;
9066 rtx insn;
9068 rtx n;
9069 for (n = insn;
9070 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
9071 n = NEXT_INSN (n))
9073 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
9074 return 1;
9076 return 0;
9079 /* Called via `note_stores' to record the initial value of a biv. Here we
9080 just record the location of the set and process it later. */
9082 static void
9083 record_initial (dest, set, data)
9084 rtx dest;
9085 rtx set;
9086 void *data ATTRIBUTE_UNUSED;
9088 struct loop_ivs *ivs = (struct loop_ivs *) data;
9089 struct iv_class *bl;
9091 if (GET_CODE (dest) != REG
9092 || REGNO (dest) >= ivs->n_regs
9093 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
9094 return;
9096 bl = REG_IV_CLASS (ivs, REGNO (dest));
9098 /* If this is the first set found, record it. */
9099 if (bl->init_insn == 0)
9101 bl->init_insn = note_insn;
9102 bl->init_set = set;
9106 /* If any of the registers in X are "old" and currently have a last use earlier
9107 than INSN, update them to have a last use of INSN. Their actual last use
9108 will be the previous insn but it will not have a valid uid_luid so we can't
9109 use it. X must be a source expression only. */
9111 static void
9112 update_reg_last_use (x, insn)
9113 rtx x;
9114 rtx insn;
9116 /* Check for the case where INSN does not have a valid luid. In this case,
9117 there is no need to modify the regno_last_uid, as this can only happen
9118 when code is inserted after the loop_end to set a pseudo's final value,
9119 and hence this insn will never be the last use of x.
9120 ???? This comment is not correct. See for example loop_givs_reduce.
9121 This may insert an insn before another new insn. */
9122 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
9123 && INSN_UID (insn) < max_uid_for_loop
9124 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
9126 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
9128 else
9130 int i, j;
9131 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
9132 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9134 if (fmt[i] == 'e')
9135 update_reg_last_use (XEXP (x, i), insn);
9136 else if (fmt[i] == 'E')
9137 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9138 update_reg_last_use (XVECEXP (x, i, j), insn);
9143 /* Given an insn INSN and condition COND, return the condition in a
9144 canonical form to simplify testing by callers. Specifically:
9146 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
9147 (2) Both operands will be machine operands; (cc0) will have been replaced.
9148 (3) If an operand is a constant, it will be the second operand.
9149 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
9150 for GE, GEU, and LEU.
9152 If the condition cannot be understood, or is an inequality floating-point
9153 comparison which needs to be reversed, 0 will be returned.
9155 If REVERSE is non-zero, then reverse the condition prior to canonizing it.
9157 If EARLIEST is non-zero, it is a pointer to a place where the earliest
9158 insn used in locating the condition was found. If a replacement test
9159 of the condition is desired, it should be placed in front of that
9160 insn and we will be sure that the inputs are still valid.
9162 If WANT_REG is non-zero, we wish the condition to be relative to that
9163 register, if possible. Therefore, do not canonicalize the condition
9164 further. */
9167 canonicalize_condition (insn, cond, reverse, earliest, want_reg)
9168 rtx insn;
9169 rtx cond;
9170 int reverse;
9171 rtx *earliest;
9172 rtx want_reg;
9174 enum rtx_code code;
9175 rtx prev = insn;
9176 rtx set;
9177 rtx tem;
9178 rtx op0, op1;
9179 int reverse_code = 0;
9180 enum machine_mode mode;
9182 code = GET_CODE (cond);
9183 mode = GET_MODE (cond);
9184 op0 = XEXP (cond, 0);
9185 op1 = XEXP (cond, 1);
9187 if (reverse)
9188 code = reversed_comparison_code (cond, insn);
9189 if (code == UNKNOWN)
9190 return 0;
9192 if (earliest)
9193 *earliest = insn;
9195 /* If we are comparing a register with zero, see if the register is set
9196 in the previous insn to a COMPARE or a comparison operation. Perform
9197 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
9198 in cse.c */
9200 while (GET_RTX_CLASS (code) == '<'
9201 && op1 == CONST0_RTX (GET_MODE (op0))
9202 && op0 != want_reg)
9204 /* Set non-zero when we find something of interest. */
9205 rtx x = 0;
9207 #ifdef HAVE_cc0
9208 /* If comparison with cc0, import actual comparison from compare
9209 insn. */
9210 if (op0 == cc0_rtx)
9212 if ((prev = prev_nonnote_insn (prev)) == 0
9213 || GET_CODE (prev) != INSN
9214 || (set = single_set (prev)) == 0
9215 || SET_DEST (set) != cc0_rtx)
9216 return 0;
9218 op0 = SET_SRC (set);
9219 op1 = CONST0_RTX (GET_MODE (op0));
9220 if (earliest)
9221 *earliest = prev;
9223 #endif
9225 /* If this is a COMPARE, pick up the two things being compared. */
9226 if (GET_CODE (op0) == COMPARE)
9228 op1 = XEXP (op0, 1);
9229 op0 = XEXP (op0, 0);
9230 continue;
9232 else if (GET_CODE (op0) != REG)
9233 break;
9235 /* Go back to the previous insn. Stop if it is not an INSN. We also
9236 stop if it isn't a single set or if it has a REG_INC note because
9237 we don't want to bother dealing with it. */
9239 if ((prev = prev_nonnote_insn (prev)) == 0
9240 || GET_CODE (prev) != INSN
9241 || FIND_REG_INC_NOTE (prev, NULL_RTX))
9242 break;
9244 set = set_of (op0, prev);
9246 if (set
9247 && (GET_CODE (set) != SET
9248 || !rtx_equal_p (SET_DEST (set), op0)))
9249 break;
9251 /* If this is setting OP0, get what it sets it to if it looks
9252 relevant. */
9253 if (set)
9255 enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
9257 /* ??? We may not combine comparisons done in a CCmode with
9258 comparisons not done in a CCmode. This is to aid targets
9259 like Alpha that have an IEEE compliant EQ instruction, and
9260 a non-IEEE compliant BEQ instruction. The use of CCmode is
9261 actually artificial, simply to prevent the combination, but
9262 should not affect other platforms.
9264 However, we must allow VOIDmode comparisons to match either
9265 CCmode or non-CCmode comparison, because some ports have
9266 modeless comparisons inside branch patterns.
9268 ??? This mode check should perhaps look more like the mode check
9269 in simplify_comparison in combine. */
9271 if ((GET_CODE (SET_SRC (set)) == COMPARE
9272 || (((code == NE
9273 || (code == LT
9274 && GET_MODE_CLASS (inner_mode) == MODE_INT
9275 && (GET_MODE_BITSIZE (inner_mode)
9276 <= HOST_BITS_PER_WIDE_INT)
9277 && (STORE_FLAG_VALUE
9278 & ((HOST_WIDE_INT) 1
9279 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9280 #ifdef FLOAT_STORE_FLAG_VALUE
9281 || (code == LT
9282 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9283 && (REAL_VALUE_NEGATIVE
9284 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9285 #endif
9287 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
9288 && (((GET_MODE_CLASS (mode) == MODE_CC)
9289 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9290 || mode == VOIDmode || inner_mode == VOIDmode))
9291 x = SET_SRC (set);
9292 else if (((code == EQ
9293 || (code == GE
9294 && (GET_MODE_BITSIZE (inner_mode)
9295 <= HOST_BITS_PER_WIDE_INT)
9296 && GET_MODE_CLASS (inner_mode) == MODE_INT
9297 && (STORE_FLAG_VALUE
9298 & ((HOST_WIDE_INT) 1
9299 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9300 #ifdef FLOAT_STORE_FLAG_VALUE
9301 || (code == GE
9302 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9303 && (REAL_VALUE_NEGATIVE
9304 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9305 #endif
9307 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
9308 && (((GET_MODE_CLASS (mode) == MODE_CC)
9309 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9310 || mode == VOIDmode || inner_mode == VOIDmode))
9313 reverse_code = 1;
9314 x = SET_SRC (set);
9316 else
9317 break;
9320 else if (reg_set_p (op0, prev))
9321 /* If this sets OP0, but not directly, we have to give up. */
9322 break;
9324 if (x)
9326 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9327 code = GET_CODE (x);
9328 if (reverse_code)
9330 code = reversed_comparison_code (x, prev);
9331 if (code == UNKNOWN)
9332 return 0;
9333 reverse_code = 0;
9336 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
9337 if (earliest)
9338 *earliest = prev;
9342 /* If constant is first, put it last. */
9343 if (CONSTANT_P (op0))
9344 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
9346 /* If OP0 is the result of a comparison, we weren't able to find what
9347 was really being compared, so fail. */
9348 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
9349 return 0;
9351 /* Canonicalize any ordered comparison with integers involving equality
9352 if we can do computations in the relevant mode and we do not
9353 overflow. */
9355 if (GET_CODE (op1) == CONST_INT
9356 && GET_MODE (op0) != VOIDmode
9357 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
9359 HOST_WIDE_INT const_val = INTVAL (op1);
9360 unsigned HOST_WIDE_INT uconst_val = const_val;
9361 unsigned HOST_WIDE_INT max_val
9362 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
9364 switch (code)
9366 case LE:
9367 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
9368 code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
9369 break;
9371 /* When cross-compiling, const_val might be sign-extended from
9372 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9373 case GE:
9374 if ((HOST_WIDE_INT) (const_val & max_val)
9375 != (((HOST_WIDE_INT) 1
9376 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9377 code = GT, op1 = gen_int_mode (const_val - 1, GET_MODE (op0));
9378 break;
9380 case LEU:
9381 if (uconst_val < max_val)
9382 code = LTU, op1 = gen_int_mode (uconst_val + 1, GET_MODE (op0));
9383 break;
9385 case GEU:
9386 if (uconst_val != 0)
9387 code = GTU, op1 = gen_int_mode (uconst_val - 1, GET_MODE (op0));
9388 break;
9390 default:
9391 break;
9395 #ifdef HAVE_cc0
9396 /* Never return CC0; return zero instead. */
9397 if (op0 == cc0_rtx)
9398 return 0;
9399 #endif
9401 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
9404 /* Given a jump insn JUMP, return the condition that will cause it to branch
9405 to its JUMP_LABEL. If the condition cannot be understood, or is an
9406 inequality floating-point comparison which needs to be reversed, 0 will
9407 be returned.
9409 If EARLIEST is non-zero, it is a pointer to a place where the earliest
9410 insn used in locating the condition was found. If a replacement test
9411 of the condition is desired, it should be placed in front of that
9412 insn and we will be sure that the inputs are still valid. */
9415 get_condition (jump, earliest)
9416 rtx jump;
9417 rtx *earliest;
9419 rtx cond;
9420 int reverse;
9421 rtx set;
9423 /* If this is not a standard conditional jump, we can't parse it. */
9424 if (GET_CODE (jump) != JUMP_INSN
9425 || ! any_condjump_p (jump))
9426 return 0;
9427 set = pc_set (jump);
9429 cond = XEXP (SET_SRC (set), 0);
9431 /* If this branches to JUMP_LABEL when the condition is false, reverse
9432 the condition. */
9433 reverse
9434 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
9435 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
9437 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
9440 /* Similar to above routine, except that we also put an invariant last
9441 unless both operands are invariants. */
9444 get_condition_for_loop (loop, x)
9445 const struct loop *loop;
9446 rtx x;
9448 rtx comparison = get_condition (x, (rtx*) 0);
9450 if (comparison == 0
9451 || ! loop_invariant_p (loop, XEXP (comparison, 0))
9452 || loop_invariant_p (loop, XEXP (comparison, 1)))
9453 return comparison;
9455 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
9456 XEXP (comparison, 1), XEXP (comparison, 0));
9459 /* Scan the function and determine whether it has indirect (computed) jumps.
9461 This is taken mostly from flow.c; similar code exists elsewhere
9462 in the compiler. It may be useful to put this into rtlanal.c. */
9463 static int
9464 indirect_jump_in_function_p (start)
9465 rtx start;
9467 rtx insn;
9469 for (insn = start; insn; insn = NEXT_INSN (insn))
9470 if (computed_jump_p (insn))
9471 return 1;
9473 return 0;
9476 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9477 documentation for LOOP_MEMS for the definition of `appropriate'.
9478 This function is called from prescan_loop via for_each_rtx. */
9480 static int
9481 insert_loop_mem (mem, data)
9482 rtx *mem;
9483 void *data ATTRIBUTE_UNUSED;
9485 struct loop_info *loop_info = data;
9486 int i;
9487 rtx m = *mem;
9489 if (m == NULL_RTX)
9490 return 0;
9492 switch (GET_CODE (m))
9494 case MEM:
9495 break;
9497 case CLOBBER:
9498 /* We're not interested in MEMs that are only clobbered. */
9499 return -1;
9501 case CONST_DOUBLE:
9502 /* We're not interested in the MEM associated with a
9503 CONST_DOUBLE, so there's no need to traverse into this. */
9504 return -1;
9506 case EXPR_LIST:
9507 /* We're not interested in any MEMs that only appear in notes. */
9508 return -1;
9510 default:
9511 /* This is not a MEM. */
9512 return 0;
9515 /* See if we've already seen this MEM. */
9516 for (i = 0; i < loop_info->mems_idx; ++i)
9517 if (rtx_equal_p (m, loop_info->mems[i].mem))
9519 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
9520 /* The modes of the two memory accesses are different. If
9521 this happens, something tricky is going on, and we just
9522 don't optimize accesses to this MEM. */
9523 loop_info->mems[i].optimize = 0;
9525 return 0;
9528 /* Resize the array, if necessary. */
9529 if (loop_info->mems_idx == loop_info->mems_allocated)
9531 if (loop_info->mems_allocated != 0)
9532 loop_info->mems_allocated *= 2;
9533 else
9534 loop_info->mems_allocated = 32;
9536 loop_info->mems = (loop_mem_info *)
9537 xrealloc (loop_info->mems,
9538 loop_info->mems_allocated * sizeof (loop_mem_info));
9541 /* Actually insert the MEM. */
9542 loop_info->mems[loop_info->mems_idx].mem = m;
9543 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9544 because we can't put it in a register. We still store it in the
9545 table, though, so that if we see the same address later, but in a
9546 non-BLK mode, we'll not think we can optimize it at that point. */
9547 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
9548 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
9549 ++loop_info->mems_idx;
9551 return 0;
9555 /* Allocate REGS->ARRAY or reallocate it if it is too small.
9557 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
9558 register that is modified by an insn between FROM and TO. If the
9559 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
9560 more, stop incrementing it, to avoid overflow.
9562 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
9563 register I is used, if it is only used once. Otherwise, it is set
9564 to 0 (for no uses) or const0_rtx for more than one use. This
9565 parameter may be zero, in which case this processing is not done.
9567 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
9568 optimize register I. */
9570 static void
9571 loop_regs_scan (loop, extra_size)
9572 const struct loop *loop;
9573 int extra_size;
9575 struct loop_regs *regs = LOOP_REGS (loop);
9576 int old_nregs;
9577 /* last_set[n] is nonzero iff reg n has been set in the current
9578 basic block. In that case, it is the insn that last set reg n. */
9579 rtx *last_set;
9580 rtx insn;
9581 int i;
9583 old_nregs = regs->num;
9584 regs->num = max_reg_num ();
9586 /* Grow the regs array if not allocated or too small. */
9587 if (regs->num >= regs->size)
9589 regs->size = regs->num + extra_size;
9591 regs->array = (struct loop_reg *)
9592 xrealloc (regs->array, regs->size * sizeof (*regs->array));
9594 /* Zero the new elements. */
9595 memset (regs->array + old_nregs, 0,
9596 (regs->size - old_nregs) * sizeof (*regs->array));
9599 /* Clear previously scanned fields but do not clear n_times_set. */
9600 for (i = 0; i < old_nregs; i++)
9602 regs->array[i].set_in_loop = 0;
9603 regs->array[i].may_not_optimize = 0;
9604 regs->array[i].single_usage = NULL_RTX;
9607 last_set = (rtx *) xcalloc (regs->num, sizeof (rtx));
9609 /* Scan the loop, recording register usage. */
9610 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9611 insn = NEXT_INSN (insn))
9613 if (INSN_P (insn))
9615 /* Record registers that have exactly one use. */
9616 find_single_use_in_loop (regs, insn, PATTERN (insn));
9618 /* Include uses in REG_EQUAL notes. */
9619 if (REG_NOTES (insn))
9620 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
9622 if (GET_CODE (PATTERN (insn)) == SET
9623 || GET_CODE (PATTERN (insn)) == CLOBBER)
9624 count_one_set (regs, insn, PATTERN (insn), last_set);
9625 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
9627 int i;
9628 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
9629 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
9630 last_set);
9634 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
9635 memset (last_set, 0, regs->num * sizeof (rtx));
9638 /* Invalidate all hard registers clobbered by calls. With one exception:
9639 a call-clobbered PIC register is still function-invariant for our
9640 purposes, since we can hoist any PIC calculations out of the loop.
9641 Thus the call to rtx_varies_p. */
9642 if (LOOP_INFO (loop)->has_call)
9643 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9644 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
9645 && rtx_varies_p (gen_rtx_REG (Pmode, i), /*for_alias=*/1))
9647 regs->array[i].may_not_optimize = 1;
9648 regs->array[i].set_in_loop = 1;
9651 #ifdef AVOID_CCMODE_COPIES
9652 /* Don't try to move insns which set CC registers if we should not
9653 create CCmode register copies. */
9654 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
9655 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
9656 regs->array[i].may_not_optimize = 1;
9657 #endif
9659 /* Set regs->array[I].n_times_set for the new registers. */
9660 for (i = old_nregs; i < regs->num; i++)
9661 regs->array[i].n_times_set = regs->array[i].set_in_loop;
9663 free (last_set);
9666 /* Returns the number of real INSNs in the LOOP. */
9668 static int
9669 count_insns_in_loop (loop)
9670 const struct loop *loop;
9672 int count = 0;
9673 rtx insn;
9675 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9676 insn = NEXT_INSN (insn))
9677 if (INSN_P (insn))
9678 ++count;
9680 return count;
9683 /* Move MEMs into registers for the duration of the loop. */
9685 static void
9686 load_mems (loop)
9687 const struct loop *loop;
9689 struct loop_info *loop_info = LOOP_INFO (loop);
9690 struct loop_regs *regs = LOOP_REGS (loop);
9691 int maybe_never = 0;
9692 int i;
9693 rtx p, prev_ebb_head;
9694 rtx label = NULL_RTX;
9695 rtx end_label;
9696 /* Nonzero if the next instruction may never be executed. */
9697 int next_maybe_never = 0;
9698 unsigned int last_max_reg = max_reg_num ();
9700 if (loop_info->mems_idx == 0)
9701 return;
9703 /* We cannot use next_label here because it skips over normal insns. */
9704 end_label = next_nonnote_insn (loop->end);
9705 if (end_label && GET_CODE (end_label) != CODE_LABEL)
9706 end_label = NULL_RTX;
9708 /* Check to see if it's possible that some instructions in the loop are
9709 never executed. Also check if there is a goto out of the loop other
9710 than right after the end of the loop. */
9711 for (p = next_insn_in_loop (loop, loop->scan_start);
9712 p != NULL_RTX;
9713 p = next_insn_in_loop (loop, p))
9715 if (GET_CODE (p) == CODE_LABEL)
9716 maybe_never = 1;
9717 else if (GET_CODE (p) == JUMP_INSN
9718 /* If we enter the loop in the middle, and scan
9719 around to the beginning, don't set maybe_never
9720 for that. This must be an unconditional jump,
9721 otherwise the code at the top of the loop might
9722 never be executed. Unconditional jumps are
9723 followed a by barrier then loop end. */
9724 && ! (GET_CODE (p) == JUMP_INSN
9725 && JUMP_LABEL (p) == loop->top
9726 && NEXT_INSN (NEXT_INSN (p)) == loop->end
9727 && any_uncondjump_p (p)))
9729 /* If this is a jump outside of the loop but not right
9730 after the end of the loop, we would have to emit new fixup
9731 sequences for each such label. */
9732 if (/* If we can't tell where control might go when this
9733 JUMP_INSN is executed, we must be conservative. */
9734 !JUMP_LABEL (p)
9735 || (JUMP_LABEL (p) != end_label
9736 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
9737 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
9738 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end))))
9739 return;
9741 if (!any_condjump_p (p))
9742 /* Something complicated. */
9743 maybe_never = 1;
9744 else
9745 /* If there are any more instructions in the loop, they
9746 might not be reached. */
9747 next_maybe_never = 1;
9749 else if (next_maybe_never)
9750 maybe_never = 1;
9753 /* Find start of the extended basic block that enters the loop. */
9754 for (p = loop->start;
9755 PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
9756 p = PREV_INSN (p))
9758 prev_ebb_head = p;
9760 cselib_init ();
9762 /* Build table of mems that get set to constant values before the
9763 loop. */
9764 for (; p != loop->start; p = NEXT_INSN (p))
9765 cselib_process_insn (p);
9767 /* Actually move the MEMs. */
9768 for (i = 0; i < loop_info->mems_idx; ++i)
9770 regset_head load_copies;
9771 regset_head store_copies;
9772 int written = 0;
9773 rtx reg;
9774 rtx mem = loop_info->mems[i].mem;
9775 rtx mem_list_entry;
9777 if (MEM_VOLATILE_P (mem)
9778 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
9779 /* There's no telling whether or not MEM is modified. */
9780 loop_info->mems[i].optimize = 0;
9782 /* Go through the MEMs written to in the loop to see if this
9783 one is aliased by one of them. */
9784 mem_list_entry = loop_info->store_mems;
9785 while (mem_list_entry)
9787 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
9788 written = 1;
9789 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
9790 mem, rtx_varies_p))
9792 /* MEM is indeed aliased by this store. */
9793 loop_info->mems[i].optimize = 0;
9794 break;
9796 mem_list_entry = XEXP (mem_list_entry, 1);
9799 if (flag_float_store && written
9800 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
9801 loop_info->mems[i].optimize = 0;
9803 /* If this MEM is written to, we must be sure that there
9804 are no reads from another MEM that aliases this one. */
9805 if (loop_info->mems[i].optimize && written)
9807 int j;
9809 for (j = 0; j < loop_info->mems_idx; ++j)
9811 if (j == i)
9812 continue;
9813 else if (true_dependence (mem,
9814 VOIDmode,
9815 loop_info->mems[j].mem,
9816 rtx_varies_p))
9818 /* It's not safe to hoist loop_info->mems[i] out of
9819 the loop because writes to it might not be
9820 seen by reads from loop_info->mems[j]. */
9821 loop_info->mems[i].optimize = 0;
9822 break;
9827 if (maybe_never && may_trap_p (mem))
9828 /* We can't access the MEM outside the loop; it might
9829 cause a trap that wouldn't have happened otherwise. */
9830 loop_info->mems[i].optimize = 0;
9832 if (!loop_info->mems[i].optimize)
9833 /* We thought we were going to lift this MEM out of the
9834 loop, but later discovered that we could not. */
9835 continue;
9837 INIT_REG_SET (&load_copies);
9838 INIT_REG_SET (&store_copies);
9840 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9841 order to keep scan_loop from moving stores to this MEM
9842 out of the loop just because this REG is neither a
9843 user-variable nor used in the loop test. */
9844 reg = gen_reg_rtx (GET_MODE (mem));
9845 REG_USERVAR_P (reg) = 1;
9846 loop_info->mems[i].reg = reg;
9848 /* Now, replace all references to the MEM with the
9849 corresponding pseudos. */
9850 maybe_never = 0;
9851 for (p = next_insn_in_loop (loop, loop->scan_start);
9852 p != NULL_RTX;
9853 p = next_insn_in_loop (loop, p))
9855 if (INSN_P (p))
9857 rtx set;
9859 set = single_set (p);
9861 /* See if this copies the mem into a register that isn't
9862 modified afterwards. We'll try to do copy propagation
9863 a little further on. */
9864 if (set
9865 /* @@@ This test is _way_ too conservative. */
9866 && ! maybe_never
9867 && GET_CODE (SET_DEST (set)) == REG
9868 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
9869 && REGNO (SET_DEST (set)) < last_max_reg
9870 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
9871 && rtx_equal_p (SET_SRC (set), mem))
9872 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
9874 /* See if this copies the mem from a register that isn't
9875 modified afterwards. We'll try to remove the
9876 redundant copy later on by doing a little register
9877 renaming and copy propagation. This will help
9878 to untangle things for the BIV detection code. */
9879 if (set
9880 && ! maybe_never
9881 && GET_CODE (SET_SRC (set)) == REG
9882 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
9883 && REGNO (SET_SRC (set)) < last_max_reg
9884 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
9885 && rtx_equal_p (SET_DEST (set), mem))
9886 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
9888 /* If this is a call which uses / clobbers this memory
9889 location, we must not change the interface here. */
9890 if (GET_CODE (p) == CALL_INSN
9891 && reg_mentioned_p (loop_info->mems[i].mem,
9892 CALL_INSN_FUNCTION_USAGE (p)))
9894 cancel_changes (0);
9895 loop_info->mems[i].optimize = 0;
9896 break;
9898 else
9899 /* Replace the memory reference with the shadow register. */
9900 replace_loop_mems (p, loop_info->mems[i].mem,
9901 loop_info->mems[i].reg);
9904 if (GET_CODE (p) == CODE_LABEL
9905 || GET_CODE (p) == JUMP_INSN)
9906 maybe_never = 1;
9909 if (! loop_info->mems[i].optimize)
9910 ; /* We found we couldn't do the replacement, so do nothing. */
9911 else if (! apply_change_group ())
9912 /* We couldn't replace all occurrences of the MEM. */
9913 loop_info->mems[i].optimize = 0;
9914 else
9916 /* Load the memory immediately before LOOP->START, which is
9917 the NOTE_LOOP_BEG. */
9918 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9919 rtx set;
9920 rtx best = mem;
9921 int j;
9922 struct elt_loc_list *const_equiv = 0;
9924 if (e)
9926 struct elt_loc_list *equiv;
9927 struct elt_loc_list *best_equiv = 0;
9928 for (equiv = e->locs; equiv; equiv = equiv->next)
9930 if (CONSTANT_P (equiv->loc))
9931 const_equiv = equiv;
9932 else if (GET_CODE (equiv->loc) == REG
9933 /* Extending hard register lifetimes causes crash
9934 on SRC targets. Doing so on non-SRC is
9935 probably also not good idea, since we most
9936 probably have pseudoregister equivalence as
9937 well. */
9938 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
9939 best_equiv = equiv;
9941 /* Use the constant equivalence if that is cheap enough. */
9942 if (! best_equiv)
9943 best_equiv = const_equiv;
9944 else if (const_equiv
9945 && (rtx_cost (const_equiv->loc, SET)
9946 <= rtx_cost (best_equiv->loc, SET)))
9948 best_equiv = const_equiv;
9949 const_equiv = 0;
9952 /* If best_equiv is nonzero, we know that MEM is set to a
9953 constant or register before the loop. We will use this
9954 knowledge to initialize the shadow register with that
9955 constant or reg rather than by loading from MEM. */
9956 if (best_equiv)
9957 best = copy_rtx (best_equiv->loc);
9960 set = gen_move_insn (reg, best);
9961 set = loop_insn_hoist (loop, set);
9962 if (REG_P (best))
9964 for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
9965 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
9967 REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
9968 break;
9972 if (const_equiv)
9973 set_unique_reg_note (set, REG_EQUAL, copy_rtx (const_equiv->loc));
9975 if (written)
9977 if (label == NULL_RTX)
9979 label = gen_label_rtx ();
9980 emit_label_after (label, loop->end);
9983 /* Store the memory immediately after END, which is
9984 the NOTE_LOOP_END. */
9985 set = gen_move_insn (copy_rtx (mem), reg);
9986 loop_insn_emit_after (loop, 0, label, set);
9989 if (loop_dump_stream)
9991 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9992 REGNO (reg), (written ? "r/w" : "r/o"));
9993 print_rtl (loop_dump_stream, mem);
9994 fputc ('\n', loop_dump_stream);
9997 /* Attempt a bit of copy propagation. This helps untangle the
9998 data flow, and enables {basic,general}_induction_var to find
9999 more bivs/givs. */
10000 EXECUTE_IF_SET_IN_REG_SET
10001 (&load_copies, FIRST_PSEUDO_REGISTER, j,
10003 try_copy_prop (loop, reg, j);
10005 CLEAR_REG_SET (&load_copies);
10007 EXECUTE_IF_SET_IN_REG_SET
10008 (&store_copies, FIRST_PSEUDO_REGISTER, j,
10010 try_swap_copy_prop (loop, reg, j);
10012 CLEAR_REG_SET (&store_copies);
10016 if (label != NULL_RTX && end_label != NULL_RTX)
10018 /* Now, we need to replace all references to the previous exit
10019 label with the new one. */
10020 rtx_pair rr;
10021 rr.r1 = end_label;
10022 rr.r2 = label;
10024 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
10026 for_each_rtx (&p, replace_label, &rr);
10028 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
10029 field. This is not handled by for_each_rtx because it doesn't
10030 handle unprinted ('0') fields. We need to update JUMP_LABEL
10031 because the immediately following unroll pass will use it.
10032 replace_label would not work anyways, because that only handles
10033 LABEL_REFs. */
10034 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
10035 JUMP_LABEL (p) = label;
10039 cselib_finish ();
10042 /* For communication between note_reg_stored and its caller. */
10043 struct note_reg_stored_arg
10045 int set_seen;
10046 rtx reg;
10049 /* Called via note_stores, record in SET_SEEN whether X, which is written,
10050 is equal to ARG. */
10051 static void
10052 note_reg_stored (x, setter, arg)
10053 rtx x, setter ATTRIBUTE_UNUSED;
10054 void *arg;
10056 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
10057 if (t->reg == x)
10058 t->set_seen = 1;
10061 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
10062 There must be exactly one insn that sets this pseudo; it will be
10063 deleted if all replacements succeed and we can prove that the register
10064 is not used after the loop. */
10066 static void
10067 try_copy_prop (loop, replacement, regno)
10068 const struct loop *loop;
10069 rtx replacement;
10070 unsigned int regno;
10072 /* This is the reg that we are copying from. */
10073 rtx reg_rtx = regno_reg_rtx[regno];
10074 rtx init_insn = 0;
10075 rtx insn;
10076 /* These help keep track of whether we replaced all uses of the reg. */
10077 int replaced_last = 0;
10078 int store_is_first = 0;
10080 for (insn = next_insn_in_loop (loop, loop->scan_start);
10081 insn != NULL_RTX;
10082 insn = next_insn_in_loop (loop, insn))
10084 rtx set;
10086 /* Only substitute within one extended basic block from the initializing
10087 insn. */
10088 if (GET_CODE (insn) == CODE_LABEL && init_insn)
10089 break;
10091 if (! INSN_P (insn))
10092 continue;
10094 /* Is this the initializing insn? */
10095 set = single_set (insn);
10096 if (set
10097 && GET_CODE (SET_DEST (set)) == REG
10098 && REGNO (SET_DEST (set)) == regno)
10100 if (init_insn)
10101 abort ();
10103 init_insn = insn;
10104 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
10105 store_is_first = 1;
10108 /* Only substitute after seeing the initializing insn. */
10109 if (init_insn && insn != init_insn)
10111 struct note_reg_stored_arg arg;
10113 replace_loop_regs (insn, reg_rtx, replacement);
10114 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
10115 replaced_last = 1;
10117 /* Stop replacing when REPLACEMENT is modified. */
10118 arg.reg = replacement;
10119 arg.set_seen = 0;
10120 note_stores (PATTERN (insn), note_reg_stored, &arg);
10121 if (arg.set_seen)
10123 rtx note = find_reg_note (insn, REG_EQUAL, NULL);
10125 /* It is possible that we've turned previously valid REG_EQUAL to
10126 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
10127 REPLACEMENT is modified, we get different meaning. */
10128 if (note && reg_mentioned_p (replacement, XEXP (note, 0)))
10129 remove_note (insn, note);
10130 break;
10134 if (! init_insn)
10135 abort ();
10136 if (apply_change_group ())
10138 if (loop_dump_stream)
10139 fprintf (loop_dump_stream, " Replaced reg %d", regno);
10140 if (store_is_first && replaced_last)
10142 rtx first;
10143 rtx retval_note;
10145 /* Assume we're just deleting INIT_INSN. */
10146 first = init_insn;
10147 /* Look for REG_RETVAL note. If we're deleting the end of
10148 the libcall sequence, the whole sequence can go. */
10149 retval_note = find_reg_note (init_insn, REG_RETVAL, NULL_RTX);
10150 /* If we found a REG_RETVAL note, find the first instruction
10151 in the sequence. */
10152 if (retval_note)
10153 first = XEXP (retval_note, 0);
10155 /* Delete the instructions. */
10156 loop_delete_insns (first, init_insn);
10158 if (loop_dump_stream)
10159 fprintf (loop_dump_stream, ".\n");
10163 /* Replace all the instructions from FIRST up to and including LAST
10164 with NOTE_INSN_DELETED notes. */
10166 static void
10167 loop_delete_insns (first, last)
10168 rtx first;
10169 rtx last;
10171 while (1)
10173 if (loop_dump_stream)
10174 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
10175 INSN_UID (first));
10176 delete_insn (first);
10178 /* If this was the LAST instructions we're supposed to delete,
10179 we're done. */
10180 if (first == last)
10181 break;
10183 first = NEXT_INSN (first);
10187 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
10188 loop LOOP if the order of the sets of these registers can be
10189 swapped. There must be exactly one insn within the loop that sets
10190 this pseudo followed immediately by a move insn that sets
10191 REPLACEMENT with REGNO. */
10192 static void
10193 try_swap_copy_prop (loop, replacement, regno)
10194 const struct loop *loop;
10195 rtx replacement;
10196 unsigned int regno;
10198 rtx insn;
10199 rtx set = NULL_RTX;
10200 unsigned int new_regno;
10202 new_regno = REGNO (replacement);
10204 for (insn = next_insn_in_loop (loop, loop->scan_start);
10205 insn != NULL_RTX;
10206 insn = next_insn_in_loop (loop, insn))
10208 /* Search for the insn that copies REGNO to NEW_REGNO? */
10209 if (INSN_P (insn)
10210 && (set = single_set (insn))
10211 && GET_CODE (SET_DEST (set)) == REG
10212 && REGNO (SET_DEST (set)) == new_regno
10213 && GET_CODE (SET_SRC (set)) == REG
10214 && REGNO (SET_SRC (set)) == regno)
10215 break;
10218 if (insn != NULL_RTX)
10220 rtx prev_insn;
10221 rtx prev_set;
10223 /* Some DEF-USE info would come in handy here to make this
10224 function more general. For now, just check the previous insn
10225 which is the most likely candidate for setting REGNO. */
10227 prev_insn = PREV_INSN (insn);
10229 if (INSN_P (insn)
10230 && (prev_set = single_set (prev_insn))
10231 && GET_CODE (SET_DEST (prev_set)) == REG
10232 && REGNO (SET_DEST (prev_set)) == regno)
10234 /* We have:
10235 (set (reg regno) (expr))
10236 (set (reg new_regno) (reg regno))
10238 so try converting this to:
10239 (set (reg new_regno) (expr))
10240 (set (reg regno) (reg new_regno))
10242 The former construct is often generated when a global
10243 variable used for an induction variable is shadowed by a
10244 register (NEW_REGNO). The latter construct improves the
10245 chances of GIV replacement and BIV elimination. */
10247 validate_change (prev_insn, &SET_DEST (prev_set),
10248 replacement, 1);
10249 validate_change (insn, &SET_DEST (set),
10250 SET_SRC (set), 1);
10251 validate_change (insn, &SET_SRC (set),
10252 replacement, 1);
10254 if (apply_change_group ())
10256 if (loop_dump_stream)
10257 fprintf (loop_dump_stream,
10258 " Swapped set of reg %d at %d with reg %d at %d.\n",
10259 regno, INSN_UID (insn),
10260 new_regno, INSN_UID (prev_insn));
10262 /* Update first use of REGNO. */
10263 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
10264 REGNO_FIRST_UID (regno) = INSN_UID (insn);
10266 /* Now perform copy propagation to hopefully
10267 remove all uses of REGNO within the loop. */
10268 try_copy_prop (loop, replacement, regno);
10274 /* Replace MEM with its associated pseudo register. This function is
10275 called from load_mems via for_each_rtx. DATA is actually a pointer
10276 to a structure describing the instruction currently being scanned
10277 and the MEM we are currently replacing. */
10279 static int
10280 replace_loop_mem (mem, data)
10281 rtx *mem;
10282 void *data;
10284 loop_replace_args *args = (loop_replace_args *) data;
10285 rtx m = *mem;
10287 if (m == NULL_RTX)
10288 return 0;
10290 switch (GET_CODE (m))
10292 case MEM:
10293 break;
10295 case CONST_DOUBLE:
10296 /* We're not interested in the MEM associated with a
10297 CONST_DOUBLE, so there's no need to traverse into one. */
10298 return -1;
10300 default:
10301 /* This is not a MEM. */
10302 return 0;
10305 if (!rtx_equal_p (args->match, m))
10306 /* This is not the MEM we are currently replacing. */
10307 return 0;
10309 /* Actually replace the MEM. */
10310 validate_change (args->insn, mem, args->replacement, 1);
10312 return 0;
10315 static void
10316 replace_loop_mems (insn, mem, reg)
10317 rtx insn;
10318 rtx mem;
10319 rtx reg;
10321 loop_replace_args args;
10323 args.insn = insn;
10324 args.match = mem;
10325 args.replacement = reg;
10327 for_each_rtx (&insn, replace_loop_mem, &args);
10330 /* Replace one register with another. Called through for_each_rtx; PX points
10331 to the rtx being scanned. DATA is actually a pointer to
10332 a structure of arguments. */
10334 static int
10335 replace_loop_reg (px, data)
10336 rtx *px;
10337 void *data;
10339 rtx x = *px;
10340 loop_replace_args *args = (loop_replace_args *) data;
10342 if (x == NULL_RTX)
10343 return 0;
10345 if (x == args->match)
10346 validate_change (args->insn, px, args->replacement, 1);
10348 return 0;
10351 static void
10352 replace_loop_regs (insn, reg, replacement)
10353 rtx insn;
10354 rtx reg;
10355 rtx replacement;
10357 loop_replace_args args;
10359 args.insn = insn;
10360 args.match = reg;
10361 args.replacement = replacement;
10363 for_each_rtx (&insn, replace_loop_reg, &args);
10366 /* Replace occurrences of the old exit label for the loop with the new
10367 one. DATA is an rtx_pair containing the old and new labels,
10368 respectively. */
10370 static int
10371 replace_label (x, data)
10372 rtx *x;
10373 void *data;
10375 rtx l = *x;
10376 rtx old_label = ((rtx_pair *) data)->r1;
10377 rtx new_label = ((rtx_pair *) data)->r2;
10379 if (l == NULL_RTX)
10380 return 0;
10382 if (GET_CODE (l) != LABEL_REF)
10383 return 0;
10385 if (XEXP (l, 0) != old_label)
10386 return 0;
10388 XEXP (l, 0) = new_label;
10389 ++LABEL_NUSES (new_label);
10390 --LABEL_NUSES (old_label);
10392 return 0;
10395 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
10396 (ignored in the interim). */
10398 static rtx
10399 loop_insn_emit_after (loop, where_bb, where_insn, pattern)
10400 const struct loop *loop ATTRIBUTE_UNUSED;
10401 basic_block where_bb ATTRIBUTE_UNUSED;
10402 rtx where_insn;
10403 rtx pattern;
10405 return emit_insn_after (pattern, where_insn);
10409 /* If WHERE_INSN is non-zero emit insn for PATTERN before WHERE_INSN
10410 in basic block WHERE_BB (ignored in the interim) within the loop
10411 otherwise hoist PATTERN into the loop pre-header. */
10414 loop_insn_emit_before (loop, where_bb, where_insn, pattern)
10415 const struct loop *loop;
10416 basic_block where_bb ATTRIBUTE_UNUSED;
10417 rtx where_insn;
10418 rtx pattern;
10420 if (! where_insn)
10421 return loop_insn_hoist (loop, pattern);
10422 return emit_insn_before (pattern, where_insn);
10426 /* Emit call insn for PATTERN before WHERE_INSN in basic block
10427 WHERE_BB (ignored in the interim) within the loop. */
10429 static rtx
10430 loop_call_insn_emit_before (loop, where_bb, where_insn, pattern)
10431 const struct loop *loop ATTRIBUTE_UNUSED;
10432 basic_block where_bb ATTRIBUTE_UNUSED;
10433 rtx where_insn;
10434 rtx pattern;
10436 return emit_call_insn_before (pattern, where_insn);
10440 /* Hoist insn for PATTERN into the loop pre-header. */
10443 loop_insn_hoist (loop, pattern)
10444 const struct loop *loop;
10445 rtx pattern;
10447 return loop_insn_emit_before (loop, 0, loop->start, pattern);
10451 /* Hoist call insn for PATTERN into the loop pre-header. */
10453 static rtx
10454 loop_call_insn_hoist (loop, pattern)
10455 const struct loop *loop;
10456 rtx pattern;
10458 return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
10462 /* Sink insn for PATTERN after the loop end. */
10465 loop_insn_sink (loop, pattern)
10466 const struct loop *loop;
10467 rtx pattern;
10469 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
10472 /* bl->final_value can be eighter general_operand or PLUS of general_operand
10473 and constant. Emit sequence of intructions to load it into REG */
10474 static rtx
10475 gen_load_of_final_value (reg, final_value)
10476 rtx reg, final_value;
10478 rtx seq;
10479 start_sequence ();
10480 final_value = force_operand (final_value, reg);
10481 if (final_value != reg)
10482 emit_move_insn (reg, final_value);
10483 seq = gen_sequence ();
10484 end_sequence ();
10485 return seq;
10488 /* If the loop has multiple exits, emit insn for PATTERN before the
10489 loop to ensure that it will always be executed no matter how the
10490 loop exits. Otherwise, emit the insn for PATTERN after the loop,
10491 since this is slightly more efficient. */
10493 static rtx
10494 loop_insn_sink_or_swim (loop, pattern)
10495 const struct loop *loop;
10496 rtx pattern;
10498 if (loop->exit_count)
10499 return loop_insn_hoist (loop, pattern);
10500 else
10501 return loop_insn_sink (loop, pattern);
10504 static void
10505 loop_ivs_dump (loop, file, verbose)
10506 const struct loop *loop;
10507 FILE *file;
10508 int verbose;
10510 struct iv_class *bl;
10511 int iv_num = 0;
10513 if (! loop || ! file)
10514 return;
10516 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10517 iv_num++;
10519 fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
10521 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10523 loop_iv_class_dump (bl, file, verbose);
10524 fputc ('\n', file);
10529 static void
10530 loop_iv_class_dump (bl, file, verbose)
10531 const struct iv_class *bl;
10532 FILE *file;
10533 int verbose ATTRIBUTE_UNUSED;
10535 struct induction *v;
10536 rtx incr;
10537 int i;
10539 if (! bl || ! file)
10540 return;
10542 fprintf (file, "IV class for reg %d, benefit %d\n",
10543 bl->regno, bl->total_benefit);
10545 fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
10546 if (bl->initial_value)
10548 fprintf (file, ", init val: ");
10549 print_simple_rtl (file, bl->initial_value);
10551 if (bl->initial_test)
10553 fprintf (file, ", init test: ");
10554 print_simple_rtl (file, bl->initial_test);
10556 fputc ('\n', file);
10558 if (bl->final_value)
10560 fprintf (file, " Final val: ");
10561 print_simple_rtl (file, bl->final_value);
10562 fputc ('\n', file);
10565 if ((incr = biv_total_increment (bl)))
10567 fprintf (file, " Total increment: ");
10568 print_simple_rtl (file, incr);
10569 fputc ('\n', file);
10572 /* List the increments. */
10573 for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
10575 fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
10576 print_simple_rtl (file, v->add_val);
10577 fputc ('\n', file);
10580 /* List the givs. */
10581 for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
10583 fprintf (file, " Giv%d: insn %d, benefit %d, ",
10584 i, INSN_UID (v->insn), v->benefit);
10585 if (v->giv_type == DEST_ADDR)
10586 print_simple_rtl (file, v->mem);
10587 else
10588 print_simple_rtl (file, single_set (v->insn));
10589 fputc ('\n', file);
10594 static void
10595 loop_biv_dump (v, file, verbose)
10596 const struct induction *v;
10597 FILE *file;
10598 int verbose;
10600 if (! v || ! file)
10601 return;
10603 fprintf (file,
10604 "Biv %d: insn %d",
10605 REGNO (v->dest_reg), INSN_UID (v->insn));
10606 fprintf (file, " const ");
10607 print_simple_rtl (file, v->add_val);
10609 if (verbose && v->final_value)
10611 fputc ('\n', file);
10612 fprintf (file, " final ");
10613 print_simple_rtl (file, v->final_value);
10616 fputc ('\n', file);
10620 static void
10621 loop_giv_dump (v, file, verbose)
10622 const struct induction *v;
10623 FILE *file;
10624 int verbose;
10626 if (! v || ! file)
10627 return;
10629 if (v->giv_type == DEST_REG)
10630 fprintf (file, "Giv %d: insn %d",
10631 REGNO (v->dest_reg), INSN_UID (v->insn));
10632 else
10633 fprintf (file, "Dest address: insn %d",
10634 INSN_UID (v->insn));
10636 fprintf (file, " src reg %d benefit %d",
10637 REGNO (v->src_reg), v->benefit);
10638 fprintf (file, " lifetime %d",
10639 v->lifetime);
10641 if (v->replaceable)
10642 fprintf (file, " replaceable");
10644 if (v->no_const_addval)
10645 fprintf (file, " ncav");
10647 if (v->ext_dependent)
10649 switch (GET_CODE (v->ext_dependent))
10651 case SIGN_EXTEND:
10652 fprintf (file, " ext se");
10653 break;
10654 case ZERO_EXTEND:
10655 fprintf (file, " ext ze");
10656 break;
10657 case TRUNCATE:
10658 fprintf (file, " ext tr");
10659 break;
10660 default:
10661 abort ();
10665 fputc ('\n', file);
10666 fprintf (file, " mult ");
10667 print_simple_rtl (file, v->mult_val);
10669 fputc ('\n', file);
10670 fprintf (file, " add ");
10671 print_simple_rtl (file, v->add_val);
10673 if (verbose && v->final_value)
10675 fputc ('\n', file);
10676 fprintf (file, " final ");
10677 print_simple_rtl (file, v->final_value);
10680 fputc ('\n', file);
10684 void
10685 debug_ivs (loop)
10686 const struct loop *loop;
10688 loop_ivs_dump (loop, stderr, 1);
10692 void
10693 debug_iv_class (bl)
10694 const struct iv_class *bl;
10696 loop_iv_class_dump (bl, stderr, 1);
10700 void
10701 debug_biv (v)
10702 const struct induction *v;
10704 loop_biv_dump (v, stderr, 1);
10708 void
10709 debug_giv (v)
10710 const struct induction *v;
10712 loop_giv_dump (v, stderr, 1);
10716 #define LOOP_BLOCK_NUM_1(INSN) \
10717 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
10719 /* The notes do not have an assigned block, so look at the next insn. */
10720 #define LOOP_BLOCK_NUM(INSN) \
10721 ((INSN) ? (GET_CODE (INSN) == NOTE \
10722 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
10723 : LOOP_BLOCK_NUM_1 (INSN)) \
10724 : -1)
10726 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
10728 static void
10729 loop_dump_aux (loop, file, verbose)
10730 const struct loop *loop;
10731 FILE *file;
10732 int verbose ATTRIBUTE_UNUSED;
10734 rtx label;
10736 if (! loop || ! file)
10737 return;
10739 /* Print diagnostics to compare our concept of a loop with
10740 what the loop notes say. */
10741 if (! PREV_INSN (loop->first->head)
10742 || GET_CODE (PREV_INSN (loop->first->head)) != NOTE
10743 || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
10744 != NOTE_INSN_LOOP_BEG)
10745 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
10746 INSN_UID (PREV_INSN (loop->first->head)));
10747 if (! NEXT_INSN (loop->last->end)
10748 || GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
10749 || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
10750 != NOTE_INSN_LOOP_END)
10751 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
10752 INSN_UID (NEXT_INSN (loop->last->end)));
10754 if (loop->start)
10756 fprintf (file,
10757 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
10758 LOOP_BLOCK_NUM (loop->start),
10759 LOOP_INSN_UID (loop->start),
10760 LOOP_BLOCK_NUM (loop->cont),
10761 LOOP_INSN_UID (loop->cont),
10762 LOOP_BLOCK_NUM (loop->cont),
10763 LOOP_INSN_UID (loop->cont),
10764 LOOP_BLOCK_NUM (loop->vtop),
10765 LOOP_INSN_UID (loop->vtop),
10766 LOOP_BLOCK_NUM (loop->end),
10767 LOOP_INSN_UID (loop->end));
10768 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
10769 LOOP_BLOCK_NUM (loop->top),
10770 LOOP_INSN_UID (loop->top),
10771 LOOP_BLOCK_NUM (loop->scan_start),
10772 LOOP_INSN_UID (loop->scan_start));
10773 fprintf (file, ";; exit_count %d", loop->exit_count);
10774 if (loop->exit_count)
10776 fputs (", labels:", file);
10777 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
10779 fprintf (file, " %d ",
10780 LOOP_INSN_UID (XEXP (label, 0)));
10783 fputs ("\n", file);
10785 /* This can happen when a marked loop appears as two nested loops,
10786 say from while (a || b) {}. The inner loop won't match
10787 the loop markers but the outer one will. */
10788 if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
10789 fprintf (file, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
10793 /* Call this function from the debugger to dump LOOP. */
10795 void
10796 debug_loop (loop)
10797 const struct loop *loop;
10799 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
10802 /* Call this function from the debugger to dump LOOPS. */
10804 void
10805 debug_loops (loops)
10806 const struct loops *loops;
10808 flow_loops_dump (loops, stderr, loop_dump_aux, 1);