1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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. */
39 #include "coretypes.h"
45 #include "hard-reg-set.h"
46 #include "basic-block.h"
47 #include "insn-config.h"
57 #include "insn-flags.h"
61 /* Not really meaningful values, but at least something. */
62 #ifndef SIMULTANEOUS_PREFETCHES
63 #define SIMULTANEOUS_PREFETCHES 3
65 #ifndef PREFETCH_BLOCK
66 #define PREFETCH_BLOCK 32
69 #define HAVE_prefetch 0
70 #define CODE_FOR_prefetch 0
71 #define gen_prefetch(a,b,c) (abort(), NULL_RTX)
74 /* Give up the prefetch optimizations once we exceed a given threshhold.
75 It is unlikely that we would be able to optimize something in a loop
76 with so many detected prefetches. */
77 #define MAX_PREFETCHES 100
78 /* The number of prefetch blocks that are beneficial to fetch at once before
79 a loop with a known (and low) iteration count. */
80 #define PREFETCH_BLOCKS_BEFORE_LOOP_MAX 6
81 /* For very tiny loops it is not worthwhile to prefetch even before the loop,
82 since it is likely that the data are already in the cache. */
83 #define PREFETCH_BLOCKS_BEFORE_LOOP_MIN 2
85 /* Parameterize some prefetch heuristics so they can be turned on and off
86 easily for performance testing on new architectures. These can be
87 defined in target-dependent files. */
89 /* Prefetch is worthwhile only when loads/stores are dense. */
90 #ifndef PREFETCH_ONLY_DENSE_MEM
91 #define PREFETCH_ONLY_DENSE_MEM 1
94 /* Define what we mean by "dense" loads and stores; This value divided by 256
95 is the minimum percentage of memory references that worth prefetching. */
96 #ifndef PREFETCH_DENSE_MEM
97 #define PREFETCH_DENSE_MEM 220
100 /* Do not prefetch for a loop whose iteration count is known to be low. */
101 #ifndef PREFETCH_NO_LOW_LOOPCNT
102 #define PREFETCH_NO_LOW_LOOPCNT 1
105 /* Define what we mean by a "low" iteration count. */
106 #ifndef PREFETCH_LOW_LOOPCNT
107 #define PREFETCH_LOW_LOOPCNT 32
110 /* Do not prefetch for a loop that contains a function call; such a loop is
111 probably not an internal loop. */
112 #ifndef PREFETCH_NO_CALL
113 #define PREFETCH_NO_CALL 1
116 /* Do not prefetch accesses with an extreme stride. */
117 #ifndef PREFETCH_NO_EXTREME_STRIDE
118 #define PREFETCH_NO_EXTREME_STRIDE 1
121 /* Define what we mean by an "extreme" stride. */
122 #ifndef PREFETCH_EXTREME_STRIDE
123 #define PREFETCH_EXTREME_STRIDE 4096
126 /* Define a limit to how far apart indices can be and still be merged
127 into a single prefetch. */
128 #ifndef PREFETCH_EXTREME_DIFFERENCE
129 #define PREFETCH_EXTREME_DIFFERENCE 4096
132 /* Issue prefetch instructions before the loop to fetch data to be used
133 in the first few loop iterations. */
134 #ifndef PREFETCH_BEFORE_LOOP
135 #define PREFETCH_BEFORE_LOOP 1
138 /* Do not handle reversed order prefetches (negative stride). */
139 #ifndef PREFETCH_NO_REVERSE_ORDER
140 #define PREFETCH_NO_REVERSE_ORDER 1
143 /* Prefetch even if the GIV is in conditional code. */
144 #ifndef PREFETCH_CONDITIONAL
145 #define PREFETCH_CONDITIONAL 1
148 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
149 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
151 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
152 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
153 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
155 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
156 ((REGNO) < FIRST_PSEUDO_REGISTER \
157 ? (int) HARD_REGNO_NREGS ((REGNO), GET_MODE (SET_DEST)) : 1)
160 /* Vector mapping INSN_UIDs to luids.
161 The luids are like uids but increase monotonically always.
162 We use them to see whether a jump comes from outside a given loop. */
166 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
167 number the insn is contained in. */
169 struct loop
**uid_loop
;
171 /* 1 + largest uid of any insn. */
173 int max_uid_for_loop
;
175 /* Number of loops detected in current function. Used as index to the
178 static int max_loop_num
;
180 /* Bound on pseudo register number before loop optimization.
181 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
182 unsigned int max_reg_before_loop
;
184 /* The value to pass to the next call of reg_scan_update. */
185 static int loop_max_reg
;
187 /* During the analysis of a loop, a chain of `struct movable's
188 is made to record all the movable insns found.
189 Then the entire chain can be scanned to decide which to move. */
193 rtx insn
; /* A movable insn */
194 rtx set_src
; /* The expression this reg is set from. */
195 rtx set_dest
; /* The destination of this SET. */
196 rtx dependencies
; /* When INSN is libcall, this is an EXPR_LIST
197 of any registers used within the LIBCALL. */
198 int consec
; /* Number of consecutive following insns
199 that must be moved with this one. */
200 unsigned int regno
; /* The register it sets */
201 short lifetime
; /* lifetime of that register;
202 may be adjusted when matching movables
203 that load the same value are found. */
204 short savings
; /* Number of insns we can move for this reg,
205 including other movables that force this
206 or match this one. */
207 ENUM_BITFIELD(machine_mode
) savemode
: 8; /* Nonzero means it is a mode for
208 a low part that we should avoid changing when
209 clearing the rest of the reg. */
210 unsigned int cond
: 1; /* 1 if only conditionally movable */
211 unsigned int force
: 1; /* 1 means MUST move this insn */
212 unsigned int global
: 1; /* 1 means reg is live outside this loop */
213 /* If PARTIAL is 1, GLOBAL means something different:
214 that the reg is live outside the range from where it is set
215 to the following label. */
216 unsigned int done
: 1; /* 1 inhibits further processing of this */
218 unsigned int partial
: 1; /* 1 means this reg is used for zero-extending.
219 In particular, moving it does not make it
221 unsigned int move_insn
: 1; /* 1 means that we call emit_move_insn to
222 load SRC, rather than copying INSN. */
223 unsigned int move_insn_first
:1;/* Same as above, if this is necessary for the
224 first insn of a consecutive sets group. */
225 unsigned int is_equiv
: 1; /* 1 means a REG_EQUIV is present on INSN. */
226 unsigned int insert_temp
: 1; /* 1 means we copy to a new pseudo and replace
227 the original insn with a copy from that
228 pseudo, rather than deleting it. */
229 struct movable
*match
; /* First entry for same value */
230 struct movable
*forces
; /* An insn that must be moved if this is */
231 struct movable
*next
;
235 FILE *loop_dump_stream
;
237 /* Forward declarations. */
239 static void invalidate_loops_containing_label
PARAMS ((rtx
));
240 static void find_and_verify_loops
PARAMS ((rtx
, struct loops
*));
241 static void mark_loop_jump
PARAMS ((rtx
, struct loop
*));
242 static void prescan_loop
PARAMS ((struct loop
*));
243 static int reg_in_basic_block_p
PARAMS ((rtx
, rtx
));
244 static int consec_sets_invariant_p
PARAMS ((const struct loop
*,
246 static int labels_in_range_p
PARAMS ((rtx
, int));
247 static void count_one_set
PARAMS ((struct loop_regs
*, rtx
, rtx
, rtx
*));
248 static void note_addr_stored
PARAMS ((rtx
, rtx
, void *));
249 static void note_set_pseudo_multiple_uses
PARAMS ((rtx
, rtx
, void *));
250 static int loop_reg_used_before_p
PARAMS ((const struct loop
*, rtx
, rtx
));
251 static void scan_loop
PARAMS ((struct loop
*, int));
253 static void replace_call_address
PARAMS ((rtx
, rtx
, rtx
));
255 static rtx skip_consec_insns
PARAMS ((rtx
, int));
256 static int libcall_benefit
PARAMS ((rtx
));
257 static void ignore_some_movables
PARAMS ((struct loop_movables
*));
258 static void force_movables
PARAMS ((struct loop_movables
*));
259 static void combine_movables
PARAMS ((struct loop_movables
*,
260 struct loop_regs
*));
261 static int num_unmoved_movables
PARAMS ((const struct loop
*));
262 static int regs_match_p
PARAMS ((rtx
, rtx
, struct loop_movables
*));
263 static int rtx_equal_for_loop_p
PARAMS ((rtx
, rtx
, struct loop_movables
*,
264 struct loop_regs
*));
265 static void add_label_notes
PARAMS ((rtx
, rtx
));
266 static void move_movables
PARAMS ((struct loop
*loop
, struct loop_movables
*,
268 static void loop_movables_add
PARAMS((struct loop_movables
*,
270 static void loop_movables_free
PARAMS((struct loop_movables
*));
271 static int count_nonfixed_reads
PARAMS ((const struct loop
*, rtx
));
272 static void loop_bivs_find
PARAMS((struct loop
*));
273 static void loop_bivs_init_find
PARAMS((struct loop
*));
274 static void loop_bivs_check
PARAMS((struct loop
*));
275 static void loop_givs_find
PARAMS((struct loop
*));
276 static void loop_givs_check
PARAMS((struct loop
*));
277 static int loop_biv_eliminable_p
PARAMS((struct loop
*, struct iv_class
*,
279 static int loop_giv_reduce_benefit
PARAMS((struct loop
*, struct iv_class
*,
280 struct induction
*, rtx
));
281 static void loop_givs_dead_check
PARAMS((struct loop
*, struct iv_class
*));
282 static void loop_givs_reduce
PARAMS((struct loop
*, struct iv_class
*));
283 static void loop_givs_rescan
PARAMS((struct loop
*, struct iv_class
*,
285 static void loop_ivs_free
PARAMS((struct loop
*));
286 static void strength_reduce
PARAMS ((struct loop
*, int));
287 static void find_single_use_in_loop
PARAMS ((struct loop_regs
*, rtx
, rtx
));
288 static int valid_initial_value_p
PARAMS ((rtx
, rtx
, int, rtx
));
289 static void find_mem_givs
PARAMS ((const struct loop
*, rtx
, rtx
, int, int));
290 static void record_biv
PARAMS ((struct loop
*, struct induction
*,
291 rtx
, rtx
, rtx
, rtx
, rtx
*,
293 static void check_final_value
PARAMS ((const struct loop
*,
294 struct induction
*));
295 static void loop_ivs_dump
PARAMS((const struct loop
*, FILE *, int));
296 static void loop_iv_class_dump
PARAMS((const struct iv_class
*, FILE *, int));
297 static void loop_biv_dump
PARAMS((const struct induction
*, FILE *, int));
298 static void loop_giv_dump
PARAMS((const struct induction
*, FILE *, int));
299 static void record_giv
PARAMS ((const struct loop
*, struct induction
*,
300 rtx
, rtx
, rtx
, rtx
, rtx
, rtx
, int,
301 enum g_types
, int, int, rtx
*));
302 static void update_giv_derive
PARAMS ((const struct loop
*, rtx
));
303 static void check_ext_dependent_givs
PARAMS ((struct iv_class
*,
304 struct loop_info
*));
305 static int basic_induction_var
PARAMS ((const struct loop
*, rtx
,
306 enum machine_mode
, rtx
, rtx
,
307 rtx
*, rtx
*, rtx
**));
308 static rtx simplify_giv_expr
PARAMS ((const struct loop
*, rtx
, rtx
*, int *));
309 static int general_induction_var
PARAMS ((const struct loop
*loop
, rtx
, rtx
*,
310 rtx
*, rtx
*, rtx
*, int, int *,
312 static int consec_sets_giv
PARAMS ((const struct loop
*, int, rtx
,
313 rtx
, rtx
, rtx
*, rtx
*, rtx
*, rtx
*));
314 static int check_dbra_loop
PARAMS ((struct loop
*, int));
315 static rtx express_from_1
PARAMS ((rtx
, rtx
, rtx
));
316 static rtx combine_givs_p
PARAMS ((struct induction
*, struct induction
*));
317 static int cmp_combine_givs_stats
PARAMS ((const PTR
, const PTR
));
318 static void combine_givs
PARAMS ((struct loop_regs
*, struct iv_class
*));
319 static int product_cheap_p
PARAMS ((rtx
, rtx
));
320 static int maybe_eliminate_biv
PARAMS ((const struct loop
*, struct iv_class
*,
322 static int maybe_eliminate_biv_1
PARAMS ((const struct loop
*, rtx
, rtx
,
323 struct iv_class
*, int,
325 static int last_use_this_basic_block
PARAMS ((rtx
, rtx
));
326 static void record_initial
PARAMS ((rtx
, rtx
, void *));
327 static void update_reg_last_use
PARAMS ((rtx
, rtx
));
328 static rtx next_insn_in_loop
PARAMS ((const struct loop
*, rtx
));
329 static void loop_regs_scan
PARAMS ((const struct loop
*, int));
330 static int count_insns_in_loop
PARAMS ((const struct loop
*));
331 static int find_mem_in_note_1
PARAMS ((rtx
*, void *));
332 static rtx find_mem_in_note
PARAMS ((rtx
));
333 static void load_mems
PARAMS ((const struct loop
*));
334 static int insert_loop_mem
PARAMS ((rtx
*, void *));
335 static int replace_loop_mem
PARAMS ((rtx
*, void *));
336 static void replace_loop_mems
PARAMS ((rtx
, rtx
, rtx
, int));
337 static int replace_loop_reg
PARAMS ((rtx
*, void *));
338 static void replace_loop_regs
PARAMS ((rtx insn
, rtx
, rtx
));
339 static void note_reg_stored
PARAMS ((rtx
, rtx
, void *));
340 static void try_copy_prop
PARAMS ((const struct loop
*, rtx
, unsigned int));
341 static void try_swap_copy_prop
PARAMS ((const struct loop
*, rtx
,
343 static rtx check_insn_for_givs
PARAMS((struct loop
*, rtx
, int, int));
344 static rtx check_insn_for_bivs
PARAMS((struct loop
*, rtx
, int, int));
345 static rtx gen_add_mult
PARAMS ((rtx
, rtx
, rtx
, rtx
));
346 static void loop_regs_update
PARAMS ((const struct loop
*, rtx
));
347 static int iv_add_mult_cost
PARAMS ((rtx
, rtx
, rtx
, rtx
));
349 static rtx loop_insn_emit_after
PARAMS((const struct loop
*, basic_block
,
351 static rtx loop_call_insn_emit_before
PARAMS((const struct loop
*,
352 basic_block
, rtx
, rtx
));
353 static rtx loop_call_insn_hoist
PARAMS((const struct loop
*, rtx
));
354 static rtx loop_insn_sink_or_swim
PARAMS((const struct loop
*, rtx
));
356 static void loop_dump_aux
PARAMS ((const struct loop
*, FILE *, int));
357 static void loop_delete_insns
PARAMS ((rtx
, rtx
));
358 static HOST_WIDE_INT remove_constant_addition
PARAMS ((rtx
*));
359 static rtx gen_load_of_final_value
PARAMS ((rtx
, rtx
));
360 void debug_ivs
PARAMS ((const struct loop
*));
361 void debug_iv_class
PARAMS ((const struct iv_class
*));
362 void debug_biv
PARAMS ((const struct induction
*));
363 void debug_giv
PARAMS ((const struct induction
*));
364 void debug_loop
PARAMS ((const struct loop
*));
365 void debug_loops
PARAMS ((const struct loops
*));
367 typedef struct loop_replace_args
374 /* Nonzero iff INSN is between START and END, inclusive. */
375 #define INSN_IN_RANGE_P(INSN, START, END) \
376 (INSN_UID (INSN) < max_uid_for_loop \
377 && INSN_LUID (INSN) >= INSN_LUID (START) \
378 && INSN_LUID (INSN) <= INSN_LUID (END))
380 /* Indirect_jump_in_function is computed once per function. */
381 static int indirect_jump_in_function
;
382 static int indirect_jump_in_function_p
PARAMS ((rtx
));
384 static int compute_luids
PARAMS ((rtx
, rtx
, int));
386 static int biv_elimination_giv_has_0_offset
PARAMS ((struct induction
*,
390 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
391 copy the value of the strength reduced giv to its original register. */
392 static int copy_cost
;
394 /* Cost of using a register, to normalize the benefits of a giv. */
395 static int reg_address_cost
;
400 rtx reg
= gen_rtx_REG (word_mode
, LAST_VIRTUAL_REGISTER
+ 1);
402 reg_address_cost
= address_cost (reg
, SImode
);
404 copy_cost
= COSTS_N_INSNS (1);
407 /* Compute the mapping from uids to luids.
408 LUIDs are numbers assigned to insns, like uids,
409 except that luids increase monotonically through the code.
410 Start at insn START and stop just before END. Assign LUIDs
411 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
413 compute_luids (start
, end
, prev_luid
)
420 for (insn
= start
, i
= prev_luid
; insn
!= end
; insn
= NEXT_INSN (insn
))
422 if (INSN_UID (insn
) >= max_uid_for_loop
)
424 /* Don't assign luids to line-number NOTEs, so that the distance in
425 luids between two insns is not affected by -g. */
426 if (GET_CODE (insn
) != NOTE
427 || NOTE_LINE_NUMBER (insn
) <= 0)
428 uid_luid
[INSN_UID (insn
)] = ++i
;
430 /* Give a line number note the same luid as preceding insn. */
431 uid_luid
[INSN_UID (insn
)] = i
;
436 /* Entry point of this file. Perform loop optimization
437 on the current function. F is the first insn of the function
438 and DUMPFILE is a stream for output of a trace of actions taken
439 (or 0 if none should be output). */
442 loop_optimize (f
, dumpfile
, flags
)
443 /* f is the first instruction of a chain of insns for one function */
450 struct loops loops_data
;
451 struct loops
*loops
= &loops_data
;
452 struct loop_info
*loops_info
;
454 loop_dump_stream
= dumpfile
;
456 init_recog_no_volatile ();
458 max_reg_before_loop
= max_reg_num ();
459 loop_max_reg
= max_reg_before_loop
;
463 /* Count the number of loops. */
466 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
468 if (GET_CODE (insn
) == NOTE
469 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
473 /* Don't waste time if no loops. */
474 if (max_loop_num
== 0)
477 loops
->num
= max_loop_num
;
479 /* Get size to use for tables indexed by uids.
480 Leave some space for labels allocated by find_and_verify_loops. */
481 max_uid_for_loop
= get_max_uid () + 1 + max_loop_num
* 32;
483 uid_luid
= (int *) xcalloc (max_uid_for_loop
, sizeof (int));
484 uid_loop
= (struct loop
**) xcalloc (max_uid_for_loop
,
485 sizeof (struct loop
*));
487 /* Allocate storage for array of loops. */
488 loops
->array
= (struct loop
*)
489 xcalloc (loops
->num
, sizeof (struct loop
));
491 /* Find and process each loop.
492 First, find them, and record them in order of their beginnings. */
493 find_and_verify_loops (f
, loops
);
495 /* Allocate and initialize auxiliary loop information. */
496 loops_info
= xcalloc (loops
->num
, sizeof (struct loop_info
));
497 for (i
= 0; i
< (int) loops
->num
; i
++)
498 loops
->array
[i
].aux
= loops_info
+ i
;
500 /* Now find all register lifetimes. This must be done after
501 find_and_verify_loops, because it might reorder the insns in the
503 reg_scan (f
, max_reg_before_loop
, 1);
505 /* This must occur after reg_scan so that registers created by gcse
506 will have entries in the register tables.
508 We could have added a call to reg_scan after gcse_main in toplev.c,
509 but moving this call to init_alias_analysis is more efficient. */
510 init_alias_analysis ();
512 /* See if we went too far. Note that get_max_uid already returns
513 one more that the maximum uid of all insn. */
514 if (get_max_uid () > max_uid_for_loop
)
516 /* Now reset it to the actual size we need. See above. */
517 max_uid_for_loop
= get_max_uid ();
519 /* find_and_verify_loops has already called compute_luids, but it
520 might have rearranged code afterwards, so we need to recompute
522 compute_luids (f
, NULL_RTX
, 0);
524 /* Don't leave gaps in uid_luid for insns that have been
525 deleted. It is possible that the first or last insn
526 using some register has been deleted by cross-jumping.
527 Make sure that uid_luid for that former insn's uid
528 points to the general area where that insn used to be. */
529 for (i
= 0; i
< max_uid_for_loop
; i
++)
531 uid_luid
[0] = uid_luid
[i
];
532 if (uid_luid
[0] != 0)
535 for (i
= 0; i
< max_uid_for_loop
; i
++)
536 if (uid_luid
[i
] == 0)
537 uid_luid
[i
] = uid_luid
[i
- 1];
539 /* Determine if the function has indirect jump. On some systems
540 this prevents low overhead loop instructions from being used. */
541 indirect_jump_in_function
= indirect_jump_in_function_p (f
);
543 /* Now scan the loops, last ones first, since this means inner ones are done
544 before outer ones. */
545 for (i
= max_loop_num
- 1; i
>= 0; i
--)
547 struct loop
*loop
= &loops
->array
[i
];
549 if (! loop
->invalid
&& loop
->end
)
550 scan_loop (loop
, flags
);
553 end_alias_analysis ();
562 /* Returns the next insn, in execution order, after INSN. START and
563 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
564 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
565 insn-stream; it is used with loops that are entered near the
569 next_insn_in_loop (loop
, insn
)
570 const struct loop
*loop
;
573 insn
= NEXT_INSN (insn
);
575 if (insn
== loop
->end
)
578 /* Go to the top of the loop, and continue there. */
585 if (insn
== loop
->scan_start
)
592 /* Optimize one loop described by LOOP. */
594 /* ??? Could also move memory writes out of loops if the destination address
595 is invariant, the source is invariant, the memory write is not volatile,
596 and if we can prove that no read inside the loop can read this address
597 before the write occurs. If there is a read of this address after the
598 write, then we can also mark the memory read as invariant. */
601 scan_loop (loop
, flags
)
605 struct loop_info
*loop_info
= LOOP_INFO (loop
);
606 struct loop_regs
*regs
= LOOP_REGS (loop
);
608 rtx loop_start
= loop
->start
;
609 rtx loop_end
= loop
->end
;
611 /* 1 if we are scanning insns that could be executed zero times. */
613 /* 1 if we are scanning insns that might never be executed
614 due to a subroutine call which might exit before they are reached. */
616 /* Number of insns in the loop. */
619 rtx temp
, update_start
, update_end
;
620 /* The SET from an insn, if it is the only SET in the insn. */
622 /* Chain describing insns movable in current loop. */
623 struct loop_movables
*movables
= LOOP_MOVABLES (loop
);
624 /* Ratio of extra register life span we can justify
625 for saving an instruction. More if loop doesn't call subroutines
626 since in that case saving an insn makes more difference
627 and more registers are available. */
629 /* Nonzero if we are scanning instructions in a sub-loop. */
638 /* Determine whether this loop starts with a jump down to a test at
639 the end. This will occur for a small number of loops with a test
640 that is too complex to duplicate in front of the loop.
642 We search for the first insn or label in the loop, skipping NOTEs.
643 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
644 (because we might have a loop executed only once that contains a
645 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
646 (in case we have a degenerate loop).
648 Note that if we mistakenly think that a loop is entered at the top
649 when, in fact, it is entered at the exit test, the only effect will be
650 slightly poorer optimization. Making the opposite error can generate
651 incorrect code. Since very few loops now start with a jump to the
652 exit test, the code here to detect that case is very conservative. */
654 for (p
= NEXT_INSN (loop_start
);
656 && GET_CODE (p
) != CODE_LABEL
&& ! INSN_P (p
)
657 && (GET_CODE (p
) != NOTE
658 || (NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_BEG
659 && NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_END
));
663 loop
->scan_start
= p
;
665 /* If loop end is the end of the current function, then emit a
666 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
667 note insn. This is the position we use when sinking insns out of
669 if (NEXT_INSN (loop
->end
) != 0)
670 loop
->sink
= NEXT_INSN (loop
->end
);
672 loop
->sink
= emit_note_after (NOTE_INSN_DELETED
, loop
->end
);
674 /* Set up variables describing this loop. */
676 threshold
= (loop_info
->has_call
? 1 : 2) * (1 + n_non_fixed_regs
);
678 /* If loop has a jump before the first label,
679 the true entry is the target of that jump.
680 Start scan from there.
681 But record in LOOP->TOP the place where the end-test jumps
682 back to so we can scan that after the end of the loop. */
683 if (GET_CODE (p
) == JUMP_INSN
684 /* Loop entry must be unconditional jump (and not a RETURN) */
685 && any_uncondjump_p (p
)
686 && JUMP_LABEL (p
) != 0
687 /* Check to see whether the jump actually
688 jumps out of the loop (meaning it's no loop).
689 This case can happen for things like
690 do {..} while (0). If this label was generated previously
691 by loop, we can't tell anything about it and have to reject
693 && INSN_IN_RANGE_P (JUMP_LABEL (p
), loop_start
, loop_end
))
695 loop
->top
= next_label (loop
->scan_start
);
696 loop
->scan_start
= JUMP_LABEL (p
);
699 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
700 as required by loop_reg_used_before_p. So skip such loops. (This
701 test may never be true, but it's best to play it safe.)
703 Also, skip loops where we do not start scanning at a label. This
704 test also rejects loops starting with a JUMP_INSN that failed the
707 if (INSN_UID (loop
->scan_start
) >= max_uid_for_loop
708 || GET_CODE (loop
->scan_start
) != CODE_LABEL
)
710 if (loop_dump_stream
)
711 fprintf (loop_dump_stream
, "\nLoop from %d to %d is phony.\n\n",
712 INSN_UID (loop_start
), INSN_UID (loop_end
));
716 /* Allocate extra space for REGs that might be created by load_mems.
717 We allocate a little extra slop as well, in the hopes that we
718 won't have to reallocate the regs array. */
719 loop_regs_scan (loop
, loop_info
->mems_idx
+ 16);
720 insn_count
= count_insns_in_loop (loop
);
722 if (loop_dump_stream
)
724 fprintf (loop_dump_stream
, "\nLoop from %d to %d: %d real insns.\n",
725 INSN_UID (loop_start
), INSN_UID (loop_end
), insn_count
);
727 fprintf (loop_dump_stream
, "Continue at insn %d.\n",
728 INSN_UID (loop
->cont
));
731 /* Scan through the loop finding insns that are safe to move.
732 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
733 this reg will be considered invariant for subsequent insns.
734 We consider whether subsequent insns use the reg
735 in deciding whether it is worth actually moving.
737 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
738 and therefore it is possible that the insns we are scanning
739 would never be executed. At such times, we must make sure
740 that it is safe to execute the insn once instead of zero times.
741 When MAYBE_NEVER is 0, all insns will be executed at least once
742 so that is not a problem. */
744 for (in_libcall
= 0, p
= next_insn_in_loop (loop
, loop
->scan_start
);
746 p
= next_insn_in_loop (loop
, p
))
748 if (in_libcall
&& INSN_P (p
) && find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
750 if (GET_CODE (p
) == INSN
)
752 temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
);
756 && (set
= single_set (p
))
757 && GET_CODE (SET_DEST (set
)) == REG
758 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
759 && SET_DEST (set
) != pic_offset_table_rtx
761 && ! regs
->array
[REGNO (SET_DEST (set
))].may_not_optimize
)
767 rtx src
= SET_SRC (set
);
768 rtx dependencies
= 0;
770 /* Figure out what to use as a source of this insn. If a
771 REG_EQUIV note is given or if a REG_EQUAL note with a
772 constant operand is specified, use it as the source and
773 mark that we should move this insn by calling
774 emit_move_insn rather that duplicating the insn.
776 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL
778 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
780 src
= XEXP (temp
, 0), move_insn
= 1;
783 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
784 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
785 src
= XEXP (temp
, 0), move_insn
= 1;
786 if (temp
&& find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
788 src
= XEXP (temp
, 0);
789 /* A libcall block can use regs that don't appear in
790 the equivalent expression. To move the libcall,
791 we must move those regs too. */
792 dependencies
= libcall_other_reg (p
, src
);
796 /* For parallels, add any possible uses to the dependencies, as
797 we can't move the insn without resolving them first. */
798 if (GET_CODE (PATTERN (p
)) == PARALLEL
)
800 for (i
= 0; i
< XVECLEN (PATTERN (p
), 0); i
++)
802 rtx x
= XVECEXP (PATTERN (p
), 0, i
);
803 if (GET_CODE (x
) == USE
)
805 = gen_rtx_EXPR_LIST (VOIDmode
, XEXP (x
, 0),
810 if (/* The register is used in basic blocks other
811 than the one where it is set (meaning that
812 something after this point in the loop might
813 depend on its value before the set). */
814 ! reg_in_basic_block_p (p
, SET_DEST (set
))
815 /* And the set is not guaranteed to be executed once
816 the loop starts, or the value before the set is
817 needed before the set occurs...
819 ??? Note we have quadratic behavior here, mitigated
820 by the fact that the previous test will often fail for
821 large loops. Rather than re-scanning the entire loop
822 each time for register usage, we should build tables
823 of the register usage and use them here instead. */
825 || loop_reg_used_before_p (loop
, set
, p
)))
826 /* It is unsafe to move the set. However, it may be OK to
827 move the source into a new pseudo, and substitute a
828 reg-to-reg copy for the original insn.
830 This code used to consider it OK to move a set of a variable
831 which was not created by the user and not used in an exit
833 That behavior is incorrect and was removed. */
836 /* Don't try to optimize a MODE_CC set with a constant
837 source. It probably will be combined with a conditional
839 if (GET_MODE_CLASS (GET_MODE (SET_DEST (set
))) == MODE_CC
842 /* Don't try to optimize a register that was made
843 by loop-optimization for an inner loop.
844 We don't know its life-span, so we can't compute
846 else if (REGNO (SET_DEST (set
)) >= max_reg_before_loop
)
848 /* Don't move the source and add a reg-to-reg copy:
849 - with -Os (this certainly increases size),
850 - if the mode doesn't support copy operations (obviously),
851 - if the source is already a reg (the motion will gain nothing),
852 - if the source is a legitimate constant (likewise). */
855 || ! can_copy_p (GET_MODE (SET_SRC (set
)))
856 || GET_CODE (SET_SRC (set
)) == REG
857 || (CONSTANT_P (SET_SRC (set
))
858 && LEGITIMATE_CONSTANT_P (SET_SRC (set
)))))
860 else if ((tem
= loop_invariant_p (loop
, src
))
861 && (dependencies
== 0
863 = loop_invariant_p (loop
, dependencies
)) != 0)
864 && (regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
== 1
866 = consec_sets_invariant_p
867 (loop
, SET_DEST (set
),
868 regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
,
870 /* If the insn can cause a trap (such as divide by zero),
871 can't move it unless it's guaranteed to be executed
872 once loop is entered. Even a function call might
873 prevent the trap insn from being reached
874 (since it might exit!) */
875 && ! ((maybe_never
|| call_passed
)
876 && may_trap_p (src
)))
879 int regno
= REGNO (SET_DEST (set
));
881 /* A potential lossage is where we have a case where two insns
882 can be combined as long as they are both in the loop, but
883 we move one of them outside the loop. For large loops,
884 this can lose. The most common case of this is the address
885 of a function being called.
887 Therefore, if this register is marked as being used
888 exactly once if we are in a loop with calls
889 (a "large loop"), see if we can replace the usage of
890 this register with the source of this SET. If we can,
893 Don't do this if P has a REG_RETVAL note or if we have
894 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
896 if (loop_info
->has_call
897 && regs
->array
[regno
].single_usage
!= 0
898 && regs
->array
[regno
].single_usage
!= const0_rtx
899 && REGNO_FIRST_UID (regno
) == INSN_UID (p
)
900 && (REGNO_LAST_UID (regno
)
901 == INSN_UID (regs
->array
[regno
].single_usage
))
902 && regs
->array
[regno
].set_in_loop
== 1
903 && GET_CODE (SET_SRC (set
)) != ASM_OPERANDS
904 && ! side_effects_p (SET_SRC (set
))
905 && ! find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
906 && (! SMALL_REGISTER_CLASSES
907 || (! (GET_CODE (SET_SRC (set
)) == REG
908 && (REGNO (SET_SRC (set
))
909 < FIRST_PSEUDO_REGISTER
))))
910 /* This test is not redundant; SET_SRC (set) might be
911 a call-clobbered register and the life of REGNO
912 might span a call. */
913 && ! modified_between_p (SET_SRC (set
), p
,
914 regs
->array
[regno
].single_usage
)
915 && no_labels_between_p (p
,
916 regs
->array
[regno
].single_usage
)
917 && validate_replace_rtx (SET_DEST (set
), SET_SRC (set
),
918 regs
->array
[regno
].single_usage
))
920 /* Replace any usage in a REG_EQUAL note. Must copy
921 the new source, so that we don't get rtx sharing
922 between the SET_SOURCE and REG_NOTES of insn p. */
923 REG_NOTES (regs
->array
[regno
].single_usage
)
925 (REG_NOTES (regs
->array
[regno
].single_usage
),
926 SET_DEST (set
), copy_rtx (SET_SRC (set
))));
929 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
));
931 regs
->array
[regno
+i
].set_in_loop
= 0;
935 m
= (struct movable
*) xmalloc (sizeof (struct movable
));
939 m
->dependencies
= dependencies
;
940 m
->set_dest
= SET_DEST (set
);
943 = regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
- 1;
947 m
->move_insn
= move_insn
;
948 m
->move_insn_first
= 0;
949 m
->insert_temp
= insert_temp
;
950 m
->is_equiv
= (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
951 m
->savemode
= VOIDmode
;
953 /* Set M->cond if either loop_invariant_p
954 or consec_sets_invariant_p returned 2
955 (only conditionally invariant). */
956 m
->cond
= ((tem
| tem1
| tem2
) > 1);
957 m
->global
= LOOP_REG_GLOBAL_P (loop
, regno
);
959 m
->lifetime
= LOOP_REG_LIFETIME (loop
, regno
);
960 m
->savings
= regs
->array
[regno
].n_times_set
;
961 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
962 m
->savings
+= libcall_benefit (p
);
963 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
)); i
++)
964 regs
->array
[regno
+i
].set_in_loop
= move_insn
? -2 : -1;
965 /* Add M to the end of the chain MOVABLES. */
966 loop_movables_add (movables
, m
);
970 /* It is possible for the first instruction to have a
971 REG_EQUAL note but a non-invariant SET_SRC, so we must
972 remember the status of the first instruction in case
973 the last instruction doesn't have a REG_EQUAL note. */
974 m
->move_insn_first
= m
->move_insn
;
976 /* Skip this insn, not checking REG_LIBCALL notes. */
977 p
= next_nonnote_insn (p
);
978 /* Skip the consecutive insns, if there are any. */
979 p
= skip_consec_insns (p
, m
->consec
);
980 /* Back up to the last insn of the consecutive group. */
981 p
= prev_nonnote_insn (p
);
983 /* We must now reset m->move_insn, m->is_equiv, and
984 possibly m->set_src to correspond to the effects of
986 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
988 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
991 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
992 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
993 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
999 = (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
1002 /* If this register is always set within a STRICT_LOW_PART
1003 or set to zero, then its high bytes are constant.
1004 So clear them outside the loop and within the loop
1005 just load the low bytes.
1006 We must check that the machine has an instruction to do so.
1007 Also, if the value loaded into the register
1008 depends on the same register, this cannot be done. */
1009 else if (SET_SRC (set
) == const0_rtx
1010 && GET_CODE (NEXT_INSN (p
)) == INSN
1011 && (set1
= single_set (NEXT_INSN (p
)))
1012 && GET_CODE (set1
) == SET
1013 && (GET_CODE (SET_DEST (set1
)) == STRICT_LOW_PART
)
1014 && (GET_CODE (XEXP (SET_DEST (set1
), 0)) == SUBREG
)
1015 && (SUBREG_REG (XEXP (SET_DEST (set1
), 0))
1017 && !reg_mentioned_p (SET_DEST (set
), SET_SRC (set1
)))
1019 int regno
= REGNO (SET_DEST (set
));
1020 if (regs
->array
[regno
].set_in_loop
== 2)
1023 m
= (struct movable
*) xmalloc (sizeof (struct movable
));
1026 m
->set_dest
= SET_DEST (set
);
1027 m
->dependencies
= 0;
1033 m
->move_insn_first
= 0;
1034 m
->insert_temp
= insert_temp
;
1036 /* If the insn may not be executed on some cycles,
1037 we can't clear the whole reg; clear just high part.
1038 Not even if the reg is used only within this loop.
1045 Clearing x before the inner loop could clobber a value
1046 being saved from the last time around the outer loop.
1047 However, if the reg is not used outside this loop
1048 and all uses of the register are in the same
1049 basic block as the store, there is no problem.
1051 If this insn was made by loop, we don't know its
1052 INSN_LUID and hence must make a conservative
1054 m
->global
= (INSN_UID (p
) >= max_uid_for_loop
1055 || LOOP_REG_GLOBAL_P (loop
, regno
)
1056 || (labels_in_range_p
1057 (p
, REGNO_FIRST_LUID (regno
))));
1058 if (maybe_never
&& m
->global
)
1059 m
->savemode
= GET_MODE (SET_SRC (set1
));
1061 m
->savemode
= VOIDmode
;
1065 m
->lifetime
= LOOP_REG_LIFETIME (loop
, regno
);
1068 i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
));
1070 regs
->array
[regno
+i
].set_in_loop
= -1;
1071 /* Add M to the end of the chain MOVABLES. */
1072 loop_movables_add (movables
, m
);
1077 /* Past a call insn, we get to insns which might not be executed
1078 because the call might exit. This matters for insns that trap.
1079 Constant and pure call insns always return, so they don't count. */
1080 else if (GET_CODE (p
) == CALL_INSN
&& ! CONST_OR_PURE_CALL_P (p
))
1082 /* Past a label or a jump, we get to insns for which we
1083 can't count on whether or how many times they will be
1084 executed during each iteration. Therefore, we can
1085 only move out sets of trivial variables
1086 (those not used after the loop). */
1087 /* Similar code appears twice in strength_reduce. */
1088 else if ((GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
)
1089 /* If we enter the loop in the middle, and scan around to the
1090 beginning, don't set maybe_never for that. This must be an
1091 unconditional jump, otherwise the code at the top of the
1092 loop might never be executed. Unconditional jumps are
1093 followed by a barrier then the loop_end. */
1094 && ! (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == loop
->top
1095 && NEXT_INSN (NEXT_INSN (p
)) == loop_end
1096 && any_uncondjump_p (p
)))
1098 else if (GET_CODE (p
) == NOTE
)
1100 /* At the virtual top of a converted loop, insns are again known to
1101 be executed: logically, the loop begins here even though the exit
1102 code has been duplicated. */
1103 if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
&& loop_depth
== 0)
1104 maybe_never
= call_passed
= 0;
1105 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
1107 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)
1112 /* If one movable subsumes another, ignore that other. */
1114 ignore_some_movables (movables
);
1116 /* For each movable insn, see if the reg that it loads
1117 leads when it dies right into another conditionally movable insn.
1118 If so, record that the second insn "forces" the first one,
1119 since the second can be moved only if the first is. */
1121 force_movables (movables
);
1123 /* See if there are multiple movable insns that load the same value.
1124 If there are, make all but the first point at the first one
1125 through the `match' field, and add the priorities of them
1126 all together as the priority of the first. */
1128 combine_movables (movables
, regs
);
1130 /* Now consider each movable insn to decide whether it is worth moving.
1131 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1133 For machines with few registers this increases code size, so do not
1134 move moveables when optimizing for code size on such machines.
1135 (The 18 below is the value for i386.) */
1138 || (reg_class_size
[GENERAL_REGS
] > 18 && !loop_info
->has_call
))
1140 move_movables (loop
, movables
, threshold
, insn_count
);
1142 /* Recalculate regs->array if move_movables has created new
1144 if (max_reg_num () > regs
->num
)
1146 loop_regs_scan (loop
, 0);
1147 for (update_start
= loop_start
;
1148 PREV_INSN (update_start
)
1149 && GET_CODE (PREV_INSN (update_start
)) != CODE_LABEL
;
1150 update_start
= PREV_INSN (update_start
))
1152 update_end
= NEXT_INSN (loop_end
);
1154 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1155 loop_max_reg
= max_reg_num ();
1159 /* Now candidates that still are negative are those not moved.
1160 Change regs->array[I].set_in_loop to indicate that those are not actually
1162 for (i
= 0; i
< regs
->num
; i
++)
1163 if (regs
->array
[i
].set_in_loop
< 0)
1164 regs
->array
[i
].set_in_loop
= regs
->array
[i
].n_times_set
;
1166 /* Now that we've moved some things out of the loop, we might be able to
1167 hoist even more memory references. */
1170 /* Recalculate regs->array if load_mems has created new registers. */
1171 if (max_reg_num () > regs
->num
)
1172 loop_regs_scan (loop
, 0);
1174 for (update_start
= loop_start
;
1175 PREV_INSN (update_start
)
1176 && GET_CODE (PREV_INSN (update_start
)) != CODE_LABEL
;
1177 update_start
= PREV_INSN (update_start
))
1179 update_end
= NEXT_INSN (loop_end
);
1181 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1182 loop_max_reg
= max_reg_num ();
1184 if (flag_strength_reduce
)
1186 if (update_end
&& GET_CODE (update_end
) == CODE_LABEL
)
1187 /* Ensure our label doesn't go away. */
1188 LABEL_NUSES (update_end
)++;
1190 strength_reduce (loop
, flags
);
1192 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1193 loop_max_reg
= max_reg_num ();
1195 if (update_end
&& GET_CODE (update_end
) == CODE_LABEL
1196 && --LABEL_NUSES (update_end
) == 0)
1197 delete_related_insns (update_end
);
1201 /* The movable information is required for strength reduction. */
1202 loop_movables_free (movables
);
1209 /* Add elements to *OUTPUT to record all the pseudo-regs
1210 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1213 record_excess_regs (in_this
, not_in_this
, output
)
1214 rtx in_this
, not_in_this
;
1221 code
= GET_CODE (in_this
);
1235 if (REGNO (in_this
) >= FIRST_PSEUDO_REGISTER
1236 && ! reg_mentioned_p (in_this
, not_in_this
))
1237 *output
= gen_rtx_EXPR_LIST (VOIDmode
, in_this
, *output
);
1244 fmt
= GET_RTX_FORMAT (code
);
1245 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1252 for (j
= 0; j
< XVECLEN (in_this
, i
); j
++)
1253 record_excess_regs (XVECEXP (in_this
, i
, j
), not_in_this
, output
);
1257 record_excess_regs (XEXP (in_this
, i
), not_in_this
, output
);
1263 /* Check what regs are referred to in the libcall block ending with INSN,
1264 aside from those mentioned in the equivalent value.
1265 If there are none, return 0.
1266 If there are one or more, return an EXPR_LIST containing all of them. */
1269 libcall_other_reg (insn
, equiv
)
1272 rtx note
= find_reg_note (insn
, REG_RETVAL
, NULL_RTX
);
1273 rtx p
= XEXP (note
, 0);
1276 /* First, find all the regs used in the libcall block
1277 that are not mentioned as inputs to the result. */
1281 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
1282 || GET_CODE (p
) == CALL_INSN
)
1283 record_excess_regs (PATTERN (p
), equiv
, &output
);
1290 /* Return 1 if all uses of REG
1291 are between INSN and the end of the basic block. */
1294 reg_in_basic_block_p (insn
, reg
)
1297 int regno
= REGNO (reg
);
1300 if (REGNO_FIRST_UID (regno
) != INSN_UID (insn
))
1303 /* Search this basic block for the already recorded last use of the reg. */
1304 for (p
= insn
; p
; p
= NEXT_INSN (p
))
1306 switch (GET_CODE (p
))
1313 /* Ordinary insn: if this is the last use, we win. */
1314 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1319 /* Jump insn: if this is the last use, we win. */
1320 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1322 /* Otherwise, it's the end of the basic block, so we lose. */
1327 /* It's the end of the basic block, so we lose. */
1335 /* The "last use" that was recorded can't be found after the first
1336 use. This can happen when the last use was deleted while
1337 processing an inner loop, this inner loop was then completely
1338 unrolled, and the outer loop is always exited after the inner loop,
1339 so that everything after the first use becomes a single basic block. */
1343 /* Compute the benefit of eliminating the insns in the block whose
1344 last insn is LAST. This may be a group of insns used to compute a
1345 value directly or can contain a library call. */
1348 libcall_benefit (last
)
1354 for (insn
= XEXP (find_reg_note (last
, REG_RETVAL
, NULL_RTX
), 0);
1355 insn
!= last
; insn
= NEXT_INSN (insn
))
1357 if (GET_CODE (insn
) == CALL_INSN
)
1358 benefit
+= 10; /* Assume at least this many insns in a library
1360 else if (GET_CODE (insn
) == INSN
1361 && GET_CODE (PATTERN (insn
)) != USE
1362 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
1369 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1372 skip_consec_insns (insn
, count
)
1376 for (; count
> 0; count
--)
1380 /* If first insn of libcall sequence, skip to end. */
1381 /* Do this at start of loop, since INSN is guaranteed to
1383 if (GET_CODE (insn
) != NOTE
1384 && (temp
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)))
1385 insn
= XEXP (temp
, 0);
1388 insn
= NEXT_INSN (insn
);
1389 while (GET_CODE (insn
) == NOTE
);
1395 /* Ignore any movable whose insn falls within a libcall
1396 which is part of another movable.
1397 We make use of the fact that the movable for the libcall value
1398 was made later and so appears later on the chain. */
1401 ignore_some_movables (movables
)
1402 struct loop_movables
*movables
;
1404 struct movable
*m
, *m1
;
1406 for (m
= movables
->head
; m
; m
= m
->next
)
1408 /* Is this a movable for the value of a libcall? */
1409 rtx note
= find_reg_note (m
->insn
, REG_RETVAL
, NULL_RTX
);
1413 /* Check for earlier movables inside that range,
1414 and mark them invalid. We cannot use LUIDs here because
1415 insns created by loop.c for prior loops don't have LUIDs.
1416 Rather than reject all such insns from movables, we just
1417 explicitly check each insn in the libcall (since invariant
1418 libcalls aren't that common). */
1419 for (insn
= XEXP (note
, 0); insn
!= m
->insn
; insn
= NEXT_INSN (insn
))
1420 for (m1
= movables
->head
; m1
!= m
; m1
= m1
->next
)
1421 if (m1
->insn
== insn
)
1427 /* For each movable insn, see if the reg that it loads
1428 leads when it dies right into another conditionally movable insn.
1429 If so, record that the second insn "forces" the first one,
1430 since the second can be moved only if the first is. */
1433 force_movables (movables
)
1434 struct loop_movables
*movables
;
1436 struct movable
*m
, *m1
;
1438 for (m1
= movables
->head
; m1
; m1
= m1
->next
)
1439 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1440 if (!m1
->partial
&& !m1
->done
)
1442 int regno
= m1
->regno
;
1443 for (m
= m1
->next
; m
; m
= m
->next
)
1444 /* ??? Could this be a bug? What if CSE caused the
1445 register of M1 to be used after this insn?
1446 Since CSE does not update regno_last_uid,
1447 this insn M->insn might not be where it dies.
1448 But very likely this doesn't matter; what matters is
1449 that M's reg is computed from M1's reg. */
1450 if (INSN_UID (m
->insn
) == REGNO_LAST_UID (regno
)
1453 if (m
!= 0 && m
->set_src
== m1
->set_dest
1454 /* If m->consec, m->set_src isn't valid. */
1458 /* Increase the priority of the moving the first insn
1459 since it permits the second to be moved as well. */
1463 m1
->lifetime
+= m
->lifetime
;
1464 m1
->savings
+= m
->savings
;
1469 /* Find invariant expressions that are equal and can be combined into
1473 combine_movables (movables
, regs
)
1474 struct loop_movables
*movables
;
1475 struct loop_regs
*regs
;
1478 char *matched_regs
= (char *) xmalloc (regs
->num
);
1479 enum machine_mode mode
;
1481 /* Regs that are set more than once are not allowed to match
1482 or be matched. I'm no longer sure why not. */
1483 /* Only pseudo registers are allowed to match or be matched,
1484 since move_movables does not validate the change. */
1485 /* Perhaps testing m->consec_sets would be more appropriate here? */
1487 for (m
= movables
->head
; m
; m
= m
->next
)
1488 if (m
->match
== 0 && regs
->array
[m
->regno
].n_times_set
== 1
1489 && m
->regno
>= FIRST_PSEUDO_REGISTER
1494 int regno
= m
->regno
;
1496 memset (matched_regs
, 0, regs
->num
);
1497 matched_regs
[regno
] = 1;
1499 /* We want later insns to match the first one. Don't make the first
1500 one match any later ones. So start this loop at m->next. */
1501 for (m1
= m
->next
; m1
; m1
= m1
->next
)
1502 if (m
!= m1
&& m1
->match
== 0
1504 && regs
->array
[m1
->regno
].n_times_set
== 1
1505 && m1
->regno
>= FIRST_PSEUDO_REGISTER
1506 /* A reg used outside the loop mustn't be eliminated. */
1508 /* A reg used for zero-extending mustn't be eliminated. */
1510 && (matched_regs
[m1
->regno
]
1513 /* Can combine regs with different modes loaded from the
1514 same constant only if the modes are the same or
1515 if both are integer modes with M wider or the same
1516 width as M1. The check for integer is redundant, but
1517 safe, since the only case of differing destination
1518 modes with equal sources is when both sources are
1519 VOIDmode, i.e., CONST_INT. */
1520 (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
)
1521 || (GET_MODE_CLASS (GET_MODE (m
->set_dest
)) == MODE_INT
1522 && GET_MODE_CLASS (GET_MODE (m1
->set_dest
)) == MODE_INT
1523 && (GET_MODE_BITSIZE (GET_MODE (m
->set_dest
))
1524 >= GET_MODE_BITSIZE (GET_MODE (m1
->set_dest
)))))
1525 /* See if the source of M1 says it matches M. */
1526 && ((GET_CODE (m1
->set_src
) == REG
1527 && matched_regs
[REGNO (m1
->set_src
)])
1528 || rtx_equal_for_loop_p (m
->set_src
, m1
->set_src
,
1530 && ((m
->dependencies
== m1
->dependencies
)
1531 || rtx_equal_p (m
->dependencies
, m1
->dependencies
)))
1533 m
->lifetime
+= m1
->lifetime
;
1534 m
->savings
+= m1
->savings
;
1537 matched_regs
[m1
->regno
] = 1;
1541 /* Now combine the regs used for zero-extension.
1542 This can be done for those not marked `global'
1543 provided their lives don't overlap. */
1545 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1546 mode
= GET_MODE_WIDER_MODE (mode
))
1548 struct movable
*m0
= 0;
1550 /* Combine all the registers for extension from mode MODE.
1551 Don't combine any that are used outside this loop. */
1552 for (m
= movables
->head
; m
; m
= m
->next
)
1553 if (m
->partial
&& ! m
->global
1554 && mode
== GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m
->insn
)))))
1558 int first
= REGNO_FIRST_LUID (m
->regno
);
1559 int last
= REGNO_LAST_LUID (m
->regno
);
1563 /* First one: don't check for overlap, just record it. */
1568 /* Make sure they extend to the same mode.
1569 (Almost always true.) */
1570 if (GET_MODE (m
->set_dest
) != GET_MODE (m0
->set_dest
))
1573 /* We already have one: check for overlap with those
1574 already combined together. */
1575 for (m1
= movables
->head
; m1
!= m
; m1
= m1
->next
)
1576 if (m1
== m0
|| (m1
->partial
&& m1
->match
== m0
))
1577 if (! (REGNO_FIRST_LUID (m1
->regno
) > last
1578 || REGNO_LAST_LUID (m1
->regno
) < first
))
1581 /* No overlap: we can combine this with the others. */
1582 m0
->lifetime
+= m
->lifetime
;
1583 m0
->savings
+= m
->savings
;
1593 free (matched_regs
);
1596 /* Returns the number of movable instructions in LOOP that were not
1597 moved outside the loop. */
1600 num_unmoved_movables (loop
)
1601 const struct loop
*loop
;
1606 for (m
= LOOP_MOVABLES (loop
)->head
; m
; m
= m
->next
)
1614 /* Return 1 if regs X and Y will become the same if moved. */
1617 regs_match_p (x
, y
, movables
)
1619 struct loop_movables
*movables
;
1621 unsigned int xn
= REGNO (x
);
1622 unsigned int yn
= REGNO (y
);
1623 struct movable
*mx
, *my
;
1625 for (mx
= movables
->head
; mx
; mx
= mx
->next
)
1626 if (mx
->regno
== xn
)
1629 for (my
= movables
->head
; my
; my
= my
->next
)
1630 if (my
->regno
== yn
)
1634 && ((mx
->match
== my
->match
&& mx
->match
!= 0)
1636 || mx
== my
->match
));
1639 /* Return 1 if X and Y are identical-looking rtx's.
1640 This is the Lisp function EQUAL for rtx arguments.
1642 If two registers are matching movables or a movable register and an
1643 equivalent constant, consider them equal. */
1646 rtx_equal_for_loop_p (x
, y
, movables
, regs
)
1648 struct loop_movables
*movables
;
1649 struct loop_regs
*regs
;
1659 if (x
== 0 || y
== 0)
1662 code
= GET_CODE (x
);
1664 /* If we have a register and a constant, they may sometimes be
1666 if (GET_CODE (x
) == REG
&& regs
->array
[REGNO (x
)].set_in_loop
== -2
1669 for (m
= movables
->head
; m
; m
= m
->next
)
1670 if (m
->move_insn
&& m
->regno
== REGNO (x
)
1671 && rtx_equal_p (m
->set_src
, y
))
1674 else if (GET_CODE (y
) == REG
&& regs
->array
[REGNO (y
)].set_in_loop
== -2
1677 for (m
= movables
->head
; m
; m
= m
->next
)
1678 if (m
->move_insn
&& m
->regno
== REGNO (y
)
1679 && rtx_equal_p (m
->set_src
, x
))
1683 /* Otherwise, rtx's of different codes cannot be equal. */
1684 if (code
!= GET_CODE (y
))
1687 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1688 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1690 if (GET_MODE (x
) != GET_MODE (y
))
1693 /* These three types of rtx's can be compared nonrecursively. */
1695 return (REGNO (x
) == REGNO (y
) || regs_match_p (x
, y
, movables
));
1697 if (code
== LABEL_REF
)
1698 return XEXP (x
, 0) == XEXP (y
, 0);
1699 if (code
== SYMBOL_REF
)
1700 return XSTR (x
, 0) == XSTR (y
, 0);
1702 /* Compare the elements. If any pair of corresponding elements
1703 fail to match, return 0 for the whole things. */
1705 fmt
= GET_RTX_FORMAT (code
);
1706 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1711 if (XWINT (x
, i
) != XWINT (y
, i
))
1716 if (XINT (x
, i
) != XINT (y
, i
))
1721 /* Two vectors must have the same length. */
1722 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1725 /* And the corresponding elements must match. */
1726 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1727 if (rtx_equal_for_loop_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
),
1728 movables
, regs
) == 0)
1733 if (rtx_equal_for_loop_p (XEXP (x
, i
), XEXP (y
, i
), movables
, regs
)
1739 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1744 /* These are just backpointers, so they don't matter. */
1750 /* It is believed that rtx's at this level will never
1751 contain anything but integers and other rtx's,
1752 except for within LABEL_REFs and SYMBOL_REFs. */
1760 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1761 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1762 references is incremented once for each added note. */
1765 add_label_notes (x
, insns
)
1769 enum rtx_code code
= GET_CODE (x
);
1774 if (code
== LABEL_REF
&& !LABEL_REF_NONLOCAL_P (x
))
1776 /* This code used to ignore labels that referred to dispatch tables to
1777 avoid flow generating (slighly) worse code.
1779 We no longer ignore such label references (see LABEL_REF handling in
1780 mark_jump_label for additional information). */
1781 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
1782 if (reg_mentioned_p (XEXP (x
, 0), insn
))
1784 REG_NOTES (insn
) = gen_rtx_INSN_LIST (REG_LABEL
, XEXP (x
, 0),
1786 if (LABEL_P (XEXP (x
, 0)))
1787 LABEL_NUSES (XEXP (x
, 0))++;
1791 fmt
= GET_RTX_FORMAT (code
);
1792 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1795 add_label_notes (XEXP (x
, i
), insns
);
1796 else if (fmt
[i
] == 'E')
1797 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1798 add_label_notes (XVECEXP (x
, i
, j
), insns
);
1802 /* Scan MOVABLES, and move the insns that deserve to be moved.
1803 If two matching movables are combined, replace one reg with the
1804 other throughout. */
1807 move_movables (loop
, movables
, threshold
, insn_count
)
1809 struct loop_movables
*movables
;
1813 struct loop_regs
*regs
= LOOP_REGS (loop
);
1814 int nregs
= regs
->num
;
1818 rtx loop_start
= loop
->start
;
1819 rtx loop_end
= loop
->end
;
1820 /* Map of pseudo-register replacements to handle combining
1821 when we move several insns that load the same value
1822 into different pseudo-registers. */
1823 rtx
*reg_map
= (rtx
*) xcalloc (nregs
, sizeof (rtx
));
1824 char *already_moved
= (char *) xcalloc (nregs
, sizeof (char));
1826 for (m
= movables
->head
; m
; m
= m
->next
)
1828 /* Describe this movable insn. */
1830 if (loop_dump_stream
)
1832 fprintf (loop_dump_stream
, "Insn %d: regno %d (life %d), ",
1833 INSN_UID (m
->insn
), m
->regno
, m
->lifetime
);
1835 fprintf (loop_dump_stream
, "consec %d, ", m
->consec
);
1837 fprintf (loop_dump_stream
, "cond ");
1839 fprintf (loop_dump_stream
, "force ");
1841 fprintf (loop_dump_stream
, "global ");
1843 fprintf (loop_dump_stream
, "done ");
1845 fprintf (loop_dump_stream
, "move-insn ");
1847 fprintf (loop_dump_stream
, "matches %d ",
1848 INSN_UID (m
->match
->insn
));
1850 fprintf (loop_dump_stream
, "forces %d ",
1851 INSN_UID (m
->forces
->insn
));
1854 /* Ignore the insn if it's already done (it matched something else).
1855 Otherwise, see if it is now safe to move. */
1859 || (1 == loop_invariant_p (loop
, m
->set_src
)
1860 && (m
->dependencies
== 0
1861 || 1 == loop_invariant_p (loop
, m
->dependencies
))
1863 || 1 == consec_sets_invariant_p (loop
, m
->set_dest
,
1866 && (! m
->forces
|| m
->forces
->done
))
1870 int savings
= m
->savings
;
1872 /* We have an insn that is safe to move.
1873 Compute its desirability. */
1878 if (loop_dump_stream
)
1879 fprintf (loop_dump_stream
, "savings %d ", savings
);
1881 if (regs
->array
[regno
].moved_once
&& loop_dump_stream
)
1882 fprintf (loop_dump_stream
, "halved since already moved ");
1884 /* An insn MUST be moved if we already moved something else
1885 which is safe only if this one is moved too: that is,
1886 if already_moved[REGNO] is nonzero. */
1888 /* An insn is desirable to move if the new lifetime of the
1889 register is no more than THRESHOLD times the old lifetime.
1890 If it's not desirable, it means the loop is so big
1891 that moving won't speed things up much,
1892 and it is liable to make register usage worse. */
1894 /* It is also desirable to move if it can be moved at no
1895 extra cost because something else was already moved. */
1897 if (already_moved
[regno
]
1898 || flag_move_all_movables
1899 || (threshold
* savings
* m
->lifetime
) >=
1900 (regs
->array
[regno
].moved_once
? insn_count
* 2 : insn_count
)
1901 || (m
->forces
&& m
->forces
->done
1902 && regs
->array
[m
->forces
->regno
].n_times_set
== 1))
1906 rtx first
= NULL_RTX
;
1907 rtx newreg
= NULL_RTX
;
1910 newreg
= gen_reg_rtx (GET_MODE (m
->set_dest
));
1912 /* Now move the insns that set the reg. */
1914 if (m
->partial
&& m
->match
)
1918 /* Find the end of this chain of matching regs.
1919 Thus, we load each reg in the chain from that one reg.
1920 And that reg is loaded with 0 directly,
1921 since it has ->match == 0. */
1922 for (m1
= m
; m1
->match
; m1
= m1
->match
);
1923 newpat
= gen_move_insn (SET_DEST (PATTERN (m
->insn
)),
1924 SET_DEST (PATTERN (m1
->insn
)));
1925 i1
= loop_insn_hoist (loop
, newpat
);
1927 /* Mark the moved, invariant reg as being allowed to
1928 share a hard reg with the other matching invariant. */
1929 REG_NOTES (i1
) = REG_NOTES (m
->insn
);
1930 r1
= SET_DEST (PATTERN (m
->insn
));
1931 r2
= SET_DEST (PATTERN (m1
->insn
));
1933 = gen_rtx_EXPR_LIST (VOIDmode
, r1
,
1934 gen_rtx_EXPR_LIST (VOIDmode
, r2
,
1936 delete_insn (m
->insn
);
1941 if (loop_dump_stream
)
1942 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
1944 /* If we are to re-generate the item being moved with a
1945 new move insn, first delete what we have and then emit
1946 the move insn before the loop. */
1947 else if (m
->move_insn
)
1951 for (count
= m
->consec
; count
>= 0; count
--)
1953 /* If this is the first insn of a library call sequence,
1954 something is very wrong. */
1955 if (GET_CODE (p
) != NOTE
1956 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
1959 /* If this is the last insn of a libcall sequence, then
1960 delete every insn in the sequence except the last.
1961 The last insn is handled in the normal manner. */
1962 if (GET_CODE (p
) != NOTE
1963 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
1965 temp
= XEXP (temp
, 0);
1967 temp
= delete_insn (temp
);
1971 p
= delete_insn (p
);
1973 /* simplify_giv_expr expects that it can walk the insns
1974 at m->insn forwards and see this old sequence we are
1975 tossing here. delete_insn does preserve the next
1976 pointers, but when we skip over a NOTE we must fix
1977 it up. Otherwise that code walks into the non-deleted
1979 while (p
&& GET_CODE (p
) == NOTE
)
1980 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
1984 /* Replace the original insn with a move from
1985 our newly created temp. */
1987 emit_move_insn (m
->set_dest
, newreg
);
1990 emit_insn_before (seq
, p
);
1995 emit_move_insn (m
->insert_temp
? newreg
: m
->set_dest
,
2000 add_label_notes (m
->set_src
, seq
);
2002 i1
= loop_insn_hoist (loop
, seq
);
2003 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2004 set_unique_reg_note (i1
,
2005 m
->is_equiv
? REG_EQUIV
: REG_EQUAL
,
2008 if (loop_dump_stream
)
2009 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
2011 /* The more regs we move, the less we like moving them. */
2016 for (count
= m
->consec
; count
>= 0; count
--)
2020 /* If first insn of libcall sequence, skip to end. */
2021 /* Do this at start of loop, since p is guaranteed to
2023 if (GET_CODE (p
) != NOTE
2024 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
2027 /* If last insn of libcall sequence, move all
2028 insns except the last before the loop. The last
2029 insn is handled in the normal manner. */
2030 if (GET_CODE (p
) != NOTE
2031 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
2035 rtx fn_address_insn
= 0;
2038 for (temp
= XEXP (temp
, 0); temp
!= p
;
2039 temp
= NEXT_INSN (temp
))
2045 if (GET_CODE (temp
) == NOTE
)
2048 body
= PATTERN (temp
);
2050 /* Find the next insn after TEMP,
2051 not counting USE or NOTE insns. */
2052 for (next
= NEXT_INSN (temp
); next
!= p
;
2053 next
= NEXT_INSN (next
))
2054 if (! (GET_CODE (next
) == INSN
2055 && GET_CODE (PATTERN (next
)) == USE
)
2056 && GET_CODE (next
) != NOTE
)
2059 /* If that is the call, this may be the insn
2060 that loads the function address.
2062 Extract the function address from the insn
2063 that loads it into a register.
2064 If this insn was cse'd, we get incorrect code.
2066 So emit a new move insn that copies the
2067 function address into the register that the
2068 call insn will use. flow.c will delete any
2069 redundant stores that we have created. */
2070 if (GET_CODE (next
) == CALL_INSN
2071 && GET_CODE (body
) == SET
2072 && GET_CODE (SET_DEST (body
)) == REG
2073 && (n
= find_reg_note (temp
, REG_EQUAL
,
2076 fn_reg
= SET_SRC (body
);
2077 if (GET_CODE (fn_reg
) != REG
)
2078 fn_reg
= SET_DEST (body
);
2079 fn_address
= XEXP (n
, 0);
2080 fn_address_insn
= temp
;
2082 /* We have the call insn.
2083 If it uses the register we suspect it might,
2084 load it with the correct address directly. */
2085 if (GET_CODE (temp
) == CALL_INSN
2087 && reg_referenced_p (fn_reg
, body
))
2088 loop_insn_emit_after (loop
, 0, fn_address_insn
,
2090 (fn_reg
, fn_address
));
2092 if (GET_CODE (temp
) == CALL_INSN
)
2094 i1
= loop_call_insn_hoist (loop
, body
);
2095 /* Because the USAGE information potentially
2096 contains objects other than hard registers
2097 we need to copy it. */
2098 if (CALL_INSN_FUNCTION_USAGE (temp
))
2099 CALL_INSN_FUNCTION_USAGE (i1
)
2100 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp
));
2103 i1
= loop_insn_hoist (loop
, body
);
2106 if (temp
== fn_address_insn
)
2107 fn_address_insn
= i1
;
2108 REG_NOTES (i1
) = REG_NOTES (temp
);
2109 REG_NOTES (temp
) = NULL
;
2115 if (m
->savemode
!= VOIDmode
)
2117 /* P sets REG to zero; but we should clear only
2118 the bits that are not covered by the mode
2120 rtx reg
= m
->set_dest
;
2125 tem
= expand_simple_binop
2126 (GET_MODE (reg
), AND
, reg
,
2127 GEN_INT ((((HOST_WIDE_INT
) 1
2128 << GET_MODE_BITSIZE (m
->savemode
)))
2130 reg
, 1, OPTAB_LIB_WIDEN
);
2134 emit_move_insn (reg
, tem
);
2135 sequence
= get_insns ();
2137 i1
= loop_insn_hoist (loop
, sequence
);
2139 else if (GET_CODE (p
) == CALL_INSN
)
2141 i1
= loop_call_insn_hoist (loop
, PATTERN (p
));
2142 /* Because the USAGE information potentially
2143 contains objects other than hard registers
2144 we need to copy it. */
2145 if (CALL_INSN_FUNCTION_USAGE (p
))
2146 CALL_INSN_FUNCTION_USAGE (i1
)
2147 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p
));
2149 else if (count
== m
->consec
&& m
->move_insn_first
)
2152 /* The SET_SRC might not be invariant, so we must
2153 use the REG_EQUAL note. */
2155 emit_move_insn (m
->set_dest
, m
->set_src
);
2159 add_label_notes (m
->set_src
, seq
);
2161 i1
= loop_insn_hoist (loop
, seq
);
2162 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2163 set_unique_reg_note (i1
, m
->is_equiv
? REG_EQUIV
2164 : REG_EQUAL
, m
->set_src
);
2166 else if (m
->insert_temp
)
2168 rtx
*reg_map2
= (rtx
*) xcalloc (REGNO (newreg
),
2170 reg_map2
[m
->regno
] = newreg
;
2172 i1
= loop_insn_hoist (loop
, copy_rtx (PATTERN (p
)));
2173 replace_regs (i1
, reg_map2
, REGNO (newreg
), 1);
2177 i1
= loop_insn_hoist (loop
, PATTERN (p
));
2179 if (REG_NOTES (i1
) == 0)
2181 REG_NOTES (i1
) = REG_NOTES (p
);
2182 REG_NOTES (p
) = NULL
;
2184 /* If there is a REG_EQUAL note present whose value
2185 is not loop invariant, then delete it, since it
2186 may cause problems with later optimization passes.
2187 It is possible for cse to create such notes
2188 like this as a result of record_jump_cond. */
2190 if ((temp
= find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2191 && ! loop_invariant_p (loop
, XEXP (temp
, 0)))
2192 remove_note (i1
, temp
);
2198 if (loop_dump_stream
)
2199 fprintf (loop_dump_stream
, " moved to %d",
2202 /* If library call, now fix the REG_NOTES that contain
2203 insn pointers, namely REG_LIBCALL on FIRST
2204 and REG_RETVAL on I1. */
2205 if ((temp
= find_reg_note (i1
, REG_RETVAL
, NULL_RTX
)))
2207 XEXP (temp
, 0) = first
;
2208 temp
= find_reg_note (first
, REG_LIBCALL
, NULL_RTX
);
2209 XEXP (temp
, 0) = i1
;
2216 /* simplify_giv_expr expects that it can walk the insns
2217 at m->insn forwards and see this old sequence we are
2218 tossing here. delete_insn does preserve the next
2219 pointers, but when we skip over a NOTE we must fix
2220 it up. Otherwise that code walks into the non-deleted
2222 while (p
&& GET_CODE (p
) == NOTE
)
2223 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
2228 /* Replace the original insn with a move from
2229 our newly created temp. */
2231 emit_move_insn (m
->set_dest
, newreg
);
2234 emit_insn_before (seq
, p
);
2238 /* The more regs we move, the less we like moving them. */
2244 if (!m
->insert_temp
)
2246 /* Any other movable that loads the same register
2248 already_moved
[regno
] = 1;
2250 /* This reg has been moved out of one loop. */
2251 regs
->array
[regno
].moved_once
= 1;
2253 /* The reg set here is now invariant. */
2257 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, m
->set_dest
); i
++)
2258 regs
->array
[regno
+i
].set_in_loop
= 0;
2261 /* Change the length-of-life info for the register
2262 to say it lives at least the full length of this loop.
2263 This will help guide optimizations in outer loops. */
2265 if (REGNO_FIRST_LUID (regno
) > INSN_LUID (loop_start
))
2266 /* This is the old insn before all the moved insns.
2267 We can't use the moved insn because it is out of range
2268 in uid_luid. Only the old insns have luids. */
2269 REGNO_FIRST_UID (regno
) = INSN_UID (loop_start
);
2270 if (REGNO_LAST_LUID (regno
) < INSN_LUID (loop_end
))
2271 REGNO_LAST_UID (regno
) = INSN_UID (loop_end
);
2274 /* Combine with this moved insn any other matching movables. */
2277 for (m1
= movables
->head
; m1
; m1
= m1
->next
)
2282 /* Schedule the reg loaded by M1
2283 for replacement so that shares the reg of M.
2284 If the modes differ (only possible in restricted
2285 circumstances, make a SUBREG.
2287 Note this assumes that the target dependent files
2288 treat REG and SUBREG equally, including within
2289 GO_IF_LEGITIMATE_ADDRESS and in all the
2290 predicates since we never verify that replacing the
2291 original register with a SUBREG results in a
2292 recognizable insn. */
2293 if (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
))
2294 reg_map
[m1
->regno
] = m
->set_dest
;
2297 = gen_lowpart_common (GET_MODE (m1
->set_dest
),
2300 /* Get rid of the matching insn
2301 and prevent further processing of it. */
2304 /* if library call, delete all insns. */
2305 if ((temp
= find_reg_note (m1
->insn
, REG_RETVAL
,
2307 delete_insn_chain (XEXP (temp
, 0), m1
->insn
);
2309 delete_insn (m1
->insn
);
2311 /* Any other movable that loads the same register
2313 already_moved
[m1
->regno
] = 1;
2315 /* The reg merged here is now invariant,
2316 if the reg it matches is invariant. */
2321 i
< LOOP_REGNO_NREGS (regno
, m1
->set_dest
);
2323 regs
->array
[m1
->regno
+i
].set_in_loop
= 0;
2327 else if (loop_dump_stream
)
2328 fprintf (loop_dump_stream
, "not desirable");
2330 else if (loop_dump_stream
&& !m
->match
)
2331 fprintf (loop_dump_stream
, "not safe");
2333 if (loop_dump_stream
)
2334 fprintf (loop_dump_stream
, "\n");
2338 new_start
= loop_start
;
2340 /* Go through all the instructions in the loop, making
2341 all the register substitutions scheduled in REG_MAP. */
2342 for (p
= new_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
2343 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
2344 || GET_CODE (p
) == CALL_INSN
)
2346 replace_regs (PATTERN (p
), reg_map
, nregs
, 0);
2347 replace_regs (REG_NOTES (p
), reg_map
, nregs
, 0);
2353 free (already_moved
);
2358 loop_movables_add (movables
, m
)
2359 struct loop_movables
*movables
;
2362 if (movables
->head
== 0)
2365 movables
->last
->next
= m
;
2371 loop_movables_free (movables
)
2372 struct loop_movables
*movables
;
2375 struct movable
*m_next
;
2377 for (m
= movables
->head
; m
; m
= m_next
)
2385 /* Scan X and replace the address of any MEM in it with ADDR.
2386 REG is the address that MEM should have before the replacement. */
2389 replace_call_address (x
, reg
, addr
)
2398 code
= GET_CODE (x
);
2412 /* Short cut for very common case. */
2413 replace_call_address (XEXP (x
, 1), reg
, addr
);
2417 /* Short cut for very common case. */
2418 replace_call_address (XEXP (x
, 0), reg
, addr
);
2422 /* If this MEM uses a reg other than the one we expected,
2423 something is wrong. */
2424 if (XEXP (x
, 0) != reg
)
2433 fmt
= GET_RTX_FORMAT (code
);
2434 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2437 replace_call_address (XEXP (x
, i
), reg
, addr
);
2438 else if (fmt
[i
] == 'E')
2441 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2442 replace_call_address (XVECEXP (x
, i
, j
), reg
, addr
);
2448 /* Return the number of memory refs to addresses that vary
2452 count_nonfixed_reads (loop
, x
)
2453 const struct loop
*loop
;
2464 code
= GET_CODE (x
);
2478 return ((loop_invariant_p (loop
, XEXP (x
, 0)) != 1)
2479 + count_nonfixed_reads (loop
, XEXP (x
, 0)));
2486 fmt
= GET_RTX_FORMAT (code
);
2487 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2490 value
+= count_nonfixed_reads (loop
, XEXP (x
, i
));
2494 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2495 value
+= count_nonfixed_reads (loop
, XVECEXP (x
, i
, j
));
2501 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2502 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2503 `unknown_address_altered', `unknown_constant_address_altered', and
2504 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2505 list `store_mems' in LOOP. */
2513 struct loop_info
*loop_info
= LOOP_INFO (loop
);
2514 rtx start
= loop
->start
;
2515 rtx end
= loop
->end
;
2516 /* The label after END. Jumping here is just like falling off the
2517 end of the loop. We use next_nonnote_insn instead of next_label
2518 as a hedge against the (pathological) case where some actual insn
2519 might end up between the two. */
2520 rtx exit_target
= next_nonnote_insn (end
);
2522 loop_info
->has_indirect_jump
= indirect_jump_in_function
;
2523 loop_info
->pre_header_has_call
= 0;
2524 loop_info
->has_call
= 0;
2525 loop_info
->has_nonconst_call
= 0;
2526 loop_info
->has_prefetch
= 0;
2527 loop_info
->has_volatile
= 0;
2528 loop_info
->has_tablejump
= 0;
2529 loop_info
->has_multiple_exit_targets
= 0;
2532 loop_info
->unknown_address_altered
= 0;
2533 loop_info
->unknown_constant_address_altered
= 0;
2534 loop_info
->store_mems
= NULL_RTX
;
2535 loop_info
->first_loop_store_insn
= NULL_RTX
;
2536 loop_info
->mems_idx
= 0;
2537 loop_info
->num_mem_sets
= 0;
2538 /* If loop opts run twice, this was set on 1st pass for 2nd. */
2539 loop_info
->preconditioned
= NOTE_PRECONDITIONED (end
);
2541 for (insn
= start
; insn
&& GET_CODE (insn
) != CODE_LABEL
;
2542 insn
= PREV_INSN (insn
))
2544 if (GET_CODE (insn
) == CALL_INSN
)
2546 loop_info
->pre_header_has_call
= 1;
2551 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2552 insn
= NEXT_INSN (insn
))
2554 switch (GET_CODE (insn
))
2557 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
2560 /* Count number of loops contained in this one. */
2563 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
2568 if (! CONST_OR_PURE_CALL_P (insn
))
2570 loop_info
->unknown_address_altered
= 1;
2571 loop_info
->has_nonconst_call
= 1;
2573 else if (pure_call_p (insn
))
2574 loop_info
->has_nonconst_call
= 1;
2575 loop_info
->has_call
= 1;
2576 if (can_throw_internal (insn
))
2577 loop_info
->has_multiple_exit_targets
= 1;
2579 /* Calls initializing constant objects have CLOBBER of MEM /u in the
2580 attached FUNCTION_USAGE expression list, not accounted for by the
2581 code above. We should note these to avoid missing dependencies in
2582 later references. */
2586 for (fusage_entry
= CALL_INSN_FUNCTION_USAGE (insn
);
2587 fusage_entry
; fusage_entry
= XEXP (fusage_entry
, 1))
2589 rtx fusage
= XEXP (fusage_entry
, 0);
2591 if (GET_CODE (fusage
) == CLOBBER
2592 && GET_CODE (XEXP (fusage
, 0)) == MEM
2593 && RTX_UNCHANGING_P (XEXP (fusage
, 0)))
2595 note_stores (fusage
, note_addr_stored
, loop_info
);
2596 if (! loop_info
->first_loop_store_insn
2597 && loop_info
->store_mems
)
2598 loop_info
->first_loop_store_insn
= insn
;
2605 if (! loop_info
->has_multiple_exit_targets
)
2607 rtx set
= pc_set (insn
);
2611 rtx src
= SET_SRC (set
);
2614 if (GET_CODE (src
) == IF_THEN_ELSE
)
2616 label1
= XEXP (src
, 1);
2617 label2
= XEXP (src
, 2);
2627 if (label1
&& label1
!= pc_rtx
)
2629 if (GET_CODE (label1
) != LABEL_REF
)
2631 /* Something tricky. */
2632 loop_info
->has_multiple_exit_targets
= 1;
2635 else if (XEXP (label1
, 0) != exit_target
2636 && LABEL_OUTSIDE_LOOP_P (label1
))
2638 /* A jump outside the current loop. */
2639 loop_info
->has_multiple_exit_targets
= 1;
2651 /* A return, or something tricky. */
2652 loop_info
->has_multiple_exit_targets
= 1;
2658 if (volatile_refs_p (PATTERN (insn
)))
2659 loop_info
->has_volatile
= 1;
2661 if (GET_CODE (insn
) == JUMP_INSN
2662 && (GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
2663 || GET_CODE (PATTERN (insn
)) == ADDR_VEC
))
2664 loop_info
->has_tablejump
= 1;
2666 note_stores (PATTERN (insn
), note_addr_stored
, loop_info
);
2667 if (! loop_info
->first_loop_store_insn
&& loop_info
->store_mems
)
2668 loop_info
->first_loop_store_insn
= insn
;
2670 if (flag_non_call_exceptions
&& can_throw_internal (insn
))
2671 loop_info
->has_multiple_exit_targets
= 1;
2679 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2680 if (/* An exception thrown by a called function might land us
2682 ! loop_info
->has_nonconst_call
2683 /* We don't want loads for MEMs moved to a location before the
2684 one at which their stack memory becomes allocated. (Note
2685 that this is not a problem for malloc, etc., since those
2686 require actual function calls. */
2687 && ! current_function_calls_alloca
2688 /* There are ways to leave the loop other than falling off the
2690 && ! loop_info
->has_multiple_exit_targets
)
2691 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2692 insn
= NEXT_INSN (insn
))
2693 for_each_rtx (&insn
, insert_loop_mem
, loop_info
);
2695 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2696 that loop_invariant_p and load_mems can use true_dependence
2697 to determine what is really clobbered. */
2698 if (loop_info
->unknown_address_altered
)
2700 rtx mem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
2702 loop_info
->store_mems
2703 = gen_rtx_EXPR_LIST (VOIDmode
, mem
, loop_info
->store_mems
);
2705 if (loop_info
->unknown_constant_address_altered
)
2707 rtx mem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
2709 RTX_UNCHANGING_P (mem
) = 1;
2710 loop_info
->store_mems
2711 = gen_rtx_EXPR_LIST (VOIDmode
, mem
, loop_info
->store_mems
);
2715 /* Invalidate all loops containing LABEL. */
2718 invalidate_loops_containing_label (label
)
2722 for (loop
= uid_loop
[INSN_UID (label
)]; loop
; loop
= loop
->outer
)
2726 /* Scan the function looking for loops. Record the start and end of each loop.
2727 Also mark as invalid loops any loops that contain a setjmp or are branched
2728 to from outside the loop. */
2731 find_and_verify_loops (f
, loops
)
2733 struct loops
*loops
;
2738 struct loop
*current_loop
;
2739 struct loop
*next_loop
;
2742 num_loops
= loops
->num
;
2744 compute_luids (f
, NULL_RTX
, 0);
2746 /* If there are jumps to undefined labels,
2747 treat them as jumps out of any/all loops.
2748 This also avoids writing past end of tables when there are no loops. */
2751 /* Find boundaries of loops, mark which loops are contained within
2752 loops, and invalidate loops that have setjmp. */
2755 current_loop
= NULL
;
2756 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2758 if (GET_CODE (insn
) == NOTE
)
2759 switch (NOTE_LINE_NUMBER (insn
))
2761 case NOTE_INSN_LOOP_BEG
:
2762 next_loop
= loops
->array
+ num_loops
;
2763 next_loop
->num
= num_loops
;
2765 next_loop
->start
= insn
;
2766 next_loop
->outer
= current_loop
;
2767 current_loop
= next_loop
;
2770 case NOTE_INSN_LOOP_CONT
:
2771 current_loop
->cont
= insn
;
2774 case NOTE_INSN_LOOP_VTOP
:
2775 current_loop
->vtop
= insn
;
2778 case NOTE_INSN_LOOP_END
:
2782 current_loop
->end
= insn
;
2783 current_loop
= current_loop
->outer
;
2790 if (GET_CODE (insn
) == CALL_INSN
2791 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2793 /* In this case, we must invalidate our current loop and any
2795 for (loop
= current_loop
; loop
; loop
= loop
->outer
)
2798 if (loop_dump_stream
)
2799 fprintf (loop_dump_stream
,
2800 "\nLoop at %d ignored due to setjmp.\n",
2801 INSN_UID (loop
->start
));
2805 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2806 enclosing loop, but this doesn't matter. */
2807 uid_loop
[INSN_UID (insn
)] = current_loop
;
2810 /* Any loop containing a label used in an initializer must be invalidated,
2811 because it can be jumped into from anywhere. */
2812 for (label
= forced_labels
; label
; label
= XEXP (label
, 1))
2813 invalidate_loops_containing_label (XEXP (label
, 0));
2815 /* Any loop containing a label used for an exception handler must be
2816 invalidated, because it can be jumped into from anywhere. */
2817 for_each_eh_label (invalidate_loops_containing_label
);
2819 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2820 loop that it is not contained within, that loop is marked invalid.
2821 If any INSN or CALL_INSN uses a label's address, then the loop containing
2822 that label is marked invalid, because it could be jumped into from
2825 Also look for blocks of code ending in an unconditional branch that
2826 exits the loop. If such a block is surrounded by a conditional
2827 branch around the block, move the block elsewhere (see below) and
2828 invert the jump to point to the code block. This may eliminate a
2829 label in our loop and will simplify processing by both us and a
2830 possible second cse pass. */
2832 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2835 struct loop
*this_loop
= uid_loop
[INSN_UID (insn
)];
2837 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
2839 rtx note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
);
2841 invalidate_loops_containing_label (XEXP (note
, 0));
2844 if (GET_CODE (insn
) != JUMP_INSN
)
2847 mark_loop_jump (PATTERN (insn
), this_loop
);
2849 /* See if this is an unconditional branch outside the loop. */
2851 && (GET_CODE (PATTERN (insn
)) == RETURN
2852 || (any_uncondjump_p (insn
)
2853 && onlyjump_p (insn
)
2854 && (uid_loop
[INSN_UID (JUMP_LABEL (insn
))]
2856 && get_max_uid () < max_uid_for_loop
)
2859 rtx our_next
= next_real_insn (insn
);
2860 rtx last_insn_to_move
= NEXT_INSN (insn
);
2861 struct loop
*dest_loop
;
2862 struct loop
*outer_loop
= NULL
;
2864 /* Go backwards until we reach the start of the loop, a label,
2866 for (p
= PREV_INSN (insn
);
2867 GET_CODE (p
) != CODE_LABEL
2868 && ! (GET_CODE (p
) == NOTE
2869 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
2870 && GET_CODE (p
) != JUMP_INSN
;
2874 /* Check for the case where we have a jump to an inner nested
2875 loop, and do not perform the optimization in that case. */
2877 if (JUMP_LABEL (insn
))
2879 dest_loop
= uid_loop
[INSN_UID (JUMP_LABEL (insn
))];
2882 for (outer_loop
= dest_loop
; outer_loop
;
2883 outer_loop
= outer_loop
->outer
)
2884 if (outer_loop
== this_loop
)
2889 /* Make sure that the target of P is within the current loop. */
2891 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
2892 && uid_loop
[INSN_UID (JUMP_LABEL (p
))] != this_loop
)
2893 outer_loop
= this_loop
;
2895 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2896 we have a block of code to try to move.
2898 We look backward and then forward from the target of INSN
2899 to find a BARRIER at the same loop depth as the target.
2900 If we find such a BARRIER, we make a new label for the start
2901 of the block, invert the jump in P and point it to that label,
2902 and move the block of code to the spot we found. */
2905 && GET_CODE (p
) == JUMP_INSN
2906 && JUMP_LABEL (p
) != 0
2907 /* Just ignore jumps to labels that were never emitted.
2908 These always indicate compilation errors. */
2909 && INSN_UID (JUMP_LABEL (p
)) != 0
2910 && any_condjump_p (p
) && onlyjump_p (p
)
2911 && next_real_insn (JUMP_LABEL (p
)) == our_next
2912 /* If it's not safe to move the sequence, then we
2914 && insns_safe_to_move_p (p
, NEXT_INSN (insn
),
2915 &last_insn_to_move
))
2918 = JUMP_LABEL (insn
) ? JUMP_LABEL (insn
) : get_last_insn ();
2919 struct loop
*target_loop
= uid_loop
[INSN_UID (target
)];
2923 /* Search for possible garbage past the conditional jumps
2924 and look for the last barrier. */
2925 for (tmp
= last_insn_to_move
;
2926 tmp
&& GET_CODE (tmp
) != CODE_LABEL
; tmp
= NEXT_INSN (tmp
))
2927 if (GET_CODE (tmp
) == BARRIER
)
2928 last_insn_to_move
= tmp
;
2930 for (loc
= target
; loc
; loc
= PREV_INSN (loc
))
2931 if (GET_CODE (loc
) == BARRIER
2932 /* Don't move things inside a tablejump. */
2933 && ((loc2
= next_nonnote_insn (loc
)) == 0
2934 || GET_CODE (loc2
) != CODE_LABEL
2935 || (loc2
= next_nonnote_insn (loc2
)) == 0
2936 || GET_CODE (loc2
) != JUMP_INSN
2937 || (GET_CODE (PATTERN (loc2
)) != ADDR_VEC
2938 && GET_CODE (PATTERN (loc2
)) != ADDR_DIFF_VEC
))
2939 && uid_loop
[INSN_UID (loc
)] == target_loop
)
2943 for (loc
= target
; loc
; loc
= NEXT_INSN (loc
))
2944 if (GET_CODE (loc
) == BARRIER
2945 /* Don't move things inside a tablejump. */
2946 && ((loc2
= next_nonnote_insn (loc
)) == 0
2947 || GET_CODE (loc2
) != CODE_LABEL
2948 || (loc2
= next_nonnote_insn (loc2
)) == 0
2949 || GET_CODE (loc2
) != JUMP_INSN
2950 || (GET_CODE (PATTERN (loc2
)) != ADDR_VEC
2951 && GET_CODE (PATTERN (loc2
)) != ADDR_DIFF_VEC
))
2952 && uid_loop
[INSN_UID (loc
)] == target_loop
)
2957 rtx cond_label
= JUMP_LABEL (p
);
2958 rtx new_label
= get_label_after (p
);
2960 /* Ensure our label doesn't go away. */
2961 LABEL_NUSES (cond_label
)++;
2963 /* Verify that uid_loop is large enough and that
2965 if (invert_jump (p
, new_label
, 1))
2969 /* If no suitable BARRIER was found, create a suitable
2970 one before TARGET. Since TARGET is a fall through
2971 path, we'll need to insert a jump around our block
2972 and add a BARRIER before TARGET.
2974 This creates an extra unconditional jump outside
2975 the loop. However, the benefits of removing rarely
2976 executed instructions from inside the loop usually
2977 outweighs the cost of the extra unconditional jump
2978 outside the loop. */
2983 temp
= gen_jump (JUMP_LABEL (insn
));
2984 temp
= emit_jump_insn_before (temp
, target
);
2985 JUMP_LABEL (temp
) = JUMP_LABEL (insn
);
2986 LABEL_NUSES (JUMP_LABEL (insn
))++;
2987 loc
= emit_barrier_before (target
);
2990 /* Include the BARRIER after INSN and copy the
2992 if (squeeze_notes (&new_label
, &last_insn_to_move
))
2994 reorder_insns (new_label
, last_insn_to_move
, loc
);
2996 /* All those insns are now in TARGET_LOOP. */
2998 q
!= NEXT_INSN (last_insn_to_move
);
3000 uid_loop
[INSN_UID (q
)] = target_loop
;
3002 /* The label jumped to by INSN is no longer a loop
3003 exit. Unless INSN does not have a label (e.g.,
3004 it is a RETURN insn), search loop->exit_labels
3005 to find its label_ref, and remove it. Also turn
3006 off LABEL_OUTSIDE_LOOP_P bit. */
3007 if (JUMP_LABEL (insn
))
3009 for (q
= 0, r
= this_loop
->exit_labels
;
3011 q
= r
, r
= LABEL_NEXTREF (r
))
3012 if (XEXP (r
, 0) == JUMP_LABEL (insn
))
3014 LABEL_OUTSIDE_LOOP_P (r
) = 0;
3016 LABEL_NEXTREF (q
) = LABEL_NEXTREF (r
);
3018 this_loop
->exit_labels
= LABEL_NEXTREF (r
);
3022 for (loop
= this_loop
; loop
&& loop
!= target_loop
;
3026 /* If we didn't find it, then something is
3032 /* P is now a jump outside the loop, so it must be put
3033 in loop->exit_labels, and marked as such.
3034 The easiest way to do this is to just call
3035 mark_loop_jump again for P. */
3036 mark_loop_jump (PATTERN (p
), this_loop
);
3038 /* If INSN now jumps to the insn after it,
3040 if (JUMP_LABEL (insn
) != 0
3041 && (next_real_insn (JUMP_LABEL (insn
))
3042 == next_real_insn (insn
)))
3043 delete_related_insns (insn
);
3046 /* Continue the loop after where the conditional
3047 branch used to jump, since the only branch insn
3048 in the block (if it still remains) is an inter-loop
3049 branch and hence needs no processing. */
3050 insn
= NEXT_INSN (cond_label
);
3052 if (--LABEL_NUSES (cond_label
) == 0)
3053 delete_related_insns (cond_label
);
3055 /* This loop will be continued with NEXT_INSN (insn). */
3056 insn
= PREV_INSN (insn
);
3063 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
3064 loops it is contained in, mark the target loop invalid.
3066 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
3069 mark_loop_jump (x
, loop
)
3073 struct loop
*dest_loop
;
3074 struct loop
*outer_loop
;
3077 switch (GET_CODE (x
))
3090 /* There could be a label reference in here. */
3091 mark_loop_jump (XEXP (x
, 0), loop
);
3097 mark_loop_jump (XEXP (x
, 0), loop
);
3098 mark_loop_jump (XEXP (x
, 1), loop
);
3102 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3103 mark_loop_jump (XEXP (x
, 1), loop
);
3108 mark_loop_jump (XEXP (x
, 0), loop
);
3112 dest_loop
= uid_loop
[INSN_UID (XEXP (x
, 0))];
3114 /* Link together all labels that branch outside the loop. This
3115 is used by final_[bg]iv_value and the loop unrolling code. Also
3116 mark this LABEL_REF so we know that this branch should predict
3119 /* A check to make sure the label is not in an inner nested loop,
3120 since this does not count as a loop exit. */
3123 for (outer_loop
= dest_loop
; outer_loop
;
3124 outer_loop
= outer_loop
->outer
)
3125 if (outer_loop
== loop
)
3131 if (loop
&& ! outer_loop
)
3133 LABEL_OUTSIDE_LOOP_P (x
) = 1;
3134 LABEL_NEXTREF (x
) = loop
->exit_labels
;
3135 loop
->exit_labels
= x
;
3137 for (outer_loop
= loop
;
3138 outer_loop
&& outer_loop
!= dest_loop
;
3139 outer_loop
= outer_loop
->outer
)
3140 outer_loop
->exit_count
++;
3143 /* If this is inside a loop, but not in the current loop or one enclosed
3144 by it, it invalidates at least one loop. */
3149 /* We must invalidate every nested loop containing the target of this
3150 label, except those that also contain the jump insn. */
3152 for (; dest_loop
; dest_loop
= dest_loop
->outer
)
3154 /* Stop when we reach a loop that also contains the jump insn. */
3155 for (outer_loop
= loop
; outer_loop
; outer_loop
= outer_loop
->outer
)
3156 if (dest_loop
== outer_loop
)
3159 /* If we get here, we know we need to invalidate a loop. */
3160 if (loop_dump_stream
&& ! dest_loop
->invalid
)
3161 fprintf (loop_dump_stream
,
3162 "\nLoop at %d ignored due to multiple entry points.\n",
3163 INSN_UID (dest_loop
->start
));
3165 dest_loop
->invalid
= 1;
3170 /* If this is not setting pc, ignore. */
3171 if (SET_DEST (x
) == pc_rtx
)
3172 mark_loop_jump (SET_SRC (x
), loop
);
3176 mark_loop_jump (XEXP (x
, 1), loop
);
3177 mark_loop_jump (XEXP (x
, 2), loop
);
3182 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
3183 mark_loop_jump (XVECEXP (x
, 0, i
), loop
);
3187 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
3188 mark_loop_jump (XVECEXP (x
, 1, i
), loop
);
3192 /* Strictly speaking this is not a jump into the loop, only a possible
3193 jump out of the loop. However, we have no way to link the destination
3194 of this jump onto the list of exit labels. To be safe we mark this
3195 loop and any containing loops as invalid. */
3198 for (outer_loop
= loop
; outer_loop
; outer_loop
= outer_loop
->outer
)
3200 if (loop_dump_stream
&& ! outer_loop
->invalid
)
3201 fprintf (loop_dump_stream
,
3202 "\nLoop at %d ignored due to unknown exit jump.\n",
3203 INSN_UID (outer_loop
->start
));
3204 outer_loop
->invalid
= 1;
3211 /* Return nonzero if there is a label in the range from
3212 insn INSN to and including the insn whose luid is END
3213 INSN must have an assigned luid (i.e., it must not have
3214 been previously created by loop.c). */
3217 labels_in_range_p (insn
, end
)
3221 while (insn
&& INSN_LUID (insn
) <= end
)
3223 if (GET_CODE (insn
) == CODE_LABEL
)
3225 insn
= NEXT_INSN (insn
);
3231 /* Record that a memory reference X is being set. */
3234 note_addr_stored (x
, y
, data
)
3236 rtx y ATTRIBUTE_UNUSED
;
3237 void *data ATTRIBUTE_UNUSED
;
3239 struct loop_info
*loop_info
= data
;
3241 if (x
== 0 || GET_CODE (x
) != MEM
)
3244 /* Count number of memory writes.
3245 This affects heuristics in strength_reduce. */
3246 loop_info
->num_mem_sets
++;
3248 /* BLKmode MEM means all memory is clobbered. */
3249 if (GET_MODE (x
) == BLKmode
)
3251 if (RTX_UNCHANGING_P (x
))
3252 loop_info
->unknown_constant_address_altered
= 1;
3254 loop_info
->unknown_address_altered
= 1;
3259 loop_info
->store_mems
= gen_rtx_EXPR_LIST (VOIDmode
, x
,
3260 loop_info
->store_mems
);
3263 /* X is a value modified by an INSN that references a biv inside a loop
3264 exit test (ie, X is somehow related to the value of the biv). If X
3265 is a pseudo that is used more than once, then the biv is (effectively)
3266 used more than once. DATA is a pointer to a loop_regs structure. */
3269 note_set_pseudo_multiple_uses (x
, y
, data
)
3271 rtx y ATTRIBUTE_UNUSED
;
3274 struct loop_regs
*regs
= (struct loop_regs
*) data
;
3279 while (GET_CODE (x
) == STRICT_LOW_PART
3280 || GET_CODE (x
) == SIGN_EXTRACT
3281 || GET_CODE (x
) == ZERO_EXTRACT
3282 || GET_CODE (x
) == SUBREG
)
3285 if (GET_CODE (x
) != REG
|| REGNO (x
) < FIRST_PSEUDO_REGISTER
)
3288 /* If we do not have usage information, or if we know the register
3289 is used more than once, note that fact for check_dbra_loop. */
3290 if (REGNO (x
) >= max_reg_before_loop
3291 || ! regs
->array
[REGNO (x
)].single_usage
3292 || regs
->array
[REGNO (x
)].single_usage
== const0_rtx
)
3293 regs
->multiple_uses
= 1;
3296 /* Return nonzero if the rtx X is invariant over the current loop.
3298 The value is 2 if we refer to something only conditionally invariant.
3300 A memory ref is invariant if it is not volatile and does not conflict
3301 with anything stored in `loop_info->store_mems'. */
3304 loop_invariant_p (loop
, x
)
3305 const struct loop
*loop
;
3308 struct loop_info
*loop_info
= LOOP_INFO (loop
);
3309 struct loop_regs
*regs
= LOOP_REGS (loop
);
3313 int conditional
= 0;
3318 code
= GET_CODE (x
);
3328 /* A LABEL_REF is normally invariant, however, if we are unrolling
3329 loops, and this label is inside the loop, then it isn't invariant.
3330 This is because each unrolled copy of the loop body will have
3331 a copy of this label. If this was invariant, then an insn loading
3332 the address of this label into a register might get moved outside
3333 the loop, and then each loop body would end up using the same label.
3335 We don't know the loop bounds here though, so just fail for all
3337 if (flag_old_unroll_loops
)
3344 case UNSPEC_VOLATILE
:
3348 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3349 since the reg might be set by initialization within the loop. */
3351 if ((x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
3352 || x
== arg_pointer_rtx
|| x
== pic_offset_table_rtx
)
3353 && ! current_function_has_nonlocal_goto
)
3356 if (LOOP_INFO (loop
)->has_call
3357 && REGNO (x
) < FIRST_PSEUDO_REGISTER
&& call_used_regs
[REGNO (x
)])
3360 /* Out-of-range regs can occur when we are called from unrolling.
3361 These have always been created by the unroller and are set in
3362 the loop, hence are never invariant. */
3364 if (REGNO (x
) >= (unsigned) regs
->num
)
3367 if (regs
->array
[REGNO (x
)].set_in_loop
< 0)
3370 return regs
->array
[REGNO (x
)].set_in_loop
== 0;
3373 /* Volatile memory references must be rejected. Do this before
3374 checking for read-only items, so that volatile read-only items
3375 will be rejected also. */
3376 if (MEM_VOLATILE_P (x
))
3379 /* See if there is any dependence between a store and this load. */
3380 mem_list_entry
= loop_info
->store_mems
;
3381 while (mem_list_entry
)
3383 if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
3387 mem_list_entry
= XEXP (mem_list_entry
, 1);
3390 /* It's not invalidated by a store in memory
3391 but we must still verify the address is invariant. */
3395 /* Don't mess with insns declared volatile. */
3396 if (MEM_VOLATILE_P (x
))
3404 fmt
= GET_RTX_FORMAT (code
);
3405 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3409 int tem
= loop_invariant_p (loop
, XEXP (x
, i
));
3415 else if (fmt
[i
] == 'E')
3418 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
3420 int tem
= loop_invariant_p (loop
, XVECEXP (x
, i
, j
));
3430 return 1 + conditional
;
3433 /* Return nonzero if all the insns in the loop that set REG
3434 are INSN and the immediately following insns,
3435 and if each of those insns sets REG in an invariant way
3436 (not counting uses of REG in them).
3438 The value is 2 if some of these insns are only conditionally invariant.
3440 We assume that INSN itself is the first set of REG
3441 and that its source is invariant. */
3444 consec_sets_invariant_p (loop
, reg
, n_sets
, insn
)
3445 const struct loop
*loop
;
3449 struct loop_regs
*regs
= LOOP_REGS (loop
);
3451 unsigned int regno
= REGNO (reg
);
3453 /* Number of sets we have to insist on finding after INSN. */
3454 int count
= n_sets
- 1;
3455 int old
= regs
->array
[regno
].set_in_loop
;
3459 /* If N_SETS hit the limit, we can't rely on its value. */
3463 regs
->array
[regno
].set_in_loop
= 0;
3471 code
= GET_CODE (p
);
3473 /* If library call, skip to end of it. */
3474 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
3479 && (set
= single_set (p
))
3480 && GET_CODE (SET_DEST (set
)) == REG
3481 && REGNO (SET_DEST (set
)) == regno
)
3483 this = loop_invariant_p (loop
, SET_SRC (set
));
3486 else if ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
)))
3488 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3489 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3491 this = (CONSTANT_P (XEXP (temp
, 0))
3492 || (find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
3493 && loop_invariant_p (loop
, XEXP (temp
, 0))));
3500 else if (code
!= NOTE
)
3502 regs
->array
[regno
].set_in_loop
= old
;
3507 regs
->array
[regno
].set_in_loop
= old
;
3508 /* If loop_invariant_p ever returned 2, we return 2. */
3509 return 1 + (value
& 2);
3513 /* I don't think this condition is sufficient to allow INSN
3514 to be moved, so we no longer test it. */
3516 /* Return 1 if all insns in the basic block of INSN and following INSN
3517 that set REG are invariant according to TABLE. */
3520 all_sets_invariant_p (reg
, insn
, table
)
3525 int regno
= REGNO (reg
);
3531 code
= GET_CODE (p
);
3532 if (code
== CODE_LABEL
|| code
== JUMP_INSN
)
3534 if (code
== INSN
&& GET_CODE (PATTERN (p
)) == SET
3535 && GET_CODE (SET_DEST (PATTERN (p
))) == REG
3536 && REGNO (SET_DEST (PATTERN (p
))) == regno
)
3538 if (! loop_invariant_p (loop
, SET_SRC (PATTERN (p
)), table
))
3545 /* Look at all uses (not sets) of registers in X. For each, if it is
3546 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3547 a different insn, set USAGE[REGNO] to const0_rtx. */
3550 find_single_use_in_loop (regs
, insn
, x
)
3551 struct loop_regs
*regs
;
3555 enum rtx_code code
= GET_CODE (x
);
3556 const char *fmt
= GET_RTX_FORMAT (code
);
3560 regs
->array
[REGNO (x
)].single_usage
3561 = (regs
->array
[REGNO (x
)].single_usage
!= 0
3562 && regs
->array
[REGNO (x
)].single_usage
!= insn
)
3563 ? const0_rtx
: insn
;
3565 else if (code
== SET
)
3567 /* Don't count SET_DEST if it is a REG; otherwise count things
3568 in SET_DEST because if a register is partially modified, it won't
3569 show up as a potential movable so we don't care how USAGE is set
3571 if (GET_CODE (SET_DEST (x
)) != REG
)
3572 find_single_use_in_loop (regs
, insn
, SET_DEST (x
));
3573 find_single_use_in_loop (regs
, insn
, SET_SRC (x
));
3576 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3578 if (fmt
[i
] == 'e' && XEXP (x
, i
) != 0)
3579 find_single_use_in_loop (regs
, insn
, XEXP (x
, i
));
3580 else if (fmt
[i
] == 'E')
3581 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
3582 find_single_use_in_loop (regs
, insn
, XVECEXP (x
, i
, j
));
3586 /* Count and record any set in X which is contained in INSN. Update
3587 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3591 count_one_set (regs
, insn
, x
, last_set
)
3592 struct loop_regs
*regs
;
3596 if (GET_CODE (x
) == CLOBBER
&& GET_CODE (XEXP (x
, 0)) == REG
)
3597 /* Don't move a reg that has an explicit clobber.
3598 It's not worth the pain to try to do it correctly. */
3599 regs
->array
[REGNO (XEXP (x
, 0))].may_not_optimize
= 1;
3601 if (GET_CODE (x
) == SET
|| GET_CODE (x
) == CLOBBER
)
3603 rtx dest
= SET_DEST (x
);
3604 while (GET_CODE (dest
) == SUBREG
3605 || GET_CODE (dest
) == ZERO_EXTRACT
3606 || GET_CODE (dest
) == SIGN_EXTRACT
3607 || GET_CODE (dest
) == STRICT_LOW_PART
)
3608 dest
= XEXP (dest
, 0);
3609 if (GET_CODE (dest
) == REG
)
3612 int regno
= REGNO (dest
);
3613 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, dest
); i
++)
3615 /* If this is the first setting of this reg
3616 in current basic block, and it was set before,
3617 it must be set in two basic blocks, so it cannot
3618 be moved out of the loop. */
3619 if (regs
->array
[regno
].set_in_loop
> 0
3621 regs
->array
[regno
+i
].may_not_optimize
= 1;
3622 /* If this is not first setting in current basic block,
3623 see if reg was used in between previous one and this.
3624 If so, neither one can be moved. */
3625 if (last_set
[regno
] != 0
3626 && reg_used_between_p (dest
, last_set
[regno
], insn
))
3627 regs
->array
[regno
+i
].may_not_optimize
= 1;
3628 if (regs
->array
[regno
+i
].set_in_loop
< 127)
3629 ++regs
->array
[regno
+i
].set_in_loop
;
3630 last_set
[regno
+i
] = insn
;
3636 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3637 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3638 contained in insn INSN is used by any insn that precedes INSN in
3639 cyclic order starting from the loop entry point.
3641 We don't want to use INSN_LUID here because if we restrict INSN to those
3642 that have a valid INSN_LUID, it means we cannot move an invariant out
3643 from an inner loop past two loops. */
3646 loop_reg_used_before_p (loop
, set
, insn
)
3647 const struct loop
*loop
;
3650 rtx reg
= SET_DEST (set
);
3653 /* Scan forward checking for register usage. If we hit INSN, we
3654 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3655 for (p
= loop
->scan_start
; p
!= insn
; p
= NEXT_INSN (p
))
3657 if (INSN_P (p
) && reg_overlap_mentioned_p (reg
, PATTERN (p
)))
3668 /* Information we collect about arrays that we might want to prefetch. */
3669 struct prefetch_info
3671 struct iv_class
*class; /* Class this prefetch is based on. */
3672 struct induction
*giv
; /* GIV this prefetch is based on. */
3673 rtx base_address
; /* Start prefetching from this address plus
3675 HOST_WIDE_INT index
;
3676 HOST_WIDE_INT stride
; /* Prefetch stride in bytes in each
3678 unsigned int bytes_accessed
; /* Sum of sizes of all accesses to this
3679 prefetch area in one iteration. */
3680 unsigned int total_bytes
; /* Total bytes loop will access in this block.
3681 This is set only for loops with known
3682 iteration counts and is 0xffffffff
3684 int prefetch_in_loop
; /* Number of prefetch insns in loop. */
3685 int prefetch_before_loop
; /* Number of prefetch insns before loop. */
3686 unsigned int write
: 1; /* 1 for read/write prefetches. */
3689 /* Data used by check_store function. */
3690 struct check_store_data
3696 static void check_store
PARAMS ((rtx
, rtx
, void *));
3697 static void emit_prefetch_instructions
PARAMS ((struct loop
*));
3698 static int rtx_equal_for_prefetch_p
PARAMS ((rtx
, rtx
));
3700 /* Set mem_write when mem_address is found. Used as callback to
3703 check_store (x
, pat
, data
)
3704 rtx x
, pat ATTRIBUTE_UNUSED
;
3707 struct check_store_data
*d
= (struct check_store_data
*) data
;
3709 if ((GET_CODE (x
) == MEM
) && rtx_equal_p (d
->mem_address
, XEXP (x
, 0)))
3713 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3714 important to get some addresses combined. Later more sophisticated
3715 transformations can be added when necessary.
3717 ??? Same trick with swapping operand is done at several other places.
3718 It can be nice to develop some common way to handle this. */
3721 rtx_equal_for_prefetch_p (x
, y
)
3726 enum rtx_code code
= GET_CODE (x
);
3731 if (code
!= GET_CODE (y
))
3734 code
= GET_CODE (x
);
3736 if (GET_RTX_CLASS (code
) == 'c')
3738 return ((rtx_equal_for_prefetch_p (XEXP (x
, 0), XEXP (y
, 0))
3739 && rtx_equal_for_prefetch_p (XEXP (x
, 1), XEXP (y
, 1)))
3740 || (rtx_equal_for_prefetch_p (XEXP (x
, 0), XEXP (y
, 1))
3741 && rtx_equal_for_prefetch_p (XEXP (x
, 1), XEXP (y
, 0))));
3743 /* Compare the elements. If any pair of corresponding elements fails to
3744 match, return 0 for the whole thing. */
3746 fmt
= GET_RTX_FORMAT (code
);
3747 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3752 if (XWINT (x
, i
) != XWINT (y
, i
))
3757 if (XINT (x
, i
) != XINT (y
, i
))
3762 /* Two vectors must have the same length. */
3763 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
3766 /* And the corresponding elements must match. */
3767 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
3768 if (rtx_equal_for_prefetch_p (XVECEXP (x
, i
, j
),
3769 XVECEXP (y
, i
, j
)) == 0)
3774 if (rtx_equal_for_prefetch_p (XEXP (x
, i
), XEXP (y
, i
)) == 0)
3779 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
3784 /* These are just backpointers, so they don't matter. */
3790 /* It is believed that rtx's at this level will never
3791 contain anything but integers and other rtx's,
3792 except for within LABEL_REFs and SYMBOL_REFs. */
3800 /* Remove constant addition value from the expression X (when present)
3803 static HOST_WIDE_INT
3804 remove_constant_addition (x
)
3807 HOST_WIDE_INT addval
= 0;
3810 /* Avoid clobbering a shared CONST expression. */
3811 if (GET_CODE (exp
) == CONST
)
3813 if (GET_CODE (XEXP (exp
, 0)) == PLUS
3814 && GET_CODE (XEXP (XEXP (exp
, 0), 0)) == SYMBOL_REF
3815 && GET_CODE (XEXP (XEXP (exp
, 0), 1)) == CONST_INT
)
3817 *x
= XEXP (XEXP (exp
, 0), 0);
3818 return INTVAL (XEXP (XEXP (exp
, 0), 1));
3823 if (GET_CODE (exp
) == CONST_INT
)
3825 addval
= INTVAL (exp
);
3829 /* For plus expression recurse on ourself. */
3830 else if (GET_CODE (exp
) == PLUS
)
3832 addval
+= remove_constant_addition (&XEXP (exp
, 0));
3833 addval
+= remove_constant_addition (&XEXP (exp
, 1));
3835 /* In case our parameter was constant, remove extra zero from the
3837 if (XEXP (exp
, 0) == const0_rtx
)
3839 else if (XEXP (exp
, 1) == const0_rtx
)
3846 /* Attempt to identify accesses to arrays that are most likely to cause cache
3847 misses, and emit prefetch instructions a few prefetch blocks forward.
3849 To detect the arrays we use the GIV information that was collected by the
3850 strength reduction pass.
3852 The prefetch instructions are generated after the GIV information is done
3853 and before the strength reduction process. The new GIVs are injected into
3854 the strength reduction tables, so the prefetch addresses are optimized as
3857 GIVs are split into base address, stride, and constant addition values.
3858 GIVs with the same address, stride and close addition values are combined
3859 into a single prefetch. Also writes to GIVs are detected, so that prefetch
3860 for write instructions can be used for the block we write to, on machines
3861 that support write prefetches.
3863 Several heuristics are used to determine when to prefetch. They are
3864 controlled by defined symbols that can be overridden for each target. */
3867 emit_prefetch_instructions (loop
)
3870 int num_prefetches
= 0;
3871 int num_real_prefetches
= 0;
3872 int num_real_write_prefetches
= 0;
3873 int num_prefetches_before
= 0;
3874 int num_write_prefetches_before
= 0;
3877 struct iv_class
*bl
;
3878 struct induction
*iv
;
3879 struct prefetch_info info
[MAX_PREFETCHES
];
3880 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
3885 /* Consider only loops w/o calls. When a call is done, the loop is probably
3886 slow enough to read the memory. */
3887 if (PREFETCH_NO_CALL
&& LOOP_INFO (loop
)->has_call
)
3889 if (loop_dump_stream
)
3890 fprintf (loop_dump_stream
, "Prefetch: ignoring loop: has call.\n");
3895 /* Don't prefetch in loops known to have few iterations. */
3896 if (PREFETCH_NO_LOW_LOOPCNT
3897 && LOOP_INFO (loop
)->n_iterations
3898 && LOOP_INFO (loop
)->n_iterations
<= PREFETCH_LOW_LOOPCNT
)
3900 if (loop_dump_stream
)
3901 fprintf (loop_dump_stream
,
3902 "Prefetch: ignoring loop: not enough iterations.\n");
3906 /* Search all induction variables and pick those interesting for the prefetch
3908 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
3910 struct induction
*biv
= bl
->biv
, *biv1
;
3915 /* Expect all BIVs to be executed in each iteration. This makes our
3916 analysis more conservative. */
3919 /* Discard non-constant additions that we can't handle well yet, and
3920 BIVs that are executed multiple times; such BIVs ought to be
3921 handled in the nested loop. We accept not_every_iteration BIVs,
3922 since these only result in larger strides and make our
3923 heuristics more conservative. */
3924 if (GET_CODE (biv
->add_val
) != CONST_INT
)
3926 if (loop_dump_stream
)
3928 fprintf (loop_dump_stream
,
3929 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
3930 REGNO (biv
->src_reg
), INSN_UID (biv
->insn
));
3931 print_rtl (loop_dump_stream
, biv
->add_val
);
3932 fprintf (loop_dump_stream
, "\n");
3937 if (biv
->maybe_multiple
)
3939 if (loop_dump_stream
)
3941 fprintf (loop_dump_stream
,
3942 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
3943 REGNO (biv
->src_reg
), INSN_UID (biv
->insn
));
3944 print_rtl (loop_dump_stream
, biv
->add_val
);
3945 fprintf (loop_dump_stream
, "\n");
3950 basestride
+= INTVAL (biv1
->add_val
);
3951 biv1
= biv1
->next_iv
;
3954 if (biv1
|| !basestride
)
3957 for (iv
= bl
->giv
; iv
; iv
= iv
->next_iv
)
3961 HOST_WIDE_INT index
= 0;
3963 HOST_WIDE_INT stride
= 0;
3964 int stride_sign
= 1;
3965 struct check_store_data d
;
3966 const char *ignore_reason
= NULL
;
3967 int size
= GET_MODE_SIZE (GET_MODE (iv
));
3969 /* See whether an induction variable is interesting to us and if
3970 not, report the reason. */
3971 if (iv
->giv_type
!= DEST_ADDR
)
3972 ignore_reason
= "giv is not a destination address";
3974 /* We are interested only in constant stride memory references
3975 in order to be able to compute density easily. */
3976 else if (GET_CODE (iv
->mult_val
) != CONST_INT
)
3977 ignore_reason
= "stride is not constant";
3981 stride
= INTVAL (iv
->mult_val
) * basestride
;
3988 /* On some targets, reversed order prefetches are not
3990 if (PREFETCH_NO_REVERSE_ORDER
&& stride_sign
< 0)
3991 ignore_reason
= "reversed order stride";
3993 /* Prefetch of accesses with an extreme stride might not be
3994 worthwhile, either. */
3995 else if (PREFETCH_NO_EXTREME_STRIDE
3996 && stride
> PREFETCH_EXTREME_STRIDE
)
3997 ignore_reason
= "extreme stride";
3999 /* Ignore GIVs with varying add values; we can't predict the
4000 value for the next iteration. */
4001 else if (!loop_invariant_p (loop
, iv
->add_val
))
4002 ignore_reason
= "giv has varying add value";
4004 /* Ignore GIVs in the nested loops; they ought to have been
4006 else if (iv
->maybe_multiple
)
4007 ignore_reason
= "giv is in nested loop";
4010 if (ignore_reason
!= NULL
)
4012 if (loop_dump_stream
)
4013 fprintf (loop_dump_stream
,
4014 "Prefetch: ignoring giv at %d: %s.\n",
4015 INSN_UID (iv
->insn
), ignore_reason
);
4019 /* Determine the pointer to the basic array we are examining. It is
4020 the sum of the BIV's initial value and the GIV's add_val. */
4021 address
= copy_rtx (iv
->add_val
);
4022 temp
= copy_rtx (bl
->initial_value
);
4024 address
= simplify_gen_binary (PLUS
, Pmode
, temp
, address
);
4025 index
= remove_constant_addition (&address
);
4028 d
.mem_address
= *iv
->location
;
4030 /* When the GIV is not always executed, we might be better off by
4031 not dirtying the cache pages. */
4032 if (PREFETCH_CONDITIONAL
|| iv
->always_executed
)
4033 note_stores (PATTERN (iv
->insn
), check_store
, &d
);
4036 if (loop_dump_stream
)
4037 fprintf (loop_dump_stream
, "Prefetch: Ignoring giv at %d: %s\n",
4038 INSN_UID (iv
->insn
), "in conditional code.");
4042 /* Attempt to find another prefetch to the same array and see if we
4043 can merge this one. */
4044 for (i
= 0; i
< num_prefetches
; i
++)
4045 if (rtx_equal_for_prefetch_p (address
, info
[i
].base_address
)
4046 && stride
== info
[i
].stride
)
4048 /* In case both access same array (same location
4049 just with small difference in constant indexes), merge
4050 the prefetches. Just do the later and the earlier will
4051 get prefetched from previous iteration.
4052 The artificial threshold should not be too small,
4053 but also not bigger than small portion of memory usually
4054 traversed by single loop. */
4055 if (index
>= info
[i
].index
4056 && index
- info
[i
].index
< PREFETCH_EXTREME_DIFFERENCE
)
4058 info
[i
].write
|= d
.mem_write
;
4059 info
[i
].bytes_accessed
+= size
;
4060 info
[i
].index
= index
;
4063 info
[num_prefetches
].base_address
= address
;
4068 if (index
< info
[i
].index
4069 && info
[i
].index
- index
< PREFETCH_EXTREME_DIFFERENCE
)
4071 info
[i
].write
|= d
.mem_write
;
4072 info
[i
].bytes_accessed
+= size
;
4078 /* Merging failed. */
4081 info
[num_prefetches
].giv
= iv
;
4082 info
[num_prefetches
].class = bl
;
4083 info
[num_prefetches
].index
= index
;
4084 info
[num_prefetches
].stride
= stride
;
4085 info
[num_prefetches
].base_address
= address
;
4086 info
[num_prefetches
].write
= d
.mem_write
;
4087 info
[num_prefetches
].bytes_accessed
= size
;
4089 if (num_prefetches
>= MAX_PREFETCHES
)
4091 if (loop_dump_stream
)
4092 fprintf (loop_dump_stream
,
4093 "Maximal number of prefetches exceeded.\n");
4100 for (i
= 0; i
< num_prefetches
; i
++)
4104 /* Attempt to calculate the total number of bytes fetched by all
4105 iterations of the loop. Avoid overflow. */
4106 if (LOOP_INFO (loop
)->n_iterations
4107 && ((unsigned HOST_WIDE_INT
) (0xffffffff / info
[i
].stride
)
4108 >= LOOP_INFO (loop
)->n_iterations
))
4109 info
[i
].total_bytes
= info
[i
].stride
* LOOP_INFO (loop
)->n_iterations
;
4111 info
[i
].total_bytes
= 0xffffffff;
4113 density
= info
[i
].bytes_accessed
* 100 / info
[i
].stride
;
4115 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4116 if (PREFETCH_ONLY_DENSE_MEM
)
4117 if (density
* 256 > PREFETCH_DENSE_MEM
* 100
4118 && (info
[i
].total_bytes
/ PREFETCH_BLOCK
4119 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN
))
4121 info
[i
].prefetch_before_loop
= 1;
4122 info
[i
].prefetch_in_loop
4123 = (info
[i
].total_bytes
/ PREFETCH_BLOCK
4124 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX
);
4128 info
[i
].prefetch_in_loop
= 0, info
[i
].prefetch_before_loop
= 0;
4129 if (loop_dump_stream
)
4130 fprintf (loop_dump_stream
,
4131 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4132 INSN_UID (info
[i
].giv
->insn
), density
);
4135 info
[i
].prefetch_in_loop
= 1, info
[i
].prefetch_before_loop
= 1;
4137 /* Find how many prefetch instructions we'll use within the loop. */
4138 if (info
[i
].prefetch_in_loop
!= 0)
4140 info
[i
].prefetch_in_loop
= ((info
[i
].stride
+ PREFETCH_BLOCK
- 1)
4142 num_real_prefetches
+= info
[i
].prefetch_in_loop
;
4144 num_real_write_prefetches
+= info
[i
].prefetch_in_loop
;
4148 /* Determine how many iterations ahead to prefetch within the loop, based
4149 on how many prefetches we currently expect to do within the loop. */
4150 if (num_real_prefetches
!= 0)
4152 if ((ahead
= SIMULTANEOUS_PREFETCHES
/ num_real_prefetches
) == 0)
4154 if (loop_dump_stream
)
4155 fprintf (loop_dump_stream
,
4156 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4157 SIMULTANEOUS_PREFETCHES
, num_real_prefetches
);
4158 num_real_prefetches
= 0, num_real_write_prefetches
= 0;
4161 /* We'll also use AHEAD to determine how many prefetch instructions to
4162 emit before a loop, so don't leave it zero. */
4164 ahead
= PREFETCH_BLOCKS_BEFORE_LOOP_MAX
;
4166 for (i
= 0; i
< num_prefetches
; i
++)
4168 /* Update if we've decided not to prefetch anything within the loop. */
4169 if (num_real_prefetches
== 0)
4170 info
[i
].prefetch_in_loop
= 0;
4172 /* Find how many prefetch instructions we'll use before the loop. */
4173 if (info
[i
].prefetch_before_loop
!= 0)
4175 int n
= info
[i
].total_bytes
/ PREFETCH_BLOCK
;
4178 info
[i
].prefetch_before_loop
= n
;
4179 num_prefetches_before
+= n
;
4181 num_write_prefetches_before
+= n
;
4184 if (loop_dump_stream
)
4186 if (info
[i
].prefetch_in_loop
== 0
4187 && info
[i
].prefetch_before_loop
== 0)
4189 fprintf (loop_dump_stream
, "Prefetch insn: %d",
4190 INSN_UID (info
[i
].giv
->insn
));
4191 fprintf (loop_dump_stream
,
4192 "; in loop: %d; before: %d; %s\n",
4193 info
[i
].prefetch_in_loop
,
4194 info
[i
].prefetch_before_loop
,
4195 info
[i
].write
? "read/write" : "read only");
4196 fprintf (loop_dump_stream
,
4197 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4198 (int) (info
[i
].bytes_accessed
* 100 / info
[i
].stride
),
4199 info
[i
].bytes_accessed
, info
[i
].total_bytes
);
4200 fprintf (loop_dump_stream
, " index: " HOST_WIDE_INT_PRINT_DEC
4201 "; stride: " HOST_WIDE_INT_PRINT_DEC
"; address: ",
4202 info
[i
].index
, info
[i
].stride
);
4203 print_rtl (loop_dump_stream
, info
[i
].base_address
);
4204 fprintf (loop_dump_stream
, "\n");
4208 if (num_real_prefetches
+ num_prefetches_before
> 0)
4210 /* Record that this loop uses prefetch instructions. */
4211 LOOP_INFO (loop
)->has_prefetch
= 1;
4213 if (loop_dump_stream
)
4215 fprintf (loop_dump_stream
, "Real prefetches needed within loop: %d (write: %d)\n",
4216 num_real_prefetches
, num_real_write_prefetches
);
4217 fprintf (loop_dump_stream
, "Real prefetches needed before loop: %d (write: %d)\n",
4218 num_prefetches_before
, num_write_prefetches_before
);
4222 for (i
= 0; i
< num_prefetches
; i
++)
4226 for (y
= 0; y
< info
[i
].prefetch_in_loop
; y
++)
4228 rtx loc
= copy_rtx (*info
[i
].giv
->location
);
4230 int bytes_ahead
= PREFETCH_BLOCK
* (ahead
+ y
);
4231 rtx before_insn
= info
[i
].giv
->insn
;
4232 rtx prev_insn
= PREV_INSN (info
[i
].giv
->insn
);
4235 /* We can save some effort by offsetting the address on
4236 architectures with offsettable memory references. */
4237 if (offsettable_address_p (0, VOIDmode
, loc
))
4238 loc
= plus_constant (loc
, bytes_ahead
);
4241 rtx reg
= gen_reg_rtx (Pmode
);
4242 loop_iv_add_mult_emit_before (loop
, loc
, const1_rtx
,
4243 GEN_INT (bytes_ahead
), reg
,
4249 /* Make sure the address operand is valid for prefetch. */
4250 if (! (*insn_data
[(int)CODE_FOR_prefetch
].operand
[0].predicate
)
4251 (loc
, insn_data
[(int)CODE_FOR_prefetch
].operand
[0].mode
))
4252 loc
= force_reg (Pmode
, loc
);
4253 emit_insn (gen_prefetch (loc
, GEN_INT (info
[i
].write
),
4257 emit_insn_before (seq
, before_insn
);
4259 /* Check all insns emitted and record the new GIV
4261 insn
= NEXT_INSN (prev_insn
);
4262 while (insn
!= before_insn
)
4264 insn
= check_insn_for_givs (loop
, insn
,
4265 info
[i
].giv
->always_executed
,
4266 info
[i
].giv
->maybe_multiple
);
4267 insn
= NEXT_INSN (insn
);
4271 if (PREFETCH_BEFORE_LOOP
)
4273 /* Emit insns before the loop to fetch the first cache lines or,
4274 if we're not prefetching within the loop, everything we expect
4276 for (y
= 0; y
< info
[i
].prefetch_before_loop
; y
++)
4278 rtx reg
= gen_reg_rtx (Pmode
);
4279 rtx loop_start
= loop
->start
;
4280 rtx init_val
= info
[i
].class->initial_value
;
4281 rtx add_val
= simplify_gen_binary (PLUS
, Pmode
,
4282 info
[i
].giv
->add_val
,
4283 GEN_INT (y
* PREFETCH_BLOCK
));
4285 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4286 non-constant INIT_VAL to have the same mode as REG, which
4287 in this case we know to be Pmode. */
4288 if (GET_MODE (init_val
) != Pmode
&& !CONSTANT_P (init_val
))
4293 init_val
= convert_to_mode (Pmode
, init_val
, 0);
4296 loop_insn_emit_before (loop
, 0, loop_start
, seq
);
4298 loop_iv_add_mult_emit_before (loop
, init_val
,
4299 info
[i
].giv
->mult_val
,
4300 add_val
, reg
, 0, loop_start
);
4301 emit_insn_before (gen_prefetch (reg
, GEN_INT (info
[i
].write
),
4311 /* A "basic induction variable" or biv is a pseudo reg that is set
4312 (within this loop) only by incrementing or decrementing it. */
4313 /* A "general induction variable" or giv is a pseudo reg whose
4314 value is a linear function of a biv. */
4316 /* Bivs are recognized by `basic_induction_var';
4317 Givs by `general_induction_var'. */
4319 /* Communication with routines called via `note_stores'. */
4321 static rtx note_insn
;
4323 /* Dummy register to have nonzero DEST_REG for DEST_ADDR type givs. */
4325 static rtx addr_placeholder
;
4327 /* ??? Unfinished optimizations, and possible future optimizations,
4328 for the strength reduction code. */
4330 /* ??? The interaction of biv elimination, and recognition of 'constant'
4331 bivs, may cause problems. */
4333 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4334 performance problems.
4336 Perhaps don't eliminate things that can be combined with an addressing
4337 mode. Find all givs that have the same biv, mult_val, and add_val;
4338 then for each giv, check to see if its only use dies in a following
4339 memory address. If so, generate a new memory address and check to see
4340 if it is valid. If it is valid, then store the modified memory address,
4341 otherwise, mark the giv as not done so that it will get its own iv. */
4343 /* ??? Could try to optimize branches when it is known that a biv is always
4346 /* ??? When replace a biv in a compare insn, we should replace with closest
4347 giv so that an optimized branch can still be recognized by the combiner,
4348 e.g. the VAX acb insn. */
4350 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4351 was rerun in loop_optimize whenever a register was added or moved.
4352 Also, some of the optimizations could be a little less conservative. */
4354 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4355 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4358 NOT_EVERY_ITERATION is 1 if current insn is not known to be executed at
4359 least once for every loop iteration except for the last one.
4361 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4365 for_each_insn_in_loop (loop
, fncall
)
4367 loop_insn_callback fncall
;
4369 int not_every_iteration
= 0;
4370 int maybe_multiple
= 0;
4371 int past_loop_latch
= 0;
4375 /* If loop_scan_start points to the loop exit test, we have to be wary of
4376 subversive use of gotos inside expression statements. */
4377 if (prev_nonnote_insn (loop
->scan_start
) != prev_nonnote_insn (loop
->start
))
4378 maybe_multiple
= back_branch_in_range_p (loop
, loop
->scan_start
);
4380 /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE. */
4381 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
4383 p
= next_insn_in_loop (loop
, p
))
4385 p
= fncall (loop
, p
, not_every_iteration
, maybe_multiple
);
4387 /* Past CODE_LABEL, we get to insns that may be executed multiple
4388 times. The only way we can be sure that they can't is if every
4389 jump insn between here and the end of the loop either
4390 returns, exits the loop, is a jump to a location that is still
4391 behind the label, or is a jump to the loop start. */
4393 if (GET_CODE (p
) == CODE_LABEL
)
4401 insn
= NEXT_INSN (insn
);
4402 if (insn
== loop
->scan_start
)
4404 if (insn
== loop
->end
)
4410 if (insn
== loop
->scan_start
)
4414 if (GET_CODE (insn
) == JUMP_INSN
4415 && GET_CODE (PATTERN (insn
)) != RETURN
4416 && (!any_condjump_p (insn
)
4417 || (JUMP_LABEL (insn
) != 0
4418 && JUMP_LABEL (insn
) != loop
->scan_start
4419 && !loop_insn_first_p (p
, JUMP_LABEL (insn
)))))
4427 /* Past a jump, we get to insns for which we can't count
4428 on whether they will be executed during each iteration. */
4429 /* This code appears twice in strength_reduce. There is also similar
4430 code in scan_loop. */
4431 if (GET_CODE (p
) == JUMP_INSN
4432 /* If we enter the loop in the middle, and scan around to the
4433 beginning, don't set not_every_iteration for that.
4434 This can be any kind of jump, since we want to know if insns
4435 will be executed if the loop is executed. */
4436 && !(JUMP_LABEL (p
) == loop
->top
4437 && ((NEXT_INSN (NEXT_INSN (p
)) == loop
->end
4438 && any_uncondjump_p (p
))
4439 || (NEXT_INSN (p
) == loop
->end
&& any_condjump_p (p
)))))
4443 /* If this is a jump outside the loop, then it also doesn't
4444 matter. Check to see if the target of this branch is on the
4445 loop->exits_labels list. */
4447 for (label
= loop
->exit_labels
; label
; label
= LABEL_NEXTREF (label
))
4448 if (XEXP (label
, 0) == JUMP_LABEL (p
))
4452 not_every_iteration
= 1;
4455 else if (GET_CODE (p
) == NOTE
)
4457 /* At the virtual top of a converted loop, insns are again known to
4458 be executed each iteration: logically, the loop begins here
4459 even though the exit code has been duplicated.
4461 Insns are also again known to be executed each iteration at
4462 the LOOP_CONT note. */
4463 if ((NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
4464 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_CONT
)
4466 not_every_iteration
= 0;
4467 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
4469 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)
4473 /* Note if we pass a loop latch. If we do, then we can not clear
4474 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4475 a loop since a jump before the last CODE_LABEL may have started
4476 a new loop iteration.
4478 Note that LOOP_TOP is only set for rotated loops and we need
4479 this check for all loops, so compare against the CODE_LABEL
4480 which immediately follows LOOP_START. */
4481 if (GET_CODE (p
) == JUMP_INSN
4482 && JUMP_LABEL (p
) == NEXT_INSN (loop
->start
))
4483 past_loop_latch
= 1;
4485 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4486 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4487 or not an insn is known to be executed each iteration of the
4488 loop, whether or not any iterations are known to occur.
4490 Therefore, if we have just passed a label and have no more labels
4491 between here and the test insn of the loop, and we have not passed
4492 a jump to the top of the loop, then we know these insns will be
4493 executed each iteration. */
4495 if (not_every_iteration
4497 && GET_CODE (p
) == CODE_LABEL
4498 && no_labels_between_p (p
, loop
->end
)
4499 && loop_insn_first_p (p
, loop
->cont
))
4500 not_every_iteration
= 0;
4505 loop_bivs_find (loop
)
4508 struct loop_regs
*regs
= LOOP_REGS (loop
);
4509 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4510 /* Temporary list pointers for traversing ivs->list. */
4511 struct iv_class
*bl
, **backbl
;
4515 for_each_insn_in_loop (loop
, check_insn_for_bivs
);
4517 /* Scan ivs->list to remove all regs that proved not to be bivs.
4518 Make a sanity check against regs->n_times_set. */
4519 for (backbl
= &ivs
->list
, bl
= *backbl
; bl
; bl
= bl
->next
)
4521 if (REG_IV_TYPE (ivs
, bl
->regno
) != BASIC_INDUCT
4522 /* Above happens if register modified by subreg, etc. */
4523 /* Make sure it is not recognized as a basic induction var: */
4524 || regs
->array
[bl
->regno
].n_times_set
!= bl
->biv_count
4525 /* If never incremented, it is invariant that we decided not to
4526 move. So leave it alone. */
4527 || ! bl
->incremented
)
4529 if (loop_dump_stream
)
4530 fprintf (loop_dump_stream
, "Biv %d: discarded, %s\n",
4532 (REG_IV_TYPE (ivs
, bl
->regno
) != BASIC_INDUCT
4533 ? "not induction variable"
4534 : (! bl
->incremented
? "never incremented"
4537 REG_IV_TYPE (ivs
, bl
->regno
) = NOT_BASIC_INDUCT
;
4544 if (loop_dump_stream
)
4545 fprintf (loop_dump_stream
, "Biv %d: verified\n", bl
->regno
);
4551 /* Determine how BIVS are initialized by looking through pre-header
4552 extended basic block. */
4554 loop_bivs_init_find (loop
)
4557 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4558 /* Temporary list pointers for traversing ivs->list. */
4559 struct iv_class
*bl
;
4563 /* Find initial value for each biv by searching backwards from loop_start,
4564 halting at first label. Also record any test condition. */
4567 for (p
= loop
->start
; p
&& GET_CODE (p
) != CODE_LABEL
; p
= PREV_INSN (p
))
4573 if (GET_CODE (p
) == CALL_INSN
)
4577 note_stores (PATTERN (p
), record_initial
, ivs
);
4579 /* Record any test of a biv that branches around the loop if no store
4580 between it and the start of loop. We only care about tests with
4581 constants and registers and only certain of those. */
4582 if (GET_CODE (p
) == JUMP_INSN
4583 && JUMP_LABEL (p
) != 0
4584 && next_real_insn (JUMP_LABEL (p
)) == next_real_insn (loop
->end
)
4585 && (test
= get_condition_for_loop (loop
, p
)) != 0
4586 && GET_CODE (XEXP (test
, 0)) == REG
4587 && REGNO (XEXP (test
, 0)) < max_reg_before_loop
4588 && (bl
= REG_IV_CLASS (ivs
, REGNO (XEXP (test
, 0)))) != 0
4589 && valid_initial_value_p (XEXP (test
, 1), p
, call_seen
, loop
->start
)
4590 && bl
->init_insn
== 0)
4592 /* If an NE test, we have an initial value! */
4593 if (GET_CODE (test
) == NE
)
4596 bl
->init_set
= gen_rtx_SET (VOIDmode
,
4597 XEXP (test
, 0), XEXP (test
, 1));
4600 bl
->initial_test
= test
;
4606 /* Look at the each biv and see if we can say anything better about its
4607 initial value from any initializing insns set up above. (This is done
4608 in two passes to avoid missing SETs in a PARALLEL.) */
4610 loop_bivs_check (loop
)
4613 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4614 /* Temporary list pointers for traversing ivs->list. */
4615 struct iv_class
*bl
;
4616 struct iv_class
**backbl
;
4618 for (backbl
= &ivs
->list
; (bl
= *backbl
); backbl
= &bl
->next
)
4623 if (! bl
->init_insn
)
4626 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4627 is a constant, use the value of that. */
4628 if (((note
= find_reg_note (bl
->init_insn
, REG_EQUAL
, 0)) != NULL
4629 && CONSTANT_P (XEXP (note
, 0)))
4630 || ((note
= find_reg_note (bl
->init_insn
, REG_EQUIV
, 0)) != NULL
4631 && CONSTANT_P (XEXP (note
, 0))))
4632 src
= XEXP (note
, 0);
4634 src
= SET_SRC (bl
->init_set
);
4636 if (loop_dump_stream
)
4637 fprintf (loop_dump_stream
,
4638 "Biv %d: initialized at insn %d: initial value ",
4639 bl
->regno
, INSN_UID (bl
->init_insn
));
4641 if ((GET_MODE (src
) == GET_MODE (regno_reg_rtx
[bl
->regno
])
4642 || GET_MODE (src
) == VOIDmode
)
4643 && valid_initial_value_p (src
, bl
->init_insn
,
4644 LOOP_INFO (loop
)->pre_header_has_call
,
4647 bl
->initial_value
= src
;
4649 if (loop_dump_stream
)
4651 print_simple_rtl (loop_dump_stream
, src
);
4652 fputc ('\n', loop_dump_stream
);
4655 /* If we can't make it a giv,
4656 let biv keep initial value of "itself". */
4657 else if (loop_dump_stream
)
4658 fprintf (loop_dump_stream
, "is complex\n");
4663 /* Search the loop for general induction variables. */
4666 loop_givs_find (loop
)
4669 for_each_insn_in_loop (loop
, check_insn_for_givs
);
4673 /* For each giv for which we still don't know whether or not it is
4674 replaceable, check to see if it is replaceable because its final value
4675 can be calculated. */
4678 loop_givs_check (loop
)
4681 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4682 struct iv_class
*bl
;
4684 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
4686 struct induction
*v
;
4688 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4689 if (! v
->replaceable
&& ! v
->not_replaceable
)
4690 check_final_value (loop
, v
);
4695 /* Return nonzero if it is possible to eliminate the biv BL provided
4696 all givs are reduced. This is possible if either the reg is not
4697 used outside the loop, or we can compute what its final value will
4701 loop_biv_eliminable_p (loop
, bl
, threshold
, insn_count
)
4703 struct iv_class
*bl
;
4707 /* For architectures with a decrement_and_branch_until_zero insn,
4708 don't do this if we put a REG_NONNEG note on the endtest for this
4711 #ifdef HAVE_decrement_and_branch_until_zero
4714 if (loop_dump_stream
)
4715 fprintf (loop_dump_stream
,
4716 "Cannot eliminate nonneg biv %d.\n", bl
->regno
);
4721 /* Check that biv is used outside loop or if it has a final value.
4722 Compare against bl->init_insn rather than loop->start. We aren't
4723 concerned with any uses of the biv between init_insn and
4724 loop->start since these won't be affected by the value of the biv
4725 elsewhere in the function, so long as init_insn doesn't use the
4728 if ((REGNO_LAST_LUID (bl
->regno
) < INSN_LUID (loop
->end
)
4730 && INSN_UID (bl
->init_insn
) < max_uid_for_loop
4731 && REGNO_FIRST_LUID (bl
->regno
) >= INSN_LUID (bl
->init_insn
)
4732 && ! reg_mentioned_p (bl
->biv
->dest_reg
, SET_SRC (bl
->init_set
)))
4733 || (bl
->final_value
= final_biv_value (loop
, bl
)))
4734 return maybe_eliminate_biv (loop
, bl
, 0, threshold
, insn_count
);
4736 if (loop_dump_stream
)
4738 fprintf (loop_dump_stream
,
4739 "Cannot eliminate biv %d.\n",
4741 fprintf (loop_dump_stream
,
4742 "First use: insn %d, last use: insn %d.\n",
4743 REGNO_FIRST_UID (bl
->regno
),
4744 REGNO_LAST_UID (bl
->regno
));
4750 /* Reduce each giv of BL that we have decided to reduce. */
4753 loop_givs_reduce (loop
, bl
)
4755 struct iv_class
*bl
;
4757 struct induction
*v
;
4759 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4761 struct induction
*tv
;
4762 if (! v
->ignore
&& v
->same
== 0)
4764 int auto_inc_opt
= 0;
4766 /* If the code for derived givs immediately below has already
4767 allocated a new_reg, we must keep it. */
4769 v
->new_reg
= gen_reg_rtx (v
->mode
);
4772 /* If the target has auto-increment addressing modes, and
4773 this is an address giv, then try to put the increment
4774 immediately after its use, so that flow can create an
4775 auto-increment addressing mode. */
4776 if (v
->giv_type
== DEST_ADDR
&& bl
->biv_count
== 1
4777 && bl
->biv
->always_executed
&& ! bl
->biv
->maybe_multiple
4778 /* We don't handle reversed biv's because bl->biv->insn
4779 does not have a valid INSN_LUID. */
4781 && v
->always_executed
&& ! v
->maybe_multiple
4782 && INSN_UID (v
->insn
) < max_uid_for_loop
)
4784 /* If other giv's have been combined with this one, then
4785 this will work only if all uses of the other giv's occur
4786 before this giv's insn. This is difficult to check.
4788 We simplify this by looking for the common case where
4789 there is one DEST_REG giv, and this giv's insn is the
4790 last use of the dest_reg of that DEST_REG giv. If the
4791 increment occurs after the address giv, then we can
4792 perform the optimization. (Otherwise, the increment
4793 would have to go before other_giv, and we would not be
4794 able to combine it with the address giv to get an
4795 auto-inc address.) */
4796 if (v
->combined_with
)
4798 struct induction
*other_giv
= 0;
4800 for (tv
= bl
->giv
; tv
; tv
= tv
->next_iv
)
4808 if (! tv
&& other_giv
4809 && REGNO (other_giv
->dest_reg
) < max_reg_before_loop
4810 && (REGNO_LAST_UID (REGNO (other_giv
->dest_reg
))
4811 == INSN_UID (v
->insn
))
4812 && INSN_LUID (v
->insn
) < INSN_LUID (bl
->biv
->insn
))
4815 /* Check for case where increment is before the address
4816 giv. Do this test in "loop order". */
4817 else if ((INSN_LUID (v
->insn
) > INSN_LUID (bl
->biv
->insn
)
4818 && (INSN_LUID (v
->insn
) < INSN_LUID (loop
->scan_start
)
4819 || (INSN_LUID (bl
->biv
->insn
)
4820 > INSN_LUID (loop
->scan_start
))))
4821 || (INSN_LUID (v
->insn
) < INSN_LUID (loop
->scan_start
)
4822 && (INSN_LUID (loop
->scan_start
)
4823 < INSN_LUID (bl
->biv
->insn
))))
4832 /* We can't put an insn immediately after one setting
4833 cc0, or immediately before one using cc0. */
4834 if ((auto_inc_opt
== 1 && sets_cc0_p (PATTERN (v
->insn
)))
4835 || (auto_inc_opt
== -1
4836 && (prev
= prev_nonnote_insn (v
->insn
)) != 0
4838 && sets_cc0_p (PATTERN (prev
))))
4844 v
->auto_inc_opt
= 1;
4848 /* For each place where the biv is incremented, add an insn
4849 to increment the new, reduced reg for the giv. */
4850 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
4854 /* Skip if location is the same as a previous one. */
4858 insert_before
= NEXT_INSN (tv
->insn
);
4859 else if (auto_inc_opt
== 1)
4860 insert_before
= NEXT_INSN (v
->insn
);
4862 insert_before
= v
->insn
;
4864 if (tv
->mult_val
== const1_rtx
)
4865 loop_iv_add_mult_emit_before (loop
, tv
->add_val
, v
->mult_val
,
4866 v
->new_reg
, v
->new_reg
,
4868 else /* tv->mult_val == const0_rtx */
4869 /* A multiply is acceptable here
4870 since this is presumed to be seldom executed. */
4871 loop_iv_add_mult_emit_before (loop
, tv
->add_val
, v
->mult_val
,
4872 v
->add_val
, v
->new_reg
,
4876 /* Add code at loop start to initialize giv's reduced reg. */
4878 loop_iv_add_mult_hoist (loop
,
4879 extend_value_for_giv (v
, bl
->initial_value
),
4880 v
->mult_val
, v
->add_val
, v
->new_reg
);
4886 /* Check for givs whose first use is their definition and whose
4887 last use is the definition of another giv. If so, it is likely
4888 dead and should not be used to derive another giv nor to
4892 loop_givs_dead_check (loop
, bl
)
4893 struct loop
*loop ATTRIBUTE_UNUSED
;
4894 struct iv_class
*bl
;
4896 struct induction
*v
;
4898 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4901 || (v
->same
&& v
->same
->ignore
))
4904 if (v
->giv_type
== DEST_REG
4905 && REGNO_FIRST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v
->insn
))
4907 struct induction
*v1
;
4909 for (v1
= bl
->giv
; v1
; v1
= v1
->next_iv
)
4910 if (REGNO_LAST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v1
->insn
))
4918 loop_givs_rescan (loop
, bl
, reg_map
)
4920 struct iv_class
*bl
;
4923 struct induction
*v
;
4925 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4927 if (v
->same
&& v
->same
->ignore
)
4933 /* Update expression if this was combined, in case other giv was
4936 v
->new_reg
= replace_rtx (v
->new_reg
,
4937 v
->same
->dest_reg
, v
->same
->new_reg
);
4939 /* See if this register is known to be a pointer to something. If
4940 so, see if we can find the alignment. First see if there is a
4941 destination register that is a pointer. If so, this shares the
4942 alignment too. Next see if we can deduce anything from the
4943 computational information. If not, and this is a DEST_ADDR
4944 giv, at least we know that it's a pointer, though we don't know
4946 if (GET_CODE (v
->new_reg
) == REG
4947 && v
->giv_type
== DEST_REG
4948 && REG_POINTER (v
->dest_reg
))
4949 mark_reg_pointer (v
->new_reg
,
4950 REGNO_POINTER_ALIGN (REGNO (v
->dest_reg
)));
4951 else if (GET_CODE (v
->new_reg
) == REG
4952 && REG_POINTER (v
->src_reg
))
4954 unsigned int align
= REGNO_POINTER_ALIGN (REGNO (v
->src_reg
));
4957 || GET_CODE (v
->add_val
) != CONST_INT
4958 || INTVAL (v
->add_val
) % (align
/ BITS_PER_UNIT
) != 0)
4961 mark_reg_pointer (v
->new_reg
, align
);
4963 else if (GET_CODE (v
->new_reg
) == REG
4964 && GET_CODE (v
->add_val
) == REG
4965 && REG_POINTER (v
->add_val
))
4967 unsigned int align
= REGNO_POINTER_ALIGN (REGNO (v
->add_val
));
4969 if (align
== 0 || GET_CODE (v
->mult_val
) != CONST_INT
4970 || INTVAL (v
->mult_val
) % (align
/ BITS_PER_UNIT
) != 0)
4973 mark_reg_pointer (v
->new_reg
, align
);
4975 else if (GET_CODE (v
->new_reg
) == REG
&& v
->giv_type
== DEST_ADDR
)
4976 mark_reg_pointer (v
->new_reg
, 0);
4978 if (v
->giv_type
== DEST_ADDR
)
4979 /* Store reduced reg as the address in the memref where we found
4981 validate_change (v
->insn
, v
->location
, v
->new_reg
, 0);
4982 else if (v
->replaceable
)
4984 reg_map
[REGNO (v
->dest_reg
)] = v
->new_reg
;
4988 rtx original_insn
= v
->insn
;
4991 /* Not replaceable; emit an insn to set the original giv reg from
4992 the reduced giv, same as above. */
4993 v
->insn
= loop_insn_emit_after (loop
, 0, original_insn
,
4994 gen_move_insn (v
->dest_reg
,
4997 /* The original insn may have a REG_EQUAL note. This note is
4998 now incorrect and may result in invalid substitutions later.
4999 The original insn is dead, but may be part of a libcall
5000 sequence, which doesn't seem worth the bother of handling. */
5001 note
= find_reg_note (original_insn
, REG_EQUAL
, NULL_RTX
);
5003 remove_note (original_insn
, note
);
5006 /* When a loop is reversed, givs which depend on the reversed
5007 biv, and which are live outside the loop, must be set to their
5008 correct final value. This insn is only needed if the giv is
5009 not replaceable. The correct final value is the same as the
5010 value that the giv starts the reversed loop with. */
5011 if (bl
->reversed
&& ! v
->replaceable
)
5012 loop_iv_add_mult_sink (loop
,
5013 extend_value_for_giv (v
, bl
->initial_value
),
5014 v
->mult_val
, v
->add_val
, v
->dest_reg
);
5015 else if (v
->final_value
)
5016 loop_insn_sink_or_swim (loop
,
5017 gen_load_of_final_value (v
->dest_reg
,
5020 if (loop_dump_stream
)
5022 fprintf (loop_dump_stream
, "giv at %d reduced to ",
5023 INSN_UID (v
->insn
));
5024 print_simple_rtl (loop_dump_stream
, v
->new_reg
);
5025 fprintf (loop_dump_stream
, "\n");
5032 loop_giv_reduce_benefit (loop
, bl
, v
, test_reg
)
5033 struct loop
*loop ATTRIBUTE_UNUSED
;
5034 struct iv_class
*bl
;
5035 struct induction
*v
;
5041 benefit
= v
->benefit
;
5042 PUT_MODE (test_reg
, v
->mode
);
5043 add_cost
= iv_add_mult_cost (bl
->biv
->add_val
, v
->mult_val
,
5044 test_reg
, test_reg
);
5046 /* Reduce benefit if not replaceable, since we will insert a
5047 move-insn to replace the insn that calculates this giv. Don't do
5048 this unless the giv is a user variable, since it will often be
5049 marked non-replaceable because of the duplication of the exit
5050 code outside the loop. In such a case, the copies we insert are
5051 dead and will be deleted. So they don't have a cost. Similar
5052 situations exist. */
5053 /* ??? The new final_[bg]iv_value code does a much better job of
5054 finding replaceable giv's, and hence this code may no longer be
5056 if (! v
->replaceable
&& ! bl
->eliminable
5057 && REG_USERVAR_P (v
->dest_reg
))
5058 benefit
-= copy_cost
;
5060 /* Decrease the benefit to count the add-insns that we will insert
5061 to increment the reduced reg for the giv. ??? This can
5062 overestimate the run-time cost of the additional insns, e.g. if
5063 there are multiple basic blocks that increment the biv, but only
5064 one of these blocks is executed during each iteration. There is
5065 no good way to detect cases like this with the current structure
5066 of the loop optimizer. This code is more accurate for
5067 determining code size than run-time benefits. */
5068 benefit
-= add_cost
* bl
->biv_count
;
5070 /* Decide whether to strength-reduce this giv or to leave the code
5071 unchanged (recompute it from the biv each time it is used). This
5072 decision can be made independently for each giv. */
5075 /* Attempt to guess whether autoincrement will handle some of the
5076 new add insns; if so, increase BENEFIT (undo the subtraction of
5077 add_cost that was done above). */
5078 if (v
->giv_type
== DEST_ADDR
5079 /* Increasing the benefit is risky, since this is only a guess.
5080 Avoid increasing register pressure in cases where there would
5081 be no other benefit from reducing this giv. */
5083 && GET_CODE (v
->mult_val
) == CONST_INT
)
5085 int size
= GET_MODE_SIZE (GET_MODE (v
->mem
));
5087 if (HAVE_POST_INCREMENT
5088 && INTVAL (v
->mult_val
) == size
)
5089 benefit
+= add_cost
* bl
->biv_count
;
5090 else if (HAVE_PRE_INCREMENT
5091 && INTVAL (v
->mult_val
) == size
)
5092 benefit
+= add_cost
* bl
->biv_count
;
5093 else if (HAVE_POST_DECREMENT
5094 && -INTVAL (v
->mult_val
) == size
)
5095 benefit
+= add_cost
* bl
->biv_count
;
5096 else if (HAVE_PRE_DECREMENT
5097 && -INTVAL (v
->mult_val
) == size
)
5098 benefit
+= add_cost
* bl
->biv_count
;
5106 /* Free IV structures for LOOP. */
5109 loop_ivs_free (loop
)
5112 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5113 struct iv_class
*iv
= ivs
->list
;
5119 struct iv_class
*next
= iv
->next
;
5120 struct induction
*induction
;
5121 struct induction
*next_induction
;
5123 for (induction
= iv
->biv
; induction
; induction
= next_induction
)
5125 next_induction
= induction
->next_iv
;
5128 for (induction
= iv
->giv
; induction
; induction
= next_induction
)
5130 next_induction
= induction
->next_iv
;
5140 /* Perform strength reduction and induction variable elimination.
5142 Pseudo registers created during this function will be beyond the
5143 last valid index in several tables including
5144 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
5145 problem here, because the added registers cannot be givs outside of
5146 their loop, and hence will never be reconsidered. But scan_loop
5147 must check regnos to make sure they are in bounds. */
5150 strength_reduce (loop
, flags
)
5154 struct loop_info
*loop_info
= LOOP_INFO (loop
);
5155 struct loop_regs
*regs
= LOOP_REGS (loop
);
5156 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5158 /* Temporary list pointer for traversing ivs->list. */
5159 struct iv_class
*bl
;
5160 /* Ratio of extra register life span we can justify
5161 for saving an instruction. More if loop doesn't call subroutines
5162 since in that case saving an insn makes more difference
5163 and more registers are available. */
5164 /* ??? could set this to last value of threshold in move_movables */
5165 int threshold
= (loop_info
->has_call
? 1 : 2) * (3 + n_non_fixed_regs
);
5166 /* Map of pseudo-register replacements. */
5167 rtx
*reg_map
= NULL
;
5169 int unrolled_insn_copies
= 0;
5170 rtx test_reg
= gen_rtx_REG (word_mode
, LAST_VIRTUAL_REGISTER
+ 1);
5171 int insn_count
= count_insns_in_loop (loop
);
5173 addr_placeholder
= gen_reg_rtx (Pmode
);
5175 ivs
->n_regs
= max_reg_before_loop
;
5176 ivs
->regs
= (struct iv
*) xcalloc (ivs
->n_regs
, sizeof (struct iv
));
5178 /* Find all BIVs in loop. */
5179 loop_bivs_find (loop
);
5181 /* Exit if there are no bivs. */
5184 /* Can still unroll the loop anyways, but indicate that there is no
5185 strength reduction info available. */
5186 if (flags
& LOOP_UNROLL
)
5187 unroll_loop (loop
, insn_count
, 0);
5189 loop_ivs_free (loop
);
5193 /* Determine how BIVS are initialized by looking through pre-header
5194 extended basic block. */
5195 loop_bivs_init_find (loop
);
5197 /* Look at the each biv and see if we can say anything better about its
5198 initial value from any initializing insns set up above. */
5199 loop_bivs_check (loop
);
5201 /* Search the loop for general induction variables. */
5202 loop_givs_find (loop
);
5204 /* Try to calculate and save the number of loop iterations. This is
5205 set to zero if the actual number can not be calculated. This must
5206 be called after all giv's have been identified, since otherwise it may
5207 fail if the iteration variable is a giv. */
5208 loop_iterations (loop
);
5210 #ifdef HAVE_prefetch
5211 if (flags
& LOOP_PREFETCH
)
5212 emit_prefetch_instructions (loop
);
5215 /* Now for each giv for which we still don't know whether or not it is
5216 replaceable, check to see if it is replaceable because its final value
5217 can be calculated. This must be done after loop_iterations is called,
5218 so that final_giv_value will work correctly. */
5219 loop_givs_check (loop
);
5221 /* Try to prove that the loop counter variable (if any) is always
5222 nonnegative; if so, record that fact with a REG_NONNEG note
5223 so that "decrement and branch until zero" insn can be used. */
5224 check_dbra_loop (loop
, insn_count
);
5226 /* Create reg_map to hold substitutions for replaceable giv regs.
5227 Some givs might have been made from biv increments, so look at
5228 ivs->reg_iv_type for a suitable size. */
5229 reg_map_size
= ivs
->n_regs
;
5230 reg_map
= (rtx
*) xcalloc (reg_map_size
, sizeof (rtx
));
5232 /* Examine each iv class for feasibility of strength reduction/induction
5233 variable elimination. */
5235 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
5237 struct induction
*v
;
5240 /* Test whether it will be possible to eliminate this biv
5241 provided all givs are reduced. */
5242 bl
->eliminable
= loop_biv_eliminable_p (loop
, bl
, threshold
, insn_count
);
5244 /* This will be true at the end, if all givs which depend on this
5245 biv have been strength reduced.
5246 We can't (currently) eliminate the biv unless this is so. */
5247 bl
->all_reduced
= 1;
5249 /* Check each extension dependent giv in this class to see if its
5250 root biv is safe from wrapping in the interior mode. */
5251 check_ext_dependent_givs (bl
, loop_info
);
5253 /* Combine all giv's for this iv_class. */
5254 combine_givs (regs
, bl
);
5256 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5258 struct induction
*tv
;
5260 if (v
->ignore
|| v
->same
)
5263 benefit
= loop_giv_reduce_benefit (loop
, bl
, v
, test_reg
);
5265 /* If an insn is not to be strength reduced, then set its ignore
5266 flag, and clear bl->all_reduced. */
5268 /* A giv that depends on a reversed biv must be reduced if it is
5269 used after the loop exit, otherwise, it would have the wrong
5270 value after the loop exit. To make it simple, just reduce all
5271 of such giv's whether or not we know they are used after the loop
5274 if (! flag_reduce_all_givs
5275 && v
->lifetime
* threshold
* benefit
< insn_count
5278 if (loop_dump_stream
)
5279 fprintf (loop_dump_stream
,
5280 "giv of insn %d not worth while, %d vs %d.\n",
5282 v
->lifetime
* threshold
* benefit
, insn_count
);
5284 bl
->all_reduced
= 0;
5288 /* Check that we can increment the reduced giv without a
5289 multiply insn. If not, reject it. */
5291 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
5292 if (tv
->mult_val
== const1_rtx
5293 && ! product_cheap_p (tv
->add_val
, v
->mult_val
))
5295 if (loop_dump_stream
)
5296 fprintf (loop_dump_stream
,
5297 "giv of insn %d: would need a multiply.\n",
5298 INSN_UID (v
->insn
));
5300 bl
->all_reduced
= 0;
5306 /* Check for givs whose first use is their definition and whose
5307 last use is the definition of another giv. If so, it is likely
5308 dead and should not be used to derive another giv nor to
5310 loop_givs_dead_check (loop
, bl
);
5312 /* Reduce each giv that we decided to reduce. */
5313 loop_givs_reduce (loop
, bl
);
5315 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
5318 For each giv register that can be reduced now: if replaceable,
5319 substitute reduced reg wherever the old giv occurs;
5320 else add new move insn "giv_reg = reduced_reg". */
5321 loop_givs_rescan (loop
, bl
, reg_map
);
5323 /* All the givs based on the biv bl have been reduced if they
5326 /* For each giv not marked as maybe dead that has been combined with a
5327 second giv, clear any "maybe dead" mark on that second giv.
5328 v->new_reg will either be or refer to the register of the giv it
5331 Doing this clearing avoids problems in biv elimination where
5332 a giv's new_reg is a complex value that can't be put in the
5333 insn but the giv combined with (with a reg as new_reg) is
5334 marked maybe_dead. Since the register will be used in either
5335 case, we'd prefer it be used from the simpler giv. */
5337 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5338 if (! v
->maybe_dead
&& v
->same
)
5339 v
->same
->maybe_dead
= 0;
5341 /* Try to eliminate the biv, if it is a candidate.
5342 This won't work if ! bl->all_reduced,
5343 since the givs we planned to use might not have been reduced.
5345 We have to be careful that we didn't initially think we could
5346 eliminate this biv because of a giv that we now think may be
5347 dead and shouldn't be used as a biv replacement.
5349 Also, there is the possibility that we may have a giv that looks
5350 like it can be used to eliminate a biv, but the resulting insn
5351 isn't valid. This can happen, for example, on the 88k, where a
5352 JUMP_INSN can compare a register only with zero. Attempts to
5353 replace it with a compare with a constant will fail.
5355 Note that in cases where this call fails, we may have replaced some
5356 of the occurrences of the biv with a giv, but no harm was done in
5357 doing so in the rare cases where it can occur. */
5359 if (bl
->all_reduced
== 1 && bl
->eliminable
5360 && maybe_eliminate_biv (loop
, bl
, 1, threshold
, insn_count
))
5362 /* ?? If we created a new test to bypass the loop entirely,
5363 or otherwise drop straight in, based on this test, then
5364 we might want to rewrite it also. This way some later
5365 pass has more hope of removing the initialization of this
5368 /* If final_value != 0, then the biv may be used after loop end
5369 and we must emit an insn to set it just in case.
5371 Reversed bivs already have an insn after the loop setting their
5372 value, so we don't need another one. We can't calculate the
5373 proper final value for such a biv here anyways. */
5374 if (bl
->final_value
&& ! bl
->reversed
)
5375 loop_insn_sink_or_swim (loop
,
5376 gen_load_of_final_value (bl
->biv
->dest_reg
,
5379 if (loop_dump_stream
)
5380 fprintf (loop_dump_stream
, "Reg %d: biv eliminated\n",
5383 /* See above note wrt final_value. But since we couldn't eliminate
5384 the biv, we must set the value after the loop instead of before. */
5385 else if (bl
->final_value
&& ! bl
->reversed
)
5386 loop_insn_sink (loop
, gen_load_of_final_value (bl
->biv
->dest_reg
,
5390 /* Go through all the instructions in the loop, making all the
5391 register substitutions scheduled in REG_MAP. */
5393 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
5394 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
5395 || GET_CODE (p
) == CALL_INSN
)
5397 replace_regs (PATTERN (p
), reg_map
, reg_map_size
, 0);
5398 replace_regs (REG_NOTES (p
), reg_map
, reg_map_size
, 0);
5402 if (loop_info
->n_iterations
> 0)
5404 /* When we completely unroll a loop we will likely not need the increment
5405 of the loop BIV and we will not need the conditional branch at the
5407 unrolled_insn_copies
= insn_count
- 2;
5410 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5411 need the comparison before the conditional branch at the end of the
5413 unrolled_insn_copies
-= 1;
5416 /* We'll need one copy for each loop iteration. */
5417 unrolled_insn_copies
*= loop_info
->n_iterations
;
5419 /* A little slop to account for the ability to remove initialization
5420 code, better CSE, and other secondary benefits of completely
5421 unrolling some loops. */
5422 unrolled_insn_copies
-= 1;
5424 /* Clamp the value. */
5425 if (unrolled_insn_copies
< 0)
5426 unrolled_insn_copies
= 0;
5429 /* Unroll loops from within strength reduction so that we can use the
5430 induction variable information that strength_reduce has already
5431 collected. Always unroll loops that would be as small or smaller
5432 unrolled than when rolled. */
5433 if ((flags
& LOOP_UNROLL
)
5434 || ((flags
& LOOP_AUTO_UNROLL
)
5435 && loop_info
->n_iterations
> 0
5436 && unrolled_insn_copies
<= insn_count
))
5437 unroll_loop (loop
, insn_count
, 1);
5439 #ifdef HAVE_doloop_end
5440 if (HAVE_doloop_end
&& (flags
& LOOP_BCT
) && flag_branch_on_count_reg
)
5441 doloop_optimize (loop
);
5442 #endif /* HAVE_doloop_end */
5444 /* In case number of iterations is known, drop branch prediction note
5445 in the branch. Do that only in second loop pass, as loop unrolling
5446 may change the number of iterations performed. */
5447 if (flags
& LOOP_BCT
)
5449 unsigned HOST_WIDE_INT n
5450 = loop_info
->n_iterations
/ loop_info
->unroll_number
;
5452 predict_insn (prev_nonnote_insn (loop
->end
), PRED_LOOP_ITERATIONS
,
5453 REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ n
);
5456 if (loop_dump_stream
)
5457 fprintf (loop_dump_stream
, "\n");
5459 loop_ivs_free (loop
);
5464 /*Record all basic induction variables calculated in the insn. */
5466 check_insn_for_bivs (loop
, p
, not_every_iteration
, maybe_multiple
)
5469 int not_every_iteration
;
5472 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5479 if (GET_CODE (p
) == INSN
5480 && (set
= single_set (p
))
5481 && GET_CODE (SET_DEST (set
)) == REG
)
5483 dest_reg
= SET_DEST (set
);
5484 if (REGNO (dest_reg
) < max_reg_before_loop
5485 && REGNO (dest_reg
) >= FIRST_PSEUDO_REGISTER
5486 && REG_IV_TYPE (ivs
, REGNO (dest_reg
)) != NOT_BASIC_INDUCT
)
5488 if (basic_induction_var (loop
, SET_SRC (set
),
5489 GET_MODE (SET_SRC (set
)),
5490 dest_reg
, p
, &inc_val
, &mult_val
,
5493 /* It is a possible basic induction variable.
5494 Create and initialize an induction structure for it. */
5497 = (struct induction
*) xmalloc (sizeof (struct induction
));
5499 record_biv (loop
, v
, p
, dest_reg
, inc_val
, mult_val
, location
,
5500 not_every_iteration
, maybe_multiple
);
5501 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = BASIC_INDUCT
;
5503 else if (REGNO (dest_reg
) < ivs
->n_regs
)
5504 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = NOT_BASIC_INDUCT
;
5510 /* Record all givs calculated in the insn.
5511 A register is a giv if: it is only set once, it is a function of a
5512 biv and a constant (or invariant), and it is not a biv. */
5514 check_insn_for_givs (loop
, p
, not_every_iteration
, maybe_multiple
)
5517 int not_every_iteration
;
5520 struct loop_regs
*regs
= LOOP_REGS (loop
);
5523 /* Look for a general induction variable in a register. */
5524 if (GET_CODE (p
) == INSN
5525 && (set
= single_set (p
))
5526 && GET_CODE (SET_DEST (set
)) == REG
5527 && ! regs
->array
[REGNO (SET_DEST (set
))].may_not_optimize
)
5536 rtx last_consec_insn
;
5538 dest_reg
= SET_DEST (set
);
5539 if (REGNO (dest_reg
) < FIRST_PSEUDO_REGISTER
)
5542 if (/* SET_SRC is a giv. */
5543 (general_induction_var (loop
, SET_SRC (set
), &src_reg
, &add_val
,
5544 &mult_val
, &ext_val
, 0, &benefit
, VOIDmode
)
5545 /* Equivalent expression is a giv. */
5546 || ((regnote
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
5547 && general_induction_var (loop
, XEXP (regnote
, 0), &src_reg
,
5548 &add_val
, &mult_val
, &ext_val
, 0,
5549 &benefit
, VOIDmode
)))
5550 /* Don't try to handle any regs made by loop optimization.
5551 We have nothing on them in regno_first_uid, etc. */
5552 && REGNO (dest_reg
) < max_reg_before_loop
5553 /* Don't recognize a BASIC_INDUCT_VAR here. */
5554 && dest_reg
!= src_reg
5555 /* This must be the only place where the register is set. */
5556 && (regs
->array
[REGNO (dest_reg
)].n_times_set
== 1
5557 /* or all sets must be consecutive and make a giv. */
5558 || (benefit
= consec_sets_giv (loop
, benefit
, p
,
5560 &add_val
, &mult_val
, &ext_val
,
5561 &last_consec_insn
))))
5564 = (struct induction
*) xmalloc (sizeof (struct induction
));
5566 /* If this is a library call, increase benefit. */
5567 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
5568 benefit
+= libcall_benefit (p
);
5570 /* Skip the consecutive insns, if there are any. */
5571 if (regs
->array
[REGNO (dest_reg
)].n_times_set
!= 1)
5572 p
= last_consec_insn
;
5574 record_giv (loop
, v
, p
, src_reg
, dest_reg
, mult_val
, add_val
,
5575 ext_val
, benefit
, DEST_REG
, not_every_iteration
,
5576 maybe_multiple
, (rtx
*) 0);
5581 /* Look for givs which are memory addresses. */
5582 if (GET_CODE (p
) == INSN
)
5583 find_mem_givs (loop
, PATTERN (p
), p
, not_every_iteration
,
5586 /* Update the status of whether giv can derive other givs. This can
5587 change when we pass a label or an insn that updates a biv. */
5588 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
5589 || GET_CODE (p
) == CODE_LABEL
)
5590 update_giv_derive (loop
, p
);
5594 /* Return 1 if X is a valid source for an initial value (or as value being
5595 compared against in an initial test).
5597 X must be either a register or constant and must not be clobbered between
5598 the current insn and the start of the loop.
5600 INSN is the insn containing X. */
5603 valid_initial_value_p (x
, insn
, call_seen
, loop_start
)
5612 /* Only consider pseudos we know about initialized in insns whose luids
5614 if (GET_CODE (x
) != REG
5615 || REGNO (x
) >= max_reg_before_loop
)
5618 /* Don't use call-clobbered registers across a call which clobbers it. On
5619 some machines, don't use any hard registers at all. */
5620 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
5621 && (SMALL_REGISTER_CLASSES
5622 || (call_used_regs
[REGNO (x
)] && call_seen
)))
5625 /* Don't use registers that have been clobbered before the start of the
5627 if (reg_set_between_p (x
, insn
, loop_start
))
5633 /* Scan X for memory refs and check each memory address
5634 as a possible giv. INSN is the insn whose pattern X comes from.
5635 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5636 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
5637 more than once in each loop iteration. */
5640 find_mem_givs (loop
, x
, insn
, not_every_iteration
, maybe_multiple
)
5641 const struct loop
*loop
;
5644 int not_every_iteration
, maybe_multiple
;
5653 code
= GET_CODE (x
);
5678 /* This code used to disable creating GIVs with mult_val == 1 and
5679 add_val == 0. However, this leads to lost optimizations when
5680 it comes time to combine a set of related DEST_ADDR GIVs, since
5681 this one would not be seen. */
5683 if (general_induction_var (loop
, XEXP (x
, 0), &src_reg
, &add_val
,
5684 &mult_val
, &ext_val
, 1, &benefit
,
5687 /* Found one; record it. */
5689 = (struct induction
*) xmalloc (sizeof (struct induction
));
5691 record_giv (loop
, v
, insn
, src_reg
, addr_placeholder
, mult_val
,
5692 add_val
, ext_val
, benefit
, DEST_ADDR
,
5693 not_every_iteration
, maybe_multiple
, &XEXP (x
, 0));
5704 /* Recursively scan the subexpressions for other mem refs. */
5706 fmt
= GET_RTX_FORMAT (code
);
5707 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
5709 find_mem_givs (loop
, XEXP (x
, i
), insn
, not_every_iteration
,
5711 else if (fmt
[i
] == 'E')
5712 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
5713 find_mem_givs (loop
, XVECEXP (x
, i
, j
), insn
, not_every_iteration
,
5717 /* Fill in the data about one biv update.
5718 V is the `struct induction' in which we record the biv. (It is
5719 allocated by the caller, with alloca.)
5720 INSN is the insn that sets it.
5721 DEST_REG is the biv's reg.
5723 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5724 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5725 being set to INC_VAL.
5727 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5728 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5729 can be executed more than once per iteration. If MAYBE_MULTIPLE
5730 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5731 executed exactly once per iteration. */
5734 record_biv (loop
, v
, insn
, dest_reg
, inc_val
, mult_val
, location
,
5735 not_every_iteration
, maybe_multiple
)
5737 struct induction
*v
;
5743 int not_every_iteration
;
5746 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5747 struct iv_class
*bl
;
5750 v
->src_reg
= dest_reg
;
5751 v
->dest_reg
= dest_reg
;
5752 v
->mult_val
= mult_val
;
5753 v
->add_val
= inc_val
;
5754 v
->ext_dependent
= NULL_RTX
;
5755 v
->location
= location
;
5756 v
->mode
= GET_MODE (dest_reg
);
5757 v
->always_computable
= ! not_every_iteration
;
5758 v
->always_executed
= ! not_every_iteration
;
5759 v
->maybe_multiple
= maybe_multiple
;
5762 /* Add this to the reg's iv_class, creating a class
5763 if this is the first incrementation of the reg. */
5765 bl
= REG_IV_CLASS (ivs
, REGNO (dest_reg
));
5768 /* Create and initialize new iv_class. */
5770 bl
= (struct iv_class
*) xmalloc (sizeof (struct iv_class
));
5772 bl
->regno
= REGNO (dest_reg
);
5778 /* Set initial value to the reg itself. */
5779 bl
->initial_value
= dest_reg
;
5780 bl
->final_value
= 0;
5781 /* We haven't seen the initializing insn yet */
5784 bl
->initial_test
= 0;
5785 bl
->incremented
= 0;
5789 bl
->total_benefit
= 0;
5791 /* Add this class to ivs->list. */
5792 bl
->next
= ivs
->list
;
5795 /* Put it in the array of biv register classes. */
5796 REG_IV_CLASS (ivs
, REGNO (dest_reg
)) = bl
;
5800 /* Check if location is the same as a previous one. */
5801 struct induction
*induction
;
5802 for (induction
= bl
->biv
; induction
; induction
= induction
->next_iv
)
5803 if (location
== induction
->location
)
5805 v
->same
= induction
;
5810 /* Update IV_CLASS entry for this biv. */
5811 v
->next_iv
= bl
->biv
;
5814 if (mult_val
== const1_rtx
)
5815 bl
->incremented
= 1;
5817 if (loop_dump_stream
)
5818 loop_biv_dump (v
, loop_dump_stream
, 0);
5821 /* Fill in the data about one giv.
5822 V is the `struct induction' in which we record the giv. (It is
5823 allocated by the caller, with alloca.)
5824 INSN is the insn that sets it.
5825 BENEFIT estimates the savings from deleting this insn.
5826 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5827 into a register or is used as a memory address.
5829 SRC_REG is the biv reg which the giv is computed from.
5830 DEST_REG is the giv's reg (if the giv is stored in a reg).
5831 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5832 LOCATION points to the place where this giv's value appears in INSN. */
5835 record_giv (loop
, v
, insn
, src_reg
, dest_reg
, mult_val
, add_val
, ext_val
,
5836 benefit
, type
, not_every_iteration
, maybe_multiple
, location
)
5837 const struct loop
*loop
;
5838 struct induction
*v
;
5842 rtx mult_val
, add_val
, ext_val
;
5845 int not_every_iteration
, maybe_multiple
;
5848 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5849 struct induction
*b
;
5850 struct iv_class
*bl
;
5851 rtx set
= single_set (insn
);
5854 /* Attempt to prove constantness of the values. Don't let simplify_rtx
5855 undo the MULT canonicalization that we performed earlier. */
5856 temp
= simplify_rtx (add_val
);
5858 && ! (GET_CODE (add_val
) == MULT
5859 && GET_CODE (temp
) == ASHIFT
))
5863 v
->src_reg
= src_reg
;
5865 v
->dest_reg
= dest_reg
;
5866 v
->mult_val
= mult_val
;
5867 v
->add_val
= add_val
;
5868 v
->ext_dependent
= ext_val
;
5869 v
->benefit
= benefit
;
5870 v
->location
= location
;
5872 v
->combined_with
= 0;
5873 v
->maybe_multiple
= maybe_multiple
;
5875 v
->derive_adjustment
= 0;
5881 v
->auto_inc_opt
= 0;
5885 /* The v->always_computable field is used in update_giv_derive, to
5886 determine whether a giv can be used to derive another giv. For a
5887 DEST_REG giv, INSN computes a new value for the giv, so its value
5888 isn't computable if INSN insn't executed every iteration.
5889 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5890 it does not compute a new value. Hence the value is always computable
5891 regardless of whether INSN is executed each iteration. */
5893 if (type
== DEST_ADDR
)
5894 v
->always_computable
= 1;
5896 v
->always_computable
= ! not_every_iteration
;
5898 v
->always_executed
= ! not_every_iteration
;
5900 if (type
== DEST_ADDR
)
5902 v
->mode
= GET_MODE (*location
);
5905 else /* type == DEST_REG */
5907 v
->mode
= GET_MODE (SET_DEST (set
));
5909 v
->lifetime
= LOOP_REG_LIFETIME (loop
, REGNO (dest_reg
));
5911 /* If the lifetime is zero, it means that this register is
5912 really a dead store. So mark this as a giv that can be
5913 ignored. This will not prevent the biv from being eliminated. */
5914 if (v
->lifetime
== 0)
5917 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = GENERAL_INDUCT
;
5918 REG_IV_INFO (ivs
, REGNO (dest_reg
)) = v
;
5921 /* Add the giv to the class of givs computed from one biv. */
5923 bl
= REG_IV_CLASS (ivs
, REGNO (src_reg
));
5926 v
->next_iv
= bl
->giv
;
5928 /* Don't count DEST_ADDR. This is supposed to count the number of
5929 insns that calculate givs. */
5930 if (type
== DEST_REG
)
5932 bl
->total_benefit
+= benefit
;
5935 /* Fatal error, biv missing for this giv? */
5938 if (type
== DEST_ADDR
)
5941 v
->not_replaceable
= 0;
5945 /* The giv can be replaced outright by the reduced register only if all
5946 of the following conditions are true:
5947 - the insn that sets the giv is always executed on any iteration
5948 on which the giv is used at all
5949 (there are two ways to deduce this:
5950 either the insn is executed on every iteration,
5951 or all uses follow that insn in the same basic block),
5952 - the giv is not used outside the loop
5953 - no assignments to the biv occur during the giv's lifetime. */
5955 if (REGNO_FIRST_UID (REGNO (dest_reg
)) == INSN_UID (insn
)
5956 /* Previous line always fails if INSN was moved by loop opt. */
5957 && REGNO_LAST_LUID (REGNO (dest_reg
))
5958 < INSN_LUID (loop
->end
)
5959 && (! not_every_iteration
5960 || last_use_this_basic_block (dest_reg
, insn
)))
5962 /* Now check that there are no assignments to the biv within the
5963 giv's lifetime. This requires two separate checks. */
5965 /* Check each biv update, and fail if any are between the first
5966 and last use of the giv.
5968 If this loop contains an inner loop that was unrolled, then
5969 the insn modifying the biv may have been emitted by the loop
5970 unrolling code, and hence does not have a valid luid. Just
5971 mark the biv as not replaceable in this case. It is not very
5972 useful as a biv, because it is used in two different loops.
5973 It is very unlikely that we would be able to optimize the giv
5974 using this biv anyways. */
5977 v
->not_replaceable
= 0;
5978 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
5980 if (INSN_UID (b
->insn
) >= max_uid_for_loop
5981 || ((INSN_LUID (b
->insn
)
5982 >= REGNO_FIRST_LUID (REGNO (dest_reg
)))
5983 && (INSN_LUID (b
->insn
)
5984 <= REGNO_LAST_LUID (REGNO (dest_reg
)))))
5987 v
->not_replaceable
= 1;
5992 /* If there are any backwards branches that go from after the
5993 biv update to before it, then this giv is not replaceable. */
5995 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
5996 if (back_branch_in_range_p (loop
, b
->insn
))
5999 v
->not_replaceable
= 1;
6005 /* May still be replaceable, we don't have enough info here to
6008 v
->not_replaceable
= 0;
6012 /* Record whether the add_val contains a const_int, for later use by
6017 v
->no_const_addval
= 1;
6018 if (tem
== const0_rtx
)
6020 else if (CONSTANT_P (add_val
))
6021 v
->no_const_addval
= 0;
6022 if (GET_CODE (tem
) == PLUS
)
6026 if (GET_CODE (XEXP (tem
, 0)) == PLUS
)
6027 tem
= XEXP (tem
, 0);
6028 else if (GET_CODE (XEXP (tem
, 1)) == PLUS
)
6029 tem
= XEXP (tem
, 1);
6033 if (CONSTANT_P (XEXP (tem
, 1)))
6034 v
->no_const_addval
= 0;
6038 if (loop_dump_stream
)
6039 loop_giv_dump (v
, loop_dump_stream
, 0);
6042 /* All this does is determine whether a giv can be made replaceable because
6043 its final value can be calculated. This code can not be part of record_giv
6044 above, because final_giv_value requires that the number of loop iterations
6045 be known, and that can not be accurately calculated until after all givs
6046 have been identified. */
6049 check_final_value (loop
, v
)
6050 const struct loop
*loop
;
6051 struct induction
*v
;
6053 rtx final_value
= 0;
6055 /* DEST_ADDR givs will never reach here, because they are always marked
6056 replaceable above in record_giv. */
6058 /* The giv can be replaced outright by the reduced register only if all
6059 of the following conditions are true:
6060 - the insn that sets the giv is always executed on any iteration
6061 on which the giv is used at all
6062 (there are two ways to deduce this:
6063 either the insn is executed on every iteration,
6064 or all uses follow that insn in the same basic block),
6065 - its final value can be calculated (this condition is different
6066 than the one above in record_giv)
6067 - it's not used before the it's set
6068 - no assignments to the biv occur during the giv's lifetime. */
6071 /* This is only called now when replaceable is known to be false. */
6072 /* Clear replaceable, so that it won't confuse final_giv_value. */
6076 if ((final_value
= final_giv_value (loop
, v
))
6077 && (v
->always_executed
6078 || last_use_this_basic_block (v
->dest_reg
, v
->insn
)))
6080 int biv_increment_seen
= 0, before_giv_insn
= 0;
6085 v
->not_replaceable
= 0;
6087 /* When trying to determine whether or not a biv increment occurs
6088 during the lifetime of the giv, we can ignore uses of the variable
6089 outside the loop because final_value is true. Hence we can not
6090 use regno_last_uid and regno_first_uid as above in record_giv. */
6092 /* Search the loop to determine whether any assignments to the
6093 biv occur during the giv's lifetime. Start with the insn
6094 that sets the giv, and search around the loop until we come
6095 back to that insn again.
6097 Also fail if there is a jump within the giv's lifetime that jumps
6098 to somewhere outside the lifetime but still within the loop. This
6099 catches spaghetti code where the execution order is not linear, and
6100 hence the above test fails. Here we assume that the giv lifetime
6101 does not extend from one iteration of the loop to the next, so as
6102 to make the test easier. Since the lifetime isn't known yet,
6103 this requires two loops. See also record_giv above. */
6105 last_giv_use
= v
->insn
;
6112 before_giv_insn
= 1;
6113 p
= NEXT_INSN (loop
->start
);
6118 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
6119 || GET_CODE (p
) == CALL_INSN
)
6121 /* It is possible for the BIV increment to use the GIV if we
6122 have a cycle. Thus we must be sure to check each insn for
6123 both BIV and GIV uses, and we must check for BIV uses
6126 if (! biv_increment_seen
6127 && reg_set_p (v
->src_reg
, PATTERN (p
)))
6128 biv_increment_seen
= 1;
6130 if (reg_mentioned_p (v
->dest_reg
, PATTERN (p
)))
6132 if (biv_increment_seen
|| before_giv_insn
)
6135 v
->not_replaceable
= 1;
6143 /* Now that the lifetime of the giv is known, check for branches
6144 from within the lifetime to outside the lifetime if it is still
6154 p
= NEXT_INSN (loop
->start
);
6155 if (p
== last_giv_use
)
6158 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
6159 && LABEL_NAME (JUMP_LABEL (p
))
6160 && ((loop_insn_first_p (JUMP_LABEL (p
), v
->insn
)
6161 && loop_insn_first_p (loop
->start
, JUMP_LABEL (p
)))
6162 || (loop_insn_first_p (last_giv_use
, JUMP_LABEL (p
))
6163 && loop_insn_first_p (JUMP_LABEL (p
), loop
->end
))))
6166 v
->not_replaceable
= 1;
6168 if (loop_dump_stream
)
6169 fprintf (loop_dump_stream
,
6170 "Found branch outside giv lifetime.\n");
6177 /* If it is replaceable, then save the final value. */
6179 v
->final_value
= final_value
;
6182 if (loop_dump_stream
&& v
->replaceable
)
6183 fprintf (loop_dump_stream
, "Insn %d: giv reg %d final_value replaceable\n",
6184 INSN_UID (v
->insn
), REGNO (v
->dest_reg
));
6187 /* Update the status of whether a giv can derive other givs.
6189 We need to do something special if there is or may be an update to the biv
6190 between the time the giv is defined and the time it is used to derive
6193 In addition, a giv that is only conditionally set is not allowed to
6194 derive another giv once a label has been passed.
6196 The cases we look at are when a label or an update to a biv is passed. */
6199 update_giv_derive (loop
, p
)
6200 const struct loop
*loop
;
6203 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6204 struct iv_class
*bl
;
6205 struct induction
*biv
, *giv
;
6209 /* Search all IV classes, then all bivs, and finally all givs.
6211 There are three cases we are concerned with. First we have the situation
6212 of a giv that is only updated conditionally. In that case, it may not
6213 derive any givs after a label is passed.
6215 The second case is when a biv update occurs, or may occur, after the
6216 definition of a giv. For certain biv updates (see below) that are
6217 known to occur between the giv definition and use, we can adjust the
6218 giv definition. For others, or when the biv update is conditional,
6219 we must prevent the giv from deriving any other givs. There are two
6220 sub-cases within this case.
6222 If this is a label, we are concerned with any biv update that is done
6223 conditionally, since it may be done after the giv is defined followed by
6224 a branch here (actually, we need to pass both a jump and a label, but
6225 this extra tracking doesn't seem worth it).
6227 If this is a jump, we are concerned about any biv update that may be
6228 executed multiple times. We are actually only concerned about
6229 backward jumps, but it is probably not worth performing the test
6230 on the jump again here.
6232 If this is a biv update, we must adjust the giv status to show that a
6233 subsequent biv update was performed. If this adjustment cannot be done,
6234 the giv cannot derive further givs. */
6236 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
6237 for (biv
= bl
->biv
; biv
; biv
= biv
->next_iv
)
6238 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
6241 for (giv
= bl
->giv
; giv
; giv
= giv
->next_iv
)
6243 /* If cant_derive is already true, there is no point in
6244 checking all of these conditions again. */
6245 if (giv
->cant_derive
)
6248 /* If this giv is conditionally set and we have passed a label,
6249 it cannot derive anything. */
6250 if (GET_CODE (p
) == CODE_LABEL
&& ! giv
->always_computable
)
6251 giv
->cant_derive
= 1;
6253 /* Skip givs that have mult_val == 0, since
6254 they are really invariants. Also skip those that are
6255 replaceable, since we know their lifetime doesn't contain
6257 else if (giv
->mult_val
== const0_rtx
|| giv
->replaceable
)
6260 /* The only way we can allow this giv to derive another
6261 is if this is a biv increment and we can form the product
6262 of biv->add_val and giv->mult_val. In this case, we will
6263 be able to compute a compensation. */
6264 else if (biv
->insn
== p
)
6269 if (biv
->mult_val
== const1_rtx
)
6270 tem
= simplify_giv_expr (loop
,
6271 gen_rtx_MULT (giv
->mode
,
6274 &ext_val_dummy
, &dummy
);
6276 if (tem
&& giv
->derive_adjustment
)
6277 tem
= simplify_giv_expr
6279 gen_rtx_PLUS (giv
->mode
, tem
, giv
->derive_adjustment
),
6280 &ext_val_dummy
, &dummy
);
6283 giv
->derive_adjustment
= tem
;
6285 giv
->cant_derive
= 1;
6287 else if ((GET_CODE (p
) == CODE_LABEL
&& ! biv
->always_computable
)
6288 || (GET_CODE (p
) == JUMP_INSN
&& biv
->maybe_multiple
))
6289 giv
->cant_derive
= 1;
6294 /* Check whether an insn is an increment legitimate for a basic induction var.
6295 X is the source of insn P, or a part of it.
6296 MODE is the mode in which X should be interpreted.
6298 DEST_REG is the putative biv, also the destination of the insn.
6299 We accept patterns of these forms:
6300 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
6301 REG = INVARIANT + REG
6303 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
6304 store the additive term into *INC_VAL, and store the place where
6305 we found the additive term into *LOCATION.
6307 If X is an assignment of an invariant into DEST_REG, we set
6308 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
6310 We also want to detect a BIV when it corresponds to a variable
6311 whose mode was promoted via PROMOTED_MODE. In that case, an increment
6312 of the variable may be a PLUS that adds a SUBREG of that variable to
6313 an invariant and then sign- or zero-extends the result of the PLUS
6316 Most GIVs in such cases will be in the promoted mode, since that is the
6317 probably the natural computation mode (and almost certainly the mode
6318 used for addresses) on the machine. So we view the pseudo-reg containing
6319 the variable as the BIV, as if it were simply incremented.
6321 Note that treating the entire pseudo as a BIV will result in making
6322 simple increments to any GIVs based on it. However, if the variable
6323 overflows in its declared mode but not its promoted mode, the result will
6324 be incorrect. This is acceptable if the variable is signed, since
6325 overflows in such cases are undefined, but not if it is unsigned, since
6326 those overflows are defined. So we only check for SIGN_EXTEND and
6329 If we cannot find a biv, we return 0. */
6332 basic_induction_var (loop
, x
, mode
, dest_reg
, p
, inc_val
, mult_val
, location
)
6333 const struct loop
*loop
;
6335 enum machine_mode mode
;
6346 code
= GET_CODE (x
);
6351 if (rtx_equal_p (XEXP (x
, 0), dest_reg
)
6352 || (GET_CODE (XEXP (x
, 0)) == SUBREG
6353 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 0))
6354 && SUBREG_REG (XEXP (x
, 0)) == dest_reg
))
6356 argp
= &XEXP (x
, 1);
6358 else if (rtx_equal_p (XEXP (x
, 1), dest_reg
)
6359 || (GET_CODE (XEXP (x
, 1)) == SUBREG
6360 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 1))
6361 && SUBREG_REG (XEXP (x
, 1)) == dest_reg
))
6363 argp
= &XEXP (x
, 0);
6369 if (loop_invariant_p (loop
, arg
) != 1)
6372 *inc_val
= convert_modes (GET_MODE (dest_reg
), GET_MODE (x
), arg
, 0);
6373 *mult_val
= const1_rtx
;
6378 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
6379 handle addition of promoted variables.
6380 ??? The comment at the start of this function is wrong: promoted
6381 variable increments don't look like it says they do. */
6382 return basic_induction_var (loop
, SUBREG_REG (x
),
6383 GET_MODE (SUBREG_REG (x
)),
6384 dest_reg
, p
, inc_val
, mult_val
, location
);
6387 /* If this register is assigned in a previous insn, look at its
6388 source, but don't go outside the loop or past a label. */
6390 /* If this sets a register to itself, we would repeat any previous
6391 biv increment if we applied this strategy blindly. */
6392 if (rtx_equal_p (dest_reg
, x
))
6401 insn
= PREV_INSN (insn
);
6403 while (insn
&& GET_CODE (insn
) == NOTE
6404 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
6408 set
= single_set (insn
);
6411 dest
= SET_DEST (set
);
6413 || (GET_CODE (dest
) == SUBREG
6414 && (GET_MODE_SIZE (GET_MODE (dest
)) <= UNITS_PER_WORD
)
6415 && (GET_MODE_CLASS (GET_MODE (dest
)) == MODE_INT
)
6416 && SUBREG_REG (dest
) == x
))
6417 return basic_induction_var (loop
, SET_SRC (set
),
6418 (GET_MODE (SET_SRC (set
)) == VOIDmode
6420 : GET_MODE (SET_SRC (set
))),
6422 inc_val
, mult_val
, location
);
6424 while (GET_CODE (dest
) == SIGN_EXTRACT
6425 || GET_CODE (dest
) == ZERO_EXTRACT
6426 || GET_CODE (dest
) == SUBREG
6427 || GET_CODE (dest
) == STRICT_LOW_PART
)
6428 dest
= XEXP (dest
, 0);
6434 /* Can accept constant setting of biv only when inside inner most loop.
6435 Otherwise, a biv of an inner loop may be incorrectly recognized
6436 as a biv of the outer loop,
6437 causing code to be moved INTO the inner loop. */
6439 if (loop_invariant_p (loop
, x
) != 1)
6444 /* convert_modes aborts if we try to convert to or from CCmode, so just
6445 exclude that case. It is very unlikely that a condition code value
6446 would be a useful iterator anyways. convert_modes aborts if we try to
6447 convert a float mode to non-float or vice versa too. */
6448 if (loop
->level
== 1
6449 && GET_MODE_CLASS (mode
) == GET_MODE_CLASS (GET_MODE (dest_reg
))
6450 && GET_MODE_CLASS (mode
) != MODE_CC
)
6452 /* Possible bug here? Perhaps we don't know the mode of X. */
6453 *inc_val
= convert_modes (GET_MODE (dest_reg
), mode
, x
, 0);
6454 *mult_val
= const0_rtx
;
6461 /* Ignore this BIV if signed arithmetic overflow is defined. */
6464 return basic_induction_var (loop
, XEXP (x
, 0), GET_MODE (XEXP (x
, 0)),
6465 dest_reg
, p
, inc_val
, mult_val
, location
);
6468 /* Similar, since this can be a sign extension. */
6469 for (insn
= PREV_INSN (p
);
6470 (insn
&& GET_CODE (insn
) == NOTE
6471 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
6472 insn
= PREV_INSN (insn
))
6476 set
= single_set (insn
);
6478 if (! rtx_equal_p (dest_reg
, XEXP (x
, 0))
6479 && set
&& SET_DEST (set
) == XEXP (x
, 0)
6480 && GET_CODE (XEXP (x
, 1)) == CONST_INT
6481 && INTVAL (XEXP (x
, 1)) >= 0
6482 && GET_CODE (SET_SRC (set
)) == ASHIFT
6483 && XEXP (x
, 1) == XEXP (SET_SRC (set
), 1))
6484 return basic_induction_var (loop
, XEXP (SET_SRC (set
), 0),
6485 GET_MODE (XEXP (x
, 0)),
6486 dest_reg
, insn
, inc_val
, mult_val
,
6495 /* A general induction variable (giv) is any quantity that is a linear
6496 function of a basic induction variable,
6497 i.e. giv = biv * mult_val + add_val.
6498 The coefficients can be any loop invariant quantity.
6499 A giv need not be computed directly from the biv;
6500 it can be computed by way of other givs. */
6502 /* Determine whether X computes a giv.
6503 If it does, return a nonzero value
6504 which is the benefit from eliminating the computation of X;
6505 set *SRC_REG to the register of the biv that it is computed from;
6506 set *ADD_VAL and *MULT_VAL to the coefficients,
6507 such that the value of X is biv * mult + add; */
6510 general_induction_var (loop
, x
, src_reg
, add_val
, mult_val
, ext_val
,
6511 is_addr
, pbenefit
, addr_mode
)
6512 const struct loop
*loop
;
6520 enum machine_mode addr_mode
;
6522 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6525 /* If this is an invariant, forget it, it isn't a giv. */
6526 if (loop_invariant_p (loop
, x
) == 1)
6530 *ext_val
= NULL_RTX
;
6531 x
= simplify_giv_expr (loop
, x
, ext_val
, pbenefit
);
6535 switch (GET_CODE (x
))
6539 /* Since this is now an invariant and wasn't before, it must be a giv
6540 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6542 *src_reg
= ivs
->list
->biv
->dest_reg
;
6543 *mult_val
= const0_rtx
;
6548 /* This is equivalent to a BIV. */
6550 *mult_val
= const1_rtx
;
6551 *add_val
= const0_rtx
;
6555 /* Either (plus (biv) (invar)) or
6556 (plus (mult (biv) (invar_1)) (invar_2)). */
6557 if (GET_CODE (XEXP (x
, 0)) == MULT
)
6559 *src_reg
= XEXP (XEXP (x
, 0), 0);
6560 *mult_val
= XEXP (XEXP (x
, 0), 1);
6564 *src_reg
= XEXP (x
, 0);
6565 *mult_val
= const1_rtx
;
6567 *add_val
= XEXP (x
, 1);
6571 /* ADD_VAL is zero. */
6572 *src_reg
= XEXP (x
, 0);
6573 *mult_val
= XEXP (x
, 1);
6574 *add_val
= const0_rtx
;
6581 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6582 unless they are CONST_INT). */
6583 if (GET_CODE (*add_val
) == USE
)
6584 *add_val
= XEXP (*add_val
, 0);
6585 if (GET_CODE (*mult_val
) == USE
)
6586 *mult_val
= XEXP (*mult_val
, 0);
6589 *pbenefit
+= address_cost (orig_x
, addr_mode
) - reg_address_cost
;
6591 *pbenefit
+= rtx_cost (orig_x
, SET
);
6593 /* Always return true if this is a giv so it will be detected as such,
6594 even if the benefit is zero or negative. This allows elimination
6595 of bivs that might otherwise not be eliminated. */
6599 /* Given an expression, X, try to form it as a linear function of a biv.
6600 We will canonicalize it to be of the form
6601 (plus (mult (BIV) (invar_1))
6603 with possible degeneracies.
6605 The invariant expressions must each be of a form that can be used as a
6606 machine operand. We surround then with a USE rtx (a hack, but localized
6607 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6608 routine; it is the caller's responsibility to strip them.
6610 If no such canonicalization is possible (i.e., two biv's are used or an
6611 expression that is neither invariant nor a biv or giv), this routine
6614 For a nonzero return, the result will have a code of CONST_INT, USE,
6615 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6617 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6619 static rtx sge_plus
PARAMS ((enum machine_mode
, rtx
, rtx
));
6620 static rtx sge_plus_constant
PARAMS ((rtx
, rtx
));
6623 simplify_giv_expr (loop
, x
, ext_val
, benefit
)
6624 const struct loop
*loop
;
6629 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6630 struct loop_regs
*regs
= LOOP_REGS (loop
);
6631 enum machine_mode mode
= GET_MODE (x
);
6635 /* If this is not an integer mode, or if we cannot do arithmetic in this
6636 mode, this can't be a giv. */
6637 if (mode
!= VOIDmode
6638 && (GET_MODE_CLASS (mode
) != MODE_INT
6639 || GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
))
6642 switch (GET_CODE (x
))
6645 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
6646 arg1
= simplify_giv_expr (loop
, XEXP (x
, 1), ext_val
, benefit
);
6647 if (arg0
== 0 || arg1
== 0)
6650 /* Put constant last, CONST_INT last if both constant. */
6651 if ((GET_CODE (arg0
) == USE
6652 || GET_CODE (arg0
) == CONST_INT
)
6653 && ! ((GET_CODE (arg0
) == USE
6654 && GET_CODE (arg1
) == USE
)
6655 || GET_CODE (arg1
) == CONST_INT
))
6656 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6658 /* Handle addition of zero, then addition of an invariant. */
6659 if (arg1
== const0_rtx
)
6661 else if (GET_CODE (arg1
) == CONST_INT
|| GET_CODE (arg1
) == USE
)
6662 switch (GET_CODE (arg0
))
6666 /* Adding two invariants must result in an invariant, so enclose
6667 addition operation inside a USE and return it. */
6668 if (GET_CODE (arg0
) == USE
)
6669 arg0
= XEXP (arg0
, 0);
6670 if (GET_CODE (arg1
) == USE
)
6671 arg1
= XEXP (arg1
, 0);
6673 if (GET_CODE (arg0
) == CONST_INT
)
6674 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6675 if (GET_CODE (arg1
) == CONST_INT
)
6676 tem
= sge_plus_constant (arg0
, arg1
);
6678 tem
= sge_plus (mode
, arg0
, arg1
);
6680 if (GET_CODE (tem
) != CONST_INT
)
6681 tem
= gen_rtx_USE (mode
, tem
);
6686 /* biv + invar or mult + invar. Return sum. */
6687 return gen_rtx_PLUS (mode
, arg0
, arg1
);
6690 /* (a + invar_1) + invar_2. Associate. */
6692 simplify_giv_expr (loop
,
6704 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6705 MULT to reduce cases. */
6706 if (GET_CODE (arg0
) == REG
)
6707 arg0
= gen_rtx_MULT (mode
, arg0
, const1_rtx
);
6708 if (GET_CODE (arg1
) == REG
)
6709 arg1
= gen_rtx_MULT (mode
, arg1
, const1_rtx
);
6711 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6712 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6713 Recurse to associate the second PLUS. */
6714 if (GET_CODE (arg1
) == MULT
)
6715 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6717 if (GET_CODE (arg1
) == PLUS
)
6719 simplify_giv_expr (loop
,
6721 gen_rtx_PLUS (mode
, arg0
,
6726 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6727 if (GET_CODE (arg0
) != MULT
|| GET_CODE (arg1
) != MULT
)
6730 if (!rtx_equal_p (arg0
, arg1
))
6733 return simplify_giv_expr (loop
,
6742 /* Handle "a - b" as "a + b * (-1)". */
6743 return simplify_giv_expr (loop
,
6752 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
6753 arg1
= simplify_giv_expr (loop
, XEXP (x
, 1), ext_val
, benefit
);
6754 if (arg0
== 0 || arg1
== 0)
6757 /* Put constant last, CONST_INT last if both constant. */
6758 if ((GET_CODE (arg0
) == USE
|| GET_CODE (arg0
) == CONST_INT
)
6759 && GET_CODE (arg1
) != CONST_INT
)
6760 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6762 /* If second argument is not now constant, not giv. */
6763 if (GET_CODE (arg1
) != USE
&& GET_CODE (arg1
) != CONST_INT
)
6766 /* Handle multiply by 0 or 1. */
6767 if (arg1
== const0_rtx
)
6770 else if (arg1
== const1_rtx
)
6773 switch (GET_CODE (arg0
))
6776 /* biv * invar. Done. */
6777 return gen_rtx_MULT (mode
, arg0
, arg1
);
6780 /* Product of two constants. */
6781 return GEN_INT (INTVAL (arg0
) * INTVAL (arg1
));
6784 /* invar * invar is a giv, but attempt to simplify it somehow. */
6785 if (GET_CODE (arg1
) != CONST_INT
)
6788 arg0
= XEXP (arg0
, 0);
6789 if (GET_CODE (arg0
) == MULT
)
6791 /* (invar_0 * invar_1) * invar_2. Associate. */
6792 return simplify_giv_expr (loop
,
6801 /* Propagate the MULT expressions to the intermost nodes. */
6802 else if (GET_CODE (arg0
) == PLUS
)
6804 /* (invar_0 + invar_1) * invar_2. Distribute. */
6805 return simplify_giv_expr (loop
,
6817 return gen_rtx_USE (mode
, gen_rtx_MULT (mode
, arg0
, arg1
));
6820 /* (a * invar_1) * invar_2. Associate. */
6821 return simplify_giv_expr (loop
,
6830 /* (a + invar_1) * invar_2. Distribute. */
6831 return simplify_giv_expr (loop
,
6846 /* Shift by constant is multiply by power of two. */
6847 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
6851 simplify_giv_expr (loop
,
6854 GEN_INT ((HOST_WIDE_INT
) 1
6855 << INTVAL (XEXP (x
, 1)))),
6859 /* "-a" is "a * (-1)" */
6860 return simplify_giv_expr (loop
,
6861 gen_rtx_MULT (mode
, XEXP (x
, 0), constm1_rtx
),
6865 /* "~a" is "-a - 1". Silly, but easy. */
6866 return simplify_giv_expr (loop
,
6867 gen_rtx_MINUS (mode
,
6868 gen_rtx_NEG (mode
, XEXP (x
, 0)),
6873 /* Already in proper form for invariant. */
6879 /* Conditionally recognize extensions of simple IVs. After we've
6880 computed loop traversal counts and verified the range of the
6881 source IV, we'll reevaluate this as a GIV. */
6882 if (*ext_val
== NULL_RTX
)
6884 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
6885 if (arg0
&& *ext_val
== NULL_RTX
&& GET_CODE (arg0
) == REG
)
6887 *ext_val
= gen_rtx_fmt_e (GET_CODE (x
), mode
, arg0
);
6894 /* If this is a new register, we can't deal with it. */
6895 if (REGNO (x
) >= max_reg_before_loop
)
6898 /* Check for biv or giv. */
6899 switch (REG_IV_TYPE (ivs
, REGNO (x
)))
6903 case GENERAL_INDUCT
:
6905 struct induction
*v
= REG_IV_INFO (ivs
, REGNO (x
));
6907 /* Form expression from giv and add benefit. Ensure this giv
6908 can derive another and subtract any needed adjustment if so. */
6910 /* Increasing the benefit here is risky. The only case in which it
6911 is arguably correct is if this is the only use of V. In other
6912 cases, this will artificially inflate the benefit of the current
6913 giv, and lead to suboptimal code. Thus, it is disabled, since
6914 potentially not reducing an only marginally beneficial giv is
6915 less harmful than reducing many givs that are not really
6918 rtx single_use
= regs
->array
[REGNO (x
)].single_usage
;
6919 if (single_use
&& single_use
!= const0_rtx
)
6920 *benefit
+= v
->benefit
;
6926 tem
= gen_rtx_PLUS (mode
, gen_rtx_MULT (mode
,
6927 v
->src_reg
, v
->mult_val
),
6930 if (v
->derive_adjustment
)
6931 tem
= gen_rtx_MINUS (mode
, tem
, v
->derive_adjustment
);
6932 arg0
= simplify_giv_expr (loop
, tem
, ext_val
, benefit
);
6935 if (!v
->ext_dependent
)
6940 *ext_val
= v
->ext_dependent
;
6948 /* If it isn't an induction variable, and it is invariant, we
6949 may be able to simplify things further by looking through
6950 the bits we just moved outside the loop. */
6951 if (loop_invariant_p (loop
, x
) == 1)
6954 struct loop_movables
*movables
= LOOP_MOVABLES (loop
);
6956 for (m
= movables
->head
; m
; m
= m
->next
)
6957 if (rtx_equal_p (x
, m
->set_dest
))
6959 /* Ok, we found a match. Substitute and simplify. */
6961 /* If we match another movable, we must use that, as
6962 this one is going away. */
6964 return simplify_giv_expr (loop
, m
->match
->set_dest
,
6967 /* If consec is nonzero, this is a member of a group of
6968 instructions that were moved together. We handle this
6969 case only to the point of seeking to the last insn and
6970 looking for a REG_EQUAL. Fail if we don't find one. */
6977 tem
= NEXT_INSN (tem
);
6981 tem
= find_reg_note (tem
, REG_EQUAL
, NULL_RTX
);
6983 tem
= XEXP (tem
, 0);
6987 tem
= single_set (m
->insn
);
6989 tem
= SET_SRC (tem
);
6994 /* What we are most interested in is pointer
6995 arithmetic on invariants -- only take
6996 patterns we may be able to do something with. */
6997 if (GET_CODE (tem
) == PLUS
6998 || GET_CODE (tem
) == MULT
6999 || GET_CODE (tem
) == ASHIFT
7000 || GET_CODE (tem
) == CONST_INT
7001 || GET_CODE (tem
) == SYMBOL_REF
)
7003 tem
= simplify_giv_expr (loop
, tem
, ext_val
,
7008 else if (GET_CODE (tem
) == CONST
7009 && GET_CODE (XEXP (tem
, 0)) == PLUS
7010 && GET_CODE (XEXP (XEXP (tem
, 0), 0)) == SYMBOL_REF
7011 && GET_CODE (XEXP (XEXP (tem
, 0), 1)) == CONST_INT
)
7013 tem
= simplify_giv_expr (loop
, XEXP (tem
, 0),
7025 /* Fall through to general case. */
7027 /* If invariant, return as USE (unless CONST_INT).
7028 Otherwise, not giv. */
7029 if (GET_CODE (x
) == USE
)
7032 if (loop_invariant_p (loop
, x
) == 1)
7034 if (GET_CODE (x
) == CONST_INT
)
7036 if (GET_CODE (x
) == CONST
7037 && GET_CODE (XEXP (x
, 0)) == PLUS
7038 && GET_CODE (XEXP (XEXP (x
, 0), 0)) == SYMBOL_REF
7039 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
)
7041 return gen_rtx_USE (mode
, x
);
7048 /* This routine folds invariants such that there is only ever one
7049 CONST_INT in the summation. It is only used by simplify_giv_expr. */
7052 sge_plus_constant (x
, c
)
7055 if (GET_CODE (x
) == CONST_INT
)
7056 return GEN_INT (INTVAL (x
) + INTVAL (c
));
7057 else if (GET_CODE (x
) != PLUS
)
7058 return gen_rtx_PLUS (GET_MODE (x
), x
, c
);
7059 else if (GET_CODE (XEXP (x
, 1)) == CONST_INT
)
7061 return gen_rtx_PLUS (GET_MODE (x
), XEXP (x
, 0),
7062 GEN_INT (INTVAL (XEXP (x
, 1)) + INTVAL (c
)));
7064 else if (GET_CODE (XEXP (x
, 0)) == PLUS
7065 || GET_CODE (XEXP (x
, 1)) != PLUS
)
7067 return gen_rtx_PLUS (GET_MODE (x
),
7068 sge_plus_constant (XEXP (x
, 0), c
), XEXP (x
, 1));
7072 return gen_rtx_PLUS (GET_MODE (x
),
7073 sge_plus_constant (XEXP (x
, 1), c
), XEXP (x
, 0));
7078 sge_plus (mode
, x
, y
)
7079 enum machine_mode mode
;
7082 while (GET_CODE (y
) == PLUS
)
7084 rtx a
= XEXP (y
, 0);
7085 if (GET_CODE (a
) == CONST_INT
)
7086 x
= sge_plus_constant (x
, a
);
7088 x
= gen_rtx_PLUS (mode
, x
, a
);
7091 if (GET_CODE (y
) == CONST_INT
)
7092 x
= sge_plus_constant (x
, y
);
7094 x
= gen_rtx_PLUS (mode
, x
, y
);
7098 /* Help detect a giv that is calculated by several consecutive insns;
7102 The caller has already identified the first insn P as having a giv as dest;
7103 we check that all other insns that set the same register follow
7104 immediately after P, that they alter nothing else,
7105 and that the result of the last is still a giv.
7107 The value is 0 if the reg set in P is not really a giv.
7108 Otherwise, the value is the amount gained by eliminating
7109 all the consecutive insns that compute the value.
7111 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
7112 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
7114 The coefficients of the ultimate giv value are stored in
7115 *MULT_VAL and *ADD_VAL. */
7118 consec_sets_giv (loop
, first_benefit
, p
, src_reg
, dest_reg
,
7119 add_val
, mult_val
, ext_val
, last_consec_insn
)
7120 const struct loop
*loop
;
7128 rtx
*last_consec_insn
;
7130 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
7131 struct loop_regs
*regs
= LOOP_REGS (loop
);
7138 /* Indicate that this is a giv so that we can update the value produced in
7139 each insn of the multi-insn sequence.
7141 This induction structure will be used only by the call to
7142 general_induction_var below, so we can allocate it on our stack.
7143 If this is a giv, our caller will replace the induct var entry with
7144 a new induction structure. */
7145 struct induction
*v
;
7147 if (REG_IV_TYPE (ivs
, REGNO (dest_reg
)) != UNKNOWN_INDUCT
)
7150 v
= (struct induction
*) alloca (sizeof (struct induction
));
7151 v
->src_reg
= src_reg
;
7152 v
->mult_val
= *mult_val
;
7153 v
->add_val
= *add_val
;
7154 v
->benefit
= first_benefit
;
7156 v
->derive_adjustment
= 0;
7157 v
->ext_dependent
= NULL_RTX
;
7159 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = GENERAL_INDUCT
;
7160 REG_IV_INFO (ivs
, REGNO (dest_reg
)) = v
;
7162 count
= regs
->array
[REGNO (dest_reg
)].n_times_set
- 1;
7167 code
= GET_CODE (p
);
7169 /* If libcall, skip to end of call sequence. */
7170 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
7174 && (set
= single_set (p
))
7175 && GET_CODE (SET_DEST (set
)) == REG
7176 && SET_DEST (set
) == dest_reg
7177 && (general_induction_var (loop
, SET_SRC (set
), &src_reg
,
7178 add_val
, mult_val
, ext_val
, 0,
7180 /* Giv created by equivalent expression. */
7181 || ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
7182 && general_induction_var (loop
, XEXP (temp
, 0), &src_reg
,
7183 add_val
, mult_val
, ext_val
, 0,
7184 &benefit
, VOIDmode
)))
7185 && src_reg
== v
->src_reg
)
7187 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
7188 benefit
+= libcall_benefit (p
);
7191 v
->mult_val
= *mult_val
;
7192 v
->add_val
= *add_val
;
7193 v
->benefit
+= benefit
;
7195 else if (code
!= NOTE
)
7197 /* Allow insns that set something other than this giv to a
7198 constant. Such insns are needed on machines which cannot
7199 include long constants and should not disqualify a giv. */
7201 && (set
= single_set (p
))
7202 && SET_DEST (set
) != dest_reg
7203 && CONSTANT_P (SET_SRC (set
)))
7206 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = UNKNOWN_INDUCT
;
7211 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = UNKNOWN_INDUCT
;
7212 *last_consec_insn
= p
;
7216 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7217 represented by G1. If no such expression can be found, or it is clear that
7218 it cannot possibly be a valid address, 0 is returned.
7220 To perform the computation, we note that
7223 where `v' is the biv.
7225 So G2 = (y/b) * G1 + (b - a*y/x).
7227 Note that MULT = y/x.
7229 Update: A and B are now allowed to be additive expressions such that
7230 B contains all variables in A. That is, computing B-A will not require
7231 subtracting variables. */
7234 express_from_1 (a
, b
, mult
)
7237 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
7239 if (mult
== const0_rtx
)
7242 /* If MULT is not 1, we cannot handle A with non-constants, since we
7243 would then be required to subtract multiples of the registers in A.
7244 This is theoretically possible, and may even apply to some Fortran
7245 constructs, but it is a lot of work and we do not attempt it here. */
7247 if (mult
!= const1_rtx
&& GET_CODE (a
) != CONST_INT
)
7250 /* In general these structures are sorted top to bottom (down the PLUS
7251 chain), but not left to right across the PLUS. If B is a higher
7252 order giv than A, we can strip one level and recurse. If A is higher
7253 order, we'll eventually bail out, but won't know that until the end.
7254 If they are the same, we'll strip one level around this loop. */
7256 while (GET_CODE (a
) == PLUS
&& GET_CODE (b
) == PLUS
)
7258 rtx ra
, rb
, oa
, ob
, tmp
;
7260 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
7261 if (GET_CODE (ra
) == PLUS
)
7262 tmp
= ra
, ra
= oa
, oa
= tmp
;
7264 rb
= XEXP (b
, 0), ob
= XEXP (b
, 1);
7265 if (GET_CODE (rb
) == PLUS
)
7266 tmp
= rb
, rb
= ob
, ob
= tmp
;
7268 if (rtx_equal_p (ra
, rb
))
7269 /* We matched: remove one reg completely. */
7271 else if (GET_CODE (ob
) != PLUS
&& rtx_equal_p (ra
, ob
))
7272 /* An alternate match. */
7274 else if (GET_CODE (oa
) != PLUS
&& rtx_equal_p (oa
, rb
))
7275 /* An alternate match. */
7279 /* Indicates an extra register in B. Strip one level from B and
7280 recurse, hoping B was the higher order expression. */
7281 ob
= express_from_1 (a
, ob
, mult
);
7284 return gen_rtx_PLUS (GET_MODE (b
), rb
, ob
);
7288 /* Here we are at the last level of A, go through the cases hoping to
7289 get rid of everything but a constant. */
7291 if (GET_CODE (a
) == PLUS
)
7295 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
7296 if (rtx_equal_p (oa
, b
))
7298 else if (!rtx_equal_p (ra
, b
))
7301 if (GET_CODE (oa
) != CONST_INT
)
7304 return GEN_INT (-INTVAL (oa
) * INTVAL (mult
));
7306 else if (GET_CODE (a
) == CONST_INT
)
7308 return plus_constant (b
, -INTVAL (a
) * INTVAL (mult
));
7310 else if (CONSTANT_P (a
))
7312 enum machine_mode mode_a
= GET_MODE (a
);
7313 enum machine_mode mode_b
= GET_MODE (b
);
7314 enum machine_mode mode
= mode_b
== VOIDmode
? mode_a
: mode_b
;
7315 return simplify_gen_binary (MINUS
, mode
, b
, a
);
7317 else if (GET_CODE (b
) == PLUS
)
7319 if (rtx_equal_p (a
, XEXP (b
, 0)))
7321 else if (rtx_equal_p (a
, XEXP (b
, 1)))
7326 else if (rtx_equal_p (a
, b
))
7333 express_from (g1
, g2
)
7334 struct induction
*g1
, *g2
;
7338 /* The value that G1 will be multiplied by must be a constant integer. Also,
7339 the only chance we have of getting a valid address is if b*c/a (see above
7340 for notation) is also an integer. */
7341 if (GET_CODE (g1
->mult_val
) == CONST_INT
7342 && GET_CODE (g2
->mult_val
) == CONST_INT
)
7344 if (g1
->mult_val
== const0_rtx
7345 || INTVAL (g2
->mult_val
) % INTVAL (g1
->mult_val
) != 0)
7347 mult
= GEN_INT (INTVAL (g2
->mult_val
) / INTVAL (g1
->mult_val
));
7349 else if (rtx_equal_p (g1
->mult_val
, g2
->mult_val
))
7353 /* ??? Find out if the one is a multiple of the other? */
7357 add
= express_from_1 (g1
->add_val
, g2
->add_val
, mult
);
7358 if (add
== NULL_RTX
)
7360 /* Failed. If we've got a multiplication factor between G1 and G2,
7361 scale G1's addend and try again. */
7362 if (INTVAL (mult
) > 1)
7364 rtx g1_add_val
= g1
->add_val
;
7365 if (GET_CODE (g1_add_val
) == MULT
7366 && GET_CODE (XEXP (g1_add_val
, 1)) == CONST_INT
)
7369 m
= INTVAL (mult
) * INTVAL (XEXP (g1_add_val
, 1));
7370 g1_add_val
= gen_rtx_MULT (GET_MODE (g1_add_val
),
7371 XEXP (g1_add_val
, 0), GEN_INT (m
));
7375 g1_add_val
= gen_rtx_MULT (GET_MODE (g1_add_val
), g1_add_val
,
7379 add
= express_from_1 (g1_add_val
, g2
->add_val
, const1_rtx
);
7382 if (add
== NULL_RTX
)
7385 /* Form simplified final result. */
7386 if (mult
== const0_rtx
)
7388 else if (mult
== const1_rtx
)
7389 mult
= g1
->dest_reg
;
7391 mult
= gen_rtx_MULT (g2
->mode
, g1
->dest_reg
, mult
);
7393 if (add
== const0_rtx
)
7397 if (GET_CODE (add
) == PLUS
7398 && CONSTANT_P (XEXP (add
, 1)))
7400 rtx tem
= XEXP (add
, 1);
7401 mult
= gen_rtx_PLUS (g2
->mode
, mult
, XEXP (add
, 0));
7405 return gen_rtx_PLUS (g2
->mode
, mult
, add
);
7409 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7410 represented by G1. This indicates that G2 should be combined with G1 and
7411 that G2 can use (either directly or via an address expression) a register
7412 used to represent G1. */
7415 combine_givs_p (g1
, g2
)
7416 struct induction
*g1
, *g2
;
7420 /* With the introduction of ext dependent givs, we must care for modes.
7421 G2 must not use a wider mode than G1. */
7422 if (GET_MODE_SIZE (g1
->mode
) < GET_MODE_SIZE (g2
->mode
))
7425 ret
= comb
= express_from (g1
, g2
);
7426 if (comb
== NULL_RTX
)
7428 if (g1
->mode
!= g2
->mode
)
7429 ret
= gen_lowpart (g2
->mode
, comb
);
7431 /* If these givs are identical, they can be combined. We use the results
7432 of express_from because the addends are not in a canonical form, so
7433 rtx_equal_p is a weaker test. */
7434 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
7435 combination to be the other way round. */
7436 if (comb
== g1
->dest_reg
7437 && (g1
->giv_type
== DEST_REG
|| g2
->giv_type
== DEST_ADDR
))
7442 /* If G2 can be expressed as a function of G1 and that function is valid
7443 as an address and no more expensive than using a register for G2,
7444 the expression of G2 in terms of G1 can be used. */
7446 && g2
->giv_type
== DEST_ADDR
7447 && memory_address_p (GET_MODE (g2
->mem
), ret
))
7453 /* Check each extension dependent giv in this class to see if its
7454 root biv is safe from wrapping in the interior mode, which would
7455 make the giv illegal. */
7458 check_ext_dependent_givs (bl
, loop_info
)
7459 struct iv_class
*bl
;
7460 struct loop_info
*loop_info
;
7462 int ze_ok
= 0, se_ok
= 0, info_ok
= 0;
7463 enum machine_mode biv_mode
= GET_MODE (bl
->biv
->src_reg
);
7464 HOST_WIDE_INT start_val
;
7465 unsigned HOST_WIDE_INT u_end_val
= 0;
7466 unsigned HOST_WIDE_INT u_start_val
= 0;
7468 struct induction
*v
;
7470 /* Make sure the iteration data is available. We must have
7471 constants in order to be certain of no overflow. */
7472 /* ??? An unknown iteration count with an increment of +-1
7473 combined with friendly exit tests of against an invariant
7474 value is also amenable to optimization. Not implemented. */
7475 if (loop_info
->n_iterations
> 0
7476 && bl
->initial_value
7477 && GET_CODE (bl
->initial_value
) == CONST_INT
7478 && (incr
= biv_total_increment (bl
))
7479 && GET_CODE (incr
) == CONST_INT
7480 /* Make sure the host can represent the arithmetic. */
7481 && HOST_BITS_PER_WIDE_INT
>= GET_MODE_BITSIZE (biv_mode
))
7483 unsigned HOST_WIDE_INT abs_incr
, total_incr
;
7484 HOST_WIDE_INT s_end_val
;
7488 start_val
= INTVAL (bl
->initial_value
);
7489 u_start_val
= start_val
;
7491 neg_incr
= 0, abs_incr
= INTVAL (incr
);
7492 if (INTVAL (incr
) < 0)
7493 neg_incr
= 1, abs_incr
= -abs_incr
;
7494 total_incr
= abs_incr
* loop_info
->n_iterations
;
7496 /* Check for host arithmetic overflow. */
7497 if (total_incr
/ loop_info
->n_iterations
== abs_incr
)
7499 unsigned HOST_WIDE_INT u_max
;
7500 HOST_WIDE_INT s_max
;
7502 u_end_val
= start_val
+ (neg_incr
? -total_incr
: total_incr
);
7503 s_end_val
= u_end_val
;
7504 u_max
= GET_MODE_MASK (biv_mode
);
7507 /* Check zero extension of biv ok. */
7509 /* Check for host arithmetic overflow. */
7511 ? u_end_val
< u_start_val
7512 : u_end_val
> u_start_val
)
7513 /* Check for target arithmetic overflow. */
7515 ? 1 /* taken care of with host overflow */
7516 : u_end_val
<= u_max
))
7521 /* Check sign extension of biv ok. */
7522 /* ??? While it is true that overflow with signed and pointer
7523 arithmetic is undefined, I fear too many programmers don't
7524 keep this fact in mind -- myself included on occasion.
7525 So leave alone with the signed overflow optimizations. */
7526 if (start_val
>= -s_max
- 1
7527 /* Check for host arithmetic overflow. */
7529 ? s_end_val
< start_val
7530 : s_end_val
> start_val
)
7531 /* Check for target arithmetic overflow. */
7533 ? s_end_val
>= -s_max
- 1
7534 : s_end_val
<= s_max
))
7541 /* Invalidate givs that fail the tests. */
7542 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
7543 if (v
->ext_dependent
)
7545 enum rtx_code code
= GET_CODE (v
->ext_dependent
);
7558 /* We don't know whether this value is being used as either
7559 signed or unsigned, so to safely truncate we must satisfy
7560 both. The initial check here verifies the BIV itself;
7561 once that is successful we may check its range wrt the
7565 enum machine_mode outer_mode
= GET_MODE (v
->ext_dependent
);
7566 unsigned HOST_WIDE_INT max
= GET_MODE_MASK (outer_mode
) >> 1;
7568 /* We know from the above that both endpoints are nonnegative,
7569 and that there is no wrapping. Verify that both endpoints
7570 are within the (signed) range of the outer mode. */
7571 if (u_start_val
<= max
&& u_end_val
<= max
)
7582 if (loop_dump_stream
)
7584 fprintf (loop_dump_stream
,
7585 "Verified ext dependent giv at %d of reg %d\n",
7586 INSN_UID (v
->insn
), bl
->regno
);
7591 if (loop_dump_stream
)
7596 why
= "biv iteration values overflowed";
7600 incr
= biv_total_increment (bl
);
7601 if (incr
== const1_rtx
)
7602 why
= "biv iteration info incomplete; incr by 1";
7604 why
= "biv iteration info incomplete";
7607 fprintf (loop_dump_stream
,
7608 "Failed ext dependent giv at %d, %s\n",
7609 INSN_UID (v
->insn
), why
);
7612 bl
->all_reduced
= 0;
7617 /* Generate a version of VALUE in a mode appropriate for initializing V. */
7620 extend_value_for_giv (v
, value
)
7621 struct induction
*v
;
7624 rtx ext_dep
= v
->ext_dependent
;
7629 /* Recall that check_ext_dependent_givs verified that the known bounds
7630 of a biv did not overflow or wrap with respect to the extension for
7631 the giv. Therefore, constants need no additional adjustment. */
7632 if (CONSTANT_P (value
) && GET_MODE (value
) == VOIDmode
)
7635 /* Otherwise, we must adjust the value to compensate for the
7636 differing modes of the biv and the giv. */
7637 return gen_rtx_fmt_e (GET_CODE (ext_dep
), GET_MODE (ext_dep
), value
);
7640 struct combine_givs_stats
7647 cmp_combine_givs_stats (xp
, yp
)
7651 const struct combine_givs_stats
* const x
=
7652 (const struct combine_givs_stats
*) xp
;
7653 const struct combine_givs_stats
* const y
=
7654 (const struct combine_givs_stats
*) yp
;
7656 d
= y
->total_benefit
- x
->total_benefit
;
7657 /* Stabilize the sort. */
7659 d
= x
->giv_number
- y
->giv_number
;
7663 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7664 any other. If so, point SAME to the giv combined with and set NEW_REG to
7665 be an expression (in terms of the other giv's DEST_REG) equivalent to the
7666 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
7669 combine_givs (regs
, bl
)
7670 struct loop_regs
*regs
;
7671 struct iv_class
*bl
;
7673 /* Additional benefit to add for being combined multiple times. */
7674 const int extra_benefit
= 3;
7676 struct induction
*g1
, *g2
, **giv_array
;
7677 int i
, j
, k
, giv_count
;
7678 struct combine_givs_stats
*stats
;
7681 /* Count givs, because bl->giv_count is incorrect here. */
7683 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
7688 = (struct induction
**) alloca (giv_count
* sizeof (struct induction
*));
7690 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
7692 giv_array
[i
++] = g1
;
7694 stats
= (struct combine_givs_stats
*) xcalloc (giv_count
, sizeof (*stats
));
7695 can_combine
= (rtx
*) xcalloc (giv_count
, giv_count
* sizeof (rtx
));
7697 for (i
= 0; i
< giv_count
; i
++)
7703 stats
[i
].giv_number
= i
;
7705 /* If a DEST_REG GIV is used only once, do not allow it to combine
7706 with anything, for in doing so we will gain nothing that cannot
7707 be had by simply letting the GIV with which we would have combined
7708 to be reduced on its own. The losage shows up in particular with
7709 DEST_ADDR targets on hosts with reg+reg addressing, though it can
7710 be seen elsewhere as well. */
7711 if (g1
->giv_type
== DEST_REG
7712 && (single_use
= regs
->array
[REGNO (g1
->dest_reg
)].single_usage
)
7713 && single_use
!= const0_rtx
)
7716 this_benefit
= g1
->benefit
;
7717 /* Add an additional weight for zero addends. */
7718 if (g1
->no_const_addval
)
7721 for (j
= 0; j
< giv_count
; j
++)
7727 && (this_combine
= combine_givs_p (g1
, g2
)) != NULL_RTX
)
7729 can_combine
[i
* giv_count
+ j
] = this_combine
;
7730 this_benefit
+= g2
->benefit
+ extra_benefit
;
7733 stats
[i
].total_benefit
= this_benefit
;
7736 /* Iterate, combining until we can't. */
7738 qsort (stats
, giv_count
, sizeof (*stats
), cmp_combine_givs_stats
);
7740 if (loop_dump_stream
)
7742 fprintf (loop_dump_stream
, "Sorted combine statistics:\n");
7743 for (k
= 0; k
< giv_count
; k
++)
7745 g1
= giv_array
[stats
[k
].giv_number
];
7746 if (!g1
->combined_with
&& !g1
->same
)
7747 fprintf (loop_dump_stream
, " {%d, %d}",
7748 INSN_UID (giv_array
[stats
[k
].giv_number
]->insn
),
7749 stats
[k
].total_benefit
);
7751 putc ('\n', loop_dump_stream
);
7754 for (k
= 0; k
< giv_count
; k
++)
7756 int g1_add_benefit
= 0;
7758 i
= stats
[k
].giv_number
;
7761 /* If it has already been combined, skip. */
7762 if (g1
->combined_with
|| g1
->same
)
7765 for (j
= 0; j
< giv_count
; j
++)
7768 if (g1
!= g2
&& can_combine
[i
* giv_count
+ j
]
7769 /* If it has already been combined, skip. */
7770 && ! g2
->same
&& ! g2
->combined_with
)
7774 g2
->new_reg
= can_combine
[i
* giv_count
+ j
];
7776 /* For destination, we now may replace by mem expression instead
7777 of register. This changes the costs considerably, so add the
7779 if (g2
->giv_type
== DEST_ADDR
)
7780 g2
->benefit
= (g2
->benefit
+ reg_address_cost
7781 - address_cost (g2
->new_reg
,
7782 GET_MODE (g2
->mem
)));
7783 g1
->combined_with
++;
7784 g1
->lifetime
+= g2
->lifetime
;
7786 g1_add_benefit
+= g2
->benefit
;
7788 /* ??? The new final_[bg]iv_value code does a much better job
7789 of finding replaceable giv's, and hence this code may no
7790 longer be necessary. */
7791 if (! g2
->replaceable
&& REG_USERVAR_P (g2
->dest_reg
))
7792 g1_add_benefit
-= copy_cost
;
7794 /* To help optimize the next set of combinations, remove
7795 this giv from the benefits of other potential mates. */
7796 for (l
= 0; l
< giv_count
; ++l
)
7798 int m
= stats
[l
].giv_number
;
7799 if (can_combine
[m
* giv_count
+ j
])
7800 stats
[l
].total_benefit
-= g2
->benefit
+ extra_benefit
;
7803 if (loop_dump_stream
)
7804 fprintf (loop_dump_stream
,
7805 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
7806 INSN_UID (g2
->insn
), INSN_UID (g1
->insn
),
7807 g1
->benefit
, g1_add_benefit
, g1
->lifetime
);
7811 /* To help optimize the next set of combinations, remove
7812 this giv from the benefits of other potential mates. */
7813 if (g1
->combined_with
)
7815 for (j
= 0; j
< giv_count
; ++j
)
7817 int m
= stats
[j
].giv_number
;
7818 if (can_combine
[m
* giv_count
+ i
])
7819 stats
[j
].total_benefit
-= g1
->benefit
+ extra_benefit
;
7822 g1
->benefit
+= g1_add_benefit
;
7824 /* We've finished with this giv, and everything it touched.
7825 Restart the combination so that proper weights for the
7826 rest of the givs are properly taken into account. */
7827 /* ??? Ideally we would compact the arrays at this point, so
7828 as to not cover old ground. But sanely compacting
7829 can_combine is tricky. */
7839 /* Generate sequence for REG = B * M + A. */
7842 gen_add_mult (b
, m
, a
, reg
)
7843 rtx b
; /* initial value of basic induction variable */
7844 rtx m
; /* multiplicative constant */
7845 rtx a
; /* additive constant */
7846 rtx reg
; /* destination register */
7852 /* Use unsigned arithmetic. */
7853 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 1);
7855 emit_move_insn (reg
, result
);
7863 /* Update registers created in insn sequence SEQ. */
7866 loop_regs_update (loop
, seq
)
7867 const struct loop
*loop ATTRIBUTE_UNUSED
;
7872 /* Update register info for alias analysis. */
7874 if (seq
== NULL_RTX
)
7880 while (insn
!= NULL_RTX
)
7882 rtx set
= single_set (insn
);
7884 if (set
&& GET_CODE (SET_DEST (set
)) == REG
)
7885 record_base_value (REGNO (SET_DEST (set
)), SET_SRC (set
), 0);
7887 insn
= NEXT_INSN (insn
);
7890 else if (GET_CODE (seq
) == SET
7891 && GET_CODE (SET_DEST (seq
)) == REG
)
7892 record_base_value (REGNO (SET_DEST (seq
)), SET_SRC (seq
), 0);
7896 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. */
7899 loop_iv_add_mult_emit_before (loop
, b
, m
, a
, reg
, before_bb
, before_insn
)
7900 const struct loop
*loop
;
7901 rtx b
; /* initial value of basic induction variable */
7902 rtx m
; /* multiplicative constant */
7903 rtx a
; /* additive constant */
7904 rtx reg
; /* destination register */
7905 basic_block before_bb
;
7912 loop_iv_add_mult_hoist (loop
, b
, m
, a
, reg
);
7916 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7917 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
7919 /* Increase the lifetime of any invariants moved further in code. */
7920 update_reg_last_use (a
, before_insn
);
7921 update_reg_last_use (b
, before_insn
);
7922 update_reg_last_use (m
, before_insn
);
7924 loop_insn_emit_before (loop
, before_bb
, before_insn
, seq
);
7926 /* It is possible that the expansion created lots of new registers.
7927 Iterate over the sequence we just created and record them all. */
7928 loop_regs_update (loop
, seq
);
7932 /* Emit insns in loop pre-header to set REG = B * M + A. */
7935 loop_iv_add_mult_sink (loop
, b
, m
, a
, reg
)
7936 const struct loop
*loop
;
7937 rtx b
; /* initial value of basic induction variable */
7938 rtx m
; /* multiplicative constant */
7939 rtx a
; /* additive constant */
7940 rtx reg
; /* destination register */
7944 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7945 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
7947 /* Increase the lifetime of any invariants moved further in code.
7948 ???? Is this really necessary? */
7949 update_reg_last_use (a
, loop
->sink
);
7950 update_reg_last_use (b
, loop
->sink
);
7951 update_reg_last_use (m
, loop
->sink
);
7953 loop_insn_sink (loop
, seq
);
7955 /* It is possible that the expansion created lots of new registers.
7956 Iterate over the sequence we just created and record them all. */
7957 loop_regs_update (loop
, seq
);
7961 /* Emit insns after loop to set REG = B * M + A. */
7964 loop_iv_add_mult_hoist (loop
, b
, m
, a
, reg
)
7965 const struct loop
*loop
;
7966 rtx b
; /* initial value of basic induction variable */
7967 rtx m
; /* multiplicative constant */
7968 rtx a
; /* additive constant */
7969 rtx reg
; /* destination register */
7973 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7974 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
7976 loop_insn_hoist (loop
, seq
);
7978 /* It is possible that the expansion created lots of new registers.
7979 Iterate over the sequence we just created and record them all. */
7980 loop_regs_update (loop
, seq
);
7985 /* Similar to gen_add_mult, but compute cost rather than generating
7989 iv_add_mult_cost (b
, m
, a
, reg
)
7990 rtx b
; /* initial value of basic induction variable */
7991 rtx m
; /* multiplicative constant */
7992 rtx a
; /* additive constant */
7993 rtx reg
; /* destination register */
7999 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 1);
8001 emit_move_insn (reg
, result
);
8002 last
= get_last_insn ();
8005 rtx t
= single_set (last
);
8007 cost
+= rtx_cost (SET_SRC (t
), SET
);
8008 last
= PREV_INSN (last
);
8014 /* Test whether A * B can be computed without
8015 an actual multiply insn. Value is 1 if so.
8017 ??? This function stinks because it generates a ton of wasted RTL
8018 ??? and as a result fragments GC memory to no end. There are other
8019 ??? places in the compiler which are invoked a lot and do the same
8020 ??? thing, generate wasted RTL just to see if something is possible. */
8023 product_cheap_p (a
, b
)
8030 /* If only one is constant, make it B. */
8031 if (GET_CODE (a
) == CONST_INT
)
8032 tmp
= a
, a
= b
, b
= tmp
;
8034 /* If first constant, both constant, so don't need multiply. */
8035 if (GET_CODE (a
) == CONST_INT
)
8038 /* If second not constant, neither is constant, so would need multiply. */
8039 if (GET_CODE (b
) != CONST_INT
)
8042 /* One operand is constant, so might not need multiply insn. Generate the
8043 code for the multiply and see if a call or multiply, or long sequence
8044 of insns is generated. */
8047 expand_mult (GET_MODE (a
), a
, b
, NULL_RTX
, 1);
8055 while (tmp
!= NULL_RTX
)
8057 rtx next
= NEXT_INSN (tmp
);
8060 || GET_CODE (tmp
) != INSN
8061 || (GET_CODE (PATTERN (tmp
)) == SET
8062 && GET_CODE (SET_SRC (PATTERN (tmp
))) == MULT
)
8063 || (GET_CODE (PATTERN (tmp
)) == PARALLEL
8064 && GET_CODE (XVECEXP (PATTERN (tmp
), 0, 0)) == SET
8065 && GET_CODE (SET_SRC (XVECEXP (PATTERN (tmp
), 0, 0))) == MULT
))
8074 else if (GET_CODE (tmp
) == SET
8075 && GET_CODE (SET_SRC (tmp
)) == MULT
)
8077 else if (GET_CODE (tmp
) == PARALLEL
8078 && GET_CODE (XVECEXP (tmp
, 0, 0)) == SET
8079 && GET_CODE (SET_SRC (XVECEXP (tmp
, 0, 0))) == MULT
)
8085 /* Check to see if loop can be terminated by a "decrement and branch until
8086 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
8087 Also try reversing an increment loop to a decrement loop
8088 to see if the optimization can be performed.
8089 Value is nonzero if optimization was performed. */
8091 /* This is useful even if the architecture doesn't have such an insn,
8092 because it might change a loops which increments from 0 to n to a loop
8093 which decrements from n to 0. A loop that decrements to zero is usually
8094 faster than one that increments from zero. */
8096 /* ??? This could be rewritten to use some of the loop unrolling procedures,
8097 such as approx_final_value, biv_total_increment, loop_iterations, and
8098 final_[bg]iv_value. */
8101 check_dbra_loop (loop
, insn_count
)
8105 struct loop_info
*loop_info
= LOOP_INFO (loop
);
8106 struct loop_regs
*regs
= LOOP_REGS (loop
);
8107 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
8108 struct iv_class
*bl
;
8115 rtx before_comparison
;
8119 int compare_and_branch
;
8120 rtx loop_start
= loop
->start
;
8121 rtx loop_end
= loop
->end
;
8123 /* If last insn is a conditional branch, and the insn before tests a
8124 register value, try to optimize it. Otherwise, we can't do anything. */
8126 jump
= PREV_INSN (loop_end
);
8127 comparison
= get_condition_for_loop (loop
, jump
);
8128 if (comparison
== 0)
8130 if (!onlyjump_p (jump
))
8133 /* Try to compute whether the compare/branch at the loop end is one or
8134 two instructions. */
8135 get_condition (jump
, &first_compare
);
8136 if (first_compare
== jump
)
8137 compare_and_branch
= 1;
8138 else if (first_compare
== prev_nonnote_insn (jump
))
8139 compare_and_branch
= 2;
8144 /* If more than one condition is present to control the loop, then
8145 do not proceed, as this function does not know how to rewrite
8146 loop tests with more than one condition.
8148 Look backwards from the first insn in the last comparison
8149 sequence and see if we've got another comparison sequence. */
8152 if ((jump1
= prev_nonnote_insn (first_compare
)) != loop
->cont
)
8153 if (GET_CODE (jump1
) == JUMP_INSN
)
8157 /* Check all of the bivs to see if the compare uses one of them.
8158 Skip biv's set more than once because we can't guarantee that
8159 it will be zero on the last iteration. Also skip if the biv is
8160 used between its update and the test insn. */
8162 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
8164 if (bl
->biv_count
== 1
8165 && ! bl
->biv
->maybe_multiple
8166 && bl
->biv
->dest_reg
== XEXP (comparison
, 0)
8167 && ! reg_used_between_p (regno_reg_rtx
[bl
->regno
], bl
->biv
->insn
,
8175 /* Look for the case where the basic induction variable is always
8176 nonnegative, and equals zero on the last iteration.
8177 In this case, add a reg_note REG_NONNEG, which allows the
8178 m68k DBRA instruction to be used. */
8180 if (((GET_CODE (comparison
) == GT
8181 && GET_CODE (XEXP (comparison
, 1)) == CONST_INT
8182 && INTVAL (XEXP (comparison
, 1)) == -1)
8183 || (GET_CODE (comparison
) == NE
&& XEXP (comparison
, 1) == const0_rtx
))
8184 && GET_CODE (bl
->biv
->add_val
) == CONST_INT
8185 && INTVAL (bl
->biv
->add_val
) < 0)
8187 /* Initial value must be greater than 0,
8188 init_val % -dec_value == 0 to ensure that it equals zero on
8189 the last iteration */
8191 if (GET_CODE (bl
->initial_value
) == CONST_INT
8192 && INTVAL (bl
->initial_value
) > 0
8193 && (INTVAL (bl
->initial_value
)
8194 % (-INTVAL (bl
->biv
->add_val
))) == 0)
8196 /* register always nonnegative, add REG_NOTE to branch */
8197 if (! find_reg_note (jump
, REG_NONNEG
, NULL_RTX
))
8199 = gen_rtx_EXPR_LIST (REG_NONNEG
, bl
->biv
->dest_reg
,
8206 /* If the decrement is 1 and the value was tested as >= 0 before
8207 the loop, then we can safely optimize. */
8208 for (p
= loop_start
; p
; p
= PREV_INSN (p
))
8210 if (GET_CODE (p
) == CODE_LABEL
)
8212 if (GET_CODE (p
) != JUMP_INSN
)
8215 before_comparison
= get_condition_for_loop (loop
, p
);
8216 if (before_comparison
8217 && XEXP (before_comparison
, 0) == bl
->biv
->dest_reg
8218 && GET_CODE (before_comparison
) == LT
8219 && XEXP (before_comparison
, 1) == const0_rtx
8220 && ! reg_set_between_p (bl
->biv
->dest_reg
, p
, loop_start
)
8221 && INTVAL (bl
->biv
->add_val
) == -1)
8223 if (! find_reg_note (jump
, REG_NONNEG
, NULL_RTX
))
8225 = gen_rtx_EXPR_LIST (REG_NONNEG
, bl
->biv
->dest_reg
,
8233 else if (GET_CODE (bl
->biv
->add_val
) == CONST_INT
8234 && INTVAL (bl
->biv
->add_val
) > 0)
8236 /* Try to change inc to dec, so can apply above optimization. */
8238 all registers modified are induction variables or invariant,
8239 all memory references have non-overlapping addresses
8240 (obviously true if only one write)
8241 allow 2 insns for the compare/jump at the end of the loop. */
8242 /* Also, we must avoid any instructions which use both the reversed
8243 biv and another biv. Such instructions will fail if the loop is
8244 reversed. We meet this condition by requiring that either
8245 no_use_except_counting is true, or else that there is only
8247 int num_nonfixed_reads
= 0;
8248 /* 1 if the iteration var is used only to count iterations. */
8249 int no_use_except_counting
= 0;
8250 /* 1 if the loop has no memory store, or it has a single memory store
8251 which is reversible. */
8252 int reversible_mem_store
= 1;
8254 if (bl
->giv_count
== 0
8255 && !loop
->exit_count
8256 && !loop_info
->has_multiple_exit_targets
)
8258 rtx bivreg
= regno_reg_rtx
[bl
->regno
];
8259 struct iv_class
*blt
;
8261 /* If there are no givs for this biv, and the only exit is the
8262 fall through at the end of the loop, then
8263 see if perhaps there are no uses except to count. */
8264 no_use_except_counting
= 1;
8265 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
8268 rtx set
= single_set (p
);
8270 if (set
&& GET_CODE (SET_DEST (set
)) == REG
8271 && REGNO (SET_DEST (set
)) == bl
->regno
)
8272 /* An insn that sets the biv is okay. */
8274 else if (!reg_mentioned_p (bivreg
, PATTERN (p
)))
8275 /* An insn that doesn't mention the biv is okay. */
8277 else if (p
== prev_nonnote_insn (prev_nonnote_insn (loop_end
))
8278 || p
== prev_nonnote_insn (loop_end
))
8280 /* If either of these insns uses the biv and sets a pseudo
8281 that has more than one usage, then the biv has uses
8282 other than counting since it's used to derive a value
8283 that is used more than one time. */
8284 note_stores (PATTERN (p
), note_set_pseudo_multiple_uses
,
8286 if (regs
->multiple_uses
)
8288 no_use_except_counting
= 0;
8294 no_use_except_counting
= 0;
8299 /* A biv has uses besides counting if it is used to set
8301 for (blt
= ivs
->list
; blt
; blt
= blt
->next
)
8303 && reg_mentioned_p (bivreg
, SET_SRC (blt
->init_set
)))
8305 no_use_except_counting
= 0;
8310 if (no_use_except_counting
)
8311 /* No need to worry about MEMs. */
8313 else if (loop_info
->num_mem_sets
<= 1)
8315 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
8317 num_nonfixed_reads
+= count_nonfixed_reads (loop
, PATTERN (p
));
8319 /* If the loop has a single store, and the destination address is
8320 invariant, then we can't reverse the loop, because this address
8321 might then have the wrong value at loop exit.
8322 This would work if the source was invariant also, however, in that
8323 case, the insn should have been moved out of the loop. */
8325 if (loop_info
->num_mem_sets
== 1)
8327 struct induction
*v
;
8329 /* If we could prove that each of the memory locations
8330 written to was different, then we could reverse the
8331 store -- but we don't presently have any way of
8333 reversible_mem_store
= 0;
8335 /* If the store depends on a register that is set after the
8336 store, it depends on the initial value, and is thus not
8338 for (v
= bl
->giv
; reversible_mem_store
&& v
; v
= v
->next_iv
)
8340 if (v
->giv_type
== DEST_REG
8341 && reg_mentioned_p (v
->dest_reg
,
8342 PATTERN (loop_info
->first_loop_store_insn
))
8343 && loop_insn_first_p (loop_info
->first_loop_store_insn
,
8345 reversible_mem_store
= 0;
8352 /* This code only acts for innermost loops. Also it simplifies
8353 the memory address check by only reversing loops with
8354 zero or one memory access.
8355 Two memory accesses could involve parts of the same array,
8356 and that can't be reversed.
8357 If the biv is used only for counting, than we don't need to worry
8358 about all these things. */
8360 if ((num_nonfixed_reads
<= 1
8361 && ! loop_info
->has_nonconst_call
8362 && ! loop_info
->has_prefetch
8363 && ! loop_info
->has_volatile
8364 && reversible_mem_store
8365 && (bl
->giv_count
+ bl
->biv_count
+ loop_info
->num_mem_sets
8366 + num_unmoved_movables (loop
) + compare_and_branch
== insn_count
)
8367 && (bl
== ivs
->list
&& bl
->next
== 0))
8368 || (no_use_except_counting
&& ! loop_info
->has_prefetch
))
8372 /* Loop can be reversed. */
8373 if (loop_dump_stream
)
8374 fprintf (loop_dump_stream
, "Can reverse loop\n");
8376 /* Now check other conditions:
8378 The increment must be a constant, as must the initial value,
8379 and the comparison code must be LT.
8381 This test can probably be improved since +/- 1 in the constant
8382 can be obtained by changing LT to LE and vice versa; this is
8386 /* for constants, LE gets turned into LT */
8387 && (GET_CODE (comparison
) == LT
8388 || (GET_CODE (comparison
) == LE
8389 && no_use_except_counting
)))
8391 HOST_WIDE_INT add_val
, add_adjust
, comparison_val
= 0;
8392 rtx initial_value
, comparison_value
;
8394 enum rtx_code cmp_code
;
8395 int comparison_const_width
;
8396 unsigned HOST_WIDE_INT comparison_sign_mask
;
8398 add_val
= INTVAL (bl
->biv
->add_val
);
8399 comparison_value
= XEXP (comparison
, 1);
8400 if (GET_MODE (comparison_value
) == VOIDmode
)
8401 comparison_const_width
8402 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison
, 0)));
8404 comparison_const_width
8405 = GET_MODE_BITSIZE (GET_MODE (comparison_value
));
8406 if (comparison_const_width
> HOST_BITS_PER_WIDE_INT
)
8407 comparison_const_width
= HOST_BITS_PER_WIDE_INT
;
8408 comparison_sign_mask
8409 = (unsigned HOST_WIDE_INT
) 1 << (comparison_const_width
- 1);
8411 /* If the comparison value is not a loop invariant, then we
8412 can not reverse this loop.
8414 ??? If the insns which initialize the comparison value as
8415 a whole compute an invariant result, then we could move
8416 them out of the loop and proceed with loop reversal. */
8417 if (! loop_invariant_p (loop
, comparison_value
))
8420 if (GET_CODE (comparison_value
) == CONST_INT
)
8421 comparison_val
= INTVAL (comparison_value
);
8422 initial_value
= bl
->initial_value
;
8424 /* Normalize the initial value if it is an integer and
8425 has no other use except as a counter. This will allow
8426 a few more loops to be reversed. */
8427 if (no_use_except_counting
8428 && GET_CODE (comparison_value
) == CONST_INT
8429 && GET_CODE (initial_value
) == CONST_INT
)
8431 comparison_val
= comparison_val
- INTVAL (bl
->initial_value
);
8432 /* The code below requires comparison_val to be a multiple
8433 of add_val in order to do the loop reversal, so
8434 round up comparison_val to a multiple of add_val.
8435 Since comparison_value is constant, we know that the
8436 current comparison code is LT. */
8437 comparison_val
= comparison_val
+ add_val
- 1;
8439 -= (unsigned HOST_WIDE_INT
) comparison_val
% add_val
;
8440 /* We postpone overflow checks for COMPARISON_VAL here;
8441 even if there is an overflow, we might still be able to
8442 reverse the loop, if converting the loop exit test to
8444 initial_value
= const0_rtx
;
8447 /* First check if we can do a vanilla loop reversal. */
8448 if (initial_value
== const0_rtx
8449 /* If we have a decrement_and_branch_on_count,
8450 prefer the NE test, since this will allow that
8451 instruction to be generated. Note that we must
8452 use a vanilla loop reversal if the biv is used to
8453 calculate a giv or has a non-counting use. */
8454 #if ! defined (HAVE_decrement_and_branch_until_zero) \
8455 && defined (HAVE_decrement_and_branch_on_count)
8456 && (! (add_val
== 1 && loop
->vtop
8457 && (bl
->biv_count
== 0
8458 || no_use_except_counting
)))
8460 && GET_CODE (comparison_value
) == CONST_INT
8461 /* Now do postponed overflow checks on COMPARISON_VAL. */
8462 && ! (((comparison_val
- add_val
) ^ INTVAL (comparison_value
))
8463 & comparison_sign_mask
))
8465 /* Register will always be nonnegative, with value
8466 0 on last iteration */
8467 add_adjust
= add_val
;
8471 else if (add_val
== 1 && loop
->vtop
8472 && (bl
->biv_count
== 0
8473 || no_use_except_counting
))
8481 if (GET_CODE (comparison
) == LE
)
8482 add_adjust
-= add_val
;
8484 /* If the initial value is not zero, or if the comparison
8485 value is not an exact multiple of the increment, then we
8486 can not reverse this loop. */
8487 if (initial_value
== const0_rtx
8488 && GET_CODE (comparison_value
) == CONST_INT
)
8490 if (((unsigned HOST_WIDE_INT
) comparison_val
% add_val
) != 0)
8495 if (! no_use_except_counting
|| add_val
!= 1)
8499 final_value
= comparison_value
;
8501 /* Reset these in case we normalized the initial value
8502 and comparison value above. */
8503 if (GET_CODE (comparison_value
) == CONST_INT
8504 && GET_CODE (initial_value
) == CONST_INT
)
8506 comparison_value
= GEN_INT (comparison_val
);
8508 = GEN_INT (comparison_val
+ INTVAL (bl
->initial_value
));
8510 bl
->initial_value
= initial_value
;
8512 /* Save some info needed to produce the new insns. */
8513 reg
= bl
->biv
->dest_reg
;
8514 jump_label
= condjump_label (PREV_INSN (loop_end
));
8515 new_add_val
= GEN_INT (-INTVAL (bl
->biv
->add_val
));
8517 /* Set start_value; if this is not a CONST_INT, we need
8519 Initialize biv to start_value before loop start.
8520 The old initializing insn will be deleted as a
8521 dead store by flow.c. */
8522 if (initial_value
== const0_rtx
8523 && GET_CODE (comparison_value
) == CONST_INT
)
8525 start_value
= GEN_INT (comparison_val
- add_adjust
);
8526 loop_insn_hoist (loop
, gen_move_insn (reg
, start_value
));
8528 else if (GET_CODE (initial_value
) == CONST_INT
)
8530 enum machine_mode mode
= GET_MODE (reg
);
8531 rtx offset
= GEN_INT (-INTVAL (initial_value
) - add_adjust
);
8532 rtx add_insn
= gen_add3_insn (reg
, comparison_value
, offset
);
8538 = gen_rtx_PLUS (mode
, comparison_value
, offset
);
8539 loop_insn_hoist (loop
, add_insn
);
8540 if (GET_CODE (comparison
) == LE
)
8541 final_value
= gen_rtx_PLUS (mode
, comparison_value
,
8544 else if (! add_adjust
)
8546 enum machine_mode mode
= GET_MODE (reg
);
8547 rtx sub_insn
= gen_sub3_insn (reg
, comparison_value
,
8553 = gen_rtx_MINUS (mode
, comparison_value
, initial_value
);
8554 loop_insn_hoist (loop
, sub_insn
);
8557 /* We could handle the other cases too, but it'll be
8558 better to have a testcase first. */
8561 /* We may not have a single insn which can increment a reg, so
8562 create a sequence to hold all the insns from expand_inc. */
8564 expand_inc (reg
, new_add_val
);
8568 p
= loop_insn_emit_before (loop
, 0, bl
->biv
->insn
, tem
);
8569 delete_insn (bl
->biv
->insn
);
8571 /* Update biv info to reflect its new status. */
8573 bl
->initial_value
= start_value
;
8574 bl
->biv
->add_val
= new_add_val
;
8576 /* Update loop info. */
8577 loop_info
->initial_value
= reg
;
8578 loop_info
->initial_equiv_value
= reg
;
8579 loop_info
->final_value
= const0_rtx
;
8580 loop_info
->final_equiv_value
= const0_rtx
;
8581 loop_info
->comparison_value
= const0_rtx
;
8582 loop_info
->comparison_code
= cmp_code
;
8583 loop_info
->increment
= new_add_val
;
8585 /* Inc LABEL_NUSES so that delete_insn will
8586 not delete the label. */
8587 LABEL_NUSES (XEXP (jump_label
, 0))++;
8589 /* Emit an insn after the end of the loop to set the biv's
8590 proper exit value if it is used anywhere outside the loop. */
8591 if ((REGNO_LAST_UID (bl
->regno
) != INSN_UID (first_compare
))
8593 || REGNO_FIRST_UID (bl
->regno
) != INSN_UID (bl
->init_insn
))
8594 loop_insn_sink (loop
, gen_load_of_final_value (reg
, final_value
));
8596 /* Delete compare/branch at end of loop. */
8597 delete_related_insns (PREV_INSN (loop_end
));
8598 if (compare_and_branch
== 2)
8599 delete_related_insns (first_compare
);
8601 /* Add new compare/branch insn at end of loop. */
8603 emit_cmp_and_jump_insns (reg
, const0_rtx
, cmp_code
, NULL_RTX
,
8605 XEXP (jump_label
, 0));
8608 emit_jump_insn_before (tem
, loop_end
);
8610 for (tem
= PREV_INSN (loop_end
);
8611 tem
&& GET_CODE (tem
) != JUMP_INSN
;
8612 tem
= PREV_INSN (tem
))
8616 JUMP_LABEL (tem
) = XEXP (jump_label
, 0);
8622 /* Increment of LABEL_NUSES done above. */
8623 /* Register is now always nonnegative,
8624 so add REG_NONNEG note to the branch. */
8625 REG_NOTES (tem
) = gen_rtx_EXPR_LIST (REG_NONNEG
, reg
,
8631 /* No insn may reference both the reversed and another biv or it
8632 will fail (see comment near the top of the loop reversal
8634 Earlier on, we have verified that the biv has no use except
8635 counting, or it is the only biv in this function.
8636 However, the code that computes no_use_except_counting does
8637 not verify reg notes. It's possible to have an insn that
8638 references another biv, and has a REG_EQUAL note with an
8639 expression based on the reversed biv. To avoid this case,
8640 remove all REG_EQUAL notes based on the reversed biv
8642 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
8646 rtx set
= single_set (p
);
8647 /* If this is a set of a GIV based on the reversed biv, any
8648 REG_EQUAL notes should still be correct. */
8650 || GET_CODE (SET_DEST (set
)) != REG
8651 || (size_t) REGNO (SET_DEST (set
)) >= ivs
->n_regs
8652 || REG_IV_TYPE (ivs
, REGNO (SET_DEST (set
))) != GENERAL_INDUCT
8653 || REG_IV_INFO (ivs
, REGNO (SET_DEST (set
)))->src_reg
!= bl
->biv
->src_reg
)
8654 for (pnote
= ®_NOTES (p
); *pnote
;)
8656 if (REG_NOTE_KIND (*pnote
) == REG_EQUAL
8657 && reg_mentioned_p (regno_reg_rtx
[bl
->regno
],
8659 *pnote
= XEXP (*pnote
, 1);
8661 pnote
= &XEXP (*pnote
, 1);
8665 /* Mark that this biv has been reversed. Each giv which depends
8666 on this biv, and which is also live past the end of the loop
8667 will have to be fixed up. */
8671 if (loop_dump_stream
)
8673 fprintf (loop_dump_stream
, "Reversed loop");
8675 fprintf (loop_dump_stream
, " and added reg_nonneg\n");
8677 fprintf (loop_dump_stream
, "\n");
8688 /* Verify whether the biv BL appears to be eliminable,
8689 based on the insns in the loop that refer to it.
8691 If ELIMINATE_P is nonzero, actually do the elimination.
8693 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8694 determine whether invariant insns should be placed inside or at the
8695 start of the loop. */
8698 maybe_eliminate_biv (loop
, bl
, eliminate_p
, threshold
, insn_count
)
8699 const struct loop
*loop
;
8700 struct iv_class
*bl
;
8702 int threshold
, insn_count
;
8704 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
8705 rtx reg
= bl
->biv
->dest_reg
;
8708 /* Scan all insns in the loop, stopping if we find one that uses the
8709 biv in a way that we cannot eliminate. */
8711 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
8713 enum rtx_code code
= GET_CODE (p
);
8714 basic_block where_bb
= 0;
8715 rtx where_insn
= threshold
>= insn_count
? 0 : p
;
8718 /* If this is a libcall that sets a giv, skip ahead to its end. */
8719 if (GET_RTX_CLASS (code
) == 'i')
8721 note
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
);
8725 rtx last
= XEXP (note
, 0);
8726 rtx set
= single_set (last
);
8728 if (set
&& GET_CODE (SET_DEST (set
)) == REG
)
8730 unsigned int regno
= REGNO (SET_DEST (set
));
8732 if (regno
< ivs
->n_regs
8733 && REG_IV_TYPE (ivs
, regno
) == GENERAL_INDUCT
8734 && REG_IV_INFO (ivs
, regno
)->src_reg
== bl
->biv
->src_reg
)
8740 /* Closely examine the insn if the biv is mentioned. */
8741 if ((code
== INSN
|| code
== JUMP_INSN
|| code
== CALL_INSN
)
8742 && reg_mentioned_p (reg
, PATTERN (p
))
8743 && ! maybe_eliminate_biv_1 (loop
, PATTERN (p
), p
, bl
,
8744 eliminate_p
, where_bb
, where_insn
))
8746 if (loop_dump_stream
)
8747 fprintf (loop_dump_stream
,
8748 "Cannot eliminate biv %d: biv used in insn %d.\n",
8749 bl
->regno
, INSN_UID (p
));
8753 /* If we are eliminating, kill REG_EQUAL notes mentioning the biv. */
8755 && (note
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
)) != NULL_RTX
8756 && reg_mentioned_p (reg
, XEXP (note
, 0)))
8757 remove_note (p
, note
);
8762 if (loop_dump_stream
)
8763 fprintf (loop_dump_stream
, "biv %d %s eliminated.\n",
8764 bl
->regno
, eliminate_p
? "was" : "can be");
8771 /* INSN and REFERENCE are instructions in the same insn chain.
8772 Return nonzero if INSN is first. */
8775 loop_insn_first_p (insn
, reference
)
8776 rtx insn
, reference
;
8780 for (p
= insn
, q
= reference
;;)
8782 /* Start with test for not first so that INSN == REFERENCE yields not
8784 if (q
== insn
|| ! p
)
8786 if (p
== reference
|| ! q
)
8789 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8790 previous insn, hence the <= comparison below does not work if
8792 if (INSN_UID (p
) < max_uid_for_loop
8793 && INSN_UID (q
) < max_uid_for_loop
8794 && GET_CODE (p
) != NOTE
)
8795 return INSN_LUID (p
) <= INSN_LUID (q
);
8797 if (INSN_UID (p
) >= max_uid_for_loop
8798 || GET_CODE (p
) == NOTE
)
8800 if (INSN_UID (q
) >= max_uid_for_loop
)
8805 /* We are trying to eliminate BIV in INSN using GIV. Return nonzero if
8806 the offset that we have to take into account due to auto-increment /
8807 div derivation is zero. */
8809 biv_elimination_giv_has_0_offset (biv
, giv
, insn
)
8810 struct induction
*biv
, *giv
;
8813 /* If the giv V had the auto-inc address optimization applied
8814 to it, and INSN occurs between the giv insn and the biv
8815 insn, then we'd have to adjust the value used here.
8816 This is rare, so we don't bother to make this possible. */
8817 if (giv
->auto_inc_opt
8818 && ((loop_insn_first_p (giv
->insn
, insn
)
8819 && loop_insn_first_p (insn
, biv
->insn
))
8820 || (loop_insn_first_p (biv
->insn
, insn
)
8821 && loop_insn_first_p (insn
, giv
->insn
))))
8827 /* If BL appears in X (part of the pattern of INSN), see if we can
8828 eliminate its use. If so, return 1. If not, return 0.
8830 If BIV does not appear in X, return 1.
8832 If ELIMINATE_P is nonzero, actually do the elimination.
8833 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
8834 Depending on how many items have been moved out of the loop, it
8835 will either be before INSN (when WHERE_INSN is nonzero) or at the
8836 start of the loop (when WHERE_INSN is zero). */
8839 maybe_eliminate_biv_1 (loop
, x
, insn
, bl
, eliminate_p
, where_bb
, where_insn
)
8840 const struct loop
*loop
;
8842 struct iv_class
*bl
;
8844 basic_block where_bb
;
8847 enum rtx_code code
= GET_CODE (x
);
8848 rtx reg
= bl
->biv
->dest_reg
;
8849 enum machine_mode mode
= GET_MODE (reg
);
8850 struct induction
*v
;
8862 /* If we haven't already been able to do something with this BIV,
8863 we can't eliminate it. */
8869 /* If this sets the BIV, it is not a problem. */
8870 if (SET_DEST (x
) == reg
)
8873 /* If this is an insn that defines a giv, it is also ok because
8874 it will go away when the giv is reduced. */
8875 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8876 if (v
->giv_type
== DEST_REG
&& SET_DEST (x
) == v
->dest_reg
)
8880 if (SET_DEST (x
) == cc0_rtx
&& SET_SRC (x
) == reg
)
8882 /* Can replace with any giv that was reduced and
8883 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8884 Require a constant for MULT_VAL, so we know it's nonzero.
8885 ??? We disable this optimization to avoid potential
8888 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8889 if (GET_CODE (v
->mult_val
) == CONST_INT
&& v
->mult_val
!= const0_rtx
8890 && v
->add_val
== const0_rtx
8891 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8895 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8901 /* If the giv has the opposite direction of change,
8902 then reverse the comparison. */
8903 if (INTVAL (v
->mult_val
) < 0)
8904 new = gen_rtx_COMPARE (GET_MODE (v
->new_reg
),
8905 const0_rtx
, v
->new_reg
);
8909 /* We can probably test that giv's reduced reg. */
8910 if (validate_change (insn
, &SET_SRC (x
), new, 0))
8914 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8915 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8916 Require a constant for MULT_VAL, so we know it's nonzero.
8917 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8918 overflow problem. */
8920 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8921 if (GET_CODE (v
->mult_val
) == CONST_INT
8922 && v
->mult_val
!= const0_rtx
8923 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8925 && (GET_CODE (v
->add_val
) == SYMBOL_REF
8926 || GET_CODE (v
->add_val
) == LABEL_REF
8927 || GET_CODE (v
->add_val
) == CONST
8928 || (GET_CODE (v
->add_val
) == REG
8929 && REG_POINTER (v
->add_val
))))
8931 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8937 /* If the giv has the opposite direction of change,
8938 then reverse the comparison. */
8939 if (INTVAL (v
->mult_val
) < 0)
8940 new = gen_rtx_COMPARE (VOIDmode
, copy_rtx (v
->add_val
),
8943 new = gen_rtx_COMPARE (VOIDmode
, v
->new_reg
,
8944 copy_rtx (v
->add_val
));
8946 /* Replace biv with the giv's reduced register. */
8947 update_reg_last_use (v
->add_val
, insn
);
8948 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
8951 /* Insn doesn't support that constant or invariant. Copy it
8952 into a register (it will be a loop invariant.) */
8953 tem
= gen_reg_rtx (GET_MODE (v
->new_reg
));
8955 loop_insn_emit_before (loop
, 0, where_insn
,
8957 copy_rtx (v
->add_val
)));
8959 /* Substitute the new register for its invariant value in
8960 the compare expression. */
8961 XEXP (new, (INTVAL (v
->mult_val
) < 0) ? 0 : 1) = tem
;
8962 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
8971 case GT
: case GE
: case GTU
: case GEU
:
8972 case LT
: case LE
: case LTU
: case LEU
:
8973 /* See if either argument is the biv. */
8974 if (XEXP (x
, 0) == reg
)
8975 arg
= XEXP (x
, 1), arg_operand
= 1;
8976 else if (XEXP (x
, 1) == reg
)
8977 arg
= XEXP (x
, 0), arg_operand
= 0;
8981 if (CONSTANT_P (arg
))
8983 /* First try to replace with any giv that has constant positive
8984 mult_val and constant add_val. We might be able to support
8985 negative mult_val, but it seems complex to do it in general. */
8987 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8988 if (GET_CODE (v
->mult_val
) == CONST_INT
8989 && INTVAL (v
->mult_val
) > 0
8990 && (GET_CODE (v
->add_val
) == SYMBOL_REF
8991 || GET_CODE (v
->add_val
) == LABEL_REF
8992 || GET_CODE (v
->add_val
) == CONST
8993 || (GET_CODE (v
->add_val
) == REG
8994 && REG_POINTER (v
->add_val
)))
8995 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8998 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
9001 /* Don't eliminate if the linear combination that makes up
9002 the giv overflows when it is applied to ARG. */
9003 if (GET_CODE (arg
) == CONST_INT
)
9007 if (GET_CODE (v
->add_val
) == CONST_INT
)
9008 add_val
= v
->add_val
;
9010 add_val
= const0_rtx
;
9012 if (const_mult_add_overflow_p (arg
, v
->mult_val
,
9020 /* Replace biv with the giv's reduced reg. */
9021 validate_change (insn
, &XEXP (x
, 1 - arg_operand
), v
->new_reg
, 1);
9023 /* If all constants are actually constant integers and
9024 the derived constant can be directly placed in the COMPARE,
9026 if (GET_CODE (arg
) == CONST_INT
9027 && GET_CODE (v
->add_val
) == CONST_INT
)
9029 tem
= expand_mult_add (arg
, NULL_RTX
, v
->mult_val
,
9030 v
->add_val
, mode
, 1);
9034 /* Otherwise, load it into a register. */
9035 tem
= gen_reg_rtx (mode
);
9036 loop_iv_add_mult_emit_before (loop
, arg
,
9037 v
->mult_val
, v
->add_val
,
9038 tem
, where_bb
, where_insn
);
9041 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
9043 if (apply_change_group ())
9047 /* Look for giv with positive constant mult_val and nonconst add_val.
9048 Insert insns to calculate new compare value.
9049 ??? Turn this off due to possible overflow. */
9051 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
9052 if (GET_CODE (v
->mult_val
) == CONST_INT
9053 && INTVAL (v
->mult_val
) > 0
9054 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
9060 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
9066 tem
= gen_reg_rtx (mode
);
9068 /* Replace biv with giv's reduced register. */
9069 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
9072 /* Compute value to compare against. */
9073 loop_iv_add_mult_emit_before (loop
, arg
,
9074 v
->mult_val
, v
->add_val
,
9075 tem
, where_bb
, where_insn
);
9076 /* Use it in this insn. */
9077 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
9078 if (apply_change_group ())
9082 else if (GET_CODE (arg
) == REG
|| GET_CODE (arg
) == MEM
)
9084 if (loop_invariant_p (loop
, arg
) == 1)
9086 /* Look for giv with constant positive mult_val and nonconst
9087 add_val. Insert insns to compute new compare value.
9088 ??? Turn this off due to possible overflow. */
9090 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
9091 if (GET_CODE (v
->mult_val
) == CONST_INT
&& INTVAL (v
->mult_val
) > 0
9092 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
9098 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
9104 tem
= gen_reg_rtx (mode
);
9106 /* Replace biv with giv's reduced register. */
9107 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
9110 /* Compute value to compare against. */
9111 loop_iv_add_mult_emit_before (loop
, arg
,
9112 v
->mult_val
, v
->add_val
,
9113 tem
, where_bb
, where_insn
);
9114 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
9115 if (apply_change_group ())
9120 /* This code has problems. Basically, you can't know when
9121 seeing if we will eliminate BL, whether a particular giv
9122 of ARG will be reduced. If it isn't going to be reduced,
9123 we can't eliminate BL. We can try forcing it to be reduced,
9124 but that can generate poor code.
9126 The problem is that the benefit of reducing TV, below should
9127 be increased if BL can actually be eliminated, but this means
9128 we might have to do a topological sort of the order in which
9129 we try to process biv. It doesn't seem worthwhile to do
9130 this sort of thing now. */
9133 /* Otherwise the reg compared with had better be a biv. */
9134 if (GET_CODE (arg
) != REG
9135 || REG_IV_TYPE (ivs
, REGNO (arg
)) != BASIC_INDUCT
)
9138 /* Look for a pair of givs, one for each biv,
9139 with identical coefficients. */
9140 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
9142 struct induction
*tv
;
9144 if (v
->ignore
|| v
->maybe_dead
|| v
->mode
!= mode
)
9147 for (tv
= REG_IV_CLASS (ivs
, REGNO (arg
))->giv
; tv
;
9149 if (! tv
->ignore
&& ! tv
->maybe_dead
9150 && rtx_equal_p (tv
->mult_val
, v
->mult_val
)
9151 && rtx_equal_p (tv
->add_val
, v
->add_val
)
9152 && tv
->mode
== mode
)
9154 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
9160 /* Replace biv with its giv's reduced reg. */
9161 XEXP (x
, 1 - arg_operand
) = v
->new_reg
;
9162 /* Replace other operand with the other giv's
9164 XEXP (x
, arg_operand
) = tv
->new_reg
;
9171 /* If we get here, the biv can't be eliminated. */
9175 /* If this address is a DEST_ADDR giv, it doesn't matter if the
9176 biv is used in it, since it will be replaced. */
9177 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
9178 if (v
->giv_type
== DEST_ADDR
&& v
->location
== &XEXP (x
, 0))
9186 /* See if any subexpression fails elimination. */
9187 fmt
= GET_RTX_FORMAT (code
);
9188 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
9193 if (! maybe_eliminate_biv_1 (loop
, XEXP (x
, i
), insn
, bl
,
9194 eliminate_p
, where_bb
, where_insn
))
9199 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
9200 if (! maybe_eliminate_biv_1 (loop
, XVECEXP (x
, i
, j
), insn
, bl
,
9201 eliminate_p
, where_bb
, where_insn
))
9210 /* Return nonzero if the last use of REG
9211 is in an insn following INSN in the same basic block. */
9214 last_use_this_basic_block (reg
, insn
)
9220 n
&& GET_CODE (n
) != CODE_LABEL
&& GET_CODE (n
) != JUMP_INSN
;
9223 if (REGNO_LAST_UID (REGNO (reg
)) == INSN_UID (n
))
9229 /* Called via `note_stores' to record the initial value of a biv. Here we
9230 just record the location of the set and process it later. */
9233 record_initial (dest
, set
, data
)
9236 void *data ATTRIBUTE_UNUSED
;
9238 struct loop_ivs
*ivs
= (struct loop_ivs
*) data
;
9239 struct iv_class
*bl
;
9241 if (GET_CODE (dest
) != REG
9242 || REGNO (dest
) >= ivs
->n_regs
9243 || REG_IV_TYPE (ivs
, REGNO (dest
)) != BASIC_INDUCT
)
9246 bl
= REG_IV_CLASS (ivs
, REGNO (dest
));
9248 /* If this is the first set found, record it. */
9249 if (bl
->init_insn
== 0)
9251 bl
->init_insn
= note_insn
;
9256 /* If any of the registers in X are "old" and currently have a last use earlier
9257 than INSN, update them to have a last use of INSN. Their actual last use
9258 will be the previous insn but it will not have a valid uid_luid so we can't
9259 use it. X must be a source expression only. */
9262 update_reg_last_use (x
, insn
)
9266 /* Check for the case where INSN does not have a valid luid. In this case,
9267 there is no need to modify the regno_last_uid, as this can only happen
9268 when code is inserted after the loop_end to set a pseudo's final value,
9269 and hence this insn will never be the last use of x.
9270 ???? This comment is not correct. See for example loop_givs_reduce.
9271 This may insert an insn before another new insn. */
9272 if (GET_CODE (x
) == REG
&& REGNO (x
) < max_reg_before_loop
9273 && INSN_UID (insn
) < max_uid_for_loop
9274 && REGNO_LAST_LUID (REGNO (x
)) < INSN_LUID (insn
))
9276 REGNO_LAST_UID (REGNO (x
)) = INSN_UID (insn
);
9281 const char *fmt
= GET_RTX_FORMAT (GET_CODE (x
));
9282 for (i
= GET_RTX_LENGTH (GET_CODE (x
)) - 1; i
>= 0; i
--)
9285 update_reg_last_use (XEXP (x
, i
), insn
);
9286 else if (fmt
[i
] == 'E')
9287 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
9288 update_reg_last_use (XVECEXP (x
, i
, j
), insn
);
9293 /* Given an insn INSN and condition COND, return the condition in a
9294 canonical form to simplify testing by callers. Specifically:
9296 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
9297 (2) Both operands will be machine operands; (cc0) will have been replaced.
9298 (3) If an operand is a constant, it will be the second operand.
9299 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
9300 for GE, GEU, and LEU.
9302 If the condition cannot be understood, or is an inequality floating-point
9303 comparison which needs to be reversed, 0 will be returned.
9305 If REVERSE is nonzero, then reverse the condition prior to canonizing it.
9307 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9308 insn used in locating the condition was found. If a replacement test
9309 of the condition is desired, it should be placed in front of that
9310 insn and we will be sure that the inputs are still valid.
9312 If WANT_REG is nonzero, we wish the condition to be relative to that
9313 register, if possible. Therefore, do not canonicalize the condition
9317 canonicalize_condition (insn
, cond
, reverse
, earliest
, want_reg
)
9329 int reverse_code
= 0;
9330 enum machine_mode mode
;
9332 code
= GET_CODE (cond
);
9333 mode
= GET_MODE (cond
);
9334 op0
= XEXP (cond
, 0);
9335 op1
= XEXP (cond
, 1);
9338 code
= reversed_comparison_code (cond
, insn
);
9339 if (code
== UNKNOWN
)
9345 /* If we are comparing a register with zero, see if the register is set
9346 in the previous insn to a COMPARE or a comparison operation. Perform
9347 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
9350 while (GET_RTX_CLASS (code
) == '<'
9351 && op1
== CONST0_RTX (GET_MODE (op0
))
9354 /* Set nonzero when we find something of interest. */
9358 /* If comparison with cc0, import actual comparison from compare
9362 if ((prev
= prev_nonnote_insn (prev
)) == 0
9363 || GET_CODE (prev
) != INSN
9364 || (set
= single_set (prev
)) == 0
9365 || SET_DEST (set
) != cc0_rtx
)
9368 op0
= SET_SRC (set
);
9369 op1
= CONST0_RTX (GET_MODE (op0
));
9375 /* If this is a COMPARE, pick up the two things being compared. */
9376 if (GET_CODE (op0
) == COMPARE
)
9378 op1
= XEXP (op0
, 1);
9379 op0
= XEXP (op0
, 0);
9382 else if (GET_CODE (op0
) != REG
)
9385 /* Go back to the previous insn. Stop if it is not an INSN. We also
9386 stop if it isn't a single set or if it has a REG_INC note because
9387 we don't want to bother dealing with it. */
9389 if ((prev
= prev_nonnote_insn (prev
)) == 0
9390 || GET_CODE (prev
) != INSN
9391 || FIND_REG_INC_NOTE (prev
, NULL_RTX
))
9394 set
= set_of (op0
, prev
);
9397 && (GET_CODE (set
) != SET
9398 || !rtx_equal_p (SET_DEST (set
), op0
)))
9401 /* If this is setting OP0, get what it sets it to if it looks
9405 enum machine_mode inner_mode
= GET_MODE (SET_DEST (set
));
9406 #ifdef FLOAT_STORE_FLAG_VALUE
9407 REAL_VALUE_TYPE fsfv
;
9410 /* ??? We may not combine comparisons done in a CCmode with
9411 comparisons not done in a CCmode. This is to aid targets
9412 like Alpha that have an IEEE compliant EQ instruction, and
9413 a non-IEEE compliant BEQ instruction. The use of CCmode is
9414 actually artificial, simply to prevent the combination, but
9415 should not affect other platforms.
9417 However, we must allow VOIDmode comparisons to match either
9418 CCmode or non-CCmode comparison, because some ports have
9419 modeless comparisons inside branch patterns.
9421 ??? This mode check should perhaps look more like the mode check
9422 in simplify_comparison in combine. */
9424 if ((GET_CODE (SET_SRC (set
)) == COMPARE
9427 && GET_MODE_CLASS (inner_mode
) == MODE_INT
9428 && (GET_MODE_BITSIZE (inner_mode
)
9429 <= HOST_BITS_PER_WIDE_INT
)
9430 && (STORE_FLAG_VALUE
9431 & ((HOST_WIDE_INT
) 1
9432 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
9433 #ifdef FLOAT_STORE_FLAG_VALUE
9435 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
9436 && (fsfv
= FLOAT_STORE_FLAG_VALUE (inner_mode
),
9437 REAL_VALUE_NEGATIVE (fsfv
)))
9440 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<'))
9441 && (((GET_MODE_CLASS (mode
) == MODE_CC
)
9442 == (GET_MODE_CLASS (inner_mode
) == MODE_CC
))
9443 || mode
== VOIDmode
|| inner_mode
== VOIDmode
))
9445 else if (((code
== EQ
9447 && (GET_MODE_BITSIZE (inner_mode
)
9448 <= HOST_BITS_PER_WIDE_INT
)
9449 && GET_MODE_CLASS (inner_mode
) == MODE_INT
9450 && (STORE_FLAG_VALUE
9451 & ((HOST_WIDE_INT
) 1
9452 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
9453 #ifdef FLOAT_STORE_FLAG_VALUE
9455 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
9456 && (fsfv
= FLOAT_STORE_FLAG_VALUE (inner_mode
),
9457 REAL_VALUE_NEGATIVE (fsfv
)))
9460 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<'
9461 && (((GET_MODE_CLASS (mode
) == MODE_CC
)
9462 == (GET_MODE_CLASS (inner_mode
) == MODE_CC
))
9463 || mode
== VOIDmode
|| inner_mode
== VOIDmode
))
9473 else if (reg_set_p (op0
, prev
))
9474 /* If this sets OP0, but not directly, we have to give up. */
9479 if (GET_RTX_CLASS (GET_CODE (x
)) == '<')
9480 code
= GET_CODE (x
);
9483 code
= reversed_comparison_code (x
, prev
);
9484 if (code
== UNKNOWN
)
9489 op0
= XEXP (x
, 0), op1
= XEXP (x
, 1);
9495 /* If constant is first, put it last. */
9496 if (CONSTANT_P (op0
))
9497 code
= swap_condition (code
), tem
= op0
, op0
= op1
, op1
= tem
;
9499 /* If OP0 is the result of a comparison, we weren't able to find what
9500 was really being compared, so fail. */
9501 if (GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
)
9504 /* Canonicalize any ordered comparison with integers involving equality
9505 if we can do computations in the relevant mode and we do not
9508 if (GET_CODE (op1
) == CONST_INT
9509 && GET_MODE (op0
) != VOIDmode
9510 && GET_MODE_BITSIZE (GET_MODE (op0
)) <= HOST_BITS_PER_WIDE_INT
)
9512 HOST_WIDE_INT const_val
= INTVAL (op1
);
9513 unsigned HOST_WIDE_INT uconst_val
= const_val
;
9514 unsigned HOST_WIDE_INT max_val
9515 = (unsigned HOST_WIDE_INT
) GET_MODE_MASK (GET_MODE (op0
));
9520 if ((unsigned HOST_WIDE_INT
) const_val
!= max_val
>> 1)
9521 code
= LT
, op1
= gen_int_mode (const_val
+ 1, GET_MODE (op0
));
9524 /* When cross-compiling, const_val might be sign-extended from
9525 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9527 if ((HOST_WIDE_INT
) (const_val
& max_val
)
9528 != (((HOST_WIDE_INT
) 1
9529 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
9530 code
= GT
, op1
= gen_int_mode (const_val
- 1, GET_MODE (op0
));
9534 if (uconst_val
< max_val
)
9535 code
= LTU
, op1
= gen_int_mode (uconst_val
+ 1, GET_MODE (op0
));
9539 if (uconst_val
!= 0)
9540 code
= GTU
, op1
= gen_int_mode (uconst_val
- 1, GET_MODE (op0
));
9548 /* Never return CC0; return zero instead. */
9552 return gen_rtx_fmt_ee (code
, VOIDmode
, op0
, op1
);
9555 /* Given a jump insn JUMP, return the condition that will cause it to branch
9556 to its JUMP_LABEL. If the condition cannot be understood, or is an
9557 inequality floating-point comparison which needs to be reversed, 0 will
9560 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9561 insn used in locating the condition was found. If a replacement test
9562 of the condition is desired, it should be placed in front of that
9563 insn and we will be sure that the inputs are still valid. */
9566 get_condition (jump
, earliest
)
9574 /* If this is not a standard conditional jump, we can't parse it. */
9575 if (GET_CODE (jump
) != JUMP_INSN
9576 || ! any_condjump_p (jump
))
9578 set
= pc_set (jump
);
9580 cond
= XEXP (SET_SRC (set
), 0);
9582 /* If this branches to JUMP_LABEL when the condition is false, reverse
9585 = GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
9586 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (jump
);
9588 return canonicalize_condition (jump
, cond
, reverse
, earliest
, NULL_RTX
);
9591 /* Similar to above routine, except that we also put an invariant last
9592 unless both operands are invariants. */
9595 get_condition_for_loop (loop
, x
)
9596 const struct loop
*loop
;
9599 rtx comparison
= get_condition (x
, (rtx
*) 0);
9602 || ! loop_invariant_p (loop
, XEXP (comparison
, 0))
9603 || loop_invariant_p (loop
, XEXP (comparison
, 1)))
9606 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison
)), VOIDmode
,
9607 XEXP (comparison
, 1), XEXP (comparison
, 0));
9610 /* Scan the function and determine whether it has indirect (computed) jumps.
9612 This is taken mostly from flow.c; similar code exists elsewhere
9613 in the compiler. It may be useful to put this into rtlanal.c. */
9615 indirect_jump_in_function_p (start
)
9620 for (insn
= start
; insn
; insn
= NEXT_INSN (insn
))
9621 if (computed_jump_p (insn
))
9627 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9628 documentation for LOOP_MEMS for the definition of `appropriate'.
9629 This function is called from prescan_loop via for_each_rtx. */
9632 insert_loop_mem (mem
, data
)
9634 void *data ATTRIBUTE_UNUSED
;
9636 struct loop_info
*loop_info
= data
;
9643 switch (GET_CODE (m
))
9649 /* We're not interested in MEMs that are only clobbered. */
9653 /* We're not interested in the MEM associated with a
9654 CONST_DOUBLE, so there's no need to traverse into this. */
9658 /* We're not interested in any MEMs that only appear in notes. */
9662 /* This is not a MEM. */
9666 /* See if we've already seen this MEM. */
9667 for (i
= 0; i
< loop_info
->mems_idx
; ++i
)
9668 if (rtx_equal_p (m
, loop_info
->mems
[i
].mem
))
9670 if (GET_MODE (m
) != GET_MODE (loop_info
->mems
[i
].mem
))
9671 /* The modes of the two memory accesses are different. If
9672 this happens, something tricky is going on, and we just
9673 don't optimize accesses to this MEM. */
9674 loop_info
->mems
[i
].optimize
= 0;
9679 /* Resize the array, if necessary. */
9680 if (loop_info
->mems_idx
== loop_info
->mems_allocated
)
9682 if (loop_info
->mems_allocated
!= 0)
9683 loop_info
->mems_allocated
*= 2;
9685 loop_info
->mems_allocated
= 32;
9687 loop_info
->mems
= (loop_mem_info
*)
9688 xrealloc (loop_info
->mems
,
9689 loop_info
->mems_allocated
* sizeof (loop_mem_info
));
9692 /* Actually insert the MEM. */
9693 loop_info
->mems
[loop_info
->mems_idx
].mem
= m
;
9694 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9695 because we can't put it in a register. We still store it in the
9696 table, though, so that if we see the same address later, but in a
9697 non-BLK mode, we'll not think we can optimize it at that point. */
9698 loop_info
->mems
[loop_info
->mems_idx
].optimize
= (GET_MODE (m
) != BLKmode
);
9699 loop_info
->mems
[loop_info
->mems_idx
].reg
= NULL_RTX
;
9700 ++loop_info
->mems_idx
;
9706 /* Allocate REGS->ARRAY or reallocate it if it is too small.
9708 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
9709 register that is modified by an insn between FROM and TO. If the
9710 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
9711 more, stop incrementing it, to avoid overflow.
9713 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
9714 register I is used, if it is only used once. Otherwise, it is set
9715 to 0 (for no uses) or const0_rtx for more than one use. This
9716 parameter may be zero, in which case this processing is not done.
9718 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
9719 optimize register I. */
9722 loop_regs_scan (loop
, extra_size
)
9723 const struct loop
*loop
;
9726 struct loop_regs
*regs
= LOOP_REGS (loop
);
9728 /* last_set[n] is nonzero iff reg n has been set in the current
9729 basic block. In that case, it is the insn that last set reg n. */
9734 old_nregs
= regs
->num
;
9735 regs
->num
= max_reg_num ();
9737 /* Grow the regs array if not allocated or too small. */
9738 if (regs
->num
>= regs
->size
)
9740 regs
->size
= regs
->num
+ extra_size
;
9742 regs
->array
= (struct loop_reg
*)
9743 xrealloc (regs
->array
, regs
->size
* sizeof (*regs
->array
));
9745 /* Zero the new elements. */
9746 memset (regs
->array
+ old_nregs
, 0,
9747 (regs
->size
- old_nregs
) * sizeof (*regs
->array
));
9750 /* Clear previously scanned fields but do not clear n_times_set. */
9751 for (i
= 0; i
< old_nregs
; i
++)
9753 regs
->array
[i
].set_in_loop
= 0;
9754 regs
->array
[i
].may_not_optimize
= 0;
9755 regs
->array
[i
].single_usage
= NULL_RTX
;
9758 last_set
= (rtx
*) xcalloc (regs
->num
, sizeof (rtx
));
9760 /* Scan the loop, recording register usage. */
9761 for (insn
= loop
->top
? loop
->top
: loop
->start
; insn
!= loop
->end
;
9762 insn
= NEXT_INSN (insn
))
9766 /* Record registers that have exactly one use. */
9767 find_single_use_in_loop (regs
, insn
, PATTERN (insn
));
9769 /* Include uses in REG_EQUAL notes. */
9770 if (REG_NOTES (insn
))
9771 find_single_use_in_loop (regs
, insn
, REG_NOTES (insn
));
9773 if (GET_CODE (PATTERN (insn
)) == SET
9774 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
9775 count_one_set (regs
, insn
, PATTERN (insn
), last_set
);
9776 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
9779 for (i
= XVECLEN (PATTERN (insn
), 0) - 1; i
>= 0; i
--)
9780 count_one_set (regs
, insn
, XVECEXP (PATTERN (insn
), 0, i
),
9785 if (GET_CODE (insn
) == CODE_LABEL
|| GET_CODE (insn
) == JUMP_INSN
)
9786 memset (last_set
, 0, regs
->num
* sizeof (rtx
));
9788 /* Invalidate all registers used for function argument passing.
9789 We check rtx_varies_p for the same reason as below, to allow
9790 optimizing PIC calculations. */
9791 if (GET_CODE (insn
) == CALL_INSN
)
9794 for (link
= CALL_INSN_FUNCTION_USAGE (insn
);
9796 link
= XEXP (link
, 1))
9800 if (GET_CODE (op
= XEXP (link
, 0)) == USE
9801 && GET_CODE (reg
= XEXP (op
, 0)) == REG
9802 && rtx_varies_p (reg
, 1))
9803 regs
->array
[REGNO (reg
)].may_not_optimize
= 1;
9808 /* Invalidate all hard registers clobbered by calls. With one exception:
9809 a call-clobbered PIC register is still function-invariant for our
9810 purposes, since we can hoist any PIC calculations out of the loop.
9811 Thus the call to rtx_varies_p. */
9812 if (LOOP_INFO (loop
)->has_call
)
9813 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
9814 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
)
9815 && rtx_varies_p (regno_reg_rtx
[i
], 1))
9817 regs
->array
[i
].may_not_optimize
= 1;
9818 regs
->array
[i
].set_in_loop
= 1;
9821 #ifdef AVOID_CCMODE_COPIES
9822 /* Don't try to move insns which set CC registers if we should not
9823 create CCmode register copies. */
9824 for (i
= regs
->num
- 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
9825 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx
[i
])) == MODE_CC
)
9826 regs
->array
[i
].may_not_optimize
= 1;
9829 /* Set regs->array[I].n_times_set for the new registers. */
9830 for (i
= old_nregs
; i
< regs
->num
; i
++)
9831 regs
->array
[i
].n_times_set
= regs
->array
[i
].set_in_loop
;
9836 /* Returns the number of real INSNs in the LOOP. */
9839 count_insns_in_loop (loop
)
9840 const struct loop
*loop
;
9845 for (insn
= loop
->top
? loop
->top
: loop
->start
; insn
!= loop
->end
;
9846 insn
= NEXT_INSN (insn
))
9853 /* Move MEMs into registers for the duration of the loop. */
9857 const struct loop
*loop
;
9859 struct loop_info
*loop_info
= LOOP_INFO (loop
);
9860 struct loop_regs
*regs
= LOOP_REGS (loop
);
9861 int maybe_never
= 0;
9863 rtx p
, prev_ebb_head
;
9864 rtx label
= NULL_RTX
;
9866 /* Nonzero if the next instruction may never be executed. */
9867 int next_maybe_never
= 0;
9868 unsigned int last_max_reg
= max_reg_num ();
9870 if (loop_info
->mems_idx
== 0)
9873 /* We cannot use next_label here because it skips over normal insns. */
9874 end_label
= next_nonnote_insn (loop
->end
);
9875 if (end_label
&& GET_CODE (end_label
) != CODE_LABEL
)
9876 end_label
= NULL_RTX
;
9878 /* Check to see if it's possible that some instructions in the loop are
9879 never executed. Also check if there is a goto out of the loop other
9880 than right after the end of the loop. */
9881 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
9883 p
= next_insn_in_loop (loop
, p
))
9885 if (GET_CODE (p
) == CODE_LABEL
)
9887 else if (GET_CODE (p
) == JUMP_INSN
9888 /* If we enter the loop in the middle, and scan
9889 around to the beginning, don't set maybe_never
9890 for that. This must be an unconditional jump,
9891 otherwise the code at the top of the loop might
9892 never be executed. Unconditional jumps are
9893 followed a by barrier then loop end. */
9894 && ! (GET_CODE (p
) == JUMP_INSN
9895 && JUMP_LABEL (p
) == loop
->top
9896 && NEXT_INSN (NEXT_INSN (p
)) == loop
->end
9897 && any_uncondjump_p (p
)))
9899 /* If this is a jump outside of the loop but not right
9900 after the end of the loop, we would have to emit new fixup
9901 sequences for each such label. */
9902 if (/* If we can't tell where control might go when this
9903 JUMP_INSN is executed, we must be conservative. */
9905 || (JUMP_LABEL (p
) != end_label
9906 && (INSN_UID (JUMP_LABEL (p
)) >= max_uid_for_loop
9907 || INSN_LUID (JUMP_LABEL (p
)) < INSN_LUID (loop
->start
)
9908 || INSN_LUID (JUMP_LABEL (p
)) > INSN_LUID (loop
->end
))))
9911 if (!any_condjump_p (p
))
9912 /* Something complicated. */
9915 /* If there are any more instructions in the loop, they
9916 might not be reached. */
9917 next_maybe_never
= 1;
9919 else if (next_maybe_never
)
9923 /* Find start of the extended basic block that enters the loop. */
9924 for (p
= loop
->start
;
9925 PREV_INSN (p
) && GET_CODE (p
) != CODE_LABEL
;
9932 /* Build table of mems that get set to constant values before the
9934 for (; p
!= loop
->start
; p
= NEXT_INSN (p
))
9935 cselib_process_insn (p
);
9937 /* Actually move the MEMs. */
9938 for (i
= 0; i
< loop_info
->mems_idx
; ++i
)
9940 regset_head load_copies
;
9941 regset_head store_copies
;
9944 rtx mem
= loop_info
->mems
[i
].mem
;
9947 if (MEM_VOLATILE_P (mem
)
9948 || loop_invariant_p (loop
, XEXP (mem
, 0)) != 1)
9949 /* There's no telling whether or not MEM is modified. */
9950 loop_info
->mems
[i
].optimize
= 0;
9952 /* Go through the MEMs written to in the loop to see if this
9953 one is aliased by one of them. */
9954 mem_list_entry
= loop_info
->store_mems
;
9955 while (mem_list_entry
)
9957 if (rtx_equal_p (mem
, XEXP (mem_list_entry
, 0)))
9959 else if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
9962 /* MEM is indeed aliased by this store. */
9963 loop_info
->mems
[i
].optimize
= 0;
9966 mem_list_entry
= XEXP (mem_list_entry
, 1);
9969 if (flag_float_store
&& written
9970 && GET_MODE_CLASS (GET_MODE (mem
)) == MODE_FLOAT
)
9971 loop_info
->mems
[i
].optimize
= 0;
9973 /* If this MEM is written to, we must be sure that there
9974 are no reads from another MEM that aliases this one. */
9975 if (loop_info
->mems
[i
].optimize
&& written
)
9979 for (j
= 0; j
< loop_info
->mems_idx
; ++j
)
9983 else if (true_dependence (mem
,
9985 loop_info
->mems
[j
].mem
,
9988 /* It's not safe to hoist loop_info->mems[i] out of
9989 the loop because writes to it might not be
9990 seen by reads from loop_info->mems[j]. */
9991 loop_info
->mems
[i
].optimize
= 0;
9997 if (maybe_never
&& may_trap_p (mem
))
9998 /* We can't access the MEM outside the loop; it might
9999 cause a trap that wouldn't have happened otherwise. */
10000 loop_info
->mems
[i
].optimize
= 0;
10002 if (!loop_info
->mems
[i
].optimize
)
10003 /* We thought we were going to lift this MEM out of the
10004 loop, but later discovered that we could not. */
10007 INIT_REG_SET (&load_copies
);
10008 INIT_REG_SET (&store_copies
);
10010 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
10011 order to keep scan_loop from moving stores to this MEM
10012 out of the loop just because this REG is neither a
10013 user-variable nor used in the loop test. */
10014 reg
= gen_reg_rtx (GET_MODE (mem
));
10015 REG_USERVAR_P (reg
) = 1;
10016 loop_info
->mems
[i
].reg
= reg
;
10018 /* Now, replace all references to the MEM with the
10019 corresponding pseudos. */
10021 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
10023 p
= next_insn_in_loop (loop
, p
))
10029 set
= single_set (p
);
10031 /* See if this copies the mem into a register that isn't
10032 modified afterwards. We'll try to do copy propagation
10033 a little further on. */
10035 /* @@@ This test is _way_ too conservative. */
10037 && GET_CODE (SET_DEST (set
)) == REG
10038 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
10039 && REGNO (SET_DEST (set
)) < last_max_reg
10040 && regs
->array
[REGNO (SET_DEST (set
))].n_times_set
== 1
10041 && rtx_equal_p (SET_SRC (set
), mem
))
10042 SET_REGNO_REG_SET (&load_copies
, REGNO (SET_DEST (set
)));
10044 /* See if this copies the mem from a register that isn't
10045 modified afterwards. We'll try to remove the
10046 redundant copy later on by doing a little register
10047 renaming and copy propagation. This will help
10048 to untangle things for the BIV detection code. */
10051 && GET_CODE (SET_SRC (set
)) == REG
10052 && REGNO (SET_SRC (set
)) >= FIRST_PSEUDO_REGISTER
10053 && REGNO (SET_SRC (set
)) < last_max_reg
10054 && regs
->array
[REGNO (SET_SRC (set
))].n_times_set
== 1
10055 && rtx_equal_p (SET_DEST (set
), mem
))
10056 SET_REGNO_REG_SET (&store_copies
, REGNO (SET_SRC (set
)));
10058 /* If this is a call which uses / clobbers this memory
10059 location, we must not change the interface here. */
10060 if (GET_CODE (p
) == CALL_INSN
10061 && reg_mentioned_p (loop_info
->mems
[i
].mem
,
10062 CALL_INSN_FUNCTION_USAGE (p
)))
10064 cancel_changes (0);
10065 loop_info
->mems
[i
].optimize
= 0;
10069 /* Replace the memory reference with the shadow register. */
10070 replace_loop_mems (p
, loop_info
->mems
[i
].mem
,
10071 loop_info
->mems
[i
].reg
, written
);
10074 if (GET_CODE (p
) == CODE_LABEL
10075 || GET_CODE (p
) == JUMP_INSN
)
10079 if (! loop_info
->mems
[i
].optimize
)
10080 ; /* We found we couldn't do the replacement, so do nothing. */
10081 else if (! apply_change_group ())
10082 /* We couldn't replace all occurrences of the MEM. */
10083 loop_info
->mems
[i
].optimize
= 0;
10086 /* Load the memory immediately before LOOP->START, which is
10087 the NOTE_LOOP_BEG. */
10088 cselib_val
*e
= cselib_lookup (mem
, VOIDmode
, 0);
10092 struct elt_loc_list
*const_equiv
= 0;
10096 struct elt_loc_list
*equiv
;
10097 struct elt_loc_list
*best_equiv
= 0;
10098 for (equiv
= e
->locs
; equiv
; equiv
= equiv
->next
)
10100 if (CONSTANT_P (equiv
->loc
))
10101 const_equiv
= equiv
;
10102 else if (GET_CODE (equiv
->loc
) == REG
10103 /* Extending hard register lifetimes causes crash
10104 on SRC targets. Doing so on non-SRC is
10105 probably also not good idea, since we most
10106 probably have pseudoregister equivalence as
10108 && REGNO (equiv
->loc
) >= FIRST_PSEUDO_REGISTER
)
10109 best_equiv
= equiv
;
10111 /* Use the constant equivalence if that is cheap enough. */
10113 best_equiv
= const_equiv
;
10114 else if (const_equiv
10115 && (rtx_cost (const_equiv
->loc
, SET
)
10116 <= rtx_cost (best_equiv
->loc
, SET
)))
10118 best_equiv
= const_equiv
;
10122 /* If best_equiv is nonzero, we know that MEM is set to a
10123 constant or register before the loop. We will use this
10124 knowledge to initialize the shadow register with that
10125 constant or reg rather than by loading from MEM. */
10127 best
= copy_rtx (best_equiv
->loc
);
10130 set
= gen_move_insn (reg
, best
);
10131 set
= loop_insn_hoist (loop
, set
);
10134 for (p
= prev_ebb_head
; p
!= loop
->start
; p
= NEXT_INSN (p
))
10135 if (REGNO_LAST_UID (REGNO (best
)) == INSN_UID (p
))
10137 REGNO_LAST_UID (REGNO (best
)) = INSN_UID (set
);
10143 set_unique_reg_note (set
, REG_EQUAL
, copy_rtx (const_equiv
->loc
));
10147 if (label
== NULL_RTX
)
10149 label
= gen_label_rtx ();
10150 emit_label_after (label
, loop
->end
);
10153 /* Store the memory immediately after END, which is
10154 the NOTE_LOOP_END. */
10155 set
= gen_move_insn (copy_rtx (mem
), reg
);
10156 loop_insn_emit_after (loop
, 0, label
, set
);
10159 if (loop_dump_stream
)
10161 fprintf (loop_dump_stream
, "Hoisted regno %d %s from ",
10162 REGNO (reg
), (written
? "r/w" : "r/o"));
10163 print_rtl (loop_dump_stream
, mem
);
10164 fputc ('\n', loop_dump_stream
);
10167 /* Attempt a bit of copy propagation. This helps untangle the
10168 data flow, and enables {basic,general}_induction_var to find
10170 EXECUTE_IF_SET_IN_REG_SET
10171 (&load_copies
, FIRST_PSEUDO_REGISTER
, j
,
10173 try_copy_prop (loop
, reg
, j
);
10175 CLEAR_REG_SET (&load_copies
);
10177 EXECUTE_IF_SET_IN_REG_SET
10178 (&store_copies
, FIRST_PSEUDO_REGISTER
, j
,
10180 try_swap_copy_prop (loop
, reg
, j
);
10182 CLEAR_REG_SET (&store_copies
);
10186 if (label
!= NULL_RTX
&& end_label
!= NULL_RTX
)
10188 /* Now, we need to replace all references to the previous exit
10189 label with the new one. */
10190 replace_label_data rr
;
10193 rr
.update_label_nuses
= true;
10195 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
10197 for_each_rtx (&p
, replace_label
, &rr
);
10204 /* For communication between note_reg_stored and its caller. */
10205 struct note_reg_stored_arg
10211 /* Called via note_stores, record in SET_SEEN whether X, which is written,
10212 is equal to ARG. */
10214 note_reg_stored (x
, setter
, arg
)
10215 rtx x
, setter ATTRIBUTE_UNUSED
;
10218 struct note_reg_stored_arg
*t
= (struct note_reg_stored_arg
*) arg
;
10223 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
10224 There must be exactly one insn that sets this pseudo; it will be
10225 deleted if all replacements succeed and we can prove that the register
10226 is not used after the loop. */
10229 try_copy_prop (loop
, replacement
, regno
)
10230 const struct loop
*loop
;
10232 unsigned int regno
;
10234 /* This is the reg that we are copying from. */
10235 rtx reg_rtx
= regno_reg_rtx
[regno
];
10238 /* These help keep track of whether we replaced all uses of the reg. */
10239 int replaced_last
= 0;
10240 int store_is_first
= 0;
10242 for (insn
= next_insn_in_loop (loop
, loop
->scan_start
);
10244 insn
= next_insn_in_loop (loop
, insn
))
10248 /* Only substitute within one extended basic block from the initializing
10250 if (GET_CODE (insn
) == CODE_LABEL
&& init_insn
)
10253 if (! INSN_P (insn
))
10256 /* Is this the initializing insn? */
10257 set
= single_set (insn
);
10259 && GET_CODE (SET_DEST (set
)) == REG
10260 && REGNO (SET_DEST (set
)) == regno
)
10266 if (REGNO_FIRST_UID (regno
) == INSN_UID (insn
))
10267 store_is_first
= 1;
10270 /* Only substitute after seeing the initializing insn. */
10271 if (init_insn
&& insn
!= init_insn
)
10273 struct note_reg_stored_arg arg
;
10275 replace_loop_regs (insn
, reg_rtx
, replacement
);
10276 if (REGNO_LAST_UID (regno
) == INSN_UID (insn
))
10279 /* Stop replacing when REPLACEMENT is modified. */
10280 arg
.reg
= replacement
;
10282 note_stores (PATTERN (insn
), note_reg_stored
, &arg
);
10285 rtx note
= find_reg_note (insn
, REG_EQUAL
, NULL
);
10287 /* It is possible that we've turned previously valid REG_EQUAL to
10288 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
10289 REPLACEMENT is modified, we get different meaning. */
10290 if (note
&& reg_mentioned_p (replacement
, XEXP (note
, 0)))
10291 remove_note (insn
, note
);
10298 if (apply_change_group ())
10300 if (loop_dump_stream
)
10301 fprintf (loop_dump_stream
, " Replaced reg %d", regno
);
10302 if (store_is_first
&& replaced_last
)
10307 /* Assume we're just deleting INIT_INSN. */
10309 /* Look for REG_RETVAL note. If we're deleting the end of
10310 the libcall sequence, the whole sequence can go. */
10311 retval_note
= find_reg_note (init_insn
, REG_RETVAL
, NULL_RTX
);
10312 /* If we found a REG_RETVAL note, find the first instruction
10313 in the sequence. */
10315 first
= XEXP (retval_note
, 0);
10317 /* Delete the instructions. */
10318 loop_delete_insns (first
, init_insn
);
10320 if (loop_dump_stream
)
10321 fprintf (loop_dump_stream
, ".\n");
10325 /* Replace all the instructions from FIRST up to and including LAST
10326 with NOTE_INSN_DELETED notes. */
10329 loop_delete_insns (first
, last
)
10335 if (loop_dump_stream
)
10336 fprintf (loop_dump_stream
, ", deleting init_insn (%d)",
10338 delete_insn (first
);
10340 /* If this was the LAST instructions we're supposed to delete,
10345 first
= NEXT_INSN (first
);
10349 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
10350 loop LOOP if the order of the sets of these registers can be
10351 swapped. There must be exactly one insn within the loop that sets
10352 this pseudo followed immediately by a move insn that sets
10353 REPLACEMENT with REGNO. */
10355 try_swap_copy_prop (loop
, replacement
, regno
)
10356 const struct loop
*loop
;
10358 unsigned int regno
;
10361 rtx set
= NULL_RTX
;
10362 unsigned int new_regno
;
10364 new_regno
= REGNO (replacement
);
10366 for (insn
= next_insn_in_loop (loop
, loop
->scan_start
);
10368 insn
= next_insn_in_loop (loop
, insn
))
10370 /* Search for the insn that copies REGNO to NEW_REGNO? */
10372 && (set
= single_set (insn
))
10373 && GET_CODE (SET_DEST (set
)) == REG
10374 && REGNO (SET_DEST (set
)) == new_regno
10375 && GET_CODE (SET_SRC (set
)) == REG
10376 && REGNO (SET_SRC (set
)) == regno
)
10380 if (insn
!= NULL_RTX
)
10385 /* Some DEF-USE info would come in handy here to make this
10386 function more general. For now, just check the previous insn
10387 which is the most likely candidate for setting REGNO. */
10389 prev_insn
= PREV_INSN (insn
);
10392 && (prev_set
= single_set (prev_insn
))
10393 && GET_CODE (SET_DEST (prev_set
)) == REG
10394 && REGNO (SET_DEST (prev_set
)) == regno
)
10397 (set (reg regno) (expr))
10398 (set (reg new_regno) (reg regno))
10400 so try converting this to:
10401 (set (reg new_regno) (expr))
10402 (set (reg regno) (reg new_regno))
10404 The former construct is often generated when a global
10405 variable used for an induction variable is shadowed by a
10406 register (NEW_REGNO). The latter construct improves the
10407 chances of GIV replacement and BIV elimination. */
10409 validate_change (prev_insn
, &SET_DEST (prev_set
),
10411 validate_change (insn
, &SET_DEST (set
),
10413 validate_change (insn
, &SET_SRC (set
),
10416 if (apply_change_group ())
10418 if (loop_dump_stream
)
10419 fprintf (loop_dump_stream
,
10420 " Swapped set of reg %d at %d with reg %d at %d.\n",
10421 regno
, INSN_UID (insn
),
10422 new_regno
, INSN_UID (prev_insn
));
10424 /* Update first use of REGNO. */
10425 if (REGNO_FIRST_UID (regno
) == INSN_UID (prev_insn
))
10426 REGNO_FIRST_UID (regno
) = INSN_UID (insn
);
10428 /* Now perform copy propagation to hopefully
10429 remove all uses of REGNO within the loop. */
10430 try_copy_prop (loop
, replacement
, regno
);
10436 /* Worker function for find_mem_in_note, called via for_each_rtx. */
10439 find_mem_in_note_1 (x
, data
)
10443 if (*x
!= NULL_RTX
&& GET_CODE (*x
) == MEM
)
10445 rtx
*res
= (rtx
*) data
;
10452 /* Returns the first MEM found in NOTE by depth-first search. */
10455 find_mem_in_note (note
)
10458 if (note
&& for_each_rtx (¬e
, find_mem_in_note_1
, ¬e
))
10463 /* Replace MEM with its associated pseudo register. This function is
10464 called from load_mems via for_each_rtx. DATA is actually a pointer
10465 to a structure describing the instruction currently being scanned
10466 and the MEM we are currently replacing. */
10469 replace_loop_mem (mem
, data
)
10473 loop_replace_args
*args
= (loop_replace_args
*) data
;
10479 switch (GET_CODE (m
))
10485 /* We're not interested in the MEM associated with a
10486 CONST_DOUBLE, so there's no need to traverse into one. */
10490 /* This is not a MEM. */
10494 if (!rtx_equal_p (args
->match
, m
))
10495 /* This is not the MEM we are currently replacing. */
10498 /* Actually replace the MEM. */
10499 validate_change (args
->insn
, mem
, args
->replacement
, 1);
10505 replace_loop_mems (insn
, mem
, reg
, written
)
10511 loop_replace_args args
;
10515 args
.replacement
= reg
;
10517 for_each_rtx (&insn
, replace_loop_mem
, &args
);
10519 /* If we hoist a mem write out of the loop, then REG_EQUAL
10520 notes referring to the mem are no longer valid. */
10526 for (link
= ®_NOTES (insn
); (note
= *link
); link
= &XEXP (note
, 1))
10528 if (REG_NOTE_KIND (note
) == REG_EQUAL
10529 && (sub
= find_mem_in_note (note
))
10530 && true_dependence (mem
, VOIDmode
, sub
, rtx_varies_p
))
10532 /* Remove the note. */
10533 validate_change (NULL_RTX
, link
, XEXP (note
, 1), 1);
10540 /* Replace one register with another. Called through for_each_rtx; PX points
10541 to the rtx being scanned. DATA is actually a pointer to
10542 a structure of arguments. */
10545 replace_loop_reg (px
, data
)
10550 loop_replace_args
*args
= (loop_replace_args
*) data
;
10555 if (x
== args
->match
)
10556 validate_change (args
->insn
, px
, args
->replacement
, 1);
10562 replace_loop_regs (insn
, reg
, replacement
)
10567 loop_replace_args args
;
10571 args
.replacement
= replacement
;
10573 for_each_rtx (&insn
, replace_loop_reg
, &args
);
10576 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
10577 (ignored in the interim). */
10580 loop_insn_emit_after (loop
, where_bb
, where_insn
, pattern
)
10581 const struct loop
*loop ATTRIBUTE_UNUSED
;
10582 basic_block where_bb ATTRIBUTE_UNUSED
;
10586 return emit_insn_after (pattern
, where_insn
);
10590 /* If WHERE_INSN is nonzero emit insn for PATTERN before WHERE_INSN
10591 in basic block WHERE_BB (ignored in the interim) within the loop
10592 otherwise hoist PATTERN into the loop pre-header. */
10595 loop_insn_emit_before (loop
, where_bb
, where_insn
, pattern
)
10596 const struct loop
*loop
;
10597 basic_block where_bb ATTRIBUTE_UNUSED
;
10602 return loop_insn_hoist (loop
, pattern
);
10603 return emit_insn_before (pattern
, where_insn
);
10607 /* Emit call insn for PATTERN before WHERE_INSN in basic block
10608 WHERE_BB (ignored in the interim) within the loop. */
10611 loop_call_insn_emit_before (loop
, where_bb
, where_insn
, pattern
)
10612 const struct loop
*loop ATTRIBUTE_UNUSED
;
10613 basic_block where_bb ATTRIBUTE_UNUSED
;
10617 return emit_call_insn_before (pattern
, where_insn
);
10621 /* Hoist insn for PATTERN into the loop pre-header. */
10624 loop_insn_hoist (loop
, pattern
)
10625 const struct loop
*loop
;
10628 return loop_insn_emit_before (loop
, 0, loop
->start
, pattern
);
10632 /* Hoist call insn for PATTERN into the loop pre-header. */
10635 loop_call_insn_hoist (loop
, pattern
)
10636 const struct loop
*loop
;
10639 return loop_call_insn_emit_before (loop
, 0, loop
->start
, pattern
);
10643 /* Sink insn for PATTERN after the loop end. */
10646 loop_insn_sink (loop
, pattern
)
10647 const struct loop
*loop
;
10650 return loop_insn_emit_before (loop
, 0, loop
->sink
, pattern
);
10653 /* bl->final_value can be eighter general_operand or PLUS of general_operand
10654 and constant. Emit sequence of instructions to load it into REG. */
10656 gen_load_of_final_value (reg
, final_value
)
10657 rtx reg
, final_value
;
10661 final_value
= force_operand (final_value
, reg
);
10662 if (final_value
!= reg
)
10663 emit_move_insn (reg
, final_value
);
10664 seq
= get_insns ();
10669 /* If the loop has multiple exits, emit insn for PATTERN before the
10670 loop to ensure that it will always be executed no matter how the
10671 loop exits. Otherwise, emit the insn for PATTERN after the loop,
10672 since this is slightly more efficient. */
10675 loop_insn_sink_or_swim (loop
, pattern
)
10676 const struct loop
*loop
;
10679 if (loop
->exit_count
)
10680 return loop_insn_hoist (loop
, pattern
);
10682 return loop_insn_sink (loop
, pattern
);
10686 loop_ivs_dump (loop
, file
, verbose
)
10687 const struct loop
*loop
;
10691 struct iv_class
*bl
;
10694 if (! loop
|| ! file
)
10697 for (bl
= LOOP_IVS (loop
)->list
; bl
; bl
= bl
->next
)
10700 fprintf (file
, "Loop %d: %d IV classes\n", loop
->num
, iv_num
);
10702 for (bl
= LOOP_IVS (loop
)->list
; bl
; bl
= bl
->next
)
10704 loop_iv_class_dump (bl
, file
, verbose
);
10705 fputc ('\n', file
);
10711 loop_iv_class_dump (bl
, file
, verbose
)
10712 const struct iv_class
*bl
;
10714 int verbose ATTRIBUTE_UNUSED
;
10716 struct induction
*v
;
10720 if (! bl
|| ! file
)
10723 fprintf (file
, "IV class for reg %d, benefit %d\n",
10724 bl
->regno
, bl
->total_benefit
);
10726 fprintf (file
, " Init insn %d", INSN_UID (bl
->init_insn
));
10727 if (bl
->initial_value
)
10729 fprintf (file
, ", init val: ");
10730 print_simple_rtl (file
, bl
->initial_value
);
10732 if (bl
->initial_test
)
10734 fprintf (file
, ", init test: ");
10735 print_simple_rtl (file
, bl
->initial_test
);
10737 fputc ('\n', file
);
10739 if (bl
->final_value
)
10741 fprintf (file
, " Final val: ");
10742 print_simple_rtl (file
, bl
->final_value
);
10743 fputc ('\n', file
);
10746 if ((incr
= biv_total_increment (bl
)))
10748 fprintf (file
, " Total increment: ");
10749 print_simple_rtl (file
, incr
);
10750 fputc ('\n', file
);
10753 /* List the increments. */
10754 for (i
= 0, v
= bl
->biv
; v
; v
= v
->next_iv
, i
++)
10756 fprintf (file
, " Inc%d: insn %d, incr: ", i
, INSN_UID (v
->insn
));
10757 print_simple_rtl (file
, v
->add_val
);
10758 fputc ('\n', file
);
10761 /* List the givs. */
10762 for (i
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
, i
++)
10764 fprintf (file
, " Giv%d: insn %d, benefit %d, ",
10765 i
, INSN_UID (v
->insn
), v
->benefit
);
10766 if (v
->giv_type
== DEST_ADDR
)
10767 print_simple_rtl (file
, v
->mem
);
10769 print_simple_rtl (file
, single_set (v
->insn
));
10770 fputc ('\n', file
);
10776 loop_biv_dump (v
, file
, verbose
)
10777 const struct induction
*v
;
10786 REGNO (v
->dest_reg
), INSN_UID (v
->insn
));
10787 fprintf (file
, " const ");
10788 print_simple_rtl (file
, v
->add_val
);
10790 if (verbose
&& v
->final_value
)
10792 fputc ('\n', file
);
10793 fprintf (file
, " final ");
10794 print_simple_rtl (file
, v
->final_value
);
10797 fputc ('\n', file
);
10802 loop_giv_dump (v
, file
, verbose
)
10803 const struct induction
*v
;
10810 if (v
->giv_type
== DEST_REG
)
10811 fprintf (file
, "Giv %d: insn %d",
10812 REGNO (v
->dest_reg
), INSN_UID (v
->insn
));
10814 fprintf (file
, "Dest address: insn %d",
10815 INSN_UID (v
->insn
));
10817 fprintf (file
, " src reg %d benefit %d",
10818 REGNO (v
->src_reg
), v
->benefit
);
10819 fprintf (file
, " lifetime %d",
10822 if (v
->replaceable
)
10823 fprintf (file
, " replaceable");
10825 if (v
->no_const_addval
)
10826 fprintf (file
, " ncav");
10828 if (v
->ext_dependent
)
10830 switch (GET_CODE (v
->ext_dependent
))
10833 fprintf (file
, " ext se");
10836 fprintf (file
, " ext ze");
10839 fprintf (file
, " ext tr");
10846 fputc ('\n', file
);
10847 fprintf (file
, " mult ");
10848 print_simple_rtl (file
, v
->mult_val
);
10850 fputc ('\n', file
);
10851 fprintf (file
, " add ");
10852 print_simple_rtl (file
, v
->add_val
);
10854 if (verbose
&& v
->final_value
)
10856 fputc ('\n', file
);
10857 fprintf (file
, " final ");
10858 print_simple_rtl (file
, v
->final_value
);
10861 fputc ('\n', file
);
10867 const struct loop
*loop
;
10869 loop_ivs_dump (loop
, stderr
, 1);
10874 debug_iv_class (bl
)
10875 const struct iv_class
*bl
;
10877 loop_iv_class_dump (bl
, stderr
, 1);
10883 const struct induction
*v
;
10885 loop_biv_dump (v
, stderr
, 1);
10891 const struct induction
*v
;
10893 loop_giv_dump (v
, stderr
, 1);
10897 #define LOOP_BLOCK_NUM_1(INSN) \
10898 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
10900 /* The notes do not have an assigned block, so look at the next insn. */
10901 #define LOOP_BLOCK_NUM(INSN) \
10902 ((INSN) ? (GET_CODE (INSN) == NOTE \
10903 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
10904 : LOOP_BLOCK_NUM_1 (INSN)) \
10907 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
10910 loop_dump_aux (loop
, file
, verbose
)
10911 const struct loop
*loop
;
10913 int verbose ATTRIBUTE_UNUSED
;
10917 if (! loop
|| ! file
)
10920 /* Print diagnostics to compare our concept of a loop with
10921 what the loop notes say. */
10922 if (! PREV_INSN (loop
->first
->head
)
10923 || GET_CODE (PREV_INSN (loop
->first
->head
)) != NOTE
10924 || NOTE_LINE_NUMBER (PREV_INSN (loop
->first
->head
))
10925 != NOTE_INSN_LOOP_BEG
)
10926 fprintf (file
, ";; No NOTE_INSN_LOOP_BEG at %d\n",
10927 INSN_UID (PREV_INSN (loop
->first
->head
)));
10928 if (! NEXT_INSN (loop
->last
->end
)
10929 || GET_CODE (NEXT_INSN (loop
->last
->end
)) != NOTE
10930 || NOTE_LINE_NUMBER (NEXT_INSN (loop
->last
->end
))
10931 != NOTE_INSN_LOOP_END
)
10932 fprintf (file
, ";; No NOTE_INSN_LOOP_END at %d\n",
10933 INSN_UID (NEXT_INSN (loop
->last
->end
)));
10938 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
10939 LOOP_BLOCK_NUM (loop
->start
),
10940 LOOP_INSN_UID (loop
->start
),
10941 LOOP_BLOCK_NUM (loop
->cont
),
10942 LOOP_INSN_UID (loop
->cont
),
10943 LOOP_BLOCK_NUM (loop
->cont
),
10944 LOOP_INSN_UID (loop
->cont
),
10945 LOOP_BLOCK_NUM (loop
->vtop
),
10946 LOOP_INSN_UID (loop
->vtop
),
10947 LOOP_BLOCK_NUM (loop
->end
),
10948 LOOP_INSN_UID (loop
->end
));
10949 fprintf (file
, ";; top %d (%d), scan start %d (%d)\n",
10950 LOOP_BLOCK_NUM (loop
->top
),
10951 LOOP_INSN_UID (loop
->top
),
10952 LOOP_BLOCK_NUM (loop
->scan_start
),
10953 LOOP_INSN_UID (loop
->scan_start
));
10954 fprintf (file
, ";; exit_count %d", loop
->exit_count
);
10955 if (loop
->exit_count
)
10957 fputs (", labels:", file
);
10958 for (label
= loop
->exit_labels
; label
; label
= LABEL_NEXTREF (label
))
10960 fprintf (file
, " %d ",
10961 LOOP_INSN_UID (XEXP (label
, 0)));
10964 fputs ("\n", file
);
10966 /* This can happen when a marked loop appears as two nested loops,
10967 say from while (a || b) {}. The inner loop won't match
10968 the loop markers but the outer one will. */
10969 if (LOOP_BLOCK_NUM (loop
->cont
) != loop
->latch
->index
)
10970 fprintf (file
, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
10974 /* Call this function from the debugger to dump LOOP. */
10978 const struct loop
*loop
;
10980 flow_loop_dump (loop
, stderr
, loop_dump_aux
, 1);
10983 /* Call this function from the debugger to dump LOOPS. */
10986 debug_loops (loops
)
10987 const struct loops
*loops
;
10989 flow_loops_dump (loops
, stderr
, loop_dump_aux
, 1);