* c-common.h (truthvalue_conversion, type_for_mode,
[official-gcc.git] / gcc / loop.c
blob085b7c8b54a32633003ae10cf0da0bfeb411fb70
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 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 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"
56 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
57 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
59 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
60 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
61 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
64 /* Vector mapping INSN_UIDs to luids.
65 The luids are like uids but increase monotonically always.
66 We use them to see whether a jump comes from outside a given loop. */
68 int *uid_luid;
70 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
71 number the insn is contained in. */
73 struct loop **uid_loop;
75 /* 1 + largest uid of any insn. */
77 int max_uid_for_loop;
79 /* 1 + luid of last insn. */
81 static int max_luid;
83 /* Number of loops detected in current function. Used as index to the
84 next few tables. */
86 static int max_loop_num;
88 /* Bound on pseudo register number before loop optimization.
89 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
90 unsigned int max_reg_before_loop;
92 /* The value to pass to the next call of reg_scan_update. */
93 static int loop_max_reg;
95 #define obstack_chunk_alloc xmalloc
96 #define obstack_chunk_free free
98 /* During the analysis of a loop, a chain of `struct movable's
99 is made to record all the movable insns found.
100 Then the entire chain can be scanned to decide which to move. */
102 struct movable
104 rtx insn; /* A movable insn */
105 rtx set_src; /* The expression this reg is set from. */
106 rtx set_dest; /* The destination of this SET. */
107 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
108 of any registers used within the LIBCALL. */
109 int consec; /* Number of consecutive following insns
110 that must be moved with this one. */
111 unsigned int regno; /* The register it sets */
112 short lifetime; /* lifetime of that register;
113 may be adjusted when matching movables
114 that load the same value are found. */
115 short savings; /* Number of insns we can move for this reg,
116 including other movables that force this
117 or match this one. */
118 unsigned int cond : 1; /* 1 if only conditionally movable */
119 unsigned int force : 1; /* 1 means MUST move this insn */
120 unsigned int global : 1; /* 1 means reg is live outside this loop */
121 /* If PARTIAL is 1, GLOBAL means something different:
122 that the reg is live outside the range from where it is set
123 to the following label. */
124 unsigned int done : 1; /* 1 inhibits further processing of this */
126 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
127 In particular, moving it does not make it
128 invariant. */
129 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
130 load SRC, rather than copying INSN. */
131 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
132 first insn of a consecutive sets group. */
133 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
134 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
135 that we should avoid changing when clearing
136 the rest of the reg. */
137 struct movable *match; /* First entry for same value */
138 struct movable *forces; /* An insn that must be moved if this is */
139 struct movable *next;
143 FILE *loop_dump_stream;
145 /* Forward declarations. */
147 static void find_and_verify_loops PARAMS ((rtx, struct loops *));
148 static void mark_loop_jump PARAMS ((rtx, struct loop *));
149 static void prescan_loop PARAMS ((struct loop *));
150 static int reg_in_basic_block_p PARAMS ((rtx, rtx));
151 static int consec_sets_invariant_p PARAMS ((const struct loop *,
152 rtx, int, rtx));
153 static int labels_in_range_p PARAMS ((rtx, int));
154 static void count_one_set PARAMS ((struct loop_regs *, rtx, rtx, rtx *));
155 static void note_addr_stored PARAMS ((rtx, rtx, void *));
156 static void note_set_pseudo_multiple_uses PARAMS ((rtx, rtx, void *));
157 static int loop_reg_used_before_p PARAMS ((const struct loop *, rtx, rtx));
158 static void scan_loop PARAMS ((struct loop*, int));
159 #if 0
160 static void replace_call_address PARAMS ((rtx, rtx, rtx));
161 #endif
162 static rtx skip_consec_insns PARAMS ((rtx, int));
163 static int libcall_benefit PARAMS ((rtx));
164 static void ignore_some_movables PARAMS ((struct loop_movables *));
165 static void force_movables PARAMS ((struct loop_movables *));
166 static void combine_movables PARAMS ((struct loop_movables *,
167 struct loop_regs *));
168 static int regs_match_p PARAMS ((rtx, rtx, struct loop_movables *));
169 static int rtx_equal_for_loop_p PARAMS ((rtx, rtx, struct loop_movables *,
170 struct loop_regs *));
171 static void add_label_notes PARAMS ((rtx, rtx));
172 static void move_movables PARAMS ((struct loop *loop, struct loop_movables *,
173 int, int));
174 static void loop_movables_add PARAMS((struct loop_movables *,
175 struct movable *));
176 static void loop_movables_free PARAMS((struct loop_movables *));
177 static int count_nonfixed_reads PARAMS ((const struct loop *, rtx));
178 static void loop_bivs_find PARAMS((struct loop *));
179 static void loop_bivs_init_find PARAMS((struct loop *));
180 static void loop_bivs_check PARAMS((struct loop *));
181 static void loop_givs_find PARAMS((struct loop *));
182 static void loop_givs_check PARAMS((struct loop *));
183 static int loop_biv_eliminable_p PARAMS((struct loop *, struct iv_class *,
184 int, int));
185 static int loop_giv_reduce_benefit PARAMS((struct loop *, struct iv_class *,
186 struct induction *, rtx));
187 static void loop_givs_dead_check PARAMS((struct loop *, struct iv_class *));
188 static void loop_givs_reduce PARAMS((struct loop *, struct iv_class *));
189 static void loop_givs_rescan PARAMS((struct loop *, struct iv_class *,
190 rtx *));
191 static void loop_ivs_free PARAMS((struct loop *));
192 static void strength_reduce PARAMS ((struct loop *, int, int));
193 static void find_single_use_in_loop PARAMS ((struct loop_regs *, rtx, rtx));
194 static int valid_initial_value_p PARAMS ((rtx, rtx, int, rtx));
195 static void find_mem_givs PARAMS ((const struct loop *, rtx, rtx, int, int));
196 static void record_biv PARAMS ((struct loop *, struct induction *,
197 rtx, rtx, rtx, rtx, rtx *,
198 int, int));
199 static void check_final_value PARAMS ((const struct loop *,
200 struct induction *));
201 static void loop_ivs_dump PARAMS((const struct loop *, FILE *, int));
202 static void loop_iv_class_dump PARAMS((const struct iv_class *, FILE *, int));
203 static void loop_biv_dump PARAMS((const struct induction *, FILE *, int));
204 static void loop_giv_dump PARAMS((const struct induction *, FILE *, int));
205 static void record_giv PARAMS ((const struct loop *, struct induction *,
206 rtx, rtx, rtx, rtx, rtx, rtx, int,
207 enum g_types, int, int, rtx *));
208 static void update_giv_derive PARAMS ((const struct loop *, rtx));
209 static void check_ext_dependant_givs PARAMS ((struct iv_class *,
210 struct loop_info *));
211 static int basic_induction_var PARAMS ((const struct loop *, rtx,
212 enum machine_mode, rtx, rtx,
213 rtx *, rtx *, rtx **));
214 static rtx simplify_giv_expr PARAMS ((const struct loop *, rtx, rtx *, int *));
215 static int general_induction_var PARAMS ((const struct loop *loop, rtx, rtx *,
216 rtx *, rtx *, rtx *, int, int *,
217 enum machine_mode));
218 static int consec_sets_giv PARAMS ((const struct loop *, int, rtx,
219 rtx, rtx, rtx *, rtx *, rtx *, rtx *));
220 static int check_dbra_loop PARAMS ((struct loop *, int));
221 static rtx express_from_1 PARAMS ((rtx, rtx, rtx));
222 static rtx combine_givs_p PARAMS ((struct induction *, struct induction *));
223 static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
224 static void combine_givs PARAMS ((struct loop_regs *, struct iv_class *));
225 static int product_cheap_p PARAMS ((rtx, rtx));
226 static int maybe_eliminate_biv PARAMS ((const struct loop *, struct iv_class *,
227 int, int, int));
228 static int maybe_eliminate_biv_1 PARAMS ((const struct loop *, rtx, rtx,
229 struct iv_class *, int,
230 basic_block, rtx));
231 static int last_use_this_basic_block PARAMS ((rtx, rtx));
232 static void record_initial PARAMS ((rtx, rtx, void *));
233 static void update_reg_last_use PARAMS ((rtx, rtx));
234 static rtx next_insn_in_loop PARAMS ((const struct loop *, rtx));
235 static void loop_regs_scan PARAMS ((const struct loop*, int, int *));
236 static void load_mems PARAMS ((const struct loop *));
237 static int insert_loop_mem PARAMS ((rtx *, void *));
238 static int replace_loop_mem PARAMS ((rtx *, void *));
239 static void replace_loop_mems PARAMS ((rtx, rtx, rtx));
240 static int replace_loop_reg PARAMS ((rtx *, void *));
241 static void replace_loop_regs PARAMS ((rtx insn, rtx, rtx));
242 static void note_reg_stored PARAMS ((rtx, rtx, void *));
243 static void try_copy_prop PARAMS ((const struct loop *, rtx, unsigned int));
244 static void try_swap_copy_prop PARAMS ((const struct loop *, rtx,
245 unsigned int));
246 static int replace_label PARAMS ((rtx *, void *));
247 static rtx check_insn_for_givs PARAMS((struct loop *, rtx, int, int));
248 static rtx check_insn_for_bivs PARAMS((struct loop *, rtx, int, int));
249 static rtx gen_add_mult PARAMS ((rtx, rtx, rtx, rtx));
250 static void loop_regs_update PARAMS ((const struct loop *, rtx));
251 static int iv_add_mult_cost PARAMS ((rtx, rtx, rtx, rtx));
253 static rtx loop_insn_emit_after PARAMS((const struct loop *, basic_block,
254 rtx, rtx));
255 static rtx loop_call_insn_emit_before PARAMS((const struct loop *,
256 basic_block, rtx, rtx));
257 static rtx loop_call_insn_hoist PARAMS((const struct loop *, rtx));
258 static rtx loop_insn_sink_or_swim PARAMS((const struct loop *, rtx));
260 static void loop_dump_aux PARAMS ((const struct loop *, FILE *, int));
261 void debug_ivs PARAMS ((const struct loop *));
262 void debug_iv_class PARAMS ((const struct iv_class *));
263 void debug_biv PARAMS ((const struct induction *));
264 void debug_giv PARAMS ((const struct induction *));
265 void debug_loop PARAMS ((const struct loop *));
266 void debug_loops PARAMS ((const struct loops *));
268 typedef struct rtx_pair
270 rtx r1;
271 rtx r2;
272 } rtx_pair;
274 typedef struct loop_replace_args
276 rtx match;
277 rtx replacement;
278 rtx insn;
279 } loop_replace_args;
281 /* Nonzero iff INSN is between START and END, inclusive. */
282 #define INSN_IN_RANGE_P(INSN, START, END) \
283 (INSN_UID (INSN) < max_uid_for_loop \
284 && INSN_LUID (INSN) >= INSN_LUID (START) \
285 && INSN_LUID (INSN) <= INSN_LUID (END))
287 /* Indirect_jump_in_function is computed once per function. */
288 static int indirect_jump_in_function;
289 static int indirect_jump_in_function_p PARAMS ((rtx));
291 static int compute_luids PARAMS ((rtx, rtx, int));
293 static int biv_elimination_giv_has_0_offset PARAMS ((struct induction *,
294 struct induction *,
295 rtx));
297 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
298 copy the value of the strength reduced giv to its original register. */
299 static int copy_cost;
301 /* Cost of using a register, to normalize the benefits of a giv. */
302 static int reg_address_cost;
304 void
305 init_loop ()
307 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
309 reg_address_cost = address_cost (reg, SImode);
311 copy_cost = COSTS_N_INSNS (1);
314 /* Compute the mapping from uids to luids.
315 LUIDs are numbers assigned to insns, like uids,
316 except that luids increase monotonically through the code.
317 Start at insn START and stop just before END. Assign LUIDs
318 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
319 static int
320 compute_luids (start, end, prev_luid)
321 rtx start, end;
322 int prev_luid;
324 int i;
325 rtx insn;
327 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
329 if (INSN_UID (insn) >= max_uid_for_loop)
330 continue;
331 /* Don't assign luids to line-number NOTEs, so that the distance in
332 luids between two insns is not affected by -g. */
333 if (GET_CODE (insn) != NOTE
334 || NOTE_LINE_NUMBER (insn) <= 0)
335 uid_luid[INSN_UID (insn)] = ++i;
336 else
337 /* Give a line number note the same luid as preceding insn. */
338 uid_luid[INSN_UID (insn)] = i;
340 return i + 1;
343 /* Entry point of this file. Perform loop optimization
344 on the current function. F is the first insn of the function
345 and DUMPFILE is a stream for output of a trace of actions taken
346 (or 0 if none should be output). */
348 void
349 loop_optimize (f, dumpfile, flags)
350 /* f is the first instruction of a chain of insns for one function */
351 rtx f;
352 FILE *dumpfile;
353 int flags;
355 register rtx insn;
356 register int i;
357 struct loops loops_data;
358 struct loops *loops = &loops_data;
359 struct loop_info *loops_info;
361 loop_dump_stream = dumpfile;
363 init_recog_no_volatile ();
365 max_reg_before_loop = max_reg_num ();
366 loop_max_reg = max_reg_before_loop;
368 regs_may_share = 0;
370 /* Count the number of loops. */
372 max_loop_num = 0;
373 for (insn = f; insn; insn = NEXT_INSN (insn))
375 if (GET_CODE (insn) == NOTE
376 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
377 max_loop_num++;
380 /* Don't waste time if no loops. */
381 if (max_loop_num == 0)
382 return;
384 loops->num = max_loop_num;
386 /* Get size to use for tables indexed by uids.
387 Leave some space for labels allocated by find_and_verify_loops. */
388 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
390 uid_luid = (int *) xcalloc (max_uid_for_loop, sizeof (int));
391 uid_loop = (struct loop **) xcalloc (max_uid_for_loop,
392 sizeof (struct loop *));
394 /* Allocate storage for array of loops. */
395 loops->array = (struct loop *)
396 xcalloc (loops->num, sizeof (struct loop));
398 /* Find and process each loop.
399 First, find them, and record them in order of their beginnings. */
400 find_and_verify_loops (f, loops);
402 /* Allocate and initialize auxiliary loop information. */
403 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
404 for (i = 0; i < loops->num; i++)
405 loops->array[i].aux = loops_info + i;
407 /* Now find all register lifetimes. This must be done after
408 find_and_verify_loops, because it might reorder the insns in the
409 function. */
410 reg_scan (f, max_reg_before_loop, 1);
412 /* This must occur after reg_scan so that registers created by gcse
413 will have entries in the register tables.
415 We could have added a call to reg_scan after gcse_main in toplev.c,
416 but moving this call to init_alias_analysis is more efficient. */
417 init_alias_analysis ();
419 /* See if we went too far. Note that get_max_uid already returns
420 one more that the maximum uid of all insn. */
421 if (get_max_uid () > max_uid_for_loop)
422 abort ();
423 /* Now reset it to the actual size we need. See above. */
424 max_uid_for_loop = get_max_uid ();
426 /* find_and_verify_loops has already called compute_luids, but it
427 might have rearranged code afterwards, so we need to recompute
428 the luids now. */
429 max_luid = compute_luids (f, NULL_RTX, 0);
431 /* Don't leave gaps in uid_luid for insns that have been
432 deleted. It is possible that the first or last insn
433 using some register has been deleted by cross-jumping.
434 Make sure that uid_luid for that former insn's uid
435 points to the general area where that insn used to be. */
436 for (i = 0; i < max_uid_for_loop; i++)
438 uid_luid[0] = uid_luid[i];
439 if (uid_luid[0] != 0)
440 break;
442 for (i = 0; i < max_uid_for_loop; i++)
443 if (uid_luid[i] == 0)
444 uid_luid[i] = uid_luid[i - 1];
446 /* Determine if the function has indirect jump. On some systems
447 this prevents low overhead loop instructions from being used. */
448 indirect_jump_in_function = indirect_jump_in_function_p (f);
450 /* Now scan the loops, last ones first, since this means inner ones are done
451 before outer ones. */
452 for (i = max_loop_num - 1; i >= 0; i--)
454 struct loop *loop = &loops->array[i];
456 if (! loop->invalid && loop->end)
457 scan_loop (loop, flags);
460 /* If there were lexical blocks inside the loop, they have been
461 replicated. We will now have more than one NOTE_INSN_BLOCK_BEG
462 and NOTE_INSN_BLOCK_END for each such block. We must duplicate
463 the BLOCKs as well. */
464 if (write_symbols != NO_DEBUG)
465 reorder_blocks ();
467 end_alias_analysis ();
469 /* Clean up. */
470 free (uid_luid);
471 free (uid_loop);
472 free (loops_info);
473 free (loops->array);
476 /* Returns the next insn, in execution order, after INSN. START and
477 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
478 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
479 insn-stream; it is used with loops that are entered near the
480 bottom. */
482 static rtx
483 next_insn_in_loop (loop, insn)
484 const struct loop *loop;
485 rtx insn;
487 insn = NEXT_INSN (insn);
489 if (insn == loop->end)
491 if (loop->top)
492 /* Go to the top of the loop, and continue there. */
493 insn = loop->top;
494 else
495 /* We're done. */
496 insn = NULL_RTX;
499 if (insn == loop->scan_start)
500 /* We're done. */
501 insn = NULL_RTX;
503 return insn;
506 /* Optimize one loop described by LOOP. */
508 /* ??? Could also move memory writes out of loops if the destination address
509 is invariant, the source is invariant, the memory write is not volatile,
510 and if we can prove that no read inside the loop can read this address
511 before the write occurs. If there is a read of this address after the
512 write, then we can also mark the memory read as invariant. */
514 static void
515 scan_loop (loop, flags)
516 struct loop *loop;
517 int flags;
519 struct loop_info *loop_info = LOOP_INFO (loop);
520 struct loop_regs *regs = LOOP_REGS (loop);
521 register int i;
522 rtx loop_start = loop->start;
523 rtx loop_end = loop->end;
524 rtx p;
525 /* 1 if we are scanning insns that could be executed zero times. */
526 int maybe_never = 0;
527 /* 1 if we are scanning insns that might never be executed
528 due to a subroutine call which might exit before they are reached. */
529 int call_passed = 0;
530 /* Jump insn that enters the loop, or 0 if control drops in. */
531 rtx loop_entry_jump = 0;
532 /* Number of insns in the loop. */
533 int insn_count;
534 int tem;
535 rtx temp, update_start, update_end;
536 /* The SET from an insn, if it is the only SET in the insn. */
537 rtx set, set1;
538 /* Chain describing insns movable in current loop. */
539 struct loop_movables *movables = LOOP_MOVABLES (loop);
540 /* Ratio of extra register life span we can justify
541 for saving an instruction. More if loop doesn't call subroutines
542 since in that case saving an insn makes more difference
543 and more registers are available. */
544 int threshold;
545 /* Nonzero if we are scanning instructions in a sub-loop. */
546 int loop_depth = 0;
548 loop->top = 0;
550 movables->head = 0;
551 movables->last = 0;
552 movables->num = 0;
554 /* Determine whether this loop starts with a jump down to a test at
555 the end. This will occur for a small number of loops with a test
556 that is too complex to duplicate in front of the loop.
558 We search for the first insn or label in the loop, skipping NOTEs.
559 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
560 (because we might have a loop executed only once that contains a
561 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
562 (in case we have a degenerate loop).
564 Note that if we mistakenly think that a loop is entered at the top
565 when, in fact, it is entered at the exit test, the only effect will be
566 slightly poorer optimization. Making the opposite error can generate
567 incorrect code. Since very few loops now start with a jump to the
568 exit test, the code here to detect that case is very conservative. */
570 for (p = NEXT_INSN (loop_start);
571 p != loop_end
572 && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
573 && (GET_CODE (p) != NOTE
574 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
575 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
576 p = NEXT_INSN (p))
579 loop->scan_start = p;
581 /* If loop end is the end of the current function, then emit a
582 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
583 note insn. This is the position we use when sinking insns out of
584 the loop. */
585 if (NEXT_INSN (loop->end) != 0)
586 loop->sink = NEXT_INSN (loop->end);
587 else
588 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
590 /* Set up variables describing this loop. */
591 prescan_loop (loop);
592 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
594 /* If loop has a jump before the first label,
595 the true entry is the target of that jump.
596 Start scan from there.
597 But record in LOOP->TOP the place where the end-test jumps
598 back to so we can scan that after the end of the loop. */
599 if (GET_CODE (p) == JUMP_INSN)
601 loop_entry_jump = p;
603 /* Loop entry must be unconditional jump (and not a RETURN) */
604 if (any_uncondjump_p (p)
605 && JUMP_LABEL (p) != 0
606 /* Check to see whether the jump actually
607 jumps out of the loop (meaning it's no loop).
608 This case can happen for things like
609 do {..} while (0). If this label was generated previously
610 by loop, we can't tell anything about it and have to reject
611 the loop. */
612 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
614 loop->top = next_label (loop->scan_start);
615 loop->scan_start = JUMP_LABEL (p);
619 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
620 as required by loop_reg_used_before_p. So skip such loops. (This
621 test may never be true, but it's best to play it safe.)
623 Also, skip loops where we do not start scanning at a label. This
624 test also rejects loops starting with a JUMP_INSN that failed the
625 test above. */
627 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
628 || GET_CODE (loop->scan_start) != CODE_LABEL)
630 if (loop_dump_stream)
631 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
632 INSN_UID (loop_start), INSN_UID (loop_end));
633 return;
636 /* Allocate extra space for REGs that might be created by load_mems.
637 We allocate a little extra slop as well, in the hopes that we
638 won't have to reallocate the regs array. */
639 loop_regs_scan (loop, loop_info->mems_idx + 16, &insn_count);
641 if (loop_dump_stream)
643 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
644 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
645 if (loop->cont)
646 fprintf (loop_dump_stream, "Continue at insn %d.\n",
647 INSN_UID (loop->cont));
650 /* Scan through the loop finding insns that are safe to move.
651 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
652 this reg will be considered invariant for subsequent insns.
653 We consider whether subsequent insns use the reg
654 in deciding whether it is worth actually moving.
656 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
657 and therefore it is possible that the insns we are scanning
658 would never be executed. At such times, we must make sure
659 that it is safe to execute the insn once instead of zero times.
660 When MAYBE_NEVER is 0, all insns will be executed at least once
661 so that is not a problem. */
663 for (p = next_insn_in_loop (loop, loop->scan_start);
664 p != NULL_RTX;
665 p = next_insn_in_loop (loop, p))
667 if (GET_CODE (p) == INSN
668 && (set = single_set (p))
669 && GET_CODE (SET_DEST (set)) == REG
670 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
672 int tem1 = 0;
673 int tem2 = 0;
674 int move_insn = 0;
675 rtx src = SET_SRC (set);
676 rtx dependencies = 0;
678 /* Figure out what to use as a source of this insn. If a REG_EQUIV
679 note is given or if a REG_EQUAL note with a constant operand is
680 specified, use it as the source and mark that we should move
681 this insn by calling emit_move_insn rather that duplicating the
682 insn.
684 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
685 is present. */
686 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
687 if (temp)
688 src = XEXP (temp, 0), move_insn = 1;
689 else
691 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
692 if (temp && CONSTANT_P (XEXP (temp, 0)))
693 src = XEXP (temp, 0), move_insn = 1;
694 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
696 src = XEXP (temp, 0);
697 /* A libcall block can use regs that don't appear in
698 the equivalent expression. To move the libcall,
699 we must move those regs too. */
700 dependencies = libcall_other_reg (p, src);
704 /* Don't try to optimize a register that was made
705 by loop-optimization for an inner loop.
706 We don't know its life-span, so we can't compute the benefit. */
707 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
709 else if (/* The register is used in basic blocks other
710 than the one where it is set (meaning that
711 something after this point in the loop might
712 depend on its value before the set). */
713 ! reg_in_basic_block_p (p, SET_DEST (set))
714 /* And the set is not guaranteed to be executed one
715 the loop starts, or the value before the set is
716 needed before the set occurs...
718 ??? Note we have quadratic behaviour here, mitigated
719 by the fact that the previous test will often fail for
720 large loops. Rather than re-scanning the entire loop
721 each time for register usage, we should build tables
722 of the register usage and use them here instead. */
723 && (maybe_never
724 || loop_reg_used_before_p (loop, set, p)))
725 /* It is unsafe to move the set.
727 This code used to consider it OK to move a set of a variable
728 which was not created by the user and not used in an exit test.
729 That behavior is incorrect and was removed. */
731 else if ((tem = loop_invariant_p (loop, src))
732 && (dependencies == 0
733 || (tem2 = loop_invariant_p (loop, dependencies)) != 0)
734 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
735 || (tem1
736 = consec_sets_invariant_p
737 (loop, SET_DEST (set),
738 regs->array[REGNO (SET_DEST (set))].set_in_loop,
739 p)))
740 /* If the insn can cause a trap (such as divide by zero),
741 can't move it unless it's guaranteed to be executed
742 once loop is entered. Even a function call might
743 prevent the trap insn from being reached
744 (since it might exit!) */
745 && ! ((maybe_never || call_passed)
746 && may_trap_p (src)))
748 register struct movable *m;
749 register int regno = REGNO (SET_DEST (set));
751 /* A potential lossage is where we have a case where two insns
752 can be combined as long as they are both in the loop, but
753 we move one of them outside the loop. For large loops,
754 this can lose. The most common case of this is the address
755 of a function being called.
757 Therefore, if this register is marked as being used exactly
758 once if we are in a loop with calls (a "large loop"), see if
759 we can replace the usage of this register with the source
760 of this SET. If we can, delete this insn.
762 Don't do this if P has a REG_RETVAL note or if we have
763 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
765 if (loop_info->has_call
766 && regs->array[regno].single_usage != 0
767 && regs->array[regno].single_usage != const0_rtx
768 && REGNO_FIRST_UID (regno) == INSN_UID (p)
769 && (REGNO_LAST_UID (regno)
770 == INSN_UID (regs->array[regno].single_usage))
771 && regs->array[regno].set_in_loop == 1
772 && ! side_effects_p (SET_SRC (set))
773 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
774 && (! SMALL_REGISTER_CLASSES
775 || (! (GET_CODE (SET_SRC (set)) == REG
776 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
777 /* This test is not redundant; SET_SRC (set) might be
778 a call-clobbered register and the life of REGNO
779 might span a call. */
780 && ! modified_between_p (SET_SRC (set), p,
781 regs->array[regno].single_usage)
782 && no_labels_between_p (p, regs->array[regno].single_usage)
783 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
784 regs->array[regno].single_usage))
786 /* Replace any usage in a REG_EQUAL note. Must copy the
787 new source, so that we don't get rtx sharing between the
788 SET_SOURCE and REG_NOTES of insn p. */
789 REG_NOTES (regs->array[regno].single_usage)
790 = replace_rtx (REG_NOTES (regs->array[regno].single_usage),
791 SET_DEST (set), copy_rtx (SET_SRC (set)));
793 PUT_CODE (p, NOTE);
794 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
795 NOTE_SOURCE_FILE (p) = 0;
796 regs->array[regno].set_in_loop = 0;
797 continue;
800 m = (struct movable *) xmalloc (sizeof (struct movable));
801 m->next = 0;
802 m->insn = p;
803 m->set_src = src;
804 m->dependencies = dependencies;
805 m->set_dest = SET_DEST (set);
806 m->force = 0;
807 m->consec = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
808 m->done = 0;
809 m->forces = 0;
810 m->partial = 0;
811 m->move_insn = move_insn;
812 m->move_insn_first = 0;
813 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
814 m->savemode = VOIDmode;
815 m->regno = regno;
816 /* Set M->cond if either loop_invariant_p
817 or consec_sets_invariant_p returned 2
818 (only conditionally invariant). */
819 m->cond = ((tem | tem1 | tem2) > 1);
820 m->global = LOOP_REG_GLOBAL_P (loop, regno);
821 m->match = 0;
822 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
823 m->savings = regs->array[regno].n_times_set;
824 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
825 m->savings += libcall_benefit (p);
826 regs->array[regno].set_in_loop = move_insn ? -2 : -1;
827 /* Add M to the end of the chain MOVABLES. */
828 loop_movables_add (movables, m);
830 if (m->consec > 0)
832 /* It is possible for the first instruction to have a
833 REG_EQUAL note but a non-invariant SET_SRC, so we must
834 remember the status of the first instruction in case
835 the last instruction doesn't have a REG_EQUAL note. */
836 m->move_insn_first = m->move_insn;
838 /* Skip this insn, not checking REG_LIBCALL notes. */
839 p = next_nonnote_insn (p);
840 /* Skip the consecutive insns, if there are any. */
841 p = skip_consec_insns (p, m->consec);
842 /* Back up to the last insn of the consecutive group. */
843 p = prev_nonnote_insn (p);
845 /* We must now reset m->move_insn, m->is_equiv, and possibly
846 m->set_src to correspond to the effects of all the
847 insns. */
848 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
849 if (temp)
850 m->set_src = XEXP (temp, 0), m->move_insn = 1;
851 else
853 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
854 if (temp && CONSTANT_P (XEXP (temp, 0)))
855 m->set_src = XEXP (temp, 0), m->move_insn = 1;
856 else
857 m->move_insn = 0;
860 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
863 /* If this register is always set within a STRICT_LOW_PART
864 or set to zero, then its high bytes are constant.
865 So clear them outside the loop and within the loop
866 just load the low bytes.
867 We must check that the machine has an instruction to do so.
868 Also, if the value loaded into the register
869 depends on the same register, this cannot be done. */
870 else if (SET_SRC (set) == const0_rtx
871 && GET_CODE (NEXT_INSN (p)) == INSN
872 && (set1 = single_set (NEXT_INSN (p)))
873 && GET_CODE (set1) == SET
874 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
875 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
876 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
877 == SET_DEST (set))
878 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
880 register int regno = REGNO (SET_DEST (set));
881 if (regs->array[regno].set_in_loop == 2)
883 register struct movable *m;
884 m = (struct movable *) xmalloc (sizeof (struct movable));
885 m->next = 0;
886 m->insn = p;
887 m->set_dest = SET_DEST (set);
888 m->dependencies = 0;
889 m->force = 0;
890 m->consec = 0;
891 m->done = 0;
892 m->forces = 0;
893 m->move_insn = 0;
894 m->move_insn_first = 0;
895 m->partial = 1;
896 /* If the insn may not be executed on some cycles,
897 we can't clear the whole reg; clear just high part.
898 Not even if the reg is used only within this loop.
899 Consider this:
900 while (1)
901 while (s != t) {
902 if (foo ()) x = *s;
903 use (x);
905 Clearing x before the inner loop could clobber a value
906 being saved from the last time around the outer loop.
907 However, if the reg is not used outside this loop
908 and all uses of the register are in the same
909 basic block as the store, there is no problem.
911 If this insn was made by loop, we don't know its
912 INSN_LUID and hence must make a conservative
913 assumption. */
914 m->global = (INSN_UID (p) >= max_uid_for_loop
915 || LOOP_REG_GLOBAL_P (loop, regno)
916 || (labels_in_range_p
917 (p, REGNO_FIRST_LUID (regno))));
918 if (maybe_never && m->global)
919 m->savemode = GET_MODE (SET_SRC (set1));
920 else
921 m->savemode = VOIDmode;
922 m->regno = regno;
923 m->cond = 0;
924 m->match = 0;
925 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
926 m->savings = 1;
927 regs->array[regno].set_in_loop = -1;
928 /* Add M to the end of the chain MOVABLES. */
929 loop_movables_add (movables, m);
933 /* Past a call insn, we get to insns which might not be executed
934 because the call might exit. This matters for insns that trap.
935 Constant and pure call insns always return, so they don't count. */
936 else if (GET_CODE (p) == CALL_INSN && ! CONST_CALL_P (p))
937 call_passed = 1;
938 /* Past a label or a jump, we get to insns for which we
939 can't count on whether or how many times they will be
940 executed during each iteration. Therefore, we can
941 only move out sets of trivial variables
942 (those not used after the loop). */
943 /* Similar code appears twice in strength_reduce. */
944 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
945 /* If we enter the loop in the middle, and scan around to the
946 beginning, don't set maybe_never for that. This must be an
947 unconditional jump, otherwise the code at the top of the
948 loop might never be executed. Unconditional jumps are
949 followed a by barrier then loop end. */
950 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
951 && NEXT_INSN (NEXT_INSN (p)) == loop_end
952 && any_uncondjump_p (p)))
953 maybe_never = 1;
954 else if (GET_CODE (p) == NOTE)
956 /* At the virtual top of a converted loop, insns are again known to
957 be executed: logically, the loop begins here even though the exit
958 code has been duplicated. */
959 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
960 maybe_never = call_passed = 0;
961 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
962 loop_depth++;
963 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
964 loop_depth--;
968 /* If one movable subsumes another, ignore that other. */
970 ignore_some_movables (movables);
972 /* For each movable insn, see if the reg that it loads
973 leads when it dies right into another conditionally movable insn.
974 If so, record that the second insn "forces" the first one,
975 since the second can be moved only if the first is. */
977 force_movables (movables);
979 /* See if there are multiple movable insns that load the same value.
980 If there are, make all but the first point at the first one
981 through the `match' field, and add the priorities of them
982 all together as the priority of the first. */
984 combine_movables (movables, regs);
986 /* Now consider each movable insn to decide whether it is worth moving.
987 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
989 Generally this increases code size, so do not move moveables when
990 optimizing for code size. */
992 if (! optimize_size)
993 move_movables (loop, movables, threshold, insn_count);
995 /* Now candidates that still are negative are those not moved.
996 Change regs->array[I].set_in_loop to indicate that those are not actually
997 invariant. */
998 for (i = 0; i < regs->num; i++)
999 if (regs->array[i].set_in_loop < 0)
1000 regs->array[i].set_in_loop = regs->array[i].n_times_set;
1002 /* Now that we've moved some things out of the loop, we might be able to
1003 hoist even more memory references. */
1004 load_mems (loop);
1006 /* Recalculate regs->array if load_mems has created new registers. */
1007 if (max_reg_num () > regs->num)
1008 loop_regs_scan (loop, 0, &insn_count);
1010 for (update_start = loop_start;
1011 PREV_INSN (update_start)
1012 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1013 update_start = PREV_INSN (update_start))
1015 update_end = NEXT_INSN (loop_end);
1017 reg_scan_update (update_start, update_end, loop_max_reg);
1018 loop_max_reg = max_reg_num ();
1020 if (flag_strength_reduce)
1022 if (update_end && GET_CODE (update_end) == CODE_LABEL)
1023 /* Ensure our label doesn't go away. */
1024 LABEL_NUSES (update_end)++;
1026 strength_reduce (loop, insn_count, flags);
1028 reg_scan_update (update_start, update_end, loop_max_reg);
1029 loop_max_reg = max_reg_num ();
1031 if (update_end && GET_CODE (update_end) == CODE_LABEL
1032 && --LABEL_NUSES (update_end) == 0)
1033 delete_insn (update_end);
1037 /* The movable information is required for strength reduction. */
1038 loop_movables_free (movables);
1040 free (regs->array);
1041 regs->array = 0;
1042 regs->num = 0;
1045 /* Add elements to *OUTPUT to record all the pseudo-regs
1046 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1048 void
1049 record_excess_regs (in_this, not_in_this, output)
1050 rtx in_this, not_in_this;
1051 rtx *output;
1053 enum rtx_code code;
1054 const char *fmt;
1055 int i;
1057 code = GET_CODE (in_this);
1059 switch (code)
1061 case PC:
1062 case CC0:
1063 case CONST_INT:
1064 case CONST_DOUBLE:
1065 case CONST:
1066 case SYMBOL_REF:
1067 case LABEL_REF:
1068 return;
1070 case REG:
1071 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1072 && ! reg_mentioned_p (in_this, not_in_this))
1073 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1074 return;
1076 default:
1077 break;
1080 fmt = GET_RTX_FORMAT (code);
1081 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1083 int j;
1085 switch (fmt[i])
1087 case 'E':
1088 for (j = 0; j < XVECLEN (in_this, i); j++)
1089 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1090 break;
1092 case 'e':
1093 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1094 break;
1099 /* Check what regs are referred to in the libcall block ending with INSN,
1100 aside from those mentioned in the equivalent value.
1101 If there are none, return 0.
1102 If there are one or more, return an EXPR_LIST containing all of them. */
1105 libcall_other_reg (insn, equiv)
1106 rtx insn, equiv;
1108 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1109 rtx p = XEXP (note, 0);
1110 rtx output = 0;
1112 /* First, find all the regs used in the libcall block
1113 that are not mentioned as inputs to the result. */
1115 while (p != insn)
1117 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1118 || GET_CODE (p) == CALL_INSN)
1119 record_excess_regs (PATTERN (p), equiv, &output);
1120 p = NEXT_INSN (p);
1123 return output;
1126 /* Return 1 if all uses of REG
1127 are between INSN and the end of the basic block. */
1129 static int
1130 reg_in_basic_block_p (insn, reg)
1131 rtx insn, reg;
1133 int regno = REGNO (reg);
1134 rtx p;
1136 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1137 return 0;
1139 /* Search this basic block for the already recorded last use of the reg. */
1140 for (p = insn; p; p = NEXT_INSN (p))
1142 switch (GET_CODE (p))
1144 case NOTE:
1145 break;
1147 case INSN:
1148 case CALL_INSN:
1149 /* Ordinary insn: if this is the last use, we win. */
1150 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1151 return 1;
1152 break;
1154 case JUMP_INSN:
1155 /* Jump insn: if this is the last use, we win. */
1156 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1157 return 1;
1158 /* Otherwise, it's the end of the basic block, so we lose. */
1159 return 0;
1161 case CODE_LABEL:
1162 case BARRIER:
1163 /* It's the end of the basic block, so we lose. */
1164 return 0;
1166 default:
1167 break;
1171 /* The "last use" that was recorded can't be found after the first
1172 use. This can happen when the last use was deleted while
1173 processing an inner loop, this inner loop was then completely
1174 unrolled, and the outer loop is always exited after the inner loop,
1175 so that everything after the first use becomes a single basic block. */
1176 return 1;
1179 /* Compute the benefit of eliminating the insns in the block whose
1180 last insn is LAST. This may be a group of insns used to compute a
1181 value directly or can contain a library call. */
1183 static int
1184 libcall_benefit (last)
1185 rtx last;
1187 rtx insn;
1188 int benefit = 0;
1190 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1191 insn != last; insn = NEXT_INSN (insn))
1193 if (GET_CODE (insn) == CALL_INSN)
1194 benefit += 10; /* Assume at least this many insns in a library
1195 routine. */
1196 else if (GET_CODE (insn) == INSN
1197 && GET_CODE (PATTERN (insn)) != USE
1198 && GET_CODE (PATTERN (insn)) != CLOBBER)
1199 benefit++;
1202 return benefit;
1205 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1207 static rtx
1208 skip_consec_insns (insn, count)
1209 rtx insn;
1210 int count;
1212 for (; count > 0; count--)
1214 rtx temp;
1216 /* If first insn of libcall sequence, skip to end. */
1217 /* Do this at start of loop, since INSN is guaranteed to
1218 be an insn here. */
1219 if (GET_CODE (insn) != NOTE
1220 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1221 insn = XEXP (temp, 0);
1224 insn = NEXT_INSN (insn);
1225 while (GET_CODE (insn) == NOTE);
1228 return insn;
1231 /* Ignore any movable whose insn falls within a libcall
1232 which is part of another movable.
1233 We make use of the fact that the movable for the libcall value
1234 was made later and so appears later on the chain. */
1236 static void
1237 ignore_some_movables (movables)
1238 struct loop_movables *movables;
1240 register struct movable *m, *m1;
1242 for (m = movables->head; m; m = m->next)
1244 /* Is this a movable for the value of a libcall? */
1245 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1246 if (note)
1248 rtx insn;
1249 /* Check for earlier movables inside that range,
1250 and mark them invalid. We cannot use LUIDs here because
1251 insns created by loop.c for prior loops don't have LUIDs.
1252 Rather than reject all such insns from movables, we just
1253 explicitly check each insn in the libcall (since invariant
1254 libcalls aren't that common). */
1255 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1256 for (m1 = movables->head; m1 != m; m1 = m1->next)
1257 if (m1->insn == insn)
1258 m1->done = 1;
1263 /* For each movable insn, see if the reg that it loads
1264 leads when it dies right into another conditionally movable insn.
1265 If so, record that the second insn "forces" the first one,
1266 since the second can be moved only if the first is. */
1268 static void
1269 force_movables (movables)
1270 struct loop_movables *movables;
1272 register struct movable *m, *m1;
1273 for (m1 = movables->head; m1; m1 = m1->next)
1274 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1275 if (!m1->partial && !m1->done)
1277 int regno = m1->regno;
1278 for (m = m1->next; m; m = m->next)
1279 /* ??? Could this be a bug? What if CSE caused the
1280 register of M1 to be used after this insn?
1281 Since CSE does not update regno_last_uid,
1282 this insn M->insn might not be where it dies.
1283 But very likely this doesn't matter; what matters is
1284 that M's reg is computed from M1's reg. */
1285 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1286 && !m->done)
1287 break;
1288 if (m != 0 && m->set_src == m1->set_dest
1289 /* If m->consec, m->set_src isn't valid. */
1290 && m->consec == 0)
1291 m = 0;
1293 /* Increase the priority of the moving the first insn
1294 since it permits the second to be moved as well. */
1295 if (m != 0)
1297 m->forces = m1;
1298 m1->lifetime += m->lifetime;
1299 m1->savings += m->savings;
1304 /* Find invariant expressions that are equal and can be combined into
1305 one register. */
1307 static void
1308 combine_movables (movables, regs)
1309 struct loop_movables *movables;
1310 struct loop_regs *regs;
1312 register struct movable *m;
1313 char *matched_regs = (char *) xmalloc (regs->num);
1314 enum machine_mode mode;
1316 /* Regs that are set more than once are not allowed to match
1317 or be matched. I'm no longer sure why not. */
1318 /* Perhaps testing m->consec_sets would be more appropriate here? */
1320 for (m = movables->head; m; m = m->next)
1321 if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1322 && !m->partial)
1324 register struct movable *m1;
1325 int regno = m->regno;
1327 memset (matched_regs, 0, regs->num);
1328 matched_regs[regno] = 1;
1330 /* We want later insns to match the first one. Don't make the first
1331 one match any later ones. So start this loop at m->next. */
1332 for (m1 = m->next; m1; m1 = m1->next)
1333 if (m != m1 && m1->match == 0
1334 && regs->array[m1->regno].n_times_set == 1
1335 /* A reg used outside the loop mustn't be eliminated. */
1336 && !m1->global
1337 /* A reg used for zero-extending mustn't be eliminated. */
1338 && !m1->partial
1339 && (matched_regs[m1->regno]
1342 /* Can combine regs with different modes loaded from the
1343 same constant only if the modes are the same or
1344 if both are integer modes with M wider or the same
1345 width as M1. The check for integer is redundant, but
1346 safe, since the only case of differing destination
1347 modes with equal sources is when both sources are
1348 VOIDmode, i.e., CONST_INT. */
1349 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1350 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1351 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1352 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1353 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1354 /* See if the source of M1 says it matches M. */
1355 && ((GET_CODE (m1->set_src) == REG
1356 && matched_regs[REGNO (m1->set_src)])
1357 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1358 movables, regs))))
1359 && ((m->dependencies == m1->dependencies)
1360 || rtx_equal_p (m->dependencies, m1->dependencies)))
1362 m->lifetime += m1->lifetime;
1363 m->savings += m1->savings;
1364 m1->done = 1;
1365 m1->match = m;
1366 matched_regs[m1->regno] = 1;
1370 /* Now combine the regs used for zero-extension.
1371 This can be done for those not marked `global'
1372 provided their lives don't overlap. */
1374 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1375 mode = GET_MODE_WIDER_MODE (mode))
1377 register struct movable *m0 = 0;
1379 /* Combine all the registers for extension from mode MODE.
1380 Don't combine any that are used outside this loop. */
1381 for (m = movables->head; m; m = m->next)
1382 if (m->partial && ! m->global
1383 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1385 register struct movable *m1;
1386 int first = REGNO_FIRST_LUID (m->regno);
1387 int last = REGNO_LAST_LUID (m->regno);
1389 if (m0 == 0)
1391 /* First one: don't check for overlap, just record it. */
1392 m0 = m;
1393 continue;
1396 /* Make sure they extend to the same mode.
1397 (Almost always true.) */
1398 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1399 continue;
1401 /* We already have one: check for overlap with those
1402 already combined together. */
1403 for (m1 = movables->head; m1 != m; m1 = m1->next)
1404 if (m1 == m0 || (m1->partial && m1->match == m0))
1405 if (! (REGNO_FIRST_LUID (m1->regno) > last
1406 || REGNO_LAST_LUID (m1->regno) < first))
1407 goto overlap;
1409 /* No overlap: we can combine this with the others. */
1410 m0->lifetime += m->lifetime;
1411 m0->savings += m->savings;
1412 m->done = 1;
1413 m->match = m0;
1415 overlap:
1420 /* Clean up. */
1421 free (matched_regs);
1424 /* Return 1 if regs X and Y will become the same if moved. */
1426 static int
1427 regs_match_p (x, y, movables)
1428 rtx x, y;
1429 struct loop_movables *movables;
1431 unsigned int xn = REGNO (x);
1432 unsigned int yn = REGNO (y);
1433 struct movable *mx, *my;
1435 for (mx = movables->head; mx; mx = mx->next)
1436 if (mx->regno == xn)
1437 break;
1439 for (my = movables->head; my; my = my->next)
1440 if (my->regno == yn)
1441 break;
1443 return (mx && my
1444 && ((mx->match == my->match && mx->match != 0)
1445 || mx->match == my
1446 || mx == my->match));
1449 /* Return 1 if X and Y are identical-looking rtx's.
1450 This is the Lisp function EQUAL for rtx arguments.
1452 If two registers are matching movables or a movable register and an
1453 equivalent constant, consider them equal. */
1455 static int
1456 rtx_equal_for_loop_p (x, y, movables, regs)
1457 rtx x, y;
1458 struct loop_movables *movables;
1459 struct loop_regs *regs;
1461 register int i;
1462 register int j;
1463 register struct movable *m;
1464 register enum rtx_code code;
1465 register const char *fmt;
1467 if (x == y)
1468 return 1;
1469 if (x == 0 || y == 0)
1470 return 0;
1472 code = GET_CODE (x);
1474 /* If we have a register and a constant, they may sometimes be
1475 equal. */
1476 if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
1477 && CONSTANT_P (y))
1479 for (m = movables->head; m; m = m->next)
1480 if (m->move_insn && m->regno == REGNO (x)
1481 && rtx_equal_p (m->set_src, y))
1482 return 1;
1484 else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
1485 && CONSTANT_P (x))
1487 for (m = movables->head; m; m = m->next)
1488 if (m->move_insn && m->regno == REGNO (y)
1489 && rtx_equal_p (m->set_src, x))
1490 return 1;
1493 /* Otherwise, rtx's of different codes cannot be equal. */
1494 if (code != GET_CODE (y))
1495 return 0;
1497 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1498 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1500 if (GET_MODE (x) != GET_MODE (y))
1501 return 0;
1503 /* These three types of rtx's can be compared nonrecursively. */
1504 if (code == REG)
1505 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1507 if (code == LABEL_REF)
1508 return XEXP (x, 0) == XEXP (y, 0);
1509 if (code == SYMBOL_REF)
1510 return XSTR (x, 0) == XSTR (y, 0);
1512 /* Compare the elements. If any pair of corresponding elements
1513 fail to match, return 0 for the whole things. */
1515 fmt = GET_RTX_FORMAT (code);
1516 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1518 switch (fmt[i])
1520 case 'w':
1521 if (XWINT (x, i) != XWINT (y, i))
1522 return 0;
1523 break;
1525 case 'i':
1526 if (XINT (x, i) != XINT (y, i))
1527 return 0;
1528 break;
1530 case 'E':
1531 /* Two vectors must have the same length. */
1532 if (XVECLEN (x, i) != XVECLEN (y, i))
1533 return 0;
1535 /* And the corresponding elements must match. */
1536 for (j = 0; j < XVECLEN (x, i); j++)
1537 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
1538 movables, regs) == 0)
1539 return 0;
1540 break;
1542 case 'e':
1543 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
1544 == 0)
1545 return 0;
1546 break;
1548 case 's':
1549 if (strcmp (XSTR (x, i), XSTR (y, i)))
1550 return 0;
1551 break;
1553 case 'u':
1554 /* These are just backpointers, so they don't matter. */
1555 break;
1557 case '0':
1558 break;
1560 /* It is believed that rtx's at this level will never
1561 contain anything but integers and other rtx's,
1562 except for within LABEL_REFs and SYMBOL_REFs. */
1563 default:
1564 abort ();
1567 return 1;
1570 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1571 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1572 references is incremented once for each added note. */
1574 static void
1575 add_label_notes (x, insns)
1576 rtx x;
1577 rtx insns;
1579 enum rtx_code code = GET_CODE (x);
1580 int i, j;
1581 const char *fmt;
1582 rtx insn;
1584 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1586 /* This code used to ignore labels that referred to dispatch tables to
1587 avoid flow generating (slighly) worse code.
1589 We no longer ignore such label references (see LABEL_REF handling in
1590 mark_jump_label for additional information). */
1591 for (insn = insns; insn; insn = NEXT_INSN (insn))
1592 if (reg_mentioned_p (XEXP (x, 0), insn))
1594 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
1595 REG_NOTES (insn));
1596 if (LABEL_P (XEXP (x, 0)))
1597 LABEL_NUSES (XEXP (x, 0))++;
1601 fmt = GET_RTX_FORMAT (code);
1602 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1604 if (fmt[i] == 'e')
1605 add_label_notes (XEXP (x, i), insns);
1606 else if (fmt[i] == 'E')
1607 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1608 add_label_notes (XVECEXP (x, i, j), insns);
1612 /* Scan MOVABLES, and move the insns that deserve to be moved.
1613 If two matching movables are combined, replace one reg with the
1614 other throughout. */
1616 static void
1617 move_movables (loop, movables, threshold, insn_count)
1618 struct loop *loop;
1619 struct loop_movables *movables;
1620 int threshold;
1621 int insn_count;
1623 struct loop_regs *regs = LOOP_REGS (loop);
1624 int nregs = regs->num;
1625 rtx new_start = 0;
1626 register struct movable *m;
1627 register rtx p;
1628 rtx loop_start = loop->start;
1629 rtx loop_end = loop->end;
1630 /* Map of pseudo-register replacements to handle combining
1631 when we move several insns that load the same value
1632 into different pseudo-registers. */
1633 rtx *reg_map = (rtx *) xcalloc (nregs, sizeof (rtx));
1634 char *already_moved = (char *) xcalloc (nregs, sizeof (char));
1636 movables->num = 0;
1638 for (m = movables->head; m; m = m->next)
1640 /* Describe this movable insn. */
1642 if (loop_dump_stream)
1644 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1645 INSN_UID (m->insn), m->regno, m->lifetime);
1646 if (m->consec > 0)
1647 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1648 if (m->cond)
1649 fprintf (loop_dump_stream, "cond ");
1650 if (m->force)
1651 fprintf (loop_dump_stream, "force ");
1652 if (m->global)
1653 fprintf (loop_dump_stream, "global ");
1654 if (m->done)
1655 fprintf (loop_dump_stream, "done ");
1656 if (m->move_insn)
1657 fprintf (loop_dump_stream, "move-insn ");
1658 if (m->match)
1659 fprintf (loop_dump_stream, "matches %d ",
1660 INSN_UID (m->match->insn));
1661 if (m->forces)
1662 fprintf (loop_dump_stream, "forces %d ",
1663 INSN_UID (m->forces->insn));
1666 /* Count movables. Value used in heuristics in strength_reduce. */
1667 movables->num++;
1669 /* Ignore the insn if it's already done (it matched something else).
1670 Otherwise, see if it is now safe to move. */
1672 if (!m->done
1673 && (! m->cond
1674 || (1 == loop_invariant_p (loop, m->set_src)
1675 && (m->dependencies == 0
1676 || 1 == loop_invariant_p (loop, m->dependencies))
1677 && (m->consec == 0
1678 || 1 == consec_sets_invariant_p (loop, m->set_dest,
1679 m->consec + 1,
1680 m->insn))))
1681 && (! m->forces || m->forces->done))
1683 register int regno;
1684 register rtx p;
1685 int savings = m->savings;
1687 /* We have an insn that is safe to move.
1688 Compute its desirability. */
1690 p = m->insn;
1691 regno = m->regno;
1693 if (loop_dump_stream)
1694 fprintf (loop_dump_stream, "savings %d ", savings);
1696 if (regs->array[regno].moved_once && loop_dump_stream)
1697 fprintf (loop_dump_stream, "halved since already moved ");
1699 /* An insn MUST be moved if we already moved something else
1700 which is safe only if this one is moved too: that is,
1701 if already_moved[REGNO] is nonzero. */
1703 /* An insn is desirable to move if the new lifetime of the
1704 register is no more than THRESHOLD times the old lifetime.
1705 If it's not desirable, it means the loop is so big
1706 that moving won't speed things up much,
1707 and it is liable to make register usage worse. */
1709 /* It is also desirable to move if it can be moved at no
1710 extra cost because something else was already moved. */
1712 if (already_moved[regno]
1713 || flag_move_all_movables
1714 || (threshold * savings * m->lifetime) >=
1715 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
1716 || (m->forces && m->forces->done
1717 && regs->array[m->forces->regno].n_times_set == 1))
1719 int count;
1720 register struct movable *m1;
1721 rtx first = NULL_RTX;
1723 /* Now move the insns that set the reg. */
1725 if (m->partial && m->match)
1727 rtx newpat, i1;
1728 rtx r1, r2;
1729 /* Find the end of this chain of matching regs.
1730 Thus, we load each reg in the chain from that one reg.
1731 And that reg is loaded with 0 directly,
1732 since it has ->match == 0. */
1733 for (m1 = m; m1->match; m1 = m1->match);
1734 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1735 SET_DEST (PATTERN (m1->insn)));
1736 i1 = loop_insn_hoist (loop, newpat);
1738 /* Mark the moved, invariant reg as being allowed to
1739 share a hard reg with the other matching invariant. */
1740 REG_NOTES (i1) = REG_NOTES (m->insn);
1741 r1 = SET_DEST (PATTERN (m->insn));
1742 r2 = SET_DEST (PATTERN (m1->insn));
1743 regs_may_share
1744 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1745 gen_rtx_EXPR_LIST (VOIDmode, r2,
1746 regs_may_share));
1747 delete_insn (m->insn);
1749 if (new_start == 0)
1750 new_start = i1;
1752 if (loop_dump_stream)
1753 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1755 /* If we are to re-generate the item being moved with a
1756 new move insn, first delete what we have and then emit
1757 the move insn before the loop. */
1758 else if (m->move_insn)
1760 rtx i1, temp, seq;
1762 for (count = m->consec; count >= 0; count--)
1764 /* If this is the first insn of a library call sequence,
1765 skip to the end. */
1766 if (GET_CODE (p) != NOTE
1767 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1768 p = XEXP (temp, 0);
1770 /* If this is the last insn of a libcall sequence, then
1771 delete every insn in the sequence except the last.
1772 The last insn is handled in the normal manner. */
1773 if (GET_CODE (p) != NOTE
1774 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1776 temp = XEXP (temp, 0);
1777 while (temp != p)
1778 temp = delete_insn (temp);
1781 temp = p;
1782 p = delete_insn (p);
1784 /* simplify_giv_expr expects that it can walk the insns
1785 at m->insn forwards and see this old sequence we are
1786 tossing here. delete_insn does preserve the next
1787 pointers, but when we skip over a NOTE we must fix
1788 it up. Otherwise that code walks into the non-deleted
1789 insn stream. */
1790 while (p && GET_CODE (p) == NOTE)
1791 p = NEXT_INSN (temp) = NEXT_INSN (p);
1794 start_sequence ();
1795 emit_move_insn (m->set_dest, m->set_src);
1796 temp = get_insns ();
1797 seq = gen_sequence ();
1798 end_sequence ();
1800 add_label_notes (m->set_src, temp);
1802 i1 = loop_insn_hoist (loop, seq);
1803 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1804 REG_NOTES (i1)
1805 = gen_rtx_EXPR_LIST (m->is_equiv ? REG_EQUIV : REG_EQUAL,
1806 m->set_src, REG_NOTES (i1));
1808 if (loop_dump_stream)
1809 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1811 /* The more regs we move, the less we like moving them. */
1812 threshold -= 3;
1814 else
1816 for (count = m->consec; count >= 0; count--)
1818 rtx i1, temp;
1820 /* If first insn of libcall sequence, skip to end. */
1821 /* Do this at start of loop, since p is guaranteed to
1822 be an insn here. */
1823 if (GET_CODE (p) != NOTE
1824 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1825 p = XEXP (temp, 0);
1827 /* If last insn of libcall sequence, move all
1828 insns except the last before the loop. The last
1829 insn is handled in the normal manner. */
1830 if (GET_CODE (p) != NOTE
1831 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1833 rtx fn_address = 0;
1834 rtx fn_reg = 0;
1835 rtx fn_address_insn = 0;
1837 first = 0;
1838 for (temp = XEXP (temp, 0); temp != p;
1839 temp = NEXT_INSN (temp))
1841 rtx body;
1842 rtx n;
1843 rtx next;
1845 if (GET_CODE (temp) == NOTE)
1846 continue;
1848 body = PATTERN (temp);
1850 /* Find the next insn after TEMP,
1851 not counting USE or NOTE insns. */
1852 for (next = NEXT_INSN (temp); next != p;
1853 next = NEXT_INSN (next))
1854 if (! (GET_CODE (next) == INSN
1855 && GET_CODE (PATTERN (next)) == USE)
1856 && GET_CODE (next) != NOTE)
1857 break;
1859 /* If that is the call, this may be the insn
1860 that loads the function address.
1862 Extract the function address from the insn
1863 that loads it into a register.
1864 If this insn was cse'd, we get incorrect code.
1866 So emit a new move insn that copies the
1867 function address into the register that the
1868 call insn will use. flow.c will delete any
1869 redundant stores that we have created. */
1870 if (GET_CODE (next) == CALL_INSN
1871 && GET_CODE (body) == SET
1872 && GET_CODE (SET_DEST (body)) == REG
1873 && (n = find_reg_note (temp, REG_EQUAL,
1874 NULL_RTX)))
1876 fn_reg = SET_SRC (body);
1877 if (GET_CODE (fn_reg) != REG)
1878 fn_reg = SET_DEST (body);
1879 fn_address = XEXP (n, 0);
1880 fn_address_insn = temp;
1882 /* We have the call insn.
1883 If it uses the register we suspect it might,
1884 load it with the correct address directly. */
1885 if (GET_CODE (temp) == CALL_INSN
1886 && fn_address != 0
1887 && reg_referenced_p (fn_reg, body))
1888 loop_insn_emit_after (loop, 0, fn_address_insn,
1889 gen_move_insn
1890 (fn_reg, fn_address));
1892 if (GET_CODE (temp) == CALL_INSN)
1894 i1 = loop_call_insn_hoist (loop, body);
1895 /* Because the USAGE information potentially
1896 contains objects other than hard registers
1897 we need to copy it. */
1898 if (CALL_INSN_FUNCTION_USAGE (temp))
1899 CALL_INSN_FUNCTION_USAGE (i1)
1900 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1902 else
1903 i1 = loop_insn_hoist (loop, body);
1904 if (first == 0)
1905 first = i1;
1906 if (temp == fn_address_insn)
1907 fn_address_insn = i1;
1908 REG_NOTES (i1) = REG_NOTES (temp);
1909 delete_insn (temp);
1911 if (new_start == 0)
1912 new_start = first;
1914 if (m->savemode != VOIDmode)
1916 /* P sets REG to zero; but we should clear only
1917 the bits that are not covered by the mode
1918 m->savemode. */
1919 rtx reg = m->set_dest;
1920 rtx sequence;
1921 rtx tem;
1923 start_sequence ();
1924 tem = expand_binop
1925 (GET_MODE (reg), and_optab, reg,
1926 GEN_INT ((((HOST_WIDE_INT) 1
1927 << GET_MODE_BITSIZE (m->savemode)))
1928 - 1),
1929 reg, 1, OPTAB_LIB_WIDEN);
1930 if (tem == 0)
1931 abort ();
1932 if (tem != reg)
1933 emit_move_insn (reg, tem);
1934 sequence = gen_sequence ();
1935 end_sequence ();
1936 i1 = loop_insn_hoist (loop, sequence);
1938 else if (GET_CODE (p) == CALL_INSN)
1940 i1 = loop_call_insn_hoist (loop, PATTERN (p));
1941 /* Because the USAGE information potentially
1942 contains objects other than hard registers
1943 we need to copy it. */
1944 if (CALL_INSN_FUNCTION_USAGE (p))
1945 CALL_INSN_FUNCTION_USAGE (i1)
1946 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
1948 else if (count == m->consec && m->move_insn_first)
1950 rtx seq;
1951 /* The SET_SRC might not be invariant, so we must
1952 use the REG_EQUAL note. */
1953 start_sequence ();
1954 emit_move_insn (m->set_dest, m->set_src);
1955 temp = get_insns ();
1956 seq = gen_sequence ();
1957 end_sequence ();
1959 add_label_notes (m->set_src, temp);
1961 i1 = loop_insn_hoist (loop, seq);
1962 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1963 REG_NOTES (i1)
1964 = gen_rtx_EXPR_LIST ((m->is_equiv ? REG_EQUIV
1965 : REG_EQUAL),
1966 m->set_src, REG_NOTES (i1));
1968 else
1969 i1 = loop_insn_hoist (loop, PATTERN (p));
1971 if (REG_NOTES (i1) == 0)
1973 REG_NOTES (i1) = REG_NOTES (p);
1975 /* If there is a REG_EQUAL note present whose value
1976 is not loop invariant, then delete it, since it
1977 may cause problems with later optimization passes.
1978 It is possible for cse to create such notes
1979 like this as a result of record_jump_cond. */
1981 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
1982 && ! loop_invariant_p (loop, XEXP (temp, 0)))
1983 remove_note (i1, temp);
1986 if (new_start == 0)
1987 new_start = i1;
1989 if (loop_dump_stream)
1990 fprintf (loop_dump_stream, " moved to %d",
1991 INSN_UID (i1));
1993 /* If library call, now fix the REG_NOTES that contain
1994 insn pointers, namely REG_LIBCALL on FIRST
1995 and REG_RETVAL on I1. */
1996 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
1998 XEXP (temp, 0) = first;
1999 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2000 XEXP (temp, 0) = i1;
2003 temp = p;
2004 delete_insn (p);
2005 p = NEXT_INSN (p);
2007 /* simplify_giv_expr expects that it can walk the insns
2008 at m->insn forwards and see this old sequence we are
2009 tossing here. delete_insn does preserve the next
2010 pointers, but when we skip over a NOTE we must fix
2011 it up. Otherwise that code walks into the non-deleted
2012 insn stream. */
2013 while (p && GET_CODE (p) == NOTE)
2014 p = NEXT_INSN (temp) = NEXT_INSN (p);
2017 /* The more regs we move, the less we like moving them. */
2018 threshold -= 3;
2021 /* Any other movable that loads the same register
2022 MUST be moved. */
2023 already_moved[regno] = 1;
2025 /* This reg has been moved out of one loop. */
2026 regs->array[regno].moved_once = 1;
2028 /* The reg set here is now invariant. */
2029 if (! m->partial)
2030 regs->array[regno].set_in_loop = 0;
2032 m->done = 1;
2034 /* Change the length-of-life info for the register
2035 to say it lives at least the full length of this loop.
2036 This will help guide optimizations in outer loops. */
2038 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2039 /* This is the old insn before all the moved insns.
2040 We can't use the moved insn because it is out of range
2041 in uid_luid. Only the old insns have luids. */
2042 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2043 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2044 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2046 /* Combine with this moved insn any other matching movables. */
2048 if (! m->partial)
2049 for (m1 = movables->head; m1; m1 = m1->next)
2050 if (m1->match == m)
2052 rtx temp;
2054 /* Schedule the reg loaded by M1
2055 for replacement so that shares the reg of M.
2056 If the modes differ (only possible in restricted
2057 circumstances, make a SUBREG.
2059 Note this assumes that the target dependent files
2060 treat REG and SUBREG equally, including within
2061 GO_IF_LEGITIMATE_ADDRESS and in all the
2062 predicates since we never verify that replacing the
2063 original register with a SUBREG results in a
2064 recognizable insn. */
2065 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2066 reg_map[m1->regno] = m->set_dest;
2067 else
2068 reg_map[m1->regno]
2069 = gen_lowpart_common (GET_MODE (m1->set_dest),
2070 m->set_dest);
2072 /* Get rid of the matching insn
2073 and prevent further processing of it. */
2074 m1->done = 1;
2076 /* if library call, delete all insn except last, which
2077 is deleted below */
2078 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2079 NULL_RTX)))
2081 for (temp = XEXP (temp, 0); temp != m1->insn;
2082 temp = NEXT_INSN (temp))
2083 delete_insn (temp);
2085 delete_insn (m1->insn);
2087 /* Any other movable that loads the same register
2088 MUST be moved. */
2089 already_moved[m1->regno] = 1;
2091 /* The reg merged here is now invariant,
2092 if the reg it matches is invariant. */
2093 if (! m->partial)
2094 regs->array[m1->regno].set_in_loop = 0;
2097 else if (loop_dump_stream)
2098 fprintf (loop_dump_stream, "not desirable");
2100 else if (loop_dump_stream && !m->match)
2101 fprintf (loop_dump_stream, "not safe");
2103 if (loop_dump_stream)
2104 fprintf (loop_dump_stream, "\n");
2107 if (new_start == 0)
2108 new_start = loop_start;
2110 /* Go through all the instructions in the loop, making
2111 all the register substitutions scheduled in REG_MAP. */
2112 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2113 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2114 || GET_CODE (p) == CALL_INSN)
2116 replace_regs (PATTERN (p), reg_map, nregs, 0);
2117 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2118 INSN_CODE (p) = -1;
2121 /* Clean up. */
2122 free (reg_map);
2123 free (already_moved);
2127 static void
2128 loop_movables_add (movables, m)
2129 struct loop_movables *movables;
2130 struct movable *m;
2132 if (movables->head == 0)
2133 movables->head = m;
2134 else
2135 movables->last->next = m;
2136 movables->last = m;
2140 static void
2141 loop_movables_free (movables)
2142 struct loop_movables *movables;
2144 struct movable *m;
2145 struct movable *m_next;
2147 for (m = movables->head; m; m = m_next)
2149 m_next = m->next;
2150 free (m);
2154 #if 0
2155 /* Scan X and replace the address of any MEM in it with ADDR.
2156 REG is the address that MEM should have before the replacement. */
2158 static void
2159 replace_call_address (x, reg, addr)
2160 rtx x, reg, addr;
2162 register enum rtx_code code;
2163 register int i;
2164 register const char *fmt;
2166 if (x == 0)
2167 return;
2168 code = GET_CODE (x);
2169 switch (code)
2171 case PC:
2172 case CC0:
2173 case CONST_INT:
2174 case CONST_DOUBLE:
2175 case CONST:
2176 case SYMBOL_REF:
2177 case LABEL_REF:
2178 case REG:
2179 return;
2181 case SET:
2182 /* Short cut for very common case. */
2183 replace_call_address (XEXP (x, 1), reg, addr);
2184 return;
2186 case CALL:
2187 /* Short cut for very common case. */
2188 replace_call_address (XEXP (x, 0), reg, addr);
2189 return;
2191 case MEM:
2192 /* If this MEM uses a reg other than the one we expected,
2193 something is wrong. */
2194 if (XEXP (x, 0) != reg)
2195 abort ();
2196 XEXP (x, 0) = addr;
2197 return;
2199 default:
2200 break;
2203 fmt = GET_RTX_FORMAT (code);
2204 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2206 if (fmt[i] == 'e')
2207 replace_call_address (XEXP (x, i), reg, addr);
2208 else if (fmt[i] == 'E')
2210 register int j;
2211 for (j = 0; j < XVECLEN (x, i); j++)
2212 replace_call_address (XVECEXP (x, i, j), reg, addr);
2216 #endif
2218 /* Return the number of memory refs to addresses that vary
2219 in the rtx X. */
2221 static int
2222 count_nonfixed_reads (loop, x)
2223 const struct loop *loop;
2224 rtx x;
2226 register enum rtx_code code;
2227 register int i;
2228 register const char *fmt;
2229 int value;
2231 if (x == 0)
2232 return 0;
2234 code = GET_CODE (x);
2235 switch (code)
2237 case PC:
2238 case CC0:
2239 case CONST_INT:
2240 case CONST_DOUBLE:
2241 case CONST:
2242 case SYMBOL_REF:
2243 case LABEL_REF:
2244 case REG:
2245 return 0;
2247 case MEM:
2248 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2249 + count_nonfixed_reads (loop, XEXP (x, 0)));
2251 default:
2252 break;
2255 value = 0;
2256 fmt = GET_RTX_FORMAT (code);
2257 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2259 if (fmt[i] == 'e')
2260 value += count_nonfixed_reads (loop, XEXP (x, i));
2261 if (fmt[i] == 'E')
2263 register int j;
2264 for (j = 0; j < XVECLEN (x, i); j++)
2265 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2268 return value;
2271 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2272 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2273 `unknown_address_altered', `unknown_constant_address_altered', and
2274 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2275 list `store_mems' in LOOP. */
2277 static void
2278 prescan_loop (loop)
2279 struct loop *loop;
2281 register int level = 1;
2282 rtx insn;
2283 struct loop_info *loop_info = LOOP_INFO (loop);
2284 rtx start = loop->start;
2285 rtx end = loop->end;
2286 /* The label after END. Jumping here is just like falling off the
2287 end of the loop. We use next_nonnote_insn instead of next_label
2288 as a hedge against the (pathological) case where some actual insn
2289 might end up between the two. */
2290 rtx exit_target = next_nonnote_insn (end);
2292 loop_info->has_indirect_jump = indirect_jump_in_function;
2293 loop_info->pre_header_has_call = 0;
2294 loop_info->has_call = 0;
2295 loop_info->has_nonconst_call = 0;
2296 loop_info->has_volatile = 0;
2297 loop_info->has_tablejump = 0;
2298 loop_info->has_multiple_exit_targets = 0;
2299 loop->level = 1;
2301 loop_info->unknown_address_altered = 0;
2302 loop_info->unknown_constant_address_altered = 0;
2303 loop_info->store_mems = NULL_RTX;
2304 loop_info->first_loop_store_insn = NULL_RTX;
2305 loop_info->mems_idx = 0;
2306 loop_info->num_mem_sets = 0;
2309 for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
2310 insn = PREV_INSN (insn))
2312 if (GET_CODE (insn) == CALL_INSN)
2314 loop_info->pre_header_has_call = 1;
2315 break;
2319 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2320 insn = NEXT_INSN (insn))
2322 if (GET_CODE (insn) == NOTE)
2324 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2326 ++level;
2327 /* Count number of loops contained in this one. */
2328 loop->level++;
2330 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2332 --level;
2335 else if (GET_CODE (insn) == CALL_INSN)
2337 if (! CONST_CALL_P (insn))
2339 loop_info->unknown_address_altered = 1;
2340 loop_info->has_nonconst_call = 1;
2342 loop_info->has_call = 1;
2344 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2346 rtx label1 = NULL_RTX;
2347 rtx label2 = NULL_RTX;
2349 if (volatile_refs_p (PATTERN (insn)))
2350 loop_info->has_volatile = 1;
2352 if (GET_CODE (insn) == JUMP_INSN
2353 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2354 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2355 loop_info->has_tablejump = 1;
2357 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2358 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2359 loop_info->first_loop_store_insn = insn;
2361 if (! loop_info->has_multiple_exit_targets
2362 && GET_CODE (insn) == JUMP_INSN
2363 && GET_CODE (PATTERN (insn)) == SET
2364 && SET_DEST (PATTERN (insn)) == pc_rtx)
2366 if (GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE)
2368 label1 = XEXP (SET_SRC (PATTERN (insn)), 1);
2369 label2 = XEXP (SET_SRC (PATTERN (insn)), 2);
2371 else
2373 label1 = SET_SRC (PATTERN (insn));
2378 if (label1 && label1 != pc_rtx)
2380 if (GET_CODE (label1) != LABEL_REF)
2382 /* Something tricky. */
2383 loop_info->has_multiple_exit_targets = 1;
2384 break;
2386 else if (XEXP (label1, 0) != exit_target
2387 && LABEL_OUTSIDE_LOOP_P (label1))
2389 /* A jump outside the current loop. */
2390 loop_info->has_multiple_exit_targets = 1;
2391 break;
2395 label1 = label2;
2396 label2 = NULL_RTX;
2398 while (label1);
2401 else if (GET_CODE (insn) == RETURN)
2402 loop_info->has_multiple_exit_targets = 1;
2405 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2406 if (/* An exception thrown by a called function might land us
2407 anywhere. */
2408 ! loop_info->has_nonconst_call
2409 /* We don't want loads for MEMs moved to a location before the
2410 one at which their stack memory becomes allocated. (Note
2411 that this is not a problem for malloc, etc., since those
2412 require actual function calls. */
2413 && ! current_function_calls_alloca
2414 /* There are ways to leave the loop other than falling off the
2415 end. */
2416 && ! loop_info->has_multiple_exit_targets)
2417 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2418 insn = NEXT_INSN (insn))
2419 for_each_rtx (&insn, insert_loop_mem, loop_info);
2421 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2422 that loop_invariant_p and load_mems can use true_dependence
2423 to determine what is really clobbered. */
2424 if (loop_info->unknown_address_altered)
2426 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2428 loop_info->store_mems
2429 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2431 if (loop_info->unknown_constant_address_altered)
2433 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2435 RTX_UNCHANGING_P (mem) = 1;
2436 loop_info->store_mems
2437 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2441 /* Scan the function looking for loops. Record the start and end of each loop.
2442 Also mark as invalid loops any loops that contain a setjmp or are branched
2443 to from outside the loop. */
2445 static void
2446 find_and_verify_loops (f, loops)
2447 rtx f;
2448 struct loops *loops;
2450 rtx insn;
2451 rtx label;
2452 int num_loops;
2453 struct loop *current_loop;
2454 struct loop *next_loop;
2455 struct loop *loop;
2457 num_loops = loops->num;
2459 compute_luids (f, NULL_RTX, 0);
2461 /* If there are jumps to undefined labels,
2462 treat them as jumps out of any/all loops.
2463 This also avoids writing past end of tables when there are no loops. */
2464 uid_loop[0] = NULL;
2466 /* Find boundaries of loops, mark which loops are contained within
2467 loops, and invalidate loops that have setjmp. */
2469 num_loops = 0;
2470 current_loop = NULL;
2471 for (insn = f; insn; insn = NEXT_INSN (insn))
2473 if (GET_CODE (insn) == NOTE)
2474 switch (NOTE_LINE_NUMBER (insn))
2476 case NOTE_INSN_LOOP_BEG:
2477 next_loop = loops->array + num_loops;
2478 next_loop->num = num_loops;
2479 num_loops++;
2480 next_loop->start = insn;
2481 next_loop->outer = current_loop;
2482 current_loop = next_loop;
2483 break;
2485 case NOTE_INSN_SETJMP:
2486 /* In this case, we must invalidate our current loop and any
2487 enclosing loop. */
2488 for (loop = current_loop; loop; loop = loop->outer)
2490 loop->invalid = 1;
2491 if (loop_dump_stream)
2492 fprintf (loop_dump_stream,
2493 "\nLoop at %d ignored due to setjmp.\n",
2494 INSN_UID (loop->start));
2496 break;
2498 case NOTE_INSN_LOOP_CONT:
2499 current_loop->cont = insn;
2500 break;
2502 case NOTE_INSN_LOOP_VTOP:
2503 current_loop->vtop = insn;
2504 break;
2506 case NOTE_INSN_LOOP_END:
2507 if (! current_loop)
2508 abort ();
2510 current_loop->end = insn;
2511 current_loop = current_loop->outer;
2512 break;
2514 default:
2515 break;
2518 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2519 enclosing loop, but this doesn't matter. */
2520 uid_loop[INSN_UID (insn)] = current_loop;
2523 /* Any loop containing a label used in an initializer must be invalidated,
2524 because it can be jumped into from anywhere. */
2526 for (label = forced_labels; label; label = XEXP (label, 1))
2528 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2529 loop; loop = loop->outer)
2530 loop->invalid = 1;
2533 /* Any loop containing a label used for an exception handler must be
2534 invalidated, because it can be jumped into from anywhere. */
2536 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2538 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2539 loop; loop = loop->outer)
2540 loop->invalid = 1;
2543 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2544 loop that it is not contained within, that loop is marked invalid.
2545 If any INSN or CALL_INSN uses a label's address, then the loop containing
2546 that label is marked invalid, because it could be jumped into from
2547 anywhere.
2549 Also look for blocks of code ending in an unconditional branch that
2550 exits the loop. If such a block is surrounded by a conditional
2551 branch around the block, move the block elsewhere (see below) and
2552 invert the jump to point to the code block. This may eliminate a
2553 label in our loop and will simplify processing by both us and a
2554 possible second cse pass. */
2556 for (insn = f; insn; insn = NEXT_INSN (insn))
2557 if (INSN_P (insn))
2559 struct loop *this_loop = uid_loop[INSN_UID (insn)];
2561 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2563 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2564 if (note)
2566 for (loop = uid_loop[INSN_UID (XEXP (note, 0))];
2567 loop; loop = loop->outer)
2568 loop->invalid = 1;
2572 if (GET_CODE (insn) != JUMP_INSN)
2573 continue;
2575 mark_loop_jump (PATTERN (insn), this_loop);
2577 /* See if this is an unconditional branch outside the loop. */
2578 if (this_loop
2579 && (GET_CODE (PATTERN (insn)) == RETURN
2580 || (any_uncondjump_p (insn)
2581 && onlyjump_p (insn)
2582 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2583 != this_loop)))
2584 && get_max_uid () < max_uid_for_loop)
2586 rtx p;
2587 rtx our_next = next_real_insn (insn);
2588 rtx last_insn_to_move = NEXT_INSN (insn);
2589 struct loop *dest_loop;
2590 struct loop *outer_loop = NULL;
2592 /* Go backwards until we reach the start of the loop, a label,
2593 or a JUMP_INSN. */
2594 for (p = PREV_INSN (insn);
2595 GET_CODE (p) != CODE_LABEL
2596 && ! (GET_CODE (p) == NOTE
2597 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2598 && GET_CODE (p) != JUMP_INSN;
2599 p = PREV_INSN (p))
2602 /* Check for the case where we have a jump to an inner nested
2603 loop, and do not perform the optimization in that case. */
2605 if (JUMP_LABEL (insn))
2607 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2608 if (dest_loop)
2610 for (outer_loop = dest_loop; outer_loop;
2611 outer_loop = outer_loop->outer)
2612 if (outer_loop == this_loop)
2613 break;
2617 /* Make sure that the target of P is within the current loop. */
2619 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2620 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2621 outer_loop = this_loop;
2623 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2624 we have a block of code to try to move.
2626 We look backward and then forward from the target of INSN
2627 to find a BARRIER at the same loop depth as the target.
2628 If we find such a BARRIER, we make a new label for the start
2629 of the block, invert the jump in P and point it to that label,
2630 and move the block of code to the spot we found. */
2632 if (! outer_loop
2633 && GET_CODE (p) == JUMP_INSN
2634 && JUMP_LABEL (p) != 0
2635 /* Just ignore jumps to labels that were never emitted.
2636 These always indicate compilation errors. */
2637 && INSN_UID (JUMP_LABEL (p)) != 0
2638 && any_condjump_p (p) && onlyjump_p (p)
2639 && next_real_insn (JUMP_LABEL (p)) == our_next
2640 /* If it's not safe to move the sequence, then we
2641 mustn't try. */
2642 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2643 &last_insn_to_move))
2645 rtx target
2646 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2647 struct loop *target_loop = uid_loop[INSN_UID (target)];
2648 rtx loc, loc2;
2650 for (loc = target; loc; loc = PREV_INSN (loc))
2651 if (GET_CODE (loc) == BARRIER
2652 /* Don't move things inside a tablejump. */
2653 && ((loc2 = next_nonnote_insn (loc)) == 0
2654 || GET_CODE (loc2) != CODE_LABEL
2655 || (loc2 = next_nonnote_insn (loc2)) == 0
2656 || GET_CODE (loc2) != JUMP_INSN
2657 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2658 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2659 && uid_loop[INSN_UID (loc)] == target_loop)
2660 break;
2662 if (loc == 0)
2663 for (loc = target; loc; loc = NEXT_INSN (loc))
2664 if (GET_CODE (loc) == BARRIER
2665 /* Don't move things inside a tablejump. */
2666 && ((loc2 = next_nonnote_insn (loc)) == 0
2667 || GET_CODE (loc2) != CODE_LABEL
2668 || (loc2 = next_nonnote_insn (loc2)) == 0
2669 || GET_CODE (loc2) != JUMP_INSN
2670 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2671 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2672 && uid_loop[INSN_UID (loc)] == target_loop)
2673 break;
2675 if (loc)
2677 rtx cond_label = JUMP_LABEL (p);
2678 rtx new_label = get_label_after (p);
2680 /* Ensure our label doesn't go away. */
2681 LABEL_NUSES (cond_label)++;
2683 /* Verify that uid_loop is large enough and that
2684 we can invert P. */
2685 if (invert_jump (p, new_label, 1))
2687 rtx q, r;
2689 /* If no suitable BARRIER was found, create a suitable
2690 one before TARGET. Since TARGET is a fall through
2691 path, we'll need to insert an jump around our block
2692 and a add a BARRIER before TARGET.
2694 This creates an extra unconditional jump outside
2695 the loop. However, the benefits of removing rarely
2696 executed instructions from inside the loop usually
2697 outweighs the cost of the extra unconditional jump
2698 outside the loop. */
2699 if (loc == 0)
2701 rtx temp;
2703 temp = gen_jump (JUMP_LABEL (insn));
2704 temp = emit_jump_insn_before (temp, target);
2705 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2706 LABEL_NUSES (JUMP_LABEL (insn))++;
2707 loc = emit_barrier_before (target);
2710 /* Include the BARRIER after INSN and copy the
2711 block after LOC. */
2712 new_label = squeeze_notes (new_label,
2713 last_insn_to_move);
2714 reorder_insns (new_label, last_insn_to_move, loc);
2716 /* All those insns are now in TARGET_LOOP. */
2717 for (q = new_label;
2718 q != NEXT_INSN (last_insn_to_move);
2719 q = NEXT_INSN (q))
2720 uid_loop[INSN_UID (q)] = target_loop;
2722 /* The label jumped to by INSN is no longer a loop
2723 exit. Unless INSN does not have a label (e.g.,
2724 it is a RETURN insn), search loop->exit_labels
2725 to find its label_ref, and remove it. Also turn
2726 off LABEL_OUTSIDE_LOOP_P bit. */
2727 if (JUMP_LABEL (insn))
2729 for (q = 0, r = this_loop->exit_labels;
2731 q = r, r = LABEL_NEXTREF (r))
2732 if (XEXP (r, 0) == JUMP_LABEL (insn))
2734 LABEL_OUTSIDE_LOOP_P (r) = 0;
2735 if (q)
2736 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2737 else
2738 this_loop->exit_labels = LABEL_NEXTREF (r);
2739 break;
2742 for (loop = this_loop; loop && loop != target_loop;
2743 loop = loop->outer)
2744 loop->exit_count--;
2746 /* If we didn't find it, then something is
2747 wrong. */
2748 if (! r)
2749 abort ();
2752 /* P is now a jump outside the loop, so it must be put
2753 in loop->exit_labels, and marked as such.
2754 The easiest way to do this is to just call
2755 mark_loop_jump again for P. */
2756 mark_loop_jump (PATTERN (p), this_loop);
2758 /* If INSN now jumps to the insn after it,
2759 delete INSN. */
2760 if (JUMP_LABEL (insn) != 0
2761 && (next_real_insn (JUMP_LABEL (insn))
2762 == next_real_insn (insn)))
2763 delete_insn (insn);
2766 /* Continue the loop after where the conditional
2767 branch used to jump, since the only branch insn
2768 in the block (if it still remains) is an inter-loop
2769 branch and hence needs no processing. */
2770 insn = NEXT_INSN (cond_label);
2772 if (--LABEL_NUSES (cond_label) == 0)
2773 delete_insn (cond_label);
2775 /* This loop will be continued with NEXT_INSN (insn). */
2776 insn = PREV_INSN (insn);
2783 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2784 loops it is contained in, mark the target loop invalid.
2786 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2788 static void
2789 mark_loop_jump (x, loop)
2790 rtx x;
2791 struct loop *loop;
2793 struct loop *dest_loop;
2794 struct loop *outer_loop;
2795 int i;
2797 switch (GET_CODE (x))
2799 case PC:
2800 case USE:
2801 case CLOBBER:
2802 case REG:
2803 case MEM:
2804 case CONST_INT:
2805 case CONST_DOUBLE:
2806 case RETURN:
2807 return;
2809 case CONST:
2810 /* There could be a label reference in here. */
2811 mark_loop_jump (XEXP (x, 0), loop);
2812 return;
2814 case PLUS:
2815 case MINUS:
2816 case MULT:
2817 mark_loop_jump (XEXP (x, 0), loop);
2818 mark_loop_jump (XEXP (x, 1), loop);
2819 return;
2821 case LO_SUM:
2822 /* This may refer to a LABEL_REF or SYMBOL_REF. */
2823 mark_loop_jump (XEXP (x, 1), loop);
2824 return;
2826 case SIGN_EXTEND:
2827 case ZERO_EXTEND:
2828 mark_loop_jump (XEXP (x, 0), loop);
2829 return;
2831 case LABEL_REF:
2832 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
2834 /* Link together all labels that branch outside the loop. This
2835 is used by final_[bg]iv_value and the loop unrolling code. Also
2836 mark this LABEL_REF so we know that this branch should predict
2837 false. */
2839 /* A check to make sure the label is not in an inner nested loop,
2840 since this does not count as a loop exit. */
2841 if (dest_loop)
2843 for (outer_loop = dest_loop; outer_loop;
2844 outer_loop = outer_loop->outer)
2845 if (outer_loop == loop)
2846 break;
2848 else
2849 outer_loop = NULL;
2851 if (loop && ! outer_loop)
2853 LABEL_OUTSIDE_LOOP_P (x) = 1;
2854 LABEL_NEXTREF (x) = loop->exit_labels;
2855 loop->exit_labels = x;
2857 for (outer_loop = loop;
2858 outer_loop && outer_loop != dest_loop;
2859 outer_loop = outer_loop->outer)
2860 outer_loop->exit_count++;
2863 /* If this is inside a loop, but not in the current loop or one enclosed
2864 by it, it invalidates at least one loop. */
2866 if (! dest_loop)
2867 return;
2869 /* We must invalidate every nested loop containing the target of this
2870 label, except those that also contain the jump insn. */
2872 for (; dest_loop; dest_loop = dest_loop->outer)
2874 /* Stop when we reach a loop that also contains the jump insn. */
2875 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
2876 if (dest_loop == outer_loop)
2877 return;
2879 /* If we get here, we know we need to invalidate a loop. */
2880 if (loop_dump_stream && ! dest_loop->invalid)
2881 fprintf (loop_dump_stream,
2882 "\nLoop at %d ignored due to multiple entry points.\n",
2883 INSN_UID (dest_loop->start));
2885 dest_loop->invalid = 1;
2887 return;
2889 case SET:
2890 /* If this is not setting pc, ignore. */
2891 if (SET_DEST (x) == pc_rtx)
2892 mark_loop_jump (SET_SRC (x), loop);
2893 return;
2895 case IF_THEN_ELSE:
2896 mark_loop_jump (XEXP (x, 1), loop);
2897 mark_loop_jump (XEXP (x, 2), loop);
2898 return;
2900 case PARALLEL:
2901 case ADDR_VEC:
2902 for (i = 0; i < XVECLEN (x, 0); i++)
2903 mark_loop_jump (XVECEXP (x, 0, i), loop);
2904 return;
2906 case ADDR_DIFF_VEC:
2907 for (i = 0; i < XVECLEN (x, 1); i++)
2908 mark_loop_jump (XVECEXP (x, 1, i), loop);
2909 return;
2911 default:
2912 /* Strictly speaking this is not a jump into the loop, only a possible
2913 jump out of the loop. However, we have no way to link the destination
2914 of this jump onto the list of exit labels. To be safe we mark this
2915 loop and any containing loops as invalid. */
2916 if (loop)
2918 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
2920 if (loop_dump_stream && ! outer_loop->invalid)
2921 fprintf (loop_dump_stream,
2922 "\nLoop at %d ignored due to unknown exit jump.\n",
2923 INSN_UID (outer_loop->start));
2924 outer_loop->invalid = 1;
2927 return;
2931 /* Return nonzero if there is a label in the range from
2932 insn INSN to and including the insn whose luid is END
2933 INSN must have an assigned luid (i.e., it must not have
2934 been previously created by loop.c). */
2936 static int
2937 labels_in_range_p (insn, end)
2938 rtx insn;
2939 int end;
2941 while (insn && INSN_LUID (insn) <= end)
2943 if (GET_CODE (insn) == CODE_LABEL)
2944 return 1;
2945 insn = NEXT_INSN (insn);
2948 return 0;
2951 /* Record that a memory reference X is being set. */
2953 static void
2954 note_addr_stored (x, y, data)
2955 rtx x;
2956 rtx y ATTRIBUTE_UNUSED;
2957 void *data ATTRIBUTE_UNUSED;
2959 struct loop_info *loop_info = data;
2961 if (x == 0 || GET_CODE (x) != MEM)
2962 return;
2964 /* Count number of memory writes.
2965 This affects heuristics in strength_reduce. */
2966 loop_info->num_mem_sets++;
2968 /* BLKmode MEM means all memory is clobbered. */
2969 if (GET_MODE (x) == BLKmode)
2971 if (RTX_UNCHANGING_P (x))
2972 loop_info->unknown_constant_address_altered = 1;
2973 else
2974 loop_info->unknown_address_altered = 1;
2976 return;
2979 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
2980 loop_info->store_mems);
2983 /* X is a value modified by an INSN that references a biv inside a loop
2984 exit test (ie, X is somehow related to the value of the biv). If X
2985 is a pseudo that is used more than once, then the biv is (effectively)
2986 used more than once. DATA is a pointer to a loop_regs structure. */
2988 static void
2989 note_set_pseudo_multiple_uses (x, y, data)
2990 rtx x;
2991 rtx y ATTRIBUTE_UNUSED;
2992 void *data;
2994 struct loop_regs *regs = (struct loop_regs *) data;
2996 if (x == 0)
2997 return;
2999 while (GET_CODE (x) == STRICT_LOW_PART
3000 || GET_CODE (x) == SIGN_EXTRACT
3001 || GET_CODE (x) == ZERO_EXTRACT
3002 || GET_CODE (x) == SUBREG)
3003 x = XEXP (x, 0);
3005 if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3006 return;
3008 /* If we do not have usage information, or if we know the register
3009 is used more than once, note that fact for check_dbra_loop. */
3010 if (REGNO (x) >= max_reg_before_loop
3011 || ! regs->array[REGNO (x)].single_usage
3012 || regs->array[REGNO (x)].single_usage == const0_rtx)
3013 regs->multiple_uses = 1;
3016 /* Return nonzero if the rtx X is invariant over the current loop.
3018 The value is 2 if we refer to something only conditionally invariant.
3020 A memory ref is invariant if it is not volatile and does not conflict
3021 with anything stored in `loop_info->store_mems'. */
3024 loop_invariant_p (loop, x)
3025 const struct loop *loop;
3026 register rtx x;
3028 struct loop_info *loop_info = LOOP_INFO (loop);
3029 struct loop_regs *regs = LOOP_REGS (loop);
3030 register int i;
3031 register enum rtx_code code;
3032 register const char *fmt;
3033 int conditional = 0;
3034 rtx mem_list_entry;
3036 if (x == 0)
3037 return 1;
3038 code = GET_CODE (x);
3039 switch (code)
3041 case CONST_INT:
3042 case CONST_DOUBLE:
3043 case SYMBOL_REF:
3044 case CONST:
3045 return 1;
3047 case LABEL_REF:
3048 /* A LABEL_REF is normally invariant, however, if we are unrolling
3049 loops, and this label is inside the loop, then it isn't invariant.
3050 This is because each unrolled copy of the loop body will have
3051 a copy of this label. If this was invariant, then an insn loading
3052 the address of this label into a register might get moved outside
3053 the loop, and then each loop body would end up using the same label.
3055 We don't know the loop bounds here though, so just fail for all
3056 labels. */
3057 if (flag_unroll_loops)
3058 return 0;
3059 else
3060 return 1;
3062 case PC:
3063 case CC0:
3064 case UNSPEC_VOLATILE:
3065 return 0;
3067 case REG:
3068 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3069 since the reg might be set by initialization within the loop. */
3071 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3072 || x == arg_pointer_rtx)
3073 && ! current_function_has_nonlocal_goto)
3074 return 1;
3076 if (LOOP_INFO (loop)->has_call
3077 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3078 return 0;
3080 if (regs->array[REGNO (x)].set_in_loop < 0)
3081 return 2;
3083 return regs->array[REGNO (x)].set_in_loop == 0;
3085 case MEM:
3086 /* Volatile memory references must be rejected. Do this before
3087 checking for read-only items, so that volatile read-only items
3088 will be rejected also. */
3089 if (MEM_VOLATILE_P (x))
3090 return 0;
3092 /* See if there is any dependence between a store and this load. */
3093 mem_list_entry = loop_info->store_mems;
3094 while (mem_list_entry)
3096 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3097 x, rtx_varies_p))
3098 return 0;
3100 mem_list_entry = XEXP (mem_list_entry, 1);
3103 /* It's not invalidated by a store in memory
3104 but we must still verify the address is invariant. */
3105 break;
3107 case ASM_OPERANDS:
3108 /* Don't mess with insns declared volatile. */
3109 if (MEM_VOLATILE_P (x))
3110 return 0;
3111 break;
3113 default:
3114 break;
3117 fmt = GET_RTX_FORMAT (code);
3118 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3120 if (fmt[i] == 'e')
3122 int tem = loop_invariant_p (loop, XEXP (x, i));
3123 if (tem == 0)
3124 return 0;
3125 if (tem == 2)
3126 conditional = 1;
3128 else if (fmt[i] == 'E')
3130 register int j;
3131 for (j = 0; j < XVECLEN (x, i); j++)
3133 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3134 if (tem == 0)
3135 return 0;
3136 if (tem == 2)
3137 conditional = 1;
3143 return 1 + conditional;
3146 /* Return nonzero if all the insns in the loop that set REG
3147 are INSN and the immediately following insns,
3148 and if each of those insns sets REG in an invariant way
3149 (not counting uses of REG in them).
3151 The value is 2 if some of these insns are only conditionally invariant.
3153 We assume that INSN itself is the first set of REG
3154 and that its source is invariant. */
3156 static int
3157 consec_sets_invariant_p (loop, reg, n_sets, insn)
3158 const struct loop *loop;
3159 int n_sets;
3160 rtx reg, insn;
3162 struct loop_regs *regs = LOOP_REGS (loop);
3163 rtx p = insn;
3164 unsigned int regno = REGNO (reg);
3165 rtx temp;
3166 /* Number of sets we have to insist on finding after INSN. */
3167 int count = n_sets - 1;
3168 int old = regs->array[regno].set_in_loop;
3169 int value = 0;
3170 int this;
3172 /* If N_SETS hit the limit, we can't rely on its value. */
3173 if (n_sets == 127)
3174 return 0;
3176 regs->array[regno].set_in_loop = 0;
3178 while (count > 0)
3180 register enum rtx_code code;
3181 rtx set;
3183 p = NEXT_INSN (p);
3184 code = GET_CODE (p);
3186 /* If library call, skip to end of it. */
3187 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3188 p = XEXP (temp, 0);
3190 this = 0;
3191 if (code == INSN
3192 && (set = single_set (p))
3193 && GET_CODE (SET_DEST (set)) == REG
3194 && REGNO (SET_DEST (set)) == regno)
3196 this = loop_invariant_p (loop, SET_SRC (set));
3197 if (this != 0)
3198 value |= this;
3199 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3201 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3202 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3203 notes are OK. */
3204 this = (CONSTANT_P (XEXP (temp, 0))
3205 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3206 && loop_invariant_p (loop, XEXP (temp, 0))));
3207 if (this != 0)
3208 value |= this;
3211 if (this != 0)
3212 count--;
3213 else if (code != NOTE)
3215 regs->array[regno].set_in_loop = old;
3216 return 0;
3220 regs->array[regno].set_in_loop = old;
3221 /* If loop_invariant_p ever returned 2, we return 2. */
3222 return 1 + (value & 2);
3225 #if 0
3226 /* I don't think this condition is sufficient to allow INSN
3227 to be moved, so we no longer test it. */
3229 /* Return 1 if all insns in the basic block of INSN and following INSN
3230 that set REG are invariant according to TABLE. */
3232 static int
3233 all_sets_invariant_p (reg, insn, table)
3234 rtx reg, insn;
3235 short *table;
3237 register rtx p = insn;
3238 register int regno = REGNO (reg);
3240 while (1)
3242 register enum rtx_code code;
3243 p = NEXT_INSN (p);
3244 code = GET_CODE (p);
3245 if (code == CODE_LABEL || code == JUMP_INSN)
3246 return 1;
3247 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3248 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3249 && REGNO (SET_DEST (PATTERN (p))) == regno)
3251 if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3252 return 0;
3256 #endif /* 0 */
3258 /* Look at all uses (not sets) of registers in X. For each, if it is
3259 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3260 a different insn, set USAGE[REGNO] to const0_rtx. */
3262 static void
3263 find_single_use_in_loop (regs, insn, x)
3264 struct loop_regs *regs;
3265 rtx insn;
3266 rtx x;
3268 enum rtx_code code = GET_CODE (x);
3269 const char *fmt = GET_RTX_FORMAT (code);
3270 int i, j;
3272 if (code == REG)
3273 regs->array[REGNO (x)].single_usage
3274 = (regs->array[REGNO (x)].single_usage != 0
3275 && regs->array[REGNO (x)].single_usage != insn)
3276 ? const0_rtx : insn;
3278 else if (code == SET)
3280 /* Don't count SET_DEST if it is a REG; otherwise count things
3281 in SET_DEST because if a register is partially modified, it won't
3282 show up as a potential movable so we don't care how USAGE is set
3283 for it. */
3284 if (GET_CODE (SET_DEST (x)) != REG)
3285 find_single_use_in_loop (regs, insn, SET_DEST (x));
3286 find_single_use_in_loop (regs, insn, SET_SRC (x));
3288 else
3289 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3291 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3292 find_single_use_in_loop (regs, insn, XEXP (x, i));
3293 else if (fmt[i] == 'E')
3294 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3295 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3299 /* Count and record any set in X which is contained in INSN. Update
3300 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3301 in X. */
3303 static void
3304 count_one_set (regs, insn, x, last_set)
3305 struct loop_regs *regs;
3306 rtx insn, x;
3307 rtx *last_set;
3309 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3310 /* Don't move a reg that has an explicit clobber.
3311 It's not worth the pain to try to do it correctly. */
3312 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3314 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3316 rtx dest = SET_DEST (x);
3317 while (GET_CODE (dest) == SUBREG
3318 || GET_CODE (dest) == ZERO_EXTRACT
3319 || GET_CODE (dest) == SIGN_EXTRACT
3320 || GET_CODE (dest) == STRICT_LOW_PART)
3321 dest = XEXP (dest, 0);
3322 if (GET_CODE (dest) == REG)
3324 register int regno = REGNO (dest);
3325 /* If this is the first setting of this reg
3326 in current basic block, and it was set before,
3327 it must be set in two basic blocks, so it cannot
3328 be moved out of the loop. */
3329 if (regs->array[regno].set_in_loop > 0
3330 && last_set == 0)
3331 regs->array[regno].may_not_optimize = 1;
3332 /* If this is not first setting in current basic block,
3333 see if reg was used in between previous one and this.
3334 If so, neither one can be moved. */
3335 if (last_set[regno] != 0
3336 && reg_used_between_p (dest, last_set[regno], insn))
3337 regs->array[regno].may_not_optimize = 1;
3338 if (regs->array[regno].set_in_loop < 127)
3339 ++regs->array[regno].set_in_loop;
3340 last_set[regno] = insn;
3345 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3346 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3347 contained in insn INSN is used by any insn that precedes INSN in
3348 cyclic order starting from the loop entry point.
3350 We don't want to use INSN_LUID here because if we restrict INSN to those
3351 that have a valid INSN_LUID, it means we cannot move an invariant out
3352 from an inner loop past two loops. */
3354 static int
3355 loop_reg_used_before_p (loop, set, insn)
3356 const struct loop *loop;
3357 rtx set, insn;
3359 rtx reg = SET_DEST (set);
3360 rtx p;
3362 /* Scan forward checking for register usage. If we hit INSN, we
3363 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3364 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3366 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3367 return 1;
3369 if (p == loop->end)
3370 p = loop->start;
3373 return 0;
3376 /* A "basic induction variable" or biv is a pseudo reg that is set
3377 (within this loop) only by incrementing or decrementing it. */
3378 /* A "general induction variable" or giv is a pseudo reg whose
3379 value is a linear function of a biv. */
3381 /* Bivs are recognized by `basic_induction_var';
3382 Givs by `general_induction_var'. */
3384 /* Communication with routines called via `note_stores'. */
3386 static rtx note_insn;
3388 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3390 static rtx addr_placeholder;
3392 /* ??? Unfinished optimizations, and possible future optimizations,
3393 for the strength reduction code. */
3395 /* ??? The interaction of biv elimination, and recognition of 'constant'
3396 bivs, may cause problems. */
3398 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3399 performance problems.
3401 Perhaps don't eliminate things that can be combined with an addressing
3402 mode. Find all givs that have the same biv, mult_val, and add_val;
3403 then for each giv, check to see if its only use dies in a following
3404 memory address. If so, generate a new memory address and check to see
3405 if it is valid. If it is valid, then store the modified memory address,
3406 otherwise, mark the giv as not done so that it will get its own iv. */
3408 /* ??? Could try to optimize branches when it is known that a biv is always
3409 positive. */
3411 /* ??? When replace a biv in a compare insn, we should replace with closest
3412 giv so that an optimized branch can still be recognized by the combiner,
3413 e.g. the VAX acb insn. */
3415 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3416 was rerun in loop_optimize whenever a register was added or moved.
3417 Also, some of the optimizations could be a little less conservative. */
3419 /* Scan the loop body and call FNCALL for each insn. In the addition to the
3420 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
3421 callback.
3423 NOT_EVERY_ITERATION if current insn is not executed at least once for every
3424 loop iteration except for the last one.
3426 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
3427 loop iteration.
3429 void
3430 for_each_insn_in_loop (loop, fncall)
3431 struct loop *loop;
3432 loop_insn_callback fncall;
3434 /* This is 1 if current insn is not executed at least once for every loop
3435 iteration. */
3436 int not_every_iteration = 0;
3437 int maybe_multiple = 0;
3438 int past_loop_latch = 0;
3439 int loop_depth = 0;
3440 rtx p;
3442 /* If loop_scan_start points to the loop exit test, we have to be wary of
3443 subversive use of gotos inside expression statements. */
3444 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
3445 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
3447 /* Scan through loop to find all possible bivs. */
3449 for (p = next_insn_in_loop (loop, loop->scan_start);
3450 p != NULL_RTX;
3451 p = next_insn_in_loop (loop, p))
3453 p = fncall (loop, p, not_every_iteration, maybe_multiple);
3455 /* Past CODE_LABEL, we get to insns that may be executed multiple
3456 times. The only way we can be sure that they can't is if every
3457 jump insn between here and the end of the loop either
3458 returns, exits the loop, is a jump to a location that is still
3459 behind the label, or is a jump to the loop start. */
3461 if (GET_CODE (p) == CODE_LABEL)
3463 rtx insn = p;
3465 maybe_multiple = 0;
3467 while (1)
3469 insn = NEXT_INSN (insn);
3470 if (insn == loop->scan_start)
3471 break;
3472 if (insn == loop->end)
3474 if (loop->top != 0)
3475 insn = loop->top;
3476 else
3477 break;
3478 if (insn == loop->scan_start)
3479 break;
3482 if (GET_CODE (insn) == JUMP_INSN
3483 && GET_CODE (PATTERN (insn)) != RETURN
3484 && (!any_condjump_p (insn)
3485 || (JUMP_LABEL (insn) != 0
3486 && JUMP_LABEL (insn) != loop->scan_start
3487 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
3489 maybe_multiple = 1;
3490 break;
3495 /* Past a jump, we get to insns for which we can't count
3496 on whether they will be executed during each iteration. */
3497 /* This code appears twice in strength_reduce. There is also similar
3498 code in scan_loop. */
3499 if (GET_CODE (p) == JUMP_INSN
3500 /* If we enter the loop in the middle, and scan around to the
3501 beginning, don't set not_every_iteration for that.
3502 This can be any kind of jump, since we want to know if insns
3503 will be executed if the loop is executed. */
3504 && !(JUMP_LABEL (p) == loop->top
3505 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
3506 && any_uncondjump_p (p))
3507 || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
3509 rtx label = 0;
3511 /* If this is a jump outside the loop, then it also doesn't
3512 matter. Check to see if the target of this branch is on the
3513 loop->exits_labels list. */
3515 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
3516 if (XEXP (label, 0) == JUMP_LABEL (p))
3517 break;
3519 if (!label)
3520 not_every_iteration = 1;
3523 else if (GET_CODE (p) == NOTE)
3525 /* At the virtual top of a converted loop, insns are again known to
3526 be executed each iteration: logically, the loop begins here
3527 even though the exit code has been duplicated.
3529 Insns are also again known to be executed each iteration at
3530 the LOOP_CONT note. */
3531 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
3532 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
3533 && loop_depth == 0)
3534 not_every_iteration = 0;
3535 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3536 loop_depth++;
3537 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3538 loop_depth--;
3541 /* Note if we pass a loop latch. If we do, then we can not clear
3542 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
3543 a loop since a jump before the last CODE_LABEL may have started
3544 a new loop iteration.
3546 Note that LOOP_TOP is only set for rotated loops and we need
3547 this check for all loops, so compare against the CODE_LABEL
3548 which immediately follows LOOP_START. */
3549 if (GET_CODE (p) == JUMP_INSN
3550 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
3551 past_loop_latch = 1;
3553 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3554 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3555 or not an insn is known to be executed each iteration of the
3556 loop, whether or not any iterations are known to occur.
3558 Therefore, if we have just passed a label and have no more labels
3559 between here and the test insn of the loop, and we have not passed
3560 a jump to the top of the loop, then we know these insns will be
3561 executed each iteration. */
3563 if (not_every_iteration
3564 && !past_loop_latch
3565 && GET_CODE (p) == CODE_LABEL
3566 && no_labels_between_p (p, loop->end)
3567 && loop_insn_first_p (p, loop->cont))
3568 not_every_iteration = 0;
3572 static void
3573 loop_bivs_find (loop)
3574 struct loop *loop;
3576 struct loop_regs *regs = LOOP_REGS (loop);
3577 struct loop_ivs *ivs = LOOP_IVS (loop);
3578 /* Temporary list pointers for traversing ivs->list. */
3579 struct iv_class *bl, **backbl;
3581 ivs->list = 0;
3583 for_each_insn_in_loop (loop, check_insn_for_bivs);
3585 /* Scan ivs->list to remove all regs that proved not to be bivs.
3586 Make a sanity check against regs->n_times_set. */
3587 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
3589 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
3590 /* Above happens if register modified by subreg, etc. */
3591 /* Make sure it is not recognized as a basic induction var: */
3592 || regs->array[bl->regno].n_times_set != bl->biv_count
3593 /* If never incremented, it is invariant that we decided not to
3594 move. So leave it alone. */
3595 || ! bl->incremented)
3597 if (loop_dump_stream)
3598 fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
3599 bl->regno,
3600 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
3601 ? "not induction variable"
3602 : (! bl->incremented ? "never incremented"
3603 : "count error")));
3605 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
3606 *backbl = bl->next;
3608 else
3610 backbl = &bl->next;
3612 if (loop_dump_stream)
3613 fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
3619 /* Determine how BIVS are initialised by looking through pre-header
3620 extended basic block. */
3621 static void
3622 loop_bivs_init_find (loop)
3623 struct loop *loop;
3625 struct loop_ivs *ivs = LOOP_IVS (loop);
3626 /* Temporary list pointers for traversing ivs->list. */
3627 struct iv_class *bl;
3628 int call_seen;
3629 rtx p;
3631 /* Find initial value for each biv by searching backwards from loop_start,
3632 halting at first label. Also record any test condition. */
3634 call_seen = 0;
3635 for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3637 rtx test;
3639 note_insn = p;
3641 if (GET_CODE (p) == CALL_INSN)
3642 call_seen = 1;
3644 if (INSN_P (p))
3645 note_stores (PATTERN (p), record_initial, ivs);
3647 /* Record any test of a biv that branches around the loop if no store
3648 between it and the start of loop. We only care about tests with
3649 constants and registers and only certain of those. */
3650 if (GET_CODE (p) == JUMP_INSN
3651 && JUMP_LABEL (p) != 0
3652 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
3653 && (test = get_condition_for_loop (loop, p)) != 0
3654 && GET_CODE (XEXP (test, 0)) == REG
3655 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3656 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
3657 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
3658 && bl->init_insn == 0)
3660 /* If an NE test, we have an initial value! */
3661 if (GET_CODE (test) == NE)
3663 bl->init_insn = p;
3664 bl->init_set = gen_rtx_SET (VOIDmode,
3665 XEXP (test, 0), XEXP (test, 1));
3667 else
3668 bl->initial_test = test;
3674 /* Look at the each biv and see if we can say anything better about its
3675 initial value from any initializing insns set up above. (This is done
3676 in two passes to avoid missing SETs in a PARALLEL.) */
3677 static void
3678 loop_bivs_check (loop)
3679 struct loop *loop;
3681 struct loop_ivs *ivs = LOOP_IVS (loop);
3682 /* Temporary list pointers for traversing ivs->list. */
3683 struct iv_class *bl;
3684 struct iv_class **backbl;
3686 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
3688 rtx src;
3689 rtx note;
3691 if (! bl->init_insn)
3692 continue;
3694 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3695 is a constant, use the value of that. */
3696 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
3697 && CONSTANT_P (XEXP (note, 0)))
3698 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
3699 && CONSTANT_P (XEXP (note, 0))))
3700 src = XEXP (note, 0);
3701 else
3702 src = SET_SRC (bl->init_set);
3704 if (loop_dump_stream)
3705 fprintf (loop_dump_stream,
3706 "Biv %d: initialized at insn %d: initial value ",
3707 bl->regno, INSN_UID (bl->init_insn));
3709 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3710 || GET_MODE (src) == VOIDmode)
3711 && valid_initial_value_p (src, bl->init_insn,
3712 LOOP_INFO (loop)->pre_header_has_call,
3713 loop->start))
3715 bl->initial_value = src;
3717 if (loop_dump_stream)
3719 print_simple_rtl (loop_dump_stream, src);
3720 fputc ('\n', loop_dump_stream);
3723 /* If we can't make it a giv,
3724 let biv keep initial value of "itself". */
3725 else if (loop_dump_stream)
3726 fprintf (loop_dump_stream, "is complex\n");
3731 /* Search the loop for general induction variables. */
3733 static void
3734 loop_givs_find (loop)
3735 struct loop* loop;
3737 for_each_insn_in_loop (loop, check_insn_for_givs);
3741 /* For each giv for which we still don't know whether or not it is
3742 replaceable, check to see if it is replaceable because its final value
3743 can be calculated. */
3745 static void
3746 loop_givs_check (loop)
3747 struct loop *loop;
3749 struct loop_ivs *ivs = LOOP_IVS (loop);
3750 struct iv_class *bl;
3752 for (bl = ivs->list; bl; bl = bl->next)
3754 struct induction *v;
3756 for (v = bl->giv; v; v = v->next_iv)
3757 if (! v->replaceable && ! v->not_replaceable)
3758 check_final_value (loop, v);
3763 /* Return non-zero if it is possible to eliminate the biv BL provided
3764 all givs are reduced. This is possible if either the reg is not
3765 used outside the loop, or we can compute what its final value will
3766 be. */
3768 static int
3769 loop_biv_eliminable_p (loop, bl, threshold, insn_count)
3770 struct loop *loop;
3771 struct iv_class *bl;
3772 int threshold;
3773 int insn_count;
3775 /* For architectures with a decrement_and_branch_until_zero insn,
3776 don't do this if we put a REG_NONNEG note on the endtest for this
3777 biv. */
3779 #ifdef HAVE_decrement_and_branch_until_zero
3780 if (bl->nonneg)
3782 if (loop_dump_stream)
3783 fprintf (loop_dump_stream,
3784 "Cannot eliminate nonneg biv %d.\n", bl->regno);
3785 return 0;
3787 #endif
3789 /* Check that biv is used outside loop or if it has a final value.
3790 Compare against bl->init_insn rather than loop->start. We aren't
3791 concerned with any uses of the biv between init_insn and
3792 loop->start since these won't be affected by the value of the biv
3793 elsewhere in the function, so long as init_insn doesn't use the
3794 biv itself. */
3796 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
3797 && bl->init_insn
3798 && INSN_UID (bl->init_insn) < max_uid_for_loop
3799 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
3800 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3801 || (bl->final_value = final_biv_value (loop, bl)))
3802 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
3804 if (loop_dump_stream)
3806 fprintf (loop_dump_stream,
3807 "Cannot eliminate biv %d.\n",
3808 bl->regno);
3809 fprintf (loop_dump_stream,
3810 "First use: insn %d, last use: insn %d.\n",
3811 REGNO_FIRST_UID (bl->regno),
3812 REGNO_LAST_UID (bl->regno));
3814 return 0;
3818 /* Reduce each giv of BL that we have decided to reduce. */
3820 static void
3821 loop_givs_reduce (loop, bl)
3822 struct loop *loop;
3823 struct iv_class *bl;
3825 struct induction *v;
3827 for (v = bl->giv; v; v = v->next_iv)
3829 struct induction *tv;
3830 if (! v->ignore && v->same == 0)
3832 int auto_inc_opt = 0;
3834 /* If the code for derived givs immediately below has already
3835 allocated a new_reg, we must keep it. */
3836 if (! v->new_reg)
3837 v->new_reg = gen_reg_rtx (v->mode);
3839 #ifdef AUTO_INC_DEC
3840 /* If the target has auto-increment addressing modes, and
3841 this is an address giv, then try to put the increment
3842 immediately after its use, so that flow can create an
3843 auto-increment addressing mode. */
3844 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
3845 && bl->biv->always_executed && ! bl->biv->maybe_multiple
3846 /* We don't handle reversed biv's because bl->biv->insn
3847 does not have a valid INSN_LUID. */
3848 && ! bl->reversed
3849 && v->always_executed && ! v->maybe_multiple
3850 && INSN_UID (v->insn) < max_uid_for_loop)
3852 /* If other giv's have been combined with this one, then
3853 this will work only if all uses of the other giv's occur
3854 before this giv's insn. This is difficult to check.
3856 We simplify this by looking for the common case where
3857 there is one DEST_REG giv, and this giv's insn is the
3858 last use of the dest_reg of that DEST_REG giv. If the
3859 increment occurs after the address giv, then we can
3860 perform the optimization. (Otherwise, the increment
3861 would have to go before other_giv, and we would not be
3862 able to combine it with the address giv to get an
3863 auto-inc address.) */
3864 if (v->combined_with)
3866 struct induction *other_giv = 0;
3868 for (tv = bl->giv; tv; tv = tv->next_iv)
3869 if (tv->same == v)
3871 if (other_giv)
3872 break;
3873 else
3874 other_giv = tv;
3876 if (! tv && other_giv
3877 && REGNO (other_giv->dest_reg) < max_reg_before_loop
3878 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
3879 == INSN_UID (v->insn))
3880 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
3881 auto_inc_opt = 1;
3883 /* Check for case where increment is before the address
3884 giv. Do this test in "loop order". */
3885 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
3886 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
3887 || (INSN_LUID (bl->biv->insn)
3888 > INSN_LUID (loop->scan_start))))
3889 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
3890 && (INSN_LUID (loop->scan_start)
3891 < INSN_LUID (bl->biv->insn))))
3892 auto_inc_opt = -1;
3893 else
3894 auto_inc_opt = 1;
3896 #ifdef HAVE_cc0
3898 rtx prev;
3900 /* We can't put an insn immediately after one setting
3901 cc0, or immediately before one using cc0. */
3902 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
3903 || (auto_inc_opt == -1
3904 && (prev = prev_nonnote_insn (v->insn)) != 0
3905 && INSN_P (prev)
3906 && sets_cc0_p (PATTERN (prev))))
3907 auto_inc_opt = 0;
3909 #endif
3911 if (auto_inc_opt)
3912 v->auto_inc_opt = 1;
3914 #endif
3916 /* For each place where the biv is incremented, add an insn
3917 to increment the new, reduced reg for the giv. */
3918 for (tv = bl->biv; tv; tv = tv->next_iv)
3920 rtx insert_before;
3922 if (! auto_inc_opt)
3923 insert_before = tv->insn;
3924 else if (auto_inc_opt == 1)
3925 insert_before = NEXT_INSN (v->insn);
3926 else
3927 insert_before = v->insn;
3929 if (tv->mult_val == const1_rtx)
3930 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
3931 v->new_reg, v->new_reg,
3932 0, insert_before);
3933 else /* tv->mult_val == const0_rtx */
3934 /* A multiply is acceptable here
3935 since this is presumed to be seldom executed. */
3936 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
3937 v->add_val, v->new_reg,
3938 0, insert_before);
3941 /* Add code at loop start to initialize giv's reduced reg. */
3943 loop_iv_add_mult_hoist (loop,
3944 extend_value_for_giv (v, bl->initial_value),
3945 v->mult_val, v->add_val, v->new_reg);
3951 /* Check for givs whose first use is their definition and whose
3952 last use is the definition of another giv. If so, it is likely
3953 dead and should not be used to derive another giv nor to
3954 eliminate a biv. */
3956 static void
3957 loop_givs_dead_check (loop, bl)
3958 struct loop *loop ATTRIBUTE_UNUSED;
3959 struct iv_class *bl;
3961 struct induction *v;
3963 for (v = bl->giv; v; v = v->next_iv)
3965 if (v->ignore
3966 || (v->same && v->same->ignore))
3967 continue;
3969 if (v->giv_type == DEST_REG
3970 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
3972 struct induction *v1;
3974 for (v1 = bl->giv; v1; v1 = v1->next_iv)
3975 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
3976 v->maybe_dead = 1;
3982 static void
3983 loop_givs_rescan (loop, bl, reg_map)
3984 struct loop *loop;
3985 struct iv_class *bl;
3986 rtx *reg_map;
3988 struct induction *v;
3990 for (v = bl->giv; v; v = v->next_iv)
3992 if (v->same && v->same->ignore)
3993 v->ignore = 1;
3995 if (v->ignore)
3996 continue;
3998 /* Update expression if this was combined, in case other giv was
3999 replaced. */
4000 if (v->same)
4001 v->new_reg = replace_rtx (v->new_reg,
4002 v->same->dest_reg, v->same->new_reg);
4004 /* See if this register is known to be a pointer to something. If
4005 so, see if we can find the alignment. First see if there is a
4006 destination register that is a pointer. If so, this shares the
4007 alignment too. Next see if we can deduce anything from the
4008 computational information. If not, and this is a DEST_ADDR
4009 giv, at least we know that it's a pointer, though we don't know
4010 the alignment. */
4011 if (GET_CODE (v->new_reg) == REG
4012 && v->giv_type == DEST_REG
4013 && REG_POINTER (v->dest_reg))
4014 mark_reg_pointer (v->new_reg,
4015 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
4016 else if (GET_CODE (v->new_reg) == REG
4017 && REG_POINTER (v->src_reg))
4019 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
4021 if (align == 0
4022 || GET_CODE (v->add_val) != CONST_INT
4023 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
4024 align = 0;
4026 mark_reg_pointer (v->new_reg, align);
4028 else if (GET_CODE (v->new_reg) == REG
4029 && GET_CODE (v->add_val) == REG
4030 && REG_POINTER (v->add_val))
4032 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
4034 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
4035 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
4036 align = 0;
4038 mark_reg_pointer (v->new_reg, align);
4040 else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
4041 mark_reg_pointer (v->new_reg, 0);
4043 if (v->giv_type == DEST_ADDR)
4044 /* Store reduced reg as the address in the memref where we found
4045 this giv. */
4046 validate_change (v->insn, v->location, v->new_reg, 0);
4047 else if (v->replaceable)
4049 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4051 else
4053 /* Not replaceable; emit an insn to set the original giv reg from
4054 the reduced giv, same as above. */
4055 loop_insn_emit_after (loop, 0, v->insn,
4056 gen_move_insn (v->dest_reg, v->new_reg));
4059 /* When a loop is reversed, givs which depend on the reversed
4060 biv, and which are live outside the loop, must be set to their
4061 correct final value. This insn is only needed if the giv is
4062 not replaceable. The correct final value is the same as the
4063 value that the giv starts the reversed loop with. */
4064 if (bl->reversed && ! v->replaceable)
4065 loop_iv_add_mult_sink (loop,
4066 extend_value_for_giv (v, bl->initial_value),
4067 v->mult_val, v->add_val, v->dest_reg);
4068 else if (v->final_value)
4069 loop_insn_sink_or_swim (loop,
4070 gen_move_insn (v->dest_reg, v->final_value));
4072 if (loop_dump_stream)
4074 fprintf (loop_dump_stream, "giv at %d reduced to ",
4075 INSN_UID (v->insn));
4076 print_simple_rtl (loop_dump_stream, v->new_reg);
4077 fprintf (loop_dump_stream, "\n");
4083 static int
4084 loop_giv_reduce_benefit (loop, bl, v, test_reg)
4085 struct loop *loop ATTRIBUTE_UNUSED;
4086 struct iv_class *bl;
4087 struct induction *v;
4088 rtx test_reg;
4090 int add_cost;
4091 int benefit;
4093 benefit = v->benefit;
4094 PUT_MODE (test_reg, v->mode);
4095 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
4096 test_reg, test_reg);
4098 /* Reduce benefit if not replaceable, since we will insert a
4099 move-insn to replace the insn that calculates this giv. Don't do
4100 this unless the giv is a user variable, since it will often be
4101 marked non-replaceable because of the duplication of the exit
4102 code outside the loop. In such a case, the copies we insert are
4103 dead and will be deleted. So they don't have a cost. Similar
4104 situations exist. */
4105 /* ??? The new final_[bg]iv_value code does a much better job of
4106 finding replaceable giv's, and hence this code may no longer be
4107 necessary. */
4108 if (! v->replaceable && ! bl->eliminable
4109 && REG_USERVAR_P (v->dest_reg))
4110 benefit -= copy_cost;
4112 /* Decrease the benefit to count the add-insns that we will insert
4113 to increment the reduced reg for the giv. ??? This can
4114 overestimate the run-time cost of the additional insns, e.g. if
4115 there are multiple basic blocks that increment the biv, but only
4116 one of these blocks is executed during each iteration. There is
4117 no good way to detect cases like this with the current structure
4118 of the loop optimizer. This code is more accurate for
4119 determining code size than run-time benefits. */
4120 benefit -= add_cost * bl->biv_count;
4122 /* Decide whether to strength-reduce this giv or to leave the code
4123 unchanged (recompute it from the biv each time it is used). This
4124 decision can be made independently for each giv. */
4126 #ifdef AUTO_INC_DEC
4127 /* Attempt to guess whether autoincrement will handle some of the
4128 new add insns; if so, increase BENEFIT (undo the subtraction of
4129 add_cost that was done above). */
4130 if (v->giv_type == DEST_ADDR
4131 /* Increasing the benefit is risky, since this is only a guess.
4132 Avoid increasing register pressure in cases where there would
4133 be no other benefit from reducing this giv. */
4134 && benefit > 0
4135 && GET_CODE (v->mult_val) == CONST_INT)
4137 int size = GET_MODE_SIZE (GET_MODE (v->mem));
4139 if (HAVE_POST_INCREMENT
4140 && INTVAL (v->mult_val) == size)
4141 benefit += add_cost * bl->biv_count;
4142 else if (HAVE_PRE_INCREMENT
4143 && INTVAL (v->mult_val) == size)
4144 benefit += add_cost * bl->biv_count;
4145 else if (HAVE_POST_DECREMENT
4146 && -INTVAL (v->mult_val) == size)
4147 benefit += add_cost * bl->biv_count;
4148 else if (HAVE_PRE_DECREMENT
4149 && -INTVAL (v->mult_val) == size)
4150 benefit += add_cost * bl->biv_count;
4152 #endif
4154 return benefit;
4158 /* Free IV structures for LOOP. */
4160 static void
4161 loop_ivs_free (loop)
4162 struct loop *loop;
4164 struct loop_ivs *ivs = LOOP_IVS (loop);
4165 struct iv_class *iv = ivs->list;
4167 free (ivs->regs);
4169 while (iv)
4171 struct iv_class *next = iv->next;
4172 struct induction *induction;
4173 struct induction *next_induction;
4175 for (induction = iv->biv; induction; induction = next_induction)
4177 next_induction = induction->next_iv;
4178 free (induction);
4180 for (induction = iv->giv; induction; induction = next_induction)
4182 next_induction = induction->next_iv;
4183 free (induction);
4186 free (iv);
4187 iv = next;
4192 /* Perform strength reduction and induction variable elimination.
4194 Pseudo registers created during this function will be beyond the
4195 last valid index in several tables including
4196 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
4197 problem here, because the added registers cannot be givs outside of
4198 their loop, and hence will never be reconsidered. But scan_loop
4199 must check regnos to make sure they are in bounds. */
4201 static void
4202 strength_reduce (loop, insn_count, flags)
4203 struct loop *loop;
4204 int insn_count;
4205 int flags;
4207 struct loop_info *loop_info = LOOP_INFO (loop);
4208 struct loop_regs *regs = LOOP_REGS (loop);
4209 struct loop_ivs *ivs = LOOP_IVS (loop);
4210 rtx p;
4211 /* Temporary list pointer for traversing ivs->list. */
4212 struct iv_class *bl;
4213 /* Ratio of extra register life span we can justify
4214 for saving an instruction. More if loop doesn't call subroutines
4215 since in that case saving an insn makes more difference
4216 and more registers are available. */
4217 /* ??? could set this to last value of threshold in move_movables */
4218 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
4219 /* Map of pseudo-register replacements. */
4220 rtx *reg_map = NULL;
4221 int reg_map_size;
4222 int unrolled_insn_copies = 0;
4223 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
4225 addr_placeholder = gen_reg_rtx (Pmode);
4227 ivs->n_regs = max_reg_before_loop;
4228 ivs->regs = (struct iv *) xcalloc (ivs->n_regs, sizeof (struct iv));
4230 /* Find all BIVs in loop. */
4231 loop_bivs_find (loop);
4233 /* Exit if there are no bivs. */
4234 if (! ivs->list)
4236 /* Can still unroll the loop anyways, but indicate that there is no
4237 strength reduction info available. */
4238 if (flags & LOOP_UNROLL)
4239 unroll_loop (loop, insn_count, 0);
4241 loop_ivs_free (loop);
4242 return;
4245 /* Determine how BIVS are initialised by looking through pre-header
4246 extended basic block. */
4247 loop_bivs_init_find (loop);
4249 /* Look at the each biv and see if we can say anything better about its
4250 initial value from any initializing insns set up above. */
4251 loop_bivs_check (loop);
4253 /* Search the loop for general induction variables. */
4254 loop_givs_find (loop);
4256 /* Try to calculate and save the number of loop iterations. This is
4257 set to zero if the actual number can not be calculated. This must
4258 be called after all giv's have been identified, since otherwise it may
4259 fail if the iteration variable is a giv. */
4260 loop_iterations (loop);
4262 /* Now for each giv for which we still don't know whether or not it is
4263 replaceable, check to see if it is replaceable because its final value
4264 can be calculated. This must be done after loop_iterations is called,
4265 so that final_giv_value will work correctly. */
4266 loop_givs_check (loop);
4268 /* Try to prove that the loop counter variable (if any) is always
4269 nonnegative; if so, record that fact with a REG_NONNEG note
4270 so that "decrement and branch until zero" insn can be used. */
4271 check_dbra_loop (loop, insn_count);
4273 /* Create reg_map to hold substitutions for replaceable giv regs.
4274 Some givs might have been made from biv increments, so look at
4275 ivs->reg_iv_type for a suitable size. */
4276 reg_map_size = ivs->n_regs;
4277 reg_map = (rtx *) xcalloc (reg_map_size, sizeof (rtx));
4279 /* Examine each iv class for feasibility of strength reduction/induction
4280 variable elimination. */
4282 for (bl = ivs->list; bl; bl = bl->next)
4284 struct induction *v;
4285 int benefit;
4287 /* Test whether it will be possible to eliminate this biv
4288 provided all givs are reduced. */
4289 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
4291 /* Check each extension dependent giv in this class to see if its
4292 root biv is safe from wrapping in the interior mode. */
4293 check_ext_dependant_givs (bl, loop_info);
4295 /* Combine all giv's for this iv_class. */
4296 combine_givs (regs, bl);
4298 /* This will be true at the end, if all givs which depend on this
4299 biv have been strength reduced.
4300 We can't (currently) eliminate the biv unless this is so. */
4301 bl->all_reduced = 1;
4303 for (v = bl->giv; v; v = v->next_iv)
4305 struct induction *tv;
4307 if (v->ignore || v->same)
4308 continue;
4310 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
4312 /* If an insn is not to be strength reduced, then set its ignore
4313 flag, and clear bl->all_reduced. */
4315 /* A giv that depends on a reversed biv must be reduced if it is
4316 used after the loop exit, otherwise, it would have the wrong
4317 value after the loop exit. To make it simple, just reduce all
4318 of such giv's whether or not we know they are used after the loop
4319 exit. */
4321 if (! flag_reduce_all_givs
4322 && v->lifetime * threshold * benefit < insn_count
4323 && ! bl->reversed)
4325 if (loop_dump_stream)
4326 fprintf (loop_dump_stream,
4327 "giv of insn %d not worth while, %d vs %d.\n",
4328 INSN_UID (v->insn),
4329 v->lifetime * threshold * benefit, insn_count);
4330 v->ignore = 1;
4331 bl->all_reduced = 0;
4333 else
4335 /* Check that we can increment the reduced giv without a
4336 multiply insn. If not, reject it. */
4338 for (tv = bl->biv; tv; tv = tv->next_iv)
4339 if (tv->mult_val == const1_rtx
4340 && ! product_cheap_p (tv->add_val, v->mult_val))
4342 if (loop_dump_stream)
4343 fprintf (loop_dump_stream,
4344 "giv of insn %d: would need a multiply.\n",
4345 INSN_UID (v->insn));
4346 v->ignore = 1;
4347 bl->all_reduced = 0;
4348 break;
4353 /* Check for givs whose first use is their definition and whose
4354 last use is the definition of another giv. If so, it is likely
4355 dead and should not be used to derive another giv nor to
4356 eliminate a biv. */
4357 loop_givs_dead_check (loop, bl);
4359 /* Reduce each giv that we decided to reduce. */
4360 loop_givs_reduce (loop, bl);
4362 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4363 as not reduced.
4365 For each giv register that can be reduced now: if replaceable,
4366 substitute reduced reg wherever the old giv occurs;
4367 else add new move insn "giv_reg = reduced_reg". */
4368 loop_givs_rescan (loop, bl, reg_map);
4370 /* All the givs based on the biv bl have been reduced if they
4371 merit it. */
4373 /* For each giv not marked as maybe dead that has been combined with a
4374 second giv, clear any "maybe dead" mark on that second giv.
4375 v->new_reg will either be or refer to the register of the giv it
4376 combined with.
4378 Doing this clearing avoids problems in biv elimination where
4379 a giv's new_reg is a complex value that can't be put in the
4380 insn but the giv combined with (with a reg as new_reg) is
4381 marked maybe_dead. Since the register will be used in either
4382 case, we'd prefer it be used from the simpler giv. */
4384 for (v = bl->giv; v; v = v->next_iv)
4385 if (! v->maybe_dead && v->same)
4386 v->same->maybe_dead = 0;
4388 /* Try to eliminate the biv, if it is a candidate.
4389 This won't work if ! bl->all_reduced,
4390 since the givs we planned to use might not have been reduced.
4392 We have to be careful that we didn't initially think we could
4393 eliminate this biv because of a giv that we now think may be
4394 dead and shouldn't be used as a biv replacement.
4396 Also, there is the possibility that we may have a giv that looks
4397 like it can be used to eliminate a biv, but the resulting insn
4398 isn't valid. This can happen, for example, on the 88k, where a
4399 JUMP_INSN can compare a register only with zero. Attempts to
4400 replace it with a compare with a constant will fail.
4402 Note that in cases where this call fails, we may have replaced some
4403 of the occurrences of the biv with a giv, but no harm was done in
4404 doing so in the rare cases where it can occur. */
4406 if (bl->all_reduced == 1 && bl->eliminable
4407 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
4409 /* ?? If we created a new test to bypass the loop entirely,
4410 or otherwise drop straight in, based on this test, then
4411 we might want to rewrite it also. This way some later
4412 pass has more hope of removing the initialization of this
4413 biv entirely. */
4415 /* If final_value != 0, then the biv may be used after loop end
4416 and we must emit an insn to set it just in case.
4418 Reversed bivs already have an insn after the loop setting their
4419 value, so we don't need another one. We can't calculate the
4420 proper final value for such a biv here anyways. */
4421 if (bl->final_value && ! bl->reversed)
4422 loop_insn_sink_or_swim (loop, gen_move_insn
4423 (bl->biv->dest_reg, bl->final_value));
4425 if (loop_dump_stream)
4426 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4427 bl->regno);
4431 /* Go through all the instructions in the loop, making all the
4432 register substitutions scheduled in REG_MAP. */
4434 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
4435 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4436 || GET_CODE (p) == CALL_INSN)
4438 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
4439 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
4440 INSN_CODE (p) = -1;
4443 if (loop_info->n_iterations > 0)
4445 /* When we completely unroll a loop we will likely not need the increment
4446 of the loop BIV and we will not need the conditional branch at the
4447 end of the loop. */
4448 unrolled_insn_copies = insn_count - 2;
4450 #ifdef HAVE_cc0
4451 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
4452 need the comparison before the conditional branch at the end of the
4453 loop. */
4454 unrolled_insn_copies -= 1;
4455 #endif
4457 /* We'll need one copy for each loop iteration. */
4458 unrolled_insn_copies *= loop_info->n_iterations;
4460 /* A little slop to account for the ability to remove initialization
4461 code, better CSE, and other secondary benefits of completely
4462 unrolling some loops. */
4463 unrolled_insn_copies -= 1;
4465 /* Clamp the value. */
4466 if (unrolled_insn_copies < 0)
4467 unrolled_insn_copies = 0;
4470 /* Unroll loops from within strength reduction so that we can use the
4471 induction variable information that strength_reduce has already
4472 collected. Always unroll loops that would be as small or smaller
4473 unrolled than when rolled. */
4474 if ((flags & LOOP_UNROLL)
4475 || (loop_info->n_iterations > 0
4476 && unrolled_insn_copies <= insn_count))
4477 unroll_loop (loop, insn_count, 1);
4479 #ifdef HAVE_doloop_end
4480 if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
4481 doloop_optimize (loop);
4482 #endif /* HAVE_doloop_end */
4484 if (loop_dump_stream)
4485 fprintf (loop_dump_stream, "\n");
4487 loop_ivs_free (loop);
4488 if (reg_map)
4489 free (reg_map);
4492 /*Record all basic induction variables calculated in the insn. */
4493 static rtx
4494 check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
4495 struct loop *loop;
4496 rtx p;
4497 int not_every_iteration;
4498 int maybe_multiple;
4500 struct loop_ivs *ivs = LOOP_IVS (loop);
4501 rtx set;
4502 rtx dest_reg;
4503 rtx inc_val;
4504 rtx mult_val;
4505 rtx *location;
4507 if (GET_CODE (p) == INSN
4508 && (set = single_set (p))
4509 && GET_CODE (SET_DEST (set)) == REG)
4511 dest_reg = SET_DEST (set);
4512 if (REGNO (dest_reg) < max_reg_before_loop
4513 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
4514 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
4516 if (basic_induction_var (loop, SET_SRC (set),
4517 GET_MODE (SET_SRC (set)),
4518 dest_reg, p, &inc_val, &mult_val,
4519 &location))
4521 /* It is a possible basic induction variable.
4522 Create and initialize an induction structure for it. */
4524 struct induction *v
4525 = (struct induction *) xmalloc (sizeof (struct induction));
4527 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
4528 not_every_iteration, maybe_multiple);
4529 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
4531 else if (REGNO (dest_reg) < ivs->n_regs)
4532 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
4535 return p;
4538 /* Record all givs calculated in the insn.
4539 A register is a giv if: it is only set once, it is a function of a
4540 biv and a constant (or invariant), and it is not a biv. */
4541 static rtx
4542 check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
4543 struct loop *loop;
4544 rtx p;
4545 int not_every_iteration;
4546 int maybe_multiple;
4548 struct loop_regs *regs = LOOP_REGS (loop);
4550 rtx set;
4551 /* Look for a general induction variable in a register. */
4552 if (GET_CODE (p) == INSN
4553 && (set = single_set (p))
4554 && GET_CODE (SET_DEST (set)) == REG
4555 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
4557 rtx src_reg;
4558 rtx dest_reg;
4559 rtx add_val;
4560 rtx mult_val;
4561 rtx ext_val;
4562 int benefit;
4563 rtx regnote = 0;
4564 rtx last_consec_insn;
4566 dest_reg = SET_DEST (set);
4567 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
4568 return p;
4570 if (/* SET_SRC is a giv. */
4571 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
4572 &mult_val, &ext_val, 0, &benefit, VOIDmode)
4573 /* Equivalent expression is a giv. */
4574 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
4575 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
4576 &add_val, &mult_val, &ext_val, 0,
4577 &benefit, VOIDmode)))
4578 /* Don't try to handle any regs made by loop optimization.
4579 We have nothing on them in regno_first_uid, etc. */
4580 && REGNO (dest_reg) < max_reg_before_loop
4581 /* Don't recognize a BASIC_INDUCT_VAR here. */
4582 && dest_reg != src_reg
4583 /* This must be the only place where the register is set. */
4584 && (regs->array[REGNO (dest_reg)].n_times_set == 1
4585 /* or all sets must be consecutive and make a giv. */
4586 || (benefit = consec_sets_giv (loop, benefit, p,
4587 src_reg, dest_reg,
4588 &add_val, &mult_val, &ext_val,
4589 &last_consec_insn))))
4591 struct induction *v
4592 = (struct induction *) xmalloc (sizeof (struct induction));
4594 /* If this is a library call, increase benefit. */
4595 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
4596 benefit += libcall_benefit (p);
4598 /* Skip the consecutive insns, if there are any. */
4599 if (regs->array[REGNO (dest_reg)].n_times_set != 1)
4600 p = last_consec_insn;
4602 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
4603 ext_val, benefit, DEST_REG, not_every_iteration,
4604 maybe_multiple, NULL_PTR);
4609 #ifndef DONT_REDUCE_ADDR
4610 /* Look for givs which are memory addresses. */
4611 /* This resulted in worse code on a VAX 8600. I wonder if it
4612 still does. */
4613 if (GET_CODE (p) == INSN)
4614 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
4615 maybe_multiple);
4616 #endif
4618 /* Update the status of whether giv can derive other givs. This can
4619 change when we pass a label or an insn that updates a biv. */
4620 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4621 || GET_CODE (p) == CODE_LABEL)
4622 update_giv_derive (loop, p);
4623 return p;
4626 /* Return 1 if X is a valid source for an initial value (or as value being
4627 compared against in an initial test).
4629 X must be either a register or constant and must not be clobbered between
4630 the current insn and the start of the loop.
4632 INSN is the insn containing X. */
4634 static int
4635 valid_initial_value_p (x, insn, call_seen, loop_start)
4636 rtx x;
4637 rtx insn;
4638 int call_seen;
4639 rtx loop_start;
4641 if (CONSTANT_P (x))
4642 return 1;
4644 /* Only consider pseudos we know about initialized in insns whose luids
4645 we know. */
4646 if (GET_CODE (x) != REG
4647 || REGNO (x) >= max_reg_before_loop)
4648 return 0;
4650 /* Don't use call-clobbered registers across a call which clobbers it. On
4651 some machines, don't use any hard registers at all. */
4652 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4653 && (SMALL_REGISTER_CLASSES
4654 || (call_used_regs[REGNO (x)] && call_seen)))
4655 return 0;
4657 /* Don't use registers that have been clobbered before the start of the
4658 loop. */
4659 if (reg_set_between_p (x, insn, loop_start))
4660 return 0;
4662 return 1;
4665 /* Scan X for memory refs and check each memory address
4666 as a possible giv. INSN is the insn whose pattern X comes from.
4667 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4668 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
4669 more thanonce in each loop iteration. */
4671 static void
4672 find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
4673 const struct loop *loop;
4674 rtx x;
4675 rtx insn;
4676 int not_every_iteration, maybe_multiple;
4678 register int i, j;
4679 register enum rtx_code code;
4680 register const char *fmt;
4682 if (x == 0)
4683 return;
4685 code = GET_CODE (x);
4686 switch (code)
4688 case REG:
4689 case CONST_INT:
4690 case CONST:
4691 case CONST_DOUBLE:
4692 case SYMBOL_REF:
4693 case LABEL_REF:
4694 case PC:
4695 case CC0:
4696 case ADDR_VEC:
4697 case ADDR_DIFF_VEC:
4698 case USE:
4699 case CLOBBER:
4700 return;
4702 case MEM:
4704 rtx src_reg;
4705 rtx add_val;
4706 rtx mult_val;
4707 rtx ext_val;
4708 int benefit;
4710 /* This code used to disable creating GIVs with mult_val == 1 and
4711 add_val == 0. However, this leads to lost optimizations when
4712 it comes time to combine a set of related DEST_ADDR GIVs, since
4713 this one would not be seen. */
4715 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
4716 &mult_val, &ext_val, 1, &benefit,
4717 GET_MODE (x)))
4719 /* Found one; record it. */
4720 struct induction *v
4721 = (struct induction *) xmalloc (sizeof (struct induction));
4723 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
4724 add_val, ext_val, benefit, DEST_ADDR,
4725 not_every_iteration, maybe_multiple, &XEXP (x, 0));
4727 v->mem = x;
4730 return;
4732 default:
4733 break;
4736 /* Recursively scan the subexpressions for other mem refs. */
4738 fmt = GET_RTX_FORMAT (code);
4739 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4740 if (fmt[i] == 'e')
4741 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
4742 maybe_multiple);
4743 else if (fmt[i] == 'E')
4744 for (j = 0; j < XVECLEN (x, i); j++)
4745 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
4746 maybe_multiple);
4749 /* Fill in the data about one biv update.
4750 V is the `struct induction' in which we record the biv. (It is
4751 allocated by the caller, with alloca.)
4752 INSN is the insn that sets it.
4753 DEST_REG is the biv's reg.
4755 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4756 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4757 being set to INC_VAL.
4759 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4760 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4761 can be executed more than once per iteration. If MAYBE_MULTIPLE
4762 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4763 executed exactly once per iteration. */
4765 static void
4766 record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
4767 not_every_iteration, maybe_multiple)
4768 struct loop *loop;
4769 struct induction *v;
4770 rtx insn;
4771 rtx dest_reg;
4772 rtx inc_val;
4773 rtx mult_val;
4774 rtx *location;
4775 int not_every_iteration;
4776 int maybe_multiple;
4778 struct loop_ivs *ivs = LOOP_IVS (loop);
4779 struct iv_class *bl;
4781 v->insn = insn;
4782 v->src_reg = dest_reg;
4783 v->dest_reg = dest_reg;
4784 v->mult_val = mult_val;
4785 v->add_val = inc_val;
4786 v->ext_dependant = NULL_RTX;
4787 v->location = location;
4788 v->mode = GET_MODE (dest_reg);
4789 v->always_computable = ! not_every_iteration;
4790 v->always_executed = ! not_every_iteration;
4791 v->maybe_multiple = maybe_multiple;
4793 /* Add this to the reg's iv_class, creating a class
4794 if this is the first incrementation of the reg. */
4796 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
4797 if (bl == 0)
4799 /* Create and initialize new iv_class. */
4801 bl = (struct iv_class *) xmalloc (sizeof (struct iv_class));
4803 bl->regno = REGNO (dest_reg);
4804 bl->biv = 0;
4805 bl->giv = 0;
4806 bl->biv_count = 0;
4807 bl->giv_count = 0;
4809 /* Set initial value to the reg itself. */
4810 bl->initial_value = dest_reg;
4811 bl->final_value = 0;
4812 /* We haven't seen the initializing insn yet */
4813 bl->init_insn = 0;
4814 bl->init_set = 0;
4815 bl->initial_test = 0;
4816 bl->incremented = 0;
4817 bl->eliminable = 0;
4818 bl->nonneg = 0;
4819 bl->reversed = 0;
4820 bl->total_benefit = 0;
4822 /* Add this class to ivs->list. */
4823 bl->next = ivs->list;
4824 ivs->list = bl;
4826 /* Put it in the array of biv register classes. */
4827 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
4830 /* Update IV_CLASS entry for this biv. */
4831 v->next_iv = bl->biv;
4832 bl->biv = v;
4833 bl->biv_count++;
4834 if (mult_val == const1_rtx)
4835 bl->incremented = 1;
4837 if (loop_dump_stream)
4838 loop_biv_dump (v, loop_dump_stream, 0);
4841 /* Fill in the data about one giv.
4842 V is the `struct induction' in which we record the giv. (It is
4843 allocated by the caller, with alloca.)
4844 INSN is the insn that sets it.
4845 BENEFIT estimates the savings from deleting this insn.
4846 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4847 into a register or is used as a memory address.
4849 SRC_REG is the biv reg which the giv is computed from.
4850 DEST_REG is the giv's reg (if the giv is stored in a reg).
4851 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4852 LOCATION points to the place where this giv's value appears in INSN. */
4854 static void
4855 record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
4856 benefit, type, not_every_iteration, maybe_multiple, location)
4857 const struct loop *loop;
4858 struct induction *v;
4859 rtx insn;
4860 rtx src_reg;
4861 rtx dest_reg;
4862 rtx mult_val, add_val, ext_val;
4863 int benefit;
4864 enum g_types type;
4865 int not_every_iteration, maybe_multiple;
4866 rtx *location;
4868 struct loop_ivs *ivs = LOOP_IVS (loop);
4869 struct induction *b;
4870 struct iv_class *bl;
4871 rtx set = single_set (insn);
4872 rtx temp;
4874 /* Attempt to prove constantness of the values. */
4875 temp = simplify_rtx (add_val);
4876 if (temp)
4877 add_val = temp;
4879 v->insn = insn;
4880 v->src_reg = src_reg;
4881 v->giv_type = type;
4882 v->dest_reg = dest_reg;
4883 v->mult_val = mult_val;
4884 v->add_val = add_val;
4885 v->ext_dependant = ext_val;
4886 v->benefit = benefit;
4887 v->location = location;
4888 v->cant_derive = 0;
4889 v->combined_with = 0;
4890 v->maybe_multiple = maybe_multiple;
4891 v->maybe_dead = 0;
4892 v->derive_adjustment = 0;
4893 v->same = 0;
4894 v->ignore = 0;
4895 v->new_reg = 0;
4896 v->final_value = 0;
4897 v->same_insn = 0;
4898 v->auto_inc_opt = 0;
4899 v->unrolled = 0;
4900 v->shared = 0;
4902 /* The v->always_computable field is used in update_giv_derive, to
4903 determine whether a giv can be used to derive another giv. For a
4904 DEST_REG giv, INSN computes a new value for the giv, so its value
4905 isn't computable if INSN insn't executed every iteration.
4906 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4907 it does not compute a new value. Hence the value is always computable
4908 regardless of whether INSN is executed each iteration. */
4910 if (type == DEST_ADDR)
4911 v->always_computable = 1;
4912 else
4913 v->always_computable = ! not_every_iteration;
4915 v->always_executed = ! not_every_iteration;
4917 if (type == DEST_ADDR)
4919 v->mode = GET_MODE (*location);
4920 v->lifetime = 1;
4922 else /* type == DEST_REG */
4924 v->mode = GET_MODE (SET_DEST (set));
4926 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
4928 /* If the lifetime is zero, it means that this register is
4929 really a dead store. So mark this as a giv that can be
4930 ignored. This will not prevent the biv from being eliminated. */
4931 if (v->lifetime == 0)
4932 v->ignore = 1;
4934 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
4935 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
4938 /* Add the giv to the class of givs computed from one biv. */
4940 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
4941 if (bl)
4943 v->next_iv = bl->giv;
4944 bl->giv = v;
4945 /* Don't count DEST_ADDR. This is supposed to count the number of
4946 insns that calculate givs. */
4947 if (type == DEST_REG)
4948 bl->giv_count++;
4949 bl->total_benefit += benefit;
4951 else
4952 /* Fatal error, biv missing for this giv? */
4953 abort ();
4955 if (type == DEST_ADDR)
4956 v->replaceable = 1;
4957 else
4959 /* The giv can be replaced outright by the reduced register only if all
4960 of the following conditions are true:
4961 - the insn that sets the giv is always executed on any iteration
4962 on which the giv is used at all
4963 (there are two ways to deduce this:
4964 either the insn is executed on every iteration,
4965 or all uses follow that insn in the same basic block),
4966 - the giv is not used outside the loop
4967 - no assignments to the biv occur during the giv's lifetime. */
4969 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4970 /* Previous line always fails if INSN was moved by loop opt. */
4971 && REGNO_LAST_LUID (REGNO (dest_reg))
4972 < INSN_LUID (loop->end)
4973 && (! not_every_iteration
4974 || last_use_this_basic_block (dest_reg, insn)))
4976 /* Now check that there are no assignments to the biv within the
4977 giv's lifetime. This requires two separate checks. */
4979 /* Check each biv update, and fail if any are between the first
4980 and last use of the giv.
4982 If this loop contains an inner loop that was unrolled, then
4983 the insn modifying the biv may have been emitted by the loop
4984 unrolling code, and hence does not have a valid luid. Just
4985 mark the biv as not replaceable in this case. It is not very
4986 useful as a biv, because it is used in two different loops.
4987 It is very unlikely that we would be able to optimize the giv
4988 using this biv anyways. */
4990 v->replaceable = 1;
4991 for (b = bl->biv; b; b = b->next_iv)
4993 if (INSN_UID (b->insn) >= max_uid_for_loop
4994 || ((INSN_LUID (b->insn)
4995 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
4996 && (INSN_LUID (b->insn)
4997 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
4999 v->replaceable = 0;
5000 v->not_replaceable = 1;
5001 break;
5005 /* If there are any backwards branches that go from after the
5006 biv update to before it, then this giv is not replaceable. */
5007 if (v->replaceable)
5008 for (b = bl->biv; b; b = b->next_iv)
5009 if (back_branch_in_range_p (loop, b->insn))
5011 v->replaceable = 0;
5012 v->not_replaceable = 1;
5013 break;
5016 else
5018 /* May still be replaceable, we don't have enough info here to
5019 decide. */
5020 v->replaceable = 0;
5021 v->not_replaceable = 0;
5025 /* Record whether the add_val contains a const_int, for later use by
5026 combine_givs. */
5028 rtx tem = add_val;
5030 v->no_const_addval = 1;
5031 if (tem == const0_rtx)
5033 else if (CONSTANT_P (add_val))
5034 v->no_const_addval = 0;
5035 if (GET_CODE (tem) == PLUS)
5037 while (1)
5039 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5040 tem = XEXP (tem, 0);
5041 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5042 tem = XEXP (tem, 1);
5043 else
5044 break;
5046 if (CONSTANT_P (XEXP (tem, 1)))
5047 v->no_const_addval = 0;
5051 if (loop_dump_stream)
5052 loop_giv_dump (v, loop_dump_stream, 0);
5055 /* All this does is determine whether a giv can be made replaceable because
5056 its final value can be calculated. This code can not be part of record_giv
5057 above, because final_giv_value requires that the number of loop iterations
5058 be known, and that can not be accurately calculated until after all givs
5059 have been identified. */
5061 static void
5062 check_final_value (loop, v)
5063 const struct loop *loop;
5064 struct induction *v;
5066 struct loop_ivs *ivs = LOOP_IVS (loop);
5067 struct iv_class *bl;
5068 rtx final_value = 0;
5070 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
5072 /* DEST_ADDR givs will never reach here, because they are always marked
5073 replaceable above in record_giv. */
5075 /* The giv can be replaced outright by the reduced register only if all
5076 of the following conditions are true:
5077 - the insn that sets the giv is always executed on any iteration
5078 on which the giv is used at all
5079 (there are two ways to deduce this:
5080 either the insn is executed on every iteration,
5081 or all uses follow that insn in the same basic block),
5082 - its final value can be calculated (this condition is different
5083 than the one above in record_giv)
5084 - it's not used before the it's set
5085 - no assignments to the biv occur during the giv's lifetime. */
5087 #if 0
5088 /* This is only called now when replaceable is known to be false. */
5089 /* Clear replaceable, so that it won't confuse final_giv_value. */
5090 v->replaceable = 0;
5091 #endif
5093 if ((final_value = final_giv_value (loop, v))
5094 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5096 int biv_increment_seen = 0, before_giv_insn = 0;
5097 rtx p = v->insn;
5098 rtx last_giv_use;
5100 v->replaceable = 1;
5102 /* When trying to determine whether or not a biv increment occurs
5103 during the lifetime of the giv, we can ignore uses of the variable
5104 outside the loop because final_value is true. Hence we can not
5105 use regno_last_uid and regno_first_uid as above in record_giv. */
5107 /* Search the loop to determine whether any assignments to the
5108 biv occur during the giv's lifetime. Start with the insn
5109 that sets the giv, and search around the loop until we come
5110 back to that insn again.
5112 Also fail if there is a jump within the giv's lifetime that jumps
5113 to somewhere outside the lifetime but still within the loop. This
5114 catches spaghetti code where the execution order is not linear, and
5115 hence the above test fails. Here we assume that the giv lifetime
5116 does not extend from one iteration of the loop to the next, so as
5117 to make the test easier. Since the lifetime isn't known yet,
5118 this requires two loops. See also record_giv above. */
5120 last_giv_use = v->insn;
5122 while (1)
5124 p = NEXT_INSN (p);
5125 if (p == loop->end)
5127 before_giv_insn = 1;
5128 p = NEXT_INSN (loop->start);
5130 if (p == v->insn)
5131 break;
5133 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5134 || GET_CODE (p) == CALL_INSN)
5136 /* It is possible for the BIV increment to use the GIV if we
5137 have a cycle. Thus we must be sure to check each insn for
5138 both BIV and GIV uses, and we must check for BIV uses
5139 first. */
5141 if (! biv_increment_seen
5142 && reg_set_p (v->src_reg, PATTERN (p)))
5143 biv_increment_seen = 1;
5145 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5147 if (biv_increment_seen || before_giv_insn)
5149 v->replaceable = 0;
5150 v->not_replaceable = 1;
5151 break;
5153 last_giv_use = p;
5158 /* Now that the lifetime of the giv is known, check for branches
5159 from within the lifetime to outside the lifetime if it is still
5160 replaceable. */
5162 if (v->replaceable)
5164 p = v->insn;
5165 while (1)
5167 p = NEXT_INSN (p);
5168 if (p == loop->end)
5169 p = NEXT_INSN (loop->start);
5170 if (p == last_giv_use)
5171 break;
5173 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5174 && LABEL_NAME (JUMP_LABEL (p))
5175 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
5176 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
5177 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
5178 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
5180 v->replaceable = 0;
5181 v->not_replaceable = 1;
5183 if (loop_dump_stream)
5184 fprintf (loop_dump_stream,
5185 "Found branch outside giv lifetime.\n");
5187 break;
5192 /* If it is replaceable, then save the final value. */
5193 if (v->replaceable)
5194 v->final_value = final_value;
5197 if (loop_dump_stream && v->replaceable)
5198 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5199 INSN_UID (v->insn), REGNO (v->dest_reg));
5202 /* Update the status of whether a giv can derive other givs.
5204 We need to do something special if there is or may be an update to the biv
5205 between the time the giv is defined and the time it is used to derive
5206 another giv.
5208 In addition, a giv that is only conditionally set is not allowed to
5209 derive another giv once a label has been passed.
5211 The cases we look at are when a label or an update to a biv is passed. */
5213 static void
5214 update_giv_derive (loop, p)
5215 const struct loop *loop;
5216 rtx p;
5218 struct loop_ivs *ivs = LOOP_IVS (loop);
5219 struct iv_class *bl;
5220 struct induction *biv, *giv;
5221 rtx tem;
5222 int dummy;
5224 /* Search all IV classes, then all bivs, and finally all givs.
5226 There are three cases we are concerned with. First we have the situation
5227 of a giv that is only updated conditionally. In that case, it may not
5228 derive any givs after a label is passed.
5230 The second case is when a biv update occurs, or may occur, after the
5231 definition of a giv. For certain biv updates (see below) that are
5232 known to occur between the giv definition and use, we can adjust the
5233 giv definition. For others, or when the biv update is conditional,
5234 we must prevent the giv from deriving any other givs. There are two
5235 sub-cases within this case.
5237 If this is a label, we are concerned with any biv update that is done
5238 conditionally, since it may be done after the giv is defined followed by
5239 a branch here (actually, we need to pass both a jump and a label, but
5240 this extra tracking doesn't seem worth it).
5242 If this is a jump, we are concerned about any biv update that may be
5243 executed multiple times. We are actually only concerned about
5244 backward jumps, but it is probably not worth performing the test
5245 on the jump again here.
5247 If this is a biv update, we must adjust the giv status to show that a
5248 subsequent biv update was performed. If this adjustment cannot be done,
5249 the giv cannot derive further givs. */
5251 for (bl = ivs->list; bl; bl = bl->next)
5252 for (biv = bl->biv; biv; biv = biv->next_iv)
5253 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5254 || biv->insn == p)
5256 for (giv = bl->giv; giv; giv = giv->next_iv)
5258 /* If cant_derive is already true, there is no point in
5259 checking all of these conditions again. */
5260 if (giv->cant_derive)
5261 continue;
5263 /* If this giv is conditionally set and we have passed a label,
5264 it cannot derive anything. */
5265 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5266 giv->cant_derive = 1;
5268 /* Skip givs that have mult_val == 0, since
5269 they are really invariants. Also skip those that are
5270 replaceable, since we know their lifetime doesn't contain
5271 any biv update. */
5272 else if (giv->mult_val == const0_rtx || giv->replaceable)
5273 continue;
5275 /* The only way we can allow this giv to derive another
5276 is if this is a biv increment and we can form the product
5277 of biv->add_val and giv->mult_val. In this case, we will
5278 be able to compute a compensation. */
5279 else if (biv->insn == p)
5281 rtx ext_val_dummy;
5283 tem = 0;
5284 if (biv->mult_val == const1_rtx)
5285 tem = simplify_giv_expr (loop,
5286 gen_rtx_MULT (giv->mode,
5287 biv->add_val,
5288 giv->mult_val),
5289 &ext_val_dummy, &dummy);
5291 if (tem && giv->derive_adjustment)
5292 tem = simplify_giv_expr
5293 (loop,
5294 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
5295 &ext_val_dummy, &dummy);
5297 if (tem)
5298 giv->derive_adjustment = tem;
5299 else
5300 giv->cant_derive = 1;
5302 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5303 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5304 giv->cant_derive = 1;
5309 /* Check whether an insn is an increment legitimate for a basic induction var.
5310 X is the source of insn P, or a part of it.
5311 MODE is the mode in which X should be interpreted.
5313 DEST_REG is the putative biv, also the destination of the insn.
5314 We accept patterns of these forms:
5315 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5316 REG = INVARIANT + REG
5318 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5319 store the additive term into *INC_VAL, and store the place where
5320 we found the additive term into *LOCATION.
5322 If X is an assignment of an invariant into DEST_REG, we set
5323 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5325 We also want to detect a BIV when it corresponds to a variable
5326 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5327 of the variable may be a PLUS that adds a SUBREG of that variable to
5328 an invariant and then sign- or zero-extends the result of the PLUS
5329 into the variable.
5331 Most GIVs in such cases will be in the promoted mode, since that is the
5332 probably the natural computation mode (and almost certainly the mode
5333 used for addresses) on the machine. So we view the pseudo-reg containing
5334 the variable as the BIV, as if it were simply incremented.
5336 Note that treating the entire pseudo as a BIV will result in making
5337 simple increments to any GIVs based on it. However, if the variable
5338 overflows in its declared mode but not its promoted mode, the result will
5339 be incorrect. This is acceptable if the variable is signed, since
5340 overflows in such cases are undefined, but not if it is unsigned, since
5341 those overflows are defined. So we only check for SIGN_EXTEND and
5342 not ZERO_EXTEND.
5344 If we cannot find a biv, we return 0. */
5346 static int
5347 basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
5348 const struct loop *loop;
5349 register rtx x;
5350 enum machine_mode mode;
5351 rtx dest_reg;
5352 rtx p;
5353 rtx *inc_val;
5354 rtx *mult_val;
5355 rtx **location;
5357 register enum rtx_code code;
5358 rtx *argp, arg;
5359 rtx insn, set = 0;
5361 code = GET_CODE (x);
5362 *location = NULL;
5363 switch (code)
5365 case PLUS:
5366 if (rtx_equal_p (XEXP (x, 0), dest_reg)
5367 || (GET_CODE (XEXP (x, 0)) == SUBREG
5368 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5369 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5371 argp = &XEXP (x, 1);
5373 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
5374 || (GET_CODE (XEXP (x, 1)) == SUBREG
5375 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5376 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5378 argp = &XEXP (x, 0);
5380 else
5381 return 0;
5383 arg = *argp;
5384 if (loop_invariant_p (loop, arg) != 1)
5385 return 0;
5387 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5388 *mult_val = const1_rtx;
5389 *location = argp;
5390 return 1;
5392 case SUBREG:
5393 /* If this is a SUBREG for a promoted variable, check the inner
5394 value. */
5395 if (SUBREG_PROMOTED_VAR_P (x))
5396 return basic_induction_var (loop, SUBREG_REG (x),
5397 GET_MODE (SUBREG_REG (x)),
5398 dest_reg, p, inc_val, mult_val, location);
5399 return 0;
5401 case REG:
5402 /* If this register is assigned in a previous insn, look at its
5403 source, but don't go outside the loop or past a label. */
5405 /* If this sets a register to itself, we would repeat any previous
5406 biv increment if we applied this strategy blindly. */
5407 if (rtx_equal_p (dest_reg, x))
5408 return 0;
5410 insn = p;
5411 while (1)
5413 rtx dest;
5416 insn = PREV_INSN (insn);
5418 while (insn && GET_CODE (insn) == NOTE
5419 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5421 if (!insn)
5422 break;
5423 set = single_set (insn);
5424 if (set == 0)
5425 break;
5426 dest = SET_DEST (set);
5427 if (dest == x
5428 || (GET_CODE (dest) == SUBREG
5429 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
5430 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
5431 && SUBREG_REG (dest) == x))
5432 return basic_induction_var (loop, SET_SRC (set),
5433 (GET_MODE (SET_SRC (set)) == VOIDmode
5434 ? GET_MODE (x)
5435 : GET_MODE (SET_SRC (set))),
5436 dest_reg, insn,
5437 inc_val, mult_val, location);
5439 while (GET_CODE (dest) == SIGN_EXTRACT
5440 || GET_CODE (dest) == ZERO_EXTRACT
5441 || GET_CODE (dest) == SUBREG
5442 || GET_CODE (dest) == STRICT_LOW_PART)
5443 dest = XEXP (dest, 0);
5444 if (dest == x)
5445 break;
5447 /* Fall through. */
5449 /* Can accept constant setting of biv only when inside inner most loop.
5450 Otherwise, a biv of an inner loop may be incorrectly recognized
5451 as a biv of the outer loop,
5452 causing code to be moved INTO the inner loop. */
5453 case MEM:
5454 if (loop_invariant_p (loop, x) != 1)
5455 return 0;
5456 case CONST_INT:
5457 case SYMBOL_REF:
5458 case CONST:
5459 /* convert_modes aborts if we try to convert to or from CCmode, so just
5460 exclude that case. It is very unlikely that a condition code value
5461 would be a useful iterator anyways. */
5462 if (loop->level == 1
5463 && GET_MODE_CLASS (mode) != MODE_CC
5464 && GET_MODE_CLASS (GET_MODE (dest_reg)) != MODE_CC)
5466 /* Possible bug here? Perhaps we don't know the mode of X. */
5467 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5468 *mult_val = const0_rtx;
5469 return 1;
5471 else
5472 return 0;
5474 case SIGN_EXTEND:
5475 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5476 dest_reg, p, inc_val, mult_val, location);
5478 case ASHIFTRT:
5479 /* Similar, since this can be a sign extension. */
5480 for (insn = PREV_INSN (p);
5481 (insn && GET_CODE (insn) == NOTE
5482 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5483 insn = PREV_INSN (insn))
5486 if (insn)
5487 set = single_set (insn);
5489 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
5490 && set && SET_DEST (set) == XEXP (x, 0)
5491 && GET_CODE (XEXP (x, 1)) == CONST_INT
5492 && INTVAL (XEXP (x, 1)) >= 0
5493 && GET_CODE (SET_SRC (set)) == ASHIFT
5494 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5495 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
5496 GET_MODE (XEXP (x, 0)),
5497 dest_reg, insn, inc_val, mult_val,
5498 location);
5499 return 0;
5501 default:
5502 return 0;
5506 /* A general induction variable (giv) is any quantity that is a linear
5507 function of a basic induction variable,
5508 i.e. giv = biv * mult_val + add_val.
5509 The coefficients can be any loop invariant quantity.
5510 A giv need not be computed directly from the biv;
5511 it can be computed by way of other givs. */
5513 /* Determine whether X computes a giv.
5514 If it does, return a nonzero value
5515 which is the benefit from eliminating the computation of X;
5516 set *SRC_REG to the register of the biv that it is computed from;
5517 set *ADD_VAL and *MULT_VAL to the coefficients,
5518 such that the value of X is biv * mult + add; */
5520 static int
5521 general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
5522 is_addr, pbenefit, addr_mode)
5523 const struct loop *loop;
5524 rtx x;
5525 rtx *src_reg;
5526 rtx *add_val;
5527 rtx *mult_val;
5528 rtx *ext_val;
5529 int is_addr;
5530 int *pbenefit;
5531 enum machine_mode addr_mode;
5533 struct loop_ivs *ivs = LOOP_IVS (loop);
5534 rtx orig_x = x;
5536 /* If this is an invariant, forget it, it isn't a giv. */
5537 if (loop_invariant_p (loop, x) == 1)
5538 return 0;
5540 *pbenefit = 0;
5541 *ext_val = NULL_RTX;
5542 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
5543 if (x == 0)
5544 return 0;
5546 switch (GET_CODE (x))
5548 case USE:
5549 case CONST_INT:
5550 /* Since this is now an invariant and wasn't before, it must be a giv
5551 with MULT_VAL == 0. It doesn't matter which BIV we associate this
5552 with. */
5553 *src_reg = ivs->list->biv->dest_reg;
5554 *mult_val = const0_rtx;
5555 *add_val = x;
5556 break;
5558 case REG:
5559 /* This is equivalent to a BIV. */
5560 *src_reg = x;
5561 *mult_val = const1_rtx;
5562 *add_val = const0_rtx;
5563 break;
5565 case PLUS:
5566 /* Either (plus (biv) (invar)) or
5567 (plus (mult (biv) (invar_1)) (invar_2)). */
5568 if (GET_CODE (XEXP (x, 0)) == MULT)
5570 *src_reg = XEXP (XEXP (x, 0), 0);
5571 *mult_val = XEXP (XEXP (x, 0), 1);
5573 else
5575 *src_reg = XEXP (x, 0);
5576 *mult_val = const1_rtx;
5578 *add_val = XEXP (x, 1);
5579 break;
5581 case MULT:
5582 /* ADD_VAL is zero. */
5583 *src_reg = XEXP (x, 0);
5584 *mult_val = XEXP (x, 1);
5585 *add_val = const0_rtx;
5586 break;
5588 default:
5589 abort ();
5592 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5593 unless they are CONST_INT). */
5594 if (GET_CODE (*add_val) == USE)
5595 *add_val = XEXP (*add_val, 0);
5596 if (GET_CODE (*mult_val) == USE)
5597 *mult_val = XEXP (*mult_val, 0);
5599 if (is_addr)
5600 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
5601 else
5602 *pbenefit += rtx_cost (orig_x, SET);
5604 /* Always return true if this is a giv so it will be detected as such,
5605 even if the benefit is zero or negative. This allows elimination
5606 of bivs that might otherwise not be eliminated. */
5607 return 1;
5610 /* Given an expression, X, try to form it as a linear function of a biv.
5611 We will canonicalize it to be of the form
5612 (plus (mult (BIV) (invar_1))
5613 (invar_2))
5614 with possible degeneracies.
5616 The invariant expressions must each be of a form that can be used as a
5617 machine operand. We surround then with a USE rtx (a hack, but localized
5618 and certainly unambiguous!) if not a CONST_INT for simplicity in this
5619 routine; it is the caller's responsibility to strip them.
5621 If no such canonicalization is possible (i.e., two biv's are used or an
5622 expression that is neither invariant nor a biv or giv), this routine
5623 returns 0.
5625 For a non-zero return, the result will have a code of CONST_INT, USE,
5626 REG (for a BIV), PLUS, or MULT. No other codes will occur.
5628 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
5630 static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
5631 static rtx sge_plus_constant PARAMS ((rtx, rtx));
5633 static rtx
5634 simplify_giv_expr (loop, x, ext_val, benefit)
5635 const struct loop *loop;
5636 rtx x;
5637 rtx *ext_val;
5638 int *benefit;
5640 struct loop_ivs *ivs = LOOP_IVS (loop);
5641 struct loop_regs *regs = LOOP_REGS (loop);
5642 enum machine_mode mode = GET_MODE (x);
5643 rtx arg0, arg1;
5644 rtx tem;
5646 /* If this is not an integer mode, or if we cannot do arithmetic in this
5647 mode, this can't be a giv. */
5648 if (mode != VOIDmode
5649 && (GET_MODE_CLASS (mode) != MODE_INT
5650 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5651 return NULL_RTX;
5653 switch (GET_CODE (x))
5655 case PLUS:
5656 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
5657 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
5658 if (arg0 == 0 || arg1 == 0)
5659 return NULL_RTX;
5661 /* Put constant last, CONST_INT last if both constant. */
5662 if ((GET_CODE (arg0) == USE
5663 || GET_CODE (arg0) == CONST_INT)
5664 && ! ((GET_CODE (arg0) == USE
5665 && GET_CODE (arg1) == USE)
5666 || GET_CODE (arg1) == CONST_INT))
5667 tem = arg0, arg0 = arg1, arg1 = tem;
5669 /* Handle addition of zero, then addition of an invariant. */
5670 if (arg1 == const0_rtx)
5671 return arg0;
5672 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5673 switch (GET_CODE (arg0))
5675 case CONST_INT:
5676 case USE:
5677 /* Adding two invariants must result in an invariant, so enclose
5678 addition operation inside a USE and return it. */
5679 if (GET_CODE (arg0) == USE)
5680 arg0 = XEXP (arg0, 0);
5681 if (GET_CODE (arg1) == USE)
5682 arg1 = XEXP (arg1, 0);
5684 if (GET_CODE (arg0) == CONST_INT)
5685 tem = arg0, arg0 = arg1, arg1 = tem;
5686 if (GET_CODE (arg1) == CONST_INT)
5687 tem = sge_plus_constant (arg0, arg1);
5688 else
5689 tem = sge_plus (mode, arg0, arg1);
5691 if (GET_CODE (tem) != CONST_INT)
5692 tem = gen_rtx_USE (mode, tem);
5693 return tem;
5695 case REG:
5696 case MULT:
5697 /* biv + invar or mult + invar. Return sum. */
5698 return gen_rtx_PLUS (mode, arg0, arg1);
5700 case PLUS:
5701 /* (a + invar_1) + invar_2. Associate. */
5702 return
5703 simplify_giv_expr (loop,
5704 gen_rtx_PLUS (mode,
5705 XEXP (arg0, 0),
5706 gen_rtx_PLUS (mode,
5707 XEXP (arg0, 1),
5708 arg1)),
5709 ext_val, benefit);
5711 default:
5712 abort ();
5715 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5716 MULT to reduce cases. */
5717 if (GET_CODE (arg0) == REG)
5718 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
5719 if (GET_CODE (arg1) == REG)
5720 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
5722 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5723 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5724 Recurse to associate the second PLUS. */
5725 if (GET_CODE (arg1) == MULT)
5726 tem = arg0, arg0 = arg1, arg1 = tem;
5728 if (GET_CODE (arg1) == PLUS)
5729 return
5730 simplify_giv_expr (loop,
5731 gen_rtx_PLUS (mode,
5732 gen_rtx_PLUS (mode, arg0,
5733 XEXP (arg1, 0)),
5734 XEXP (arg1, 1)),
5735 ext_val, benefit);
5737 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5738 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5739 return NULL_RTX;
5741 if (!rtx_equal_p (arg0, arg1))
5742 return NULL_RTX;
5744 return simplify_giv_expr (loop,
5745 gen_rtx_MULT (mode,
5746 XEXP (arg0, 0),
5747 gen_rtx_PLUS (mode,
5748 XEXP (arg0, 1),
5749 XEXP (arg1, 1))),
5750 ext_val, benefit);
5752 case MINUS:
5753 /* Handle "a - b" as "a + b * (-1)". */
5754 return simplify_giv_expr (loop,
5755 gen_rtx_PLUS (mode,
5756 XEXP (x, 0),
5757 gen_rtx_MULT (mode,
5758 XEXP (x, 1),
5759 constm1_rtx)),
5760 ext_val, benefit);
5762 case MULT:
5763 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
5764 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
5765 if (arg0 == 0 || arg1 == 0)
5766 return NULL_RTX;
5768 /* Put constant last, CONST_INT last if both constant. */
5769 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5770 && GET_CODE (arg1) != CONST_INT)
5771 tem = arg0, arg0 = arg1, arg1 = tem;
5773 /* If second argument is not now constant, not giv. */
5774 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5775 return NULL_RTX;
5777 /* Handle multiply by 0 or 1. */
5778 if (arg1 == const0_rtx)
5779 return const0_rtx;
5781 else if (arg1 == const1_rtx)
5782 return arg0;
5784 switch (GET_CODE (arg0))
5786 case REG:
5787 /* biv * invar. Done. */
5788 return gen_rtx_MULT (mode, arg0, arg1);
5790 case CONST_INT:
5791 /* Product of two constants. */
5792 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5794 case USE:
5795 /* invar * invar is a giv, but attempt to simplify it somehow. */
5796 if (GET_CODE (arg1) != CONST_INT)
5797 return NULL_RTX;
5799 arg0 = XEXP (arg0, 0);
5800 if (GET_CODE (arg0) == MULT)
5802 /* (invar_0 * invar_1) * invar_2. Associate. */
5803 return simplify_giv_expr (loop,
5804 gen_rtx_MULT (mode,
5805 XEXP (arg0, 0),
5806 gen_rtx_MULT (mode,
5807 XEXP (arg0,
5809 arg1)),
5810 ext_val, benefit);
5812 /* Porpagate the MULT expressions to the intermost nodes. */
5813 else if (GET_CODE (arg0) == PLUS)
5815 /* (invar_0 + invar_1) * invar_2. Distribute. */
5816 return simplify_giv_expr (loop,
5817 gen_rtx_PLUS (mode,
5818 gen_rtx_MULT (mode,
5819 XEXP (arg0,
5821 arg1),
5822 gen_rtx_MULT (mode,
5823 XEXP (arg0,
5825 arg1)),
5826 ext_val, benefit);
5828 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
5830 case MULT:
5831 /* (a * invar_1) * invar_2. Associate. */
5832 return simplify_giv_expr (loop,
5833 gen_rtx_MULT (mode,
5834 XEXP (arg0, 0),
5835 gen_rtx_MULT (mode,
5836 XEXP (arg0, 1),
5837 arg1)),
5838 ext_val, benefit);
5840 case PLUS:
5841 /* (a + invar_1) * invar_2. Distribute. */
5842 return simplify_giv_expr (loop,
5843 gen_rtx_PLUS (mode,
5844 gen_rtx_MULT (mode,
5845 XEXP (arg0, 0),
5846 arg1),
5847 gen_rtx_MULT (mode,
5848 XEXP (arg0, 1),
5849 arg1)),
5850 ext_val, benefit);
5852 default:
5853 abort ();
5856 case ASHIFT:
5857 /* Shift by constant is multiply by power of two. */
5858 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5859 return 0;
5861 return
5862 simplify_giv_expr (loop,
5863 gen_rtx_MULT (mode,
5864 XEXP (x, 0),
5865 GEN_INT ((HOST_WIDE_INT) 1
5866 << INTVAL (XEXP (x, 1)))),
5867 ext_val, benefit);
5869 case NEG:
5870 /* "-a" is "a * (-1)" */
5871 return simplify_giv_expr (loop,
5872 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
5873 ext_val, benefit);
5875 case NOT:
5876 /* "~a" is "-a - 1". Silly, but easy. */
5877 return simplify_giv_expr (loop,
5878 gen_rtx_MINUS (mode,
5879 gen_rtx_NEG (mode, XEXP (x, 0)),
5880 const1_rtx),
5881 ext_val, benefit);
5883 case USE:
5884 /* Already in proper form for invariant. */
5885 return x;
5887 case SIGN_EXTEND:
5888 case ZERO_EXTEND:
5889 case TRUNCATE:
5890 /* Conditionally recognize extensions of simple IVs. After we've
5891 computed loop traversal counts and verified the range of the
5892 source IV, we'll reevaluate this as a GIV. */
5893 if (*ext_val == NULL_RTX)
5895 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
5896 if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
5898 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
5899 return arg0;
5902 goto do_default;
5904 case REG:
5905 /* If this is a new register, we can't deal with it. */
5906 if (REGNO (x) >= max_reg_before_loop)
5907 return 0;
5909 /* Check for biv or giv. */
5910 switch (REG_IV_TYPE (ivs, REGNO (x)))
5912 case BASIC_INDUCT:
5913 return x;
5914 case GENERAL_INDUCT:
5916 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
5918 /* Form expression from giv and add benefit. Ensure this giv
5919 can derive another and subtract any needed adjustment if so. */
5921 /* Increasing the benefit here is risky. The only case in which it
5922 is arguably correct is if this is the only use of V. In other
5923 cases, this will artificially inflate the benefit of the current
5924 giv, and lead to suboptimal code. Thus, it is disabled, since
5925 potentially not reducing an only marginally beneficial giv is
5926 less harmful than reducing many givs that are not really
5927 beneficial. */
5929 rtx single_use = regs->array[REGNO (x)].single_usage;
5930 if (single_use && single_use != const0_rtx)
5931 *benefit += v->benefit;
5934 if (v->cant_derive)
5935 return 0;
5937 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
5938 v->src_reg, v->mult_val),
5939 v->add_val);
5941 if (v->derive_adjustment)
5942 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
5943 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
5944 if (*ext_val)
5946 if (!v->ext_dependant)
5947 return arg0;
5949 else
5951 *ext_val = v->ext_dependant;
5952 return arg0;
5954 return 0;
5957 default:
5958 do_default:
5959 /* If it isn't an induction variable, and it is invariant, we
5960 may be able to simplify things further by looking through
5961 the bits we just moved outside the loop. */
5962 if (loop_invariant_p (loop, x) == 1)
5964 struct movable *m;
5965 struct loop_movables *movables = LOOP_MOVABLES (loop);
5967 for (m = movables->head; m; m = m->next)
5968 if (rtx_equal_p (x, m->set_dest))
5970 /* Ok, we found a match. Substitute and simplify. */
5972 /* If we match another movable, we must use that, as
5973 this one is going away. */
5974 if (m->match)
5975 return simplify_giv_expr (loop, m->match->set_dest,
5976 ext_val, benefit);
5978 /* If consec is non-zero, this is a member of a group of
5979 instructions that were moved together. We handle this
5980 case only to the point of seeking to the last insn and
5981 looking for a REG_EQUAL. Fail if we don't find one. */
5982 if (m->consec != 0)
5984 int i = m->consec;
5985 tem = m->insn;
5988 tem = NEXT_INSN (tem);
5990 while (--i > 0);
5992 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
5993 if (tem)
5994 tem = XEXP (tem, 0);
5996 else
5998 tem = single_set (m->insn);
5999 if (tem)
6000 tem = SET_SRC (tem);
6003 if (tem)
6005 /* What we are most interested in is pointer
6006 arithmetic on invariants -- only take
6007 patterns we may be able to do something with. */
6008 if (GET_CODE (tem) == PLUS
6009 || GET_CODE (tem) == MULT
6010 || GET_CODE (tem) == ASHIFT
6011 || GET_CODE (tem) == CONST_INT
6012 || GET_CODE (tem) == SYMBOL_REF)
6014 tem = simplify_giv_expr (loop, tem, ext_val,
6015 benefit);
6016 if (tem)
6017 return tem;
6019 else if (GET_CODE (tem) == CONST
6020 && GET_CODE (XEXP (tem, 0)) == PLUS
6021 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6022 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6024 tem = simplify_giv_expr (loop, XEXP (tem, 0),
6025 ext_val, benefit);
6026 if (tem)
6027 return tem;
6030 break;
6033 break;
6036 /* Fall through to general case. */
6037 default:
6038 /* If invariant, return as USE (unless CONST_INT).
6039 Otherwise, not giv. */
6040 if (GET_CODE (x) == USE)
6041 x = XEXP (x, 0);
6043 if (loop_invariant_p (loop, x) == 1)
6045 if (GET_CODE (x) == CONST_INT)
6046 return x;
6047 if (GET_CODE (x) == CONST
6048 && GET_CODE (XEXP (x, 0)) == PLUS
6049 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6050 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6051 x = XEXP (x, 0);
6052 return gen_rtx_USE (mode, x);
6054 else
6055 return 0;
6059 /* This routine folds invariants such that there is only ever one
6060 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6062 static rtx
6063 sge_plus_constant (x, c)
6064 rtx x, c;
6066 if (GET_CODE (x) == CONST_INT)
6067 return GEN_INT (INTVAL (x) + INTVAL (c));
6068 else if (GET_CODE (x) != PLUS)
6069 return gen_rtx_PLUS (GET_MODE (x), x, c);
6070 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6072 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6073 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6075 else if (GET_CODE (XEXP (x, 0)) == PLUS
6076 || GET_CODE (XEXP (x, 1)) != PLUS)
6078 return gen_rtx_PLUS (GET_MODE (x),
6079 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6081 else
6083 return gen_rtx_PLUS (GET_MODE (x),
6084 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6088 static rtx
6089 sge_plus (mode, x, y)
6090 enum machine_mode mode;
6091 rtx x, y;
6093 while (GET_CODE (y) == PLUS)
6095 rtx a = XEXP (y, 0);
6096 if (GET_CODE (a) == CONST_INT)
6097 x = sge_plus_constant (x, a);
6098 else
6099 x = gen_rtx_PLUS (mode, x, a);
6100 y = XEXP (y, 1);
6102 if (GET_CODE (y) == CONST_INT)
6103 x = sge_plus_constant (x, y);
6104 else
6105 x = gen_rtx_PLUS (mode, x, y);
6106 return x;
6109 /* Help detect a giv that is calculated by several consecutive insns;
6110 for example,
6111 giv = biv * M
6112 giv = giv + A
6113 The caller has already identified the first insn P as having a giv as dest;
6114 we check that all other insns that set the same register follow
6115 immediately after P, that they alter nothing else,
6116 and that the result of the last is still a giv.
6118 The value is 0 if the reg set in P is not really a giv.
6119 Otherwise, the value is the amount gained by eliminating
6120 all the consecutive insns that compute the value.
6122 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6123 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6125 The coefficients of the ultimate giv value are stored in
6126 *MULT_VAL and *ADD_VAL. */
6128 static int
6129 consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
6130 add_val, mult_val, ext_val, last_consec_insn)
6131 const struct loop *loop;
6132 int first_benefit;
6133 rtx p;
6134 rtx src_reg;
6135 rtx dest_reg;
6136 rtx *add_val;
6137 rtx *mult_val;
6138 rtx *ext_val;
6139 rtx *last_consec_insn;
6141 struct loop_ivs *ivs = LOOP_IVS (loop);
6142 struct loop_regs *regs = LOOP_REGS (loop);
6143 int count;
6144 enum rtx_code code;
6145 int benefit;
6146 rtx temp;
6147 rtx set;
6149 /* Indicate that this is a giv so that we can update the value produced in
6150 each insn of the multi-insn sequence.
6152 This induction structure will be used only by the call to
6153 general_induction_var below, so we can allocate it on our stack.
6154 If this is a giv, our caller will replace the induct var entry with
6155 a new induction structure. */
6156 struct induction *v;
6158 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
6159 return 0;
6161 v = (struct induction *) alloca (sizeof (struct induction));
6162 v->src_reg = src_reg;
6163 v->mult_val = *mult_val;
6164 v->add_val = *add_val;
6165 v->benefit = first_benefit;
6166 v->cant_derive = 0;
6167 v->derive_adjustment = 0;
6168 v->ext_dependant = NULL_RTX;
6170 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
6171 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
6173 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
6175 while (count > 0)
6177 p = NEXT_INSN (p);
6178 code = GET_CODE (p);
6180 /* If libcall, skip to end of call sequence. */
6181 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
6182 p = XEXP (temp, 0);
6184 if (code == INSN
6185 && (set = single_set (p))
6186 && GET_CODE (SET_DEST (set)) == REG
6187 && SET_DEST (set) == dest_reg
6188 && (general_induction_var (loop, SET_SRC (set), &src_reg,
6189 add_val, mult_val, ext_val, 0,
6190 &benefit, VOIDmode)
6191 /* Giv created by equivalent expression. */
6192 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
6193 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
6194 add_val, mult_val, ext_val, 0,
6195 &benefit, VOIDmode)))
6196 && src_reg == v->src_reg)
6198 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
6199 benefit += libcall_benefit (p);
6201 count--;
6202 v->mult_val = *mult_val;
6203 v->add_val = *add_val;
6204 v->benefit += benefit;
6206 else if (code != NOTE)
6208 /* Allow insns that set something other than this giv to a
6209 constant. Such insns are needed on machines which cannot
6210 include long constants and should not disqualify a giv. */
6211 if (code == INSN
6212 && (set = single_set (p))
6213 && SET_DEST (set) != dest_reg
6214 && CONSTANT_P (SET_SRC (set)))
6215 continue;
6217 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
6218 return 0;
6222 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
6223 *last_consec_insn = p;
6224 return v->benefit;
6227 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6228 represented by G1. If no such expression can be found, or it is clear that
6229 it cannot possibly be a valid address, 0 is returned.
6231 To perform the computation, we note that
6232 G1 = x * v + a and
6233 G2 = y * v + b
6234 where `v' is the biv.
6236 So G2 = (y/b) * G1 + (b - a*y/x).
6238 Note that MULT = y/x.
6240 Update: A and B are now allowed to be additive expressions such that
6241 B contains all variables in A. That is, computing B-A will not require
6242 subtracting variables. */
6244 static rtx
6245 express_from_1 (a, b, mult)
6246 rtx a, b, mult;
6248 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
6250 if (mult == const0_rtx)
6251 return b;
6253 /* If MULT is not 1, we cannot handle A with non-constants, since we
6254 would then be required to subtract multiples of the registers in A.
6255 This is theoretically possible, and may even apply to some Fortran
6256 constructs, but it is a lot of work and we do not attempt it here. */
6258 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
6259 return NULL_RTX;
6261 /* In general these structures are sorted top to bottom (down the PLUS
6262 chain), but not left to right across the PLUS. If B is a higher
6263 order giv than A, we can strip one level and recurse. If A is higher
6264 order, we'll eventually bail out, but won't know that until the end.
6265 If they are the same, we'll strip one level around this loop. */
6267 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
6269 rtx ra, rb, oa, ob, tmp;
6271 ra = XEXP (a, 0), oa = XEXP (a, 1);
6272 if (GET_CODE (ra) == PLUS)
6273 tmp = ra, ra = oa, oa = tmp;
6275 rb = XEXP (b, 0), ob = XEXP (b, 1);
6276 if (GET_CODE (rb) == PLUS)
6277 tmp = rb, rb = ob, ob = tmp;
6279 if (rtx_equal_p (ra, rb))
6280 /* We matched: remove one reg completely. */
6281 a = oa, b = ob;
6282 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
6283 /* An alternate match. */
6284 a = oa, b = rb;
6285 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
6286 /* An alternate match. */
6287 a = ra, b = ob;
6288 else
6290 /* Indicates an extra register in B. Strip one level from B and
6291 recurse, hoping B was the higher order expression. */
6292 ob = express_from_1 (a, ob, mult);
6293 if (ob == NULL_RTX)
6294 return NULL_RTX;
6295 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
6299 /* Here we are at the last level of A, go through the cases hoping to
6300 get rid of everything but a constant. */
6302 if (GET_CODE (a) == PLUS)
6304 rtx ra, oa;
6306 ra = XEXP (a, 0), oa = XEXP (a, 1);
6307 if (rtx_equal_p (oa, b))
6308 oa = ra;
6309 else if (!rtx_equal_p (ra, b))
6310 return NULL_RTX;
6312 if (GET_CODE (oa) != CONST_INT)
6313 return NULL_RTX;
6315 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
6317 else if (GET_CODE (a) == CONST_INT)
6319 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
6321 else if (CONSTANT_P (a))
6323 return simplify_gen_binary (MINUS, GET_MODE (b) != VOIDmode ? GET_MODE (b) : GET_MODE (a), const0_rtx, a);
6325 else if (GET_CODE (b) == PLUS)
6327 if (rtx_equal_p (a, XEXP (b, 0)))
6328 return XEXP (b, 1);
6329 else if (rtx_equal_p (a, XEXP (b, 1)))
6330 return XEXP (b, 0);
6331 else
6332 return NULL_RTX;
6334 else if (rtx_equal_p (a, b))
6335 return const0_rtx;
6337 return NULL_RTX;
6341 express_from (g1, g2)
6342 struct induction *g1, *g2;
6344 rtx mult, add;
6346 /* The value that G1 will be multiplied by must be a constant integer. Also,
6347 the only chance we have of getting a valid address is if b*c/a (see above
6348 for notation) is also an integer. */
6349 if (GET_CODE (g1->mult_val) == CONST_INT
6350 && GET_CODE (g2->mult_val) == CONST_INT)
6352 if (g1->mult_val == const0_rtx
6353 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
6354 return NULL_RTX;
6355 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
6357 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
6358 mult = const1_rtx;
6359 else
6361 /* ??? Find out if the one is a multiple of the other? */
6362 return NULL_RTX;
6365 add = express_from_1 (g1->add_val, g2->add_val, mult);
6366 if (add == NULL_RTX)
6368 /* Failed. If we've got a multiplication factor between G1 and G2,
6369 scale G1's addend and try again. */
6370 if (INTVAL (mult) > 1)
6372 rtx g1_add_val = g1->add_val;
6373 if (GET_CODE (g1_add_val) == MULT
6374 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
6376 HOST_WIDE_INT m;
6377 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
6378 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
6379 XEXP (g1_add_val, 0), GEN_INT (m));
6381 else
6383 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
6384 mult);
6387 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
6390 if (add == NULL_RTX)
6391 return NULL_RTX;
6393 /* Form simplified final result. */
6394 if (mult == const0_rtx)
6395 return add;
6396 else if (mult == const1_rtx)
6397 mult = g1->dest_reg;
6398 else
6399 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
6401 if (add == const0_rtx)
6402 return mult;
6403 else
6405 if (GET_CODE (add) == PLUS
6406 && CONSTANT_P (XEXP (add, 1)))
6408 rtx tem = XEXP (add, 1);
6409 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
6410 add = tem;
6413 return gen_rtx_PLUS (g2->mode, mult, add);
6417 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6418 represented by G1. This indicates that G2 should be combined with G1 and
6419 that G2 can use (either directly or via an address expression) a register
6420 used to represent G1. */
6422 static rtx
6423 combine_givs_p (g1, g2)
6424 struct induction *g1, *g2;
6426 rtx comb, ret;
6428 /* With the introduction of ext dependant givs, we must care for modes.
6429 G2 must not use a wider mode than G1. */
6430 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
6431 return NULL_RTX;
6433 ret = comb = express_from (g1, g2);
6434 if (comb == NULL_RTX)
6435 return NULL_RTX;
6436 if (g1->mode != g2->mode)
6437 ret = gen_lowpart (g2->mode, comb);
6439 /* If these givs are identical, they can be combined. We use the results
6440 of express_from because the addends are not in a canonical form, so
6441 rtx_equal_p is a weaker test. */
6442 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
6443 combination to be the other way round. */
6444 if (comb == g1->dest_reg
6445 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
6447 return ret;
6450 /* If G2 can be expressed as a function of G1 and that function is valid
6451 as an address and no more expensive than using a register for G2,
6452 the expression of G2 in terms of G1 can be used. */
6453 if (ret != NULL_RTX
6454 && g2->giv_type == DEST_ADDR
6455 && memory_address_p (GET_MODE (g2->mem), ret)
6456 /* ??? Looses, especially with -fforce-addr, where *g2->location
6457 will always be a register, and so anything more complicated
6458 gets discarded. */
6459 #if 0
6460 #ifdef ADDRESS_COST
6461 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
6462 #else
6463 && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
6464 #endif
6465 #endif
6468 return ret;
6471 return NULL_RTX;
6474 /* Check each extension dependant giv in this class to see if its
6475 root biv is safe from wrapping in the interior mode, which would
6476 make the giv illegal. */
6478 static void
6479 check_ext_dependant_givs (bl, loop_info)
6480 struct iv_class *bl;
6481 struct loop_info *loop_info;
6483 int ze_ok = 0, se_ok = 0, info_ok = 0;
6484 enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
6485 HOST_WIDE_INT start_val;
6486 unsigned HOST_WIDE_INT u_end_val = 0;
6487 unsigned HOST_WIDE_INT u_start_val = 0;
6488 rtx incr = pc_rtx;
6489 struct induction *v;
6491 /* Make sure the iteration data is available. We must have
6492 constants in order to be certain of no overflow. */
6493 /* ??? An unknown iteration count with an increment of +-1
6494 combined with friendly exit tests of against an invariant
6495 value is also ameanable to optimization. Not implemented. */
6496 if (loop_info->n_iterations > 0
6497 && bl->initial_value
6498 && GET_CODE (bl->initial_value) == CONST_INT
6499 && (incr = biv_total_increment (bl))
6500 && GET_CODE (incr) == CONST_INT
6501 /* Make sure the host can represent the arithmetic. */
6502 && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
6504 unsigned HOST_WIDE_INT abs_incr, total_incr;
6505 HOST_WIDE_INT s_end_val;
6506 int neg_incr;
6508 info_ok = 1;
6509 start_val = INTVAL (bl->initial_value);
6510 u_start_val = start_val;
6512 neg_incr = 0, abs_incr = INTVAL (incr);
6513 if (INTVAL (incr) < 0)
6514 neg_incr = 1, abs_incr = -abs_incr;
6515 total_incr = abs_incr * loop_info->n_iterations;
6517 /* Check for host arithmatic overflow. */
6518 if (total_incr / loop_info->n_iterations == abs_incr)
6520 unsigned HOST_WIDE_INT u_max;
6521 HOST_WIDE_INT s_max;
6523 u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
6524 s_end_val = u_end_val;
6525 u_max = GET_MODE_MASK (biv_mode);
6526 s_max = u_max >> 1;
6528 /* Check zero extension of biv ok. */
6529 if (start_val >= 0
6530 /* Check for host arithmatic overflow. */
6531 && (neg_incr
6532 ? u_end_val < u_start_val
6533 : u_end_val > u_start_val)
6534 /* Check for target arithmetic overflow. */
6535 && (neg_incr
6536 ? 1 /* taken care of with host overflow */
6537 : u_end_val <= u_max))
6539 ze_ok = 1;
6542 /* Check sign extension of biv ok. */
6543 /* ??? While it is true that overflow with signed and pointer
6544 arithmetic is undefined, I fear too many programmers don't
6545 keep this fact in mind -- myself included on occasion.
6546 So leave alone with the signed overflow optimizations. */
6547 if (start_val >= -s_max - 1
6548 /* Check for host arithmatic overflow. */
6549 && (neg_incr
6550 ? s_end_val < start_val
6551 : s_end_val > start_val)
6552 /* Check for target arithmetic overflow. */
6553 && (neg_incr
6554 ? s_end_val >= -s_max - 1
6555 : s_end_val <= s_max))
6557 se_ok = 1;
6562 /* Invalidate givs that fail the tests. */
6563 for (v = bl->giv; v; v = v->next_iv)
6564 if (v->ext_dependant)
6566 enum rtx_code code = GET_CODE (v->ext_dependant);
6567 int ok = 0;
6569 switch (code)
6571 case SIGN_EXTEND:
6572 ok = se_ok;
6573 break;
6574 case ZERO_EXTEND:
6575 ok = ze_ok;
6576 break;
6578 case TRUNCATE:
6579 /* We don't know whether this value is being used as either
6580 signed or unsigned, so to safely truncate we must satisfy
6581 both. The initial check here verifies the BIV itself;
6582 once that is successful we may check its range wrt the
6583 derived GIV. */
6584 if (se_ok && ze_ok)
6586 enum machine_mode outer_mode = GET_MODE (v->ext_dependant);
6587 unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
6589 /* We know from the above that both endpoints are nonnegative,
6590 and that there is no wrapping. Verify that both endpoints
6591 are within the (signed) range of the outer mode. */
6592 if (u_start_val <= max && u_end_val <= max)
6593 ok = 1;
6595 break;
6597 default:
6598 abort ();
6601 if (ok)
6603 if (loop_dump_stream)
6605 fprintf (loop_dump_stream,
6606 "Verified ext dependant giv at %d of reg %d\n",
6607 INSN_UID (v->insn), bl->regno);
6610 else
6612 if (loop_dump_stream)
6614 const char *why;
6616 if (info_ok)
6617 why = "biv iteration values overflowed";
6618 else
6620 if (incr == pc_rtx)
6621 incr = biv_total_increment (bl);
6622 if (incr == const1_rtx)
6623 why = "biv iteration info incomplete; incr by 1";
6624 else
6625 why = "biv iteration info incomplete";
6628 fprintf (loop_dump_stream,
6629 "Failed ext dependant giv at %d, %s\n",
6630 INSN_UID (v->insn), why);
6632 v->ignore = 1;
6637 /* Generate a version of VALUE in a mode appropriate for initializing V. */
6640 extend_value_for_giv (v, value)
6641 struct induction *v;
6642 rtx value;
6644 rtx ext_dep = v->ext_dependant;
6646 if (! ext_dep)
6647 return value;
6649 /* Recall that check_ext_dependant_givs verified that the known bounds
6650 of a biv did not overflow or wrap with respect to the extension for
6651 the giv. Therefore, constants need no additional adjustment. */
6652 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
6653 return value;
6655 /* Otherwise, we must adjust the value to compensate for the
6656 differing modes of the biv and the giv. */
6657 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
6660 struct combine_givs_stats
6662 int giv_number;
6663 int total_benefit;
6666 static int
6667 cmp_combine_givs_stats (xp, yp)
6668 const PTR xp;
6669 const PTR yp;
6671 const struct combine_givs_stats * const x =
6672 (const struct combine_givs_stats *) xp;
6673 const struct combine_givs_stats * const y =
6674 (const struct combine_givs_stats *) yp;
6675 int d;
6676 d = y->total_benefit - x->total_benefit;
6677 /* Stabilize the sort. */
6678 if (!d)
6679 d = x->giv_number - y->giv_number;
6680 return d;
6683 /* Check all pairs of givs for iv_class BL and see if any can be combined with
6684 any other. If so, point SAME to the giv combined with and set NEW_REG to
6685 be an expression (in terms of the other giv's DEST_REG) equivalent to the
6686 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
6688 static void
6689 combine_givs (regs, bl)
6690 struct loop_regs *regs;
6691 struct iv_class *bl;
6693 /* Additional benefit to add for being combined multiple times. */
6694 const int extra_benefit = 3;
6696 struct induction *g1, *g2, **giv_array;
6697 int i, j, k, giv_count;
6698 struct combine_givs_stats *stats;
6699 rtx *can_combine;
6701 /* Count givs, because bl->giv_count is incorrect here. */
6702 giv_count = 0;
6703 for (g1 = bl->giv; g1; g1 = g1->next_iv)
6704 if (!g1->ignore)
6705 giv_count++;
6707 giv_array
6708 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
6709 i = 0;
6710 for (g1 = bl->giv; g1; g1 = g1->next_iv)
6711 if (!g1->ignore)
6712 giv_array[i++] = g1;
6714 stats = (struct combine_givs_stats *) xcalloc (giv_count, sizeof (*stats));
6715 can_combine = (rtx *) xcalloc (giv_count, giv_count * sizeof (rtx));
6717 for (i = 0; i < giv_count; i++)
6719 int this_benefit;
6720 rtx single_use;
6722 g1 = giv_array[i];
6723 stats[i].giv_number = i;
6725 /* If a DEST_REG GIV is used only once, do not allow it to combine
6726 with anything, for in doing so we will gain nothing that cannot
6727 be had by simply letting the GIV with which we would have combined
6728 to be reduced on its own. The losage shows up in particular with
6729 DEST_ADDR targets on hosts with reg+reg addressing, though it can
6730 be seen elsewhere as well. */
6731 if (g1->giv_type == DEST_REG
6732 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
6733 && single_use != const0_rtx)
6734 continue;
6736 this_benefit = g1->benefit;
6737 /* Add an additional weight for zero addends. */
6738 if (g1->no_const_addval)
6739 this_benefit += 1;
6741 for (j = 0; j < giv_count; j++)
6743 rtx this_combine;
6745 g2 = giv_array[j];
6746 if (g1 != g2
6747 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
6749 can_combine[i * giv_count + j] = this_combine;
6750 this_benefit += g2->benefit + extra_benefit;
6753 stats[i].total_benefit = this_benefit;
6756 /* Iterate, combining until we can't. */
6757 restart:
6758 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
6760 if (loop_dump_stream)
6762 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
6763 for (k = 0; k < giv_count; k++)
6765 g1 = giv_array[stats[k].giv_number];
6766 if (!g1->combined_with && !g1->same)
6767 fprintf (loop_dump_stream, " {%d, %d}",
6768 INSN_UID (giv_array[stats[k].giv_number]->insn),
6769 stats[k].total_benefit);
6771 putc ('\n', loop_dump_stream);
6774 for (k = 0; k < giv_count; k++)
6776 int g1_add_benefit = 0;
6778 i = stats[k].giv_number;
6779 g1 = giv_array[i];
6781 /* If it has already been combined, skip. */
6782 if (g1->combined_with || g1->same)
6783 continue;
6785 for (j = 0; j < giv_count; j++)
6787 g2 = giv_array[j];
6788 if (g1 != g2 && can_combine[i * giv_count + j]
6789 /* If it has already been combined, skip. */
6790 && ! g2->same && ! g2->combined_with)
6792 int l;
6794 g2->new_reg = can_combine[i * giv_count + j];
6795 g2->same = g1;
6796 g1->combined_with++;
6797 g1->lifetime += g2->lifetime;
6799 g1_add_benefit += g2->benefit;
6801 /* ??? The new final_[bg]iv_value code does a much better job
6802 of finding replaceable giv's, and hence this code may no
6803 longer be necessary. */
6804 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
6805 g1_add_benefit -= copy_cost;
6807 /* To help optimize the next set of combinations, remove
6808 this giv from the benefits of other potential mates. */
6809 for (l = 0; l < giv_count; ++l)
6811 int m = stats[l].giv_number;
6812 if (can_combine[m * giv_count + j])
6813 stats[l].total_benefit -= g2->benefit + extra_benefit;
6816 if (loop_dump_stream)
6817 fprintf (loop_dump_stream,
6818 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
6819 INSN_UID (g2->insn), INSN_UID (g1->insn),
6820 g1->benefit, g1_add_benefit, g1->lifetime);
6824 /* To help optimize the next set of combinations, remove
6825 this giv from the benefits of other potential mates. */
6826 if (g1->combined_with)
6828 for (j = 0; j < giv_count; ++j)
6830 int m = stats[j].giv_number;
6831 if (can_combine[m * giv_count + i])
6832 stats[j].total_benefit -= g1->benefit + extra_benefit;
6835 g1->benefit += g1_add_benefit;
6837 /* We've finished with this giv, and everything it touched.
6838 Restart the combination so that proper weights for the
6839 rest of the givs are properly taken into account. */
6840 /* ??? Ideally we would compact the arrays at this point, so
6841 as to not cover old ground. But sanely compacting
6842 can_combine is tricky. */
6843 goto restart;
6847 /* Clean up. */
6848 free (stats);
6849 free (can_combine);
6852 /* Generate sequence for REG = B * M + A. */
6854 static rtx
6855 gen_add_mult (b, m, a, reg)
6856 rtx b; /* initial value of basic induction variable */
6857 rtx m; /* multiplicative constant */
6858 rtx a; /* additive constant */
6859 rtx reg; /* destination register */
6861 rtx seq;
6862 rtx result;
6864 start_sequence ();
6865 /* Use unsigned arithmetic. */
6866 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
6867 if (reg != result)
6868 emit_move_insn (reg, result);
6869 seq = gen_sequence ();
6870 end_sequence ();
6872 return seq;
6876 /* Update registers created in insn sequence SEQ. */
6878 static void
6879 loop_regs_update (loop, seq)
6880 const struct loop *loop ATTRIBUTE_UNUSED;
6881 rtx seq;
6883 /* Update register info for alias analysis. */
6885 if (GET_CODE (seq) == SEQUENCE)
6887 int i;
6888 for (i = 0; i < XVECLEN (seq, 0); ++i)
6890 rtx set = single_set (XVECEXP (seq, 0, i));
6891 if (set && GET_CODE (SET_DEST (set)) == REG)
6892 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
6895 else
6897 rtx set = single_set (seq);
6898 if (set && GET_CODE (SET_DEST (set)) == REG)
6899 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
6904 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. */
6906 void
6907 loop_iv_add_mult_emit_before (loop, b, m, a, reg, before_bb, before_insn)
6908 const struct loop *loop;
6909 rtx b; /* initial value of basic induction variable */
6910 rtx m; /* multiplicative constant */
6911 rtx a; /* additive constant */
6912 rtx reg; /* destination register */
6913 basic_block before_bb;
6914 rtx before_insn;
6916 rtx seq;
6918 if (! before_insn)
6920 loop_iv_add_mult_hoist (loop, b, m, a, reg);
6921 return;
6924 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
6925 seq = gen_add_mult (copy_rtx (b), m, copy_rtx (a), reg);
6927 /* Increase the lifetime of any invariants moved further in code. */
6928 update_reg_last_use (a, before_insn);
6929 update_reg_last_use (b, before_insn);
6930 update_reg_last_use (m, before_insn);
6932 loop_insn_emit_before (loop, before_bb, before_insn, seq);
6934 /* It is possible that the expansion created lots of new registers.
6935 Iterate over the sequence we just created and record them all. */
6936 loop_regs_update (loop, seq);
6940 /* Emit insns in loop pre-header to set REG = B * M + A. */
6942 void
6943 loop_iv_add_mult_sink (loop, b, m, a, reg)
6944 const struct loop *loop;
6945 rtx b; /* initial value of basic induction variable */
6946 rtx m; /* multiplicative constant */
6947 rtx a; /* additive constant */
6948 rtx reg; /* destination register */
6950 rtx seq;
6952 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
6953 seq = gen_add_mult (copy_rtx (b), m, copy_rtx (a), reg);
6955 /* Increase the lifetime of any invariants moved further in code.
6956 ???? Is this really necessary? */
6957 update_reg_last_use (a, loop->sink);
6958 update_reg_last_use (b, loop->sink);
6959 update_reg_last_use (m, loop->sink);
6961 loop_insn_sink (loop, seq);
6963 /* It is possible that the expansion created lots of new registers.
6964 Iterate over the sequence we just created and record them all. */
6965 loop_regs_update (loop, seq);
6969 /* Emit insns after loop to set REG = B * M + A. */
6971 void
6972 loop_iv_add_mult_hoist (loop, b, m, a, reg)
6973 const struct loop *loop;
6974 rtx b; /* initial value of basic induction variable */
6975 rtx m; /* multiplicative constant */
6976 rtx a; /* additive constant */
6977 rtx reg; /* destination register */
6979 rtx seq;
6981 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
6982 seq = gen_add_mult (copy_rtx (b), m, copy_rtx (a), reg);
6984 loop_insn_hoist (loop, seq);
6986 /* It is possible that the expansion created lots of new registers.
6987 Iterate over the sequence we just created and record them all. */
6988 loop_regs_update (loop, seq);
6993 /* Similar to gen_add_mult, but compute cost rather than generating
6994 sequence. */
6996 static int
6997 iv_add_mult_cost (b, m, a, reg)
6998 rtx b; /* initial value of basic induction variable */
6999 rtx m; /* multiplicative constant */
7000 rtx a; /* additive constant */
7001 rtx reg; /* destination register */
7003 int cost = 0;
7004 rtx last, result;
7006 start_sequence ();
7007 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7008 if (reg != result)
7009 emit_move_insn (reg, result);
7010 last = get_last_insn ();
7011 while (last)
7013 rtx t = single_set (last);
7014 if (t)
7015 cost += rtx_cost (SET_SRC (t), SET);
7016 last = PREV_INSN (last);
7018 end_sequence ();
7019 return cost;
7022 /* Test whether A * B can be computed without
7023 an actual multiply insn. Value is 1 if so. */
7025 static int
7026 product_cheap_p (a, b)
7027 rtx a;
7028 rtx b;
7030 int i;
7031 rtx tmp;
7032 int win = 1;
7034 /* If only one is constant, make it B. */
7035 if (GET_CODE (a) == CONST_INT)
7036 tmp = a, a = b, b = tmp;
7038 /* If first constant, both constant, so don't need multiply. */
7039 if (GET_CODE (a) == CONST_INT)
7040 return 1;
7042 /* If second not constant, neither is constant, so would need multiply. */
7043 if (GET_CODE (b) != CONST_INT)
7044 return 0;
7046 /* One operand is constant, so might not need multiply insn. Generate the
7047 code for the multiply and see if a call or multiply, or long sequence
7048 of insns is generated. */
7050 start_sequence ();
7051 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
7052 tmp = gen_sequence ();
7053 end_sequence ();
7055 if (GET_CODE (tmp) == SEQUENCE)
7057 if (XVEC (tmp, 0) == 0)
7058 win = 1;
7059 else if (XVECLEN (tmp, 0) > 3)
7060 win = 0;
7061 else
7062 for (i = 0; i < XVECLEN (tmp, 0); i++)
7064 rtx insn = XVECEXP (tmp, 0, i);
7066 if (GET_CODE (insn) != INSN
7067 || (GET_CODE (PATTERN (insn)) == SET
7068 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7069 || (GET_CODE (PATTERN (insn)) == PARALLEL
7070 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7071 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7073 win = 0;
7074 break;
7078 else if (GET_CODE (tmp) == SET
7079 && GET_CODE (SET_SRC (tmp)) == MULT)
7080 win = 0;
7081 else if (GET_CODE (tmp) == PARALLEL
7082 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7083 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7084 win = 0;
7086 return win;
7089 /* Check to see if loop can be terminated by a "decrement and branch until
7090 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7091 Also try reversing an increment loop to a decrement loop
7092 to see if the optimization can be performed.
7093 Value is nonzero if optimization was performed. */
7095 /* This is useful even if the architecture doesn't have such an insn,
7096 because it might change a loops which increments from 0 to n to a loop
7097 which decrements from n to 0. A loop that decrements to zero is usually
7098 faster than one that increments from zero. */
7100 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7101 such as approx_final_value, biv_total_increment, loop_iterations, and
7102 final_[bg]iv_value. */
7104 static int
7105 check_dbra_loop (loop, insn_count)
7106 struct loop *loop;
7107 int insn_count;
7109 struct loop_info *loop_info = LOOP_INFO (loop);
7110 struct loop_regs *regs = LOOP_REGS (loop);
7111 struct loop_ivs *ivs = LOOP_IVS (loop);
7112 struct iv_class *bl;
7113 rtx reg;
7114 rtx jump_label;
7115 rtx final_value;
7116 rtx start_value;
7117 rtx new_add_val;
7118 rtx comparison;
7119 rtx before_comparison;
7120 rtx p;
7121 rtx jump;
7122 rtx first_compare;
7123 int compare_and_branch;
7124 rtx loop_start = loop->start;
7125 rtx loop_end = loop->end;
7127 /* If last insn is a conditional branch, and the insn before tests a
7128 register value, try to optimize it. Otherwise, we can't do anything. */
7130 jump = PREV_INSN (loop_end);
7131 comparison = get_condition_for_loop (loop, jump);
7132 if (comparison == 0)
7133 return 0;
7134 if (!onlyjump_p (jump))
7135 return 0;
7137 /* Try to compute whether the compare/branch at the loop end is one or
7138 two instructions. */
7139 get_condition (jump, &first_compare);
7140 if (first_compare == jump)
7141 compare_and_branch = 1;
7142 else if (first_compare == prev_nonnote_insn (jump))
7143 compare_and_branch = 2;
7144 else
7145 return 0;
7148 /* If more than one condition is present to control the loop, then
7149 do not proceed, as this function does not know how to rewrite
7150 loop tests with more than one condition.
7152 Look backwards from the first insn in the last comparison
7153 sequence and see if we've got another comparison sequence. */
7155 rtx jump1;
7156 if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
7157 if (GET_CODE (jump1) == JUMP_INSN)
7158 return 0;
7161 /* Check all of the bivs to see if the compare uses one of them.
7162 Skip biv's set more than once because we can't guarantee that
7163 it will be zero on the last iteration. Also skip if the biv is
7164 used between its update and the test insn. */
7166 for (bl = ivs->list; bl; bl = bl->next)
7168 if (bl->biv_count == 1
7169 && ! bl->biv->maybe_multiple
7170 && bl->biv->dest_reg == XEXP (comparison, 0)
7171 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
7172 first_compare))
7173 break;
7176 if (! bl)
7177 return 0;
7179 /* Look for the case where the basic induction variable is always
7180 nonnegative, and equals zero on the last iteration.
7181 In this case, add a reg_note REG_NONNEG, which allows the
7182 m68k DBRA instruction to be used. */
7184 if (((GET_CODE (comparison) == GT
7185 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
7186 && INTVAL (XEXP (comparison, 1)) == -1)
7187 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
7188 && GET_CODE (bl->biv->add_val) == CONST_INT
7189 && INTVAL (bl->biv->add_val) < 0)
7191 /* Initial value must be greater than 0,
7192 init_val % -dec_value == 0 to ensure that it equals zero on
7193 the last iteration */
7195 if (GET_CODE (bl->initial_value) == CONST_INT
7196 && INTVAL (bl->initial_value) > 0
7197 && (INTVAL (bl->initial_value)
7198 % (-INTVAL (bl->biv->add_val))) == 0)
7200 /* register always nonnegative, add REG_NOTE to branch */
7201 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7202 REG_NOTES (jump)
7203 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7204 REG_NOTES (jump));
7205 bl->nonneg = 1;
7207 return 1;
7210 /* If the decrement is 1 and the value was tested as >= 0 before
7211 the loop, then we can safely optimize. */
7212 for (p = loop_start; p; p = PREV_INSN (p))
7214 if (GET_CODE (p) == CODE_LABEL)
7215 break;
7216 if (GET_CODE (p) != JUMP_INSN)
7217 continue;
7219 before_comparison = get_condition_for_loop (loop, p);
7220 if (before_comparison
7221 && XEXP (before_comparison, 0) == bl->biv->dest_reg
7222 && GET_CODE (before_comparison) == LT
7223 && XEXP (before_comparison, 1) == const0_rtx
7224 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
7225 && INTVAL (bl->biv->add_val) == -1)
7227 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7228 REG_NOTES (jump)
7229 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7230 REG_NOTES (jump));
7231 bl->nonneg = 1;
7233 return 1;
7237 else if (GET_CODE (bl->biv->add_val) == CONST_INT
7238 && INTVAL (bl->biv->add_val) > 0)
7240 /* Try to change inc to dec, so can apply above optimization. */
7241 /* Can do this if:
7242 all registers modified are induction variables or invariant,
7243 all memory references have non-overlapping addresses
7244 (obviously true if only one write)
7245 allow 2 insns for the compare/jump at the end of the loop. */
7246 /* Also, we must avoid any instructions which use both the reversed
7247 biv and another biv. Such instructions will fail if the loop is
7248 reversed. We meet this condition by requiring that either
7249 no_use_except_counting is true, or else that there is only
7250 one biv. */
7251 int num_nonfixed_reads = 0;
7252 /* 1 if the iteration var is used only to count iterations. */
7253 int no_use_except_counting = 0;
7254 /* 1 if the loop has no memory store, or it has a single memory store
7255 which is reversible. */
7256 int reversible_mem_store = 1;
7258 if (bl->giv_count == 0 && ! loop->exit_count)
7260 rtx bivreg = regno_reg_rtx[bl->regno];
7261 struct iv_class *blt;
7263 /* If there are no givs for this biv, and the only exit is the
7264 fall through at the end of the loop, then
7265 see if perhaps there are no uses except to count. */
7266 no_use_except_counting = 1;
7267 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7268 if (INSN_P (p))
7270 rtx set = single_set (p);
7272 if (set && GET_CODE (SET_DEST (set)) == REG
7273 && REGNO (SET_DEST (set)) == bl->regno)
7274 /* An insn that sets the biv is okay. */
7276 else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
7277 || p == prev_nonnote_insn (loop_end))
7278 && reg_mentioned_p (bivreg, PATTERN (p)))
7280 /* If either of these insns uses the biv and sets a pseudo
7281 that has more than one usage, then the biv has uses
7282 other than counting since it's used to derive a value
7283 that is used more than one time. */
7284 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
7285 regs);
7286 if (regs->multiple_uses)
7288 no_use_except_counting = 0;
7289 break;
7292 else if (reg_mentioned_p (bivreg, PATTERN (p)))
7294 no_use_except_counting = 0;
7295 break;
7299 /* A biv has uses besides counting if it is used to set another biv. */
7300 for (blt = ivs->list; blt; blt = blt->next)
7301 if (blt->init_set && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
7303 no_use_except_counting = 0;
7304 break;
7308 if (no_use_except_counting)
7309 /* No need to worry about MEMs. */
7311 else if (loop_info->num_mem_sets <= 1)
7313 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7314 if (INSN_P (p))
7315 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
7317 /* If the loop has a single store, and the destination address is
7318 invariant, then we can't reverse the loop, because this address
7319 might then have the wrong value at loop exit.
7320 This would work if the source was invariant also, however, in that
7321 case, the insn should have been moved out of the loop. */
7323 if (loop_info->num_mem_sets == 1)
7325 struct induction *v;
7327 reversible_mem_store
7328 = (! loop_info->unknown_address_altered
7329 && ! loop_info->unknown_constant_address_altered
7330 && ! loop_invariant_p (loop,
7331 XEXP (XEXP (loop_info->store_mems, 0),
7332 0)));
7334 /* If the store depends on a register that is set after the
7335 store, it depends on the initial value, and is thus not
7336 reversible. */
7337 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
7339 if (v->giv_type == DEST_REG
7340 && reg_mentioned_p (v->dest_reg,
7341 PATTERN (loop_info->first_loop_store_insn))
7342 && loop_insn_first_p (loop_info->first_loop_store_insn,
7343 v->insn))
7344 reversible_mem_store = 0;
7348 else
7349 return 0;
7351 /* This code only acts for innermost loops. Also it simplifies
7352 the memory address check by only reversing loops with
7353 zero or one memory access.
7354 Two memory accesses could involve parts of the same array,
7355 and that can't be reversed.
7356 If the biv is used only for counting, than we don't need to worry
7357 about all these things. */
7359 if ((num_nonfixed_reads <= 1
7360 && ! loop_info->has_nonconst_call
7361 && ! loop_info->has_volatile
7362 && reversible_mem_store
7363 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
7364 + LOOP_MOVABLES (loop)->num + compare_and_branch == insn_count)
7365 && (bl == ivs->list && bl->next == 0))
7366 || no_use_except_counting)
7368 rtx tem;
7370 /* Loop can be reversed. */
7371 if (loop_dump_stream)
7372 fprintf (loop_dump_stream, "Can reverse loop\n");
7374 /* Now check other conditions:
7376 The increment must be a constant, as must the initial value,
7377 and the comparison code must be LT.
7379 This test can probably be improved since +/- 1 in the constant
7380 can be obtained by changing LT to LE and vice versa; this is
7381 confusing. */
7383 if (comparison
7384 /* for constants, LE gets turned into LT */
7385 && (GET_CODE (comparison) == LT
7386 || (GET_CODE (comparison) == LE
7387 && no_use_except_counting)))
7389 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
7390 rtx initial_value, comparison_value;
7391 int nonneg = 0;
7392 enum rtx_code cmp_code;
7393 int comparison_const_width;
7394 unsigned HOST_WIDE_INT comparison_sign_mask;
7396 add_val = INTVAL (bl->biv->add_val);
7397 comparison_value = XEXP (comparison, 1);
7398 if (GET_MODE (comparison_value) == VOIDmode)
7399 comparison_const_width
7400 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
7401 else
7402 comparison_const_width
7403 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
7404 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
7405 comparison_const_width = HOST_BITS_PER_WIDE_INT;
7406 comparison_sign_mask
7407 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
7409 /* If the comparison value is not a loop invariant, then we
7410 can not reverse this loop.
7412 ??? If the insns which initialize the comparison value as
7413 a whole compute an invariant result, then we could move
7414 them out of the loop and proceed with loop reversal. */
7415 if (! loop_invariant_p (loop, comparison_value))
7416 return 0;
7418 if (GET_CODE (comparison_value) == CONST_INT)
7419 comparison_val = INTVAL (comparison_value);
7420 initial_value = bl->initial_value;
7422 /* Normalize the initial value if it is an integer and
7423 has no other use except as a counter. This will allow
7424 a few more loops to be reversed. */
7425 if (no_use_except_counting
7426 && GET_CODE (comparison_value) == CONST_INT
7427 && GET_CODE (initial_value) == CONST_INT)
7429 comparison_val = comparison_val - INTVAL (bl->initial_value);
7430 /* The code below requires comparison_val to be a multiple
7431 of add_val in order to do the loop reversal, so
7432 round up comparison_val to a multiple of add_val.
7433 Since comparison_value is constant, we know that the
7434 current comparison code is LT. */
7435 comparison_val = comparison_val + add_val - 1;
7436 comparison_val
7437 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
7438 /* We postpone overflow checks for COMPARISON_VAL here;
7439 even if there is an overflow, we might still be able to
7440 reverse the loop, if converting the loop exit test to
7441 NE is possible. */
7442 initial_value = const0_rtx;
7445 /* First check if we can do a vanilla loop reversal. */
7446 if (initial_value == const0_rtx
7447 /* If we have a decrement_and_branch_on_count,
7448 prefer the NE test, since this will allow that
7449 instruction to be generated. Note that we must
7450 use a vanilla loop reversal if the biv is used to
7451 calculate a giv or has a non-counting use. */
7452 #if ! defined (HAVE_decrement_and_branch_until_zero) \
7453 && defined (HAVE_decrement_and_branch_on_count)
7454 && (! (add_val == 1 && loop->vtop
7455 && (bl->biv_count == 0
7456 || no_use_except_counting)))
7457 #endif
7458 && GET_CODE (comparison_value) == CONST_INT
7459 /* Now do postponed overflow checks on COMPARISON_VAL. */
7460 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
7461 & comparison_sign_mask))
7463 /* Register will always be nonnegative, with value
7464 0 on last iteration */
7465 add_adjust = add_val;
7466 nonneg = 1;
7467 cmp_code = GE;
7469 else if (add_val == 1 && loop->vtop
7470 && (bl->biv_count == 0
7471 || no_use_except_counting))
7473 add_adjust = 0;
7474 cmp_code = NE;
7476 else
7477 return 0;
7479 if (GET_CODE (comparison) == LE)
7480 add_adjust -= add_val;
7482 /* If the initial value is not zero, or if the comparison
7483 value is not an exact multiple of the increment, then we
7484 can not reverse this loop. */
7485 if (initial_value == const0_rtx
7486 && GET_CODE (comparison_value) == CONST_INT)
7488 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
7489 return 0;
7491 else
7493 if (! no_use_except_counting || add_val != 1)
7494 return 0;
7497 final_value = comparison_value;
7499 /* Reset these in case we normalized the initial value
7500 and comparison value above. */
7501 if (GET_CODE (comparison_value) == CONST_INT
7502 && GET_CODE (initial_value) == CONST_INT)
7504 comparison_value = GEN_INT (comparison_val);
7505 final_value
7506 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
7508 bl->initial_value = initial_value;
7510 /* Save some info needed to produce the new insns. */
7511 reg = bl->biv->dest_reg;
7512 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
7513 if (jump_label == pc_rtx)
7514 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
7515 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
7517 /* Set start_value; if this is not a CONST_INT, we need
7518 to generate a SUB.
7519 Initialize biv to start_value before loop start.
7520 The old initializing insn will be deleted as a
7521 dead store by flow.c. */
7522 if (initial_value == const0_rtx
7523 && GET_CODE (comparison_value) == CONST_INT)
7525 start_value = GEN_INT (comparison_val - add_adjust);
7526 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
7528 else if (GET_CODE (initial_value) == CONST_INT)
7530 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
7531 enum machine_mode mode = GET_MODE (reg);
7532 enum insn_code icode
7533 = add_optab->handlers[(int) mode].insn_code;
7535 if (! (*insn_data[icode].operand[0].predicate) (reg, mode)
7536 || ! ((*insn_data[icode].operand[1].predicate)
7537 (comparison_value, mode))
7538 || ! ((*insn_data[icode].operand[2].predicate)
7539 (offset, mode)))
7540 return 0;
7541 start_value
7542 = gen_rtx_PLUS (mode, comparison_value, offset);
7543 loop_insn_hoist (loop, (GEN_FCN (icode)
7544 (reg, comparison_value, offset)));
7545 if (GET_CODE (comparison) == LE)
7546 final_value = gen_rtx_PLUS (mode, comparison_value,
7547 GEN_INT (add_val));
7549 else if (! add_adjust)
7551 enum machine_mode mode = GET_MODE (reg);
7552 enum insn_code icode
7553 = sub_optab->handlers[(int) mode].insn_code;
7554 if (! (*insn_data[icode].operand[0].predicate) (reg, mode)
7555 || ! ((*insn_data[icode].operand[1].predicate)
7556 (comparison_value, mode))
7557 || ! ((*insn_data[icode].operand[2].predicate)
7558 (initial_value, mode)))
7559 return 0;
7560 start_value
7561 = gen_rtx_MINUS (mode, comparison_value, initial_value);
7562 loop_insn_hoist (loop, (GEN_FCN (icode)
7563 (reg, comparison_value,
7564 initial_value)));
7566 else
7567 /* We could handle the other cases too, but it'll be
7568 better to have a testcase first. */
7569 return 0;
7571 /* We may not have a single insn which can increment a reg, so
7572 create a sequence to hold all the insns from expand_inc. */
7573 start_sequence ();
7574 expand_inc (reg, new_add_val);
7575 tem = gen_sequence ();
7576 end_sequence ();
7578 p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
7579 delete_insn (bl->biv->insn);
7581 /* Update biv info to reflect its new status. */
7582 bl->biv->insn = p;
7583 bl->initial_value = start_value;
7584 bl->biv->add_val = new_add_val;
7586 /* Update loop info. */
7587 loop_info->initial_value = reg;
7588 loop_info->initial_equiv_value = reg;
7589 loop_info->final_value = const0_rtx;
7590 loop_info->final_equiv_value = const0_rtx;
7591 loop_info->comparison_value = const0_rtx;
7592 loop_info->comparison_code = cmp_code;
7593 loop_info->increment = new_add_val;
7595 /* Inc LABEL_NUSES so that delete_insn will
7596 not delete the label. */
7597 LABEL_NUSES (XEXP (jump_label, 0))++;
7599 /* Emit an insn after the end of the loop to set the biv's
7600 proper exit value if it is used anywhere outside the loop. */
7601 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
7602 || ! bl->init_insn
7603 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
7604 loop_insn_sink (loop, gen_move_insn (reg, final_value));
7606 /* Delete compare/branch at end of loop. */
7607 delete_insn (PREV_INSN (loop_end));
7608 if (compare_and_branch == 2)
7609 delete_insn (first_compare);
7611 /* Add new compare/branch insn at end of loop. */
7612 start_sequence ();
7613 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
7614 GET_MODE (reg), 0, 0,
7615 XEXP (jump_label, 0));
7616 tem = gen_sequence ();
7617 end_sequence ();
7618 emit_jump_insn_before (tem, loop_end);
7620 for (tem = PREV_INSN (loop_end);
7621 tem && GET_CODE (tem) != JUMP_INSN;
7622 tem = PREV_INSN (tem))
7625 if (tem)
7626 JUMP_LABEL (tem) = XEXP (jump_label, 0);
7628 if (nonneg)
7630 if (tem)
7632 /* Increment of LABEL_NUSES done above. */
7633 /* Register is now always nonnegative,
7634 so add REG_NONNEG note to the branch. */
7635 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
7636 REG_NOTES (tem));
7638 bl->nonneg = 1;
7641 /* No insn may reference both the reversed and another biv or it
7642 will fail (see comment near the top of the loop reversal
7643 code).
7644 Earlier on, we have verified that the biv has no use except
7645 counting, or it is the only biv in this function.
7646 However, the code that computes no_use_except_counting does
7647 not verify reg notes. It's possible to have an insn that
7648 references another biv, and has a REG_EQUAL note with an
7649 expression based on the reversed biv. To avoid this case,
7650 remove all REG_EQUAL notes based on the reversed biv
7651 here. */
7652 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7653 if (INSN_P (p))
7655 rtx *pnote;
7656 rtx set = single_set (p);
7657 /* If this is a set of a GIV based on the reversed biv, any
7658 REG_EQUAL notes should still be correct. */
7659 if (! set
7660 || GET_CODE (SET_DEST (set)) != REG
7661 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
7662 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
7663 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
7664 for (pnote = &REG_NOTES (p); *pnote;)
7666 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
7667 && reg_mentioned_p (regno_reg_rtx[bl->regno],
7668 XEXP (*pnote, 0)))
7669 *pnote = XEXP (*pnote, 1);
7670 else
7671 pnote = &XEXP (*pnote, 1);
7675 /* Mark that this biv has been reversed. Each giv which depends
7676 on this biv, and which is also live past the end of the loop
7677 will have to be fixed up. */
7679 bl->reversed = 1;
7681 if (loop_dump_stream)
7683 fprintf (loop_dump_stream, "Reversed loop");
7684 if (bl->nonneg)
7685 fprintf (loop_dump_stream, " and added reg_nonneg\n");
7686 else
7687 fprintf (loop_dump_stream, "\n");
7690 return 1;
7695 return 0;
7698 /* Verify whether the biv BL appears to be eliminable,
7699 based on the insns in the loop that refer to it.
7701 If ELIMINATE_P is non-zero, actually do the elimination.
7703 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
7704 determine whether invariant insns should be placed inside or at the
7705 start of the loop. */
7707 static int
7708 maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
7709 const struct loop *loop;
7710 struct iv_class *bl;
7711 int eliminate_p;
7712 int threshold, insn_count;
7714 struct loop_ivs *ivs = LOOP_IVS (loop);
7715 rtx reg = bl->biv->dest_reg;
7716 rtx p;
7718 /* Scan all insns in the loop, stopping if we find one that uses the
7719 biv in a way that we cannot eliminate. */
7721 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
7723 enum rtx_code code = GET_CODE (p);
7724 basic_block where_bb = 0;
7725 rtx where_insn = threshold >= insn_count ? 0 : p;
7727 /* If this is a libcall that sets a giv, skip ahead to its end. */
7728 if (GET_RTX_CLASS (code) == 'i')
7730 rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
7732 if (note)
7734 rtx last = XEXP (note, 0);
7735 rtx set = single_set (last);
7737 if (set && GET_CODE (SET_DEST (set)) == REG)
7739 unsigned int regno = REGNO (SET_DEST (set));
7741 if (regno < ivs->n_regs
7742 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
7743 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
7744 p = last;
7748 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
7749 && reg_mentioned_p (reg, PATTERN (p))
7750 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
7751 eliminate_p, where_bb, where_insn))
7753 if (loop_dump_stream)
7754 fprintf (loop_dump_stream,
7755 "Cannot eliminate biv %d: biv used in insn %d.\n",
7756 bl->regno, INSN_UID (p));
7757 break;
7761 if (p == loop->end)
7763 if (loop_dump_stream)
7764 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
7765 bl->regno, eliminate_p ? "was" : "can be");
7766 return 1;
7769 return 0;
7772 /* INSN and REFERENCE are instructions in the same insn chain.
7773 Return non-zero if INSN is first. */
7776 loop_insn_first_p (insn, reference)
7777 rtx insn, reference;
7779 rtx p, q;
7781 for (p = insn, q = reference;;)
7783 /* Start with test for not first so that INSN == REFERENCE yields not
7784 first. */
7785 if (q == insn || ! p)
7786 return 0;
7787 if (p == reference || ! q)
7788 return 1;
7790 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
7791 previous insn, hence the <= comparison below does not work if
7792 P is a note. */
7793 if (INSN_UID (p) < max_uid_for_loop
7794 && INSN_UID (q) < max_uid_for_loop
7795 && GET_CODE (p) != NOTE)
7796 return INSN_LUID (p) <= INSN_LUID (q);
7798 if (INSN_UID (p) >= max_uid_for_loop
7799 || GET_CODE (p) == NOTE)
7800 p = NEXT_INSN (p);
7801 if (INSN_UID (q) >= max_uid_for_loop)
7802 q = NEXT_INSN (q);
7806 /* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
7807 the offset that we have to take into account due to auto-increment /
7808 div derivation is zero. */
7809 static int
7810 biv_elimination_giv_has_0_offset (biv, giv, insn)
7811 struct induction *biv, *giv;
7812 rtx insn;
7814 /* If the giv V had the auto-inc address optimization applied
7815 to it, and INSN occurs between the giv insn and the biv
7816 insn, then we'd have to adjust the value used here.
7817 This is rare, so we don't bother to make this possible. */
7818 if (giv->auto_inc_opt
7819 && ((loop_insn_first_p (giv->insn, insn)
7820 && loop_insn_first_p (insn, biv->insn))
7821 || (loop_insn_first_p (biv->insn, insn)
7822 && loop_insn_first_p (insn, giv->insn))))
7823 return 0;
7825 return 1;
7828 /* If BL appears in X (part of the pattern of INSN), see if we can
7829 eliminate its use. If so, return 1. If not, return 0.
7831 If BIV does not appear in X, return 1.
7833 If ELIMINATE_P is non-zero, actually do the elimination.
7834 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
7835 Depending on how many items have been moved out of the loop, it
7836 will either be before INSN (when WHERE_INSN is non-zero) or at the
7837 start of the loop (when WHERE_INSN is zero). */
7839 static int
7840 maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
7841 const struct loop *loop;
7842 rtx x, insn;
7843 struct iv_class *bl;
7844 int eliminate_p;
7845 basic_block where_bb;
7846 rtx where_insn;
7848 enum rtx_code code = GET_CODE (x);
7849 rtx reg = bl->biv->dest_reg;
7850 enum machine_mode mode = GET_MODE (reg);
7851 struct induction *v;
7852 rtx arg, tem;
7853 #ifdef HAVE_cc0
7854 rtx new;
7855 #endif
7856 int arg_operand;
7857 const char *fmt;
7858 int i, j;
7860 switch (code)
7862 case REG:
7863 /* If we haven't already been able to do something with this BIV,
7864 we can't eliminate it. */
7865 if (x == reg)
7866 return 0;
7867 return 1;
7869 case SET:
7870 /* If this sets the BIV, it is not a problem. */
7871 if (SET_DEST (x) == reg)
7872 return 1;
7874 /* If this is an insn that defines a giv, it is also ok because
7875 it will go away when the giv is reduced. */
7876 for (v = bl->giv; v; v = v->next_iv)
7877 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
7878 return 1;
7880 #ifdef HAVE_cc0
7881 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
7883 /* Can replace with any giv that was reduced and
7884 that has (MULT_VAL != 0) and (ADD_VAL == 0).
7885 Require a constant for MULT_VAL, so we know it's nonzero.
7886 ??? We disable this optimization to avoid potential
7887 overflows. */
7889 for (v = bl->giv; v; v = v->next_iv)
7890 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
7891 && v->add_val == const0_rtx
7892 && ! v->ignore && ! v->maybe_dead && v->always_computable
7893 && v->mode == mode
7894 && 0)
7896 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
7897 continue;
7899 if (! eliminate_p)
7900 return 1;
7902 /* If the giv has the opposite direction of change,
7903 then reverse the comparison. */
7904 if (INTVAL (v->mult_val) < 0)
7905 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
7906 const0_rtx, v->new_reg);
7907 else
7908 new = v->new_reg;
7910 /* We can probably test that giv's reduced reg. */
7911 if (validate_change (insn, &SET_SRC (x), new, 0))
7912 return 1;
7915 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
7916 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
7917 Require a constant for MULT_VAL, so we know it's nonzero.
7918 ??? Do this only if ADD_VAL is a pointer to avoid a potential
7919 overflow problem. */
7921 for (v = bl->giv; v; v = v->next_iv)
7922 if (GET_CODE (v->mult_val) == CONST_INT
7923 && v->mult_val != const0_rtx
7924 && ! v->ignore && ! v->maybe_dead && v->always_computable
7925 && v->mode == mode
7926 && (GET_CODE (v->add_val) == SYMBOL_REF
7927 || GET_CODE (v->add_val) == LABEL_REF
7928 || GET_CODE (v->add_val) == CONST
7929 || (GET_CODE (v->add_val) == REG
7930 && REG_POINTER (v->add_val))))
7932 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
7933 continue;
7935 if (! eliminate_p)
7936 return 1;
7938 /* If the giv has the opposite direction of change,
7939 then reverse the comparison. */
7940 if (INTVAL (v->mult_val) < 0)
7941 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
7942 v->new_reg);
7943 else
7944 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
7945 copy_rtx (v->add_val));
7947 /* Replace biv with the giv's reduced register. */
7948 update_reg_last_use (v->add_val, insn);
7949 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
7950 return 1;
7952 /* Insn doesn't support that constant or invariant. Copy it
7953 into a register (it will be a loop invariant.) */
7954 tem = gen_reg_rtx (GET_MODE (v->new_reg));
7956 loop_insn_emit_before (loop, 0, where_insn,
7957 gen_move_insn (tem,
7958 copy_rtx (v->add_val)));
7960 /* Substitute the new register for its invariant value in
7961 the compare expression. */
7962 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
7963 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
7964 return 1;
7967 #endif
7968 break;
7970 case COMPARE:
7971 case EQ: case NE:
7972 case GT: case GE: case GTU: case GEU:
7973 case LT: case LE: case LTU: case LEU:
7974 /* See if either argument is the biv. */
7975 if (XEXP (x, 0) == reg)
7976 arg = XEXP (x, 1), arg_operand = 1;
7977 else if (XEXP (x, 1) == reg)
7978 arg = XEXP (x, 0), arg_operand = 0;
7979 else
7980 break;
7982 if (CONSTANT_P (arg))
7984 /* First try to replace with any giv that has constant positive
7985 mult_val and constant add_val. We might be able to support
7986 negative mult_val, but it seems complex to do it in general. */
7988 for (v = bl->giv; v; v = v->next_iv)
7989 if (GET_CODE (v->mult_val) == CONST_INT
7990 && INTVAL (v->mult_val) > 0
7991 && (GET_CODE (v->add_val) == SYMBOL_REF
7992 || GET_CODE (v->add_val) == LABEL_REF
7993 || GET_CODE (v->add_val) == CONST
7994 || (GET_CODE (v->add_val) == REG
7995 && REG_POINTER (v->add_val)))
7996 && ! v->ignore && ! v->maybe_dead && v->always_computable
7997 && v->mode == mode)
7999 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8000 continue;
8002 if (! eliminate_p)
8003 return 1;
8005 /* Replace biv with the giv's reduced reg. */
8006 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
8008 /* If all constants are actually constant integers and
8009 the derived constant can be directly placed in the COMPARE,
8010 do so. */
8011 if (GET_CODE (arg) == CONST_INT
8012 && GET_CODE (v->mult_val) == CONST_INT
8013 && GET_CODE (v->add_val) == CONST_INT)
8015 validate_change (insn, &XEXP (x, arg_operand),
8016 GEN_INT (INTVAL (arg)
8017 * INTVAL (v->mult_val)
8018 + INTVAL (v->add_val)), 1);
8020 else
8022 /* Otherwise, load it into a register. */
8023 tem = gen_reg_rtx (mode);
8024 loop_iv_add_mult_emit_before (loop, arg,
8025 v->mult_val, v->add_val,
8026 tem, where_bb, where_insn);
8027 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8029 if (apply_change_group ())
8030 return 1;
8033 /* Look for giv with positive constant mult_val and nonconst add_val.
8034 Insert insns to calculate new compare value.
8035 ??? Turn this off due to possible overflow. */
8037 for (v = bl->giv; v; v = v->next_iv)
8038 if (GET_CODE (v->mult_val) == CONST_INT
8039 && INTVAL (v->mult_val) > 0
8040 && ! v->ignore && ! v->maybe_dead && v->always_computable
8041 && v->mode == mode
8042 && 0)
8044 rtx tem;
8046 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8047 continue;
8049 if (! eliminate_p)
8050 return 1;
8052 tem = gen_reg_rtx (mode);
8054 /* Replace biv with giv's reduced register. */
8055 validate_change (insn, &XEXP (x, 1 - arg_operand),
8056 v->new_reg, 1);
8058 /* Compute value to compare against. */
8059 loop_iv_add_mult_emit_before (loop, arg,
8060 v->mult_val, v->add_val,
8061 tem, where_bb, where_insn);
8062 /* Use it in this insn. */
8063 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8064 if (apply_change_group ())
8065 return 1;
8068 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8070 if (loop_invariant_p (loop, arg) == 1)
8072 /* Look for giv with constant positive mult_val and nonconst
8073 add_val. Insert insns to compute new compare value.
8074 ??? Turn this off due to possible overflow. */
8076 for (v = bl->giv; v; v = v->next_iv)
8077 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8078 && ! v->ignore && ! v->maybe_dead && v->always_computable
8079 && v->mode == mode
8080 && 0)
8082 rtx tem;
8084 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8085 continue;
8087 if (! eliminate_p)
8088 return 1;
8090 tem = gen_reg_rtx (mode);
8092 /* Replace biv with giv's reduced register. */
8093 validate_change (insn, &XEXP (x, 1 - arg_operand),
8094 v->new_reg, 1);
8096 /* Compute value to compare against. */
8097 loop_iv_add_mult_emit_before (loop, arg,
8098 v->mult_val, v->add_val,
8099 tem, where_bb, where_insn);
8100 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8101 if (apply_change_group ())
8102 return 1;
8106 /* This code has problems. Basically, you can't know when
8107 seeing if we will eliminate BL, whether a particular giv
8108 of ARG will be reduced. If it isn't going to be reduced,
8109 we can't eliminate BL. We can try forcing it to be reduced,
8110 but that can generate poor code.
8112 The problem is that the benefit of reducing TV, below should
8113 be increased if BL can actually be eliminated, but this means
8114 we might have to do a topological sort of the order in which
8115 we try to process biv. It doesn't seem worthwhile to do
8116 this sort of thing now. */
8118 #if 0
8119 /* Otherwise the reg compared with had better be a biv. */
8120 if (GET_CODE (arg) != REG
8121 || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
8122 return 0;
8124 /* Look for a pair of givs, one for each biv,
8125 with identical coefficients. */
8126 for (v = bl->giv; v; v = v->next_iv)
8128 struct induction *tv;
8130 if (v->ignore || v->maybe_dead || v->mode != mode)
8131 continue;
8133 for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
8134 tv = tv->next_iv)
8135 if (! tv->ignore && ! tv->maybe_dead
8136 && rtx_equal_p (tv->mult_val, v->mult_val)
8137 && rtx_equal_p (tv->add_val, v->add_val)
8138 && tv->mode == mode)
8140 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8141 continue;
8143 if (! eliminate_p)
8144 return 1;
8146 /* Replace biv with its giv's reduced reg. */
8147 XEXP (x, 1 - arg_operand) = v->new_reg;
8148 /* Replace other operand with the other giv's
8149 reduced reg. */
8150 XEXP (x, arg_operand) = tv->new_reg;
8151 return 1;
8154 #endif
8157 /* If we get here, the biv can't be eliminated. */
8158 return 0;
8160 case MEM:
8161 /* If this address is a DEST_ADDR giv, it doesn't matter if the
8162 biv is used in it, since it will be replaced. */
8163 for (v = bl->giv; v; v = v->next_iv)
8164 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
8165 return 1;
8166 break;
8168 default:
8169 break;
8172 /* See if any subexpression fails elimination. */
8173 fmt = GET_RTX_FORMAT (code);
8174 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8176 switch (fmt[i])
8178 case 'e':
8179 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
8180 eliminate_p, where_bb, where_insn))
8181 return 0;
8182 break;
8184 case 'E':
8185 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8186 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
8187 eliminate_p, where_bb, where_insn))
8188 return 0;
8189 break;
8193 return 1;
8196 /* Return nonzero if the last use of REG
8197 is in an insn following INSN in the same basic block. */
8199 static int
8200 last_use_this_basic_block (reg, insn)
8201 rtx reg;
8202 rtx insn;
8204 rtx n;
8205 for (n = insn;
8206 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
8207 n = NEXT_INSN (n))
8209 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
8210 return 1;
8212 return 0;
8215 /* Called via `note_stores' to record the initial value of a biv. Here we
8216 just record the location of the set and process it later. */
8218 static void
8219 record_initial (dest, set, data)
8220 rtx dest;
8221 rtx set;
8222 void *data ATTRIBUTE_UNUSED;
8224 struct loop_ivs *ivs = (struct loop_ivs *) data;
8225 struct iv_class *bl;
8227 if (GET_CODE (dest) != REG
8228 || REGNO (dest) >= ivs->n_regs
8229 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
8230 return;
8232 bl = REG_IV_CLASS (ivs, REGNO (dest));
8234 /* If this is the first set found, record it. */
8235 if (bl->init_insn == 0)
8237 bl->init_insn = note_insn;
8238 bl->init_set = set;
8242 /* If any of the registers in X are "old" and currently have a last use earlier
8243 than INSN, update them to have a last use of INSN. Their actual last use
8244 will be the previous insn but it will not have a valid uid_luid so we can't
8245 use it. X must be a source expression only. */
8247 static void
8248 update_reg_last_use (x, insn)
8249 rtx x;
8250 rtx insn;
8252 /* Check for the case where INSN does not have a valid luid. In this case,
8253 there is no need to modify the regno_last_uid, as this can only happen
8254 when code is inserted after the loop_end to set a pseudo's final value,
8255 and hence this insn will never be the last use of x.
8256 ???? This comment is not correct. See for example loop_givs_reduce.
8257 This may insert an insn before another new insn. */
8258 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
8259 && INSN_UID (insn) < max_uid_for_loop
8260 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
8262 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
8264 else
8266 register int i, j;
8267 register const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
8268 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8270 if (fmt[i] == 'e')
8271 update_reg_last_use (XEXP (x, i), insn);
8272 else if (fmt[i] == 'E')
8273 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8274 update_reg_last_use (XVECEXP (x, i, j), insn);
8279 /* Given an insn INSN and condition COND, return the condition in a
8280 canonical form to simplify testing by callers. Specifically:
8282 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
8283 (2) Both operands will be machine operands; (cc0) will have been replaced.
8284 (3) If an operand is a constant, it will be the second operand.
8285 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
8286 for GE, GEU, and LEU.
8288 If the condition cannot be understood, or is an inequality floating-point
8289 comparison which needs to be reversed, 0 will be returned.
8291 If REVERSE is non-zero, then reverse the condition prior to canonizing it.
8293 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8294 insn used in locating the condition was found. If a replacement test
8295 of the condition is desired, it should be placed in front of that
8296 insn and we will be sure that the inputs are still valid.
8298 If WANT_REG is non-zero, we wish the condition to be relative to that
8299 register, if possible. Therefore, do not canonicalize the condition
8300 further. */
8303 canonicalize_condition (insn, cond, reverse, earliest, want_reg)
8304 rtx insn;
8305 rtx cond;
8306 int reverse;
8307 rtx *earliest;
8308 rtx want_reg;
8310 enum rtx_code code;
8311 rtx prev = insn;
8312 rtx set;
8313 rtx tem;
8314 rtx op0, op1;
8315 int reverse_code = 0;
8316 enum machine_mode mode;
8318 code = GET_CODE (cond);
8319 mode = GET_MODE (cond);
8320 op0 = XEXP (cond, 0);
8321 op1 = XEXP (cond, 1);
8323 if (reverse)
8324 code = reversed_comparison_code (cond, insn);
8325 if (code == UNKNOWN)
8326 return 0;
8328 if (earliest)
8329 *earliest = insn;
8331 /* If we are comparing a register with zero, see if the register is set
8332 in the previous insn to a COMPARE or a comparison operation. Perform
8333 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
8334 in cse.c */
8336 while (GET_RTX_CLASS (code) == '<'
8337 && op1 == CONST0_RTX (GET_MODE (op0))
8338 && op0 != want_reg)
8340 /* Set non-zero when we find something of interest. */
8341 rtx x = 0;
8343 #ifdef HAVE_cc0
8344 /* If comparison with cc0, import actual comparison from compare
8345 insn. */
8346 if (op0 == cc0_rtx)
8348 if ((prev = prev_nonnote_insn (prev)) == 0
8349 || GET_CODE (prev) != INSN
8350 || (set = single_set (prev)) == 0
8351 || SET_DEST (set) != cc0_rtx)
8352 return 0;
8354 op0 = SET_SRC (set);
8355 op1 = CONST0_RTX (GET_MODE (op0));
8356 if (earliest)
8357 *earliest = prev;
8359 #endif
8361 /* If this is a COMPARE, pick up the two things being compared. */
8362 if (GET_CODE (op0) == COMPARE)
8364 op1 = XEXP (op0, 1);
8365 op0 = XEXP (op0, 0);
8366 continue;
8368 else if (GET_CODE (op0) != REG)
8369 break;
8371 /* Go back to the previous insn. Stop if it is not an INSN. We also
8372 stop if it isn't a single set or if it has a REG_INC note because
8373 we don't want to bother dealing with it. */
8375 if ((prev = prev_nonnote_insn (prev)) == 0
8376 || GET_CODE (prev) != INSN
8377 || FIND_REG_INC_NOTE (prev, 0))
8378 break;
8380 set = set_of (op0, prev);
8382 if (set
8383 && (GET_CODE (set) != SET
8384 || !rtx_equal_p (SET_DEST (set), op0)))
8385 break;
8387 /* If this is setting OP0, get what it sets it to if it looks
8388 relevant. */
8389 if (set)
8391 enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
8393 /* ??? We may not combine comparisons done in a CCmode with
8394 comparisons not done in a CCmode. This is to aid targets
8395 like Alpha that have an IEEE compliant EQ instruction, and
8396 a non-IEEE compliant BEQ instruction. The use of CCmode is
8397 actually artificial, simply to prevent the combination, but
8398 should not affect other platforms.
8400 However, we must allow VOIDmode comparisons to match either
8401 CCmode or non-CCmode comparison, because some ports have
8402 modeless comparisons inside branch patterns.
8404 ??? This mode check should perhaps look more like the mode check
8405 in simplify_comparison in combine. */
8407 if ((GET_CODE (SET_SRC (set)) == COMPARE
8408 || (((code == NE
8409 || (code == LT
8410 && GET_MODE_CLASS (inner_mode) == MODE_INT
8411 && (GET_MODE_BITSIZE (inner_mode)
8412 <= HOST_BITS_PER_WIDE_INT)
8413 && (STORE_FLAG_VALUE
8414 & ((HOST_WIDE_INT) 1
8415 << (GET_MODE_BITSIZE (inner_mode) - 1))))
8416 #ifdef FLOAT_STORE_FLAG_VALUE
8417 || (code == LT
8418 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8419 && (REAL_VALUE_NEGATIVE
8420 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
8421 #endif
8423 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
8424 && (((GET_MODE_CLASS (mode) == MODE_CC)
8425 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8426 || mode == VOIDmode || inner_mode == VOIDmode))
8427 x = SET_SRC (set);
8428 else if (((code == EQ
8429 || (code == GE
8430 && (GET_MODE_BITSIZE (inner_mode)
8431 <= HOST_BITS_PER_WIDE_INT)
8432 && GET_MODE_CLASS (inner_mode) == MODE_INT
8433 && (STORE_FLAG_VALUE
8434 & ((HOST_WIDE_INT) 1
8435 << (GET_MODE_BITSIZE (inner_mode) - 1))))
8436 #ifdef FLOAT_STORE_FLAG_VALUE
8437 || (code == GE
8438 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8439 && (REAL_VALUE_NEGATIVE
8440 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
8441 #endif
8443 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
8444 && (((GET_MODE_CLASS (mode) == MODE_CC)
8445 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8446 || mode == VOIDmode || inner_mode == VOIDmode))
8449 reverse_code = 1;
8450 x = SET_SRC (set);
8452 else
8453 break;
8456 else if (reg_set_p (op0, prev))
8457 /* If this sets OP0, but not directly, we have to give up. */
8458 break;
8460 if (x)
8462 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
8463 code = GET_CODE (x);
8464 if (reverse_code)
8466 code = reversed_comparison_code (x, prev);
8467 if (code == UNKNOWN)
8468 return 0;
8469 reverse_code = 0;
8472 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
8473 if (earliest)
8474 *earliest = prev;
8478 /* If constant is first, put it last. */
8479 if (CONSTANT_P (op0))
8480 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
8482 /* If OP0 is the result of a comparison, we weren't able to find what
8483 was really being compared, so fail. */
8484 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
8485 return 0;
8487 /* Canonicalize any ordered comparison with integers involving equality
8488 if we can do computations in the relevant mode and we do not
8489 overflow. */
8491 if (GET_CODE (op1) == CONST_INT
8492 && GET_MODE (op0) != VOIDmode
8493 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
8495 HOST_WIDE_INT const_val = INTVAL (op1);
8496 unsigned HOST_WIDE_INT uconst_val = const_val;
8497 unsigned HOST_WIDE_INT max_val
8498 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
8500 switch (code)
8502 case LE:
8503 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
8504 code = LT, op1 = GEN_INT (const_val + 1);
8505 break;
8507 /* When cross-compiling, const_val might be sign-extended from
8508 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
8509 case GE:
8510 if ((HOST_WIDE_INT) (const_val & max_val)
8511 != (((HOST_WIDE_INT) 1
8512 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
8513 code = GT, op1 = GEN_INT (const_val - 1);
8514 break;
8516 case LEU:
8517 if (uconst_val < max_val)
8518 code = LTU, op1 = GEN_INT (uconst_val + 1);
8519 break;
8521 case GEU:
8522 if (uconst_val != 0)
8523 code = GTU, op1 = GEN_INT (uconst_val - 1);
8524 break;
8526 default:
8527 break;
8531 #ifdef HAVE_cc0
8532 /* Never return CC0; return zero instead. */
8533 if (op0 == cc0_rtx)
8534 return 0;
8535 #endif
8537 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
8540 /* Given a jump insn JUMP, return the condition that will cause it to branch
8541 to its JUMP_LABEL. If the condition cannot be understood, or is an
8542 inequality floating-point comparison which needs to be reversed, 0 will
8543 be returned.
8545 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8546 insn used in locating the condition was found. If a replacement test
8547 of the condition is desired, it should be placed in front of that
8548 insn and we will be sure that the inputs are still valid. */
8551 get_condition (jump, earliest)
8552 rtx jump;
8553 rtx *earliest;
8555 rtx cond;
8556 int reverse;
8557 rtx set;
8559 /* If this is not a standard conditional jump, we can't parse it. */
8560 if (GET_CODE (jump) != JUMP_INSN
8561 || ! any_condjump_p (jump))
8562 return 0;
8563 set = pc_set (jump);
8565 cond = XEXP (SET_SRC (set), 0);
8567 /* If this branches to JUMP_LABEL when the condition is false, reverse
8568 the condition. */
8569 reverse
8570 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
8571 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
8573 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
8576 /* Similar to above routine, except that we also put an invariant last
8577 unless both operands are invariants. */
8580 get_condition_for_loop (loop, x)
8581 const struct loop *loop;
8582 rtx x;
8584 rtx comparison = get_condition (x, NULL_PTR);
8586 if (comparison == 0
8587 || ! loop_invariant_p (loop, XEXP (comparison, 0))
8588 || loop_invariant_p (loop, XEXP (comparison, 1)))
8589 return comparison;
8591 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
8592 XEXP (comparison, 1), XEXP (comparison, 0));
8595 /* Scan the function and determine whether it has indirect (computed) jumps.
8597 This is taken mostly from flow.c; similar code exists elsewhere
8598 in the compiler. It may be useful to put this into rtlanal.c. */
8599 static int
8600 indirect_jump_in_function_p (start)
8601 rtx start;
8603 rtx insn;
8605 for (insn = start; insn; insn = NEXT_INSN (insn))
8606 if (computed_jump_p (insn))
8607 return 1;
8609 return 0;
8612 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
8613 documentation for LOOP_MEMS for the definition of `appropriate'.
8614 This function is called from prescan_loop via for_each_rtx. */
8616 static int
8617 insert_loop_mem (mem, data)
8618 rtx *mem;
8619 void *data ATTRIBUTE_UNUSED;
8621 struct loop_info *loop_info = data;
8622 int i;
8623 rtx m = *mem;
8625 if (m == NULL_RTX)
8626 return 0;
8628 switch (GET_CODE (m))
8630 case MEM:
8631 break;
8633 case CLOBBER:
8634 /* We're not interested in MEMs that are only clobbered. */
8635 return -1;
8637 case CONST_DOUBLE:
8638 /* We're not interested in the MEM associated with a
8639 CONST_DOUBLE, so there's no need to traverse into this. */
8640 return -1;
8642 case EXPR_LIST:
8643 /* We're not interested in any MEMs that only appear in notes. */
8644 return -1;
8646 default:
8647 /* This is not a MEM. */
8648 return 0;
8651 /* See if we've already seen this MEM. */
8652 for (i = 0; i < loop_info->mems_idx; ++i)
8653 if (rtx_equal_p (m, loop_info->mems[i].mem))
8655 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
8656 /* The modes of the two memory accesses are different. If
8657 this happens, something tricky is going on, and we just
8658 don't optimize accesses to this MEM. */
8659 loop_info->mems[i].optimize = 0;
8661 return 0;
8664 /* Resize the array, if necessary. */
8665 if (loop_info->mems_idx == loop_info->mems_allocated)
8667 if (loop_info->mems_allocated != 0)
8668 loop_info->mems_allocated *= 2;
8669 else
8670 loop_info->mems_allocated = 32;
8672 loop_info->mems = (loop_mem_info *)
8673 xrealloc (loop_info->mems,
8674 loop_info->mems_allocated * sizeof (loop_mem_info));
8677 /* Actually insert the MEM. */
8678 loop_info->mems[loop_info->mems_idx].mem = m;
8679 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
8680 because we can't put it in a register. We still store it in the
8681 table, though, so that if we see the same address later, but in a
8682 non-BLK mode, we'll not think we can optimize it at that point. */
8683 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
8684 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
8685 ++loop_info->mems_idx;
8687 return 0;
8691 /* Allocate REGS->ARRAY or reallocate it if it is too small.
8693 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
8694 register that is modified by an insn between FROM and TO. If the
8695 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
8696 more, stop incrementing it, to avoid overflow.
8698 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
8699 register I is used, if it is only used once. Otherwise, it is set
8700 to 0 (for no uses) or const0_rtx for more than one use. This
8701 parameter may be zero, in which case this processing is not done.
8703 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
8704 optimize register I.
8706 Store in *COUNT_PTR the number of actual instructions
8707 in the loop. We use this to decide what is worth moving out. */
8709 static void
8710 loop_regs_scan (loop, extra_size, count_ptr)
8711 const struct loop *loop;
8712 int extra_size;
8713 int *count_ptr;
8715 struct loop_regs *regs = LOOP_REGS (loop);
8716 int old_nregs;
8717 /* last_set[n] is nonzero iff reg n has been set in the current
8718 basic block. In that case, it is the insn that last set reg n. */
8719 rtx *last_set;
8720 rtx insn;
8721 int count = 0;
8722 int i;
8724 old_nregs = regs->num;
8725 regs->num = max_reg_num ();
8727 /* Grow the regs array if not allocated or too small. */
8728 if (regs->num >= regs->size)
8730 regs->size = regs->num + extra_size;
8732 regs->array = (struct loop_reg *)
8733 xrealloc (regs->array, regs->size * sizeof (*regs->array));
8735 /* Zero the new elements. */
8736 memset (regs->array + old_nregs, 0,
8737 (regs->size - old_nregs) * sizeof (*regs->array));
8740 /* Clear previously scanned fields but do not clear n_times_set. */
8741 for (i = 0; i < old_nregs; i++)
8743 regs->array[i].set_in_loop = 0;
8744 regs->array[i].may_not_optimize = 0;
8745 regs->array[i].single_usage = NULL_RTX;
8748 last_set = (rtx *) xcalloc (regs->num, sizeof (rtx));
8750 /* Scan the loop, recording register usage. */
8751 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
8752 insn = NEXT_INSN (insn))
8754 if (INSN_P (insn))
8756 ++count;
8758 /* Record registers that have exactly one use. */
8759 find_single_use_in_loop (regs, insn, PATTERN (insn));
8761 /* Include uses in REG_EQUAL notes. */
8762 if (REG_NOTES (insn))
8763 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
8765 if (GET_CODE (PATTERN (insn)) == SET
8766 || GET_CODE (PATTERN (insn)) == CLOBBER)
8767 count_one_set (regs, insn, PATTERN (insn), last_set);
8768 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
8770 register int i;
8771 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
8772 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
8773 last_set);
8777 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
8778 memset (last_set, 0, regs->num * sizeof (rtx));
8781 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8783 regs->array[i].may_not_optimize = 1;
8784 regs->array[i].set_in_loop = 1;
8787 #ifdef AVOID_CCMODE_COPIES
8788 /* Don't try to move insns which set CC registers if we should not
8789 create CCmode register copies. */
8790 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
8791 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
8792 regs->array[i].may_not_optimize = 1;
8793 #endif
8795 /* Set regs->array[I].n_times_set for the new registers. */
8796 for (i = old_nregs; i < regs->num; i++)
8797 regs->array[i].n_times_set = regs->array[i].set_in_loop;
8799 free (last_set);
8800 *count_ptr = count;
8804 /* Move MEMs into registers for the duration of the loop. */
8806 static void
8807 load_mems (loop)
8808 const struct loop *loop;
8810 struct loop_info *loop_info = LOOP_INFO (loop);
8811 struct loop_regs *regs = LOOP_REGS (loop);
8812 int maybe_never = 0;
8813 int i;
8814 rtx p, prev_ebb_head;
8815 rtx label = NULL_RTX;
8816 rtx end_label;
8817 /* Nonzero if the next instruction may never be executed. */
8818 int next_maybe_never = 0;
8819 unsigned int last_max_reg = max_reg_num ();
8821 if (loop_info->mems_idx == 0)
8822 return;
8824 /* We cannot use next_label here because it skips over normal insns. */
8825 end_label = next_nonnote_insn (loop->end);
8826 if (end_label && GET_CODE (end_label) != CODE_LABEL)
8827 end_label = NULL_RTX;
8829 /* Check to see if it's possible that some instructions in the loop are
8830 never executed. Also check if there is a goto out of the loop other
8831 than right after the end of the loop. */
8832 for (p = next_insn_in_loop (loop, loop->scan_start);
8833 p != NULL_RTX && ! maybe_never;
8834 p = next_insn_in_loop (loop, p))
8836 if (GET_CODE (p) == CODE_LABEL)
8837 maybe_never = 1;
8838 else if (GET_CODE (p) == JUMP_INSN
8839 /* If we enter the loop in the middle, and scan
8840 around to the beginning, don't set maybe_never
8841 for that. This must be an unconditional jump,
8842 otherwise the code at the top of the loop might
8843 never be executed. Unconditional jumps are
8844 followed a by barrier then loop end. */
8845 && ! (GET_CODE (p) == JUMP_INSN
8846 && JUMP_LABEL (p) == loop->top
8847 && NEXT_INSN (NEXT_INSN (p)) == loop->end
8848 && any_uncondjump_p (p)))
8850 /* If this is a jump outside of the loop but not right
8851 after the end of the loop, we would have to emit new fixup
8852 sequences for each such label. */
8853 if (JUMP_LABEL (p) != end_label
8854 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
8855 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
8856 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end)))
8857 return;
8859 if (!any_condjump_p (p))
8860 /* Something complicated. */
8861 maybe_never = 1;
8862 else
8863 /* If there are any more instructions in the loop, they
8864 might not be reached. */
8865 next_maybe_never = 1;
8867 else if (next_maybe_never)
8868 maybe_never = 1;
8871 /* Find start of the extended basic block that enters the loop. */
8872 for (p = loop->start;
8873 PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
8874 p = PREV_INSN (p))
8876 prev_ebb_head = p;
8878 cselib_init ();
8880 /* Build table of mems that get set to constant values before the
8881 loop. */
8882 for (; p != loop->start; p = NEXT_INSN (p))
8883 cselib_process_insn (p);
8885 /* Actually move the MEMs. */
8886 for (i = 0; i < loop_info->mems_idx; ++i)
8888 regset_head load_copies;
8889 regset_head store_copies;
8890 int written = 0;
8891 rtx reg;
8892 rtx mem = loop_info->mems[i].mem;
8893 rtx mem_list_entry;
8895 if (MEM_VOLATILE_P (mem)
8896 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
8897 /* There's no telling whether or not MEM is modified. */
8898 loop_info->mems[i].optimize = 0;
8900 /* Go through the MEMs written to in the loop to see if this
8901 one is aliased by one of them. */
8902 mem_list_entry = loop_info->store_mems;
8903 while (mem_list_entry)
8905 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
8906 written = 1;
8907 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
8908 mem, rtx_varies_p))
8910 /* MEM is indeed aliased by this store. */
8911 loop_info->mems[i].optimize = 0;
8912 break;
8914 mem_list_entry = XEXP (mem_list_entry, 1);
8917 if (flag_float_store && written
8918 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
8919 loop_info->mems[i].optimize = 0;
8921 /* If this MEM is written to, we must be sure that there
8922 are no reads from another MEM that aliases this one. */
8923 if (loop_info->mems[i].optimize && written)
8925 int j;
8927 for (j = 0; j < loop_info->mems_idx; ++j)
8929 if (j == i)
8930 continue;
8931 else if (true_dependence (mem,
8932 VOIDmode,
8933 loop_info->mems[j].mem,
8934 rtx_varies_p))
8936 /* It's not safe to hoist loop_info->mems[i] out of
8937 the loop because writes to it might not be
8938 seen by reads from loop_info->mems[j]. */
8939 loop_info->mems[i].optimize = 0;
8940 break;
8945 if (maybe_never && may_trap_p (mem))
8946 /* We can't access the MEM outside the loop; it might
8947 cause a trap that wouldn't have happened otherwise. */
8948 loop_info->mems[i].optimize = 0;
8950 if (!loop_info->mems[i].optimize)
8951 /* We thought we were going to lift this MEM out of the
8952 loop, but later discovered that we could not. */
8953 continue;
8955 INIT_REG_SET (&load_copies);
8956 INIT_REG_SET (&store_copies);
8958 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
8959 order to keep scan_loop from moving stores to this MEM
8960 out of the loop just because this REG is neither a
8961 user-variable nor used in the loop test. */
8962 reg = gen_reg_rtx (GET_MODE (mem));
8963 REG_USERVAR_P (reg) = 1;
8964 loop_info->mems[i].reg = reg;
8966 /* Now, replace all references to the MEM with the
8967 corresponding pseudos. */
8968 maybe_never = 0;
8969 for (p = next_insn_in_loop (loop, loop->scan_start);
8970 p != NULL_RTX;
8971 p = next_insn_in_loop (loop, p))
8973 if (INSN_P (p))
8975 rtx set;
8977 set = single_set (p);
8979 /* See if this copies the mem into a register that isn't
8980 modified afterwards. We'll try to do copy propagation
8981 a little further on. */
8982 if (set
8983 /* @@@ This test is _way_ too conservative. */
8984 && ! maybe_never
8985 && GET_CODE (SET_DEST (set)) == REG
8986 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
8987 && REGNO (SET_DEST (set)) < last_max_reg
8988 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
8989 && rtx_equal_p (SET_SRC (set), mem))
8990 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
8992 /* See if this copies the mem from a register that isn't
8993 modified afterwards. We'll try to remove the
8994 redundant copy later on by doing a little register
8995 renaming and copy propagation. This will help
8996 to untangle things for the BIV detection code. */
8997 if (set
8998 && ! maybe_never
8999 && GET_CODE (SET_SRC (set)) == REG
9000 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
9001 && REGNO (SET_SRC (set)) < last_max_reg
9002 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
9003 && rtx_equal_p (SET_DEST (set), mem))
9004 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
9006 /* Replace the memory reference with the shadow register. */
9007 replace_loop_mems (p, loop_info->mems[i].mem,
9008 loop_info->mems[i].reg);
9011 if (GET_CODE (p) == CODE_LABEL
9012 || GET_CODE (p) == JUMP_INSN)
9013 maybe_never = 1;
9016 if (! apply_change_group ())
9017 /* We couldn't replace all occurrences of the MEM. */
9018 loop_info->mems[i].optimize = 0;
9019 else
9021 /* Load the memory immediately before LOOP->START, which is
9022 the NOTE_LOOP_BEG. */
9023 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9024 rtx set;
9025 rtx best = mem;
9026 int j;
9027 struct elt_loc_list *const_equiv = 0;
9029 if (e)
9031 struct elt_loc_list *equiv;
9032 struct elt_loc_list *best_equiv = 0;
9033 for (equiv = e->locs; equiv; equiv = equiv->next)
9035 if (CONSTANT_P (equiv->loc))
9036 const_equiv = equiv;
9037 else if (GET_CODE (equiv->loc) == REG
9038 /* Extending hard register lifetimes causes crash
9039 on SRC targets. Doing so on non-SRC is
9040 probably also not good idea, since we most
9041 probably have pseudoregister equivalence as
9042 well. */
9043 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
9044 best_equiv = equiv;
9046 /* Use the constant equivalence if that is cheap enough. */
9047 if (! best_equiv)
9048 best_equiv = const_equiv;
9049 else if (const_equiv
9050 && (rtx_cost (const_equiv->loc, SET)
9051 <= rtx_cost (best_equiv->loc, SET)))
9053 best_equiv = const_equiv;
9054 const_equiv = 0;
9057 /* If best_equiv is nonzero, we know that MEM is set to a
9058 constant or register before the loop. We will use this
9059 knowledge to initialize the shadow register with that
9060 constant or reg rather than by loading from MEM. */
9061 if (best_equiv)
9062 best = copy_rtx (best_equiv->loc);
9065 set = gen_move_insn (reg, best);
9066 set = loop_insn_hoist (loop, set);
9067 if (REG_P (best))
9069 for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
9070 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
9072 REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
9073 break;
9077 if (const_equiv)
9078 REG_NOTES (set) = gen_rtx_EXPR_LIST (REG_EQUAL,
9079 copy_rtx (const_equiv->loc),
9080 REG_NOTES (set));
9082 if (written)
9084 if (label == NULL_RTX)
9086 label = gen_label_rtx ();
9087 emit_label_after (label, loop->end);
9090 /* Store the memory immediately after END, which is
9091 the NOTE_LOOP_END. */
9092 set = gen_move_insn (copy_rtx (mem), reg);
9093 loop_insn_emit_after (loop, 0, label, set);
9096 if (loop_dump_stream)
9098 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9099 REGNO (reg), (written ? "r/w" : "r/o"));
9100 print_rtl (loop_dump_stream, mem);
9101 fputc ('\n', loop_dump_stream);
9104 /* Attempt a bit of copy propagation. This helps untangle the
9105 data flow, and enables {basic,general}_induction_var to find
9106 more bivs/givs. */
9107 EXECUTE_IF_SET_IN_REG_SET
9108 (&load_copies, FIRST_PSEUDO_REGISTER, j,
9110 try_copy_prop (loop, reg, j);
9112 CLEAR_REG_SET (&load_copies);
9114 EXECUTE_IF_SET_IN_REG_SET
9115 (&store_copies, FIRST_PSEUDO_REGISTER, j,
9117 try_swap_copy_prop (loop, reg, j);
9119 CLEAR_REG_SET (&store_copies);
9123 if (label != NULL_RTX && end_label != NULL_RTX)
9125 /* Now, we need to replace all references to the previous exit
9126 label with the new one. */
9127 rtx_pair rr;
9128 rr.r1 = end_label;
9129 rr.r2 = label;
9131 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
9133 for_each_rtx (&p, replace_label, &rr);
9135 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
9136 field. This is not handled by for_each_rtx because it doesn't
9137 handle unprinted ('0') fields. We need to update JUMP_LABEL
9138 because the immediately following unroll pass will use it.
9139 replace_label would not work anyways, because that only handles
9140 LABEL_REFs. */
9141 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
9142 JUMP_LABEL (p) = label;
9146 cselib_finish ();
9149 /* For communication between note_reg_stored and its caller. */
9150 struct note_reg_stored_arg
9152 int set_seen;
9153 rtx reg;
9156 /* Called via note_stores, record in SET_SEEN whether X, which is written,
9157 is equal to ARG. */
9158 static void
9159 note_reg_stored (x, setter, arg)
9160 rtx x, setter ATTRIBUTE_UNUSED;
9161 void *arg;
9163 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
9164 if (t->reg == x)
9165 t->set_seen = 1;
9168 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
9169 There must be exactly one insn that sets this pseudo; it will be
9170 deleted if all replacements succeed and we can prove that the register
9171 is not used after the loop. */
9173 static void
9174 try_copy_prop (loop, replacement, regno)
9175 const struct loop *loop;
9176 rtx replacement;
9177 unsigned int regno;
9179 /* This is the reg that we are copying from. */
9180 rtx reg_rtx = regno_reg_rtx[regno];
9181 rtx init_insn = 0;
9182 rtx insn;
9183 /* These help keep track of whether we replaced all uses of the reg. */
9184 int replaced_last = 0;
9185 int store_is_first = 0;
9187 for (insn = next_insn_in_loop (loop, loop->scan_start);
9188 insn != NULL_RTX;
9189 insn = next_insn_in_loop (loop, insn))
9191 rtx set;
9193 /* Only substitute within one extended basic block from the initializing
9194 insn. */
9195 if (GET_CODE (insn) == CODE_LABEL && init_insn)
9196 break;
9198 if (! INSN_P (insn))
9199 continue;
9201 /* Is this the initializing insn? */
9202 set = single_set (insn);
9203 if (set
9204 && GET_CODE (SET_DEST (set)) == REG
9205 && REGNO (SET_DEST (set)) == regno)
9207 if (init_insn)
9208 abort ();
9210 init_insn = insn;
9211 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
9212 store_is_first = 1;
9215 /* Only substitute after seeing the initializing insn. */
9216 if (init_insn && insn != init_insn)
9218 struct note_reg_stored_arg arg;
9220 replace_loop_regs (insn, reg_rtx, replacement);
9221 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
9222 replaced_last = 1;
9224 /* Stop replacing when REPLACEMENT is modified. */
9225 arg.reg = replacement;
9226 arg.set_seen = 0;
9227 note_stores (PATTERN (insn), note_reg_stored, &arg);
9228 if (arg.set_seen)
9229 break;
9232 if (! init_insn)
9233 abort ();
9234 if (apply_change_group ())
9236 if (loop_dump_stream)
9237 fprintf (loop_dump_stream, " Replaced reg %d", regno);
9238 if (store_is_first && replaced_last)
9240 PUT_CODE (init_insn, NOTE);
9241 NOTE_LINE_NUMBER (init_insn) = NOTE_INSN_DELETED;
9242 if (loop_dump_stream)
9243 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
9244 INSN_UID (init_insn));
9246 if (loop_dump_stream)
9247 fprintf (loop_dump_stream, ".\n");
9251 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
9252 loop LOOP if the order of the sets of these registers can be
9253 swapped. There must be exactly one insn within the loop that sets
9254 this pseudo followed immediately by a move insn that sets
9255 REPLACEMENT with REGNO. */
9256 static void
9257 try_swap_copy_prop (loop, replacement, regno)
9258 const struct loop *loop;
9259 rtx replacement;
9260 unsigned int regno;
9262 rtx insn;
9263 rtx set = NULL_RTX;
9264 unsigned int new_regno;
9266 new_regno = REGNO (replacement);
9268 for (insn = next_insn_in_loop (loop, loop->scan_start);
9269 insn != NULL_RTX;
9270 insn = next_insn_in_loop (loop, insn))
9272 /* Search for the insn that copies REGNO to NEW_REGNO? */
9273 if (INSN_P (insn)
9274 && (set = single_set (insn))
9275 && GET_CODE (SET_DEST (set)) == REG
9276 && REGNO (SET_DEST (set)) == new_regno
9277 && GET_CODE (SET_SRC (set)) == REG
9278 && REGNO (SET_SRC (set)) == regno)
9279 break;
9282 if (set)
9284 rtx prev_insn;
9285 rtx prev_set;
9287 /* Some DEF-USE info would come in handy here to make this
9288 function more general. For now, just check the previous insn
9289 which is the most likely candidate for setting REGNO. */
9291 prev_insn = PREV_INSN (insn);
9293 if (INSN_P (insn)
9294 && (prev_set = single_set (prev_insn))
9295 && GET_CODE (SET_DEST (prev_set)) == REG
9296 && REGNO (SET_DEST (prev_set)) == regno)
9298 /* We have:
9299 (set (reg regno) (expr))
9300 (set (reg new_regno) (reg regno))
9302 so try converting this to:
9303 (set (reg new_regno) (expr))
9304 (set (reg regno) (reg new_regno))
9306 The former construct is often generated when a global
9307 variable used for an induction variable is shadowed by a
9308 register (NEW_REGNO). The latter construct improves the
9309 chances of GIV replacement and BIV elimination. */
9311 validate_change (prev_insn, &SET_DEST (prev_set),
9312 replacement, 1);
9313 validate_change (insn, &SET_DEST (set),
9314 SET_SRC (set), 1);
9315 validate_change (insn, &SET_SRC (set),
9316 replacement, 1);
9318 if (apply_change_group ())
9320 if (loop_dump_stream)
9321 fprintf (loop_dump_stream,
9322 " Swapped set of reg %d at %d with reg %d at %d.\n",
9323 regno, INSN_UID (insn),
9324 new_regno, INSN_UID (prev_insn));
9326 /* Update first use of REGNO. */
9327 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
9328 REGNO_FIRST_UID (regno) = INSN_UID (insn);
9330 /* Now perform copy propagation to hopefully
9331 remove all uses of REGNO within the loop. */
9332 try_copy_prop (loop, replacement, regno);
9338 /* Replace MEM with its associated pseudo register. This function is
9339 called from load_mems via for_each_rtx. DATA is actually a pointer
9340 to a structure describing the instruction currently being scanned
9341 and the MEM we are currently replacing. */
9343 static int
9344 replace_loop_mem (mem, data)
9345 rtx *mem;
9346 void *data;
9348 loop_replace_args *args = (loop_replace_args *) data;
9349 rtx m = *mem;
9351 if (m == NULL_RTX)
9352 return 0;
9354 switch (GET_CODE (m))
9356 case MEM:
9357 break;
9359 case CONST_DOUBLE:
9360 /* We're not interested in the MEM associated with a
9361 CONST_DOUBLE, so there's no need to traverse into one. */
9362 return -1;
9364 default:
9365 /* This is not a MEM. */
9366 return 0;
9369 if (!rtx_equal_p (args->match, m))
9370 /* This is not the MEM we are currently replacing. */
9371 return 0;
9373 /* Actually replace the MEM. */
9374 validate_change (args->insn, mem, args->replacement, 1);
9376 return 0;
9379 static void
9380 replace_loop_mems (insn, mem, reg)
9381 rtx insn;
9382 rtx mem;
9383 rtx reg;
9385 loop_replace_args args;
9387 args.insn = insn;
9388 args.match = mem;
9389 args.replacement = reg;
9391 for_each_rtx (&insn, replace_loop_mem, &args);
9394 /* Replace one register with another. Called through for_each_rtx; PX points
9395 to the rtx being scanned. DATA is actually a pointer to
9396 a structure of arguments. */
9398 static int
9399 replace_loop_reg (px, data)
9400 rtx *px;
9401 void *data;
9403 rtx x = *px;
9404 loop_replace_args *args = (loop_replace_args *) data;
9406 if (x == NULL_RTX)
9407 return 0;
9409 if (x == args->match)
9410 validate_change (args->insn, px, args->replacement, 1);
9412 return 0;
9415 static void
9416 replace_loop_regs (insn, reg, replacement)
9417 rtx insn;
9418 rtx reg;
9419 rtx replacement;
9421 loop_replace_args args;
9423 args.insn = insn;
9424 args.match = reg;
9425 args.replacement = replacement;
9427 for_each_rtx (&insn, replace_loop_reg, &args);
9430 /* Replace occurrences of the old exit label for the loop with the new
9431 one. DATA is an rtx_pair containing the old and new labels,
9432 respectively. */
9434 static int
9435 replace_label (x, data)
9436 rtx *x;
9437 void *data;
9439 rtx l = *x;
9440 rtx old_label = ((rtx_pair *) data)->r1;
9441 rtx new_label = ((rtx_pair *) data)->r2;
9443 if (l == NULL_RTX)
9444 return 0;
9446 if (GET_CODE (l) != LABEL_REF)
9447 return 0;
9449 if (XEXP (l, 0) != old_label)
9450 return 0;
9452 XEXP (l, 0) = new_label;
9453 ++LABEL_NUSES (new_label);
9454 --LABEL_NUSES (old_label);
9456 return 0;
9459 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
9460 (ignored in the interim). */
9462 static rtx
9463 loop_insn_emit_after (loop, where_bb, where_insn, pattern)
9464 const struct loop *loop ATTRIBUTE_UNUSED;
9465 basic_block where_bb ATTRIBUTE_UNUSED;
9466 rtx where_insn;
9467 rtx pattern;
9469 return emit_insn_after (pattern, where_insn);
9473 /* If WHERE_INSN is non-zero emit insn for PATTERN before WHERE_INSN
9474 in basic block WHERE_BB (ignored in the interim) within the loop
9475 otherwise hoist PATTERN into the loop pre-header. */
9478 loop_insn_emit_before (loop, where_bb, where_insn, pattern)
9479 const struct loop *loop;
9480 basic_block where_bb ATTRIBUTE_UNUSED;
9481 rtx where_insn;
9482 rtx pattern;
9484 if (! where_insn)
9485 return loop_insn_hoist (loop, pattern);
9486 return emit_insn_before (pattern, where_insn);
9490 /* Emit call insn for PATTERN before WHERE_INSN in basic block
9491 WHERE_BB (ignored in the interim) within the loop. */
9493 static rtx
9494 loop_call_insn_emit_before (loop, where_bb, where_insn, pattern)
9495 const struct loop *loop ATTRIBUTE_UNUSED;
9496 basic_block where_bb ATTRIBUTE_UNUSED;
9497 rtx where_insn;
9498 rtx pattern;
9500 return emit_call_insn_before (pattern, where_insn);
9504 /* Hoist insn for PATTERN into the loop pre-header. */
9507 loop_insn_hoist (loop, pattern)
9508 const struct loop *loop;
9509 rtx pattern;
9511 return loop_insn_emit_before (loop, 0, loop->start, pattern);
9515 /* Hoist call insn for PATTERN into the loop pre-header. */
9517 static rtx
9518 loop_call_insn_hoist (loop, pattern)
9519 const struct loop *loop;
9520 rtx pattern;
9522 return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
9526 /* Sink insn for PATTERN after the loop end. */
9529 loop_insn_sink (loop, pattern)
9530 const struct loop *loop;
9531 rtx pattern;
9533 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
9537 /* If the loop has multiple exits, emit insn for PATTERN before the
9538 loop to ensure that it will always be executed no matter how the
9539 loop exits. Otherwise, emit the insn for PATTERN after the loop,
9540 since this is slightly more efficient. */
9542 static rtx
9543 loop_insn_sink_or_swim (loop, pattern)
9544 const struct loop *loop;
9545 rtx pattern;
9547 if (loop->exit_count)
9548 return loop_insn_hoist (loop, pattern);
9549 else
9550 return loop_insn_sink (loop, pattern);
9553 static void
9554 loop_ivs_dump (loop, file, verbose)
9555 const struct loop *loop;
9556 FILE *file;
9557 int verbose;
9559 struct iv_class *bl;
9560 int iv_num = 0;
9562 if (! loop || ! file)
9563 return;
9565 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
9566 iv_num++;
9568 fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
9570 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
9572 loop_iv_class_dump (bl, file, verbose);
9573 fputc ('\n', file);
9578 static void
9579 loop_iv_class_dump (bl, file, verbose)
9580 const struct iv_class *bl;
9581 FILE *file;
9582 int verbose ATTRIBUTE_UNUSED;
9584 struct induction *v;
9585 rtx incr;
9586 int i;
9588 if (! bl || ! file)
9589 return;
9591 fprintf (file, "IV class for reg %d, benefit %d\n",
9592 bl->regno, bl->total_benefit);
9594 fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
9595 if (bl->initial_value)
9597 fprintf (file, ", init val: ");
9598 print_simple_rtl (file, bl->initial_value);
9600 if (bl->initial_test)
9602 fprintf (file, ", init test: ");
9603 print_simple_rtl (file, bl->initial_test);
9605 fputc ('\n', file);
9607 if (bl->final_value)
9609 fprintf (file, " Final val: ");
9610 print_simple_rtl (file, bl->final_value);
9611 fputc ('\n', file);
9614 if ((incr = biv_total_increment (bl)))
9616 fprintf (file, " Total increment: ");
9617 print_simple_rtl (file, incr);
9618 fputc ('\n', file);
9621 /* List the increments. */
9622 for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
9624 fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
9625 print_simple_rtl (file, v->add_val);
9626 fputc ('\n', file);
9629 /* List the givs. */
9630 for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
9632 fprintf (file, " Giv%d: insn %d, benefit %d, ",
9633 i, INSN_UID (v->insn), v->benefit);
9634 if (v->giv_type == DEST_ADDR)
9635 print_simple_rtl (file, v->mem);
9636 else
9637 print_simple_rtl (file, single_set (v->insn));
9638 fputc ('\n', file);
9643 static void
9644 loop_biv_dump (v, file, verbose)
9645 const struct induction *v;
9646 FILE *file;
9647 int verbose;
9649 if (! v || ! file)
9650 return;
9652 fprintf (file,
9653 "Biv %d: insn %d",
9654 REGNO (v->dest_reg), INSN_UID (v->insn));
9655 fprintf (file, " const ");
9656 print_simple_rtl (file, v->add_val);
9658 if (verbose && v->final_value)
9660 fputc ('\n', file);
9661 fprintf (file, " final ");
9662 print_simple_rtl (file, v->final_value);
9665 fputc ('\n', file);
9669 static void
9670 loop_giv_dump (v, file, verbose)
9671 const struct induction *v;
9672 FILE *file;
9673 int verbose;
9675 if (! v || ! file)
9676 return;
9678 if (v->giv_type == DEST_REG)
9679 fprintf (file, "Giv %d: insn %d",
9680 REGNO (v->dest_reg), INSN_UID (v->insn));
9681 else
9682 fprintf (file, "Dest address: insn %d",
9683 INSN_UID (v->insn));
9685 fprintf (file, " src reg %d benefit %d",
9686 REGNO (v->src_reg), v->benefit);
9687 fprintf (file, " lifetime %d",
9688 v->lifetime);
9690 if (v->replaceable)
9691 fprintf (file, " replaceable");
9693 if (v->no_const_addval)
9694 fprintf (file, " ncav");
9696 if (v->ext_dependant)
9698 switch (GET_CODE (v->ext_dependant))
9700 case SIGN_EXTEND:
9701 fprintf (file, " ext se");
9702 break;
9703 case ZERO_EXTEND:
9704 fprintf (file, " ext ze");
9705 break;
9706 case TRUNCATE:
9707 fprintf (file, " ext tr");
9708 break;
9709 default:
9710 abort ();
9714 fputc ('\n', file);
9715 fprintf (file, " mult ");
9716 print_simple_rtl (file, v->mult_val);
9718 fputc ('\n', file);
9719 fprintf (file, " add ");
9720 print_simple_rtl (file, v->add_val);
9722 if (verbose && v->final_value)
9724 fputc ('\n', file);
9725 fprintf (file, " final ");
9726 print_simple_rtl (file, v->final_value);
9729 fputc ('\n', file);
9733 void
9734 debug_ivs (loop)
9735 const struct loop *loop;
9737 loop_ivs_dump (loop, stderr, 1);
9741 void
9742 debug_iv_class (bl)
9743 const struct iv_class *bl;
9745 loop_iv_class_dump (bl, stderr, 1);
9749 void
9750 debug_biv (v)
9751 const struct induction *v;
9753 loop_biv_dump (v, stderr, 1);
9757 void
9758 debug_giv (v)
9759 const struct induction *v;
9761 loop_giv_dump (v, stderr, 1);
9765 #define LOOP_BLOCK_NUM_1(INSN) \
9766 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
9768 /* The notes do not have an assigned block, so look at the next insn. */
9769 #define LOOP_BLOCK_NUM(INSN) \
9770 ((INSN) ? (GET_CODE (INSN) == NOTE \
9771 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
9772 : LOOP_BLOCK_NUM_1 (INSN)) \
9773 : -1)
9775 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
9777 static void
9778 loop_dump_aux (loop, file, verbose)
9779 const struct loop *loop;
9780 FILE *file;
9781 int verbose ATTRIBUTE_UNUSED;
9783 rtx label;
9785 if (! loop || ! file)
9786 return;
9788 /* Print diagnostics to compare our concept of a loop with
9789 what the loop notes say. */
9790 if (! PREV_INSN (loop->first->head)
9791 || GET_CODE (PREV_INSN (loop->first->head)) != NOTE
9792 || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
9793 != NOTE_INSN_LOOP_BEG)
9794 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
9795 INSN_UID (PREV_INSN (loop->first->head)));
9796 if (! NEXT_INSN (loop->last->end)
9797 || GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
9798 || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
9799 != NOTE_INSN_LOOP_END)
9800 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
9801 INSN_UID (NEXT_INSN (loop->last->end)));
9803 if (loop->start)
9805 fprintf (file,
9806 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
9807 LOOP_BLOCK_NUM (loop->start),
9808 LOOP_INSN_UID (loop->start),
9809 LOOP_BLOCK_NUM (loop->cont),
9810 LOOP_INSN_UID (loop->cont),
9811 LOOP_BLOCK_NUM (loop->cont),
9812 LOOP_INSN_UID (loop->cont),
9813 LOOP_BLOCK_NUM (loop->vtop),
9814 LOOP_INSN_UID (loop->vtop),
9815 LOOP_BLOCK_NUM (loop->end),
9816 LOOP_INSN_UID (loop->end));
9817 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
9818 LOOP_BLOCK_NUM (loop->top),
9819 LOOP_INSN_UID (loop->top),
9820 LOOP_BLOCK_NUM (loop->scan_start),
9821 LOOP_INSN_UID (loop->scan_start));
9822 fprintf (file, ";; exit_count %d", loop->exit_count);
9823 if (loop->exit_count)
9825 fputs (", labels:", file);
9826 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
9828 fprintf (file, " %d ",
9829 LOOP_INSN_UID (XEXP (label, 0)));
9832 fputs ("\n", file);
9834 /* This can happen when a marked loop appears as two nested loops,
9835 say from while (a || b) {}. The inner loop won't match
9836 the loop markers but the outer one will. */
9837 if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
9838 fprintf (file, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
9842 /* Call this function from the debugger to dump LOOP. */
9844 void
9845 debug_loop (loop)
9846 const struct loop *loop;
9848 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
9851 /* Call this function from the debugger to dump LOOPS. */
9853 void
9854 debug_loops (loops)
9855 const struct loops *loops;
9857 flow_loops_dump (loops, stderr, loop_dump_aux, 1);