1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 88, 89, 91-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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)
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.
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. */
22 /* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
42 #include "insn-config.h"
43 #include "insn-flags.h"
45 #include "hard-reg-set.h"
53 /* Vector mapping INSN_UIDs to luids.
54 The luids are like uids but increase monotonically always.
55 We use them to see whether a jump comes from outside a given loop. */
59 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
60 number the insn is contained in. */
64 /* 1 + largest uid of any insn. */
68 /* 1 + luid of last insn. */
72 /* Number of loops detected in current function. Used as index to the
75 static int max_loop_num
;
77 /* Indexed by loop number, contains the first and last insn of each loop. */
79 static rtx
*loop_number_loop_starts
, *loop_number_loop_ends
;
81 /* Likewise for the continue insn */
82 static rtx
*loop_number_loop_cont
;
84 /* The first code_label that is reached in every loop iteration.
85 0 when not computed yet, initially const0_rtx if a jump couldn't be
87 Also set to 0 when there is no such label before the NOTE_INSN_LOOP_CONT
88 of this loop, or in verify_dominator, if a jump couldn't be followed. */
89 static rtx
*loop_number_cont_dominator
;
91 /* For each loop, gives the containing loop number, -1 if none. */
95 #ifdef HAVE_decrement_and_branch_on_count
96 /* Records whether resource in use by inner loop. */
98 int *loop_used_count_register
;
99 #endif /* HAVE_decrement_and_branch_on_count */
101 /* Indexed by loop number, contains a nonzero value if the "loop" isn't
102 really a loop (an insn outside the loop branches into it). */
104 static char *loop_invalid
;
106 /* Indexed by loop number, links together all LABEL_REFs which refer to
107 code labels outside the loop. Used by routines that need to know all
108 loop exits, such as final_biv_value and final_giv_value.
110 This does not include loop exits due to return instructions. This is
111 because all bivs and givs are pseudos, and hence must be dead after a
112 return, so the presense of a return does not affect any of the
113 optimizations that use this info. It is simpler to just not include return
114 instructions on this list. */
116 rtx
*loop_number_exit_labels
;
118 /* Indexed by loop number, counts the number of LABEL_REFs on
119 loop_number_exit_labels for this loop and all loops nested inside it. */
121 int *loop_number_exit_count
;
123 /* Nonzero if there is a subroutine call in the current loop. */
125 static int loop_has_call
;
127 /* Nonzero if there is a volatile memory reference in the current
130 static int loop_has_volatile
;
132 /* Nonzero if there is a tablejump in the current loop. */
134 static int loop_has_tablejump
;
136 /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
137 current loop. A continue statement will generate a branch to
138 NEXT_INSN (loop_continue). */
140 static rtx loop_continue
;
142 /* Indexed by register number, contains the number of times the reg
143 is set during the loop being scanned.
144 During code motion, a negative value indicates a reg that has been
145 made a candidate; in particular -2 means that it is an candidate that
146 we know is equal to a constant and -1 means that it is an candidate
147 not known equal to a constant.
148 After code motion, regs moved have 0 (which is accurate now)
149 while the failed candidates have the original number of times set.
151 Therefore, at all times, == 0 indicates an invariant register;
152 < 0 a conditionally invariant one. */
154 static varray_type set_in_loop
;
156 /* Original value of set_in_loop; same except that this value
157 is not set negative for a reg whose sets have been made candidates
158 and not set to 0 for a reg that is moved. */
160 static varray_type n_times_set
;
162 /* Index by register number, 1 indicates that the register
163 cannot be moved or strength reduced. */
165 static varray_type may_not_optimize
;
167 /* Contains the insn in which a register was used if it was used
168 exactly once; contains const0_rtx if it was used more than once. */
170 static varray_type reg_single_usage
;
172 /* Nonzero means reg N has already been moved out of one loop.
173 This reduces the desire to move it out of another. */
175 static char *moved_once
;
177 /* List of MEMs that are stored in this loop. */
179 static rtx loop_store_mems
;
181 /* The insn where the first of these was found. */
182 static rtx first_loop_store_insn
;
184 typedef struct loop_mem_info
{
185 rtx mem
; /* The MEM itself. */
186 rtx reg
; /* Corresponding pseudo, if any. */
187 int optimize
; /* Nonzero if we can optimize access to this MEM. */
190 /* Array of MEMs that are used (read or written) in this loop, but
191 cannot be aliased by anything in this loop, except perhaps
192 themselves. In other words, if loop_mems[i] is altered during the
193 loop, it is altered by an expression that is rtx_equal_p to it. */
195 static loop_mem_info
*loop_mems
;
197 /* The index of the next available slot in LOOP_MEMS. */
199 static int loop_mems_idx
;
201 /* The number of elements allocated in LOOP_MEMs. */
203 static int loop_mems_allocated
;
205 /* Nonzero if we don't know what MEMs were changed in the current loop.
206 This happens if the loop contains a call (in which case `loop_has_call'
207 will also be set) or if we store into more than NUM_STORES MEMs. */
209 static int unknown_address_altered
;
211 /* Count of movable (i.e. invariant) instructions discovered in the loop. */
212 static int num_movables
;
214 /* Count of memory write instructions discovered in the loop. */
215 static int num_mem_sets
;
217 /* Number of loops contained within the current one, including itself. */
218 static int loops_enclosed
;
220 /* Bound on pseudo register number before loop optimization.
221 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
222 int max_reg_before_loop
;
224 /* This obstack is used in product_cheap_p to allocate its rtl. It
225 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
226 If we used the same obstack that it did, we would be deallocating
229 static struct obstack temp_obstack
;
231 /* This is where the pointer to the obstack being used for RTL is stored. */
233 extern struct obstack
*rtl_obstack
;
235 #define obstack_chunk_alloc xmalloc
236 #define obstack_chunk_free free
238 /* During the analysis of a loop, a chain of `struct movable's
239 is made to record all the movable insns found.
240 Then the entire chain can be scanned to decide which to move. */
244 rtx insn
; /* A movable insn */
245 rtx set_src
; /* The expression this reg is set from. */
246 rtx set_dest
; /* The destination of this SET. */
247 rtx dependencies
; /* When INSN is libcall, this is an EXPR_LIST
248 of any registers used within the LIBCALL. */
249 int consec
; /* Number of consecutive following insns
250 that must be moved with this one. */
251 int regno
; /* The register it sets */
252 short lifetime
; /* lifetime of that register;
253 may be adjusted when matching movables
254 that load the same value are found. */
255 short savings
; /* Number of insns we can move for this reg,
256 including other movables that force this
257 or match this one. */
258 unsigned int cond
: 1; /* 1 if only conditionally movable */
259 unsigned int force
: 1; /* 1 means MUST move this insn */
260 unsigned int global
: 1; /* 1 means reg is live outside this loop */
261 /* If PARTIAL is 1, GLOBAL means something different:
262 that the reg is live outside the range from where it is set
263 to the following label. */
264 unsigned int done
: 1; /* 1 inhibits further processing of this */
266 unsigned int partial
: 1; /* 1 means this reg is used for zero-extending.
267 In particular, moving it does not make it
269 unsigned int move_insn
: 1; /* 1 means that we call emit_move_insn to
270 load SRC, rather than copying INSN. */
271 unsigned int move_insn_first
:1;/* Same as above, if this is necessary for the
272 first insn of a consecutive sets group. */
273 unsigned int is_equiv
: 1; /* 1 means a REG_EQUIV is present on INSN. */
274 enum machine_mode savemode
; /* Nonzero means it is a mode for a low part
275 that we should avoid changing when clearing
276 the rest of the reg. */
277 struct movable
*match
; /* First entry for same value */
278 struct movable
*forces
; /* An insn that must be moved if this is */
279 struct movable
*next
;
282 static struct movable
*the_movables
;
284 FILE *loop_dump_stream
;
286 /* Forward declarations. */
288 static void verify_dominator
PROTO((int));
289 static void find_and_verify_loops
PROTO((rtx
));
290 static void mark_loop_jump
PROTO((rtx
, int));
291 static void prescan_loop
PROTO((rtx
, rtx
));
292 static int reg_in_basic_block_p
PROTO((rtx
, rtx
));
293 static int consec_sets_invariant_p
PROTO((rtx
, int, rtx
));
294 static int labels_in_range_p
PROTO((rtx
, int));
295 static void count_one_set
PROTO((rtx
, rtx
, varray_type
, rtx
*));
297 static void count_loop_regs_set
PROTO((rtx
, rtx
, varray_type
, varray_type
,
299 static void note_addr_stored
PROTO((rtx
, rtx
));
300 static int loop_reg_used_before_p
PROTO((rtx
, rtx
, rtx
, rtx
, rtx
));
301 static void scan_loop
PROTO((rtx
, rtx
, rtx
, int, int));
303 static void replace_call_address
PROTO((rtx
, rtx
, rtx
));
305 static rtx skip_consec_insns
PROTO((rtx
, int));
306 static int libcall_benefit
PROTO((rtx
));
307 static void ignore_some_movables
PROTO((struct movable
*));
308 static void force_movables
PROTO((struct movable
*));
309 static void combine_movables
PROTO((struct movable
*, int));
310 static int regs_match_p
PROTO((rtx
, rtx
, struct movable
*));
311 static int rtx_equal_for_loop_p
PROTO((rtx
, rtx
, struct movable
*));
312 static void add_label_notes
PROTO((rtx
, rtx
));
313 static void move_movables
PROTO((struct movable
*, int, int, rtx
, rtx
, int));
314 static int count_nonfixed_reads
PROTO((rtx
));
315 static void strength_reduce
PROTO((rtx
, rtx
, rtx
, int, rtx
, rtx
, rtx
, int, int));
316 static void find_single_use_in_loop
PROTO((rtx
, rtx
, varray_type
));
317 static int valid_initial_value_p
PROTO((rtx
, rtx
, int, rtx
));
318 static void find_mem_givs
PROTO((rtx
, rtx
, int, rtx
, rtx
));
319 static void record_biv
PROTO((struct induction
*, rtx
, rtx
, rtx
, rtx
, rtx
*, int, int));
320 static void check_final_value
PROTO((struct induction
*, rtx
, rtx
,
321 unsigned HOST_WIDE_INT
));
322 static void record_giv
PROTO((struct induction
*, rtx
, rtx
, rtx
, rtx
, rtx
, int, enum g_types
, int, rtx
*, rtx
, rtx
));
323 static void update_giv_derive
PROTO((rtx
));
324 static int basic_induction_var
PROTO((rtx
, enum machine_mode
, rtx
, rtx
, rtx
*, rtx
*, rtx
**));
325 static rtx simplify_giv_expr
PROTO((rtx
, int *));
326 static int general_induction_var
PROTO((rtx
, rtx
*, rtx
*, rtx
*, int, int *));
327 static int consec_sets_giv
PROTO((int, rtx
, rtx
, rtx
, rtx
*, rtx
*, rtx
*));
328 static int check_dbra_loop
PROTO((rtx
, int, rtx
, struct loop_info
*));
329 static rtx express_from_1
PROTO((rtx
, rtx
, rtx
));
330 static rtx combine_givs_p
PROTO((struct induction
*, struct induction
*));
331 static void combine_givs
PROTO((struct iv_class
*));
332 struct recombine_givs_stats
;
333 static int find_life_end
PROTO((rtx
, struct recombine_givs_stats
*, rtx
, rtx
));
334 static void recombine_givs
PROTO((struct iv_class
*, rtx
, rtx
, int));
335 static int product_cheap_p
PROTO((rtx
, rtx
));
336 static int maybe_eliminate_biv
PROTO((struct iv_class
*, rtx
, rtx
, int, int, int));
337 static int maybe_eliminate_biv_1
PROTO((rtx
, rtx
, struct iv_class
*, int, rtx
));
338 static int last_use_this_basic_block
PROTO((rtx
, rtx
));
339 static void record_initial
PROTO((rtx
, rtx
));
340 static void update_reg_last_use
PROTO((rtx
, rtx
));
341 static rtx next_insn_in_loop
PROTO((rtx
, rtx
, rtx
, rtx
));
342 static void load_mems_and_recount_loop_regs_set
PROTO((rtx
, rtx
, rtx
,
344 static void load_mems
PROTO((rtx
, rtx
, rtx
, rtx
));
345 static int insert_loop_mem
PROTO((rtx
*, void *));
346 static int replace_loop_mem
PROTO((rtx
*, void *));
347 static int replace_label
PROTO((rtx
*, void *));
349 typedef struct rtx_and_int
{
354 typedef struct rtx_pair
{
359 /* Nonzero iff INSN is between START and END, inclusive. */
360 #define INSN_IN_RANGE_P(INSN, START, END) \
361 (INSN_UID (INSN) < max_uid_for_loop \
362 && INSN_LUID (INSN) >= INSN_LUID (START) \
363 && INSN_LUID (INSN) <= INSN_LUID (END))
365 #ifdef HAVE_decrement_and_branch_on_count
366 /* Test whether BCT applicable and safe. */
367 static void insert_bct
PROTO((rtx
, rtx
, struct loop_info
*));
369 /* Auxiliary function that inserts the BCT pattern into the loop. */
370 static void instrument_loop_bct
PROTO((rtx
, rtx
, rtx
));
371 #endif /* HAVE_decrement_and_branch_on_count */
373 /* Indirect_jump_in_function is computed once per function. */
374 int indirect_jump_in_function
= 0;
375 static int indirect_jump_in_function_p
PROTO((rtx
));
377 static int compute_luids
PROTO((rtx
, rtx
, int));
379 static int biv_elimination_giv_has_0_offset
PROTO((struct induction
*,
380 struct induction
*, rtx
));
382 /* Relative gain of eliminating various kinds of operations. */
385 static int shift_cost
;
386 static int mult_cost
;
389 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
390 copy the value of the strength reduced giv to its original register. */
391 static int copy_cost
;
393 /* Cost of using a register, to normalize the benefits of a giv. */
394 static int reg_address_cost
;
400 char *free_point
= (char *) oballoc (1);
401 rtx reg
= gen_rtx_REG (word_mode
, LAST_VIRTUAL_REGISTER
+ 1);
403 add_cost
= rtx_cost (gen_rtx_PLUS (word_mode
, reg
, reg
), SET
);
406 reg_address_cost
= ADDRESS_COST (reg
);
408 reg_address_cost
= rtx_cost (reg
, MEM
);
411 /* We multiply by 2 to reconcile the difference in scale between
412 these two ways of computing costs. Otherwise the cost of a copy
413 will be far less than the cost of an add. */
417 /* Free the objects we just allocated. */
420 /* Initialize the obstack used for rtl in product_cheap_p. */
421 gcc_obstack_init (&temp_obstack
);
424 /* Compute the mapping from uids to luids.
425 LUIDs are numbers assigned to insns, like uids,
426 except that luids increase monotonically through the code.
427 Start at insn START and stop just before END. Assign LUIDs
428 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
430 compute_luids (start
, end
, prev_luid
)
437 for (insn
= start
, i
= prev_luid
; insn
!= end
; insn
= NEXT_INSN (insn
))
439 if (INSN_UID (insn
) >= max_uid_for_loop
)
441 /* Don't assign luids to line-number NOTEs, so that the distance in
442 luids between two insns is not affected by -g. */
443 if (GET_CODE (insn
) != NOTE
444 || NOTE_LINE_NUMBER (insn
) <= 0)
445 uid_luid
[INSN_UID (insn
)] = ++i
;
447 /* Give a line number note the same luid as preceding insn. */
448 uid_luid
[INSN_UID (insn
)] = i
;
453 /* Entry point of this file. Perform loop optimization
454 on the current function. F is the first insn of the function
455 and DUMPFILE is a stream for output of a trace of actions taken
456 (or 0 if none should be output). */
459 loop_optimize (f
, dumpfile
, unroll_p
, bct_p
)
460 /* f is the first instruction of a chain of insns for one function */
468 loop_dump_stream
= dumpfile
;
470 init_recog_no_volatile ();
472 max_reg_before_loop
= max_reg_num ();
474 moved_once
= (char *) alloca (max_reg_before_loop
);
475 bzero (moved_once
, max_reg_before_loop
);
479 /* Count the number of loops. */
482 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
484 if (GET_CODE (insn
) == NOTE
485 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
489 /* Don't waste time if no loops. */
490 if (max_loop_num
== 0)
493 /* Get size to use for tables indexed by uids.
494 Leave some space for labels allocated by find_and_verify_loops. */
495 max_uid_for_loop
= get_max_uid () + 1 + max_loop_num
* 32;
497 uid_luid
= (int *) alloca (max_uid_for_loop
* sizeof (int));
498 uid_loop_num
= (int *) alloca (max_uid_for_loop
* sizeof (int));
500 bzero ((char *) uid_luid
, max_uid_for_loop
* sizeof (int));
501 bzero ((char *) uid_loop_num
, max_uid_for_loop
* sizeof (int));
503 /* Allocate tables for recording each loop. We set each entry, so they need
505 loop_number_loop_starts
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
506 loop_number_loop_ends
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
507 loop_number_loop_cont
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
508 loop_number_cont_dominator
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
509 loop_outer_loop
= (int *) alloca (max_loop_num
* sizeof (int));
510 loop_invalid
= (char *) alloca (max_loop_num
* sizeof (char));
511 loop_number_exit_labels
= (rtx
*) alloca (max_loop_num
* sizeof (rtx
));
512 loop_number_exit_count
= (int *) alloca (max_loop_num
* sizeof (int));
514 #ifdef HAVE_decrement_and_branch_on_count
515 /* Allocate for BCT optimization */
516 loop_used_count_register
= (int *) alloca (max_loop_num
* sizeof (int));
517 bzero ((char *) loop_used_count_register
, max_loop_num
* sizeof (int));
518 #endif /* HAVE_decrement_and_branch_on_count */
520 /* Find and process each loop.
521 First, find them, and record them in order of their beginnings. */
522 find_and_verify_loops (f
);
524 /* Now find all register lifetimes. This must be done after
525 find_and_verify_loops, because it might reorder the insns in the
527 reg_scan (f
, max_reg_num (), 1);
529 /* This must occur after reg_scan so that registers created by gcse
530 will have entries in the register tables.
532 We could have added a call to reg_scan after gcse_main in toplev.c,
533 but moving this call to init_alias_analysis is more efficient. */
534 init_alias_analysis ();
536 /* See if we went too far. Note that get_max_uid already returns
537 one more that the maximum uid of all insn. */
538 if (get_max_uid () > max_uid_for_loop
)
540 /* Now reset it to the actual size we need. See above. */
541 max_uid_for_loop
= get_max_uid ();
543 /* find_and_verify_loops has already called compute_luids, but it might
544 have rearranged code afterwards, so we need to recompute the luids now. */
545 max_luid
= compute_luids (f
, NULL_RTX
, 0);
547 /* Don't leave gaps in uid_luid for insns that have been
548 deleted. It is possible that the first or last insn
549 using some register has been deleted by cross-jumping.
550 Make sure that uid_luid for that former insn's uid
551 points to the general area where that insn used to be. */
552 for (i
= 0; i
< max_uid_for_loop
; i
++)
554 uid_luid
[0] = uid_luid
[i
];
555 if (uid_luid
[0] != 0)
558 for (i
= 0; i
< max_uid_for_loop
; i
++)
559 if (uid_luid
[i
] == 0)
560 uid_luid
[i
] = uid_luid
[i
- 1];
562 /* Create a mapping from loops to BLOCK tree nodes. */
563 if (unroll_p
&& write_symbols
!= NO_DEBUG
)
564 find_loop_tree_blocks ();
566 /* Determine if the function has indirect jump. On some systems
567 this prevents low overhead loop instructions from being used. */
568 indirect_jump_in_function
= indirect_jump_in_function_p (f
);
570 /* Now scan the loops, last ones first, since this means inner ones are done
571 before outer ones. */
572 for (i
= max_loop_num
-1; i
>= 0; i
--)
573 if (! loop_invalid
[i
] && loop_number_loop_ends
[i
])
574 scan_loop (loop_number_loop_starts
[i
], loop_number_loop_ends
[i
],
575 loop_number_loop_cont
[i
], unroll_p
, bct_p
);
577 /* If debugging and unrolling loops, we must replicate the tree nodes
578 corresponding to the blocks inside the loop, so that the original one
579 to one mapping will remain. */
580 if (unroll_p
&& write_symbols
!= NO_DEBUG
)
581 unroll_block_trees ();
583 end_alias_analysis ();
586 /* Returns the next insn, in execution order, after INSN. START and
587 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
588 respectively. LOOP_TOP, if non-NULL, is the top of the loop in the
589 insn-stream; it is used with loops that are entered near the
593 next_insn_in_loop (insn
, start
, end
, loop_top
)
599 insn
= NEXT_INSN (insn
);
604 /* Go to the top of the loop, and continue there. */
618 /* Optimize one loop whose start is LOOP_START and end is END.
619 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
621 LOOP_CONT is the NOTE_INSN_LOOP_CONT. */
623 /* ??? Could also move memory writes out of loops if the destination address
624 is invariant, the source is invariant, the memory write is not volatile,
625 and if we can prove that no read inside the loop can read this address
626 before the write occurs. If there is a read of this address after the
627 write, then we can also mark the memory read as invariant. */
630 scan_loop (loop_start
, end
, loop_cont
, unroll_p
, bct_p
)
631 rtx loop_start
, end
, loop_cont
;
636 /* 1 if we are scanning insns that could be executed zero times. */
638 /* 1 if we are scanning insns that might never be executed
639 due to a subroutine call which might exit before they are reached. */
641 /* For a rotated loop that is entered near the bottom,
642 this is the label at the top. Otherwise it is zero. */
644 /* Jump insn that enters the loop, or 0 if control drops in. */
645 rtx loop_entry_jump
= 0;
646 /* Place in the loop where control enters. */
648 /* Number of insns in the loop. */
653 /* The SET from an insn, if it is the only SET in the insn. */
655 /* Chain describing insns movable in current loop. */
656 struct movable
*movables
= 0;
657 /* Last element in `movables' -- so we can add elements at the end. */
658 struct movable
*last_movable
= 0;
659 /* Ratio of extra register life span we can justify
660 for saving an instruction. More if loop doesn't call subroutines
661 since in that case saving an insn makes more difference
662 and more registers are available. */
664 /* Nonzero if we are scanning instructions in a sub-loop. */
668 /* Determine whether this loop starts with a jump down to a test at
669 the end. This will occur for a small number of loops with a test
670 that is too complex to duplicate in front of the loop.
672 We search for the first insn or label in the loop, skipping NOTEs.
673 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
674 (because we might have a loop executed only once that contains a
675 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
676 (in case we have a degenerate loop).
678 Note that if we mistakenly think that a loop is entered at the top
679 when, in fact, it is entered at the exit test, the only effect will be
680 slightly poorer optimization. Making the opposite error can generate
681 incorrect code. Since very few loops now start with a jump to the
682 exit test, the code here to detect that case is very conservative. */
684 for (p
= NEXT_INSN (loop_start
);
686 && GET_CODE (p
) != CODE_LABEL
&& GET_RTX_CLASS (GET_CODE (p
)) != 'i'
687 && (GET_CODE (p
) != NOTE
688 || (NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_BEG
689 && NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_END
));
695 /* Set up variables describing this loop. */
696 prescan_loop (loop_start
, end
);
697 threshold
= (loop_has_call
? 1 : 2) * (1 + n_non_fixed_regs
);
699 /* If loop has a jump before the first label,
700 the true entry is the target of that jump.
701 Start scan from there.
702 But record in LOOP_TOP the place where the end-test jumps
703 back to so we can scan that after the end of the loop. */
704 if (GET_CODE (p
) == JUMP_INSN
)
708 /* Loop entry must be unconditional jump (and not a RETURN) */
710 && JUMP_LABEL (p
) != 0
711 /* Check to see whether the jump actually
712 jumps out of the loop (meaning it's no loop).
713 This case can happen for things like
714 do {..} while (0). If this label was generated previously
715 by loop, we can't tell anything about it and have to reject
717 && INSN_IN_RANGE_P (JUMP_LABEL (p
), loop_start
, end
))
719 loop_top
= next_label (scan_start
);
720 scan_start
= JUMP_LABEL (p
);
724 /* If SCAN_START was an insn created by loop, we don't know its luid
725 as required by loop_reg_used_before_p. So skip such loops. (This
726 test may never be true, but it's best to play it safe.)
728 Also, skip loops where we do not start scanning at a label. This
729 test also rejects loops starting with a JUMP_INSN that failed the
732 if (INSN_UID (scan_start
) >= max_uid_for_loop
733 || GET_CODE (scan_start
) != CODE_LABEL
)
735 if (loop_dump_stream
)
736 fprintf (loop_dump_stream
, "\nLoop from %d to %d is phony.\n\n",
737 INSN_UID (loop_start
), INSN_UID (end
));
741 /* Count number of times each reg is set during this loop.
742 Set VARRAY_CHAR (may_not_optimize, I) if it is not safe to move out
743 the setting of register I. Set VARRAY_RTX (reg_single_usage, I). */
745 /* Allocate extra space for REGS that might be created by
746 load_mems. We allocate a little extra slop as well, in the hopes
747 that even after the moving of movables creates some new registers
748 we won't have to reallocate these arrays. However, we do grow
749 the arrays, if necessary, in load_mems_recount_loop_regs_set. */
750 nregs
= max_reg_num () + loop_mems_idx
+ 16;
751 VARRAY_INT_INIT (set_in_loop
, nregs
, "set_in_loop");
752 VARRAY_INT_INIT (n_times_set
, nregs
, "n_times_set");
753 VARRAY_CHAR_INIT (may_not_optimize
, nregs
, "may_not_optimize");
754 VARRAY_RTX_INIT (reg_single_usage
, nregs
, "reg_single_usage");
756 count_loop_regs_set (loop_top
? loop_top
: loop_start
, end
,
757 may_not_optimize
, reg_single_usage
, &insn_count
, nregs
);
759 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
761 VARRAY_CHAR (may_not_optimize
, i
) = 1;
762 VARRAY_INT (set_in_loop
, i
) = 1;
765 #ifdef AVOID_CCMODE_COPIES
766 /* Don't try to move insns which set CC registers if we should not
767 create CCmode register copies. */
768 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
769 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx
[i
])) == MODE_CC
)
770 VARRAY_CHAR (may_not_optimize
, i
) = 1;
773 bcopy ((char *) &set_in_loop
->data
,
774 (char *) &n_times_set
->data
, nregs
* sizeof (int));
776 if (loop_dump_stream
)
778 fprintf (loop_dump_stream
, "\nLoop from %d to %d: %d real insns.\n",
779 INSN_UID (loop_start
), INSN_UID (end
), insn_count
);
781 fprintf (loop_dump_stream
, "Continue at insn %d.\n",
782 INSN_UID (loop_continue
));
785 /* Scan through the loop finding insns that are safe to move.
786 Set set_in_loop negative for the reg being set, so that
787 this reg will be considered invariant for subsequent insns.
788 We consider whether subsequent insns use the reg
789 in deciding whether it is worth actually moving.
791 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
792 and therefore it is possible that the insns we are scanning
793 would never be executed. At such times, we must make sure
794 that it is safe to execute the insn once instead of zero times.
795 When MAYBE_NEVER is 0, all insns will be executed at least once
796 so that is not a problem. */
798 for (p
= next_insn_in_loop (scan_start
, scan_start
, end
, loop_top
);
800 p
= next_insn_in_loop (p
, scan_start
, end
, loop_top
))
802 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i'
803 && find_reg_note (p
, REG_LIBCALL
, NULL_RTX
))
805 else if (GET_RTX_CLASS (GET_CODE (p
)) == 'i'
806 && find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
809 if (GET_CODE (p
) == INSN
810 && (set
= single_set (p
))
811 && GET_CODE (SET_DEST (set
)) == REG
812 && ! VARRAY_CHAR (may_not_optimize
, REGNO (SET_DEST (set
))))
817 rtx src
= SET_SRC (set
);
818 rtx dependencies
= 0;
820 /* Figure out what to use as a source of this insn. If a REG_EQUIV
821 note is given or if a REG_EQUAL note with a constant operand is
822 specified, use it as the source and mark that we should move
823 this insn by calling emit_move_insn rather that duplicating the
826 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
828 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
830 src
= XEXP (temp
, 0), move_insn
= 1;
833 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
834 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
835 src
= XEXP (temp
, 0), move_insn
= 1;
836 if (temp
&& find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
838 src
= XEXP (temp
, 0);
839 /* A libcall block can use regs that don't appear in
840 the equivalent expression. To move the libcall,
841 we must move those regs too. */
842 dependencies
= libcall_other_reg (p
, src
);
846 /* Don't try to optimize a register that was made
847 by loop-optimization for an inner loop.
848 We don't know its life-span, so we can't compute the benefit. */
849 if (REGNO (SET_DEST (set
)) >= max_reg_before_loop
)
851 else if (/* The register is used in basic blocks other
852 than the one where it is set (meaning that
853 something after this point in the loop might
854 depend on its value before the set). */
855 ! reg_in_basic_block_p (p
, SET_DEST (set
))
856 /* And the set is not guaranteed to be executed one
857 the loop starts, or the value before the set is
858 needed before the set occurs...
860 ??? Note we have quadratic behaviour here, mitigated
861 by the fact that the previous test will often fail for
862 large loops. Rather than re-scanning the entire loop
863 each time for register usage, we should build tables
864 of the register usage and use them here instead. */
866 || loop_reg_used_before_p (set
, p
, loop_start
,
868 /* It is unsafe to move the set.
870 This code used to consider it OK to move a set of a variable
871 which was not created by the user and not used in an exit test.
872 That behavior is incorrect and was removed. */
874 else if ((tem
= invariant_p (src
))
875 && (dependencies
== 0
876 || (tem2
= invariant_p (dependencies
)) != 0)
877 && (VARRAY_INT (set_in_loop
,
878 REGNO (SET_DEST (set
))) == 1
880 = consec_sets_invariant_p
882 VARRAY_INT (set_in_loop
, REGNO (SET_DEST (set
))),
884 /* If the insn can cause a trap (such as divide by zero),
885 can't move it unless it's guaranteed to be executed
886 once loop is entered. Even a function call might
887 prevent the trap insn from being reached
888 (since it might exit!) */
889 && ! ((maybe_never
|| call_passed
)
890 && may_trap_p (src
)))
892 register struct movable
*m
;
893 register int regno
= REGNO (SET_DEST (set
));
895 /* A potential lossage is where we have a case where two insns
896 can be combined as long as they are both in the loop, but
897 we move one of them outside the loop. For large loops,
898 this can lose. The most common case of this is the address
899 of a function being called.
901 Therefore, if this register is marked as being used exactly
902 once if we are in a loop with calls (a "large loop"), see if
903 we can replace the usage of this register with the source
904 of this SET. If we can, delete this insn.
906 Don't do this if P has a REG_RETVAL note or if we have
907 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
910 && VARRAY_RTX (reg_single_usage
, regno
) != 0
911 && VARRAY_RTX (reg_single_usage
, regno
) != const0_rtx
912 && REGNO_FIRST_UID (regno
) == INSN_UID (p
)
913 && (REGNO_LAST_UID (regno
)
914 == INSN_UID (VARRAY_RTX (reg_single_usage
, regno
)))
915 && VARRAY_INT (set_in_loop
, regno
) == 1
916 && ! side_effects_p (SET_SRC (set
))
917 && ! find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
918 && (! SMALL_REGISTER_CLASSES
919 || (! (GET_CODE (SET_SRC (set
)) == REG
920 && REGNO (SET_SRC (set
)) < FIRST_PSEUDO_REGISTER
)))
921 /* This test is not redundant; SET_SRC (set) might be
922 a call-clobbered register and the life of REGNO
923 might span a call. */
924 && ! modified_between_p (SET_SRC (set
), p
,
926 (reg_single_usage
, regno
))
927 && no_labels_between_p (p
, VARRAY_RTX (reg_single_usage
, regno
))
928 && validate_replace_rtx (SET_DEST (set
), SET_SRC (set
),
930 (reg_single_usage
, regno
)))
932 /* Replace any usage in a REG_EQUAL note. Must copy the
933 new source, so that we don't get rtx sharing between the
934 SET_SOURCE and REG_NOTES of insn p. */
935 REG_NOTES (VARRAY_RTX (reg_single_usage
, regno
))
936 = replace_rtx (REG_NOTES (VARRAY_RTX
937 (reg_single_usage
, regno
)),
938 SET_DEST (set
), copy_rtx (SET_SRC (set
)));
941 NOTE_LINE_NUMBER (p
) = NOTE_INSN_DELETED
;
942 NOTE_SOURCE_FILE (p
) = 0;
943 VARRAY_INT (set_in_loop
, regno
) = 0;
947 m
= (struct movable
*) alloca (sizeof (struct movable
));
951 m
->dependencies
= dependencies
;
952 m
->set_dest
= SET_DEST (set
);
954 m
->consec
= VARRAY_INT (set_in_loop
,
955 REGNO (SET_DEST (set
))) - 1;
959 m
->move_insn
= move_insn
;
960 m
->move_insn_first
= 0;
961 m
->is_equiv
= (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
962 m
->savemode
= VOIDmode
;
964 /* Set M->cond if either invariant_p or consec_sets_invariant_p
965 returned 2 (only conditionally invariant). */
966 m
->cond
= ((tem
| tem1
| tem2
) > 1);
967 m
->global
= (uid_luid
[REGNO_LAST_UID (regno
)] > INSN_LUID (end
)
968 || uid_luid
[REGNO_FIRST_UID (regno
)] < INSN_LUID (loop_start
));
970 m
->lifetime
= (uid_luid
[REGNO_LAST_UID (regno
)]
971 - uid_luid
[REGNO_FIRST_UID (regno
)]);
972 m
->savings
= VARRAY_INT (n_times_set
, regno
);
973 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
974 m
->savings
+= libcall_benefit (p
);
975 VARRAY_INT (set_in_loop
, regno
) = move_insn
? -2 : -1;
976 /* Add M to the end of the chain MOVABLES. */
980 last_movable
->next
= m
;
985 /* It is possible for the first instruction to have a
986 REG_EQUAL note but a non-invariant SET_SRC, so we must
987 remember the status of the first instruction in case
988 the last instruction doesn't have a REG_EQUAL note. */
989 m
->move_insn_first
= m
->move_insn
;
991 /* Skip this insn, not checking REG_LIBCALL notes. */
992 p
= next_nonnote_insn (p
);
993 /* Skip the consecutive insns, if there are any. */
994 p
= skip_consec_insns (p
, m
->consec
);
995 /* Back up to the last insn of the consecutive group. */
996 p
= prev_nonnote_insn (p
);
998 /* We must now reset m->move_insn, m->is_equiv, and possibly
999 m->set_src to correspond to the effects of all the
1001 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
1003 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
1006 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
1007 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
1008 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
1013 m
->is_equiv
= (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
1016 /* If this register is always set within a STRICT_LOW_PART
1017 or set to zero, then its high bytes are constant.
1018 So clear them outside the loop and within the loop
1019 just load the low bytes.
1020 We must check that the machine has an instruction to do so.
1021 Also, if the value loaded into the register
1022 depends on the same register, this cannot be done. */
1023 else if (SET_SRC (set
) == const0_rtx
1024 && GET_CODE (NEXT_INSN (p
)) == INSN
1025 && (set1
= single_set (NEXT_INSN (p
)))
1026 && GET_CODE (set1
) == SET
1027 && (GET_CODE (SET_DEST (set1
)) == STRICT_LOW_PART
)
1028 && (GET_CODE (XEXP (SET_DEST (set1
), 0)) == SUBREG
)
1029 && (SUBREG_REG (XEXP (SET_DEST (set1
), 0))
1031 && !reg_mentioned_p (SET_DEST (set
), SET_SRC (set1
)))
1033 register int regno
= REGNO (SET_DEST (set
));
1034 if (VARRAY_INT (set_in_loop
, regno
) == 2)
1036 register struct movable
*m
;
1037 m
= (struct movable
*) alloca (sizeof (struct movable
));
1040 m
->set_dest
= SET_DEST (set
);
1041 m
->dependencies
= 0;
1047 m
->move_insn_first
= 0;
1049 /* If the insn may not be executed on some cycles,
1050 we can't clear the whole reg; clear just high part.
1051 Not even if the reg is used only within this loop.
1058 Clearing x before the inner loop could clobber a value
1059 being saved from the last time around the outer loop.
1060 However, if the reg is not used outside this loop
1061 and all uses of the register are in the same
1062 basic block as the store, there is no problem.
1064 If this insn was made by loop, we don't know its
1065 INSN_LUID and hence must make a conservative
1067 m
->global
= (INSN_UID (p
) >= max_uid_for_loop
1068 || (uid_luid
[REGNO_LAST_UID (regno
)]
1070 || (uid_luid
[REGNO_FIRST_UID (regno
)]
1072 || (labels_in_range_p
1073 (p
, uid_luid
[REGNO_FIRST_UID (regno
)])));
1074 if (maybe_never
&& m
->global
)
1075 m
->savemode
= GET_MODE (SET_SRC (set1
));
1077 m
->savemode
= VOIDmode
;
1081 m
->lifetime
= (uid_luid
[REGNO_LAST_UID (regno
)]
1082 - uid_luid
[REGNO_FIRST_UID (regno
)]);
1084 VARRAY_INT (set_in_loop
, regno
) = -1;
1085 /* Add M to the end of the chain MOVABLES. */
1089 last_movable
->next
= m
;
1094 /* Past a call insn, we get to insns which might not be executed
1095 because the call might exit. This matters for insns that trap.
1096 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
1097 so they don't count. */
1098 else if (GET_CODE (p
) == CALL_INSN
&& ! in_libcall
)
1100 /* Past a label or a jump, we get to insns for which we
1101 can't count on whether or how many times they will be
1102 executed during each iteration. Therefore, we can
1103 only move out sets of trivial variables
1104 (those not used after the loop). */
1105 /* Similar code appears twice in strength_reduce. */
1106 else if ((GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
)
1107 /* If we enter the loop in the middle, and scan around to the
1108 beginning, don't set maybe_never for that. This must be an
1109 unconditional jump, otherwise the code at the top of the
1110 loop might never be executed. Unconditional jumps are
1111 followed a by barrier then loop end. */
1112 && ! (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == loop_top
1113 && NEXT_INSN (NEXT_INSN (p
)) == end
1114 && simplejump_p (p
)))
1116 else if (GET_CODE (p
) == NOTE
)
1118 /* At the virtual top of a converted loop, insns are again known to
1119 be executed: logically, the loop begins here even though the exit
1120 code has been duplicated. */
1121 if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
&& loop_depth
== 0)
1122 maybe_never
= call_passed
= 0;
1123 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
1125 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)
1130 /* If one movable subsumes another, ignore that other. */
1132 ignore_some_movables (movables
);
1134 /* For each movable insn, see if the reg that it loads
1135 leads when it dies right into another conditionally movable insn.
1136 If so, record that the second insn "forces" the first one,
1137 since the second can be moved only if the first is. */
1139 force_movables (movables
);
1141 /* See if there are multiple movable insns that load the same value.
1142 If there are, make all but the first point at the first one
1143 through the `match' field, and add the priorities of them
1144 all together as the priority of the first. */
1146 combine_movables (movables
, nregs
);
1148 /* Now consider each movable insn to decide whether it is worth moving.
1149 Store 0 in set_in_loop for each reg that is moved.
1151 Generally this increases code size, so do not move moveables when
1152 optimizing for code size. */
1154 if (! optimize_size
)
1155 move_movables (movables
, threshold
,
1156 insn_count
, loop_start
, end
, nregs
);
1158 /* Now candidates that still are negative are those not moved.
1159 Change set_in_loop to indicate that those are not actually invariant. */
1160 for (i
= 0; i
< nregs
; i
++)
1161 if (VARRAY_INT (set_in_loop
, i
) < 0)
1162 VARRAY_INT (set_in_loop
, i
) = VARRAY_INT (n_times_set
, i
);
1164 /* Now that we've moved some things out of the loop, we might be able to
1165 hoist even more memory references. */
1166 load_mems_and_recount_loop_regs_set (scan_start
, end
, loop_top
,
1167 loop_start
, &insn_count
);
1169 if (flag_strength_reduce
)
1171 the_movables
= movables
;
1172 strength_reduce (scan_start
, end
, loop_top
,
1173 insn_count
, loop_start
, end
, loop_cont
, unroll_p
, bct_p
);
1176 VARRAY_FREE (reg_single_usage
);
1177 VARRAY_FREE (set_in_loop
);
1178 VARRAY_FREE (n_times_set
);
1179 VARRAY_FREE (may_not_optimize
);
1182 /* Add elements to *OUTPUT to record all the pseudo-regs
1183 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1186 record_excess_regs (in_this
, not_in_this
, output
)
1187 rtx in_this
, not_in_this
;
1194 code
= GET_CODE (in_this
);
1208 if (REGNO (in_this
) >= FIRST_PSEUDO_REGISTER
1209 && ! reg_mentioned_p (in_this
, not_in_this
))
1210 *output
= gen_rtx_EXPR_LIST (VOIDmode
, in_this
, *output
);
1217 fmt
= GET_RTX_FORMAT (code
);
1218 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1225 for (j
= 0; j
< XVECLEN (in_this
, i
); j
++)
1226 record_excess_regs (XVECEXP (in_this
, i
, j
), not_in_this
, output
);
1230 record_excess_regs (XEXP (in_this
, i
), not_in_this
, output
);
1236 /* Check what regs are referred to in the libcall block ending with INSN,
1237 aside from those mentioned in the equivalent value.
1238 If there are none, return 0.
1239 If there are one or more, return an EXPR_LIST containing all of them. */
1242 libcall_other_reg (insn
, equiv
)
1245 rtx note
= find_reg_note (insn
, REG_RETVAL
, NULL_RTX
);
1246 rtx p
= XEXP (note
, 0);
1249 /* First, find all the regs used in the libcall block
1250 that are not mentioned as inputs to the result. */
1254 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
1255 || GET_CODE (p
) == CALL_INSN
)
1256 record_excess_regs (PATTERN (p
), equiv
, &output
);
1263 /* Return 1 if all uses of REG
1264 are between INSN and the end of the basic block. */
1267 reg_in_basic_block_p (insn
, reg
)
1270 int regno
= REGNO (reg
);
1273 if (REGNO_FIRST_UID (regno
) != INSN_UID (insn
))
1276 /* Search this basic block for the already recorded last use of the reg. */
1277 for (p
= insn
; p
; p
= NEXT_INSN (p
))
1279 switch (GET_CODE (p
))
1286 /* Ordinary insn: if this is the last use, we win. */
1287 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1292 /* Jump insn: if this is the last use, we win. */
1293 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1295 /* Otherwise, it's the end of the basic block, so we lose. */
1300 /* It's the end of the basic block, so we lose. */
1308 /* The "last use" doesn't follow the "first use"?? */
1312 /* Compute the benefit of eliminating the insns in the block whose
1313 last insn is LAST. This may be a group of insns used to compute a
1314 value directly or can contain a library call. */
1317 libcall_benefit (last
)
1323 for (insn
= XEXP (find_reg_note (last
, REG_RETVAL
, NULL_RTX
), 0);
1324 insn
!= last
; insn
= NEXT_INSN (insn
))
1326 if (GET_CODE (insn
) == CALL_INSN
)
1327 benefit
+= 10; /* Assume at least this many insns in a library
1329 else if (GET_CODE (insn
) == INSN
1330 && GET_CODE (PATTERN (insn
)) != USE
1331 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
1338 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1341 skip_consec_insns (insn
, count
)
1345 for (; count
> 0; count
--)
1349 /* If first insn of libcall sequence, skip to end. */
1350 /* Do this at start of loop, since INSN is guaranteed to
1352 if (GET_CODE (insn
) != NOTE
1353 && (temp
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)))
1354 insn
= XEXP (temp
, 0);
1356 do insn
= NEXT_INSN (insn
);
1357 while (GET_CODE (insn
) == NOTE
);
1363 /* Ignore any movable whose insn falls within a libcall
1364 which is part of another movable.
1365 We make use of the fact that the movable for the libcall value
1366 was made later and so appears later on the chain. */
1369 ignore_some_movables (movables
)
1370 struct movable
*movables
;
1372 register struct movable
*m
, *m1
;
1374 for (m
= movables
; m
; m
= m
->next
)
1376 /* Is this a movable for the value of a libcall? */
1377 rtx note
= find_reg_note (m
->insn
, REG_RETVAL
, NULL_RTX
);
1381 /* Check for earlier movables inside that range,
1382 and mark them invalid. We cannot use LUIDs here because
1383 insns created by loop.c for prior loops don't have LUIDs.
1384 Rather than reject all such insns from movables, we just
1385 explicitly check each insn in the libcall (since invariant
1386 libcalls aren't that common). */
1387 for (insn
= XEXP (note
, 0); insn
!= m
->insn
; insn
= NEXT_INSN (insn
))
1388 for (m1
= movables
; m1
!= m
; m1
= m1
->next
)
1389 if (m1
->insn
== insn
)
1395 /* For each movable insn, see if the reg that it loads
1396 leads when it dies right into another conditionally movable insn.
1397 If so, record that the second insn "forces" the first one,
1398 since the second can be moved only if the first is. */
1401 force_movables (movables
)
1402 struct movable
*movables
;
1404 register struct movable
*m
, *m1
;
1405 for (m1
= movables
; m1
; m1
= m1
->next
)
1406 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1407 if (!m1
->partial
&& !m1
->done
)
1409 int regno
= m1
->regno
;
1410 for (m
= m1
->next
; m
; m
= m
->next
)
1411 /* ??? Could this be a bug? What if CSE caused the
1412 register of M1 to be used after this insn?
1413 Since CSE does not update regno_last_uid,
1414 this insn M->insn might not be where it dies.
1415 But very likely this doesn't matter; what matters is
1416 that M's reg is computed from M1's reg. */
1417 if (INSN_UID (m
->insn
) == REGNO_LAST_UID (regno
)
1420 if (m
!= 0 && m
->set_src
== m1
->set_dest
1421 /* If m->consec, m->set_src isn't valid. */
1425 /* Increase the priority of the moving the first insn
1426 since it permits the second to be moved as well. */
1430 m1
->lifetime
+= m
->lifetime
;
1431 m1
->savings
+= m
->savings
;
1436 /* Find invariant expressions that are equal and can be combined into
1440 combine_movables (movables
, nregs
)
1441 struct movable
*movables
;
1444 register struct movable
*m
;
1445 char *matched_regs
= (char *) alloca (nregs
);
1446 enum machine_mode mode
;
1448 /* Regs that are set more than once are not allowed to match
1449 or be matched. I'm no longer sure why not. */
1450 /* Perhaps testing m->consec_sets would be more appropriate here? */
1452 for (m
= movables
; m
; m
= m
->next
)
1453 if (m
->match
== 0 && VARRAY_INT (n_times_set
, m
->regno
) == 1 && !m
->partial
)
1455 register struct movable
*m1
;
1456 int regno
= m
->regno
;
1458 bzero (matched_regs
, nregs
);
1459 matched_regs
[regno
] = 1;
1461 /* We want later insns to match the first one. Don't make the first
1462 one match any later ones. So start this loop at m->next. */
1463 for (m1
= m
->next
; m1
; m1
= m1
->next
)
1464 if (m
!= m1
&& m1
->match
== 0 && VARRAY_INT (n_times_set
, m1
->regno
) == 1
1465 /* A reg used outside the loop mustn't be eliminated. */
1467 /* A reg used for zero-extending mustn't be eliminated. */
1469 && (matched_regs
[m1
->regno
]
1472 /* Can combine regs with different modes loaded from the
1473 same constant only if the modes are the same or
1474 if both are integer modes with M wider or the same
1475 width as M1. The check for integer is redundant, but
1476 safe, since the only case of differing destination
1477 modes with equal sources is when both sources are
1478 VOIDmode, i.e., CONST_INT. */
1479 (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
)
1480 || (GET_MODE_CLASS (GET_MODE (m
->set_dest
)) == MODE_INT
1481 && GET_MODE_CLASS (GET_MODE (m1
->set_dest
)) == MODE_INT
1482 && (GET_MODE_BITSIZE (GET_MODE (m
->set_dest
))
1483 >= GET_MODE_BITSIZE (GET_MODE (m1
->set_dest
)))))
1484 /* See if the source of M1 says it matches M. */
1485 && ((GET_CODE (m1
->set_src
) == REG
1486 && matched_regs
[REGNO (m1
->set_src
)])
1487 || rtx_equal_for_loop_p (m
->set_src
, m1
->set_src
,
1489 && ((m
->dependencies
== m1
->dependencies
)
1490 || rtx_equal_p (m
->dependencies
, m1
->dependencies
)))
1492 m
->lifetime
+= m1
->lifetime
;
1493 m
->savings
+= m1
->savings
;
1496 matched_regs
[m1
->regno
] = 1;
1500 /* Now combine the regs used for zero-extension.
1501 This can be done for those not marked `global'
1502 provided their lives don't overlap. */
1504 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1505 mode
= GET_MODE_WIDER_MODE (mode
))
1507 register struct movable
*m0
= 0;
1509 /* Combine all the registers for extension from mode MODE.
1510 Don't combine any that are used outside this loop. */
1511 for (m
= movables
; m
; m
= m
->next
)
1512 if (m
->partial
&& ! m
->global
1513 && mode
== GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m
->insn
)))))
1515 register struct movable
*m1
;
1516 int first
= uid_luid
[REGNO_FIRST_UID (m
->regno
)];
1517 int last
= uid_luid
[REGNO_LAST_UID (m
->regno
)];
1521 /* First one: don't check for overlap, just record it. */
1526 /* Make sure they extend to the same mode.
1527 (Almost always true.) */
1528 if (GET_MODE (m
->set_dest
) != GET_MODE (m0
->set_dest
))
1531 /* We already have one: check for overlap with those
1532 already combined together. */
1533 for (m1
= movables
; m1
!= m
; m1
= m1
->next
)
1534 if (m1
== m0
|| (m1
->partial
&& m1
->match
== m0
))
1535 if (! (uid_luid
[REGNO_FIRST_UID (m1
->regno
)] > last
1536 || uid_luid
[REGNO_LAST_UID (m1
->regno
)] < first
))
1539 /* No overlap: we can combine this with the others. */
1540 m0
->lifetime
+= m
->lifetime
;
1541 m0
->savings
+= m
->savings
;
1550 /* Return 1 if regs X and Y will become the same if moved. */
1553 regs_match_p (x
, y
, movables
)
1555 struct movable
*movables
;
1559 struct movable
*mx
, *my
;
1561 for (mx
= movables
; mx
; mx
= mx
->next
)
1562 if (mx
->regno
== xn
)
1565 for (my
= movables
; my
; my
= my
->next
)
1566 if (my
->regno
== yn
)
1570 && ((mx
->match
== my
->match
&& mx
->match
!= 0)
1572 || mx
== my
->match
));
1575 /* Return 1 if X and Y are identical-looking rtx's.
1576 This is the Lisp function EQUAL for rtx arguments.
1578 If two registers are matching movables or a movable register and an
1579 equivalent constant, consider them equal. */
1582 rtx_equal_for_loop_p (x
, y
, movables
)
1584 struct movable
*movables
;
1588 register struct movable
*m
;
1589 register enum rtx_code code
;
1594 if (x
== 0 || y
== 0)
1597 code
= GET_CODE (x
);
1599 /* If we have a register and a constant, they may sometimes be
1601 if (GET_CODE (x
) == REG
&& VARRAY_INT (set_in_loop
, REGNO (x
)) == -2
1604 for (m
= movables
; m
; m
= m
->next
)
1605 if (m
->move_insn
&& m
->regno
== REGNO (x
)
1606 && rtx_equal_p (m
->set_src
, y
))
1609 else if (GET_CODE (y
) == REG
&& VARRAY_INT (set_in_loop
, REGNO (y
)) == -2
1612 for (m
= movables
; m
; m
= m
->next
)
1613 if (m
->move_insn
&& m
->regno
== REGNO (y
)
1614 && rtx_equal_p (m
->set_src
, x
))
1618 /* Otherwise, rtx's of different codes cannot be equal. */
1619 if (code
!= GET_CODE (y
))
1622 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1623 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1625 if (GET_MODE (x
) != GET_MODE (y
))
1628 /* These three types of rtx's can be compared nonrecursively. */
1630 return (REGNO (x
) == REGNO (y
) || regs_match_p (x
, y
, movables
));
1632 if (code
== LABEL_REF
)
1633 return XEXP (x
, 0) == XEXP (y
, 0);
1634 if (code
== SYMBOL_REF
)
1635 return XSTR (x
, 0) == XSTR (y
, 0);
1637 /* Compare the elements. If any pair of corresponding elements
1638 fail to match, return 0 for the whole things. */
1640 fmt
= GET_RTX_FORMAT (code
);
1641 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1646 if (XWINT (x
, i
) != XWINT (y
, i
))
1651 if (XINT (x
, i
) != XINT (y
, i
))
1656 /* Two vectors must have the same length. */
1657 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1660 /* And the corresponding elements must match. */
1661 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1662 if (rtx_equal_for_loop_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
), movables
) == 0)
1667 if (rtx_equal_for_loop_p (XEXP (x
, i
), XEXP (y
, i
), movables
) == 0)
1672 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1677 /* These are just backpointers, so they don't matter. */
1683 /* It is believed that rtx's at this level will never
1684 contain anything but integers and other rtx's,
1685 except for within LABEL_REFs and SYMBOL_REFs. */
1693 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1694 insns in INSNS which use thet reference. */
1697 add_label_notes (x
, insns
)
1701 enum rtx_code code
= GET_CODE (x
);
1706 if (code
== LABEL_REF
&& !LABEL_REF_NONLOCAL_P (x
))
1708 /* This code used to ignore labels that referred to dispatch tables to
1709 avoid flow generating (slighly) worse code.
1711 We no longer ignore such label references (see LABEL_REF handling in
1712 mark_jump_label for additional information). */
1713 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
1714 if (reg_mentioned_p (XEXP (x
, 0), insn
))
1715 REG_NOTES (insn
) = gen_rtx_EXPR_LIST (REG_LABEL
, XEXP (x
, 0),
1719 fmt
= GET_RTX_FORMAT (code
);
1720 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1723 add_label_notes (XEXP (x
, i
), insns
);
1724 else if (fmt
[i
] == 'E')
1725 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1726 add_label_notes (XVECEXP (x
, i
, j
), insns
);
1730 /* Scan MOVABLES, and move the insns that deserve to be moved.
1731 If two matching movables are combined, replace one reg with the
1732 other throughout. */
1735 move_movables (movables
, threshold
, insn_count
, loop_start
, end
, nregs
)
1736 struct movable
*movables
;
1744 register struct movable
*m
;
1746 /* Map of pseudo-register replacements to handle combining
1747 when we move several insns that load the same value
1748 into different pseudo-registers. */
1749 rtx
*reg_map
= (rtx
*) alloca (nregs
* sizeof (rtx
));
1750 char *already_moved
= (char *) alloca (nregs
);
1752 bzero (already_moved
, nregs
);
1753 bzero ((char *) reg_map
, nregs
* sizeof (rtx
));
1757 for (m
= movables
; m
; m
= m
->next
)
1759 /* Describe this movable insn. */
1761 if (loop_dump_stream
)
1763 fprintf (loop_dump_stream
, "Insn %d: regno %d (life %d), ",
1764 INSN_UID (m
->insn
), m
->regno
, m
->lifetime
);
1766 fprintf (loop_dump_stream
, "consec %d, ", m
->consec
);
1768 fprintf (loop_dump_stream
, "cond ");
1770 fprintf (loop_dump_stream
, "force ");
1772 fprintf (loop_dump_stream
, "global ");
1774 fprintf (loop_dump_stream
, "done ");
1776 fprintf (loop_dump_stream
, "move-insn ");
1778 fprintf (loop_dump_stream
, "matches %d ",
1779 INSN_UID (m
->match
->insn
));
1781 fprintf (loop_dump_stream
, "forces %d ",
1782 INSN_UID (m
->forces
->insn
));
1785 /* Count movables. Value used in heuristics in strength_reduce. */
1788 /* Ignore the insn if it's already done (it matched something else).
1789 Otherwise, see if it is now safe to move. */
1793 || (1 == invariant_p (m
->set_src
)
1794 && (m
->dependencies
== 0
1795 || 1 == invariant_p (m
->dependencies
))
1797 || 1 == consec_sets_invariant_p (m
->set_dest
,
1800 && (! m
->forces
|| m
->forces
->done
))
1804 int savings
= m
->savings
;
1806 /* We have an insn that is safe to move.
1807 Compute its desirability. */
1812 if (loop_dump_stream
)
1813 fprintf (loop_dump_stream
, "savings %d ", savings
);
1815 if (moved_once
[regno
] && loop_dump_stream
)
1816 fprintf (loop_dump_stream
, "halved since already moved ");
1818 /* An insn MUST be moved if we already moved something else
1819 which is safe only if this one is moved too: that is,
1820 if already_moved[REGNO] is nonzero. */
1822 /* An insn is desirable to move if the new lifetime of the
1823 register is no more than THRESHOLD times the old lifetime.
1824 If it's not desirable, it means the loop is so big
1825 that moving won't speed things up much,
1826 and it is liable to make register usage worse. */
1828 /* It is also desirable to move if it can be moved at no
1829 extra cost because something else was already moved. */
1831 if (already_moved
[regno
]
1832 || flag_move_all_movables
1833 || (threshold
* savings
* m
->lifetime
) >=
1834 (moved_once
[regno
] ? insn_count
* 2 : insn_count
)
1835 || (m
->forces
&& m
->forces
->done
1836 && VARRAY_INT (n_times_set
, m
->forces
->regno
) == 1))
1839 register struct movable
*m1
;
1842 /* Now move the insns that set the reg. */
1844 if (m
->partial
&& m
->match
)
1848 /* Find the end of this chain of matching regs.
1849 Thus, we load each reg in the chain from that one reg.
1850 And that reg is loaded with 0 directly,
1851 since it has ->match == 0. */
1852 for (m1
= m
; m1
->match
; m1
= m1
->match
);
1853 newpat
= gen_move_insn (SET_DEST (PATTERN (m
->insn
)),
1854 SET_DEST (PATTERN (m1
->insn
)));
1855 i1
= emit_insn_before (newpat
, loop_start
);
1857 /* Mark the moved, invariant reg as being allowed to
1858 share a hard reg with the other matching invariant. */
1859 REG_NOTES (i1
) = REG_NOTES (m
->insn
);
1860 r1
= SET_DEST (PATTERN (m
->insn
));
1861 r2
= SET_DEST (PATTERN (m1
->insn
));
1863 = gen_rtx_EXPR_LIST (VOIDmode
, r1
,
1864 gen_rtx_EXPR_LIST (VOIDmode
, r2
,
1866 delete_insn (m
->insn
);
1871 if (loop_dump_stream
)
1872 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
1874 /* If we are to re-generate the item being moved with a
1875 new move insn, first delete what we have and then emit
1876 the move insn before the loop. */
1877 else if (m
->move_insn
)
1881 for (count
= m
->consec
; count
>= 0; count
--)
1883 /* If this is the first insn of a library call sequence,
1885 if (GET_CODE (p
) != NOTE
1886 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
1889 /* If this is the last insn of a libcall sequence, then
1890 delete every insn in the sequence except the last.
1891 The last insn is handled in the normal manner. */
1892 if (GET_CODE (p
) != NOTE
1893 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
1895 temp
= XEXP (temp
, 0);
1897 temp
= delete_insn (temp
);
1901 p
= delete_insn (p
);
1903 /* simplify_giv_expr expects that it can walk the insns
1904 at m->insn forwards and see this old sequence we are
1905 tossing here. delete_insn does preserve the next
1906 pointers, but when we skip over a NOTE we must fix
1907 it up. Otherwise that code walks into the non-deleted
1909 while (p
&& GET_CODE (p
) == NOTE
)
1910 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
1914 emit_move_insn (m
->set_dest
, m
->set_src
);
1915 temp
= get_insns ();
1918 add_label_notes (m
->set_src
, temp
);
1920 i1
= emit_insns_before (temp
, loop_start
);
1921 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
1923 = gen_rtx_EXPR_LIST (m
->is_equiv
? REG_EQUIV
: REG_EQUAL
,
1924 m
->set_src
, REG_NOTES (i1
));
1926 if (loop_dump_stream
)
1927 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
1929 /* The more regs we move, the less we like moving them. */
1934 for (count
= m
->consec
; count
>= 0; count
--)
1938 /* If first insn of libcall sequence, skip to end. */
1939 /* Do this at start of loop, since p is guaranteed to
1941 if (GET_CODE (p
) != NOTE
1942 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
1945 /* If last insn of libcall sequence, move all
1946 insns except the last before the loop. The last
1947 insn is handled in the normal manner. */
1948 if (GET_CODE (p
) != NOTE
1949 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
1953 rtx fn_address_insn
= 0;
1956 for (temp
= XEXP (temp
, 0); temp
!= p
;
1957 temp
= NEXT_INSN (temp
))
1963 if (GET_CODE (temp
) == NOTE
)
1966 body
= PATTERN (temp
);
1968 /* Find the next insn after TEMP,
1969 not counting USE or NOTE insns. */
1970 for (next
= NEXT_INSN (temp
); next
!= p
;
1971 next
= NEXT_INSN (next
))
1972 if (! (GET_CODE (next
) == INSN
1973 && GET_CODE (PATTERN (next
)) == USE
)
1974 && GET_CODE (next
) != NOTE
)
1977 /* If that is the call, this may be the insn
1978 that loads the function address.
1980 Extract the function address from the insn
1981 that loads it into a register.
1982 If this insn was cse'd, we get incorrect code.
1984 So emit a new move insn that copies the
1985 function address into the register that the
1986 call insn will use. flow.c will delete any
1987 redundant stores that we have created. */
1988 if (GET_CODE (next
) == CALL_INSN
1989 && GET_CODE (body
) == SET
1990 && GET_CODE (SET_DEST (body
)) == REG
1991 && (n
= find_reg_note (temp
, REG_EQUAL
,
1994 fn_reg
= SET_SRC (body
);
1995 if (GET_CODE (fn_reg
) != REG
)
1996 fn_reg
= SET_DEST (body
);
1997 fn_address
= XEXP (n
, 0);
1998 fn_address_insn
= temp
;
2000 /* We have the call insn.
2001 If it uses the register we suspect it might,
2002 load it with the correct address directly. */
2003 if (GET_CODE (temp
) == CALL_INSN
2005 && reg_referenced_p (fn_reg
, body
))
2006 emit_insn_after (gen_move_insn (fn_reg
,
2010 if (GET_CODE (temp
) == CALL_INSN
)
2012 i1
= emit_call_insn_before (body
, loop_start
);
2013 /* Because the USAGE information potentially
2014 contains objects other than hard registers
2015 we need to copy it. */
2016 if (CALL_INSN_FUNCTION_USAGE (temp
))
2017 CALL_INSN_FUNCTION_USAGE (i1
)
2018 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp
));
2021 i1
= emit_insn_before (body
, loop_start
);
2024 if (temp
== fn_address_insn
)
2025 fn_address_insn
= i1
;
2026 REG_NOTES (i1
) = REG_NOTES (temp
);
2032 if (m
->savemode
!= VOIDmode
)
2034 /* P sets REG to zero; but we should clear only
2035 the bits that are not covered by the mode
2037 rtx reg
= m
->set_dest
;
2043 (GET_MODE (reg
), and_optab
, reg
,
2044 GEN_INT ((((HOST_WIDE_INT
) 1
2045 << GET_MODE_BITSIZE (m
->savemode
)))
2047 reg
, 1, OPTAB_LIB_WIDEN
);
2051 emit_move_insn (reg
, tem
);
2052 sequence
= gen_sequence ();
2054 i1
= emit_insn_before (sequence
, loop_start
);
2056 else if (GET_CODE (p
) == CALL_INSN
)
2058 i1
= emit_call_insn_before (PATTERN (p
), loop_start
);
2059 /* Because the USAGE information potentially
2060 contains objects other than hard registers
2061 we need to copy it. */
2062 if (CALL_INSN_FUNCTION_USAGE (p
))
2063 CALL_INSN_FUNCTION_USAGE (i1
)
2064 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p
));
2066 else if (count
== m
->consec
&& m
->move_insn_first
)
2068 /* The SET_SRC might not be invariant, so we must
2069 use the REG_EQUAL note. */
2071 emit_move_insn (m
->set_dest
, m
->set_src
);
2072 temp
= get_insns ();
2075 add_label_notes (m
->set_src
, temp
);
2077 i1
= emit_insns_before (temp
, loop_start
);
2078 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2080 = gen_rtx_EXPR_LIST ((m
->is_equiv
? REG_EQUIV
2082 m
->set_src
, REG_NOTES (i1
));
2085 i1
= emit_insn_before (PATTERN (p
), loop_start
);
2087 if (REG_NOTES (i1
) == 0)
2089 REG_NOTES (i1
) = REG_NOTES (p
);
2091 /* If there is a REG_EQUAL note present whose value
2092 is not loop invariant, then delete it, since it
2093 may cause problems with later optimization passes.
2094 It is possible for cse to create such notes
2095 like this as a result of record_jump_cond. */
2097 if ((temp
= find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2098 && ! invariant_p (XEXP (temp
, 0)))
2099 remove_note (i1
, temp
);
2105 if (loop_dump_stream
)
2106 fprintf (loop_dump_stream
, " moved to %d",
2109 /* If library call, now fix the REG_NOTES that contain
2110 insn pointers, namely REG_LIBCALL on FIRST
2111 and REG_RETVAL on I1. */
2112 if ((temp
= find_reg_note (i1
, REG_RETVAL
, NULL_RTX
)))
2114 XEXP (temp
, 0) = first
;
2115 temp
= find_reg_note (first
, REG_LIBCALL
, NULL_RTX
);
2116 XEXP (temp
, 0) = i1
;
2123 /* simplify_giv_expr expects that it can walk the insns
2124 at m->insn forwards and see this old sequence we are
2125 tossing here. delete_insn does preserve the next
2126 pointers, but when we skip over a NOTE we must fix
2127 it up. Otherwise that code walks into the non-deleted
2129 while (p
&& GET_CODE (p
) == NOTE
)
2130 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
2133 /* The more regs we move, the less we like moving them. */
2137 /* Any other movable that loads the same register
2139 already_moved
[regno
] = 1;
2141 /* This reg has been moved out of one loop. */
2142 moved_once
[regno
] = 1;
2144 /* The reg set here is now invariant. */
2146 VARRAY_INT (set_in_loop
, regno
) = 0;
2150 /* Change the length-of-life info for the register
2151 to say it lives at least the full length of this loop.
2152 This will help guide optimizations in outer loops. */
2154 if (uid_luid
[REGNO_FIRST_UID (regno
)] > INSN_LUID (loop_start
))
2155 /* This is the old insn before all the moved insns.
2156 We can't use the moved insn because it is out of range
2157 in uid_luid. Only the old insns have luids. */
2158 REGNO_FIRST_UID (regno
) = INSN_UID (loop_start
);
2159 if (uid_luid
[REGNO_LAST_UID (regno
)] < INSN_LUID (end
))
2160 REGNO_LAST_UID (regno
) = INSN_UID (end
);
2162 /* Combine with this moved insn any other matching movables. */
2165 for (m1
= movables
; m1
; m1
= m1
->next
)
2170 /* Schedule the reg loaded by M1
2171 for replacement so that shares the reg of M.
2172 If the modes differ (only possible in restricted
2173 circumstances, make a SUBREG.
2175 Note this assumes that the target dependent files
2176 treat REG and SUBREG equally, including within
2177 GO_IF_LEGITIMATE_ADDRESS and in all the
2178 predicates since we never verify that replacing the
2179 original register with a SUBREG results in a
2180 recognizable insn. */
2181 if (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
))
2182 reg_map
[m1
->regno
] = m
->set_dest
;
2185 = gen_lowpart_common (GET_MODE (m1
->set_dest
),
2188 /* Get rid of the matching insn
2189 and prevent further processing of it. */
2192 /* if library call, delete all insn except last, which
2194 if ((temp
= find_reg_note (m1
->insn
, REG_RETVAL
,
2197 for (temp
= XEXP (temp
, 0); temp
!= m1
->insn
;
2198 temp
= NEXT_INSN (temp
))
2201 delete_insn (m1
->insn
);
2203 /* Any other movable that loads the same register
2205 already_moved
[m1
->regno
] = 1;
2207 /* The reg merged here is now invariant,
2208 if the reg it matches is invariant. */
2210 VARRAY_INT (set_in_loop
, m1
->regno
) = 0;
2213 else if (loop_dump_stream
)
2214 fprintf (loop_dump_stream
, "not desirable");
2216 else if (loop_dump_stream
&& !m
->match
)
2217 fprintf (loop_dump_stream
, "not safe");
2219 if (loop_dump_stream
)
2220 fprintf (loop_dump_stream
, "\n");
2224 new_start
= loop_start
;
2226 /* Go through all the instructions in the loop, making
2227 all the register substitutions scheduled in REG_MAP. */
2228 for (p
= new_start
; p
!= end
; p
= NEXT_INSN (p
))
2229 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
2230 || GET_CODE (p
) == CALL_INSN
)
2232 replace_regs (PATTERN (p
), reg_map
, nregs
, 0);
2233 replace_regs (REG_NOTES (p
), reg_map
, nregs
, 0);
2239 /* Scan X and replace the address of any MEM in it with ADDR.
2240 REG is the address that MEM should have before the replacement. */
2243 replace_call_address (x
, reg
, addr
)
2246 register enum rtx_code code
;
2252 code
= GET_CODE (x
);
2266 /* Short cut for very common case. */
2267 replace_call_address (XEXP (x
, 1), reg
, addr
);
2271 /* Short cut for very common case. */
2272 replace_call_address (XEXP (x
, 0), reg
, addr
);
2276 /* If this MEM uses a reg other than the one we expected,
2277 something is wrong. */
2278 if (XEXP (x
, 0) != reg
)
2287 fmt
= GET_RTX_FORMAT (code
);
2288 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2291 replace_call_address (XEXP (x
, i
), reg
, addr
);
2295 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2296 replace_call_address (XVECEXP (x
, i
, j
), reg
, addr
);
2302 /* Return the number of memory refs to addresses that vary
2306 count_nonfixed_reads (x
)
2309 register enum rtx_code code
;
2317 code
= GET_CODE (x
);
2331 return ((invariant_p (XEXP (x
, 0)) != 1)
2332 + count_nonfixed_reads (XEXP (x
, 0)));
2339 fmt
= GET_RTX_FORMAT (code
);
2340 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2343 value
+= count_nonfixed_reads (XEXP (x
, i
));
2347 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2348 value
+= count_nonfixed_reads (XVECEXP (x
, i
, j
));
2356 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2357 Replace it with an instruction to load just the low bytes
2358 if the machine supports such an instruction,
2359 and insert above LOOP_START an instruction to clear the register. */
2362 constant_high_bytes (p
, loop_start
)
2366 register int insn_code_number
;
2368 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2369 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2371 new = gen_rtx_SET (VOIDmode
,
2372 gen_rtx_STRICT_LOW_PART (VOIDmode
,
2373 gen_rtx_SUBREG (GET_MODE (XEXP (SET_SRC (PATTERN (p
)), 0)),
2374 SET_DEST (PATTERN (p
)),
2376 XEXP (SET_SRC (PATTERN (p
)), 0));
2377 insn_code_number
= recog (new, p
);
2379 if (insn_code_number
)
2383 /* Clear destination register before the loop. */
2384 emit_insn_before (gen_rtx_SET (VOIDmode
, SET_DEST (PATTERN (p
)),
2388 /* Inside the loop, just load the low part. */
2394 /* Scan a loop setting the variables `unknown_address_altered',
2395 `num_mem_sets', `loop_continue', `loops_enclosed', `loop_has_call',
2396 `loop_has_volatile', and `loop_has_tablejump'.
2397 Also, fill in the array `loop_mems' and the list `loop_store_mems'. */
2400 prescan_loop (start
, end
)
2403 register int level
= 1;
2405 int loop_has_multiple_exit_targets
= 0;
2406 /* The label after END. Jumping here is just like falling off the
2407 end of the loop. We use next_nonnote_insn instead of next_label
2408 as a hedge against the (pathological) case where some actual insn
2409 might end up between the two. */
2410 rtx exit_target
= next_nonnote_insn (end
);
2411 if (exit_target
== NULL_RTX
|| GET_CODE (exit_target
) != CODE_LABEL
)
2412 loop_has_multiple_exit_targets
= 1;
2414 unknown_address_altered
= 0;
2416 loop_has_volatile
= 0;
2417 loop_has_tablejump
= 0;
2418 loop_store_mems
= NULL_RTX
;
2419 first_loop_store_insn
= NULL_RTX
;
2426 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2427 insn
= NEXT_INSN (insn
))
2429 if (GET_CODE (insn
) == NOTE
)
2431 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
2434 /* Count number of loops contained in this one. */
2437 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
2446 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_CONT
)
2449 loop_continue
= insn
;
2452 else if (GET_CODE (insn
) == CALL_INSN
)
2454 if (! CONST_CALL_P (insn
))
2455 unknown_address_altered
= 1;
2458 else if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == JUMP_INSN
)
2460 rtx label1
= NULL_RTX
;
2461 rtx label2
= NULL_RTX
;
2463 if (volatile_refs_p (PATTERN (insn
)))
2464 loop_has_volatile
= 1;
2466 if (GET_CODE (insn
) == JUMP_INSN
2467 && (GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
2468 || GET_CODE (PATTERN (insn
)) == ADDR_VEC
))
2469 loop_has_tablejump
= 1;
2471 note_stores (PATTERN (insn
), note_addr_stored
);
2472 if (! first_loop_store_insn
&& loop_store_mems
)
2473 first_loop_store_insn
= insn
;
2475 if (! loop_has_multiple_exit_targets
2476 && GET_CODE (insn
) == JUMP_INSN
2477 && GET_CODE (PATTERN (insn
)) == SET
2478 && SET_DEST (PATTERN (insn
)) == pc_rtx
)
2480 if (GET_CODE (SET_SRC (PATTERN (insn
))) == IF_THEN_ELSE
)
2482 label1
= XEXP (SET_SRC (PATTERN (insn
)), 1);
2483 label2
= XEXP (SET_SRC (PATTERN (insn
)), 2);
2487 label1
= SET_SRC (PATTERN (insn
));
2491 if (label1
&& label1
!= pc_rtx
)
2493 if (GET_CODE (label1
) != LABEL_REF
)
2495 /* Something tricky. */
2496 loop_has_multiple_exit_targets
= 1;
2499 else if (XEXP (label1
, 0) != exit_target
2500 && LABEL_OUTSIDE_LOOP_P (label1
))
2502 /* A jump outside the current loop. */
2503 loop_has_multiple_exit_targets
= 1;
2513 else if (GET_CODE (insn
) == RETURN
)
2514 loop_has_multiple_exit_targets
= 1;
2517 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2518 if (/* We can't tell what MEMs are aliased by what. */
2519 !unknown_address_altered
2520 /* An exception thrown by a called function might land us
2523 /* We don't want loads for MEMs moved to a location before the
2524 one at which their stack memory becomes allocated. (Note
2525 that this is not a problem for malloc, etc., since those
2526 require actual function calls. */
2527 && !current_function_calls_alloca
2528 /* There are ways to leave the loop other than falling off the
2530 && !loop_has_multiple_exit_targets
)
2531 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2532 insn
= NEXT_INSN (insn
))
2533 for_each_rtx (&insn
, insert_loop_mem
, 0);
2536 /* LOOP_NUMBER_CONT_DOMINATOR is now the last label between the loop start
2537 and the continue note that is a the destination of a (cond)jump after
2538 the continue note. If there is any (cond)jump between the loop start
2539 and what we have so far as LOOP_NUMBER_CONT_DOMINATOR that has a
2540 target between LOOP_DOMINATOR and the continue note, move
2541 LOOP_NUMBER_CONT_DOMINATOR forward to that label; if a jump's
2542 destination cannot be determined, clear LOOP_NUMBER_CONT_DOMINATOR. */
2545 verify_dominator (loop_number
)
2550 if (! loop_number_cont_dominator
[loop_number
])
2551 /* This can happen for an empty loop, e.g. in
2552 gcc.c-torture/compile/920410-2.c */
2554 if (loop_number_cont_dominator
[loop_number
] == const0_rtx
)
2556 loop_number_cont_dominator
[loop_number
] = 0;
2559 for (insn
= loop_number_loop_starts
[loop_number
];
2560 insn
!= loop_number_cont_dominator
[loop_number
];
2561 insn
= NEXT_INSN (insn
))
2563 if (GET_CODE (insn
) == JUMP_INSN
2564 && GET_CODE (PATTERN (insn
)) != RETURN
)
2566 rtx label
= JUMP_LABEL (insn
);
2567 int label_luid
= INSN_LUID (label
);
2569 if (! condjump_p (insn
)
2570 && ! condjump_in_parallel_p (insn
))
2572 loop_number_cont_dominator
[loop_number
] = NULL_RTX
;
2575 if (label_luid
< INSN_LUID (loop_number_loop_cont
[loop_number
])
2577 > INSN_LUID (loop_number_cont_dominator
[loop_number
])))
2578 loop_number_cont_dominator
[loop_number
] = label
;
2583 /* Scan the function looking for loops. Record the start and end of each loop.
2584 Also mark as invalid loops any loops that contain a setjmp or are branched
2585 to from outside the loop. */
2588 find_and_verify_loops (f
)
2592 int current_loop
= -1;
2596 compute_luids (f
, NULL_RTX
, 0);
2598 /* If there are jumps to undefined labels,
2599 treat them as jumps out of any/all loops.
2600 This also avoids writing past end of tables when there are no loops. */
2601 uid_loop_num
[0] = -1;
2603 /* Find boundaries of loops, mark which loops are contained within
2604 loops, and invalidate loops that have setjmp. */
2606 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2608 if (GET_CODE (insn
) == NOTE
)
2609 switch (NOTE_LINE_NUMBER (insn
))
2611 case NOTE_INSN_LOOP_BEG
:
2612 loop_number_loop_starts
[++next_loop
] = insn
;
2613 loop_number_loop_ends
[next_loop
] = 0;
2614 loop_number_loop_cont
[next_loop
] = 0;
2615 loop_number_cont_dominator
[next_loop
] = 0;
2616 loop_outer_loop
[next_loop
] = current_loop
;
2617 loop_invalid
[next_loop
] = 0;
2618 loop_number_exit_labels
[next_loop
] = 0;
2619 loop_number_exit_count
[next_loop
] = 0;
2620 current_loop
= next_loop
;
2623 case NOTE_INSN_SETJMP
:
2624 /* In this case, we must invalidate our current loop and any
2626 for (loop
= current_loop
; loop
!= -1; loop
= loop_outer_loop
[loop
])
2628 loop_invalid
[loop
] = 1;
2629 if (loop_dump_stream
)
2630 fprintf (loop_dump_stream
,
2631 "\nLoop at %d ignored due to setjmp.\n",
2632 INSN_UID (loop_number_loop_starts
[loop
]));
2636 case NOTE_INSN_LOOP_CONT
:
2637 loop_number_loop_cont
[current_loop
] = insn
;
2639 case NOTE_INSN_LOOP_END
:
2640 if (current_loop
== -1)
2643 loop_number_loop_ends
[current_loop
] = insn
;
2644 verify_dominator (current_loop
);
2645 current_loop
= loop_outer_loop
[current_loop
];
2651 /* If for any loop, this is a jump insn between the NOTE_INSN_LOOP_CONT
2652 and NOTE_INSN_LOOP_END notes, update loop_number_loop_dominator. */
2653 else if (GET_CODE (insn
) == JUMP_INSN
2654 && GET_CODE (PATTERN (insn
)) != RETURN
2655 && current_loop
>= 0)
2658 rtx label
= JUMP_LABEL (insn
);
2660 if (! condjump_p (insn
) && ! condjump_in_parallel_p (insn
))
2663 this_loop
= current_loop
;
2666 /* First see if we care about this loop. */
2667 if (loop_number_loop_cont
[this_loop
]
2668 && loop_number_cont_dominator
[this_loop
] != const0_rtx
)
2670 /* If the jump destination is not known, invalidate
2671 loop_number_const_dominator. */
2673 loop_number_cont_dominator
[this_loop
] = const0_rtx
;
2675 /* Check if the destination is between loop start and
2677 if ((INSN_LUID (label
)
2678 < INSN_LUID (loop_number_loop_cont
[this_loop
]))
2679 && (INSN_LUID (label
)
2680 > INSN_LUID (loop_number_loop_starts
[this_loop
]))
2681 /* And if there is no later destination already
2683 && (! loop_number_cont_dominator
[this_loop
]
2684 || (INSN_LUID (label
)
2685 > INSN_LUID (loop_number_cont_dominator
2687 loop_number_cont_dominator
[this_loop
] = label
;
2689 this_loop
= loop_outer_loop
[this_loop
];
2691 while (this_loop
>= 0);
2694 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2695 enclosing loop, but this doesn't matter. */
2696 uid_loop_num
[INSN_UID (insn
)] = current_loop
;
2699 /* Any loop containing a label used in an initializer must be invalidated,
2700 because it can be jumped into from anywhere. */
2702 for (label
= forced_labels
; label
; label
= XEXP (label
, 1))
2706 for (loop_num
= uid_loop_num
[INSN_UID (XEXP (label
, 0))];
2708 loop_num
= loop_outer_loop
[loop_num
])
2709 loop_invalid
[loop_num
] = 1;
2712 /* Any loop containing a label used for an exception handler must be
2713 invalidated, because it can be jumped into from anywhere. */
2715 for (label
= exception_handler_labels
; label
; label
= XEXP (label
, 1))
2719 for (loop_num
= uid_loop_num
[INSN_UID (XEXP (label
, 0))];
2721 loop_num
= loop_outer_loop
[loop_num
])
2722 loop_invalid
[loop_num
] = 1;
2725 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2726 loop that it is not contained within, that loop is marked invalid.
2727 If any INSN or CALL_INSN uses a label's address, then the loop containing
2728 that label is marked invalid, because it could be jumped into from
2731 Also look for blocks of code ending in an unconditional branch that
2732 exits the loop. If such a block is surrounded by a conditional
2733 branch around the block, move the block elsewhere (see below) and
2734 invert the jump to point to the code block. This may eliminate a
2735 label in our loop and will simplify processing by both us and a
2736 possible second cse pass. */
2738 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2739 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
2741 int this_loop_num
= uid_loop_num
[INSN_UID (insn
)];
2743 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
2745 rtx note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
);
2750 for (loop_num
= uid_loop_num
[INSN_UID (XEXP (note
, 0))];
2752 loop_num
= loop_outer_loop
[loop_num
])
2753 loop_invalid
[loop_num
] = 1;
2757 if (GET_CODE (insn
) != JUMP_INSN
)
2760 mark_loop_jump (PATTERN (insn
), this_loop_num
);
2762 /* See if this is an unconditional branch outside the loop. */
2763 if (this_loop_num
!= -1
2764 && (GET_CODE (PATTERN (insn
)) == RETURN
2765 || (simplejump_p (insn
)
2766 && (uid_loop_num
[INSN_UID (JUMP_LABEL (insn
))]
2768 && get_max_uid () < max_uid_for_loop
)
2771 rtx our_next
= next_real_insn (insn
);
2773 int outer_loop
= -1;
2775 /* Go backwards until we reach the start of the loop, a label,
2777 for (p
= PREV_INSN (insn
);
2778 GET_CODE (p
) != CODE_LABEL
2779 && ! (GET_CODE (p
) == NOTE
2780 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
2781 && GET_CODE (p
) != JUMP_INSN
;
2785 /* Check for the case where we have a jump to an inner nested
2786 loop, and do not perform the optimization in that case. */
2788 if (JUMP_LABEL (insn
))
2790 dest_loop
= uid_loop_num
[INSN_UID (JUMP_LABEL (insn
))];
2791 if (dest_loop
!= -1)
2793 for (outer_loop
= dest_loop
; outer_loop
!= -1;
2794 outer_loop
= loop_outer_loop
[outer_loop
])
2795 if (outer_loop
== this_loop_num
)
2800 /* Make sure that the target of P is within the current loop. */
2802 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
2803 && uid_loop_num
[INSN_UID (JUMP_LABEL (p
))] != this_loop_num
)
2804 outer_loop
= this_loop_num
;
2806 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2807 we have a block of code to try to move.
2809 We look backward and then forward from the target of INSN
2810 to find a BARRIER at the same loop depth as the target.
2811 If we find such a BARRIER, we make a new label for the start
2812 of the block, invert the jump in P and point it to that label,
2813 and move the block of code to the spot we found. */
2815 if (outer_loop
== -1
2816 && GET_CODE (p
) == JUMP_INSN
2817 && JUMP_LABEL (p
) != 0
2818 /* Just ignore jumps to labels that were never emitted.
2819 These always indicate compilation errors. */
2820 && INSN_UID (JUMP_LABEL (p
)) != 0
2822 && ! simplejump_p (p
)
2823 && next_real_insn (JUMP_LABEL (p
)) == our_next
)
2826 = JUMP_LABEL (insn
) ? JUMP_LABEL (insn
) : get_last_insn ();
2827 int target_loop_num
= uid_loop_num
[INSN_UID (target
)];
2830 for (loc
= target
; loc
; loc
= PREV_INSN (loc
))
2831 if (GET_CODE (loc
) == BARRIER
2832 && uid_loop_num
[INSN_UID (loc
)] == target_loop_num
)
2836 for (loc
= target
; loc
; loc
= NEXT_INSN (loc
))
2837 if (GET_CODE (loc
) == BARRIER
2838 && uid_loop_num
[INSN_UID (loc
)] == target_loop_num
)
2843 rtx cond_label
= JUMP_LABEL (p
);
2844 rtx new_label
= get_label_after (p
);
2846 /* Ensure our label doesn't go away. */
2847 LABEL_NUSES (cond_label
)++;
2849 /* Verify that uid_loop_num is large enough and that
2851 if (invert_jump (p
, new_label
))
2855 /* If no suitable BARRIER was found, create a suitable
2856 one before TARGET. Since TARGET is a fall through
2857 path, we'll need to insert an jump around our block
2858 and a add a BARRIER before TARGET.
2860 This creates an extra unconditional jump outside
2861 the loop. However, the benefits of removing rarely
2862 executed instructions from inside the loop usually
2863 outweighs the cost of the extra unconditional jump
2864 outside the loop. */
2869 temp
= gen_jump (JUMP_LABEL (insn
));
2870 temp
= emit_jump_insn_before (temp
, target
);
2871 JUMP_LABEL (temp
) = JUMP_LABEL (insn
);
2872 LABEL_NUSES (JUMP_LABEL (insn
))++;
2873 loc
= emit_barrier_before (target
);
2876 /* Include the BARRIER after INSN and copy the
2878 new_label
= squeeze_notes (new_label
, NEXT_INSN (insn
));
2879 reorder_insns (new_label
, NEXT_INSN (insn
), loc
);
2881 /* All those insns are now in TARGET_LOOP_NUM. */
2882 for (q
= new_label
; q
!= NEXT_INSN (NEXT_INSN (insn
));
2884 uid_loop_num
[INSN_UID (q
)] = target_loop_num
;
2886 /* The label jumped to by INSN is no longer a loop exit.
2887 Unless INSN does not have a label (e.g., it is a
2888 RETURN insn), search loop_number_exit_labels to find
2889 its label_ref, and remove it. Also turn off
2890 LABEL_OUTSIDE_LOOP_P bit. */
2891 if (JUMP_LABEL (insn
))
2896 r
= loop_number_exit_labels
[this_loop_num
];
2897 r
; q
= r
, r
= LABEL_NEXTREF (r
))
2898 if (XEXP (r
, 0) == JUMP_LABEL (insn
))
2900 LABEL_OUTSIDE_LOOP_P (r
) = 0;
2902 LABEL_NEXTREF (q
) = LABEL_NEXTREF (r
);
2904 loop_number_exit_labels
[this_loop_num
]
2905 = LABEL_NEXTREF (r
);
2909 for (loop_num
= this_loop_num
;
2910 loop_num
!= -1 && loop_num
!= target_loop_num
;
2911 loop_num
= loop_outer_loop
[loop_num
])
2912 loop_number_exit_count
[loop_num
]--;
2914 /* If we didn't find it, then something is wrong. */
2919 /* P is now a jump outside the loop, so it must be put
2920 in loop_number_exit_labels, and marked as such.
2921 The easiest way to do this is to just call
2922 mark_loop_jump again for P. */
2923 mark_loop_jump (PATTERN (p
), this_loop_num
);
2925 /* If INSN now jumps to the insn after it,
2927 if (JUMP_LABEL (insn
) != 0
2928 && (next_real_insn (JUMP_LABEL (insn
))
2929 == next_real_insn (insn
)))
2933 /* Continue the loop after where the conditional
2934 branch used to jump, since the only branch insn
2935 in the block (if it still remains) is an inter-loop
2936 branch and hence needs no processing. */
2937 insn
= NEXT_INSN (cond_label
);
2939 if (--LABEL_NUSES (cond_label
) == 0)
2940 delete_insn (cond_label
);
2942 /* This loop will be continued with NEXT_INSN (insn). */
2943 insn
= PREV_INSN (insn
);
2950 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2951 loops it is contained in, mark the target loop invalid.
2953 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2956 mark_loop_jump (x
, loop_num
)
2964 switch (GET_CODE (x
))
2977 /* There could be a label reference in here. */
2978 mark_loop_jump (XEXP (x
, 0), loop_num
);
2984 mark_loop_jump (XEXP (x
, 0), loop_num
);
2985 mark_loop_jump (XEXP (x
, 1), loop_num
);
2989 /* This may refer to a LABEL_REF or SYMBOL_REF. */
2990 mark_loop_jump (XEXP (x
, 1), loop_num
);
2995 mark_loop_jump (XEXP (x
, 0), loop_num
);
2999 dest_loop
= uid_loop_num
[INSN_UID (XEXP (x
, 0))];
3001 /* Link together all labels that branch outside the loop. This
3002 is used by final_[bg]iv_value and the loop unrolling code. Also
3003 mark this LABEL_REF so we know that this branch should predict
3006 /* A check to make sure the label is not in an inner nested loop,
3007 since this does not count as a loop exit. */
3008 if (dest_loop
!= -1)
3010 for (outer_loop
= dest_loop
; outer_loop
!= -1;
3011 outer_loop
= loop_outer_loop
[outer_loop
])
3012 if (outer_loop
== loop_num
)
3018 if (loop_num
!= -1 && outer_loop
== -1)
3020 LABEL_OUTSIDE_LOOP_P (x
) = 1;
3021 LABEL_NEXTREF (x
) = loop_number_exit_labels
[loop_num
];
3022 loop_number_exit_labels
[loop_num
] = x
;
3024 for (outer_loop
= loop_num
;
3025 outer_loop
!= -1 && outer_loop
!= dest_loop
;
3026 outer_loop
= loop_outer_loop
[outer_loop
])
3027 loop_number_exit_count
[outer_loop
]++;
3030 /* If this is inside a loop, but not in the current loop or one enclosed
3031 by it, it invalidates at least one loop. */
3033 if (dest_loop
== -1)
3036 /* We must invalidate every nested loop containing the target of this
3037 label, except those that also contain the jump insn. */
3039 for (; dest_loop
!= -1; dest_loop
= loop_outer_loop
[dest_loop
])
3041 /* Stop when we reach a loop that also contains the jump insn. */
3042 for (outer_loop
= loop_num
; outer_loop
!= -1;
3043 outer_loop
= loop_outer_loop
[outer_loop
])
3044 if (dest_loop
== outer_loop
)
3047 /* If we get here, we know we need to invalidate a loop. */
3048 if (loop_dump_stream
&& ! loop_invalid
[dest_loop
])
3049 fprintf (loop_dump_stream
,
3050 "\nLoop at %d ignored due to multiple entry points.\n",
3051 INSN_UID (loop_number_loop_starts
[dest_loop
]));
3053 loop_invalid
[dest_loop
] = 1;
3058 /* If this is not setting pc, ignore. */
3059 if (SET_DEST (x
) == pc_rtx
)
3060 mark_loop_jump (SET_SRC (x
), loop_num
);
3064 mark_loop_jump (XEXP (x
, 1), loop_num
);
3065 mark_loop_jump (XEXP (x
, 2), loop_num
);
3070 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
3071 mark_loop_jump (XVECEXP (x
, 0, i
), loop_num
);
3075 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
3076 mark_loop_jump (XVECEXP (x
, 1, i
), loop_num
);
3080 /* Strictly speaking this is not a jump into the loop, only a possible
3081 jump out of the loop. However, we have no way to link the destination
3082 of this jump onto the list of exit labels. To be safe we mark this
3083 loop and any containing loops as invalid. */
3086 for (outer_loop
= loop_num
; outer_loop
!= -1;
3087 outer_loop
= loop_outer_loop
[outer_loop
])
3089 if (loop_dump_stream
&& ! loop_invalid
[outer_loop
])
3090 fprintf (loop_dump_stream
,
3091 "\nLoop at %d ignored due to unknown exit jump.\n",
3092 INSN_UID (loop_number_loop_starts
[outer_loop
]));
3093 loop_invalid
[outer_loop
] = 1;
3100 /* Return nonzero if there is a label in the range from
3101 insn INSN to and including the insn whose luid is END
3102 INSN must have an assigned luid (i.e., it must not have
3103 been previously created by loop.c). */
3106 labels_in_range_p (insn
, end
)
3110 while (insn
&& INSN_LUID (insn
) <= end
)
3112 if (GET_CODE (insn
) == CODE_LABEL
)
3114 insn
= NEXT_INSN (insn
);
3120 /* Record that a memory reference X is being set. */
3123 note_addr_stored (x
, y
)
3125 rtx y ATTRIBUTE_UNUSED
;
3127 if (x
== 0 || GET_CODE (x
) != MEM
)
3130 /* Count number of memory writes.
3131 This affects heuristics in strength_reduce. */
3134 /* BLKmode MEM means all memory is clobbered. */
3135 if (GET_MODE (x
) == BLKmode
)
3136 unknown_address_altered
= 1;
3138 if (unknown_address_altered
)
3141 loop_store_mems
= gen_rtx_EXPR_LIST (VOIDmode
, x
, loop_store_mems
);
3144 /* Return nonzero if the rtx X is invariant over the current loop.
3146 The value is 2 if we refer to something only conditionally invariant.
3148 If `unknown_address_altered' is nonzero, no memory ref is invariant.
3149 Otherwise, a memory ref is invariant if it does not conflict with
3150 anything stored in `loop_store_mems'. */
3157 register enum rtx_code code
;
3159 int conditional
= 0;
3164 code
= GET_CODE (x
);
3174 /* A LABEL_REF is normally invariant, however, if we are unrolling
3175 loops, and this label is inside the loop, then it isn't invariant.
3176 This is because each unrolled copy of the loop body will have
3177 a copy of this label. If this was invariant, then an insn loading
3178 the address of this label into a register might get moved outside
3179 the loop, and then each loop body would end up using the same label.
3181 We don't know the loop bounds here though, so just fail for all
3183 if (flag_unroll_loops
)
3190 case UNSPEC_VOLATILE
:
3194 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3195 since the reg might be set by initialization within the loop. */
3197 if ((x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
3198 || x
== arg_pointer_rtx
)
3199 && ! current_function_has_nonlocal_goto
)
3203 && REGNO (x
) < FIRST_PSEUDO_REGISTER
&& call_used_regs
[REGNO (x
)])
3206 if (VARRAY_INT (set_in_loop
, REGNO (x
)) < 0)
3209 return VARRAY_INT (set_in_loop
, REGNO (x
)) == 0;
3212 /* Volatile memory references must be rejected. Do this before
3213 checking for read-only items, so that volatile read-only items
3214 will be rejected also. */
3215 if (MEM_VOLATILE_P (x
))
3218 /* Read-only items (such as constants in a constant pool) are
3219 invariant if their address is. */
3220 if (RTX_UNCHANGING_P (x
))
3223 /* If we had a subroutine call, any location in memory could have been
3225 if (unknown_address_altered
)
3228 /* See if there is any dependence between a store and this load. */
3229 mem_list_entry
= loop_store_mems
;
3230 while (mem_list_entry
)
3232 if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
3235 mem_list_entry
= XEXP (mem_list_entry
, 1);
3238 /* It's not invalidated by a store in memory
3239 but we must still verify the address is invariant. */
3243 /* Don't mess with insns declared volatile. */
3244 if (MEM_VOLATILE_P (x
))
3252 fmt
= GET_RTX_FORMAT (code
);
3253 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3257 int tem
= invariant_p (XEXP (x
, i
));
3263 else if (fmt
[i
] == 'E')
3266 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
3268 int tem
= invariant_p (XVECEXP (x
, i
, j
));
3278 return 1 + conditional
;
3282 /* Return nonzero if all the insns in the loop that set REG
3283 are INSN and the immediately following insns,
3284 and if each of those insns sets REG in an invariant way
3285 (not counting uses of REG in them).
3287 The value is 2 if some of these insns are only conditionally invariant.
3289 We assume that INSN itself is the first set of REG
3290 and that its source is invariant. */
3293 consec_sets_invariant_p (reg
, n_sets
, insn
)
3297 register rtx p
= insn
;
3298 register int regno
= REGNO (reg
);
3300 /* Number of sets we have to insist on finding after INSN. */
3301 int count
= n_sets
- 1;
3302 int old
= VARRAY_INT (set_in_loop
, regno
);
3306 /* If N_SETS hit the limit, we can't rely on its value. */
3310 VARRAY_INT (set_in_loop
, regno
) = 0;
3314 register enum rtx_code code
;
3318 code
= GET_CODE (p
);
3320 /* If library call, skip to end of it. */
3321 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
3326 && (set
= single_set (p
))
3327 && GET_CODE (SET_DEST (set
)) == REG
3328 && REGNO (SET_DEST (set
)) == regno
)
3330 this = invariant_p (SET_SRC (set
));
3333 else if ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
)))
3335 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3336 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3338 this = (CONSTANT_P (XEXP (temp
, 0))
3339 || (find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
3340 && invariant_p (XEXP (temp
, 0))));
3347 else if (code
!= NOTE
)
3349 VARRAY_INT (set_in_loop
, regno
) = old
;
3354 VARRAY_INT (set_in_loop
, regno
) = old
;
3355 /* If invariant_p ever returned 2, we return 2. */
3356 return 1 + (value
& 2);
3360 /* I don't think this condition is sufficient to allow INSN
3361 to be moved, so we no longer test it. */
3363 /* Return 1 if all insns in the basic block of INSN and following INSN
3364 that set REG are invariant according to TABLE. */
3367 all_sets_invariant_p (reg
, insn
, table
)
3371 register rtx p
= insn
;
3372 register int regno
= REGNO (reg
);
3376 register enum rtx_code code
;
3378 code
= GET_CODE (p
);
3379 if (code
== CODE_LABEL
|| code
== JUMP_INSN
)
3381 if (code
== INSN
&& GET_CODE (PATTERN (p
)) == SET
3382 && GET_CODE (SET_DEST (PATTERN (p
))) == REG
3383 && REGNO (SET_DEST (PATTERN (p
))) == regno
)
3385 if (!invariant_p (SET_SRC (PATTERN (p
)), table
))
3392 /* Look at all uses (not sets) of registers in X. For each, if it is
3393 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3394 a different insn, set USAGE[REGNO] to const0_rtx. */
3397 find_single_use_in_loop (insn
, x
, usage
)
3402 enum rtx_code code
= GET_CODE (x
);
3403 char *fmt
= GET_RTX_FORMAT (code
);
3407 VARRAY_RTX (usage
, REGNO (x
))
3408 = (VARRAY_RTX (usage
, REGNO (x
)) != 0
3409 && VARRAY_RTX (usage
, REGNO (x
)) != insn
)
3410 ? const0_rtx
: insn
;
3412 else if (code
== SET
)
3414 /* Don't count SET_DEST if it is a REG; otherwise count things
3415 in SET_DEST because if a register is partially modified, it won't
3416 show up as a potential movable so we don't care how USAGE is set
3418 if (GET_CODE (SET_DEST (x
)) != REG
)
3419 find_single_use_in_loop (insn
, SET_DEST (x
), usage
);
3420 find_single_use_in_loop (insn
, SET_SRC (x
), usage
);
3423 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3425 if (fmt
[i
] == 'e' && XEXP (x
, i
) != 0)
3426 find_single_use_in_loop (insn
, XEXP (x
, i
), usage
);
3427 else if (fmt
[i
] == 'E')
3428 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
3429 find_single_use_in_loop (insn
, XVECEXP (x
, i
, j
), usage
);
3433 /* Count and record any set in X which is contained in INSN. Update
3434 MAY_NOT_MOVE and LAST_SET for any register set in X. */
3437 count_one_set (insn
, x
, may_not_move
, last_set
)
3439 varray_type may_not_move
;
3442 if (GET_CODE (x
) == CLOBBER
&& GET_CODE (XEXP (x
, 0)) == REG
)
3443 /* Don't move a reg that has an explicit clobber.
3444 It's not worth the pain to try to do it correctly. */
3445 VARRAY_CHAR (may_not_move
, REGNO (XEXP (x
, 0))) = 1;
3447 if (GET_CODE (x
) == SET
|| GET_CODE (x
) == CLOBBER
)
3449 rtx dest
= SET_DEST (x
);
3450 while (GET_CODE (dest
) == SUBREG
3451 || GET_CODE (dest
) == ZERO_EXTRACT
3452 || GET_CODE (dest
) == SIGN_EXTRACT
3453 || GET_CODE (dest
) == STRICT_LOW_PART
)
3454 dest
= XEXP (dest
, 0);
3455 if (GET_CODE (dest
) == REG
)
3457 register int regno
= REGNO (dest
);
3458 /* If this is the first setting of this reg
3459 in current basic block, and it was set before,
3460 it must be set in two basic blocks, so it cannot
3461 be moved out of the loop. */
3462 if (VARRAY_INT (set_in_loop
, regno
) > 0
3463 && last_set
[regno
] == 0)
3464 VARRAY_CHAR (may_not_move
, regno
) = 1;
3465 /* If this is not first setting in current basic block,
3466 see if reg was used in between previous one and this.
3467 If so, neither one can be moved. */
3468 if (last_set
[regno
] != 0
3469 && reg_used_between_p (dest
, last_set
[regno
], insn
))
3470 VARRAY_CHAR (may_not_move
, regno
) = 1;
3471 if (VARRAY_INT (set_in_loop
, regno
) < 127)
3472 ++VARRAY_INT (set_in_loop
, regno
);
3473 last_set
[regno
] = insn
;
3478 /* Increment SET_IN_LOOP at the index of each register
3479 that is modified by an insn between FROM and TO.
3480 If the value of an element of SET_IN_LOOP becomes 127 or more,
3481 stop incrementing it, to avoid overflow.
3483 Store in SINGLE_USAGE[I] the single insn in which register I is
3484 used, if it is only used once. Otherwise, it is set to 0 (for no
3485 uses) or const0_rtx for more than one use. This parameter may be zero,
3486 in which case this processing is not done.
3488 Store in *COUNT_PTR the number of actual instruction
3489 in the loop. We use this to decide what is worth moving out. */
3491 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3492 In that case, it is the insn that last set reg n. */
3495 count_loop_regs_set (from
, to
, may_not_move
, single_usage
, count_ptr
, nregs
)
3496 register rtx from
, to
;
3497 varray_type may_not_move
;
3498 varray_type single_usage
;
3502 register rtx
*last_set
= (rtx
*) alloca (nregs
* sizeof (rtx
));
3504 register int count
= 0;
3506 bzero ((char *) last_set
, nregs
* sizeof (rtx
));
3507 for (insn
= from
; insn
!= to
; insn
= NEXT_INSN (insn
))
3509 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
3513 /* Record registers that have exactly one use. */
3514 find_single_use_in_loop (insn
, PATTERN (insn
), single_usage
);
3516 /* Include uses in REG_EQUAL notes. */
3517 if (REG_NOTES (insn
))
3518 find_single_use_in_loop (insn
, REG_NOTES (insn
), single_usage
);
3520 if (GET_CODE (PATTERN (insn
)) == SET
3521 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
3522 count_one_set (insn
, PATTERN (insn
), may_not_move
, last_set
);
3523 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
3526 for (i
= XVECLEN (PATTERN (insn
), 0) - 1; i
>= 0; i
--)
3527 count_one_set (insn
, XVECEXP (PATTERN (insn
), 0, i
),
3528 may_not_move
, last_set
);
3532 if (GET_CODE (insn
) == CODE_LABEL
|| GET_CODE (insn
) == JUMP_INSN
)
3533 bzero ((char *) last_set
, nregs
* sizeof (rtx
));
3538 /* Given a loop that is bounded by LOOP_START and LOOP_END
3539 and that is entered at SCAN_START,
3540 return 1 if the register set in SET contained in insn INSN is used by
3541 any insn that precedes INSN in cyclic order starting
3542 from the loop entry point.
3544 We don't want to use INSN_LUID here because if we restrict INSN to those
3545 that have a valid INSN_LUID, it means we cannot move an invariant out
3546 from an inner loop past two loops. */
3549 loop_reg_used_before_p (set
, insn
, loop_start
, scan_start
, loop_end
)
3550 rtx set
, insn
, loop_start
, scan_start
, loop_end
;
3552 rtx reg
= SET_DEST (set
);
3555 /* Scan forward checking for register usage. If we hit INSN, we
3556 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3557 for (p
= scan_start
; p
!= insn
; p
= NEXT_INSN (p
))
3559 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i'
3560 && reg_overlap_mentioned_p (reg
, PATTERN (p
)))
3570 /* A "basic induction variable" or biv is a pseudo reg that is set
3571 (within this loop) only by incrementing or decrementing it. */
3572 /* A "general induction variable" or giv is a pseudo reg whose
3573 value is a linear function of a biv. */
3575 /* Bivs are recognized by `basic_induction_var';
3576 Givs by `general_induction_var'. */
3578 /* Indexed by register number, indicates whether or not register is an
3579 induction variable, and if so what type. */
3581 varray_type reg_iv_type
;
3583 /* Indexed by register number, contains pointer to `struct induction'
3584 if register is an induction variable. This holds general info for
3585 all induction variables. */
3587 varray_type reg_iv_info
;
3589 /* Indexed by register number, contains pointer to `struct iv_class'
3590 if register is a basic induction variable. This holds info describing
3591 the class (a related group) of induction variables that the biv belongs
3594 struct iv_class
**reg_biv_class
;
3596 /* The head of a list which links together (via the next field)
3597 every iv class for the current loop. */
3599 struct iv_class
*loop_iv_list
;
3601 /* Givs made from biv increments are always splittable for loop unrolling.
3602 Since there is no regscan info for them, we have to keep track of them
3604 int first_increment_giv
, last_increment_giv
;
3606 /* Communication with routines called via `note_stores'. */
3608 static rtx note_insn
;
3610 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3612 static rtx addr_placeholder
;
3614 /* ??? Unfinished optimizations, and possible future optimizations,
3615 for the strength reduction code. */
3617 /* ??? The interaction of biv elimination, and recognition of 'constant'
3618 bivs, may cause problems. */
3620 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3621 performance problems.
3623 Perhaps don't eliminate things that can be combined with an addressing
3624 mode. Find all givs that have the same biv, mult_val, and add_val;
3625 then for each giv, check to see if its only use dies in a following
3626 memory address. If so, generate a new memory address and check to see
3627 if it is valid. If it is valid, then store the modified memory address,
3628 otherwise, mark the giv as not done so that it will get its own iv. */
3630 /* ??? Could try to optimize branches when it is known that a biv is always
3633 /* ??? When replace a biv in a compare insn, we should replace with closest
3634 giv so that an optimized branch can still be recognized by the combiner,
3635 e.g. the VAX acb insn. */
3637 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3638 was rerun in loop_optimize whenever a register was added or moved.
3639 Also, some of the optimizations could be a little less conservative. */
3641 /* Perform strength reduction and induction variable elimination.
3643 Pseudo registers created during this function will be beyond the last
3644 valid index in several tables including n_times_set and regno_last_uid.
3645 This does not cause a problem here, because the added registers cannot be
3646 givs outside of their loop, and hence will never be reconsidered.
3647 But scan_loop must check regnos to make sure they are in bounds.
3649 SCAN_START is the first instruction in the loop, as the loop would
3650 actually be executed. END is the NOTE_INSN_LOOP_END. LOOP_TOP is
3651 the first instruction in the loop, as it is layed out in the
3652 instruction stream. LOOP_START is the NOTE_INSN_LOOP_BEG.
3653 LOOP_CONT is the NOTE_INSN_LOOP_CONT. */
3656 strength_reduce (scan_start
, end
, loop_top
, insn_count
,
3657 loop_start
, loop_end
, loop_cont
, unroll_p
, bct_p
)
3665 int unroll_p
, bct_p ATTRIBUTE_UNUSED
;
3673 /* This is 1 if current insn is not executed at least once for every loop
3675 int not_every_iteration
= 0;
3676 /* This is 1 if current insn may be executed more than once for every
3678 int maybe_multiple
= 0;
3679 /* This is 1 if we have past a branch back to the top of the loop
3680 (aka a loop latch). */
3681 int past_loop_latch
= 0;
3682 /* Temporary list pointers for traversing loop_iv_list. */
3683 struct iv_class
*bl
, **backbl
;
3684 /* Ratio of extra register life span we can justify
3685 for saving an instruction. More if loop doesn't call subroutines
3686 since in that case saving an insn makes more difference
3687 and more registers are available. */
3688 /* ??? could set this to last value of threshold in move_movables */
3689 int threshold
= (loop_has_call
? 1 : 2) * (3 + n_non_fixed_regs
);
3690 /* Map of pseudo-register replacements. */
3695 rtx end_insert_before
;
3697 int n_extra_increment
;
3698 struct loop_info loop_iteration_info
;
3699 struct loop_info
*loop_info
= &loop_iteration_info
;
3701 /* If scan_start points to the loop exit test, we have to be wary of
3702 subversive use of gotos inside expression statements. */
3703 if (prev_nonnote_insn (scan_start
) != prev_nonnote_insn (loop_start
))
3704 maybe_multiple
= back_branch_in_range_p (scan_start
, loop_start
, loop_end
);
3706 VARRAY_INT_INIT (reg_iv_type
, max_reg_before_loop
, "reg_iv_type");
3707 VARRAY_GENERIC_PTR_INIT (reg_iv_info
, max_reg_before_loop
, "reg_iv_info");
3708 reg_biv_class
= (struct iv_class
**)
3709 alloca (max_reg_before_loop
* sizeof (struct iv_class
*));
3710 bzero ((char *) reg_biv_class
, (max_reg_before_loop
3711 * sizeof (struct iv_class
*)));
3714 addr_placeholder
= gen_reg_rtx (Pmode
);
3716 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3717 must be put before this insn, so that they will appear in the right
3718 order (i.e. loop order).
3720 If loop_end is the end of the current function, then emit a
3721 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3723 if (NEXT_INSN (loop_end
) != 0)
3724 end_insert_before
= NEXT_INSN (loop_end
);
3726 end_insert_before
= emit_note_after (NOTE_INSN_DELETED
, loop_end
);
3728 /* Scan through loop to find all possible bivs. */
3730 for (p
= next_insn_in_loop (scan_start
, scan_start
, end
, loop_top
);
3732 p
= next_insn_in_loop (p
, scan_start
, end
, loop_top
))
3734 if (GET_CODE (p
) == INSN
3735 && (set
= single_set (p
))
3736 && GET_CODE (SET_DEST (set
)) == REG
)
3738 dest_reg
= SET_DEST (set
);
3739 if (REGNO (dest_reg
) < max_reg_before_loop
3740 && REGNO (dest_reg
) >= FIRST_PSEUDO_REGISTER
3741 && REG_IV_TYPE (REGNO (dest_reg
)) != NOT_BASIC_INDUCT
)
3743 if (basic_induction_var (SET_SRC (set
), GET_MODE (SET_SRC (set
)),
3744 dest_reg
, p
, &inc_val
, &mult_val
,
3747 /* It is a possible basic induction variable.
3748 Create and initialize an induction structure for it. */
3751 = (struct induction
*) alloca (sizeof (struct induction
));
3753 record_biv (v
, p
, dest_reg
, inc_val
, mult_val
, location
,
3754 not_every_iteration
, maybe_multiple
);
3755 REG_IV_TYPE (REGNO (dest_reg
)) = BASIC_INDUCT
;
3757 else if (REGNO (dest_reg
) < max_reg_before_loop
)
3758 REG_IV_TYPE (REGNO (dest_reg
)) = NOT_BASIC_INDUCT
;
3762 /* Past CODE_LABEL, we get to insns that may be executed multiple
3763 times. The only way we can be sure that they can't is if every
3764 jump insn between here and the end of the loop either
3765 returns, exits the loop, is a jump to a location that is still
3766 behind the label, or is a jump to the loop start. */
3768 if (GET_CODE (p
) == CODE_LABEL
)
3776 insn
= NEXT_INSN (insn
);
3777 if (insn
== scan_start
)
3785 if (insn
== scan_start
)
3789 if (GET_CODE (insn
) == JUMP_INSN
3790 && GET_CODE (PATTERN (insn
)) != RETURN
3791 && (! condjump_p (insn
)
3792 || (JUMP_LABEL (insn
) != 0
3793 && JUMP_LABEL (insn
) != scan_start
3794 && ! loop_insn_first_p (p
, JUMP_LABEL (insn
)))))
3802 /* Past a jump, we get to insns for which we can't count
3803 on whether they will be executed during each iteration. */
3804 /* This code appears twice in strength_reduce. There is also similar
3805 code in scan_loop. */
3806 if (GET_CODE (p
) == JUMP_INSN
3807 /* If we enter the loop in the middle, and scan around to the
3808 beginning, don't set not_every_iteration for that.
3809 This can be any kind of jump, since we want to know if insns
3810 will be executed if the loop is executed. */
3811 && ! (JUMP_LABEL (p
) == loop_top
3812 && ((NEXT_INSN (NEXT_INSN (p
)) == loop_end
&& simplejump_p (p
))
3813 || (NEXT_INSN (p
) == loop_end
&& condjump_p (p
)))))
3817 /* If this is a jump outside the loop, then it also doesn't
3818 matter. Check to see if the target of this branch is on the
3819 loop_number_exits_labels list. */
3821 for (label
= loop_number_exit_labels
[uid_loop_num
[INSN_UID (loop_start
)]];
3823 label
= LABEL_NEXTREF (label
))
3824 if (XEXP (label
, 0) == JUMP_LABEL (p
))
3828 not_every_iteration
= 1;
3831 else if (GET_CODE (p
) == NOTE
)
3833 /* At the virtual top of a converted loop, insns are again known to
3834 be executed each iteration: logically, the loop begins here
3835 even though the exit code has been duplicated.
3837 Insns are also again known to be executed each iteration at
3838 the LOOP_CONT note. */
3839 if ((NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
3840 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_CONT
)
3842 not_every_iteration
= 0;
3843 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
3845 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)
3849 /* Note if we pass a loop latch. If we do, then we can not clear
3850 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
3851 a loop since a jump before the last CODE_LABEL may have started
3852 a new loop iteration.
3854 Note that LOOP_TOP is only set for rotated loops and we need
3855 this check for all loops, so compare against the CODE_LABEL
3856 which immediately follows LOOP_START. */
3857 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == NEXT_INSN (loop_start
))
3858 past_loop_latch
= 1;
3860 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3861 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3862 or not an insn is known to be executed each iteration of the
3863 loop, whether or not any iterations are known to occur.
3865 Therefore, if we have just passed a label and have no more labels
3866 between here and the test insn of the loop, and we have not passed
3867 a jump to the top of the loop, then we know these insns will be
3868 executed each iteration. */
3870 if (not_every_iteration
3871 && ! past_loop_latch
3872 && GET_CODE (p
) == CODE_LABEL
3873 && no_labels_between_p (p
, loop_end
)
3874 && loop_insn_first_p (p
, loop_cont
))
3875 not_every_iteration
= 0;
3878 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3879 Make a sanity check against n_times_set. */
3880 for (backbl
= &loop_iv_list
, bl
= *backbl
; bl
; bl
= bl
->next
)
3882 if (REG_IV_TYPE (bl
->regno
) != BASIC_INDUCT
3883 /* Above happens if register modified by subreg, etc. */
3884 /* Make sure it is not recognized as a basic induction var: */
3885 || VARRAY_INT (n_times_set
, bl
->regno
) != bl
->biv_count
3886 /* If never incremented, it is invariant that we decided not to
3887 move. So leave it alone. */
3888 || ! bl
->incremented
)
3890 if (loop_dump_stream
)
3891 fprintf (loop_dump_stream
, "Reg %d: biv discarded, %s\n",
3893 (REG_IV_TYPE (bl
->regno
) != BASIC_INDUCT
3894 ? "not induction variable"
3895 : (! bl
->incremented
? "never incremented"
3898 REG_IV_TYPE (bl
->regno
) = NOT_BASIC_INDUCT
;
3905 if (loop_dump_stream
)
3906 fprintf (loop_dump_stream
, "Reg %d: biv verified\n", bl
->regno
);
3910 /* Exit if there are no bivs. */
3913 /* Can still unroll the loop anyways, but indicate that there is no
3914 strength reduction info available. */
3916 unroll_loop (loop_end
, insn_count
, loop_start
, end_insert_before
,
3922 /* Find initial value for each biv by searching backwards from loop_start,
3923 halting at first label. Also record any test condition. */
3926 for (p
= loop_start
; p
&& GET_CODE (p
) != CODE_LABEL
; p
= PREV_INSN (p
))
3930 if (GET_CODE (p
) == CALL_INSN
)
3933 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
3934 || GET_CODE (p
) == CALL_INSN
)
3935 note_stores (PATTERN (p
), record_initial
);
3937 /* Record any test of a biv that branches around the loop if no store
3938 between it and the start of loop. We only care about tests with
3939 constants and registers and only certain of those. */
3940 if (GET_CODE (p
) == JUMP_INSN
3941 && JUMP_LABEL (p
) != 0
3942 && next_real_insn (JUMP_LABEL (p
)) == next_real_insn (loop_end
)
3943 && (test
= get_condition_for_loop (p
)) != 0
3944 && GET_CODE (XEXP (test
, 0)) == REG
3945 && REGNO (XEXP (test
, 0)) < max_reg_before_loop
3946 && (bl
= reg_biv_class
[REGNO (XEXP (test
, 0))]) != 0
3947 && valid_initial_value_p (XEXP (test
, 1), p
, call_seen
, loop_start
)
3948 && bl
->init_insn
== 0)
3950 /* If an NE test, we have an initial value! */
3951 if (GET_CODE (test
) == NE
)
3954 bl
->init_set
= gen_rtx_SET (VOIDmode
,
3955 XEXP (test
, 0), XEXP (test
, 1));
3958 bl
->initial_test
= test
;
3962 /* Look at the each biv and see if we can say anything better about its
3963 initial value from any initializing insns set up above. (This is done
3964 in two passes to avoid missing SETs in a PARALLEL.) */
3965 for (backbl
= &loop_iv_list
; (bl
= *backbl
); backbl
= &bl
->next
)
3970 if (! bl
->init_insn
)
3973 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3974 is a constant, use the value of that. */
3975 if (((note
= find_reg_note (bl
->init_insn
, REG_EQUAL
, 0)) != NULL
3976 && CONSTANT_P (XEXP (note
, 0)))
3977 || ((note
= find_reg_note (bl
->init_insn
, REG_EQUIV
, 0)) != NULL
3978 && CONSTANT_P (XEXP (note
, 0))))
3979 src
= XEXP (note
, 0);
3981 src
= SET_SRC (bl
->init_set
);
3983 if (loop_dump_stream
)
3984 fprintf (loop_dump_stream
,
3985 "Biv %d initialized at insn %d: initial value ",
3986 bl
->regno
, INSN_UID (bl
->init_insn
));
3988 if ((GET_MODE (src
) == GET_MODE (regno_reg_rtx
[bl
->regno
])
3989 || GET_MODE (src
) == VOIDmode
)
3990 && valid_initial_value_p (src
, bl
->init_insn
, call_seen
, loop_start
))
3992 bl
->initial_value
= src
;
3994 if (loop_dump_stream
)
3996 if (GET_CODE (src
) == CONST_INT
)
3998 fprintf (loop_dump_stream
, HOST_WIDE_INT_PRINT_DEC
, INTVAL (src
));
3999 fputc ('\n', loop_dump_stream
);
4003 print_rtl (loop_dump_stream
, src
);
4004 fprintf (loop_dump_stream
, "\n");
4010 struct iv_class
*bl2
= 0;
4013 /* Biv initial value is not a simple move. If it is the sum of
4014 another biv and a constant, check if both bivs are incremented
4015 in lockstep. Then we are actually looking at a giv.
4016 For simplicity, we only handle the case where there is but a
4017 single increment, and the register is not used elsewhere. */
4018 if (bl
->biv_count
== 1
4019 && bl
->regno
< max_reg_before_loop
4020 && uid_luid
[REGNO_LAST_UID (bl
->regno
)] < INSN_LUID (loop_end
)
4021 && GET_CODE (src
) == PLUS
4022 && GET_CODE (XEXP (src
, 0)) == REG
4023 && CONSTANT_P (XEXP (src
, 1))
4024 && ((increment
= biv_total_increment (bl
, loop_start
, loop_end
))
4027 int regno
= REGNO (XEXP (src
, 0));
4029 for (bl2
= loop_iv_list
; bl2
; bl2
= bl2
->next
)
4030 if (bl2
->regno
== regno
)
4034 /* Now, can we transform this biv into a giv? */
4036 && bl2
->biv_count
== 1
4037 && rtx_equal_p (increment
,
4038 biv_total_increment (bl2
, loop_start
, loop_end
))
4039 /* init_insn is only set to insns that are before loop_start
4040 without any intervening labels. */
4041 && ! reg_set_between_p (bl2
->biv
->src_reg
,
4042 PREV_INSN (bl
->init_insn
), loop_start
)
4043 /* The register from BL2 must be set before the register from
4044 BL is set, or we must be able to move the latter set after
4045 the former set. Currently there can't be any labels
4046 in-between when biv_toal_increment returns nonzero both times
4047 but we test it here in case some day some real cfg analysis
4048 gets used to set always_computable. */
4049 && ((loop_insn_first_p (bl2
->biv
->insn
, bl
->biv
->insn
)
4050 && no_labels_between_p (bl2
->biv
->insn
, bl
->biv
->insn
))
4051 || (! reg_used_between_p (bl
->biv
->src_reg
, bl
->biv
->insn
,
4053 && no_jumps_between_p (bl
->biv
->insn
, bl2
->biv
->insn
)))
4054 && validate_change (bl
->biv
->insn
,
4055 &SET_SRC (single_set (bl
->biv
->insn
)),
4058 int loop_num
= uid_loop_num
[INSN_UID (loop_start
)];
4059 rtx dominator
= loop_number_cont_dominator
[loop_num
];
4060 rtx giv
= bl
->biv
->src_reg
;
4061 rtx giv_insn
= bl
->biv
->insn
;
4062 rtx after_giv
= NEXT_INSN (giv_insn
);
4064 if (loop_dump_stream
)
4065 fprintf (loop_dump_stream
, "is giv of biv %d\n", bl2
->regno
);
4066 /* Let this giv be discovered by the generic code. */
4067 REG_IV_TYPE (bl
->regno
) = UNKNOWN_INDUCT
;
4068 reg_biv_class
[bl
->regno
] = NULL_PTR
;
4069 /* We can get better optimization if we can move the giv setting
4070 before the first giv use. */
4072 && ! loop_insn_first_p (dominator
, scan_start
)
4073 && ! reg_set_between_p (bl2
->biv
->src_reg
, loop_start
,
4075 && ! reg_used_between_p (giv
, loop_start
, dominator
)
4076 && ! reg_used_between_p (giv
, giv_insn
, loop_end
))
4081 for (next
= NEXT_INSN (dominator
); ; next
= NEXT_INSN (next
))
4083 if ((GET_RTX_CLASS (GET_CODE (next
)) == 'i'
4084 && (reg_mentioned_p (giv
, PATTERN (next
))
4085 || reg_set_p (bl2
->biv
->src_reg
, next
)))
4086 || GET_CODE (next
) == JUMP_INSN
)
4089 if (GET_RTX_CLASS (GET_CODE (next
)) != 'i'
4090 || ! sets_cc0_p (PATTERN (next
)))
4094 if (loop_dump_stream
)
4095 fprintf (loop_dump_stream
, "move after insn %d\n",
4096 INSN_UID (dominator
));
4097 /* Avoid problems with luids by actually moving the insn
4098 and adjusting all luids in the range. */
4099 reorder_insns (giv_insn
, giv_insn
, dominator
);
4100 for (p
= dominator
; INSN_UID (p
) >= max_uid_for_loop
; )
4102 compute_luids (giv_insn
, after_giv
, INSN_LUID (p
));
4103 /* If the only purpose of the init insn is to initialize
4104 this giv, delete it. */
4105 if (single_set (bl
->init_insn
)
4106 && ! reg_used_between_p (giv
, bl
->init_insn
, loop_start
))
4107 delete_insn (bl
->init_insn
);
4109 else if (! loop_insn_first_p (bl2
->biv
->insn
, bl
->biv
->insn
))
4111 rtx p
= PREV_INSN (giv_insn
);
4112 while (INSN_UID (p
) >= max_uid_for_loop
)
4114 reorder_insns (giv_insn
, giv_insn
, bl2
->biv
->insn
);
4115 compute_luids (after_giv
, NEXT_INSN (giv_insn
),
4118 /* Remove this biv from the chain. */
4121 /* We move the following giv from *bl->next into *bl.
4122 We have to update reg_biv_class for that moved biv
4123 to point to its new address. */
4125 reg_biv_class
[bl
->regno
] = bl
;
4134 /* If we can't make it a giv,
4135 let biv keep initial value of "itself". */
4136 else if (loop_dump_stream
)
4137 fprintf (loop_dump_stream
, "is complex\n");
4141 /* If a biv is unconditionally incremented several times in a row, convert
4142 all but the last increment into a giv. */
4144 /* Get an upper bound for the number of registers
4145 we might have after all bivs have been processed. */
4146 first_increment_giv
= max_reg_num ();
4147 for (n_extra_increment
= 0, bl
= loop_iv_list
; bl
; bl
= bl
->next
)
4148 n_extra_increment
+= bl
->biv_count
- 1;
4150 /* If the loop contains volatile memory references do not allow any
4151 replacements to take place, since this could loose the volatile markers. */
4152 if (n_extra_increment
&& ! loop_has_volatile
)
4154 int nregs
= first_increment_giv
+ n_extra_increment
;
4156 /* Reallocate reg_iv_type and reg_iv_info. */
4157 VARRAY_GROW (reg_iv_type
, nregs
);
4158 VARRAY_GROW (reg_iv_info
, nregs
);
4160 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
4162 struct induction
**vp
, *v
, *next
;
4163 int biv_dead_after_loop
= 0;
4165 /* The biv increments lists are in reverse order. Fix this first. */
4166 for (v
= bl
->biv
, bl
->biv
= 0; v
; v
= next
)
4169 v
->next_iv
= bl
->biv
;
4173 /* We must guard against the case that an early exit between v->insn
4174 and next->insn leaves the biv live after the loop, since that
4175 would mean that we'd be missing an increment for the final
4176 value. The following test to set biv_dead_after_loop is like
4177 the first part of the test to set bl->eliminable.
4178 We don't check here if we can calculate the final value, since
4179 this can't succeed if we already know that there is a jump
4180 between v->insn and next->insn, yet next->always_executed is
4181 set and next->maybe_multiple is cleared. Such a combination
4182 implies that the jump destination is outside the loop.
4183 If we want to make this check more sophisticated, we should
4184 check each branch between v->insn and next->insn individually
4185 to see if the biv is dead at its destination. */
4187 if (uid_luid
[REGNO_LAST_UID (bl
->regno
)] < INSN_LUID (loop_end
)
4189 && INSN_UID (bl
->init_insn
) < max_uid_for_loop
4190 && (uid_luid
[REGNO_FIRST_UID (bl
->regno
)]
4191 >= INSN_LUID (bl
->init_insn
))
4192 #ifdef HAVE_decrement_and_branch_until_zero
4195 && ! reg_mentioned_p (bl
->biv
->dest_reg
, SET_SRC (bl
->init_set
)))
4196 biv_dead_after_loop
= 1;
4198 for (vp
= &bl
->biv
, next
= *vp
; v
= next
, next
= v
->next_iv
;)
4200 HOST_WIDE_INT offset
;
4201 rtx set
, add_val
, old_reg
, dest_reg
, last_use_insn
;
4202 int old_regno
, new_regno
;
4204 if (! v
->always_executed
4205 || v
->maybe_multiple
4206 || GET_CODE (v
->add_val
) != CONST_INT
4207 || ! next
->always_executed
4208 || next
->maybe_multiple
4209 || ! CONSTANT_P (next
->add_val
)
4210 || v
->mult_val
!= const1_rtx
4211 || next
->mult_val
!= const1_rtx
4212 || ! (biv_dead_after_loop
4213 || no_jumps_between_p (v
->insn
, next
->insn
)))
4218 offset
= INTVAL (v
->add_val
);
4219 set
= single_set (v
->insn
);
4220 add_val
= plus_constant (next
->add_val
, offset
);
4221 old_reg
= v
->dest_reg
;
4222 dest_reg
= gen_reg_rtx (v
->mode
);
4224 /* Unlike reg_iv_type / reg_iv_info, the other three arrays
4225 have been allocated with some slop space, so we may not
4226 actually need to reallocate them. If we do, the following
4227 if statement will be executed just once in this loop. */
4228 if ((unsigned) max_reg_num () > n_times_set
->num_elements
)
4230 /* Grow all the remaining arrays. */
4231 VARRAY_GROW (set_in_loop
, nregs
);
4232 VARRAY_GROW (n_times_set
, nregs
);
4233 VARRAY_GROW (may_not_optimize
, nregs
);
4234 VARRAY_GROW (reg_single_usage
, nregs
);
4237 if (! validate_change (next
->insn
, next
->location
, add_val
, 0))
4243 /* Here we can try to eliminate the increment by combining
4244 it into the uses. */
4246 /* Set last_use_insn so that we can check against it. */
4248 for (last_use_insn
= v
->insn
, p
= NEXT_INSN (v
->insn
);
4250 p
= next_insn_in_loop (p
, scan_start
, end
, loop_top
))
4252 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
4254 if (reg_mentioned_p (old_reg
, PATTERN (p
)))
4260 /* If we can't get the LUIDs for the insns, we can't
4261 calculate the lifetime. This is likely from unrolling
4262 of an inner loop, so there is little point in making this
4263 a DEST_REG giv anyways. */
4264 if (INSN_UID (v
->insn
) >= max_uid_for_loop
4265 || INSN_UID (last_use_insn
) >= max_uid_for_loop
4266 || ! validate_change (v
->insn
, &SET_DEST (set
), dest_reg
, 0))
4268 /* Change the increment at NEXT back to what it was. */
4269 if (! validate_change (next
->insn
, next
->location
,
4275 next
->add_val
= add_val
;
4276 v
->dest_reg
= dest_reg
;
4277 v
->giv_type
= DEST_REG
;
4278 v
->location
= &SET_SRC (set
);
4280 v
->combined_with
= 0;
4282 v
->derive_adjustment
= 0;
4288 v
->auto_inc_opt
= 0;
4291 v
->derived_from
= 0;
4292 v
->always_computable
= 1;
4293 v
->always_executed
= 1;
4295 v
->no_const_addval
= 0;
4297 old_regno
= REGNO (old_reg
);
4298 new_regno
= REGNO (dest_reg
);
4299 VARRAY_INT (set_in_loop
, old_regno
)--;
4300 VARRAY_INT (set_in_loop
, new_regno
) = 1;
4301 VARRAY_INT (n_times_set
, old_regno
)--;
4302 VARRAY_INT (n_times_set
, new_regno
) = 1;
4303 VARRAY_CHAR (may_not_optimize
, new_regno
) = 0;
4305 REG_IV_TYPE (new_regno
) = GENERAL_INDUCT
;
4306 REG_IV_INFO (new_regno
) = v
;
4308 /* Remove the increment from the list of biv increments,
4309 and record it as a giv. */
4312 v
->next_iv
= bl
->giv
;
4315 v
->benefit
= rtx_cost (SET_SRC (set
), SET
);
4316 bl
->total_benefit
+= v
->benefit
;
4318 /* Now replace the biv with DEST_REG in all insns between
4319 the replaced increment and the next increment, and
4320 remember the last insn that needed a replacement. */
4321 for (last_use_insn
= v
->insn
, p
= NEXT_INSN (v
->insn
);
4323 p
= next_insn_in_loop (p
, scan_start
, end
, loop_top
))
4327 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
4329 if (reg_mentioned_p (old_reg
, PATTERN (p
)))
4332 if (! validate_replace_rtx (old_reg
, dest_reg
, p
))
4335 for (note
= REG_NOTES (p
); note
; note
= XEXP (note
, 1))
4337 if (GET_CODE (note
) == EXPR_LIST
)
4339 = replace_rtx (XEXP (note
, 0), old_reg
, dest_reg
);
4343 v
->last_use
= last_use_insn
;
4344 v
->lifetime
= INSN_LUID (v
->insn
) - INSN_LUID (last_use_insn
);
4345 /* If the lifetime is zero, it means that this register is really
4346 a dead store. So mark this as a giv that can be ignored.
4347 This will not prevent the biv from being eliminated. */
4348 if (v
->lifetime
== 0)
4351 if (loop_dump_stream
)
4352 fprintf (loop_dump_stream
,
4353 "Increment %d of biv %d converted to giv %d.\n\n",
4354 INSN_UID (v
->insn
), old_regno
, new_regno
);
4358 last_increment_giv
= max_reg_num () - 1;
4360 /* Search the loop for general induction variables. */
4362 /* A register is a giv if: it is only set once, it is a function of a
4363 biv and a constant (or invariant), and it is not a biv. */
4365 not_every_iteration
= 0;
4371 /* At end of a straight-in loop, we are done.
4372 At end of a loop entered at the bottom, scan the top. */
4373 if (p
== scan_start
)
4381 if (p
== scan_start
)
4385 /* Look for a general induction variable in a register. */
4386 if (GET_CODE (p
) == INSN
4387 && (set
= single_set (p
))
4388 && GET_CODE (SET_DEST (set
)) == REG
4389 && ! VARRAY_CHAR (may_not_optimize
, REGNO (SET_DEST (set
))))
4396 rtx last_consec_insn
;
4398 dest_reg
= SET_DEST (set
);
4399 if (REGNO (dest_reg
) < FIRST_PSEUDO_REGISTER
)
4402 if (/* SET_SRC is a giv. */
4403 (general_induction_var (SET_SRC (set
), &src_reg
, &add_val
,
4404 &mult_val
, 0, &benefit
)
4405 /* Equivalent expression is a giv. */
4406 || ((regnote
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
4407 && general_induction_var (XEXP (regnote
, 0), &src_reg
,
4408 &add_val
, &mult_val
, 0,
4410 /* Don't try to handle any regs made by loop optimization.
4411 We have nothing on them in regno_first_uid, etc. */
4412 && REGNO (dest_reg
) < max_reg_before_loop
4413 /* Don't recognize a BASIC_INDUCT_VAR here. */
4414 && dest_reg
!= src_reg
4415 /* This must be the only place where the register is set. */
4416 && (VARRAY_INT (n_times_set
, REGNO (dest_reg
)) == 1
4417 /* or all sets must be consecutive and make a giv. */
4418 || (benefit
= consec_sets_giv (benefit
, p
,
4420 &add_val
, &mult_val
,
4421 &last_consec_insn
))))
4424 = (struct induction
*) alloca (sizeof (struct induction
));
4426 /* If this is a library call, increase benefit. */
4427 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
4428 benefit
+= libcall_benefit (p
);
4430 /* Skip the consecutive insns, if there are any. */
4431 if (VARRAY_INT (n_times_set
, REGNO (dest_reg
)) != 1)
4432 p
= last_consec_insn
;
4434 record_giv (v
, p
, src_reg
, dest_reg
, mult_val
, add_val
, benefit
,
4435 DEST_REG
, not_every_iteration
, NULL_PTR
, loop_start
,
4441 #ifndef DONT_REDUCE_ADDR
4442 /* Look for givs which are memory addresses. */
4443 /* This resulted in worse code on a VAX 8600. I wonder if it
4445 if (GET_CODE (p
) == INSN
)
4446 find_mem_givs (PATTERN (p
), p
, not_every_iteration
, loop_start
,
4450 /* Update the status of whether giv can derive other givs. This can
4451 change when we pass a label or an insn that updates a biv. */
4452 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
4453 || GET_CODE (p
) == CODE_LABEL
)
4454 update_giv_derive (p
);
4456 /* Past a jump, we get to insns for which we can't count
4457 on whether they will be executed during each iteration. */
4458 /* This code appears twice in strength_reduce. There is also similar
4459 code in scan_loop. */
4460 if (GET_CODE (p
) == JUMP_INSN
4461 /* If we enter the loop in the middle, and scan around to the
4462 beginning, don't set not_every_iteration for that.
4463 This can be any kind of jump, since we want to know if insns
4464 will be executed if the loop is executed. */
4465 && ! (JUMP_LABEL (p
) == loop_top
4466 && ((NEXT_INSN (NEXT_INSN (p
)) == loop_end
&& simplejump_p (p
))
4467 || (NEXT_INSN (p
) == loop_end
&& condjump_p (p
)))))
4471 /* If this is a jump outside the loop, then it also doesn't
4472 matter. Check to see if the target of this branch is on the
4473 loop_number_exits_labels list. */
4475 for (label
= loop_number_exit_labels
[uid_loop_num
[INSN_UID (loop_start
)]];
4477 label
= LABEL_NEXTREF (label
))
4478 if (XEXP (label
, 0) == JUMP_LABEL (p
))
4482 not_every_iteration
= 1;
4485 else if (GET_CODE (p
) == NOTE
)
4487 /* At the virtual top of a converted loop, insns are again known to
4488 be executed each iteration: logically, the loop begins here
4489 even though the exit code has been duplicated.
4491 Insns are also again known to be executed each iteration at
4492 the LOOP_CONT note. */
4493 if ((NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
4494 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_CONT
)
4496 not_every_iteration
= 0;
4497 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
4499 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)
4503 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4504 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4505 or not an insn is known to be executed each iteration of the
4506 loop, whether or not any iterations are known to occur.
4508 Therefore, if we have just passed a label and have no more labels
4509 between here and the test insn of the loop, we know these insns
4510 will be executed each iteration. */
4512 if (not_every_iteration
&& GET_CODE (p
) == CODE_LABEL
4513 && no_labels_between_p (p
, loop_end
)
4514 && loop_insn_first_p (p
, loop_cont
))
4515 not_every_iteration
= 0;
4518 /* Try to calculate and save the number of loop iterations. This is
4519 set to zero if the actual number can not be calculated. This must
4520 be called after all giv's have been identified, since otherwise it may
4521 fail if the iteration variable is a giv. */
4523 loop_iterations (loop_start
, loop_end
, loop_info
);
4525 /* Now for each giv for which we still don't know whether or not it is
4526 replaceable, check to see if it is replaceable because its final value
4527 can be calculated. This must be done after loop_iterations is called,
4528 so that final_giv_value will work correctly. */
4530 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
4532 struct induction
*v
;
4534 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4535 if (! v
->replaceable
&& ! v
->not_replaceable
)
4536 check_final_value (v
, loop_start
, loop_end
, loop_info
->n_iterations
);
4539 /* Try to prove that the loop counter variable (if any) is always
4540 nonnegative; if so, record that fact with a REG_NONNEG note
4541 so that "decrement and branch until zero" insn can be used. */
4542 check_dbra_loop (loop_end
, insn_count
, loop_start
, loop_info
);
4544 /* Create reg_map to hold substitutions for replaceable giv regs.
4545 Some givs might have been made from biv increments, so look at
4546 reg_iv_type for a suitable size. */
4547 reg_map_size
= reg_iv_type
->num_elements
;
4548 reg_map
= (rtx
*) alloca (reg_map_size
* sizeof (rtx
));
4549 bzero ((char *) reg_map
, reg_map_size
* sizeof (rtx
));
4551 /* Examine each iv class for feasibility of strength reduction/induction
4552 variable elimination. */
4554 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
4556 struct induction
*v
;
4559 rtx final_value
= 0;
4562 /* Test whether it will be possible to eliminate this biv
4563 provided all givs are reduced. This is possible if either
4564 the reg is not used outside the loop, or we can compute
4565 what its final value will be.
4567 For architectures with a decrement_and_branch_until_zero insn,
4568 don't do this if we put a REG_NONNEG note on the endtest for
4571 /* Compare against bl->init_insn rather than loop_start.
4572 We aren't concerned with any uses of the biv between
4573 init_insn and loop_start since these won't be affected
4574 by the value of the biv elsewhere in the function, so
4575 long as init_insn doesn't use the biv itself.
4576 March 14, 1989 -- self@bayes.arc.nasa.gov */
4578 if ((uid_luid
[REGNO_LAST_UID (bl
->regno
)] < INSN_LUID (loop_end
)
4580 && INSN_UID (bl
->init_insn
) < max_uid_for_loop
4581 && uid_luid
[REGNO_FIRST_UID (bl
->regno
)] >= INSN_LUID (bl
->init_insn
)
4582 #ifdef HAVE_decrement_and_branch_until_zero
4585 && ! reg_mentioned_p (bl
->biv
->dest_reg
, SET_SRC (bl
->init_set
)))
4586 || ((final_value
= final_biv_value (bl
, loop_start
, loop_end
,
4587 loop_info
->n_iterations
))
4588 #ifdef HAVE_decrement_and_branch_until_zero
4592 bl
->eliminable
= maybe_eliminate_biv (bl
, loop_start
, end
, 0,
4593 threshold
, insn_count
);
4596 if (loop_dump_stream
)
4598 fprintf (loop_dump_stream
,
4599 "Cannot eliminate biv %d.\n",
4601 fprintf (loop_dump_stream
,
4602 "First use: insn %d, last use: insn %d.\n",
4603 REGNO_FIRST_UID (bl
->regno
),
4604 REGNO_LAST_UID (bl
->regno
));
4608 /* Combine all giv's for this iv_class. */
4611 /* This will be true at the end, if all givs which depend on this
4612 biv have been strength reduced.
4613 We can't (currently) eliminate the biv unless this is so. */
4616 /* Check each giv in this class to see if we will benefit by reducing
4617 it. Skip giv's combined with others. */
4618 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4620 struct induction
*tv
;
4622 if (v
->ignore
|| v
->same
)
4625 benefit
= v
->benefit
;
4627 /* Reduce benefit if not replaceable, since we will insert
4628 a move-insn to replace the insn that calculates this giv.
4629 Don't do this unless the giv is a user variable, since it
4630 will often be marked non-replaceable because of the duplication
4631 of the exit code outside the loop. In such a case, the copies
4632 we insert are dead and will be deleted. So they don't have
4633 a cost. Similar situations exist. */
4634 /* ??? The new final_[bg]iv_value code does a much better job
4635 of finding replaceable giv's, and hence this code may no longer
4637 if (! v
->replaceable
&& ! bl
->eliminable
4638 && REG_USERVAR_P (v
->dest_reg
))
4639 benefit
-= copy_cost
;
4641 /* Decrease the benefit to count the add-insns that we will
4642 insert to increment the reduced reg for the giv. */
4643 benefit
-= add_cost
* bl
->biv_count
;
4645 /* Decide whether to strength-reduce this giv or to leave the code
4646 unchanged (recompute it from the biv each time it is used).
4647 This decision can be made independently for each giv. */
4650 /* Attempt to guess whether autoincrement will handle some of the
4651 new add insns; if so, increase BENEFIT (undo the subtraction of
4652 add_cost that was done above). */
4653 if (v
->giv_type
== DEST_ADDR
4654 && GET_CODE (v
->mult_val
) == CONST_INT
)
4656 if (HAVE_POST_INCREMENT
4657 && INTVAL (v
->mult_val
) == GET_MODE_SIZE (v
->mem_mode
))
4658 benefit
+= add_cost
* bl
->biv_count
;
4659 else if (HAVE_PRE_INCREMENT
4660 && INTVAL (v
->mult_val
) == GET_MODE_SIZE (v
->mem_mode
))
4661 benefit
+= add_cost
* bl
->biv_count
;
4662 else if (HAVE_POST_DECREMENT
4663 && -INTVAL (v
->mult_val
) == GET_MODE_SIZE (v
->mem_mode
))
4664 benefit
+= add_cost
* bl
->biv_count
;
4665 else if (HAVE_PRE_DECREMENT
4666 && -INTVAL (v
->mult_val
) == GET_MODE_SIZE (v
->mem_mode
))
4667 benefit
+= add_cost
* bl
->biv_count
;
4671 /* If an insn is not to be strength reduced, then set its ignore
4672 flag, and clear all_reduced. */
4674 /* A giv that depends on a reversed biv must be reduced if it is
4675 used after the loop exit, otherwise, it would have the wrong
4676 value after the loop exit. To make it simple, just reduce all
4677 of such giv's whether or not we know they are used after the loop
4680 if ( ! flag_reduce_all_givs
&& v
->lifetime
* threshold
* benefit
< insn_count
4683 if (loop_dump_stream
)
4684 fprintf (loop_dump_stream
,
4685 "giv of insn %d not worth while, %d vs %d.\n",
4687 v
->lifetime
* threshold
* benefit
, insn_count
);
4693 /* Check that we can increment the reduced giv without a
4694 multiply insn. If not, reject it. */
4696 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
4697 if (tv
->mult_val
== const1_rtx
4698 && ! product_cheap_p (tv
->add_val
, v
->mult_val
))
4700 if (loop_dump_stream
)
4701 fprintf (loop_dump_stream
,
4702 "giv of insn %d: would need a multiply.\n",
4703 INSN_UID (v
->insn
));
4711 /* Check for givs whose first use is their definition and whose
4712 last use is the definition of another giv. If so, it is likely
4713 dead and should not be used to derive another giv nor to
4715 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4718 || (v
->same
&& v
->same
->ignore
))
4723 struct induction
*v1
;
4725 for (v1
= bl
->giv
; v1
; v1
= v1
->next_iv
)
4726 if (v
->last_use
== v1
->insn
)
4729 else if (v
->giv_type
== DEST_REG
4730 && REGNO_FIRST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v
->insn
))
4732 struct induction
*v1
;
4734 for (v1
= bl
->giv
; v1
; v1
= v1
->next_iv
)
4735 if (REGNO_LAST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v1
->insn
))
4740 /* Now that we know which givs will be reduced, try to rearrange the
4741 combinations to reduce register pressure.
4742 recombine_givs calls find_life_end, which needs reg_iv_type and
4743 reg_iv_info to be valid for all pseudos. We do the necessary
4744 reallocation here since it allows to check if there are still
4745 more bivs to process. */
4746 nregs
= max_reg_num ();
4747 if (nregs
> reg_iv_type
->num_elements
)
4749 /* If there are still more bivs to process, allocate some slack
4750 space so that we're not constantly reallocating these arrays. */
4753 /* Reallocate reg_iv_type and reg_iv_info. */
4754 VARRAY_GROW (reg_iv_type
, nregs
);
4755 VARRAY_GROW (reg_iv_info
, nregs
);
4757 recombine_givs (bl
, loop_start
, loop_end
, unroll_p
);
4759 /* Reduce each giv that we decided to reduce. */
4761 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4763 struct induction
*tv
;
4764 if (! v
->ignore
&& v
->same
== 0)
4766 int auto_inc_opt
= 0;
4768 /* If the code for derived givs immediately below has already
4769 allocated a new_reg, we must keep it. */
4771 v
->new_reg
= gen_reg_rtx (v
->mode
);
4773 if (v
->derived_from
)
4775 struct induction
*d
= v
->derived_from
;
4777 /* In case d->dest_reg is not replaceable, we have
4778 to replace it in v->insn now. */
4780 d
->new_reg
= gen_reg_rtx (d
->mode
);
4782 = replace_rtx (PATTERN (v
->insn
), d
->dest_reg
, d
->new_reg
);
4784 = replace_rtx (PATTERN (v
->insn
), v
->dest_reg
, v
->new_reg
);
4785 /* For each place where the biv is incremented, add an
4786 insn to set the new, reduced reg for the giv.
4787 We used to do this only for biv_count != 1, but
4788 this fails when there is a giv after a single biv
4789 increment, e.g. when the last giv was expressed as
4791 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
4793 /* We always emit reduced giv increments before the
4794 biv increment when bl->biv_count != 1. So by
4795 emitting the add insns for derived givs after the
4796 biv increment, they pick up the updated value of
4798 If the reduced giv is processed with
4799 auto_inc_opt == 1, then it is incremented earlier
4800 than the biv, hence we'll still pick up the right
4802 If it's processed with auto_inc_opt == -1,
4803 that implies that the biv increment is before the
4804 first reduced giv's use. The derived giv's lifetime
4805 is after the reduced giv's lifetime, hence in this
4806 case, the biv increment doesn't matter. */
4807 emit_insn_after (copy_rtx (PATTERN (v
->insn
)), tv
->insn
);
4813 /* If the target has auto-increment addressing modes, and
4814 this is an address giv, then try to put the increment
4815 immediately after its use, so that flow can create an
4816 auto-increment addressing mode. */
4817 if (v
->giv_type
== DEST_ADDR
&& bl
->biv_count
== 1
4818 && bl
->biv
->always_executed
&& ! bl
->biv
->maybe_multiple
4819 /* We don't handle reversed biv's because bl->biv->insn
4820 does not have a valid INSN_LUID. */
4822 && v
->always_executed
&& ! v
->maybe_multiple
4823 && INSN_UID (v
->insn
) < max_uid_for_loop
)
4825 /* If other giv's have been combined with this one, then
4826 this will work only if all uses of the other giv's occur
4827 before this giv's insn. This is difficult to check.
4829 We simplify this by looking for the common case where
4830 there is one DEST_REG giv, and this giv's insn is the
4831 last use of the dest_reg of that DEST_REG giv. If the
4832 increment occurs after the address giv, then we can
4833 perform the optimization. (Otherwise, the increment
4834 would have to go before other_giv, and we would not be
4835 able to combine it with the address giv to get an
4836 auto-inc address.) */
4837 if (v
->combined_with
)
4839 struct induction
*other_giv
= 0;
4841 for (tv
= bl
->giv
; tv
; tv
= tv
->next_iv
)
4849 if (! tv
&& other_giv
4850 && REGNO (other_giv
->dest_reg
) < max_reg_before_loop
4851 && (REGNO_LAST_UID (REGNO (other_giv
->dest_reg
))
4852 == INSN_UID (v
->insn
))
4853 && INSN_LUID (v
->insn
) < INSN_LUID (bl
->biv
->insn
))
4856 /* Check for case where increment is before the address
4857 giv. Do this test in "loop order". */
4858 else if ((INSN_LUID (v
->insn
) > INSN_LUID (bl
->biv
->insn
)
4859 && (INSN_LUID (v
->insn
) < INSN_LUID (scan_start
)
4860 || (INSN_LUID (bl
->biv
->insn
)
4861 > INSN_LUID (scan_start
))))
4862 || (INSN_LUID (v
->insn
) < INSN_LUID (scan_start
)
4863 && (INSN_LUID (scan_start
)
4864 < INSN_LUID (bl
->biv
->insn
))))
4873 /* We can't put an insn immediately after one setting
4874 cc0, or immediately before one using cc0. */
4875 if ((auto_inc_opt
== 1 && sets_cc0_p (PATTERN (v
->insn
)))
4876 || (auto_inc_opt
== -1
4877 && (prev
= prev_nonnote_insn (v
->insn
)) != 0
4878 && GET_RTX_CLASS (GET_CODE (prev
)) == 'i'
4879 && sets_cc0_p (PATTERN (prev
))))
4885 v
->auto_inc_opt
= 1;
4889 /* For each place where the biv is incremented, add an insn
4890 to increment the new, reduced reg for the giv. */
4891 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
4896 insert_before
= tv
->insn
;
4897 else if (auto_inc_opt
== 1)
4898 insert_before
= NEXT_INSN (v
->insn
);
4900 insert_before
= v
->insn
;
4902 if (tv
->mult_val
== const1_rtx
)
4903 emit_iv_add_mult (tv
->add_val
, v
->mult_val
,
4904 v
->new_reg
, v
->new_reg
, insert_before
);
4905 else /* tv->mult_val == const0_rtx */
4906 /* A multiply is acceptable here
4907 since this is presumed to be seldom executed. */
4908 emit_iv_add_mult (tv
->add_val
, v
->mult_val
,
4909 v
->add_val
, v
->new_reg
, insert_before
);
4912 /* Add code at loop start to initialize giv's reduced reg. */
4914 emit_iv_add_mult (bl
->initial_value
, v
->mult_val
,
4915 v
->add_val
, v
->new_reg
, loop_start
);
4919 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4922 For each giv register that can be reduced now: if replaceable,
4923 substitute reduced reg wherever the old giv occurs;
4924 else add new move insn "giv_reg = reduced_reg". */
4926 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4928 if (v
->same
&& v
->same
->ignore
)
4934 /* Update expression if this was combined, in case other giv was
4937 v
->new_reg
= replace_rtx (v
->new_reg
,
4938 v
->same
->dest_reg
, v
->same
->new_reg
);
4940 if (v
->giv_type
== DEST_ADDR
)
4941 /* Store reduced reg as the address in the memref where we found
4943 validate_change (v
->insn
, v
->location
, v
->new_reg
, 0);
4944 else if (v
->replaceable
)
4946 reg_map
[REGNO (v
->dest_reg
)] = v
->new_reg
;
4949 /* I can no longer duplicate the original problem. Perhaps
4950 this is unnecessary now? */
4952 /* Replaceable; it isn't strictly necessary to delete the old
4953 insn and emit a new one, because v->dest_reg is now dead.
4955 However, especially when unrolling loops, the special
4956 handling for (set REG0 REG1) in the second cse pass may
4957 make v->dest_reg live again. To avoid this problem, emit
4958 an insn to set the original giv reg from the reduced giv.
4959 We can not delete the original insn, since it may be part
4960 of a LIBCALL, and the code in flow that eliminates dead
4961 libcalls will fail if it is deleted. */
4962 emit_insn_after (gen_move_insn (v
->dest_reg
, v
->new_reg
),
4968 /* Not replaceable; emit an insn to set the original giv reg from
4969 the reduced giv, same as above. */
4970 emit_insn_after (gen_move_insn (v
->dest_reg
, v
->new_reg
),
4974 /* When a loop is reversed, givs which depend on the reversed
4975 biv, and which are live outside the loop, must be set to their
4976 correct final value. This insn is only needed if the giv is
4977 not replaceable. The correct final value is the same as the
4978 value that the giv starts the reversed loop with. */
4979 if (bl
->reversed
&& ! v
->replaceable
)
4980 emit_iv_add_mult (bl
->initial_value
, v
->mult_val
,
4981 v
->add_val
, v
->dest_reg
, end_insert_before
);
4982 else if (v
->final_value
)
4986 /* If the loop has multiple exits, emit the insn before the
4987 loop to ensure that it will always be executed no matter
4988 how the loop exits. Otherwise, emit the insn after the loop,
4989 since this is slightly more efficient. */
4990 if (loop_number_exit_count
[uid_loop_num
[INSN_UID (loop_start
)]])
4991 insert_before
= loop_start
;
4993 insert_before
= end_insert_before
;
4994 emit_insn_before (gen_move_insn (v
->dest_reg
, v
->final_value
),
4998 /* If the insn to set the final value of the giv was emitted
4999 before the loop, then we must delete the insn inside the loop
5000 that sets it. If this is a LIBCALL, then we must delete
5001 every insn in the libcall. Note, however, that
5002 final_giv_value will only succeed when there are multiple
5003 exits if the giv is dead at each exit, hence it does not
5004 matter that the original insn remains because it is dead
5006 /* Delete the insn inside the loop that sets the giv since
5007 the giv is now set before (or after) the loop. */
5008 delete_insn (v
->insn
);
5012 if (loop_dump_stream
)
5014 fprintf (loop_dump_stream
, "giv at %d reduced to ",
5015 INSN_UID (v
->insn
));
5016 print_rtl (loop_dump_stream
, v
->new_reg
);
5017 fprintf (loop_dump_stream
, "\n");
5021 /* All the givs based on the biv bl have been reduced if they
5024 /* For each giv not marked as maybe dead that has been combined with a
5025 second giv, clear any "maybe dead" mark on that second giv.
5026 v->new_reg will either be or refer to the register of the giv it
5029 Doing this clearing avoids problems in biv elimination where a
5030 giv's new_reg is a complex value that can't be put in the insn but
5031 the giv combined with (with a reg as new_reg) is marked maybe_dead.
5032 Since the register will be used in either case, we'd prefer it be
5033 used from the simpler giv. */
5035 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5036 if (! v
->maybe_dead
&& v
->same
)
5037 v
->same
->maybe_dead
= 0;
5039 /* Try to eliminate the biv, if it is a candidate.
5040 This won't work if ! all_reduced,
5041 since the givs we planned to use might not have been reduced.
5043 We have to be careful that we didn't initially think we could eliminate
5044 this biv because of a giv that we now think may be dead and shouldn't
5045 be used as a biv replacement.
5047 Also, there is the possibility that we may have a giv that looks
5048 like it can be used to eliminate a biv, but the resulting insn
5049 isn't valid. This can happen, for example, on the 88k, where a
5050 JUMP_INSN can compare a register only with zero. Attempts to
5051 replace it with a compare with a constant will fail.
5053 Note that in cases where this call fails, we may have replaced some
5054 of the occurrences of the biv with a giv, but no harm was done in
5055 doing so in the rare cases where it can occur. */
5057 if (all_reduced
== 1 && bl
->eliminable
5058 && maybe_eliminate_biv (bl
, loop_start
, end
, 1,
5059 threshold
, insn_count
))
5062 /* ?? If we created a new test to bypass the loop entirely,
5063 or otherwise drop straight in, based on this test, then
5064 we might want to rewrite it also. This way some later
5065 pass has more hope of removing the initialization of this
5068 /* If final_value != 0, then the biv may be used after loop end
5069 and we must emit an insn to set it just in case.
5071 Reversed bivs already have an insn after the loop setting their
5072 value, so we don't need another one. We can't calculate the
5073 proper final value for such a biv here anyways. */
5074 if (final_value
!= 0 && ! bl
->reversed
)
5078 /* If the loop has multiple exits, emit the insn before the
5079 loop to ensure that it will always be executed no matter
5080 how the loop exits. Otherwise, emit the insn after the
5081 loop, since this is slightly more efficient. */
5082 if (loop_number_exit_count
[uid_loop_num
[INSN_UID (loop_start
)]])
5083 insert_before
= loop_start
;
5085 insert_before
= end_insert_before
;
5087 emit_insn_before (gen_move_insn (bl
->biv
->dest_reg
, final_value
),
5092 /* Delete all of the instructions inside the loop which set
5093 the biv, as they are all dead. If is safe to delete them,
5094 because an insn setting a biv will never be part of a libcall. */
5095 /* However, deleting them will invalidate the regno_last_uid info,
5096 so keeping them around is more convenient. Final_biv_value
5097 will only succeed when there are multiple exits if the biv
5098 is dead at each exit, hence it does not matter that the original
5099 insn remains, because it is dead anyways. */
5100 for (v
= bl
->biv
; v
; v
= v
->next_iv
)
5101 delete_insn (v
->insn
);
5104 if (loop_dump_stream
)
5105 fprintf (loop_dump_stream
, "Reg %d: biv eliminated\n",
5110 /* Go through all the instructions in the loop, making all the
5111 register substitutions scheduled in REG_MAP. */
5113 for (p
= loop_start
; p
!= end
; p
= NEXT_INSN (p
))
5114 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
5115 || GET_CODE (p
) == CALL_INSN
)
5117 replace_regs (PATTERN (p
), reg_map
, reg_map_size
, 0);
5118 replace_regs (REG_NOTES (p
), reg_map
, reg_map_size
, 0);
5122 /* Unroll loops from within strength reduction so that we can use the
5123 induction variable information that strength_reduce has already
5127 unroll_loop (loop_end
, insn_count
, loop_start
, end_insert_before
,
5130 #ifdef HAVE_decrement_and_branch_on_count
5131 /* Instrument the loop with BCT insn. */
5132 if (HAVE_decrement_and_branch_on_count
&& bct_p
5133 && flag_branch_on_count_reg
)
5134 insert_bct (loop_start
, loop_end
, loop_info
);
5135 #endif /* HAVE_decrement_and_branch_on_count */
5137 if (loop_dump_stream
)
5138 fprintf (loop_dump_stream
, "\n");
5139 VARRAY_FREE (reg_iv_type
);
5140 VARRAY_FREE (reg_iv_info
);
5143 /* Return 1 if X is a valid source for an initial value (or as value being
5144 compared against in an initial test).
5146 X must be either a register or constant and must not be clobbered between
5147 the current insn and the start of the loop.
5149 INSN is the insn containing X. */
5152 valid_initial_value_p (x
, insn
, call_seen
, loop_start
)
5161 /* Only consider pseudos we know about initialized in insns whose luids
5163 if (GET_CODE (x
) != REG
5164 || REGNO (x
) >= max_reg_before_loop
)
5167 /* Don't use call-clobbered registers across a call which clobbers it. On
5168 some machines, don't use any hard registers at all. */
5169 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
5170 && (SMALL_REGISTER_CLASSES
5171 || (call_used_regs
[REGNO (x
)] && call_seen
)))
5174 /* Don't use registers that have been clobbered before the start of the
5176 if (reg_set_between_p (x
, insn
, loop_start
))
5182 /* Scan X for memory refs and check each memory address
5183 as a possible giv. INSN is the insn whose pattern X comes from.
5184 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5185 every loop iteration. */
5188 find_mem_givs (x
, insn
, not_every_iteration
, loop_start
, loop_end
)
5191 int not_every_iteration
;
5192 rtx loop_start
, loop_end
;
5195 register enum rtx_code code
;
5201 code
= GET_CODE (x
);
5225 /* This code used to disable creating GIVs with mult_val == 1 and
5226 add_val == 0. However, this leads to lost optimizations when
5227 it comes time to combine a set of related DEST_ADDR GIVs, since
5228 this one would not be seen. */
5230 if (general_induction_var (XEXP (x
, 0), &src_reg
, &add_val
,
5231 &mult_val
, 1, &benefit
))
5233 /* Found one; record it. */
5235 = (struct induction
*) oballoc (sizeof (struct induction
));
5237 record_giv (v
, insn
, src_reg
, addr_placeholder
, mult_val
,
5238 add_val
, benefit
, DEST_ADDR
, not_every_iteration
,
5239 &XEXP (x
, 0), loop_start
, loop_end
);
5241 v
->mem_mode
= GET_MODE (x
);
5250 /* Recursively scan the subexpressions for other mem refs. */
5252 fmt
= GET_RTX_FORMAT (code
);
5253 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
5255 find_mem_givs (XEXP (x
, i
), insn
, not_every_iteration
, loop_start
,
5257 else if (fmt
[i
] == 'E')
5258 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
5259 find_mem_givs (XVECEXP (x
, i
, j
), insn
, not_every_iteration
,
5260 loop_start
, loop_end
);
5263 /* Fill in the data about one biv update.
5264 V is the `struct induction' in which we record the biv. (It is
5265 allocated by the caller, with alloca.)
5266 INSN is the insn that sets it.
5267 DEST_REG is the biv's reg.
5269 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5270 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5271 being set to INC_VAL.
5273 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5274 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5275 can be executed more than once per iteration. If MAYBE_MULTIPLE
5276 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5277 executed exactly once per iteration. */
5280 record_biv (v
, insn
, dest_reg
, inc_val
, mult_val
, location
,
5281 not_every_iteration
, maybe_multiple
)
5282 struct induction
*v
;
5288 int not_every_iteration
;
5291 struct iv_class
*bl
;
5294 v
->src_reg
= dest_reg
;
5295 v
->dest_reg
= dest_reg
;
5296 v
->mult_val
= mult_val
;
5297 v
->add_val
= inc_val
;
5298 v
->location
= location
;
5299 v
->mode
= GET_MODE (dest_reg
);
5300 v
->always_computable
= ! not_every_iteration
;
5301 v
->always_executed
= ! not_every_iteration
;
5302 v
->maybe_multiple
= maybe_multiple
;
5304 /* Add this to the reg's iv_class, creating a class
5305 if this is the first incrementation of the reg. */
5307 bl
= reg_biv_class
[REGNO (dest_reg
)];
5310 /* Create and initialize new iv_class. */
5312 bl
= (struct iv_class
*) oballoc (sizeof (struct iv_class
));
5314 bl
->regno
= REGNO (dest_reg
);
5320 /* Set initial value to the reg itself. */
5321 bl
->initial_value
= dest_reg
;
5322 /* We haven't seen the initializing insn yet */
5325 bl
->initial_test
= 0;
5326 bl
->incremented
= 0;
5330 bl
->total_benefit
= 0;
5332 /* Add this class to loop_iv_list. */
5333 bl
->next
= loop_iv_list
;
5336 /* Put it in the array of biv register classes. */
5337 reg_biv_class
[REGNO (dest_reg
)] = bl
;
5340 /* Update IV_CLASS entry for this biv. */
5341 v
->next_iv
= bl
->biv
;
5344 if (mult_val
== const1_rtx
)
5345 bl
->incremented
= 1;
5347 if (loop_dump_stream
)
5349 fprintf (loop_dump_stream
,
5350 "Insn %d: possible biv, reg %d,",
5351 INSN_UID (insn
), REGNO (dest_reg
));
5352 if (GET_CODE (inc_val
) == CONST_INT
)
5354 fprintf (loop_dump_stream
, " const =");
5355 fprintf (loop_dump_stream
, HOST_WIDE_INT_PRINT_DEC
, INTVAL (inc_val
));
5356 fputc ('\n', loop_dump_stream
);
5360 fprintf (loop_dump_stream
, " const = ");
5361 print_rtl (loop_dump_stream
, inc_val
);
5362 fprintf (loop_dump_stream
, "\n");
5367 /* Fill in the data about one giv.
5368 V is the `struct induction' in which we record the giv. (It is
5369 allocated by the caller, with alloca.)
5370 INSN is the insn that sets it.
5371 BENEFIT estimates the savings from deleting this insn.
5372 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5373 into a register or is used as a memory address.
5375 SRC_REG is the biv reg which the giv is computed from.
5376 DEST_REG is the giv's reg (if the giv is stored in a reg).
5377 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5378 LOCATION points to the place where this giv's value appears in INSN. */
5381 record_giv (v
, insn
, src_reg
, dest_reg
, mult_val
, add_val
, benefit
,
5382 type
, not_every_iteration
, location
, loop_start
, loop_end
)
5383 struct induction
*v
;
5387 rtx mult_val
, add_val
;
5390 int not_every_iteration
;
5392 rtx loop_start
, loop_end
;
5394 struct induction
*b
;
5395 struct iv_class
*bl
;
5396 rtx set
= single_set (insn
);
5399 v
->src_reg
= src_reg
;
5401 v
->dest_reg
= dest_reg
;
5402 v
->mult_val
= mult_val
;
5403 v
->add_val
= add_val
;
5404 v
->benefit
= benefit
;
5405 v
->location
= location
;
5407 v
->combined_with
= 0;
5408 v
->maybe_multiple
= 0;
5410 v
->derive_adjustment
= 0;
5416 v
->auto_inc_opt
= 0;
5419 v
->derived_from
= 0;
5422 /* The v->always_computable field is used in update_giv_derive, to
5423 determine whether a giv can be used to derive another giv. For a
5424 DEST_REG giv, INSN computes a new value for the giv, so its value
5425 isn't computable if INSN insn't executed every iteration.
5426 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5427 it does not compute a new value. Hence the value is always computable
5428 regardless of whether INSN is executed each iteration. */
5430 if (type
== DEST_ADDR
)
5431 v
->always_computable
= 1;
5433 v
->always_computable
= ! not_every_iteration
;
5435 v
->always_executed
= ! not_every_iteration
;
5437 if (type
== DEST_ADDR
)
5439 v
->mode
= GET_MODE (*location
);
5442 else /* type == DEST_REG */
5444 v
->mode
= GET_MODE (SET_DEST (set
));
5446 v
->lifetime
= (uid_luid
[REGNO_LAST_UID (REGNO (dest_reg
))]
5447 - uid_luid
[REGNO_FIRST_UID (REGNO (dest_reg
))]);
5449 /* If the lifetime is zero, it means that this register is
5450 really a dead store. So mark this as a giv that can be
5451 ignored. This will not prevent the biv from being eliminated. */
5452 if (v
->lifetime
== 0)
5455 REG_IV_TYPE (REGNO (dest_reg
)) = GENERAL_INDUCT
;
5456 REG_IV_INFO (REGNO (dest_reg
)) = v
;
5459 /* Add the giv to the class of givs computed from one biv. */
5461 bl
= reg_biv_class
[REGNO (src_reg
)];
5464 v
->next_iv
= bl
->giv
;
5466 /* Don't count DEST_ADDR. This is supposed to count the number of
5467 insns that calculate givs. */
5468 if (type
== DEST_REG
)
5470 bl
->total_benefit
+= benefit
;
5473 /* Fatal error, biv missing for this giv? */
5476 if (type
== DEST_ADDR
)
5480 /* The giv can be replaced outright by the reduced register only if all
5481 of the following conditions are true:
5482 - the insn that sets the giv is always executed on any iteration
5483 on which the giv is used at all
5484 (there are two ways to deduce this:
5485 either the insn is executed on every iteration,
5486 or all uses follow that insn in the same basic block),
5487 - the giv is not used outside the loop
5488 - no assignments to the biv occur during the giv's lifetime. */
5490 if (REGNO_FIRST_UID (REGNO (dest_reg
)) == INSN_UID (insn
)
5491 /* Previous line always fails if INSN was moved by loop opt. */
5492 && uid_luid
[REGNO_LAST_UID (REGNO (dest_reg
))] < INSN_LUID (loop_end
)
5493 && (! not_every_iteration
5494 || last_use_this_basic_block (dest_reg
, insn
)))
5496 /* Now check that there are no assignments to the biv within the
5497 giv's lifetime. This requires two separate checks. */
5499 /* Check each biv update, and fail if any are between the first
5500 and last use of the giv.
5502 If this loop contains an inner loop that was unrolled, then
5503 the insn modifying the biv may have been emitted by the loop
5504 unrolling code, and hence does not have a valid luid. Just
5505 mark the biv as not replaceable in this case. It is not very
5506 useful as a biv, because it is used in two different loops.
5507 It is very unlikely that we would be able to optimize the giv
5508 using this biv anyways. */
5511 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
5513 if (INSN_UID (b
->insn
) >= max_uid_for_loop
5514 || ((uid_luid
[INSN_UID (b
->insn
)]
5515 >= uid_luid
[REGNO_FIRST_UID (REGNO (dest_reg
))])
5516 && (uid_luid
[INSN_UID (b
->insn
)]
5517 <= uid_luid
[REGNO_LAST_UID (REGNO (dest_reg
))])))
5520 v
->not_replaceable
= 1;
5525 /* If there are any backwards branches that go from after the
5526 biv update to before it, then this giv is not replaceable. */
5528 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
5529 if (back_branch_in_range_p (b
->insn
, loop_start
, loop_end
))
5532 v
->not_replaceable
= 1;
5538 /* May still be replaceable, we don't have enough info here to
5541 v
->not_replaceable
= 0;
5545 /* Record whether the add_val contains a const_int, for later use by
5550 v
->no_const_addval
= 1;
5551 if (tem
== const0_rtx
)
5553 else if (GET_CODE (tem
) == CONST_INT
)
5554 v
->no_const_addval
= 0;
5555 else if (GET_CODE (tem
) == PLUS
)
5559 if (GET_CODE (XEXP (tem
, 0)) == PLUS
)
5560 tem
= XEXP (tem
, 0);
5561 else if (GET_CODE (XEXP (tem
, 1)) == PLUS
)
5562 tem
= XEXP (tem
, 1);
5566 if (GET_CODE (XEXP (tem
, 1)) == CONST_INT
)
5567 v
->no_const_addval
= 0;
5571 if (loop_dump_stream
)
5573 if (type
== DEST_REG
)
5574 fprintf (loop_dump_stream
, "Insn %d: giv reg %d",
5575 INSN_UID (insn
), REGNO (dest_reg
));
5577 fprintf (loop_dump_stream
, "Insn %d: dest address",
5580 fprintf (loop_dump_stream
, " src reg %d benefit %d",
5581 REGNO (src_reg
), v
->benefit
);
5582 fprintf (loop_dump_stream
, " lifetime %d",
5586 fprintf (loop_dump_stream
, " replaceable");
5588 if (v
->no_const_addval
)
5589 fprintf (loop_dump_stream
, " ncav");
5591 if (GET_CODE (mult_val
) == CONST_INT
)
5593 fprintf (loop_dump_stream
, " mult ");
5594 fprintf (loop_dump_stream
, HOST_WIDE_INT_PRINT_DEC
, INTVAL (mult_val
));
5598 fprintf (loop_dump_stream
, " mult ");
5599 print_rtl (loop_dump_stream
, mult_val
);
5602 if (GET_CODE (add_val
) == CONST_INT
)
5604 fprintf (loop_dump_stream
, " add ");
5605 fprintf (loop_dump_stream
, HOST_WIDE_INT_PRINT_DEC
, INTVAL (add_val
));
5609 fprintf (loop_dump_stream
, " add ");
5610 print_rtl (loop_dump_stream
, add_val
);
5614 if (loop_dump_stream
)
5615 fprintf (loop_dump_stream
, "\n");
5620 /* All this does is determine whether a giv can be made replaceable because
5621 its final value can be calculated. This code can not be part of record_giv
5622 above, because final_giv_value requires that the number of loop iterations
5623 be known, and that can not be accurately calculated until after all givs
5624 have been identified. */
5627 check_final_value (v
, loop_start
, loop_end
, n_iterations
)
5628 struct induction
*v
;
5629 rtx loop_start
, loop_end
;
5630 unsigned HOST_WIDE_INT n_iterations
;
5632 struct iv_class
*bl
;
5633 rtx final_value
= 0;
5635 bl
= reg_biv_class
[REGNO (v
->src_reg
)];
5637 /* DEST_ADDR givs will never reach here, because they are always marked
5638 replaceable above in record_giv. */
5640 /* The giv can be replaced outright by the reduced register only if all
5641 of the following conditions are true:
5642 - the insn that sets the giv is always executed on any iteration
5643 on which the giv is used at all
5644 (there are two ways to deduce this:
5645 either the insn is executed on every iteration,
5646 or all uses follow that insn in the same basic block),
5647 - its final value can be calculated (this condition is different
5648 than the one above in record_giv)
5649 - no assignments to the biv occur during the giv's lifetime. */
5652 /* This is only called now when replaceable is known to be false. */
5653 /* Clear replaceable, so that it won't confuse final_giv_value. */
5657 if ((final_value
= final_giv_value (v
, loop_start
, loop_end
, n_iterations
))
5658 && (v
->always_computable
|| last_use_this_basic_block (v
->dest_reg
, v
->insn
)))
5660 int biv_increment_seen
= 0;
5666 /* When trying to determine whether or not a biv increment occurs
5667 during the lifetime of the giv, we can ignore uses of the variable
5668 outside the loop because final_value is true. Hence we can not
5669 use regno_last_uid and regno_first_uid as above in record_giv. */
5671 /* Search the loop to determine whether any assignments to the
5672 biv occur during the giv's lifetime. Start with the insn
5673 that sets the giv, and search around the loop until we come
5674 back to that insn again.
5676 Also fail if there is a jump within the giv's lifetime that jumps
5677 to somewhere outside the lifetime but still within the loop. This
5678 catches spaghetti code where the execution order is not linear, and
5679 hence the above test fails. Here we assume that the giv lifetime
5680 does not extend from one iteration of the loop to the next, so as
5681 to make the test easier. Since the lifetime isn't known yet,
5682 this requires two loops. See also record_giv above. */
5684 last_giv_use
= v
->insn
;
5690 p
= NEXT_INSN (loop_start
);
5694 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
5695 || GET_CODE (p
) == CALL_INSN
)
5697 if (biv_increment_seen
)
5699 if (reg_mentioned_p (v
->dest_reg
, PATTERN (p
)))
5702 v
->not_replaceable
= 1;
5706 else if (reg_set_p (v
->src_reg
, PATTERN (p
)))
5707 biv_increment_seen
= 1;
5708 else if (reg_mentioned_p (v
->dest_reg
, PATTERN (p
)))
5713 /* Now that the lifetime of the giv is known, check for branches
5714 from within the lifetime to outside the lifetime if it is still
5724 p
= NEXT_INSN (loop_start
);
5725 if (p
== last_giv_use
)
5728 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
5729 && LABEL_NAME (JUMP_LABEL (p
))
5730 && ((loop_insn_first_p (JUMP_LABEL (p
), v
->insn
)
5731 && loop_insn_first_p (loop_start
, JUMP_LABEL (p
)))
5732 || (loop_insn_first_p (last_giv_use
, JUMP_LABEL (p
))
5733 && loop_insn_first_p (JUMP_LABEL (p
), loop_end
))))
5736 v
->not_replaceable
= 1;
5738 if (loop_dump_stream
)
5739 fprintf (loop_dump_stream
,
5740 "Found branch outside giv lifetime.\n");
5747 /* If it is replaceable, then save the final value. */
5749 v
->final_value
= final_value
;
5752 if (loop_dump_stream
&& v
->replaceable
)
5753 fprintf (loop_dump_stream
, "Insn %d: giv reg %d final_value replaceable\n",
5754 INSN_UID (v
->insn
), REGNO (v
->dest_reg
));
5757 /* Update the status of whether a giv can derive other givs.
5759 We need to do something special if there is or may be an update to the biv
5760 between the time the giv is defined and the time it is used to derive
5763 In addition, a giv that is only conditionally set is not allowed to
5764 derive another giv once a label has been passed.
5766 The cases we look at are when a label or an update to a biv is passed. */
5769 update_giv_derive (p
)
5772 struct iv_class
*bl
;
5773 struct induction
*biv
, *giv
;
5777 /* Search all IV classes, then all bivs, and finally all givs.
5779 There are three cases we are concerned with. First we have the situation
5780 of a giv that is only updated conditionally. In that case, it may not
5781 derive any givs after a label is passed.
5783 The second case is when a biv update occurs, or may occur, after the
5784 definition of a giv. For certain biv updates (see below) that are
5785 known to occur between the giv definition and use, we can adjust the
5786 giv definition. For others, or when the biv update is conditional,
5787 we must prevent the giv from deriving any other givs. There are two
5788 sub-cases within this case.
5790 If this is a label, we are concerned with any biv update that is done
5791 conditionally, since it may be done after the giv is defined followed by
5792 a branch here (actually, we need to pass both a jump and a label, but
5793 this extra tracking doesn't seem worth it).
5795 If this is a jump, we are concerned about any biv update that may be
5796 executed multiple times. We are actually only concerned about
5797 backward jumps, but it is probably not worth performing the test
5798 on the jump again here.
5800 If this is a biv update, we must adjust the giv status to show that a
5801 subsequent biv update was performed. If this adjustment cannot be done,
5802 the giv cannot derive further givs. */
5804 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
5805 for (biv
= bl
->biv
; biv
; biv
= biv
->next_iv
)
5806 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
5809 for (giv
= bl
->giv
; giv
; giv
= giv
->next_iv
)
5811 /* If cant_derive is already true, there is no point in
5812 checking all of these conditions again. */
5813 if (giv
->cant_derive
)
5816 /* If this giv is conditionally set and we have passed a label,
5817 it cannot derive anything. */
5818 if (GET_CODE (p
) == CODE_LABEL
&& ! giv
->always_computable
)
5819 giv
->cant_derive
= 1;
5821 /* Skip givs that have mult_val == 0, since
5822 they are really invariants. Also skip those that are
5823 replaceable, since we know their lifetime doesn't contain
5825 else if (giv
->mult_val
== const0_rtx
|| giv
->replaceable
)
5828 /* The only way we can allow this giv to derive another
5829 is if this is a biv increment and we can form the product
5830 of biv->add_val and giv->mult_val. In this case, we will
5831 be able to compute a compensation. */
5832 else if (biv
->insn
== p
)
5836 if (biv
->mult_val
== const1_rtx
)
5837 tem
= simplify_giv_expr (gen_rtx_MULT (giv
->mode
,
5842 if (tem
&& giv
->derive_adjustment
)
5843 tem
= simplify_giv_expr (gen_rtx_PLUS (giv
->mode
, tem
,
5844 giv
->derive_adjustment
),
5847 giv
->derive_adjustment
= tem
;
5849 giv
->cant_derive
= 1;
5851 else if ((GET_CODE (p
) == CODE_LABEL
&& ! biv
->always_computable
)
5852 || (GET_CODE (p
) == JUMP_INSN
&& biv
->maybe_multiple
))
5853 giv
->cant_derive
= 1;
5858 /* Check whether an insn is an increment legitimate for a basic induction var.
5859 X is the source of insn P, or a part of it.
5860 MODE is the mode in which X should be interpreted.
5862 DEST_REG is the putative biv, also the destination of the insn.
5863 We accept patterns of these forms:
5864 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5865 REG = INVARIANT + REG
5867 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5868 store the additive term into *INC_VAL, and store the place where
5869 we found the additive term into *LOCATION.
5871 If X is an assignment of an invariant into DEST_REG, we set
5872 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5874 We also want to detect a BIV when it corresponds to a variable
5875 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5876 of the variable may be a PLUS that adds a SUBREG of that variable to
5877 an invariant and then sign- or zero-extends the result of the PLUS
5880 Most GIVs in such cases will be in the promoted mode, since that is the
5881 probably the natural computation mode (and almost certainly the mode
5882 used for addresses) on the machine. So we view the pseudo-reg containing
5883 the variable as the BIV, as if it were simply incremented.
5885 Note that treating the entire pseudo as a BIV will result in making
5886 simple increments to any GIVs based on it. However, if the variable
5887 overflows in its declared mode but not its promoted mode, the result will
5888 be incorrect. This is acceptable if the variable is signed, since
5889 overflows in such cases are undefined, but not if it is unsigned, since
5890 those overflows are defined. So we only check for SIGN_EXTEND and
5893 If we cannot find a biv, we return 0. */
5896 basic_induction_var (x
, mode
, dest_reg
, p
, inc_val
, mult_val
, location
)
5898 enum machine_mode mode
;
5905 register enum rtx_code code
;
5909 code
= GET_CODE (x
);
5913 if (rtx_equal_p (XEXP (x
, 0), dest_reg
)
5914 || (GET_CODE (XEXP (x
, 0)) == SUBREG
5915 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 0))
5916 && SUBREG_REG (XEXP (x
, 0)) == dest_reg
))
5918 argp
= &XEXP (x
, 1);
5920 else if (rtx_equal_p (XEXP (x
, 1), dest_reg
)
5921 || (GET_CODE (XEXP (x
, 1)) == SUBREG
5922 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 1))
5923 && SUBREG_REG (XEXP (x
, 1)) == dest_reg
))
5925 argp
= &XEXP (x
, 0);
5931 if (invariant_p (arg
) != 1)
5934 *inc_val
= convert_modes (GET_MODE (dest_reg
), GET_MODE (x
), arg
, 0);
5935 *mult_val
= const1_rtx
;
5940 /* If this is a SUBREG for a promoted variable, check the inner
5942 if (SUBREG_PROMOTED_VAR_P (x
))
5943 return basic_induction_var (SUBREG_REG (x
), GET_MODE (SUBREG_REG (x
)),
5944 dest_reg
, p
, inc_val
, mult_val
, location
);
5948 /* If this register is assigned in a previous insn, look at its
5949 source, but don't go outside the loop or past a label. */
5955 insn
= PREV_INSN (insn
);
5956 } while (insn
&& GET_CODE (insn
) == NOTE
5957 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
5961 set
= single_set (insn
);
5965 if ((SET_DEST (set
) == x
5966 || (GET_CODE (SET_DEST (set
)) == SUBREG
5967 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set
)))
5969 && SUBREG_REG (SET_DEST (set
)) == x
))
5970 && basic_induction_var (SET_SRC (set
),
5971 (GET_MODE (SET_SRC (set
)) == VOIDmode
5973 : GET_MODE (SET_SRC (set
))),
5975 inc_val
, mult_val
, location
))
5978 /* ... fall through ... */
5980 /* Can accept constant setting of biv only when inside inner most loop.
5981 Otherwise, a biv of an inner loop may be incorrectly recognized
5982 as a biv of the outer loop,
5983 causing code to be moved INTO the inner loop. */
5985 if (invariant_p (x
) != 1)
5990 /* convert_modes aborts if we try to convert to or from CCmode, so just
5991 exclude that case. It is very unlikely that a condition code value
5992 would be a useful iterator anyways. */
5993 if (loops_enclosed
== 1
5994 && GET_MODE_CLASS (mode
) != MODE_CC
5995 && GET_MODE_CLASS (GET_MODE (dest_reg
)) != MODE_CC
)
5997 /* Possible bug here? Perhaps we don't know the mode of X. */
5998 *inc_val
= convert_modes (GET_MODE (dest_reg
), mode
, x
, 0);
5999 *mult_val
= const0_rtx
;
6006 return basic_induction_var (XEXP (x
, 0), GET_MODE (XEXP (x
, 0)),
6007 dest_reg
, p
, inc_val
, mult_val
, location
);
6010 /* Similar, since this can be a sign extension. */
6011 for (insn
= PREV_INSN (p
);
6012 (insn
&& GET_CODE (insn
) == NOTE
6013 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
6014 insn
= PREV_INSN (insn
))
6018 set
= single_set (insn
);
6020 if (set
&& SET_DEST (set
) == XEXP (x
, 0)
6021 && GET_CODE (XEXP (x
, 1)) == CONST_INT
6022 && INTVAL (XEXP (x
, 1)) >= 0
6023 && GET_CODE (SET_SRC (set
)) == ASHIFT
6024 && XEXP (x
, 1) == XEXP (SET_SRC (set
), 1))
6025 return basic_induction_var (XEXP (SET_SRC (set
), 0),
6026 GET_MODE (XEXP (x
, 0)),
6027 dest_reg
, insn
, inc_val
, mult_val
,
6036 /* A general induction variable (giv) is any quantity that is a linear
6037 function of a basic induction variable,
6038 i.e. giv = biv * mult_val + add_val.
6039 The coefficients can be any loop invariant quantity.
6040 A giv need not be computed directly from the biv;
6041 it can be computed by way of other givs. */
6043 /* Determine whether X computes a giv.
6044 If it does, return a nonzero value
6045 which is the benefit from eliminating the computation of X;
6046 set *SRC_REG to the register of the biv that it is computed from;
6047 set *ADD_VAL and *MULT_VAL to the coefficients,
6048 such that the value of X is biv * mult + add; */
6051 general_induction_var (x
, src_reg
, add_val
, mult_val
, is_addr
, pbenefit
)
6062 /* If this is an invariant, forget it, it isn't a giv. */
6063 if (invariant_p (x
) == 1)
6066 /* See if the expression could be a giv and get its form.
6067 Mark our place on the obstack in case we don't find a giv. */
6068 storage
= (char *) oballoc (0);
6070 x
= simplify_giv_expr (x
, pbenefit
);
6077 switch (GET_CODE (x
))
6081 /* Since this is now an invariant and wasn't before, it must be a giv
6082 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6084 *src_reg
= loop_iv_list
->biv
->dest_reg
;
6085 *mult_val
= const0_rtx
;
6090 /* This is equivalent to a BIV. */
6092 *mult_val
= const1_rtx
;
6093 *add_val
= const0_rtx
;
6097 /* Either (plus (biv) (invar)) or
6098 (plus (mult (biv) (invar_1)) (invar_2)). */
6099 if (GET_CODE (XEXP (x
, 0)) == MULT
)
6101 *src_reg
= XEXP (XEXP (x
, 0), 0);
6102 *mult_val
= XEXP (XEXP (x
, 0), 1);
6106 *src_reg
= XEXP (x
, 0);
6107 *mult_val
= const1_rtx
;
6109 *add_val
= XEXP (x
, 1);
6113 /* ADD_VAL is zero. */
6114 *src_reg
= XEXP (x
, 0);
6115 *mult_val
= XEXP (x
, 1);
6116 *add_val
= const0_rtx
;
6123 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6124 unless they are CONST_INT). */
6125 if (GET_CODE (*add_val
) == USE
)
6126 *add_val
= XEXP (*add_val
, 0);
6127 if (GET_CODE (*mult_val
) == USE
)
6128 *mult_val
= XEXP (*mult_val
, 0);
6133 *pbenefit
+= ADDRESS_COST (orig_x
) - reg_address_cost
;
6135 *pbenefit
+= rtx_cost (orig_x
, MEM
) - reg_address_cost
;
6139 *pbenefit
+= rtx_cost (orig_x
, SET
);
6141 /* Always return true if this is a giv so it will be detected as such,
6142 even if the benefit is zero or negative. This allows elimination
6143 of bivs that might otherwise not be eliminated. */
6147 /* Given an expression, X, try to form it as a linear function of a biv.
6148 We will canonicalize it to be of the form
6149 (plus (mult (BIV) (invar_1))
6151 with possible degeneracies.
6153 The invariant expressions must each be of a form that can be used as a
6154 machine operand. We surround then with a USE rtx (a hack, but localized
6155 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6156 routine; it is the caller's responsibility to strip them.
6158 If no such canonicalization is possible (i.e., two biv's are used or an
6159 expression that is neither invariant nor a biv or giv), this routine
6162 For a non-zero return, the result will have a code of CONST_INT, USE,
6163 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6165 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6167 static rtx sge_plus
PROTO ((enum machine_mode
, rtx
, rtx
));
6168 static rtx sge_plus_constant
PROTO ((rtx
, rtx
));
6171 simplify_giv_expr (x
, benefit
)
6175 enum machine_mode mode
= GET_MODE (x
);
6179 /* If this is not an integer mode, or if we cannot do arithmetic in this
6180 mode, this can't be a giv. */
6181 if (mode
!= VOIDmode
6182 && (GET_MODE_CLASS (mode
) != MODE_INT
6183 || GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
))
6186 switch (GET_CODE (x
))
6189 arg0
= simplify_giv_expr (XEXP (x
, 0), benefit
);
6190 arg1
= simplify_giv_expr (XEXP (x
, 1), benefit
);
6191 if (arg0
== 0 || arg1
== 0)
6194 /* Put constant last, CONST_INT last if both constant. */
6195 if ((GET_CODE (arg0
) == USE
6196 || GET_CODE (arg0
) == CONST_INT
)
6197 && ! ((GET_CODE (arg0
) == USE
6198 && GET_CODE (arg1
) == USE
)
6199 || GET_CODE (arg1
) == CONST_INT
))
6200 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6202 /* Handle addition of zero, then addition of an invariant. */
6203 if (arg1
== const0_rtx
)
6205 else if (GET_CODE (arg1
) == CONST_INT
|| GET_CODE (arg1
) == USE
)
6206 switch (GET_CODE (arg0
))
6210 /* Adding two invariants must result in an invariant, so enclose
6211 addition operation inside a USE and return it. */
6212 if (GET_CODE (arg0
) == USE
)
6213 arg0
= XEXP (arg0
, 0);
6214 if (GET_CODE (arg1
) == USE
)
6215 arg1
= XEXP (arg1
, 0);
6217 if (GET_CODE (arg0
) == CONST_INT
)
6218 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6219 if (GET_CODE (arg1
) == CONST_INT
)
6220 tem
= sge_plus_constant (arg0
, arg1
);
6222 tem
= sge_plus (mode
, arg0
, arg1
);
6224 if (GET_CODE (tem
) != CONST_INT
)
6225 tem
= gen_rtx_USE (mode
, tem
);
6230 /* biv + invar or mult + invar. Return sum. */
6231 return gen_rtx_PLUS (mode
, arg0
, arg1
);
6234 /* (a + invar_1) + invar_2. Associate. */
6235 return simplify_giv_expr (
6236 gen_rtx_PLUS (mode
, XEXP (arg0
, 0),
6237 gen_rtx_PLUS (mode
, XEXP (arg0
, 1), arg1
)),
6244 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6245 MULT to reduce cases. */
6246 if (GET_CODE (arg0
) == REG
)
6247 arg0
= gen_rtx_MULT (mode
, arg0
, const1_rtx
);
6248 if (GET_CODE (arg1
) == REG
)
6249 arg1
= gen_rtx_MULT (mode
, arg1
, const1_rtx
);
6251 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6252 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6253 Recurse to associate the second PLUS. */
6254 if (GET_CODE (arg1
) == MULT
)
6255 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6257 if (GET_CODE (arg1
) == PLUS
)
6258 return simplify_giv_expr (gen_rtx_PLUS (mode
,
6259 gen_rtx_PLUS (mode
, arg0
,
6264 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6265 if (GET_CODE (arg0
) != MULT
|| GET_CODE (arg1
) != MULT
)
6268 if (!rtx_equal_p (arg0
, arg1
))
6271 return simplify_giv_expr (gen_rtx_MULT (mode
,
6279 /* Handle "a - b" as "a + b * (-1)". */
6280 return simplify_giv_expr (gen_rtx_PLUS (mode
,
6282 gen_rtx_MULT (mode
, XEXP (x
, 1),
6287 arg0
= simplify_giv_expr (XEXP (x
, 0), benefit
);
6288 arg1
= simplify_giv_expr (XEXP (x
, 1), benefit
);
6289 if (arg0
== 0 || arg1
== 0)
6292 /* Put constant last, CONST_INT last if both constant. */
6293 if ((GET_CODE (arg0
) == USE
|| GET_CODE (arg0
) == CONST_INT
)
6294 && GET_CODE (arg1
) != CONST_INT
)
6295 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6297 /* If second argument is not now constant, not giv. */
6298 if (GET_CODE (arg1
) != USE
&& GET_CODE (arg1
) != CONST_INT
)
6301 /* Handle multiply by 0 or 1. */
6302 if (arg1
== const0_rtx
)
6305 else if (arg1
== const1_rtx
)
6308 switch (GET_CODE (arg0
))
6311 /* biv * invar. Done. */
6312 return gen_rtx_MULT (mode
, arg0
, arg1
);
6315 /* Product of two constants. */
6316 return GEN_INT (INTVAL (arg0
) * INTVAL (arg1
));
6319 /* invar * invar. It is a giv, but very few of these will
6320 actually pay off, so limit to simple registers. */
6321 if (GET_CODE (arg1
) != CONST_INT
)
6324 arg0
= XEXP (arg0
, 0);
6325 if (GET_CODE (arg0
) == REG
)
6326 tem
= gen_rtx_MULT (mode
, arg0
, arg1
);
6327 else if (GET_CODE (arg0
) == MULT
6328 && GET_CODE (XEXP (arg0
, 0)) == REG
6329 && GET_CODE (XEXP (arg0
, 1)) == CONST_INT
)
6331 tem
= gen_rtx_MULT (mode
, XEXP (arg0
, 0),
6332 GEN_INT (INTVAL (XEXP (arg0
, 1))
6337 return gen_rtx_USE (mode
, tem
);
6340 /* (a * invar_1) * invar_2. Associate. */
6341 return simplify_giv_expr (gen_rtx_MULT (mode
, XEXP (arg0
, 0),
6348 /* (a + invar_1) * invar_2. Distribute. */
6349 return simplify_giv_expr (gen_rtx_PLUS (mode
,
6363 /* Shift by constant is multiply by power of two. */
6364 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
6367 return simplify_giv_expr (gen_rtx_MULT (mode
,
6369 GEN_INT ((HOST_WIDE_INT
) 1
6370 << INTVAL (XEXP (x
, 1)))),
6374 /* "-a" is "a * (-1)" */
6375 return simplify_giv_expr (gen_rtx_MULT (mode
, XEXP (x
, 0), constm1_rtx
),
6379 /* "~a" is "-a - 1". Silly, but easy. */
6380 return simplify_giv_expr (gen_rtx_MINUS (mode
,
6381 gen_rtx_NEG (mode
, XEXP (x
, 0)),
6386 /* Already in proper form for invariant. */
6390 /* If this is a new register, we can't deal with it. */
6391 if (REGNO (x
) >= max_reg_before_loop
)
6394 /* Check for biv or giv. */
6395 switch (REG_IV_TYPE (REGNO (x
)))
6399 case GENERAL_INDUCT
:
6401 struct induction
*v
= REG_IV_INFO (REGNO (x
));
6403 /* Form expression from giv and add benefit. Ensure this giv
6404 can derive another and subtract any needed adjustment if so. */
6405 *benefit
+= v
->benefit
;
6409 tem
= gen_rtx_PLUS (mode
, gen_rtx_MULT (mode
, v
->src_reg
,
6412 if (v
->derive_adjustment
)
6413 tem
= gen_rtx_MINUS (mode
, tem
, v
->derive_adjustment
);
6414 return simplify_giv_expr (tem
, benefit
);
6418 /* If it isn't an induction variable, and it is invariant, we
6419 may be able to simplify things further by looking through
6420 the bits we just moved outside the loop. */
6421 if (invariant_p (x
) == 1)
6425 for (m
= the_movables
; m
; m
= m
->next
)
6426 if (rtx_equal_p (x
, m
->set_dest
))
6428 /* Ok, we found a match. Substitute and simplify. */
6430 /* If we match another movable, we must use that, as
6431 this one is going away. */
6433 return simplify_giv_expr (m
->match
->set_dest
, benefit
);
6435 /* If consec is non-zero, this is a member of a group of
6436 instructions that were moved together. We handle this
6437 case only to the point of seeking to the last insn and
6438 looking for a REG_EQUAL. Fail if we don't find one. */
6443 do { tem
= NEXT_INSN (tem
); } while (--i
> 0);
6445 tem
= find_reg_note (tem
, REG_EQUAL
, NULL_RTX
);
6447 tem
= XEXP (tem
, 0);
6451 tem
= single_set (m
->insn
);
6453 tem
= SET_SRC (tem
);
6458 /* What we are most interested in is pointer
6459 arithmetic on invariants -- only take
6460 patterns we may be able to do something with. */
6461 if (GET_CODE (tem
) == PLUS
6462 || GET_CODE (tem
) == MULT
6463 || GET_CODE (tem
) == ASHIFT
6464 || GET_CODE (tem
) == CONST_INT
6465 || GET_CODE (tem
) == SYMBOL_REF
)
6467 tem
= simplify_giv_expr (tem
, benefit
);
6471 else if (GET_CODE (tem
) == CONST
6472 && GET_CODE (XEXP (tem
, 0)) == PLUS
6473 && GET_CODE (XEXP (XEXP (tem
, 0), 0)) == SYMBOL_REF
6474 && GET_CODE (XEXP (XEXP (tem
, 0), 1)) == CONST_INT
)
6476 tem
= simplify_giv_expr (XEXP (tem
, 0), benefit
);
6487 /* Fall through to general case. */
6489 /* If invariant, return as USE (unless CONST_INT).
6490 Otherwise, not giv. */
6491 if (GET_CODE (x
) == USE
)
6494 if (invariant_p (x
) == 1)
6496 if (GET_CODE (x
) == CONST_INT
)
6498 if (GET_CODE (x
) == CONST
6499 && GET_CODE (XEXP (x
, 0)) == PLUS
6500 && GET_CODE (XEXP (XEXP (x
, 0), 0)) == SYMBOL_REF
6501 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
)
6503 return gen_rtx_USE (mode
, x
);
6510 /* This routine folds invariants such that there is only ever one
6511 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6514 sge_plus_constant (x
, c
)
6517 if (GET_CODE (x
) == CONST_INT
)
6518 return GEN_INT (INTVAL (x
) + INTVAL (c
));
6519 else if (GET_CODE (x
) != PLUS
)
6520 return gen_rtx_PLUS (GET_MODE (x
), x
, c
);
6521 else if (GET_CODE (XEXP (x
, 1)) == CONST_INT
)
6523 return gen_rtx_PLUS (GET_MODE (x
), XEXP (x
, 0),
6524 GEN_INT (INTVAL (XEXP (x
, 1)) + INTVAL (c
)));
6526 else if (GET_CODE (XEXP (x
, 0)) == PLUS
6527 || GET_CODE (XEXP (x
, 1)) != PLUS
)
6529 return gen_rtx_PLUS (GET_MODE (x
),
6530 sge_plus_constant (XEXP (x
, 0), c
), XEXP (x
, 1));
6534 return gen_rtx_PLUS (GET_MODE (x
),
6535 sge_plus_constant (XEXP (x
, 1), c
), XEXP (x
, 0));
6540 sge_plus (mode
, x
, y
)
6541 enum machine_mode mode
;
6544 while (GET_CODE (y
) == PLUS
)
6546 rtx a
= XEXP (y
, 0);
6547 if (GET_CODE (a
) == CONST_INT
)
6548 x
= sge_plus_constant (x
, a
);
6550 x
= gen_rtx_PLUS (mode
, x
, a
);
6553 if (GET_CODE (y
) == CONST_INT
)
6554 x
= sge_plus_constant (x
, y
);
6556 x
= gen_rtx_PLUS (mode
, x
, y
);
6560 /* Help detect a giv that is calculated by several consecutive insns;
6564 The caller has already identified the first insn P as having a giv as dest;
6565 we check that all other insns that set the same register follow
6566 immediately after P, that they alter nothing else,
6567 and that the result of the last is still a giv.
6569 The value is 0 if the reg set in P is not really a giv.
6570 Otherwise, the value is the amount gained by eliminating
6571 all the consecutive insns that compute the value.
6573 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6574 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6576 The coefficients of the ultimate giv value are stored in
6577 *MULT_VAL and *ADD_VAL. */
6580 consec_sets_giv (first_benefit
, p
, src_reg
, dest_reg
,
6581 add_val
, mult_val
, last_consec_insn
)
6588 rtx
*last_consec_insn
;
6596 /* Indicate that this is a giv so that we can update the value produced in
6597 each insn of the multi-insn sequence.
6599 This induction structure will be used only by the call to
6600 general_induction_var below, so we can allocate it on our stack.
6601 If this is a giv, our caller will replace the induct var entry with
6602 a new induction structure. */
6604 = (struct induction
*) alloca (sizeof (struct induction
));
6605 v
->src_reg
= src_reg
;
6606 v
->mult_val
= *mult_val
;
6607 v
->add_val
= *add_val
;
6608 v
->benefit
= first_benefit
;
6610 v
->derive_adjustment
= 0;
6612 REG_IV_TYPE (REGNO (dest_reg
)) = GENERAL_INDUCT
;
6613 REG_IV_INFO (REGNO (dest_reg
)) = v
;
6615 count
= VARRAY_INT (n_times_set
, REGNO (dest_reg
)) - 1;
6620 code
= GET_CODE (p
);
6622 /* If libcall, skip to end of call sequence. */
6623 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
6627 && (set
= single_set (p
))
6628 && GET_CODE (SET_DEST (set
)) == REG
6629 && SET_DEST (set
) == dest_reg
6630 && (general_induction_var (SET_SRC (set
), &src_reg
,
6631 add_val
, mult_val
, 0, &benefit
)
6632 /* Giv created by equivalent expression. */
6633 || ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
6634 && general_induction_var (XEXP (temp
, 0), &src_reg
,
6635 add_val
, mult_val
, 0, &benefit
)))
6636 && src_reg
== v
->src_reg
)
6638 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
6639 benefit
+= libcall_benefit (p
);
6642 v
->mult_val
= *mult_val
;
6643 v
->add_val
= *add_val
;
6644 v
->benefit
= benefit
;
6646 else if (code
!= NOTE
)
6648 /* Allow insns that set something other than this giv to a
6649 constant. Such insns are needed on machines which cannot
6650 include long constants and should not disqualify a giv. */
6652 && (set
= single_set (p
))
6653 && SET_DEST (set
) != dest_reg
6654 && CONSTANT_P (SET_SRC (set
)))
6657 REG_IV_TYPE (REGNO (dest_reg
)) = UNKNOWN_INDUCT
;
6662 *last_consec_insn
= p
;
6666 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6667 represented by G1. If no such expression can be found, or it is clear that
6668 it cannot possibly be a valid address, 0 is returned.
6670 To perform the computation, we note that
6673 where `v' is the biv.
6675 So G2 = (y/b) * G1 + (b - a*y/x).
6677 Note that MULT = y/x.
6679 Update: A and B are now allowed to be additive expressions such that
6680 B contains all variables in A. That is, computing B-A will not require
6681 subtracting variables. */
6684 express_from_1 (a
, b
, mult
)
6687 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
6689 if (mult
== const0_rtx
)
6692 /* If MULT is not 1, we cannot handle A with non-constants, since we
6693 would then be required to subtract multiples of the registers in A.
6694 This is theoretically possible, and may even apply to some Fortran
6695 constructs, but it is a lot of work and we do not attempt it here. */
6697 if (mult
!= const1_rtx
&& GET_CODE (a
) != CONST_INT
)
6700 /* In general these structures are sorted top to bottom (down the PLUS
6701 chain), but not left to right across the PLUS. If B is a higher
6702 order giv than A, we can strip one level and recurse. If A is higher
6703 order, we'll eventually bail out, but won't know that until the end.
6704 If they are the same, we'll strip one level around this loop. */
6706 while (GET_CODE (a
) == PLUS
&& GET_CODE (b
) == PLUS
)
6708 rtx ra
, rb
, oa
, ob
, tmp
;
6710 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
6711 if (GET_CODE (ra
) == PLUS
)
6712 tmp
= ra
, ra
= oa
, oa
= tmp
;
6714 rb
= XEXP (b
, 0), ob
= XEXP (b
, 1);
6715 if (GET_CODE (rb
) == PLUS
)
6716 tmp
= rb
, rb
= ob
, ob
= tmp
;
6718 if (rtx_equal_p (ra
, rb
))
6719 /* We matched: remove one reg completely. */
6721 else if (GET_CODE (ob
) != PLUS
&& rtx_equal_p (ra
, ob
))
6722 /* An alternate match. */
6724 else if (GET_CODE (oa
) != PLUS
&& rtx_equal_p (oa
, rb
))
6725 /* An alternate match. */
6729 /* Indicates an extra register in B. Strip one level from B and
6730 recurse, hoping B was the higher order expression. */
6731 ob
= express_from_1 (a
, ob
, mult
);
6734 return gen_rtx_PLUS (GET_MODE (b
), rb
, ob
);
6738 /* Here we are at the last level of A, go through the cases hoping to
6739 get rid of everything but a constant. */
6741 if (GET_CODE (a
) == PLUS
)
6745 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
6746 if (rtx_equal_p (oa
, b
))
6748 else if (!rtx_equal_p (ra
, b
))
6751 if (GET_CODE (oa
) != CONST_INT
)
6754 return GEN_INT (-INTVAL (oa
) * INTVAL (mult
));
6756 else if (GET_CODE (a
) == CONST_INT
)
6758 return plus_constant (b
, -INTVAL (a
) * INTVAL (mult
));
6760 else if (GET_CODE (b
) == PLUS
)
6762 if (rtx_equal_p (a
, XEXP (b
, 0)))
6764 else if (rtx_equal_p (a
, XEXP (b
, 1)))
6769 else if (rtx_equal_p (a
, b
))
6776 express_from (g1
, g2
)
6777 struct induction
*g1
, *g2
;
6781 /* The value that G1 will be multiplied by must be a constant integer. Also,
6782 the only chance we have of getting a valid address is if b*c/a (see above
6783 for notation) is also an integer. */
6784 if (GET_CODE (g1
->mult_val
) == CONST_INT
6785 && GET_CODE (g2
->mult_val
) == CONST_INT
)
6787 if (g1
->mult_val
== const0_rtx
6788 || INTVAL (g2
->mult_val
) % INTVAL (g1
->mult_val
) != 0)
6790 mult
= GEN_INT (INTVAL (g2
->mult_val
) / INTVAL (g1
->mult_val
));
6792 else if (rtx_equal_p (g1
->mult_val
, g2
->mult_val
))
6796 /* ??? Find out if the one is a multiple of the other? */
6800 add
= express_from_1 (g1
->add_val
, g2
->add_val
, mult
);
6801 if (add
== NULL_RTX
)
6804 /* Form simplified final result. */
6805 if (mult
== const0_rtx
)
6807 else if (mult
== const1_rtx
)
6808 mult
= g1
->dest_reg
;
6810 mult
= gen_rtx_MULT (g2
->mode
, g1
->dest_reg
, mult
);
6812 if (add
== const0_rtx
)
6816 if (GET_CODE (add
) == PLUS
6817 && CONSTANT_P (XEXP (add
, 1)))
6819 rtx tem
= XEXP (add
, 1);
6820 mult
= gen_rtx_PLUS (g2
->mode
, mult
, XEXP (add
, 0));
6824 return gen_rtx_PLUS (g2
->mode
, mult
, add
);
6829 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6830 represented by G1. This indicates that G2 should be combined with G1 and
6831 that G2 can use (either directly or via an address expression) a register
6832 used to represent G1. */
6835 combine_givs_p (g1
, g2
)
6836 struct induction
*g1
, *g2
;
6838 rtx tem
= express_from (g1
, g2
);
6840 /* If these givs are identical, they can be combined. We use the results
6841 of express_from because the addends are not in a canonical form, so
6842 rtx_equal_p is a weaker test. */
6843 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
6844 combination to be the other way round. */
6845 if (tem
== g1
->dest_reg
6846 && (g1
->giv_type
== DEST_REG
|| g2
->giv_type
== DEST_ADDR
))
6848 return g1
->dest_reg
;
6851 /* If G2 can be expressed as a function of G1 and that function is valid
6852 as an address and no more expensive than using a register for G2,
6853 the expression of G2 in terms of G1 can be used. */
6855 && g2
->giv_type
== DEST_ADDR
6856 && memory_address_p (g2
->mem_mode
, tem
)
6857 /* ??? Looses, especially with -fforce-addr, where *g2->location
6858 will always be a register, and so anything more complicated
6862 && ADDRESS_COST (tem
) <= ADDRESS_COST (*g2
->location
)
6864 && rtx_cost (tem
, MEM
) <= rtx_cost (*g2
->location
, MEM
)
6875 struct combine_givs_stats
6882 cmp_combine_givs_stats (x
, y
)
6883 struct combine_givs_stats
*x
, *y
;
6886 d
= y
->total_benefit
- x
->total_benefit
;
6887 /* Stabilize the sort. */
6889 d
= x
->giv_number
- y
->giv_number
;
6893 /* Check all pairs of givs for iv_class BL and see if any can be combined with
6894 any other. If so, point SAME to the giv combined with and set NEW_REG to
6895 be an expression (in terms of the other giv's DEST_REG) equivalent to the
6896 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
6900 struct iv_class
*bl
;
6902 /* Additional benefit to add for being combined multiple times. */
6903 const int extra_benefit
= 3;
6905 struct induction
*g1
, *g2
, **giv_array
;
6906 int i
, j
, k
, giv_count
;
6907 struct combine_givs_stats
*stats
;
6910 /* Count givs, because bl->giv_count is incorrect here. */
6912 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
6917 = (struct induction
**) alloca (giv_count
* sizeof (struct induction
*));
6919 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
6921 giv_array
[i
++] = g1
;
6923 stats
= (struct combine_givs_stats
*) alloca (giv_count
* sizeof (*stats
));
6924 bzero ((char *) stats
, giv_count
* sizeof (*stats
));
6926 can_combine
= (rtx
*) alloca (giv_count
* giv_count
* sizeof(rtx
));
6927 bzero ((char *) can_combine
, giv_count
* giv_count
* sizeof(rtx
));
6929 for (i
= 0; i
< giv_count
; i
++)
6935 stats
[i
].giv_number
= i
;
6937 /* If a DEST_REG GIV is used only once, do not allow it to combine
6938 with anything, for in doing so we will gain nothing that cannot
6939 be had by simply letting the GIV with which we would have combined
6940 to be reduced on its own. The losage shows up in particular with
6941 DEST_ADDR targets on hosts with reg+reg addressing, though it can
6942 be seen elsewhere as well. */
6943 if (g1
->giv_type
== DEST_REG
6944 && (single_use
= VARRAY_RTX (reg_single_usage
, REGNO (g1
->dest_reg
)))
6945 && single_use
!= const0_rtx
)
6948 this_benefit
= g1
->benefit
;
6949 /* Add an additional weight for zero addends. */
6950 if (g1
->no_const_addval
)
6953 for (j
= 0; j
< giv_count
; j
++)
6959 && (this_combine
= combine_givs_p (g1
, g2
)) != NULL_RTX
)
6961 can_combine
[i
*giv_count
+ j
] = this_combine
;
6962 this_benefit
+= g2
->benefit
+ extra_benefit
;
6965 stats
[i
].total_benefit
= this_benefit
;
6968 /* Iterate, combining until we can't. */
6970 qsort (stats
, giv_count
, sizeof(*stats
), cmp_combine_givs_stats
);
6972 if (loop_dump_stream
)
6974 fprintf (loop_dump_stream
, "Sorted combine statistics:\n");
6975 for (k
= 0; k
< giv_count
; k
++)
6977 g1
= giv_array
[stats
[k
].giv_number
];
6978 if (!g1
->combined_with
&& !g1
->same
)
6979 fprintf (loop_dump_stream
, " {%d, %d}",
6980 INSN_UID (giv_array
[stats
[k
].giv_number
]->insn
),
6981 stats
[k
].total_benefit
);
6983 putc ('\n', loop_dump_stream
);
6986 for (k
= 0; k
< giv_count
; k
++)
6988 int g1_add_benefit
= 0;
6990 i
= stats
[k
].giv_number
;
6993 /* If it has already been combined, skip. */
6994 if (g1
->combined_with
|| g1
->same
)
6997 for (j
= 0; j
< giv_count
; j
++)
7000 if (g1
!= g2
&& can_combine
[i
*giv_count
+ j
]
7001 /* If it has already been combined, skip. */
7002 && ! g2
->same
&& ! g2
->combined_with
)
7006 g2
->new_reg
= can_combine
[i
*giv_count
+ j
];
7008 g1
->combined_with
++;
7009 g1
->lifetime
+= g2
->lifetime
;
7011 g1_add_benefit
+= g2
->benefit
;
7013 /* ??? The new final_[bg]iv_value code does a much better job
7014 of finding replaceable giv's, and hence this code may no
7015 longer be necessary. */
7016 if (! g2
->replaceable
&& REG_USERVAR_P (g2
->dest_reg
))
7017 g1_add_benefit
-= copy_cost
;
7019 /* To help optimize the next set of combinations, remove
7020 this giv from the benefits of other potential mates. */
7021 for (l
= 0; l
< giv_count
; ++l
)
7023 int m
= stats
[l
].giv_number
;
7024 if (can_combine
[m
*giv_count
+ j
])
7025 stats
[l
].total_benefit
-= g2
->benefit
+ extra_benefit
;
7028 if (loop_dump_stream
)
7029 fprintf (loop_dump_stream
,
7030 "giv at %d combined with giv at %d\n",
7031 INSN_UID (g2
->insn
), INSN_UID (g1
->insn
));
7035 /* To help optimize the next set of combinations, remove
7036 this giv from the benefits of other potential mates. */
7037 if (g1
->combined_with
)
7039 for (j
= 0; j
< giv_count
; ++j
)
7041 int m
= stats
[j
].giv_number
;
7042 if (can_combine
[m
*giv_count
+ i
])
7043 stats
[j
].total_benefit
-= g1
->benefit
+ extra_benefit
;
7046 g1
->benefit
+= g1_add_benefit
;
7048 /* We've finished with this giv, and everything it touched.
7049 Restart the combination so that proper weights for the
7050 rest of the givs are properly taken into account. */
7051 /* ??? Ideally we would compact the arrays at this point, so
7052 as to not cover old ground. But sanely compacting
7053 can_combine is tricky. */
7059 struct recombine_givs_stats
7062 int start_luid
, end_luid
;
7065 /* Used below as comparison function for qsort. We want a ascending luid
7066 when scanning the array starting at the end, thus the arguments are
7069 cmp_recombine_givs_stats (x
, y
)
7070 struct recombine_givs_stats
*x
, *y
;
7073 d
= y
->start_luid
- x
->start_luid
;
7074 /* Stabilize the sort. */
7076 d
= y
->giv_number
- x
->giv_number
;
7080 /* Scan X, which is a part of INSN, for the end of life of a giv. Also
7081 look for the start of life of a giv where the start has not been seen
7082 yet to unlock the search for the end of its life.
7083 Only consider givs that belong to BIV.
7084 Return the total number of lifetime ends that have been found. */
7086 find_life_end (x
, stats
, insn
, biv
)
7088 struct recombine_givs_stats
*stats
;
7095 code
= GET_CODE (x
);
7100 rtx reg
= SET_DEST (x
);
7101 if (GET_CODE (reg
) == REG
)
7103 int regno
= REGNO (reg
);
7104 struct induction
*v
= REG_IV_INFO (regno
);
7106 if (REG_IV_TYPE (regno
) == GENERAL_INDUCT
7108 && v
->src_reg
== biv
7109 && stats
[v
->ix
].end_luid
<= 0)
7111 /* If we see a 0 here for end_luid, it means that we have
7112 scanned the entire loop without finding any use at all.
7113 We must not predicate this code on a start_luid match
7114 since that would make the test fail for givs that have
7115 been hoisted out of inner loops. */
7116 if (stats
[v
->ix
].end_luid
== 0)
7118 stats
[v
->ix
].end_luid
= stats
[v
->ix
].start_luid
;
7119 return 1 + find_life_end (SET_SRC (x
), stats
, insn
, biv
);
7121 else if (stats
[v
->ix
].start_luid
== INSN_LUID (insn
))
7122 stats
[v
->ix
].end_luid
= 0;
7124 return find_life_end (SET_SRC (x
), stats
, insn
, biv
);
7130 int regno
= REGNO (x
);
7131 struct induction
*v
= REG_IV_INFO (regno
);
7133 if (REG_IV_TYPE (regno
) == GENERAL_INDUCT
7135 && v
->src_reg
== biv
7136 && stats
[v
->ix
].end_luid
== 0)
7138 while (INSN_UID (insn
) >= max_uid_for_loop
)
7139 insn
= NEXT_INSN (insn
);
7140 stats
[v
->ix
].end_luid
= INSN_LUID (insn
);
7153 fmt
= GET_RTX_FORMAT (code
);
7155 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
7158 retval
+= find_life_end (XEXP (x
, i
), stats
, insn
, biv
);
7160 else if (fmt
[i
] == 'E')
7161 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
7162 retval
+= find_life_end (XVECEXP (x
, i
, j
), stats
, insn
, biv
);
7167 /* For each giv that has been combined with another, look if
7168 we can combine it with the most recently used one instead.
7169 This tends to shorten giv lifetimes, and helps the next step:
7170 try to derive givs from other givs. */
7172 recombine_givs (bl
, loop_start
, loop_end
, unroll_p
)
7173 struct iv_class
*bl
;
7174 rtx loop_start
, loop_end
;
7177 struct induction
*v
, **giv_array
, *last_giv
;
7178 struct recombine_givs_stats
*stats
;
7181 int ends_need_computing
;
7183 for (giv_count
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
)
7189 = (struct induction
**) alloca (giv_count
* sizeof (struct induction
*));
7190 stats
= (struct recombine_givs_stats
*) alloca (giv_count
* sizeof *stats
);
7192 /* Initialize stats and set up the ix field for each giv in stats to name
7193 the corresponding index into stats. */
7194 for (i
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
)
7201 stats
[i
].giv_number
= i
;
7202 /* If this giv has been hoisted out of an inner loop, use the luid of
7203 the previous insn. */
7204 for (p
= v
->insn
; INSN_UID (p
) >= max_uid_for_loop
; )
7206 stats
[i
].start_luid
= INSN_LUID (p
);
7210 qsort (stats
, giv_count
, sizeof(*stats
), cmp_recombine_givs_stats
);
7212 /* Set up the ix field for each giv in stats to name
7213 the corresponding index into stats, and
7214 do the actual most-recently-used recombination. */
7215 for (last_giv
= 0, i
= giv_count
- 1; i
>= 0; i
--)
7217 v
= giv_array
[stats
[i
].giv_number
];
7221 struct induction
*old_same
= v
->same
;
7224 /* combine_givs_p actually says if we can make this transformation.
7225 The other tests are here only to avoid keeping a giv alive
7226 that could otherwise be eliminated. */
7228 && ((old_same
->maybe_dead
&& ! old_same
->combined_with
)
7229 || ! last_giv
->maybe_dead
7230 || last_giv
->combined_with
)
7231 && (new_combine
= combine_givs_p (last_giv
, v
)))
7233 old_same
->combined_with
--;
7234 v
->new_reg
= new_combine
;
7236 last_giv
->combined_with
++;
7237 /* No need to update lifetimes / benefits here since we have
7238 already decided what to reduce. */
7240 if (loop_dump_stream
)
7242 fprintf (loop_dump_stream
,
7243 "giv at %d recombined with giv at %d as ",
7244 INSN_UID (v
->insn
), INSN_UID (last_giv
->insn
));
7245 print_rtl (loop_dump_stream
, v
->new_reg
);
7246 putc ('\n', loop_dump_stream
);
7252 else if (v
->giv_type
!= DEST_REG
)
7255 || (last_giv
->maybe_dead
&& ! last_giv
->combined_with
)
7257 || v
->combined_with
)
7261 ends_need_computing
= 0;
7262 /* For each DEST_REG giv, compute lifetime starts, and try to compute
7263 lifetime ends from regscan info. */
7264 for (i
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
)
7268 if (v
->giv_type
== DEST_ADDR
)
7270 /* Loop unrolling of an inner loop can even create new DEST_REG
7273 for (p
= v
->insn
; INSN_UID (p
) >= max_uid_for_loop
; )
7275 stats
[i
].start_luid
= stats
[i
].end_luid
= INSN_LUID (p
);
7277 stats
[i
].end_luid
++;
7279 else /* v->giv_type == DEST_REG */
7283 stats
[i
].start_luid
= INSN_LUID (v
->insn
);
7284 stats
[i
].end_luid
= INSN_LUID (v
->last_use
);
7286 else if (INSN_UID (v
->insn
) >= max_uid_for_loop
)
7289 /* This insn has been created by loop optimization on an inner
7290 loop. We don't have a proper start_luid that will match
7291 when we see the first set. But we do know that there will
7292 be no use before the set, so we can set end_luid to 0 so that
7293 we'll start looking for the last use right away. */
7294 for (p
= PREV_INSN (v
->insn
); INSN_UID (p
) >= max_uid_for_loop
; )
7296 stats
[i
].start_luid
= INSN_LUID (p
);
7297 stats
[i
].end_luid
= 0;
7298 ends_need_computing
++;
7302 int regno
= REGNO (v
->dest_reg
);
7303 int count
= VARRAY_INT (n_times_set
, regno
) - 1;
7306 /* Find the first insn that sets the giv, so that we can verify
7307 if this giv's lifetime wraps around the loop. We also need
7308 the luid of the first setting insn in order to detect the
7309 last use properly. */
7312 p
= prev_nonnote_insn (p
);
7313 if (reg_set_p (v
->dest_reg
, p
))
7317 stats
[i
].start_luid
= INSN_LUID (p
);
7318 if (stats
[i
].start_luid
> uid_luid
[REGNO_FIRST_UID (regno
)])
7320 stats
[i
].end_luid
= -1;
7321 ends_need_computing
++;
7325 stats
[i
].end_luid
= uid_luid
[REGNO_LAST_UID (regno
)];
7326 if (stats
[i
].end_luid
> INSN_LUID (loop_end
))
7328 stats
[i
].end_luid
= -1;
7329 ends_need_computing
++;
7337 /* If the regscan information was unconclusive for one or more DEST_REG
7338 givs, scan the all insn in the loop to find out lifetime ends. */
7339 if (ends_need_computing
)
7341 rtx biv
= bl
->biv
->src_reg
;
7346 if (p
== loop_start
)
7349 if (GET_RTX_CLASS (GET_CODE (p
)) != 'i')
7351 ends_need_computing
-= find_life_end (PATTERN (p
), stats
, p
, biv
);
7353 while (ends_need_computing
);
7356 /* Set start_luid back to the last insn that sets the giv. This allows
7357 more combinations. */
7358 for (i
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
)
7362 if (INSN_UID (v
->insn
) < max_uid_for_loop
)
7363 stats
[i
].start_luid
= INSN_LUID (v
->insn
);
7367 /* Now adjust lifetime ends by taking combined givs into account. */
7368 for (i
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
)
7375 if (v
->same
&& ! v
->same
->ignore
)
7378 luid
= stats
[i
].start_luid
;
7379 /* Use unsigned arithmetic to model loop wrap-around. */
7380 if (luid
- stats
[j
].start_luid
7381 > (unsigned) stats
[j
].end_luid
- stats
[j
].start_luid
)
7382 stats
[j
].end_luid
= luid
;
7387 qsort (stats
, giv_count
, sizeof(*stats
), cmp_recombine_givs_stats
);
7389 /* Try to derive DEST_REG givs from previous DEST_REG givs with the
7390 same mult_val and non-overlapping lifetime. This reduces register
7392 Once we find a DEST_REG giv that is suitable to derive others from,
7393 we set last_giv to this giv, and try to derive as many other DEST_REG
7394 givs from it without joining overlapping lifetimes. If we then
7395 encounter a DEST_REG giv that we can't derive, we set rescan to the
7396 index for this giv (unless rescan is already set).
7397 When we are finished with the current LAST_GIV (i.e. the inner loop
7398 terminates), we start again with rescan, which then becomes the new
7400 for (i
= giv_count
- 1; i
>= 0; i
= rescan
)
7402 int life_start
, life_end
;
7404 for (last_giv
= 0, rescan
= -1; i
>= 0; i
--)
7408 v
= giv_array
[stats
[i
].giv_number
];
7409 if (v
->giv_type
!= DEST_REG
|| v
->derived_from
|| v
->same
)
7413 /* Don't use a giv that's likely to be dead to derive
7414 others - that would be likely to keep that giv alive. */
7415 if (! v
->maybe_dead
|| v
->combined_with
)
7418 life_start
= stats
[i
].start_luid
;
7419 life_end
= stats
[i
].end_luid
;
7423 /* Use unsigned arithmetic to model loop wrap around. */
7424 if (((unsigned) stats
[i
].start_luid
- life_start
7425 >= (unsigned) life_end
- life_start
)
7426 && ((unsigned) stats
[i
].end_luid
- life_start
7427 > (unsigned) life_end
- life_start
)
7428 /* Check that the giv insn we're about to use for deriving
7429 precedes all uses of that giv. Note that initializing the
7430 derived giv would defeat the purpose of reducing register
7432 ??? We could arrange to move the insn. */
7433 && ((unsigned) stats
[i
].end_luid
- INSN_LUID (loop_start
)
7434 > (unsigned) stats
[i
].start_luid
- INSN_LUID (loop_start
))
7435 && rtx_equal_p (last_giv
->mult_val
, v
->mult_val
)
7436 /* ??? Could handle libcalls, but would need more logic. */
7437 && ! find_reg_note (v
->insn
, REG_RETVAL
, NULL_RTX
)
7438 /* We would really like to know if for any giv that v
7439 is combined with, v->insn or any intervening biv increment
7440 dominates that combined giv. However, we
7441 don't have this detailed control flow information.
7442 N.B. since last_giv will be reduced, it is valid
7443 anywhere in the loop, so we don't need to check the
7444 validity of last_giv.
7445 We rely here on the fact that v->always_executed implies that
7446 there is no jump to someplace else in the loop before the
7447 giv insn, and hence any insn that is executed before the
7448 giv insn in the loop will have a lower luid. */
7449 && (v
->always_executed
|| ! v
->combined_with
)
7450 && (sum
= express_from (last_giv
, v
))
7451 /* Make sure we don't make the add more expensive. ADD_COST
7452 doesn't take different costs of registers and constants into
7453 account, so compare the cost of the actual SET_SRCs. */
7454 && (rtx_cost (sum
, SET
)
7455 <= rtx_cost (SET_SRC (single_set (v
->insn
)), SET
))
7456 /* ??? unroll can't understand anything but reg + const_int
7457 sums. It would be cleaner to fix unroll. */
7458 && ((GET_CODE (sum
) == PLUS
7459 && GET_CODE (XEXP (sum
, 0)) == REG
7460 && GET_CODE (XEXP (sum
, 1)) == CONST_INT
)
7462 && validate_change (v
->insn
, &PATTERN (v
->insn
),
7463 gen_rtx_SET (VOIDmode
, v
->dest_reg
, sum
), 0))
7465 v
->derived_from
= last_giv
;
7466 life_end
= stats
[i
].end_luid
;
7468 if (loop_dump_stream
)
7470 fprintf (loop_dump_stream
,
7471 "giv at %d derived from %d as ",
7472 INSN_UID (v
->insn
), INSN_UID (last_giv
->insn
));
7473 print_rtl (loop_dump_stream
, sum
);
7474 putc ('\n', loop_dump_stream
);
7477 else if (rescan
< 0)
7483 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
7486 emit_iv_add_mult (b
, m
, a
, reg
, insert_before
)
7487 rtx b
; /* initial value of basic induction variable */
7488 rtx m
; /* multiplicative constant */
7489 rtx a
; /* additive constant */
7490 rtx reg
; /* destination register */
7496 /* Prevent unexpected sharing of these rtx. */
7500 /* Increase the lifetime of any invariants moved further in code. */
7501 update_reg_last_use (a
, insert_before
);
7502 update_reg_last_use (b
, insert_before
);
7503 update_reg_last_use (m
, insert_before
);
7506 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 0);
7508 emit_move_insn (reg
, result
);
7509 seq
= gen_sequence ();
7512 emit_insn_before (seq
, insert_before
);
7514 /* It is entirely possible that the expansion created lots of new
7515 registers. Iterate over the sequence we just created and
7518 if (GET_CODE (seq
) == SEQUENCE
)
7521 for (i
= 0; i
< XVECLEN (seq
, 0); ++i
)
7523 rtx set
= single_set (XVECEXP (seq
, 0, i
));
7524 if (set
&& GET_CODE (SET_DEST (set
)) == REG
)
7525 record_base_value (REGNO (SET_DEST (set
)), SET_SRC (set
), 0);
7528 else if (GET_CODE (seq
) == SET
7529 && GET_CODE (SET_DEST (seq
)) == REG
)
7530 record_base_value (REGNO (SET_DEST (seq
)), SET_SRC (seq
), 0);
7533 /* Test whether A * B can be computed without
7534 an actual multiply insn. Value is 1 if so. */
7537 product_cheap_p (a
, b
)
7543 struct obstack
*old_rtl_obstack
= rtl_obstack
;
7544 char *storage
= (char *) obstack_alloc (&temp_obstack
, 0);
7547 /* If only one is constant, make it B. */
7548 if (GET_CODE (a
) == CONST_INT
)
7549 tmp
= a
, a
= b
, b
= tmp
;
7551 /* If first constant, both constant, so don't need multiply. */
7552 if (GET_CODE (a
) == CONST_INT
)
7555 /* If second not constant, neither is constant, so would need multiply. */
7556 if (GET_CODE (b
) != CONST_INT
)
7559 /* One operand is constant, so might not need multiply insn. Generate the
7560 code for the multiply and see if a call or multiply, or long sequence
7561 of insns is generated. */
7563 rtl_obstack
= &temp_obstack
;
7565 expand_mult (GET_MODE (a
), a
, b
, NULL_RTX
, 0);
7566 tmp
= gen_sequence ();
7569 if (GET_CODE (tmp
) == SEQUENCE
)
7571 if (XVEC (tmp
, 0) == 0)
7573 else if (XVECLEN (tmp
, 0) > 3)
7576 for (i
= 0; i
< XVECLEN (tmp
, 0); i
++)
7578 rtx insn
= XVECEXP (tmp
, 0, i
);
7580 if (GET_CODE (insn
) != INSN
7581 || (GET_CODE (PATTERN (insn
)) == SET
7582 && GET_CODE (SET_SRC (PATTERN (insn
))) == MULT
)
7583 || (GET_CODE (PATTERN (insn
)) == PARALLEL
7584 && GET_CODE (XVECEXP (PATTERN (insn
), 0, 0)) == SET
7585 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn
), 0, 0))) == MULT
))
7592 else if (GET_CODE (tmp
) == SET
7593 && GET_CODE (SET_SRC (tmp
)) == MULT
)
7595 else if (GET_CODE (tmp
) == PARALLEL
7596 && GET_CODE (XVECEXP (tmp
, 0, 0)) == SET
7597 && GET_CODE (SET_SRC (XVECEXP (tmp
, 0, 0))) == MULT
)
7600 /* Free any storage we obtained in generating this multiply and restore rtl
7601 allocation to its normal obstack. */
7602 obstack_free (&temp_obstack
, storage
);
7603 rtl_obstack
= old_rtl_obstack
;
7608 /* Check to see if loop can be terminated by a "decrement and branch until
7609 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7610 Also try reversing an increment loop to a decrement loop
7611 to see if the optimization can be performed.
7612 Value is nonzero if optimization was performed. */
7614 /* This is useful even if the architecture doesn't have such an insn,
7615 because it might change a loops which increments from 0 to n to a loop
7616 which decrements from n to 0. A loop that decrements to zero is usually
7617 faster than one that increments from zero. */
7619 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7620 such as approx_final_value, biv_total_increment, loop_iterations, and
7621 final_[bg]iv_value. */
7624 check_dbra_loop (loop_end
, insn_count
, loop_start
, loop_info
)
7628 struct loop_info
*loop_info
;
7630 struct iv_class
*bl
;
7637 rtx before_comparison
;
7641 int compare_and_branch
;
7643 /* If last insn is a conditional branch, and the insn before tests a
7644 register value, try to optimize it. Otherwise, we can't do anything. */
7646 jump
= PREV_INSN (loop_end
);
7647 comparison
= get_condition_for_loop (jump
);
7648 if (comparison
== 0)
7651 /* Try to compute whether the compare/branch at the loop end is one or
7652 two instructions. */
7653 get_condition (jump
, &first_compare
);
7654 if (first_compare
== jump
)
7655 compare_and_branch
= 1;
7656 else if (first_compare
== prev_nonnote_insn (jump
))
7657 compare_and_branch
= 2;
7661 /* Check all of the bivs to see if the compare uses one of them.
7662 Skip biv's set more than once because we can't guarantee that
7663 it will be zero on the last iteration. Also skip if the biv is
7664 used between its update and the test insn. */
7666 for (bl
= loop_iv_list
; bl
; bl
= bl
->next
)
7668 if (bl
->biv_count
== 1
7669 && bl
->biv
->dest_reg
== XEXP (comparison
, 0)
7670 && ! reg_used_between_p (regno_reg_rtx
[bl
->regno
], bl
->biv
->insn
,
7678 /* Look for the case where the basic induction variable is always
7679 nonnegative, and equals zero on the last iteration.
7680 In this case, add a reg_note REG_NONNEG, which allows the
7681 m68k DBRA instruction to be used. */
7683 if (((GET_CODE (comparison
) == GT
7684 && GET_CODE (XEXP (comparison
, 1)) == CONST_INT
7685 && INTVAL (XEXP (comparison
, 1)) == -1)
7686 || (GET_CODE (comparison
) == NE
&& XEXP (comparison
, 1) == const0_rtx
))
7687 && GET_CODE (bl
->biv
->add_val
) == CONST_INT
7688 && INTVAL (bl
->biv
->add_val
) < 0)
7690 /* Initial value must be greater than 0,
7691 init_val % -dec_value == 0 to ensure that it equals zero on
7692 the last iteration */
7694 if (GET_CODE (bl
->initial_value
) == CONST_INT
7695 && INTVAL (bl
->initial_value
) > 0
7696 && (INTVAL (bl
->initial_value
)
7697 % (-INTVAL (bl
->biv
->add_val
))) == 0)
7699 /* register always nonnegative, add REG_NOTE to branch */
7700 REG_NOTES (PREV_INSN (loop_end
))
7701 = gen_rtx_EXPR_LIST (REG_NONNEG
, NULL_RTX
,
7702 REG_NOTES (PREV_INSN (loop_end
)));
7708 /* If the decrement is 1 and the value was tested as >= 0 before
7709 the loop, then we can safely optimize. */
7710 for (p
= loop_start
; p
; p
= PREV_INSN (p
))
7712 if (GET_CODE (p
) == CODE_LABEL
)
7714 if (GET_CODE (p
) != JUMP_INSN
)
7717 before_comparison
= get_condition_for_loop (p
);
7718 if (before_comparison
7719 && XEXP (before_comparison
, 0) == bl
->biv
->dest_reg
7720 && GET_CODE (before_comparison
) == LT
7721 && XEXP (before_comparison
, 1) == const0_rtx
7722 && ! reg_set_between_p (bl
->biv
->dest_reg
, p
, loop_start
)
7723 && INTVAL (bl
->biv
->add_val
) == -1)
7725 REG_NOTES (PREV_INSN (loop_end
))
7726 = gen_rtx_EXPR_LIST (REG_NONNEG
, NULL_RTX
,
7727 REG_NOTES (PREV_INSN (loop_end
)));
7734 else if (INTVAL (bl
->biv
->add_val
) > 0)
7736 /* Try to change inc to dec, so can apply above optimization. */
7738 all registers modified are induction variables or invariant,
7739 all memory references have non-overlapping addresses
7740 (obviously true if only one write)
7741 allow 2 insns for the compare/jump at the end of the loop. */
7742 /* Also, we must avoid any instructions which use both the reversed
7743 biv and another biv. Such instructions will fail if the loop is
7744 reversed. We meet this condition by requiring that either
7745 no_use_except_counting is true, or else that there is only
7747 int num_nonfixed_reads
= 0;
7748 /* 1 if the iteration var is used only to count iterations. */
7749 int no_use_except_counting
= 0;
7750 /* 1 if the loop has no memory store, or it has a single memory store
7751 which is reversible. */
7752 int reversible_mem_store
= 1;
7754 if (bl
->giv_count
== 0
7755 && ! loop_number_exit_count
[uid_loop_num
[INSN_UID (loop_start
)]])
7757 rtx bivreg
= regno_reg_rtx
[bl
->regno
];
7759 /* If there are no givs for this biv, and the only exit is the
7760 fall through at the end of the loop, then
7761 see if perhaps there are no uses except to count. */
7762 no_use_except_counting
= 1;
7763 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
7764 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i')
7766 rtx set
= single_set (p
);
7768 if (set
&& GET_CODE (SET_DEST (set
)) == REG
7769 && REGNO (SET_DEST (set
)) == bl
->regno
)
7770 /* An insn that sets the biv is okay. */
7772 else if (p
== prev_nonnote_insn (prev_nonnote_insn (loop_end
))
7773 || p
== prev_nonnote_insn (loop_end
))
7774 /* Don't bother about the end test. */
7776 else if (reg_mentioned_p (bivreg
, PATTERN (p
)))
7778 no_use_except_counting
= 0;
7784 if (no_use_except_counting
)
7785 ; /* no need to worry about MEMs. */
7786 else if (num_mem_sets
<= 1)
7788 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
7789 if (GET_RTX_CLASS (GET_CODE (p
)) == 'i')
7790 num_nonfixed_reads
+= count_nonfixed_reads (PATTERN (p
));
7792 /* If the loop has a single store, and the destination address is
7793 invariant, then we can't reverse the loop, because this address
7794 might then have the wrong value at loop exit.
7795 This would work if the source was invariant also, however, in that
7796 case, the insn should have been moved out of the loop. */
7798 if (num_mem_sets
== 1)
7800 struct induction
*v
;
7802 reversible_mem_store
7803 = (! unknown_address_altered
7804 && ! invariant_p (XEXP (XEXP (loop_store_mems
, 0), 0)));
7806 /* If the store depends on a register that is set after the
7807 store, it depends on the initial value, and is thus not
7809 for (v
= bl
->giv
; reversible_mem_store
&& v
; v
= v
->next_iv
)
7811 if (v
->giv_type
== DEST_REG
7812 && reg_mentioned_p (v
->dest_reg
,
7813 XEXP (loop_store_mems
, 0))
7814 && loop_insn_first_p (first_loop_store_insn
, v
->insn
))
7815 reversible_mem_store
= 0;
7822 /* This code only acts for innermost loops. Also it simplifies
7823 the memory address check by only reversing loops with
7824 zero or one memory access.
7825 Two memory accesses could involve parts of the same array,
7826 and that can't be reversed.
7827 If the biv is used only for counting, than we don't need to worry
7828 about all these things. */
7830 if ((num_nonfixed_reads
<= 1
7832 && !loop_has_volatile
7833 && reversible_mem_store
7834 && (bl
->giv_count
+ bl
->biv_count
+ num_mem_sets
7835 + num_movables
+ compare_and_branch
== insn_count
)
7836 && (bl
== loop_iv_list
&& bl
->next
== 0))
7837 || no_use_except_counting
)
7841 /* Loop can be reversed. */
7842 if (loop_dump_stream
)
7843 fprintf (loop_dump_stream
, "Can reverse loop\n");
7845 /* Now check other conditions:
7847 The increment must be a constant, as must the initial value,
7848 and the comparison code must be LT.
7850 This test can probably be improved since +/- 1 in the constant
7851 can be obtained by changing LT to LE and vice versa; this is
7855 /* for constants, LE gets turned into LT */
7856 && (GET_CODE (comparison
) == LT
7857 || (GET_CODE (comparison
) == LE
7858 && no_use_except_counting
)))
7860 HOST_WIDE_INT add_val
, add_adjust
, comparison_val
;
7861 rtx initial_value
, comparison_value
;
7863 enum rtx_code cmp_code
;
7864 int comparison_const_width
;
7865 unsigned HOST_WIDE_INT comparison_sign_mask
;
7867 add_val
= INTVAL (bl
->biv
->add_val
);
7868 comparison_value
= XEXP (comparison
, 1);
7869 if (GET_MODE (comparison_value
) == VOIDmode
)
7870 comparison_const_width
7871 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison
, 0)));
7873 comparison_const_width
7874 = GET_MODE_BITSIZE (GET_MODE (comparison_value
));
7875 if (comparison_const_width
> HOST_BITS_PER_WIDE_INT
)
7876 comparison_const_width
= HOST_BITS_PER_WIDE_INT
;
7877 comparison_sign_mask
7878 = (unsigned HOST_WIDE_INT
)1 << (comparison_const_width
- 1);
7880 /* If the comparison value is not a loop invariant, then we
7881 can not reverse this loop.
7883 ??? If the insns which initialize the comparison value as
7884 a whole compute an invariant result, then we could move
7885 them out of the loop and proceed with loop reversal. */
7886 if (!invariant_p (comparison_value
))
7889 if (GET_CODE (comparison_value
) == CONST_INT
)
7890 comparison_val
= INTVAL (comparison_value
);
7891 initial_value
= bl
->initial_value
;
7893 /* Normalize the initial value if it is an integer and
7894 has no other use except as a counter. This will allow
7895 a few more loops to be reversed. */
7896 if (no_use_except_counting
7897 && GET_CODE (comparison_value
) == CONST_INT
7898 && GET_CODE (initial_value
) == CONST_INT
)
7900 comparison_val
= comparison_val
- INTVAL (bl
->initial_value
);
7901 /* The code below requires comparison_val to be a multiple
7902 of add_val in order to do the loop reversal, so
7903 round up comparison_val to a multiple of add_val.
7904 Since comparison_value is constant, we know that the
7905 current comparison code is LT. */
7906 comparison_val
= comparison_val
+ add_val
- 1;
7908 -= (unsigned HOST_WIDE_INT
) comparison_val
% add_val
;
7909 /* We postpone overflow checks for COMPARISON_VAL here;
7910 even if there is an overflow, we might still be able to
7911 reverse the loop, if converting the loop exit test to
7913 initial_value
= const0_rtx
;
7916 /* First check if we can do a vanilla loop reversal. */
7917 if (initial_value
== const0_rtx
7918 /* If we have a decrement_and_branch_on_count, prefer
7919 the NE test, since this will allow that instruction to
7920 be generated. Note that we must use a vanilla loop
7921 reversal if the biv is used to calculate a giv or has
7922 a non-counting use. */
7923 #if ! defined (HAVE_decrement_and_branch_until_zero) && defined (HAVE_decrement_and_branch_on_count)
7924 && (! (add_val
== 1 && loop_info
->vtop
7925 && (bl
->biv_count
== 0
7926 || no_use_except_counting
)))
7928 && GET_CODE (comparison_value
) == CONST_INT
7929 /* Now do postponed overflow checks on COMPARISON_VAL. */
7930 && ! (((comparison_val
- add_val
) ^ INTVAL (comparison_value
))
7931 & comparison_sign_mask
))
7933 /* Register will always be nonnegative, with value
7934 0 on last iteration */
7935 add_adjust
= add_val
;
7939 else if (add_val
== 1 && loop_info
->vtop
7940 && (bl
->biv_count
== 0
7941 || no_use_except_counting
))
7949 if (GET_CODE (comparison
) == LE
)
7950 add_adjust
-= add_val
;
7952 /* If the initial value is not zero, or if the comparison
7953 value is not an exact multiple of the increment, then we
7954 can not reverse this loop. */
7955 if (initial_value
== const0_rtx
7956 && GET_CODE (comparison_value
) == CONST_INT
)
7958 if (((unsigned HOST_WIDE_INT
) comparison_val
% add_val
) != 0)
7963 if (! no_use_except_counting
|| add_val
!= 1)
7967 final_value
= comparison_value
;
7969 /* Reset these in case we normalized the initial value
7970 and comparison value above. */
7971 if (GET_CODE (comparison_value
) == CONST_INT
7972 && GET_CODE (initial_value
) == CONST_INT
)
7974 comparison_value
= GEN_INT (comparison_val
);
7976 = GEN_INT (comparison_val
+ INTVAL (bl
->initial_value
));
7978 bl
->initial_value
= initial_value
;
7980 /* Save some info needed to produce the new insns. */
7981 reg
= bl
->biv
->dest_reg
;
7982 jump_label
= XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end
))), 1);
7983 if (jump_label
== pc_rtx
)
7984 jump_label
= XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end
))), 2);
7985 new_add_val
= GEN_INT (- INTVAL (bl
->biv
->add_val
));
7987 /* Set start_value; if this is not a CONST_INT, we need
7989 Initialize biv to start_value before loop start.
7990 The old initializing insn will be deleted as a
7991 dead store by flow.c. */
7992 if (initial_value
== const0_rtx
7993 && GET_CODE (comparison_value
) == CONST_INT
)
7995 start_value
= GEN_INT (comparison_val
- add_adjust
);
7996 emit_insn_before (gen_move_insn (reg
, start_value
),
7999 else if (GET_CODE (initial_value
) == CONST_INT
)
8001 rtx offset
= GEN_INT (-INTVAL (initial_value
) - add_adjust
);
8002 enum machine_mode mode
= GET_MODE (reg
);
8003 enum insn_code icode
8004 = add_optab
->handlers
[(int) mode
].insn_code
;
8005 if (! (*insn_operand_predicate
[icode
][0]) (reg
, mode
)
8006 || ! ((*insn_operand_predicate
[icode
][1])
8007 (comparison_value
, mode
))
8008 || ! (*insn_operand_predicate
[icode
][2]) (offset
, mode
))
8011 = gen_rtx_PLUS (mode
, comparison_value
, offset
);
8012 emit_insn_before ((GEN_FCN (icode
)
8013 (reg
, comparison_value
, offset
)),
8015 if (GET_CODE (comparison
) == LE
)
8016 final_value
= gen_rtx_PLUS (mode
, comparison_value
,
8019 else if (! add_adjust
)
8021 enum machine_mode mode
= GET_MODE (reg
);
8022 enum insn_code icode
8023 = sub_optab
->handlers
[(int) mode
].insn_code
;
8024 if (! (*insn_operand_predicate
[icode
][0]) (reg
, mode
)
8025 || ! ((*insn_operand_predicate
[icode
][1])
8026 (comparison_value
, mode
))
8027 || ! ((*insn_operand_predicate
[icode
][2])
8028 (initial_value
, mode
)))
8031 = gen_rtx_MINUS (mode
, comparison_value
, initial_value
);
8032 emit_insn_before ((GEN_FCN (icode
)
8033 (reg
, comparison_value
, initial_value
)),
8037 /* We could handle the other cases too, but it'll be
8038 better to have a testcase first. */
8041 /* We may not have a single insn which can increment a reg, so
8042 create a sequence to hold all the insns from expand_inc. */
8044 expand_inc (reg
, new_add_val
);
8045 tem
= gen_sequence ();
8048 p
= emit_insn_before (tem
, bl
->biv
->insn
);
8049 delete_insn (bl
->biv
->insn
);
8051 /* Update biv info to reflect its new status. */
8053 bl
->initial_value
= start_value
;
8054 bl
->biv
->add_val
= new_add_val
;
8056 /* Update loop info. */
8057 loop_info
->initial_value
= reg
;
8058 loop_info
->initial_equiv_value
= reg
;
8059 loop_info
->final_value
= const0_rtx
;
8060 loop_info
->final_equiv_value
= const0_rtx
;
8061 loop_info
->comparison_value
= const0_rtx
;
8062 loop_info
->comparison_code
= cmp_code
;
8063 loop_info
->increment
= new_add_val
;
8065 /* Inc LABEL_NUSES so that delete_insn will
8066 not delete the label. */
8067 LABEL_NUSES (XEXP (jump_label
, 0)) ++;
8069 /* Emit an insn after the end of the loop to set the biv's
8070 proper exit value if it is used anywhere outside the loop. */
8071 if ((REGNO_LAST_UID (bl
->regno
) != INSN_UID (first_compare
))
8073 || REGNO_FIRST_UID (bl
->regno
) != INSN_UID (bl
->init_insn
))
8074 emit_insn_after (gen_move_insn (reg
, final_value
),
8077 /* Delete compare/branch at end of loop. */
8078 delete_insn (PREV_INSN (loop_end
));
8079 if (compare_and_branch
== 2)
8080 delete_insn (first_compare
);
8082 /* Add new compare/branch insn at end of loop. */
8084 emit_cmp_and_jump_insns (reg
, const0_rtx
, cmp_code
, NULL_RTX
,
8085 GET_MODE (reg
), 0, 0,
8086 XEXP (jump_label
, 0));
8087 tem
= gen_sequence ();
8089 emit_jump_insn_before (tem
, loop_end
);
8091 for (tem
= PREV_INSN (loop_end
);
8092 tem
&& GET_CODE (tem
) != JUMP_INSN
;
8093 tem
= PREV_INSN (tem
))
8097 JUMP_LABEL (tem
) = XEXP (jump_label
, 0);
8103 /* Increment of LABEL_NUSES done above. */
8104 /* Register is now always nonnegative,
8105 so add REG_NONNEG note to the branch. */
8106 REG_NOTES (tem
) = gen_rtx_EXPR_LIST (REG_NONNEG
, NULL_RTX
,
8112 /* Mark that this biv has been reversed. Each giv which depends
8113 on this biv, and which is also live past the end of the loop
8114 will have to be fixed up. */
8118 if (loop_dump_stream
)
8120 fprintf (loop_dump_stream
, "Reversed loop");
8122 fprintf (loop_dump_stream
, " and added reg_nonneg\n");
8124 fprintf (loop_dump_stream
, "\n");
8135 /* Verify whether the biv BL appears to be eliminable,
8136 based on the insns in the loop that refer to it.
8137 LOOP_START is the first insn of the loop, and END is the end insn.
8139 If ELIMINATE_P is non-zero, actually do the elimination.
8141 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8142 determine whether invariant insns should be placed inside or at the
8143 start of the loop. */
8146 maybe_eliminate_biv (bl
, loop_start
, end
, eliminate_p
, threshold
, insn_count
)
8147 struct iv_class
*bl
;
8151 int threshold
, insn_count
;
8153 rtx reg
= bl
->biv
->dest_reg
;
8156 /* Scan all insns in the loop, stopping if we find one that uses the
8157 biv in a way that we cannot eliminate. */
8159 for (p
= loop_start
; p
!= end
; p
= NEXT_INSN (p
))
8161 enum rtx_code code
= GET_CODE (p
);
8162 rtx where
= threshold
>= insn_count
? loop_start
: p
;
8164 /* If this is a libcall that sets a giv, skip ahead to its end. */
8165 if (GET_RTX_CLASS (code
) == 'i')
8167 rtx note
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
);
8171 rtx last
= XEXP (note
, 0);
8172 rtx set
= single_set (last
);
8174 if (set
&& GET_CODE (SET_DEST (set
)) == REG
)
8176 int regno
= REGNO (SET_DEST (set
));
8178 if (regno
< max_reg_before_loop
8179 && REG_IV_TYPE (regno
) == GENERAL_INDUCT
8180 && REG_IV_INFO (regno
)->src_reg
== bl
->biv
->src_reg
)
8185 if ((code
== INSN
|| code
== JUMP_INSN
|| code
== CALL_INSN
)
8186 && reg_mentioned_p (reg
, PATTERN (p
))
8187 && ! maybe_eliminate_biv_1 (PATTERN (p
), p
, bl
, eliminate_p
, where
))
8189 if (loop_dump_stream
)
8190 fprintf (loop_dump_stream
,
8191 "Cannot eliminate biv %d: biv used in insn %d.\n",
8192 bl
->regno
, INSN_UID (p
));
8199 if (loop_dump_stream
)
8200 fprintf (loop_dump_stream
, "biv %d %s eliminated.\n",
8201 bl
->regno
, eliminate_p
? "was" : "can be");
8208 /* INSN and REFERENCE are instructions in the same insn chain.
8209 Return non-zero if INSN is first. */
8212 loop_insn_first_p (insn
, reference
)
8213 rtx insn
, reference
;
8217 for (p
= insn
, q
= reference
; ;)
8219 /* Start with test for not first so that INSN == REFERENCE yields not
8221 if (q
== insn
|| ! p
)
8223 if (p
== reference
|| ! q
)
8226 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8227 previous insn, hence the <= comparison below does not work if
8229 if (INSN_UID (p
) < max_uid_for_loop
8230 && INSN_UID (q
) < max_uid_for_loop
8231 && GET_CODE (p
) != NOTE
)
8232 return INSN_LUID (p
) <= INSN_LUID (q
);
8234 if (INSN_UID (p
) >= max_uid_for_loop
8235 || GET_CODE (p
) == NOTE
)
8237 if (INSN_UID (q
) >= max_uid_for_loop
)
8242 /* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
8243 the offset that we have to take into account due to auto-increment /
8244 div derivation is zero. */
8246 biv_elimination_giv_has_0_offset (biv
, giv
, insn
)
8247 struct induction
*biv
, *giv
;
8250 /* If the giv V had the auto-inc address optimization applied
8251 to it, and INSN occurs between the giv insn and the biv
8252 insn, then we'd have to adjust the value used here.
8253 This is rare, so we don't bother to make this possible. */
8254 if (giv
->auto_inc_opt
8255 && ((loop_insn_first_p (giv
->insn
, insn
)
8256 && loop_insn_first_p (insn
, biv
->insn
))
8257 || (loop_insn_first_p (biv
->insn
, insn
)
8258 && loop_insn_first_p (insn
, giv
->insn
))))
8261 /* If the giv V was derived from another giv, and INSN does
8262 not occur between the giv insn and the biv insn, then we'd
8263 have to adjust the value used here. This is rare, so we don't
8264 bother to make this possible. */
8265 if (giv
->derived_from
8266 && ! (giv
->always_executed
8267 && loop_insn_first_p (giv
->insn
, insn
)
8268 && loop_insn_first_p (insn
, biv
->insn
)))
8271 && giv
->same
->derived_from
8272 && ! (giv
->same
->always_executed
8273 && loop_insn_first_p (giv
->same
->insn
, insn
)
8274 && loop_insn_first_p (insn
, biv
->insn
)))
8280 /* If BL appears in X (part of the pattern of INSN), see if we can
8281 eliminate its use. If so, return 1. If not, return 0.
8283 If BIV does not appear in X, return 1.
8285 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
8286 where extra insns should be added. Depending on how many items have been
8287 moved out of the loop, it will either be before INSN or at the start of
8291 maybe_eliminate_biv_1 (x
, insn
, bl
, eliminate_p
, where
)
8293 struct iv_class
*bl
;
8297 enum rtx_code code
= GET_CODE (x
);
8298 rtx reg
= bl
->biv
->dest_reg
;
8299 enum machine_mode mode
= GET_MODE (reg
);
8300 struct induction
*v
;
8312 /* If we haven't already been able to do something with this BIV,
8313 we can't eliminate it. */
8319 /* If this sets the BIV, it is not a problem. */
8320 if (SET_DEST (x
) == reg
)
8323 /* If this is an insn that defines a giv, it is also ok because
8324 it will go away when the giv is reduced. */
8325 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8326 if (v
->giv_type
== DEST_REG
&& SET_DEST (x
) == v
->dest_reg
)
8330 if (SET_DEST (x
) == cc0_rtx
&& SET_SRC (x
) == reg
)
8332 /* Can replace with any giv that was reduced and
8333 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8334 Require a constant for MULT_VAL, so we know it's nonzero.
8335 ??? We disable this optimization to avoid potential
8338 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8339 if (CONSTANT_P (v
->mult_val
) && v
->mult_val
!= const0_rtx
8340 && v
->add_val
== const0_rtx
8341 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8345 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8351 /* If the giv has the opposite direction of change,
8352 then reverse the comparison. */
8353 if (INTVAL (v
->mult_val
) < 0)
8354 new = gen_rtx_COMPARE (GET_MODE (v
->new_reg
),
8355 const0_rtx
, v
->new_reg
);
8359 /* We can probably test that giv's reduced reg. */
8360 if (validate_change (insn
, &SET_SRC (x
), new, 0))
8364 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8365 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8366 Require a constant for MULT_VAL, so we know it's nonzero.
8367 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8368 overflow problem. */
8370 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8371 if (CONSTANT_P (v
->mult_val
) && v
->mult_val
!= const0_rtx
8372 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8374 && (GET_CODE (v
->add_val
) == SYMBOL_REF
8375 || GET_CODE (v
->add_val
) == LABEL_REF
8376 || GET_CODE (v
->add_val
) == CONST
8377 || (GET_CODE (v
->add_val
) == REG
8378 && REGNO_POINTER_FLAG (REGNO (v
->add_val
)))))
8380 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8386 /* If the giv has the opposite direction of change,
8387 then reverse the comparison. */
8388 if (INTVAL (v
->mult_val
) < 0)
8389 new = gen_rtx_COMPARE (VOIDmode
, copy_rtx (v
->add_val
),
8392 new = gen_rtx_COMPARE (VOIDmode
, v
->new_reg
,
8393 copy_rtx (v
->add_val
));
8395 /* Replace biv with the giv's reduced register. */
8396 update_reg_last_use (v
->add_val
, insn
);
8397 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
8400 /* Insn doesn't support that constant or invariant. Copy it
8401 into a register (it will be a loop invariant.) */
8402 tem
= gen_reg_rtx (GET_MODE (v
->new_reg
));
8404 emit_insn_before (gen_move_insn (tem
, copy_rtx (v
->add_val
)),
8407 /* Substitute the new register for its invariant value in
8408 the compare expression. */
8409 XEXP (new, (INTVAL (v
->mult_val
) < 0) ? 0 : 1) = tem
;
8410 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
8419 case GT
: case GE
: case GTU
: case GEU
:
8420 case LT
: case LE
: case LTU
: case LEU
:
8421 /* See if either argument is the biv. */
8422 if (XEXP (x
, 0) == reg
)
8423 arg
= XEXP (x
, 1), arg_operand
= 1;
8424 else if (XEXP (x
, 1) == reg
)
8425 arg
= XEXP (x
, 0), arg_operand
= 0;
8429 if (CONSTANT_P (arg
))
8431 /* First try to replace with any giv that has constant positive
8432 mult_val and constant add_val. We might be able to support
8433 negative mult_val, but it seems complex to do it in general. */
8435 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8436 if (CONSTANT_P (v
->mult_val
) && INTVAL (v
->mult_val
) > 0
8437 && (GET_CODE (v
->add_val
) == SYMBOL_REF
8438 || GET_CODE (v
->add_val
) == LABEL_REF
8439 || GET_CODE (v
->add_val
) == CONST
8440 || (GET_CODE (v
->add_val
) == REG
8441 && REGNO_POINTER_FLAG (REGNO (v
->add_val
))))
8442 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8445 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8451 /* Replace biv with the giv's reduced reg. */
8452 XEXP (x
, 1-arg_operand
) = v
->new_reg
;
8454 /* If all constants are actually constant integers and
8455 the derived constant can be directly placed in the COMPARE,
8457 if (GET_CODE (arg
) == CONST_INT
8458 && GET_CODE (v
->mult_val
) == CONST_INT
8459 && GET_CODE (v
->add_val
) == CONST_INT
8460 && validate_change (insn
, &XEXP (x
, arg_operand
),
8461 GEN_INT (INTVAL (arg
)
8462 * INTVAL (v
->mult_val
)
8463 + INTVAL (v
->add_val
)), 0))
8466 /* Otherwise, load it into a register. */
8467 tem
= gen_reg_rtx (mode
);
8468 emit_iv_add_mult (arg
, v
->mult_val
, v
->add_val
, tem
, where
);
8469 if (validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 0))
8472 /* If that failed, put back the change we made above. */
8473 XEXP (x
, 1-arg_operand
) = reg
;
8476 /* Look for giv with positive constant mult_val and nonconst add_val.
8477 Insert insns to calculate new compare value.
8478 ??? Turn this off due to possible overflow. */
8480 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8481 if (CONSTANT_P (v
->mult_val
) && INTVAL (v
->mult_val
) > 0
8482 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8488 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8494 tem
= gen_reg_rtx (mode
);
8496 /* Replace biv with giv's reduced register. */
8497 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
8500 /* Compute value to compare against. */
8501 emit_iv_add_mult (arg
, v
->mult_val
, v
->add_val
, tem
, where
);
8502 /* Use it in this insn. */
8503 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
8504 if (apply_change_group ())
8508 else if (GET_CODE (arg
) == REG
|| GET_CODE (arg
) == MEM
)
8510 if (invariant_p (arg
) == 1)
8512 /* Look for giv with constant positive mult_val and nonconst
8513 add_val. Insert insns to compute new compare value.
8514 ??? Turn this off due to possible overflow. */
8516 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8517 if (CONSTANT_P (v
->mult_val
) && INTVAL (v
->mult_val
) > 0
8518 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8524 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8530 tem
= gen_reg_rtx (mode
);
8532 /* Replace biv with giv's reduced register. */
8533 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
8536 /* Compute value to compare against. */
8537 emit_iv_add_mult (arg
, v
->mult_val
, v
->add_val
,
8539 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
8540 if (apply_change_group ())
8545 /* This code has problems. Basically, you can't know when
8546 seeing if we will eliminate BL, whether a particular giv
8547 of ARG will be reduced. If it isn't going to be reduced,
8548 we can't eliminate BL. We can try forcing it to be reduced,
8549 but that can generate poor code.
8551 The problem is that the benefit of reducing TV, below should
8552 be increased if BL can actually be eliminated, but this means
8553 we might have to do a topological sort of the order in which
8554 we try to process biv. It doesn't seem worthwhile to do
8555 this sort of thing now. */
8558 /* Otherwise the reg compared with had better be a biv. */
8559 if (GET_CODE (arg
) != REG
8560 || REG_IV_TYPE (REGNO (arg
)) != BASIC_INDUCT
)
8563 /* Look for a pair of givs, one for each biv,
8564 with identical coefficients. */
8565 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8567 struct induction
*tv
;
8569 if (v
->ignore
|| v
->maybe_dead
|| v
->mode
!= mode
)
8572 for (tv
= reg_biv_class
[REGNO (arg
)]->giv
; tv
; tv
= tv
->next_iv
)
8573 if (! tv
->ignore
&& ! tv
->maybe_dead
8574 && rtx_equal_p (tv
->mult_val
, v
->mult_val
)
8575 && rtx_equal_p (tv
->add_val
, v
->add_val
)
8576 && tv
->mode
== mode
)
8578 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8584 /* Replace biv with its giv's reduced reg. */
8585 XEXP (x
, 1-arg_operand
) = v
->new_reg
;
8586 /* Replace other operand with the other giv's
8588 XEXP (x
, arg_operand
) = tv
->new_reg
;
8595 /* If we get here, the biv can't be eliminated. */
8599 /* If this address is a DEST_ADDR giv, it doesn't matter if the
8600 biv is used in it, since it will be replaced. */
8601 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8602 if (v
->giv_type
== DEST_ADDR
&& v
->location
== &XEXP (x
, 0))
8610 /* See if any subexpression fails elimination. */
8611 fmt
= GET_RTX_FORMAT (code
);
8612 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
8617 if (! maybe_eliminate_biv_1 (XEXP (x
, i
), insn
, bl
,
8618 eliminate_p
, where
))
8623 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
8624 if (! maybe_eliminate_biv_1 (XVECEXP (x
, i
, j
), insn
, bl
,
8625 eliminate_p
, where
))
8634 /* Return nonzero if the last use of REG
8635 is in an insn following INSN in the same basic block. */
8638 last_use_this_basic_block (reg
, insn
)
8644 n
&& GET_CODE (n
) != CODE_LABEL
&& GET_CODE (n
) != JUMP_INSN
;
8647 if (REGNO_LAST_UID (REGNO (reg
)) == INSN_UID (n
))
8653 /* Called via `note_stores' to record the initial value of a biv. Here we
8654 just record the location of the set and process it later. */
8657 record_initial (dest
, set
)
8661 struct iv_class
*bl
;
8663 if (GET_CODE (dest
) != REG
8664 || REGNO (dest
) >= max_reg_before_loop
8665 || REG_IV_TYPE (REGNO (dest
)) != BASIC_INDUCT
)
8668 bl
= reg_biv_class
[REGNO (dest
)];
8670 /* If this is the first set found, record it. */
8671 if (bl
->init_insn
== 0)
8673 bl
->init_insn
= note_insn
;
8678 /* If any of the registers in X are "old" and currently have a last use earlier
8679 than INSN, update them to have a last use of INSN. Their actual last use
8680 will be the previous insn but it will not have a valid uid_luid so we can't
8684 update_reg_last_use (x
, insn
)
8688 /* Check for the case where INSN does not have a valid luid. In this case,
8689 there is no need to modify the regno_last_uid, as this can only happen
8690 when code is inserted after the loop_end to set a pseudo's final value,
8691 and hence this insn will never be the last use of x. */
8692 if (GET_CODE (x
) == REG
&& REGNO (x
) < max_reg_before_loop
8693 && INSN_UID (insn
) < max_uid_for_loop
8694 && uid_luid
[REGNO_LAST_UID (REGNO (x
))] < uid_luid
[INSN_UID (insn
)])
8695 REGNO_LAST_UID (REGNO (x
)) = INSN_UID (insn
);
8699 register char *fmt
= GET_RTX_FORMAT (GET_CODE (x
));
8700 for (i
= GET_RTX_LENGTH (GET_CODE (x
)) - 1; i
>= 0; i
--)
8703 update_reg_last_use (XEXP (x
, i
), insn
);
8704 else if (fmt
[i
] == 'E')
8705 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
8706 update_reg_last_use (XVECEXP (x
, i
, j
), insn
);
8711 /* Given a jump insn JUMP, return the condition that will cause it to branch
8712 to its JUMP_LABEL. If the condition cannot be understood, or is an
8713 inequality floating-point comparison which needs to be reversed, 0 will
8716 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8717 insn used in locating the condition was found. If a replacement test
8718 of the condition is desired, it should be placed in front of that
8719 insn and we will be sure that the inputs are still valid.
8721 The condition will be returned in a canonical form to simplify testing by
8722 callers. Specifically:
8724 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
8725 (2) Both operands will be machine operands; (cc0) will have been replaced.
8726 (3) If an operand is a constant, it will be the second operand.
8727 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
8728 for GE, GEU, and LEU. */
8731 get_condition (jump
, earliest
)
8740 int reverse_code
= 0;
8741 int did_reverse_condition
= 0;
8742 enum machine_mode mode
;
8744 /* If this is not a standard conditional jump, we can't parse it. */
8745 if (GET_CODE (jump
) != JUMP_INSN
8746 || ! condjump_p (jump
) || simplejump_p (jump
))
8749 code
= GET_CODE (XEXP (SET_SRC (PATTERN (jump
)), 0));
8750 mode
= GET_MODE (XEXP (SET_SRC (PATTERN (jump
)), 0));
8751 op0
= XEXP (XEXP (SET_SRC (PATTERN (jump
)), 0), 0);
8752 op1
= XEXP (XEXP (SET_SRC (PATTERN (jump
)), 0), 1);
8757 /* If this branches to JUMP_LABEL when the condition is false, reverse
8759 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump
)), 2)) == LABEL_REF
8760 && XEXP (XEXP (SET_SRC (PATTERN (jump
)), 2), 0) == JUMP_LABEL (jump
))
8761 code
= reverse_condition (code
), did_reverse_condition
^= 1;
8763 /* If we are comparing a register with zero, see if the register is set
8764 in the previous insn to a COMPARE or a comparison operation. Perform
8765 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
8768 while (GET_RTX_CLASS (code
) == '<' && op1
== CONST0_RTX (GET_MODE (op0
)))
8770 /* Set non-zero when we find something of interest. */
8774 /* If comparison with cc0, import actual comparison from compare
8778 if ((prev
= prev_nonnote_insn (prev
)) == 0
8779 || GET_CODE (prev
) != INSN
8780 || (set
= single_set (prev
)) == 0
8781 || SET_DEST (set
) != cc0_rtx
)
8784 op0
= SET_SRC (set
);
8785 op1
= CONST0_RTX (GET_MODE (op0
));
8791 /* If this is a COMPARE, pick up the two things being compared. */
8792 if (GET_CODE (op0
) == COMPARE
)
8794 op1
= XEXP (op0
, 1);
8795 op0
= XEXP (op0
, 0);
8798 else if (GET_CODE (op0
) != REG
)
8801 /* Go back to the previous insn. Stop if it is not an INSN. We also
8802 stop if it isn't a single set or if it has a REG_INC note because
8803 we don't want to bother dealing with it. */
8805 if ((prev
= prev_nonnote_insn (prev
)) == 0
8806 || GET_CODE (prev
) != INSN
8807 || FIND_REG_INC_NOTE (prev
, 0)
8808 || (set
= single_set (prev
)) == 0)
8811 /* If this is setting OP0, get what it sets it to if it looks
8813 if (rtx_equal_p (SET_DEST (set
), op0
))
8815 enum machine_mode inner_mode
= GET_MODE (SET_SRC (set
));
8817 /* ??? We may not combine comparisons done in a CCmode with
8818 comparisons not done in a CCmode. This is to aid targets
8819 like Alpha that have an IEEE compliant EQ instruction, and
8820 a non-IEEE compliant BEQ instruction. The use of CCmode is
8821 actually artificial, simply to prevent the combination, but
8822 should not affect other platforms.
8824 However, we must allow VOIDmode comparisons to match either
8825 CCmode or non-CCmode comparison, because some ports have
8826 modeless comparisons inside branch patterns.
8828 ??? This mode check should perhaps look more like the mode check
8829 in simplify_comparison in combine. */
8831 if ((GET_CODE (SET_SRC (set
)) == COMPARE
8834 && GET_MODE_CLASS (inner_mode
) == MODE_INT
8835 && (GET_MODE_BITSIZE (inner_mode
)
8836 <= HOST_BITS_PER_WIDE_INT
)
8837 && (STORE_FLAG_VALUE
8838 & ((HOST_WIDE_INT
) 1
8839 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
8840 #ifdef FLOAT_STORE_FLAG_VALUE
8842 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
8843 && FLOAT_STORE_FLAG_VALUE
< 0)
8846 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<'))
8847 && (((GET_MODE_CLASS (mode
) == MODE_CC
)
8848 == (GET_MODE_CLASS (inner_mode
) == MODE_CC
))
8849 || mode
== VOIDmode
|| inner_mode
== VOIDmode
))
8851 else if (((code
== EQ
8853 && (GET_MODE_BITSIZE (inner_mode
)
8854 <= HOST_BITS_PER_WIDE_INT
)
8855 && GET_MODE_CLASS (inner_mode
) == MODE_INT
8856 && (STORE_FLAG_VALUE
8857 & ((HOST_WIDE_INT
) 1
8858 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
8859 #ifdef FLOAT_STORE_FLAG_VALUE
8861 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
8862 && FLOAT_STORE_FLAG_VALUE
< 0)
8865 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<'
8866 && (((GET_MODE_CLASS (mode
) == MODE_CC
)
8867 == (GET_MODE_CLASS (inner_mode
) == MODE_CC
))
8868 || mode
== VOIDmode
|| inner_mode
== VOIDmode
))
8871 /* We might have reversed a LT to get a GE here. But this wasn't
8872 actually the comparison of data, so we don't flag that we
8873 have had to reverse the condition. */
8874 did_reverse_condition
^= 1;
8882 else if (reg_set_p (op0
, prev
))
8883 /* If this sets OP0, but not directly, we have to give up. */
8888 if (GET_RTX_CLASS (GET_CODE (x
)) == '<')
8889 code
= GET_CODE (x
);
8892 code
= reverse_condition (code
);
8893 did_reverse_condition
^= 1;
8897 op0
= XEXP (x
, 0), op1
= XEXP (x
, 1);
8903 /* If constant is first, put it last. */
8904 if (CONSTANT_P (op0
))
8905 code
= swap_condition (code
), tem
= op0
, op0
= op1
, op1
= tem
;
8907 /* If OP0 is the result of a comparison, we weren't able to find what
8908 was really being compared, so fail. */
8909 if (GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
)
8912 /* Canonicalize any ordered comparison with integers involving equality
8913 if we can do computations in the relevant mode and we do not
8916 if (GET_CODE (op1
) == CONST_INT
8917 && GET_MODE (op0
) != VOIDmode
8918 && GET_MODE_BITSIZE (GET_MODE (op0
)) <= HOST_BITS_PER_WIDE_INT
)
8920 HOST_WIDE_INT const_val
= INTVAL (op1
);
8921 unsigned HOST_WIDE_INT uconst_val
= const_val
;
8922 unsigned HOST_WIDE_INT max_val
8923 = (unsigned HOST_WIDE_INT
) GET_MODE_MASK (GET_MODE (op0
));
8928 if ((unsigned HOST_WIDE_INT
) const_val
!= max_val
>> 1)
8929 code
= LT
, op1
= GEN_INT (const_val
+ 1);
8932 /* When cross-compiling, const_val might be sign-extended from
8933 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
8935 if ((HOST_WIDE_INT
) (const_val
& max_val
)
8936 != (((HOST_WIDE_INT
) 1
8937 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
8938 code
= GT
, op1
= GEN_INT (const_val
- 1);
8942 if (uconst_val
< max_val
)
8943 code
= LTU
, op1
= GEN_INT (uconst_val
+ 1);
8947 if (uconst_val
!= 0)
8948 code
= GTU
, op1
= GEN_INT (uconst_val
- 1);
8956 /* If this was floating-point and we reversed anything other than an
8957 EQ or NE, return zero. */
8958 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
8959 && did_reverse_condition
&& code
!= NE
&& code
!= EQ
8961 && GET_MODE_CLASS (GET_MODE (op0
)) == MODE_FLOAT
)
8965 /* Never return CC0; return zero instead. */
8970 return gen_rtx_fmt_ee (code
, VOIDmode
, op0
, op1
);
8973 /* Similar to above routine, except that we also put an invariant last
8974 unless both operands are invariants. */
8977 get_condition_for_loop (x
)
8980 rtx comparison
= get_condition (x
, NULL_PTR
);
8983 || ! invariant_p (XEXP (comparison
, 0))
8984 || invariant_p (XEXP (comparison
, 1)))
8987 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison
)), VOIDmode
,
8988 XEXP (comparison
, 1), XEXP (comparison
, 0));
8991 #ifdef HAVE_decrement_and_branch_on_count
8992 /* Instrument loop for insertion of bct instruction. We distinguish between
8993 loops with compile-time bounds and those with run-time bounds.
8994 Information from loop_iterations() is used to compute compile-time bounds.
8995 Run-time bounds should use loop preconditioning, but currently ignored.
8999 insert_bct (loop_start
, loop_end
, loop_info
)
9000 rtx loop_start
, loop_end
;
9001 struct loop_info
*loop_info
;
9004 unsigned HOST_WIDE_INT n_iterations
;
9006 int increment_direction
, compare_direction
;
9008 /* If the loop condition is <= or >=, the number of iteration
9009 is 1 more than the range of the bounds of the loop. */
9010 int add_iteration
= 0;
9012 enum machine_mode loop_var_mode
= word_mode
;
9014 int loop_num
= uid_loop_num
[INSN_UID (loop_start
)];
9016 /* It's impossible to instrument a competely unrolled loop. */
9017 if (loop_info
->unroll_number
== -1)
9020 /* Make sure that the count register is not in use. */
9021 if (loop_used_count_register
[loop_num
])
9023 if (loop_dump_stream
)
9024 fprintf (loop_dump_stream
,
9025 "insert_bct %d: BCT instrumentation failed: count register already in use\n",
9030 /* Make sure that the function has no indirect jumps. */
9031 if (indirect_jump_in_function
)
9033 if (loop_dump_stream
)
9034 fprintf (loop_dump_stream
,
9035 "insert_bct %d: BCT instrumentation failed: indirect jump in function\n",
9040 /* Make sure that the last loop insn is a conditional jump. */
9041 if (GET_CODE (PREV_INSN (loop_end
)) != JUMP_INSN
9042 || ! condjump_p (PREV_INSN (loop_end
))
9043 || simplejump_p (PREV_INSN (loop_end
)))
9045 if (loop_dump_stream
)
9046 fprintf (loop_dump_stream
,
9047 "insert_bct %d: BCT instrumentation failed: invalid jump at loop end\n",
9052 /* Make sure that the loop does not contain a function call
9053 (the count register might be altered by the called function). */
9056 if (loop_dump_stream
)
9057 fprintf (loop_dump_stream
,
9058 "insert_bct %d: BCT instrumentation failed: function call in loop\n",
9063 /* Make sure that the loop does not jump via a table.
9064 (the count register might be used to perform the branch on table). */
9065 if (loop_has_tablejump
)
9067 if (loop_dump_stream
)
9068 fprintf (loop_dump_stream
,
9069 "insert_bct %d: BCT instrumentation failed: computed branch in the loop\n",
9074 /* Account for loop unrolling in instrumented iteration count. */
9075 if (loop_info
->unroll_number
> 1)
9076 n_iterations
= loop_info
->n_iterations
/ loop_info
->unroll_number
;
9078 n_iterations
= loop_info
->n_iterations
;
9080 if (n_iterations
!= 0 && n_iterations
< 3)
9082 /* Allow an enclosing outer loop to benefit if possible. */
9083 if (loop_dump_stream
)
9084 fprintf (loop_dump_stream
,
9085 "insert_bct %d: Too few iterations to benefit from BCT optimization\n",
9090 /* Try to instrument the loop. */
9092 /* Handle the simpler case, where the bounds are known at compile time. */
9093 if (n_iterations
> 0)
9095 /* Mark all enclosing loops that they cannot use count register. */
9096 for (i
= loop_num
; i
!= -1; i
= loop_outer_loop
[i
])
9097 loop_used_count_register
[i
] = 1;
9098 instrument_loop_bct (loop_start
, loop_end
, GEN_INT (n_iterations
));
9102 /* Handle the more complex case, that the bounds are NOT known
9103 at compile time. In this case we generate run_time calculation
9104 of the number of iterations. */
9106 if (loop_info
->iteration_var
== 0)
9108 if (loop_dump_stream
)
9109 fprintf (loop_dump_stream
,
9110 "insert_bct %d: BCT Runtime Instrumentation failed: no loop iteration variable found\n",
9115 if (GET_MODE_CLASS (GET_MODE (loop_info
->iteration_var
)) != MODE_INT
9116 || GET_MODE_SIZE (GET_MODE (loop_info
->iteration_var
)) != UNITS_PER_WORD
)
9118 if (loop_dump_stream
)
9119 fprintf (loop_dump_stream
,
9120 "insert_bct %d: BCT Runtime Instrumentation failed: loop variable not integer\n",
9125 /* With runtime bounds, if the compare is of the form '!=' we give up */
9126 if (loop_info
->comparison_code
== NE
)
9128 if (loop_dump_stream
)
9129 fprintf (loop_dump_stream
,
9130 "insert_bct %d: BCT Runtime Instrumentation failed: runtime bounds with != comparison\n",
9134 /* Use common loop preconditioning code instead. */
9138 /* We rely on the existence of run-time guard to ensure that the
9139 loop executes at least once. */
9141 rtx iterations_num_reg
;
9143 unsigned HOST_WIDE_INT increment_value_abs
9144 = INTVAL (increment
) * increment_direction
;
9146 /* make sure that the increment is a power of two, otherwise (an
9147 expensive) divide is needed. */
9148 if (exact_log2 (increment_value_abs
) == -1)
9150 if (loop_dump_stream
)
9151 fprintf (loop_dump_stream
,
9152 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
9156 /* compute the number of iterations */
9161 /* Again, the number of iterations is calculated by:
9163 ; compare-val - initial-val + (increment -1) + additional-iteration
9164 ; num_iterations = -----------------------------------------------------------------
9167 /* ??? Do we have to call copy_rtx here before passing rtx to
9169 if (compare_direction
> 0)
9171 /* <, <= :the loop variable is increasing */
9172 temp_reg
= expand_binop (loop_var_mode
, sub_optab
,
9173 comparison_value
, initial_value
,
9174 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
9178 temp_reg
= expand_binop (loop_var_mode
, sub_optab
,
9179 initial_value
, comparison_value
,
9180 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
9183 if (increment_value_abs
- 1 + add_iteration
!= 0)
9184 temp_reg
= expand_binop (loop_var_mode
, add_optab
, temp_reg
,
9185 GEN_INT (increment_value_abs
- 1
9187 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
9189 if (increment_value_abs
!= 1)
9191 /* ??? This will generate an expensive divide instruction for
9192 most targets. The original authors apparently expected this
9193 to be a shift, since they test for power-of-2 divisors above,
9194 but just naively generating a divide instruction will not give
9195 a shift. It happens to work for the PowerPC target because
9196 the rs6000.md file has a divide pattern that emits shifts.
9197 It will probably not work for any other target. */
9198 iterations_num_reg
= expand_binop (loop_var_mode
, sdiv_optab
,
9200 GEN_INT (increment_value_abs
),
9201 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
9204 iterations_num_reg
= temp_reg
;
9206 sequence
= gen_sequence ();
9208 emit_insn_before (sequence
, loop_start
);
9209 instrument_loop_bct (loop_start
, loop_end
, iterations_num_reg
);
9213 #endif /* Complex case */
9216 /* Instrument loop by inserting a bct in it as follows:
9217 1. A new counter register is created.
9218 2. In the head of the loop the new variable is initialized to the value
9219 passed in the loop_num_iterations parameter.
9220 3. At the end of the loop, comparison of the register with 0 is generated.
9221 The created comparison follows the pattern defined for the
9222 decrement_and_branch_on_count insn, so this insn will be generated.
9223 4. The branch on the old variable are deleted. The compare must remain
9224 because it might be used elsewhere. If the loop-variable or condition
9225 register are used elsewhere, they will be eliminated by flow. */
9228 instrument_loop_bct (loop_start
, loop_end
, loop_num_iterations
)
9229 rtx loop_start
, loop_end
;
9230 rtx loop_num_iterations
;
9236 if (HAVE_decrement_and_branch_on_count
)
9238 if (loop_dump_stream
)
9240 fputs ("instrument_bct: Inserting BCT (", loop_dump_stream
);
9241 if (GET_CODE (loop_num_iterations
) == CONST_INT
)
9242 fprintf (loop_dump_stream
, HOST_WIDE_INT_PRINT_DEC
,
9243 INTVAL (loop_num_iterations
));
9245 fputs ("runtime", loop_dump_stream
);
9246 fputs (" iterations)", loop_dump_stream
);
9249 /* Discard original jump to continue loop. Original compare result
9250 may still be live, so it cannot be discarded explicitly. */
9251 delete_insn (PREV_INSN (loop_end
));
9253 /* Insert the label which will delimit the start of the loop. */
9254 start_label
= gen_label_rtx ();
9255 emit_label_after (start_label
, loop_start
);
9257 /* Insert initialization of the count register into the loop header. */
9259 counter_reg
= gen_reg_rtx (word_mode
);
9260 emit_insn (gen_move_insn (counter_reg
, loop_num_iterations
));
9261 sequence
= gen_sequence ();
9263 emit_insn_before (sequence
, loop_start
);
9265 /* Insert new comparison on the count register instead of the
9266 old one, generating the needed BCT pattern (that will be
9267 later recognized by assembly generation phase). */
9268 emit_jump_insn_before (gen_decrement_and_branch_on_count (counter_reg
,
9271 LABEL_NUSES (start_label
)++;
9275 #endif /* HAVE_decrement_and_branch_on_count */
9277 /* Scan the function and determine whether it has indirect (computed) jumps.
9279 This is taken mostly from flow.c; similar code exists elsewhere
9280 in the compiler. It may be useful to put this into rtlanal.c. */
9282 indirect_jump_in_function_p (start
)
9287 for (insn
= start
; insn
; insn
= NEXT_INSN (insn
))
9288 if (computed_jump_p (insn
))
9294 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9295 documentation for LOOP_MEMS for the definition of `appropriate'.
9296 This function is called from prescan_loop via for_each_rtx. */
9299 insert_loop_mem (mem
, data
)
9301 void *data ATTRIBUTE_UNUSED
;
9309 switch (GET_CODE (m
))
9315 /* We're not interested in the MEM associated with a
9316 CONST_DOUBLE, so there's no need to traverse into this. */
9320 /* This is not a MEM. */
9324 /* See if we've already seen this MEM. */
9325 for (i
= 0; i
< loop_mems_idx
; ++i
)
9326 if (rtx_equal_p (m
, loop_mems
[i
].mem
))
9328 if (GET_MODE (m
) != GET_MODE (loop_mems
[i
].mem
))
9329 /* The modes of the two memory accesses are different. If
9330 this happens, something tricky is going on, and we just
9331 don't optimize accesses to this MEM. */
9332 loop_mems
[i
].optimize
= 0;
9337 /* Resize the array, if necessary. */
9338 if (loop_mems_idx
== loop_mems_allocated
)
9340 if (loop_mems_allocated
!= 0)
9341 loop_mems_allocated
*= 2;
9343 loop_mems_allocated
= 32;
9345 loop_mems
= (loop_mem_info
*)
9346 xrealloc (loop_mems
,
9347 loop_mems_allocated
* sizeof (loop_mem_info
));
9350 /* Actually insert the MEM. */
9351 loop_mems
[loop_mems_idx
].mem
= m
;
9352 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9353 because we can't put it in a register. We still store it in the
9354 table, though, so that if we see the same address later, but in a
9355 non-BLK mode, we'll not think we can optimize it at that point. */
9356 loop_mems
[loop_mems_idx
].optimize
= (GET_MODE (m
) != BLKmode
);
9357 loop_mems
[loop_mems_idx
].reg
= NULL_RTX
;
9363 /* Like load_mems, but also ensures that SET_IN_LOOP,
9364 MAY_NOT_OPTIMIZE, REG_SINGLE_USAGE, and INSN_COUNT have the correct
9365 values after load_mems. */
9368 load_mems_and_recount_loop_regs_set (scan_start
, end
, loop_top
, start
,
9376 int nregs
= max_reg_num ();
9378 load_mems (scan_start
, end
, loop_top
, start
);
9380 /* Recalculate set_in_loop and friends since load_mems may have
9381 created new registers. */
9382 if (max_reg_num () > nregs
)
9388 nregs
= max_reg_num ();
9390 if ((unsigned) nregs
> set_in_loop
->num_elements
)
9392 /* Grow all the arrays. */
9393 VARRAY_GROW (set_in_loop
, nregs
);
9394 VARRAY_GROW (n_times_set
, nregs
);
9395 VARRAY_GROW (may_not_optimize
, nregs
);
9396 VARRAY_GROW (reg_single_usage
, nregs
);
9398 /* Clear the arrays */
9399 bzero ((char *) &set_in_loop
->data
, nregs
* sizeof (int));
9400 bzero ((char *) &may_not_optimize
->data
, nregs
* sizeof (char));
9401 bzero ((char *) ®_single_usage
->data
, nregs
* sizeof (rtx
));
9403 count_loop_regs_set (loop_top
? loop_top
: start
, end
,
9404 may_not_optimize
, reg_single_usage
,
9407 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
9409 VARRAY_CHAR (may_not_optimize
, i
) = 1;
9410 VARRAY_INT (set_in_loop
, i
) = 1;
9413 #ifdef AVOID_CCMODE_COPIES
9414 /* Don't try to move insns which set CC registers if we should not
9415 create CCmode register copies. */
9416 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
9417 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx
[i
])) == MODE_CC
)
9418 VARRAY_CHAR (may_not_optimize
, i
) = 1;
9421 /* Set n_times_set for the new registers. */
9422 bcopy ((char *) (&set_in_loop
->data
.i
[0] + old_nregs
),
9423 (char *) (&n_times_set
->data
.i
[0] + old_nregs
),
9424 (nregs
- old_nregs
) * sizeof (int));
9428 /* Move MEMs into registers for the duration of the loop. SCAN_START
9429 is the first instruction in the loop (as it is executed). The
9430 other parameters are as for next_insn_in_loop. */
9433 load_mems (scan_start
, end
, loop_top
, start
)
9439 int maybe_never
= 0;
9442 rtx label
= NULL_RTX
;
9445 if (loop_mems_idx
> 0)
9447 /* Nonzero if the next instruction may never be executed. */
9448 int next_maybe_never
= 0;
9450 /* Check to see if it's possible that some instructions in the
9451 loop are never executed. */
9452 for (p
= next_insn_in_loop (scan_start
, scan_start
, end
, loop_top
);
9453 p
!= NULL_RTX
&& !maybe_never
;
9454 p
= next_insn_in_loop (p
, scan_start
, end
, loop_top
))
9456 if (GET_CODE (p
) == CODE_LABEL
)
9458 else if (GET_CODE (p
) == JUMP_INSN
9459 /* If we enter the loop in the middle, and scan
9460 around to the beginning, don't set maybe_never
9461 for that. This must be an unconditional jump,
9462 otherwise the code at the top of the loop might
9463 never be executed. Unconditional jumps are
9464 followed a by barrier then loop end. */
9465 && ! (GET_CODE (p
) == JUMP_INSN
9466 && JUMP_LABEL (p
) == loop_top
9467 && NEXT_INSN (NEXT_INSN (p
)) == end
9468 && simplejump_p (p
)))
9470 if (!condjump_p (p
))
9471 /* Something complicated. */
9474 /* If there are any more instructions in the loop, they
9475 might not be reached. */
9476 next_maybe_never
= 1;
9478 else if (next_maybe_never
)
9482 /* Actually move the MEMs. */
9483 for (i
= 0; i
< loop_mems_idx
; ++i
)
9487 rtx mem
= loop_mems
[i
].mem
;
9490 if (MEM_VOLATILE_P (mem
)
9491 || invariant_p (XEXP (mem
, 0)) != 1)
9492 /* There's no telling whether or not MEM is modified. */
9493 loop_mems
[i
].optimize
= 0;
9495 /* Go through the MEMs written to in the loop to see if this
9496 one is aliased by one of them. */
9497 mem_list_entry
= loop_store_mems
;
9498 while (mem_list_entry
)
9500 if (rtx_equal_p (mem
, XEXP (mem_list_entry
, 0)))
9502 else if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
9505 /* MEM is indeed aliased by this store. */
9506 loop_mems
[i
].optimize
= 0;
9509 mem_list_entry
= XEXP (mem_list_entry
, 1);
9512 /* If this MEM is written to, we must be sure that there
9513 are no reads from another MEM that aliases this one. */
9514 if (loop_mems
[i
].optimize
&& written
)
9518 for (j
= 0; j
< loop_mems_idx
; ++j
)
9522 else if (true_dependence (mem
,
9527 /* It's not safe to hoist loop_mems[i] out of
9528 the loop because writes to it might not be
9529 seen by reads from loop_mems[j]. */
9530 loop_mems
[i
].optimize
= 0;
9536 if (maybe_never
&& may_trap_p (mem
))
9537 /* We can't access the MEM outside the loop; it might
9538 cause a trap that wouldn't have happened otherwise. */
9539 loop_mems
[i
].optimize
= 0;
9541 if (!loop_mems
[i
].optimize
)
9542 /* We thought we were going to lift this MEM out of the
9543 loop, but later discovered that we could not. */
9546 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9547 order to keep scan_loop from moving stores to this MEM
9548 out of the loop just because this REG is neither a
9549 user-variable nor used in the loop test. */
9550 reg
= gen_reg_rtx (GET_MODE (mem
));
9551 REG_USERVAR_P (reg
) = 1;
9552 loop_mems
[i
].reg
= reg
;
9554 /* Now, replace all references to the MEM with the
9555 corresponding pesudos. */
9556 for (p
= next_insn_in_loop (scan_start
, scan_start
, end
, loop_top
);
9558 p
= next_insn_in_loop (p
, scan_start
, end
, loop_top
))
9563 for_each_rtx (&p
, replace_loop_mem
, &ri
);
9566 if (!apply_change_group ())
9567 /* We couldn't replace all occurrences of the MEM. */
9568 loop_mems
[i
].optimize
= 0;
9573 /* Load the memory immediately before START, which is
9574 the NOTE_LOOP_BEG. */
9575 set
= gen_move_insn (reg
, mem
);
9576 emit_insn_before (set
, start
);
9580 if (label
== NULL_RTX
)
9582 /* We must compute the former
9583 right-after-the-end label before we insert
9585 end_label
= next_label (end
);
9586 label
= gen_label_rtx ();
9587 emit_label_after (label
, end
);
9590 /* Store the memory immediately after END, which is
9591 the NOTE_LOOP_END. */
9592 set
= gen_move_insn (copy_rtx (mem
), reg
);
9593 emit_insn_after (set
, label
);
9596 if (loop_dump_stream
)
9598 fprintf (loop_dump_stream
, "Hoisted regno %d %s from ",
9599 REGNO (reg
), (written
? "r/w" : "r/o"));
9600 print_rtl (loop_dump_stream
, mem
);
9601 fputc ('\n', loop_dump_stream
);
9607 if (label
!= NULL_RTX
)
9609 /* Now, we need to replace all references to the previous exit
9610 label with the new one. */
9615 for (p
= start
; p
!= end
; p
= NEXT_INSN (p
))
9617 for_each_rtx (&p
, replace_label
, &rr
);
9619 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
9620 field. This is not handled by for_each_rtx because it doesn't
9621 handle unprinted ('0') fields. We need to update JUMP_LABEL
9622 because the immediately following unroll pass will use it.
9623 replace_label would not work anyways, because that only handles
9625 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == end_label
)
9626 JUMP_LABEL (p
) = label
;
9631 /* Replace MEM with its associated pseudo register. This function is
9632 called from load_mems via for_each_rtx. DATA is actually an
9633 rtx_and_int * describing the instruction currently being scanned
9634 and the MEM we are currently replacing. */
9637 replace_loop_mem (mem
, data
)
9649 switch (GET_CODE (m
))
9655 /* We're not interested in the MEM associated with a
9656 CONST_DOUBLE, so there's no need to traverse into one. */
9660 /* This is not a MEM. */
9664 ri
= (rtx_and_int
*) data
;
9667 if (!rtx_equal_p (loop_mems
[i
].mem
, m
))
9668 /* This is not the MEM we are currently replacing. */
9673 /* Actually replace the MEM. */
9674 validate_change (insn
, mem
, loop_mems
[i
].reg
, 1);
9679 /* Replace occurrences of the old exit label for the loop with the new
9680 one. DATA is an rtx_pair containing the old and new labels,
9684 replace_label (x
, data
)
9689 rtx old_label
= ((rtx_pair
*) data
)->r1
;
9690 rtx new_label
= ((rtx_pair
*) data
)->r2
;
9695 if (GET_CODE (l
) != LABEL_REF
)
9698 if (XEXP (l
, 0) != old_label
)
9701 XEXP (l
, 0) = new_label
;
9702 ++LABEL_NUSES (new_label
);
9703 --LABEL_NUSES (old_label
);