loop.c (is_power_of_2, [...]): Delete unused functions and declarations.
[gcc.git] / gcc / loop.c
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 88, 89, 91-6, 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
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 <stdio.h>
38 #include "config.h"
39 #include "rtl.h"
40 #include "obstack.h"
41 #include "expr.h"
42 #include "insn-config.h"
43 #include "insn-flags.h"
44 #include "regs.h"
45 #include "hard-reg-set.h"
46 #include "recog.h"
47 #include "flags.h"
48 #include "real.h"
49 #include "loop.h"
50 #include "except.h"
51
52 /* Vector mapping INSN_UIDs to luids.
53 The luids are like uids but increase monotonically always.
54 We use them to see whether a jump comes from outside a given loop. */
55
56 int *uid_luid;
57
58 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
59 number the insn is contained in. */
60
61 int *uid_loop_num;
62
63 /* 1 + largest uid of any insn. */
64
65 int max_uid_for_loop;
66
67 /* 1 + luid of last insn. */
68
69 static int max_luid;
70
71 /* Number of loops detected in current function. Used as index to the
72 next few tables. */
73
74 static int max_loop_num;
75
76 /* Indexed by loop number, contains the first and last insn of each loop. */
77
78 static rtx *loop_number_loop_starts, *loop_number_loop_ends;
79
80 /* For each loop, gives the containing loop number, -1 if none. */
81
82 int *loop_outer_loop;
83
84 #ifdef HAIFA
85 /* The main output of analyze_loop_iterations is placed here */
86
87 int *loop_can_insert_bct;
88
89 /* For each loop, determines whether some of its inner loops has used
90 count register */
91
92 int *loop_used_count_register;
93
94 /* For each loop, remember its unrolling factor (if at all).
95 contents of the array:
96 0/1: not unrolled.
97 -1: completely unrolled - no further instrumentation is needed.
98 >1: holds the exact amount of unrolling. */
99
100 int *loop_unroll_factor;
101 int *loop_unroll_iter;
102
103 /* loop parameters for arithmetic loops. These loops have a loop variable
104 which is initialized to loop_start_value, incremented in each iteration
105 by "loop_increment". At the end of the iteration the loop variable is
106 compared to the loop_comparison_value (using loop_comparison_code). */
107
108 rtx *loop_increment;
109 rtx *loop_comparison_value;
110 rtx *loop_start_value;
111 enum rtx_code *loop_comparison_code;
112
113 /* for debugging: selects sub-range of loops for which the bct optimization
114 is invoked. The numbering is per compilation-unit. */
115 int dbg_bct_min = -1;
116 int dbg_bct_max = -1;
117 #endif /* HAIFA */
118
119
120 /* Indexed by loop number, contains a nonzero value if the "loop" isn't
121 really a loop (an insn outside the loop branches into it). */
122
123 static char *loop_invalid;
124
125 /* Indexed by loop number, links together all LABEL_REFs which refer to
126 code labels outside the loop. Used by routines that need to know all
127 loop exits, such as final_biv_value and final_giv_value.
128
129 This does not include loop exits due to return instructions. This is
130 because all bivs and givs are pseudos, and hence must be dead after a
131 return, so the presense of a return does not affect any of the
132 optimizations that use this info. It is simpler to just not include return
133 instructions on this list. */
134
135 rtx *loop_number_exit_labels;
136
137 /* Indexed by loop number, counts the number of LABEL_REFs on
138 loop_number_exit_labels for this loop and all loops nested inside it. */
139
140 int *loop_number_exit_count;
141
142 /* Holds the number of loop iterations. It is zero if the number could not be
143 calculated. Must be unsigned since the number of iterations can
144 be as high as 2^wordsize-1. For loops with a wider iterator, this number
145 will will be zero if the number of loop iterations is too large for an
146 unsigned integer to hold. */
147
148 unsigned HOST_WIDE_INT loop_n_iterations;
149
150 /* Nonzero if there is a subroutine call in the current loop. */
151
152 static int loop_has_call;
153
154 /* Nonzero if there is a volatile memory reference in the current
155 loop. */
156
157 static int loop_has_volatile;
158
159 /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
160 current loop. A continue statement will generate a branch to
161 NEXT_INSN (loop_continue). */
162
163 static rtx loop_continue;
164
165 /* Indexed by register number, contains the number of times the reg
166 is set during the loop being scanned.
167 During code motion, a negative value indicates a reg that has been
168 made a candidate; in particular -2 means that it is an candidate that
169 we know is equal to a constant and -1 means that it is an candidate
170 not known equal to a constant.
171 After code motion, regs moved have 0 (which is accurate now)
172 while the failed candidates have the original number of times set.
173
174 Therefore, at all times, == 0 indicates an invariant register;
175 < 0 a conditionally invariant one. */
176
177 static int *n_times_set;
178
179 /* Original value of n_times_set; same except that this value
180 is not set negative for a reg whose sets have been made candidates
181 and not set to 0 for a reg that is moved. */
182
183 static int *n_times_used;
184
185 /* Index by register number, 1 indicates that the register
186 cannot be moved or strength reduced. */
187
188 static char *may_not_optimize;
189
190 /* Nonzero means reg N has already been moved out of one loop.
191 This reduces the desire to move it out of another. */
192
193 static char *moved_once;
194
195 /* Array of MEMs that are stored in this loop. If there are too many to fit
196 here, we just turn on unknown_address_altered. */
197
198 #define NUM_STORES 30
199 static rtx loop_store_mems[NUM_STORES];
200
201 /* Index of first available slot in above array. */
202 static int loop_store_mems_idx;
203
204 /* Nonzero if we don't know what MEMs were changed in the current loop.
205 This happens if the loop contains a call (in which case `loop_has_call'
206 will also be set) or if we store into more than NUM_STORES MEMs. */
207
208 static int unknown_address_altered;
209
210 /* Count of movable (i.e. invariant) instructions discovered in the loop. */
211 static int num_movables;
212
213 /* Count of memory write instructions discovered in the loop. */
214 static int num_mem_sets;
215
216 /* Number of loops contained within the current one, including itself. */
217 static int loops_enclosed;
218
219 /* Bound on pseudo register number before loop optimization.
220 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
221 int max_reg_before_loop;
222
223 /* This obstack is used in product_cheap_p to allocate its rtl. It
224 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
225 If we used the same obstack that it did, we would be deallocating
226 that array. */
227
228 static struct obstack temp_obstack;
229
230 /* This is where the pointer to the obstack being used for RTL is stored. */
231
232 extern struct obstack *rtl_obstack;
233
234 #define obstack_chunk_alloc xmalloc
235 #define obstack_chunk_free free
236
237 extern char *oballoc ();
238 \f
239 /* During the analysis of a loop, a chain of `struct movable's
240 is made to record all the movable insns found.
241 Then the entire chain can be scanned to decide which to move. */
242
243 struct movable
244 {
245 rtx insn; /* A movable insn */
246 rtx set_src; /* The expression this reg is set from. */
247 rtx set_dest; /* The destination of this SET. */
248 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
249 of any registers used within the LIBCALL. */
250 int consec; /* Number of consecutive following insns
251 that must be moved with this one. */
252 int regno; /* The register it sets */
253 short lifetime; /* lifetime of that register;
254 may be adjusted when matching movables
255 that load the same value are found. */
256 short savings; /* Number of insns we can move for this reg,
257 including other movables that force this
258 or match this one. */
259 unsigned int cond : 1; /* 1 if only conditionally movable */
260 unsigned int force : 1; /* 1 means MUST move this insn */
261 unsigned int global : 1; /* 1 means reg is live outside this loop */
262 /* If PARTIAL is 1, GLOBAL means something different:
263 that the reg is live outside the range from where it is set
264 to the following label. */
265 unsigned int done : 1; /* 1 inhibits further processing of this */
266
267 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
268 In particular, moving it does not make it
269 invariant. */
270 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
271 load SRC, rather than copying INSN. */
272 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
273 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
274 that we should avoid changing when clearing
275 the rest of the reg. */
276 struct movable *match; /* First entry for same value */
277 struct movable *forces; /* An insn that must be moved if this is */
278 struct movable *next;
279 };
280
281 FILE *loop_dump_stream;
282
283 /* Forward declarations. */
284
285 static void find_and_verify_loops ();
286 static void mark_loop_jump ();
287 static void prescan_loop ();
288 static int reg_in_basic_block_p ();
289 static int consec_sets_invariant_p ();
290 static rtx libcall_other_reg ();
291 static int labels_in_range_p ();
292 static void count_loop_regs_set ();
293 static void note_addr_stored ();
294 static int loop_reg_used_before_p ();
295 static void scan_loop ();
296 static void replace_call_address ();
297 static rtx skip_consec_insns ();
298 static int libcall_benefit ();
299 static void ignore_some_movables ();
300 static void force_movables ();
301 static void combine_movables ();
302 static int rtx_equal_for_loop_p ();
303 static void move_movables ();
304 static void strength_reduce ();
305 static int valid_initial_value_p ();
306 static void find_mem_givs ();
307 static void record_biv ();
308 static void check_final_value ();
309 static void record_giv ();
310 static void update_giv_derive ();
311 static int basic_induction_var ();
312 static rtx simplify_giv_expr ();
313 static int general_induction_var ();
314 static int consec_sets_giv ();
315 static int check_dbra_loop ();
316 static rtx express_from ();
317 static int combine_givs_p ();
318 static void combine_givs ();
319 static int product_cheap_p ();
320 static int maybe_eliminate_biv ();
321 static int maybe_eliminate_biv_1 ();
322 static int last_use_this_basic_block ();
323 static void record_initial ();
324 static void update_reg_last_use ();
325
326 #ifdef HAIFA
327 /* This is extern from unroll.c */
328 void iteration_info ();
329
330 /* Two main functions for implementing bct:
331 first - to be called before loop unrolling, and the second - after */
332 static void analyze_loop_iterations ();
333 static void insert_bct ();
334
335 /* Auxiliary function that inserts the bct pattern into the loop */
336 static void instrument_loop_bct ();
337
338 /* Indirect_jump_in_function is computed once per function. */
339 int indirect_jump_in_function = 0;
340 static int indirect_jump_in_function_p ();
341
342 int loop_number ();
343
344 /* Debugging functions. */
345 int fix_bct_param ();
346 static int check_bct_param ();
347 #endif /* HAIFA */
348
349 \f
350 /* Relative gain of eliminating various kinds of operations. */
351 int add_cost;
352 #if 0
353 int shift_cost;
354 int mult_cost;
355 #endif
356
357 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
358 copy the value of the strength reduced giv to its original register. */
359 int copy_cost;
360
361 void
362 init_loop ()
363 {
364 char *free_point = (char *) oballoc (1);
365 rtx reg = gen_rtx (REG, word_mode, LAST_VIRTUAL_REGISTER + 1);
366
367 add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
368
369 /* We multiply by 2 to reconcile the difference in scale between
370 these two ways of computing costs. Otherwise the cost of a copy
371 will be far less than the cost of an add. */
372
373 copy_cost = 2 * 2;
374
375 /* Free the objects we just allocated. */
376 obfree (free_point);
377
378 /* Initialize the obstack used for rtl in product_cheap_p. */
379 gcc_obstack_init (&temp_obstack);
380 }
381 \f
382 /* Entry point of this file. Perform loop optimization
383 on the current function. F is the first insn of the function
384 and DUMPFILE is a stream for output of a trace of actions taken
385 (or 0 if none should be output). */
386
387 void
388 loop_optimize (f, dumpfile)
389 /* f is the first instruction of a chain of insns for one function */
390 rtx f;
391 FILE *dumpfile;
392 {
393 register rtx insn;
394 register int i;
395 rtx last_insn;
396
397 loop_dump_stream = dumpfile;
398
399 init_recog_no_volatile ();
400 init_alias_analysis ();
401
402 max_reg_before_loop = max_reg_num ();
403
404 moved_once = (char *) alloca (max_reg_before_loop);
405 bzero (moved_once, max_reg_before_loop);
406
407 regs_may_share = 0;
408
409 /* Count the number of loops. */
410
411 max_loop_num = 0;
412 for (insn = f; insn; insn = NEXT_INSN (insn))
413 {
414 if (GET_CODE (insn) == NOTE
415 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
416 max_loop_num++;
417 }
418
419 /* Don't waste time if no loops. */
420 if (max_loop_num == 0)
421 return;
422
423 /* Get size to use for tables indexed by uids.
424 Leave some space for labels allocated by find_and_verify_loops. */
425 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
426
427 uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
428 uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
429
430 bzero ((char *) uid_luid, max_uid_for_loop * sizeof (int));
431 bzero ((char *) uid_loop_num, max_uid_for_loop * sizeof (int));
432
433 /* Allocate tables for recording each loop. We set each entry, so they need
434 not be zeroed. */
435 loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
436 loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
437 loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
438 loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
439 loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
440 loop_number_exit_count = (int *) alloca (max_loop_num * sizeof (int));
441
442 #ifdef HAIFA
443 /* Allocate for BCT optimization */
444 loop_can_insert_bct = (int *) alloca (max_loop_num * sizeof (int));
445 bzero ((char *) loop_can_insert_bct, max_loop_num * sizeof (int));
446
447 loop_used_count_register = (int *) alloca (max_loop_num * sizeof (int));
448 bzero ((char *) loop_used_count_register, max_loop_num * sizeof (int));
449
450 loop_unroll_factor = (int *) alloca (max_loop_num *sizeof (int));
451 bzero ((char *) loop_unroll_factor, max_loop_num * sizeof (int));
452
453 loop_unroll_iter = (int *) alloca (max_loop_num *sizeof (int));
454 bzero ((char *) loop_unroll_iter, max_loop_num * sizeof (int));
455
456 loop_increment = (rtx *) alloca (max_loop_num * sizeof (rtx));
457 loop_comparison_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
458 loop_start_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
459 bzero ((char *) loop_increment, max_loop_num * sizeof (rtx));
460 bzero ((char *) loop_comparison_value, max_loop_num * sizeof (rtx));
461 bzero ((char *) loop_start_value, max_loop_num * sizeof (rtx));
462
463 loop_comparison_code
464 = (enum rtx_code *) alloca (max_loop_num * sizeof (enum rtx_code));
465 bzero ((char *) loop_comparison_code, max_loop_num * sizeof (enum rtx_code));
466 #endif /* HAIFA */
467
468 /* Find and process each loop.
469 First, find them, and record them in order of their beginnings. */
470 find_and_verify_loops (f);
471
472 /* Now find all register lifetimes. This must be done after
473 find_and_verify_loops, because it might reorder the insns in the
474 function. */
475 reg_scan (f, max_reg_num (), 1);
476
477 /* See if we went too far. */
478 if (get_max_uid () > max_uid_for_loop)
479 abort ();
480
481 /* Compute the mapping from uids to luids.
482 LUIDs are numbers assigned to insns, like uids,
483 except that luids increase monotonically through the code.
484 Don't assign luids to line-number NOTEs, so that the distance in luids
485 between two insns is not affected by -g. */
486
487 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
488 {
489 last_insn = insn;
490 if (GET_CODE (insn) != NOTE
491 || NOTE_LINE_NUMBER (insn) <= 0)
492 uid_luid[INSN_UID (insn)] = ++i;
493 else
494 /* Give a line number note the same luid as preceding insn. */
495 uid_luid[INSN_UID (insn)] = i;
496 }
497
498 max_luid = i + 1;
499
500 /* Don't leave gaps in uid_luid for insns that have been
501 deleted. It is possible that the first or last insn
502 using some register has been deleted by cross-jumping.
503 Make sure that uid_luid for that former insn's uid
504 points to the general area where that insn used to be. */
505 for (i = 0; i < max_uid_for_loop; i++)
506 {
507 uid_luid[0] = uid_luid[i];
508 if (uid_luid[0] != 0)
509 break;
510 }
511 for (i = 0; i < max_uid_for_loop; i++)
512 if (uid_luid[i] == 0)
513 uid_luid[i] = uid_luid[i - 1];
514
515 /* Create a mapping from loops to BLOCK tree nodes. */
516 if (flag_unroll_loops && write_symbols != NO_DEBUG)
517 find_loop_tree_blocks ();
518
519 #ifdef HAIFA
520 /* determine if the function has indirect jump. If it does,
521 we cannot instrument loops in this function with bct */
522 indirect_jump_in_function = indirect_jump_in_function_p (f);
523 #endif /* HAIFA */
524
525 /* Now scan the loops, last ones first, since this means inner ones are done
526 before outer ones. */
527 for (i = max_loop_num-1; i >= 0; i--)
528 if (! loop_invalid[i] && loop_number_loop_ends[i])
529 scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
530 max_reg_num ());
531
532 /* If debugging and unrolling loops, we must replicate the tree nodes
533 corresponding to the blocks inside the loop, so that the original one
534 to one mapping will remain. */
535 if (flag_unroll_loops && write_symbols != NO_DEBUG)
536 unroll_block_trees ();
537 }
538 \f
539 /* Optimize one loop whose start is LOOP_START and end is END.
540 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
541 NOTE_INSN_LOOP_END. */
542
543 /* ??? Could also move memory writes out of loops if the destination address
544 is invariant, the source is invariant, the memory write is not volatile,
545 and if we can prove that no read inside the loop can read this address
546 before the write occurs. If there is a read of this address after the
547 write, then we can also mark the memory read as invariant. */
548
549 static void
550 scan_loop (loop_start, end, nregs)
551 rtx loop_start, end;
552 int nregs;
553 {
554 register int i;
555 register rtx p;
556 /* 1 if we are scanning insns that could be executed zero times. */
557 int maybe_never = 0;
558 /* 1 if we are scanning insns that might never be executed
559 due to a subroutine call which might exit before they are reached. */
560 int call_passed = 0;
561 /* For a rotated loop that is entered near the bottom,
562 this is the label at the top. Otherwise it is zero. */
563 rtx loop_top = 0;
564 /* Jump insn that enters the loop, or 0 if control drops in. */
565 rtx loop_entry_jump = 0;
566 /* Place in the loop where control enters. */
567 rtx scan_start;
568 /* Number of insns in the loop. */
569 int insn_count;
570 int in_libcall = 0;
571 int tem;
572 rtx temp;
573 /* The SET from an insn, if it is the only SET in the insn. */
574 rtx set, set1;
575 /* Chain describing insns movable in current loop. */
576 struct movable *movables = 0;
577 /* Last element in `movables' -- so we can add elements at the end. */
578 struct movable *last_movable = 0;
579 /* Ratio of extra register life span we can justify
580 for saving an instruction. More if loop doesn't call subroutines
581 since in that case saving an insn makes more difference
582 and more registers are available. */
583 int threshold;
584 /* If we have calls, contains the insn in which a register was used
585 if it was used exactly once; contains const0_rtx if it was used more
586 than once. */
587 rtx *reg_single_usage = 0;
588 /* Nonzero if we are scanning instructions in a sub-loop. */
589 int loop_depth = 0;
590
591 n_times_set = (int *) alloca (nregs * sizeof (int));
592 n_times_used = (int *) alloca (nregs * sizeof (int));
593 may_not_optimize = (char *) alloca (nregs);
594
595 /* Determine whether this loop starts with a jump down to a test at
596 the end. This will occur for a small number of loops with a test
597 that is too complex to duplicate in front of the loop.
598
599 We search for the first insn or label in the loop, skipping NOTEs.
600 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
601 (because we might have a loop executed only once that contains a
602 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
603 (in case we have a degenerate loop).
604
605 Note that if we mistakenly think that a loop is entered at the top
606 when, in fact, it is entered at the exit test, the only effect will be
607 slightly poorer optimization. Making the opposite error can generate
608 incorrect code. Since very few loops now start with a jump to the
609 exit test, the code here to detect that case is very conservative. */
610
611 for (p = NEXT_INSN (loop_start);
612 p != end
613 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
614 && (GET_CODE (p) != NOTE
615 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
616 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
617 p = NEXT_INSN (p))
618 ;
619
620 scan_start = p;
621
622 /* Set up variables describing this loop. */
623 prescan_loop (loop_start, end);
624 threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
625
626 /* If loop has a jump before the first label,
627 the true entry is the target of that jump.
628 Start scan from there.
629 But record in LOOP_TOP the place where the end-test jumps
630 back to so we can scan that after the end of the loop. */
631 if (GET_CODE (p) == JUMP_INSN)
632 {
633 loop_entry_jump = p;
634
635 /* Loop entry must be unconditional jump (and not a RETURN) */
636 if (simplejump_p (p)
637 && JUMP_LABEL (p) != 0
638 /* Check to see whether the jump actually
639 jumps out of the loop (meaning it's no loop).
640 This case can happen for things like
641 do {..} while (0). If this label was generated previously
642 by loop, we can't tell anything about it and have to reject
643 the loop. */
644 && INSN_UID (JUMP_LABEL (p)) < max_uid_for_loop
645 && INSN_LUID (JUMP_LABEL (p)) >= INSN_LUID (loop_start)
646 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (end))
647 {
648 loop_top = next_label (scan_start);
649 scan_start = JUMP_LABEL (p);
650 }
651 }
652
653 /* If SCAN_START was an insn created by loop, we don't know its luid
654 as required by loop_reg_used_before_p. So skip such loops. (This
655 test may never be true, but it's best to play it safe.)
656
657 Also, skip loops where we do not start scanning at a label. This
658 test also rejects loops starting with a JUMP_INSN that failed the
659 test above. */
660
661 if (INSN_UID (scan_start) >= max_uid_for_loop
662 || GET_CODE (scan_start) != CODE_LABEL)
663 {
664 if (loop_dump_stream)
665 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
666 INSN_UID (loop_start), INSN_UID (end));
667 return;
668 }
669
670 /* Count number of times each reg is set during this loop.
671 Set may_not_optimize[I] if it is not safe to move out
672 the setting of register I. If this loop has calls, set
673 reg_single_usage[I]. */
674
675 bzero ((char *) n_times_set, nregs * sizeof (int));
676 bzero (may_not_optimize, nregs);
677
678 if (loop_has_call)
679 {
680 reg_single_usage = (rtx *) alloca (nregs * sizeof (rtx));
681 bzero ((char *) reg_single_usage, nregs * sizeof (rtx));
682 }
683
684 count_loop_regs_set (loop_top ? loop_top : loop_start, end,
685 may_not_optimize, reg_single_usage, &insn_count, nregs);
686
687 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
688 may_not_optimize[i] = 1, n_times_set[i] = 1;
689 bcopy ((char *) n_times_set, (char *) n_times_used, nregs * sizeof (int));
690
691 if (loop_dump_stream)
692 {
693 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
694 INSN_UID (loop_start), INSN_UID (end), insn_count);
695 if (loop_continue)
696 fprintf (loop_dump_stream, "Continue at insn %d.\n",
697 INSN_UID (loop_continue));
698 }
699
700 /* Scan through the loop finding insns that are safe to move.
701 Set n_times_set negative for the reg being set, so that
702 this reg will be considered invariant for subsequent insns.
703 We consider whether subsequent insns use the reg
704 in deciding whether it is worth actually moving.
705
706 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
707 and therefore it is possible that the insns we are scanning
708 would never be executed. At such times, we must make sure
709 that it is safe to execute the insn once instead of zero times.
710 When MAYBE_NEVER is 0, all insns will be executed at least once
711 so that is not a problem. */
712
713 p = scan_start;
714 while (1)
715 {
716 p = NEXT_INSN (p);
717 /* At end of a straight-in loop, we are done.
718 At end of a loop entered at the bottom, scan the top. */
719 if (p == scan_start)
720 break;
721 if (p == end)
722 {
723 if (loop_top != 0)
724 p = loop_top;
725 else
726 break;
727 if (p == scan_start)
728 break;
729 }
730
731 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
732 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
733 in_libcall = 1;
734 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
735 && find_reg_note (p, REG_RETVAL, NULL_RTX))
736 in_libcall = 0;
737
738 if (GET_CODE (p) == INSN
739 && (set = single_set (p))
740 && GET_CODE (SET_DEST (set)) == REG
741 && ! may_not_optimize[REGNO (SET_DEST (set))])
742 {
743 int tem1 = 0;
744 int tem2 = 0;
745 int move_insn = 0;
746 rtx src = SET_SRC (set);
747 rtx dependencies = 0;
748
749 /* Figure out what to use as a source of this insn. If a REG_EQUIV
750 note is given or if a REG_EQUAL note with a constant operand is
751 specified, use it as the source and mark that we should move
752 this insn by calling emit_move_insn rather that duplicating the
753 insn.
754
755 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
756 is present. */
757 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
758 if (temp)
759 src = XEXP (temp, 0), move_insn = 1;
760 else
761 {
762 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
763 if (temp && CONSTANT_P (XEXP (temp, 0)))
764 src = XEXP (temp, 0), move_insn = 1;
765 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
766 {
767 src = XEXP (temp, 0);
768 /* A libcall block can use regs that don't appear in
769 the equivalent expression. To move the libcall,
770 we must move those regs too. */
771 dependencies = libcall_other_reg (p, src);
772 }
773 }
774
775 /* Don't try to optimize a register that was made
776 by loop-optimization for an inner loop.
777 We don't know its life-span, so we can't compute the benefit. */
778 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
779 ;
780 /* In order to move a register, we need to have one of three cases:
781 (1) it is used only in the same basic block as the set
782 (2) it is not a user variable and it is not used in the
783 exit test (this can cause the variable to be used
784 before it is set just like a user-variable).
785 (3) the set is guaranteed to be executed once the loop starts,
786 and the reg is not used until after that. */
787 else if (! ((! maybe_never
788 && ! loop_reg_used_before_p (set, p, loop_start,
789 scan_start, end))
790 || (! REG_USERVAR_P (SET_DEST (set))
791 && ! REG_LOOP_TEST_P (SET_DEST (set)))
792 || reg_in_basic_block_p (p, SET_DEST (set))))
793 ;
794 else if ((tem = invariant_p (src))
795 && (dependencies == 0
796 || (tem2 = invariant_p (dependencies)) != 0)
797 && (n_times_set[REGNO (SET_DEST (set))] == 1
798 || (tem1
799 = consec_sets_invariant_p (SET_DEST (set),
800 n_times_set[REGNO (SET_DEST (set))],
801 p)))
802 /* If the insn can cause a trap (such as divide by zero),
803 can't move it unless it's guaranteed to be executed
804 once loop is entered. Even a function call might
805 prevent the trap insn from being reached
806 (since it might exit!) */
807 && ! ((maybe_never || call_passed)
808 && may_trap_p (src)))
809 {
810 register struct movable *m;
811 register int regno = REGNO (SET_DEST (set));
812
813 /* A potential lossage is where we have a case where two insns
814 can be combined as long as they are both in the loop, but
815 we move one of them outside the loop. For large loops,
816 this can lose. The most common case of this is the address
817 of a function being called.
818
819 Therefore, if this register is marked as being used exactly
820 once if we are in a loop with calls (a "large loop"), see if
821 we can replace the usage of this register with the source
822 of this SET. If we can, delete this insn.
823
824 Don't do this if P has a REG_RETVAL note or if we have
825 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
826
827 if (reg_single_usage && reg_single_usage[regno] != 0
828 && reg_single_usage[regno] != const0_rtx
829 && REGNO_FIRST_UID (regno) == INSN_UID (p)
830 && (REGNO_LAST_UID (regno)
831 == INSN_UID (reg_single_usage[regno]))
832 && n_times_set[REGNO (SET_DEST (set))] == 1
833 && ! side_effects_p (SET_SRC (set))
834 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
835 #ifdef SMALL_REGISTER_CLASSES
836 && ! (SMALL_REGISTER_CLASSES
837 && GET_CODE (SET_SRC (set)) == REG
838 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)
839 #endif
840 /* This test is not redundant; SET_SRC (set) might be
841 a call-clobbered register and the life of REGNO
842 might span a call. */
843 && ! modified_between_p (SET_SRC (set), p,
844 reg_single_usage[regno])
845 && no_labels_between_p (p, reg_single_usage[regno])
846 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
847 reg_single_usage[regno]))
848 {
849 /* Replace any usage in a REG_EQUAL note. Must copy the
850 new source, so that we don't get rtx sharing between the
851 SET_SOURCE and REG_NOTES of insn p. */
852 REG_NOTES (reg_single_usage[regno])
853 = replace_rtx (REG_NOTES (reg_single_usage[regno]),
854 SET_DEST (set), copy_rtx (SET_SRC (set)));
855
856 PUT_CODE (p, NOTE);
857 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
858 NOTE_SOURCE_FILE (p) = 0;
859 n_times_set[regno] = 0;
860 continue;
861 }
862
863 m = (struct movable *) alloca (sizeof (struct movable));
864 m->next = 0;
865 m->insn = p;
866 m->set_src = src;
867 m->dependencies = dependencies;
868 m->set_dest = SET_DEST (set);
869 m->force = 0;
870 m->consec = n_times_set[REGNO (SET_DEST (set))] - 1;
871 m->done = 0;
872 m->forces = 0;
873 m->partial = 0;
874 m->move_insn = move_insn;
875 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
876 m->savemode = VOIDmode;
877 m->regno = regno;
878 /* Set M->cond if either invariant_p or consec_sets_invariant_p
879 returned 2 (only conditionally invariant). */
880 m->cond = ((tem | tem1 | tem2) > 1);
881 m->global = (uid_luid[REGNO_LAST_UID (regno)] > INSN_LUID (end)
882 || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
883 m->match = 0;
884 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
885 - uid_luid[REGNO_FIRST_UID (regno)]);
886 m->savings = n_times_used[regno];
887 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
888 m->savings += libcall_benefit (p);
889 n_times_set[regno] = move_insn ? -2 : -1;
890 /* Add M to the end of the chain MOVABLES. */
891 if (movables == 0)
892 movables = m;
893 else
894 last_movable->next = m;
895 last_movable = m;
896
897 if (m->consec > 0)
898 {
899 /* Skip this insn, not checking REG_LIBCALL notes. */
900 p = next_nonnote_insn (p);
901 /* Skip the consecutive insns, if there are any. */
902 p = skip_consec_insns (p, m->consec);
903 /* Back up to the last insn of the consecutive group. */
904 p = prev_nonnote_insn (p);
905
906 /* We must now reset m->move_insn, m->is_equiv, and possibly
907 m->set_src to correspond to the effects of all the
908 insns. */
909 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
910 if (temp)
911 m->set_src = XEXP (temp, 0), m->move_insn = 1;
912 else
913 {
914 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
915 if (temp && CONSTANT_P (XEXP (temp, 0)))
916 m->set_src = XEXP (temp, 0), m->move_insn = 1;
917 else
918 m->move_insn = 0;
919
920 }
921 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
922 }
923 }
924 /* If this register is always set within a STRICT_LOW_PART
925 or set to zero, then its high bytes are constant.
926 So clear them outside the loop and within the loop
927 just load the low bytes.
928 We must check that the machine has an instruction to do so.
929 Also, if the value loaded into the register
930 depends on the same register, this cannot be done. */
931 else if (SET_SRC (set) == const0_rtx
932 && GET_CODE (NEXT_INSN (p)) == INSN
933 && (set1 = single_set (NEXT_INSN (p)))
934 && GET_CODE (set1) == SET
935 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
936 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
937 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
938 == SET_DEST (set))
939 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
940 {
941 register int regno = REGNO (SET_DEST (set));
942 if (n_times_set[regno] == 2)
943 {
944 register struct movable *m;
945 m = (struct movable *) alloca (sizeof (struct movable));
946 m->next = 0;
947 m->insn = p;
948 m->set_dest = SET_DEST (set);
949 m->dependencies = 0;
950 m->force = 0;
951 m->consec = 0;
952 m->done = 0;
953 m->forces = 0;
954 m->move_insn = 0;
955 m->partial = 1;
956 /* If the insn may not be executed on some cycles,
957 we can't clear the whole reg; clear just high part.
958 Not even if the reg is used only within this loop.
959 Consider this:
960 while (1)
961 while (s != t) {
962 if (foo ()) x = *s;
963 use (x);
964 }
965 Clearing x before the inner loop could clobber a value
966 being saved from the last time around the outer loop.
967 However, if the reg is not used outside this loop
968 and all uses of the register are in the same
969 basic block as the store, there is no problem.
970
971 If this insn was made by loop, we don't know its
972 INSN_LUID and hence must make a conservative
973 assumption. */
974 m->global = (INSN_UID (p) >= max_uid_for_loop
975 || (uid_luid[REGNO_LAST_UID (regno)]
976 > INSN_LUID (end))
977 || (uid_luid[REGNO_FIRST_UID (regno)]
978 < INSN_LUID (p))
979 || (labels_in_range_p
980 (p, uid_luid[REGNO_FIRST_UID (regno)])));
981 if (maybe_never && m->global)
982 m->savemode = GET_MODE (SET_SRC (set1));
983 else
984 m->savemode = VOIDmode;
985 m->regno = regno;
986 m->cond = 0;
987 m->match = 0;
988 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
989 - uid_luid[REGNO_FIRST_UID (regno)]);
990 m->savings = 1;
991 n_times_set[regno] = -1;
992 /* Add M to the end of the chain MOVABLES. */
993 if (movables == 0)
994 movables = m;
995 else
996 last_movable->next = m;
997 last_movable = m;
998 }
999 }
1000 }
1001 /* Past a call insn, we get to insns which might not be executed
1002 because the call might exit. This matters for insns that trap.
1003 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
1004 so they don't count. */
1005 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
1006 call_passed = 1;
1007 /* Past a label or a jump, we get to insns for which we
1008 can't count on whether or how many times they will be
1009 executed during each iteration. Therefore, we can
1010 only move out sets of trivial variables
1011 (those not used after the loop). */
1012 /* Similar code appears twice in strength_reduce. */
1013 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1014 /* If we enter the loop in the middle, and scan around to the
1015 beginning, don't set maybe_never for that. This must be an
1016 unconditional jump, otherwise the code at the top of the
1017 loop might never be executed. Unconditional jumps are
1018 followed a by barrier then loop end. */
1019 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
1020 && NEXT_INSN (NEXT_INSN (p)) == end
1021 && simplejump_p (p)))
1022 maybe_never = 1;
1023 else if (GET_CODE (p) == NOTE)
1024 {
1025 /* At the virtual top of a converted loop, insns are again known to
1026 be executed: logically, the loop begins here even though the exit
1027 code has been duplicated. */
1028 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1029 maybe_never = call_passed = 0;
1030 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1031 loop_depth++;
1032 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1033 loop_depth--;
1034 }
1035 }
1036
1037 /* If one movable subsumes another, ignore that other. */
1038
1039 ignore_some_movables (movables);
1040
1041 /* For each movable insn, see if the reg that it loads
1042 leads when it dies right into another conditionally movable insn.
1043 If so, record that the second insn "forces" the first one,
1044 since the second can be moved only if the first is. */
1045
1046 force_movables (movables);
1047
1048 /* See if there are multiple movable insns that load the same value.
1049 If there are, make all but the first point at the first one
1050 through the `match' field, and add the priorities of them
1051 all together as the priority of the first. */
1052
1053 combine_movables (movables, nregs);
1054
1055 /* Now consider each movable insn to decide whether it is worth moving.
1056 Store 0 in n_times_set for each reg that is moved. */
1057
1058 move_movables (movables, threshold,
1059 insn_count, loop_start, end, nregs);
1060
1061 /* Now candidates that still are negative are those not moved.
1062 Change n_times_set to indicate that those are not actually invariant. */
1063 for (i = 0; i < nregs; i++)
1064 if (n_times_set[i] < 0)
1065 n_times_set[i] = n_times_used[i];
1066
1067 if (flag_strength_reduce)
1068 strength_reduce (scan_start, end, loop_top,
1069 insn_count, loop_start, end);
1070 }
1071 \f
1072 /* Add elements to *OUTPUT to record all the pseudo-regs
1073 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1074
1075 void
1076 record_excess_regs (in_this, not_in_this, output)
1077 rtx in_this, not_in_this;
1078 rtx *output;
1079 {
1080 enum rtx_code code;
1081 char *fmt;
1082 int i;
1083
1084 code = GET_CODE (in_this);
1085
1086 switch (code)
1087 {
1088 case PC:
1089 case CC0:
1090 case CONST_INT:
1091 case CONST_DOUBLE:
1092 case CONST:
1093 case SYMBOL_REF:
1094 case LABEL_REF:
1095 return;
1096
1097 case REG:
1098 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1099 && ! reg_mentioned_p (in_this, not_in_this))
1100 *output = gen_rtx (EXPR_LIST, VOIDmode, in_this, *output);
1101 return;
1102 }
1103
1104 fmt = GET_RTX_FORMAT (code);
1105 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1106 {
1107 int j;
1108
1109 switch (fmt[i])
1110 {
1111 case 'E':
1112 for (j = 0; j < XVECLEN (in_this, i); j++)
1113 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1114 break;
1115
1116 case 'e':
1117 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1118 break;
1119 }
1120 }
1121 }
1122 \f
1123 /* Check what regs are referred to in the libcall block ending with INSN,
1124 aside from those mentioned in the equivalent value.
1125 If there are none, return 0.
1126 If there are one or more, return an EXPR_LIST containing all of them. */
1127
1128 static rtx
1129 libcall_other_reg (insn, equiv)
1130 rtx insn, equiv;
1131 {
1132 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1133 rtx p = XEXP (note, 0);
1134 rtx output = 0;
1135
1136 /* First, find all the regs used in the libcall block
1137 that are not mentioned as inputs to the result. */
1138
1139 while (p != insn)
1140 {
1141 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1142 || GET_CODE (p) == CALL_INSN)
1143 record_excess_regs (PATTERN (p), equiv, &output);
1144 p = NEXT_INSN (p);
1145 }
1146
1147 return output;
1148 }
1149 \f
1150 /* Return 1 if all uses of REG
1151 are between INSN and the end of the basic block. */
1152
1153 static int
1154 reg_in_basic_block_p (insn, reg)
1155 rtx insn, reg;
1156 {
1157 int regno = REGNO (reg);
1158 rtx p;
1159
1160 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1161 return 0;
1162
1163 /* Search this basic block for the already recorded last use of the reg. */
1164 for (p = insn; p; p = NEXT_INSN (p))
1165 {
1166 switch (GET_CODE (p))
1167 {
1168 case NOTE:
1169 break;
1170
1171 case INSN:
1172 case CALL_INSN:
1173 /* Ordinary insn: if this is the last use, we win. */
1174 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1175 return 1;
1176 break;
1177
1178 case JUMP_INSN:
1179 /* Jump insn: if this is the last use, we win. */
1180 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1181 return 1;
1182 /* Otherwise, it's the end of the basic block, so we lose. */
1183 return 0;
1184
1185 case CODE_LABEL:
1186 case BARRIER:
1187 /* It's the end of the basic block, so we lose. */
1188 return 0;
1189 }
1190 }
1191
1192 /* The "last use" doesn't follow the "first use"?? */
1193 abort ();
1194 }
1195 \f
1196 /* Compute the benefit of eliminating the insns in the block whose
1197 last insn is LAST. This may be a group of insns used to compute a
1198 value directly or can contain a library call. */
1199
1200 static int
1201 libcall_benefit (last)
1202 rtx last;
1203 {
1204 rtx insn;
1205 int benefit = 0;
1206
1207 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1208 insn != last; insn = NEXT_INSN (insn))
1209 {
1210 if (GET_CODE (insn) == CALL_INSN)
1211 benefit += 10; /* Assume at least this many insns in a library
1212 routine. */
1213 else if (GET_CODE (insn) == INSN
1214 && GET_CODE (PATTERN (insn)) != USE
1215 && GET_CODE (PATTERN (insn)) != CLOBBER)
1216 benefit++;
1217 }
1218
1219 return benefit;
1220 }
1221 \f
1222 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1223
1224 static rtx
1225 skip_consec_insns (insn, count)
1226 rtx insn;
1227 int count;
1228 {
1229 for (; count > 0; count--)
1230 {
1231 rtx temp;
1232
1233 /* If first insn of libcall sequence, skip to end. */
1234 /* Do this at start of loop, since INSN is guaranteed to
1235 be an insn here. */
1236 if (GET_CODE (insn) != NOTE
1237 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1238 insn = XEXP (temp, 0);
1239
1240 do insn = NEXT_INSN (insn);
1241 while (GET_CODE (insn) == NOTE);
1242 }
1243
1244 return insn;
1245 }
1246
1247 /* Ignore any movable whose insn falls within a libcall
1248 which is part of another movable.
1249 We make use of the fact that the movable for the libcall value
1250 was made later and so appears later on the chain. */
1251
1252 static void
1253 ignore_some_movables (movables)
1254 struct movable *movables;
1255 {
1256 register struct movable *m, *m1;
1257
1258 for (m = movables; m; m = m->next)
1259 {
1260 /* Is this a movable for the value of a libcall? */
1261 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1262 if (note)
1263 {
1264 rtx insn;
1265 /* Check for earlier movables inside that range,
1266 and mark them invalid. We cannot use LUIDs here because
1267 insns created by loop.c for prior loops don't have LUIDs.
1268 Rather than reject all such insns from movables, we just
1269 explicitly check each insn in the libcall (since invariant
1270 libcalls aren't that common). */
1271 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1272 for (m1 = movables; m1 != m; m1 = m1->next)
1273 if (m1->insn == insn)
1274 m1->done = 1;
1275 }
1276 }
1277 }
1278
1279 /* For each movable insn, see if the reg that it loads
1280 leads when it dies right into another conditionally movable insn.
1281 If so, record that the second insn "forces" the first one,
1282 since the second can be moved only if the first is. */
1283
1284 static void
1285 force_movables (movables)
1286 struct movable *movables;
1287 {
1288 register struct movable *m, *m1;
1289 for (m1 = movables; m1; m1 = m1->next)
1290 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1291 if (!m1->partial && !m1->done)
1292 {
1293 int regno = m1->regno;
1294 for (m = m1->next; m; m = m->next)
1295 /* ??? Could this be a bug? What if CSE caused the
1296 register of M1 to be used after this insn?
1297 Since CSE does not update regno_last_uid,
1298 this insn M->insn might not be where it dies.
1299 But very likely this doesn't matter; what matters is
1300 that M's reg is computed from M1's reg. */
1301 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1302 && !m->done)
1303 break;
1304 if (m != 0 && m->set_src == m1->set_dest
1305 /* If m->consec, m->set_src isn't valid. */
1306 && m->consec == 0)
1307 m = 0;
1308
1309 /* Increase the priority of the moving the first insn
1310 since it permits the second to be moved as well. */
1311 if (m != 0)
1312 {
1313 m->forces = m1;
1314 m1->lifetime += m->lifetime;
1315 m1->savings += m1->savings;
1316 }
1317 }
1318 }
1319 \f
1320 /* Find invariant expressions that are equal and can be combined into
1321 one register. */
1322
1323 static void
1324 combine_movables (movables, nregs)
1325 struct movable *movables;
1326 int nregs;
1327 {
1328 register struct movable *m;
1329 char *matched_regs = (char *) alloca (nregs);
1330 enum machine_mode mode;
1331
1332 /* Regs that are set more than once are not allowed to match
1333 or be matched. I'm no longer sure why not. */
1334 /* Perhaps testing m->consec_sets would be more appropriate here? */
1335
1336 for (m = movables; m; m = m->next)
1337 if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
1338 {
1339 register struct movable *m1;
1340 int regno = m->regno;
1341
1342 bzero (matched_regs, nregs);
1343 matched_regs[regno] = 1;
1344
1345 for (m1 = movables; m1; m1 = m1->next)
1346 if (m != m1 && m1->match == 0 && n_times_used[m1->regno] == 1
1347 /* A reg used outside the loop mustn't be eliminated. */
1348 && !m1->global
1349 /* A reg used for zero-extending mustn't be eliminated. */
1350 && !m1->partial
1351 && (matched_regs[m1->regno]
1352 ||
1353 (
1354 /* Can combine regs with different modes loaded from the
1355 same constant only if the modes are the same or
1356 if both are integer modes with M wider or the same
1357 width as M1. The check for integer is redundant, but
1358 safe, since the only case of differing destination
1359 modes with equal sources is when both sources are
1360 VOIDmode, i.e., CONST_INT. */
1361 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1362 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1363 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1364 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1365 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1366 /* See if the source of M1 says it matches M. */
1367 && ((GET_CODE (m1->set_src) == REG
1368 && matched_regs[REGNO (m1->set_src)])
1369 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1370 movables))))
1371 && ((m->dependencies == m1->dependencies)
1372 || rtx_equal_p (m->dependencies, m1->dependencies)))
1373 {
1374 m->lifetime += m1->lifetime;
1375 m->savings += m1->savings;
1376 m1->done = 1;
1377 m1->match = m;
1378 matched_regs[m1->regno] = 1;
1379 }
1380 }
1381
1382 /* Now combine the regs used for zero-extension.
1383 This can be done for those not marked `global'
1384 provided their lives don't overlap. */
1385
1386 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1387 mode = GET_MODE_WIDER_MODE (mode))
1388 {
1389 register struct movable *m0 = 0;
1390
1391 /* Combine all the registers for extension from mode MODE.
1392 Don't combine any that are used outside this loop. */
1393 for (m = movables; m; m = m->next)
1394 if (m->partial && ! m->global
1395 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1396 {
1397 register struct movable *m1;
1398 int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1399 int last = uid_luid[REGNO_LAST_UID (m->regno)];
1400
1401 if (m0 == 0)
1402 {
1403 /* First one: don't check for overlap, just record it. */
1404 m0 = m;
1405 continue;
1406 }
1407
1408 /* Make sure they extend to the same mode.
1409 (Almost always true.) */
1410 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1411 continue;
1412
1413 /* We already have one: check for overlap with those
1414 already combined together. */
1415 for (m1 = movables; m1 != m; m1 = m1->next)
1416 if (m1 == m0 || (m1->partial && m1->match == m0))
1417 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1418 || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
1419 goto overlap;
1420
1421 /* No overlap: we can combine this with the others. */
1422 m0->lifetime += m->lifetime;
1423 m0->savings += m->savings;
1424 m->done = 1;
1425 m->match = m0;
1426
1427 overlap: ;
1428 }
1429 }
1430 }
1431 \f
1432 /* Return 1 if regs X and Y will become the same if moved. */
1433
1434 static int
1435 regs_match_p (x, y, movables)
1436 rtx x, y;
1437 struct movable *movables;
1438 {
1439 int xn = REGNO (x);
1440 int yn = REGNO (y);
1441 struct movable *mx, *my;
1442
1443 for (mx = movables; mx; mx = mx->next)
1444 if (mx->regno == xn)
1445 break;
1446
1447 for (my = movables; my; my = my->next)
1448 if (my->regno == yn)
1449 break;
1450
1451 return (mx && my
1452 && ((mx->match == my->match && mx->match != 0)
1453 || mx->match == my
1454 || mx == my->match));
1455 }
1456
1457 /* Return 1 if X and Y are identical-looking rtx's.
1458 This is the Lisp function EQUAL for rtx arguments.
1459
1460 If two registers are matching movables or a movable register and an
1461 equivalent constant, consider them equal. */
1462
1463 static int
1464 rtx_equal_for_loop_p (x, y, movables)
1465 rtx x, y;
1466 struct movable *movables;
1467 {
1468 register int i;
1469 register int j;
1470 register struct movable *m;
1471 register enum rtx_code code;
1472 register char *fmt;
1473
1474 if (x == y)
1475 return 1;
1476 if (x == 0 || y == 0)
1477 return 0;
1478
1479 code = GET_CODE (x);
1480
1481 /* If we have a register and a constant, they may sometimes be
1482 equal. */
1483 if (GET_CODE (x) == REG && n_times_set[REGNO (x)] == -2
1484 && CONSTANT_P (y))
1485 for (m = movables; m; m = m->next)
1486 if (m->move_insn && m->regno == REGNO (x)
1487 && rtx_equal_p (m->set_src, y))
1488 return 1;
1489
1490 else if (GET_CODE (y) == REG && n_times_set[REGNO (y)] == -2
1491 && CONSTANT_P (x))
1492 for (m = movables; m; m = m->next)
1493 if (m->move_insn && m->regno == REGNO (y)
1494 && rtx_equal_p (m->set_src, x))
1495 return 1;
1496
1497 /* Otherwise, rtx's of different codes cannot be equal. */
1498 if (code != GET_CODE (y))
1499 return 0;
1500
1501 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1502 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1503
1504 if (GET_MODE (x) != GET_MODE (y))
1505 return 0;
1506
1507 /* These three types of rtx's can be compared nonrecursively. */
1508 if (code == REG)
1509 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1510
1511 if (code == LABEL_REF)
1512 return XEXP (x, 0) == XEXP (y, 0);
1513 if (code == SYMBOL_REF)
1514 return XSTR (x, 0) == XSTR (y, 0);
1515
1516 /* Compare the elements. If any pair of corresponding elements
1517 fail to match, return 0 for the whole things. */
1518
1519 fmt = GET_RTX_FORMAT (code);
1520 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1521 {
1522 switch (fmt[i])
1523 {
1524 case 'w':
1525 if (XWINT (x, i) != XWINT (y, i))
1526 return 0;
1527 break;
1528
1529 case 'i':
1530 if (XINT (x, i) != XINT (y, i))
1531 return 0;
1532 break;
1533
1534 case 'E':
1535 /* Two vectors must have the same length. */
1536 if (XVECLEN (x, i) != XVECLEN (y, i))
1537 return 0;
1538
1539 /* And the corresponding elements must match. */
1540 for (j = 0; j < XVECLEN (x, i); j++)
1541 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1542 return 0;
1543 break;
1544
1545 case 'e':
1546 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1547 return 0;
1548 break;
1549
1550 case 's':
1551 if (strcmp (XSTR (x, i), XSTR (y, i)))
1552 return 0;
1553 break;
1554
1555 case 'u':
1556 /* These are just backpointers, so they don't matter. */
1557 break;
1558
1559 case '0':
1560 break;
1561
1562 /* It is believed that rtx's at this level will never
1563 contain anything but integers and other rtx's,
1564 except for within LABEL_REFs and SYMBOL_REFs. */
1565 default:
1566 abort ();
1567 }
1568 }
1569 return 1;
1570 }
1571 \f
1572 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1573 insns in INSNS which use thet reference. */
1574
1575 static void
1576 add_label_notes (x, insns)
1577 rtx x;
1578 rtx insns;
1579 {
1580 enum rtx_code code = GET_CODE (x);
1581 int i, j;
1582 char *fmt;
1583 rtx insn;
1584
1585 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1586 {
1587 rtx next = next_real_insn (XEXP (x, 0));
1588
1589 /* Don't record labels that refer to dispatch tables.
1590 This is not necessary, since the tablejump references the same label.
1591 And if we did record them, flow.c would make worse code. */
1592 if (next == 0
1593 || ! (GET_CODE (next) == JUMP_INSN
1594 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1595 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
1596 {
1597 for (insn = insns; insn; insn = NEXT_INSN (insn))
1598 if (reg_mentioned_p (XEXP (x, 0), insn))
1599 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, XEXP (x, 0),
1600 REG_NOTES (insn));
1601 }
1602 return;
1603 }
1604
1605 fmt = GET_RTX_FORMAT (code);
1606 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1607 {
1608 if (fmt[i] == 'e')
1609 add_label_notes (XEXP (x, i), insns);
1610 else if (fmt[i] == 'E')
1611 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1612 add_label_notes (XVECEXP (x, i, j), insns);
1613 }
1614 }
1615 \f
1616 /* Scan MOVABLES, and move the insns that deserve to be moved.
1617 If two matching movables are combined, replace one reg with the
1618 other throughout. */
1619
1620 static void
1621 move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1622 struct movable *movables;
1623 int threshold;
1624 int insn_count;
1625 rtx loop_start;
1626 rtx end;
1627 int nregs;
1628 {
1629 rtx new_start = 0;
1630 register struct movable *m;
1631 register rtx p;
1632 /* Map of pseudo-register replacements to handle combining
1633 when we move several insns that load the same value
1634 into different pseudo-registers. */
1635 rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1636 char *already_moved = (char *) alloca (nregs);
1637
1638 bzero (already_moved, nregs);
1639 bzero ((char *) reg_map, nregs * sizeof (rtx));
1640
1641 num_movables = 0;
1642
1643 for (m = movables; m; m = m->next)
1644 {
1645 /* Describe this movable insn. */
1646
1647 if (loop_dump_stream)
1648 {
1649 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1650 INSN_UID (m->insn), m->regno, m->lifetime);
1651 if (m->consec > 0)
1652 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1653 if (m->cond)
1654 fprintf (loop_dump_stream, "cond ");
1655 if (m->force)
1656 fprintf (loop_dump_stream, "force ");
1657 if (m->global)
1658 fprintf (loop_dump_stream, "global ");
1659 if (m->done)
1660 fprintf (loop_dump_stream, "done ");
1661 if (m->move_insn)
1662 fprintf (loop_dump_stream, "move-insn ");
1663 if (m->match)
1664 fprintf (loop_dump_stream, "matches %d ",
1665 INSN_UID (m->match->insn));
1666 if (m->forces)
1667 fprintf (loop_dump_stream, "forces %d ",
1668 INSN_UID (m->forces->insn));
1669 }
1670
1671 /* Count movables. Value used in heuristics in strength_reduce. */
1672 num_movables++;
1673
1674 /* Ignore the insn if it's already done (it matched something else).
1675 Otherwise, see if it is now safe to move. */
1676
1677 if (!m->done
1678 && (! m->cond
1679 || (1 == invariant_p (m->set_src)
1680 && (m->dependencies == 0
1681 || 1 == invariant_p (m->dependencies))
1682 && (m->consec == 0
1683 || 1 == consec_sets_invariant_p (m->set_dest,
1684 m->consec + 1,
1685 m->insn))))
1686 && (! m->forces || m->forces->done))
1687 {
1688 register int regno;
1689 register rtx p;
1690 int savings = m->savings;
1691
1692 /* We have an insn that is safe to move.
1693 Compute its desirability. */
1694
1695 p = m->insn;
1696 regno = m->regno;
1697
1698 if (loop_dump_stream)
1699 fprintf (loop_dump_stream, "savings %d ", savings);
1700
1701 if (moved_once[regno])
1702 {
1703 insn_count *= 2;
1704
1705 if (loop_dump_stream)
1706 fprintf (loop_dump_stream, "halved since already moved ");
1707 }
1708
1709 /* An insn MUST be moved if we already moved something else
1710 which is safe only if this one is moved too: that is,
1711 if already_moved[REGNO] is nonzero. */
1712
1713 /* An insn is desirable to move if the new lifetime of the
1714 register is no more than THRESHOLD times the old lifetime.
1715 If it's not desirable, it means the loop is so big
1716 that moving won't speed things up much,
1717 and it is liable to make register usage worse. */
1718
1719 /* It is also desirable to move if it can be moved at no
1720 extra cost because something else was already moved. */
1721
1722 if (already_moved[regno]
1723 || (threshold * savings * m->lifetime) >= insn_count
1724 || (m->forces && m->forces->done
1725 && n_times_used[m->forces->regno] == 1))
1726 {
1727 int count;
1728 register struct movable *m1;
1729 rtx first;
1730
1731 /* Now move the insns that set the reg. */
1732
1733 if (m->partial && m->match)
1734 {
1735 rtx newpat, i1;
1736 rtx r1, r2;
1737 /* Find the end of this chain of matching regs.
1738 Thus, we load each reg in the chain from that one reg.
1739 And that reg is loaded with 0 directly,
1740 since it has ->match == 0. */
1741 for (m1 = m; m1->match; m1 = m1->match);
1742 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1743 SET_DEST (PATTERN (m1->insn)));
1744 i1 = emit_insn_before (newpat, loop_start);
1745
1746 /* Mark the moved, invariant reg as being allowed to
1747 share a hard reg with the other matching invariant. */
1748 REG_NOTES (i1) = REG_NOTES (m->insn);
1749 r1 = SET_DEST (PATTERN (m->insn));
1750 r2 = SET_DEST (PATTERN (m1->insn));
1751 regs_may_share = gen_rtx (EXPR_LIST, VOIDmode, r1,
1752 gen_rtx (EXPR_LIST, VOIDmode, r2,
1753 regs_may_share));
1754 delete_insn (m->insn);
1755
1756 if (new_start == 0)
1757 new_start = i1;
1758
1759 if (loop_dump_stream)
1760 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1761 }
1762 /* If we are to re-generate the item being moved with a
1763 new move insn, first delete what we have and then emit
1764 the move insn before the loop. */
1765 else if (m->move_insn)
1766 {
1767 rtx i1, temp;
1768
1769 for (count = m->consec; count >= 0; count--)
1770 {
1771 /* If this is the first insn of a library call sequence,
1772 skip to the end. */
1773 if (GET_CODE (p) != NOTE
1774 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1775 p = XEXP (temp, 0);
1776
1777 /* If this is the last insn of a libcall sequence, then
1778 delete every insn in the sequence except the last.
1779 The last insn is handled in the normal manner. */
1780 if (GET_CODE (p) != NOTE
1781 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1782 {
1783 temp = XEXP (temp, 0);
1784 while (temp != p)
1785 temp = delete_insn (temp);
1786 }
1787
1788 p = delete_insn (p);
1789 while (p && GET_CODE (p) == NOTE)
1790 p = NEXT_INSN (p);
1791 }
1792
1793 start_sequence ();
1794 emit_move_insn (m->set_dest, m->set_src);
1795 temp = get_insns ();
1796 end_sequence ();
1797
1798 add_label_notes (m->set_src, temp);
1799
1800 i1 = emit_insns_before (temp, loop_start);
1801 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1802 REG_NOTES (i1)
1803 = gen_rtx (EXPR_LIST,
1804 m->is_equiv ? REG_EQUIV : REG_EQUAL,
1805 m->set_src, REG_NOTES (i1));
1806
1807 if (loop_dump_stream)
1808 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1809
1810 /* The more regs we move, the less we like moving them. */
1811 threshold -= 3;
1812 }
1813 else
1814 {
1815 for (count = m->consec; count >= 0; count--)
1816 {
1817 rtx i1, temp;
1818
1819 /* If first insn of libcall sequence, skip to end. */
1820 /* Do this at start of loop, since p is guaranteed to
1821 be an insn here. */
1822 if (GET_CODE (p) != NOTE
1823 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1824 p = XEXP (temp, 0);
1825
1826 /* If last insn of libcall sequence, move all
1827 insns except the last before the loop. The last
1828 insn is handled in the normal manner. */
1829 if (GET_CODE (p) != NOTE
1830 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1831 {
1832 rtx fn_address = 0;
1833 rtx fn_reg = 0;
1834 rtx fn_address_insn = 0;
1835
1836 first = 0;
1837 for (temp = XEXP (temp, 0); temp != p;
1838 temp = NEXT_INSN (temp))
1839 {
1840 rtx body;
1841 rtx n;
1842 rtx next;
1843
1844 if (GET_CODE (temp) == NOTE)
1845 continue;
1846
1847 body = PATTERN (temp);
1848
1849 /* Find the next insn after TEMP,
1850 not counting USE or NOTE insns. */
1851 for (next = NEXT_INSN (temp); next != p;
1852 next = NEXT_INSN (next))
1853 if (! (GET_CODE (next) == INSN
1854 && GET_CODE (PATTERN (next)) == USE)
1855 && GET_CODE (next) != NOTE)
1856 break;
1857
1858 /* If that is the call, this may be the insn
1859 that loads the function address.
1860
1861 Extract the function address from the insn
1862 that loads it into a register.
1863 If this insn was cse'd, we get incorrect code.
1864
1865 So emit a new move insn that copies the
1866 function address into the register that the
1867 call insn will use. flow.c will delete any
1868 redundant stores that we have created. */
1869 if (GET_CODE (next) == CALL_INSN
1870 && GET_CODE (body) == SET
1871 && GET_CODE (SET_DEST (body)) == REG
1872 && (n = find_reg_note (temp, REG_EQUAL,
1873 NULL_RTX)))
1874 {
1875 fn_reg = SET_SRC (body);
1876 if (GET_CODE (fn_reg) != REG)
1877 fn_reg = SET_DEST (body);
1878 fn_address = XEXP (n, 0);
1879 fn_address_insn = temp;
1880 }
1881 /* We have the call insn.
1882 If it uses the register we suspect it might,
1883 load it with the correct address directly. */
1884 if (GET_CODE (temp) == CALL_INSN
1885 && fn_address != 0
1886 && reg_referenced_p (fn_reg, body))
1887 emit_insn_after (gen_move_insn (fn_reg,
1888 fn_address),
1889 fn_address_insn);
1890
1891 if (GET_CODE (temp) == CALL_INSN)
1892 {
1893 i1 = emit_call_insn_before (body, loop_start);
1894 /* Because the USAGE information potentially
1895 contains objects other than hard registers
1896 we need to copy it. */
1897 if (CALL_INSN_FUNCTION_USAGE (temp))
1898 CALL_INSN_FUNCTION_USAGE (i1)
1899 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1900 }
1901 else
1902 i1 = emit_insn_before (body, loop_start);
1903 if (first == 0)
1904 first = i1;
1905 if (temp == fn_address_insn)
1906 fn_address_insn = i1;
1907 REG_NOTES (i1) = REG_NOTES (temp);
1908 delete_insn (temp);
1909 }
1910 }
1911 if (m->savemode != VOIDmode)
1912 {
1913 /* P sets REG to zero; but we should clear only
1914 the bits that are not covered by the mode
1915 m->savemode. */
1916 rtx reg = m->set_dest;
1917 rtx sequence;
1918 rtx tem;
1919
1920 start_sequence ();
1921 tem = expand_binop
1922 (GET_MODE (reg), and_optab, reg,
1923 GEN_INT ((((HOST_WIDE_INT) 1
1924 << GET_MODE_BITSIZE (m->savemode)))
1925 - 1),
1926 reg, 1, OPTAB_LIB_WIDEN);
1927 if (tem == 0)
1928 abort ();
1929 if (tem != reg)
1930 emit_move_insn (reg, tem);
1931 sequence = gen_sequence ();
1932 end_sequence ();
1933 i1 = emit_insn_before (sequence, loop_start);
1934 }
1935 else if (GET_CODE (p) == CALL_INSN)
1936 {
1937 i1 = emit_call_insn_before (PATTERN (p), loop_start);
1938 /* Because the USAGE information potentially
1939 contains objects other than hard registers
1940 we need to copy it. */
1941 if (CALL_INSN_FUNCTION_USAGE (p))
1942 CALL_INSN_FUNCTION_USAGE (i1)
1943 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
1944 }
1945 else
1946 i1 = emit_insn_before (PATTERN (p), loop_start);
1947
1948 REG_NOTES (i1) = REG_NOTES (p);
1949
1950 /* If there is a REG_EQUAL note present whose value is
1951 not loop invariant, then delete it, since it may
1952 cause problems with later optimization passes.
1953 It is possible for cse to create such notes
1954 like this as a result of record_jump_cond. */
1955
1956 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
1957 && ! invariant_p (XEXP (temp, 0)))
1958 remove_note (i1, temp);
1959
1960 if (new_start == 0)
1961 new_start = i1;
1962
1963 if (loop_dump_stream)
1964 fprintf (loop_dump_stream, " moved to %d",
1965 INSN_UID (i1));
1966
1967 #if 0
1968 /* This isn't needed because REG_NOTES is copied
1969 below and is wrong since P might be a PARALLEL. */
1970 if (REG_NOTES (i1) == 0
1971 && ! m->partial /* But not if it's a zero-extend clr. */
1972 && ! m->global /* and not if used outside the loop
1973 (since it might get set outside). */
1974 && CONSTANT_P (SET_SRC (PATTERN (p))))
1975 REG_NOTES (i1)
1976 = gen_rtx (EXPR_LIST, REG_EQUAL,
1977 SET_SRC (PATTERN (p)), REG_NOTES (i1));
1978 #endif
1979
1980 /* If library call, now fix the REG_NOTES that contain
1981 insn pointers, namely REG_LIBCALL on FIRST
1982 and REG_RETVAL on I1. */
1983 if (temp = find_reg_note (i1, REG_RETVAL, NULL_RTX))
1984 {
1985 XEXP (temp, 0) = first;
1986 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
1987 XEXP (temp, 0) = i1;
1988 }
1989
1990 delete_insn (p);
1991 do p = NEXT_INSN (p);
1992 while (p && GET_CODE (p) == NOTE);
1993 }
1994
1995 /* The more regs we move, the less we like moving them. */
1996 threshold -= 3;
1997 }
1998
1999 /* Any other movable that loads the same register
2000 MUST be moved. */
2001 already_moved[regno] = 1;
2002
2003 /* This reg has been moved out of one loop. */
2004 moved_once[regno] = 1;
2005
2006 /* The reg set here is now invariant. */
2007 if (! m->partial)
2008 n_times_set[regno] = 0;
2009
2010 m->done = 1;
2011
2012 /* Change the length-of-life info for the register
2013 to say it lives at least the full length of this loop.
2014 This will help guide optimizations in outer loops. */
2015
2016 if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
2017 /* This is the old insn before all the moved insns.
2018 We can't use the moved insn because it is out of range
2019 in uid_luid. Only the old insns have luids. */
2020 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2021 if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (end))
2022 REGNO_LAST_UID (regno) = INSN_UID (end);
2023
2024 /* Combine with this moved insn any other matching movables. */
2025
2026 if (! m->partial)
2027 for (m1 = movables; m1; m1 = m1->next)
2028 if (m1->match == m)
2029 {
2030 rtx temp;
2031
2032 /* Schedule the reg loaded by M1
2033 for replacement so that shares the reg of M.
2034 If the modes differ (only possible in restricted
2035 circumstances, make a SUBREG. */
2036 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2037 reg_map[m1->regno] = m->set_dest;
2038 else
2039 reg_map[m1->regno]
2040 = gen_lowpart_common (GET_MODE (m1->set_dest),
2041 m->set_dest);
2042
2043 /* Get rid of the matching insn
2044 and prevent further processing of it. */
2045 m1->done = 1;
2046
2047 /* if library call, delete all insn except last, which
2048 is deleted below */
2049 if (temp = find_reg_note (m1->insn, REG_RETVAL,
2050 NULL_RTX))
2051 {
2052 for (temp = XEXP (temp, 0); temp != m1->insn;
2053 temp = NEXT_INSN (temp))
2054 delete_insn (temp);
2055 }
2056 delete_insn (m1->insn);
2057
2058 /* Any other movable that loads the same register
2059 MUST be moved. */
2060 already_moved[m1->regno] = 1;
2061
2062 /* The reg merged here is now invariant,
2063 if the reg it matches is invariant. */
2064 if (! m->partial)
2065 n_times_set[m1->regno] = 0;
2066 }
2067 }
2068 else if (loop_dump_stream)
2069 fprintf (loop_dump_stream, "not desirable");
2070 }
2071 else if (loop_dump_stream && !m->match)
2072 fprintf (loop_dump_stream, "not safe");
2073
2074 if (loop_dump_stream)
2075 fprintf (loop_dump_stream, "\n");
2076 }
2077
2078 if (new_start == 0)
2079 new_start = loop_start;
2080
2081 /* Go through all the instructions in the loop, making
2082 all the register substitutions scheduled in REG_MAP. */
2083 for (p = new_start; p != end; p = NEXT_INSN (p))
2084 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2085 || GET_CODE (p) == CALL_INSN)
2086 {
2087 replace_regs (PATTERN (p), reg_map, nregs, 0);
2088 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2089 INSN_CODE (p) = -1;
2090 }
2091 }
2092 \f
2093 #if 0
2094 /* Scan X and replace the address of any MEM in it with ADDR.
2095 REG is the address that MEM should have before the replacement. */
2096
2097 static void
2098 replace_call_address (x, reg, addr)
2099 rtx x, reg, addr;
2100 {
2101 register enum rtx_code code;
2102 register int i;
2103 register char *fmt;
2104
2105 if (x == 0)
2106 return;
2107 code = GET_CODE (x);
2108 switch (code)
2109 {
2110 case PC:
2111 case CC0:
2112 case CONST_INT:
2113 case CONST_DOUBLE:
2114 case CONST:
2115 case SYMBOL_REF:
2116 case LABEL_REF:
2117 case REG:
2118 return;
2119
2120 case SET:
2121 /* Short cut for very common case. */
2122 replace_call_address (XEXP (x, 1), reg, addr);
2123 return;
2124
2125 case CALL:
2126 /* Short cut for very common case. */
2127 replace_call_address (XEXP (x, 0), reg, addr);
2128 return;
2129
2130 case MEM:
2131 /* If this MEM uses a reg other than the one we expected,
2132 something is wrong. */
2133 if (XEXP (x, 0) != reg)
2134 abort ();
2135 XEXP (x, 0) = addr;
2136 return;
2137 }
2138
2139 fmt = GET_RTX_FORMAT (code);
2140 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2141 {
2142 if (fmt[i] == 'e')
2143 replace_call_address (XEXP (x, i), reg, addr);
2144 if (fmt[i] == 'E')
2145 {
2146 register int j;
2147 for (j = 0; j < XVECLEN (x, i); j++)
2148 replace_call_address (XVECEXP (x, i, j), reg, addr);
2149 }
2150 }
2151 }
2152 #endif
2153 \f
2154 /* Return the number of memory refs to addresses that vary
2155 in the rtx X. */
2156
2157 static int
2158 count_nonfixed_reads (x)
2159 rtx x;
2160 {
2161 register enum rtx_code code;
2162 register int i;
2163 register char *fmt;
2164 int value;
2165
2166 if (x == 0)
2167 return 0;
2168
2169 code = GET_CODE (x);
2170 switch (code)
2171 {
2172 case PC:
2173 case CC0:
2174 case CONST_INT:
2175 case CONST_DOUBLE:
2176 case CONST:
2177 case SYMBOL_REF:
2178 case LABEL_REF:
2179 case REG:
2180 return 0;
2181
2182 case MEM:
2183 return ((invariant_p (XEXP (x, 0)) != 1)
2184 + count_nonfixed_reads (XEXP (x, 0)));
2185 }
2186
2187 value = 0;
2188 fmt = GET_RTX_FORMAT (code);
2189 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2190 {
2191 if (fmt[i] == 'e')
2192 value += count_nonfixed_reads (XEXP (x, i));
2193 if (fmt[i] == 'E')
2194 {
2195 register int j;
2196 for (j = 0; j < XVECLEN (x, i); j++)
2197 value += count_nonfixed_reads (XVECEXP (x, i, j));
2198 }
2199 }
2200 return value;
2201 }
2202
2203 \f
2204 #if 0
2205 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2206 Replace it with an instruction to load just the low bytes
2207 if the machine supports such an instruction,
2208 and insert above LOOP_START an instruction to clear the register. */
2209
2210 static void
2211 constant_high_bytes (p, loop_start)
2212 rtx p, loop_start;
2213 {
2214 register rtx new;
2215 register int insn_code_number;
2216
2217 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2218 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2219
2220 new = gen_rtx (SET, VOIDmode,
2221 gen_rtx (STRICT_LOW_PART, VOIDmode,
2222 gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2223 SET_DEST (PATTERN (p)),
2224 0)),
2225 XEXP (SET_SRC (PATTERN (p)), 0));
2226 insn_code_number = recog (new, p);
2227
2228 if (insn_code_number)
2229 {
2230 register int i;
2231
2232 /* Clear destination register before the loop. */
2233 emit_insn_before (gen_rtx (SET, VOIDmode,
2234 SET_DEST (PATTERN (p)),
2235 const0_rtx),
2236 loop_start);
2237
2238 /* Inside the loop, just load the low part. */
2239 PATTERN (p) = new;
2240 }
2241 }
2242 #endif
2243 \f
2244 /* Scan a loop setting the variables `unknown_address_altered',
2245 `num_mem_sets', `loop_continue', loops_enclosed', `loop_has_call',
2246 and `loop_has_volatile'.
2247 Also, fill in the array `loop_store_mems'. */
2248
2249 static void
2250 prescan_loop (start, end)
2251 rtx start, end;
2252 {
2253 register int level = 1;
2254 register rtx insn;
2255
2256 unknown_address_altered = 0;
2257 loop_has_call = 0;
2258 loop_has_volatile = 0;
2259 loop_store_mems_idx = 0;
2260
2261 num_mem_sets = 0;
2262 loops_enclosed = 1;
2263 loop_continue = 0;
2264
2265 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2266 insn = NEXT_INSN (insn))
2267 {
2268 if (GET_CODE (insn) == NOTE)
2269 {
2270 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2271 {
2272 ++level;
2273 /* Count number of loops contained in this one. */
2274 loops_enclosed++;
2275 }
2276 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2277 {
2278 --level;
2279 if (level == 0)
2280 {
2281 end = insn;
2282 break;
2283 }
2284 }
2285 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2286 {
2287 if (level == 1)
2288 loop_continue = insn;
2289 }
2290 }
2291 else if (GET_CODE (insn) == CALL_INSN)
2292 {
2293 if (! CONST_CALL_P (insn))
2294 unknown_address_altered = 1;
2295 loop_has_call = 1;
2296 }
2297 else
2298 {
2299 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2300 {
2301 if (volatile_refs_p (PATTERN (insn)))
2302 loop_has_volatile = 1;
2303
2304 note_stores (PATTERN (insn), note_addr_stored);
2305 }
2306 }
2307 }
2308 }
2309 \f
2310 /* Scan the function looking for loops. Record the start and end of each loop.
2311 Also mark as invalid loops any loops that contain a setjmp or are branched
2312 to from outside the loop. */
2313
2314 static void
2315 find_and_verify_loops (f)
2316 rtx f;
2317 {
2318 rtx insn, label;
2319 int current_loop = -1;
2320 int next_loop = -1;
2321 int loop;
2322
2323 /* If there are jumps to undefined labels,
2324 treat them as jumps out of any/all loops.
2325 This also avoids writing past end of tables when there are no loops. */
2326 uid_loop_num[0] = -1;
2327
2328 /* Find boundaries of loops, mark which loops are contained within
2329 loops, and invalidate loops that have setjmp. */
2330
2331 for (insn = f; insn; insn = NEXT_INSN (insn))
2332 {
2333 if (GET_CODE (insn) == NOTE)
2334 switch (NOTE_LINE_NUMBER (insn))
2335 {
2336 case NOTE_INSN_LOOP_BEG:
2337 loop_number_loop_starts[++next_loop] = insn;
2338 loop_number_loop_ends[next_loop] = 0;
2339 loop_outer_loop[next_loop] = current_loop;
2340 loop_invalid[next_loop] = 0;
2341 loop_number_exit_labels[next_loop] = 0;
2342 loop_number_exit_count[next_loop] = 0;
2343 current_loop = next_loop;
2344 break;
2345
2346 case NOTE_INSN_SETJMP:
2347 /* In this case, we must invalidate our current loop and any
2348 enclosing loop. */
2349 for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2350 {
2351 loop_invalid[loop] = 1;
2352 if (loop_dump_stream)
2353 fprintf (loop_dump_stream,
2354 "\nLoop at %d ignored due to setjmp.\n",
2355 INSN_UID (loop_number_loop_starts[loop]));
2356 }
2357 break;
2358
2359 case NOTE_INSN_LOOP_END:
2360 if (current_loop == -1)
2361 abort ();
2362
2363 loop_number_loop_ends[current_loop] = insn;
2364 current_loop = loop_outer_loop[current_loop];
2365 break;
2366
2367 }
2368
2369 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2370 enclosing loop, but this doesn't matter. */
2371 uid_loop_num[INSN_UID (insn)] = current_loop;
2372 }
2373
2374 /* Any loop containing a label used in an initializer must be invalidated,
2375 because it can be jumped into from anywhere. */
2376
2377 for (label = forced_labels; label; label = XEXP (label, 1))
2378 {
2379 int loop_num;
2380
2381 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2382 loop_num != -1;
2383 loop_num = loop_outer_loop[loop_num])
2384 loop_invalid[loop_num] = 1;
2385 }
2386
2387 /* Any loop containing a label used for an exception handler must be
2388 invalidated, because it can be jumped into from anywhere. */
2389
2390 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2391 {
2392 int loop_num;
2393
2394 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2395 loop_num != -1;
2396 loop_num = loop_outer_loop[loop_num])
2397 loop_invalid[loop_num] = 1;
2398 }
2399
2400 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2401 loop that it is not contained within, that loop is marked invalid.
2402 If any INSN or CALL_INSN uses a label's address, then the loop containing
2403 that label is marked invalid, because it could be jumped into from
2404 anywhere.
2405
2406 Also look for blocks of code ending in an unconditional branch that
2407 exits the loop. If such a block is surrounded by a conditional
2408 branch around the block, move the block elsewhere (see below) and
2409 invert the jump to point to the code block. This may eliminate a
2410 label in our loop and will simplify processing by both us and a
2411 possible second cse pass. */
2412
2413 for (insn = f; insn; insn = NEXT_INSN (insn))
2414 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2415 {
2416 int this_loop_num = uid_loop_num[INSN_UID (insn)];
2417
2418 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2419 {
2420 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2421 if (note)
2422 {
2423 int loop_num;
2424
2425 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2426 loop_num != -1;
2427 loop_num = loop_outer_loop[loop_num])
2428 loop_invalid[loop_num] = 1;
2429 }
2430 }
2431
2432 if (GET_CODE (insn) != JUMP_INSN)
2433 continue;
2434
2435 mark_loop_jump (PATTERN (insn), this_loop_num);
2436
2437 /* See if this is an unconditional branch outside the loop. */
2438 if (this_loop_num != -1
2439 && (GET_CODE (PATTERN (insn)) == RETURN
2440 || (simplejump_p (insn)
2441 && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
2442 != this_loop_num)))
2443 && get_max_uid () < max_uid_for_loop)
2444 {
2445 rtx p;
2446 rtx our_next = next_real_insn (insn);
2447 int dest_loop;
2448 int outer_loop = -1;
2449
2450 /* Go backwards until we reach the start of the loop, a label,
2451 or a JUMP_INSN. */
2452 for (p = PREV_INSN (insn);
2453 GET_CODE (p) != CODE_LABEL
2454 && ! (GET_CODE (p) == NOTE
2455 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2456 && GET_CODE (p) != JUMP_INSN;
2457 p = PREV_INSN (p))
2458 ;
2459
2460 /* Check for the case where we have a jump to an inner nested
2461 loop, and do not perform the optimization in that case. */
2462
2463 if (JUMP_LABEL (insn))
2464 {
2465 dest_loop = uid_loop_num[INSN_UID (JUMP_LABEL (insn))];
2466 if (dest_loop != -1)
2467 {
2468 for (outer_loop = dest_loop; outer_loop != -1;
2469 outer_loop = loop_outer_loop[outer_loop])
2470 if (outer_loop == this_loop_num)
2471 break;
2472 }
2473 }
2474
2475 /* Make sure that the target of P is within the current loop. */
2476
2477 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2478 && uid_loop_num[INSN_UID (JUMP_LABEL (p))] != this_loop_num)
2479 outer_loop = this_loop_num;
2480
2481 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2482 we have a block of code to try to move.
2483
2484 We look backward and then forward from the target of INSN
2485 to find a BARRIER at the same loop depth as the target.
2486 If we find such a BARRIER, we make a new label for the start
2487 of the block, invert the jump in P and point it to that label,
2488 and move the block of code to the spot we found. */
2489
2490 if (outer_loop == -1
2491 && GET_CODE (p) == JUMP_INSN
2492 && JUMP_LABEL (p) != 0
2493 /* Just ignore jumps to labels that were never emitted.
2494 These always indicate compilation errors. */
2495 && INSN_UID (JUMP_LABEL (p)) != 0
2496 && condjump_p (p)
2497 && ! simplejump_p (p)
2498 && next_real_insn (JUMP_LABEL (p)) == our_next)
2499 {
2500 rtx target
2501 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2502 int target_loop_num = uid_loop_num[INSN_UID (target)];
2503 rtx loc;
2504
2505 for (loc = target; loc; loc = PREV_INSN (loc))
2506 if (GET_CODE (loc) == BARRIER
2507 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2508 break;
2509
2510 if (loc == 0)
2511 for (loc = target; loc; loc = NEXT_INSN (loc))
2512 if (GET_CODE (loc) == BARRIER
2513 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2514 break;
2515
2516 if (loc)
2517 {
2518 rtx cond_label = JUMP_LABEL (p);
2519 rtx new_label = get_label_after (p);
2520
2521 /* Ensure our label doesn't go away. */
2522 LABEL_NUSES (cond_label)++;
2523
2524 /* Verify that uid_loop_num is large enough and that
2525 we can invert P. */
2526 if (invert_jump (p, new_label))
2527 {
2528 rtx q, r;
2529
2530 /* Include the BARRIER after INSN and copy the
2531 block after LOC. */
2532 new_label = squeeze_notes (new_label, NEXT_INSN (insn));
2533 reorder_insns (new_label, NEXT_INSN (insn), loc);
2534
2535 /* All those insns are now in TARGET_LOOP_NUM. */
2536 for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2537 q = NEXT_INSN (q))
2538 uid_loop_num[INSN_UID (q)] = target_loop_num;
2539
2540 /* The label jumped to by INSN is no longer a loop exit.
2541 Unless INSN does not have a label (e.g., it is a
2542 RETURN insn), search loop_number_exit_labels to find
2543 its label_ref, and remove it. Also turn off
2544 LABEL_OUTSIDE_LOOP_P bit. */
2545 if (JUMP_LABEL (insn))
2546 {
2547 int loop_num;
2548
2549 for (q = 0,
2550 r = loop_number_exit_labels[this_loop_num];
2551 r; q = r, r = LABEL_NEXTREF (r))
2552 if (XEXP (r, 0) == JUMP_LABEL (insn))
2553 {
2554 LABEL_OUTSIDE_LOOP_P (r) = 0;
2555 if (q)
2556 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2557 else
2558 loop_number_exit_labels[this_loop_num]
2559 = LABEL_NEXTREF (r);
2560 break;
2561 }
2562
2563 for (loop_num = this_loop_num;
2564 loop_num != -1 && loop_num != target_loop_num;
2565 loop_num = loop_outer_loop[loop_num])
2566 loop_number_exit_count[loop_num]--;
2567
2568 /* If we didn't find it, then something is wrong. */
2569 if (! r)
2570 abort ();
2571 }
2572
2573 /* P is now a jump outside the loop, so it must be put
2574 in loop_number_exit_labels, and marked as such.
2575 The easiest way to do this is to just call
2576 mark_loop_jump again for P. */
2577 mark_loop_jump (PATTERN (p), this_loop_num);
2578
2579 /* If INSN now jumps to the insn after it,
2580 delete INSN. */
2581 if (JUMP_LABEL (insn) != 0
2582 && (next_real_insn (JUMP_LABEL (insn))
2583 == next_real_insn (insn)))
2584 delete_insn (insn);
2585 }
2586
2587 /* Continue the loop after where the conditional
2588 branch used to jump, since the only branch insn
2589 in the block (if it still remains) is an inter-loop
2590 branch and hence needs no processing. */
2591 insn = NEXT_INSN (cond_label);
2592
2593 if (--LABEL_NUSES (cond_label) == 0)
2594 delete_insn (cond_label);
2595
2596 /* This loop will be continued with NEXT_INSN (insn). */
2597 insn = PREV_INSN (insn);
2598 }
2599 }
2600 }
2601 }
2602 }
2603
2604 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2605 loops it is contained in, mark the target loop invalid.
2606
2607 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2608
2609 static void
2610 mark_loop_jump (x, loop_num)
2611 rtx x;
2612 int loop_num;
2613 {
2614 int dest_loop;
2615 int outer_loop;
2616 int i;
2617
2618 switch (GET_CODE (x))
2619 {
2620 case PC:
2621 case USE:
2622 case CLOBBER:
2623 case REG:
2624 case MEM:
2625 case CONST_INT:
2626 case CONST_DOUBLE:
2627 case RETURN:
2628 return;
2629
2630 case CONST:
2631 /* There could be a label reference in here. */
2632 mark_loop_jump (XEXP (x, 0), loop_num);
2633 return;
2634
2635 case PLUS:
2636 case MINUS:
2637 case MULT:
2638 mark_loop_jump (XEXP (x, 0), loop_num);
2639 mark_loop_jump (XEXP (x, 1), loop_num);
2640 return;
2641
2642 case SIGN_EXTEND:
2643 case ZERO_EXTEND:
2644 mark_loop_jump (XEXP (x, 0), loop_num);
2645 return;
2646
2647 case LABEL_REF:
2648 dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
2649
2650 /* Link together all labels that branch outside the loop. This
2651 is used by final_[bg]iv_value and the loop unrolling code. Also
2652 mark this LABEL_REF so we know that this branch should predict
2653 false. */
2654
2655 /* A check to make sure the label is not in an inner nested loop,
2656 since this does not count as a loop exit. */
2657 if (dest_loop != -1)
2658 {
2659 for (outer_loop = dest_loop; outer_loop != -1;
2660 outer_loop = loop_outer_loop[outer_loop])
2661 if (outer_loop == loop_num)
2662 break;
2663 }
2664 else
2665 outer_loop = -1;
2666
2667 if (loop_num != -1 && outer_loop == -1)
2668 {
2669 LABEL_OUTSIDE_LOOP_P (x) = 1;
2670 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2671 loop_number_exit_labels[loop_num] = x;
2672
2673 for (outer_loop = loop_num;
2674 outer_loop != -1 && outer_loop != dest_loop;
2675 outer_loop = loop_outer_loop[outer_loop])
2676 loop_number_exit_count[outer_loop]++;
2677 }
2678
2679 /* If this is inside a loop, but not in the current loop or one enclosed
2680 by it, it invalidates at least one loop. */
2681
2682 if (dest_loop == -1)
2683 return;
2684
2685 /* We must invalidate every nested loop containing the target of this
2686 label, except those that also contain the jump insn. */
2687
2688 for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
2689 {
2690 /* Stop when we reach a loop that also contains the jump insn. */
2691 for (outer_loop = loop_num; outer_loop != -1;
2692 outer_loop = loop_outer_loop[outer_loop])
2693 if (dest_loop == outer_loop)
2694 return;
2695
2696 /* If we get here, we know we need to invalidate a loop. */
2697 if (loop_dump_stream && ! loop_invalid[dest_loop])
2698 fprintf (loop_dump_stream,
2699 "\nLoop at %d ignored due to multiple entry points.\n",
2700 INSN_UID (loop_number_loop_starts[dest_loop]));
2701
2702 loop_invalid[dest_loop] = 1;
2703 }
2704 return;
2705
2706 case SET:
2707 /* If this is not setting pc, ignore. */
2708 if (SET_DEST (x) == pc_rtx)
2709 mark_loop_jump (SET_SRC (x), loop_num);
2710 return;
2711
2712 case IF_THEN_ELSE:
2713 mark_loop_jump (XEXP (x, 1), loop_num);
2714 mark_loop_jump (XEXP (x, 2), loop_num);
2715 return;
2716
2717 case PARALLEL:
2718 case ADDR_VEC:
2719 for (i = 0; i < XVECLEN (x, 0); i++)
2720 mark_loop_jump (XVECEXP (x, 0, i), loop_num);
2721 return;
2722
2723 case ADDR_DIFF_VEC:
2724 for (i = 0; i < XVECLEN (x, 1); i++)
2725 mark_loop_jump (XVECEXP (x, 1, i), loop_num);
2726 return;
2727
2728 default:
2729 /* Treat anything else (such as a symbol_ref)
2730 as a branch out of this loop, but not into any loop. */
2731
2732 if (loop_num != -1)
2733 {
2734 #ifdef HAIFA
2735 LABEL_OUTSIDE_LOOP_P (x) = 1;
2736 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2737 #endif /* HAIFA */
2738
2739 loop_number_exit_labels[loop_num] = x;
2740
2741 for (outer_loop = loop_num; outer_loop != -1;
2742 outer_loop = loop_outer_loop[outer_loop])
2743 loop_number_exit_count[outer_loop]++;
2744 }
2745 return;
2746 }
2747 }
2748 \f
2749 /* Return nonzero if there is a label in the range from
2750 insn INSN to and including the insn whose luid is END
2751 INSN must have an assigned luid (i.e., it must not have
2752 been previously created by loop.c). */
2753
2754 static int
2755 labels_in_range_p (insn, end)
2756 rtx insn;
2757 int end;
2758 {
2759 while (insn && INSN_LUID (insn) <= end)
2760 {
2761 if (GET_CODE (insn) == CODE_LABEL)
2762 return 1;
2763 insn = NEXT_INSN (insn);
2764 }
2765
2766 return 0;
2767 }
2768
2769 /* Record that a memory reference X is being set. */
2770
2771 static void
2772 note_addr_stored (x)
2773 rtx x;
2774 {
2775 register int i;
2776
2777 if (x == 0 || GET_CODE (x) != MEM)
2778 return;
2779
2780 /* Count number of memory writes.
2781 This affects heuristics in strength_reduce. */
2782 num_mem_sets++;
2783
2784 /* BLKmode MEM means all memory is clobbered. */
2785 if (GET_MODE (x) == BLKmode)
2786 unknown_address_altered = 1;
2787
2788 if (unknown_address_altered)
2789 return;
2790
2791 for (i = 0; i < loop_store_mems_idx; i++)
2792 if (rtx_equal_p (XEXP (loop_store_mems[i], 0), XEXP (x, 0))
2793 && MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (loop_store_mems[i]))
2794 {
2795 /* We are storing at the same address as previously noted. Save the
2796 wider reference. */
2797 if (GET_MODE_SIZE (GET_MODE (x))
2798 > GET_MODE_SIZE (GET_MODE (loop_store_mems[i])))
2799 loop_store_mems[i] = x;
2800 break;
2801 }
2802
2803 if (i == NUM_STORES)
2804 unknown_address_altered = 1;
2805
2806 else if (i == loop_store_mems_idx)
2807 loop_store_mems[loop_store_mems_idx++] = x;
2808 }
2809 \f
2810 /* Return nonzero if the rtx X is invariant over the current loop.
2811
2812 The value is 2 if we refer to something only conditionally invariant.
2813
2814 If `unknown_address_altered' is nonzero, no memory ref is invariant.
2815 Otherwise, a memory ref is invariant if it does not conflict with
2816 anything stored in `loop_store_mems'. */
2817
2818 int
2819 invariant_p (x)
2820 register rtx x;
2821 {
2822 register int i;
2823 register enum rtx_code code;
2824 register char *fmt;
2825 int conditional = 0;
2826
2827 if (x == 0)
2828 return 1;
2829 code = GET_CODE (x);
2830 switch (code)
2831 {
2832 case CONST_INT:
2833 case CONST_DOUBLE:
2834 case SYMBOL_REF:
2835 case CONST:
2836 return 1;
2837
2838 case LABEL_REF:
2839 /* A LABEL_REF is normally invariant, however, if we are unrolling
2840 loops, and this label is inside the loop, then it isn't invariant.
2841 This is because each unrolled copy of the loop body will have
2842 a copy of this label. If this was invariant, then an insn loading
2843 the address of this label into a register might get moved outside
2844 the loop, and then each loop body would end up using the same label.
2845
2846 We don't know the loop bounds here though, so just fail for all
2847 labels. */
2848 if (flag_unroll_loops)
2849 return 0;
2850 else
2851 return 1;
2852
2853 case PC:
2854 case CC0:
2855 case UNSPEC_VOLATILE:
2856 return 0;
2857
2858 case REG:
2859 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2860 since the reg might be set by initialization within the loop. */
2861
2862 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2863 || x == arg_pointer_rtx)
2864 && ! current_function_has_nonlocal_goto)
2865 return 1;
2866
2867 if (loop_has_call
2868 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
2869 return 0;
2870
2871 if (n_times_set[REGNO (x)] < 0)
2872 return 2;
2873
2874 return n_times_set[REGNO (x)] == 0;
2875
2876 case MEM:
2877 /* Volatile memory references must be rejected. Do this before
2878 checking for read-only items, so that volatile read-only items
2879 will be rejected also. */
2880 if (MEM_VOLATILE_P (x))
2881 return 0;
2882
2883 /* Read-only items (such as constants in a constant pool) are
2884 invariant if their address is. */
2885 if (RTX_UNCHANGING_P (x))
2886 break;
2887
2888 /* If we filled the table (or had a subroutine call), any location
2889 in memory could have been clobbered. */
2890 if (unknown_address_altered)
2891 return 0;
2892
2893 /* See if there is any dependence between a store and this load. */
2894 for (i = loop_store_mems_idx - 1; i >= 0; i--)
2895 if (true_dependence (loop_store_mems[i], VOIDmode, x, rtx_varies_p))
2896 return 0;
2897
2898 /* It's not invalidated by a store in memory
2899 but we must still verify the address is invariant. */
2900 break;
2901
2902 case ASM_OPERANDS:
2903 /* Don't mess with insns declared volatile. */
2904 if (MEM_VOLATILE_P (x))
2905 return 0;
2906 }
2907
2908 fmt = GET_RTX_FORMAT (code);
2909 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2910 {
2911 if (fmt[i] == 'e')
2912 {
2913 int tem = invariant_p (XEXP (x, i));
2914 if (tem == 0)
2915 return 0;
2916 if (tem == 2)
2917 conditional = 1;
2918 }
2919 else if (fmt[i] == 'E')
2920 {
2921 register int j;
2922 for (j = 0; j < XVECLEN (x, i); j++)
2923 {
2924 int tem = invariant_p (XVECEXP (x, i, j));
2925 if (tem == 0)
2926 return 0;
2927 if (tem == 2)
2928 conditional = 1;
2929 }
2930
2931 }
2932 }
2933
2934 return 1 + conditional;
2935 }
2936
2937 \f
2938 /* Return nonzero if all the insns in the loop that set REG
2939 are INSN and the immediately following insns,
2940 and if each of those insns sets REG in an invariant way
2941 (not counting uses of REG in them).
2942
2943 The value is 2 if some of these insns are only conditionally invariant.
2944
2945 We assume that INSN itself is the first set of REG
2946 and that its source is invariant. */
2947
2948 static int
2949 consec_sets_invariant_p (reg, n_sets, insn)
2950 int n_sets;
2951 rtx reg, insn;
2952 {
2953 register rtx p = insn;
2954 register int regno = REGNO (reg);
2955 rtx temp;
2956 /* Number of sets we have to insist on finding after INSN. */
2957 int count = n_sets - 1;
2958 int old = n_times_set[regno];
2959 int value = 0;
2960 int this;
2961
2962 /* If N_SETS hit the limit, we can't rely on its value. */
2963 if (n_sets == 127)
2964 return 0;
2965
2966 n_times_set[regno] = 0;
2967
2968 while (count > 0)
2969 {
2970 register enum rtx_code code;
2971 rtx set;
2972
2973 p = NEXT_INSN (p);
2974 code = GET_CODE (p);
2975
2976 /* If library call, skip to end of of it. */
2977 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
2978 p = XEXP (temp, 0);
2979
2980 this = 0;
2981 if (code == INSN
2982 && (set = single_set (p))
2983 && GET_CODE (SET_DEST (set)) == REG
2984 && REGNO (SET_DEST (set)) == regno)
2985 {
2986 this = invariant_p (SET_SRC (set));
2987 if (this != 0)
2988 value |= this;
2989 else if (temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
2990 {
2991 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
2992 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
2993 notes are OK. */
2994 this = (CONSTANT_P (XEXP (temp, 0))
2995 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
2996 && invariant_p (XEXP (temp, 0))));
2997 if (this != 0)
2998 value |= this;
2999 }
3000 }
3001 if (this != 0)
3002 count--;
3003 else if (code != NOTE)
3004 {
3005 n_times_set[regno] = old;
3006 return 0;
3007 }
3008 }
3009
3010 n_times_set[regno] = old;
3011 /* If invariant_p ever returned 2, we return 2. */
3012 return 1 + (value & 2);
3013 }
3014
3015 #if 0
3016 /* I don't think this condition is sufficient to allow INSN
3017 to be moved, so we no longer test it. */
3018
3019 /* Return 1 if all insns in the basic block of INSN and following INSN
3020 that set REG are invariant according to TABLE. */
3021
3022 static int
3023 all_sets_invariant_p (reg, insn, table)
3024 rtx reg, insn;
3025 short *table;
3026 {
3027 register rtx p = insn;
3028 register int regno = REGNO (reg);
3029
3030 while (1)
3031 {
3032 register enum rtx_code code;
3033 p = NEXT_INSN (p);
3034 code = GET_CODE (p);
3035 if (code == CODE_LABEL || code == JUMP_INSN)
3036 return 1;
3037 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3038 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3039 && REGNO (SET_DEST (PATTERN (p))) == regno)
3040 {
3041 if (!invariant_p (SET_SRC (PATTERN (p)), table))
3042 return 0;
3043 }
3044 }
3045 }
3046 #endif /* 0 */
3047 \f
3048 /* Look at all uses (not sets) of registers in X. For each, if it is
3049 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3050 a different insn, set USAGE[REGNO] to const0_rtx. */
3051
3052 static void
3053 find_single_use_in_loop (insn, x, usage)
3054 rtx insn;
3055 rtx x;
3056 rtx *usage;
3057 {
3058 enum rtx_code code = GET_CODE (x);
3059 char *fmt = GET_RTX_FORMAT (code);
3060 int i, j;
3061
3062 if (code == REG)
3063 usage[REGNO (x)]
3064 = (usage[REGNO (x)] != 0 && usage[REGNO (x)] != insn)
3065 ? const0_rtx : insn;
3066
3067 else if (code == SET)
3068 {
3069 /* Don't count SET_DEST if it is a REG; otherwise count things
3070 in SET_DEST because if a register is partially modified, it won't
3071 show up as a potential movable so we don't care how USAGE is set
3072 for it. */
3073 if (GET_CODE (SET_DEST (x)) != REG)
3074 find_single_use_in_loop (insn, SET_DEST (x), usage);
3075 find_single_use_in_loop (insn, SET_SRC (x), usage);
3076 }
3077 else
3078 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3079 {
3080 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3081 find_single_use_in_loop (insn, XEXP (x, i), usage);
3082 else if (fmt[i] == 'E')
3083 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3084 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3085 }
3086 }
3087 \f
3088 /* Increment N_TIMES_SET at the index of each register
3089 that is modified by an insn between FROM and TO.
3090 If the value of an element of N_TIMES_SET becomes 127 or more,
3091 stop incrementing it, to avoid overflow.
3092
3093 Store in SINGLE_USAGE[I] the single insn in which register I is
3094 used, if it is only used once. Otherwise, it is set to 0 (for no
3095 uses) or const0_rtx for more than one use. This parameter may be zero,
3096 in which case this processing is not done.
3097
3098 Store in *COUNT_PTR the number of actual instruction
3099 in the loop. We use this to decide what is worth moving out. */
3100
3101 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3102 In that case, it is the insn that last set reg n. */
3103
3104 static void
3105 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3106 register rtx from, to;
3107 char *may_not_move;
3108 rtx *single_usage;
3109 int *count_ptr;
3110 int nregs;
3111 {
3112 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3113 register rtx insn;
3114 register int count = 0;
3115 register rtx dest;
3116
3117 bzero ((char *) last_set, nregs * sizeof (rtx));
3118 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3119 {
3120 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3121 {
3122 ++count;
3123
3124 /* If requested, record registers that have exactly one use. */
3125 if (single_usage)
3126 {
3127 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3128
3129 /* Include uses in REG_EQUAL notes. */
3130 if (REG_NOTES (insn))
3131 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3132 }
3133
3134 if (GET_CODE (PATTERN (insn)) == CLOBBER
3135 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
3136 /* Don't move a reg that has an explicit clobber.
3137 We might do so sometimes, but it's not worth the pain. */
3138 may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
3139
3140 if (GET_CODE (PATTERN (insn)) == SET
3141 || GET_CODE (PATTERN (insn)) == CLOBBER)
3142 {
3143 dest = SET_DEST (PATTERN (insn));
3144 while (GET_CODE (dest) == SUBREG
3145 || GET_CODE (dest) == ZERO_EXTRACT
3146 || GET_CODE (dest) == SIGN_EXTRACT
3147 || GET_CODE (dest) == STRICT_LOW_PART)
3148 dest = XEXP (dest, 0);
3149 if (GET_CODE (dest) == REG)
3150 {
3151 register int regno = REGNO (dest);
3152 /* If this is the first setting of this reg
3153 in current basic block, and it was set before,
3154 it must be set in two basic blocks, so it cannot
3155 be moved out of the loop. */
3156 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3157 may_not_move[regno] = 1;
3158 /* If this is not first setting in current basic block,
3159 see if reg was used in between previous one and this.
3160 If so, neither one can be moved. */
3161 if (last_set[regno] != 0
3162 && reg_used_between_p (dest, last_set[regno], insn))
3163 may_not_move[regno] = 1;
3164 if (n_times_set[regno] < 127)
3165 ++n_times_set[regno];
3166 last_set[regno] = insn;
3167 }
3168 }
3169 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3170 {
3171 register int i;
3172 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3173 {
3174 register rtx x = XVECEXP (PATTERN (insn), 0, i);
3175 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3176 /* Don't move a reg that has an explicit clobber.
3177 It's not worth the pain to try to do it correctly. */
3178 may_not_move[REGNO (XEXP (x, 0))] = 1;
3179
3180 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3181 {
3182 dest = SET_DEST (x);
3183 while (GET_CODE (dest) == SUBREG
3184 || GET_CODE (dest) == ZERO_EXTRACT
3185 || GET_CODE (dest) == SIGN_EXTRACT
3186 || GET_CODE (dest) == STRICT_LOW_PART)
3187 dest = XEXP (dest, 0);
3188 if (GET_CODE (dest) == REG)
3189 {
3190 register int regno = REGNO (dest);
3191 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3192 may_not_move[regno] = 1;
3193 if (last_set[regno] != 0
3194 && reg_used_between_p (dest, last_set[regno], insn))
3195 may_not_move[regno] = 1;
3196 if (n_times_set[regno] < 127)
3197 ++n_times_set[regno];
3198 last_set[regno] = insn;
3199 }
3200 }
3201 }
3202 }
3203 }
3204
3205 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3206 bzero ((char *) last_set, nregs * sizeof (rtx));
3207 }
3208 *count_ptr = count;
3209 }
3210 \f
3211 /* Given a loop that is bounded by LOOP_START and LOOP_END
3212 and that is entered at SCAN_START,
3213 return 1 if the register set in SET contained in insn INSN is used by
3214 any insn that precedes INSN in cyclic order starting
3215 from the loop entry point.
3216
3217 We don't want to use INSN_LUID here because if we restrict INSN to those
3218 that have a valid INSN_LUID, it means we cannot move an invariant out
3219 from an inner loop past two loops. */
3220
3221 static int
3222 loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3223 rtx set, insn, loop_start, scan_start, loop_end;
3224 {
3225 rtx reg = SET_DEST (set);
3226 rtx p;
3227
3228 /* Scan forward checking for register usage. If we hit INSN, we
3229 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3230 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3231 {
3232 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3233 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3234 return 1;
3235
3236 if (p == loop_end)
3237 p = loop_start;
3238 }
3239
3240 return 0;
3241 }
3242 \f
3243 /* A "basic induction variable" or biv is a pseudo reg that is set
3244 (within this loop) only by incrementing or decrementing it. */
3245 /* A "general induction variable" or giv is a pseudo reg whose
3246 value is a linear function of a biv. */
3247
3248 /* Bivs are recognized by `basic_induction_var';
3249 Givs by `general_induct_var'. */
3250
3251 /* Indexed by register number, indicates whether or not register is an
3252 induction variable, and if so what type. */
3253
3254 enum iv_mode *reg_iv_type;
3255
3256 /* Indexed by register number, contains pointer to `struct induction'
3257 if register is an induction variable. This holds general info for
3258 all induction variables. */
3259
3260 struct induction **reg_iv_info;
3261
3262 /* Indexed by register number, contains pointer to `struct iv_class'
3263 if register is a basic induction variable. This holds info describing
3264 the class (a related group) of induction variables that the biv belongs
3265 to. */
3266
3267 struct iv_class **reg_biv_class;
3268
3269 /* The head of a list which links together (via the next field)
3270 every iv class for the current loop. */
3271
3272 struct iv_class *loop_iv_list;
3273
3274 /* Communication with routines called via `note_stores'. */
3275
3276 static rtx note_insn;
3277
3278 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3279
3280 static rtx addr_placeholder;
3281
3282 /* ??? Unfinished optimizations, and possible future optimizations,
3283 for the strength reduction code. */
3284
3285 /* ??? There is one more optimization you might be interested in doing: to
3286 allocate pseudo registers for frequently-accessed memory locations.
3287 If the same memory location is referenced each time around, it might
3288 be possible to copy it into a register before and out after.
3289 This is especially useful when the memory location is a variable which
3290 is in a stack slot because somewhere its address is taken. If the
3291 loop doesn't contain a function call and the variable isn't volatile,
3292 it is safe to keep the value in a register for the duration of the
3293 loop. One tricky thing is that the copying of the value back from the
3294 register has to be done on all exits from the loop. You need to check that
3295 all the exits from the loop go to the same place. */
3296
3297 /* ??? The interaction of biv elimination, and recognition of 'constant'
3298 bivs, may cause problems. */
3299
3300 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3301 performance problems.
3302
3303 Perhaps don't eliminate things that can be combined with an addressing
3304 mode. Find all givs that have the same biv, mult_val, and add_val;
3305 then for each giv, check to see if its only use dies in a following
3306 memory address. If so, generate a new memory address and check to see
3307 if it is valid. If it is valid, then store the modified memory address,
3308 otherwise, mark the giv as not done so that it will get its own iv. */
3309
3310 /* ??? Could try to optimize branches when it is known that a biv is always
3311 positive. */
3312
3313 /* ??? When replace a biv in a compare insn, we should replace with closest
3314 giv so that an optimized branch can still be recognized by the combiner,
3315 e.g. the VAX acb insn. */
3316
3317 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3318 was rerun in loop_optimize whenever a register was added or moved.
3319 Also, some of the optimizations could be a little less conservative. */
3320 \f
3321 /* Perform strength reduction and induction variable elimination. */
3322
3323 /* Pseudo registers created during this function will be beyond the last
3324 valid index in several tables including n_times_set and regno_last_uid.
3325 This does not cause a problem here, because the added registers cannot be
3326 givs outside of their loop, and hence will never be reconsidered.
3327 But scan_loop must check regnos to make sure they are in bounds. */
3328
3329 static void
3330 strength_reduce (scan_start, end, loop_top, insn_count,
3331 loop_start, loop_end)
3332 rtx scan_start;
3333 rtx end;
3334 rtx loop_top;
3335 int insn_count;
3336 rtx loop_start;
3337 rtx loop_end;
3338 {
3339 rtx p;
3340 rtx set;
3341 rtx inc_val;
3342 rtx mult_val;
3343 rtx dest_reg;
3344 /* This is 1 if current insn is not executed at least once for every loop
3345 iteration. */
3346 int not_every_iteration = 0;
3347 /* This is 1 if current insn may be executed more than once for every
3348 loop iteration. */
3349 int maybe_multiple = 0;
3350 /* Temporary list pointers for traversing loop_iv_list. */
3351 struct iv_class *bl, **backbl;
3352 /* Ratio of extra register life span we can justify
3353 for saving an instruction. More if loop doesn't call subroutines
3354 since in that case saving an insn makes more difference
3355 and more registers are available. */
3356 /* ??? could set this to last value of threshold in move_movables */
3357 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3358 /* Map of pseudo-register replacements. */
3359 rtx *reg_map;
3360 int call_seen;
3361 rtx test;
3362 rtx end_insert_before;
3363 int loop_depth = 0;
3364
3365 reg_iv_type = (enum iv_mode *) alloca (max_reg_before_loop
3366 * sizeof (enum iv_mode *));
3367 bzero ((char *) reg_iv_type, max_reg_before_loop * sizeof (enum iv_mode *));
3368 reg_iv_info = (struct induction **)
3369 alloca (max_reg_before_loop * sizeof (struct induction *));
3370 bzero ((char *) reg_iv_info, (max_reg_before_loop
3371 * sizeof (struct induction *)));
3372 reg_biv_class = (struct iv_class **)
3373 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3374 bzero ((char *) reg_biv_class, (max_reg_before_loop
3375 * sizeof (struct iv_class *)));
3376
3377 loop_iv_list = 0;
3378 addr_placeholder = gen_reg_rtx (Pmode);
3379
3380 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3381 must be put before this insn, so that they will appear in the right
3382 order (i.e. loop order).
3383
3384 If loop_end is the end of the current function, then emit a
3385 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3386 dummy note insn. */
3387 if (NEXT_INSN (loop_end) != 0)
3388 end_insert_before = NEXT_INSN (loop_end);
3389 else
3390 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3391
3392 /* Scan through loop to find all possible bivs. */
3393
3394 p = scan_start;
3395 while (1)
3396 {
3397 p = NEXT_INSN (p);
3398 /* At end of a straight-in loop, we are done.
3399 At end of a loop entered at the bottom, scan the top. */
3400 if (p == scan_start)
3401 break;
3402 if (p == end)
3403 {
3404 if (loop_top != 0)
3405 p = loop_top;
3406 else
3407 break;
3408 if (p == scan_start)
3409 break;
3410 }
3411
3412 if (GET_CODE (p) == INSN
3413 && (set = single_set (p))
3414 && GET_CODE (SET_DEST (set)) == REG)
3415 {
3416 dest_reg = SET_DEST (set);
3417 if (REGNO (dest_reg) < max_reg_before_loop
3418 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3419 && reg_iv_type[REGNO (dest_reg)] != NOT_BASIC_INDUCT)
3420 {
3421 if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3422 dest_reg, p, &inc_val, &mult_val))
3423 {
3424 /* It is a possible basic induction variable.
3425 Create and initialize an induction structure for it. */
3426
3427 struct induction *v
3428 = (struct induction *) alloca (sizeof (struct induction));
3429
3430 record_biv (v, p, dest_reg, inc_val, mult_val,
3431 not_every_iteration, maybe_multiple);
3432 reg_iv_type[REGNO (dest_reg)] = BASIC_INDUCT;
3433 }
3434 else if (REGNO (dest_reg) < max_reg_before_loop)
3435 reg_iv_type[REGNO (dest_reg)] = NOT_BASIC_INDUCT;
3436 }
3437 }
3438
3439 /* Past CODE_LABEL, we get to insns that may be executed multiple
3440 times. The only way we can be sure that they can't is if every
3441 every jump insn between here and the end of the loop either
3442 returns, exits the loop, is a forward jump, or is a jump
3443 to the loop start. */
3444
3445 if (GET_CODE (p) == CODE_LABEL)
3446 {
3447 rtx insn = p;
3448
3449 maybe_multiple = 0;
3450
3451 while (1)
3452 {
3453 insn = NEXT_INSN (insn);
3454 if (insn == scan_start)
3455 break;
3456 if (insn == end)
3457 {
3458 if (loop_top != 0)
3459 insn = loop_top;
3460 else
3461 break;
3462 if (insn == scan_start)
3463 break;
3464 }
3465
3466 if (GET_CODE (insn) == JUMP_INSN
3467 && GET_CODE (PATTERN (insn)) != RETURN
3468 && (! condjump_p (insn)
3469 || (JUMP_LABEL (insn) != 0
3470 && JUMP_LABEL (insn) != scan_start
3471 && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3472 || INSN_UID (insn) >= max_uid_for_loop
3473 || (INSN_LUID (JUMP_LABEL (insn))
3474 < INSN_LUID (insn))))))
3475 {
3476 maybe_multiple = 1;
3477 break;
3478 }
3479 }
3480 }
3481
3482 /* Past a jump, we get to insns for which we can't count
3483 on whether they will be executed during each iteration. */
3484 /* This code appears twice in strength_reduce. There is also similar
3485 code in scan_loop. */
3486 if (GET_CODE (p) == JUMP_INSN
3487 /* If we enter the loop in the middle, and scan around to the
3488 beginning, don't set not_every_iteration for that.
3489 This can be any kind of jump, since we want to know if insns
3490 will be executed if the loop is executed. */
3491 && ! (JUMP_LABEL (p) == loop_top
3492 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3493 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3494 {
3495 rtx label = 0;
3496
3497 /* If this is a jump outside the loop, then it also doesn't
3498 matter. Check to see if the target of this branch is on the
3499 loop_number_exits_labels list. */
3500
3501 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3502 label;
3503 label = LABEL_NEXTREF (label))
3504 if (XEXP (label, 0) == JUMP_LABEL (p))
3505 break;
3506
3507 if (! label)
3508 not_every_iteration = 1;
3509 }
3510
3511 else if (GET_CODE (p) == NOTE)
3512 {
3513 /* At the virtual top of a converted loop, insns are again known to
3514 be executed each iteration: logically, the loop begins here
3515 even though the exit code has been duplicated. */
3516 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3517 not_every_iteration = 0;
3518 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3519 loop_depth++;
3520 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3521 loop_depth--;
3522 }
3523
3524 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3525 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3526 or not an insn is known to be executed each iteration of the
3527 loop, whether or not any iterations are known to occur.
3528
3529 Therefore, if we have just passed a label and have no more labels
3530 between here and the test insn of the loop, we know these insns
3531 will be executed each iteration. */
3532
3533 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3534 && no_labels_between_p (p, loop_end))
3535 not_every_iteration = 0;
3536 }
3537
3538 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3539 Make a sanity check against n_times_set. */
3540 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3541 {
3542 if (reg_iv_type[bl->regno] != BASIC_INDUCT
3543 /* Above happens if register modified by subreg, etc. */
3544 /* Make sure it is not recognized as a basic induction var: */
3545 || n_times_set[bl->regno] != bl->biv_count
3546 /* If never incremented, it is invariant that we decided not to
3547 move. So leave it alone. */
3548 || ! bl->incremented)
3549 {
3550 if (loop_dump_stream)
3551 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3552 bl->regno,
3553 (reg_iv_type[bl->regno] != BASIC_INDUCT
3554 ? "not induction variable"
3555 : (! bl->incremented ? "never incremented"
3556 : "count error")));
3557
3558 reg_iv_type[bl->regno] = NOT_BASIC_INDUCT;
3559 *backbl = bl->next;
3560 }
3561 else
3562 {
3563 backbl = &bl->next;
3564
3565 if (loop_dump_stream)
3566 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3567 }
3568 }
3569
3570 /* Exit if there are no bivs. */
3571 if (! loop_iv_list)
3572 {
3573 /* Can still unroll the loop anyways, but indicate that there is no
3574 strength reduction info available. */
3575 if (flag_unroll_loops)
3576 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 0);
3577
3578 return;
3579 }
3580
3581 /* Find initial value for each biv by searching backwards from loop_start,
3582 halting at first label. Also record any test condition. */
3583
3584 call_seen = 0;
3585 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3586 {
3587 note_insn = p;
3588
3589 if (GET_CODE (p) == CALL_INSN)
3590 call_seen = 1;
3591
3592 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3593 || GET_CODE (p) == CALL_INSN)
3594 note_stores (PATTERN (p), record_initial);
3595
3596 /* Record any test of a biv that branches around the loop if no store
3597 between it and the start of loop. We only care about tests with
3598 constants and registers and only certain of those. */
3599 if (GET_CODE (p) == JUMP_INSN
3600 && JUMP_LABEL (p) != 0
3601 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3602 && (test = get_condition_for_loop (p)) != 0
3603 && GET_CODE (XEXP (test, 0)) == REG
3604 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3605 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3606 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3607 && bl->init_insn == 0)
3608 {
3609 /* If an NE test, we have an initial value! */
3610 if (GET_CODE (test) == NE)
3611 {
3612 bl->init_insn = p;
3613 bl->init_set = gen_rtx (SET, VOIDmode,
3614 XEXP (test, 0), XEXP (test, 1));
3615 }
3616 else
3617 bl->initial_test = test;
3618 }
3619 }
3620
3621 /* Look at the each biv and see if we can say anything better about its
3622 initial value from any initializing insns set up above. (This is done
3623 in two passes to avoid missing SETs in a PARALLEL.) */
3624 for (bl = loop_iv_list; bl; bl = bl->next)
3625 {
3626 rtx src;
3627
3628 if (! bl->init_insn)
3629 continue;
3630
3631 src = SET_SRC (bl->init_set);
3632
3633 if (loop_dump_stream)
3634 fprintf (loop_dump_stream,
3635 "Biv %d initialized at insn %d: initial value ",
3636 bl->regno, INSN_UID (bl->init_insn));
3637
3638 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3639 || GET_MODE (src) == VOIDmode)
3640 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3641 {
3642 bl->initial_value = src;
3643
3644 if (loop_dump_stream)
3645 {
3646 if (GET_CODE (src) == CONST_INT)
3647 fprintf (loop_dump_stream, "%d\n", INTVAL (src));
3648 else
3649 {
3650 print_rtl (loop_dump_stream, src);
3651 fprintf (loop_dump_stream, "\n");
3652 }
3653 }
3654 }
3655 else
3656 {
3657 /* Biv initial value is not simple move,
3658 so let it keep initial value of "itself". */
3659
3660 if (loop_dump_stream)
3661 fprintf (loop_dump_stream, "is complex\n");
3662 }
3663 }
3664
3665 /* Search the loop for general induction variables. */
3666
3667 /* A register is a giv if: it is only set once, it is a function of a
3668 biv and a constant (or invariant), and it is not a biv. */
3669
3670 not_every_iteration = 0;
3671 loop_depth = 0;
3672 p = scan_start;
3673 while (1)
3674 {
3675 p = NEXT_INSN (p);
3676 /* At end of a straight-in loop, we are done.
3677 At end of a loop entered at the bottom, scan the top. */
3678 if (p == scan_start)
3679 break;
3680 if (p == end)
3681 {
3682 if (loop_top != 0)
3683 p = loop_top;
3684 else
3685 break;
3686 if (p == scan_start)
3687 break;
3688 }
3689
3690 /* Look for a general induction variable in a register. */
3691 if (GET_CODE (p) == INSN
3692 && (set = single_set (p))
3693 && GET_CODE (SET_DEST (set)) == REG
3694 && ! may_not_optimize[REGNO (SET_DEST (set))])
3695 {
3696 rtx src_reg;
3697 rtx add_val;
3698 rtx mult_val;
3699 int benefit;
3700 rtx regnote = 0;
3701
3702 dest_reg = SET_DEST (set);
3703 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
3704 continue;
3705
3706 if (/* SET_SRC is a giv. */
3707 ((benefit = general_induction_var (SET_SRC (set),
3708 &src_reg, &add_val,
3709 &mult_val))
3710 /* Equivalent expression is a giv. */
3711 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
3712 && (benefit = general_induction_var (XEXP (regnote, 0),
3713 &src_reg,
3714 &add_val, &mult_val))))
3715 /* Don't try to handle any regs made by loop optimization.
3716 We have nothing on them in regno_first_uid, etc. */
3717 && REGNO (dest_reg) < max_reg_before_loop
3718 /* Don't recognize a BASIC_INDUCT_VAR here. */
3719 && dest_reg != src_reg
3720 /* This must be the only place where the register is set. */
3721 && (n_times_set[REGNO (dest_reg)] == 1
3722 /* or all sets must be consecutive and make a giv. */
3723 || (benefit = consec_sets_giv (benefit, p,
3724 src_reg, dest_reg,
3725 &add_val, &mult_val))))
3726 {
3727 int count;
3728 struct induction *v
3729 = (struct induction *) alloca (sizeof (struct induction));
3730 rtx temp;
3731
3732 /* If this is a library call, increase benefit. */
3733 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
3734 benefit += libcall_benefit (p);
3735
3736 /* Skip the consecutive insns, if there are any. */
3737 for (count = n_times_set[REGNO (dest_reg)] - 1;
3738 count > 0; count--)
3739 {
3740 /* If first insn of libcall sequence, skip to end.
3741 Do this at start of loop, since INSN is guaranteed to
3742 be an insn here. */
3743 if (GET_CODE (p) != NOTE
3744 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3745 p = XEXP (temp, 0);
3746
3747 do p = NEXT_INSN (p);
3748 while (GET_CODE (p) == NOTE);
3749 }
3750
3751 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
3752 DEST_REG, not_every_iteration, NULL_PTR, loop_start,
3753 loop_end);
3754
3755 }
3756 }
3757
3758 #ifndef DONT_REDUCE_ADDR
3759 /* Look for givs which are memory addresses. */
3760 /* This resulted in worse code on a VAX 8600. I wonder if it
3761 still does. */
3762 if (GET_CODE (p) == INSN)
3763 find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
3764 loop_end);
3765 #endif
3766
3767 /* Update the status of whether giv can derive other givs. This can
3768 change when we pass a label or an insn that updates a biv. */
3769 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3770 || GET_CODE (p) == CODE_LABEL)
3771 update_giv_derive (p);
3772
3773 /* Past a jump, we get to insns for which we can't count
3774 on whether they will be executed during each iteration. */
3775 /* This code appears twice in strength_reduce. There is also similar
3776 code in scan_loop. */
3777 if (GET_CODE (p) == JUMP_INSN
3778 /* If we enter the loop in the middle, and scan around to the
3779 beginning, don't set not_every_iteration for that.
3780 This can be any kind of jump, since we want to know if insns
3781 will be executed if the loop is executed. */
3782 && ! (JUMP_LABEL (p) == loop_top
3783 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3784 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3785 {
3786 rtx label = 0;
3787
3788 /* If this is a jump outside the loop, then it also doesn't
3789 matter. Check to see if the target of this branch is on the
3790 loop_number_exits_labels list. */
3791
3792 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3793 label;
3794 label = LABEL_NEXTREF (label))
3795 if (XEXP (label, 0) == JUMP_LABEL (p))
3796 break;
3797
3798 if (! label)
3799 not_every_iteration = 1;
3800 }
3801
3802 else if (GET_CODE (p) == NOTE)
3803 {
3804 /* At the virtual top of a converted loop, insns are again known to
3805 be executed each iteration: logically, the loop begins here
3806 even though the exit code has been duplicated. */
3807 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3808 not_every_iteration = 0;
3809 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3810 loop_depth++;
3811 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3812 loop_depth--;
3813 }
3814
3815 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3816 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3817 or not an insn is known to be executed each iteration of the
3818 loop, whether or not any iterations are known to occur.
3819
3820 Therefore, if we have just passed a label and have no more labels
3821 between here and the test insn of the loop, we know these insns
3822 will be executed each iteration. */
3823
3824 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3825 && no_labels_between_p (p, loop_end))
3826 not_every_iteration = 0;
3827 }
3828
3829 /* Try to calculate and save the number of loop iterations. This is
3830 set to zero if the actual number can not be calculated. This must
3831 be called after all giv's have been identified, since otherwise it may
3832 fail if the iteration variable is a giv. */
3833
3834 loop_n_iterations = loop_iterations (loop_start, loop_end);
3835
3836 /* Now for each giv for which we still don't know whether or not it is
3837 replaceable, check to see if it is replaceable because its final value
3838 can be calculated. This must be done after loop_iterations is called,
3839 so that final_giv_value will work correctly. */
3840
3841 for (bl = loop_iv_list; bl; bl = bl->next)
3842 {
3843 struct induction *v;
3844
3845 for (v = bl->giv; v; v = v->next_iv)
3846 if (! v->replaceable && ! v->not_replaceable)
3847 check_final_value (v, loop_start, loop_end);
3848 }
3849
3850 /* Try to prove that the loop counter variable (if any) is always
3851 nonnegative; if so, record that fact with a REG_NONNEG note
3852 so that "decrement and branch until zero" insn can be used. */
3853 check_dbra_loop (loop_end, insn_count, loop_start);
3854
3855 #ifdef HAIFA
3856 /* record loop-variables relevant for BCT optimization before unrolling
3857 the loop. Unrolling may update part of this information, and the
3858 correct data will be used for generating the BCT. */
3859 #ifdef HAVE_decrement_and_branch_on_count
3860 if (HAVE_decrement_and_branch_on_count)
3861 analyze_loop_iterations (loop_start, loop_end);
3862 #endif
3863 #endif /* HAIFA */
3864
3865 /* Create reg_map to hold substitutions for replaceable giv regs. */
3866 reg_map = (rtx *) alloca (max_reg_before_loop * sizeof (rtx));
3867 bzero ((char *) reg_map, max_reg_before_loop * sizeof (rtx));
3868
3869 /* Examine each iv class for feasibility of strength reduction/induction
3870 variable elimination. */
3871
3872 for (bl = loop_iv_list; bl; bl = bl->next)
3873 {
3874 struct induction *v;
3875 int benefit;
3876 int all_reduced;
3877 rtx final_value = 0;
3878
3879 /* Test whether it will be possible to eliminate this biv
3880 provided all givs are reduced. This is possible if either
3881 the reg is not used outside the loop, or we can compute
3882 what its final value will be.
3883
3884 For architectures with a decrement_and_branch_until_zero insn,
3885 don't do this if we put a REG_NONNEG note on the endtest for
3886 this biv. */
3887
3888 /* Compare against bl->init_insn rather than loop_start.
3889 We aren't concerned with any uses of the biv between
3890 init_insn and loop_start since these won't be affected
3891 by the value of the biv elsewhere in the function, so
3892 long as init_insn doesn't use the biv itself.
3893 March 14, 1989 -- self@bayes.arc.nasa.gov */
3894
3895 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
3896 && bl->init_insn
3897 && INSN_UID (bl->init_insn) < max_uid_for_loop
3898 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
3899 #ifdef HAVE_decrement_and_branch_until_zero
3900 && ! bl->nonneg
3901 #endif
3902 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3903 || ((final_value = final_biv_value (bl, loop_start, loop_end))
3904 #ifdef HAVE_decrement_and_branch_until_zero
3905 && ! bl->nonneg
3906 #endif
3907 ))
3908 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
3909 threshold, insn_count);
3910 else
3911 {
3912 if (loop_dump_stream)
3913 {
3914 fprintf (loop_dump_stream,
3915 "Cannot eliminate biv %d.\n",
3916 bl->regno);
3917 fprintf (loop_dump_stream,
3918 "First use: insn %d, last use: insn %d.\n",
3919 REGNO_FIRST_UID (bl->regno),
3920 REGNO_LAST_UID (bl->regno));
3921 }
3922 }
3923
3924 /* Combine all giv's for this iv_class. */
3925 combine_givs (bl);
3926
3927 /* This will be true at the end, if all givs which depend on this
3928 biv have been strength reduced.
3929 We can't (currently) eliminate the biv unless this is so. */
3930 all_reduced = 1;
3931
3932 /* Check each giv in this class to see if we will benefit by reducing
3933 it. Skip giv's combined with others. */
3934 for (v = bl->giv; v; v = v->next_iv)
3935 {
3936 struct induction *tv;
3937
3938 if (v->ignore || v->same)
3939 continue;
3940
3941 benefit = v->benefit;
3942
3943 /* Reduce benefit if not replaceable, since we will insert
3944 a move-insn to replace the insn that calculates this giv.
3945 Don't do this unless the giv is a user variable, since it
3946 will often be marked non-replaceable because of the duplication
3947 of the exit code outside the loop. In such a case, the copies
3948 we insert are dead and will be deleted. So they don't have
3949 a cost. Similar situations exist. */
3950 /* ??? The new final_[bg]iv_value code does a much better job
3951 of finding replaceable giv's, and hence this code may no longer
3952 be necessary. */
3953 if (! v->replaceable && ! bl->eliminable
3954 && REG_USERVAR_P (v->dest_reg))
3955 benefit -= copy_cost;
3956
3957 /* Decrease the benefit to count the add-insns that we will
3958 insert to increment the reduced reg for the giv. */
3959 benefit -= add_cost * bl->biv_count;
3960
3961 /* Decide whether to strength-reduce this giv or to leave the code
3962 unchanged (recompute it from the biv each time it is used).
3963 This decision can be made independently for each giv. */
3964
3965 #ifdef AUTO_INC_DEC
3966 /* Attempt to guess whether autoincrement will handle some of the
3967 new add insns; if so, increase BENEFIT (undo the subtraction of
3968 add_cost that was done above). */
3969 if (v->giv_type == DEST_ADDR
3970 && GET_CODE (v->mult_val) == CONST_INT)
3971 {
3972 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_PRE_INCREMENT)
3973 if (INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3974 benefit += add_cost * bl->biv_count;
3975 #endif
3976 #if defined (HAVE_POST_DECREMENT) || defined (HAVE_PRE_DECREMENT)
3977 if (-INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3978 benefit += add_cost * bl->biv_count;
3979 #endif
3980 }
3981 #endif
3982
3983 /* If an insn is not to be strength reduced, then set its ignore
3984 flag, and clear all_reduced. */
3985
3986 /* A giv that depends on a reversed biv must be reduced if it is
3987 used after the loop exit, otherwise, it would have the wrong
3988 value after the loop exit. To make it simple, just reduce all
3989 of such giv's whether or not we know they are used after the loop
3990 exit. */
3991
3992 if (v->lifetime * threshold * benefit < insn_count
3993 && ! bl->reversed)
3994 {
3995 if (loop_dump_stream)
3996 fprintf (loop_dump_stream,
3997 "giv of insn %d not worth while, %d vs %d.\n",
3998 INSN_UID (v->insn),
3999 v->lifetime * threshold * benefit, insn_count);
4000 v->ignore = 1;
4001 all_reduced = 0;
4002 }
4003 else
4004 {
4005 /* Check that we can increment the reduced giv without a
4006 multiply insn. If not, reject it. */
4007
4008 for (tv = bl->biv; tv; tv = tv->next_iv)
4009 if (tv->mult_val == const1_rtx
4010 && ! product_cheap_p (tv->add_val, v->mult_val))
4011 {
4012 if (loop_dump_stream)
4013 fprintf (loop_dump_stream,
4014 "giv of insn %d: would need a multiply.\n",
4015 INSN_UID (v->insn));
4016 v->ignore = 1;
4017 all_reduced = 0;
4018 break;
4019 }
4020 }
4021 }
4022
4023 /* Reduce each giv that we decided to reduce. */
4024
4025 for (v = bl->giv; v; v = v->next_iv)
4026 {
4027 struct induction *tv;
4028 if (! v->ignore && v->same == 0)
4029 {
4030 int auto_inc_opt = 0;
4031
4032 v->new_reg = gen_reg_rtx (v->mode);
4033
4034 #ifdef AUTO_INC_DEC
4035 /* If the target has auto-increment addressing modes, and
4036 this is an address giv, then try to put the increment
4037 immediately after its use, so that flow can create an
4038 auto-increment addressing mode. */
4039 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4040 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4041 /* We don't handle reversed biv's because bl->biv->insn
4042 does not have a valid INSN_LUID. */
4043 && ! bl->reversed
4044 && v->always_executed && ! v->maybe_multiple)
4045 {
4046 /* If other giv's have been combined with this one, then
4047 this will work only if all uses of the other giv's occur
4048 before this giv's insn. This is difficult to check.
4049
4050 We simplify this by looking for the common case where
4051 there is one DEST_REG giv, and this giv's insn is the
4052 last use of the dest_reg of that DEST_REG giv. If the
4053 the increment occurs after the address giv, then we can
4054 perform the optimization. (Otherwise, the increment
4055 would have to go before other_giv, and we would not be
4056 able to combine it with the address giv to get an
4057 auto-inc address.) */
4058 if (v->combined_with)
4059 {
4060 struct induction *other_giv = 0;
4061
4062 for (tv = bl->giv; tv; tv = tv->next_iv)
4063 if (tv->same == v)
4064 {
4065 if (other_giv)
4066 break;
4067 else
4068 other_giv = tv;
4069 }
4070 if (! tv && other_giv
4071 && REGNO (other_giv->dest_reg) <= max_reg_before_loop
4072 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4073 == INSN_UID (v->insn))
4074 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4075 auto_inc_opt = 1;
4076 }
4077 /* Check for case where increment is before the the address
4078 giv. */
4079 else if (INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn))
4080 auto_inc_opt = -1;
4081 else
4082 auto_inc_opt = 1;
4083
4084 #ifdef HAVE_cc0
4085 {
4086 rtx prev;
4087
4088 /* We can't put an insn immediately after one setting
4089 cc0, or immediately before one using cc0. */
4090 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4091 || (auto_inc_opt == -1
4092 && (prev = prev_nonnote_insn (v->insn)) != 0
4093 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4094 && sets_cc0_p (PATTERN (prev))))
4095 auto_inc_opt = 0;
4096 }
4097 #endif
4098
4099 if (auto_inc_opt)
4100 v->auto_inc_opt = 1;
4101 }
4102 #endif
4103
4104 /* For each place where the biv is incremented, add an insn
4105 to increment the new, reduced reg for the giv. */
4106 for (tv = bl->biv; tv; tv = tv->next_iv)
4107 {
4108 rtx insert_before;
4109
4110 if (! auto_inc_opt)
4111 insert_before = tv->insn;
4112 else if (auto_inc_opt == 1)
4113 insert_before = NEXT_INSN (v->insn);
4114 else
4115 insert_before = v->insn;
4116
4117 if (tv->mult_val == const1_rtx)
4118 emit_iv_add_mult (tv->add_val, v->mult_val,
4119 v->new_reg, v->new_reg, insert_before);
4120 else /* tv->mult_val == const0_rtx */
4121 /* A multiply is acceptable here
4122 since this is presumed to be seldom executed. */
4123 emit_iv_add_mult (tv->add_val, v->mult_val,
4124 v->add_val, v->new_reg, insert_before);
4125 }
4126
4127 /* Add code at loop start to initialize giv's reduced reg. */
4128
4129 emit_iv_add_mult (bl->initial_value, v->mult_val,
4130 v->add_val, v->new_reg, loop_start);
4131 }
4132 }
4133
4134 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4135 as not reduced.
4136
4137 For each giv register that can be reduced now: if replaceable,
4138 substitute reduced reg wherever the old giv occurs;
4139 else add new move insn "giv_reg = reduced_reg".
4140
4141 Also check for givs whose first use is their definition and whose
4142 last use is the definition of another giv. If so, it is likely
4143 dead and should not be used to eliminate a biv. */
4144 for (v = bl->giv; v; v = v->next_iv)
4145 {
4146 if (v->same && v->same->ignore)
4147 v->ignore = 1;
4148
4149 if (v->ignore)
4150 continue;
4151
4152 if (v->giv_type == DEST_REG
4153 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4154 {
4155 struct induction *v1;
4156
4157 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4158 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4159 v->maybe_dead = 1;
4160 }
4161
4162 /* Update expression if this was combined, in case other giv was
4163 replaced. */
4164 if (v->same)
4165 v->new_reg = replace_rtx (v->new_reg,
4166 v->same->dest_reg, v->same->new_reg);
4167
4168 if (v->giv_type == DEST_ADDR)
4169 /* Store reduced reg as the address in the memref where we found
4170 this giv. */
4171 validate_change (v->insn, v->location, v->new_reg, 0);
4172 else if (v->replaceable)
4173 {
4174 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4175
4176 #if 0
4177 /* I can no longer duplicate the original problem. Perhaps
4178 this is unnecessary now? */
4179
4180 /* Replaceable; it isn't strictly necessary to delete the old
4181 insn and emit a new one, because v->dest_reg is now dead.
4182
4183 However, especially when unrolling loops, the special
4184 handling for (set REG0 REG1) in the second cse pass may
4185 make v->dest_reg live again. To avoid this problem, emit
4186 an insn to set the original giv reg from the reduced giv.
4187 We can not delete the original insn, since it may be part
4188 of a LIBCALL, and the code in flow that eliminates dead
4189 libcalls will fail if it is deleted. */
4190 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4191 v->insn);
4192 #endif
4193 }
4194 else
4195 {
4196 /* Not replaceable; emit an insn to set the original giv reg from
4197 the reduced giv, same as above. */
4198 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4199 v->insn);
4200 }
4201
4202 /* When a loop is reversed, givs which depend on the reversed
4203 biv, and which are live outside the loop, must be set to their
4204 correct final value. This insn is only needed if the giv is
4205 not replaceable. The correct final value is the same as the
4206 value that the giv starts the reversed loop with. */
4207 if (bl->reversed && ! v->replaceable)
4208 emit_iv_add_mult (bl->initial_value, v->mult_val,
4209 v->add_val, v->dest_reg, end_insert_before);
4210 else if (v->final_value)
4211 {
4212 rtx insert_before;
4213
4214 /* If the loop has multiple exits, emit the insn before the
4215 loop to ensure that it will always be executed no matter
4216 how the loop exits. Otherwise, emit the insn after the loop,
4217 since this is slightly more efficient. */
4218 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4219 insert_before = loop_start;
4220 else
4221 insert_before = end_insert_before;
4222 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4223 insert_before);
4224
4225 #if 0
4226 /* If the insn to set the final value of the giv was emitted
4227 before the loop, then we must delete the insn inside the loop
4228 that sets it. If this is a LIBCALL, then we must delete
4229 every insn in the libcall. Note, however, that
4230 final_giv_value will only succeed when there are multiple
4231 exits if the giv is dead at each exit, hence it does not
4232 matter that the original insn remains because it is dead
4233 anyways. */
4234 /* Delete the insn inside the loop that sets the giv since
4235 the giv is now set before (or after) the loop. */
4236 delete_insn (v->insn);
4237 #endif
4238 }
4239
4240 if (loop_dump_stream)
4241 {
4242 fprintf (loop_dump_stream, "giv at %d reduced to ",
4243 INSN_UID (v->insn));
4244 print_rtl (loop_dump_stream, v->new_reg);
4245 fprintf (loop_dump_stream, "\n");
4246 }
4247 }
4248
4249 /* All the givs based on the biv bl have been reduced if they
4250 merit it. */
4251
4252 /* For each giv not marked as maybe dead that has been combined with a
4253 second giv, clear any "maybe dead" mark on that second giv.
4254 v->new_reg will either be or refer to the register of the giv it
4255 combined with.
4256
4257 Doing this clearing avoids problems in biv elimination where a
4258 giv's new_reg is a complex value that can't be put in the insn but
4259 the giv combined with (with a reg as new_reg) is marked maybe_dead.
4260 Since the register will be used in either case, we'd prefer it be
4261 used from the simpler giv. */
4262
4263 for (v = bl->giv; v; v = v->next_iv)
4264 if (! v->maybe_dead && v->same)
4265 v->same->maybe_dead = 0;
4266
4267 /* Try to eliminate the biv, if it is a candidate.
4268 This won't work if ! all_reduced,
4269 since the givs we planned to use might not have been reduced.
4270
4271 We have to be careful that we didn't initially think we could eliminate
4272 this biv because of a giv that we now think may be dead and shouldn't
4273 be used as a biv replacement.
4274
4275 Also, there is the possibility that we may have a giv that looks
4276 like it can be used to eliminate a biv, but the resulting insn
4277 isn't valid. This can happen, for example, on the 88k, where a
4278 JUMP_INSN can compare a register only with zero. Attempts to
4279 replace it with a compare with a constant will fail.
4280
4281 Note that in cases where this call fails, we may have replaced some
4282 of the occurrences of the biv with a giv, but no harm was done in
4283 doing so in the rare cases where it can occur. */
4284
4285 if (all_reduced == 1 && bl->eliminable
4286 && maybe_eliminate_biv (bl, loop_start, end, 1,
4287 threshold, insn_count))
4288
4289 {
4290 /* ?? If we created a new test to bypass the loop entirely,
4291 or otherwise drop straight in, based on this test, then
4292 we might want to rewrite it also. This way some later
4293 pass has more hope of removing the initialization of this
4294 biv entirely. */
4295
4296 /* If final_value != 0, then the biv may be used after loop end
4297 and we must emit an insn to set it just in case.
4298
4299 Reversed bivs already have an insn after the loop setting their
4300 value, so we don't need another one. We can't calculate the
4301 proper final value for such a biv here anyways. */
4302 if (final_value != 0 && ! bl->reversed)
4303 {
4304 rtx insert_before;
4305
4306 /* If the loop has multiple exits, emit the insn before the
4307 loop to ensure that it will always be executed no matter
4308 how the loop exits. Otherwise, emit the insn after the
4309 loop, since this is slightly more efficient. */
4310 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4311 insert_before = loop_start;
4312 else
4313 insert_before = end_insert_before;
4314
4315 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
4316 end_insert_before);
4317 }
4318
4319 #if 0
4320 /* Delete all of the instructions inside the loop which set
4321 the biv, as they are all dead. If is safe to delete them,
4322 because an insn setting a biv will never be part of a libcall. */
4323 /* However, deleting them will invalidate the regno_last_uid info,
4324 so keeping them around is more convenient. Final_biv_value
4325 will only succeed when there are multiple exits if the biv
4326 is dead at each exit, hence it does not matter that the original
4327 insn remains, because it is dead anyways. */
4328 for (v = bl->biv; v; v = v->next_iv)
4329 delete_insn (v->insn);
4330 #endif
4331
4332 if (loop_dump_stream)
4333 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4334 bl->regno);
4335 }
4336 }
4337
4338 /* Go through all the instructions in the loop, making all the
4339 register substitutions scheduled in REG_MAP. */
4340
4341 for (p = loop_start; p != end; p = NEXT_INSN (p))
4342 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4343 || GET_CODE (p) == CALL_INSN)
4344 {
4345 replace_regs (PATTERN (p), reg_map, max_reg_before_loop, 0);
4346 replace_regs (REG_NOTES (p), reg_map, max_reg_before_loop, 0);
4347 INSN_CODE (p) = -1;
4348 }
4349
4350 /* Unroll loops from within strength reduction so that we can use the
4351 induction variable information that strength_reduce has already
4352 collected. */
4353
4354 if (flag_unroll_loops)
4355 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 1);
4356
4357 #ifdef HAIFA
4358 /* instrument the loop with bct insn */
4359 #ifdef HAVE_decrement_and_branch_on_count
4360 if (HAVE_decrement_and_branch_on_count)
4361 insert_bct (loop_start, loop_end);
4362 #endif
4363 #endif /* HAIFA */
4364
4365 if (loop_dump_stream)
4366 fprintf (loop_dump_stream, "\n");
4367 }
4368 \f
4369 /* Return 1 if X is a valid source for an initial value (or as value being
4370 compared against in an initial test).
4371
4372 X must be either a register or constant and must not be clobbered between
4373 the current insn and the start of the loop.
4374
4375 INSN is the insn containing X. */
4376
4377 static int
4378 valid_initial_value_p (x, insn, call_seen, loop_start)
4379 rtx x;
4380 rtx insn;
4381 int call_seen;
4382 rtx loop_start;
4383 {
4384 if (CONSTANT_P (x))
4385 return 1;
4386
4387 /* Only consider pseudos we know about initialized in insns whose luids
4388 we know. */
4389 if (GET_CODE (x) != REG
4390 || REGNO (x) >= max_reg_before_loop)
4391 return 0;
4392
4393 /* Don't use call-clobbered registers across a call which clobbers it. On
4394 some machines, don't use any hard registers at all. */
4395 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4396 && (
4397 #ifdef SMALL_REGISTER_CLASSES
4398 SMALL_REGISTER_CLASSES
4399 #else
4400 0
4401 #endif
4402 || (call_used_regs[REGNO (x)] && call_seen))
4403 )
4404 return 0;
4405
4406 /* Don't use registers that have been clobbered before the start of the
4407 loop. */
4408 if (reg_set_between_p (x, insn, loop_start))
4409 return 0;
4410
4411 return 1;
4412 }
4413 \f
4414 /* Scan X for memory refs and check each memory address
4415 as a possible giv. INSN is the insn whose pattern X comes from.
4416 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4417 every loop iteration. */
4418
4419 static void
4420 find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
4421 rtx x;
4422 rtx insn;
4423 int not_every_iteration;
4424 rtx loop_start, loop_end;
4425 {
4426 register int i, j;
4427 register enum rtx_code code;
4428 register char *fmt;
4429
4430 if (x == 0)
4431 return;
4432
4433 code = GET_CODE (x);
4434 switch (code)
4435 {
4436 case REG:
4437 case CONST_INT:
4438 case CONST:
4439 case CONST_DOUBLE:
4440 case SYMBOL_REF:
4441 case LABEL_REF:
4442 case PC:
4443 case CC0:
4444 case ADDR_VEC:
4445 case ADDR_DIFF_VEC:
4446 case USE:
4447 case CLOBBER:
4448 return;
4449
4450 case MEM:
4451 {
4452 rtx src_reg;
4453 rtx add_val;
4454 rtx mult_val;
4455 int benefit;
4456
4457 benefit = general_induction_var (XEXP (x, 0),
4458 &src_reg, &add_val, &mult_val);
4459
4460 /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4461 Such a giv isn't useful. */
4462 if (benefit > 0 && (mult_val != const1_rtx || add_val != const0_rtx))
4463 {
4464 /* Found one; record it. */
4465 struct induction *v
4466 = (struct induction *) oballoc (sizeof (struct induction));
4467
4468 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
4469 add_val, benefit, DEST_ADDR, not_every_iteration,
4470 &XEXP (x, 0), loop_start, loop_end);
4471
4472 v->mem_mode = GET_MODE (x);
4473 }
4474 return;
4475 }
4476 }
4477
4478 /* Recursively scan the subexpressions for other mem refs. */
4479
4480 fmt = GET_RTX_FORMAT (code);
4481 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4482 if (fmt[i] == 'e')
4483 find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
4484 loop_end);
4485 else if (fmt[i] == 'E')
4486 for (j = 0; j < XVECLEN (x, i); j++)
4487 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
4488 loop_start, loop_end);
4489 }
4490 \f
4491 /* Fill in the data about one biv update.
4492 V is the `struct induction' in which we record the biv. (It is
4493 allocated by the caller, with alloca.)
4494 INSN is the insn that sets it.
4495 DEST_REG is the biv's reg.
4496
4497 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4498 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4499 being set to INC_VAL.
4500
4501 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4502 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4503 can be executed more than once per iteration. If MAYBE_MULTIPLE
4504 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4505 executed exactly once per iteration. */
4506
4507 static void
4508 record_biv (v, insn, dest_reg, inc_val, mult_val,
4509 not_every_iteration, maybe_multiple)
4510 struct induction *v;
4511 rtx insn;
4512 rtx dest_reg;
4513 rtx inc_val;
4514 rtx mult_val;
4515 int not_every_iteration;
4516 int maybe_multiple;
4517 {
4518 struct iv_class *bl;
4519
4520 v->insn = insn;
4521 v->src_reg = dest_reg;
4522 v->dest_reg = dest_reg;
4523 v->mult_val = mult_val;
4524 v->add_val = inc_val;
4525 v->mode = GET_MODE (dest_reg);
4526 v->always_computable = ! not_every_iteration;
4527 v->always_executed = ! not_every_iteration;
4528 v->maybe_multiple = maybe_multiple;
4529
4530 /* Add this to the reg's iv_class, creating a class
4531 if this is the first incrementation of the reg. */
4532
4533 bl = reg_biv_class[REGNO (dest_reg)];
4534 if (bl == 0)
4535 {
4536 /* Create and initialize new iv_class. */
4537
4538 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
4539
4540 bl->regno = REGNO (dest_reg);
4541 bl->biv = 0;
4542 bl->giv = 0;
4543 bl->biv_count = 0;
4544 bl->giv_count = 0;
4545
4546 /* Set initial value to the reg itself. */
4547 bl->initial_value = dest_reg;
4548 /* We haven't seen the initializing insn yet */
4549 bl->init_insn = 0;
4550 bl->init_set = 0;
4551 bl->initial_test = 0;
4552 bl->incremented = 0;
4553 bl->eliminable = 0;
4554 bl->nonneg = 0;
4555 bl->reversed = 0;
4556 bl->total_benefit = 0;
4557
4558 /* Add this class to loop_iv_list. */
4559 bl->next = loop_iv_list;
4560 loop_iv_list = bl;
4561
4562 /* Put it in the array of biv register classes. */
4563 reg_biv_class[REGNO (dest_reg)] = bl;
4564 }
4565
4566 /* Update IV_CLASS entry for this biv. */
4567 v->next_iv = bl->biv;
4568 bl->biv = v;
4569 bl->biv_count++;
4570 if (mult_val == const1_rtx)
4571 bl->incremented = 1;
4572
4573 if (loop_dump_stream)
4574 {
4575 fprintf (loop_dump_stream,
4576 "Insn %d: possible biv, reg %d,",
4577 INSN_UID (insn), REGNO (dest_reg));
4578 if (GET_CODE (inc_val) == CONST_INT)
4579 fprintf (loop_dump_stream, " const = %d\n",
4580 INTVAL (inc_val));
4581 else
4582 {
4583 fprintf (loop_dump_stream, " const = ");
4584 print_rtl (loop_dump_stream, inc_val);
4585 fprintf (loop_dump_stream, "\n");
4586 }
4587 }
4588 }
4589 \f
4590 /* Fill in the data about one giv.
4591 V is the `struct induction' in which we record the giv. (It is
4592 allocated by the caller, with alloca.)
4593 INSN is the insn that sets it.
4594 BENEFIT estimates the savings from deleting this insn.
4595 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4596 into a register or is used as a memory address.
4597
4598 SRC_REG is the biv reg which the giv is computed from.
4599 DEST_REG is the giv's reg (if the giv is stored in a reg).
4600 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4601 LOCATION points to the place where this giv's value appears in INSN. */
4602
4603 static void
4604 record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
4605 type, not_every_iteration, location, loop_start, loop_end)
4606 struct induction *v;
4607 rtx insn;
4608 rtx src_reg;
4609 rtx dest_reg;
4610 rtx mult_val, add_val;
4611 int benefit;
4612 enum g_types type;
4613 int not_every_iteration;
4614 rtx *location;
4615 rtx loop_start, loop_end;
4616 {
4617 struct induction *b;
4618 struct iv_class *bl;
4619 rtx set = single_set (insn);
4620 rtx p;
4621
4622 v->insn = insn;
4623 v->src_reg = src_reg;
4624 v->giv_type = type;
4625 v->dest_reg = dest_reg;
4626 v->mult_val = mult_val;
4627 v->add_val = add_val;
4628 v->benefit = benefit;
4629 v->location = location;
4630 v->cant_derive = 0;
4631 v->combined_with = 0;
4632 v->maybe_multiple = 0;
4633 v->maybe_dead = 0;
4634 v->derive_adjustment = 0;
4635 v->same = 0;
4636 v->ignore = 0;
4637 v->new_reg = 0;
4638 v->final_value = 0;
4639 v->same_insn = 0;
4640 v->auto_inc_opt = 0;
4641 v->unrolled = 0;
4642 v->shared = 0;
4643
4644 /* The v->always_computable field is used in update_giv_derive, to
4645 determine whether a giv can be used to derive another giv. For a
4646 DEST_REG giv, INSN computes a new value for the giv, so its value
4647 isn't computable if INSN insn't executed every iteration.
4648 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4649 it does not compute a new value. Hence the value is always computable
4650 regardless of whether INSN is executed each iteration. */
4651
4652 if (type == DEST_ADDR)
4653 v->always_computable = 1;
4654 else
4655 v->always_computable = ! not_every_iteration;
4656
4657 v->always_executed = ! not_every_iteration;
4658
4659 if (type == DEST_ADDR)
4660 {
4661 v->mode = GET_MODE (*location);
4662 v->lifetime = 1;
4663 v->times_used = 1;
4664 }
4665 else /* type == DEST_REG */
4666 {
4667 v->mode = GET_MODE (SET_DEST (set));
4668
4669 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
4670 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
4671
4672 v->times_used = n_times_used[REGNO (dest_reg)];
4673
4674 /* If the lifetime is zero, it means that this register is
4675 really a dead store. So mark this as a giv that can be
4676 ignored. This will not prevent the biv from being eliminated. */
4677 if (v->lifetime == 0)
4678 v->ignore = 1;
4679
4680 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
4681 reg_iv_info[REGNO (dest_reg)] = v;
4682 }
4683
4684 /* Add the giv to the class of givs computed from one biv. */
4685
4686 bl = reg_biv_class[REGNO (src_reg)];
4687 if (bl)
4688 {
4689 v->next_iv = bl->giv;
4690 bl->giv = v;
4691 /* Don't count DEST_ADDR. This is supposed to count the number of
4692 insns that calculate givs. */
4693 if (type == DEST_REG)
4694 bl->giv_count++;
4695 bl->total_benefit += benefit;
4696 }
4697 else
4698 /* Fatal error, biv missing for this giv? */
4699 abort ();
4700
4701 if (type == DEST_ADDR)
4702 v->replaceable = 1;
4703 else
4704 {
4705 /* The giv can be replaced outright by the reduced register only if all
4706 of the following conditions are true:
4707 - the insn that sets the giv is always executed on any iteration
4708 on which the giv is used at all
4709 (there are two ways to deduce this:
4710 either the insn is executed on every iteration,
4711 or all uses follow that insn in the same basic block),
4712 - the giv is not used outside the loop
4713 - no assignments to the biv occur during the giv's lifetime. */
4714
4715 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4716 /* Previous line always fails if INSN was moved by loop opt. */
4717 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
4718 && (! not_every_iteration
4719 || last_use_this_basic_block (dest_reg, insn)))
4720 {
4721 /* Now check that there are no assignments to the biv within the
4722 giv's lifetime. This requires two separate checks. */
4723
4724 /* Check each biv update, and fail if any are between the first
4725 and last use of the giv.
4726
4727 If this loop contains an inner loop that was unrolled, then
4728 the insn modifying the biv may have been emitted by the loop
4729 unrolling code, and hence does not have a valid luid. Just
4730 mark the biv as not replaceable in this case. It is not very
4731 useful as a biv, because it is used in two different loops.
4732 It is very unlikely that we would be able to optimize the giv
4733 using this biv anyways. */
4734
4735 v->replaceable = 1;
4736 for (b = bl->biv; b; b = b->next_iv)
4737 {
4738 if (INSN_UID (b->insn) >= max_uid_for_loop
4739 || ((uid_luid[INSN_UID (b->insn)]
4740 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
4741 && (uid_luid[INSN_UID (b->insn)]
4742 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
4743 {
4744 v->replaceable = 0;
4745 v->not_replaceable = 1;
4746 break;
4747 }
4748 }
4749
4750 /* If there are any backwards branches that go from after the
4751 biv update to before it, then this giv is not replaceable. */
4752 if (v->replaceable)
4753 for (b = bl->biv; b; b = b->next_iv)
4754 if (back_branch_in_range_p (b->insn, loop_start, loop_end))
4755 {
4756 v->replaceable = 0;
4757 v->not_replaceable = 1;
4758 break;
4759 }
4760 }
4761 else
4762 {
4763 /* May still be replaceable, we don't have enough info here to
4764 decide. */
4765 v->replaceable = 0;
4766 v->not_replaceable = 0;
4767 }
4768 }
4769
4770 if (loop_dump_stream)
4771 {
4772 if (type == DEST_REG)
4773 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
4774 INSN_UID (insn), REGNO (dest_reg));
4775 else
4776 fprintf (loop_dump_stream, "Insn %d: dest address",
4777 INSN_UID (insn));
4778
4779 fprintf (loop_dump_stream, " src reg %d benefit %d",
4780 REGNO (src_reg), v->benefit);
4781 fprintf (loop_dump_stream, " used %d lifetime %d",
4782 v->times_used, v->lifetime);
4783
4784 if (v->replaceable)
4785 fprintf (loop_dump_stream, " replaceable");
4786
4787 if (GET_CODE (mult_val) == CONST_INT)
4788 fprintf (loop_dump_stream, " mult %d",
4789 INTVAL (mult_val));
4790 else
4791 {
4792 fprintf (loop_dump_stream, " mult ");
4793 print_rtl (loop_dump_stream, mult_val);
4794 }
4795
4796 if (GET_CODE (add_val) == CONST_INT)
4797 fprintf (loop_dump_stream, " add %d",
4798 INTVAL (add_val));
4799 else
4800 {
4801 fprintf (loop_dump_stream, " add ");
4802 print_rtl (loop_dump_stream, add_val);
4803 }
4804 }
4805
4806 if (loop_dump_stream)
4807 fprintf (loop_dump_stream, "\n");
4808
4809 }
4810
4811
4812 /* All this does is determine whether a giv can be made replaceable because
4813 its final value can be calculated. This code can not be part of record_giv
4814 above, because final_giv_value requires that the number of loop iterations
4815 be known, and that can not be accurately calculated until after all givs
4816 have been identified. */
4817
4818 static void
4819 check_final_value (v, loop_start, loop_end)
4820 struct induction *v;
4821 rtx loop_start, loop_end;
4822 {
4823 struct iv_class *bl;
4824 rtx final_value = 0;
4825
4826 bl = reg_biv_class[REGNO (v->src_reg)];
4827
4828 /* DEST_ADDR givs will never reach here, because they are always marked
4829 replaceable above in record_giv. */
4830
4831 /* The giv can be replaced outright by the reduced register only if all
4832 of the following conditions are true:
4833 - the insn that sets the giv is always executed on any iteration
4834 on which the giv is used at all
4835 (there are two ways to deduce this:
4836 either the insn is executed on every iteration,
4837 or all uses follow that insn in the same basic block),
4838 - its final value can be calculated (this condition is different
4839 than the one above in record_giv)
4840 - no assignments to the biv occur during the giv's lifetime. */
4841
4842 #if 0
4843 /* This is only called now when replaceable is known to be false. */
4844 /* Clear replaceable, so that it won't confuse final_giv_value. */
4845 v->replaceable = 0;
4846 #endif
4847
4848 if ((final_value = final_giv_value (v, loop_start, loop_end))
4849 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
4850 {
4851 int biv_increment_seen = 0;
4852 rtx p = v->insn;
4853 rtx last_giv_use;
4854
4855 v->replaceable = 1;
4856
4857 /* When trying to determine whether or not a biv increment occurs
4858 during the lifetime of the giv, we can ignore uses of the variable
4859 outside the loop because final_value is true. Hence we can not
4860 use regno_last_uid and regno_first_uid as above in record_giv. */
4861
4862 /* Search the loop to determine whether any assignments to the
4863 biv occur during the giv's lifetime. Start with the insn
4864 that sets the giv, and search around the loop until we come
4865 back to that insn again.
4866
4867 Also fail if there is a jump within the giv's lifetime that jumps
4868 to somewhere outside the lifetime but still within the loop. This
4869 catches spaghetti code where the execution order is not linear, and
4870 hence the above test fails. Here we assume that the giv lifetime
4871 does not extend from one iteration of the loop to the next, so as
4872 to make the test easier. Since the lifetime isn't known yet,
4873 this requires two loops. See also record_giv above. */
4874
4875 last_giv_use = v->insn;
4876
4877 while (1)
4878 {
4879 p = NEXT_INSN (p);
4880 if (p == loop_end)
4881 p = NEXT_INSN (loop_start);
4882 if (p == v->insn)
4883 break;
4884
4885 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4886 || GET_CODE (p) == CALL_INSN)
4887 {
4888 if (biv_increment_seen)
4889 {
4890 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4891 {
4892 v->replaceable = 0;
4893 v->not_replaceable = 1;
4894 break;
4895 }
4896 }
4897 else if (GET_CODE (PATTERN (p)) == SET
4898 && SET_DEST (PATTERN (p)) == v->src_reg)
4899 biv_increment_seen = 1;
4900 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4901 last_giv_use = p;
4902 }
4903 }
4904
4905 /* Now that the lifetime of the giv is known, check for branches
4906 from within the lifetime to outside the lifetime if it is still
4907 replaceable. */
4908
4909 if (v->replaceable)
4910 {
4911 p = v->insn;
4912 while (1)
4913 {
4914 p = NEXT_INSN (p);
4915 if (p == loop_end)
4916 p = NEXT_INSN (loop_start);
4917 if (p == last_giv_use)
4918 break;
4919
4920 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
4921 && LABEL_NAME (JUMP_LABEL (p))
4922 && ((INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop)
4923 || (INSN_UID (v->insn) >= max_uid_for_loop)
4924 || (INSN_UID (last_giv_use) >= max_uid_for_loop)
4925 || (INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (v->insn)
4926 && INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start))
4927 || (INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (last_giv_use)
4928 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end))))
4929 {
4930 v->replaceable = 0;
4931 v->not_replaceable = 1;
4932
4933 if (loop_dump_stream)
4934 fprintf (loop_dump_stream,
4935 "Found branch outside giv lifetime.\n");
4936
4937 break;
4938 }
4939 }
4940 }
4941
4942 /* If it is replaceable, then save the final value. */
4943 if (v->replaceable)
4944 v->final_value = final_value;
4945 }
4946
4947 if (loop_dump_stream && v->replaceable)
4948 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
4949 INSN_UID (v->insn), REGNO (v->dest_reg));
4950 }
4951 \f
4952 /* Update the status of whether a giv can derive other givs.
4953
4954 We need to do something special if there is or may be an update to the biv
4955 between the time the giv is defined and the time it is used to derive
4956 another giv.
4957
4958 In addition, a giv that is only conditionally set is not allowed to
4959 derive another giv once a label has been passed.
4960
4961 The cases we look at are when a label or an update to a biv is passed. */
4962
4963 static void
4964 update_giv_derive (p)
4965 rtx p;
4966 {
4967 struct iv_class *bl;
4968 struct induction *biv, *giv;
4969 rtx tem;
4970 int dummy;
4971
4972 /* Search all IV classes, then all bivs, and finally all givs.
4973
4974 There are three cases we are concerned with. First we have the situation
4975 of a giv that is only updated conditionally. In that case, it may not
4976 derive any givs after a label is passed.
4977
4978 The second case is when a biv update occurs, or may occur, after the
4979 definition of a giv. For certain biv updates (see below) that are
4980 known to occur between the giv definition and use, we can adjust the
4981 giv definition. For others, or when the biv update is conditional,
4982 we must prevent the giv from deriving any other givs. There are two
4983 sub-cases within this case.
4984
4985 If this is a label, we are concerned with any biv update that is done
4986 conditionally, since it may be done after the giv is defined followed by
4987 a branch here (actually, we need to pass both a jump and a label, but
4988 this extra tracking doesn't seem worth it).
4989
4990 If this is a jump, we are concerned about any biv update that may be
4991 executed multiple times. We are actually only concerned about
4992 backward jumps, but it is probably not worth performing the test
4993 on the jump again here.
4994
4995 If this is a biv update, we must adjust the giv status to show that a
4996 subsequent biv update was performed. If this adjustment cannot be done,
4997 the giv cannot derive further givs. */
4998
4999 for (bl = loop_iv_list; bl; bl = bl->next)
5000 for (biv = bl->biv; biv; biv = biv->next_iv)
5001 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5002 || biv->insn == p)
5003 {
5004 for (giv = bl->giv; giv; giv = giv->next_iv)
5005 {
5006 /* If cant_derive is already true, there is no point in
5007 checking all of these conditions again. */
5008 if (giv->cant_derive)
5009 continue;
5010
5011 /* If this giv is conditionally set and we have passed a label,
5012 it cannot derive anything. */
5013 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5014 giv->cant_derive = 1;
5015
5016 /* Skip givs that have mult_val == 0, since
5017 they are really invariants. Also skip those that are
5018 replaceable, since we know their lifetime doesn't contain
5019 any biv update. */
5020 else if (giv->mult_val == const0_rtx || giv->replaceable)
5021 continue;
5022
5023 /* The only way we can allow this giv to derive another
5024 is if this is a biv increment and we can form the product
5025 of biv->add_val and giv->mult_val. In this case, we will
5026 be able to compute a compensation. */
5027 else if (biv->insn == p)
5028 {
5029 tem = 0;
5030
5031 if (biv->mult_val == const1_rtx)
5032 tem = simplify_giv_expr (gen_rtx (MULT, giv->mode,
5033 biv->add_val,
5034 giv->mult_val),
5035 &dummy);
5036
5037 if (tem && giv->derive_adjustment)
5038 tem = simplify_giv_expr (gen_rtx (PLUS, giv->mode, tem,
5039 giv->derive_adjustment),
5040 &dummy);
5041 if (tem)
5042 giv->derive_adjustment = tem;
5043 else
5044 giv->cant_derive = 1;
5045 }
5046 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5047 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5048 giv->cant_derive = 1;
5049 }
5050 }
5051 }
5052 \f
5053 /* Check whether an insn is an increment legitimate for a basic induction var.
5054 X is the source of insn P, or a part of it.
5055 MODE is the mode in which X should be interpreted.
5056
5057 DEST_REG is the putative biv, also the destination of the insn.
5058 We accept patterns of these forms:
5059 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5060 REG = INVARIANT + REG
5061
5062 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5063 and store the additive term into *INC_VAL.
5064
5065 If X is an assignment of an invariant into DEST_REG, we set
5066 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5067
5068 We also want to detect a BIV when it corresponds to a variable
5069 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5070 of the variable may be a PLUS that adds a SUBREG of that variable to
5071 an invariant and then sign- or zero-extends the result of the PLUS
5072 into the variable.
5073
5074 Most GIVs in such cases will be in the promoted mode, since that is the
5075 probably the natural computation mode (and almost certainly the mode
5076 used for addresses) on the machine. So we view the pseudo-reg containing
5077 the variable as the BIV, as if it were simply incremented.
5078
5079 Note that treating the entire pseudo as a BIV will result in making
5080 simple increments to any GIVs based on it. However, if the variable
5081 overflows in its declared mode but not its promoted mode, the result will
5082 be incorrect. This is acceptable if the variable is signed, since
5083 overflows in such cases are undefined, but not if it is unsigned, since
5084 those overflows are defined. So we only check for SIGN_EXTEND and
5085 not ZERO_EXTEND.
5086
5087 If we cannot find a biv, we return 0. */
5088
5089 static int
5090 basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val)
5091 register rtx x;
5092 enum machine_mode mode;
5093 rtx p;
5094 rtx dest_reg;
5095 rtx *inc_val;
5096 rtx *mult_val;
5097 {
5098 register enum rtx_code code;
5099 rtx arg;
5100 rtx insn, set = 0;
5101
5102 code = GET_CODE (x);
5103 switch (code)
5104 {
5105 case PLUS:
5106 if (XEXP (x, 0) == dest_reg
5107 || (GET_CODE (XEXP (x, 0)) == SUBREG
5108 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5109 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5110 arg = XEXP (x, 1);
5111 else if (XEXP (x, 1) == dest_reg
5112 || (GET_CODE (XEXP (x, 1)) == SUBREG
5113 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5114 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5115 arg = XEXP (x, 0);
5116 else
5117 return 0;
5118
5119 if (invariant_p (arg) != 1)
5120 return 0;
5121
5122 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5123 *mult_val = const1_rtx;
5124 return 1;
5125
5126 case SUBREG:
5127 /* If this is a SUBREG for a promoted variable, check the inner
5128 value. */
5129 if (SUBREG_PROMOTED_VAR_P (x))
5130 return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
5131 dest_reg, p, inc_val, mult_val);
5132 return 0;
5133
5134 case REG:
5135 /* If this register is assigned in the previous insn, look at its
5136 source, but don't go outside the loop or past a label. */
5137
5138 for (insn = PREV_INSN (p);
5139 (insn && GET_CODE (insn) == NOTE
5140 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5141 insn = PREV_INSN (insn))
5142 ;
5143
5144 if (insn)
5145 set = single_set (insn);
5146
5147 if (set != 0
5148 && (SET_DEST (set) == x
5149 || (GET_CODE (SET_DEST (set)) == SUBREG
5150 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5151 <= UNITS_PER_WORD)
5152 && SUBREG_REG (SET_DEST (set)) == x)))
5153 return basic_induction_var (SET_SRC (set),
5154 (GET_MODE (SET_SRC (set)) == VOIDmode
5155 ? GET_MODE (x)
5156 : GET_MODE (SET_SRC (set))),
5157 dest_reg, insn,
5158 inc_val, mult_val);
5159 /* ... fall through ... */
5160
5161 /* Can accept constant setting of biv only when inside inner most loop.
5162 Otherwise, a biv of an inner loop may be incorrectly recognized
5163 as a biv of the outer loop,
5164 causing code to be moved INTO the inner loop. */
5165 case MEM:
5166 if (invariant_p (x) != 1)
5167 return 0;
5168 case CONST_INT:
5169 case SYMBOL_REF:
5170 case CONST:
5171 if (loops_enclosed == 1)
5172 {
5173 /* Possible bug here? Perhaps we don't know the mode of X. */
5174 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5175 *mult_val = const0_rtx;
5176 return 1;
5177 }
5178 else
5179 return 0;
5180
5181 case SIGN_EXTEND:
5182 return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5183 dest_reg, p, inc_val, mult_val);
5184 case ASHIFTRT:
5185 /* Similar, since this can be a sign extension. */
5186 for (insn = PREV_INSN (p);
5187 (insn && GET_CODE (insn) == NOTE
5188 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5189 insn = PREV_INSN (insn))
5190 ;
5191
5192 if (insn)
5193 set = single_set (insn);
5194
5195 if (set && SET_DEST (set) == XEXP (x, 0)
5196 && GET_CODE (XEXP (x, 1)) == CONST_INT
5197 && INTVAL (XEXP (x, 1)) >= 0
5198 && GET_CODE (SET_SRC (set)) == ASHIFT
5199 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5200 return basic_induction_var (XEXP (SET_SRC (set), 0),
5201 GET_MODE (XEXP (x, 0)),
5202 dest_reg, insn, inc_val, mult_val);
5203 return 0;
5204
5205 default:
5206 return 0;
5207 }
5208 }
5209 \f
5210 /* A general induction variable (giv) is any quantity that is a linear
5211 function of a basic induction variable,
5212 i.e. giv = biv * mult_val + add_val.
5213 The coefficients can be any loop invariant quantity.
5214 A giv need not be computed directly from the biv;
5215 it can be computed by way of other givs. */
5216
5217 /* Determine whether X computes a giv.
5218 If it does, return a nonzero value
5219 which is the benefit from eliminating the computation of X;
5220 set *SRC_REG to the register of the biv that it is computed from;
5221 set *ADD_VAL and *MULT_VAL to the coefficients,
5222 such that the value of X is biv * mult + add; */
5223
5224 static int
5225 general_induction_var (x, src_reg, add_val, mult_val)
5226 rtx x;
5227 rtx *src_reg;
5228 rtx *add_val;
5229 rtx *mult_val;
5230 {
5231 rtx orig_x = x;
5232 int benefit = 0;
5233 char *storage;
5234
5235 /* If this is an invariant, forget it, it isn't a giv. */
5236 if (invariant_p (x) == 1)
5237 return 0;
5238
5239 /* See if the expression could be a giv and get its form.
5240 Mark our place on the obstack in case we don't find a giv. */
5241 storage = (char *) oballoc (0);
5242 x = simplify_giv_expr (x, &benefit);
5243 if (x == 0)
5244 {
5245 obfree (storage);
5246 return 0;
5247 }
5248
5249 switch (GET_CODE (x))
5250 {
5251 case USE:
5252 case CONST_INT:
5253 /* Since this is now an invariant and wasn't before, it must be a giv
5254 with MULT_VAL == 0. It doesn't matter which BIV we associate this
5255 with. */
5256 *src_reg = loop_iv_list->biv->dest_reg;
5257 *mult_val = const0_rtx;
5258 *add_val = x;
5259 break;
5260
5261 case REG:
5262 /* This is equivalent to a BIV. */
5263 *src_reg = x;
5264 *mult_val = const1_rtx;
5265 *add_val = const0_rtx;
5266 break;
5267
5268 case PLUS:
5269 /* Either (plus (biv) (invar)) or
5270 (plus (mult (biv) (invar_1)) (invar_2)). */
5271 if (GET_CODE (XEXP (x, 0)) == MULT)
5272 {
5273 *src_reg = XEXP (XEXP (x, 0), 0);
5274 *mult_val = XEXP (XEXP (x, 0), 1);
5275 }
5276 else
5277 {
5278 *src_reg = XEXP (x, 0);
5279 *mult_val = const1_rtx;
5280 }
5281 *add_val = XEXP (x, 1);
5282 break;
5283
5284 case MULT:
5285 /* ADD_VAL is zero. */
5286 *src_reg = XEXP (x, 0);
5287 *mult_val = XEXP (x, 1);
5288 *add_val = const0_rtx;
5289 break;
5290
5291 default:
5292 abort ();
5293 }
5294
5295 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5296 unless they are CONST_INT). */
5297 if (GET_CODE (*add_val) == USE)
5298 *add_val = XEXP (*add_val, 0);
5299 if (GET_CODE (*mult_val) == USE)
5300 *mult_val = XEXP (*mult_val, 0);
5301
5302 benefit += rtx_cost (orig_x, SET);
5303
5304 /* Always return some benefit if this is a giv so it will be detected
5305 as such. This allows elimination of bivs that might otherwise
5306 not be eliminated. */
5307 return benefit == 0 ? 1 : benefit;
5308 }
5309 \f
5310 /* Given an expression, X, try to form it as a linear function of a biv.
5311 We will canonicalize it to be of the form
5312 (plus (mult (BIV) (invar_1))
5313 (invar_2))
5314 with possible degeneracies.
5315
5316 The invariant expressions must each be of a form that can be used as a
5317 machine operand. We surround then with a USE rtx (a hack, but localized
5318 and certainly unambiguous!) if not a CONST_INT for simplicity in this
5319 routine; it is the caller's responsibility to strip them.
5320
5321 If no such canonicalization is possible (i.e., two biv's are used or an
5322 expression that is neither invariant nor a biv or giv), this routine
5323 returns 0.
5324
5325 For a non-zero return, the result will have a code of CONST_INT, USE,
5326 REG (for a BIV), PLUS, or MULT. No other codes will occur.
5327
5328 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
5329
5330 static rtx
5331 simplify_giv_expr (x, benefit)
5332 rtx x;
5333 int *benefit;
5334 {
5335 enum machine_mode mode = GET_MODE (x);
5336 rtx arg0, arg1;
5337 rtx tem;
5338
5339 /* If this is not an integer mode, or if we cannot do arithmetic in this
5340 mode, this can't be a giv. */
5341 if (mode != VOIDmode
5342 && (GET_MODE_CLASS (mode) != MODE_INT
5343 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5344 return 0;
5345
5346 switch (GET_CODE (x))
5347 {
5348 case PLUS:
5349 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5350 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5351 if (arg0 == 0 || arg1 == 0)
5352 return 0;
5353
5354 /* Put constant last, CONST_INT last if both constant. */
5355 if ((GET_CODE (arg0) == USE
5356 || GET_CODE (arg0) == CONST_INT)
5357 && GET_CODE (arg1) != CONST_INT)
5358 tem = arg0, arg0 = arg1, arg1 = tem;
5359
5360 /* Handle addition of zero, then addition of an invariant. */
5361 if (arg1 == const0_rtx)
5362 return arg0;
5363 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5364 switch (GET_CODE (arg0))
5365 {
5366 case CONST_INT:
5367 case USE:
5368 /* Both invariant. Only valid if sum is machine operand.
5369 First strip off possible USE on first operand. */
5370 if (GET_CODE (arg0) == USE)
5371 arg0 = XEXP (arg0, 0);
5372
5373 tem = 0;
5374 if (CONSTANT_P (arg0) && GET_CODE (arg1) == CONST_INT)
5375 {
5376 tem = plus_constant (arg0, INTVAL (arg1));
5377 if (GET_CODE (tem) != CONST_INT)
5378 tem = gen_rtx (USE, mode, tem);
5379 }
5380
5381 return tem;
5382
5383 case REG:
5384 case MULT:
5385 /* biv + invar or mult + invar. Return sum. */
5386 return gen_rtx (PLUS, mode, arg0, arg1);
5387
5388 case PLUS:
5389 /* (a + invar_1) + invar_2. Associate. */
5390 return simplify_giv_expr (gen_rtx (PLUS, mode,
5391 XEXP (arg0, 0),
5392 gen_rtx (PLUS, mode,
5393 XEXP (arg0, 1), arg1)),
5394 benefit);
5395
5396 default:
5397 abort ();
5398 }
5399
5400 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5401 MULT to reduce cases. */
5402 if (GET_CODE (arg0) == REG)
5403 arg0 = gen_rtx (MULT, mode, arg0, const1_rtx);
5404 if (GET_CODE (arg1) == REG)
5405 arg1 = gen_rtx (MULT, mode, arg1, const1_rtx);
5406
5407 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5408 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5409 Recurse to associate the second PLUS. */
5410 if (GET_CODE (arg1) == MULT)
5411 tem = arg0, arg0 = arg1, arg1 = tem;
5412
5413 if (GET_CODE (arg1) == PLUS)
5414 return simplify_giv_expr (gen_rtx (PLUS, mode,
5415 gen_rtx (PLUS, mode,
5416 arg0, XEXP (arg1, 0)),
5417 XEXP (arg1, 1)),
5418 benefit);
5419
5420 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5421 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5422 abort ();
5423
5424 if (XEXP (arg0, 0) != XEXP (arg1, 0))
5425 return 0;
5426
5427 return simplify_giv_expr (gen_rtx (MULT, mode,
5428 XEXP (arg0, 0),
5429 gen_rtx (PLUS, mode,
5430 XEXP (arg0, 1),
5431 XEXP (arg1, 1))),
5432 benefit);
5433
5434 case MINUS:
5435 /* Handle "a - b" as "a + b * (-1)". */
5436 return simplify_giv_expr (gen_rtx (PLUS, mode,
5437 XEXP (x, 0),
5438 gen_rtx (MULT, mode,
5439 XEXP (x, 1), constm1_rtx)),
5440 benefit);
5441
5442 case MULT:
5443 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5444 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5445 if (arg0 == 0 || arg1 == 0)
5446 return 0;
5447
5448 /* Put constant last, CONST_INT last if both constant. */
5449 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5450 && GET_CODE (arg1) != CONST_INT)
5451 tem = arg0, arg0 = arg1, arg1 = tem;
5452
5453 /* If second argument is not now constant, not giv. */
5454 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5455 return 0;
5456
5457 /* Handle multiply by 0 or 1. */
5458 if (arg1 == const0_rtx)
5459 return const0_rtx;
5460
5461 else if (arg1 == const1_rtx)
5462 return arg0;
5463
5464 switch (GET_CODE (arg0))
5465 {
5466 case REG:
5467 /* biv * invar. Done. */
5468 return gen_rtx (MULT, mode, arg0, arg1);
5469
5470 case CONST_INT:
5471 /* Product of two constants. */
5472 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5473
5474 case USE:
5475 /* invar * invar. Not giv. */
5476 return 0;
5477
5478 case MULT:
5479 /* (a * invar_1) * invar_2. Associate. */
5480 return simplify_giv_expr (gen_rtx (MULT, mode,
5481 XEXP (arg0, 0),
5482 gen_rtx (MULT, mode,
5483 XEXP (arg0, 1), arg1)),
5484 benefit);
5485
5486 case PLUS:
5487 /* (a + invar_1) * invar_2. Distribute. */
5488 return simplify_giv_expr (gen_rtx (PLUS, mode,
5489 gen_rtx (MULT, mode,
5490 XEXP (arg0, 0), arg1),
5491 gen_rtx (MULT, mode,
5492 XEXP (arg0, 1), arg1)),
5493 benefit);
5494
5495 default:
5496 abort ();
5497 }
5498
5499 case ASHIFT:
5500 /* Shift by constant is multiply by power of two. */
5501 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5502 return 0;
5503
5504 return simplify_giv_expr (gen_rtx (MULT, mode,
5505 XEXP (x, 0),
5506 GEN_INT ((HOST_WIDE_INT) 1
5507 << INTVAL (XEXP (x, 1)))),
5508 benefit);
5509
5510 case NEG:
5511 /* "-a" is "a * (-1)" */
5512 return simplify_giv_expr (gen_rtx (MULT, mode, XEXP (x, 0), constm1_rtx),
5513 benefit);
5514
5515 case NOT:
5516 /* "~a" is "-a - 1". Silly, but easy. */
5517 return simplify_giv_expr (gen_rtx (MINUS, mode,
5518 gen_rtx (NEG, mode, XEXP (x, 0)),
5519 const1_rtx),
5520 benefit);
5521
5522 case USE:
5523 /* Already in proper form for invariant. */
5524 return x;
5525
5526 case REG:
5527 /* If this is a new register, we can't deal with it. */
5528 if (REGNO (x) >= max_reg_before_loop)
5529 return 0;
5530
5531 /* Check for biv or giv. */
5532 switch (reg_iv_type[REGNO (x)])
5533 {
5534 case BASIC_INDUCT:
5535 return x;
5536 case GENERAL_INDUCT:
5537 {
5538 struct induction *v = reg_iv_info[REGNO (x)];
5539
5540 /* Form expression from giv and add benefit. Ensure this giv
5541 can derive another and subtract any needed adjustment if so. */
5542 *benefit += v->benefit;
5543 if (v->cant_derive)
5544 return 0;
5545
5546 tem = gen_rtx (PLUS, mode, gen_rtx (MULT, mode,
5547 v->src_reg, v->mult_val),
5548 v->add_val);
5549 if (v->derive_adjustment)
5550 tem = gen_rtx (MINUS, mode, tem, v->derive_adjustment);
5551 return simplify_giv_expr (tem, benefit);
5552 }
5553 }
5554
5555 /* Fall through to general case. */
5556 default:
5557 /* If invariant, return as USE (unless CONST_INT).
5558 Otherwise, not giv. */
5559 if (GET_CODE (x) == USE)
5560 x = XEXP (x, 0);
5561
5562 if (invariant_p (x) == 1)
5563 {
5564 if (GET_CODE (x) == CONST_INT)
5565 return x;
5566 else
5567 return gen_rtx (USE, mode, x);
5568 }
5569 else
5570 return 0;
5571 }
5572 }
5573 \f
5574 /* Help detect a giv that is calculated by several consecutive insns;
5575 for example,
5576 giv = biv * M
5577 giv = giv + A
5578 The caller has already identified the first insn P as having a giv as dest;
5579 we check that all other insns that set the same register follow
5580 immediately after P, that they alter nothing else,
5581 and that the result of the last is still a giv.
5582
5583 The value is 0 if the reg set in P is not really a giv.
5584 Otherwise, the value is the amount gained by eliminating
5585 all the consecutive insns that compute the value.
5586
5587 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5588 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5589
5590 The coefficients of the ultimate giv value are stored in
5591 *MULT_VAL and *ADD_VAL. */
5592
5593 static int
5594 consec_sets_giv (first_benefit, p, src_reg, dest_reg,
5595 add_val, mult_val)
5596 int first_benefit;
5597 rtx p;
5598 rtx src_reg;
5599 rtx dest_reg;
5600 rtx *add_val;
5601 rtx *mult_val;
5602 {
5603 int count;
5604 enum rtx_code code;
5605 int benefit;
5606 rtx temp;
5607 rtx set;
5608
5609 /* Indicate that this is a giv so that we can update the value produced in
5610 each insn of the multi-insn sequence.
5611
5612 This induction structure will be used only by the call to
5613 general_induction_var below, so we can allocate it on our stack.
5614 If this is a giv, our caller will replace the induct var entry with
5615 a new induction structure. */
5616 struct induction *v
5617 = (struct induction *) alloca (sizeof (struct induction));
5618 v->src_reg = src_reg;
5619 v->mult_val = *mult_val;
5620 v->add_val = *add_val;
5621 v->benefit = first_benefit;
5622 v->cant_derive = 0;
5623 v->derive_adjustment = 0;
5624
5625 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
5626 reg_iv_info[REGNO (dest_reg)] = v;
5627
5628 count = n_times_set[REGNO (dest_reg)] - 1;
5629
5630 while (count > 0)
5631 {
5632 p = NEXT_INSN (p);
5633 code = GET_CODE (p);
5634
5635 /* If libcall, skip to end of call sequence. */
5636 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
5637 p = XEXP (temp, 0);
5638
5639 if (code == INSN
5640 && (set = single_set (p))
5641 && GET_CODE (SET_DEST (set)) == REG
5642 && SET_DEST (set) == dest_reg
5643 && ((benefit = general_induction_var (SET_SRC (set), &src_reg,
5644 add_val, mult_val))
5645 /* Giv created by equivalent expression. */
5646 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
5647 && (benefit = general_induction_var (XEXP (temp, 0), &src_reg,
5648 add_val, mult_val))))
5649 && src_reg == v->src_reg)
5650 {
5651 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5652 benefit += libcall_benefit (p);
5653
5654 count--;
5655 v->mult_val = *mult_val;
5656 v->add_val = *add_val;
5657 v->benefit = benefit;
5658 }
5659 else if (code != NOTE)
5660 {
5661 /* Allow insns that set something other than this giv to a
5662 constant. Such insns are needed on machines which cannot
5663 include long constants and should not disqualify a giv. */
5664 if (code == INSN
5665 && (set = single_set (p))
5666 && SET_DEST (set) != dest_reg
5667 && CONSTANT_P (SET_SRC (set)))
5668 continue;
5669
5670 reg_iv_type[REGNO (dest_reg)] = UNKNOWN_INDUCT;
5671 return 0;
5672 }
5673 }
5674
5675 return v->benefit;
5676 }
5677 \f
5678 /* Return an rtx, if any, that expresses giv G2 as a function of the register
5679 represented by G1. If no such expression can be found, or it is clear that
5680 it cannot possibly be a valid address, 0 is returned.
5681
5682 To perform the computation, we note that
5683 G1 = a * v + b and
5684 G2 = c * v + d
5685 where `v' is the biv.
5686
5687 So G2 = (c/a) * G1 + (d - b*c/a) */
5688
5689 #ifdef ADDRESS_COST
5690 static rtx
5691 express_from (g1, g2)
5692 struct induction *g1, *g2;
5693 {
5694 rtx mult, add;
5695
5696 /* The value that G1 will be multiplied by must be a constant integer. Also,
5697 the only chance we have of getting a valid address is if b*c/a (see above
5698 for notation) is also an integer. */
5699 if (GET_CODE (g1->mult_val) != CONST_INT
5700 || GET_CODE (g2->mult_val) != CONST_INT
5701 || GET_CODE (g1->add_val) != CONST_INT
5702 || g1->mult_val == const0_rtx
5703 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
5704 return 0;
5705
5706 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
5707 add = plus_constant (g2->add_val, - INTVAL (g1->add_val) * INTVAL (mult));
5708
5709 /* Form simplified final result. */
5710 if (mult == const0_rtx)
5711 return add;
5712 else if (mult == const1_rtx)
5713 mult = g1->dest_reg;
5714 else
5715 mult = gen_rtx (MULT, g2->mode, g1->dest_reg, mult);
5716
5717 if (add == const0_rtx)
5718 return mult;
5719 else
5720 return gen_rtx (PLUS, g2->mode, mult, add);
5721 }
5722 #endif
5723 \f
5724 /* Return 1 if giv G2 can be combined with G1. This means that G2 can use
5725 (either directly or via an address expression) a register used to represent
5726 G1. Set g2->new_reg to a represtation of G1 (normally just
5727 g1->dest_reg). */
5728
5729 static int
5730 combine_givs_p (g1, g2)
5731 struct induction *g1, *g2;
5732 {
5733 rtx tem;
5734
5735 /* If these givs are identical, they can be combined. */
5736 if (rtx_equal_p (g1->mult_val, g2->mult_val)
5737 && rtx_equal_p (g1->add_val, g2->add_val))
5738 {
5739 g2->new_reg = g1->dest_reg;
5740 return 1;
5741 }
5742
5743 #ifdef ADDRESS_COST
5744 /* If G2 can be expressed as a function of G1 and that function is valid
5745 as an address and no more expensive than using a register for G2,
5746 the expression of G2 in terms of G1 can be used. */
5747 if (g2->giv_type == DEST_ADDR
5748 && (tem = express_from (g1, g2)) != 0
5749 && memory_address_p (g2->mem_mode, tem)
5750 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location))
5751 {
5752 g2->new_reg = tem;
5753 return 1;
5754 }
5755 #endif
5756
5757 return 0;
5758 }
5759 \f
5760 #ifdef GIV_SORT_CRITERION
5761 /* Compare two givs and sort the most desirable one for combinations first.
5762 This is used only in one qsort call below. */
5763
5764 static int
5765 giv_sort (x, y)
5766 struct induction **x, **y;
5767 {
5768 GIV_SORT_CRITERION (*x, *y);
5769
5770 return 0;
5771 }
5772 #endif
5773
5774 /* Check all pairs of givs for iv_class BL and see if any can be combined with
5775 any other. If so, point SAME to the giv combined with and set NEW_REG to
5776 be an expression (in terms of the other giv's DEST_REG) equivalent to the
5777 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
5778
5779 static void
5780 combine_givs (bl)
5781 struct iv_class *bl;
5782 {
5783 struct induction *g1, *g2, **giv_array, *temp_iv;
5784 int i, j, giv_count, pass;
5785
5786 /* Count givs, because bl->giv_count is incorrect here. */
5787 giv_count = 0;
5788 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5789 giv_count++;
5790
5791 giv_array
5792 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
5793 i = 0;
5794 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5795 giv_array[i++] = g1;
5796
5797 #ifdef GIV_SORT_CRITERION
5798 /* Sort the givs if GIV_SORT_CRITERION is defined.
5799 This is usually defined for processors which lack
5800 negative register offsets so more givs may be combined. */
5801
5802 if (loop_dump_stream)
5803 fprintf (loop_dump_stream, "%d givs counted, sorting...\n", giv_count);
5804
5805 qsort (giv_array, giv_count, sizeof (struct induction *), giv_sort);
5806 #endif
5807
5808 for (i = 0; i < giv_count; i++)
5809 {
5810 g1 = giv_array[i];
5811 for (pass = 0; pass <= 1; pass++)
5812 for (j = 0; j < giv_count; j++)
5813 {
5814 g2 = giv_array[j];
5815 if (g1 != g2
5816 /* First try to combine with replaceable givs, then all givs. */
5817 && (g1->replaceable || pass == 1)
5818 /* If either has already been combined or is to be ignored, can't
5819 combine. */
5820 && ! g1->ignore && ! g2->ignore && ! g1->same && ! g2->same
5821 /* If something has been based on G2, G2 cannot itself be based
5822 on something else. */
5823 && ! g2->combined_with
5824 && combine_givs_p (g1, g2))
5825 {
5826 /* g2->new_reg set by `combine_givs_p' */
5827 g2->same = g1;
5828 g1->combined_with = 1;
5829
5830 /* If one of these givs is a DEST_REG that was only used
5831 once, by the other giv, this is actually a single use.
5832 The DEST_REG has the correct cost, while the other giv
5833 counts the REG use too often. */
5834 if (g2->giv_type == DEST_REG
5835 && n_times_used[REGNO (g2->dest_reg)] == 1
5836 && reg_mentioned_p (g2->dest_reg, PATTERN (g1->insn)))
5837 g1->benefit = g2->benefit;
5838 else if (g1->giv_type != DEST_REG
5839 || n_times_used[REGNO (g1->dest_reg)] != 1
5840 || ! reg_mentioned_p (g1->dest_reg,
5841 PATTERN (g2->insn)))
5842 {
5843 g1->benefit += g2->benefit;
5844 g1->times_used += g2->times_used;
5845 }
5846 /* ??? The new final_[bg]iv_value code does a much better job
5847 of finding replaceable giv's, and hence this code may no
5848 longer be necessary. */
5849 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
5850 g1->benefit -= copy_cost;
5851 g1->lifetime += g2->lifetime;
5852
5853 if (loop_dump_stream)
5854 fprintf (loop_dump_stream, "giv at %d combined with giv at %d\n",
5855 INSN_UID (g2->insn), INSN_UID (g1->insn));
5856 }
5857 }
5858 }
5859 }
5860 \f
5861 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
5862
5863 void
5864 emit_iv_add_mult (b, m, a, reg, insert_before)
5865 rtx b; /* initial value of basic induction variable */
5866 rtx m; /* multiplicative constant */
5867 rtx a; /* additive constant */
5868 rtx reg; /* destination register */
5869 rtx insert_before;
5870 {
5871 rtx seq;
5872 rtx result;
5873
5874 /* Prevent unexpected sharing of these rtx. */
5875 a = copy_rtx (a);
5876 b = copy_rtx (b);
5877
5878 /* Increase the lifetime of any invariants moved further in code. */
5879 update_reg_last_use (a, insert_before);
5880 update_reg_last_use (b, insert_before);
5881 update_reg_last_use (m, insert_before);
5882
5883 start_sequence ();
5884 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
5885 if (reg != result)
5886 emit_move_insn (reg, result);
5887 seq = gen_sequence ();
5888 end_sequence ();
5889
5890 emit_insn_before (seq, insert_before);
5891
5892 record_base_value (REGNO (reg), b);
5893 }
5894 \f
5895 /* Test whether A * B can be computed without
5896 an actual multiply insn. Value is 1 if so. */
5897
5898 static int
5899 product_cheap_p (a, b)
5900 rtx a;
5901 rtx b;
5902 {
5903 int i;
5904 rtx tmp;
5905 struct obstack *old_rtl_obstack = rtl_obstack;
5906 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
5907 int win = 1;
5908
5909 /* If only one is constant, make it B. */
5910 if (GET_CODE (a) == CONST_INT)
5911 tmp = a, a = b, b = tmp;
5912
5913 /* If first constant, both constant, so don't need multiply. */
5914 if (GET_CODE (a) == CONST_INT)
5915 return 1;
5916
5917 /* If second not constant, neither is constant, so would need multiply. */
5918 if (GET_CODE (b) != CONST_INT)
5919 return 0;
5920
5921 /* One operand is constant, so might not need multiply insn. Generate the
5922 code for the multiply and see if a call or multiply, or long sequence
5923 of insns is generated. */
5924
5925 rtl_obstack = &temp_obstack;
5926 start_sequence ();
5927 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
5928 tmp = gen_sequence ();
5929 end_sequence ();
5930
5931 if (GET_CODE (tmp) == SEQUENCE)
5932 {
5933 if (XVEC (tmp, 0) == 0)
5934 win = 1;
5935 else if (XVECLEN (tmp, 0) > 3)
5936 win = 0;
5937 else
5938 for (i = 0; i < XVECLEN (tmp, 0); i++)
5939 {
5940 rtx insn = XVECEXP (tmp, 0, i);
5941
5942 if (GET_CODE (insn) != INSN
5943 || (GET_CODE (PATTERN (insn)) == SET
5944 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
5945 || (GET_CODE (PATTERN (insn)) == PARALLEL
5946 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
5947 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
5948 {
5949 win = 0;
5950 break;
5951 }
5952 }
5953 }
5954 else if (GET_CODE (tmp) == SET
5955 && GET_CODE (SET_SRC (tmp)) == MULT)
5956 win = 0;
5957 else if (GET_CODE (tmp) == PARALLEL
5958 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
5959 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
5960 win = 0;
5961
5962 /* Free any storage we obtained in generating this multiply and restore rtl
5963 allocation to its normal obstack. */
5964 obstack_free (&temp_obstack, storage);
5965 rtl_obstack = old_rtl_obstack;
5966
5967 return win;
5968 }
5969 \f
5970 /* Check to see if loop can be terminated by a "decrement and branch until
5971 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
5972 Also try reversing an increment loop to a decrement loop
5973 to see if the optimization can be performed.
5974 Value is nonzero if optimization was performed. */
5975
5976 /* This is useful even if the architecture doesn't have such an insn,
5977 because it might change a loops which increments from 0 to n to a loop
5978 which decrements from n to 0. A loop that decrements to zero is usually
5979 faster than one that increments from zero. */
5980
5981 /* ??? This could be rewritten to use some of the loop unrolling procedures,
5982 such as approx_final_value, biv_total_increment, loop_iterations, and
5983 final_[bg]iv_value. */
5984
5985 static int
5986 check_dbra_loop (loop_end, insn_count, loop_start)
5987 rtx loop_end;
5988 int insn_count;
5989 rtx loop_start;
5990 {
5991 struct iv_class *bl;
5992 rtx reg;
5993 rtx jump_label;
5994 rtx final_value;
5995 rtx start_value;
5996 rtx new_add_val;
5997 rtx comparison;
5998 rtx before_comparison;
5999 rtx p;
6000
6001 /* If last insn is a conditional branch, and the insn before tests a
6002 register value, try to optimize it. Otherwise, we can't do anything. */
6003
6004 comparison = get_condition_for_loop (PREV_INSN (loop_end));
6005 if (comparison == 0)
6006 return 0;
6007
6008 /* Check all of the bivs to see if the compare uses one of them.
6009 Skip biv's set more than once because we can't guarantee that
6010 it will be zero on the last iteration. Also skip if the biv is
6011 used between its update and the test insn. */
6012
6013 for (bl = loop_iv_list; bl; bl = bl->next)
6014 {
6015 if (bl->biv_count == 1
6016 && bl->biv->dest_reg == XEXP (comparison, 0)
6017 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
6018 PREV_INSN (PREV_INSN (loop_end))))
6019 break;
6020 }
6021
6022 if (! bl)
6023 return 0;
6024
6025 /* Look for the case where the basic induction variable is always
6026 nonnegative, and equals zero on the last iteration.
6027 In this case, add a reg_note REG_NONNEG, which allows the
6028 m68k DBRA instruction to be used. */
6029
6030 if (((GET_CODE (comparison) == GT
6031 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6032 && INTVAL (XEXP (comparison, 1)) == -1)
6033 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
6034 && GET_CODE (bl->biv->add_val) == CONST_INT
6035 && INTVAL (bl->biv->add_val) < 0)
6036 {
6037 /* Initial value must be greater than 0,
6038 init_val % -dec_value == 0 to ensure that it equals zero on
6039 the last iteration */
6040
6041 if (GET_CODE (bl->initial_value) == CONST_INT
6042 && INTVAL (bl->initial_value) > 0
6043 && (INTVAL (bl->initial_value)
6044 % (-INTVAL (bl->biv->add_val))) == 0)
6045 {
6046 /* register always nonnegative, add REG_NOTE to branch */
6047 REG_NOTES (PREV_INSN (loop_end))
6048 = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6049 REG_NOTES (PREV_INSN (loop_end)));
6050 bl->nonneg = 1;
6051
6052 return 1;
6053 }
6054
6055 /* If the decrement is 1 and the value was tested as >= 0 before
6056 the loop, then we can safely optimize. */
6057 for (p = loop_start; p; p = PREV_INSN (p))
6058 {
6059 if (GET_CODE (p) == CODE_LABEL)
6060 break;
6061 if (GET_CODE (p) != JUMP_INSN)
6062 continue;
6063
6064 before_comparison = get_condition_for_loop (p);
6065 if (before_comparison
6066 && XEXP (before_comparison, 0) == bl->biv->dest_reg
6067 && GET_CODE (before_comparison) == LT
6068 && XEXP (before_comparison, 1) == const0_rtx
6069 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
6070 && INTVAL (bl->biv->add_val) == -1)
6071 {
6072 REG_NOTES (PREV_INSN (loop_end))
6073 = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6074 REG_NOTES (PREV_INSN (loop_end)));
6075 bl->nonneg = 1;
6076
6077 return 1;
6078 }
6079 }
6080 }
6081 else if (num_mem_sets <= 1)
6082 {
6083 /* Try to change inc to dec, so can apply above optimization. */
6084 /* Can do this if:
6085 all registers modified are induction variables or invariant,
6086 all memory references have non-overlapping addresses
6087 (obviously true if only one write)
6088 allow 2 insns for the compare/jump at the end of the loop. */
6089 /* Also, we must avoid any instructions which use both the reversed
6090 biv and another biv. Such instructions will fail if the loop is
6091 reversed. We meet this condition by requiring that either
6092 no_use_except_counting is true, or else that there is only
6093 one biv. */
6094 int num_nonfixed_reads = 0;
6095 /* 1 if the iteration var is used only to count iterations. */
6096 int no_use_except_counting = 0;
6097 /* 1 if the loop has no memory store, or it has a single memory store
6098 which is reversible. */
6099 int reversible_mem_store = 1;
6100
6101 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6102 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6103 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
6104
6105 if (bl->giv_count == 0
6106 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
6107 {
6108 rtx bivreg = regno_reg_rtx[bl->regno];
6109
6110 /* If there are no givs for this biv, and the only exit is the
6111 fall through at the end of the the loop, then
6112 see if perhaps there are no uses except to count. */
6113 no_use_except_counting = 1;
6114 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6115 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6116 {
6117 rtx set = single_set (p);
6118
6119 if (set && GET_CODE (SET_DEST (set)) == REG
6120 && REGNO (SET_DEST (set)) == bl->regno)
6121 /* An insn that sets the biv is okay. */
6122 ;
6123 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
6124 || p == prev_nonnote_insn (loop_end))
6125 /* Don't bother about the end test. */
6126 ;
6127 else if (reg_mentioned_p (bivreg, PATTERN (p)))
6128 /* Any other use of the biv is no good. */
6129 {
6130 no_use_except_counting = 0;
6131 break;
6132 }
6133 }
6134 }
6135
6136 /* If the loop has a single store, and the destination address is
6137 invariant, then we can't reverse the loop, because this address
6138 might then have the wrong value at loop exit.
6139 This would work if the source was invariant also, however, in that
6140 case, the insn should have been moved out of the loop. */
6141
6142 if (num_mem_sets == 1)
6143 reversible_mem_store
6144 = (! unknown_address_altered
6145 && ! invariant_p (XEXP (loop_store_mems[0], 0)));
6146
6147 /* This code only acts for innermost loops. Also it simplifies
6148 the memory address check by only reversing loops with
6149 zero or one memory access.
6150 Two memory accesses could involve parts of the same array,
6151 and that can't be reversed. */
6152
6153 if (num_nonfixed_reads <= 1
6154 && !loop_has_call
6155 && !loop_has_volatile
6156 && reversible_mem_store
6157 && (no_use_except_counting
6158 || ((bl->giv_count + bl->biv_count + num_mem_sets
6159 + num_movables + 2 == insn_count)
6160 && (bl == loop_iv_list && bl->next == 0))))
6161 {
6162 rtx tem;
6163
6164 /* Loop can be reversed. */
6165 if (loop_dump_stream)
6166 fprintf (loop_dump_stream, "Can reverse loop\n");
6167
6168 /* Now check other conditions:
6169 initial_value must be zero,
6170 final_value % add_val == 0, so that when reversed, the
6171 biv will be zero on the last iteration.
6172
6173 This test can probably be improved since +/- 1 in the constant
6174 can be obtained by changing LT to LE and vice versa; this is
6175 confusing. */
6176
6177 if (comparison && bl->initial_value == const0_rtx
6178 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6179 /* LE gets turned into LT */
6180 && GET_CODE (comparison) == LT
6181 && (INTVAL (XEXP (comparison, 1))
6182 % INTVAL (bl->biv->add_val)) == 0)
6183 {
6184 /* Register will always be nonnegative, with value
6185 0 on last iteration if loop reversed */
6186
6187 /* Save some info needed to produce the new insns. */
6188 reg = bl->biv->dest_reg;
6189 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
6190 if (jump_label == pc_rtx)
6191 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
6192 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
6193
6194 final_value = XEXP (comparison, 1);
6195 start_value = GEN_INT (INTVAL (XEXP (comparison, 1))
6196 - INTVAL (bl->biv->add_val));
6197
6198 /* Initialize biv to start_value before loop start.
6199 The old initializing insn will be deleted as a
6200 dead store by flow.c. */
6201 emit_insn_before (gen_move_insn (reg, start_value), loop_start);
6202
6203 /* Add insn to decrement register, and delete insn
6204 that incremented the register. */
6205 p = emit_insn_before (gen_add2_insn (reg, new_add_val),
6206 bl->biv->insn);
6207 delete_insn (bl->biv->insn);
6208
6209 /* Update biv info to reflect its new status. */
6210 bl->biv->insn = p;
6211 bl->initial_value = start_value;
6212 bl->biv->add_val = new_add_val;
6213
6214 /* Inc LABEL_NUSES so that delete_insn will
6215 not delete the label. */
6216 LABEL_NUSES (XEXP (jump_label, 0)) ++;
6217
6218 /* Emit an insn after the end of the loop to set the biv's
6219 proper exit value if it is used anywhere outside the loop. */
6220 if ((REGNO_LAST_UID (bl->regno)
6221 != INSN_UID (PREV_INSN (PREV_INSN (loop_end))))
6222 || ! bl->init_insn
6223 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
6224 emit_insn_after (gen_move_insn (reg, final_value),
6225 loop_end);
6226
6227 /* Delete compare/branch at end of loop. */
6228 delete_insn (PREV_INSN (loop_end));
6229 delete_insn (PREV_INSN (loop_end));
6230
6231 /* Add new compare/branch insn at end of loop. */
6232 start_sequence ();
6233 emit_cmp_insn (reg, const0_rtx, GE, NULL_RTX,
6234 GET_MODE (reg), 0, 0);
6235 emit_jump_insn (gen_bge (XEXP (jump_label, 0)));
6236 tem = gen_sequence ();
6237 end_sequence ();
6238 emit_jump_insn_before (tem, loop_end);
6239
6240 for (tem = PREV_INSN (loop_end);
6241 tem && GET_CODE (tem) != JUMP_INSN; tem = PREV_INSN (tem))
6242 ;
6243 if (tem)
6244 {
6245 JUMP_LABEL (tem) = XEXP (jump_label, 0);
6246
6247 /* Increment of LABEL_NUSES done above. */
6248 /* Register is now always nonnegative,
6249 so add REG_NONNEG note to the branch. */
6250 REG_NOTES (tem) = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6251 REG_NOTES (tem));
6252 }
6253
6254 bl->nonneg = 1;
6255
6256 /* Mark that this biv has been reversed. Each giv which depends
6257 on this biv, and which is also live past the end of the loop
6258 will have to be fixed up. */
6259
6260 bl->reversed = 1;
6261
6262 if (loop_dump_stream)
6263 fprintf (loop_dump_stream,
6264 "Reversed loop and added reg_nonneg\n");
6265
6266 return 1;
6267 }
6268 }
6269 }
6270
6271 return 0;
6272 }
6273 \f
6274 /* Verify whether the biv BL appears to be eliminable,
6275 based on the insns in the loop that refer to it.
6276 LOOP_START is the first insn of the loop, and END is the end insn.
6277
6278 If ELIMINATE_P is non-zero, actually do the elimination.
6279
6280 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
6281 determine whether invariant insns should be placed inside or at the
6282 start of the loop. */
6283
6284 static int
6285 maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
6286 struct iv_class *bl;
6287 rtx loop_start;
6288 rtx end;
6289 int eliminate_p;
6290 int threshold, insn_count;
6291 {
6292 rtx reg = bl->biv->dest_reg;
6293 rtx p;
6294
6295 /* Scan all insns in the loop, stopping if we find one that uses the
6296 biv in a way that we cannot eliminate. */
6297
6298 for (p = loop_start; p != end; p = NEXT_INSN (p))
6299 {
6300 enum rtx_code code = GET_CODE (p);
6301 rtx where = threshold >= insn_count ? loop_start : p;
6302
6303 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
6304 && reg_mentioned_p (reg, PATTERN (p))
6305 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
6306 {
6307 if (loop_dump_stream)
6308 fprintf (loop_dump_stream,
6309 "Cannot eliminate biv %d: biv used in insn %d.\n",
6310 bl->regno, INSN_UID (p));
6311 break;
6312 }
6313 }
6314
6315 if (p == end)
6316 {
6317 if (loop_dump_stream)
6318 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
6319 bl->regno, eliminate_p ? "was" : "can be");
6320 return 1;
6321 }
6322
6323 return 0;
6324 }
6325 \f
6326 /* If BL appears in X (part of the pattern of INSN), see if we can
6327 eliminate its use. If so, return 1. If not, return 0.
6328
6329 If BIV does not appear in X, return 1.
6330
6331 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
6332 where extra insns should be added. Depending on how many items have been
6333 moved out of the loop, it will either be before INSN or at the start of
6334 the loop. */
6335
6336 static int
6337 maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
6338 rtx x, insn;
6339 struct iv_class *bl;
6340 int eliminate_p;
6341 rtx where;
6342 {
6343 enum rtx_code code = GET_CODE (x);
6344 rtx reg = bl->biv->dest_reg;
6345 enum machine_mode mode = GET_MODE (reg);
6346 struct induction *v;
6347 rtx arg, new, tem;
6348 int arg_operand;
6349 char *fmt;
6350 int i, j;
6351
6352 switch (code)
6353 {
6354 case REG:
6355 /* If we haven't already been able to do something with this BIV,
6356 we can't eliminate it. */
6357 if (x == reg)
6358 return 0;
6359 return 1;
6360
6361 case SET:
6362 /* If this sets the BIV, it is not a problem. */
6363 if (SET_DEST (x) == reg)
6364 return 1;
6365
6366 /* If this is an insn that defines a giv, it is also ok because
6367 it will go away when the giv is reduced. */
6368 for (v = bl->giv; v; v = v->next_iv)
6369 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
6370 return 1;
6371
6372 #ifdef HAVE_cc0
6373 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
6374 {
6375 /* Can replace with any giv that was reduced and
6376 that has (MULT_VAL != 0) and (ADD_VAL == 0).
6377 Require a constant for MULT_VAL, so we know it's nonzero.
6378 ??? We disable this optimization to avoid potential
6379 overflows. */
6380
6381 for (v = bl->giv; v; v = v->next_iv)
6382 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6383 && v->add_val == const0_rtx
6384 && ! v->ignore && ! v->maybe_dead && v->always_computable
6385 && v->mode == mode
6386 && 0)
6387 {
6388 /* If the giv V had the auto-inc address optimization applied
6389 to it, and INSN occurs between the giv insn and the biv
6390 insn, then we must adjust the value used here.
6391 This is rare, so we don't bother to do so. */
6392 if (v->auto_inc_opt
6393 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6394 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6395 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6396 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6397 continue;
6398
6399 if (! eliminate_p)
6400 return 1;
6401
6402 /* If the giv has the opposite direction of change,
6403 then reverse the comparison. */
6404 if (INTVAL (v->mult_val) < 0)
6405 new = gen_rtx (COMPARE, GET_MODE (v->new_reg),
6406 const0_rtx, v->new_reg);
6407 else
6408 new = v->new_reg;
6409
6410 /* We can probably test that giv's reduced reg. */
6411 if (validate_change (insn, &SET_SRC (x), new, 0))
6412 return 1;
6413 }
6414
6415 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
6416 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
6417 Require a constant for MULT_VAL, so we know it's nonzero.
6418 ??? Do this only if ADD_VAL is a pointer to avoid a potential
6419 overflow problem. */
6420
6421 for (v = bl->giv; v; v = v->next_iv)
6422 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6423 && ! v->ignore && ! v->maybe_dead && v->always_computable
6424 && v->mode == mode
6425 && (GET_CODE (v->add_val) == SYMBOL_REF
6426 || GET_CODE (v->add_val) == LABEL_REF
6427 || GET_CODE (v->add_val) == CONST
6428 || (GET_CODE (v->add_val) == REG
6429 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
6430 {
6431 /* If the giv V had the auto-inc address optimization applied
6432 to it, and INSN occurs between the giv insn and the biv
6433 insn, then we must adjust the value used here.
6434 This is rare, so we don't bother to do so. */
6435 if (v->auto_inc_opt
6436 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6437 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6438 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6439 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6440 continue;
6441
6442 if (! eliminate_p)
6443 return 1;
6444
6445 /* If the giv has the opposite direction of change,
6446 then reverse the comparison. */
6447 if (INTVAL (v->mult_val) < 0)
6448 new = gen_rtx (COMPARE, VOIDmode, copy_rtx (v->add_val),
6449 v->new_reg);
6450 else
6451 new = gen_rtx (COMPARE, VOIDmode, v->new_reg,
6452 copy_rtx (v->add_val));
6453
6454 /* Replace biv with the giv's reduced register. */
6455 update_reg_last_use (v->add_val, insn);
6456 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6457 return 1;
6458
6459 /* Insn doesn't support that constant or invariant. Copy it
6460 into a register (it will be a loop invariant.) */
6461 tem = gen_reg_rtx (GET_MODE (v->new_reg));
6462
6463 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
6464 where);
6465
6466 /* Substitute the new register for its invariant value in
6467 the compare expression. */
6468 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
6469 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6470 return 1;
6471 }
6472 }
6473 #endif
6474 break;
6475
6476 case COMPARE:
6477 case EQ: case NE:
6478 case GT: case GE: case GTU: case GEU:
6479 case LT: case LE: case LTU: case LEU:
6480 /* See if either argument is the biv. */
6481 if (XEXP (x, 0) == reg)
6482 arg = XEXP (x, 1), arg_operand = 1;
6483 else if (XEXP (x, 1) == reg)
6484 arg = XEXP (x, 0), arg_operand = 0;
6485 else
6486 break;
6487
6488 if (CONSTANT_P (arg))
6489 {
6490 /* First try to replace with any giv that has constant positive
6491 mult_val and constant add_val. We might be able to support
6492 negative mult_val, but it seems complex to do it in general. */
6493
6494 for (v = bl->giv; v; v = v->next_iv)
6495 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6496 && (GET_CODE (v->add_val) == SYMBOL_REF
6497 || GET_CODE (v->add_val) == LABEL_REF
6498 || GET_CODE (v->add_val) == CONST
6499 || (GET_CODE (v->add_val) == REG
6500 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
6501 && ! v->ignore && ! v->maybe_dead && v->always_computable
6502 && v->mode == mode)
6503 {
6504 /* If the giv V had the auto-inc address optimization applied
6505 to it, and INSN occurs between the giv insn and the biv
6506 insn, then we must adjust the value used here.
6507 This is rare, so we don't bother to do so. */
6508 if (v->auto_inc_opt
6509 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6510 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6511 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6512 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6513 continue;
6514
6515 if (! eliminate_p)
6516 return 1;
6517
6518 /* Replace biv with the giv's reduced reg. */
6519 XEXP (x, 1-arg_operand) = v->new_reg;
6520
6521 /* If all constants are actually constant integers and
6522 the derived constant can be directly placed in the COMPARE,
6523 do so. */
6524 if (GET_CODE (arg) == CONST_INT
6525 && GET_CODE (v->mult_val) == CONST_INT
6526 && GET_CODE (v->add_val) == CONST_INT
6527 && validate_change (insn, &XEXP (x, arg_operand),
6528 GEN_INT (INTVAL (arg)
6529 * INTVAL (v->mult_val)
6530 + INTVAL (v->add_val)), 0))
6531 return 1;
6532
6533 /* Otherwise, load it into a register. */
6534 tem = gen_reg_rtx (mode);
6535 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6536 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
6537 return 1;
6538
6539 /* If that failed, put back the change we made above. */
6540 XEXP (x, 1-arg_operand) = reg;
6541 }
6542
6543 /* Look for giv with positive constant mult_val and nonconst add_val.
6544 Insert insns to calculate new compare value.
6545 ??? Turn this off due to possible overflow. */
6546
6547 for (v = bl->giv; v; v = v->next_iv)
6548 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6549 && ! v->ignore && ! v->maybe_dead && v->always_computable
6550 && v->mode == mode
6551 && 0)
6552 {
6553 rtx tem;
6554
6555 /* If the giv V had the auto-inc address optimization applied
6556 to it, and INSN occurs between the giv insn and the biv
6557 insn, then we must adjust the value used here.
6558 This is rare, so we don't bother to do so. */
6559 if (v->auto_inc_opt
6560 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6561 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6562 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6563 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6564 continue;
6565
6566 if (! eliminate_p)
6567 return 1;
6568
6569 tem = gen_reg_rtx (mode);
6570
6571 /* Replace biv with giv's reduced register. */
6572 validate_change (insn, &XEXP (x, 1 - arg_operand),
6573 v->new_reg, 1);
6574
6575 /* Compute value to compare against. */
6576 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6577 /* Use it in this insn. */
6578 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6579 if (apply_change_group ())
6580 return 1;
6581 }
6582 }
6583 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
6584 {
6585 if (invariant_p (arg) == 1)
6586 {
6587 /* Look for giv with constant positive mult_val and nonconst
6588 add_val. Insert insns to compute new compare value.
6589 ??? Turn this off due to possible overflow. */
6590
6591 for (v = bl->giv; v; v = v->next_iv)
6592 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6593 && ! v->ignore && ! v->maybe_dead && v->always_computable
6594 && v->mode == mode
6595 && 0)
6596 {
6597 rtx tem;
6598
6599 /* If the giv V had the auto-inc address optimization applied
6600 to it, and INSN occurs between the giv insn and the biv
6601 insn, then we must adjust the value used here.
6602 This is rare, so we don't bother to do so. */
6603 if (v->auto_inc_opt
6604 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6605 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6606 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6607 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6608 continue;
6609
6610 if (! eliminate_p)
6611 return 1;
6612
6613 tem = gen_reg_rtx (mode);
6614
6615 /* Replace biv with giv's reduced register. */
6616 validate_change (insn, &XEXP (x, 1 - arg_operand),
6617 v->new_reg, 1);
6618
6619 /* Compute value to compare against. */
6620 emit_iv_add_mult (arg, v->mult_val, v->add_val,
6621 tem, where);
6622 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6623 if (apply_change_group ())
6624 return 1;
6625 }
6626 }
6627
6628 /* This code has problems. Basically, you can't know when
6629 seeing if we will eliminate BL, whether a particular giv
6630 of ARG will be reduced. If it isn't going to be reduced,
6631 we can't eliminate BL. We can try forcing it to be reduced,
6632 but that can generate poor code.
6633
6634 The problem is that the benefit of reducing TV, below should
6635 be increased if BL can actually be eliminated, but this means
6636 we might have to do a topological sort of the order in which
6637 we try to process biv. It doesn't seem worthwhile to do
6638 this sort of thing now. */
6639
6640 #if 0
6641 /* Otherwise the reg compared with had better be a biv. */
6642 if (GET_CODE (arg) != REG
6643 || reg_iv_type[REGNO (arg)] != BASIC_INDUCT)
6644 return 0;
6645
6646 /* Look for a pair of givs, one for each biv,
6647 with identical coefficients. */
6648 for (v = bl->giv; v; v = v->next_iv)
6649 {
6650 struct induction *tv;
6651
6652 if (v->ignore || v->maybe_dead || v->mode != mode)
6653 continue;
6654
6655 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
6656 if (! tv->ignore && ! tv->maybe_dead
6657 && rtx_equal_p (tv->mult_val, v->mult_val)
6658 && rtx_equal_p (tv->add_val, v->add_val)
6659 && tv->mode == mode)
6660 {
6661 /* If the giv V had the auto-inc address optimization applied
6662 to it, and INSN occurs between the giv insn and the biv
6663 insn, then we must adjust the value used here.
6664 This is rare, so we don't bother to do so. */
6665 if (v->auto_inc_opt
6666 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6667 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6668 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6669 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6670 continue;
6671
6672 if (! eliminate_p)
6673 return 1;
6674
6675 /* Replace biv with its giv's reduced reg. */
6676 XEXP (x, 1-arg_operand) = v->new_reg;
6677 /* Replace other operand with the other giv's
6678 reduced reg. */
6679 XEXP (x, arg_operand) = tv->new_reg;
6680 return 1;
6681 }
6682 }
6683 #endif
6684 }
6685
6686 /* If we get here, the biv can't be eliminated. */
6687 return 0;
6688
6689 case MEM:
6690 /* If this address is a DEST_ADDR giv, it doesn't matter if the
6691 biv is used in it, since it will be replaced. */
6692 for (v = bl->giv; v; v = v->next_iv)
6693 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
6694 return 1;
6695 break;
6696 }
6697
6698 /* See if any subexpression fails elimination. */
6699 fmt = GET_RTX_FORMAT (code);
6700 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6701 {
6702 switch (fmt[i])
6703 {
6704 case 'e':
6705 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
6706 eliminate_p, where))
6707 return 0;
6708 break;
6709
6710 case 'E':
6711 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6712 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
6713 eliminate_p, where))
6714 return 0;
6715 break;
6716 }
6717 }
6718
6719 return 1;
6720 }
6721 \f
6722 /* Return nonzero if the last use of REG
6723 is in an insn following INSN in the same basic block. */
6724
6725 static int
6726 last_use_this_basic_block (reg, insn)
6727 rtx reg;
6728 rtx insn;
6729 {
6730 rtx n;
6731 for (n = insn;
6732 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
6733 n = NEXT_INSN (n))
6734 {
6735 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
6736 return 1;
6737 }
6738 return 0;
6739 }
6740 \f
6741 /* Called via `note_stores' to record the initial value of a biv. Here we
6742 just record the location of the set and process it later. */
6743
6744 static void
6745 record_initial (dest, set)
6746 rtx dest;
6747 rtx set;
6748 {
6749 struct iv_class *bl;
6750
6751 if (GET_CODE (dest) != REG
6752 || REGNO (dest) >= max_reg_before_loop
6753 || reg_iv_type[REGNO (dest)] != BASIC_INDUCT)
6754 return;
6755
6756 bl = reg_biv_class[REGNO (dest)];
6757
6758 /* If this is the first set found, record it. */
6759 if (bl->init_insn == 0)
6760 {
6761 bl->init_insn = note_insn;
6762 bl->init_set = set;
6763 }
6764 }
6765 \f
6766 /* If any of the registers in X are "old" and currently have a last use earlier
6767 than INSN, update them to have a last use of INSN. Their actual last use
6768 will be the previous insn but it will not have a valid uid_luid so we can't
6769 use it. */
6770
6771 static void
6772 update_reg_last_use (x, insn)
6773 rtx x;
6774 rtx insn;
6775 {
6776 /* Check for the case where INSN does not have a valid luid. In this case,
6777 there is no need to modify the regno_last_uid, as this can only happen
6778 when code is inserted after the loop_end to set a pseudo's final value,
6779 and hence this insn will never be the last use of x. */
6780 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
6781 && INSN_UID (insn) < max_uid_for_loop
6782 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
6783 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
6784 else
6785 {
6786 register int i, j;
6787 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
6788 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6789 {
6790 if (fmt[i] == 'e')
6791 update_reg_last_use (XEXP (x, i), insn);
6792 else if (fmt[i] == 'E')
6793 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6794 update_reg_last_use (XVECEXP (x, i, j), insn);
6795 }
6796 }
6797 }
6798 \f
6799 /* Given a jump insn JUMP, return the condition that will cause it to branch
6800 to its JUMP_LABEL. If the condition cannot be understood, or is an
6801 inequality floating-point comparison which needs to be reversed, 0 will
6802 be returned.
6803
6804 If EARLIEST is non-zero, it is a pointer to a place where the earliest
6805 insn used in locating the condition was found. If a replacement test
6806 of the condition is desired, it should be placed in front of that
6807 insn and we will be sure that the inputs are still valid.
6808
6809 The condition will be returned in a canonical form to simplify testing by
6810 callers. Specifically:
6811
6812 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6813 (2) Both operands will be machine operands; (cc0) will have been replaced.
6814 (3) If an operand is a constant, it will be the second operand.
6815 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6816 for GE, GEU, and LEU. */
6817
6818 rtx
6819 get_condition (jump, earliest)
6820 rtx jump;
6821 rtx *earliest;
6822 {
6823 enum rtx_code code;
6824 rtx prev = jump;
6825 rtx set;
6826 rtx tem;
6827 rtx op0, op1;
6828 int reverse_code = 0;
6829 int did_reverse_condition = 0;
6830
6831 /* If this is not a standard conditional jump, we can't parse it. */
6832 if (GET_CODE (jump) != JUMP_INSN
6833 || ! condjump_p (jump) || simplejump_p (jump))
6834 return 0;
6835
6836 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
6837 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
6838 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
6839
6840 if (earliest)
6841 *earliest = jump;
6842
6843 /* If this branches to JUMP_LABEL when the condition is false, reverse
6844 the condition. */
6845 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
6846 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
6847 code = reverse_condition (code), did_reverse_condition ^= 1;
6848
6849 /* If we are comparing a register with zero, see if the register is set
6850 in the previous insn to a COMPARE or a comparison operation. Perform
6851 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
6852 in cse.c */
6853
6854 while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
6855 {
6856 /* Set non-zero when we find something of interest. */
6857 rtx x = 0;
6858
6859 #ifdef HAVE_cc0
6860 /* If comparison with cc0, import actual comparison from compare
6861 insn. */
6862 if (op0 == cc0_rtx)
6863 {
6864 if ((prev = prev_nonnote_insn (prev)) == 0
6865 || GET_CODE (prev) != INSN
6866 || (set = single_set (prev)) == 0
6867 || SET_DEST (set) != cc0_rtx)
6868 return 0;
6869
6870 op0 = SET_SRC (set);
6871 op1 = CONST0_RTX (GET_MODE (op0));
6872 if (earliest)
6873 *earliest = prev;
6874 }
6875 #endif
6876
6877 /* If this is a COMPARE, pick up the two things being compared. */
6878 if (GET_CODE (op0) == COMPARE)
6879 {
6880 op1 = XEXP (op0, 1);
6881 op0 = XEXP (op0, 0);
6882 continue;
6883 }
6884 else if (GET_CODE (op0) != REG)
6885 break;
6886
6887 /* Go back to the previous insn. Stop if it is not an INSN. We also
6888 stop if it isn't a single set or if it has a REG_INC note because
6889 we don't want to bother dealing with it. */
6890
6891 if ((prev = prev_nonnote_insn (prev)) == 0
6892 || GET_CODE (prev) != INSN
6893 || FIND_REG_INC_NOTE (prev, 0)
6894 || (set = single_set (prev)) == 0)
6895 break;
6896
6897 /* If this is setting OP0, get what it sets it to if it looks
6898 relevant. */
6899 if (rtx_equal_p (SET_DEST (set), op0))
6900 {
6901 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
6902
6903 if ((GET_CODE (SET_SRC (set)) == COMPARE
6904 || (((code == NE
6905 || (code == LT
6906 && GET_MODE_CLASS (inner_mode) == MODE_INT
6907 && (GET_MODE_BITSIZE (inner_mode)
6908 <= HOST_BITS_PER_WIDE_INT)
6909 && (STORE_FLAG_VALUE
6910 & ((HOST_WIDE_INT) 1
6911 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6912 #ifdef FLOAT_STORE_FLAG_VALUE
6913 || (code == LT
6914 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6915 && FLOAT_STORE_FLAG_VALUE < 0)
6916 #endif
6917 ))
6918 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')))
6919 x = SET_SRC (set);
6920 else if (((code == EQ
6921 || (code == GE
6922 && (GET_MODE_BITSIZE (inner_mode)
6923 <= HOST_BITS_PER_WIDE_INT)
6924 && GET_MODE_CLASS (inner_mode) == MODE_INT
6925 && (STORE_FLAG_VALUE
6926 & ((HOST_WIDE_INT) 1
6927 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6928 #ifdef FLOAT_STORE_FLAG_VALUE
6929 || (code == GE
6930 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6931 && FLOAT_STORE_FLAG_VALUE < 0)
6932 #endif
6933 ))
6934 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')
6935 {
6936 /* We might have reversed a LT to get a GE here. But this wasn't
6937 actually the comparison of data, so we don't flag that we
6938 have had to reverse the condition. */
6939 did_reverse_condition ^= 1;
6940 reverse_code = 1;
6941 x = SET_SRC (set);
6942 }
6943 else
6944 break;
6945 }
6946
6947 else if (reg_set_p (op0, prev))
6948 /* If this sets OP0, but not directly, we have to give up. */
6949 break;
6950
6951 if (x)
6952 {
6953 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
6954 code = GET_CODE (x);
6955 if (reverse_code)
6956 {
6957 code = reverse_condition (code);
6958 did_reverse_condition ^= 1;
6959 reverse_code = 0;
6960 }
6961
6962 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
6963 if (earliest)
6964 *earliest = prev;
6965 }
6966 }
6967
6968 /* If constant is first, put it last. */
6969 if (CONSTANT_P (op0))
6970 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
6971
6972 /* If OP0 is the result of a comparison, we weren't able to find what
6973 was really being compared, so fail. */
6974 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6975 return 0;
6976
6977 /* Canonicalize any ordered comparison with integers involving equality
6978 if we can do computations in the relevant mode and we do not
6979 overflow. */
6980
6981 if (GET_CODE (op1) == CONST_INT
6982 && GET_MODE (op0) != VOIDmode
6983 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
6984 {
6985 HOST_WIDE_INT const_val = INTVAL (op1);
6986 unsigned HOST_WIDE_INT uconst_val = const_val;
6987 unsigned HOST_WIDE_INT max_val
6988 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
6989
6990 switch (code)
6991 {
6992 case LE:
6993 if (const_val != max_val >> 1)
6994 code = LT, op1 = GEN_INT (const_val + 1);
6995 break;
6996
6997 case GE:
6998 if (const_val
6999 != (((HOST_WIDE_INT) 1
7000 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
7001 code = GT, op1 = GEN_INT (const_val - 1);
7002 break;
7003
7004 case LEU:
7005 if (uconst_val != max_val)
7006 code = LTU, op1 = GEN_INT (uconst_val + 1);
7007 break;
7008
7009 case GEU:
7010 if (uconst_val != 0)
7011 code = GTU, op1 = GEN_INT (uconst_val - 1);
7012 break;
7013 }
7014 }
7015
7016 /* If this was floating-point and we reversed anything other than an
7017 EQ or NE, return zero. */
7018 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
7019 && did_reverse_condition && code != NE && code != EQ
7020 && ! flag_fast_math
7021 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7022 return 0;
7023
7024 #ifdef HAVE_cc0
7025 /* Never return CC0; return zero instead. */
7026 if (op0 == cc0_rtx)
7027 return 0;
7028 #endif
7029
7030 return gen_rtx (code, VOIDmode, op0, op1);
7031 }
7032
7033 /* Similar to above routine, except that we also put an invariant last
7034 unless both operands are invariants. */
7035
7036 rtx
7037 get_condition_for_loop (x)
7038 rtx x;
7039 {
7040 rtx comparison = get_condition (x, NULL_PTR);
7041
7042 if (comparison == 0
7043 || ! invariant_p (XEXP (comparison, 0))
7044 || invariant_p (XEXP (comparison, 1)))
7045 return comparison;
7046
7047 return gen_rtx (swap_condition (GET_CODE (comparison)), VOIDmode,
7048 XEXP (comparison, 1), XEXP (comparison, 0));
7049 }
7050
7051 #ifdef HAIFA
7052 /* Analyze a loop in order to instrument it with the use of count register.
7053 loop_start and loop_end are the first and last insns of the loop.
7054 This function works in cooperation with insert_bct ().
7055 loop_can_insert_bct[loop_num] is set according to whether the optimization
7056 is applicable to the loop. When it is applicable, the following variables
7057 are also set:
7058 loop_start_value[loop_num]
7059 loop_comparison_value[loop_num]
7060 loop_increment[loop_num]
7061 loop_comparison_code[loop_num] */
7062
7063 static
7064 void analyze_loop_iterations (loop_start, loop_end)
7065 rtx loop_start, loop_end;
7066 {
7067 rtx comparison, comparison_value;
7068 rtx iteration_var, initial_value, increment;
7069 enum rtx_code comparison_code;
7070
7071 rtx last_loop_insn;
7072 rtx insn;
7073 int i;
7074
7075 /* loop_variable mode */
7076 enum machine_mode original_mode;
7077
7078 /* find the number of the loop */
7079 int loop_num = loop_number (loop_start, loop_end);
7080
7081 /* we change our mind only when we are sure that loop will be instrumented */
7082 loop_can_insert_bct[loop_num] = 0;
7083
7084 /* debugging: do we wish to instrument this loop? */
7085 if ( !check_bct_param () )
7086 return;
7087
7088 /* is the optimization suppressed. */
7089 if ( !flag_branch_on_count_reg )
7090 return;
7091
7092 /* make sure that count-reg is not in use */
7093 if (loop_used_count_register[loop_num]){
7094 if (loop_dump_stream)
7095 fprintf (loop_dump_stream,
7096 "analyze_loop_iterations %d: BCT instrumentation failed: count register already in use\n",
7097 loop_num);
7098 return;
7099 }
7100
7101 /* make sure that the function has no indirect jumps. */
7102 if (indirect_jump_in_function){
7103 if (loop_dump_stream)
7104 fprintf (loop_dump_stream,
7105 "analyze_loop_iterations %d: BCT instrumentation failed: indirect jump in function\n",
7106 loop_num);
7107 return;
7108 }
7109
7110 /* make sure that the last loop insn is a conditional jump */
7111 last_loop_insn = PREV_INSN (loop_end);
7112 if (GET_CODE (last_loop_insn != JUMP_INSN) || !condjump_p (last_loop_insn))
7113 if (loop_dump_stream)
7114 fprintf (loop_dump_stream,
7115 "analyze_loop_iterations %d: BCT instrumentation failed: invalid jump at loop end\n",
7116 loop_num);
7117 return;
7118 }
7119
7120 /* First find the iteration variable. If the last insn is a conditional
7121 branch, and the insn preceding it tests a register value, make that
7122 register the iteration variable. */
7123
7124 /* We used to use prev_nonnote_insn here, but that fails because it might
7125 accidentally get the branch for a contained loop if the branch for this
7126 loop was deleted. We can only trust branches immediately before the
7127 loop_end. */
7128
7129 comparison = get_condition_for_loop (last_loop_insn);
7130 /* ??? Get_condition may switch position of induction variable and
7131 invariant register when it canonicalizes the comparison. */
7132
7133 if (comparison == 0) {
7134 if (loop_dump_stream)
7135 fprintf (loop_dump_stream,
7136 "analyze_loop_iterations %d: BCT instrumentation failed: comparison not found\n",
7137 loop_num);
7138 return;
7139 }
7140
7141 comparison_code = GET_CODE (comparison);
7142 iteration_var = XEXP (comparison, 0);
7143 comparison_value = XEXP (comparison, 1);
7144
7145 original_mode = GET_MODE (iteration_var);
7146 if (GET_MODE_CLASS (original_mode) != MODE_INT
7147 || GET_MODE_SIZE (original_mode) != UNITS_PER_WORD) {
7148 if (loop_dump_stream)
7149 fprintf (loop_dump_stream,
7150 "analyze_loop_iterations %d: BCT Instrumentation failed: loop variable not integer\n",
7151 loop_num);
7152 return;
7153 }
7154
7155 /* get info about loop bounds and increment */
7156 iteration_info (iteration_var, &initial_value, &increment,
7157 loop_start, loop_end);
7158
7159 /* make sure that all required loop data were found */
7160 if (!(initial_value && increment && comparison_value
7161 && invariant_p (comparison_value) && invariant_p (increment)
7162 && ! indirect_jump_in_function))
7163 {
7164 if (loop_dump_stream) {
7165 fprintf (loop_dump_stream,
7166 "analyze_loop_iterations %d: BCT instrumentation failed because of wrong loop: ", loop_num);
7167 if (!(initial_value && increment && comparison_value)) {
7168 fprintf (loop_dump_stream, "\tbounds not available: ");
7169 if ( ! initial_value )
7170 fprintf (loop_dump_stream, "initial ");
7171 if ( ! increment )
7172 fprintf (loop_dump_stream, "increment ");
7173 if ( ! comparison_value )
7174 fprintf (loop_dump_stream, "comparison ");
7175 fprintf (loop_dump_stream, "\n");
7176 }
7177 if (!invariant_p (comparison_value) || !invariant_p (increment))
7178 fprintf (loop_dump_stream, "\tloop bounds not invariant\n");
7179 }
7180 return;
7181 }
7182
7183 /* make sure that the increment is constant */
7184 if (GET_CODE (increment) != CONST_INT) {
7185 if (loop_dump_stream)
7186 fprintf (loop_dump_stream,
7187 "analyze_loop_iterations %d: instrumentation failed: not arithmetic loop\n",
7188 loop_num);
7189 return;
7190 }
7191
7192 /* make sure that the loop contains neither function call, nor jump on table.
7193 (the count register might be altered by the called function, and might
7194 be used for a branch on table). */
7195 for (insn = loop_start; insn && insn != loop_end; insn = NEXT_INSN (insn)) {
7196 if (GET_CODE (insn) == CALL_INSN){
7197 if (loop_dump_stream)
7198 fprintf (loop_dump_stream,
7199 "analyze_loop_iterations %d: BCT instrumentation failed: function call in the loop\n",
7200 loop_num);
7201 return;
7202 }
7203
7204 if (GET_CODE (insn) == JUMP_INSN
7205 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
7206 || GET_CODE (PATTERN (insn)) == ADDR_VEC)){
7207 if (loop_dump_stream)
7208 fprintf (loop_dump_stream,
7209 "analyze_loop_iterations %d: BCT instrumentation failed: computed branch in the loop\n",
7210 loop_num);
7211 return;
7212 }
7213 }
7214
7215 /* At this point, we are sure that the loop can be instrumented with BCT.
7216 Some of the loops, however, will not be instrumented - the final decision
7217 is taken by insert_bct () */
7218 if (loop_dump_stream)
7219 fprintf (loop_dump_stream,
7220 "analyze_loop_iterations: loop (luid =%d) can be BCT instrumented.\n",
7221 loop_num);
7222
7223 /* mark all enclosing loops that they cannot use count register */
7224 /* ???: In fact, since insert_bct may decide not to instrument this loop,
7225 marking here may prevent instrumenting an enclosing loop that could
7226 actually be instrumented. But since this is rare, it is safer to mark
7227 here in case the order of calling (analyze/insert)_bct would be changed. */
7228 for (i=loop_num; i != -1; i = loop_outer_loop[i])
7229 loop_used_count_register[i] = 1;
7230
7231 /* Set data structures which will be used by the instrumentation phase */
7232 loop_start_value[loop_num] = initial_value;
7233 loop_comparison_value[loop_num] = comparison_value;
7234 loop_increment[loop_num] = increment;
7235 loop_comparison_code[loop_num] = comparison_code;
7236 loop_can_insert_bct[loop_num] = 1;
7237 }
7238
7239
7240 /* instrument loop for insertion of bct instruction. We distinguish between
7241 loops with compile-time bounds, to those with run-time bounds. The loop
7242 behaviour is analized according to the following characteristics/variables:
7243 ; Input variables:
7244 ; comparison-value: the value to which the iteration counter is compared.
7245 ; initial-value: iteration-counter initial value.
7246 ; increment: iteration-counter increment.
7247 ; Computed variables:
7248 ; increment-direction: the sign of the increment.
7249 ; compare-direction: '1' for GT, GTE, '-1' for LT, LTE, '0' for NE.
7250 ; range-direction: sign (comparison-value - initial-value)
7251 We give up on the following cases:
7252 ; loop variable overflow.
7253 ; run-time loop bounds with comparison code NE.
7254 */
7255
7256 static void
7257 insert_bct (loop_start, loop_end)
7258 rtx loop_start, loop_end;
7259 {
7260 rtx initial_value, comparison_value, increment;
7261 enum rtx_code comparison_code;
7262
7263 int increment_direction, compare_direction;
7264 int unsigned_p = 0;
7265
7266 /* if the loop condition is <= or >=, the number of iteration
7267 is 1 more than the range of the bounds of the loop */
7268 int add_iteration = 0;
7269
7270 /* the only machine mode we work with - is the integer of the size that the
7271 machine has */
7272 enum machine_mode loop_var_mode = SImode;
7273
7274 int loop_num = loop_number (loop_start, loop_end);
7275
7276 /* get loop-variables. No need to check that these are valid - already
7277 checked in analyze_loop_iterations (). */
7278 comparison_code = loop_comparison_code[loop_num];
7279 initial_value = loop_start_value[loop_num];
7280 comparison_value = loop_comparison_value[loop_num];
7281 increment = loop_increment[loop_num];
7282
7283 /* check analyze_loop_iterations decision for this loop. */
7284 if (! loop_can_insert_bct[loop_num]){
7285 if (loop_dump_stream)
7286 fprintf (loop_dump_stream,
7287 "insert_bct: [%d] - was decided not to instrument by analyze_loop_iterations ()\n",
7288 loop_num);
7289 return;
7290 }
7291
7292 /* make sure that the loop was not fully unrolled. */
7293 if (loop_unroll_factor[loop_num] == -1){
7294 if (loop_dump_stream)
7295 fprintf (loop_dump_stream, "insert_bct %d: was completely unrolled\n", loop_num);
7296 return;
7297 }
7298
7299 /* make sure that the last loop insn is a conditional jump .
7300 This check is repeated from analyze_loop_iterations (),
7301 because unrolling might have changed that. */
7302 if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
7303 || !is_condjump_p (PREV_INSN (loop_end)))
7304 if (loop_dump_stream)
7305 fprintf (loop_dump_stream,
7306 "insert_bct: not instrumenting BCT because of invalid branch\n");
7307 return;
7308 }
7309
7310 /* fix increment in case loop was unrolled. */
7311 if (loop_unroll_factor[loop_num] > 1)
7312 increment = GEN_INT ( INTVAL (increment) * loop_unroll_factor[loop_num] );
7313
7314 /* determine properties and directions of the loop */
7315 increment_direction = (INTVAL (increment) > 0) ? 1:-1;
7316 switch ( comparison_code ) {
7317 case LEU:
7318 unsigned_p = 1;
7319 /* fallthrough */
7320 case LE:
7321 compare_direction = 1;
7322 add_iteration = 1;
7323 break;
7324 case GEU:
7325 unsigned_p = 1;
7326 /* fallthrough */
7327 case GE:
7328 compare_direction = -1;
7329 add_iteration = 1;
7330 break;
7331 case EQ:
7332 /* in this case we cannot know the number of iterations */
7333 if (loop_dump_stream)
7334 fprintf (loop_dump_stream,
7335 "insert_bct: %d: loop cannot be instrumented: == in condition\n",
7336 loop_num);
7337 return;
7338 case LTU:
7339 unsigned_p = 1;
7340 /* fallthrough */
7341 case LT:
7342 compare_direction = 1;
7343 break;
7344 case GTU:
7345 unsigned_p = 1;
7346 /* fallthrough */
7347 case GT:
7348 compare_direction = -1;
7349 break;
7350 case NE:
7351 compare_direction = 0;
7352 break;
7353 default:
7354 abort ();
7355 }
7356
7357
7358 /* make sure that the loop does not end by an overflow */
7359 if (compare_direction != increment_direction) {
7360 if (loop_dump_stream)
7361 fprintf (loop_dump_stream,
7362 "insert_bct: %d: loop cannot be instrumented: terminated by overflow\n",
7363 loop_num);
7364 return;
7365 }
7366
7367 /* try to instrument the loop. */
7368
7369 /* Handle the simpler case, where the bounds are known at compile time. */
7370 if (GET_CODE (initial_value) == CONST_INT && GET_CODE (comparison_value) == CONST_INT)
7371 {
7372 int n_iterations;
7373 int increment_value_abs = INTVAL (increment) * increment_direction;
7374
7375 /* check the relation between compare-val and initial-val */
7376 int difference = INTVAL (comparison_value) - INTVAL (initial_value);
7377 int range_direction = (difference > 0) ? 1 : -1;
7378
7379 /* make sure the loop executes enough iterations to gain from BCT */
7380 if (difference > -3 && difference < 3) {
7381 if (loop_dump_stream)
7382 fprintf (loop_dump_stream,
7383 "insert_bct: loop %d not BCT instrumented: too small iteration count.\n",
7384 loop_num);
7385 return;
7386 }
7387
7388 /* make sure that the loop executes at least once */
7389 if ((range_direction == 1 && compare_direction == -1)
7390 || (range_direction == -1 && compare_direction == 1))
7391 {
7392 if (loop_dump_stream)
7393 fprintf (loop_dump_stream,
7394 "insert_bct: loop %d: does not iterate even once. Not instrumenting.\n",
7395 loop_num);
7396 return;
7397 }
7398
7399 /* make sure that the loop does not end by an overflow (in compile time
7400 bounds we must have an additional check for overflow, because here
7401 we also support the compare code of 'NE'. */
7402 if (comparison_code == NE
7403 && increment_direction != range_direction) {
7404 if (loop_dump_stream)
7405 fprintf (loop_dump_stream,
7406 "insert_bct (compile time bounds): %d: loop not instrumented: terminated by overflow\n",
7407 loop_num);
7408 return;
7409 }
7410
7411 /* Determine the number of iterations by:
7412 ;
7413 ; compare-val - initial-val + (increment -1) + additional-iteration
7414 ; num_iterations = -----------------------------------------------------------------
7415 ; increment
7416 */
7417 difference = (range_direction > 0) ? difference : -difference;
7418 #if 0
7419 fprintf (stderr, "difference is: %d\n", difference); /* @*/
7420 fprintf (stderr, "increment_value_abs is: %d\n", increment_value_abs); /* @*/
7421 fprintf (stderr, "add_iteration is: %d\n", add_iteration); /* @*/
7422 fprintf (stderr, "INTVAL (comparison_value) is: %d\n", INTVAL (comparison_value)); /* @*/
7423 fprintf (stderr, "INTVAL (initial_value) is: %d\n", INTVAL (initial_value)); /* @*/
7424 #endif
7425
7426 if (increment_value_abs == 0) {
7427 fprintf (stderr, "insert_bct: error: increment == 0 !!!\n");
7428 abort ();
7429 }
7430 n_iterations = (difference + increment_value_abs - 1 + add_iteration)
7431 / increment_value_abs;
7432
7433 #if 0
7434 fprintf (stderr, "number of iterations is: %d\n", n_iterations); /* @*/
7435 #endif
7436 instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
7437
7438 /* Done with this loop. */
7439 return;
7440 }
7441
7442 /* Handle the more complex case, that the bounds are NOT known at compile time. */
7443 /* In this case we generate run_time calculation of the number of iterations */
7444
7445 /* With runtime bounds, if the compare is of the form '!=' we give up */
7446 if (comparison_code == NE) {
7447 if (loop_dump_stream)
7448 fprintf (loop_dump_stream,
7449 "insert_bct: fail for loop %d: runtime bounds with != comparison\n",
7450 loop_num);
7451 return;
7452 }
7453
7454 else {
7455 /* We rely on the existence of run-time guard to ensure that the
7456 loop executes at least once. */
7457 rtx sequence;
7458 rtx iterations_num_reg;
7459
7460 int increment_value_abs = INTVAL (increment) * increment_direction;
7461
7462 /* make sure that the increment is a power of two, otherwise (an
7463 expensive) divide is needed. */
7464 if (exact_log2 (increment_value_abs) == -1)
7465 {
7466 if (loop_dump_stream)
7467 fprintf (loop_dump_stream,
7468 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
7469 return;
7470 }
7471
7472 /* compute the number of iterations */
7473 start_sequence ();
7474 {
7475 /* CYGNUS LOCAL: HAIFA bug fix */
7476 rtx temp_reg;
7477
7478 /* Again, the number of iterations is calculated by:
7479 ;
7480 ; compare-val - initial-val + (increment -1) + additional-iteration
7481 ; num_iterations = -----------------------------------------------------------------
7482 ; increment
7483 */
7484 /* ??? Do we have to call copy_rtx here before passing rtx to
7485 expand_binop? */
7486 if (compare_direction > 0) {
7487 /* <, <= :the loop variable is increasing */
7488 temp_reg = expand_binop (loop_var_mode, sub_optab, comparison_value,
7489 initial_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7490 }
7491 else {
7492 temp_reg = expand_binop (loop_var_mode, sub_optab, initial_value,
7493 comparison_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7494 }
7495
7496 if (increment_value_abs - 1 + add_iteration != 0)
7497 temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
7498 GEN_INT (increment_value_abs - 1 + add_iteration),
7499 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7500
7501 if (increment_value_abs != 1)
7502 {
7503 /* ??? This will generate an expensive divide instruction for
7504 most targets. The original authors apparently expected this
7505 to be a shift, since they test for power-of-2 divisors above,
7506 but just naively generating a divide instruction will not give
7507 a shift. It happens to work for the PowerPC target because
7508 the rs6000.md file has a divide pattern that emits shifts.
7509 It will probably not work for any other target. */
7510 iterations_num_reg = expand_binop (loop_var_mode, sdiv_optab,
7511 temp_reg,
7512 GEN_INT (increment_value_abs),
7513 NULL_RTX, 0, OPTAB_LIB_WIDEN);
7514 }
7515 else
7516 iterations_num_reg = temp_reg;
7517 /* END CYGNUS LOCAL: HAIFA bug fix */
7518 }
7519 sequence = gen_sequence ();
7520 end_sequence ();
7521 emit_insn_before (sequence, loop_start);
7522 instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
7523 }
7524 }
7525
7526 /* instrument loop by inserting a bct in it. This is done in the following way:
7527 1. A new register is created and assigned the hard register number of the count
7528 register.
7529 2. In the head of the loop the new variable is initialized by the value passed in the
7530 loop_num_iterations parameter.
7531 3. At the end of the loop, comparison of the register with 0 is generated.
7532 The created comparison follows the pattern defined for the
7533 decrement_and_branch_on_count insn, so this insn will be generated in assembly
7534 generation phase.
7535 4. The compare&branch on the old variable is deleted. So, if the loop-variable was
7536 not used elsewhere, it will be eliminated by data-flow analisys. */
7537
7538 static void
7539 instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
7540 rtx loop_start, loop_end;
7541 rtx loop_num_iterations;
7542 {
7543 rtx temp_reg1, temp_reg2;
7544 rtx start_label;
7545
7546 rtx sequence;
7547 enum machine_mode loop_var_mode = SImode;
7548
7549 #ifdef HAVE_decrement_and_branch_on_count
7550 if (HAVE_decrement_and_branch_on_count)
7551 {
7552 if (loop_dump_stream)
7553 fprintf (loop_dump_stream, "Loop: Inserting BCT\n");
7554
7555 /* eliminate the check on the old variable */
7556 delete_insn (PREV_INSN (loop_end));
7557 delete_insn (PREV_INSN (loop_end));
7558
7559 /* insert the label which will delimit the start of the loop */
7560 start_label = gen_label_rtx ();
7561 emit_label_after (start_label, loop_start);
7562
7563 /* insert initialization of the count register into the loop header */
7564 start_sequence ();
7565 temp_reg1 = gen_reg_rtx (loop_var_mode);
7566 emit_insn (gen_move_insn (temp_reg1, loop_num_iterations));
7567
7568 /* this will be count register */
7569 temp_reg2 = gen_rtx (REG, loop_var_mode, COUNT_REGISTER_REGNUM);
7570 /* we have to move the value to the count register from an GPR
7571 because rtx pointed to by loop_num_iterations could contain
7572 expression which cannot be moved into count register */
7573 emit_insn (gen_move_insn (temp_reg2, temp_reg1));
7574
7575 sequence = gen_sequence ();
7576 end_sequence ();
7577 emit_insn_after (sequence, loop_start);
7578
7579 /* insert new comparison on the count register instead of the
7580 old one, generating the needed BCT pattern (that will be
7581 later recognized by assembly generation phase). */
7582 emit_jump_insn_before (gen_decrement_and_branch_on_count (temp_reg2, start_label),
7583 loop_end);
7584 LABEL_NUSES (start_label)++;
7585 }
7586
7587 #endif /* HAVE_decrement_and_branch_on_count */
7588 }
7589
7590 /* calculate the uid of the given loop */
7591 int
7592 loop_number (loop_start, loop_end)
7593 rtx loop_start, loop_end;
7594 {
7595 int loop_num = -1;
7596
7597 /* assume that this insn contains the LOOP_START
7598 note, so it will not be changed by the loop unrolling */
7599 loop_num = uid_loop_num[INSN_UID (loop_start)];
7600 /* sanity check - should never happen */
7601 if (loop_num == -1)
7602 abort ();
7603
7604 return loop_num;
7605 }
7606
7607 /* scan the function and determine whether it has indirect (computed) jump */
7608 static int
7609 indirect_jump_in_function_p (start)
7610 rtx start;
7611 {
7612 rtx insn;
7613 int is_indirect_jump = 0;
7614
7615 for (insn = start; insn; insn = NEXT_INSN (insn)) {
7616 if (GET_CODE (insn) == JUMP_INSN) {
7617 if (GET_CODE (PATTERN (insn)) == SET) {
7618 rtx insn_work_code = XEXP (PATTERN (insn), 1);
7619
7620 if (GET_CODE (insn_work_code) == LABEL_REF)
7621 continue;
7622 if (GET_CODE (insn_work_code) == IF_THEN_ELSE) {
7623 rtx jump_target = XEXP (insn_work_code, 1);
7624
7625 if (jump_target == pc_rtx
7626 || (GET_CODE (jump_target) == (enum rtx_code)LABEL_REF))
7627 continue;
7628 }
7629 }
7630 is_indirect_jump = 1;
7631 }
7632 }
7633 return is_indirect_jump;
7634 }
7635
7636 /* debugging: fix_bct_param () is called from toplev.c upon detection
7637 of the -fbct-***-N options. */
7638 int
7639 fix_bct_param (param, val)
7640 char *param, *val;
7641 {
7642 if ( !strcmp (param, "max") )
7643 dbg_bct_max = atoi (val);
7644 else if ( !strcmp (param, "min") )
7645 dbg_bct_min = atoi (val);
7646 }
7647
7648 /* debugging: return 1 if the loop should be instrumented,
7649 according to bct-min/max. */
7650 static int
7651 check_bct_param ()
7652 {
7653 static int dbg_bct_num = 0;
7654
7655 dbg_bct_num++;
7656 if (dbg_bct_num > dbg_bct_min || dbg_bct_min == -1)
7657 if (dbg_bct_num <= dbg_bct_max || dbg_bct_max == -1)
7658 return 1;
7659 return 0;
7660 }
7661 #endif /* HAIFA */
7662 /* END CYGNUS LOCAL haifa */