1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004-2016 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"
58 #include "diagnostic-core.h"
64 /* Possible return values of iv_get_reaching_def. */
68 /* More than one reaching def, or reaching def that does not
72 /* The use is trivial invariant of the loop, i.e. is not changed
76 /* The use is reached by initial value and a value from the
77 previous iteration. */
80 /* The use has single dominating def. */
84 /* Information about a biv. */
88 unsigned regno
; /* The register of the biv. */
89 struct rtx_iv iv
; /* Value of the biv. */
92 static bool clean_slate
= true;
94 static unsigned int iv_ref_table_size
= 0;
96 /* Table of rtx_ivs indexed by the df_ref uid field. */
97 static struct rtx_iv
** iv_ref_table
;
99 /* Induction variable stored at the reference. */
100 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
101 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
103 /* The current loop. */
105 static struct loop
*current_loop
;
107 /* Hashtable helper. */
109 struct biv_entry_hasher
: free_ptr_hash
<biv_entry
>
111 typedef rtx_def
*compare_type
;
112 static inline hashval_t
hash (const biv_entry
*);
113 static inline bool equal (const biv_entry
*, const rtx_def
*);
116 /* Returns hash value for biv B. */
119 biv_entry_hasher::hash (const biv_entry
*b
)
124 /* Compares biv B and register R. */
127 biv_entry_hasher::equal (const biv_entry
*b
, const rtx_def
*r
)
129 return b
->regno
== REGNO (r
);
132 /* Bivs of the current loop. */
134 static hash_table
<biv_entry_hasher
> *bivs
;
136 static bool iv_analyze_op (rtx_insn
*, rtx
, struct rtx_iv
*);
138 /* Return the RTX code corresponding to the IV extend code EXTEND. */
139 static inline enum rtx_code
140 iv_extend_to_rtx_code (enum iv_extend_code extend
)
148 case IV_UNKNOWN_EXTEND
:
154 /* Dumps information about IV to FILE. */
156 extern void dump_iv_info (FILE *, struct rtx_iv
*);
158 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
162 fprintf (file
, "not simple");
166 if (iv
->step
== const0_rtx
167 && !iv
->first_special
)
168 fprintf (file
, "invariant ");
170 print_rtl (file
, iv
->base
);
171 if (iv
->step
!= const0_rtx
)
173 fprintf (file
, " + ");
174 print_rtl (file
, iv
->step
);
175 fprintf (file
, " * iteration");
177 fprintf (file
, " (in %s)", GET_MODE_NAME (iv
->mode
));
179 if (iv
->mode
!= iv
->extend_mode
)
180 fprintf (file
, " %s to %s",
181 rtx_name
[iv_extend_to_rtx_code (iv
->extend
)],
182 GET_MODE_NAME (iv
->extend_mode
));
184 if (iv
->mult
!= const1_rtx
)
186 fprintf (file
, " * ");
187 print_rtl (file
, iv
->mult
);
189 if (iv
->delta
!= const0_rtx
)
191 fprintf (file
, " + ");
192 print_rtl (file
, iv
->delta
);
194 if (iv
->first_special
)
195 fprintf (file
, " (first special)");
199 check_iv_ref_table_size (void)
201 if (iv_ref_table_size
< DF_DEFS_TABLE_SIZE ())
203 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
204 iv_ref_table
= XRESIZEVEC (struct rtx_iv
*, iv_ref_table
, new_size
);
205 memset (&iv_ref_table
[iv_ref_table_size
], 0,
206 (new_size
- iv_ref_table_size
) * sizeof (struct rtx_iv
*));
207 iv_ref_table_size
= new_size
;
212 /* Checks whether REG is a well-behaved register. */
215 simple_reg_p (rtx reg
)
219 if (GET_CODE (reg
) == SUBREG
)
221 if (!subreg_lowpart_p (reg
))
223 reg
= SUBREG_REG (reg
);
230 if (HARD_REGISTER_NUM_P (r
))
233 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
239 /* Clears the information about ivs stored in df. */
244 unsigned i
, n_defs
= DF_DEFS_TABLE_SIZE ();
247 check_iv_ref_table_size ();
248 for (i
= 0; i
< n_defs
; i
++)
250 iv
= iv_ref_table
[i
];
254 iv_ref_table
[i
] = NULL
;
262 /* Prepare the data for an induction variable analysis of a LOOP. */
265 iv_analysis_loop_init (struct loop
*loop
)
269 /* Clear the information from the analysis of the previous loop. */
272 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
273 bivs
= new hash_table
<biv_entry_hasher
> (10);
279 /* Get rid of the ud chains before processing the rescans. Then add
281 df_remove_problem (df_chain
);
282 df_process_deferred_rescans ();
283 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
284 df_chain_add_problem (DF_UD_CHAIN
);
285 df_note_add_problem ();
286 df_analyze_loop (loop
);
288 df_dump_region (dump_file
);
290 check_iv_ref_table_size ();
293 /* Finds the definition of REG that dominates loop latch and stores
294 it to DEF. Returns false if there is not a single definition
295 dominating the latch. If REG has no definition in loop, DEF
296 is set to NULL and true is returned. */
299 latch_dominating_def (rtx reg
, df_ref
*def
)
301 df_ref single_rd
= NULL
, adef
;
302 unsigned regno
= REGNO (reg
);
303 struct df_rd_bb_info
*bb_info
= DF_RD_BB_INFO (current_loop
->latch
);
305 for (adef
= DF_REG_DEF_CHAIN (regno
); adef
; adef
= DF_REF_NEXT_REG (adef
))
307 if (!bitmap_bit_p (df
->blocks_to_analyze
, DF_REF_BBNO (adef
))
308 || !bitmap_bit_p (&bb_info
->out
, DF_REF_ID (adef
)))
311 /* More than one reaching definition. */
315 if (!just_once_each_iteration_p (current_loop
, DF_REF_BB (adef
)))
325 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
327 static enum iv_grd_result
328 iv_get_reaching_def (rtx_insn
*insn
, rtx reg
, df_ref
*def
)
331 basic_block def_bb
, use_bb
;
336 if (!simple_reg_p (reg
))
338 if (GET_CODE (reg
) == SUBREG
)
339 reg
= SUBREG_REG (reg
);
340 gcc_assert (REG_P (reg
));
342 use
= df_find_use (insn
, reg
);
343 gcc_assert (use
!= NULL
);
345 if (!DF_REF_CHAIN (use
))
346 return GRD_INVARIANT
;
348 /* More than one reaching def. */
349 if (DF_REF_CHAIN (use
)->next
)
352 adef
= DF_REF_CHAIN (use
)->ref
;
354 /* We do not handle setting only part of the register. */
355 if (DF_REF_FLAGS (adef
) & DF_REF_READ_WRITE
)
358 def_insn
= DF_REF_INSN (adef
);
359 def_bb
= DF_REF_BB (adef
);
360 use_bb
= BLOCK_FOR_INSN (insn
);
362 if (use_bb
== def_bb
)
363 dom_p
= (DF_INSN_LUID (def_insn
) < DF_INSN_LUID (insn
));
365 dom_p
= dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
);
370 return GRD_SINGLE_DOM
;
373 /* The definition does not dominate the use. This is still OK if
374 this may be a use of a biv, i.e. if the def_bb dominates loop
376 if (just_once_each_iteration_p (current_loop
, def_bb
))
377 return GRD_MAYBE_BIV
;
382 /* Sets IV to invariant CST in MODE. Always returns true (just for
383 consistency with other iv manipulation functions that may fail). */
386 iv_constant (struct rtx_iv
*iv
, rtx cst
, machine_mode mode
)
388 if (mode
== VOIDmode
)
389 mode
= GET_MODE (cst
);
393 iv
->step
= const0_rtx
;
394 iv
->first_special
= false;
395 iv
->extend
= IV_UNKNOWN_EXTEND
;
396 iv
->extend_mode
= iv
->mode
;
397 iv
->delta
= const0_rtx
;
398 iv
->mult
= const1_rtx
;
403 /* Evaluates application of subreg to MODE on IV. */
406 iv_subreg (struct rtx_iv
*iv
, machine_mode mode
)
408 /* If iv is invariant, just calculate the new value. */
409 if (iv
->step
== const0_rtx
410 && !iv
->first_special
)
412 rtx val
= get_iv_value (iv
, const0_rtx
);
413 val
= lowpart_subreg (mode
, val
,
414 iv
->extend
== IV_UNKNOWN_EXTEND
415 ? iv
->mode
: iv
->extend_mode
);
418 iv
->extend
= IV_UNKNOWN_EXTEND
;
419 iv
->mode
= iv
->extend_mode
= mode
;
420 iv
->delta
= const0_rtx
;
421 iv
->mult
= const1_rtx
;
425 if (iv
->extend_mode
== mode
)
428 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
431 iv
->extend
= IV_UNKNOWN_EXTEND
;
434 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
435 simplify_gen_binary (MULT
, iv
->extend_mode
,
436 iv
->base
, iv
->mult
));
437 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
438 iv
->mult
= const1_rtx
;
439 iv
->delta
= const0_rtx
;
440 iv
->first_special
= false;
445 /* Evaluates application of EXTEND to MODE on IV. */
448 iv_extend (struct rtx_iv
*iv
, enum iv_extend_code extend
, machine_mode mode
)
450 /* If iv is invariant, just calculate the new value. */
451 if (iv
->step
== const0_rtx
452 && !iv
->first_special
)
454 rtx val
= get_iv_value (iv
, const0_rtx
);
455 if (iv
->extend_mode
!= iv
->mode
456 && iv
->extend
!= IV_UNKNOWN_EXTEND
457 && iv
->extend
!= extend
)
458 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
459 val
= simplify_gen_unary (iv_extend_to_rtx_code (extend
), mode
,
462 ? iv
->extend_mode
: iv
->mode
);
464 iv
->extend
= IV_UNKNOWN_EXTEND
;
465 iv
->mode
= iv
->extend_mode
= mode
;
466 iv
->delta
= const0_rtx
;
467 iv
->mult
= const1_rtx
;
471 if (mode
!= iv
->extend_mode
)
474 if (iv
->extend
!= IV_UNKNOWN_EXTEND
475 && iv
->extend
!= extend
)
483 /* Evaluates negation of IV. */
486 iv_neg (struct rtx_iv
*iv
)
488 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
490 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
491 iv
->base
, iv
->extend_mode
);
492 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
493 iv
->step
, iv
->extend_mode
);
497 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
498 iv
->delta
, iv
->extend_mode
);
499 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
500 iv
->mult
, iv
->extend_mode
);
506 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
509 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
514 /* Extend the constant to extend_mode of the other operand if necessary. */
515 if (iv0
->extend
== IV_UNKNOWN_EXTEND
516 && iv0
->mode
== iv0
->extend_mode
517 && iv0
->step
== const0_rtx
518 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
520 iv0
->extend_mode
= iv1
->extend_mode
;
521 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
522 iv0
->base
, iv0
->mode
);
524 if (iv1
->extend
== IV_UNKNOWN_EXTEND
525 && iv1
->mode
== iv1
->extend_mode
526 && iv1
->step
== const0_rtx
527 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
529 iv1
->extend_mode
= iv0
->extend_mode
;
530 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
531 iv1
->base
, iv1
->mode
);
534 mode
= iv0
->extend_mode
;
535 if (mode
!= iv1
->extend_mode
)
538 if (iv0
->extend
== IV_UNKNOWN_EXTEND
539 && iv1
->extend
== IV_UNKNOWN_EXTEND
)
541 if (iv0
->mode
!= iv1
->mode
)
544 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
545 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
550 /* Handle addition of constant. */
551 if (iv1
->extend
== IV_UNKNOWN_EXTEND
553 && iv1
->step
== const0_rtx
)
555 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
559 if (iv0
->extend
== IV_UNKNOWN_EXTEND
561 && iv0
->step
== const0_rtx
)
569 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
576 /* Evaluates multiplication of IV by constant CST. */
579 iv_mult (struct rtx_iv
*iv
, rtx mby
)
581 machine_mode mode
= iv
->extend_mode
;
583 if (GET_MODE (mby
) != VOIDmode
584 && GET_MODE (mby
) != mode
)
587 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
589 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
590 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
594 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
595 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
601 /* Evaluates shift of IV by constant CST. */
604 iv_shift (struct rtx_iv
*iv
, rtx mby
)
606 machine_mode mode
= iv
->extend_mode
;
608 if (GET_MODE (mby
) != VOIDmode
609 && GET_MODE (mby
) != mode
)
612 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
614 iv
->base
= simplify_gen_binary (ASHIFT
, mode
, iv
->base
, mby
);
615 iv
->step
= simplify_gen_binary (ASHIFT
, mode
, iv
->step
, mby
);
619 iv
->delta
= simplify_gen_binary (ASHIFT
, mode
, iv
->delta
, mby
);
620 iv
->mult
= simplify_gen_binary (ASHIFT
, mode
, iv
->mult
, mby
);
626 /* The recursive part of get_biv_step. Gets the value of the single value
627 defined by DEF wrto initial value of REG inside loop, in shape described
631 get_biv_step_1 (df_ref def
, rtx reg
,
632 rtx
*inner_step
, machine_mode
*inner_mode
,
633 enum iv_extend_code
*extend
, machine_mode outer_mode
,
636 rtx set
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
639 rtx_insn
*insn
= DF_REF_INSN (def
);
641 enum iv_grd_result res
;
643 set
= single_set (insn
);
647 rhs
= find_reg_equal_equiv_note (insn
);
653 code
= GET_CODE (rhs
);
666 if (code
== PLUS
&& CONSTANT_P (op0
))
667 std::swap (op0
, op1
);
669 if (!simple_reg_p (op0
)
670 || !CONSTANT_P (op1
))
673 if (GET_MODE (rhs
) != outer_mode
)
675 /* ppc64 uses expressions like
677 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
679 this is equivalent to
681 (set x':DI (plus:DI y:DI 1))
682 (set x:SI (subreg:SI (x':DI)). */
683 if (GET_CODE (op0
) != SUBREG
)
685 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
694 if (GET_MODE (rhs
) != outer_mode
)
698 if (!simple_reg_p (op0
))
708 if (GET_CODE (next
) == SUBREG
)
710 if (!subreg_lowpart_p (next
))
713 nextr
= SUBREG_REG (next
);
714 if (GET_MODE (nextr
) != outer_mode
)
720 res
= iv_get_reaching_def (insn
, nextr
, &next_def
);
722 if (res
== GRD_INVALID
|| res
== GRD_INVARIANT
)
725 if (res
== GRD_MAYBE_BIV
)
727 if (!rtx_equal_p (nextr
, reg
))
730 *inner_step
= const0_rtx
;
731 *extend
= IV_UNKNOWN_EXTEND
;
732 *inner_mode
= outer_mode
;
733 *outer_step
= const0_rtx
;
735 else if (!get_biv_step_1 (next_def
, reg
,
736 inner_step
, inner_mode
, extend
, outer_mode
,
740 if (GET_CODE (next
) == SUBREG
)
742 machine_mode amode
= GET_MODE (next
);
744 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
748 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
749 *inner_step
, *outer_step
);
750 *outer_step
= const0_rtx
;
751 *extend
= IV_UNKNOWN_EXTEND
;
762 if (*inner_mode
== outer_mode
763 /* See comment in previous switch. */
764 || GET_MODE (rhs
) != outer_mode
)
765 *inner_step
= simplify_gen_binary (code
, outer_mode
,
768 *outer_step
= simplify_gen_binary (code
, outer_mode
,
774 gcc_assert (GET_MODE (op0
) == *inner_mode
775 && *extend
== IV_UNKNOWN_EXTEND
776 && *outer_step
== const0_rtx
);
778 *extend
= (code
== SIGN_EXTEND
) ? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
788 /* Gets the operation on register REG inside loop, in shape
790 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
792 If the operation cannot be described in this shape, return false.
793 LAST_DEF is the definition of REG that dominates loop latch. */
796 get_biv_step (df_ref last_def
, rtx reg
, rtx
*inner_step
,
797 machine_mode
*inner_mode
, enum iv_extend_code
*extend
,
798 machine_mode
*outer_mode
, rtx
*outer_step
)
800 *outer_mode
= GET_MODE (reg
);
802 if (!get_biv_step_1 (last_def
, reg
,
803 inner_step
, inner_mode
, extend
, *outer_mode
,
807 gcc_assert ((*inner_mode
== *outer_mode
) != (*extend
!= IV_UNKNOWN_EXTEND
));
808 gcc_assert (*inner_mode
!= *outer_mode
|| *outer_step
== const0_rtx
);
813 /* Records information that DEF is induction variable IV. */
816 record_iv (df_ref def
, struct rtx_iv
*iv
)
818 struct rtx_iv
*recorded_iv
= XNEW (struct rtx_iv
);
821 check_iv_ref_table_size ();
822 DF_REF_IV_SET (def
, recorded_iv
);
825 /* If DEF was already analyzed for bivness, store the description of the biv to
826 IV and return true. Otherwise return false. */
829 analyzed_for_bivness_p (rtx def
, struct rtx_iv
*iv
)
831 struct biv_entry
*biv
= bivs
->find_with_hash (def
, REGNO (def
));
841 record_biv (rtx def
, struct rtx_iv
*iv
)
843 struct biv_entry
*biv
= XNEW (struct biv_entry
);
844 biv_entry
**slot
= bivs
->find_slot_with_hash (def
, REGNO (def
), INSERT
);
846 biv
->regno
= REGNO (def
);
852 /* Determines whether DEF is a biv and if so, stores its description
856 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
858 rtx inner_step
, outer_step
;
859 machine_mode inner_mode
, outer_mode
;
860 enum iv_extend_code extend
;
865 fprintf (dump_file
, "Analyzing ");
866 print_rtl (dump_file
, def
);
867 fprintf (dump_file
, " for bivness.\n");
872 if (!CONSTANT_P (def
))
875 return iv_constant (iv
, def
, VOIDmode
);
878 if (!latch_dominating_def (def
, &last_def
))
881 fprintf (dump_file
, " not simple.\n");
886 return iv_constant (iv
, def
, VOIDmode
);
888 if (analyzed_for_bivness_p (def
, iv
))
891 fprintf (dump_file
, " already analysed.\n");
892 return iv
->base
!= NULL_RTX
;
895 if (!get_biv_step (last_def
, def
, &inner_step
, &inner_mode
, &extend
,
896 &outer_mode
, &outer_step
))
902 /* Loop transforms base to es (base + inner_step) + outer_step,
903 where es means extend of subreg between inner_mode and outer_mode.
904 The corresponding induction variable is
906 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
908 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
909 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
910 iv
->mode
= inner_mode
;
911 iv
->extend_mode
= outer_mode
;
913 iv
->mult
= const1_rtx
;
914 iv
->delta
= outer_step
;
915 iv
->first_special
= inner_mode
!= outer_mode
;
920 fprintf (dump_file
, " ");
921 dump_iv_info (dump_file
, iv
);
922 fprintf (dump_file
, "\n");
925 record_biv (def
, iv
);
926 return iv
->base
!= NULL_RTX
;
929 /* Analyzes expression RHS used at INSN and stores the result to *IV.
930 The mode of the induction variable is MODE. */
933 iv_analyze_expr (rtx_insn
*insn
, rtx rhs
, machine_mode mode
,
937 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
938 struct rtx_iv iv0
, iv1
;
939 enum rtx_code code
= GET_CODE (rhs
);
940 machine_mode omode
= mode
;
946 gcc_assert (GET_MODE (rhs
) == mode
|| GET_MODE (rhs
) == VOIDmode
);
952 if (!iv_analyze_op (insn
, rhs
, iv
))
955 if (iv
->mode
== VOIDmode
)
958 iv
->extend_mode
= mode
;
974 omode
= GET_MODE (op0
);
986 if (!CONSTANT_P (mby
))
987 std::swap (op0
, mby
);
988 if (!CONSTANT_P (mby
))
995 if (!CONSTANT_P (mby
))
1004 && !iv_analyze_expr (insn
, op0
, omode
, &iv0
))
1008 && !iv_analyze_expr (insn
, op1
, omode
, &iv1
))
1014 if (!iv_extend (&iv0
, IV_SIGN_EXTEND
, mode
))
1019 if (!iv_extend (&iv0
, IV_ZERO_EXTEND
, mode
))
1030 if (!iv_add (&iv0
, &iv1
, code
))
1035 if (!iv_mult (&iv0
, mby
))
1040 if (!iv_shift (&iv0
, mby
))
1049 return iv
->base
!= NULL_RTX
;
1052 /* Analyzes iv DEF and stores the result to *IV. */
1055 iv_analyze_def (df_ref def
, struct rtx_iv
*iv
)
1057 rtx_insn
*insn
= DF_REF_INSN (def
);
1058 rtx reg
= DF_REF_REG (def
);
1063 fprintf (dump_file
, "Analyzing def of ");
1064 print_rtl (dump_file
, reg
);
1065 fprintf (dump_file
, " in insn ");
1066 print_rtl_single (dump_file
, insn
);
1069 check_iv_ref_table_size ();
1070 if (DF_REF_IV (def
))
1073 fprintf (dump_file
, " already analysed.\n");
1074 *iv
= *DF_REF_IV (def
);
1075 return iv
->base
!= NULL_RTX
;
1078 iv
->mode
= VOIDmode
;
1079 iv
->base
= NULL_RTX
;
1080 iv
->step
= NULL_RTX
;
1085 set
= single_set (insn
);
1089 if (!REG_P (SET_DEST (set
)))
1092 gcc_assert (SET_DEST (set
) == reg
);
1093 rhs
= find_reg_equal_equiv_note (insn
);
1095 rhs
= XEXP (rhs
, 0);
1097 rhs
= SET_SRC (set
);
1099 iv_analyze_expr (insn
, rhs
, GET_MODE (reg
), iv
);
1100 record_iv (def
, iv
);
1104 print_rtl (dump_file
, reg
);
1105 fprintf (dump_file
, " in insn ");
1106 print_rtl_single (dump_file
, insn
);
1107 fprintf (dump_file
, " is ");
1108 dump_iv_info (dump_file
, iv
);
1109 fprintf (dump_file
, "\n");
1112 return iv
->base
!= NULL_RTX
;
1115 /* Analyzes operand OP of INSN and stores the result to *IV. */
1118 iv_analyze_op (rtx_insn
*insn
, rtx op
, struct rtx_iv
*iv
)
1121 enum iv_grd_result res
;
1125 fprintf (dump_file
, "Analyzing operand ");
1126 print_rtl (dump_file
, op
);
1127 fprintf (dump_file
, " of insn ");
1128 print_rtl_single (dump_file
, insn
);
1131 if (function_invariant_p (op
))
1132 res
= GRD_INVARIANT
;
1133 else if (GET_CODE (op
) == SUBREG
)
1135 if (!subreg_lowpart_p (op
))
1138 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
1141 return iv_subreg (iv
, GET_MODE (op
));
1145 res
= iv_get_reaching_def (insn
, op
, &def
);
1146 if (res
== GRD_INVALID
)
1149 fprintf (dump_file
, " not simple.\n");
1154 if (res
== GRD_INVARIANT
)
1156 iv_constant (iv
, op
, VOIDmode
);
1160 fprintf (dump_file
, " ");
1161 dump_iv_info (dump_file
, iv
);
1162 fprintf (dump_file
, "\n");
1167 if (res
== GRD_MAYBE_BIV
)
1168 return iv_analyze_biv (op
, iv
);
1170 return iv_analyze_def (def
, iv
);
1173 /* Analyzes value VAL at INSN and stores the result to *IV. */
1176 iv_analyze (rtx_insn
*insn
, rtx val
, struct rtx_iv
*iv
)
1180 /* We must find the insn in that val is used, so that we get to UD chains.
1181 Since the function is sometimes called on result of get_condition,
1182 this does not necessarily have to be directly INSN; scan also the
1184 if (simple_reg_p (val
))
1186 if (GET_CODE (val
) == SUBREG
)
1187 reg
= SUBREG_REG (val
);
1191 while (!df_find_use (insn
, reg
))
1192 insn
= NEXT_INSN (insn
);
1195 return iv_analyze_op (insn
, val
, iv
);
1198 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1201 iv_analyze_result (rtx_insn
*insn
, rtx def
, struct rtx_iv
*iv
)
1205 adef
= df_find_def (insn
, def
);
1209 return iv_analyze_def (adef
, iv
);
1212 /* Checks whether definition of register REG in INSN is a basic induction
1213 variable. IV analysis must have been initialized (via a call to
1214 iv_analysis_loop_init) for this function to produce a result. */
1217 biv_p (rtx_insn
*insn
, rtx reg
)
1220 df_ref def
, last_def
;
1222 if (!simple_reg_p (reg
))
1225 def
= df_find_def (insn
, reg
);
1226 gcc_assert (def
!= NULL
);
1227 if (!latch_dominating_def (reg
, &last_def
))
1229 if (last_def
!= def
)
1232 if (!iv_analyze_biv (reg
, &iv
))
1235 return iv
.step
!= const0_rtx
;
1238 /* Calculates value of IV at ITERATION-th iteration. */
1241 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1245 /* We would need to generate some if_then_else patterns, and so far
1246 it is not needed anywhere. */
1247 gcc_assert (!iv
->first_special
);
1249 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1250 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1251 simplify_gen_binary (MULT
, iv
->extend_mode
,
1252 iv
->step
, iteration
));
1256 if (iv
->extend_mode
== iv
->mode
)
1259 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1261 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
1264 val
= simplify_gen_unary (iv_extend_to_rtx_code (iv
->extend
),
1265 iv
->extend_mode
, val
, iv
->mode
);
1266 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1267 simplify_gen_binary (MULT
, iv
->extend_mode
,
1273 /* Free the data for an induction variable analysis. */
1276 iv_analysis_done (void)
1282 df_finish_pass (true);
1285 free (iv_ref_table
);
1286 iv_ref_table
= NULL
;
1287 iv_ref_table_size
= 0;
1291 /* Computes inverse to X modulo (1 << MOD). */
1294 inverse (uint64_t x
, int mod
)
1297 ((uint64_t) 1 << (mod
- 1) << 1) - 1;
1301 for (i
= 0; i
< mod
- 1; i
++)
1303 rslt
= (rslt
* x
) & mask
;
1310 /* Checks whether any register in X is in set ALT. */
1313 altered_reg_used (const_rtx x
, bitmap alt
)
1315 subrtx_iterator::array_type array
;
1316 FOR_EACH_SUBRTX (iter
, array
, x
, NONCONST
)
1318 const_rtx x
= *iter
;
1319 if (REG_P (x
) && REGNO_REG_SET_P (alt
, REGNO (x
)))
1325 /* Marks registers altered by EXPR in set ALT. */
1328 mark_altered (rtx expr
, const_rtx by ATTRIBUTE_UNUSED
, void *alt
)
1330 if (GET_CODE (expr
) == SUBREG
)
1331 expr
= SUBREG_REG (expr
);
1335 SET_REGNO_REG_SET ((bitmap
) alt
, REGNO (expr
));
1338 /* Checks whether RHS is simple enough to process. */
1341 simple_rhs_p (rtx rhs
)
1345 if (function_invariant_p (rhs
)
1346 || (REG_P (rhs
) && !HARD_REGISTER_P (rhs
)))
1349 switch (GET_CODE (rhs
))
1354 op0
= XEXP (rhs
, 0);
1355 op1
= XEXP (rhs
, 1);
1356 /* Allow reg OP const and reg OP reg. */
1357 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
))
1358 && !function_invariant_p (op0
))
1360 if (!(REG_P (op1
) && !HARD_REGISTER_P (op1
))
1361 && !function_invariant_p (op1
))
1370 op0
= XEXP (rhs
, 0);
1371 op1
= XEXP (rhs
, 1);
1372 /* Allow reg OP const. */
1373 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
)))
1375 if (!function_invariant_p (op1
))
1385 /* If REGNO has a single definition, return its known value, otherwise return
1389 find_single_def_src (unsigned int regno
)
1397 adef
= DF_REG_DEF_CHAIN (regno
);
1398 if (adef
== NULL
|| DF_REF_NEXT_REG (adef
) != NULL
1399 || DF_REF_IS_ARTIFICIAL (adef
))
1402 set
= single_set (DF_REF_INSN (adef
));
1403 if (set
== NULL
|| !REG_P (SET_DEST (set
))
1404 || REGNO (SET_DEST (set
)) != regno
)
1407 note
= find_reg_equal_equiv_note (DF_REF_INSN (adef
));
1409 if (note
&& function_invariant_p (XEXP (note
, 0)))
1411 src
= XEXP (note
, 0);
1414 src
= SET_SRC (set
);
1418 regno
= REGNO (src
);
1423 if (!function_invariant_p (src
))
1429 /* If any registers in *EXPR that have a single definition, try to replace
1430 them with the known-equivalent values. */
1433 replace_single_def_regs (rtx
*expr
)
1435 subrtx_var_iterator::array_type array
;
1437 FOR_EACH_SUBRTX_VAR (iter
, array
, *expr
, NONCONST
)
1441 if (rtx new_x
= find_single_def_src (REGNO (x
)))
1443 *expr
= simplify_replace_rtx (*expr
, x
, new_x
);
1449 /* A subroutine of simplify_using_initial_values, this function examines INSN
1450 to see if it contains a suitable set that we can use to make a replacement.
1451 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1452 the set; return false otherwise. */
1455 suitable_set_for_replacement (rtx_insn
*insn
, rtx
*dest
, rtx
*src
)
1457 rtx set
= single_set (insn
);
1458 rtx lhs
= NULL_RTX
, rhs
;
1463 lhs
= SET_DEST (set
);
1467 rhs
= find_reg_equal_equiv_note (insn
);
1469 rhs
= XEXP (rhs
, 0);
1471 rhs
= SET_SRC (set
);
1473 if (!simple_rhs_p (rhs
))
1481 /* Using the data returned by suitable_set_for_replacement, replace DEST
1482 with SRC in *EXPR and return the new expression. Also call
1483 replace_single_def_regs if the replacement changed something. */
1485 replace_in_expr (rtx
*expr
, rtx dest
, rtx src
)
1488 *expr
= simplify_replace_rtx (*expr
, dest
, src
);
1491 replace_single_def_regs (expr
);
1494 /* Checks whether A implies B. */
1497 implies_p (rtx a
, rtx b
)
1499 rtx op0
, op1
, opb0
, opb1
;
1502 if (rtx_equal_p (a
, b
))
1505 if (GET_CODE (a
) == EQ
)
1511 || (GET_CODE (op0
) == SUBREG
1512 && REG_P (SUBREG_REG (op0
))))
1514 rtx r
= simplify_replace_rtx (b
, op0
, op1
);
1515 if (r
== const_true_rtx
)
1520 || (GET_CODE (op1
) == SUBREG
1521 && REG_P (SUBREG_REG (op1
))))
1523 rtx r
= simplify_replace_rtx (b
, op1
, op0
);
1524 if (r
== const_true_rtx
)
1529 if (b
== const_true_rtx
)
1532 if ((GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMM_COMPARE
1533 && GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMPARE
)
1534 || (GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMM_COMPARE
1535 && GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMPARE
))
1543 mode
= GET_MODE (op0
);
1544 if (mode
!= GET_MODE (opb0
))
1546 else if (mode
== VOIDmode
)
1548 mode
= GET_MODE (op1
);
1549 if (mode
!= GET_MODE (opb1
))
1553 /* A < B implies A + 1 <= B. */
1554 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1555 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1558 if (GET_CODE (a
) == GT
)
1559 std::swap (op0
, op1
);
1561 if (GET_CODE (b
) == GE
)
1562 std::swap (opb0
, opb1
);
1564 if (SCALAR_INT_MODE_P (mode
)
1565 && rtx_equal_p (op1
, opb1
)
1566 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1571 /* A < B or A > B imply A != B. TODO: Likewise
1572 A + n < B implies A != B + n if neither wraps. */
1573 if (GET_CODE (b
) == NE
1574 && (GET_CODE (a
) == GT
|| GET_CODE (a
) == GTU
1575 || GET_CODE (a
) == LT
|| GET_CODE (a
) == LTU
))
1577 if (rtx_equal_p (op0
, opb0
)
1578 && rtx_equal_p (op1
, opb1
))
1582 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1583 if (GET_CODE (a
) == NE
1584 && op1
== const0_rtx
)
1586 if ((GET_CODE (b
) == GTU
1587 && opb1
== const0_rtx
)
1588 || (GET_CODE (b
) == GEU
1589 && opb1
== const1_rtx
))
1590 return rtx_equal_p (op0
, opb0
);
1593 /* A != N is equivalent to A - (N + 1) <u -1. */
1594 if (GET_CODE (a
) == NE
1595 && CONST_INT_P (op1
)
1596 && GET_CODE (b
) == LTU
1597 && opb1
== constm1_rtx
1598 && GET_CODE (opb0
) == PLUS
1599 && CONST_INT_P (XEXP (opb0
, 1))
1600 /* Avoid overflows. */
1601 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1602 != ((unsigned HOST_WIDE_INT
)1
1603 << (HOST_BITS_PER_WIDE_INT
- 1)) - 1)
1604 && INTVAL (XEXP (opb0
, 1)) + 1 == -INTVAL (op1
))
1605 return rtx_equal_p (op0
, XEXP (opb0
, 0));
1607 /* Likewise, A != N implies A - N > 0. */
1608 if (GET_CODE (a
) == NE
1609 && CONST_INT_P (op1
))
1611 if (GET_CODE (b
) == GTU
1612 && GET_CODE (opb0
) == PLUS
1613 && opb1
== const0_rtx
1614 && CONST_INT_P (XEXP (opb0
, 1))
1615 /* Avoid overflows. */
1616 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1617 != (HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1)))
1618 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1619 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1620 if (GET_CODE (b
) == GEU
1621 && GET_CODE (opb0
) == PLUS
1622 && opb1
== const1_rtx
1623 && CONST_INT_P (XEXP (opb0
, 1))
1624 /* Avoid overflows. */
1625 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1626 != (HOST_WIDE_INT_1U
<< (HOST_BITS_PER_WIDE_INT
- 1)))
1627 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1628 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1631 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1632 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == GE
)
1633 && CONST_INT_P (op1
)
1634 && ((GET_CODE (a
) == GT
&& op1
== constm1_rtx
)
1635 || INTVAL (op1
) >= 0)
1636 && GET_CODE (b
) == LTU
1637 && CONST_INT_P (opb1
)
1638 && rtx_equal_p (op0
, opb0
))
1639 return INTVAL (opb1
) < 0;
1644 /* Canonicalizes COND so that
1646 (1) Ensure that operands are ordered according to
1647 swap_commutative_operands_p.
1648 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1649 for GE, GEU, and LEU. */
1652 canon_condition (rtx cond
)
1658 code
= GET_CODE (cond
);
1659 op0
= XEXP (cond
, 0);
1660 op1
= XEXP (cond
, 1);
1662 if (swap_commutative_operands_p (op0
, op1
))
1664 code
= swap_condition (code
);
1665 std::swap (op0
, op1
);
1668 mode
= GET_MODE (op0
);
1669 if (mode
== VOIDmode
)
1670 mode
= GET_MODE (op1
);
1671 gcc_assert (mode
!= VOIDmode
);
1673 if (CONST_SCALAR_INT_P (op1
) && GET_MODE_CLASS (mode
) != MODE_CC
)
1675 rtx_mode_t
const_val (op1
, mode
);
1680 if (wi::ne_p (const_val
, wi::max_value (mode
, SIGNED
)))
1683 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1688 if (wi::ne_p (const_val
, wi::min_value (mode
, SIGNED
)))
1691 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1696 if (wi::ne_p (const_val
, -1))
1699 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1704 if (wi::ne_p (const_val
, 0))
1707 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1716 if (op0
!= XEXP (cond
, 0)
1717 || op1
!= XEXP (cond
, 1)
1718 || code
!= GET_CODE (cond
)
1719 || GET_MODE (cond
) != SImode
)
1720 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1725 /* Reverses CONDition; returns NULL if we cannot. */
1728 reversed_condition (rtx cond
)
1730 enum rtx_code reversed
;
1731 reversed
= reversed_comparison_code (cond
, NULL
);
1732 if (reversed
== UNKNOWN
)
1735 return gen_rtx_fmt_ee (reversed
,
1736 GET_MODE (cond
), XEXP (cond
, 0),
1740 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1741 set of altered regs. */
1744 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1746 rtx rev
, reve
, exp
= *expr
;
1748 /* If some register gets altered later, we do not really speak about its
1749 value at the time of comparison. */
1750 if (altered
&& altered_reg_used (cond
, altered
))
1753 if (GET_CODE (cond
) == EQ
1754 && REG_P (XEXP (cond
, 0)) && CONSTANT_P (XEXP (cond
, 1)))
1756 *expr
= simplify_replace_rtx (*expr
, XEXP (cond
, 0), XEXP (cond
, 1));
1760 if (!COMPARISON_P (exp
))
1763 rev
= reversed_condition (cond
);
1764 reve
= reversed_condition (exp
);
1766 cond
= canon_condition (cond
);
1767 exp
= canon_condition (exp
);
1769 rev
= canon_condition (rev
);
1771 reve
= canon_condition (reve
);
1773 if (rtx_equal_p (exp
, cond
))
1775 *expr
= const_true_rtx
;
1779 if (rev
&& rtx_equal_p (exp
, rev
))
1785 if (implies_p (cond
, exp
))
1787 *expr
= const_true_rtx
;
1791 if (reve
&& implies_p (cond
, reve
))
1797 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1799 if (rev
&& implies_p (exp
, rev
))
1805 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1806 if (rev
&& reve
&& implies_p (reve
, rev
))
1808 *expr
= const_true_rtx
;
1812 /* We would like to have some other tests here. TODO. */
1817 /* Use relationship between A and *B to eventually eliminate *B.
1818 OP is the operation we consider. */
1821 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1826 /* If A implies *B, we may replace *B by true. */
1827 if (implies_p (a
, *b
))
1828 *b
= const_true_rtx
;
1832 /* If *B implies A, we may replace *B by false. */
1833 if (implies_p (*b
, a
))
1842 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1843 operation we consider. */
1846 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1850 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1851 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1852 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1853 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1856 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1857 is a list, its elements are assumed to be combined using OP. */
1860 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1862 bool expression_valid
;
1863 rtx head
, tail
, last_valid_expr
;
1864 rtx_expr_list
*cond_list
;
1867 regset altered
, this_altered
;
1873 if (CONSTANT_P (*expr
))
1876 if (GET_CODE (*expr
) == EXPR_LIST
)
1878 head
= XEXP (*expr
, 0);
1879 tail
= XEXP (*expr
, 1);
1881 eliminate_implied_conditions (op
, &head
, tail
);
1886 neutral
= const_true_rtx
;
1891 neutral
= const0_rtx
;
1892 aggr
= const_true_rtx
;
1899 simplify_using_initial_values (loop
, UNKNOWN
, &head
);
1902 XEXP (*expr
, 0) = aggr
;
1903 XEXP (*expr
, 1) = NULL_RTX
;
1906 else if (head
== neutral
)
1909 simplify_using_initial_values (loop
, op
, expr
);
1912 simplify_using_initial_values (loop
, op
, &tail
);
1914 if (tail
&& XEXP (tail
, 0) == aggr
)
1920 XEXP (*expr
, 0) = head
;
1921 XEXP (*expr
, 1) = tail
;
1925 gcc_assert (op
== UNKNOWN
);
1927 replace_single_def_regs (expr
);
1928 if (CONSTANT_P (*expr
))
1931 e
= loop_preheader_edge (loop
);
1932 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1935 altered
= ALLOC_REG_SET (®_obstack
);
1936 this_altered
= ALLOC_REG_SET (®_obstack
);
1938 expression_valid
= true;
1939 last_valid_expr
= *expr
;
1943 insn
= BB_END (e
->src
);
1944 if (any_condjump_p (insn
))
1946 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false, true);
1948 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1949 cond
= reversed_condition (cond
);
1953 simplify_using_condition (cond
, expr
, altered
);
1957 if (CONSTANT_P (*expr
))
1959 for (note
= cond_list
; note
; note
= XEXP (note
, 1))
1961 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
1962 if (CONSTANT_P (*expr
))
1966 cond_list
= alloc_EXPR_LIST (0, cond
, cond_list
);
1970 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
1978 CLEAR_REG_SET (this_altered
);
1979 note_stores (PATTERN (insn
), mark_altered
, this_altered
);
1982 /* Kill all call clobbered registers. */
1984 hard_reg_set_iterator hrsi
;
1985 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call
,
1987 SET_REGNO_REG_SET (this_altered
, i
);
1990 if (suitable_set_for_replacement (insn
, &dest
, &src
))
1992 rtx_expr_list
**pnote
, **pnote_next
;
1994 replace_in_expr (expr
, dest
, src
);
1995 if (CONSTANT_P (*expr
))
1998 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2000 rtx_expr_list
*note
= *pnote
;
2001 rtx old_cond
= XEXP (note
, 0);
2003 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2004 replace_in_expr (&XEXP (note
, 0), dest
, src
);
2006 /* We can no longer use a condition that has been simplified
2007 to a constant, and simplify_using_condition will abort if
2009 if (CONSTANT_P (XEXP (note
, 0)))
2011 *pnote
= *pnote_next
;
2013 free_EXPR_LIST_node (note
);
2015 /* Retry simplifications with this condition if either the
2016 expression or the condition changed. */
2017 else if (old_cond
!= XEXP (note
, 0) || old
!= *expr
)
2018 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
2023 rtx_expr_list
**pnote
, **pnote_next
;
2025 /* If we did not use this insn to make a replacement, any overlap
2026 between stores in this insn and our expression will cause the
2027 expression to become invalid. */
2028 if (altered_reg_used (*expr
, this_altered
))
2031 /* Likewise for the conditions. */
2032 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2034 rtx_expr_list
*note
= *pnote
;
2035 rtx old_cond
= XEXP (note
, 0);
2037 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2038 if (altered_reg_used (old_cond
, this_altered
))
2040 *pnote
= *pnote_next
;
2042 free_EXPR_LIST_node (note
);
2047 if (CONSTANT_P (*expr
))
2050 IOR_REG_SET (altered
, this_altered
);
2052 /* If the expression now contains regs that have been altered, we
2053 can't return it to the caller. However, it is still valid for
2054 further simplification, so keep searching to see if we can
2055 eventually turn it into a constant. */
2056 if (altered_reg_used (*expr
, altered
))
2057 expression_valid
= false;
2058 if (expression_valid
)
2059 last_valid_expr
= *expr
;
2062 if (!single_pred_p (e
->src
)
2063 || single_pred (e
->src
) == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
2065 e
= single_pred_edge (e
->src
);
2069 free_EXPR_LIST_list (&cond_list
);
2070 if (!CONSTANT_P (*expr
))
2071 *expr
= last_valid_expr
;
2072 FREE_REG_SET (altered
);
2073 FREE_REG_SET (this_altered
);
2076 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2077 that IV occurs as left operands of comparison COND and its signedness
2078 is SIGNED_P to DESC. */
2081 shorten_into_mode (struct rtx_iv
*iv
, machine_mode mode
,
2082 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
2084 rtx mmin
, mmax
, cond_over
, cond_under
;
2086 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
2087 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
2089 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
2098 if (cond_under
!= const0_rtx
)
2100 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2101 if (cond_over
!= const0_rtx
)
2102 desc
->noloop_assumptions
=
2103 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
2110 if (cond_over
!= const0_rtx
)
2112 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2113 if (cond_under
!= const0_rtx
)
2114 desc
->noloop_assumptions
=
2115 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
2119 if (cond_over
!= const0_rtx
)
2121 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2122 if (cond_under
!= const0_rtx
)
2124 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2132 iv
->extend
= signed_p
? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
2135 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2136 subregs of the same mode if possible (sometimes it is necessary to add
2137 some assumptions to DESC). */
2140 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
2141 enum rtx_code cond
, struct niter_desc
*desc
)
2143 machine_mode comp_mode
;
2146 /* If the ivs behave specially in the first iteration, or are
2147 added/multiplied after extending, we ignore them. */
2148 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
2150 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
2153 /* If there is some extend, it must match signedness of the comparison. */
2158 if (iv0
->extend
== IV_ZERO_EXTEND
2159 || iv1
->extend
== IV_ZERO_EXTEND
)
2166 if (iv0
->extend
== IV_SIGN_EXTEND
2167 || iv1
->extend
== IV_SIGN_EXTEND
)
2173 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
2174 && iv1
->extend
!= IV_UNKNOWN_EXTEND
2175 && iv0
->extend
!= iv1
->extend
)
2179 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
)
2180 signed_p
= iv0
->extend
== IV_SIGN_EXTEND
;
2181 if (iv1
->extend
!= IV_UNKNOWN_EXTEND
)
2182 signed_p
= iv1
->extend
== IV_SIGN_EXTEND
;
2189 /* Values of both variables should be computed in the same mode. These
2190 might indeed be different, if we have comparison like
2192 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2194 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2195 in different modes. This does not seem impossible to handle, but
2196 it hardly ever occurs in practice.
2198 The only exception is the case when one of operands is invariant.
2199 For example pentium 3 generates comparisons like
2200 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2201 definitely do not want this prevent the optimization. */
2202 comp_mode
= iv0
->extend_mode
;
2203 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
2204 comp_mode
= iv1
->extend_mode
;
2206 if (iv0
->extend_mode
!= comp_mode
)
2208 if (iv0
->mode
!= iv0
->extend_mode
2209 || iv0
->step
!= const0_rtx
)
2212 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2213 comp_mode
, iv0
->base
, iv0
->mode
);
2214 iv0
->extend_mode
= comp_mode
;
2217 if (iv1
->extend_mode
!= comp_mode
)
2219 if (iv1
->mode
!= iv1
->extend_mode
2220 || iv1
->step
!= const0_rtx
)
2223 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2224 comp_mode
, iv1
->base
, iv1
->mode
);
2225 iv1
->extend_mode
= comp_mode
;
2228 /* Check that both ivs belong to a range of a single mode. If one of the
2229 operands is an invariant, we may need to shorten it into the common
2231 if (iv0
->mode
== iv0
->extend_mode
2232 && iv0
->step
== const0_rtx
2233 && iv0
->mode
!= iv1
->mode
)
2234 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
2236 if (iv1
->mode
== iv1
->extend_mode
2237 && iv1
->step
== const0_rtx
2238 && iv0
->mode
!= iv1
->mode
)
2239 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
2241 if (iv0
->mode
!= iv1
->mode
)
2244 desc
->mode
= iv0
->mode
;
2245 desc
->signed_p
= signed_p
;
2250 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2251 result. This function is called from iv_number_of_iterations with
2252 a number of fields in DESC already filled in. OLD_NITER is the original
2253 expression for the number of iterations, before we tried to simplify it. */
2256 determine_max_iter (struct loop
*loop
, struct niter_desc
*desc
, rtx old_niter
)
2258 rtx niter
= desc
->niter_expr
;
2259 rtx mmin
, mmax
, cmp
;
2261 uint64_t andmax
= 0;
2263 /* We used to look for constant operand 0 of AND,
2264 but canonicalization should always make this impossible. */
2265 gcc_checking_assert (GET_CODE (niter
) != AND
2266 || !CONST_INT_P (XEXP (niter
, 0)));
2268 if (GET_CODE (niter
) == AND
2269 && CONST_INT_P (XEXP (niter
, 1)))
2271 andmax
= UINTVAL (XEXP (niter
, 1));
2272 niter
= XEXP (niter
, 0);
2275 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
2276 nmax
= UINTVAL (mmax
) - UINTVAL (mmin
);
2278 if (GET_CODE (niter
) == UDIV
)
2280 if (!CONST_INT_P (XEXP (niter
, 1)))
2282 inc
= INTVAL (XEXP (niter
, 1));
2283 niter
= XEXP (niter
, 0);
2288 /* We could use a binary search here, but for now improving the upper
2289 bound by just one eliminates one important corner case. */
2290 cmp
= simplify_gen_relational (desc
->signed_p
? LT
: LTU
, VOIDmode
,
2291 desc
->mode
, old_niter
, mmax
);
2292 simplify_using_initial_values (loop
, UNKNOWN
, &cmp
);
2293 if (cmp
== const_true_rtx
)
2298 fprintf (dump_file
, ";; improved upper bound by one.\n");
2302 nmax
= MIN (nmax
, andmax
);
2304 fprintf (dump_file
, ";; Determined upper bound %" PRId64
".\n",
2309 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2310 the result into DESC. Very similar to determine_number_of_iterations
2311 (basically its rtl version), complicated by things like subregs. */
2314 iv_number_of_iterations (struct loop
*loop
, rtx_insn
*insn
, rtx condition
,
2315 struct niter_desc
*desc
)
2317 rtx op0
, op1
, delta
, step
, bound
, may_xform
, tmp
, tmp0
, tmp1
;
2318 struct rtx_iv iv0
, iv1
;
2319 rtx assumption
, may_not_xform
;
2321 machine_mode mode
, comp_mode
;
2322 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
2323 uint64_t s
, size
, d
, inv
, max
, up
, down
;
2324 int64_t inc
, step_val
;
2325 int was_sharp
= false;
2329 /* The meaning of these assumptions is this:
2331 then the rest of information does not have to be valid
2332 if noloop_assumptions then the loop does not roll
2333 if infinite then this exit is never used */
2335 desc
->assumptions
= NULL_RTX
;
2336 desc
->noloop_assumptions
= NULL_RTX
;
2337 desc
->infinite
= NULL_RTX
;
2338 desc
->simple_p
= true;
2340 desc
->const_iter
= false;
2341 desc
->niter_expr
= NULL_RTX
;
2343 cond
= GET_CODE (condition
);
2344 gcc_assert (COMPARISON_P (condition
));
2346 mode
= GET_MODE (XEXP (condition
, 0));
2347 if (mode
== VOIDmode
)
2348 mode
= GET_MODE (XEXP (condition
, 1));
2349 /* The constant comparisons should be folded. */
2350 gcc_assert (mode
!= VOIDmode
);
2352 /* We only handle integers or pointers. */
2353 if (GET_MODE_CLASS (mode
) != MODE_INT
2354 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
2357 op0
= XEXP (condition
, 0);
2358 if (!iv_analyze (insn
, op0
, &iv0
))
2360 if (iv0
.extend_mode
== VOIDmode
)
2361 iv0
.mode
= iv0
.extend_mode
= mode
;
2363 op1
= XEXP (condition
, 1);
2364 if (!iv_analyze (insn
, op1
, &iv1
))
2366 if (iv1
.extend_mode
== VOIDmode
)
2367 iv1
.mode
= iv1
.extend_mode
= mode
;
2369 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
2370 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
2373 /* Check condition and normalize it. */
2381 std::swap (iv0
, iv1
);
2382 cond
= swap_condition (cond
);
2394 /* Handle extends. This is relatively nontrivial, so we only try in some
2395 easy cases, when we can canonicalize the ivs (possibly by adding some
2396 assumptions) to shape subreg (base + i * step). This function also fills
2397 in desc->mode and desc->signed_p. */
2399 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2402 comp_mode
= iv0
.extend_mode
;
2404 size
= GET_MODE_PRECISION (mode
);
2405 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2406 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2407 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2409 if (!CONST_INT_P (iv0
.step
) || !CONST_INT_P (iv1
.step
))
2412 /* We can take care of the case of two induction variables chasing each other
2413 if the test is NE. I have never seen a loop using it, but still it is
2415 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2420 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2421 iv1
.step
= const0_rtx
;
2424 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2425 iv1
.step
= lowpart_subreg (mode
, iv1
.step
, comp_mode
);
2427 /* This is either infinite loop or the one that ends immediately, depending
2428 on initial values. Unswitching should remove this kind of conditions. */
2429 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2434 if (iv0
.step
== const0_rtx
)
2435 step_val
= -INTVAL (iv1
.step
);
2437 step_val
= INTVAL (iv0
.step
);
2439 /* Ignore loops of while (i-- < 10) type. */
2443 step_is_pow2
= !(step_val
& (step_val
- 1));
2447 /* We do not care about whether the step is power of two in this
2449 step_is_pow2
= false;
2453 /* Some more condition normalization. We must record some assumptions
2454 due to overflows. */
2459 /* We want to take care only of non-sharp relationals; this is easy,
2460 as in cases the overflow would make the transformation unsafe
2461 the loop does not roll. Seemingly it would make more sense to want
2462 to take care of sharp relationals instead, as NE is more similar to
2463 them, but the problem is that here the transformation would be more
2464 difficult due to possibly infinite loops. */
2465 if (iv0
.step
== const0_rtx
)
2467 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2468 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2470 if (assumption
== const_true_rtx
)
2471 goto zero_iter_simplify
;
2472 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2473 iv0
.base
, const1_rtx
);
2477 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2478 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2480 if (assumption
== const_true_rtx
)
2481 goto zero_iter_simplify
;
2482 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2483 iv1
.base
, constm1_rtx
);
2486 if (assumption
!= const0_rtx
)
2487 desc
->noloop_assumptions
=
2488 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2489 cond
= (cond
== LT
) ? LE
: LEU
;
2491 /* It will be useful to be able to tell the difference once more in
2492 LE -> NE reduction. */
2498 /* Take care of trivially infinite loops. */
2501 if (iv0
.step
== const0_rtx
)
2503 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2504 if (rtx_equal_p (tmp
, mode_mmin
))
2507 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2508 /* Fill in the remaining fields somehow. */
2509 goto zero_iter_simplify
;
2514 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2515 if (rtx_equal_p (tmp
, mode_mmax
))
2518 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2519 /* Fill in the remaining fields somehow. */
2520 goto zero_iter_simplify
;
2525 /* If we can we want to take care of NE conditions instead of size
2526 comparisons, as they are much more friendly (most importantly
2527 this takes care of special handling of loops with step 1). We can
2528 do it if we first check that upper bound is greater or equal to
2529 lower bound, their difference is constant c modulo step and that
2530 there is not an overflow. */
2533 if (iv0
.step
== const0_rtx
)
2534 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2537 step
= lowpart_subreg (mode
, step
, comp_mode
);
2538 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2539 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2540 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2541 may_xform
= const0_rtx
;
2542 may_not_xform
= const_true_rtx
;
2544 if (CONST_INT_P (delta
))
2546 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2548 /* A special case. We have transformed condition of type
2549 for (i = 0; i < 4; i += 4)
2551 for (i = 0; i <= 3; i += 4)
2552 obviously if the test for overflow during that transformation
2553 passed, we cannot overflow here. Most importantly any
2554 loop with sharp end condition and step 1 falls into this
2555 category, so handling this case specially is definitely
2556 worth the troubles. */
2557 may_xform
= const_true_rtx
;
2559 else if (iv0
.step
== const0_rtx
)
2561 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2562 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2563 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2564 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2565 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2567 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2573 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2574 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2575 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2576 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2577 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2579 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2585 if (may_xform
!= const0_rtx
)
2587 /* We perform the transformation always provided that it is not
2588 completely senseless. This is OK, as we would need this assumption
2589 to determine the number of iterations anyway. */
2590 if (may_xform
!= const_true_rtx
)
2592 /* If the step is a power of two and the final value we have
2593 computed overflows, the cycle is infinite. Otherwise it
2594 is nontrivial to compute the number of iterations. */
2596 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2599 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2603 /* We are going to lose some information about upper bound on
2604 number of iterations in this step, so record the information
2606 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2607 if (CONST_INT_P (iv1
.base
))
2608 up
= INTVAL (iv1
.base
);
2610 up
= INTVAL (mode_mmax
) - inc
;
2611 down
= INTVAL (CONST_INT_P (iv0
.base
)
2614 max
= (up
- down
) / inc
+ 1;
2616 && !desc
->assumptions
)
2617 record_niter_bound (loop
, max
, false, true);
2619 if (iv0
.step
== const0_rtx
)
2621 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2622 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2626 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2627 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2630 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2631 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2632 assumption
= simplify_gen_relational (reverse_condition (cond
),
2633 SImode
, mode
, tmp0
, tmp1
);
2634 if (assumption
== const_true_rtx
)
2635 goto zero_iter_simplify
;
2636 else if (assumption
!= const0_rtx
)
2637 desc
->noloop_assumptions
=
2638 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2643 /* Count the number of iterations. */
2646 /* Everything we do here is just arithmetics modulo size of mode. This
2647 makes us able to do more involved computations of number of iterations
2648 than in other cases. First transform the condition into shape
2649 s * i <> c, with s positive. */
2650 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2651 iv0
.base
= const0_rtx
;
2652 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2653 iv1
.step
= const0_rtx
;
2654 if (INTVAL (iv0
.step
) < 0)
2656 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, comp_mode
);
2657 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, comp_mode
);
2659 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2661 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2662 is infinite. Otherwise, the number of iterations is
2663 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2664 s
= INTVAL (iv0
.step
); d
= 1;
2671 bound
= GEN_INT (((uint64_t) 1 << (size
- 1 ) << 1) - 1);
2673 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2674 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, gen_int_mode (d
, mode
));
2675 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2676 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2678 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, gen_int_mode (d
, mode
));
2679 inv
= inverse (s
, size
);
2680 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, gen_int_mode (inv
, mode
));
2681 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2685 if (iv1
.step
== const0_rtx
)
2686 /* Condition in shape a + s * i <= b
2687 We must know that b + s does not overflow and a <= b + s and then we
2688 can compute number of iterations as (b + s - a) / s. (It might
2689 seem that we in fact could be more clever about testing the b + s
2690 overflow condition using some information about b - a mod s,
2691 but it was already taken into account during LE -> NE transform). */
2694 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2695 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2697 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2698 lowpart_subreg (mode
, step
,
2704 /* If s is power of 2, we know that the loop is infinite if
2705 a % s <= b % s and b + s overflows. */
2706 assumption
= simplify_gen_relational (reverse_condition (cond
),
2710 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2711 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2712 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2713 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2715 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2719 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2722 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2725 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, iv0
.step
);
2726 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2727 assumption
= simplify_gen_relational (reverse_condition (cond
),
2728 SImode
, mode
, tmp0
, tmp
);
2730 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2731 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2735 /* Condition in shape a <= b - s * i
2736 We must know that a - s does not overflow and a - s <= b and then
2737 we can again compute number of iterations as (b - (a - s)) / s. */
2738 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2739 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2740 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2742 bound
= simplify_gen_binary (PLUS
, mode
, mode_mmin
,
2743 lowpart_subreg (mode
, step
, comp_mode
));
2748 /* If s is power of 2, we know that the loop is infinite if
2749 a % s <= b % s and a - s overflows. */
2750 assumption
= simplify_gen_relational (reverse_condition (cond
),
2754 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2755 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2756 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2757 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2759 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2763 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2766 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2769 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2770 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2771 assumption
= simplify_gen_relational (reverse_condition (cond
),
2774 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2775 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2777 if (assumption
== const_true_rtx
)
2778 goto zero_iter_simplify
;
2779 else if (assumption
!= const0_rtx
)
2780 desc
->noloop_assumptions
=
2781 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2782 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2783 desc
->niter_expr
= delta
;
2786 old_niter
= desc
->niter_expr
;
2788 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2789 if (desc
->assumptions
2790 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2792 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2793 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2794 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2796 /* Rerun the simplification. Consider code (created by copying loop headers)
2808 The first pass determines that i = 0, the second pass uses it to eliminate
2809 noloop assumption. */
2811 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2812 if (desc
->assumptions
2813 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2815 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2816 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2817 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2819 if (desc
->noloop_assumptions
2820 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2823 if (CONST_INT_P (desc
->niter_expr
))
2825 uint64_t val
= INTVAL (desc
->niter_expr
);
2827 desc
->const_iter
= true;
2828 desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2830 && !desc
->assumptions
)
2831 record_niter_bound (loop
, desc
->niter
, false, true);
2835 max
= determine_max_iter (loop
, desc
, old_niter
);
2837 goto zero_iter_simplify
;
2839 && !desc
->assumptions
)
2840 record_niter_bound (loop
, max
, false, true);
2842 /* simplify_using_initial_values does a copy propagation on the registers
2843 in the expression for the number of iterations. This prolongs life
2844 ranges of registers and increases register pressure, and usually
2845 brings no gain (and if it happens to do, the cse pass will take care
2846 of it anyway). So prevent this behavior, unless it enabled us to
2847 derive that the number of iterations is a constant. */
2848 desc
->niter_expr
= old_niter
;
2854 /* Simplify the assumptions. */
2855 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2856 if (desc
->assumptions
2857 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2859 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2863 desc
->const_iter
= true;
2865 record_niter_bound (loop
, 0, true, true);
2866 desc
->noloop_assumptions
= NULL_RTX
;
2867 desc
->niter_expr
= const0_rtx
;
2871 desc
->simple_p
= false;
2875 /* Checks whether E is a simple exit from LOOP and stores its description
2879 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2881 basic_block exit_bb
;
2887 desc
->simple_p
= false;
2889 /* It must belong directly to the loop. */
2890 if (exit_bb
->loop_father
!= loop
)
2893 /* It must be tested (at least) once during any iteration. */
2894 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2897 /* It must end in a simple conditional jump. */
2898 if (!any_condjump_p (BB_END (exit_bb
)))
2901 ein
= EDGE_SUCC (exit_bb
, 0);
2903 ein
= EDGE_SUCC (exit_bb
, 1);
2906 desc
->in_edge
= ein
;
2908 /* Test whether the condition is suitable. */
2909 if (!(condition
= get_condition (BB_END (ein
->src
), &at
, false, false)))
2912 if (ein
->flags
& EDGE_FALLTHRU
)
2914 condition
= reversed_condition (condition
);
2919 /* Check that we are able to determine number of iterations and fill
2920 in information about it. */
2921 iv_number_of_iterations (loop
, at
, condition
, desc
);
2924 /* Finds a simple exit of LOOP and stores its description into DESC. */
2927 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2932 struct niter_desc act
;
2936 desc
->simple_p
= false;
2937 body
= get_loop_body (loop
);
2939 for (i
= 0; i
< loop
->num_nodes
; i
++)
2941 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
2943 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2946 check_simple_exit (loop
, e
, &act
);
2954 /* Prefer constant iterations; the less the better. */
2956 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
2959 /* Also if the actual exit may be infinite, while the old one
2960 not, prefer the old one. */
2961 if (act
.infinite
&& !desc
->infinite
)
2973 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
2974 fprintf (dump_file
, " simple exit %d -> %d\n",
2975 desc
->out_edge
->src
->index
,
2976 desc
->out_edge
->dest
->index
);
2977 if (desc
->assumptions
)
2979 fprintf (dump_file
, " assumptions: ");
2980 print_rtl (dump_file
, desc
->assumptions
);
2981 fprintf (dump_file
, "\n");
2983 if (desc
->noloop_assumptions
)
2985 fprintf (dump_file
, " does not roll if: ");
2986 print_rtl (dump_file
, desc
->noloop_assumptions
);
2987 fprintf (dump_file
, "\n");
2991 fprintf (dump_file
, " infinite if: ");
2992 print_rtl (dump_file
, desc
->infinite
);
2993 fprintf (dump_file
, "\n");
2996 fprintf (dump_file
, " number of iterations: ");
2997 print_rtl (dump_file
, desc
->niter_expr
);
2998 fprintf (dump_file
, "\n");
3000 fprintf (dump_file
, " upper bound: %li\n",
3001 (long)get_max_loop_iterations_int (loop
));
3002 fprintf (dump_file
, " likely upper bound: %li\n",
3003 (long)get_likely_max_loop_iterations_int (loop
));
3004 fprintf (dump_file
, " realistic bound: %li\n",
3005 (long)get_estimated_loop_iterations_int (loop
));
3008 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
3014 /* Creates a simple loop description of LOOP if it was not computed
3018 get_simple_loop_desc (struct loop
*loop
)
3020 struct niter_desc
*desc
= simple_loop_desc (loop
);
3025 /* At least desc->infinite is not always initialized by
3026 find_simple_loop_exit. */
3027 desc
= ggc_cleared_alloc
<niter_desc
> ();
3028 iv_analysis_loop_init (loop
);
3029 find_simple_exit (loop
, desc
);
3030 loop
->simple_loop_desc
= desc
;
3034 /* Releases simple loop description for LOOP. */
3037 free_simple_loop_desc (struct loop
*loop
)
3039 struct niter_desc
*desc
= simple_loop_desc (loop
);
3045 loop
->simple_loop_desc
= NULL
;