cp-tree.h (lang_decl_flags): Rename defined_in_class to initialized_in_class.
[gcc.git] / gcc / loop.c
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.
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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. */
21
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.
28
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.
33
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
36
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 "insn-flags.h"
48 #include "regs.h"
49 #include "recog.h"
50 #include "flags.h"
51 #include "real.h"
52 #include "loop.h"
53 #include "cselib.h"
54 #include "except.h"
55 #include "toplev.h"
56
57 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
58 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
59
60 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
61 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
62 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
63
64
65 /* Vector mapping INSN_UIDs to luids.
66 The luids are like uids but increase monotonically always.
67 We use them to see whether a jump comes from outside a given loop. */
68
69 int *uid_luid;
70
71 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
72 number the insn is contained in. */
73
74 struct loop **uid_loop;
75
76 /* 1 + largest uid of any insn. */
77
78 int max_uid_for_loop;
79
80 /* 1 + luid of last insn. */
81
82 static int max_luid;
83
84 /* Number of loops detected in current function. Used as index to the
85 next few tables. */
86
87 static int max_loop_num;
88
89 /* Bound on pseudo register number before loop optimization.
90 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
91 unsigned int max_reg_before_loop;
92
93 /* The value to pass to the next call of reg_scan_update. */
94 static int loop_max_reg;
95
96 #define obstack_chunk_alloc xmalloc
97 #define obstack_chunk_free free
98 \f
99 /* During the analysis of a loop, a chain of `struct movable's
100 is made to record all the movable insns found.
101 Then the entire chain can be scanned to decide which to move. */
102
103 struct movable
104 {
105 rtx insn; /* A movable insn */
106 rtx set_src; /* The expression this reg is set from. */
107 rtx set_dest; /* The destination of this SET. */
108 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
109 of any registers used within the LIBCALL. */
110 int consec; /* Number of consecutive following insns
111 that must be moved with this one. */
112 unsigned int regno; /* The register it sets */
113 short lifetime; /* lifetime of that register;
114 may be adjusted when matching movables
115 that load the same value are found. */
116 short savings; /* Number of insns we can move for this reg,
117 including other movables that force this
118 or match this one. */
119 unsigned int cond : 1; /* 1 if only conditionally movable */
120 unsigned int force : 1; /* 1 means MUST move this insn */
121 unsigned int global : 1; /* 1 means reg is live outside this loop */
122 /* If PARTIAL is 1, GLOBAL means something different:
123 that the reg is live outside the range from where it is set
124 to the following label. */
125 unsigned int done : 1; /* 1 inhibits further processing of this */
126
127 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
128 In particular, moving it does not make it
129 invariant. */
130 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
131 load SRC, rather than copying INSN. */
132 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
133 first insn of a consecutive sets group. */
134 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
135 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
136 that we should avoid changing when clearing
137 the rest of the reg. */
138 struct movable *match; /* First entry for same value */
139 struct movable *forces; /* An insn that must be moved if this is */
140 struct movable *next;
141 };
142
143
144 FILE *loop_dump_stream;
145
146 /* Forward declarations. */
147
148 static void find_and_verify_loops PARAMS ((rtx, struct loops *));
149 static void mark_loop_jump PARAMS ((rtx, struct loop *));
150 static void prescan_loop PARAMS ((struct loop *));
151 static int reg_in_basic_block_p PARAMS ((rtx, rtx));
152 static int consec_sets_invariant_p PARAMS ((const struct loop *,
153 rtx, int, rtx));
154 static int labels_in_range_p PARAMS ((rtx, int));
155 static void count_one_set PARAMS ((struct loop_regs *, rtx, rtx, rtx *));
156 static void note_addr_stored PARAMS ((rtx, rtx, void *));
157 static void note_set_pseudo_multiple_uses PARAMS ((rtx, rtx, void *));
158 static int loop_reg_used_before_p PARAMS ((const struct loop *, rtx, rtx));
159 static void scan_loop PARAMS ((struct loop*, int));
160 #if 0
161 static void replace_call_address PARAMS ((rtx, rtx, rtx));
162 #endif
163 static rtx skip_consec_insns PARAMS ((rtx, int));
164 static int libcall_benefit PARAMS ((rtx));
165 static void ignore_some_movables PARAMS ((struct loop_movables *));
166 static void force_movables PARAMS ((struct loop_movables *));
167 static void combine_movables PARAMS ((struct loop_movables *,
168 struct loop_regs *));
169 static int regs_match_p PARAMS ((rtx, rtx, struct loop_movables *));
170 static int rtx_equal_for_loop_p PARAMS ((rtx, rtx, struct loop_movables *,
171 struct loop_regs *));
172 static void add_label_notes PARAMS ((rtx, rtx));
173 static void move_movables PARAMS ((struct loop *loop, struct loop_movables *,
174 int, int));
175 static void loop_movables_add PARAMS((struct loop_movables *,
176 struct movable *));
177 static void loop_movables_free PARAMS((struct loop_movables *));
178 static int count_nonfixed_reads PARAMS ((const struct loop *, rtx));
179 static void loop_bivs_find PARAMS((struct loop *));
180 static void loop_bivs_init_find PARAMS((struct loop *));
181 static void loop_bivs_check PARAMS((struct loop *));
182 static void loop_givs_find PARAMS((struct loop *));
183 static void loop_givs_check PARAMS((struct loop *));
184 static int loop_biv_eliminable_p PARAMS((struct loop *, struct iv_class *,
185 int, int));
186 static int loop_giv_reduce_benefit PARAMS((struct loop *, struct iv_class *,
187 struct induction *, rtx));
188 static void loop_givs_dead_check PARAMS((struct loop *, struct iv_class *));
189 static void loop_givs_reduce PARAMS((struct loop *, struct iv_class *));
190 static void loop_givs_rescan PARAMS((struct loop *, struct iv_class *,
191 rtx *));
192 static void loop_ivs_free PARAMS((struct loop *));
193 static void strength_reduce PARAMS ((struct loop *, int, int));
194 static void find_single_use_in_loop PARAMS ((struct loop_regs *, rtx, rtx));
195 static int valid_initial_value_p PARAMS ((rtx, rtx, int, rtx));
196 static void find_mem_givs PARAMS ((const struct loop *, rtx, rtx, int, int));
197 static void record_biv PARAMS ((struct loop *, struct induction *,
198 rtx, rtx, rtx, rtx, rtx *,
199 int, int));
200 static void check_final_value PARAMS ((const struct loop *,
201 struct induction *));
202 static void loop_biv_dump PARAMS((const struct induction *, FILE *, int));
203 static void loop_giv_dump PARAMS((const struct induction *, FILE *, int));
204 static void record_giv PARAMS ((const struct loop *, struct induction *,
205 rtx, rtx, rtx, rtx, rtx, rtx, int,
206 enum g_types, int, int, rtx *));
207 static void update_giv_derive PARAMS ((const struct loop *, rtx));
208 static void check_ext_dependant_givs PARAMS ((struct iv_class *,
209 struct loop_info *));
210 static int basic_induction_var PARAMS ((const struct loop *, rtx,
211 enum machine_mode, rtx, rtx,
212 rtx *, rtx *, rtx **));
213 static rtx simplify_giv_expr PARAMS ((const struct loop *, rtx, rtx *, int *));
214 static int general_induction_var PARAMS ((const struct loop *loop, rtx, rtx *,
215 rtx *, rtx *, rtx *, int, int *,
216 enum machine_mode));
217 static int consec_sets_giv PARAMS ((const struct loop *, int, rtx,
218 rtx, rtx, rtx *, rtx *, rtx *, rtx *));
219 static int check_dbra_loop PARAMS ((struct loop *, int));
220 static rtx express_from_1 PARAMS ((rtx, rtx, rtx));
221 static rtx combine_givs_p PARAMS ((struct induction *, struct induction *));
222 static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
223 static void combine_givs PARAMS ((struct loop_regs *, struct iv_class *));
224 static int product_cheap_p PARAMS ((rtx, rtx));
225 static int maybe_eliminate_biv PARAMS ((const struct loop *, struct iv_class *,
226 int, int, int));
227 static int maybe_eliminate_biv_1 PARAMS ((const struct loop *, rtx, rtx,
228 struct iv_class *, int,
229 basic_block, rtx));
230 static int last_use_this_basic_block PARAMS ((rtx, rtx));
231 static void record_initial PARAMS ((rtx, rtx, void *));
232 static void update_reg_last_use PARAMS ((rtx, rtx));
233 static rtx next_insn_in_loop PARAMS ((const struct loop *, rtx));
234 static void loop_regs_scan PARAMS ((const struct loop*, int, int *));
235 static void load_mems PARAMS ((const struct loop *));
236 static int insert_loop_mem PARAMS ((rtx *, void *));
237 static int replace_loop_mem PARAMS ((rtx *, void *));
238 static void replace_loop_mems PARAMS ((rtx, rtx, rtx));
239 static int replace_loop_reg PARAMS ((rtx *, void *));
240 static void replace_loop_regs PARAMS ((rtx insn, rtx, rtx));
241 static void note_reg_stored PARAMS ((rtx, rtx, void *));
242 static void try_copy_prop PARAMS ((const struct loop *, rtx, unsigned int));
243 static void try_swap_copy_prop PARAMS ((const struct loop *, rtx,
244 unsigned int));
245 static int replace_label PARAMS ((rtx *, void *));
246 static rtx check_insn_for_givs PARAMS((struct loop *, rtx, int, int));
247 static rtx check_insn_for_bivs PARAMS((struct loop *, rtx, int, int));
248 static rtx gen_add_mult PARAMS ((rtx, rtx, rtx, rtx));
249 static void loop_regs_update PARAMS ((const struct loop *, rtx));
250 static int iv_add_mult_cost PARAMS ((rtx, rtx, rtx, rtx));
251
252 static rtx loop_insn_emit_after PARAMS((const struct loop *, basic_block,
253 rtx, rtx));
254 static rtx loop_insn_emit_before PARAMS((const struct loop *, basic_block,
255 rtx, rtx));
256 static rtx loop_insn_sink_or_swim PARAMS((const struct loop *, rtx));
257
258 static void loop_dump_aux PARAMS ((const struct loop *, FILE *, int));
259 void debug_biv PARAMS ((const struct induction *));
260 void debug_giv PARAMS ((const struct induction *));
261 void debug_loop PARAMS ((const struct loop *));
262 void debug_loops PARAMS ((const struct loops *));
263
264 typedef struct rtx_pair
265 {
266 rtx r1;
267 rtx r2;
268 } rtx_pair;
269
270 typedef struct loop_replace_args
271 {
272 rtx match;
273 rtx replacement;
274 rtx insn;
275 } loop_replace_args;
276
277 /* Nonzero iff INSN is between START and END, inclusive. */
278 #define INSN_IN_RANGE_P(INSN, START, END) \
279 (INSN_UID (INSN) < max_uid_for_loop \
280 && INSN_LUID (INSN) >= INSN_LUID (START) \
281 && INSN_LUID (INSN) <= INSN_LUID (END))
282
283 /* Indirect_jump_in_function is computed once per function. */
284 static int indirect_jump_in_function;
285 static int indirect_jump_in_function_p PARAMS ((rtx));
286
287 static int compute_luids PARAMS ((rtx, rtx, int));
288
289 static int biv_elimination_giv_has_0_offset PARAMS ((struct induction *,
290 struct induction *,
291 rtx));
292 \f
293 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
294 copy the value of the strength reduced giv to its original register. */
295 static int copy_cost;
296
297 /* Cost of using a register, to normalize the benefits of a giv. */
298 static int reg_address_cost;
299
300 void
301 init_loop ()
302 {
303 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
304
305 reg_address_cost = address_cost (reg, SImode);
306
307 copy_cost = COSTS_N_INSNS (1);
308 }
309 \f
310 /* Compute the mapping from uids to luids.
311 LUIDs are numbers assigned to insns, like uids,
312 except that luids increase monotonically through the code.
313 Start at insn START and stop just before END. Assign LUIDs
314 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
315 static int
316 compute_luids (start, end, prev_luid)
317 rtx start, end;
318 int prev_luid;
319 {
320 int i;
321 rtx insn;
322
323 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
324 {
325 if (INSN_UID (insn) >= max_uid_for_loop)
326 continue;
327 /* Don't assign luids to line-number NOTEs, so that the distance in
328 luids between two insns is not affected by -g. */
329 if (GET_CODE (insn) != NOTE
330 || NOTE_LINE_NUMBER (insn) <= 0)
331 uid_luid[INSN_UID (insn)] = ++i;
332 else
333 /* Give a line number note the same luid as preceding insn. */
334 uid_luid[INSN_UID (insn)] = i;
335 }
336 return i + 1;
337 }
338 \f
339 /* Entry point of this file. Perform loop optimization
340 on the current function. F is the first insn of the function
341 and DUMPFILE is a stream for output of a trace of actions taken
342 (or 0 if none should be output). */
343
344 void
345 loop_optimize (f, dumpfile, flags)
346 /* f is the first instruction of a chain of insns for one function */
347 rtx f;
348 FILE *dumpfile;
349 int flags;
350 {
351 register rtx insn;
352 register int i;
353 struct loops loops_data;
354 struct loops *loops = &loops_data;
355 struct loop_info *loops_info;
356
357 loop_dump_stream = dumpfile;
358
359 init_recog_no_volatile ();
360
361 max_reg_before_loop = max_reg_num ();
362 loop_max_reg = max_reg_before_loop;
363
364 regs_may_share = 0;
365
366 /* Count the number of loops. */
367
368 max_loop_num = 0;
369 for (insn = f; insn; insn = NEXT_INSN (insn))
370 {
371 if (GET_CODE (insn) == NOTE
372 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
373 max_loop_num++;
374 }
375
376 /* Don't waste time if no loops. */
377 if (max_loop_num == 0)
378 return;
379
380 loops->num = max_loop_num;
381
382 /* Get size to use for tables indexed by uids.
383 Leave some space for labels allocated by find_and_verify_loops. */
384 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
385
386 uid_luid = (int *) xcalloc (max_uid_for_loop, sizeof (int));
387 uid_loop = (struct loop **) xcalloc (max_uid_for_loop,
388 sizeof (struct loop *));
389
390 /* Allocate storage for array of loops. */
391 loops->array = (struct loop *)
392 xcalloc (loops->num, sizeof (struct loop));
393
394 /* Find and process each loop.
395 First, find them, and record them in order of their beginnings. */
396 find_and_verify_loops (f, loops);
397
398 /* Allocate and initialize auxiliary loop information. */
399 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
400 for (i = 0; i < loops->num; i++)
401 loops->array[i].aux = loops_info + i;
402
403 /* Now find all register lifetimes. This must be done after
404 find_and_verify_loops, because it might reorder the insns in the
405 function. */
406 reg_scan (f, max_reg_before_loop, 1);
407
408 /* This must occur after reg_scan so that registers created by gcse
409 will have entries in the register tables.
410
411 We could have added a call to reg_scan after gcse_main in toplev.c,
412 but moving this call to init_alias_analysis is more efficient. */
413 init_alias_analysis ();
414
415 /* See if we went too far. Note that get_max_uid already returns
416 one more that the maximum uid of all insn. */
417 if (get_max_uid () > max_uid_for_loop)
418 abort ();
419 /* Now reset it to the actual size we need. See above. */
420 max_uid_for_loop = get_max_uid ();
421
422 /* find_and_verify_loops has already called compute_luids, but it
423 might have rearranged code afterwards, so we need to recompute
424 the luids now. */
425 max_luid = compute_luids (f, NULL_RTX, 0);
426
427 /* Don't leave gaps in uid_luid for insns that have been
428 deleted. It is possible that the first or last insn
429 using some register has been deleted by cross-jumping.
430 Make sure that uid_luid for that former insn's uid
431 points to the general area where that insn used to be. */
432 for (i = 0; i < max_uid_for_loop; i++)
433 {
434 uid_luid[0] = uid_luid[i];
435 if (uid_luid[0] != 0)
436 break;
437 }
438 for (i = 0; i < max_uid_for_loop; i++)
439 if (uid_luid[i] == 0)
440 uid_luid[i] = uid_luid[i - 1];
441
442 /* Determine if the function has indirect jump. On some systems
443 this prevents low overhead loop instructions from being used. */
444 indirect_jump_in_function = indirect_jump_in_function_p (f);
445
446 /* Now scan the loops, last ones first, since this means inner ones are done
447 before outer ones. */
448 for (i = max_loop_num - 1; i >= 0; i--)
449 {
450 struct loop *loop = &loops->array[i];
451
452 if (! loop->invalid && loop->end)
453 scan_loop (loop, flags);
454 }
455
456 /* If there were lexical blocks inside the loop, they have been
457 replicated. We will now have more than one NOTE_INSN_BLOCK_BEG
458 and NOTE_INSN_BLOCK_END for each such block. We must duplicate
459 the BLOCKs as well. */
460 if (write_symbols != NO_DEBUG)
461 reorder_blocks ();
462
463 end_alias_analysis ();
464
465 /* Clean up. */
466 free (uid_luid);
467 free (uid_loop);
468 free (loops_info);
469 free (loops->array);
470 }
471 \f
472 /* Returns the next insn, in execution order, after INSN. START and
473 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
474 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
475 insn-stream; it is used with loops that are entered near the
476 bottom. */
477
478 static rtx
479 next_insn_in_loop (loop, insn)
480 const struct loop *loop;
481 rtx insn;
482 {
483 insn = NEXT_INSN (insn);
484
485 if (insn == loop->end)
486 {
487 if (loop->top)
488 /* Go to the top of the loop, and continue there. */
489 insn = loop->top;
490 else
491 /* We're done. */
492 insn = NULL_RTX;
493 }
494
495 if (insn == loop->scan_start)
496 /* We're done. */
497 insn = NULL_RTX;
498
499 return insn;
500 }
501
502 /* Optimize one loop described by LOOP. */
503
504 /* ??? Could also move memory writes out of loops if the destination address
505 is invariant, the source is invariant, the memory write is not volatile,
506 and if we can prove that no read inside the loop can read this address
507 before the write occurs. If there is a read of this address after the
508 write, then we can also mark the memory read as invariant. */
509
510 static void
511 scan_loop (loop, flags)
512 struct loop *loop;
513 int flags;
514 {
515 struct loop_info *loop_info = LOOP_INFO (loop);
516 struct loop_regs *regs = LOOP_REGS (loop);
517 register int i;
518 rtx loop_start = loop->start;
519 rtx loop_end = loop->end;
520 rtx p;
521 /* 1 if we are scanning insns that could be executed zero times. */
522 int maybe_never = 0;
523 /* 1 if we are scanning insns that might never be executed
524 due to a subroutine call which might exit before they are reached. */
525 int call_passed = 0;
526 /* Jump insn that enters the loop, or 0 if control drops in. */
527 rtx loop_entry_jump = 0;
528 /* Number of insns in the loop. */
529 int insn_count;
530 int tem;
531 rtx temp, update_start, update_end;
532 /* The SET from an insn, if it is the only SET in the insn. */
533 rtx set, set1;
534 /* Chain describing insns movable in current loop. */
535 struct loop_movables *movables = LOOP_MOVABLES (loop);
536 /* Ratio of extra register life span we can justify
537 for saving an instruction. More if loop doesn't call subroutines
538 since in that case saving an insn makes more difference
539 and more registers are available. */
540 int threshold;
541 /* Nonzero if we are scanning instructions in a sub-loop. */
542 int loop_depth = 0;
543
544 loop->top = 0;
545
546 movables->head = 0;
547 movables->last = 0;
548 movables->num = 0;
549
550 /* Determine whether this loop starts with a jump down to a test at
551 the end. This will occur for a small number of loops with a test
552 that is too complex to duplicate in front of the loop.
553
554 We search for the first insn or label in the loop, skipping NOTEs.
555 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
556 (because we might have a loop executed only once that contains a
557 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
558 (in case we have a degenerate loop).
559
560 Note that if we mistakenly think that a loop is entered at the top
561 when, in fact, it is entered at the exit test, the only effect will be
562 slightly poorer optimization. Making the opposite error can generate
563 incorrect code. Since very few loops now start with a jump to the
564 exit test, the code here to detect that case is very conservative. */
565
566 for (p = NEXT_INSN (loop_start);
567 p != loop_end
568 && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
569 && (GET_CODE (p) != NOTE
570 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
571 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
572 p = NEXT_INSN (p))
573 ;
574
575 loop->scan_start = p;
576
577 /* If loop end is the end of the current function, then emit a
578 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
579 note insn. This is the position we use when sinking insns out of
580 the loop. */
581 if (NEXT_INSN (loop->end) != 0)
582 loop->sink = NEXT_INSN (loop->end);
583 else
584 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
585
586 /* Set up variables describing this loop. */
587 prescan_loop (loop);
588 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
589
590 /* If loop has a jump before the first label,
591 the true entry is the target of that jump.
592 Start scan from there.
593 But record in LOOP->TOP the place where the end-test jumps
594 back to so we can scan that after the end of the loop. */
595 if (GET_CODE (p) == JUMP_INSN)
596 {
597 loop_entry_jump = p;
598
599 /* Loop entry must be unconditional jump (and not a RETURN) */
600 if (any_uncondjump_p (p)
601 && JUMP_LABEL (p) != 0
602 /* Check to see whether the jump actually
603 jumps out of the loop (meaning it's no loop).
604 This case can happen for things like
605 do {..} while (0). If this label was generated previously
606 by loop, we can't tell anything about it and have to reject
607 the loop. */
608 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
609 {
610 loop->top = next_label (loop->scan_start);
611 loop->scan_start = JUMP_LABEL (p);
612 }
613 }
614
615 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
616 as required by loop_reg_used_before_p. So skip such loops. (This
617 test may never be true, but it's best to play it safe.)
618
619 Also, skip loops where we do not start scanning at a label. This
620 test also rejects loops starting with a JUMP_INSN that failed the
621 test above. */
622
623 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
624 || GET_CODE (loop->scan_start) != CODE_LABEL)
625 {
626 if (loop_dump_stream)
627 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
628 INSN_UID (loop_start), INSN_UID (loop_end));
629 return;
630 }
631
632 /* Allocate extra space for REGs that might be created by load_mems.
633 We allocate a little extra slop as well, in the hopes that we
634 won't have to reallocate the regs array. */
635 loop_regs_scan (loop, loop_info->mems_idx + 16, &insn_count);
636
637 if (loop_dump_stream)
638 {
639 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
640 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
641 if (loop->cont)
642 fprintf (loop_dump_stream, "Continue at insn %d.\n",
643 INSN_UID (loop->cont));
644 }
645
646 /* Scan through the loop finding insns that are safe to move.
647 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
648 this reg will be considered invariant for subsequent insns.
649 We consider whether subsequent insns use the reg
650 in deciding whether it is worth actually moving.
651
652 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
653 and therefore it is possible that the insns we are scanning
654 would never be executed. At such times, we must make sure
655 that it is safe to execute the insn once instead of zero times.
656 When MAYBE_NEVER is 0, all insns will be executed at least once
657 so that is not a problem. */
658
659 for (p = next_insn_in_loop (loop, loop->scan_start);
660 p != NULL_RTX;
661 p = next_insn_in_loop (loop, p))
662 {
663 if (GET_CODE (p) == INSN
664 && (set = single_set (p))
665 && GET_CODE (SET_DEST (set)) == REG
666 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
667 {
668 int tem1 = 0;
669 int tem2 = 0;
670 int move_insn = 0;
671 rtx src = SET_SRC (set);
672 rtx dependencies = 0;
673
674 /* Figure out what to use as a source of this insn. If a REG_EQUIV
675 note is given or if a REG_EQUAL note with a constant operand is
676 specified, use it as the source and mark that we should move
677 this insn by calling emit_move_insn rather that duplicating the
678 insn.
679
680 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
681 is present. */
682 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
683 if (temp)
684 src = XEXP (temp, 0), move_insn = 1;
685 else
686 {
687 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
688 if (temp && CONSTANT_P (XEXP (temp, 0)))
689 src = XEXP (temp, 0), move_insn = 1;
690 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
691 {
692 src = XEXP (temp, 0);
693 /* A libcall block can use regs that don't appear in
694 the equivalent expression. To move the libcall,
695 we must move those regs too. */
696 dependencies = libcall_other_reg (p, src);
697 }
698 }
699
700 /* Don't try to optimize a register that was made
701 by loop-optimization for an inner loop.
702 We don't know its life-span, so we can't compute the benefit. */
703 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
704 ;
705 else if (/* The register is used in basic blocks other
706 than the one where it is set (meaning that
707 something after this point in the loop might
708 depend on its value before the set). */
709 ! reg_in_basic_block_p (p, SET_DEST (set))
710 /* And the set is not guaranteed to be executed one
711 the loop starts, or the value before the set is
712 needed before the set occurs...
713
714 ??? Note we have quadratic behaviour here, mitigated
715 by the fact that the previous test will often fail for
716 large loops. Rather than re-scanning the entire loop
717 each time for register usage, we should build tables
718 of the register usage and use them here instead. */
719 && (maybe_never
720 || loop_reg_used_before_p (loop, set, p)))
721 /* It is unsafe to move the set.
722
723 This code used to consider it OK to move a set of a variable
724 which was not created by the user and not used in an exit test.
725 That behavior is incorrect and was removed. */
726 ;
727 else if ((tem = loop_invariant_p (loop, src))
728 && (dependencies == 0
729 || (tem2 = loop_invariant_p (loop, dependencies)) != 0)
730 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
731 || (tem1
732 = consec_sets_invariant_p
733 (loop, SET_DEST (set),
734 regs->array[REGNO (SET_DEST (set))].set_in_loop,
735 p)))
736 /* If the insn can cause a trap (such as divide by zero),
737 can't move it unless it's guaranteed to be executed
738 once loop is entered. Even a function call might
739 prevent the trap insn from being reached
740 (since it might exit!) */
741 && ! ((maybe_never || call_passed)
742 && may_trap_p (src)))
743 {
744 register struct movable *m;
745 register int regno = REGNO (SET_DEST (set));
746
747 /* A potential lossage is where we have a case where two insns
748 can be combined as long as they are both in the loop, but
749 we move one of them outside the loop. For large loops,
750 this can lose. The most common case of this is the address
751 of a function being called.
752
753 Therefore, if this register is marked as being used exactly
754 once if we are in a loop with calls (a "large loop"), see if
755 we can replace the usage of this register with the source
756 of this SET. If we can, delete this insn.
757
758 Don't do this if P has a REG_RETVAL note or if we have
759 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
760
761 if (loop_info->has_call
762 && regs->array[regno].single_usage != 0
763 && regs->array[regno].single_usage != const0_rtx
764 && REGNO_FIRST_UID (regno) == INSN_UID (p)
765 && (REGNO_LAST_UID (regno)
766 == INSN_UID (regs->array[regno].single_usage))
767 && regs->array[regno].set_in_loop == 1
768 && ! side_effects_p (SET_SRC (set))
769 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
770 && (! SMALL_REGISTER_CLASSES
771 || (! (GET_CODE (SET_SRC (set)) == REG
772 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
773 /* This test is not redundant; SET_SRC (set) might be
774 a call-clobbered register and the life of REGNO
775 might span a call. */
776 && ! modified_between_p (SET_SRC (set), p,
777 regs->array[regno].single_usage)
778 && no_labels_between_p (p, regs->array[regno].single_usage)
779 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
780 regs->array[regno].single_usage))
781 {
782 /* Replace any usage in a REG_EQUAL note. Must copy the
783 new source, so that we don't get rtx sharing between the
784 SET_SOURCE and REG_NOTES of insn p. */
785 REG_NOTES (regs->array[regno].single_usage)
786 = replace_rtx (REG_NOTES (regs->array[regno].single_usage),
787 SET_DEST (set), copy_rtx (SET_SRC (set)));
788
789 PUT_CODE (p, NOTE);
790 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
791 NOTE_SOURCE_FILE (p) = 0;
792 regs->array[regno].set_in_loop = 0;
793 continue;
794 }
795
796 m = (struct movable *) xmalloc (sizeof (struct movable));
797 m->next = 0;
798 m->insn = p;
799 m->set_src = src;
800 m->dependencies = dependencies;
801 m->set_dest = SET_DEST (set);
802 m->force = 0;
803 m->consec = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
804 m->done = 0;
805 m->forces = 0;
806 m->partial = 0;
807 m->move_insn = move_insn;
808 m->move_insn_first = 0;
809 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
810 m->savemode = VOIDmode;
811 m->regno = regno;
812 /* Set M->cond if either loop_invariant_p
813 or consec_sets_invariant_p returned 2
814 (only conditionally invariant). */
815 m->cond = ((tem | tem1 | tem2) > 1);
816 m->global = LOOP_REG_GLOBAL_P (loop, regno);
817 m->match = 0;
818 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
819 m->savings = regs->array[regno].n_times_set;
820 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
821 m->savings += libcall_benefit (p);
822 regs->array[regno].set_in_loop = move_insn ? -2 : -1;
823 /* Add M to the end of the chain MOVABLES. */
824 loop_movables_add (movables, m);
825
826 if (m->consec > 0)
827 {
828 /* It is possible for the first instruction to have a
829 REG_EQUAL note but a non-invariant SET_SRC, so we must
830 remember the status of the first instruction in case
831 the last instruction doesn't have a REG_EQUAL note. */
832 m->move_insn_first = m->move_insn;
833
834 /* Skip this insn, not checking REG_LIBCALL notes. */
835 p = next_nonnote_insn (p);
836 /* Skip the consecutive insns, if there are any. */
837 p = skip_consec_insns (p, m->consec);
838 /* Back up to the last insn of the consecutive group. */
839 p = prev_nonnote_insn (p);
840
841 /* We must now reset m->move_insn, m->is_equiv, and possibly
842 m->set_src to correspond to the effects of all the
843 insns. */
844 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
845 if (temp)
846 m->set_src = XEXP (temp, 0), m->move_insn = 1;
847 else
848 {
849 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
850 if (temp && CONSTANT_P (XEXP (temp, 0)))
851 m->set_src = XEXP (temp, 0), m->move_insn = 1;
852 else
853 m->move_insn = 0;
854
855 }
856 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
857 }
858 }
859 /* If this register is always set within a STRICT_LOW_PART
860 or set to zero, then its high bytes are constant.
861 So clear them outside the loop and within the loop
862 just load the low bytes.
863 We must check that the machine has an instruction to do so.
864 Also, if the value loaded into the register
865 depends on the same register, this cannot be done. */
866 else if (SET_SRC (set) == const0_rtx
867 && GET_CODE (NEXT_INSN (p)) == INSN
868 && (set1 = single_set (NEXT_INSN (p)))
869 && GET_CODE (set1) == SET
870 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
871 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
872 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
873 == SET_DEST (set))
874 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
875 {
876 register int regno = REGNO (SET_DEST (set));
877 if (regs->array[regno].set_in_loop == 2)
878 {
879 register struct movable *m;
880 m = (struct movable *) xmalloc (sizeof (struct movable));
881 m->next = 0;
882 m->insn = p;
883 m->set_dest = SET_DEST (set);
884 m->dependencies = 0;
885 m->force = 0;
886 m->consec = 0;
887 m->done = 0;
888 m->forces = 0;
889 m->move_insn = 0;
890 m->move_insn_first = 0;
891 m->partial = 1;
892 /* If the insn may not be executed on some cycles,
893 we can't clear the whole reg; clear just high part.
894 Not even if the reg is used only within this loop.
895 Consider this:
896 while (1)
897 while (s != t) {
898 if (foo ()) x = *s;
899 use (x);
900 }
901 Clearing x before the inner loop could clobber a value
902 being saved from the last time around the outer loop.
903 However, if the reg is not used outside this loop
904 and all uses of the register are in the same
905 basic block as the store, there is no problem.
906
907 If this insn was made by loop, we don't know its
908 INSN_LUID and hence must make a conservative
909 assumption. */
910 m->global = (INSN_UID (p) >= max_uid_for_loop
911 || LOOP_REG_GLOBAL_P (loop, regno)
912 || (labels_in_range_p
913 (p, REGNO_FIRST_LUID (regno))));
914 if (maybe_never && m->global)
915 m->savemode = GET_MODE (SET_SRC (set1));
916 else
917 m->savemode = VOIDmode;
918 m->regno = regno;
919 m->cond = 0;
920 m->match = 0;
921 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
922 m->savings = 1;
923 regs->array[regno].set_in_loop = -1;
924 /* Add M to the end of the chain MOVABLES. */
925 loop_movables_add (movables, m);
926 }
927 }
928 }
929 /* Past a call insn, we get to insns which might not be executed
930 because the call might exit. This matters for insns that trap.
931 Constant and pure call insns always return, so they don't count. */
932 else if (GET_CODE (p) == CALL_INSN && ! CONST_CALL_P (p))
933 call_passed = 1;
934 /* Past a label or a jump, we get to insns for which we
935 can't count on whether or how many times they will be
936 executed during each iteration. Therefore, we can
937 only move out sets of trivial variables
938 (those not used after the loop). */
939 /* Similar code appears twice in strength_reduce. */
940 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
941 /* If we enter the loop in the middle, and scan around to the
942 beginning, don't set maybe_never for that. This must be an
943 unconditional jump, otherwise the code at the top of the
944 loop might never be executed. Unconditional jumps are
945 followed a by barrier then loop end. */
946 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
947 && NEXT_INSN (NEXT_INSN (p)) == loop_end
948 && any_uncondjump_p (p)))
949 maybe_never = 1;
950 else if (GET_CODE (p) == NOTE)
951 {
952 /* At the virtual top of a converted loop, insns are again known to
953 be executed: logically, the loop begins here even though the exit
954 code has been duplicated. */
955 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
956 maybe_never = call_passed = 0;
957 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
958 loop_depth++;
959 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
960 loop_depth--;
961 }
962 }
963
964 /* If one movable subsumes another, ignore that other. */
965
966 ignore_some_movables (movables);
967
968 /* For each movable insn, see if the reg that it loads
969 leads when it dies right into another conditionally movable insn.
970 If so, record that the second insn "forces" the first one,
971 since the second can be moved only if the first is. */
972
973 force_movables (movables);
974
975 /* See if there are multiple movable insns that load the same value.
976 If there are, make all but the first point at the first one
977 through the `match' field, and add the priorities of them
978 all together as the priority of the first. */
979
980 combine_movables (movables, regs);
981
982 /* Now consider each movable insn to decide whether it is worth moving.
983 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
984
985 Generally this increases code size, so do not move moveables when
986 optimizing for code size. */
987
988 if (! optimize_size)
989 move_movables (loop, movables, threshold, insn_count);
990
991 /* Now candidates that still are negative are those not moved.
992 Change regs->array[I].set_in_loop to indicate that those are not actually
993 invariant. */
994 for (i = 0; i < regs->num; i++)
995 if (regs->array[i].set_in_loop < 0)
996 regs->array[i].set_in_loop = regs->array[i].n_times_set;
997
998 /* Now that we've moved some things out of the loop, we might be able to
999 hoist even more memory references. */
1000 load_mems (loop);
1001
1002 /* Recalculate regs->array if load_mems has created new registers. */
1003 if (max_reg_num () > regs->num)
1004 loop_regs_scan (loop, 0, &insn_count);
1005
1006 for (update_start = loop_start;
1007 PREV_INSN (update_start)
1008 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1009 update_start = PREV_INSN (update_start))
1010 ;
1011 update_end = NEXT_INSN (loop_end);
1012
1013 reg_scan_update (update_start, update_end, loop_max_reg);
1014 loop_max_reg = max_reg_num ();
1015
1016 if (flag_strength_reduce)
1017 {
1018 if (update_end && GET_CODE (update_end) == CODE_LABEL)
1019 /* Ensure our label doesn't go away. */
1020 LABEL_NUSES (update_end)++;
1021
1022 strength_reduce (loop, insn_count, flags);
1023
1024 reg_scan_update (update_start, update_end, loop_max_reg);
1025 loop_max_reg = max_reg_num ();
1026
1027 if (update_end && GET_CODE (update_end) == CODE_LABEL
1028 && --LABEL_NUSES (update_end) == 0)
1029 delete_insn (update_end);
1030 }
1031
1032
1033 /* The movable information is required for strength reduction. */
1034 loop_movables_free (movables);
1035
1036 free (regs->array);
1037 regs->array = 0;
1038 regs->num = 0;
1039 }
1040 \f
1041 /* Add elements to *OUTPUT to record all the pseudo-regs
1042 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1043
1044 void
1045 record_excess_regs (in_this, not_in_this, output)
1046 rtx in_this, not_in_this;
1047 rtx *output;
1048 {
1049 enum rtx_code code;
1050 const char *fmt;
1051 int i;
1052
1053 code = GET_CODE (in_this);
1054
1055 switch (code)
1056 {
1057 case PC:
1058 case CC0:
1059 case CONST_INT:
1060 case CONST_DOUBLE:
1061 case CONST:
1062 case SYMBOL_REF:
1063 case LABEL_REF:
1064 return;
1065
1066 case REG:
1067 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1068 && ! reg_mentioned_p (in_this, not_in_this))
1069 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1070 return;
1071
1072 default:
1073 break;
1074 }
1075
1076 fmt = GET_RTX_FORMAT (code);
1077 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1078 {
1079 int j;
1080
1081 switch (fmt[i])
1082 {
1083 case 'E':
1084 for (j = 0; j < XVECLEN (in_this, i); j++)
1085 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1086 break;
1087
1088 case 'e':
1089 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1090 break;
1091 }
1092 }
1093 }
1094 \f
1095 /* Check what regs are referred to in the libcall block ending with INSN,
1096 aside from those mentioned in the equivalent value.
1097 If there are none, return 0.
1098 If there are one or more, return an EXPR_LIST containing all of them. */
1099
1100 rtx
1101 libcall_other_reg (insn, equiv)
1102 rtx insn, equiv;
1103 {
1104 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1105 rtx p = XEXP (note, 0);
1106 rtx output = 0;
1107
1108 /* First, find all the regs used in the libcall block
1109 that are not mentioned as inputs to the result. */
1110
1111 while (p != insn)
1112 {
1113 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1114 || GET_CODE (p) == CALL_INSN)
1115 record_excess_regs (PATTERN (p), equiv, &output);
1116 p = NEXT_INSN (p);
1117 }
1118
1119 return output;
1120 }
1121 \f
1122 /* Return 1 if all uses of REG
1123 are between INSN and the end of the basic block. */
1124
1125 static int
1126 reg_in_basic_block_p (insn, reg)
1127 rtx insn, reg;
1128 {
1129 int regno = REGNO (reg);
1130 rtx p;
1131
1132 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1133 return 0;
1134
1135 /* Search this basic block for the already recorded last use of the reg. */
1136 for (p = insn; p; p = NEXT_INSN (p))
1137 {
1138 switch (GET_CODE (p))
1139 {
1140 case NOTE:
1141 break;
1142
1143 case INSN:
1144 case CALL_INSN:
1145 /* Ordinary insn: if this is the last use, we win. */
1146 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1147 return 1;
1148 break;
1149
1150 case JUMP_INSN:
1151 /* Jump insn: if this is the last use, we win. */
1152 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1153 return 1;
1154 /* Otherwise, it's the end of the basic block, so we lose. */
1155 return 0;
1156
1157 case CODE_LABEL:
1158 case BARRIER:
1159 /* It's the end of the basic block, so we lose. */
1160 return 0;
1161
1162 default:
1163 break;
1164 }
1165 }
1166
1167 /* The "last use" that was recorded can't be found after the first
1168 use. This can happen when the last use was deleted while
1169 processing an inner loop, this inner loop was then completely
1170 unrolled, and the outer loop is always exited after the inner loop,
1171 so that everything after the first use becomes a single basic block. */
1172 return 1;
1173 }
1174 \f
1175 /* Compute the benefit of eliminating the insns in the block whose
1176 last insn is LAST. This may be a group of insns used to compute a
1177 value directly or can contain a library call. */
1178
1179 static int
1180 libcall_benefit (last)
1181 rtx last;
1182 {
1183 rtx insn;
1184 int benefit = 0;
1185
1186 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1187 insn != last; insn = NEXT_INSN (insn))
1188 {
1189 if (GET_CODE (insn) == CALL_INSN)
1190 benefit += 10; /* Assume at least this many insns in a library
1191 routine. */
1192 else if (GET_CODE (insn) == INSN
1193 && GET_CODE (PATTERN (insn)) != USE
1194 && GET_CODE (PATTERN (insn)) != CLOBBER)
1195 benefit++;
1196 }
1197
1198 return benefit;
1199 }
1200 \f
1201 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1202
1203 static rtx
1204 skip_consec_insns (insn, count)
1205 rtx insn;
1206 int count;
1207 {
1208 for (; count > 0; count--)
1209 {
1210 rtx temp;
1211
1212 /* If first insn of libcall sequence, skip to end. */
1213 /* Do this at start of loop, since INSN is guaranteed to
1214 be an insn here. */
1215 if (GET_CODE (insn) != NOTE
1216 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1217 insn = XEXP (temp, 0);
1218
1219 do
1220 insn = NEXT_INSN (insn);
1221 while (GET_CODE (insn) == NOTE);
1222 }
1223
1224 return insn;
1225 }
1226
1227 /* Ignore any movable whose insn falls within a libcall
1228 which is part of another movable.
1229 We make use of the fact that the movable for the libcall value
1230 was made later and so appears later on the chain. */
1231
1232 static void
1233 ignore_some_movables (movables)
1234 struct loop_movables *movables;
1235 {
1236 register struct movable *m, *m1;
1237
1238 for (m = movables->head; m; m = m->next)
1239 {
1240 /* Is this a movable for the value of a libcall? */
1241 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1242 if (note)
1243 {
1244 rtx insn;
1245 /* Check for earlier movables inside that range,
1246 and mark them invalid. We cannot use LUIDs here because
1247 insns created by loop.c for prior loops don't have LUIDs.
1248 Rather than reject all such insns from movables, we just
1249 explicitly check each insn in the libcall (since invariant
1250 libcalls aren't that common). */
1251 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1252 for (m1 = movables->head; m1 != m; m1 = m1->next)
1253 if (m1->insn == insn)
1254 m1->done = 1;
1255 }
1256 }
1257 }
1258
1259 /* For each movable insn, see if the reg that it loads
1260 leads when it dies right into another conditionally movable insn.
1261 If so, record that the second insn "forces" the first one,
1262 since the second can be moved only if the first is. */
1263
1264 static void
1265 force_movables (movables)
1266 struct loop_movables *movables;
1267 {
1268 register struct movable *m, *m1;
1269 for (m1 = movables->head; m1; m1 = m1->next)
1270 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1271 if (!m1->partial && !m1->done)
1272 {
1273 int regno = m1->regno;
1274 for (m = m1->next; m; m = m->next)
1275 /* ??? Could this be a bug? What if CSE caused the
1276 register of M1 to be used after this insn?
1277 Since CSE does not update regno_last_uid,
1278 this insn M->insn might not be where it dies.
1279 But very likely this doesn't matter; what matters is
1280 that M's reg is computed from M1's reg. */
1281 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1282 && !m->done)
1283 break;
1284 if (m != 0 && m->set_src == m1->set_dest
1285 /* If m->consec, m->set_src isn't valid. */
1286 && m->consec == 0)
1287 m = 0;
1288
1289 /* Increase the priority of the moving the first insn
1290 since it permits the second to be moved as well. */
1291 if (m != 0)
1292 {
1293 m->forces = m1;
1294 m1->lifetime += m->lifetime;
1295 m1->savings += m->savings;
1296 }
1297 }
1298 }
1299 \f
1300 /* Find invariant expressions that are equal and can be combined into
1301 one register. */
1302
1303 static void
1304 combine_movables (movables, regs)
1305 struct loop_movables *movables;
1306 struct loop_regs *regs;
1307 {
1308 register struct movable *m;
1309 char *matched_regs = (char *) xmalloc (regs->num);
1310 enum machine_mode mode;
1311
1312 /* Regs that are set more than once are not allowed to match
1313 or be matched. I'm no longer sure why not. */
1314 /* Perhaps testing m->consec_sets would be more appropriate here? */
1315
1316 for (m = movables->head; m; m = m->next)
1317 if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1318 && !m->partial)
1319 {
1320 register struct movable *m1;
1321 int regno = m->regno;
1322
1323 memset (matched_regs, 0, regs->num);
1324 matched_regs[regno] = 1;
1325
1326 /* We want later insns to match the first one. Don't make the first
1327 one match any later ones. So start this loop at m->next. */
1328 for (m1 = m->next; m1; m1 = m1->next)
1329 if (m != m1 && m1->match == 0
1330 && regs->array[m1->regno].n_times_set == 1
1331 /* A reg used outside the loop mustn't be eliminated. */
1332 && !m1->global
1333 /* A reg used for zero-extending mustn't be eliminated. */
1334 && !m1->partial
1335 && (matched_regs[m1->regno]
1336 ||
1337 (
1338 /* Can combine regs with different modes loaded from the
1339 same constant only if the modes are the same or
1340 if both are integer modes with M wider or the same
1341 width as M1. The check for integer is redundant, but
1342 safe, since the only case of differing destination
1343 modes with equal sources is when both sources are
1344 VOIDmode, i.e., CONST_INT. */
1345 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1346 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1347 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1348 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1349 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1350 /* See if the source of M1 says it matches M. */
1351 && ((GET_CODE (m1->set_src) == REG
1352 && matched_regs[REGNO (m1->set_src)])
1353 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1354 movables, regs))))
1355 && ((m->dependencies == m1->dependencies)
1356 || rtx_equal_p (m->dependencies, m1->dependencies)))
1357 {
1358 m->lifetime += m1->lifetime;
1359 m->savings += m1->savings;
1360 m1->done = 1;
1361 m1->match = m;
1362 matched_regs[m1->regno] = 1;
1363 }
1364 }
1365
1366 /* Now combine the regs used for zero-extension.
1367 This can be done for those not marked `global'
1368 provided their lives don't overlap. */
1369
1370 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1371 mode = GET_MODE_WIDER_MODE (mode))
1372 {
1373 register struct movable *m0 = 0;
1374
1375 /* Combine all the registers for extension from mode MODE.
1376 Don't combine any that are used outside this loop. */
1377 for (m = movables->head; m; m = m->next)
1378 if (m->partial && ! m->global
1379 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1380 {
1381 register struct movable *m1;
1382 int first = REGNO_FIRST_LUID (m->regno);
1383 int last = REGNO_LAST_LUID (m->regno);
1384
1385 if (m0 == 0)
1386 {
1387 /* First one: don't check for overlap, just record it. */
1388 m0 = m;
1389 continue;
1390 }
1391
1392 /* Make sure they extend to the same mode.
1393 (Almost always true.) */
1394 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1395 continue;
1396
1397 /* We already have one: check for overlap with those
1398 already combined together. */
1399 for (m1 = movables->head; m1 != m; m1 = m1->next)
1400 if (m1 == m0 || (m1->partial && m1->match == m0))
1401 if (! (REGNO_FIRST_LUID (m1->regno) > last
1402 || REGNO_LAST_LUID (m1->regno) < first))
1403 goto overlap;
1404
1405 /* No overlap: we can combine this with the others. */
1406 m0->lifetime += m->lifetime;
1407 m0->savings += m->savings;
1408 m->done = 1;
1409 m->match = m0;
1410
1411 overlap:
1412 ;
1413 }
1414 }
1415
1416 /* Clean up. */
1417 free (matched_regs);
1418 }
1419 \f
1420 /* Return 1 if regs X and Y will become the same if moved. */
1421
1422 static int
1423 regs_match_p (x, y, movables)
1424 rtx x, y;
1425 struct loop_movables *movables;
1426 {
1427 unsigned int xn = REGNO (x);
1428 unsigned int yn = REGNO (y);
1429 struct movable *mx, *my;
1430
1431 for (mx = movables->head; mx; mx = mx->next)
1432 if (mx->regno == xn)
1433 break;
1434
1435 for (my = movables->head; my; my = my->next)
1436 if (my->regno == yn)
1437 break;
1438
1439 return (mx && my
1440 && ((mx->match == my->match && mx->match != 0)
1441 || mx->match == my
1442 || mx == my->match));
1443 }
1444
1445 /* Return 1 if X and Y are identical-looking rtx's.
1446 This is the Lisp function EQUAL for rtx arguments.
1447
1448 If two registers are matching movables or a movable register and an
1449 equivalent constant, consider them equal. */
1450
1451 static int
1452 rtx_equal_for_loop_p (x, y, movables, regs)
1453 rtx x, y;
1454 struct loop_movables *movables;
1455 struct loop_regs *regs;
1456 {
1457 register int i;
1458 register int j;
1459 register struct movable *m;
1460 register enum rtx_code code;
1461 register const char *fmt;
1462
1463 if (x == y)
1464 return 1;
1465 if (x == 0 || y == 0)
1466 return 0;
1467
1468 code = GET_CODE (x);
1469
1470 /* If we have a register and a constant, they may sometimes be
1471 equal. */
1472 if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
1473 && CONSTANT_P (y))
1474 {
1475 for (m = movables->head; m; m = m->next)
1476 if (m->move_insn && m->regno == REGNO (x)
1477 && rtx_equal_p (m->set_src, y))
1478 return 1;
1479 }
1480 else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
1481 && CONSTANT_P (x))
1482 {
1483 for (m = movables->head; m; m = m->next)
1484 if (m->move_insn && m->regno == REGNO (y)
1485 && rtx_equal_p (m->set_src, x))
1486 return 1;
1487 }
1488
1489 /* Otherwise, rtx's of different codes cannot be equal. */
1490 if (code != GET_CODE (y))
1491 return 0;
1492
1493 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1494 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1495
1496 if (GET_MODE (x) != GET_MODE (y))
1497 return 0;
1498
1499 /* These three types of rtx's can be compared nonrecursively. */
1500 if (code == REG)
1501 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1502
1503 if (code == LABEL_REF)
1504 return XEXP (x, 0) == XEXP (y, 0);
1505 if (code == SYMBOL_REF)
1506 return XSTR (x, 0) == XSTR (y, 0);
1507
1508 /* Compare the elements. If any pair of corresponding elements
1509 fail to match, return 0 for the whole things. */
1510
1511 fmt = GET_RTX_FORMAT (code);
1512 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1513 {
1514 switch (fmt[i])
1515 {
1516 case 'w':
1517 if (XWINT (x, i) != XWINT (y, i))
1518 return 0;
1519 break;
1520
1521 case 'i':
1522 if (XINT (x, i) != XINT (y, i))
1523 return 0;
1524 break;
1525
1526 case 'E':
1527 /* Two vectors must have the same length. */
1528 if (XVECLEN (x, i) != XVECLEN (y, i))
1529 return 0;
1530
1531 /* And the corresponding elements must match. */
1532 for (j = 0; j < XVECLEN (x, i); j++)
1533 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
1534 movables, regs) == 0)
1535 return 0;
1536 break;
1537
1538 case 'e':
1539 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
1540 == 0)
1541 return 0;
1542 break;
1543
1544 case 's':
1545 if (strcmp (XSTR (x, i), XSTR (y, i)))
1546 return 0;
1547 break;
1548
1549 case 'u':
1550 /* These are just backpointers, so they don't matter. */
1551 break;
1552
1553 case '0':
1554 break;
1555
1556 /* It is believed that rtx's at this level will never
1557 contain anything but integers and other rtx's,
1558 except for within LABEL_REFs and SYMBOL_REFs. */
1559 default:
1560 abort ();
1561 }
1562 }
1563 return 1;
1564 }
1565 \f
1566 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1567 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1568 references is incremented once for each added note. */
1569
1570 static void
1571 add_label_notes (x, insns)
1572 rtx x;
1573 rtx insns;
1574 {
1575 enum rtx_code code = GET_CODE (x);
1576 int i, j;
1577 const char *fmt;
1578 rtx insn;
1579
1580 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1581 {
1582 /* This code used to ignore labels that referred to dispatch tables to
1583 avoid flow generating (slighly) worse code.
1584
1585 We no longer ignore such label references (see LABEL_REF handling in
1586 mark_jump_label for additional information). */
1587 for (insn = insns; insn; insn = NEXT_INSN (insn))
1588 if (reg_mentioned_p (XEXP (x, 0), insn))
1589 {
1590 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
1591 REG_NOTES (insn));
1592 if (LABEL_P (XEXP (x, 0)))
1593 LABEL_NUSES (XEXP (x, 0))++;
1594 }
1595 }
1596
1597 fmt = GET_RTX_FORMAT (code);
1598 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1599 {
1600 if (fmt[i] == 'e')
1601 add_label_notes (XEXP (x, i), insns);
1602 else if (fmt[i] == 'E')
1603 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1604 add_label_notes (XVECEXP (x, i, j), insns);
1605 }
1606 }
1607 \f
1608 /* Scan MOVABLES, and move the insns that deserve to be moved.
1609 If two matching movables are combined, replace one reg with the
1610 other throughout. */
1611
1612 static void
1613 move_movables (loop, movables, threshold, insn_count)
1614 struct loop *loop;
1615 struct loop_movables *movables;
1616 int threshold;
1617 int insn_count;
1618 {
1619 struct loop_regs *regs = LOOP_REGS (loop);
1620 int nregs = regs->num;
1621 rtx new_start = 0;
1622 register struct movable *m;
1623 register rtx p;
1624 rtx loop_start = loop->start;
1625 rtx loop_end = loop->end;
1626 /* Map of pseudo-register replacements to handle combining
1627 when we move several insns that load the same value
1628 into different pseudo-registers. */
1629 rtx *reg_map = (rtx *) xcalloc (nregs, sizeof (rtx));
1630 char *already_moved = (char *) xcalloc (nregs, sizeof (char));
1631
1632 movables->num = 0;
1633
1634 for (m = movables->head; m; m = m->next)
1635 {
1636 /* Describe this movable insn. */
1637
1638 if (loop_dump_stream)
1639 {
1640 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1641 INSN_UID (m->insn), m->regno, m->lifetime);
1642 if (m->consec > 0)
1643 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1644 if (m->cond)
1645 fprintf (loop_dump_stream, "cond ");
1646 if (m->force)
1647 fprintf (loop_dump_stream, "force ");
1648 if (m->global)
1649 fprintf (loop_dump_stream, "global ");
1650 if (m->done)
1651 fprintf (loop_dump_stream, "done ");
1652 if (m->move_insn)
1653 fprintf (loop_dump_stream, "move-insn ");
1654 if (m->match)
1655 fprintf (loop_dump_stream, "matches %d ",
1656 INSN_UID (m->match->insn));
1657 if (m->forces)
1658 fprintf (loop_dump_stream, "forces %d ",
1659 INSN_UID (m->forces->insn));
1660 }
1661
1662 /* Count movables. Value used in heuristics in strength_reduce. */
1663 movables->num++;
1664
1665 /* Ignore the insn if it's already done (it matched something else).
1666 Otherwise, see if it is now safe to move. */
1667
1668 if (!m->done
1669 && (! m->cond
1670 || (1 == loop_invariant_p (loop, m->set_src)
1671 && (m->dependencies == 0
1672 || 1 == loop_invariant_p (loop, m->dependencies))
1673 && (m->consec == 0
1674 || 1 == consec_sets_invariant_p (loop, m->set_dest,
1675 m->consec + 1,
1676 m->insn))))
1677 && (! m->forces || m->forces->done))
1678 {
1679 register int regno;
1680 register rtx p;
1681 int savings = m->savings;
1682
1683 /* We have an insn that is safe to move.
1684 Compute its desirability. */
1685
1686 p = m->insn;
1687 regno = m->regno;
1688
1689 if (loop_dump_stream)
1690 fprintf (loop_dump_stream, "savings %d ", savings);
1691
1692 if (regs->array[regno].moved_once && loop_dump_stream)
1693 fprintf (loop_dump_stream, "halved since already moved ");
1694
1695 /* An insn MUST be moved if we already moved something else
1696 which is safe only if this one is moved too: that is,
1697 if already_moved[REGNO] is nonzero. */
1698
1699 /* An insn is desirable to move if the new lifetime of the
1700 register is no more than THRESHOLD times the old lifetime.
1701 If it's not desirable, it means the loop is so big
1702 that moving won't speed things up much,
1703 and it is liable to make register usage worse. */
1704
1705 /* It is also desirable to move if it can be moved at no
1706 extra cost because something else was already moved. */
1707
1708 if (already_moved[regno]
1709 || flag_move_all_movables
1710 || (threshold * savings * m->lifetime) >=
1711 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
1712 || (m->forces && m->forces->done
1713 && regs->array[m->forces->regno].n_times_set == 1))
1714 {
1715 int count;
1716 register struct movable *m1;
1717 rtx first = NULL_RTX;
1718
1719 /* Now move the insns that set the reg. */
1720
1721 if (m->partial && m->match)
1722 {
1723 rtx newpat, i1;
1724 rtx r1, r2;
1725 /* Find the end of this chain of matching regs.
1726 Thus, we load each reg in the chain from that one reg.
1727 And that reg is loaded with 0 directly,
1728 since it has ->match == 0. */
1729 for (m1 = m; m1->match; m1 = m1->match);
1730 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1731 SET_DEST (PATTERN (m1->insn)));
1732 i1 = loop_insn_hoist (loop, newpat);
1733
1734 /* Mark the moved, invariant reg as being allowed to
1735 share a hard reg with the other matching invariant. */
1736 REG_NOTES (i1) = REG_NOTES (m->insn);
1737 r1 = SET_DEST (PATTERN (m->insn));
1738 r2 = SET_DEST (PATTERN (m1->insn));
1739 regs_may_share
1740 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1741 gen_rtx_EXPR_LIST (VOIDmode, r2,
1742 regs_may_share));
1743 delete_insn (m->insn);
1744
1745 if (new_start == 0)
1746 new_start = i1;
1747
1748 if (loop_dump_stream)
1749 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1750 }
1751 /* If we are to re-generate the item being moved with a
1752 new move insn, first delete what we have and then emit
1753 the move insn before the loop. */
1754 else if (m->move_insn)
1755 {
1756 rtx i1, temp, seq;
1757
1758 for (count = m->consec; count >= 0; count--)
1759 {
1760 /* If this is the first insn of a library call sequence,
1761 skip to the end. */
1762 if (GET_CODE (p) != NOTE
1763 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1764 p = XEXP (temp, 0);
1765
1766 /* If this is the last insn of a libcall sequence, then
1767 delete every insn in the sequence except the last.
1768 The last insn is handled in the normal manner. */
1769 if (GET_CODE (p) != NOTE
1770 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1771 {
1772 temp = XEXP (temp, 0);
1773 while (temp != p)
1774 temp = delete_insn (temp);
1775 }
1776
1777 temp = p;
1778 p = delete_insn (p);
1779
1780 /* simplify_giv_expr expects that it can walk the insns
1781 at m->insn forwards and see this old sequence we are
1782 tossing here. delete_insn does preserve the next
1783 pointers, but when we skip over a NOTE we must fix
1784 it up. Otherwise that code walks into the non-deleted
1785 insn stream. */
1786 while (p && GET_CODE (p) == NOTE)
1787 p = NEXT_INSN (temp) = NEXT_INSN (p);
1788 }
1789
1790 start_sequence ();
1791 emit_move_insn (m->set_dest, m->set_src);
1792 temp = get_insns ();
1793 seq = gen_sequence ();
1794 end_sequence ();
1795
1796 add_label_notes (m->set_src, temp);
1797
1798 i1 = loop_insn_hoist (loop, seq);
1799 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1800 REG_NOTES (i1)
1801 = gen_rtx_EXPR_LIST (m->is_equiv ? REG_EQUIV : REG_EQUAL,
1802 m->set_src, REG_NOTES (i1));
1803
1804 if (loop_dump_stream)
1805 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1806
1807 /* The more regs we move, the less we like moving them. */
1808 threshold -= 3;
1809 }
1810 else
1811 {
1812 for (count = m->consec; count >= 0; count--)
1813 {
1814 rtx i1, temp;
1815
1816 /* If first insn of libcall sequence, skip to end. */
1817 /* Do this at start of loop, since p is guaranteed to
1818 be an insn here. */
1819 if (GET_CODE (p) != NOTE
1820 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1821 p = XEXP (temp, 0);
1822
1823 /* If last insn of libcall sequence, move all
1824 insns except the last before the loop. The last
1825 insn is handled in the normal manner. */
1826 if (GET_CODE (p) != NOTE
1827 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1828 {
1829 rtx fn_address = 0;
1830 rtx fn_reg = 0;
1831 rtx fn_address_insn = 0;
1832
1833 first = 0;
1834 for (temp = XEXP (temp, 0); temp != p;
1835 temp = NEXT_INSN (temp))
1836 {
1837 rtx body;
1838 rtx n;
1839 rtx next;
1840
1841 if (GET_CODE (temp) == NOTE)
1842 continue;
1843
1844 body = PATTERN (temp);
1845
1846 /* Find the next insn after TEMP,
1847 not counting USE or NOTE insns. */
1848 for (next = NEXT_INSN (temp); next != p;
1849 next = NEXT_INSN (next))
1850 if (! (GET_CODE (next) == INSN
1851 && GET_CODE (PATTERN (next)) == USE)
1852 && GET_CODE (next) != NOTE)
1853 break;
1854
1855 /* If that is the call, this may be the insn
1856 that loads the function address.
1857
1858 Extract the function address from the insn
1859 that loads it into a register.
1860 If this insn was cse'd, we get incorrect code.
1861
1862 So emit a new move insn that copies the
1863 function address into the register that the
1864 call insn will use. flow.c will delete any
1865 redundant stores that we have created. */
1866 if (GET_CODE (next) == CALL_INSN
1867 && GET_CODE (body) == SET
1868 && GET_CODE (SET_DEST (body)) == REG
1869 && (n = find_reg_note (temp, REG_EQUAL,
1870 NULL_RTX)))
1871 {
1872 fn_reg = SET_SRC (body);
1873 if (GET_CODE (fn_reg) != REG)
1874 fn_reg = SET_DEST (body);
1875 fn_address = XEXP (n, 0);
1876 fn_address_insn = temp;
1877 }
1878 /* We have the call insn.
1879 If it uses the register we suspect it might,
1880 load it with the correct address directly. */
1881 if (GET_CODE (temp) == CALL_INSN
1882 && fn_address != 0
1883 && reg_referenced_p (fn_reg, body))
1884 emit_insn_after (gen_move_insn (fn_reg,
1885 fn_address),
1886 fn_address_insn);
1887
1888 if (GET_CODE (temp) == CALL_INSN)
1889 {
1890 i1 = emit_call_insn_before (body, loop_start);
1891 /* Because the USAGE information potentially
1892 contains objects other than hard registers
1893 we need to copy it. */
1894 if (CALL_INSN_FUNCTION_USAGE (temp))
1895 CALL_INSN_FUNCTION_USAGE (i1)
1896 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1897 }
1898 else
1899 i1 = loop_insn_hoist (loop, body);
1900 if (first == 0)
1901 first = i1;
1902 if (temp == fn_address_insn)
1903 fn_address_insn = i1;
1904 REG_NOTES (i1) = REG_NOTES (temp);
1905 delete_insn (temp);
1906 }
1907 if (new_start == 0)
1908 new_start = first;
1909 }
1910 if (m->savemode != VOIDmode)
1911 {
1912 /* P sets REG to zero; but we should clear only
1913 the bits that are not covered by the mode
1914 m->savemode. */
1915 rtx reg = m->set_dest;
1916 rtx sequence;
1917 rtx tem;
1918
1919 start_sequence ();
1920 tem = expand_binop
1921 (GET_MODE (reg), and_optab, reg,
1922 GEN_INT ((((HOST_WIDE_INT) 1
1923 << GET_MODE_BITSIZE (m->savemode)))
1924 - 1),
1925 reg, 1, OPTAB_LIB_WIDEN);
1926 if (tem == 0)
1927 abort ();
1928 if (tem != reg)
1929 emit_move_insn (reg, tem);
1930 sequence = gen_sequence ();
1931 end_sequence ();
1932 i1 = loop_insn_hoist (loop, sequence);
1933 }
1934 else if (GET_CODE (p) == CALL_INSN)
1935 {
1936 i1 = emit_call_insn_before (PATTERN (p), loop_start);
1937 /* Because the USAGE information potentially
1938 contains objects other than hard registers
1939 we need to copy it. */
1940 if (CALL_INSN_FUNCTION_USAGE (p))
1941 CALL_INSN_FUNCTION_USAGE (i1)
1942 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
1943 }
1944 else if (count == m->consec && m->move_insn_first)
1945 {
1946 rtx seq;
1947 /* The SET_SRC might not be invariant, so we must
1948 use the REG_EQUAL note. */
1949 start_sequence ();
1950 emit_move_insn (m->set_dest, m->set_src);
1951 temp = get_insns ();
1952 seq = gen_sequence ();
1953 end_sequence ();
1954
1955 add_label_notes (m->set_src, temp);
1956
1957 i1 = loop_insn_hoist (loop, seq);
1958 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1959 REG_NOTES (i1)
1960 = gen_rtx_EXPR_LIST ((m->is_equiv ? REG_EQUIV
1961 : REG_EQUAL),
1962 m->set_src, REG_NOTES (i1));
1963 }
1964 else
1965 i1 = loop_insn_hoist (loop, PATTERN (p));
1966
1967 if (REG_NOTES (i1) == 0)
1968 {
1969 REG_NOTES (i1) = REG_NOTES (p);
1970
1971 /* If there is a REG_EQUAL note present whose value
1972 is not loop invariant, then delete it, since it
1973 may cause problems with later optimization passes.
1974 It is possible for cse to create such notes
1975 like this as a result of record_jump_cond. */
1976
1977 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
1978 && ! loop_invariant_p (loop, XEXP (temp, 0)))
1979 remove_note (i1, temp);
1980 }
1981
1982 if (new_start == 0)
1983 new_start = i1;
1984
1985 if (loop_dump_stream)
1986 fprintf (loop_dump_stream, " moved to %d",
1987 INSN_UID (i1));
1988
1989 /* If library call, now fix the REG_NOTES that contain
1990 insn pointers, namely REG_LIBCALL on FIRST
1991 and REG_RETVAL on I1. */
1992 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
1993 {
1994 XEXP (temp, 0) = first;
1995 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
1996 XEXP (temp, 0) = i1;
1997 }
1998
1999 temp = p;
2000 delete_insn (p);
2001 p = NEXT_INSN (p);
2002
2003 /* simplify_giv_expr expects that it can walk the insns
2004 at m->insn forwards and see this old sequence we are
2005 tossing here. delete_insn does preserve the next
2006 pointers, but when we skip over a NOTE we must fix
2007 it up. Otherwise that code walks into the non-deleted
2008 insn stream. */
2009 while (p && GET_CODE (p) == NOTE)
2010 p = NEXT_INSN (temp) = NEXT_INSN (p);
2011 }
2012
2013 /* The more regs we move, the less we like moving them. */
2014 threshold -= 3;
2015 }
2016
2017 /* Any other movable that loads the same register
2018 MUST be moved. */
2019 already_moved[regno] = 1;
2020
2021 /* This reg has been moved out of one loop. */
2022 regs->array[regno].moved_once = 1;
2023
2024 /* The reg set here is now invariant. */
2025 if (! m->partial)
2026 regs->array[regno].set_in_loop = 0;
2027
2028 m->done = 1;
2029
2030 /* Change the length-of-life info for the register
2031 to say it lives at least the full length of this loop.
2032 This will help guide optimizations in outer loops. */
2033
2034 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2035 /* This is the old insn before all the moved insns.
2036 We can't use the moved insn because it is out of range
2037 in uid_luid. Only the old insns have luids. */
2038 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2039 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2040 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2041
2042 /* Combine with this moved insn any other matching movables. */
2043
2044 if (! m->partial)
2045 for (m1 = movables->head; m1; m1 = m1->next)
2046 if (m1->match == m)
2047 {
2048 rtx temp;
2049
2050 /* Schedule the reg loaded by M1
2051 for replacement so that shares the reg of M.
2052 If the modes differ (only possible in restricted
2053 circumstances, make a SUBREG.
2054
2055 Note this assumes that the target dependent files
2056 treat REG and SUBREG equally, including within
2057 GO_IF_LEGITIMATE_ADDRESS and in all the
2058 predicates since we never verify that replacing the
2059 original register with a SUBREG results in a
2060 recognizable insn. */
2061 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2062 reg_map[m1->regno] = m->set_dest;
2063 else
2064 reg_map[m1->regno]
2065 = gen_lowpart_common (GET_MODE (m1->set_dest),
2066 m->set_dest);
2067
2068 /* Get rid of the matching insn
2069 and prevent further processing of it. */
2070 m1->done = 1;
2071
2072 /* if library call, delete all insn except last, which
2073 is deleted below */
2074 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2075 NULL_RTX)))
2076 {
2077 for (temp = XEXP (temp, 0); temp != m1->insn;
2078 temp = NEXT_INSN (temp))
2079 delete_insn (temp);
2080 }
2081 delete_insn (m1->insn);
2082
2083 /* Any other movable that loads the same register
2084 MUST be moved. */
2085 already_moved[m1->regno] = 1;
2086
2087 /* The reg merged here is now invariant,
2088 if the reg it matches is invariant. */
2089 if (! m->partial)
2090 regs->array[m1->regno].set_in_loop = 0;
2091 }
2092 }
2093 else if (loop_dump_stream)
2094 fprintf (loop_dump_stream, "not desirable");
2095 }
2096 else if (loop_dump_stream && !m->match)
2097 fprintf (loop_dump_stream, "not safe");
2098
2099 if (loop_dump_stream)
2100 fprintf (loop_dump_stream, "\n");
2101 }
2102
2103 if (new_start == 0)
2104 new_start = loop_start;
2105
2106 /* Go through all the instructions in the loop, making
2107 all the register substitutions scheduled in REG_MAP. */
2108 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2109 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2110 || GET_CODE (p) == CALL_INSN)
2111 {
2112 replace_regs (PATTERN (p), reg_map, nregs, 0);
2113 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2114 INSN_CODE (p) = -1;
2115 }
2116
2117 /* Clean up. */
2118 free (reg_map);
2119 free (already_moved);
2120 }
2121
2122
2123 static void
2124 loop_movables_add (movables, m)
2125 struct loop_movables *movables;
2126 struct movable *m;
2127 {
2128 if (movables->head == 0)
2129 movables->head = m;
2130 else
2131 movables->last->next = m;
2132 movables->last = m;
2133 }
2134
2135
2136 static void
2137 loop_movables_free (movables)
2138 struct loop_movables *movables;
2139 {
2140 struct movable *m;
2141 struct movable *m_next;
2142
2143 for (m = movables->head; m; m = m_next)
2144 {
2145 m_next = m->next;
2146 free (m);
2147 }
2148 }
2149 \f
2150 #if 0
2151 /* Scan X and replace the address of any MEM in it with ADDR.
2152 REG is the address that MEM should have before the replacement. */
2153
2154 static void
2155 replace_call_address (x, reg, addr)
2156 rtx x, reg, addr;
2157 {
2158 register enum rtx_code code;
2159 register int i;
2160 register const char *fmt;
2161
2162 if (x == 0)
2163 return;
2164 code = GET_CODE (x);
2165 switch (code)
2166 {
2167 case PC:
2168 case CC0:
2169 case CONST_INT:
2170 case CONST_DOUBLE:
2171 case CONST:
2172 case SYMBOL_REF:
2173 case LABEL_REF:
2174 case REG:
2175 return;
2176
2177 case SET:
2178 /* Short cut for very common case. */
2179 replace_call_address (XEXP (x, 1), reg, addr);
2180 return;
2181
2182 case CALL:
2183 /* Short cut for very common case. */
2184 replace_call_address (XEXP (x, 0), reg, addr);
2185 return;
2186
2187 case MEM:
2188 /* If this MEM uses a reg other than the one we expected,
2189 something is wrong. */
2190 if (XEXP (x, 0) != reg)
2191 abort ();
2192 XEXP (x, 0) = addr;
2193 return;
2194
2195 default:
2196 break;
2197 }
2198
2199 fmt = GET_RTX_FORMAT (code);
2200 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2201 {
2202 if (fmt[i] == 'e')
2203 replace_call_address (XEXP (x, i), reg, addr);
2204 else if (fmt[i] == 'E')
2205 {
2206 register int j;
2207 for (j = 0; j < XVECLEN (x, i); j++)
2208 replace_call_address (XVECEXP (x, i, j), reg, addr);
2209 }
2210 }
2211 }
2212 #endif
2213 \f
2214 /* Return the number of memory refs to addresses that vary
2215 in the rtx X. */
2216
2217 static int
2218 count_nonfixed_reads (loop, x)
2219 const struct loop *loop;
2220 rtx x;
2221 {
2222 register enum rtx_code code;
2223 register int i;
2224 register const char *fmt;
2225 int value;
2226
2227 if (x == 0)
2228 return 0;
2229
2230 code = GET_CODE (x);
2231 switch (code)
2232 {
2233 case PC:
2234 case CC0:
2235 case CONST_INT:
2236 case CONST_DOUBLE:
2237 case CONST:
2238 case SYMBOL_REF:
2239 case LABEL_REF:
2240 case REG:
2241 return 0;
2242
2243 case MEM:
2244 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2245 + count_nonfixed_reads (loop, XEXP (x, 0)));
2246
2247 default:
2248 break;
2249 }
2250
2251 value = 0;
2252 fmt = GET_RTX_FORMAT (code);
2253 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2254 {
2255 if (fmt[i] == 'e')
2256 value += count_nonfixed_reads (loop, XEXP (x, i));
2257 if (fmt[i] == 'E')
2258 {
2259 register int j;
2260 for (j = 0; j < XVECLEN (x, i); j++)
2261 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2262 }
2263 }
2264 return value;
2265 }
2266 \f
2267 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2268 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2269 `unknown_address_altered', `unknown_constant_address_altered', and
2270 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2271 list `store_mems' in LOOP. */
2272
2273 static void
2274 prescan_loop (loop)
2275 struct loop *loop;
2276 {
2277 register int level = 1;
2278 rtx insn;
2279 struct loop_info *loop_info = LOOP_INFO (loop);
2280 rtx start = loop->start;
2281 rtx end = loop->end;
2282 /* The label after END. Jumping here is just like falling off the
2283 end of the loop. We use next_nonnote_insn instead of next_label
2284 as a hedge against the (pathological) case where some actual insn
2285 might end up between the two. */
2286 rtx exit_target = next_nonnote_insn (end);
2287
2288 loop_info->has_indirect_jump = indirect_jump_in_function;
2289 loop_info->pre_header_has_call = 0;
2290 loop_info->has_call = 0;
2291 loop_info->has_nonconst_call = 0;
2292 loop_info->has_volatile = 0;
2293 loop_info->has_tablejump = 0;
2294 loop_info->has_multiple_exit_targets = 0;
2295 loop->level = 1;
2296
2297 loop_info->unknown_address_altered = 0;
2298 loop_info->unknown_constant_address_altered = 0;
2299 loop_info->store_mems = NULL_RTX;
2300 loop_info->first_loop_store_insn = NULL_RTX;
2301 loop_info->mems_idx = 0;
2302 loop_info->num_mem_sets = 0;
2303
2304
2305 for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
2306 insn = PREV_INSN (insn))
2307 {
2308 if (GET_CODE (insn) == CALL_INSN)
2309 {
2310 loop_info->pre_header_has_call = 1;
2311 break;
2312 }
2313 }
2314
2315 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2316 insn = NEXT_INSN (insn))
2317 {
2318 if (GET_CODE (insn) == NOTE)
2319 {
2320 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2321 {
2322 ++level;
2323 /* Count number of loops contained in this one. */
2324 loop->level++;
2325 }
2326 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2327 {
2328 --level;
2329 }
2330 }
2331 else if (GET_CODE (insn) == CALL_INSN)
2332 {
2333 if (! CONST_CALL_P (insn))
2334 {
2335 loop_info->unknown_address_altered = 1;
2336 loop_info->has_nonconst_call = 1;
2337 }
2338 loop_info->has_call = 1;
2339 }
2340 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2341 {
2342 rtx label1 = NULL_RTX;
2343 rtx label2 = NULL_RTX;
2344
2345 if (volatile_refs_p (PATTERN (insn)))
2346 loop_info->has_volatile = 1;
2347
2348 if (GET_CODE (insn) == JUMP_INSN
2349 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2350 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2351 loop_info->has_tablejump = 1;
2352
2353 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2354 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2355 loop_info->first_loop_store_insn = insn;
2356
2357 if (! loop_info->has_multiple_exit_targets
2358 && GET_CODE (insn) == JUMP_INSN
2359 && GET_CODE (PATTERN (insn)) == SET
2360 && SET_DEST (PATTERN (insn)) == pc_rtx)
2361 {
2362 if (GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE)
2363 {
2364 label1 = XEXP (SET_SRC (PATTERN (insn)), 1);
2365 label2 = XEXP (SET_SRC (PATTERN (insn)), 2);
2366 }
2367 else
2368 {
2369 label1 = SET_SRC (PATTERN (insn));
2370 }
2371
2372 do
2373 {
2374 if (label1 && label1 != pc_rtx)
2375 {
2376 if (GET_CODE (label1) != LABEL_REF)
2377 {
2378 /* Something tricky. */
2379 loop_info->has_multiple_exit_targets = 1;
2380 break;
2381 }
2382 else if (XEXP (label1, 0) != exit_target
2383 && LABEL_OUTSIDE_LOOP_P (label1))
2384 {
2385 /* A jump outside the current loop. */
2386 loop_info->has_multiple_exit_targets = 1;
2387 break;
2388 }
2389 }
2390
2391 label1 = label2;
2392 label2 = NULL_RTX;
2393 }
2394 while (label1);
2395 }
2396 }
2397 else if (GET_CODE (insn) == RETURN)
2398 loop_info->has_multiple_exit_targets = 1;
2399 }
2400
2401 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2402 if (/* An exception thrown by a called function might land us
2403 anywhere. */
2404 ! loop_info->has_nonconst_call
2405 /* We don't want loads for MEMs moved to a location before the
2406 one at which their stack memory becomes allocated. (Note
2407 that this is not a problem for malloc, etc., since those
2408 require actual function calls. */
2409 && ! current_function_calls_alloca
2410 /* There are ways to leave the loop other than falling off the
2411 end. */
2412 && ! loop_info->has_multiple_exit_targets)
2413 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2414 insn = NEXT_INSN (insn))
2415 for_each_rtx (&insn, insert_loop_mem, loop_info);
2416
2417 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2418 that loop_invariant_p and load_mems can use true_dependence
2419 to determine what is really clobbered. */
2420 if (loop_info->unknown_address_altered)
2421 {
2422 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2423
2424 loop_info->store_mems
2425 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2426 }
2427 if (loop_info->unknown_constant_address_altered)
2428 {
2429 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2430
2431 RTX_UNCHANGING_P (mem) = 1;
2432 loop_info->store_mems
2433 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2434 }
2435 }
2436 \f
2437 /* Scan the function looking for loops. Record the start and end of each loop.
2438 Also mark as invalid loops any loops that contain a setjmp or are branched
2439 to from outside the loop. */
2440
2441 static void
2442 find_and_verify_loops (f, loops)
2443 rtx f;
2444 struct loops *loops;
2445 {
2446 rtx insn;
2447 rtx label;
2448 int num_loops;
2449 struct loop *current_loop;
2450 struct loop *next_loop;
2451 struct loop *loop;
2452
2453 num_loops = loops->num;
2454
2455 compute_luids (f, NULL_RTX, 0);
2456
2457 /* If there are jumps to undefined labels,
2458 treat them as jumps out of any/all loops.
2459 This also avoids writing past end of tables when there are no loops. */
2460 uid_loop[0] = NULL;
2461
2462 /* Find boundaries of loops, mark which loops are contained within
2463 loops, and invalidate loops that have setjmp. */
2464
2465 num_loops = 0;
2466 current_loop = NULL;
2467 for (insn = f; insn; insn = NEXT_INSN (insn))
2468 {
2469 if (GET_CODE (insn) == NOTE)
2470 switch (NOTE_LINE_NUMBER (insn))
2471 {
2472 case NOTE_INSN_LOOP_BEG:
2473 next_loop = loops->array + num_loops;
2474 next_loop->num = num_loops;
2475 num_loops++;
2476 next_loop->start = insn;
2477 next_loop->outer = current_loop;
2478 current_loop = next_loop;
2479 break;
2480
2481 case NOTE_INSN_SETJMP:
2482 /* In this case, we must invalidate our current loop and any
2483 enclosing loop. */
2484 for (loop = current_loop; loop; loop = loop->outer)
2485 {
2486 loop->invalid = 1;
2487 if (loop_dump_stream)
2488 fprintf (loop_dump_stream,
2489 "\nLoop at %d ignored due to setjmp.\n",
2490 INSN_UID (loop->start));
2491 }
2492 break;
2493
2494 case NOTE_INSN_LOOP_CONT:
2495 current_loop->cont = insn;
2496 break;
2497
2498 case NOTE_INSN_LOOP_VTOP:
2499 current_loop->vtop = insn;
2500 break;
2501
2502 case NOTE_INSN_LOOP_END:
2503 if (! current_loop)
2504 abort ();
2505
2506 current_loop->end = insn;
2507 current_loop = current_loop->outer;
2508 break;
2509
2510 default:
2511 break;
2512 }
2513
2514 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2515 enclosing loop, but this doesn't matter. */
2516 uid_loop[INSN_UID (insn)] = current_loop;
2517 }
2518
2519 /* Any loop containing a label used in an initializer must be invalidated,
2520 because it can be jumped into from anywhere. */
2521
2522 for (label = forced_labels; label; label = XEXP (label, 1))
2523 {
2524 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2525 loop; loop = loop->outer)
2526 loop->invalid = 1;
2527 }
2528
2529 /* Any loop containing a label used for an exception handler must be
2530 invalidated, because it can be jumped into from anywhere. */
2531
2532 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2533 {
2534 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2535 loop; loop = loop->outer)
2536 loop->invalid = 1;
2537 }
2538
2539 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2540 loop that it is not contained within, that loop is marked invalid.
2541 If any INSN or CALL_INSN uses a label's address, then the loop containing
2542 that label is marked invalid, because it could be jumped into from
2543 anywhere.
2544
2545 Also look for blocks of code ending in an unconditional branch that
2546 exits the loop. If such a block is surrounded by a conditional
2547 branch around the block, move the block elsewhere (see below) and
2548 invert the jump to point to the code block. This may eliminate a
2549 label in our loop and will simplify processing by both us and a
2550 possible second cse pass. */
2551
2552 for (insn = f; insn; insn = NEXT_INSN (insn))
2553 if (INSN_P (insn))
2554 {
2555 struct loop *this_loop = uid_loop[INSN_UID (insn)];
2556
2557 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2558 {
2559 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2560 if (note)
2561 {
2562 for (loop = uid_loop[INSN_UID (XEXP (note, 0))];
2563 loop; loop = loop->outer)
2564 loop->invalid = 1;
2565 }
2566 }
2567
2568 if (GET_CODE (insn) != JUMP_INSN)
2569 continue;
2570
2571 mark_loop_jump (PATTERN (insn), this_loop);
2572
2573 /* See if this is an unconditional branch outside the loop. */
2574 if (this_loop
2575 && (GET_CODE (PATTERN (insn)) == RETURN
2576 || (any_uncondjump_p (insn)
2577 && onlyjump_p (insn)
2578 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2579 != this_loop)))
2580 && get_max_uid () < max_uid_for_loop)
2581 {
2582 rtx p;
2583 rtx our_next = next_real_insn (insn);
2584 rtx last_insn_to_move = NEXT_INSN (insn);
2585 struct loop *dest_loop;
2586 struct loop *outer_loop = NULL;
2587
2588 /* Go backwards until we reach the start of the loop, a label,
2589 or a JUMP_INSN. */
2590 for (p = PREV_INSN (insn);
2591 GET_CODE (p) != CODE_LABEL
2592 && ! (GET_CODE (p) == NOTE
2593 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2594 && GET_CODE (p) != JUMP_INSN;
2595 p = PREV_INSN (p))
2596 ;
2597
2598 /* Check for the case where we have a jump to an inner nested
2599 loop, and do not perform the optimization in that case. */
2600
2601 if (JUMP_LABEL (insn))
2602 {
2603 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2604 if (dest_loop)
2605 {
2606 for (outer_loop = dest_loop; outer_loop;
2607 outer_loop = outer_loop->outer)
2608 if (outer_loop == this_loop)
2609 break;
2610 }
2611 }
2612
2613 /* Make sure that the target of P is within the current loop. */
2614
2615 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2616 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2617 outer_loop = this_loop;
2618
2619 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2620 we have a block of code to try to move.
2621
2622 We look backward and then forward from the target of INSN
2623 to find a BARRIER at the same loop depth as the target.
2624 If we find such a BARRIER, we make a new label for the start
2625 of the block, invert the jump in P and point it to that label,
2626 and move the block of code to the spot we found. */
2627
2628 if (! outer_loop
2629 && GET_CODE (p) == JUMP_INSN
2630 && JUMP_LABEL (p) != 0
2631 /* Just ignore jumps to labels that were never emitted.
2632 These always indicate compilation errors. */
2633 && INSN_UID (JUMP_LABEL (p)) != 0
2634 && any_condjump_p (p) && onlyjump_p (p)
2635 && next_real_insn (JUMP_LABEL (p)) == our_next
2636 /* If it's not safe to move the sequence, then we
2637 mustn't try. */
2638 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2639 &last_insn_to_move))
2640 {
2641 rtx target
2642 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2643 struct loop *target_loop = uid_loop[INSN_UID (target)];
2644 rtx loc, loc2;
2645
2646 for (loc = target; loc; loc = PREV_INSN (loc))
2647 if (GET_CODE (loc) == BARRIER
2648 /* Don't move things inside a tablejump. */
2649 && ((loc2 = next_nonnote_insn (loc)) == 0
2650 || GET_CODE (loc2) != CODE_LABEL
2651 || (loc2 = next_nonnote_insn (loc2)) == 0
2652 || GET_CODE (loc2) != JUMP_INSN
2653 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2654 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2655 && uid_loop[INSN_UID (loc)] == target_loop)
2656 break;
2657
2658 if (loc == 0)
2659 for (loc = target; loc; loc = NEXT_INSN (loc))
2660 if (GET_CODE (loc) == BARRIER
2661 /* Don't move things inside a tablejump. */
2662 && ((loc2 = next_nonnote_insn (loc)) == 0
2663 || GET_CODE (loc2) != CODE_LABEL
2664 || (loc2 = next_nonnote_insn (loc2)) == 0
2665 || GET_CODE (loc2) != JUMP_INSN
2666 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2667 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2668 && uid_loop[INSN_UID (loc)] == target_loop)
2669 break;
2670
2671 if (loc)
2672 {
2673 rtx cond_label = JUMP_LABEL (p);
2674 rtx new_label = get_label_after (p);
2675
2676 /* Ensure our label doesn't go away. */
2677 LABEL_NUSES (cond_label)++;
2678
2679 /* Verify that uid_loop is large enough and that
2680 we can invert P. */
2681 if (invert_jump (p, new_label, 1))
2682 {
2683 rtx q, r;
2684
2685 /* If no suitable BARRIER was found, create a suitable
2686 one before TARGET. Since TARGET is a fall through
2687 path, we'll need to insert an jump around our block
2688 and a add a BARRIER before TARGET.
2689
2690 This creates an extra unconditional jump outside
2691 the loop. However, the benefits of removing rarely
2692 executed instructions from inside the loop usually
2693 outweighs the cost of the extra unconditional jump
2694 outside the loop. */
2695 if (loc == 0)
2696 {
2697 rtx temp;
2698
2699 temp = gen_jump (JUMP_LABEL (insn));
2700 temp = emit_jump_insn_before (temp, target);
2701 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2702 LABEL_NUSES (JUMP_LABEL (insn))++;
2703 loc = emit_barrier_before (target);
2704 }
2705
2706 /* Include the BARRIER after INSN and copy the
2707 block after LOC. */
2708 new_label = squeeze_notes (new_label,
2709 last_insn_to_move);
2710 reorder_insns (new_label, last_insn_to_move, loc);
2711
2712 /* All those insns are now in TARGET_LOOP. */
2713 for (q = new_label;
2714 q != NEXT_INSN (last_insn_to_move);
2715 q = NEXT_INSN (q))
2716 uid_loop[INSN_UID (q)] = target_loop;
2717
2718 /* The label jumped to by INSN is no longer a loop
2719 exit. Unless INSN does not have a label (e.g.,
2720 it is a RETURN insn), search loop->exit_labels
2721 to find its label_ref, and remove it. Also turn
2722 off LABEL_OUTSIDE_LOOP_P bit. */
2723 if (JUMP_LABEL (insn))
2724 {
2725 for (q = 0, r = this_loop->exit_labels;
2726 r;
2727 q = r, r = LABEL_NEXTREF (r))
2728 if (XEXP (r, 0) == JUMP_LABEL (insn))
2729 {
2730 LABEL_OUTSIDE_LOOP_P (r) = 0;
2731 if (q)
2732 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2733 else
2734 this_loop->exit_labels = LABEL_NEXTREF (r);
2735 break;
2736 }
2737
2738 for (loop = this_loop; loop && loop != target_loop;
2739 loop = loop->outer)
2740 loop->exit_count--;
2741
2742 /* If we didn't find it, then something is
2743 wrong. */
2744 if (! r)
2745 abort ();
2746 }
2747
2748 /* P is now a jump outside the loop, so it must be put
2749 in loop->exit_labels, and marked as such.
2750 The easiest way to do this is to just call
2751 mark_loop_jump again for P. */
2752 mark_loop_jump (PATTERN (p), this_loop);
2753
2754 /* If INSN now jumps to the insn after it,
2755 delete INSN. */
2756 if (JUMP_LABEL (insn) != 0
2757 && (next_real_insn (JUMP_LABEL (insn))
2758 == next_real_insn (insn)))
2759 delete_insn (insn);
2760 }
2761
2762 /* Continue the loop after where the conditional
2763 branch used to jump, since the only branch insn
2764 in the block (if it still remains) is an inter-loop
2765 branch and hence needs no processing. */
2766 insn = NEXT_INSN (cond_label);
2767
2768 if (--LABEL_NUSES (cond_label) == 0)
2769 delete_insn (cond_label);
2770
2771 /* This loop will be continued with NEXT_INSN (insn). */
2772 insn = PREV_INSN (insn);
2773 }
2774 }
2775 }
2776 }
2777 }
2778
2779 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2780 loops it is contained in, mark the target loop invalid.
2781
2782 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2783
2784 static void
2785 mark_loop_jump (x, loop)
2786 rtx x;
2787 struct loop *loop;
2788 {
2789 struct loop *dest_loop;
2790 struct loop *outer_loop;
2791 int i;
2792
2793 switch (GET_CODE (x))
2794 {
2795 case PC:
2796 case USE:
2797 case CLOBBER:
2798 case REG:
2799 case MEM:
2800 case CONST_INT:
2801 case CONST_DOUBLE:
2802 case RETURN:
2803 return;
2804
2805 case CONST:
2806 /* There could be a label reference in here. */
2807 mark_loop_jump (XEXP (x, 0), loop);
2808 return;
2809
2810 case PLUS:
2811 case MINUS:
2812 case MULT:
2813 mark_loop_jump (XEXP (x, 0), loop);
2814 mark_loop_jump (XEXP (x, 1), loop);
2815 return;
2816
2817 case LO_SUM:
2818 /* This may refer to a LABEL_REF or SYMBOL_REF. */
2819 mark_loop_jump (XEXP (x, 1), loop);
2820 return;
2821
2822 case SIGN_EXTEND:
2823 case ZERO_EXTEND:
2824 mark_loop_jump (XEXP (x, 0), loop);
2825 return;
2826
2827 case LABEL_REF:
2828 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
2829
2830 /* Link together all labels that branch outside the loop. This
2831 is used by final_[bg]iv_value and the loop unrolling code. Also
2832 mark this LABEL_REF so we know that this branch should predict
2833 false. */
2834
2835 /* A check to make sure the label is not in an inner nested loop,
2836 since this does not count as a loop exit. */
2837 if (dest_loop)
2838 {
2839 for (outer_loop = dest_loop; outer_loop;
2840 outer_loop = outer_loop->outer)
2841 if (outer_loop == loop)
2842 break;
2843 }
2844 else
2845 outer_loop = NULL;
2846
2847 if (loop && ! outer_loop)
2848 {
2849 LABEL_OUTSIDE_LOOP_P (x) = 1;
2850 LABEL_NEXTREF (x) = loop->exit_labels;
2851 loop->exit_labels = x;
2852
2853 for (outer_loop = loop;
2854 outer_loop && outer_loop != dest_loop;
2855 outer_loop = outer_loop->outer)
2856 outer_loop->exit_count++;
2857 }
2858
2859 /* If this is inside a loop, but not in the current loop or one enclosed
2860 by it, it invalidates at least one loop. */
2861
2862 if (! dest_loop)
2863 return;
2864
2865 /* We must invalidate every nested loop containing the target of this
2866 label, except those that also contain the jump insn. */
2867
2868 for (; dest_loop; dest_loop = dest_loop->outer)
2869 {
2870 /* Stop when we reach a loop that also contains the jump insn. */
2871 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
2872 if (dest_loop == outer_loop)
2873 return;
2874
2875 /* If we get here, we know we need to invalidate a loop. */
2876 if (loop_dump_stream && ! dest_loop->invalid)
2877 fprintf (loop_dump_stream,
2878 "\nLoop at %d ignored due to multiple entry points.\n",
2879 INSN_UID (dest_loop->start));
2880
2881 dest_loop->invalid = 1;
2882 }
2883 return;
2884
2885 case SET:
2886 /* If this is not setting pc, ignore. */
2887 if (SET_DEST (x) == pc_rtx)
2888 mark_loop_jump (SET_SRC (x), loop);
2889 return;
2890
2891 case IF_THEN_ELSE:
2892 mark_loop_jump (XEXP (x, 1), loop);
2893 mark_loop_jump (XEXP (x, 2), loop);
2894 return;
2895
2896 case PARALLEL:
2897 case ADDR_VEC:
2898 for (i = 0; i < XVECLEN (x, 0); i++)
2899 mark_loop_jump (XVECEXP (x, 0, i), loop);
2900 return;
2901
2902 case ADDR_DIFF_VEC:
2903 for (i = 0; i < XVECLEN (x, 1); i++)
2904 mark_loop_jump (XVECEXP (x, 1, i), loop);
2905 return;
2906
2907 default:
2908 /* Strictly speaking this is not a jump into the loop, only a possible
2909 jump out of the loop. However, we have no way to link the destination
2910 of this jump onto the list of exit labels. To be safe we mark this
2911 loop and any containing loops as invalid. */
2912 if (loop)
2913 {
2914 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
2915 {
2916 if (loop_dump_stream && ! outer_loop->invalid)
2917 fprintf (loop_dump_stream,
2918 "\nLoop at %d ignored due to unknown exit jump.\n",
2919 INSN_UID (outer_loop->start));
2920 outer_loop->invalid = 1;
2921 }
2922 }
2923 return;
2924 }
2925 }
2926 \f
2927 /* Return nonzero if there is a label in the range from
2928 insn INSN to and including the insn whose luid is END
2929 INSN must have an assigned luid (i.e., it must not have
2930 been previously created by loop.c). */
2931
2932 static int
2933 labels_in_range_p (insn, end)
2934 rtx insn;
2935 int end;
2936 {
2937 while (insn && INSN_LUID (insn) <= end)
2938 {
2939 if (GET_CODE (insn) == CODE_LABEL)
2940 return 1;
2941 insn = NEXT_INSN (insn);
2942 }
2943
2944 return 0;
2945 }
2946
2947 /* Record that a memory reference X is being set. */
2948
2949 static void
2950 note_addr_stored (x, y, data)
2951 rtx x;
2952 rtx y ATTRIBUTE_UNUSED;
2953 void *data ATTRIBUTE_UNUSED;
2954 {
2955 struct loop_info *loop_info = data;
2956
2957 if (x == 0 || GET_CODE (x) != MEM)
2958 return;
2959
2960 /* Count number of memory writes.
2961 This affects heuristics in strength_reduce. */
2962 loop_info->num_mem_sets++;
2963
2964 /* BLKmode MEM means all memory is clobbered. */
2965 if (GET_MODE (x) == BLKmode)
2966 {
2967 if (RTX_UNCHANGING_P (x))
2968 loop_info->unknown_constant_address_altered = 1;
2969 else
2970 loop_info->unknown_address_altered = 1;
2971
2972 return;
2973 }
2974
2975 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
2976 loop_info->store_mems);
2977 }
2978
2979 /* X is a value modified by an INSN that references a biv inside a loop
2980 exit test (ie, X is somehow related to the value of the biv). If X
2981 is a pseudo that is used more than once, then the biv is (effectively)
2982 used more than once. DATA is a pointer to a loop_regs structure. */
2983
2984 static void
2985 note_set_pseudo_multiple_uses (x, y, data)
2986 rtx x;
2987 rtx y ATTRIBUTE_UNUSED;
2988 void *data;
2989 {
2990 struct loop_regs *regs = (struct loop_regs *) data;
2991
2992 if (x == 0)
2993 return;
2994
2995 while (GET_CODE (x) == STRICT_LOW_PART
2996 || GET_CODE (x) == SIGN_EXTRACT
2997 || GET_CODE (x) == ZERO_EXTRACT
2998 || GET_CODE (x) == SUBREG)
2999 x = XEXP (x, 0);
3000
3001 if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3002 return;
3003
3004 /* If we do not have usage information, or if we know the register
3005 is used more than once, note that fact for check_dbra_loop. */
3006 if (REGNO (x) >= max_reg_before_loop
3007 || ! regs->array[REGNO (x)].single_usage
3008 || regs->array[REGNO (x)].single_usage == const0_rtx)
3009 regs->multiple_uses = 1;
3010 }
3011 \f
3012 /* Return nonzero if the rtx X is invariant over the current loop.
3013
3014 The value is 2 if we refer to something only conditionally invariant.
3015
3016 A memory ref is invariant if it is not volatile and does not conflict
3017 with anything stored in `loop_info->store_mems'. */
3018
3019 int
3020 loop_invariant_p (loop, x)
3021 const struct loop *loop;
3022 register rtx x;
3023 {
3024 struct loop_info *loop_info = LOOP_INFO (loop);
3025 struct loop_regs *regs = LOOP_REGS (loop);
3026 register int i;
3027 register enum rtx_code code;
3028 register const char *fmt;
3029 int conditional = 0;
3030 rtx mem_list_entry;
3031
3032 if (x == 0)
3033 return 1;
3034 code = GET_CODE (x);
3035 switch (code)
3036 {
3037 case CONST_INT:
3038 case CONST_DOUBLE:
3039 case SYMBOL_REF:
3040 case CONST:
3041 return 1;
3042
3043 case LABEL_REF:
3044 /* A LABEL_REF is normally invariant, however, if we are unrolling
3045 loops, and this label is inside the loop, then it isn't invariant.
3046 This is because each unrolled copy of the loop body will have
3047 a copy of this label. If this was invariant, then an insn loading
3048 the address of this label into a register might get moved outside
3049 the loop, and then each loop body would end up using the same label.
3050
3051 We don't know the loop bounds here though, so just fail for all
3052 labels. */
3053 if (flag_unroll_loops)
3054 return 0;
3055 else
3056 return 1;
3057
3058 case PC:
3059 case CC0:
3060 case UNSPEC_VOLATILE:
3061 return 0;
3062
3063 case REG:
3064 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3065 since the reg might be set by initialization within the loop. */
3066
3067 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3068 || x == arg_pointer_rtx)
3069 && ! current_function_has_nonlocal_goto)
3070 return 1;
3071
3072 if (LOOP_INFO (loop)->has_call
3073 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3074 return 0;
3075
3076 if (regs->array[REGNO (x)].set_in_loop < 0)
3077 return 2;
3078
3079 return regs->array[REGNO (x)].set_in_loop == 0;
3080
3081 case MEM:
3082 /* Volatile memory references must be rejected. Do this before
3083 checking for read-only items, so that volatile read-only items
3084 will be rejected also. */
3085 if (MEM_VOLATILE_P (x))
3086 return 0;
3087
3088 /* See if there is any dependence between a store and this load. */
3089 mem_list_entry = loop_info->store_mems;
3090 while (mem_list_entry)
3091 {
3092 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3093 x, rtx_varies_p))
3094 return 0;
3095
3096 mem_list_entry = XEXP (mem_list_entry, 1);
3097 }
3098
3099 /* It's not invalidated by a store in memory
3100 but we must still verify the address is invariant. */
3101 break;
3102
3103 case ASM_OPERANDS:
3104 /* Don't mess with insns declared volatile. */
3105 if (MEM_VOLATILE_P (x))
3106 return 0;
3107 break;
3108
3109 default:
3110 break;
3111 }
3112
3113 fmt = GET_RTX_FORMAT (code);
3114 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3115 {
3116 if (fmt[i] == 'e')
3117 {
3118 int tem = loop_invariant_p (loop, XEXP (x, i));
3119 if (tem == 0)
3120 return 0;
3121 if (tem == 2)
3122 conditional = 1;
3123 }
3124 else if (fmt[i] == 'E')
3125 {
3126 register int j;
3127 for (j = 0; j < XVECLEN (x, i); j++)
3128 {
3129 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3130 if (tem == 0)
3131 return 0;
3132 if (tem == 2)
3133 conditional = 1;
3134 }
3135
3136 }
3137 }
3138
3139 return 1 + conditional;
3140 }
3141 \f
3142 /* Return nonzero if all the insns in the loop that set REG
3143 are INSN and the immediately following insns,
3144 and if each of those insns sets REG in an invariant way
3145 (not counting uses of REG in them).
3146
3147 The value is 2 if some of these insns are only conditionally invariant.
3148
3149 We assume that INSN itself is the first set of REG
3150 and that its source is invariant. */
3151
3152 static int
3153 consec_sets_invariant_p (loop, reg, n_sets, insn)
3154 const struct loop *loop;
3155 int n_sets;
3156 rtx reg, insn;
3157 {
3158 struct loop_regs *regs = LOOP_REGS (loop);
3159 rtx p = insn;
3160 unsigned int regno = REGNO (reg);
3161 rtx temp;
3162 /* Number of sets we have to insist on finding after INSN. */
3163 int count = n_sets - 1;
3164 int old = regs->array[regno].set_in_loop;
3165 int value = 0;
3166 int this;
3167
3168 /* If N_SETS hit the limit, we can't rely on its value. */
3169 if (n_sets == 127)
3170 return 0;
3171
3172 regs->array[regno].set_in_loop = 0;
3173
3174 while (count > 0)
3175 {
3176 register enum rtx_code code;
3177 rtx set;
3178
3179 p = NEXT_INSN (p);
3180 code = GET_CODE (p);
3181
3182 /* If library call, skip to end of it. */
3183 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3184 p = XEXP (temp, 0);
3185
3186 this = 0;
3187 if (code == INSN
3188 && (set = single_set (p))
3189 && GET_CODE (SET_DEST (set)) == REG
3190 && REGNO (SET_DEST (set)) == regno)
3191 {
3192 this = loop_invariant_p (loop, SET_SRC (set));
3193 if (this != 0)
3194 value |= this;
3195 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3196 {
3197 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3198 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3199 notes are OK. */
3200 this = (CONSTANT_P (XEXP (temp, 0))
3201 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3202 && loop_invariant_p (loop, XEXP (temp, 0))));
3203 if (this != 0)
3204 value |= this;
3205 }
3206 }
3207 if (this != 0)
3208 count--;
3209 else if (code != NOTE)
3210 {
3211 regs->array[regno].set_in_loop = old;
3212 return 0;
3213 }
3214 }
3215
3216 regs->array[regno].set_in_loop = old;
3217 /* If loop_invariant_p ever returned 2, we return 2. */
3218 return 1 + (value & 2);
3219 }
3220
3221 #if 0
3222 /* I don't think this condition is sufficient to allow INSN
3223 to be moved, so we no longer test it. */
3224
3225 /* Return 1 if all insns in the basic block of INSN and following INSN
3226 that set REG are invariant according to TABLE. */
3227
3228 static int
3229 all_sets_invariant_p (reg, insn, table)
3230 rtx reg, insn;
3231 short *table;
3232 {
3233 register rtx p = insn;
3234 register int regno = REGNO (reg);
3235
3236 while (1)
3237 {
3238 register enum rtx_code code;
3239 p = NEXT_INSN (p);
3240 code = GET_CODE (p);
3241 if (code == CODE_LABEL || code == JUMP_INSN)
3242 return 1;
3243 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3244 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3245 && REGNO (SET_DEST (PATTERN (p))) == regno)
3246 {
3247 if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3248 return 0;
3249 }
3250 }
3251 }
3252 #endif /* 0 */
3253 \f
3254 /* Look at all uses (not sets) of registers in X. For each, if it is
3255 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3256 a different insn, set USAGE[REGNO] to const0_rtx. */
3257
3258 static void
3259 find_single_use_in_loop (regs, insn, x)
3260 struct loop_regs *regs;
3261 rtx insn;
3262 rtx x;
3263 {
3264 enum rtx_code code = GET_CODE (x);
3265 const char *fmt = GET_RTX_FORMAT (code);
3266 int i, j;
3267
3268 if (code == REG)
3269 regs->array[REGNO (x)].single_usage
3270 = (regs->array[REGNO (x)].single_usage != 0
3271 && regs->array[REGNO (x)].single_usage != insn)
3272 ? const0_rtx : insn;
3273
3274 else if (code == SET)
3275 {
3276 /* Don't count SET_DEST if it is a REG; otherwise count things
3277 in SET_DEST because if a register is partially modified, it won't
3278 show up as a potential movable so we don't care how USAGE is set
3279 for it. */
3280 if (GET_CODE (SET_DEST (x)) != REG)
3281 find_single_use_in_loop (regs, insn, SET_DEST (x));
3282 find_single_use_in_loop (regs, insn, SET_SRC (x));
3283 }
3284 else
3285 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3286 {
3287 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3288 find_single_use_in_loop (regs, insn, XEXP (x, i));
3289 else if (fmt[i] == 'E')
3290 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3291 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3292 }
3293 }
3294 \f
3295 /* Count and record any set in X which is contained in INSN. Update
3296 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3297 in X. */
3298
3299 static void
3300 count_one_set (regs, insn, x, last_set)
3301 struct loop_regs *regs;
3302 rtx insn, x;
3303 rtx *last_set;
3304 {
3305 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3306 /* Don't move a reg that has an explicit clobber.
3307 It's not worth the pain to try to do it correctly. */
3308 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3309
3310 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3311 {
3312 rtx dest = SET_DEST (x);
3313 while (GET_CODE (dest) == SUBREG
3314 || GET_CODE (dest) == ZERO_EXTRACT
3315 || GET_CODE (dest) == SIGN_EXTRACT
3316 || GET_CODE (dest) == STRICT_LOW_PART)
3317 dest = XEXP (dest, 0);
3318 if (GET_CODE (dest) == REG)
3319 {
3320 register int regno = REGNO (dest);
3321 /* If this is the first setting of this reg
3322 in current basic block, and it was set before,
3323 it must be set in two basic blocks, so it cannot
3324 be moved out of the loop. */
3325 if (regs->array[regno].set_in_loop > 0
3326 && last_set == 0)
3327 regs->array[regno].may_not_optimize = 1;
3328 /* If this is not first setting in current basic block,
3329 see if reg was used in between previous one and this.
3330 If so, neither one can be moved. */
3331 if (last_set[regno] != 0
3332 && reg_used_between_p (dest, last_set[regno], insn))
3333 regs->array[regno].may_not_optimize = 1;
3334 if (regs->array[regno].set_in_loop < 127)
3335 ++regs->array[regno].set_in_loop;
3336 last_set[regno] = insn;
3337 }
3338 }
3339 }
3340 \f
3341 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3342 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3343 contained in insn INSN is used by any insn that precedes INSN in
3344 cyclic order starting from the loop entry point.
3345
3346 We don't want to use INSN_LUID here because if we restrict INSN to those
3347 that have a valid INSN_LUID, it means we cannot move an invariant out
3348 from an inner loop past two loops. */
3349
3350 static int
3351 loop_reg_used_before_p (loop, set, insn)
3352 const struct loop *loop;
3353 rtx set, insn;
3354 {
3355 rtx reg = SET_DEST (set);
3356 rtx p;
3357
3358 /* Scan forward checking for register usage. If we hit INSN, we
3359 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3360 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3361 {
3362 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3363 return 1;
3364
3365 if (p == loop->end)
3366 p = loop->start;
3367 }
3368
3369 return 0;
3370 }
3371 \f
3372 /* A "basic induction variable" or biv is a pseudo reg that is set
3373 (within this loop) only by incrementing or decrementing it. */
3374 /* A "general induction variable" or giv is a pseudo reg whose
3375 value is a linear function of a biv. */
3376
3377 /* Bivs are recognized by `basic_induction_var';
3378 Givs by `general_induction_var'. */
3379
3380 /* Communication with routines called via `note_stores'. */
3381
3382 static rtx note_insn;
3383
3384 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3385
3386 static rtx addr_placeholder;
3387
3388 /* ??? Unfinished optimizations, and possible future optimizations,
3389 for the strength reduction code. */
3390
3391 /* ??? The interaction of biv elimination, and recognition of 'constant'
3392 bivs, may cause problems. */
3393
3394 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3395 performance problems.
3396
3397 Perhaps don't eliminate things that can be combined with an addressing
3398 mode. Find all givs that have the same biv, mult_val, and add_val;
3399 then for each giv, check to see if its only use dies in a following
3400 memory address. If so, generate a new memory address and check to see
3401 if it is valid. If it is valid, then store the modified memory address,
3402 otherwise, mark the giv as not done so that it will get its own iv. */
3403
3404 /* ??? Could try to optimize branches when it is known that a biv is always
3405 positive. */
3406
3407 /* ??? When replace a biv in a compare insn, we should replace with closest
3408 giv so that an optimized branch can still be recognized by the combiner,
3409 e.g. the VAX acb insn. */
3410
3411 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3412 was rerun in loop_optimize whenever a register was added or moved.
3413 Also, some of the optimizations could be a little less conservative. */
3414 \f
3415 /* Scan the loop body and call FNCALL for each insn. In the addition to the
3416 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
3417 callback.
3418
3419 NOT_EVERY_ITERATION if current insn is not executed at least once for every
3420 loop iteration except for the last one.
3421
3422 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
3423 loop iteration.
3424 */
3425 void
3426 for_each_insn_in_loop (loop, fncall)
3427 struct loop *loop;
3428 loop_insn_callback fncall;
3429 {
3430 /* This is 1 if current insn is not executed at least once for every loop
3431 iteration. */
3432 int not_every_iteration = 0;
3433 int maybe_multiple = 0;
3434 int past_loop_latch = 0;
3435 int loop_depth = 0;
3436 rtx p;
3437
3438 /* If loop_scan_start points to the loop exit test, we have to be wary of
3439 subversive use of gotos inside expression statements. */
3440 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
3441 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
3442
3443 /* Scan through loop to find all possible bivs. */
3444
3445 for (p = next_insn_in_loop (loop, loop->scan_start);
3446 p != NULL_RTX;
3447 p = next_insn_in_loop (loop, p))
3448 {
3449 p = fncall (loop, p, not_every_iteration, maybe_multiple);
3450
3451 /* Past CODE_LABEL, we get to insns that may be executed multiple
3452 times. The only way we can be sure that they can't is if every
3453 jump insn between here and the end of the loop either
3454 returns, exits the loop, is a jump to a location that is still
3455 behind the label, or is a jump to the loop start. */
3456
3457 if (GET_CODE (p) == CODE_LABEL)
3458 {
3459 rtx insn = p;
3460
3461 maybe_multiple = 0;
3462
3463 while (1)
3464 {
3465 insn = NEXT_INSN (insn);
3466 if (insn == loop->scan_start)
3467 break;
3468 if (insn == loop->end)
3469 {
3470 if (loop->top != 0)
3471 insn = loop->top;
3472 else
3473 break;
3474 if (insn == loop->scan_start)
3475 break;
3476 }
3477
3478 if (GET_CODE (insn) == JUMP_INSN
3479 && GET_CODE (PATTERN (insn)) != RETURN
3480 && (!any_condjump_p (insn)
3481 || (JUMP_LABEL (insn) != 0
3482 && JUMP_LABEL (insn) != loop->scan_start
3483 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
3484 {
3485 maybe_multiple = 1;
3486 break;
3487 }
3488 }
3489 }
3490
3491 /* Past a jump, we get to insns for which we can't count
3492 on whether they will be executed during each iteration. */
3493 /* This code appears twice in strength_reduce. There is also similar
3494 code in scan_loop. */
3495 if (GET_CODE (p) == JUMP_INSN
3496 /* If we enter the loop in the middle, and scan around to the
3497 beginning, don't set not_every_iteration for that.
3498 This can be any kind of jump, since we want to know if insns
3499 will be executed if the loop is executed. */
3500 && !(JUMP_LABEL (p) == loop->top
3501 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
3502 && any_uncondjump_p (p))
3503 || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
3504 {
3505 rtx label = 0;
3506
3507 /* If this is a jump outside the loop, then it also doesn't
3508 matter. Check to see if the target of this branch is on the
3509 loop->exits_labels list. */
3510
3511 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
3512 if (XEXP (label, 0) == JUMP_LABEL (p))
3513 break;
3514
3515 if (!label)
3516 not_every_iteration = 1;
3517 }
3518
3519 else if (GET_CODE (p) == NOTE)
3520 {
3521 /* At the virtual top of a converted loop, insns are again known to
3522 be executed each iteration: logically, the loop begins here
3523 even though the exit code has been duplicated.
3524
3525 Insns are also again known to be executed each iteration at
3526 the LOOP_CONT note. */
3527 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
3528 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
3529 && loop_depth == 0)
3530 not_every_iteration = 0;
3531 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3532 loop_depth++;
3533 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3534 loop_depth--;
3535 }
3536
3537 /* Note if we pass a loop latch. If we do, then we can not clear
3538 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
3539 a loop since a jump before the last CODE_LABEL may have started
3540 a new loop iteration.
3541
3542 Note that LOOP_TOP is only set for rotated loops and we need
3543 this check for all loops, so compare against the CODE_LABEL
3544 which immediately follows LOOP_START. */
3545 if (GET_CODE (p) == JUMP_INSN
3546 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
3547 past_loop_latch = 1;
3548
3549 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3550 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3551 or not an insn is known to be executed each iteration of the
3552 loop, whether or not any iterations are known to occur.
3553
3554 Therefore, if we have just passed a label and have no more labels
3555 between here and the test insn of the loop, and we have not passed
3556 a jump to the top of the loop, then we know these insns will be
3557 executed each iteration. */
3558
3559 if (not_every_iteration
3560 && !past_loop_latch
3561 && GET_CODE (p) == CODE_LABEL
3562 && no_labels_between_p (p, loop->end)
3563 && loop_insn_first_p (p, loop->cont))
3564 not_every_iteration = 0;
3565 }
3566 }
3567 \f
3568 static void
3569 loop_bivs_find (loop)
3570 struct loop *loop;
3571 {
3572 struct loop_regs *regs = LOOP_REGS (loop);
3573 struct loop_ivs *ivs = LOOP_IVS (loop);
3574 /* Temporary list pointers for traversing ivs->list. */
3575 struct iv_class *bl, **backbl;
3576
3577 ivs->list = 0;
3578
3579 for_each_insn_in_loop (loop, check_insn_for_bivs);
3580
3581 /* Scan ivs->list to remove all regs that proved not to be bivs.
3582 Make a sanity check against regs->n_times_set. */
3583 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
3584 {
3585 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
3586 /* Above happens if register modified by subreg, etc. */
3587 /* Make sure it is not recognized as a basic induction var: */
3588 || regs->array[bl->regno].n_times_set != bl->biv_count
3589 /* If never incremented, it is invariant that we decided not to
3590 move. So leave it alone. */
3591 || ! bl->incremented)
3592 {
3593 if (loop_dump_stream)
3594 fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
3595 bl->regno,
3596 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
3597 ? "not induction variable"
3598 : (! bl->incremented ? "never incremented"
3599 : "count error")));
3600
3601 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
3602 *backbl = bl->next;
3603 }
3604 else
3605 {
3606 backbl = &bl->next;
3607
3608 if (loop_dump_stream)
3609 fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
3610 }
3611 }
3612 }
3613
3614
3615 /* Determine how BIVS are initialised by looking through pre-header
3616 extended basic block. */
3617 static void
3618 loop_bivs_init_find (loop)
3619 struct loop *loop;
3620 {
3621 struct loop_ivs *ivs = LOOP_IVS (loop);
3622 /* Temporary list pointers for traversing ivs->list. */
3623 struct iv_class *bl;
3624 int call_seen;
3625 rtx p;
3626
3627 /* Find initial value for each biv by searching backwards from loop_start,
3628 halting at first label. Also record any test condition. */
3629
3630 call_seen = 0;
3631 for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3632 {
3633 rtx test;
3634
3635 note_insn = p;
3636
3637 if (GET_CODE (p) == CALL_INSN)
3638 call_seen = 1;
3639
3640 if (INSN_P (p))
3641 note_stores (PATTERN (p), record_initial, ivs);
3642
3643 /* Record any test of a biv that branches around the loop if no store
3644 between it and the start of loop. We only care about tests with
3645 constants and registers and only certain of those. */
3646 if (GET_CODE (p) == JUMP_INSN
3647 && JUMP_LABEL (p) != 0
3648 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
3649 && (test = get_condition_for_loop (loop, p)) != 0
3650 && GET_CODE (XEXP (test, 0)) == REG
3651 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3652 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
3653 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
3654 && bl->init_insn == 0)
3655 {
3656 /* If an NE test, we have an initial value! */
3657 if (GET_CODE (test) == NE)
3658 {
3659 bl->init_insn = p;
3660 bl->init_set = gen_rtx_SET (VOIDmode,
3661 XEXP (test, 0), XEXP (test, 1));
3662 }
3663 else
3664 bl->initial_test = test;
3665 }
3666 }
3667 }
3668
3669
3670 /* Look at the each biv and see if we can say anything better about its
3671 initial value from any initializing insns set up above. (This is done
3672 in two passes to avoid missing SETs in a PARALLEL.) */
3673 static void
3674 loop_bivs_check (loop)
3675 struct loop *loop;
3676 {
3677 struct loop_ivs *ivs = LOOP_IVS (loop);
3678 /* Temporary list pointers for traversing ivs->list. */
3679 struct iv_class *bl;
3680 struct iv_class **backbl;
3681
3682 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
3683 {
3684 rtx src;
3685 rtx note;
3686
3687 if (! bl->init_insn)
3688 continue;
3689
3690 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3691 is a constant, use the value of that. */
3692 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
3693 && CONSTANT_P (XEXP (note, 0)))
3694 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
3695 && CONSTANT_P (XEXP (note, 0))))
3696 src = XEXP (note, 0);
3697 else
3698 src = SET_SRC (bl->init_set);
3699
3700 if (loop_dump_stream)
3701 fprintf (loop_dump_stream,
3702 "Biv %d: initialized at insn %d: initial value ",
3703 bl->regno, INSN_UID (bl->init_insn));
3704
3705 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3706 || GET_MODE (src) == VOIDmode)
3707 && valid_initial_value_p (src, bl->init_insn,
3708 LOOP_INFO (loop)->pre_header_has_call,
3709 loop->start))
3710 {
3711 bl->initial_value = src;
3712
3713 if (loop_dump_stream)
3714 {
3715 print_simple_rtl (loop_dump_stream, src);
3716 fputc ('\n', loop_dump_stream);
3717 }
3718 }
3719 /* If we can't make it a giv,
3720 let biv keep initial value of "itself". */
3721 else if (loop_dump_stream)
3722 fprintf (loop_dump_stream, "is complex\n");
3723 }
3724 }
3725
3726
3727 /* Search the loop for general induction variables. */
3728
3729 static void
3730 loop_givs_find (loop)
3731 struct loop* loop;
3732 {
3733 for_each_insn_in_loop (loop, check_insn_for_givs);
3734 }
3735
3736
3737 /* For each giv for which we still don't know whether or not it is
3738 replaceable, check to see if it is replaceable because its final value
3739 can be calculated. */
3740
3741 static void
3742 loop_givs_check (loop)
3743 struct loop *loop;
3744 {
3745 struct loop_ivs *ivs = LOOP_IVS (loop);
3746 struct iv_class *bl;
3747
3748 for (bl = ivs->list; bl; bl = bl->next)
3749 {
3750 struct induction *v;
3751
3752 for (v = bl->giv; v; v = v->next_iv)
3753 if (! v->replaceable && ! v->not_replaceable)
3754 check_final_value (loop, v);
3755 }
3756 }
3757
3758
3759 /* Return non-zero if it is possible to eliminate the biv BL provided
3760 all givs are reduced. This is possible if either the reg is not
3761 used outside the loop, or we can compute what its final value will
3762 be. */
3763
3764 static int
3765 loop_biv_eliminable_p (loop, bl, threshold, insn_count)
3766 struct loop *loop;
3767 struct iv_class *bl;
3768 int threshold;
3769 int insn_count;
3770 {
3771 /* For architectures with a decrement_and_branch_until_zero insn,
3772 don't do this if we put a REG_NONNEG note on the endtest for this
3773 biv. */
3774
3775 #ifdef HAVE_decrement_and_branch_until_zero
3776 if (bl->nonneg)
3777 {
3778 if (loop_dump_stream)
3779 fprintf (loop_dump_stream,
3780 "Cannot eliminate nonneg biv %d.\n", bl->regno);
3781 return 0;
3782 }
3783 #endif
3784
3785 /* Check that biv is used outside loop or if it has a final value.
3786 Compare against bl->init_insn rather than loop->start. We aren't
3787 concerned with any uses of the biv between init_insn and
3788 loop->start since these won't be affected by the value of the biv
3789 elsewhere in the function, so long as init_insn doesn't use the
3790 biv itself. */
3791
3792 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
3793 && bl->init_insn
3794 && INSN_UID (bl->init_insn) < max_uid_for_loop
3795 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
3796 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3797 || (bl->final_value = final_biv_value (loop, bl)))
3798 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
3799
3800 if (loop_dump_stream)
3801 {
3802 fprintf (loop_dump_stream,
3803 "Cannot eliminate biv %d.\n",
3804 bl->regno);
3805 fprintf (loop_dump_stream,
3806 "First use: insn %d, last use: insn %d.\n",
3807 REGNO_FIRST_UID (bl->regno),
3808 REGNO_LAST_UID (bl->regno));
3809 }
3810 return 0;
3811 }
3812
3813
3814 /* Reduce each giv of BL that we have decided to reduce. */
3815
3816 static void
3817 loop_givs_reduce (loop, bl)
3818 struct loop *loop;
3819 struct iv_class *bl;
3820 {
3821 struct induction *v;
3822
3823 for (v = bl->giv; v; v = v->next_iv)
3824 {
3825 struct induction *tv;
3826 if (! v->ignore && v->same == 0)
3827 {
3828 int auto_inc_opt = 0;
3829
3830 /* If the code for derived givs immediately below has already
3831 allocated a new_reg, we must keep it. */
3832 if (! v->new_reg)
3833 v->new_reg = gen_reg_rtx (v->mode);
3834
3835 #ifdef AUTO_INC_DEC
3836 /* If the target has auto-increment addressing modes, and
3837 this is an address giv, then try to put the increment
3838 immediately after its use, so that flow can create an
3839 auto-increment addressing mode. */
3840 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
3841 && bl->biv->always_executed && ! bl->biv->maybe_multiple
3842 /* We don't handle reversed biv's because bl->biv->insn
3843 does not have a valid INSN_LUID. */
3844 && ! bl->reversed
3845 && v->always_executed && ! v->maybe_multiple
3846 && INSN_UID (v->insn) < max_uid_for_loop)
3847 {
3848 /* If other giv's have been combined with this one, then
3849 this will work only if all uses of the other giv's occur
3850 before this giv's insn. This is difficult to check.
3851
3852 We simplify this by looking for the common case where
3853 there is one DEST_REG giv, and this giv's insn is the
3854 last use of the dest_reg of that DEST_REG giv. If the
3855 increment occurs after the address giv, then we can
3856 perform the optimization. (Otherwise, the increment
3857 would have to go before other_giv, and we would not be
3858 able to combine it with the address giv to get an
3859 auto-inc address.) */
3860 if (v->combined_with)
3861 {
3862 struct induction *other_giv = 0;
3863
3864 for (tv = bl->giv; tv; tv = tv->next_iv)
3865 if (tv->same == v)
3866 {
3867 if (other_giv)
3868 break;
3869 else
3870 other_giv = tv;
3871 }
3872 if (! tv && other_giv
3873 && REGNO (other_giv->dest_reg) < max_reg_before_loop
3874 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
3875 == INSN_UID (v->insn))
3876 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
3877 auto_inc_opt = 1;
3878 }
3879 /* Check for case where increment is before the address
3880 giv. Do this test in "loop order". */
3881 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
3882 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
3883 || (INSN_LUID (bl->biv->insn)
3884 > INSN_LUID (loop->scan_start))))
3885 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
3886 && (INSN_LUID (loop->scan_start)
3887 < INSN_LUID (bl->biv->insn))))
3888 auto_inc_opt = -1;
3889 else
3890 auto_inc_opt = 1;
3891
3892 #ifdef HAVE_cc0
3893 {
3894 rtx prev;
3895
3896 /* We can't put an insn immediately after one setting
3897 cc0, or immediately before one using cc0. */
3898 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
3899 || (auto_inc_opt == -1
3900 && (prev = prev_nonnote_insn (v->insn)) != 0
3901 && INSN_P (prev)
3902 && sets_cc0_p (PATTERN (prev))))
3903 auto_inc_opt = 0;
3904 }
3905 #endif
3906
3907 if (auto_inc_opt)
3908 v->auto_inc_opt = 1;
3909 }
3910 #endif
3911
3912 /* For each place where the biv is incremented, add an insn
3913 to increment the new, reduced reg for the giv. */
3914 for (tv = bl->biv; tv; tv = tv->next_iv)
3915 {
3916 rtx insert_before;
3917
3918 if (! auto_inc_opt)
3919 insert_before = tv->insn;
3920 else if (auto_inc_opt == 1)
3921 insert_before = NEXT_INSN (v->insn);
3922 else
3923 insert_before = v->insn;
3924
3925 if (tv->mult_val == const1_rtx)
3926 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
3927 v->new_reg, v->new_reg,
3928 0, insert_before);
3929 else /* tv->mult_val == const0_rtx */
3930 /* A multiply is acceptable here
3931 since this is presumed to be seldom executed. */
3932 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
3933 v->add_val, v->new_reg,
3934 0, insert_before);
3935 }
3936
3937 /* Add code at loop start to initialize giv's reduced reg. */
3938
3939 loop_iv_add_mult_hoist (loop,
3940 extend_value_for_giv (v, bl->initial_value),
3941 v->mult_val, v->add_val, v->new_reg);
3942 }
3943 }
3944 }
3945
3946
3947 /* Check for givs whose first use is their definition and whose
3948 last use is the definition of another giv. If so, it is likely
3949 dead and should not be used to derive another giv nor to
3950 eliminate a biv. */
3951
3952 static void
3953 loop_givs_dead_check (loop, bl)
3954 struct loop *loop ATTRIBUTE_UNUSED;
3955 struct iv_class *bl;
3956 {
3957 struct induction *v;
3958
3959 for (v = bl->giv; v; v = v->next_iv)
3960 {
3961 if (v->ignore
3962 || (v->same && v->same->ignore))
3963 continue;
3964
3965 if (v->giv_type == DEST_REG
3966 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
3967 {
3968 struct induction *v1;
3969
3970 for (v1 = bl->giv; v1; v1 = v1->next_iv)
3971 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
3972 v->maybe_dead = 1;
3973 }
3974 }
3975 }
3976
3977
3978 static void
3979 loop_givs_rescan (loop, bl, reg_map)
3980 struct loop *loop;
3981 struct iv_class *bl;
3982 rtx *reg_map;
3983 {
3984 struct induction *v;
3985
3986 for (v = bl->giv; v; v = v->next_iv)
3987 {
3988 if (v->same && v->same->ignore)
3989 v->ignore = 1;
3990
3991 if (v->ignore)
3992 continue;
3993
3994 /* Update expression if this was combined, in case other giv was
3995 replaced. */
3996 if (v->same)
3997 v->new_reg = replace_rtx (v->new_reg,
3998 v->same->dest_reg, v->same->new_reg);
3999
4000 /* See if this register is known to be a pointer to something. If
4001 so, see if we can find the alignment. First see if there is a
4002 destination register that is a pointer. If so, this shares the
4003 alignment too. Next see if we can deduce anything from the
4004 computational information. If not, and this is a DEST_ADDR
4005 giv, at least we know that it's a pointer, though we don't know
4006 the alignment. */
4007 if (GET_CODE (v->new_reg) == REG
4008 && v->giv_type == DEST_REG
4009 && REG_POINTER (v->dest_reg))
4010 mark_reg_pointer (v->new_reg,
4011 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
4012 else if (GET_CODE (v->new_reg) == REG
4013 && REG_POINTER (v->src_reg))
4014 {
4015 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
4016
4017 if (align == 0
4018 || GET_CODE (v->add_val) != CONST_INT
4019 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
4020 align = 0;
4021
4022 mark_reg_pointer (v->new_reg, align);
4023 }
4024 else if (GET_CODE (v->new_reg) == REG
4025 && GET_CODE (v->add_val) == REG
4026 && REG_POINTER (v->add_val))
4027 {
4028 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
4029
4030 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
4031 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
4032 align = 0;
4033
4034 mark_reg_pointer (v->new_reg, align);
4035 }
4036 else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
4037 mark_reg_pointer (v->new_reg, 0);
4038
4039 if (v->giv_type == DEST_ADDR)
4040 /* Store reduced reg as the address in the memref where we found
4041 this giv. */
4042 validate_change (v->insn, v->location, v->new_reg, 0);
4043 else if (v->replaceable)
4044 {
4045 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4046 }
4047 else
4048 {
4049 /* Not replaceable; emit an insn to set the original giv reg from
4050 the reduced giv, same as above. */
4051 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4052 v->insn);
4053 }
4054
4055 /* When a loop is reversed, givs which depend on the reversed
4056 biv, and which are live outside the loop, must be set to their
4057 correct final value. This insn is only needed if the giv is
4058 not replaceable. The correct final value is the same as the
4059 value that the giv starts the reversed loop with. */
4060 if (bl->reversed && ! v->replaceable)
4061 loop_iv_add_mult_sink (loop,
4062 extend_value_for_giv (v, bl->initial_value),
4063 v->mult_val, v->add_val, v->dest_reg);
4064 else if (v->final_value)
4065 loop_insn_sink_or_swim (loop,
4066 gen_move_insn (v->dest_reg, v->final_value));
4067
4068 if (loop_dump_stream)
4069 {
4070 fprintf (loop_dump_stream, "giv at %d reduced to ",
4071 INSN_UID (v->insn));
4072 print_simple_rtl (loop_dump_stream, v->new_reg);
4073 fprintf (loop_dump_stream, "\n");
4074 }
4075 }
4076 }
4077
4078
4079 static int
4080 loop_giv_reduce_benefit (loop, bl, v, test_reg)
4081 struct loop *loop ATTRIBUTE_UNUSED;
4082 struct iv_class *bl;
4083 struct induction *v;
4084 rtx test_reg;
4085 {
4086 int add_cost;
4087 int benefit;
4088
4089 benefit = v->benefit;
4090 PUT_MODE (test_reg, v->mode);
4091 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
4092 test_reg, test_reg);
4093
4094 /* Reduce benefit if not replaceable, since we will insert a
4095 move-insn to replace the insn that calculates this giv. Don't do
4096 this unless the giv is a user variable, since it will often be
4097 marked non-replaceable because of the duplication of the exit
4098 code outside the loop. In such a case, the copies we insert are
4099 dead and will be deleted. So they don't have a cost. Similar
4100 situations exist. */
4101 /* ??? The new final_[bg]iv_value code does a much better job of
4102 finding replaceable giv's, and hence this code may no longer be
4103 necessary. */
4104 if (! v->replaceable && ! bl->eliminable
4105 && REG_USERVAR_P (v->dest_reg))
4106 benefit -= copy_cost;
4107
4108 /* Decrease the benefit to count the add-insns that we will insert
4109 to increment the reduced reg for the giv. ??? This can
4110 overestimate the run-time cost of the additional insns, e.g. if
4111 there are multiple basic blocks that increment the biv, but only
4112 one of these blocks is executed during each iteration. There is
4113 no good way to detect cases like this with the current structure
4114 of the loop optimizer. This code is more accurate for
4115 determining code size than run-time benefits. */
4116 benefit -= add_cost * bl->biv_count;
4117
4118 /* Decide whether to strength-reduce this giv or to leave the code
4119 unchanged (recompute it from the biv each time it is used). This
4120 decision can be made independently for each giv. */
4121
4122 #ifdef AUTO_INC_DEC
4123 /* Attempt to guess whether autoincrement will handle some of the
4124 new add insns; if so, increase BENEFIT (undo the subtraction of
4125 add_cost that was done above). */
4126 if (v->giv_type == DEST_ADDR
4127 /* Increasing the benefit is risky, since this is only a guess.
4128 Avoid increasing register pressure in cases where there would
4129 be no other benefit from reducing this giv. */
4130 && benefit > 0
4131 && GET_CODE (v->mult_val) == CONST_INT)
4132 {
4133 if (HAVE_POST_INCREMENT
4134 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4135 benefit += add_cost * bl->biv_count;
4136 else if (HAVE_PRE_INCREMENT
4137 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4138 benefit += add_cost * bl->biv_count;
4139 else if (HAVE_POST_DECREMENT
4140 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4141 benefit += add_cost * bl->biv_count;
4142 else if (HAVE_PRE_DECREMENT
4143 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4144 benefit += add_cost * bl->biv_count;
4145 }
4146 #endif
4147
4148 return benefit;
4149 }
4150
4151
4152 /* Free IV structures for LOOP. */
4153
4154 static void
4155 loop_ivs_free (loop)
4156 struct loop *loop;
4157 {
4158 struct loop_ivs *ivs = LOOP_IVS (loop);
4159 struct iv_class *iv = ivs->list;
4160
4161 free (ivs->regs);
4162
4163 while (iv)
4164 {
4165 struct iv_class *next = iv->next;
4166 struct induction *induction;
4167 struct induction *next_induction;
4168
4169 for (induction = iv->biv; induction; induction = next_induction)
4170 {
4171 next_induction = induction->next_iv;
4172 free (induction);
4173 }
4174 for (induction = iv->giv; induction; induction = next_induction)
4175 {
4176 next_induction = induction->next_iv;
4177 free (induction);
4178 }
4179
4180 free (iv);
4181 iv = next;
4182 }
4183 }
4184
4185
4186 /* Perform strength reduction and induction variable elimination.
4187
4188 Pseudo registers created during this function will be beyond the
4189 last valid index in several tables including
4190 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
4191 problem here, because the added registers cannot be givs outside of
4192 their loop, and hence will never be reconsidered. But scan_loop
4193 must check regnos to make sure they are in bounds. */
4194
4195 static void
4196 strength_reduce (loop, insn_count, flags)
4197 struct loop *loop;
4198 int insn_count;
4199 int flags;
4200 {
4201 struct loop_info *loop_info = LOOP_INFO (loop);
4202 struct loop_regs *regs = LOOP_REGS (loop);
4203 struct loop_ivs *ivs = LOOP_IVS (loop);
4204 rtx p;
4205 /* Temporary list pointer for traversing ivs->list. */
4206 struct iv_class *bl;
4207 /* Ratio of extra register life span we can justify
4208 for saving an instruction. More if loop doesn't call subroutines
4209 since in that case saving an insn makes more difference
4210 and more registers are available. */
4211 /* ??? could set this to last value of threshold in move_movables */
4212 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
4213 /* Map of pseudo-register replacements. */
4214 rtx *reg_map = NULL;
4215 int reg_map_size;
4216 int unrolled_insn_copies = 0;
4217 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
4218
4219 addr_placeholder = gen_reg_rtx (Pmode);
4220
4221 ivs->n_regs = max_reg_before_loop;
4222 ivs->regs = (struct iv *) xcalloc (ivs->n_regs, sizeof (struct iv));
4223
4224 /* Find all BIVs in loop. */
4225 loop_bivs_find (loop);
4226
4227 /* Exit if there are no bivs. */
4228 if (! ivs->list)
4229 {
4230 /* Can still unroll the loop anyways, but indicate that there is no
4231 strength reduction info available. */
4232 if (flags & LOOP_UNROLL)
4233 unroll_loop (loop, insn_count, 0);
4234
4235 loop_ivs_free (loop);
4236 return;
4237 }
4238
4239 /* Determine how BIVS are initialised by looking through pre-header
4240 extended basic block. */
4241 loop_bivs_init_find (loop);
4242
4243 /* Look at the each biv and see if we can say anything better about its
4244 initial value from any initializing insns set up above. */
4245 loop_bivs_check (loop);
4246
4247 /* Search the loop for general induction variables. */
4248 loop_givs_find (loop);
4249
4250 /* Try to calculate and save the number of loop iterations. This is
4251 set to zero if the actual number can not be calculated. This must
4252 be called after all giv's have been identified, since otherwise it may
4253 fail if the iteration variable is a giv. */
4254 loop_iterations (loop);
4255
4256 /* Now for each giv for which we still don't know whether or not it is
4257 replaceable, check to see if it is replaceable because its final value
4258 can be calculated. This must be done after loop_iterations is called,
4259 so that final_giv_value will work correctly. */
4260 loop_givs_check (loop);
4261
4262 /* Try to prove that the loop counter variable (if any) is always
4263 nonnegative; if so, record that fact with a REG_NONNEG note
4264 so that "decrement and branch until zero" insn can be used. */
4265 check_dbra_loop (loop, insn_count);
4266
4267 /* Create reg_map to hold substitutions for replaceable giv regs.
4268 Some givs might have been made from biv increments, so look at
4269 ivs->reg_iv_type for a suitable size. */
4270 reg_map_size = ivs->n_regs;
4271 reg_map = (rtx *) xcalloc (reg_map_size, sizeof (rtx));
4272
4273 /* Examine each iv class for feasibility of strength reduction/induction
4274 variable elimination. */
4275
4276 for (bl = ivs->list; bl; bl = bl->next)
4277 {
4278 struct induction *v;
4279 int benefit;
4280
4281 /* Test whether it will be possible to eliminate this biv
4282 provided all givs are reduced. */
4283 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
4284
4285 /* Check each extension dependent giv in this class to see if its
4286 root biv is safe from wrapping in the interior mode. */
4287 check_ext_dependant_givs (bl, loop_info);
4288
4289 /* Combine all giv's for this iv_class. */
4290 combine_givs (regs, bl);
4291
4292 /* This will be true at the end, if all givs which depend on this
4293 biv have been strength reduced.
4294 We can't (currently) eliminate the biv unless this is so. */
4295 bl->all_reduced = 1;
4296
4297 for (v = bl->giv; v; v = v->next_iv)
4298 {
4299 struct induction *tv;
4300
4301 if (v->ignore || v->same)
4302 continue;
4303
4304 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
4305
4306 /* If an insn is not to be strength reduced, then set its ignore
4307 flag, and clear bl->all_reduced. */
4308
4309 /* A giv that depends on a reversed biv must be reduced if it is
4310 used after the loop exit, otherwise, it would have the wrong
4311 value after the loop exit. To make it simple, just reduce all
4312 of such giv's whether or not we know they are used after the loop
4313 exit. */
4314
4315 if (! flag_reduce_all_givs
4316 && v->lifetime * threshold * benefit < insn_count
4317 && ! bl->reversed)
4318 {
4319 if (loop_dump_stream)
4320 fprintf (loop_dump_stream,
4321 "giv of insn %d not worth while, %d vs %d.\n",
4322 INSN_UID (v->insn),
4323 v->lifetime * threshold * benefit, insn_count);
4324 v->ignore = 1;
4325 bl->all_reduced = 0;
4326 }
4327 else
4328 {
4329 /* Check that we can increment the reduced giv without a
4330 multiply insn. If not, reject it. */
4331
4332 for (tv = bl->biv; tv; tv = tv->next_iv)
4333 if (tv->mult_val == const1_rtx
4334 && ! product_cheap_p (tv->add_val, v->mult_val))
4335 {
4336 if (loop_dump_stream)
4337 fprintf (loop_dump_stream,
4338 "giv of insn %d: would need a multiply.\n",
4339 INSN_UID (v->insn));
4340 v->ignore = 1;
4341 bl->all_reduced = 0;
4342 break;
4343 }
4344 }
4345 }
4346
4347 /* Check for givs whose first use is their definition and whose
4348 last use is the definition of another giv. If so, it is likely
4349 dead and should not be used to derive another giv nor to
4350 eliminate a biv. */
4351 loop_givs_dead_check (loop, bl);
4352
4353 /* Reduce each giv that we decided to reduce. */
4354 loop_givs_reduce (loop, bl);
4355
4356 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4357 as not reduced.
4358
4359 For each giv register that can be reduced now: if replaceable,
4360 substitute reduced reg wherever the old giv occurs;
4361 else add new move insn "giv_reg = reduced_reg". */
4362 loop_givs_rescan (loop, bl, reg_map);
4363
4364 /* All the givs based on the biv bl have been reduced if they
4365 merit it. */
4366
4367 /* For each giv not marked as maybe dead that has been combined with a
4368 second giv, clear any "maybe dead" mark on that second giv.
4369 v->new_reg will either be or refer to the register of the giv it
4370 combined with.
4371
4372 Doing this clearing avoids problems in biv elimination where
4373 a giv's new_reg is a complex value that can't be put in the
4374 insn but the giv combined with (with a reg as new_reg) is
4375 marked maybe_dead. Since the register will be used in either
4376 case, we'd prefer it be used from the simpler giv. */
4377
4378 for (v = bl->giv; v; v = v->next_iv)
4379 if (! v->maybe_dead && v->same)
4380 v->same->maybe_dead = 0;
4381
4382 /* Try to eliminate the biv, if it is a candidate.
4383 This won't work if ! bl->all_reduced,
4384 since the givs we planned to use might not have been reduced.
4385
4386 We have to be careful that we didn't initially think we could
4387 eliminate this biv because of a giv that we now think may be
4388 dead and shouldn't be used as a biv replacement.
4389
4390 Also, there is the possibility that we may have a giv that looks
4391 like it can be used to eliminate a biv, but the resulting insn
4392 isn't valid. This can happen, for example, on the 88k, where a
4393 JUMP_INSN can compare a register only with zero. Attempts to
4394 replace it with a compare with a constant will fail.
4395
4396 Note that in cases where this call fails, we may have replaced some
4397 of the occurrences of the biv with a giv, but no harm was done in
4398 doing so in the rare cases where it can occur. */
4399
4400 if (bl->all_reduced == 1 && bl->eliminable
4401 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
4402 {
4403 /* ?? If we created a new test to bypass the loop entirely,
4404 or otherwise drop straight in, based on this test, then
4405 we might want to rewrite it also. This way some later
4406 pass has more hope of removing the initialization of this
4407 biv entirely. */
4408
4409 /* If final_value != 0, then the biv may be used after loop end
4410 and we must emit an insn to set it just in case.
4411
4412 Reversed bivs already have an insn after the loop setting their
4413 value, so we don't need another one. We can't calculate the
4414 proper final value for such a biv here anyways. */
4415 if (bl->final_value && ! bl->reversed)
4416 loop_insn_sink_or_swim (loop, gen_move_insn
4417 (bl->biv->dest_reg, bl->final_value));
4418
4419 if (loop_dump_stream)
4420 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4421 bl->regno);
4422 }
4423 }
4424
4425 /* Go through all the instructions in the loop, making all the
4426 register substitutions scheduled in REG_MAP. */
4427
4428 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
4429 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4430 || GET_CODE (p) == CALL_INSN)
4431 {
4432 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
4433 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
4434 INSN_CODE (p) = -1;
4435 }
4436
4437 if (loop_info->n_iterations > 0)
4438 {
4439 /* When we completely unroll a loop we will likely not need the increment
4440 of the loop BIV and we will not need the conditional branch at the
4441 end of the loop. */
4442 unrolled_insn_copies = insn_count - 2;
4443
4444 #ifdef HAVE_cc0
4445 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
4446 need the comparison before the conditional branch at the end of the
4447 loop. */
4448 unrolled_insn_copies -= 1;
4449 #endif
4450
4451 /* We'll need one copy for each loop iteration. */
4452 unrolled_insn_copies *= loop_info->n_iterations;
4453
4454 /* A little slop to account for the ability to remove initialization
4455 code, better CSE, and other secondary benefits of completely
4456 unrolling some loops. */
4457 unrolled_insn_copies -= 1;
4458
4459 /* Clamp the value. */
4460 if (unrolled_insn_copies < 0)
4461 unrolled_insn_copies = 0;
4462 }
4463
4464 /* Unroll loops from within strength reduction so that we can use the
4465 induction variable information that strength_reduce has already
4466 collected. Always unroll loops that would be as small or smaller
4467 unrolled than when rolled. */
4468 if ((flags & LOOP_UNROLL)
4469 || (loop_info->n_iterations > 0
4470 && unrolled_insn_copies <= insn_count))
4471 unroll_loop (loop, insn_count, 1);
4472
4473 #ifdef HAVE_doloop_end
4474 if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
4475 doloop_optimize (loop);
4476 #endif /* HAVE_doloop_end */
4477
4478 if (loop_dump_stream)
4479 fprintf (loop_dump_stream, "\n");
4480
4481 loop_ivs_free (loop);
4482 if (reg_map)
4483 free (reg_map);
4484 }
4485 \f
4486 /*Record all basic induction variables calculated in the insn. */
4487 static rtx
4488 check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
4489 struct loop *loop;
4490 rtx p;
4491 int not_every_iteration;
4492 int maybe_multiple;
4493 {
4494 struct loop_ivs *ivs = LOOP_IVS (loop);
4495 rtx set;
4496 rtx dest_reg;
4497 rtx inc_val;
4498 rtx mult_val;
4499 rtx *location;
4500
4501 if (GET_CODE (p) == INSN
4502 && (set = single_set (p))
4503 && GET_CODE (SET_DEST (set)) == REG)
4504 {
4505 dest_reg = SET_DEST (set);
4506 if (REGNO (dest_reg) < max_reg_before_loop
4507 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
4508 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
4509 {
4510 if (basic_induction_var (loop, SET_SRC (set),
4511 GET_MODE (SET_SRC (set)),
4512 dest_reg, p, &inc_val, &mult_val,
4513 &location))
4514 {
4515 /* It is a possible basic induction variable.
4516 Create and initialize an induction structure for it. */
4517
4518 struct induction *v
4519 = (struct induction *) xmalloc (sizeof (struct induction));
4520
4521 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
4522 not_every_iteration, maybe_multiple);
4523 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
4524 }
4525 else if (REGNO (dest_reg) < ivs->n_regs)
4526 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
4527 }
4528 }
4529 return p;
4530 }
4531 \f
4532 /* Record all givs calculated in the insn.
4533 A register is a giv if: it is only set once, it is a function of a
4534 biv and a constant (or invariant), and it is not a biv. */
4535 static rtx
4536 check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
4537 struct loop *loop;
4538 rtx p;
4539 int not_every_iteration;
4540 int maybe_multiple;
4541 {
4542 struct loop_regs *regs = LOOP_REGS (loop);
4543
4544 rtx set;
4545 /* Look for a general induction variable in a register. */
4546 if (GET_CODE (p) == INSN
4547 && (set = single_set (p))
4548 && GET_CODE (SET_DEST (set)) == REG
4549 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
4550 {
4551 rtx src_reg;
4552 rtx dest_reg;
4553 rtx add_val;
4554 rtx mult_val;
4555 rtx ext_val;
4556 int benefit;
4557 rtx regnote = 0;
4558 rtx last_consec_insn;
4559
4560 dest_reg = SET_DEST (set);
4561 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
4562 return p;
4563
4564 if (/* SET_SRC is a giv. */
4565 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
4566 &mult_val, &ext_val, 0, &benefit, VOIDmode)
4567 /* Equivalent expression is a giv. */
4568 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
4569 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
4570 &add_val, &mult_val, &ext_val, 0,
4571 &benefit, VOIDmode)))
4572 /* Don't try to handle any regs made by loop optimization.
4573 We have nothing on them in regno_first_uid, etc. */
4574 && REGNO (dest_reg) < max_reg_before_loop
4575 /* Don't recognize a BASIC_INDUCT_VAR here. */
4576 && dest_reg != src_reg
4577 /* This must be the only place where the register is set. */
4578 && (regs->array[REGNO (dest_reg)].n_times_set == 1
4579 /* or all sets must be consecutive and make a giv. */
4580 || (benefit = consec_sets_giv (loop, benefit, p,
4581 src_reg, dest_reg,
4582 &add_val, &mult_val, &ext_val,
4583 &last_consec_insn))))
4584 {
4585 struct induction *v
4586 = (struct induction *) xmalloc (sizeof (struct induction));
4587
4588 /* If this is a library call, increase benefit. */
4589 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
4590 benefit += libcall_benefit (p);
4591
4592 /* Skip the consecutive insns, if there are any. */
4593 if (regs->array[REGNO (dest_reg)].n_times_set != 1)
4594 p = last_consec_insn;
4595
4596 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
4597 ext_val, benefit, DEST_REG, not_every_iteration,
4598 maybe_multiple, NULL_PTR);
4599
4600 }
4601 }
4602
4603 #ifndef DONT_REDUCE_ADDR
4604 /* Look for givs which are memory addresses. */
4605 /* This resulted in worse code on a VAX 8600. I wonder if it
4606 still does. */
4607 if (GET_CODE (p) == INSN)
4608 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
4609 maybe_multiple);
4610 #endif
4611
4612 /* Update the status of whether giv can derive other givs. This can
4613 change when we pass a label or an insn that updates a biv. */
4614 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4615 || GET_CODE (p) == CODE_LABEL)
4616 update_giv_derive (loop, p);
4617 return p;
4618 }
4619 \f
4620 /* Return 1 if X is a valid source for an initial value (or as value being
4621 compared against in an initial test).
4622
4623 X must be either a register or constant and must not be clobbered between
4624 the current insn and the start of the loop.
4625
4626 INSN is the insn containing X. */
4627
4628 static int
4629 valid_initial_value_p (x, insn, call_seen, loop_start)
4630 rtx x;
4631 rtx insn;
4632 int call_seen;
4633 rtx loop_start;
4634 {
4635 if (CONSTANT_P (x))
4636 return 1;
4637
4638 /* Only consider pseudos we know about initialized in insns whose luids
4639 we know. */
4640 if (GET_CODE (x) != REG
4641 || REGNO (x) >= max_reg_before_loop)
4642 return 0;
4643
4644 /* Don't use call-clobbered registers across a call which clobbers it. On
4645 some machines, don't use any hard registers at all. */
4646 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4647 && (SMALL_REGISTER_CLASSES
4648 || (call_used_regs[REGNO (x)] && call_seen)))
4649 return 0;
4650
4651 /* Don't use registers that have been clobbered before the start of the
4652 loop. */
4653 if (reg_set_between_p (x, insn, loop_start))
4654 return 0;
4655
4656 return 1;
4657 }
4658 \f
4659 /* Scan X for memory refs and check each memory address
4660 as a possible giv. INSN is the insn whose pattern X comes from.
4661 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4662 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
4663 more thanonce in each loop iteration. */
4664
4665 static void
4666 find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
4667 const struct loop *loop;
4668 rtx x;
4669 rtx insn;
4670 int not_every_iteration, maybe_multiple;
4671 {
4672 register int i, j;
4673 register enum rtx_code code;
4674 register const char *fmt;
4675
4676 if (x == 0)
4677 return;
4678
4679 code = GET_CODE (x);
4680 switch (code)
4681 {
4682 case REG:
4683 case CONST_INT:
4684 case CONST:
4685 case CONST_DOUBLE:
4686 case SYMBOL_REF:
4687 case LABEL_REF:
4688 case PC:
4689 case CC0:
4690 case ADDR_VEC:
4691 case ADDR_DIFF_VEC:
4692 case USE:
4693 case CLOBBER:
4694 return;
4695
4696 case MEM:
4697 {
4698 rtx src_reg;
4699 rtx add_val;
4700 rtx mult_val;
4701 rtx ext_val;
4702 int benefit;
4703
4704 /* This code used to disable creating GIVs with mult_val == 1 and
4705 add_val == 0. However, this leads to lost optimizations when
4706 it comes time to combine a set of related DEST_ADDR GIVs, since
4707 this one would not be seen. */
4708
4709 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
4710 &mult_val, &ext_val, 1, &benefit,
4711 GET_MODE (x)))
4712 {
4713 /* Found one; record it. */
4714 struct induction *v
4715 = (struct induction *) xmalloc (sizeof (struct induction));
4716
4717 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
4718 add_val, ext_val, benefit, DEST_ADDR,
4719 not_every_iteration, maybe_multiple, &XEXP (x, 0));
4720
4721 v->mem_mode = GET_MODE (x);
4722 }
4723 }
4724 return;
4725
4726 default:
4727 break;
4728 }
4729
4730 /* Recursively scan the subexpressions for other mem refs. */
4731
4732 fmt = GET_RTX_FORMAT (code);
4733 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4734 if (fmt[i] == 'e')
4735 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
4736 maybe_multiple);
4737 else if (fmt[i] == 'E')
4738 for (j = 0; j < XVECLEN (x, i); j++)
4739 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
4740 maybe_multiple);
4741 }
4742 \f
4743 /* Fill in the data about one biv update.
4744 V is the `struct induction' in which we record the biv. (It is
4745 allocated by the caller, with alloca.)
4746 INSN is the insn that sets it.
4747 DEST_REG is the biv's reg.
4748
4749 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4750 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4751 being set to INC_VAL.
4752
4753 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4754 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4755 can be executed more than once per iteration. If MAYBE_MULTIPLE
4756 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4757 executed exactly once per iteration. */
4758
4759 static void
4760 record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
4761 not_every_iteration, maybe_multiple)
4762 struct loop *loop;
4763 struct induction *v;
4764 rtx insn;
4765 rtx dest_reg;
4766 rtx inc_val;
4767 rtx mult_val;
4768 rtx *location;
4769 int not_every_iteration;
4770 int maybe_multiple;
4771 {
4772 struct loop_ivs *ivs = LOOP_IVS (loop);
4773 struct iv_class *bl;
4774
4775 v->insn = insn;
4776 v->src_reg = dest_reg;
4777 v->dest_reg = dest_reg;
4778 v->mult_val = mult_val;
4779 v->add_val = inc_val;
4780 v->ext_dependant = NULL_RTX;
4781 v->location = location;
4782 v->mode = GET_MODE (dest_reg);
4783 v->always_computable = ! not_every_iteration;
4784 v->always_executed = ! not_every_iteration;
4785 v->maybe_multiple = maybe_multiple;
4786
4787 /* Add this to the reg's iv_class, creating a class
4788 if this is the first incrementation of the reg. */
4789
4790 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
4791 if (bl == 0)
4792 {
4793 /* Create and initialize new iv_class. */
4794
4795 bl = (struct iv_class *) xmalloc (sizeof (struct iv_class));
4796
4797 bl->regno = REGNO (dest_reg);
4798 bl->biv = 0;
4799 bl->giv = 0;
4800 bl->biv_count = 0;
4801 bl->giv_count = 0;
4802
4803 /* Set initial value to the reg itself. */
4804 bl->initial_value = dest_reg;
4805 bl->final_value = 0;
4806 /* We haven't seen the initializing insn yet */
4807 bl->init_insn = 0;
4808 bl->init_set = 0;
4809 bl->initial_test = 0;
4810 bl->incremented = 0;
4811 bl->eliminable = 0;
4812 bl->nonneg = 0;
4813 bl->reversed = 0;
4814 bl->total_benefit = 0;
4815
4816 /* Add this class to ivs->list. */
4817 bl->next = ivs->list;
4818 ivs->list = bl;
4819
4820 /* Put it in the array of biv register classes. */
4821 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
4822 }
4823
4824 /* Update IV_CLASS entry for this biv. */
4825 v->next_iv = bl->biv;
4826 bl->biv = v;
4827 bl->biv_count++;
4828 if (mult_val == const1_rtx)
4829 bl->incremented = 1;
4830
4831 if (loop_dump_stream)
4832 loop_biv_dump (v, loop_dump_stream, 0);
4833 }
4834 \f
4835 /* Fill in the data about one giv.
4836 V is the `struct induction' in which we record the giv. (It is
4837 allocated by the caller, with alloca.)
4838 INSN is the insn that sets it.
4839 BENEFIT estimates the savings from deleting this insn.
4840 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4841 into a register or is used as a memory address.
4842
4843 SRC_REG is the biv reg which the giv is computed from.
4844 DEST_REG is the giv's reg (if the giv is stored in a reg).
4845 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4846 LOCATION points to the place where this giv's value appears in INSN. */
4847
4848 static void
4849 record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
4850 benefit, type, not_every_iteration, maybe_multiple, location)
4851 const struct loop *loop;
4852 struct induction *v;
4853 rtx insn;
4854 rtx src_reg;
4855 rtx dest_reg;
4856 rtx mult_val, add_val, ext_val;
4857 int benefit;
4858 enum g_types type;
4859 int not_every_iteration, maybe_multiple;
4860 rtx *location;
4861 {
4862 struct loop_ivs *ivs = LOOP_IVS (loop);
4863 struct induction *b;
4864 struct iv_class *bl;
4865 rtx set = single_set (insn);
4866 rtx temp;
4867
4868 /* Attempt to prove constantness of the values. */
4869 temp = simplify_rtx (add_val);
4870 if (temp)
4871 add_val = temp;
4872
4873 v->insn = insn;
4874 v->src_reg = src_reg;
4875 v->giv_type = type;
4876 v->dest_reg = dest_reg;
4877 v->mult_val = mult_val;
4878 v->add_val = add_val;
4879 v->ext_dependant = ext_val;
4880 v->benefit = benefit;
4881 v->location = location;
4882 v->cant_derive = 0;
4883 v->combined_with = 0;
4884 v->maybe_multiple = maybe_multiple;
4885 v->maybe_dead = 0;
4886 v->derive_adjustment = 0;
4887 v->same = 0;
4888 v->ignore = 0;
4889 v->new_reg = 0;
4890 v->final_value = 0;
4891 v->same_insn = 0;
4892 v->auto_inc_opt = 0;
4893 v->unrolled = 0;
4894 v->shared = 0;
4895
4896 /* The v->always_computable field is used in update_giv_derive, to
4897 determine whether a giv can be used to derive another giv. For a
4898 DEST_REG giv, INSN computes a new value for the giv, so its value
4899 isn't computable if INSN insn't executed every iteration.
4900 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4901 it does not compute a new value. Hence the value is always computable
4902 regardless of whether INSN is executed each iteration. */
4903
4904 if (type == DEST_ADDR)
4905 v->always_computable = 1;
4906 else
4907 v->always_computable = ! not_every_iteration;
4908
4909 v->always_executed = ! not_every_iteration;
4910
4911 if (type == DEST_ADDR)
4912 {
4913 v->mode = GET_MODE (*location);
4914 v->lifetime = 1;
4915 }
4916 else /* type == DEST_REG */
4917 {
4918 v->mode = GET_MODE (SET_DEST (set));
4919
4920 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
4921
4922 /* If the lifetime is zero, it means that this register is
4923 really a dead store. So mark this as a giv that can be
4924 ignored. This will not prevent the biv from being eliminated. */
4925 if (v->lifetime == 0)
4926 v->ignore = 1;
4927
4928 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
4929 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
4930 }
4931
4932 /* Add the giv to the class of givs computed from one biv. */
4933
4934 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
4935 if (bl)
4936 {
4937 v->next_iv = bl->giv;
4938 bl->giv = v;
4939 /* Don't count DEST_ADDR. This is supposed to count the number of
4940 insns that calculate givs. */
4941 if (type == DEST_REG)
4942 bl->giv_count++;
4943 bl->total_benefit += benefit;
4944 }
4945 else
4946 /* Fatal error, biv missing for this giv? */
4947 abort ();
4948
4949 if (type == DEST_ADDR)
4950 v->replaceable = 1;
4951 else
4952 {
4953 /* The giv can be replaced outright by the reduced register only if all
4954 of the following conditions are true:
4955 - the insn that sets the giv is always executed on any iteration
4956 on which the giv is used at all
4957 (there are two ways to deduce this:
4958 either the insn is executed on every iteration,
4959 or all uses follow that insn in the same basic block),
4960 - the giv is not used outside the loop
4961 - no assignments to the biv occur during the giv's lifetime. */
4962
4963 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4964 /* Previous line always fails if INSN was moved by loop opt. */
4965 && REGNO_LAST_LUID (REGNO (dest_reg))
4966 < INSN_LUID (loop->end)
4967 && (! not_every_iteration
4968 || last_use_this_basic_block (dest_reg, insn)))
4969 {
4970 /* Now check that there are no assignments to the biv within the
4971 giv's lifetime. This requires two separate checks. */
4972
4973 /* Check each biv update, and fail if any are between the first
4974 and last use of the giv.
4975
4976 If this loop contains an inner loop that was unrolled, then
4977 the insn modifying the biv may have been emitted by the loop
4978 unrolling code, and hence does not have a valid luid. Just
4979 mark the biv as not replaceable in this case. It is not very
4980 useful as a biv, because it is used in two different loops.
4981 It is very unlikely that we would be able to optimize the giv
4982 using this biv anyways. */
4983
4984 v->replaceable = 1;
4985 for (b = bl->biv; b; b = b->next_iv)
4986 {
4987 if (INSN_UID (b->insn) >= max_uid_for_loop
4988 || ((INSN_LUID (b->insn)
4989 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
4990 && (INSN_LUID (b->insn)
4991 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
4992 {
4993 v->replaceable = 0;
4994 v->not_replaceable = 1;
4995 break;
4996 }
4997 }
4998
4999 /* If there are any backwards branches that go from after the
5000 biv update to before it, then this giv is not replaceable. */
5001 if (v->replaceable)
5002 for (b = bl->biv; b; b = b->next_iv)
5003 if (back_branch_in_range_p (loop, b->insn))
5004 {
5005 v->replaceable = 0;
5006 v->not_replaceable = 1;
5007 break;
5008 }
5009 }
5010 else
5011 {
5012 /* May still be replaceable, we don't have enough info here to
5013 decide. */
5014 v->replaceable = 0;
5015 v->not_replaceable = 0;
5016 }
5017 }
5018
5019 /* Record whether the add_val contains a const_int, for later use by
5020 combine_givs. */
5021 {
5022 rtx tem = add_val;
5023
5024 v->no_const_addval = 1;
5025 if (tem == const0_rtx)
5026 ;
5027 else if (CONSTANT_P (add_val))
5028 v->no_const_addval = 0;
5029 if (GET_CODE (tem) == PLUS)
5030 {
5031 while (1)
5032 {
5033 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5034 tem = XEXP (tem, 0);
5035 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5036 tem = XEXP (tem, 1);
5037 else
5038 break;
5039 }
5040 if (CONSTANT_P (XEXP (tem, 1)))
5041 v->no_const_addval = 0;
5042 }
5043 }
5044
5045 if (loop_dump_stream)
5046 loop_giv_dump (v, loop_dump_stream, 0);
5047 }
5048
5049 /* All this does is determine whether a giv can be made replaceable because
5050 its final value can be calculated. This code can not be part of record_giv
5051 above, because final_giv_value requires that the number of loop iterations
5052 be known, and that can not be accurately calculated until after all givs
5053 have been identified. */
5054
5055 static void
5056 check_final_value (loop, v)
5057 const struct loop *loop;
5058 struct induction *v;
5059 {
5060 struct loop_ivs *ivs = LOOP_IVS (loop);
5061 struct iv_class *bl;
5062 rtx final_value = 0;
5063
5064 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
5065
5066 /* DEST_ADDR givs will never reach here, because they are always marked
5067 replaceable above in record_giv. */
5068
5069 /* The giv can be replaced outright by the reduced register only if all
5070 of the following conditions are true:
5071 - the insn that sets the giv is always executed on any iteration
5072 on which the giv is used at all
5073 (there are two ways to deduce this:
5074 either the insn is executed on every iteration,
5075 or all uses follow that insn in the same basic block),
5076 - its final value can be calculated (this condition is different
5077 than the one above in record_giv)
5078 - it's not used before the it's set
5079 - no assignments to the biv occur during the giv's lifetime. */
5080
5081 #if 0
5082 /* This is only called now when replaceable is known to be false. */
5083 /* Clear replaceable, so that it won't confuse final_giv_value. */
5084 v->replaceable = 0;
5085 #endif
5086
5087 if ((final_value = final_giv_value (loop, v))
5088 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5089 {
5090 int biv_increment_seen = 0, before_giv_insn = 0;
5091 rtx p = v->insn;
5092 rtx last_giv_use;
5093
5094 v->replaceable = 1;
5095
5096 /* When trying to determine whether or not a biv increment occurs
5097 during the lifetime of the giv, we can ignore uses of the variable
5098 outside the loop because final_value is true. Hence we can not
5099 use regno_last_uid and regno_first_uid as above in record_giv. */
5100
5101 /* Search the loop to determine whether any assignments to the
5102 biv occur during the giv's lifetime. Start with the insn
5103 that sets the giv, and search around the loop until we come
5104 back to that insn again.
5105
5106 Also fail if there is a jump within the giv's lifetime that jumps
5107 to somewhere outside the lifetime but still within the loop. This
5108 catches spaghetti code where the execution order is not linear, and
5109 hence the above test fails. Here we assume that the giv lifetime
5110 does not extend from one iteration of the loop to the next, so as
5111 to make the test easier. Since the lifetime isn't known yet,
5112 this requires two loops. See also record_giv above. */
5113
5114 last_giv_use = v->insn;
5115
5116 while (1)
5117 {
5118 p = NEXT_INSN (p);
5119 if (p == loop->end)
5120 {
5121 before_giv_insn = 1;
5122 p = NEXT_INSN (loop->start);
5123 }
5124 if (p == v->insn)
5125 break;
5126
5127 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5128 || GET_CODE (p) == CALL_INSN)
5129 {
5130 /* It is possible for the BIV increment to use the GIV if we
5131 have a cycle. Thus we must be sure to check each insn for
5132 both BIV and GIV uses, and we must check for BIV uses
5133 first. */
5134
5135 if (! biv_increment_seen
5136 && reg_set_p (v->src_reg, PATTERN (p)))
5137 biv_increment_seen = 1;
5138
5139 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5140 {
5141 if (biv_increment_seen || before_giv_insn)
5142 {
5143 v->replaceable = 0;
5144 v->not_replaceable = 1;
5145 break;
5146 }
5147 last_giv_use = p;
5148 }
5149 }
5150 }
5151
5152 /* Now that the lifetime of the giv is known, check for branches
5153 from within the lifetime to outside the lifetime if it is still
5154 replaceable. */
5155
5156 if (v->replaceable)
5157 {
5158 p = v->insn;
5159 while (1)
5160 {
5161 p = NEXT_INSN (p);
5162 if (p == loop->end)
5163 p = NEXT_INSN (loop->start);
5164 if (p == last_giv_use)
5165 break;
5166
5167 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5168 && LABEL_NAME (JUMP_LABEL (p))
5169 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
5170 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
5171 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
5172 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
5173 {
5174 v->replaceable = 0;
5175 v->not_replaceable = 1;
5176
5177 if (loop_dump_stream)
5178 fprintf (loop_dump_stream,
5179 "Found branch outside giv lifetime.\n");
5180
5181 break;
5182 }
5183 }
5184 }
5185
5186 /* If it is replaceable, then save the final value. */
5187 if (v->replaceable)
5188 v->final_value = final_value;
5189 }
5190
5191 if (loop_dump_stream && v->replaceable)
5192 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5193 INSN_UID (v->insn), REGNO (v->dest_reg));
5194 }
5195 \f
5196 /* Update the status of whether a giv can derive other givs.
5197
5198 We need to do something special if there is or may be an update to the biv
5199 between the time the giv is defined and the time it is used to derive
5200 another giv.
5201
5202 In addition, a giv that is only conditionally set is not allowed to
5203 derive another giv once a label has been passed.
5204
5205 The cases we look at are when a label or an update to a biv is passed. */
5206
5207 static void
5208 update_giv_derive (loop, p)
5209 const struct loop *loop;
5210 rtx p;
5211 {
5212 struct loop_ivs *ivs = LOOP_IVS (loop);
5213 struct iv_class *bl;
5214 struct induction *biv, *giv;
5215 rtx tem;
5216 int dummy;
5217
5218 /* Search all IV classes, then all bivs, and finally all givs.
5219
5220 There are three cases we are concerned with. First we have the situation
5221 of a giv that is only updated conditionally. In that case, it may not
5222 derive any givs after a label is passed.
5223
5224 The second case is when a biv update occurs, or may occur, after the
5225 definition of a giv. For certain biv updates (see below) that are
5226 known to occur between the giv definition and use, we can adjust the
5227 giv definition. For others, or when the biv update is conditional,
5228 we must prevent the giv from deriving any other givs. There are two
5229 sub-cases within this case.
5230
5231 If this is a label, we are concerned with any biv update that is done
5232 conditionally, since it may be done after the giv is defined followed by
5233 a branch here (actually, we need to pass both a jump and a label, but
5234 this extra tracking doesn't seem worth it).
5235
5236 If this is a jump, we are concerned about any biv update that may be
5237 executed multiple times. We are actually only concerned about
5238 backward jumps, but it is probably not worth performing the test
5239 on the jump again here.
5240
5241 If this is a biv update, we must adjust the giv status to show that a
5242 subsequent biv update was performed. If this adjustment cannot be done,
5243 the giv cannot derive further givs. */
5244
5245 for (bl = ivs->list; bl; bl = bl->next)
5246 for (biv = bl->biv; biv; biv = biv->next_iv)
5247 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5248 || biv->insn == p)
5249 {
5250 for (giv = bl->giv; giv; giv = giv->next_iv)
5251 {
5252 /* If cant_derive is already true, there is no point in
5253 checking all of these conditions again. */
5254 if (giv->cant_derive)
5255 continue;
5256
5257 /* If this giv is conditionally set and we have passed a label,
5258 it cannot derive anything. */
5259 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5260 giv->cant_derive = 1;
5261
5262 /* Skip givs that have mult_val == 0, since
5263 they are really invariants. Also skip those that are
5264 replaceable, since we know their lifetime doesn't contain
5265 any biv update. */
5266 else if (giv->mult_val == const0_rtx || giv->replaceable)
5267 continue;
5268
5269 /* The only way we can allow this giv to derive another
5270 is if this is a biv increment and we can form the product
5271 of biv->add_val and giv->mult_val. In this case, we will
5272 be able to compute a compensation. */
5273 else if (biv->insn == p)
5274 {
5275 rtx ext_val_dummy;
5276
5277 tem = 0;
5278 if (biv->mult_val == const1_rtx)
5279 tem = simplify_giv_expr (loop,
5280 gen_rtx_MULT (giv->mode,
5281 biv->add_val,
5282 giv->mult_val),
5283 &ext_val_dummy, &dummy);
5284
5285 if (tem && giv->derive_adjustment)
5286 tem = simplify_giv_expr
5287 (loop,
5288 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
5289 &ext_val_dummy, &dummy);
5290
5291 if (tem)
5292 giv->derive_adjustment = tem;
5293 else
5294 giv->cant_derive = 1;
5295 }
5296 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5297 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5298 giv->cant_derive = 1;
5299 }
5300 }
5301 }
5302 \f
5303 /* Check whether an insn is an increment legitimate for a basic induction var.
5304 X is the source of insn P, or a part of it.
5305 MODE is the mode in which X should be interpreted.
5306
5307 DEST_REG is the putative biv, also the destination of the insn.
5308 We accept patterns of these forms:
5309 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5310 REG = INVARIANT + REG
5311
5312 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5313 store the additive term into *INC_VAL, and store the place where
5314 we found the additive term into *LOCATION.
5315
5316 If X is an assignment of an invariant into DEST_REG, we set
5317 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5318
5319 We also want to detect a BIV when it corresponds to a variable
5320 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5321 of the variable may be a PLUS that adds a SUBREG of that variable to
5322 an invariant and then sign- or zero-extends the result of the PLUS
5323 into the variable.
5324
5325 Most GIVs in such cases will be in the promoted mode, since that is the
5326 probably the natural computation mode (and almost certainly the mode
5327 used for addresses) on the machine. So we view the pseudo-reg containing
5328 the variable as the BIV, as if it were simply incremented.
5329
5330 Note that treating the entire pseudo as a BIV will result in making
5331 simple increments to any GIVs based on it. However, if the variable
5332 overflows in its declared mode but not its promoted mode, the result will
5333 be incorrect. This is acceptable if the variable is signed, since
5334 overflows in such cases are undefined, but not if it is unsigned, since
5335 those overflows are defined. So we only check for SIGN_EXTEND and
5336 not ZERO_EXTEND.
5337
5338 If we cannot find a biv, we return 0. */
5339
5340 static int
5341 basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
5342 const struct loop *loop;
5343 register rtx x;
5344 enum machine_mode mode;
5345 rtx dest_reg;
5346 rtx p;
5347 rtx *inc_val;
5348 rtx *mult_val;
5349 rtx **location;
5350 {
5351 register enum rtx_code code;
5352 rtx *argp, arg;
5353 rtx insn, set = 0;
5354
5355 code = GET_CODE (x);
5356 *location = NULL;
5357 switch (code)
5358 {
5359 case PLUS:
5360 if (rtx_equal_p (XEXP (x, 0), dest_reg)
5361 || (GET_CODE (XEXP (x, 0)) == SUBREG
5362 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5363 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5364 {
5365 argp = &XEXP (x, 1);
5366 }
5367 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
5368 || (GET_CODE (XEXP (x, 1)) == SUBREG
5369 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5370 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5371 {
5372 argp = &XEXP (x, 0);
5373 }
5374 else
5375 return 0;
5376
5377 arg = *argp;
5378 if (loop_invariant_p (loop, arg) != 1)
5379 return 0;
5380
5381 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5382 *mult_val = const1_rtx;
5383 *location = argp;
5384 return 1;
5385
5386 case SUBREG:
5387 /* If this is a SUBREG for a promoted variable, check the inner
5388 value. */
5389 if (SUBREG_PROMOTED_VAR_P (x))
5390 return basic_induction_var (loop, SUBREG_REG (x),
5391 GET_MODE (SUBREG_REG (x)),
5392 dest_reg, p, inc_val, mult_val, location);
5393 return 0;
5394
5395 case REG:
5396 /* If this register is assigned in a previous insn, look at its
5397 source, but don't go outside the loop or past a label. */
5398
5399 /* If this sets a register to itself, we would repeat any previous
5400 biv increment if we applied this strategy blindly. */
5401 if (rtx_equal_p (dest_reg, x))
5402 return 0;
5403
5404 insn = p;
5405 while (1)
5406 {
5407 rtx dest;
5408 do
5409 {
5410 insn = PREV_INSN (insn);
5411 }
5412 while (insn && GET_CODE (insn) == NOTE
5413 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5414
5415 if (!insn)
5416 break;
5417 set = single_set (insn);
5418 if (set == 0)
5419 break;
5420 dest = SET_DEST (set);
5421 if (dest == x
5422 || (GET_CODE (dest) == SUBREG
5423 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
5424 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
5425 && SUBREG_REG (dest) == x))
5426 return basic_induction_var (loop, SET_SRC (set),
5427 (GET_MODE (SET_SRC (set)) == VOIDmode
5428 ? GET_MODE (x)
5429 : GET_MODE (SET_SRC (set))),
5430 dest_reg, insn,
5431 inc_val, mult_val, location);
5432
5433 while (GET_CODE (dest) == SIGN_EXTRACT
5434 || GET_CODE (dest) == ZERO_EXTRACT
5435 || GET_CODE (dest) == SUBREG
5436 || GET_CODE (dest) == STRICT_LOW_PART)
5437 dest = XEXP (dest, 0);
5438 if (dest == x)
5439 break;
5440 }
5441 /* Fall through. */
5442
5443 /* Can accept constant setting of biv only when inside inner most loop.
5444 Otherwise, a biv of an inner loop may be incorrectly recognized
5445 as a biv of the outer loop,
5446 causing code to be moved INTO the inner loop. */
5447 case MEM:
5448 if (loop_invariant_p (loop, x) != 1)
5449 return 0;
5450 case CONST_INT:
5451 case SYMBOL_REF:
5452 case CONST:
5453 /* convert_modes aborts if we try to convert to or from CCmode, so just
5454 exclude that case. It is very unlikely that a condition code value
5455 would be a useful iterator anyways. */
5456 if (loop->level == 1
5457 && GET_MODE_CLASS (mode) != MODE_CC
5458 && GET_MODE_CLASS (GET_MODE (dest_reg)) != MODE_CC)
5459 {
5460 /* Possible bug here? Perhaps we don't know the mode of X. */
5461 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5462 *mult_val = const0_rtx;
5463 return 1;
5464 }
5465 else
5466 return 0;
5467
5468 case SIGN_EXTEND:
5469 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5470 dest_reg, p, inc_val, mult_val, location);
5471
5472 case ASHIFTRT:
5473 /* Similar, since this can be a sign extension. */
5474 for (insn = PREV_INSN (p);
5475 (insn && GET_CODE (insn) == NOTE
5476 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5477 insn = PREV_INSN (insn))
5478 ;
5479
5480 if (insn)
5481 set = single_set (insn);
5482
5483 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
5484 && set && SET_DEST (set) == XEXP (x, 0)
5485 && GET_CODE (XEXP (x, 1)) == CONST_INT
5486 && INTVAL (XEXP (x, 1)) >= 0
5487 && GET_CODE (SET_SRC (set)) == ASHIFT
5488 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5489 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
5490 GET_MODE (XEXP (x, 0)),
5491 dest_reg, insn, inc_val, mult_val,
5492 location);
5493 return 0;
5494
5495 default:
5496 return 0;
5497 }
5498 }
5499 \f
5500 /* A general induction variable (giv) is any quantity that is a linear
5501 function of a basic induction variable,
5502 i.e. giv = biv * mult_val + add_val.
5503 The coefficients can be any loop invariant quantity.
5504 A giv need not be computed directly from the biv;
5505 it can be computed by way of other givs. */
5506
5507 /* Determine whether X computes a giv.
5508 If it does, return a nonzero value
5509 which is the benefit from eliminating the computation of X;
5510 set *SRC_REG to the register of the biv that it is computed from;
5511 set *ADD_VAL and *MULT_VAL to the coefficients,
5512 such that the value of X is biv * mult + add; */
5513
5514 static int
5515 general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
5516 is_addr, pbenefit, addr_mode)
5517 const struct loop *loop;
5518 rtx x;
5519 rtx *src_reg;
5520 rtx *add_val;
5521 rtx *mult_val;
5522 rtx *ext_val;
5523 int is_addr;
5524 int *pbenefit;
5525 enum machine_mode addr_mode;
5526 {
5527 struct loop_ivs *ivs = LOOP_IVS (loop);
5528 rtx orig_x = x;
5529
5530 /* If this is an invariant, forget it, it isn't a giv. */
5531 if (loop_invariant_p (loop, x) == 1)
5532 return 0;
5533
5534 *pbenefit = 0;
5535 *ext_val = NULL_RTX;
5536 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
5537 if (x == 0)
5538 return 0;
5539
5540 switch (GET_CODE (x))
5541 {
5542 case USE:
5543 case CONST_INT:
5544 /* Since this is now an invariant and wasn't before, it must be a giv
5545 with MULT_VAL == 0. It doesn't matter which BIV we associate this
5546 with. */
5547 *src_reg = ivs->list->biv->dest_reg;
5548 *mult_val = const0_rtx;
5549 *add_val = x;
5550 break;
5551
5552 case REG:
5553 /* This is equivalent to a BIV. */
5554 *src_reg = x;
5555 *mult_val = const1_rtx;
5556 *add_val = const0_rtx;
5557 break;
5558
5559 case PLUS:
5560 /* Either (plus (biv) (invar)) or
5561 (plus (mult (biv) (invar_1)) (invar_2)). */
5562 if (GET_CODE (XEXP (x, 0)) == MULT)
5563 {
5564 *src_reg = XEXP (XEXP (x, 0), 0);
5565 *mult_val = XEXP (XEXP (x, 0), 1);
5566 }
5567 else
5568 {
5569 *src_reg = XEXP (x, 0);
5570 *mult_val = const1_rtx;
5571 }
5572 *add_val = XEXP (x, 1);
5573 break;
5574
5575 case MULT:
5576 /* ADD_VAL is zero. */
5577 *src_reg = XEXP (x, 0);
5578 *mult_val = XEXP (x, 1);
5579 *add_val = const0_rtx;
5580 break;
5581
5582 default:
5583 abort ();
5584 }
5585
5586 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5587 unless they are CONST_INT). */
5588 if (GET_CODE (*add_val) == USE)
5589 *add_val = XEXP (*add_val, 0);
5590 if (GET_CODE (*mult_val) == USE)
5591 *mult_val = XEXP (*mult_val, 0);
5592
5593 if (is_addr)
5594 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
5595 else
5596 *pbenefit += rtx_cost (orig_x, SET);
5597
5598 /* Always return true if this is a giv so it will be detected as such,
5599 even if the benefit is zero or negative. This allows elimination
5600 of bivs that might otherwise not be eliminated. */
5601 return 1;
5602 }
5603 \f
5604 /* Given an expression, X, try to form it as a linear function of a biv.
5605 We will canonicalize it to be of the form
5606 (plus (mult (BIV) (invar_1))
5607 (invar_2))
5608 with possible degeneracies.
5609
5610 The invariant expressions must each be of a form that can be used as a
5611 machine operand. We surround then with a USE rtx (a hack, but localized
5612 and certainly unambiguous!) if not a CONST_INT for simplicity in this
5613 routine; it is the caller's responsibility to strip them.
5614
5615 If no such canonicalization is possible (i.e., two biv's are used or an
5616 expression that is neither invariant nor a biv or giv), this routine
5617 returns 0.
5618
5619 For a non-zero return, the result will have a code of CONST_INT, USE,
5620 REG (for a BIV), PLUS, or MULT. No other codes will occur.
5621
5622 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
5623
5624 static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
5625 static rtx sge_plus_constant PARAMS ((rtx, rtx));
5626
5627 static rtx
5628 simplify_giv_expr (loop, x, ext_val, benefit)
5629 const struct loop *loop;
5630 rtx x;
5631 rtx *ext_val;
5632 int *benefit;
5633 {
5634 struct loop_ivs *ivs = LOOP_IVS (loop);
5635 struct loop_regs *regs = LOOP_REGS (loop);
5636 enum machine_mode mode = GET_MODE (x);
5637 rtx arg0, arg1;
5638 rtx tem;
5639
5640 /* If this is not an integer mode, or if we cannot do arithmetic in this
5641 mode, this can't be a giv. */
5642 if (mode != VOIDmode
5643 && (GET_MODE_CLASS (mode) != MODE_INT
5644 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5645 return NULL_RTX;
5646
5647 switch (GET_CODE (x))
5648 {
5649 case PLUS:
5650 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
5651 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
5652 if (arg0 == 0 || arg1 == 0)
5653 return NULL_RTX;
5654
5655 /* Put constant last, CONST_INT last if both constant. */
5656 if ((GET_CODE (arg0) == USE
5657 || GET_CODE (arg0) == CONST_INT)
5658 && ! ((GET_CODE (arg0) == USE
5659 && GET_CODE (arg1) == USE)
5660 || GET_CODE (arg1) == CONST_INT))
5661 tem = arg0, arg0 = arg1, arg1 = tem;
5662
5663 /* Handle addition of zero, then addition of an invariant. */
5664 if (arg1 == const0_rtx)
5665 return arg0;
5666 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5667 switch (GET_CODE (arg0))
5668 {
5669 case CONST_INT:
5670 case USE:
5671 /* Adding two invariants must result in an invariant, so enclose
5672 addition operation inside a USE and return it. */
5673 if (GET_CODE (arg0) == USE)
5674 arg0 = XEXP (arg0, 0);
5675 if (GET_CODE (arg1) == USE)
5676 arg1 = XEXP (arg1, 0);
5677
5678 if (GET_CODE (arg0) == CONST_INT)
5679 tem = arg0, arg0 = arg1, arg1 = tem;
5680 if (GET_CODE (arg1) == CONST_INT)
5681 tem = sge_plus_constant (arg0, arg1);
5682 else
5683 tem = sge_plus (mode, arg0, arg1);
5684
5685 if (GET_CODE (tem) != CONST_INT)
5686 tem = gen_rtx_USE (mode, tem);
5687 return tem;
5688
5689 case REG:
5690 case MULT:
5691 /* biv + invar or mult + invar. Return sum. */
5692 return gen_rtx_PLUS (mode, arg0, arg1);
5693
5694 case PLUS:
5695 /* (a + invar_1) + invar_2. Associate. */
5696 return
5697 simplify_giv_expr (loop,
5698 gen_rtx_PLUS (mode,
5699 XEXP (arg0, 0),
5700 gen_rtx_PLUS (mode,
5701 XEXP (arg0, 1),
5702 arg1)),
5703 ext_val, benefit);
5704
5705 default:
5706 abort ();
5707 }
5708
5709 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5710 MULT to reduce cases. */
5711 if (GET_CODE (arg0) == REG)
5712 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
5713 if (GET_CODE (arg1) == REG)
5714 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
5715
5716 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5717 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5718 Recurse to associate the second PLUS. */
5719 if (GET_CODE (arg1) == MULT)
5720 tem = arg0, arg0 = arg1, arg1 = tem;
5721
5722 if (GET_CODE (arg1) == PLUS)
5723 return
5724 simplify_giv_expr (loop,
5725 gen_rtx_PLUS (mode,
5726 gen_rtx_PLUS (mode, arg0,
5727 XEXP (arg1, 0)),
5728 XEXP (arg1, 1)),
5729 ext_val, benefit);
5730
5731 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5732 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5733 return NULL_RTX;
5734
5735 if (!rtx_equal_p (arg0, arg1))
5736 return NULL_RTX;
5737
5738 return simplify_giv_expr (loop,
5739 gen_rtx_MULT (mode,
5740 XEXP (arg0, 0),
5741 gen_rtx_PLUS (mode,
5742 XEXP (arg0, 1),
5743 XEXP (arg1, 1))),
5744 ext_val, benefit);
5745
5746 case MINUS:
5747 /* Handle "a - b" as "a + b * (-1)". */
5748 return simplify_giv_expr (loop,
5749 gen_rtx_PLUS (mode,
5750 XEXP (x, 0),
5751 gen_rtx_MULT (mode,
5752 XEXP (x, 1),
5753 constm1_rtx)),
5754 ext_val, benefit);
5755
5756 case MULT:
5757 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
5758 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
5759 if (arg0 == 0 || arg1 == 0)
5760 return NULL_RTX;
5761
5762 /* Put constant last, CONST_INT last if both constant. */
5763 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5764 && GET_CODE (arg1) != CONST_INT)
5765 tem = arg0, arg0 = arg1, arg1 = tem;
5766
5767 /* If second argument is not now constant, not giv. */
5768 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5769 return NULL_RTX;
5770
5771 /* Handle multiply by 0 or 1. */
5772 if (arg1 == const0_rtx)
5773 return const0_rtx;
5774
5775 else if (arg1 == const1_rtx)
5776 return arg0;
5777
5778 switch (GET_CODE (arg0))
5779 {
5780 case REG:
5781 /* biv * invar. Done. */
5782 return gen_rtx_MULT (mode, arg0, arg1);
5783
5784 case CONST_INT:
5785 /* Product of two constants. */
5786 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5787
5788 case USE:
5789 /* invar * invar is a giv, but attempt to simplify it somehow. */
5790 if (GET_CODE (arg1) != CONST_INT)
5791 return NULL_RTX;
5792
5793 arg0 = XEXP (arg0, 0);
5794 if (GET_CODE (arg0) == MULT)
5795 {
5796 /* (invar_0 * invar_1) * invar_2. Associate. */
5797 return simplify_giv_expr (loop,
5798 gen_rtx_MULT (mode,
5799 XEXP (arg0, 0),
5800 gen_rtx_MULT (mode,
5801 XEXP (arg0,
5802 1),
5803 arg1)),
5804 ext_val, benefit);
5805 }
5806 /* Porpagate the MULT expressions to the intermost nodes. */
5807 else if (GET_CODE (arg0) == PLUS)
5808 {
5809 /* (invar_0 + invar_1) * invar_2. Distribute. */
5810 return simplify_giv_expr (loop,
5811 gen_rtx_PLUS (mode,
5812 gen_rtx_MULT (mode,
5813 XEXP (arg0,
5814 0),
5815 arg1),
5816 gen_rtx_MULT (mode,
5817 XEXP (arg0,
5818 1),
5819 arg1)),
5820 ext_val, benefit);
5821 }
5822 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
5823
5824 case MULT:
5825 /* (a * invar_1) * invar_2. Associate. */
5826 return simplify_giv_expr (loop,
5827 gen_rtx_MULT (mode,
5828 XEXP (arg0, 0),
5829 gen_rtx_MULT (mode,
5830 XEXP (arg0, 1),
5831 arg1)),
5832 ext_val, benefit);
5833
5834 case PLUS:
5835 /* (a + invar_1) * invar_2. Distribute. */
5836 return simplify_giv_expr (loop,
5837 gen_rtx_PLUS (mode,
5838 gen_rtx_MULT (mode,
5839 XEXP (arg0, 0),
5840 arg1),
5841 gen_rtx_MULT (mode,
5842 XEXP (arg0, 1),
5843 arg1)),
5844 ext_val, benefit);
5845
5846 default:
5847 abort ();
5848 }
5849
5850 case ASHIFT:
5851 /* Shift by constant is multiply by power of two. */
5852 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5853 return 0;
5854
5855 return
5856 simplify_giv_expr (loop,
5857 gen_rtx_MULT (mode,
5858 XEXP (x, 0),
5859 GEN_INT ((HOST_WIDE_INT) 1
5860 << INTVAL (XEXP (x, 1)))),
5861 ext_val, benefit);
5862
5863 case NEG:
5864 /* "-a" is "a * (-1)" */
5865 return simplify_giv_expr (loop,
5866 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
5867 ext_val, benefit);
5868
5869 case NOT:
5870 /* "~a" is "-a - 1". Silly, but easy. */
5871 return simplify_giv_expr (loop,
5872 gen_rtx_MINUS (mode,
5873 gen_rtx_NEG (mode, XEXP (x, 0)),
5874 const1_rtx),
5875 ext_val, benefit);
5876
5877 case USE:
5878 /* Already in proper form for invariant. */
5879 return x;
5880
5881 case SIGN_EXTEND:
5882 case ZERO_EXTEND:
5883 case TRUNCATE:
5884 /* Conditionally recognize extensions of simple IVs. After we've
5885 computed loop traversal counts and verified the range of the
5886 source IV, we'll reevaluate this as a GIV. */
5887 if (*ext_val == NULL_RTX)
5888 {
5889 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
5890 if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
5891 {
5892 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
5893 return arg0;
5894 }
5895 }
5896 goto do_default;
5897
5898 case REG:
5899 /* If this is a new register, we can't deal with it. */
5900 if (REGNO (x) >= max_reg_before_loop)
5901 return 0;
5902
5903 /* Check for biv or giv. */
5904 switch (REG_IV_TYPE (ivs, REGNO (x)))
5905 {
5906 case BASIC_INDUCT:
5907 return x;
5908 case GENERAL_INDUCT:
5909 {
5910 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
5911
5912 /* Form expression from giv and add benefit. Ensure this giv
5913 can derive another and subtract any needed adjustment if so. */
5914
5915 /* Increasing the benefit here is risky. The only case in which it
5916 is arguably correct is if this is the only use of V. In other
5917 cases, this will artificially inflate the benefit of the current
5918 giv, and lead to suboptimal code. Thus, it is disabled, since
5919 potentially not reducing an only marginally beneficial giv is
5920 less harmful than reducing many givs that are not really
5921 beneficial. */
5922 {
5923 rtx single_use = regs->array[REGNO (x)].single_usage;
5924 if (single_use && single_use != const0_rtx)
5925 *benefit += v->benefit;
5926 }
5927
5928 if (v->cant_derive)
5929 return 0;
5930
5931 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
5932 v->src_reg, v->mult_val),
5933 v->add_val);
5934
5935 if (v->derive_adjustment)
5936 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
5937 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
5938 if (*ext_val)
5939 {
5940 if (!v->ext_dependant)
5941 return arg0;
5942 }
5943 else
5944 {
5945 *ext_val = v->ext_dependant;
5946 return arg0;
5947 }
5948 return 0;
5949 }
5950
5951 default:
5952 do_default:
5953 /* If it isn't an induction variable, and it is invariant, we
5954 may be able to simplify things further by looking through
5955 the bits we just moved outside the loop. */
5956 if (loop_invariant_p (loop, x) == 1)
5957 {
5958 struct movable *m;
5959 struct loop_movables *movables = LOOP_MOVABLES (loop);
5960
5961 for (m = movables->head; m; m = m->next)
5962 if (rtx_equal_p (x, m->set_dest))
5963 {
5964 /* Ok, we found a match. Substitute and simplify. */
5965
5966 /* If we match another movable, we must use that, as
5967 this one is going away. */
5968 if (m->match)
5969 return simplify_giv_expr (loop, m->match->set_dest,
5970 ext_val, benefit);
5971
5972 /* If consec is non-zero, this is a member of a group of
5973 instructions that were moved together. We handle this
5974 case only to the point of seeking to the last insn and
5975 looking for a REG_EQUAL. Fail if we don't find one. */
5976 if (m->consec != 0)
5977 {
5978 int i = m->consec;
5979 tem = m->insn;
5980 do
5981 {
5982 tem = NEXT_INSN (tem);
5983 }
5984 while (--i > 0);
5985
5986 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
5987 if (tem)
5988 tem = XEXP (tem, 0);
5989 }
5990 else
5991 {
5992 tem = single_set (m->insn);
5993 if (tem)
5994 tem = SET_SRC (tem);
5995 }
5996
5997 if (tem)
5998 {
5999 /* What we are most interested in is pointer
6000 arithmetic on invariants -- only take
6001 patterns we may be able to do something with. */
6002 if (GET_CODE (tem) == PLUS
6003 || GET_CODE (tem) == MULT
6004 || GET_CODE (tem) == ASHIFT
6005 || GET_CODE (tem) == CONST_INT
6006 || GET_CODE (tem) == SYMBOL_REF)
6007 {
6008 tem = simplify_giv_expr (loop, tem, ext_val,
6009 benefit);
6010 if (tem)
6011 return tem;
6012 }
6013 else if (GET_CODE (tem) == CONST
6014 && GET_CODE (XEXP (tem, 0)) == PLUS
6015 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6016 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6017 {
6018 tem = simplify_giv_expr (loop, XEXP (tem, 0),
6019 ext_val, benefit);
6020 if (tem)
6021 return tem;
6022 }
6023 }
6024 break;
6025 }
6026 }
6027 break;
6028 }
6029
6030 /* Fall through to general case. */
6031 default:
6032 /* If invariant, return as USE (unless CONST_INT).
6033 Otherwise, not giv. */
6034 if (GET_CODE (x) == USE)
6035 x = XEXP (x, 0);
6036
6037 if (loop_invariant_p (loop, x) == 1)
6038 {
6039 if (GET_CODE (x) == CONST_INT)
6040 return x;
6041 if (GET_CODE (x) == CONST
6042 && GET_CODE (XEXP (x, 0)) == PLUS
6043 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6044 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6045 x = XEXP (x, 0);
6046 return gen_rtx_USE (mode, x);
6047 }
6048 else
6049 return 0;
6050 }
6051 }
6052
6053 /* This routine folds invariants such that there is only ever one
6054 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6055
6056 static rtx
6057 sge_plus_constant (x, c)
6058 rtx x, c;
6059 {
6060 if (GET_CODE (x) == CONST_INT)
6061 return GEN_INT (INTVAL (x) + INTVAL (c));
6062 else if (GET_CODE (x) != PLUS)
6063 return gen_rtx_PLUS (GET_MODE (x), x, c);
6064 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6065 {
6066 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6067 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6068 }
6069 else if (GET_CODE (XEXP (x, 0)) == PLUS
6070 || GET_CODE (XEXP (x, 1)) != PLUS)
6071 {
6072 return gen_rtx_PLUS (GET_MODE (x),
6073 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6074 }
6075 else
6076 {
6077 return gen_rtx_PLUS (GET_MODE (x),
6078 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6079 }
6080 }
6081
6082 static rtx
6083 sge_plus (mode, x, y)
6084 enum machine_mode mode;
6085 rtx x, y;
6086 {
6087 while (GET_CODE (y) == PLUS)
6088 {
6089 rtx a = XEXP (y, 0);
6090 if (GET_CODE (a) == CONST_INT)
6091 x = sge_plus_constant (x, a);
6092 else
6093 x = gen_rtx_PLUS (mode, x, a);
6094 y = XEXP (y, 1);
6095 }
6096 if (GET_CODE (y) == CONST_INT)
6097 x = sge_plus_constant (x, y);
6098 else
6099 x = gen_rtx_PLUS (mode, x, y);
6100 return x;
6101 }
6102 \f
6103 /* Help detect a giv that is calculated by several consecutive insns;
6104 for example,
6105 giv = biv * M
6106 giv = giv + A
6107 The caller has already identified the first insn P as having a giv as dest;
6108 we check that all other insns that set the same register follow
6109 immediately after P, that they alter nothing else,
6110 and that the result of the last is still a giv.
6111
6112 The value is 0 if the reg set in P is not really a giv.
6113 Otherwise, the value is the amount gained by eliminating
6114 all the consecutive insns that compute the value.
6115
6116 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6117 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6118
6119 The coefficients of the ultimate giv value are stored in
6120 *MULT_VAL and *ADD_VAL. */
6121
6122 static int
6123 consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
6124 add_val, mult_val, ext_val, last_consec_insn)
6125 const struct loop *loop;
6126 int first_benefit;
6127 rtx p;
6128 rtx src_reg;
6129 rtx dest_reg;
6130 rtx *add_val;
6131 rtx *mult_val;
6132 rtx *ext_val;
6133 rtx *last_consec_insn;
6134 {
6135 struct loop_ivs *ivs = LOOP_IVS (loop);
6136 struct loop_regs *regs = LOOP_REGS (loop);
6137 int count;
6138 enum rtx_code code;
6139 int benefit;
6140 rtx temp;
6141 rtx set;
6142
6143 /* Indicate that this is a giv so that we can update the value produced in
6144 each insn of the multi-insn sequence.
6145
6146 This induction structure will be used only by the call to
6147 general_induction_var below, so we can allocate it on our stack.
6148 If this is a giv, our caller will replace the induct var entry with
6149 a new induction structure. */
6150 struct induction *v;
6151
6152 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
6153 return 0;
6154
6155 v = (struct induction *) alloca (sizeof (struct induction));
6156 v->src_reg = src_reg;
6157 v->mult_val = *mult_val;
6158 v->add_val = *add_val;
6159 v->benefit = first_benefit;
6160 v->cant_derive = 0;
6161 v->derive_adjustment = 0;
6162 v->ext_dependant = NULL_RTX;
6163
6164 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
6165 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
6166
6167 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
6168
6169 while (count > 0)
6170 {
6171 p = NEXT_INSN (p);
6172 code = GET_CODE (p);
6173
6174 /* If libcall, skip to end of call sequence. */
6175 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
6176 p = XEXP (temp, 0);
6177
6178 if (code == INSN
6179 && (set = single_set (p))
6180 && GET_CODE (SET_DEST (set)) == REG
6181 && SET_DEST (set) == dest_reg
6182 && (general_induction_var (loop, SET_SRC (set), &src_reg,
6183 add_val, mult_val, ext_val, 0,
6184 &benefit, VOIDmode)
6185 /* Giv created by equivalent expression. */
6186 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
6187 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
6188 add_val, mult_val, ext_val, 0,
6189 &benefit, VOIDmode)))
6190 && src_reg == v->src_reg)
6191 {
6192 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
6193 benefit += libcall_benefit (p);
6194
6195 count--;
6196 v->mult_val = *mult_val;
6197 v->add_val = *add_val;
6198 v->benefit += benefit;
6199 }
6200 else if (code != NOTE)
6201 {
6202 /* Allow insns that set something other than this giv to a
6203 constant. Such insns are needed on machines which cannot
6204 include long constants and should not disqualify a giv. */
6205 if (code == INSN
6206 && (set = single_set (p))
6207 && SET_DEST (set) != dest_reg
6208 && CONSTANT_P (SET_SRC (set)))
6209 continue;
6210
6211 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
6212 return 0;
6213 }
6214 }
6215
6216 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
6217 *last_consec_insn = p;
6218 return v->benefit;
6219 }
6220 \f
6221 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6222 represented by G1. If no such expression can be found, or it is clear that
6223 it cannot possibly be a valid address, 0 is returned.
6224
6225 To perform the computation, we note that
6226 G1 = x * v + a and
6227 G2 = y * v + b
6228 where `v' is the biv.
6229
6230 So G2 = (y/b) * G1 + (b - a*y/x).
6231
6232 Note that MULT = y/x.
6233
6234 Update: A and B are now allowed to be additive expressions such that
6235 B contains all variables in A. That is, computing B-A will not require
6236 subtracting variables. */
6237
6238 static rtx
6239 express_from_1 (a, b, mult)
6240 rtx a, b, mult;
6241 {
6242 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
6243
6244 if (mult == const0_rtx)
6245 return b;
6246
6247 /* If MULT is not 1, we cannot handle A with non-constants, since we
6248 would then be required to subtract multiples of the registers in A.
6249 This is theoretically possible, and may even apply to some Fortran
6250 constructs, but it is a lot of work and we do not attempt it here. */
6251
6252 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
6253 return NULL_RTX;
6254
6255 /* In general these structures are sorted top to bottom (down the PLUS
6256 chain), but not left to right across the PLUS. If B is a higher
6257 order giv than A, we can strip one level and recurse. If A is higher
6258 order, we'll eventually bail out, but won't know that until the end.
6259 If they are the same, we'll strip one level around this loop. */
6260
6261 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
6262 {
6263 rtx ra, rb, oa, ob, tmp;
6264
6265 ra = XEXP (a, 0), oa = XEXP (a, 1);
6266 if (GET_CODE (ra) == PLUS)
6267 tmp = ra, ra = oa, oa = tmp;
6268
6269 rb = XEXP (b, 0), ob = XEXP (b, 1);
6270 if (GET_CODE (rb) == PLUS)
6271 tmp = rb, rb = ob, ob = tmp;
6272
6273 if (rtx_equal_p (ra, rb))
6274 /* We matched: remove one reg completely. */
6275 a = oa, b = ob;
6276 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
6277 /* An alternate match. */
6278 a = oa, b = rb;
6279 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
6280 /* An alternate match. */
6281 a = ra, b = ob;
6282 else
6283 {
6284 /* Indicates an extra register in B. Strip one level from B and
6285 recurse, hoping B was the higher order expression. */
6286 ob = express_from_1 (a, ob, mult);
6287 if (ob == NULL_RTX)
6288 return NULL_RTX;
6289 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
6290 }
6291 }
6292
6293 /* Here we are at the last level of A, go through the cases hoping to
6294 get rid of everything but a constant. */
6295
6296 if (GET_CODE (a) == PLUS)
6297 {
6298 rtx ra, oa;
6299
6300 ra = XEXP (a, 0), oa = XEXP (a, 1);
6301 if (rtx_equal_p (oa, b))
6302 oa = ra;
6303 else if (!rtx_equal_p (ra, b))
6304 return NULL_RTX;
6305
6306 if (GET_CODE (oa) != CONST_INT)
6307 return NULL_RTX;
6308
6309 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
6310 }
6311 else if (GET_CODE (a) == CONST_INT)
6312 {
6313 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
6314 }
6315 else if (CONSTANT_P (a))
6316 {
6317 return simplify_gen_binary (MINUS, GET_MODE (b) != VOIDmode ? GET_MODE (b) : GET_MODE (a), const0_rtx, a);
6318 }
6319 else if (GET_CODE (b) == PLUS)
6320 {
6321 if (rtx_equal_p (a, XEXP (b, 0)))
6322 return XEXP (b, 1);
6323 else if (rtx_equal_p (a, XEXP (b, 1)))
6324 return XEXP (b, 0);
6325 else
6326 return NULL_RTX;
6327 }
6328 else if (rtx_equal_p (a, b))
6329 return const0_rtx;
6330
6331 return NULL_RTX;
6332 }
6333
6334 rtx
6335 express_from (g1, g2)
6336 struct induction *g1, *g2;
6337 {
6338 rtx mult, add;
6339
6340 /* The value that G1 will be multiplied by must be a constant integer. Also,
6341 the only chance we have of getting a valid address is if b*c/a (see above
6342 for notation) is also an integer. */
6343 if (GET_CODE (g1->mult_val) == CONST_INT
6344 && GET_CODE (g2->mult_val) == CONST_INT)
6345 {
6346 if (g1->mult_val == const0_rtx
6347 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
6348 return NULL_RTX;
6349 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
6350 }
6351 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
6352 mult = const1_rtx;
6353 else
6354 {
6355 /* ??? Find out if the one is a multiple of the other? */
6356 return NULL_RTX;
6357 }
6358
6359 add = express_from_1 (g1->add_val, g2->add_val, mult);
6360 if (add == NULL_RTX)
6361 {
6362 /* Failed. If we've got a multiplication factor between G1 and G2,
6363 scale G1's addend and try again. */
6364 if (INTVAL (mult) > 1)
6365 {
6366 rtx g1_add_val = g1->add_val;
6367 if (GET_CODE (g1_add_val) == MULT
6368 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
6369 {
6370 HOST_WIDE_INT m;
6371 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
6372 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
6373 XEXP (g1_add_val, 0), GEN_INT (m));
6374 }
6375 else
6376 {
6377 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
6378 mult);
6379 }
6380
6381 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
6382 }
6383 }
6384 if (add == NULL_RTX)
6385 return NULL_RTX;
6386
6387 /* Form simplified final result. */
6388 if (mult == const0_rtx)
6389 return add;
6390 else if (mult == const1_rtx)
6391 mult = g1->dest_reg;
6392 else
6393 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
6394
6395 if (add == const0_rtx)
6396 return mult;
6397 else
6398 {
6399 if (GET_CODE (add) == PLUS
6400 && CONSTANT_P (XEXP (add, 1)))
6401 {
6402 rtx tem = XEXP (add, 1);
6403 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
6404 add = tem;
6405 }
6406
6407 return gen_rtx_PLUS (g2->mode, mult, add);
6408 }
6409 }
6410 \f
6411 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6412 represented by G1. This indicates that G2 should be combined with G1 and
6413 that G2 can use (either directly or via an address expression) a register
6414 used to represent G1. */
6415
6416 static rtx
6417 combine_givs_p (g1, g2)
6418 struct induction *g1, *g2;
6419 {
6420 rtx comb, ret;
6421
6422 /* With the introduction of ext dependant givs, we must care for modes.
6423 G2 must not use a wider mode than G1. */
6424 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
6425 return NULL_RTX;
6426
6427 ret = comb = express_from (g1, g2);
6428 if (comb == NULL_RTX)
6429 return NULL_RTX;
6430 if (g1->mode != g2->mode)
6431 ret = gen_lowpart (g2->mode, comb);
6432
6433 /* If these givs are identical, they can be combined. We use the results
6434 of express_from because the addends are not in a canonical form, so
6435 rtx_equal_p is a weaker test. */
6436 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
6437 combination to be the other way round. */
6438 if (comb == g1->dest_reg
6439 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
6440 {
6441 return ret;
6442 }
6443
6444 /* If G2 can be expressed as a function of G1 and that function is valid
6445 as an address and no more expensive than using a register for G2,
6446 the expression of G2 in terms of G1 can be used. */
6447 if (ret != NULL_RTX
6448 && g2->giv_type == DEST_ADDR
6449 && memory_address_p (g2->mem_mode, ret)
6450 /* ??? Looses, especially with -fforce-addr, where *g2->location
6451 will always be a register, and so anything more complicated
6452 gets discarded. */
6453 #if 0
6454 #ifdef ADDRESS_COST
6455 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
6456 #else
6457 && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
6458 #endif
6459 #endif
6460 )
6461 {
6462 return ret;
6463 }
6464
6465 return NULL_RTX;
6466 }
6467 \f
6468 /* Check each extension dependant giv in this class to see if its
6469 root biv is safe from wrapping in the interior mode, which would
6470 make the giv illegal. */
6471
6472 static void
6473 check_ext_dependant_givs (bl, loop_info)
6474 struct iv_class *bl;
6475 struct loop_info *loop_info;
6476 {
6477 int ze_ok = 0, se_ok = 0, info_ok = 0;
6478 enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
6479 HOST_WIDE_INT start_val;
6480 unsigned HOST_WIDE_INT u_end_val, u_start_val;
6481 rtx incr = pc_rtx;
6482 struct induction *v;
6483
6484 /* Make sure the iteration data is available. We must have
6485 constants in order to be certain of no overflow. */
6486 /* ??? An unknown iteration count with an increment of +-1
6487 combined with friendly exit tests of against an invariant
6488 value is also ameanable to optimization. Not implemented. */
6489 if (loop_info->n_iterations > 0
6490 && bl->initial_value
6491 && GET_CODE (bl->initial_value) == CONST_INT
6492 && (incr = biv_total_increment (bl))
6493 && GET_CODE (incr) == CONST_INT
6494 /* Make sure the host can represent the arithmetic. */
6495 && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
6496 {
6497 unsigned HOST_WIDE_INT abs_incr, total_incr;
6498 HOST_WIDE_INT s_end_val;
6499 int neg_incr;
6500
6501 info_ok = 1;
6502 start_val = INTVAL (bl->initial_value);
6503 u_start_val = start_val;
6504
6505 neg_incr = 0, abs_incr = INTVAL (incr);
6506 if (INTVAL (incr) < 0)
6507 neg_incr = 1, abs_incr = -abs_incr;
6508 total_incr = abs_incr * loop_info->n_iterations;
6509
6510 /* Check for host arithmatic overflow. */
6511 if (total_incr / loop_info->n_iterations == abs_incr)
6512 {
6513 unsigned HOST_WIDE_INT u_max;
6514 HOST_WIDE_INT s_max;
6515
6516 u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
6517 s_end_val = u_end_val;
6518 u_max = GET_MODE_MASK (biv_mode);
6519 s_max = u_max >> 1;
6520
6521 /* Check zero extension of biv ok. */
6522 if (start_val >= 0
6523 /* Check for host arithmatic overflow. */
6524 && (neg_incr
6525 ? u_end_val < u_start_val
6526 : u_end_val > u_start_val)
6527 /* Check for target arithmetic overflow. */
6528 && (neg_incr
6529 ? 1 /* taken care of with host overflow */
6530 : u_end_val <= u_max))
6531 {
6532 ze_ok = 1;
6533 }
6534
6535 /* Check sign extension of biv ok. */
6536 /* ??? While it is true that overflow with signed and pointer
6537 arithmetic is undefined, I fear too many programmers don't
6538 keep this fact in mind -- myself included on occasion.
6539 So leave alone with the signed overflow optimizations. */
6540 if (start_val >= -s_max - 1
6541 /* Check for host arithmatic overflow. */
6542 && (neg_incr
6543 ? s_end_val < start_val
6544 : s_end_val > start_val)
6545 /* Check for target arithmetic overflow. */
6546 && (neg_incr
6547 ? s_end_val >= -s_max - 1
6548 : s_end_val <= s_max))
6549 {
6550 se_ok = 1;
6551 }
6552 }
6553 }
6554
6555 /* Invalidate givs that fail the tests. */
6556 for (v = bl->giv; v; v = v->next_iv)
6557 if (v->ext_dependant)
6558 {
6559 enum rtx_code code = GET_CODE (v->ext_dependant);
6560 int ok = 0;
6561
6562 switch (code)
6563 {
6564 case SIGN_EXTEND:
6565 ok = se_ok;
6566 break;
6567 case ZERO_EXTEND:
6568 ok = ze_ok;
6569 break;
6570
6571 case TRUNCATE:
6572 /* We don't know whether this value is being used as either
6573 signed or unsigned, so to safely truncate we must satisfy
6574 both. The initial check here verifies the BIV itself;
6575 once that is successful we may check its range wrt the
6576 derived GIV. */
6577 if (se_ok && ze_ok)
6578 {
6579 enum machine_mode outer_mode = GET_MODE (v->ext_dependant);
6580 unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
6581
6582 /* We know from the above that both endpoints are nonnegative,
6583 and that there is no wrapping. Verify that both endpoints
6584 are within the (signed) range of the outer mode. */
6585 if (u_start_val <= max && u_end_val <= max)
6586 ok = 1;
6587 }
6588 break;
6589
6590 default:
6591 abort ();
6592 }
6593
6594 if (ok)
6595 {
6596 if (loop_dump_stream)
6597 {
6598 fprintf (loop_dump_stream,
6599 "Verified ext dependant giv at %d of reg %d\n",
6600 INSN_UID (v->insn), bl->regno);
6601 }
6602 }
6603 else
6604 {
6605 if (loop_dump_stream)
6606 {
6607 const char *why;
6608
6609 if (info_ok)
6610 why = "biv iteration values overflowed";
6611 else
6612 {
6613 if (incr == pc_rtx)
6614 incr = biv_total_increment (bl);
6615 if (incr == const1_rtx)
6616 why = "biv iteration info incomplete; incr by 1";
6617 else
6618 why = "biv iteration info incomplete";
6619 }
6620
6621 fprintf (loop_dump_stream,
6622 "Failed ext dependant giv at %d, %s\n",
6623 INSN_UID (v->insn), why);
6624 }
6625 v->ignore = 1;
6626 }
6627 }
6628 }
6629
6630 /* Generate a version of VALUE in a mode appropriate for initializing V. */
6631
6632 rtx
6633 extend_value_for_giv (v, value)
6634 struct induction *v;
6635 rtx value;
6636 {
6637 rtx ext_dep = v->ext_dependant;
6638
6639 if (! ext_dep)
6640 return value;
6641
6642 /* Recall that check_ext_dependant_givs verified that the known bounds
6643 of a biv did not overflow or wrap with respect to the extension for
6644 the giv. Therefore, constants need no additional adjustment. */
6645 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
6646 return value;
6647
6648 /* Otherwise, we must adjust the value to compensate for the
6649 differing modes of the biv and the giv. */
6650 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
6651 }
6652 \f
6653 struct combine_givs_stats
6654 {
6655 int giv_number;
6656 int total_benefit;
6657 };
6658
6659 static int
6660 cmp_combine_givs_stats (xp, yp)
6661 const PTR xp;
6662 const PTR yp;
6663 {
6664 const struct combine_givs_stats * const x =
6665 (const struct combine_givs_stats *) xp;
6666 const struct combine_givs_stats * const y =
6667 (const struct combine_givs_stats *) yp;
6668 int d;
6669 d = y->total_benefit - x->total_benefit;
6670 /* Stabilize the sort. */
6671 if (!d)
6672 d = x->giv_number - y->giv_number;
6673 return d;
6674 }
6675
6676 /* Check all pairs of givs for iv_class BL and see if any can be combined with
6677 any other. If so, point SAME to the giv combined with and set NEW_REG to
6678 be an expression (in terms of the other giv's DEST_REG) equivalent to the
6679 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
6680
6681 static void
6682 combine_givs (regs, bl)
6683 struct loop_regs *regs;
6684 struct iv_class *bl;
6685 {
6686 /* Additional benefit to add for being combined multiple times. */
6687 const int extra_benefit = 3;
6688
6689 struct induction *g1, *g2, **giv_array;
6690 int i, j, k, giv_count;
6691 struct combine_givs_stats *stats;
6692 rtx *can_combine;
6693
6694 /* Count givs, because bl->giv_count is incorrect here. */
6695 giv_count = 0;
6696 for (g1 = bl->giv; g1; g1 = g1->next_iv)
6697 if (!g1->ignore)
6698 giv_count++;
6699
6700 giv_array
6701 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
6702 i = 0;
6703 for (g1 = bl->giv; g1; g1 = g1->next_iv)
6704 if (!g1->ignore)
6705 giv_array[i++] = g1;
6706
6707 stats = (struct combine_givs_stats *) xcalloc (giv_count, sizeof (*stats));
6708 can_combine = (rtx *) xcalloc (giv_count, giv_count * sizeof (rtx));
6709
6710 for (i = 0; i < giv_count; i++)
6711 {
6712 int this_benefit;
6713 rtx single_use;
6714
6715 g1 = giv_array[i];
6716 stats[i].giv_number = i;
6717
6718 /* If a DEST_REG GIV is used only once, do not allow it to combine
6719 with anything, for in doing so we will gain nothing that cannot
6720 be had by simply letting the GIV with which we would have combined
6721 to be reduced on its own. The losage shows up in particular with
6722 DEST_ADDR targets on hosts with reg+reg addressing, though it can
6723 be seen elsewhere as well. */
6724 if (g1->giv_type == DEST_REG
6725 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
6726 && single_use != const0_rtx)
6727 continue;
6728
6729 this_benefit = g1->benefit;
6730 /* Add an additional weight for zero addends. */
6731 if (g1->no_const_addval)
6732 this_benefit += 1;
6733
6734 for (j = 0; j < giv_count; j++)
6735 {
6736 rtx this_combine;
6737
6738 g2 = giv_array[j];
6739 if (g1 != g2
6740 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
6741 {
6742 can_combine[i * giv_count + j] = this_combine;
6743 this_benefit += g2->benefit + extra_benefit;
6744 }
6745 }
6746 stats[i].total_benefit = this_benefit;
6747 }
6748
6749 /* Iterate, combining until we can't. */
6750 restart:
6751 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
6752
6753 if (loop_dump_stream)
6754 {
6755 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
6756 for (k = 0; k < giv_count; k++)
6757 {
6758 g1 = giv_array[stats[k].giv_number];
6759 if (!g1->combined_with && !g1->same)
6760 fprintf (loop_dump_stream, " {%d, %d}",
6761 INSN_UID (giv_array[stats[k].giv_number]->insn),
6762 stats[k].total_benefit);
6763 }
6764 putc ('\n', loop_dump_stream);
6765 }
6766
6767 for (k = 0; k < giv_count; k++)
6768 {
6769 int g1_add_benefit = 0;
6770
6771 i = stats[k].giv_number;
6772 g1 = giv_array[i];
6773
6774 /* If it has already been combined, skip. */
6775 if (g1->combined_with || g1->same)
6776 continue;
6777
6778 for (j = 0; j < giv_count; j++)
6779 {
6780 g2 = giv_array[j];
6781 if (g1 != g2 && can_combine[i * giv_count + j]
6782 /* If it has already been combined, skip. */
6783 && ! g2->same && ! g2->combined_with)
6784 {
6785 int l;
6786
6787 g2->new_reg = can_combine[i * giv_count + j];
6788 g2->same = g1;
6789 g1->combined_with++;
6790 g1->lifetime += g2->lifetime;
6791
6792 g1_add_benefit += g2->benefit;
6793
6794 /* ??? The new final_[bg]iv_value code does a much better job
6795 of finding replaceable giv's, and hence this code may no
6796 longer be necessary. */
6797 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
6798 g1_add_benefit -= copy_cost;
6799
6800 /* To help optimize the next set of combinations, remove
6801 this giv from the benefits of other potential mates. */
6802 for (l = 0; l < giv_count; ++l)
6803 {
6804 int m = stats[l].giv_number;
6805 if (can_combine[m * giv_count + j])
6806 stats[l].total_benefit -= g2->benefit + extra_benefit;
6807 }
6808
6809 if (loop_dump_stream)
6810 fprintf (loop_dump_stream,
6811 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
6812 INSN_UID (g2->insn), INSN_UID (g1->insn),
6813 g1->benefit, g1_add_benefit, g1->lifetime);
6814 }
6815 }
6816
6817 /* To help optimize the next set of combinations, remove
6818 this giv from the benefits of other potential mates. */
6819 if (g1->combined_with)
6820 {
6821 for (j = 0; j < giv_count; ++j)
6822 {
6823 int m = stats[j].giv_number;
6824 if (can_combine[m * giv_count + i])
6825 stats[j].total_benefit -= g1->benefit + extra_benefit;
6826 }
6827
6828 g1->benefit += g1_add_benefit;
6829
6830 /* We've finished with this giv, and everything it touched.
6831 Restart the combination so that proper weights for the
6832 rest of the givs are properly taken into account. */
6833 /* ??? Ideally we would compact the arrays at this point, so
6834 as to not cover old ground. But sanely compacting
6835 can_combine is tricky. */
6836 goto restart;
6837 }
6838 }
6839
6840 /* Clean up. */
6841 free (stats);
6842 free (can_combine);
6843 }
6844 \f
6845 /* Generate sequence for REG = B * M + A. */
6846
6847 static rtx
6848 gen_add_mult (b, m, a, reg)
6849 rtx b; /* initial value of basic induction variable */
6850 rtx m; /* multiplicative constant */
6851 rtx a; /* additive constant */
6852 rtx reg; /* destination register */
6853 {
6854 rtx seq;
6855 rtx result;
6856
6857 start_sequence ();
6858 /* Use unsigned arithmetic. */
6859 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
6860 if (reg != result)
6861 emit_move_insn (reg, result);
6862 seq = gen_sequence ();
6863 end_sequence ();
6864
6865 return seq;
6866 }
6867
6868
6869 /* Update registers created in insn sequence SEQ. */
6870
6871 static void
6872 loop_regs_update (loop, seq)
6873 const struct loop *loop ATTRIBUTE_UNUSED;
6874 rtx seq;
6875 {
6876 /* Update register info for alias analysis. */
6877
6878 if (GET_CODE (seq) == SEQUENCE)
6879 {
6880 int i;
6881 for (i = 0; i < XVECLEN (seq, 0); ++i)
6882 {
6883 rtx set = single_set (XVECEXP (seq, 0, i));
6884 if (set && GET_CODE (SET_DEST (set)) == REG)
6885 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
6886 }
6887 }
6888 else
6889 {
6890 rtx set = single_set (seq);
6891 if (set && GET_CODE (SET_DEST (set)) == REG)
6892 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
6893 }
6894 }
6895
6896
6897 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. */
6898
6899 void
6900 loop_iv_add_mult_emit_before (loop, b, m, a, reg, before_bb, before_insn)
6901 const struct loop *loop;
6902 rtx b; /* initial value of basic induction variable */
6903 rtx m; /* multiplicative constant */
6904 rtx a; /* additive constant */
6905 rtx reg; /* destination register */
6906 basic_block before_bb;
6907 rtx before_insn;
6908 {
6909 rtx seq;
6910
6911 if (! before_insn)
6912 {
6913 loop_iv_add_mult_hoist (loop, b, m, a, reg);
6914 return;
6915 }
6916
6917 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
6918 seq = gen_add_mult (copy_rtx (b), m, copy_rtx (a), reg);
6919
6920 /* Increase the lifetime of any invariants moved further in code. */
6921 update_reg_last_use (a, before_insn);
6922 update_reg_last_use (b, before_insn);
6923 update_reg_last_use (m, before_insn);
6924
6925 loop_insn_emit_before (loop, before_bb, before_insn, seq);
6926
6927 /* It is possible that the expansion created lots of new registers.
6928 Iterate over the sequence we just created and record them all. */
6929 loop_regs_update (loop, seq);
6930 }
6931
6932
6933 /* Emit insns in loop pre-header to set REG = B * M + A. */
6934
6935 void
6936 loop_iv_add_mult_sink (loop, b, m, a, reg)
6937 const struct loop *loop;
6938 rtx b; /* initial value of basic induction variable */
6939 rtx m; /* multiplicative constant */
6940 rtx a; /* additive constant */
6941 rtx reg; /* destination register */
6942 {
6943 rtx seq;
6944
6945 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
6946 seq = gen_add_mult (copy_rtx (b), m, copy_rtx (a), reg);
6947
6948 /* Increase the lifetime of any invariants moved further in code.
6949 ???? Is this really necessary? */
6950 update_reg_last_use (a, loop->sink);
6951 update_reg_last_use (b, loop->sink);
6952 update_reg_last_use (m, loop->sink);
6953
6954 loop_insn_sink (loop, seq);
6955
6956 /* It is possible that the expansion created lots of new registers.
6957 Iterate over the sequence we just created and record them all. */
6958 loop_regs_update (loop, seq);
6959 }
6960
6961
6962 /* Emit insns after loop to set REG = B * M + A. */
6963
6964 void
6965 loop_iv_add_mult_hoist (loop, b, m, a, reg)
6966 const struct loop *loop;
6967 rtx b; /* initial value of basic induction variable */
6968 rtx m; /* multiplicative constant */
6969 rtx a; /* additive constant */
6970 rtx reg; /* destination register */
6971 {
6972 rtx seq;
6973
6974 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
6975 seq = gen_add_mult (copy_rtx (b), m, copy_rtx (a), reg);
6976
6977 loop_insn_hoist (loop, seq);
6978
6979 /* It is possible that the expansion created lots of new registers.
6980 Iterate over the sequence we just created and record them all. */
6981 loop_regs_update (loop, seq);
6982 }
6983
6984
6985
6986 /* Similar to gen_add_mult, but compute cost rather than generating
6987 sequence. */
6988
6989 static int
6990 iv_add_mult_cost (b, m, a, reg)
6991 rtx b; /* initial value of basic induction variable */
6992 rtx m; /* multiplicative constant */
6993 rtx a; /* additive constant */
6994 rtx reg; /* destination register */
6995 {
6996 int cost = 0;
6997 rtx last, result;
6998
6999 start_sequence ();
7000 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7001 if (reg != result)
7002 emit_move_insn (reg, result);
7003 last = get_last_insn ();
7004 while (last)
7005 {
7006 rtx t = single_set (last);
7007 if (t)
7008 cost += rtx_cost (SET_SRC (t), SET);
7009 last = PREV_INSN (last);
7010 }
7011 end_sequence ();
7012 return cost;
7013 }
7014 \f
7015 /* Test whether A * B can be computed without
7016 an actual multiply insn. Value is 1 if so. */
7017
7018 static int
7019 product_cheap_p (a, b)
7020 rtx a;
7021 rtx b;
7022 {
7023 int i;
7024 rtx tmp;
7025 int win = 1;
7026
7027 /* If only one is constant, make it B. */
7028 if (GET_CODE (a) == CONST_INT)
7029 tmp = a, a = b, b = tmp;
7030
7031 /* If first constant, both constant, so don't need multiply. */
7032 if (GET_CODE (a) == CONST_INT)
7033 return 1;
7034
7035 /* If second not constant, neither is constant, so would need multiply. */
7036 if (GET_CODE (b) != CONST_INT)
7037 return 0;
7038
7039 /* One operand is constant, so might not need multiply insn. Generate the
7040 code for the multiply and see if a call or multiply, or long sequence
7041 of insns is generated. */
7042
7043 start_sequence ();
7044 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
7045 tmp = gen_sequence ();
7046 end_sequence ();
7047
7048 if (GET_CODE (tmp) == SEQUENCE)
7049 {
7050 if (XVEC (tmp, 0) == 0)
7051 win = 1;
7052 else if (XVECLEN (tmp, 0) > 3)
7053 win = 0;
7054 else
7055 for (i = 0; i < XVECLEN (tmp, 0); i++)
7056 {
7057 rtx insn = XVECEXP (tmp, 0, i);
7058
7059 if (GET_CODE (insn) != INSN
7060 || (GET_CODE (PATTERN (insn)) == SET
7061 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7062 || (GET_CODE (PATTERN (insn)) == PARALLEL
7063 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7064 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7065 {
7066 win = 0;
7067 break;
7068 }
7069 }
7070 }
7071 else if (GET_CODE (tmp) == SET
7072 && GET_CODE (SET_SRC (tmp)) == MULT)
7073 win = 0;
7074 else if (GET_CODE (tmp) == PARALLEL
7075 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7076 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7077 win = 0;
7078
7079 return win;
7080 }
7081 \f
7082 /* Check to see if loop can be terminated by a "decrement and branch until
7083 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7084 Also try reversing an increment loop to a decrement loop
7085 to see if the optimization can be performed.
7086 Value is nonzero if optimization was performed. */
7087
7088 /* This is useful even if the architecture doesn't have such an insn,
7089 because it might change a loops which increments from 0 to n to a loop
7090 which decrements from n to 0. A loop that decrements to zero is usually
7091 faster than one that increments from zero. */
7092
7093 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7094 such as approx_final_value, biv_total_increment, loop_iterations, and
7095 final_[bg]iv_value. */
7096
7097 static int
7098 check_dbra_loop (loop, insn_count)
7099 struct loop *loop;
7100 int insn_count;
7101 {
7102 struct loop_info *loop_info = LOOP_INFO (loop);
7103 struct loop_regs *regs = LOOP_REGS (loop);
7104 struct loop_ivs *ivs = LOOP_IVS (loop);
7105 struct iv_class *bl;
7106 rtx reg;
7107 rtx jump_label;
7108 rtx final_value;
7109 rtx start_value;
7110 rtx new_add_val;
7111 rtx comparison;
7112 rtx before_comparison;
7113 rtx p;
7114 rtx jump;
7115 rtx first_compare;
7116 int compare_and_branch;
7117 rtx loop_start = loop->start;
7118 rtx loop_end = loop->end;
7119
7120 /* If last insn is a conditional branch, and the insn before tests a
7121 register value, try to optimize it. Otherwise, we can't do anything. */
7122
7123 jump = PREV_INSN (loop_end);
7124 comparison = get_condition_for_loop (loop, jump);
7125 if (comparison == 0)
7126 return 0;
7127 if (!onlyjump_p (jump))
7128 return 0;
7129
7130 /* Try to compute whether the compare/branch at the loop end is one or
7131 two instructions. */
7132 get_condition (jump, &first_compare);
7133 if (first_compare == jump)
7134 compare_and_branch = 1;
7135 else if (first_compare == prev_nonnote_insn (jump))
7136 compare_and_branch = 2;
7137 else
7138 return 0;
7139
7140 {
7141 /* If more than one condition is present to control the loop, then
7142 do not proceed, as this function does not know how to rewrite
7143 loop tests with more than one condition.
7144
7145 Look backwards from the first insn in the last comparison
7146 sequence and see if we've got another comparison sequence. */
7147
7148 rtx jump1;
7149 if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
7150 if (GET_CODE (jump1) == JUMP_INSN)
7151 return 0;
7152 }
7153
7154 /* Check all of the bivs to see if the compare uses one of them.
7155 Skip biv's set more than once because we can't guarantee that
7156 it will be zero on the last iteration. Also skip if the biv is
7157 used between its update and the test insn. */
7158
7159 for (bl = ivs->list; bl; bl = bl->next)
7160 {
7161 if (bl->biv_count == 1
7162 && ! bl->biv->maybe_multiple
7163 && bl->biv->dest_reg == XEXP (comparison, 0)
7164 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
7165 first_compare))
7166 break;
7167 }
7168
7169 if (! bl)
7170 return 0;
7171
7172 /* Look for the case where the basic induction variable is always
7173 nonnegative, and equals zero on the last iteration.
7174 In this case, add a reg_note REG_NONNEG, which allows the
7175 m68k DBRA instruction to be used. */
7176
7177 if (((GET_CODE (comparison) == GT
7178 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
7179 && INTVAL (XEXP (comparison, 1)) == -1)
7180 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
7181 && GET_CODE (bl->biv->add_val) == CONST_INT
7182 && INTVAL (bl->biv->add_val) < 0)
7183 {
7184 /* Initial value must be greater than 0,
7185 init_val % -dec_value == 0 to ensure that it equals zero on
7186 the last iteration */
7187
7188 if (GET_CODE (bl->initial_value) == CONST_INT
7189 && INTVAL (bl->initial_value) > 0
7190 && (INTVAL (bl->initial_value)
7191 % (-INTVAL (bl->biv->add_val))) == 0)
7192 {
7193 /* register always nonnegative, add REG_NOTE to branch */
7194 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7195 REG_NOTES (jump)
7196 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7197 REG_NOTES (jump));
7198 bl->nonneg = 1;
7199
7200 return 1;
7201 }
7202
7203 /* If the decrement is 1 and the value was tested as >= 0 before
7204 the loop, then we can safely optimize. */
7205 for (p = loop_start; p; p = PREV_INSN (p))
7206 {
7207 if (GET_CODE (p) == CODE_LABEL)
7208 break;
7209 if (GET_CODE (p) != JUMP_INSN)
7210 continue;
7211
7212 before_comparison = get_condition_for_loop (loop, p);
7213 if (before_comparison
7214 && XEXP (before_comparison, 0) == bl->biv->dest_reg
7215 && GET_CODE (before_comparison) == LT
7216 && XEXP (before_comparison, 1) == const0_rtx
7217 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
7218 && INTVAL (bl->biv->add_val) == -1)
7219 {
7220 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7221 REG_NOTES (jump)
7222 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7223 REG_NOTES (jump));
7224 bl->nonneg = 1;
7225
7226 return 1;
7227 }
7228 }
7229 }
7230 else if (GET_CODE (bl->biv->add_val) == CONST_INT
7231 && INTVAL (bl->biv->add_val) > 0)
7232 {
7233 /* Try to change inc to dec, so can apply above optimization. */
7234 /* Can do this if:
7235 all registers modified are induction variables or invariant,
7236 all memory references have non-overlapping addresses
7237 (obviously true if only one write)
7238 allow 2 insns for the compare/jump at the end of the loop. */
7239 /* Also, we must avoid any instructions which use both the reversed
7240 biv and another biv. Such instructions will fail if the loop is
7241 reversed. We meet this condition by requiring that either
7242 no_use_except_counting is true, or else that there is only
7243 one biv. */
7244 int num_nonfixed_reads = 0;
7245 /* 1 if the iteration var is used only to count iterations. */
7246 int no_use_except_counting = 0;
7247 /* 1 if the loop has no memory store, or it has a single memory store
7248 which is reversible. */
7249 int reversible_mem_store = 1;
7250
7251 if (bl->giv_count == 0 && ! loop->exit_count)
7252 {
7253 rtx bivreg = regno_reg_rtx[bl->regno];
7254
7255 /* If there are no givs for this biv, and the only exit is the
7256 fall through at the end of the loop, then
7257 see if perhaps there are no uses except to count. */
7258 no_use_except_counting = 1;
7259 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7260 if (INSN_P (p))
7261 {
7262 rtx set = single_set (p);
7263
7264 if (set && GET_CODE (SET_DEST (set)) == REG
7265 && REGNO (SET_DEST (set)) == bl->regno)
7266 /* An insn that sets the biv is okay. */
7267 ;
7268 else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
7269 || p == prev_nonnote_insn (loop_end))
7270 && reg_mentioned_p (bivreg, PATTERN (p)))
7271 {
7272 /* If either of these insns uses the biv and sets a pseudo
7273 that has more than one usage, then the biv has uses
7274 other than counting since it's used to derive a value
7275 that is used more than one time. */
7276 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
7277 regs);
7278 if (regs->multiple_uses)
7279 {
7280 no_use_except_counting = 0;
7281 break;
7282 }
7283 }
7284 else if (reg_mentioned_p (bivreg, PATTERN (p)))
7285 {
7286 no_use_except_counting = 0;
7287 break;
7288 }
7289 }
7290 }
7291
7292 if (no_use_except_counting)
7293 /* No need to worry about MEMs. */
7294 ;
7295 else if (loop_info->num_mem_sets <= 1)
7296 {
7297 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7298 if (INSN_P (p))
7299 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
7300
7301 /* If the loop has a single store, and the destination address is
7302 invariant, then we can't reverse the loop, because this address
7303 might then have the wrong value at loop exit.
7304 This would work if the source was invariant also, however, in that
7305 case, the insn should have been moved out of the loop. */
7306
7307 if (loop_info->num_mem_sets == 1)
7308 {
7309 struct induction *v;
7310
7311 reversible_mem_store
7312 = (! loop_info->unknown_address_altered
7313 && ! loop_info->unknown_constant_address_altered
7314 && ! loop_invariant_p (loop,
7315 XEXP (XEXP (loop_info->store_mems, 0),
7316 0)));
7317
7318 /* If the store depends on a register that is set after the
7319 store, it depends on the initial value, and is thus not
7320 reversible. */
7321 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
7322 {
7323 if (v->giv_type == DEST_REG
7324 && reg_mentioned_p (v->dest_reg,
7325 PATTERN (loop_info->first_loop_store_insn))
7326 && loop_insn_first_p (loop_info->first_loop_store_insn,
7327 v->insn))
7328 reversible_mem_store = 0;
7329 }
7330 }
7331 }
7332 else
7333 return 0;
7334
7335 /* This code only acts for innermost loops. Also it simplifies
7336 the memory address check by only reversing loops with
7337 zero or one memory access.
7338 Two memory accesses could involve parts of the same array,
7339 and that can't be reversed.
7340 If the biv is used only for counting, than we don't need to worry
7341 about all these things. */
7342
7343 if ((num_nonfixed_reads <= 1
7344 && ! loop_info->has_nonconst_call
7345 && ! loop_info->has_volatile
7346 && reversible_mem_store
7347 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
7348 + LOOP_MOVABLES (loop)->num + compare_and_branch == insn_count)
7349 && (bl == ivs->list && bl->next == 0))
7350 || no_use_except_counting)
7351 {
7352 rtx tem;
7353
7354 /* Loop can be reversed. */
7355 if (loop_dump_stream)
7356 fprintf (loop_dump_stream, "Can reverse loop\n");
7357
7358 /* Now check other conditions:
7359
7360 The increment must be a constant, as must the initial value,
7361 and the comparison code must be LT.
7362
7363 This test can probably be improved since +/- 1 in the constant
7364 can be obtained by changing LT to LE and vice versa; this is
7365 confusing. */
7366
7367 if (comparison
7368 /* for constants, LE gets turned into LT */
7369 && (GET_CODE (comparison) == LT
7370 || (GET_CODE (comparison) == LE
7371 && no_use_except_counting)))
7372 {
7373 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
7374 rtx initial_value, comparison_value;
7375 int nonneg = 0;
7376 enum rtx_code cmp_code;
7377 int comparison_const_width;
7378 unsigned HOST_WIDE_INT comparison_sign_mask;
7379
7380 add_val = INTVAL (bl->biv->add_val);
7381 comparison_value = XEXP (comparison, 1);
7382 if (GET_MODE (comparison_value) == VOIDmode)
7383 comparison_const_width
7384 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
7385 else
7386 comparison_const_width
7387 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
7388 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
7389 comparison_const_width = HOST_BITS_PER_WIDE_INT;
7390 comparison_sign_mask
7391 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
7392
7393 /* If the comparison value is not a loop invariant, then we
7394 can not reverse this loop.
7395
7396 ??? If the insns which initialize the comparison value as
7397 a whole compute an invariant result, then we could move
7398 them out of the loop and proceed with loop reversal. */
7399 if (! loop_invariant_p (loop, comparison_value))
7400 return 0;
7401
7402 if (GET_CODE (comparison_value) == CONST_INT)
7403 comparison_val = INTVAL (comparison_value);
7404 initial_value = bl->initial_value;
7405
7406 /* Normalize the initial value if it is an integer and
7407 has no other use except as a counter. This will allow
7408 a few more loops to be reversed. */
7409 if (no_use_except_counting
7410 && GET_CODE (comparison_value) == CONST_INT
7411 && GET_CODE (initial_value) == CONST_INT)
7412 {
7413 comparison_val = comparison_val - INTVAL (bl->initial_value);
7414 /* The code below requires comparison_val to be a multiple
7415 of add_val in order to do the loop reversal, so
7416 round up comparison_val to a multiple of add_val.
7417 Since comparison_value is constant, we know that the
7418 current comparison code is LT. */
7419 comparison_val = comparison_val + add_val - 1;
7420 comparison_val
7421 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
7422 /* We postpone overflow checks for COMPARISON_VAL here;
7423 even if there is an overflow, we might still be able to
7424 reverse the loop, if converting the loop exit test to
7425 NE is possible. */
7426 initial_value = const0_rtx;
7427 }
7428
7429 /* First check if we can do a vanilla loop reversal. */
7430 if (initial_value == const0_rtx
7431 /* If we have a decrement_and_branch_on_count,
7432 prefer the NE test, since this will allow that
7433 instruction to be generated. Note that we must
7434 use a vanilla loop reversal if the biv is used to
7435 calculate a giv or has a non-counting use. */
7436 #if ! defined (HAVE_decrement_and_branch_until_zero) \
7437 && defined (HAVE_decrement_and_branch_on_count)
7438 && (! (add_val == 1 && loop->vtop
7439 && (bl->biv_count == 0
7440 || no_use_except_counting)))
7441 #endif
7442 && GET_CODE (comparison_value) == CONST_INT
7443 /* Now do postponed overflow checks on COMPARISON_VAL. */
7444 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
7445 & comparison_sign_mask))
7446 {
7447 /* Register will always be nonnegative, with value
7448 0 on last iteration */
7449 add_adjust = add_val;
7450 nonneg = 1;
7451 cmp_code = GE;
7452 }
7453 else if (add_val == 1 && loop->vtop
7454 && (bl->biv_count == 0
7455 || no_use_except_counting))
7456 {
7457 add_adjust = 0;
7458 cmp_code = NE;
7459 }
7460 else
7461 return 0;
7462
7463 if (GET_CODE (comparison) == LE)
7464 add_adjust -= add_val;
7465
7466 /* If the initial value is not zero, or if the comparison
7467 value is not an exact multiple of the increment, then we
7468 can not reverse this loop. */
7469 if (initial_value == const0_rtx
7470 && GET_CODE (comparison_value) == CONST_INT)
7471 {
7472 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
7473 return 0;
7474 }
7475 else
7476 {
7477 if (! no_use_except_counting || add_val != 1)
7478 return 0;
7479 }
7480
7481 final_value = comparison_value;
7482
7483 /* Reset these in case we normalized the initial value
7484 and comparison value above. */
7485 if (GET_CODE (comparison_value) == CONST_INT
7486 && GET_CODE (initial_value) == CONST_INT)
7487 {
7488 comparison_value = GEN_INT (comparison_val);
7489 final_value
7490 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
7491 }
7492 bl->initial_value = initial_value;
7493
7494 /* Save some info needed to produce the new insns. */
7495 reg = bl->biv->dest_reg;
7496 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
7497 if (jump_label == pc_rtx)
7498 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
7499 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
7500
7501 /* Set start_value; if this is not a CONST_INT, we need
7502 to generate a SUB.
7503 Initialize biv to start_value before loop start.
7504 The old initializing insn will be deleted as a
7505 dead store by flow.c. */
7506 if (initial_value == const0_rtx
7507 && GET_CODE (comparison_value) == CONST_INT)
7508 {
7509 start_value = GEN_INT (comparison_val - add_adjust);
7510 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
7511 }
7512 else if (GET_CODE (initial_value) == CONST_INT)
7513 {
7514 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
7515 enum machine_mode mode = GET_MODE (reg);
7516 enum insn_code icode
7517 = add_optab->handlers[(int) mode].insn_code;
7518
7519 if (! (*insn_data[icode].operand[0].predicate) (reg, mode)
7520 || ! ((*insn_data[icode].operand[1].predicate)
7521 (comparison_value, mode))
7522 || ! ((*insn_data[icode].operand[2].predicate)
7523 (offset, mode)))
7524 return 0;
7525 start_value
7526 = gen_rtx_PLUS (mode, comparison_value, offset);
7527 loop_insn_hoist (loop, (GEN_FCN (icode)
7528 (reg, comparison_value, offset)));
7529 if (GET_CODE (comparison) == LE)
7530 final_value = gen_rtx_PLUS (mode, comparison_value,
7531 GEN_INT (add_val));
7532 }
7533 else if (! add_adjust)
7534 {
7535 enum machine_mode mode = GET_MODE (reg);
7536 enum insn_code icode
7537 = sub_optab->handlers[(int) mode].insn_code;
7538 if (! (*insn_data[icode].operand[0].predicate) (reg, mode)
7539 || ! ((*insn_data[icode].operand[1].predicate)
7540 (comparison_value, mode))
7541 || ! ((*insn_data[icode].operand[2].predicate)
7542 (initial_value, mode)))
7543 return 0;
7544 start_value
7545 = gen_rtx_MINUS (mode, comparison_value, initial_value);
7546 loop_insn_hoist (loop, (GEN_FCN (icode)
7547 (reg, comparison_value,
7548 initial_value)));
7549 }
7550 else
7551 /* We could handle the other cases too, but it'll be
7552 better to have a testcase first. */
7553 return 0;
7554
7555 /* We may not have a single insn which can increment a reg, so
7556 create a sequence to hold all the insns from expand_inc. */
7557 start_sequence ();
7558 expand_inc (reg, new_add_val);
7559 tem = gen_sequence ();
7560 end_sequence ();
7561
7562 p = emit_insn_before (tem, bl->biv->insn);
7563 delete_insn (bl->biv->insn);
7564
7565 /* Update biv info to reflect its new status. */
7566 bl->biv->insn = p;
7567 bl->initial_value = start_value;
7568 bl->biv->add_val = new_add_val;
7569
7570 /* Update loop info. */
7571 loop_info->initial_value = reg;
7572 loop_info->initial_equiv_value = reg;
7573 loop_info->final_value = const0_rtx;
7574 loop_info->final_equiv_value = const0_rtx;
7575 loop_info->comparison_value = const0_rtx;
7576 loop_info->comparison_code = cmp_code;
7577 loop_info->increment = new_add_val;
7578
7579 /* Inc LABEL_NUSES so that delete_insn will
7580 not delete the label. */
7581 LABEL_NUSES (XEXP (jump_label, 0))++;
7582
7583 /* Emit an insn after the end of the loop to set the biv's
7584 proper exit value if it is used anywhere outside the loop. */
7585 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
7586 || ! bl->init_insn
7587 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
7588 loop_insn_sink (loop, gen_move_insn (reg, final_value));
7589
7590 /* Delete compare/branch at end of loop. */
7591 delete_insn (PREV_INSN (loop_end));
7592 if (compare_and_branch == 2)
7593 delete_insn (first_compare);
7594
7595 /* Add new compare/branch insn at end of loop. */
7596 start_sequence ();
7597 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
7598 GET_MODE (reg), 0, 0,
7599 XEXP (jump_label, 0));
7600 tem = gen_sequence ();
7601 end_sequence ();
7602 emit_jump_insn_before (tem, loop_end);
7603
7604 for (tem = PREV_INSN (loop_end);
7605 tem && GET_CODE (tem) != JUMP_INSN;
7606 tem = PREV_INSN (tem))
7607 ;
7608
7609 if (tem)
7610 JUMP_LABEL (tem) = XEXP (jump_label, 0);
7611
7612 if (nonneg)
7613 {
7614 if (tem)
7615 {
7616 /* Increment of LABEL_NUSES done above. */
7617 /* Register is now always nonnegative,
7618 so add REG_NONNEG note to the branch. */
7619 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
7620 REG_NOTES (tem));
7621 }
7622 bl->nonneg = 1;
7623 }
7624
7625 /* No insn may reference both the reversed and another biv or it
7626 will fail (see comment near the top of the loop reversal
7627 code).
7628 Earlier on, we have verified that the biv has no use except
7629 counting, or it is the only biv in this function.
7630 However, the code that computes no_use_except_counting does
7631 not verify reg notes. It's possible to have an insn that
7632 references another biv, and has a REG_EQUAL note with an
7633 expression based on the reversed biv. To avoid this case,
7634 remove all REG_EQUAL notes based on the reversed biv
7635 here. */
7636 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7637 if (INSN_P (p))
7638 {
7639 rtx *pnote;
7640 rtx set = single_set (p);
7641 /* If this is a set of a GIV based on the reversed biv, any
7642 REG_EQUAL notes should still be correct. */
7643 if (! set
7644 || GET_CODE (SET_DEST (set)) != REG
7645 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
7646 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
7647 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
7648 for (pnote = &REG_NOTES (p); *pnote;)
7649 {
7650 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
7651 && reg_mentioned_p (regno_reg_rtx[bl->regno],
7652 XEXP (*pnote, 0)))
7653 *pnote = XEXP (*pnote, 1);
7654 else
7655 pnote = &XEXP (*pnote, 1);
7656 }
7657 }
7658
7659 /* Mark that this biv has been reversed. Each giv which depends
7660 on this biv, and which is also live past the end of the loop
7661 will have to be fixed up. */
7662
7663 bl->reversed = 1;
7664
7665 if (loop_dump_stream)
7666 {
7667 fprintf (loop_dump_stream, "Reversed loop");
7668 if (bl->nonneg)
7669 fprintf (loop_dump_stream, " and added reg_nonneg\n");
7670 else
7671 fprintf (loop_dump_stream, "\n");
7672 }
7673
7674 return 1;
7675 }
7676 }
7677 }
7678
7679 return 0;
7680 }
7681 \f
7682 /* Verify whether the biv BL appears to be eliminable,
7683 based on the insns in the loop that refer to it.
7684
7685 If ELIMINATE_P is non-zero, actually do the elimination.
7686
7687 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
7688 determine whether invariant insns should be placed inside or at the
7689 start of the loop. */
7690
7691 static int
7692 maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
7693 const struct loop *loop;
7694 struct iv_class *bl;
7695 int eliminate_p;
7696 int threshold, insn_count;
7697 {
7698 struct loop_ivs *ivs = LOOP_IVS (loop);
7699 rtx reg = bl->biv->dest_reg;
7700 rtx p;
7701
7702 /* Scan all insns in the loop, stopping if we find one that uses the
7703 biv in a way that we cannot eliminate. */
7704
7705 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
7706 {
7707 enum rtx_code code = GET_CODE (p);
7708 basic_block where_bb = 0;
7709 rtx where_insn = threshold >= insn_count ? 0 : p;
7710
7711 /* If this is a libcall that sets a giv, skip ahead to its end. */
7712 if (GET_RTX_CLASS (code) == 'i')
7713 {
7714 rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
7715
7716 if (note)
7717 {
7718 rtx last = XEXP (note, 0);
7719 rtx set = single_set (last);
7720
7721 if (set && GET_CODE (SET_DEST (set)) == REG)
7722 {
7723 unsigned int regno = REGNO (SET_DEST (set));
7724
7725 if (regno < ivs->n_regs
7726 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
7727 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
7728 p = last;
7729 }
7730 }
7731 }
7732 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
7733 && reg_mentioned_p (reg, PATTERN (p))
7734 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
7735 eliminate_p, where_bb, where_insn))
7736 {
7737 if (loop_dump_stream)
7738 fprintf (loop_dump_stream,
7739 "Cannot eliminate biv %d: biv used in insn %d.\n",
7740 bl->regno, INSN_UID (p));
7741 break;
7742 }
7743 }
7744
7745 if (p == loop->end)
7746 {
7747 if (loop_dump_stream)
7748 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
7749 bl->regno, eliminate_p ? "was" : "can be");
7750 return 1;
7751 }
7752
7753 return 0;
7754 }
7755 \f
7756 /* INSN and REFERENCE are instructions in the same insn chain.
7757 Return non-zero if INSN is first. */
7758
7759 int
7760 loop_insn_first_p (insn, reference)
7761 rtx insn, reference;
7762 {
7763 rtx p, q;
7764
7765 for (p = insn, q = reference;;)
7766 {
7767 /* Start with test for not first so that INSN == REFERENCE yields not
7768 first. */
7769 if (q == insn || ! p)
7770 return 0;
7771 if (p == reference || ! q)
7772 return 1;
7773
7774 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
7775 previous insn, hence the <= comparison below does not work if
7776 P is a note. */
7777 if (INSN_UID (p) < max_uid_for_loop
7778 && INSN_UID (q) < max_uid_for_loop
7779 && GET_CODE (p) != NOTE)
7780 return INSN_LUID (p) <= INSN_LUID (q);
7781
7782 if (INSN_UID (p) >= max_uid_for_loop
7783 || GET_CODE (p) == NOTE)
7784 p = NEXT_INSN (p);
7785 if (INSN_UID (q) >= max_uid_for_loop)
7786 q = NEXT_INSN (q);
7787 }
7788 }
7789
7790 /* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
7791 the offset that we have to take into account due to auto-increment /
7792 div derivation is zero. */
7793 static int
7794 biv_elimination_giv_has_0_offset (biv, giv, insn)
7795 struct induction *biv, *giv;
7796 rtx insn;
7797 {
7798 /* If the giv V had the auto-inc address optimization applied
7799 to it, and INSN occurs between the giv insn and the biv
7800 insn, then we'd have to adjust the value used here.
7801 This is rare, so we don't bother to make this possible. */
7802 if (giv->auto_inc_opt
7803 && ((loop_insn_first_p (giv->insn, insn)
7804 && loop_insn_first_p (insn, biv->insn))
7805 || (loop_insn_first_p (biv->insn, insn)
7806 && loop_insn_first_p (insn, giv->insn))))
7807 return 0;
7808
7809 return 1;
7810 }
7811
7812 /* If BL appears in X (part of the pattern of INSN), see if we can
7813 eliminate its use. If so, return 1. If not, return 0.
7814
7815 If BIV does not appear in X, return 1.
7816
7817 If ELIMINATE_P is non-zero, actually do the elimination.
7818 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
7819 Depending on how many items have been moved out of the loop, it
7820 will either be before INSN (when WHERE_INSN is non-zero) or at the
7821 start of the loop (when WHERE_INSN is zero). */
7822
7823 static int
7824 maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
7825 const struct loop *loop;
7826 rtx x, insn;
7827 struct iv_class *bl;
7828 int eliminate_p;
7829 basic_block where_bb;
7830 rtx where_insn;
7831 {
7832 enum rtx_code code = GET_CODE (x);
7833 rtx reg = bl->biv->dest_reg;
7834 enum machine_mode mode = GET_MODE (reg);
7835 struct induction *v;
7836 rtx arg, tem;
7837 #ifdef HAVE_cc0
7838 rtx new;
7839 #endif
7840 int arg_operand;
7841 const char *fmt;
7842 int i, j;
7843
7844 switch (code)
7845 {
7846 case REG:
7847 /* If we haven't already been able to do something with this BIV,
7848 we can't eliminate it. */
7849 if (x == reg)
7850 return 0;
7851 return 1;
7852
7853 case SET:
7854 /* If this sets the BIV, it is not a problem. */
7855 if (SET_DEST (x) == reg)
7856 return 1;
7857
7858 /* If this is an insn that defines a giv, it is also ok because
7859 it will go away when the giv is reduced. */
7860 for (v = bl->giv; v; v = v->next_iv)
7861 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
7862 return 1;
7863
7864 #ifdef HAVE_cc0
7865 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
7866 {
7867 /* Can replace with any giv that was reduced and
7868 that has (MULT_VAL != 0) and (ADD_VAL == 0).
7869 Require a constant for MULT_VAL, so we know it's nonzero.
7870 ??? We disable this optimization to avoid potential
7871 overflows. */
7872
7873 for (v = bl->giv; v; v = v->next_iv)
7874 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
7875 && v->add_val == const0_rtx
7876 && ! v->ignore && ! v->maybe_dead && v->always_computable
7877 && v->mode == mode
7878 && 0)
7879 {
7880 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
7881 continue;
7882
7883 if (! eliminate_p)
7884 return 1;
7885
7886 /* If the giv has the opposite direction of change,
7887 then reverse the comparison. */
7888 if (INTVAL (v->mult_val) < 0)
7889 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
7890 const0_rtx, v->new_reg);
7891 else
7892 new = v->new_reg;
7893
7894 /* We can probably test that giv's reduced reg. */
7895 if (validate_change (insn, &SET_SRC (x), new, 0))
7896 return 1;
7897 }
7898
7899 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
7900 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
7901 Require a constant for MULT_VAL, so we know it's nonzero.
7902 ??? Do this only if ADD_VAL is a pointer to avoid a potential
7903 overflow problem. */
7904
7905 for (v = bl->giv; v; v = v->next_iv)
7906 if (GET_CODE (v->mult_val) == CONST_INT
7907 && v->mult_val != const0_rtx
7908 && ! v->ignore && ! v->maybe_dead && v->always_computable
7909 && v->mode == mode
7910 && (GET_CODE (v->add_val) == SYMBOL_REF
7911 || GET_CODE (v->add_val) == LABEL_REF
7912 || GET_CODE (v->add_val) == CONST
7913 || (GET_CODE (v->add_val) == REG
7914 && REG_POINTER (v->add_val))))
7915 {
7916 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
7917 continue;
7918
7919 if (! eliminate_p)
7920 return 1;
7921
7922 /* If the giv has the opposite direction of change,
7923 then reverse the comparison. */
7924 if (INTVAL (v->mult_val) < 0)
7925 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
7926 v->new_reg);
7927 else
7928 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
7929 copy_rtx (v->add_val));
7930
7931 /* Replace biv with the giv's reduced register. */
7932 update_reg_last_use (v->add_val, insn);
7933 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
7934 return 1;
7935
7936 /* Insn doesn't support that constant or invariant. Copy it
7937 into a register (it will be a loop invariant.) */
7938 tem = gen_reg_rtx (GET_MODE (v->new_reg));
7939
7940 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
7941 where_insn);
7942
7943 /* Substitute the new register for its invariant value in
7944 the compare expression. */
7945 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
7946 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
7947 return 1;
7948 }
7949 }
7950 #endif
7951 break;
7952
7953 case COMPARE:
7954 case EQ: case NE:
7955 case GT: case GE: case GTU: case GEU:
7956 case LT: case LE: case LTU: case LEU:
7957 /* See if either argument is the biv. */
7958 if (XEXP (x, 0) == reg)
7959 arg = XEXP (x, 1), arg_operand = 1;
7960 else if (XEXP (x, 1) == reg)
7961 arg = XEXP (x, 0), arg_operand = 0;
7962 else
7963 break;
7964
7965 if (CONSTANT_P (arg))
7966 {
7967 /* First try to replace with any giv that has constant positive
7968 mult_val and constant add_val. We might be able to support
7969 negative mult_val, but it seems complex to do it in general. */
7970
7971 for (v = bl->giv; v; v = v->next_iv)
7972 if (GET_CODE (v->mult_val) == CONST_INT
7973 && INTVAL (v->mult_val) > 0
7974 && (GET_CODE (v->add_val) == SYMBOL_REF
7975 || GET_CODE (v->add_val) == LABEL_REF
7976 || GET_CODE (v->add_val) == CONST
7977 || (GET_CODE (v->add_val) == REG
7978 && REG_POINTER (v->add_val)))
7979 && ! v->ignore && ! v->maybe_dead && v->always_computable
7980 && v->mode == mode)
7981 {
7982 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
7983 continue;
7984
7985 if (! eliminate_p)
7986 return 1;
7987
7988 /* Replace biv with the giv's reduced reg. */
7989 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
7990
7991 /* If all constants are actually constant integers and
7992 the derived constant can be directly placed in the COMPARE,
7993 do so. */
7994 if (GET_CODE (arg) == CONST_INT
7995 && GET_CODE (v->mult_val) == CONST_INT
7996 && GET_CODE (v->add_val) == CONST_INT)
7997 {
7998 validate_change (insn, &XEXP (x, arg_operand),
7999 GEN_INT (INTVAL (arg)
8000 * INTVAL (v->mult_val)
8001 + INTVAL (v->add_val)), 1);
8002 }
8003 else
8004 {
8005 /* Otherwise, load it into a register. */
8006 tem = gen_reg_rtx (mode);
8007 loop_iv_add_mult_emit_before (loop, arg,
8008 v->mult_val, v->add_val,
8009 tem, where_bb, where_insn);
8010 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8011 }
8012 if (apply_change_group ())
8013 return 1;
8014 }
8015
8016 /* Look for giv with positive constant mult_val and nonconst add_val.
8017 Insert insns to calculate new compare value.
8018 ??? Turn this off due to possible overflow. */
8019
8020 for (v = bl->giv; v; v = v->next_iv)
8021 if (GET_CODE (v->mult_val) == CONST_INT
8022 && INTVAL (v->mult_val) > 0
8023 && ! v->ignore && ! v->maybe_dead && v->always_computable
8024 && v->mode == mode
8025 && 0)
8026 {
8027 rtx tem;
8028
8029 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8030 continue;
8031
8032 if (! eliminate_p)
8033 return 1;
8034
8035 tem = gen_reg_rtx (mode);
8036
8037 /* Replace biv with giv's reduced register. */
8038 validate_change (insn, &XEXP (x, 1 - arg_operand),
8039 v->new_reg, 1);
8040
8041 /* Compute value to compare against. */
8042 loop_iv_add_mult_emit_before (loop, arg,
8043 v->mult_val, v->add_val,
8044 tem, where_bb, where_insn);
8045 /* Use it in this insn. */
8046 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8047 if (apply_change_group ())
8048 return 1;
8049 }
8050 }
8051 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8052 {
8053 if (loop_invariant_p (loop, arg) == 1)
8054 {
8055 /* Look for giv with constant positive mult_val and nonconst
8056 add_val. Insert insns to compute new compare value.
8057 ??? Turn this off due to possible overflow. */
8058
8059 for (v = bl->giv; v; v = v->next_iv)
8060 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8061 && ! v->ignore && ! v->maybe_dead && v->always_computable
8062 && v->mode == mode
8063 && 0)
8064 {
8065 rtx tem;
8066
8067 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8068 continue;
8069
8070 if (! eliminate_p)
8071 return 1;
8072
8073 tem = gen_reg_rtx (mode);
8074
8075 /* Replace biv with giv's reduced register. */
8076 validate_change (insn, &XEXP (x, 1 - arg_operand),
8077 v->new_reg, 1);
8078
8079 /* Compute value to compare against. */
8080 loop_iv_add_mult_emit_before (loop, arg,
8081 v->mult_val, v->add_val,
8082 tem, where_bb, where_insn);
8083 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8084 if (apply_change_group ())
8085 return 1;
8086 }
8087 }
8088
8089 /* This code has problems. Basically, you can't know when
8090 seeing if we will eliminate BL, whether a particular giv
8091 of ARG will be reduced. If it isn't going to be reduced,
8092 we can't eliminate BL. We can try forcing it to be reduced,
8093 but that can generate poor code.
8094
8095 The problem is that the benefit of reducing TV, below should
8096 be increased if BL can actually be eliminated, but this means
8097 we might have to do a topological sort of the order in which
8098 we try to process biv. It doesn't seem worthwhile to do
8099 this sort of thing now. */
8100
8101 #if 0
8102 /* Otherwise the reg compared with had better be a biv. */
8103 if (GET_CODE (arg) != REG
8104 || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
8105 return 0;
8106
8107 /* Look for a pair of givs, one for each biv,
8108 with identical coefficients. */
8109 for (v = bl->giv; v; v = v->next_iv)
8110 {
8111 struct induction *tv;
8112
8113 if (v->ignore || v->maybe_dead || v->mode != mode)
8114 continue;
8115
8116 for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
8117 tv = tv->next_iv)
8118 if (! tv->ignore && ! tv->maybe_dead
8119 && rtx_equal_p (tv->mult_val, v->mult_val)
8120 && rtx_equal_p (tv->add_val, v->add_val)
8121 && tv->mode == mode)
8122 {
8123 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8124 continue;
8125
8126 if (! eliminate_p)
8127 return 1;
8128
8129 /* Replace biv with its giv's reduced reg. */
8130 XEXP (x, 1 - arg_operand) = v->new_reg;
8131 /* Replace other operand with the other giv's
8132 reduced reg. */
8133 XEXP (x, arg_operand) = tv->new_reg;
8134 return 1;
8135 }
8136 }
8137 #endif
8138 }
8139
8140 /* If we get here, the biv can't be eliminated. */
8141 return 0;
8142
8143 case MEM:
8144 /* If this address is a DEST_ADDR giv, it doesn't matter if the
8145 biv is used in it, since it will be replaced. */
8146 for (v = bl->giv; v; v = v->next_iv)
8147 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
8148 return 1;
8149 break;
8150
8151 default:
8152 break;
8153 }
8154
8155 /* See if any subexpression fails elimination. */
8156 fmt = GET_RTX_FORMAT (code);
8157 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8158 {
8159 switch (fmt[i])
8160 {
8161 case 'e':
8162 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
8163 eliminate_p, where_bb, where_insn))
8164 return 0;
8165 break;
8166
8167 case 'E':
8168 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8169 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
8170 eliminate_p, where_bb, where_insn))
8171 return 0;
8172 break;
8173 }
8174 }
8175
8176 return 1;
8177 }
8178 \f
8179 /* Return nonzero if the last use of REG
8180 is in an insn following INSN in the same basic block. */
8181
8182 static int
8183 last_use_this_basic_block (reg, insn)
8184 rtx reg;
8185 rtx insn;
8186 {
8187 rtx n;
8188 for (n = insn;
8189 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
8190 n = NEXT_INSN (n))
8191 {
8192 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
8193 return 1;
8194 }
8195 return 0;
8196 }
8197 \f
8198 /* Called via `note_stores' to record the initial value of a biv. Here we
8199 just record the location of the set and process it later. */
8200
8201 static void
8202 record_initial (dest, set, data)
8203 rtx dest;
8204 rtx set;
8205 void *data ATTRIBUTE_UNUSED;
8206 {
8207 struct loop_ivs *ivs = (struct loop_ivs *) data;
8208 struct iv_class *bl;
8209
8210 if (GET_CODE (dest) != REG
8211 || REGNO (dest) >= ivs->n_regs
8212 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
8213 return;
8214
8215 bl = REG_IV_CLASS (ivs, REGNO (dest));
8216
8217 /* If this is the first set found, record it. */
8218 if (bl->init_insn == 0)
8219 {
8220 bl->init_insn = note_insn;
8221 bl->init_set = set;
8222 }
8223 }
8224 \f
8225 /* If any of the registers in X are "old" and currently have a last use earlier
8226 than INSN, update them to have a last use of INSN. Their actual last use
8227 will be the previous insn but it will not have a valid uid_luid so we can't
8228 use it. X must be a source expression only. */
8229
8230 static void
8231 update_reg_last_use (x, insn)
8232 rtx x;
8233 rtx insn;
8234 {
8235 /* Check for the case where INSN does not have a valid luid. In this case,
8236 there is no need to modify the regno_last_uid, as this can only happen
8237 when code is inserted after the loop_end to set a pseudo's final value,
8238 and hence this insn will never be the last use of x.
8239 ???? This comment is not correct. See for example loop_givs_reduce.
8240 This may insert an insn before another new insn. */
8241 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
8242 && INSN_UID (insn) < max_uid_for_loop
8243 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
8244 {
8245 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
8246 }
8247 else
8248 {
8249 register int i, j;
8250 register const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
8251 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8252 {
8253 if (fmt[i] == 'e')
8254 update_reg_last_use (XEXP (x, i), insn);
8255 else if (fmt[i] == 'E')
8256 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8257 update_reg_last_use (XVECEXP (x, i, j), insn);
8258 }
8259 }
8260 }
8261 \f
8262 /* Given an insn INSN and condition COND, return the condition in a
8263 canonical form to simplify testing by callers. Specifically:
8264
8265 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
8266 (2) Both operands will be machine operands; (cc0) will have been replaced.
8267 (3) If an operand is a constant, it will be the second operand.
8268 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
8269 for GE, GEU, and LEU.
8270
8271 If the condition cannot be understood, or is an inequality floating-point
8272 comparison which needs to be reversed, 0 will be returned.
8273
8274 If REVERSE is non-zero, then reverse the condition prior to canonizing it.
8275
8276 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8277 insn used in locating the condition was found. If a replacement test
8278 of the condition is desired, it should be placed in front of that
8279 insn and we will be sure that the inputs are still valid.
8280
8281 If WANT_REG is non-zero, we wish the condition to be relative to that
8282 register, if possible. Therefore, do not canonicalize the condition
8283 further. */
8284
8285 rtx
8286 canonicalize_condition (insn, cond, reverse, earliest, want_reg)
8287 rtx insn;
8288 rtx cond;
8289 int reverse;
8290 rtx *earliest;
8291 rtx want_reg;
8292 {
8293 enum rtx_code code;
8294 rtx prev = insn;
8295 rtx set;
8296 rtx tem;
8297 rtx op0, op1;
8298 int reverse_code = 0;
8299 int did_reverse_condition = 0;
8300 enum machine_mode mode;
8301
8302 code = GET_CODE (cond);
8303 mode = GET_MODE (cond);
8304 op0 = XEXP (cond, 0);
8305 op1 = XEXP (cond, 1);
8306
8307 if (reverse)
8308 {
8309 code = reverse_condition (code);
8310 did_reverse_condition ^= 1;
8311 }
8312
8313 if (earliest)
8314 *earliest = insn;
8315
8316 /* If we are comparing a register with zero, see if the register is set
8317 in the previous insn to a COMPARE or a comparison operation. Perform
8318 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
8319 in cse.c */
8320
8321 while (GET_RTX_CLASS (code) == '<'
8322 && op1 == CONST0_RTX (GET_MODE (op0))
8323 && op0 != want_reg)
8324 {
8325 /* Set non-zero when we find something of interest. */
8326 rtx x = 0;
8327
8328 #ifdef HAVE_cc0
8329 /* If comparison with cc0, import actual comparison from compare
8330 insn. */
8331 if (op0 == cc0_rtx)
8332 {
8333 if ((prev = prev_nonnote_insn (prev)) == 0
8334 || GET_CODE (prev) != INSN
8335 || (set = single_set (prev)) == 0
8336 || SET_DEST (set) != cc0_rtx)
8337 return 0;
8338
8339 op0 = SET_SRC (set);
8340 op1 = CONST0_RTX (GET_MODE (op0));
8341 if (earliest)
8342 *earliest = prev;
8343 }
8344 #endif
8345
8346 /* If this is a COMPARE, pick up the two things being compared. */
8347 if (GET_CODE (op0) == COMPARE)
8348 {
8349 op1 = XEXP (op0, 1);
8350 op0 = XEXP (op0, 0);
8351 continue;
8352 }
8353 else if (GET_CODE (op0) != REG)
8354 break;
8355
8356 /* Go back to the previous insn. Stop if it is not an INSN. We also
8357 stop if it isn't a single set or if it has a REG_INC note because
8358 we don't want to bother dealing with it. */
8359
8360 if ((prev = prev_nonnote_insn (prev)) == 0
8361 || GET_CODE (prev) != INSN
8362 || FIND_REG_INC_NOTE (prev, 0)
8363 || (set = single_set (prev)) == 0)
8364 break;
8365
8366 /* If this is setting OP0, get what it sets it to if it looks
8367 relevant. */
8368 if (rtx_equal_p (SET_DEST (set), op0))
8369 {
8370 enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
8371
8372 /* ??? We may not combine comparisons done in a CCmode with
8373 comparisons not done in a CCmode. This is to aid targets
8374 like Alpha that have an IEEE compliant EQ instruction, and
8375 a non-IEEE compliant BEQ instruction. The use of CCmode is
8376 actually artificial, simply to prevent the combination, but
8377 should not affect other platforms.
8378
8379 However, we must allow VOIDmode comparisons to match either
8380 CCmode or non-CCmode comparison, because some ports have
8381 modeless comparisons inside branch patterns.
8382
8383 ??? This mode check should perhaps look more like the mode check
8384 in simplify_comparison in combine. */
8385
8386 if ((GET_CODE (SET_SRC (set)) == COMPARE
8387 || (((code == NE
8388 || (code == LT
8389 && GET_MODE_CLASS (inner_mode) == MODE_INT
8390 && (GET_MODE_BITSIZE (inner_mode)
8391 <= HOST_BITS_PER_WIDE_INT)
8392 && (STORE_FLAG_VALUE
8393 & ((HOST_WIDE_INT) 1
8394 << (GET_MODE_BITSIZE (inner_mode) - 1))))
8395 #ifdef FLOAT_STORE_FLAG_VALUE
8396 || (code == LT
8397 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8398 && (REAL_VALUE_NEGATIVE
8399 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
8400 #endif
8401 ))
8402 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
8403 && (((GET_MODE_CLASS (mode) == MODE_CC)
8404 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8405 || mode == VOIDmode || inner_mode == VOIDmode))
8406 x = SET_SRC (set);
8407 else if (((code == EQ
8408 || (code == GE
8409 && (GET_MODE_BITSIZE (inner_mode)
8410 <= HOST_BITS_PER_WIDE_INT)
8411 && GET_MODE_CLASS (inner_mode) == MODE_INT
8412 && (STORE_FLAG_VALUE
8413 & ((HOST_WIDE_INT) 1
8414 << (GET_MODE_BITSIZE (inner_mode) - 1))))
8415 #ifdef FLOAT_STORE_FLAG_VALUE
8416 || (code == GE
8417 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8418 && (REAL_VALUE_NEGATIVE
8419 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
8420 #endif
8421 ))
8422 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
8423 && (((GET_MODE_CLASS (mode) == MODE_CC)
8424 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8425 || mode == VOIDmode || inner_mode == VOIDmode))
8426
8427 {
8428 /* We might have reversed a LT to get a GE here. But this wasn't
8429 actually the comparison of data, so we don't flag that we
8430 have had to reverse the condition. */
8431 did_reverse_condition ^= 1;
8432 reverse_code = 1;
8433 x = SET_SRC (set);
8434 }
8435 else
8436 break;
8437 }
8438
8439 else if (reg_set_p (op0, prev))
8440 /* If this sets OP0, but not directly, we have to give up. */
8441 break;
8442
8443 if (x)
8444 {
8445 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
8446 code = GET_CODE (x);
8447 if (reverse_code)
8448 {
8449 code = reverse_condition (code);
8450 if (code == UNKNOWN)
8451 return 0;
8452 did_reverse_condition ^= 1;
8453 reverse_code = 0;
8454 }
8455
8456 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
8457 if (earliest)
8458 *earliest = prev;
8459 }
8460 }
8461
8462 /* If constant is first, put it last. */
8463 if (CONSTANT_P (op0))
8464 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
8465
8466 /* If OP0 is the result of a comparison, we weren't able to find what
8467 was really being compared, so fail. */
8468 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
8469 return 0;
8470
8471 /* Canonicalize any ordered comparison with integers involving equality
8472 if we can do computations in the relevant mode and we do not
8473 overflow. */
8474
8475 if (GET_CODE (op1) == CONST_INT
8476 && GET_MODE (op0) != VOIDmode
8477 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
8478 {
8479 HOST_WIDE_INT const_val = INTVAL (op1);
8480 unsigned HOST_WIDE_INT uconst_val = const_val;
8481 unsigned HOST_WIDE_INT max_val
8482 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
8483
8484 switch (code)
8485 {
8486 case LE:
8487 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
8488 code = LT, op1 = GEN_INT (const_val + 1);
8489 break;
8490
8491 /* When cross-compiling, const_val might be sign-extended from
8492 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
8493 case GE:
8494 if ((HOST_WIDE_INT) (const_val & max_val)
8495 != (((HOST_WIDE_INT) 1
8496 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
8497 code = GT, op1 = GEN_INT (const_val - 1);
8498 break;
8499
8500 case LEU:
8501 if (uconst_val < max_val)
8502 code = LTU, op1 = GEN_INT (uconst_val + 1);
8503 break;
8504
8505 case GEU:
8506 if (uconst_val != 0)
8507 code = GTU, op1 = GEN_INT (uconst_val - 1);
8508 break;
8509
8510 default:
8511 break;
8512 }
8513 }
8514
8515 /* If this was floating-point and we reversed anything other than an
8516 EQ or NE or (UN)ORDERED, return zero. */
8517 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
8518 && did_reverse_condition
8519 && code != NE && code != EQ && code != UNORDERED && code != ORDERED
8520 && ! flag_fast_math
8521 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8522 return 0;
8523
8524 #ifdef HAVE_cc0
8525 /* Never return CC0; return zero instead. */
8526 if (op0 == cc0_rtx)
8527 return 0;
8528 #endif
8529
8530 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
8531 }
8532
8533 /* Given a jump insn JUMP, return the condition that will cause it to branch
8534 to its JUMP_LABEL. If the condition cannot be understood, or is an
8535 inequality floating-point comparison which needs to be reversed, 0 will
8536 be returned.
8537
8538 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8539 insn used in locating the condition was found. If a replacement test
8540 of the condition is desired, it should be placed in front of that
8541 insn and we will be sure that the inputs are still valid. */
8542
8543 rtx
8544 get_condition (jump, earliest)
8545 rtx jump;
8546 rtx *earliest;
8547 {
8548 rtx cond;
8549 int reverse;
8550 rtx set;
8551
8552 /* If this is not a standard conditional jump, we can't parse it. */
8553 if (GET_CODE (jump) != JUMP_INSN
8554 || ! any_condjump_p (jump))
8555 return 0;
8556 set = pc_set (jump);
8557
8558 cond = XEXP (SET_SRC (set), 0);
8559
8560 /* If this branches to JUMP_LABEL when the condition is false, reverse
8561 the condition. */
8562 reverse
8563 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
8564 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
8565
8566 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
8567 }
8568
8569 /* Similar to above routine, except that we also put an invariant last
8570 unless both operands are invariants. */
8571
8572 rtx
8573 get_condition_for_loop (loop, x)
8574 const struct loop *loop;
8575 rtx x;
8576 {
8577 rtx comparison = get_condition (x, NULL_PTR);
8578
8579 if (comparison == 0
8580 || ! loop_invariant_p (loop, XEXP (comparison, 0))
8581 || loop_invariant_p (loop, XEXP (comparison, 1)))
8582 return comparison;
8583
8584 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
8585 XEXP (comparison, 1), XEXP (comparison, 0));
8586 }
8587
8588 /* Scan the function and determine whether it has indirect (computed) jumps.
8589
8590 This is taken mostly from flow.c; similar code exists elsewhere
8591 in the compiler. It may be useful to put this into rtlanal.c. */
8592 static int
8593 indirect_jump_in_function_p (start)
8594 rtx start;
8595 {
8596 rtx insn;
8597
8598 for (insn = start; insn; insn = NEXT_INSN (insn))
8599 if (computed_jump_p (insn))
8600 return 1;
8601
8602 return 0;
8603 }
8604
8605 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
8606 documentation for LOOP_MEMS for the definition of `appropriate'.
8607 This function is called from prescan_loop via for_each_rtx. */
8608
8609 static int
8610 insert_loop_mem (mem, data)
8611 rtx *mem;
8612 void *data ATTRIBUTE_UNUSED;
8613 {
8614 struct loop_info *loop_info = data;
8615 int i;
8616 rtx m = *mem;
8617
8618 if (m == NULL_RTX)
8619 return 0;
8620
8621 switch (GET_CODE (m))
8622 {
8623 case MEM:
8624 break;
8625
8626 case CLOBBER:
8627 /* We're not interested in MEMs that are only clobbered. */
8628 return -1;
8629
8630 case CONST_DOUBLE:
8631 /* We're not interested in the MEM associated with a
8632 CONST_DOUBLE, so there's no need to traverse into this. */
8633 return -1;
8634
8635 case EXPR_LIST:
8636 /* We're not interested in any MEMs that only appear in notes. */
8637 return -1;
8638
8639 default:
8640 /* This is not a MEM. */
8641 return 0;
8642 }
8643
8644 /* See if we've already seen this MEM. */
8645 for (i = 0; i < loop_info->mems_idx; ++i)
8646 if (rtx_equal_p (m, loop_info->mems[i].mem))
8647 {
8648 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
8649 /* The modes of the two memory accesses are different. If
8650 this happens, something tricky is going on, and we just
8651 don't optimize accesses to this MEM. */
8652 loop_info->mems[i].optimize = 0;
8653
8654 return 0;
8655 }
8656
8657 /* Resize the array, if necessary. */
8658 if (loop_info->mems_idx == loop_info->mems_allocated)
8659 {
8660 if (loop_info->mems_allocated != 0)
8661 loop_info->mems_allocated *= 2;
8662 else
8663 loop_info->mems_allocated = 32;
8664
8665 loop_info->mems = (loop_mem_info *)
8666 xrealloc (loop_info->mems,
8667 loop_info->mems_allocated * sizeof (loop_mem_info));
8668 }
8669
8670 /* Actually insert the MEM. */
8671 loop_info->mems[loop_info->mems_idx].mem = m;
8672 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
8673 because we can't put it in a register. We still store it in the
8674 table, though, so that if we see the same address later, but in a
8675 non-BLK mode, we'll not think we can optimize it at that point. */
8676 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
8677 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
8678 ++loop_info->mems_idx;
8679
8680 return 0;
8681 }
8682
8683
8684 /* Allocate REGS->ARRAY or reallocate it if it is too small.
8685
8686 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
8687 register that is modified by an insn between FROM and TO. If the
8688 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
8689 more, stop incrementing it, to avoid overflow.
8690
8691 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
8692 register I is used, if it is only used once. Otherwise, it is set
8693 to 0 (for no uses) or const0_rtx for more than one use. This
8694 parameter may be zero, in which case this processing is not done.
8695
8696 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
8697 optimize register I.
8698
8699 Store in *COUNT_PTR the number of actual instructions
8700 in the loop. We use this to decide what is worth moving out. */
8701
8702 static void
8703 loop_regs_scan (loop, extra_size, count_ptr)
8704 const struct loop *loop;
8705 int extra_size;
8706 int *count_ptr;
8707 {
8708 struct loop_regs *regs = LOOP_REGS (loop);
8709 int old_nregs;
8710 /* last_set[n] is nonzero iff reg n has been set in the current
8711 basic block. In that case, it is the insn that last set reg n. */
8712 rtx *last_set;
8713 rtx insn;
8714 int count = 0;
8715 int i;
8716
8717 old_nregs = regs->num;
8718 regs->num = max_reg_num ();
8719
8720 /* Grow the regs array if not allocated or too small. */
8721 if (regs->num >= regs->size)
8722 {
8723 regs->size = regs->num + extra_size;
8724
8725 regs->array = (struct loop_reg *)
8726 xrealloc (regs->array, regs->size * sizeof (*regs->array));
8727
8728 /* Zero the new elements. */
8729 memset (regs->array + old_nregs, 0,
8730 (regs->size - old_nregs) * sizeof (*regs->array));
8731 }
8732
8733 /* Clear previously scanned fields but do not clear n_times_set. */
8734 for (i = 0; i < old_nregs; i++)
8735 {
8736 regs->array[i].set_in_loop = 0;
8737 regs->array[i].may_not_optimize = 0;
8738 regs->array[i].single_usage = NULL_RTX;
8739 }
8740
8741 last_set = (rtx *) xcalloc (regs->num, sizeof (rtx));
8742
8743 /* Scan the loop, recording register usage. */
8744 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
8745 insn = NEXT_INSN (insn))
8746 {
8747 if (INSN_P (insn))
8748 {
8749 ++count;
8750
8751 /* Record registers that have exactly one use. */
8752 find_single_use_in_loop (regs, insn, PATTERN (insn));
8753
8754 /* Include uses in REG_EQUAL notes. */
8755 if (REG_NOTES (insn))
8756 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
8757
8758 if (GET_CODE (PATTERN (insn)) == SET
8759 || GET_CODE (PATTERN (insn)) == CLOBBER)
8760 count_one_set (regs, insn, PATTERN (insn), last_set);
8761 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
8762 {
8763 register int i;
8764 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
8765 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
8766 last_set);
8767 }
8768 }
8769
8770 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
8771 memset (last_set, 0, regs->num * sizeof (rtx));
8772 }
8773
8774 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8775 {
8776 regs->array[i].may_not_optimize = 1;
8777 regs->array[i].set_in_loop = 1;
8778 }
8779
8780 #ifdef AVOID_CCMODE_COPIES
8781 /* Don't try to move insns which set CC registers if we should not
8782 create CCmode register copies. */
8783 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
8784 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
8785 regs->array[i].may_not_optimize = 1;
8786 #endif
8787
8788 /* Set regs->array[I].n_times_set for the new registers. */
8789 for (i = old_nregs; i < regs->num; i++)
8790 regs->array[i].n_times_set = regs->array[i].set_in_loop;
8791
8792 free (last_set);
8793 *count_ptr = count;
8794 }
8795
8796
8797 /* Move MEMs into registers for the duration of the loop. */
8798
8799 static void
8800 load_mems (loop)
8801 const struct loop *loop;
8802 {
8803 struct loop_info *loop_info = LOOP_INFO (loop);
8804 struct loop_regs *regs = LOOP_REGS (loop);
8805 int maybe_never = 0;
8806 int i;
8807 rtx p;
8808 rtx label = NULL_RTX;
8809 rtx end_label;
8810 /* Nonzero if the next instruction may never be executed. */
8811 int next_maybe_never = 0;
8812 int last_max_reg = max_reg_num ();
8813
8814 if (loop_info->mems_idx == 0)
8815 return;
8816
8817 /* We cannot use next_label here because it skips over normal insns. */
8818 end_label = next_nonnote_insn (loop->end);
8819 if (end_label && GET_CODE (end_label) != CODE_LABEL)
8820 end_label = NULL_RTX;
8821
8822 /* Check to see if it's possible that some instructions in the loop are
8823 never executed. Also check if there is a goto out of the loop other
8824 than right after the end of the loop. */
8825 for (p = next_insn_in_loop (loop, loop->scan_start);
8826 p != NULL_RTX && ! maybe_never;
8827 p = next_insn_in_loop (loop, p))
8828 {
8829 if (GET_CODE (p) == CODE_LABEL)
8830 maybe_never = 1;
8831 else if (GET_CODE (p) == JUMP_INSN
8832 /* If we enter the loop in the middle, and scan
8833 around to the beginning, don't set maybe_never
8834 for that. This must be an unconditional jump,
8835 otherwise the code at the top of the loop might
8836 never be executed. Unconditional jumps are
8837 followed a by barrier then loop end. */
8838 && ! (GET_CODE (p) == JUMP_INSN
8839 && JUMP_LABEL (p) == loop->top
8840 && NEXT_INSN (NEXT_INSN (p)) == loop->end
8841 && any_uncondjump_p (p)))
8842 {
8843 /* If this is a jump outside of the loop but not right
8844 after the end of the loop, we would have to emit new fixup
8845 sequences for each such label. */
8846 if (JUMP_LABEL (p) != end_label
8847 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
8848 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
8849 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end)))
8850 return;
8851
8852 if (!any_condjump_p (p))
8853 /* Something complicated. */
8854 maybe_never = 1;
8855 else
8856 /* If there are any more instructions in the loop, they
8857 might not be reached. */
8858 next_maybe_never = 1;
8859 }
8860 else if (next_maybe_never)
8861 maybe_never = 1;
8862 }
8863
8864 /* Find start of the extended basic block that enters the loop. */
8865 for (p = loop->start;
8866 PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
8867 p = PREV_INSN (p))
8868 ;
8869
8870 cselib_init ();
8871
8872 /* Build table of mems that get set to constant values before the
8873 loop. */
8874 for (; p != loop->start; p = NEXT_INSN (p))
8875 cselib_process_insn (p);
8876
8877 /* Actually move the MEMs. */
8878 for (i = 0; i < loop_info->mems_idx; ++i)
8879 {
8880 regset_head load_copies;
8881 regset_head store_copies;
8882 int written = 0;
8883 rtx reg;
8884 rtx mem = loop_info->mems[i].mem;
8885 rtx mem_list_entry;
8886
8887 if (MEM_VOLATILE_P (mem)
8888 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
8889 /* There's no telling whether or not MEM is modified. */
8890 loop_info->mems[i].optimize = 0;
8891
8892 /* Go through the MEMs written to in the loop to see if this
8893 one is aliased by one of them. */
8894 mem_list_entry = loop_info->store_mems;
8895 while (mem_list_entry)
8896 {
8897 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
8898 written = 1;
8899 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
8900 mem, rtx_varies_p))
8901 {
8902 /* MEM is indeed aliased by this store. */
8903 loop_info->mems[i].optimize = 0;
8904 break;
8905 }
8906 mem_list_entry = XEXP (mem_list_entry, 1);
8907 }
8908
8909 if (flag_float_store && written
8910 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
8911 loop_info->mems[i].optimize = 0;
8912
8913 /* If this MEM is written to, we must be sure that there
8914 are no reads from another MEM that aliases this one. */
8915 if (loop_info->mems[i].optimize && written)
8916 {
8917 int j;
8918
8919 for (j = 0; j < loop_info->mems_idx; ++j)
8920 {
8921 if (j == i)
8922 continue;
8923 else if (true_dependence (mem,
8924 VOIDmode,
8925 loop_info->mems[j].mem,
8926 rtx_varies_p))
8927 {
8928 /* It's not safe to hoist loop_info->mems[i] out of
8929 the loop because writes to it might not be
8930 seen by reads from loop_info->mems[j]. */
8931 loop_info->mems[i].optimize = 0;
8932 break;
8933 }
8934 }
8935 }
8936
8937 if (maybe_never && may_trap_p (mem))
8938 /* We can't access the MEM outside the loop; it might
8939 cause a trap that wouldn't have happened otherwise. */
8940 loop_info->mems[i].optimize = 0;
8941
8942 if (!loop_info->mems[i].optimize)
8943 /* We thought we were going to lift this MEM out of the
8944 loop, but later discovered that we could not. */
8945 continue;
8946
8947 INIT_REG_SET (&load_copies);
8948 INIT_REG_SET (&store_copies);
8949
8950 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
8951 order to keep scan_loop from moving stores to this MEM
8952 out of the loop just because this REG is neither a
8953 user-variable nor used in the loop test. */
8954 reg = gen_reg_rtx (GET_MODE (mem));
8955 REG_USERVAR_P (reg) = 1;
8956 loop_info->mems[i].reg = reg;
8957
8958 /* Now, replace all references to the MEM with the
8959 corresponding pesudos. */
8960 maybe_never = 0;
8961 for (p = next_insn_in_loop (loop, loop->scan_start);
8962 p != NULL_RTX;
8963 p = next_insn_in_loop (loop, p))
8964 {
8965 if (INSN_P (p))
8966 {
8967 rtx set;
8968
8969 set = single_set (p);
8970
8971 /* See if this copies the mem into a register that isn't
8972 modified afterwards. We'll try to do copy propagation
8973 a little further on. */
8974 if (set
8975 /* @@@ This test is _way_ too conservative. */
8976 && ! maybe_never
8977 && GET_CODE (SET_DEST (set)) == REG
8978 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
8979 && REGNO (SET_DEST (set)) < last_max_reg
8980 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
8981 && rtx_equal_p (SET_SRC (set), mem))
8982 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
8983
8984 /* See if this copies the mem from a register that isn't
8985 modified afterwards. We'll try to remove the
8986 redundant copy later on by doing a little register
8987 renaming and copy propagation. This will help
8988 to untangle things for the BIV detection code. */
8989 if (set
8990 && ! maybe_never
8991 && GET_CODE (SET_SRC (set)) == REG
8992 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
8993 && REGNO (SET_SRC (set)) < last_max_reg
8994 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
8995 && rtx_equal_p (SET_DEST (set), mem))
8996 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
8997
8998 /* Replace the memory reference with the shadow register. */
8999 replace_loop_mems (p, loop_info->mems[i].mem,
9000 loop_info->mems[i].reg);
9001 }
9002
9003 if (GET_CODE (p) == CODE_LABEL
9004 || GET_CODE (p) == JUMP_INSN)
9005 maybe_never = 1;
9006 }
9007
9008 if (! apply_change_group ())
9009 /* We couldn't replace all occurrences of the MEM. */
9010 loop_info->mems[i].optimize = 0;
9011 else
9012 {
9013 /* Load the memory immediately before LOOP->START, which is
9014 the NOTE_LOOP_BEG. */
9015 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9016 rtx set;
9017 rtx best = mem;
9018 int j;
9019 struct elt_loc_list *const_equiv = 0;
9020
9021 if (e)
9022 {
9023 struct elt_loc_list *equiv;
9024 struct elt_loc_list *best_equiv = 0;
9025 for (equiv = e->locs; equiv; equiv = equiv->next)
9026 {
9027 if (CONSTANT_P (equiv->loc))
9028 const_equiv = equiv;
9029 else if (GET_CODE (equiv->loc) == REG
9030 /* Extending hard register lifetimes cuases crash
9031 on SRC targets. Doing so on non-SRC is
9032 probably also not good idea, since we most
9033 probably have pseudoregister equivalence as
9034 well. */
9035 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
9036 best_equiv = equiv;
9037 }
9038 /* Use the constant equivalence if that is cheap enough. */
9039 if (! best_equiv)
9040 best_equiv = const_equiv;
9041 else if (const_equiv
9042 && (rtx_cost (const_equiv->loc, SET)
9043 <= rtx_cost (best_equiv->loc, SET)))
9044 {
9045 best_equiv = const_equiv;
9046 const_equiv = 0;
9047 }
9048
9049 /* If best_equiv is nonzero, we know that MEM is set to a
9050 constant or register before the loop. We will use this
9051 knowledge to initialize the shadow register with that
9052 constant or reg rather than by loading from MEM. */
9053 if (best_equiv)
9054 best = copy_rtx (best_equiv->loc);
9055 }
9056 set = gen_move_insn (reg, best);
9057 set = loop_insn_hoist (loop, set);
9058 if (const_equiv)
9059 REG_NOTES (set) = gen_rtx_EXPR_LIST (REG_EQUAL,
9060 copy_rtx (const_equiv->loc),
9061 REG_NOTES (set));
9062
9063 if (written)
9064 {
9065 if (label == NULL_RTX)
9066 {
9067 label = gen_label_rtx ();
9068 emit_label_after (label, loop->end);
9069 }
9070
9071 /* Store the memory immediately after END, which is
9072 the NOTE_LOOP_END. */
9073 set = gen_move_insn (copy_rtx (mem), reg);
9074 emit_insn_after (set, label);
9075 }
9076
9077 if (loop_dump_stream)
9078 {
9079 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9080 REGNO (reg), (written ? "r/w" : "r/o"));
9081 print_rtl (loop_dump_stream, mem);
9082 fputc ('\n', loop_dump_stream);
9083 }
9084
9085 /* Attempt a bit of copy propagation. This helps untangle the
9086 data flow, and enables {basic,general}_induction_var to find
9087 more bivs/givs. */
9088 EXECUTE_IF_SET_IN_REG_SET
9089 (&load_copies, FIRST_PSEUDO_REGISTER, j,
9090 {
9091 try_copy_prop (loop, reg, j);
9092 });
9093 CLEAR_REG_SET (&load_copies);
9094
9095 EXECUTE_IF_SET_IN_REG_SET
9096 (&store_copies, FIRST_PSEUDO_REGISTER, j,
9097 {
9098 try_swap_copy_prop (loop, reg, j);
9099 });
9100 CLEAR_REG_SET (&store_copies);
9101 }
9102 }
9103
9104 if (label != NULL_RTX && end_label != NULL_RTX)
9105 {
9106 /* Now, we need to replace all references to the previous exit
9107 label with the new one. */
9108 rtx_pair rr;
9109 rr.r1 = end_label;
9110 rr.r2 = label;
9111
9112 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
9113 {
9114 for_each_rtx (&p, replace_label, &rr);
9115
9116 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
9117 field. This is not handled by for_each_rtx because it doesn't
9118 handle unprinted ('0') fields. We need to update JUMP_LABEL
9119 because the immediately following unroll pass will use it.
9120 replace_label would not work anyways, because that only handles
9121 LABEL_REFs. */
9122 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
9123 JUMP_LABEL (p) = label;
9124 }
9125 }
9126
9127 cselib_finish ();
9128 }
9129
9130 /* For communication between note_reg_stored and its caller. */
9131 struct note_reg_stored_arg
9132 {
9133 int set_seen;
9134 rtx reg;
9135 };
9136
9137 /* Called via note_stores, record in SET_SEEN whether X, which is written,
9138 is equal to ARG. */
9139 static void
9140 note_reg_stored (x, setter, arg)
9141 rtx x, setter ATTRIBUTE_UNUSED;
9142 void *arg;
9143 {
9144 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
9145 if (t->reg == x)
9146 t->set_seen = 1;
9147 }
9148
9149 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
9150 There must be exactly one insn that sets this pseudo; it will be
9151 deleted if all replacements succeed and we can prove that the register
9152 is not used after the loop. */
9153
9154 static void
9155 try_copy_prop (loop, replacement, regno)
9156 const struct loop *loop;
9157 rtx replacement;
9158 unsigned int regno;
9159 {
9160 /* This is the reg that we are copying from. */
9161 rtx reg_rtx = regno_reg_rtx[regno];
9162 rtx init_insn = 0;
9163 rtx insn;
9164 /* These help keep track of whether we replaced all uses of the reg. */
9165 int replaced_last = 0;
9166 int store_is_first = 0;
9167
9168 for (insn = next_insn_in_loop (loop, loop->scan_start);
9169 insn != NULL_RTX;
9170 insn = next_insn_in_loop (loop, insn))
9171 {
9172 rtx set;
9173
9174 /* Only substitute within one extended basic block from the initializing
9175 insn. */
9176 if (GET_CODE (insn) == CODE_LABEL && init_insn)
9177 break;
9178
9179 if (! INSN_P (insn))
9180 continue;
9181
9182 /* Is this the initializing insn? */
9183 set = single_set (insn);
9184 if (set
9185 && GET_CODE (SET_DEST (set)) == REG
9186 && REGNO (SET_DEST (set)) == regno)
9187 {
9188 if (init_insn)
9189 abort ();
9190
9191 init_insn = insn;
9192 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
9193 store_is_first = 1;
9194 }
9195
9196 /* Only substitute after seeing the initializing insn. */
9197 if (init_insn && insn != init_insn)
9198 {
9199 struct note_reg_stored_arg arg;
9200
9201 replace_loop_regs (insn, reg_rtx, replacement);
9202 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
9203 replaced_last = 1;
9204
9205 /* Stop replacing when REPLACEMENT is modified. */
9206 arg.reg = replacement;
9207 arg.set_seen = 0;
9208 note_stores (PATTERN (insn), note_reg_stored, &arg);
9209 if (arg.set_seen)
9210 break;
9211 }
9212 }
9213 if (! init_insn)
9214 abort ();
9215 if (apply_change_group ())
9216 {
9217 if (loop_dump_stream)
9218 fprintf (loop_dump_stream, " Replaced reg %d", regno);
9219 if (store_is_first && replaced_last)
9220 {
9221 PUT_CODE (init_insn, NOTE);
9222 NOTE_LINE_NUMBER (init_insn) = NOTE_INSN_DELETED;
9223 if (loop_dump_stream)
9224 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
9225 INSN_UID (init_insn));
9226 }
9227 if (loop_dump_stream)
9228 fprintf (loop_dump_stream, ".\n");
9229 }
9230 }
9231
9232 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
9233 loop LOOP if the order of the sets of these registers can be
9234 swapped. There must be exactly one insn within the loop that sets
9235 this pseudo followed immediately by a move insn that sets
9236 REPLACEMENT with REGNO. */
9237 static void
9238 try_swap_copy_prop (loop, replacement, regno)
9239 const struct loop *loop;
9240 rtx replacement;
9241 unsigned int regno;
9242 {
9243 rtx insn;
9244 rtx set;
9245 unsigned int new_regno;
9246
9247 new_regno = REGNO (replacement);
9248
9249 for (insn = next_insn_in_loop (loop, loop->scan_start);
9250 insn != NULL_RTX;
9251 insn = next_insn_in_loop (loop, insn))
9252 {
9253 /* Search for the insn that copies REGNO to NEW_REGNO? */
9254 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
9255 && (set = single_set (insn))
9256 && GET_CODE (SET_DEST (set)) == REG
9257 && REGNO (SET_DEST (set)) == new_regno
9258 && GET_CODE (SET_SRC (set)) == REG
9259 && REGNO (SET_SRC (set)) == regno)
9260 break;
9261 }
9262
9263 if (insn != NULL_RTX)
9264 {
9265 rtx prev_insn;
9266 rtx prev_set;
9267
9268 /* Some DEF-USE info would come in handy here to make this
9269 function more general. For now, just check the previous insn
9270 which is the most likely candidate for setting REGNO. */
9271
9272 prev_insn = PREV_INSN (insn);
9273
9274 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
9275 && (prev_set = single_set (prev_insn))
9276 && GET_CODE (SET_DEST (prev_set)) == REG
9277 && REGNO (SET_DEST (prev_set)) == regno)
9278 {
9279 /* We have:
9280 (set (reg regno) (expr))
9281 (set (reg new_regno) (reg regno))
9282
9283 so try converting this to:
9284 (set (reg new_regno) (expr))
9285 (set (reg regno) (reg new_regno))
9286
9287 The former construct is often generated when a global
9288 variable used for an induction variable is shadowed by a
9289 register (NEW_REGNO). The latter construct improves the
9290 chances of GIV replacement and BIV elimination. */
9291
9292 validate_change (prev_insn, &SET_DEST (prev_set),
9293 replacement, 1);
9294 validate_change (insn, &SET_DEST (set),
9295 SET_SRC (set), 1);
9296 validate_change (insn, &SET_SRC (set),
9297 replacement, 1);
9298
9299 if (apply_change_group ())
9300 {
9301 if (loop_dump_stream)
9302 fprintf (loop_dump_stream,
9303 " Swapped set of reg %d at %d with reg %d at %d.\n",
9304 regno, INSN_UID (insn),
9305 new_regno, INSN_UID (prev_insn));
9306
9307 /* Update first use of REGNO. */
9308 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
9309 REGNO_FIRST_UID (regno) = INSN_UID (insn);
9310
9311 /* Now perform copy propagation to hopefully
9312 remove all uses of REGNO within the loop. */
9313 try_copy_prop (loop, replacement, regno);
9314 }
9315 }
9316 }
9317 }
9318
9319 /* Replace MEM with its associated pseudo register. This function is
9320 called from load_mems via for_each_rtx. DATA is actually a pointer
9321 to a structure describing the instruction currently being scanned
9322 and the MEM we are currently replacing. */
9323
9324 static int
9325 replace_loop_mem (mem, data)
9326 rtx *mem;
9327 void *data;
9328 {
9329 loop_replace_args *args = (loop_replace_args *) data;
9330 rtx m = *mem;
9331
9332 if (m == NULL_RTX)
9333 return 0;
9334
9335 switch (GET_CODE (m))
9336 {
9337 case MEM:
9338 break;
9339
9340 case CONST_DOUBLE:
9341 /* We're not interested in the MEM associated with a
9342 CONST_DOUBLE, so there's no need to traverse into one. */
9343 return -1;
9344
9345 default:
9346 /* This is not a MEM. */
9347 return 0;
9348 }
9349
9350 if (!rtx_equal_p (args->match, m))
9351 /* This is not the MEM we are currently replacing. */
9352 return 0;
9353
9354 /* Actually replace the MEM. */
9355 validate_change (args->insn, mem, args->replacement, 1);
9356
9357 return 0;
9358 }
9359
9360 static void
9361 replace_loop_mems (insn, mem, reg)
9362 rtx insn;
9363 rtx mem;
9364 rtx reg;
9365 {
9366 loop_replace_args args;
9367
9368 args.insn = insn;
9369 args.match = mem;
9370 args.replacement = reg;
9371
9372 for_each_rtx (&insn, replace_loop_mem, &args);
9373 }
9374
9375 /* Replace one register with another. Called through for_each_rtx; PX points
9376 to the rtx being scanned. DATA is actually a pointer to
9377 a structure of arguments. */
9378
9379 static int
9380 replace_loop_reg (px, data)
9381 rtx *px;
9382 void *data;
9383 {
9384 rtx x = *px;
9385 loop_replace_args *args = (loop_replace_args *) data;
9386
9387 if (x == NULL_RTX)
9388 return 0;
9389
9390 if (x == args->match)
9391 validate_change (args->insn, px, args->replacement, 1);
9392
9393 return 0;
9394 }
9395
9396 static void
9397 replace_loop_regs (insn, reg, replacement)
9398 rtx insn;
9399 rtx reg;
9400 rtx replacement;
9401 {
9402 loop_replace_args args;
9403
9404 args.insn = insn;
9405 args.match = reg;
9406 args.replacement = replacement;
9407
9408 for_each_rtx (&insn, replace_loop_reg, &args);
9409 }
9410
9411 /* Replace occurrences of the old exit label for the loop with the new
9412 one. DATA is an rtx_pair containing the old and new labels,
9413 respectively. */
9414
9415 static int
9416 replace_label (x, data)
9417 rtx *x;
9418 void *data;
9419 {
9420 rtx l = *x;
9421 rtx old_label = ((rtx_pair *) data)->r1;
9422 rtx new_label = ((rtx_pair *) data)->r2;
9423
9424 if (l == NULL_RTX)
9425 return 0;
9426
9427 if (GET_CODE (l) != LABEL_REF)
9428 return 0;
9429
9430 if (XEXP (l, 0) != old_label)
9431 return 0;
9432
9433 XEXP (l, 0) = new_label;
9434 ++LABEL_NUSES (new_label);
9435 --LABEL_NUSES (old_label);
9436
9437 return 0;
9438 }
9439 \f
9440 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
9441 (ignored in the interim). */
9442
9443 static rtx
9444 loop_insn_emit_after (loop, where_bb, where_insn, pattern)
9445 const struct loop *loop ATTRIBUTE_UNUSED;
9446 basic_block where_bb ATTRIBUTE_UNUSED;
9447 rtx where_insn;
9448 rtx pattern;
9449 {
9450 return emit_insn_after (pattern, where_insn);
9451 }
9452
9453
9454 /* If WHERE_INSN is non-zero emit insn for PATTERN before WHERE_INSN
9455 in basic block WHERE_BB (ignored in the interim) within the loop
9456 otherwise hoist PATTERN into the loop pre-header. */
9457
9458 static rtx
9459 loop_insn_emit_before (loop, where_bb, where_insn, pattern)
9460 const struct loop *loop;
9461 basic_block where_bb ATTRIBUTE_UNUSED;
9462 rtx where_insn;
9463 rtx pattern;
9464 {
9465 if (! where_insn)
9466 return loop_insn_hoist (loop, pattern);
9467 return emit_insn_before (pattern, where_insn);
9468 }
9469
9470
9471 /* Hoist insn for PATTERN into the loop pre-header. */
9472
9473 rtx
9474 loop_insn_hoist (loop, pattern)
9475 const struct loop *loop;
9476 rtx pattern;
9477 {
9478 return loop_insn_emit_before (loop, 0, loop->start, pattern);
9479 }
9480
9481
9482 /* Sink insn for PATTERN after the loop end. */
9483
9484 rtx
9485 loop_insn_sink (loop, pattern)
9486 const struct loop *loop;
9487 rtx pattern;
9488 {
9489 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
9490 }
9491
9492
9493 /* If the loop has multiple exits, emit insn for PATTERN before the
9494 loop to ensure that it will always be executed no matter how the
9495 loop exits. Otherwise, emit the insn for PATTERN after the loop,
9496 since this is slightly more efficient. */
9497
9498 static rtx
9499 loop_insn_sink_or_swim (loop, pattern)
9500 const struct loop *loop;
9501 rtx pattern;
9502 {
9503 if (loop->exit_count)
9504 return loop_insn_hoist (loop, pattern);
9505 else
9506 return loop_insn_sink (loop, pattern);
9507 }
9508 \f
9509 static void
9510 loop_biv_dump (v, file, verbose)
9511 const struct induction *v;
9512 FILE *file;
9513 int verbose;
9514 {
9515 if (! v || ! file)
9516 return;
9517
9518 fprintf (file,
9519 "Biv %d: insn %d",
9520 REGNO (v->dest_reg), INSN_UID (v->insn));
9521 fprintf (file, " const ");
9522 print_simple_rtl (file, v->add_val);
9523
9524 if (verbose && v->final_value)
9525 {
9526 fputc ('\n', file);
9527 fprintf (file, " final ");
9528 print_simple_rtl (file, v->final_value);
9529 }
9530
9531 fputc ('\n', file);
9532 }
9533
9534
9535 static void
9536 loop_giv_dump (v, file, verbose)
9537 const struct induction *v;
9538 FILE *file;
9539 int verbose;
9540 {
9541 if (! v || ! file)
9542 return;
9543
9544 if (v->giv_type == DEST_REG)
9545 fprintf (file, "Giv %d: insn %d",
9546 REGNO (v->dest_reg), INSN_UID (v->insn));
9547 else
9548 fprintf (file, "Dest address: insn %d",
9549 INSN_UID (v->insn));
9550
9551 fprintf (file, " src reg %d benefit %d",
9552 REGNO (v->src_reg), v->benefit);
9553 fprintf (file, " lifetime %d",
9554 v->lifetime);
9555
9556 if (v->replaceable)
9557 fprintf (file, " replaceable");
9558
9559 if (v->no_const_addval)
9560 fprintf (file, " ncav");
9561
9562 if (v->ext_dependant)
9563 {
9564 switch (GET_CODE (v->ext_dependant))
9565 {
9566 case SIGN_EXTEND:
9567 fprintf (file, " ext se");
9568 break;
9569 case ZERO_EXTEND:
9570 fprintf (file, " ext ze");
9571 break;
9572 case TRUNCATE:
9573 fprintf (file, " ext tr");
9574 break;
9575 default:
9576 abort ();
9577 }
9578 }
9579
9580 fputc ('\n', file);
9581 fprintf (file, " mult ");
9582 print_simple_rtl (file, v->mult_val);
9583
9584 fputc ('\n', file);
9585 fprintf (file, " add ");
9586 print_simple_rtl (file, v->add_val);
9587
9588 if (verbose && v->final_value)
9589 {
9590 fputc ('\n', file);
9591 fprintf (file, " final ");
9592 print_simple_rtl (file, v->final_value);
9593 }
9594
9595 fputc ('\n', file);
9596 }
9597
9598
9599 void
9600 debug_biv (v)
9601 const struct induction *v;
9602 {
9603 loop_biv_dump (v, stderr, 1);
9604 }
9605
9606
9607 void
9608 debug_giv (v)
9609 const struct induction *v;
9610 {
9611 loop_giv_dump (v, stderr, 1);
9612 }
9613
9614
9615 #define LOOP_BLOCK_NUM_1(INSN) \
9616 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
9617
9618 /* The notes do not have an assigned block, so look at the next insn. */
9619 #define LOOP_BLOCK_NUM(INSN) \
9620 ((INSN) ? (GET_CODE (INSN) == NOTE \
9621 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
9622 : LOOP_BLOCK_NUM_1 (INSN)) \
9623 : -1)
9624
9625 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
9626
9627 static void
9628 loop_dump_aux (loop, file, verbose)
9629 const struct loop *loop;
9630 FILE *file;
9631 int verbose ATTRIBUTE_UNUSED;
9632 {
9633 rtx label;
9634
9635 if (! loop || ! file)
9636 return;
9637
9638 /* Print diagnostics to compare our concept of a loop with
9639 what the loop notes say. */
9640 if (! PREV_INSN (loop->first->head)
9641 || GET_CODE (PREV_INSN (loop->first->head)) != NOTE
9642 || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
9643 != NOTE_INSN_LOOP_BEG)
9644 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
9645 INSN_UID (PREV_INSN (loop->first->head)));
9646 if (! NEXT_INSN (loop->last->end)
9647 || GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
9648 || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
9649 != NOTE_INSN_LOOP_END)
9650 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
9651 INSN_UID (NEXT_INSN (loop->last->end)));
9652
9653 if (loop->start)
9654 {
9655 fprintf (file,
9656 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
9657 LOOP_BLOCK_NUM (loop->start),
9658 LOOP_INSN_UID (loop->start),
9659 LOOP_BLOCK_NUM (loop->cont),
9660 LOOP_INSN_UID (loop->cont),
9661 LOOP_BLOCK_NUM (loop->cont),
9662 LOOP_INSN_UID (loop->cont),
9663 LOOP_BLOCK_NUM (loop->vtop),
9664 LOOP_INSN_UID (loop->vtop),
9665 LOOP_BLOCK_NUM (loop->end),
9666 LOOP_INSN_UID (loop->end));
9667 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
9668 LOOP_BLOCK_NUM (loop->top),
9669 LOOP_INSN_UID (loop->top),
9670 LOOP_BLOCK_NUM (loop->scan_start),
9671 LOOP_INSN_UID (loop->scan_start));
9672 fprintf (file, ";; exit_count %d", loop->exit_count);
9673 if (loop->exit_count)
9674 {
9675 fputs (", labels:", file);
9676 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
9677 {
9678 fprintf (file, " %d ",
9679 LOOP_INSN_UID (XEXP (label, 0)));
9680 }
9681 }
9682 fputs ("\n", file);
9683
9684 /* This can happen when a marked loop appears as two nested loops,
9685 say from while (a || b) {}. The inner loop won't match
9686 the loop markers but the outer one will. */
9687 if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
9688 fprintf (file, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
9689 }
9690 }
9691
9692 /* Call this function from the debugger to dump LOOP. */
9693
9694 void
9695 debug_loop (loop)
9696 const struct loop *loop;
9697 {
9698 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
9699 }
9700
9701 /* Call this function from the debugger to dump LOOPS. */
9702
9703 void
9704 debug_loops (loops)
9705 const struct loops *loops;
9706 {
9707 flow_loops_dump (loops, stderr, loop_dump_aux, 1);
9708 }