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