1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This is a simple analysis of induction variables of the loop. The major use
21 is for determining the number of iterations of a loop for loop unrolling,
22 doloop optimization and branch prediction. The iv information is computed
25 Induction variables are analyzed by walking the use-def chains. When
26 a basic induction variable (biv) is found, it is cached in the bivs
27 hash table. When register is proved to be a biv, its description
28 is stored to DF_REF_DATA of the def reference.
30 The analysis works always with one loop -- you must call
31 iv_analysis_loop_init (loop) for it. All the other functions then work with
32 this loop. When you need to work with another loop, just call
33 iv_analysis_loop_init for it. When you no longer need iv analysis, call
34 iv_analysis_done () to clean up the memory.
36 The available functions are:
38 iv_analyze (insn, reg, iv): Stores the description of the induction variable
39 corresponding to the use of register REG in INSN to IV. Returns true if
40 REG is an induction variable in INSN. false otherwise.
41 If use of REG is not found in INSN, following insns are scanned (so that
42 we may call this function on insn returned by get_condition).
43 iv_analyze_result (insn, def, iv): Stores to IV the description of the iv
44 corresponding to DEF, which is a register defined in INSN.
45 iv_analyze_expr (insn, rhs, mode, iv): Stores to IV the description of iv
46 corresponding to expression EXPR evaluated at INSN. All registers used bu
47 EXPR must also be used in INSN.
52 #include "coretypes.h"
55 #include "hard-reg-set.h"
60 #include "dominance.h"
62 #include "basic-block.h"
68 #include "insn-config.h"
78 #include "diagnostic-core.h"
83 /* Possible return values of iv_get_reaching_def. */
87 /* More than one reaching def, or reaching def that does not
91 /* The use is trivial invariant of the loop, i.e. is not changed
95 /* The use is reached by initial value and a value from the
96 previous iteration. */
99 /* The use has single dominating def. */
103 /* Information about a biv. */
107 unsigned regno
; /* The register of the biv. */
108 struct rtx_iv iv
; /* Value of the biv. */
111 static bool clean_slate
= true;
113 static unsigned int iv_ref_table_size
= 0;
115 /* Table of rtx_ivs indexed by the df_ref uid field. */
116 static struct rtx_iv
** iv_ref_table
;
118 /* Induction variable stored at the reference. */
119 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
120 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
122 /* The current loop. */
124 static struct loop
*current_loop
;
126 /* Hashtable helper. */
128 struct biv_entry_hasher
: typed_free_remove
<biv_entry
>
130 typedef biv_entry
*value_type
;
131 typedef rtx_def
*compare_type
;
132 static inline hashval_t
hash (const biv_entry
*);
133 static inline bool equal (const biv_entry
*, const rtx_def
*);
136 /* Returns hash value for biv B. */
139 biv_entry_hasher::hash (const biv_entry
*b
)
144 /* Compares biv B and register R. */
147 biv_entry_hasher::equal (const biv_entry
*b
, const rtx_def
*r
)
149 return b
->regno
== REGNO (r
);
152 /* Bivs of the current loop. */
154 static hash_table
<biv_entry_hasher
> *bivs
;
156 static bool iv_analyze_op (rtx_insn
*, rtx
, struct rtx_iv
*);
158 /* Return the RTX code corresponding to the IV extend code EXTEND. */
159 static inline enum rtx_code
160 iv_extend_to_rtx_code (enum iv_extend_code extend
)
168 case IV_UNKNOWN_EXTEND
:
174 /* Dumps information about IV to FILE. */
176 extern void dump_iv_info (FILE *, struct rtx_iv
*);
178 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
182 fprintf (file
, "not simple");
186 if (iv
->step
== const0_rtx
187 && !iv
->first_special
)
188 fprintf (file
, "invariant ");
190 print_rtl (file
, iv
->base
);
191 if (iv
->step
!= const0_rtx
)
193 fprintf (file
, " + ");
194 print_rtl (file
, iv
->step
);
195 fprintf (file
, " * iteration");
197 fprintf (file
, " (in %s)", GET_MODE_NAME (iv
->mode
));
199 if (iv
->mode
!= iv
->extend_mode
)
200 fprintf (file
, " %s to %s",
201 rtx_name
[iv_extend_to_rtx_code (iv
->extend
)],
202 GET_MODE_NAME (iv
->extend_mode
));
204 if (iv
->mult
!= const1_rtx
)
206 fprintf (file
, " * ");
207 print_rtl (file
, iv
->mult
);
209 if (iv
->delta
!= const0_rtx
)
211 fprintf (file
, " + ");
212 print_rtl (file
, iv
->delta
);
214 if (iv
->first_special
)
215 fprintf (file
, " (first special)");
218 /* Generates a subreg to get the least significant part of EXPR (in mode
219 INNER_MODE) to OUTER_MODE. */
222 lowpart_subreg (machine_mode outer_mode
, rtx expr
,
223 machine_mode inner_mode
)
225 return simplify_gen_subreg (outer_mode
, expr
, inner_mode
,
226 subreg_lowpart_offset (outer_mode
, inner_mode
));
230 check_iv_ref_table_size (void)
232 if (iv_ref_table_size
< DF_DEFS_TABLE_SIZE ())
234 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
235 iv_ref_table
= XRESIZEVEC (struct rtx_iv
*, iv_ref_table
, new_size
);
236 memset (&iv_ref_table
[iv_ref_table_size
], 0,
237 (new_size
- iv_ref_table_size
) * sizeof (struct rtx_iv
*));
238 iv_ref_table_size
= new_size
;
243 /* Checks whether REG is a well-behaved register. */
246 simple_reg_p (rtx reg
)
250 if (GET_CODE (reg
) == SUBREG
)
252 if (!subreg_lowpart_p (reg
))
254 reg
= SUBREG_REG (reg
);
261 if (HARD_REGISTER_NUM_P (r
))
264 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
270 /* Clears the information about ivs stored in df. */
275 unsigned i
, n_defs
= DF_DEFS_TABLE_SIZE ();
278 check_iv_ref_table_size ();
279 for (i
= 0; i
< n_defs
; i
++)
281 iv
= iv_ref_table
[i
];
285 iv_ref_table
[i
] = NULL
;
293 /* Prepare the data for an induction variable analysis of a LOOP. */
296 iv_analysis_loop_init (struct loop
*loop
)
300 /* Clear the information from the analysis of the previous loop. */
303 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
304 bivs
= new hash_table
<biv_entry_hasher
> (10);
310 /* Get rid of the ud chains before processing the rescans. Then add
312 df_remove_problem (df_chain
);
313 df_process_deferred_rescans ();
314 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
315 df_chain_add_problem (DF_UD_CHAIN
);
316 df_note_add_problem ();
317 df_analyze_loop (loop
);
319 df_dump_region (dump_file
);
321 check_iv_ref_table_size ();
324 /* Finds the definition of REG that dominates loop latch and stores
325 it to DEF. Returns false if there is not a single definition
326 dominating the latch. If REG has no definition in loop, DEF
327 is set to NULL and true is returned. */
330 latch_dominating_def (rtx reg
, df_ref
*def
)
332 df_ref single_rd
= NULL
, adef
;
333 unsigned regno
= REGNO (reg
);
334 struct df_rd_bb_info
*bb_info
= DF_RD_BB_INFO (current_loop
->latch
);
336 for (adef
= DF_REG_DEF_CHAIN (regno
); adef
; adef
= DF_REF_NEXT_REG (adef
))
338 if (!bitmap_bit_p (df
->blocks_to_analyze
, DF_REF_BBNO (adef
))
339 || !bitmap_bit_p (&bb_info
->out
, DF_REF_ID (adef
)))
342 /* More than one reaching definition. */
346 if (!just_once_each_iteration_p (current_loop
, DF_REF_BB (adef
)))
356 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
358 static enum iv_grd_result
359 iv_get_reaching_def (rtx_insn
*insn
, rtx reg
, df_ref
*def
)
362 basic_block def_bb
, use_bb
;
367 if (!simple_reg_p (reg
))
369 if (GET_CODE (reg
) == SUBREG
)
370 reg
= SUBREG_REG (reg
);
371 gcc_assert (REG_P (reg
));
373 use
= df_find_use (insn
, reg
);
374 gcc_assert (use
!= NULL
);
376 if (!DF_REF_CHAIN (use
))
377 return GRD_INVARIANT
;
379 /* More than one reaching def. */
380 if (DF_REF_CHAIN (use
)->next
)
383 adef
= DF_REF_CHAIN (use
)->ref
;
385 /* We do not handle setting only part of the register. */
386 if (DF_REF_FLAGS (adef
) & DF_REF_READ_WRITE
)
389 def_insn
= DF_REF_INSN (adef
);
390 def_bb
= DF_REF_BB (adef
);
391 use_bb
= BLOCK_FOR_INSN (insn
);
393 if (use_bb
== def_bb
)
394 dom_p
= (DF_INSN_LUID (def_insn
) < DF_INSN_LUID (insn
));
396 dom_p
= dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
);
401 return GRD_SINGLE_DOM
;
404 /* The definition does not dominate the use. This is still OK if
405 this may be a use of a biv, i.e. if the def_bb dominates loop
407 if (just_once_each_iteration_p (current_loop
, def_bb
))
408 return GRD_MAYBE_BIV
;
413 /* Sets IV to invariant CST in MODE. Always returns true (just for
414 consistency with other iv manipulation functions that may fail). */
417 iv_constant (struct rtx_iv
*iv
, rtx cst
, machine_mode mode
)
419 if (mode
== VOIDmode
)
420 mode
= GET_MODE (cst
);
424 iv
->step
= const0_rtx
;
425 iv
->first_special
= false;
426 iv
->extend
= IV_UNKNOWN_EXTEND
;
427 iv
->extend_mode
= iv
->mode
;
428 iv
->delta
= const0_rtx
;
429 iv
->mult
= const1_rtx
;
434 /* Evaluates application of subreg to MODE on IV. */
437 iv_subreg (struct rtx_iv
*iv
, machine_mode mode
)
439 /* If iv is invariant, just calculate the new value. */
440 if (iv
->step
== const0_rtx
441 && !iv
->first_special
)
443 rtx val
= get_iv_value (iv
, const0_rtx
);
444 val
= lowpart_subreg (mode
, val
,
445 iv
->extend
== IV_UNKNOWN_EXTEND
446 ? iv
->mode
: iv
->extend_mode
);
449 iv
->extend
= IV_UNKNOWN_EXTEND
;
450 iv
->mode
= iv
->extend_mode
= mode
;
451 iv
->delta
= const0_rtx
;
452 iv
->mult
= const1_rtx
;
456 if (iv
->extend_mode
== mode
)
459 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
462 iv
->extend
= IV_UNKNOWN_EXTEND
;
465 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
466 simplify_gen_binary (MULT
, iv
->extend_mode
,
467 iv
->base
, iv
->mult
));
468 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
469 iv
->mult
= const1_rtx
;
470 iv
->delta
= const0_rtx
;
471 iv
->first_special
= false;
476 /* Evaluates application of EXTEND to MODE on IV. */
479 iv_extend (struct rtx_iv
*iv
, enum iv_extend_code extend
, machine_mode mode
)
481 /* If iv is invariant, just calculate the new value. */
482 if (iv
->step
== const0_rtx
483 && !iv
->first_special
)
485 rtx val
= get_iv_value (iv
, const0_rtx
);
486 if (iv
->extend_mode
!= iv
->mode
487 && iv
->extend
!= IV_UNKNOWN_EXTEND
488 && iv
->extend
!= extend
)
489 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
490 val
= simplify_gen_unary (iv_extend_to_rtx_code (extend
), mode
,
493 ? iv
->extend_mode
: iv
->mode
);
495 iv
->extend
= IV_UNKNOWN_EXTEND
;
496 iv
->mode
= iv
->extend_mode
= mode
;
497 iv
->delta
= const0_rtx
;
498 iv
->mult
= const1_rtx
;
502 if (mode
!= iv
->extend_mode
)
505 if (iv
->extend
!= IV_UNKNOWN_EXTEND
506 && iv
->extend
!= extend
)
514 /* Evaluates negation of IV. */
517 iv_neg (struct rtx_iv
*iv
)
519 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
521 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
522 iv
->base
, iv
->extend_mode
);
523 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
524 iv
->step
, iv
->extend_mode
);
528 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
529 iv
->delta
, iv
->extend_mode
);
530 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
531 iv
->mult
, iv
->extend_mode
);
537 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
540 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
545 /* Extend the constant to extend_mode of the other operand if necessary. */
546 if (iv0
->extend
== IV_UNKNOWN_EXTEND
547 && iv0
->mode
== iv0
->extend_mode
548 && iv0
->step
== const0_rtx
549 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
551 iv0
->extend_mode
= iv1
->extend_mode
;
552 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
553 iv0
->base
, iv0
->mode
);
555 if (iv1
->extend
== IV_UNKNOWN_EXTEND
556 && iv1
->mode
== iv1
->extend_mode
557 && iv1
->step
== const0_rtx
558 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
560 iv1
->extend_mode
= iv0
->extend_mode
;
561 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
562 iv1
->base
, iv1
->mode
);
565 mode
= iv0
->extend_mode
;
566 if (mode
!= iv1
->extend_mode
)
569 if (iv0
->extend
== IV_UNKNOWN_EXTEND
570 && iv1
->extend
== IV_UNKNOWN_EXTEND
)
572 if (iv0
->mode
!= iv1
->mode
)
575 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
576 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
581 /* Handle addition of constant. */
582 if (iv1
->extend
== IV_UNKNOWN_EXTEND
584 && iv1
->step
== const0_rtx
)
586 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
590 if (iv0
->extend
== IV_UNKNOWN_EXTEND
592 && iv0
->step
== const0_rtx
)
600 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
607 /* Evaluates multiplication of IV by constant CST. */
610 iv_mult (struct rtx_iv
*iv
, rtx mby
)
612 machine_mode mode
= iv
->extend_mode
;
614 if (GET_MODE (mby
) != VOIDmode
615 && GET_MODE (mby
) != mode
)
618 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
620 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
621 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
625 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
626 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
632 /* Evaluates shift of IV by constant CST. */
635 iv_shift (struct rtx_iv
*iv
, rtx mby
)
637 machine_mode mode
= iv
->extend_mode
;
639 if (GET_MODE (mby
) != VOIDmode
640 && GET_MODE (mby
) != mode
)
643 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
645 iv
->base
= simplify_gen_binary (ASHIFT
, mode
, iv
->base
, mby
);
646 iv
->step
= simplify_gen_binary (ASHIFT
, mode
, iv
->step
, mby
);
650 iv
->delta
= simplify_gen_binary (ASHIFT
, mode
, iv
->delta
, mby
);
651 iv
->mult
= simplify_gen_binary (ASHIFT
, mode
, iv
->mult
, mby
);
657 /* The recursive part of get_biv_step. Gets the value of the single value
658 defined by DEF wrto initial value of REG inside loop, in shape described
662 get_biv_step_1 (df_ref def
, rtx reg
,
663 rtx
*inner_step
, machine_mode
*inner_mode
,
664 enum iv_extend_code
*extend
, machine_mode outer_mode
,
667 rtx set
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
668 rtx next
, nextr
, tmp
;
670 rtx_insn
*insn
= DF_REF_INSN (def
);
672 enum iv_grd_result res
;
674 set
= single_set (insn
);
678 rhs
= find_reg_equal_equiv_note (insn
);
684 code
= GET_CODE (rhs
);
697 if (code
== PLUS
&& CONSTANT_P (op0
))
699 tmp
= op0
; op0
= op1
; op1
= tmp
;
702 if (!simple_reg_p (op0
)
703 || !CONSTANT_P (op1
))
706 if (GET_MODE (rhs
) != outer_mode
)
708 /* ppc64 uses expressions like
710 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
712 this is equivalent to
714 (set x':DI (plus:DI y:DI 1))
715 (set x:SI (subreg:SI (x':DI)). */
716 if (GET_CODE (op0
) != SUBREG
)
718 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
727 if (GET_MODE (rhs
) != outer_mode
)
731 if (!simple_reg_p (op0
))
741 if (GET_CODE (next
) == SUBREG
)
743 if (!subreg_lowpart_p (next
))
746 nextr
= SUBREG_REG (next
);
747 if (GET_MODE (nextr
) != outer_mode
)
753 res
= iv_get_reaching_def (insn
, nextr
, &next_def
);
755 if (res
== GRD_INVALID
|| res
== GRD_INVARIANT
)
758 if (res
== GRD_MAYBE_BIV
)
760 if (!rtx_equal_p (nextr
, reg
))
763 *inner_step
= const0_rtx
;
764 *extend
= IV_UNKNOWN_EXTEND
;
765 *inner_mode
= outer_mode
;
766 *outer_step
= const0_rtx
;
768 else if (!get_biv_step_1 (next_def
, reg
,
769 inner_step
, inner_mode
, extend
, outer_mode
,
773 if (GET_CODE (next
) == SUBREG
)
775 machine_mode amode
= GET_MODE (next
);
777 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
781 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
782 *inner_step
, *outer_step
);
783 *outer_step
= const0_rtx
;
784 *extend
= IV_UNKNOWN_EXTEND
;
795 if (*inner_mode
== outer_mode
796 /* See comment in previous switch. */
797 || GET_MODE (rhs
) != outer_mode
)
798 *inner_step
= simplify_gen_binary (code
, outer_mode
,
801 *outer_step
= simplify_gen_binary (code
, outer_mode
,
807 gcc_assert (GET_MODE (op0
) == *inner_mode
808 && *extend
== IV_UNKNOWN_EXTEND
809 && *outer_step
== const0_rtx
);
811 *extend
= (code
== SIGN_EXTEND
) ? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
821 /* Gets the operation on register REG inside loop, in shape
823 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
825 If the operation cannot be described in this shape, return false.
826 LAST_DEF is the definition of REG that dominates loop latch. */
829 get_biv_step (df_ref last_def
, rtx reg
, rtx
*inner_step
,
830 machine_mode
*inner_mode
, enum iv_extend_code
*extend
,
831 machine_mode
*outer_mode
, rtx
*outer_step
)
833 *outer_mode
= GET_MODE (reg
);
835 if (!get_biv_step_1 (last_def
, reg
,
836 inner_step
, inner_mode
, extend
, *outer_mode
,
840 gcc_assert ((*inner_mode
== *outer_mode
) != (*extend
!= IV_UNKNOWN_EXTEND
));
841 gcc_assert (*inner_mode
!= *outer_mode
|| *outer_step
== const0_rtx
);
846 /* Records information that DEF is induction variable IV. */
849 record_iv (df_ref def
, struct rtx_iv
*iv
)
851 struct rtx_iv
*recorded_iv
= XNEW (struct rtx_iv
);
854 check_iv_ref_table_size ();
855 DF_REF_IV_SET (def
, recorded_iv
);
858 /* If DEF was already analyzed for bivness, store the description of the biv to
859 IV and return true. Otherwise return false. */
862 analyzed_for_bivness_p (rtx def
, struct rtx_iv
*iv
)
864 struct biv_entry
*biv
= bivs
->find_with_hash (def
, REGNO (def
));
874 record_biv (rtx def
, struct rtx_iv
*iv
)
876 struct biv_entry
*biv
= XNEW (struct biv_entry
);
877 biv_entry
**slot
= bivs
->find_slot_with_hash (def
, REGNO (def
), INSERT
);
879 biv
->regno
= REGNO (def
);
885 /* Determines whether DEF is a biv and if so, stores its description
889 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
891 rtx inner_step
, outer_step
;
892 machine_mode inner_mode
, outer_mode
;
893 enum iv_extend_code extend
;
898 fprintf (dump_file
, "Analyzing ");
899 print_rtl (dump_file
, def
);
900 fprintf (dump_file
, " for bivness.\n");
905 if (!CONSTANT_P (def
))
908 return iv_constant (iv
, def
, VOIDmode
);
911 if (!latch_dominating_def (def
, &last_def
))
914 fprintf (dump_file
, " not simple.\n");
919 return iv_constant (iv
, def
, VOIDmode
);
921 if (analyzed_for_bivness_p (def
, iv
))
924 fprintf (dump_file
, " already analysed.\n");
925 return iv
->base
!= NULL_RTX
;
928 if (!get_biv_step (last_def
, def
, &inner_step
, &inner_mode
, &extend
,
929 &outer_mode
, &outer_step
))
935 /* Loop transforms base to es (base + inner_step) + outer_step,
936 where es means extend of subreg between inner_mode and outer_mode.
937 The corresponding induction variable is
939 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
941 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
942 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
943 iv
->mode
= inner_mode
;
944 iv
->extend_mode
= outer_mode
;
946 iv
->mult
= const1_rtx
;
947 iv
->delta
= outer_step
;
948 iv
->first_special
= inner_mode
!= outer_mode
;
953 fprintf (dump_file
, " ");
954 dump_iv_info (dump_file
, iv
);
955 fprintf (dump_file
, "\n");
958 record_biv (def
, iv
);
959 return iv
->base
!= NULL_RTX
;
962 /* Analyzes expression RHS used at INSN and stores the result to *IV.
963 The mode of the induction variable is MODE. */
966 iv_analyze_expr (rtx_insn
*insn
, rtx rhs
, machine_mode mode
,
970 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
971 struct rtx_iv iv0
, iv1
;
972 enum rtx_code code
= GET_CODE (rhs
);
973 machine_mode omode
= mode
;
979 gcc_assert (GET_MODE (rhs
) == mode
|| GET_MODE (rhs
) == VOIDmode
);
985 if (!iv_analyze_op (insn
, rhs
, iv
))
988 if (iv
->mode
== VOIDmode
)
991 iv
->extend_mode
= mode
;
1006 op0
= XEXP (rhs
, 0);
1007 omode
= GET_MODE (op0
);
1012 op0
= XEXP (rhs
, 0);
1013 op1
= XEXP (rhs
, 1);
1017 op0
= XEXP (rhs
, 0);
1018 mby
= XEXP (rhs
, 1);
1019 if (!CONSTANT_P (mby
))
1020 std::swap (op0
, mby
);
1021 if (!CONSTANT_P (mby
))
1026 op0
= XEXP (rhs
, 0);
1027 mby
= XEXP (rhs
, 1);
1028 if (!CONSTANT_P (mby
))
1037 && !iv_analyze_expr (insn
, op0
, omode
, &iv0
))
1041 && !iv_analyze_expr (insn
, op1
, omode
, &iv1
))
1047 if (!iv_extend (&iv0
, IV_SIGN_EXTEND
, mode
))
1052 if (!iv_extend (&iv0
, IV_ZERO_EXTEND
, mode
))
1063 if (!iv_add (&iv0
, &iv1
, code
))
1068 if (!iv_mult (&iv0
, mby
))
1073 if (!iv_shift (&iv0
, mby
))
1082 return iv
->base
!= NULL_RTX
;
1085 /* Analyzes iv DEF and stores the result to *IV. */
1088 iv_analyze_def (df_ref def
, struct rtx_iv
*iv
)
1090 rtx_insn
*insn
= DF_REF_INSN (def
);
1091 rtx reg
= DF_REF_REG (def
);
1096 fprintf (dump_file
, "Analyzing def of ");
1097 print_rtl (dump_file
, reg
);
1098 fprintf (dump_file
, " in insn ");
1099 print_rtl_single (dump_file
, insn
);
1102 check_iv_ref_table_size ();
1103 if (DF_REF_IV (def
))
1106 fprintf (dump_file
, " already analysed.\n");
1107 *iv
= *DF_REF_IV (def
);
1108 return iv
->base
!= NULL_RTX
;
1111 iv
->mode
= VOIDmode
;
1112 iv
->base
= NULL_RTX
;
1113 iv
->step
= NULL_RTX
;
1118 set
= single_set (insn
);
1122 if (!REG_P (SET_DEST (set
)))
1125 gcc_assert (SET_DEST (set
) == reg
);
1126 rhs
= find_reg_equal_equiv_note (insn
);
1128 rhs
= XEXP (rhs
, 0);
1130 rhs
= SET_SRC (set
);
1132 iv_analyze_expr (insn
, rhs
, GET_MODE (reg
), iv
);
1133 record_iv (def
, iv
);
1137 print_rtl (dump_file
, reg
);
1138 fprintf (dump_file
, " in insn ");
1139 print_rtl_single (dump_file
, insn
);
1140 fprintf (dump_file
, " is ");
1141 dump_iv_info (dump_file
, iv
);
1142 fprintf (dump_file
, "\n");
1145 return iv
->base
!= NULL_RTX
;
1148 /* Analyzes operand OP of INSN and stores the result to *IV. */
1151 iv_analyze_op (rtx_insn
*insn
, rtx op
, struct rtx_iv
*iv
)
1154 enum iv_grd_result res
;
1158 fprintf (dump_file
, "Analyzing operand ");
1159 print_rtl (dump_file
, op
);
1160 fprintf (dump_file
, " of insn ");
1161 print_rtl_single (dump_file
, insn
);
1164 if (function_invariant_p (op
))
1165 res
= GRD_INVARIANT
;
1166 else if (GET_CODE (op
) == SUBREG
)
1168 if (!subreg_lowpart_p (op
))
1171 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
1174 return iv_subreg (iv
, GET_MODE (op
));
1178 res
= iv_get_reaching_def (insn
, op
, &def
);
1179 if (res
== GRD_INVALID
)
1182 fprintf (dump_file
, " not simple.\n");
1187 if (res
== GRD_INVARIANT
)
1189 iv_constant (iv
, op
, VOIDmode
);
1193 fprintf (dump_file
, " ");
1194 dump_iv_info (dump_file
, iv
);
1195 fprintf (dump_file
, "\n");
1200 if (res
== GRD_MAYBE_BIV
)
1201 return iv_analyze_biv (op
, iv
);
1203 return iv_analyze_def (def
, iv
);
1206 /* Analyzes value VAL at INSN and stores the result to *IV. */
1209 iv_analyze (rtx_insn
*insn
, rtx val
, struct rtx_iv
*iv
)
1213 /* We must find the insn in that val is used, so that we get to UD chains.
1214 Since the function is sometimes called on result of get_condition,
1215 this does not necessarily have to be directly INSN; scan also the
1217 if (simple_reg_p (val
))
1219 if (GET_CODE (val
) == SUBREG
)
1220 reg
= SUBREG_REG (val
);
1224 while (!df_find_use (insn
, reg
))
1225 insn
= NEXT_INSN (insn
);
1228 return iv_analyze_op (insn
, val
, iv
);
1231 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1234 iv_analyze_result (rtx_insn
*insn
, rtx def
, struct rtx_iv
*iv
)
1238 adef
= df_find_def (insn
, def
);
1242 return iv_analyze_def (adef
, iv
);
1245 /* Checks whether definition of register REG in INSN is a basic induction
1246 variable. IV analysis must have been initialized (via a call to
1247 iv_analysis_loop_init) for this function to produce a result. */
1250 biv_p (rtx_insn
*insn
, rtx reg
)
1253 df_ref def
, last_def
;
1255 if (!simple_reg_p (reg
))
1258 def
= df_find_def (insn
, reg
);
1259 gcc_assert (def
!= NULL
);
1260 if (!latch_dominating_def (reg
, &last_def
))
1262 if (last_def
!= def
)
1265 if (!iv_analyze_biv (reg
, &iv
))
1268 return iv
.step
!= const0_rtx
;
1271 /* Calculates value of IV at ITERATION-th iteration. */
1274 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1278 /* We would need to generate some if_then_else patterns, and so far
1279 it is not needed anywhere. */
1280 gcc_assert (!iv
->first_special
);
1282 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1283 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1284 simplify_gen_binary (MULT
, iv
->extend_mode
,
1285 iv
->step
, iteration
));
1289 if (iv
->extend_mode
== iv
->mode
)
1292 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1294 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
1297 val
= simplify_gen_unary (iv_extend_to_rtx_code (iv
->extend
),
1298 iv
->extend_mode
, val
, iv
->mode
);
1299 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1300 simplify_gen_binary (MULT
, iv
->extend_mode
,
1306 /* Free the data for an induction variable analysis. */
1309 iv_analysis_done (void)
1315 df_finish_pass (true);
1318 free (iv_ref_table
);
1319 iv_ref_table
= NULL
;
1320 iv_ref_table_size
= 0;
1324 /* Computes inverse to X modulo (1 << MOD). */
1327 inverse (uint64_t x
, int mod
)
1330 ((uint64_t) 1 << (mod
- 1) << 1) - 1;
1334 for (i
= 0; i
< mod
- 1; i
++)
1336 rslt
= (rslt
* x
) & mask
;
1343 /* Checks whether any register in X is in set ALT. */
1346 altered_reg_used (const_rtx x
, bitmap alt
)
1348 subrtx_iterator::array_type array
;
1349 FOR_EACH_SUBRTX (iter
, array
, x
, NONCONST
)
1351 const_rtx x
= *iter
;
1352 if (REG_P (x
) && REGNO_REG_SET_P (alt
, REGNO (x
)))
1358 /* Marks registers altered by EXPR in set ALT. */
1361 mark_altered (rtx expr
, const_rtx by ATTRIBUTE_UNUSED
, void *alt
)
1363 if (GET_CODE (expr
) == SUBREG
)
1364 expr
= SUBREG_REG (expr
);
1368 SET_REGNO_REG_SET ((bitmap
) alt
, REGNO (expr
));
1371 /* Checks whether RHS is simple enough to process. */
1374 simple_rhs_p (rtx rhs
)
1378 if (function_invariant_p (rhs
)
1379 || (REG_P (rhs
) && !HARD_REGISTER_P (rhs
)))
1382 switch (GET_CODE (rhs
))
1387 op0
= XEXP (rhs
, 0);
1388 op1
= XEXP (rhs
, 1);
1389 /* Allow reg OP const and reg OP reg. */
1390 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
))
1391 && !function_invariant_p (op0
))
1393 if (!(REG_P (op1
) && !HARD_REGISTER_P (op1
))
1394 && !function_invariant_p (op1
))
1403 op0
= XEXP (rhs
, 0);
1404 op1
= XEXP (rhs
, 1);
1405 /* Allow reg OP const. */
1406 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
)))
1408 if (!function_invariant_p (op1
))
1418 /* If REGNO has a single definition, return its known value, otherwise return
1422 find_single_def_src (unsigned int regno
)
1430 adef
= DF_REG_DEF_CHAIN (regno
);
1431 if (adef
== NULL
|| DF_REF_NEXT_REG (adef
) != NULL
1432 || DF_REF_IS_ARTIFICIAL (adef
))
1435 set
= single_set (DF_REF_INSN (adef
));
1436 if (set
== NULL
|| !REG_P (SET_DEST (set
))
1437 || REGNO (SET_DEST (set
)) != regno
)
1440 note
= find_reg_equal_equiv_note (DF_REF_INSN (adef
));
1442 if (note
&& function_invariant_p (XEXP (note
, 0)))
1444 src
= XEXP (note
, 0);
1447 src
= SET_SRC (set
);
1451 regno
= REGNO (src
);
1456 if (!function_invariant_p (src
))
1462 /* If any registers in *EXPR that have a single definition, try to replace
1463 them with the known-equivalent values. */
1466 replace_single_def_regs (rtx
*expr
)
1468 subrtx_var_iterator::array_type array
;
1470 FOR_EACH_SUBRTX_VAR (iter
, array
, *expr
, NONCONST
)
1474 if (rtx new_x
= find_single_def_src (REGNO (x
)))
1476 *expr
= simplify_replace_rtx (*expr
, x
, new_x
);
1482 /* A subroutine of simplify_using_initial_values, this function examines INSN
1483 to see if it contains a suitable set that we can use to make a replacement.
1484 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1485 the set; return false otherwise. */
1488 suitable_set_for_replacement (rtx_insn
*insn
, rtx
*dest
, rtx
*src
)
1490 rtx set
= single_set (insn
);
1491 rtx lhs
= NULL_RTX
, rhs
;
1496 lhs
= SET_DEST (set
);
1500 rhs
= find_reg_equal_equiv_note (insn
);
1502 rhs
= XEXP (rhs
, 0);
1504 rhs
= SET_SRC (set
);
1506 if (!simple_rhs_p (rhs
))
1514 /* Using the data returned by suitable_set_for_replacement, replace DEST
1515 with SRC in *EXPR and return the new expression. Also call
1516 replace_single_def_regs if the replacement changed something. */
1518 replace_in_expr (rtx
*expr
, rtx dest
, rtx src
)
1521 *expr
= simplify_replace_rtx (*expr
, dest
, src
);
1524 replace_single_def_regs (expr
);
1527 /* Checks whether A implies B. */
1530 implies_p (rtx a
, rtx b
)
1532 rtx op0
, op1
, opb0
, opb1
;
1535 if (rtx_equal_p (a
, b
))
1538 if (GET_CODE (a
) == EQ
)
1544 || (GET_CODE (op0
) == SUBREG
1545 && REG_P (SUBREG_REG (op0
))))
1547 rtx r
= simplify_replace_rtx (b
, op0
, op1
);
1548 if (r
== const_true_rtx
)
1553 || (GET_CODE (op1
) == SUBREG
1554 && REG_P (SUBREG_REG (op1
))))
1556 rtx r
= simplify_replace_rtx (b
, op1
, op0
);
1557 if (r
== const_true_rtx
)
1562 if (b
== const_true_rtx
)
1565 if ((GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMM_COMPARE
1566 && GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMPARE
)
1567 || (GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMM_COMPARE
1568 && GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMPARE
))
1576 mode
= GET_MODE (op0
);
1577 if (mode
!= GET_MODE (opb0
))
1579 else if (mode
== VOIDmode
)
1581 mode
= GET_MODE (op1
);
1582 if (mode
!= GET_MODE (opb1
))
1586 /* A < B implies A + 1 <= B. */
1587 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1588 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1591 if (GET_CODE (a
) == GT
)
1592 std::swap (op0
, op1
);
1594 if (GET_CODE (b
) == GE
)
1595 std::swap (opb0
, opb1
);
1597 if (SCALAR_INT_MODE_P (mode
)
1598 && rtx_equal_p (op1
, opb1
)
1599 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1604 /* A < B or A > B imply A != B. TODO: Likewise
1605 A + n < B implies A != B + n if neither wraps. */
1606 if (GET_CODE (b
) == NE
1607 && (GET_CODE (a
) == GT
|| GET_CODE (a
) == GTU
1608 || GET_CODE (a
) == LT
|| GET_CODE (a
) == LTU
))
1610 if (rtx_equal_p (op0
, opb0
)
1611 && rtx_equal_p (op1
, opb1
))
1615 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1616 if (GET_CODE (a
) == NE
1617 && op1
== const0_rtx
)
1619 if ((GET_CODE (b
) == GTU
1620 && opb1
== const0_rtx
)
1621 || (GET_CODE (b
) == GEU
1622 && opb1
== const1_rtx
))
1623 return rtx_equal_p (op0
, opb0
);
1626 /* A != N is equivalent to A - (N + 1) <u -1. */
1627 if (GET_CODE (a
) == NE
1628 && CONST_INT_P (op1
)
1629 && GET_CODE (b
) == LTU
1630 && opb1
== constm1_rtx
1631 && GET_CODE (opb0
) == PLUS
1632 && CONST_INT_P (XEXP (opb0
, 1))
1633 /* Avoid overflows. */
1634 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1635 != ((unsigned HOST_WIDE_INT
)1
1636 << (HOST_BITS_PER_WIDE_INT
- 1)) - 1)
1637 && INTVAL (XEXP (opb0
, 1)) + 1 == -INTVAL (op1
))
1638 return rtx_equal_p (op0
, XEXP (opb0
, 0));
1640 /* Likewise, A != N implies A - N > 0. */
1641 if (GET_CODE (a
) == NE
1642 && CONST_INT_P (op1
))
1644 if (GET_CODE (b
) == GTU
1645 && GET_CODE (opb0
) == PLUS
1646 && opb1
== const0_rtx
1647 && CONST_INT_P (XEXP (opb0
, 1))
1648 /* Avoid overflows. */
1649 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1650 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1651 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1652 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1653 if (GET_CODE (b
) == GEU
1654 && GET_CODE (opb0
) == PLUS
1655 && opb1
== const1_rtx
1656 && CONST_INT_P (XEXP (opb0
, 1))
1657 /* Avoid overflows. */
1658 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1659 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1660 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1661 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1664 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1665 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == GE
)
1666 && CONST_INT_P (op1
)
1667 && ((GET_CODE (a
) == GT
&& op1
== constm1_rtx
)
1668 || INTVAL (op1
) >= 0)
1669 && GET_CODE (b
) == LTU
1670 && CONST_INT_P (opb1
)
1671 && rtx_equal_p (op0
, opb0
))
1672 return INTVAL (opb1
) < 0;
1677 /* Canonicalizes COND so that
1679 (1) Ensure that operands are ordered according to
1680 swap_commutative_operands_p.
1681 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1682 for GE, GEU, and LEU. */
1685 canon_condition (rtx cond
)
1691 code
= GET_CODE (cond
);
1692 op0
= XEXP (cond
, 0);
1693 op1
= XEXP (cond
, 1);
1695 if (swap_commutative_operands_p (op0
, op1
))
1697 code
= swap_condition (code
);
1698 std::swap (op0
, op1
);
1701 mode
= GET_MODE (op0
);
1702 if (mode
== VOIDmode
)
1703 mode
= GET_MODE (op1
);
1704 gcc_assert (mode
!= VOIDmode
);
1706 if (CONST_SCALAR_INT_P (op1
) && GET_MODE_CLASS (mode
) != MODE_CC
)
1708 rtx_mode_t
const_val (op1
, mode
);
1713 if (wi::ne_p (const_val
, wi::max_value (mode
, SIGNED
)))
1716 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1721 if (wi::ne_p (const_val
, wi::min_value (mode
, SIGNED
)))
1724 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1729 if (wi::ne_p (const_val
, -1))
1732 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1737 if (wi::ne_p (const_val
, 0))
1740 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1749 if (op0
!= XEXP (cond
, 0)
1750 || op1
!= XEXP (cond
, 1)
1751 || code
!= GET_CODE (cond
)
1752 || GET_MODE (cond
) != SImode
)
1753 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1758 /* Reverses CONDition; returns NULL if we cannot. */
1761 reversed_condition (rtx cond
)
1763 enum rtx_code reversed
;
1764 reversed
= reversed_comparison_code (cond
, NULL
);
1765 if (reversed
== UNKNOWN
)
1768 return gen_rtx_fmt_ee (reversed
,
1769 GET_MODE (cond
), XEXP (cond
, 0),
1773 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1774 set of altered regs. */
1777 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1779 rtx rev
, reve
, exp
= *expr
;
1781 /* If some register gets altered later, we do not really speak about its
1782 value at the time of comparison. */
1783 if (altered
&& altered_reg_used (cond
, altered
))
1786 if (GET_CODE (cond
) == EQ
1787 && REG_P (XEXP (cond
, 0)) && CONSTANT_P (XEXP (cond
, 1)))
1789 *expr
= simplify_replace_rtx (*expr
, XEXP (cond
, 0), XEXP (cond
, 1));
1793 if (!COMPARISON_P (exp
))
1796 rev
= reversed_condition (cond
);
1797 reve
= reversed_condition (exp
);
1799 cond
= canon_condition (cond
);
1800 exp
= canon_condition (exp
);
1802 rev
= canon_condition (rev
);
1804 reve
= canon_condition (reve
);
1806 if (rtx_equal_p (exp
, cond
))
1808 *expr
= const_true_rtx
;
1812 if (rev
&& rtx_equal_p (exp
, rev
))
1818 if (implies_p (cond
, exp
))
1820 *expr
= const_true_rtx
;
1824 if (reve
&& implies_p (cond
, reve
))
1830 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1832 if (rev
&& implies_p (exp
, rev
))
1838 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1839 if (rev
&& reve
&& implies_p (reve
, rev
))
1841 *expr
= const_true_rtx
;
1845 /* We would like to have some other tests here. TODO. */
1850 /* Use relationship between A and *B to eventually eliminate *B.
1851 OP is the operation we consider. */
1854 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1859 /* If A implies *B, we may replace *B by true. */
1860 if (implies_p (a
, *b
))
1861 *b
= const_true_rtx
;
1865 /* If *B implies A, we may replace *B by false. */
1866 if (implies_p (*b
, a
))
1875 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1876 operation we consider. */
1879 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1883 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1884 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1885 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1886 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1889 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1890 is a list, its elements are assumed to be combined using OP. */
1893 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1895 bool expression_valid
;
1896 rtx head
, tail
, last_valid_expr
;
1897 rtx_expr_list
*cond_list
;
1900 regset altered
, this_altered
;
1906 if (CONSTANT_P (*expr
))
1909 if (GET_CODE (*expr
) == EXPR_LIST
)
1911 head
= XEXP (*expr
, 0);
1912 tail
= XEXP (*expr
, 1);
1914 eliminate_implied_conditions (op
, &head
, tail
);
1919 neutral
= const_true_rtx
;
1924 neutral
= const0_rtx
;
1925 aggr
= const_true_rtx
;
1932 simplify_using_initial_values (loop
, UNKNOWN
, &head
);
1935 XEXP (*expr
, 0) = aggr
;
1936 XEXP (*expr
, 1) = NULL_RTX
;
1939 else if (head
== neutral
)
1942 simplify_using_initial_values (loop
, op
, expr
);
1945 simplify_using_initial_values (loop
, op
, &tail
);
1947 if (tail
&& XEXP (tail
, 0) == aggr
)
1953 XEXP (*expr
, 0) = head
;
1954 XEXP (*expr
, 1) = tail
;
1958 gcc_assert (op
== UNKNOWN
);
1960 replace_single_def_regs (expr
);
1961 if (CONSTANT_P (*expr
))
1964 e
= loop_preheader_edge (loop
);
1965 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1968 altered
= ALLOC_REG_SET (®_obstack
);
1969 this_altered
= ALLOC_REG_SET (®_obstack
);
1971 expression_valid
= true;
1972 last_valid_expr
= *expr
;
1976 insn
= BB_END (e
->src
);
1977 if (any_condjump_p (insn
))
1979 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false, true);
1981 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1982 cond
= reversed_condition (cond
);
1986 simplify_using_condition (cond
, expr
, altered
);
1990 if (CONSTANT_P (*expr
))
1992 for (note
= cond_list
; note
; note
= XEXP (note
, 1))
1994 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
1995 if (CONSTANT_P (*expr
))
1999 cond_list
= alloc_EXPR_LIST (0, cond
, cond_list
);
2003 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
2011 CLEAR_REG_SET (this_altered
);
2012 note_stores (PATTERN (insn
), mark_altered
, this_altered
);
2015 /* Kill all call clobbered registers. */
2017 hard_reg_set_iterator hrsi
;
2018 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call
,
2020 SET_REGNO_REG_SET (this_altered
, i
);
2023 if (suitable_set_for_replacement (insn
, &dest
, &src
))
2025 rtx_expr_list
**pnote
, **pnote_next
;
2027 replace_in_expr (expr
, dest
, src
);
2028 if (CONSTANT_P (*expr
))
2031 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2033 rtx_expr_list
*note
= *pnote
;
2034 rtx old_cond
= XEXP (note
, 0);
2036 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2037 replace_in_expr (&XEXP (note
, 0), dest
, src
);
2039 /* We can no longer use a condition that has been simplified
2040 to a constant, and simplify_using_condition will abort if
2042 if (CONSTANT_P (XEXP (note
, 0)))
2044 *pnote
= *pnote_next
;
2046 free_EXPR_LIST_node (note
);
2048 /* Retry simplifications with this condition if either the
2049 expression or the condition changed. */
2050 else if (old_cond
!= XEXP (note
, 0) || old
!= *expr
)
2051 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
2056 rtx_expr_list
**pnote
, **pnote_next
;
2058 /* If we did not use this insn to make a replacement, any overlap
2059 between stores in this insn and our expression will cause the
2060 expression to become invalid. */
2061 if (altered_reg_used (*expr
, this_altered
))
2064 /* Likewise for the conditions. */
2065 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2067 rtx_expr_list
*note
= *pnote
;
2068 rtx old_cond
= XEXP (note
, 0);
2070 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2071 if (altered_reg_used (old_cond
, this_altered
))
2073 *pnote
= *pnote_next
;
2075 free_EXPR_LIST_node (note
);
2080 if (CONSTANT_P (*expr
))
2083 IOR_REG_SET (altered
, this_altered
);
2085 /* If the expression now contains regs that have been altered, we
2086 can't return it to the caller. However, it is still valid for
2087 further simplification, so keep searching to see if we can
2088 eventually turn it into a constant. */
2089 if (altered_reg_used (*expr
, altered
))
2090 expression_valid
= false;
2091 if (expression_valid
)
2092 last_valid_expr
= *expr
;
2095 if (!single_pred_p (e
->src
)
2096 || single_pred (e
->src
) == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
2098 e
= single_pred_edge (e
->src
);
2102 free_EXPR_LIST_list (&cond_list
);
2103 if (!CONSTANT_P (*expr
))
2104 *expr
= last_valid_expr
;
2105 FREE_REG_SET (altered
);
2106 FREE_REG_SET (this_altered
);
2109 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2110 that IV occurs as left operands of comparison COND and its signedness
2111 is SIGNED_P to DESC. */
2114 shorten_into_mode (struct rtx_iv
*iv
, machine_mode mode
,
2115 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
2117 rtx mmin
, mmax
, cond_over
, cond_under
;
2119 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
2120 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
2122 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
2131 if (cond_under
!= const0_rtx
)
2133 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2134 if (cond_over
!= const0_rtx
)
2135 desc
->noloop_assumptions
=
2136 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
2143 if (cond_over
!= const0_rtx
)
2145 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2146 if (cond_under
!= const0_rtx
)
2147 desc
->noloop_assumptions
=
2148 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
2152 if (cond_over
!= const0_rtx
)
2154 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2155 if (cond_under
!= const0_rtx
)
2157 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2165 iv
->extend
= signed_p
? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
2168 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2169 subregs of the same mode if possible (sometimes it is necessary to add
2170 some assumptions to DESC). */
2173 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
2174 enum rtx_code cond
, struct niter_desc
*desc
)
2176 machine_mode comp_mode
;
2179 /* If the ivs behave specially in the first iteration, or are
2180 added/multiplied after extending, we ignore them. */
2181 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
2183 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
2186 /* If there is some extend, it must match signedness of the comparison. */
2191 if (iv0
->extend
== IV_ZERO_EXTEND
2192 || iv1
->extend
== IV_ZERO_EXTEND
)
2199 if (iv0
->extend
== IV_SIGN_EXTEND
2200 || iv1
->extend
== IV_SIGN_EXTEND
)
2206 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
2207 && iv1
->extend
!= IV_UNKNOWN_EXTEND
2208 && iv0
->extend
!= iv1
->extend
)
2212 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
)
2213 signed_p
= iv0
->extend
== IV_SIGN_EXTEND
;
2214 if (iv1
->extend
!= IV_UNKNOWN_EXTEND
)
2215 signed_p
= iv1
->extend
== IV_SIGN_EXTEND
;
2222 /* Values of both variables should be computed in the same mode. These
2223 might indeed be different, if we have comparison like
2225 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2227 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2228 in different modes. This does not seem impossible to handle, but
2229 it hardly ever occurs in practice.
2231 The only exception is the case when one of operands is invariant.
2232 For example pentium 3 generates comparisons like
2233 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2234 definitely do not want this prevent the optimization. */
2235 comp_mode
= iv0
->extend_mode
;
2236 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
2237 comp_mode
= iv1
->extend_mode
;
2239 if (iv0
->extend_mode
!= comp_mode
)
2241 if (iv0
->mode
!= iv0
->extend_mode
2242 || iv0
->step
!= const0_rtx
)
2245 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2246 comp_mode
, iv0
->base
, iv0
->mode
);
2247 iv0
->extend_mode
= comp_mode
;
2250 if (iv1
->extend_mode
!= comp_mode
)
2252 if (iv1
->mode
!= iv1
->extend_mode
2253 || iv1
->step
!= const0_rtx
)
2256 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2257 comp_mode
, iv1
->base
, iv1
->mode
);
2258 iv1
->extend_mode
= comp_mode
;
2261 /* Check that both ivs belong to a range of a single mode. If one of the
2262 operands is an invariant, we may need to shorten it into the common
2264 if (iv0
->mode
== iv0
->extend_mode
2265 && iv0
->step
== const0_rtx
2266 && iv0
->mode
!= iv1
->mode
)
2267 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
2269 if (iv1
->mode
== iv1
->extend_mode
2270 && iv1
->step
== const0_rtx
2271 && iv0
->mode
!= iv1
->mode
)
2272 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
2274 if (iv0
->mode
!= iv1
->mode
)
2277 desc
->mode
= iv0
->mode
;
2278 desc
->signed_p
= signed_p
;
2283 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2284 result. This function is called from iv_number_of_iterations with
2285 a number of fields in DESC already filled in. OLD_NITER is the original
2286 expression for the number of iterations, before we tried to simplify it. */
2289 determine_max_iter (struct loop
*loop
, struct niter_desc
*desc
, rtx old_niter
)
2291 rtx niter
= desc
->niter_expr
;
2292 rtx mmin
, mmax
, cmp
;
2294 uint64_t andmax
= 0;
2296 /* We used to look for constant operand 0 of AND,
2297 but canonicalization should always make this impossible. */
2298 gcc_checking_assert (GET_CODE (niter
) != AND
2299 || !CONST_INT_P (XEXP (niter
, 0)));
2301 if (GET_CODE (niter
) == AND
2302 && CONST_INT_P (XEXP (niter
, 1)))
2304 andmax
= UINTVAL (XEXP (niter
, 1));
2305 niter
= XEXP (niter
, 0);
2308 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
2309 nmax
= UINTVAL (mmax
) - UINTVAL (mmin
);
2311 if (GET_CODE (niter
) == UDIV
)
2313 if (!CONST_INT_P (XEXP (niter
, 1)))
2315 inc
= INTVAL (XEXP (niter
, 1));
2316 niter
= XEXP (niter
, 0);
2321 /* We could use a binary search here, but for now improving the upper
2322 bound by just one eliminates one important corner case. */
2323 cmp
= simplify_gen_relational (desc
->signed_p
? LT
: LTU
, VOIDmode
,
2324 desc
->mode
, old_niter
, mmax
);
2325 simplify_using_initial_values (loop
, UNKNOWN
, &cmp
);
2326 if (cmp
== const_true_rtx
)
2331 fprintf (dump_file
, ";; improved upper bound by one.\n");
2335 nmax
= MIN (nmax
, andmax
);
2337 fprintf (dump_file
, ";; Determined upper bound %" PRId64
".\n",
2342 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2343 the result into DESC. Very similar to determine_number_of_iterations
2344 (basically its rtl version), complicated by things like subregs. */
2347 iv_number_of_iterations (struct loop
*loop
, rtx_insn
*insn
, rtx condition
,
2348 struct niter_desc
*desc
)
2350 rtx op0
, op1
, delta
, step
, bound
, may_xform
, tmp
, tmp0
, tmp1
;
2351 struct rtx_iv iv0
, iv1
, tmp_iv
;
2352 rtx assumption
, may_not_xform
;
2354 machine_mode mode
, comp_mode
;
2355 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
2356 uint64_t s
, size
, d
, inv
, max
;
2357 int64_t up
, down
, inc
, step_val
;
2358 int was_sharp
= false;
2362 /* The meaning of these assumptions is this:
2364 then the rest of information does not have to be valid
2365 if noloop_assumptions then the loop does not roll
2366 if infinite then this exit is never used */
2368 desc
->assumptions
= NULL_RTX
;
2369 desc
->noloop_assumptions
= NULL_RTX
;
2370 desc
->infinite
= NULL_RTX
;
2371 desc
->simple_p
= true;
2373 desc
->const_iter
= false;
2374 desc
->niter_expr
= NULL_RTX
;
2376 cond
= GET_CODE (condition
);
2377 gcc_assert (COMPARISON_P (condition
));
2379 mode
= GET_MODE (XEXP (condition
, 0));
2380 if (mode
== VOIDmode
)
2381 mode
= GET_MODE (XEXP (condition
, 1));
2382 /* The constant comparisons should be folded. */
2383 gcc_assert (mode
!= VOIDmode
);
2385 /* We only handle integers or pointers. */
2386 if (GET_MODE_CLASS (mode
) != MODE_INT
2387 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
2390 op0
= XEXP (condition
, 0);
2391 if (!iv_analyze (insn
, op0
, &iv0
))
2393 if (iv0
.extend_mode
== VOIDmode
)
2394 iv0
.mode
= iv0
.extend_mode
= mode
;
2396 op1
= XEXP (condition
, 1);
2397 if (!iv_analyze (insn
, op1
, &iv1
))
2399 if (iv1
.extend_mode
== VOIDmode
)
2400 iv1
.mode
= iv1
.extend_mode
= mode
;
2402 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
2403 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
2406 /* Check condition and normalize it. */
2414 tmp_iv
= iv0
; iv0
= iv1
; iv1
= tmp_iv
;
2415 cond
= swap_condition (cond
);
2427 /* Handle extends. This is relatively nontrivial, so we only try in some
2428 easy cases, when we can canonicalize the ivs (possibly by adding some
2429 assumptions) to shape subreg (base + i * step). This function also fills
2430 in desc->mode and desc->signed_p. */
2432 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2435 comp_mode
= iv0
.extend_mode
;
2437 size
= GET_MODE_PRECISION (mode
);
2438 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2439 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2440 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2442 if (!CONST_INT_P (iv0
.step
) || !CONST_INT_P (iv1
.step
))
2445 /* We can take care of the case of two induction variables chasing each other
2446 if the test is NE. I have never seen a loop using it, but still it is
2448 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2453 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2454 iv1
.step
= const0_rtx
;
2457 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2458 iv1
.step
= lowpart_subreg (mode
, iv1
.step
, comp_mode
);
2460 /* This is either infinite loop or the one that ends immediately, depending
2461 on initial values. Unswitching should remove this kind of conditions. */
2462 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2467 if (iv0
.step
== const0_rtx
)
2468 step_val
= -INTVAL (iv1
.step
);
2470 step_val
= INTVAL (iv0
.step
);
2472 /* Ignore loops of while (i-- < 10) type. */
2476 step_is_pow2
= !(step_val
& (step_val
- 1));
2480 /* We do not care about whether the step is power of two in this
2482 step_is_pow2
= false;
2486 /* Some more condition normalization. We must record some assumptions
2487 due to overflows. */
2492 /* We want to take care only of non-sharp relationals; this is easy,
2493 as in cases the overflow would make the transformation unsafe
2494 the loop does not roll. Seemingly it would make more sense to want
2495 to take care of sharp relationals instead, as NE is more similar to
2496 them, but the problem is that here the transformation would be more
2497 difficult due to possibly infinite loops. */
2498 if (iv0
.step
== const0_rtx
)
2500 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2501 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2503 if (assumption
== const_true_rtx
)
2504 goto zero_iter_simplify
;
2505 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2506 iv0
.base
, const1_rtx
);
2510 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2511 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2513 if (assumption
== const_true_rtx
)
2514 goto zero_iter_simplify
;
2515 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2516 iv1
.base
, constm1_rtx
);
2519 if (assumption
!= const0_rtx
)
2520 desc
->noloop_assumptions
=
2521 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2522 cond
= (cond
== LT
) ? LE
: LEU
;
2524 /* It will be useful to be able to tell the difference once more in
2525 LE -> NE reduction. */
2531 /* Take care of trivially infinite loops. */
2534 if (iv0
.step
== const0_rtx
)
2536 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2537 if (rtx_equal_p (tmp
, mode_mmin
))
2540 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2541 /* Fill in the remaining fields somehow. */
2542 goto zero_iter_simplify
;
2547 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2548 if (rtx_equal_p (tmp
, mode_mmax
))
2551 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2552 /* Fill in the remaining fields somehow. */
2553 goto zero_iter_simplify
;
2558 /* If we can we want to take care of NE conditions instead of size
2559 comparisons, as they are much more friendly (most importantly
2560 this takes care of special handling of loops with step 1). We can
2561 do it if we first check that upper bound is greater or equal to
2562 lower bound, their difference is constant c modulo step and that
2563 there is not an overflow. */
2566 if (iv0
.step
== const0_rtx
)
2567 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2570 step
= lowpart_subreg (mode
, step
, comp_mode
);
2571 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2572 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2573 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2574 may_xform
= const0_rtx
;
2575 may_not_xform
= const_true_rtx
;
2577 if (CONST_INT_P (delta
))
2579 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2581 /* A special case. We have transformed condition of type
2582 for (i = 0; i < 4; i += 4)
2584 for (i = 0; i <= 3; i += 4)
2585 obviously if the test for overflow during that transformation
2586 passed, we cannot overflow here. Most importantly any
2587 loop with sharp end condition and step 1 falls into this
2588 category, so handling this case specially is definitely
2589 worth the troubles. */
2590 may_xform
= const_true_rtx
;
2592 else if (iv0
.step
== const0_rtx
)
2594 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2595 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2596 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2597 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2598 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2600 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2606 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2607 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2608 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2609 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2610 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2612 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2618 if (may_xform
!= const0_rtx
)
2620 /* We perform the transformation always provided that it is not
2621 completely senseless. This is OK, as we would need this assumption
2622 to determine the number of iterations anyway. */
2623 if (may_xform
!= const_true_rtx
)
2625 /* If the step is a power of two and the final value we have
2626 computed overflows, the cycle is infinite. Otherwise it
2627 is nontrivial to compute the number of iterations. */
2629 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2632 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2636 /* We are going to lose some information about upper bound on
2637 number of iterations in this step, so record the information
2639 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2640 if (CONST_INT_P (iv1
.base
))
2641 up
= INTVAL (iv1
.base
);
2643 up
= INTVAL (mode_mmax
) - inc
;
2644 down
= INTVAL (CONST_INT_P (iv0
.base
)
2647 max
= (uint64_t) (up
- down
) / inc
+ 1;
2649 && !desc
->assumptions
)
2650 record_niter_bound (loop
, max
, false, true);
2652 if (iv0
.step
== const0_rtx
)
2654 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2655 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2659 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2660 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2663 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2664 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2665 assumption
= simplify_gen_relational (reverse_condition (cond
),
2666 SImode
, mode
, tmp0
, tmp1
);
2667 if (assumption
== const_true_rtx
)
2668 goto zero_iter_simplify
;
2669 else if (assumption
!= const0_rtx
)
2670 desc
->noloop_assumptions
=
2671 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2676 /* Count the number of iterations. */
2679 /* Everything we do here is just arithmetics modulo size of mode. This
2680 makes us able to do more involved computations of number of iterations
2681 than in other cases. First transform the condition into shape
2682 s * i <> c, with s positive. */
2683 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2684 iv0
.base
= const0_rtx
;
2685 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2686 iv1
.step
= const0_rtx
;
2687 if (INTVAL (iv0
.step
) < 0)
2689 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, comp_mode
);
2690 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, comp_mode
);
2692 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2694 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2695 is infinite. Otherwise, the number of iterations is
2696 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2697 s
= INTVAL (iv0
.step
); d
= 1;
2704 bound
= GEN_INT (((uint64_t) 1 << (size
- 1 ) << 1) - 1);
2706 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2707 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, gen_int_mode (d
, mode
));
2708 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2709 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2711 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, gen_int_mode (d
, mode
));
2712 inv
= inverse (s
, size
);
2713 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, gen_int_mode (inv
, mode
));
2714 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2718 if (iv1
.step
== const0_rtx
)
2719 /* Condition in shape a + s * i <= b
2720 We must know that b + s does not overflow and a <= b + s and then we
2721 can compute number of iterations as (b + s - a) / s. (It might
2722 seem that we in fact could be more clever about testing the b + s
2723 overflow condition using some information about b - a mod s,
2724 but it was already taken into account during LE -> NE transform). */
2727 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2728 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2730 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2731 lowpart_subreg (mode
, step
,
2737 /* If s is power of 2, we know that the loop is infinite if
2738 a % s <= b % s and b + s overflows. */
2739 assumption
= simplify_gen_relational (reverse_condition (cond
),
2743 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2744 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2745 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2746 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2748 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2752 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2755 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2758 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, iv0
.step
);
2759 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2760 assumption
= simplify_gen_relational (reverse_condition (cond
),
2761 SImode
, mode
, tmp0
, tmp
);
2763 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2764 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2768 /* Condition in shape a <= b - s * i
2769 We must know that a - s does not overflow and a - s <= b and then
2770 we can again compute number of iterations as (b - (a - s)) / s. */
2771 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2772 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2773 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2775 bound
= simplify_gen_binary (PLUS
, mode
, mode_mmin
,
2776 lowpart_subreg (mode
, step
, comp_mode
));
2781 /* If s is power of 2, we know that the loop is infinite if
2782 a % s <= b % s and a - s overflows. */
2783 assumption
= simplify_gen_relational (reverse_condition (cond
),
2787 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2788 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2789 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2790 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2792 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2796 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2799 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2802 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2803 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2804 assumption
= simplify_gen_relational (reverse_condition (cond
),
2807 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2808 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2810 if (assumption
== const_true_rtx
)
2811 goto zero_iter_simplify
;
2812 else if (assumption
!= const0_rtx
)
2813 desc
->noloop_assumptions
=
2814 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2815 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2816 desc
->niter_expr
= delta
;
2819 old_niter
= desc
->niter_expr
;
2821 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2822 if (desc
->assumptions
2823 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2825 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2826 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2827 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2829 /* Rerun the simplification. Consider code (created by copying loop headers)
2841 The first pass determines that i = 0, the second pass uses it to eliminate
2842 noloop assumption. */
2844 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2845 if (desc
->assumptions
2846 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2848 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2849 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2850 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2852 if (desc
->noloop_assumptions
2853 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2856 if (CONST_INT_P (desc
->niter_expr
))
2858 uint64_t val
= INTVAL (desc
->niter_expr
);
2860 desc
->const_iter
= true;
2861 desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2863 && !desc
->assumptions
)
2864 record_niter_bound (loop
, desc
->niter
, false, true);
2868 max
= determine_max_iter (loop
, desc
, old_niter
);
2870 goto zero_iter_simplify
;
2872 && !desc
->assumptions
)
2873 record_niter_bound (loop
, max
, false, true);
2875 /* simplify_using_initial_values does a copy propagation on the registers
2876 in the expression for the number of iterations. This prolongs life
2877 ranges of registers and increases register pressure, and usually
2878 brings no gain (and if it happens to do, the cse pass will take care
2879 of it anyway). So prevent this behavior, unless it enabled us to
2880 derive that the number of iterations is a constant. */
2881 desc
->niter_expr
= old_niter
;
2887 /* Simplify the assumptions. */
2888 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2889 if (desc
->assumptions
2890 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2892 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2896 desc
->const_iter
= true;
2898 record_niter_bound (loop
, 0, true, true);
2899 desc
->noloop_assumptions
= NULL_RTX
;
2900 desc
->niter_expr
= const0_rtx
;
2904 desc
->simple_p
= false;
2908 /* Checks whether E is a simple exit from LOOP and stores its description
2912 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2914 basic_block exit_bb
;
2920 desc
->simple_p
= false;
2922 /* It must belong directly to the loop. */
2923 if (exit_bb
->loop_father
!= loop
)
2926 /* It must be tested (at least) once during any iteration. */
2927 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2930 /* It must end in a simple conditional jump. */
2931 if (!any_condjump_p (BB_END (exit_bb
)))
2934 ein
= EDGE_SUCC (exit_bb
, 0);
2936 ein
= EDGE_SUCC (exit_bb
, 1);
2939 desc
->in_edge
= ein
;
2941 /* Test whether the condition is suitable. */
2942 if (!(condition
= get_condition (BB_END (ein
->src
), &at
, false, false)))
2945 if (ein
->flags
& EDGE_FALLTHRU
)
2947 condition
= reversed_condition (condition
);
2952 /* Check that we are able to determine number of iterations and fill
2953 in information about it. */
2954 iv_number_of_iterations (loop
, at
, condition
, desc
);
2957 /* Finds a simple exit of LOOP and stores its description into DESC. */
2960 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2965 struct niter_desc act
;
2969 desc
->simple_p
= false;
2970 body
= get_loop_body (loop
);
2972 for (i
= 0; i
< loop
->num_nodes
; i
++)
2974 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
2976 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2979 check_simple_exit (loop
, e
, &act
);
2987 /* Prefer constant iterations; the less the better. */
2989 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
2992 /* Also if the actual exit may be infinite, while the old one
2993 not, prefer the old one. */
2994 if (act
.infinite
&& !desc
->infinite
)
3006 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
3007 fprintf (dump_file
, " simple exit %d -> %d\n",
3008 desc
->out_edge
->src
->index
,
3009 desc
->out_edge
->dest
->index
);
3010 if (desc
->assumptions
)
3012 fprintf (dump_file
, " assumptions: ");
3013 print_rtl (dump_file
, desc
->assumptions
);
3014 fprintf (dump_file
, "\n");
3016 if (desc
->noloop_assumptions
)
3018 fprintf (dump_file
, " does not roll if: ");
3019 print_rtl (dump_file
, desc
->noloop_assumptions
);
3020 fprintf (dump_file
, "\n");
3024 fprintf (dump_file
, " infinite if: ");
3025 print_rtl (dump_file
, desc
->infinite
);
3026 fprintf (dump_file
, "\n");
3029 fprintf (dump_file
, " number of iterations: ");
3030 print_rtl (dump_file
, desc
->niter_expr
);
3031 fprintf (dump_file
, "\n");
3033 fprintf (dump_file
, " upper bound: %li\n",
3034 (long)get_max_loop_iterations_int (loop
));
3035 fprintf (dump_file
, " realistic bound: %li\n",
3036 (long)get_estimated_loop_iterations_int (loop
));
3039 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
3045 /* Creates a simple loop description of LOOP if it was not computed
3049 get_simple_loop_desc (struct loop
*loop
)
3051 struct niter_desc
*desc
= simple_loop_desc (loop
);
3056 /* At least desc->infinite is not always initialized by
3057 find_simple_loop_exit. */
3058 desc
= ggc_cleared_alloc
<niter_desc
> ();
3059 iv_analysis_loop_init (loop
);
3060 find_simple_exit (loop
, desc
);
3061 loop
->simple_loop_desc
= desc
;
3063 if (desc
->simple_p
&& (desc
->assumptions
|| desc
->infinite
))
3065 const char *wording
;
3067 /* Assume that no overflow happens and that the loop is finite.
3068 We already warned at the tree level if we ran optimizations there. */
3069 if (!flag_tree_loop_optimize
&& warn_unsafe_loop_optimizations
)
3074 flag_unsafe_loop_optimizations
3075 ? N_("assuming that the loop is not infinite")
3076 : N_("cannot optimize possibly infinite loops");
3077 warning (OPT_Wunsafe_loop_optimizations
, "%s",
3080 if (desc
->assumptions
)
3083 flag_unsafe_loop_optimizations
3084 ? N_("assuming that the loop counter does not overflow")
3085 : N_("cannot optimize loop, the loop counter may overflow");
3086 warning (OPT_Wunsafe_loop_optimizations
, "%s",
3091 if (flag_unsafe_loop_optimizations
)
3093 desc
->assumptions
= NULL_RTX
;
3094 desc
->infinite
= NULL_RTX
;
3101 /* Releases simple loop description for LOOP. */
3104 free_simple_loop_desc (struct loop
*loop
)
3106 struct niter_desc
*desc
= simple_loop_desc (loop
);
3112 loop
->simple_loop_desc
= NULL
;