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"
64 #include "dominance.h"
66 #include "basic-block.h"
70 #include "statistics.h"
71 #include "double-int.h"
73 #include "fixed-value.h"
78 #include "insn-config.h"
88 #include "diagnostic-core.h"
90 #include "hash-table.h"
94 /* Possible return values of iv_get_reaching_def. */
98 /* More than one reaching def, or reaching def that does not
102 /* The use is trivial invariant of the loop, i.e. is not changed
106 /* The use is reached by initial value and a value from the
107 previous iteration. */
110 /* The use has single dominating def. */
114 /* Information about a biv. */
118 unsigned regno
; /* The register of the biv. */
119 struct rtx_iv iv
; /* Value of the biv. */
122 static bool clean_slate
= true;
124 static unsigned int iv_ref_table_size
= 0;
126 /* Table of rtx_ivs indexed by the df_ref uid field. */
127 static struct rtx_iv
** iv_ref_table
;
129 /* Induction variable stored at the reference. */
130 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
131 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
133 /* The current loop. */
135 static struct loop
*current_loop
;
137 /* Hashtable helper. */
139 struct biv_entry_hasher
: typed_free_remove
<biv_entry
>
141 typedef biv_entry
*value_type
;
142 typedef rtx_def
*compare_type
;
143 static inline hashval_t
hash (const biv_entry
*);
144 static inline bool equal (const biv_entry
*, const rtx_def
*);
147 /* Returns hash value for biv B. */
150 biv_entry_hasher::hash (const biv_entry
*b
)
155 /* Compares biv B and register R. */
158 biv_entry_hasher::equal (const biv_entry
*b
, const rtx_def
*r
)
160 return b
->regno
== REGNO (r
);
163 /* Bivs of the current loop. */
165 static hash_table
<biv_entry_hasher
> *bivs
;
167 static bool iv_analyze_op (rtx_insn
*, rtx
, struct rtx_iv
*);
169 /* Return the RTX code corresponding to the IV extend code EXTEND. */
170 static inline enum rtx_code
171 iv_extend_to_rtx_code (enum iv_extend_code extend
)
179 case IV_UNKNOWN_EXTEND
:
185 /* Dumps information about IV to FILE. */
187 extern void dump_iv_info (FILE *, struct rtx_iv
*);
189 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
193 fprintf (file
, "not simple");
197 if (iv
->step
== const0_rtx
198 && !iv
->first_special
)
199 fprintf (file
, "invariant ");
201 print_rtl (file
, iv
->base
);
202 if (iv
->step
!= const0_rtx
)
204 fprintf (file
, " + ");
205 print_rtl (file
, iv
->step
);
206 fprintf (file
, " * iteration");
208 fprintf (file
, " (in %s)", GET_MODE_NAME (iv
->mode
));
210 if (iv
->mode
!= iv
->extend_mode
)
211 fprintf (file
, " %s to %s",
212 rtx_name
[iv_extend_to_rtx_code (iv
->extend
)],
213 GET_MODE_NAME (iv
->extend_mode
));
215 if (iv
->mult
!= const1_rtx
)
217 fprintf (file
, " * ");
218 print_rtl (file
, iv
->mult
);
220 if (iv
->delta
!= const0_rtx
)
222 fprintf (file
, " + ");
223 print_rtl (file
, iv
->delta
);
225 if (iv
->first_special
)
226 fprintf (file
, " (first special)");
229 /* Generates a subreg to get the least significant part of EXPR (in mode
230 INNER_MODE) to OUTER_MODE. */
233 lowpart_subreg (machine_mode outer_mode
, rtx expr
,
234 machine_mode inner_mode
)
236 return simplify_gen_subreg (outer_mode
, expr
, inner_mode
,
237 subreg_lowpart_offset (outer_mode
, inner_mode
));
241 check_iv_ref_table_size (void)
243 if (iv_ref_table_size
< DF_DEFS_TABLE_SIZE ())
245 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
246 iv_ref_table
= XRESIZEVEC (struct rtx_iv
*, iv_ref_table
, new_size
);
247 memset (&iv_ref_table
[iv_ref_table_size
], 0,
248 (new_size
- iv_ref_table_size
) * sizeof (struct rtx_iv
*));
249 iv_ref_table_size
= new_size
;
254 /* Checks whether REG is a well-behaved register. */
257 simple_reg_p (rtx reg
)
261 if (GET_CODE (reg
) == SUBREG
)
263 if (!subreg_lowpart_p (reg
))
265 reg
= SUBREG_REG (reg
);
272 if (HARD_REGISTER_NUM_P (r
))
275 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
281 /* Clears the information about ivs stored in df. */
286 unsigned i
, n_defs
= DF_DEFS_TABLE_SIZE ();
289 check_iv_ref_table_size ();
290 for (i
= 0; i
< n_defs
; i
++)
292 iv
= iv_ref_table
[i
];
296 iv_ref_table
[i
] = NULL
;
304 /* Prepare the data for an induction variable analysis of a LOOP. */
307 iv_analysis_loop_init (struct loop
*loop
)
311 /* Clear the information from the analysis of the previous loop. */
314 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
315 bivs
= new hash_table
<biv_entry_hasher
> (10);
321 /* Get rid of the ud chains before processing the rescans. Then add
323 df_remove_problem (df_chain
);
324 df_process_deferred_rescans ();
325 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
326 df_chain_add_problem (DF_UD_CHAIN
);
327 df_note_add_problem ();
328 df_analyze_loop (loop
);
330 df_dump_region (dump_file
);
332 check_iv_ref_table_size ();
335 /* Finds the definition of REG that dominates loop latch and stores
336 it to DEF. Returns false if there is not a single definition
337 dominating the latch. If REG has no definition in loop, DEF
338 is set to NULL and true is returned. */
341 latch_dominating_def (rtx reg
, df_ref
*def
)
343 df_ref single_rd
= NULL
, adef
;
344 unsigned regno
= REGNO (reg
);
345 struct df_rd_bb_info
*bb_info
= DF_RD_BB_INFO (current_loop
->latch
);
347 for (adef
= DF_REG_DEF_CHAIN (regno
); adef
; adef
= DF_REF_NEXT_REG (adef
))
349 if (!bitmap_bit_p (df
->blocks_to_analyze
, DF_REF_BBNO (adef
))
350 || !bitmap_bit_p (&bb_info
->out
, DF_REF_ID (adef
)))
353 /* More than one reaching definition. */
357 if (!just_once_each_iteration_p (current_loop
, DF_REF_BB (adef
)))
367 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
369 static enum iv_grd_result
370 iv_get_reaching_def (rtx_insn
*insn
, rtx reg
, df_ref
*def
)
373 basic_block def_bb
, use_bb
;
378 if (!simple_reg_p (reg
))
380 if (GET_CODE (reg
) == SUBREG
)
381 reg
= SUBREG_REG (reg
);
382 gcc_assert (REG_P (reg
));
384 use
= df_find_use (insn
, reg
);
385 gcc_assert (use
!= NULL
);
387 if (!DF_REF_CHAIN (use
))
388 return GRD_INVARIANT
;
390 /* More than one reaching def. */
391 if (DF_REF_CHAIN (use
)->next
)
394 adef
= DF_REF_CHAIN (use
)->ref
;
396 /* We do not handle setting only part of the register. */
397 if (DF_REF_FLAGS (adef
) & DF_REF_READ_WRITE
)
400 def_insn
= DF_REF_INSN (adef
);
401 def_bb
= DF_REF_BB (adef
);
402 use_bb
= BLOCK_FOR_INSN (insn
);
404 if (use_bb
== def_bb
)
405 dom_p
= (DF_INSN_LUID (def_insn
) < DF_INSN_LUID (insn
));
407 dom_p
= dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
);
412 return GRD_SINGLE_DOM
;
415 /* The definition does not dominate the use. This is still OK if
416 this may be a use of a biv, i.e. if the def_bb dominates loop
418 if (just_once_each_iteration_p (current_loop
, def_bb
))
419 return GRD_MAYBE_BIV
;
424 /* Sets IV to invariant CST in MODE. Always returns true (just for
425 consistency with other iv manipulation functions that may fail). */
428 iv_constant (struct rtx_iv
*iv
, rtx cst
, machine_mode mode
)
430 if (mode
== VOIDmode
)
431 mode
= GET_MODE (cst
);
435 iv
->step
= const0_rtx
;
436 iv
->first_special
= false;
437 iv
->extend
= IV_UNKNOWN_EXTEND
;
438 iv
->extend_mode
= iv
->mode
;
439 iv
->delta
= const0_rtx
;
440 iv
->mult
= const1_rtx
;
445 /* Evaluates application of subreg to MODE on IV. */
448 iv_subreg (struct rtx_iv
*iv
, 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 val
= lowpart_subreg (mode
, val
,
456 iv
->extend
== IV_UNKNOWN_EXTEND
457 ? iv
->mode
: iv
->extend_mode
);
460 iv
->extend
= IV_UNKNOWN_EXTEND
;
461 iv
->mode
= iv
->extend_mode
= mode
;
462 iv
->delta
= const0_rtx
;
463 iv
->mult
= const1_rtx
;
467 if (iv
->extend_mode
== mode
)
470 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
473 iv
->extend
= IV_UNKNOWN_EXTEND
;
476 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
477 simplify_gen_binary (MULT
, iv
->extend_mode
,
478 iv
->base
, iv
->mult
));
479 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
480 iv
->mult
= const1_rtx
;
481 iv
->delta
= const0_rtx
;
482 iv
->first_special
= false;
487 /* Evaluates application of EXTEND to MODE on IV. */
490 iv_extend (struct rtx_iv
*iv
, enum iv_extend_code extend
, machine_mode mode
)
492 /* If iv is invariant, just calculate the new value. */
493 if (iv
->step
== const0_rtx
494 && !iv
->first_special
)
496 rtx val
= get_iv_value (iv
, const0_rtx
);
497 if (iv
->extend_mode
!= iv
->mode
498 && iv
->extend
!= IV_UNKNOWN_EXTEND
499 && iv
->extend
!= extend
)
500 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
501 val
= simplify_gen_unary (iv_extend_to_rtx_code (extend
), mode
,
504 ? iv
->extend_mode
: iv
->mode
);
506 iv
->extend
= IV_UNKNOWN_EXTEND
;
507 iv
->mode
= iv
->extend_mode
= mode
;
508 iv
->delta
= const0_rtx
;
509 iv
->mult
= const1_rtx
;
513 if (mode
!= iv
->extend_mode
)
516 if (iv
->extend
!= IV_UNKNOWN_EXTEND
517 && iv
->extend
!= extend
)
525 /* Evaluates negation of IV. */
528 iv_neg (struct rtx_iv
*iv
)
530 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
532 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
533 iv
->base
, iv
->extend_mode
);
534 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
535 iv
->step
, iv
->extend_mode
);
539 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
540 iv
->delta
, iv
->extend_mode
);
541 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
542 iv
->mult
, iv
->extend_mode
);
548 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
551 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
556 /* Extend the constant to extend_mode of the other operand if necessary. */
557 if (iv0
->extend
== IV_UNKNOWN_EXTEND
558 && iv0
->mode
== iv0
->extend_mode
559 && iv0
->step
== const0_rtx
560 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
562 iv0
->extend_mode
= iv1
->extend_mode
;
563 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
564 iv0
->base
, iv0
->mode
);
566 if (iv1
->extend
== IV_UNKNOWN_EXTEND
567 && iv1
->mode
== iv1
->extend_mode
568 && iv1
->step
== const0_rtx
569 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
571 iv1
->extend_mode
= iv0
->extend_mode
;
572 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
573 iv1
->base
, iv1
->mode
);
576 mode
= iv0
->extend_mode
;
577 if (mode
!= iv1
->extend_mode
)
580 if (iv0
->extend
== IV_UNKNOWN_EXTEND
581 && iv1
->extend
== IV_UNKNOWN_EXTEND
)
583 if (iv0
->mode
!= iv1
->mode
)
586 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
587 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
592 /* Handle addition of constant. */
593 if (iv1
->extend
== IV_UNKNOWN_EXTEND
595 && iv1
->step
== const0_rtx
)
597 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
601 if (iv0
->extend
== IV_UNKNOWN_EXTEND
603 && iv0
->step
== const0_rtx
)
611 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
618 /* Evaluates multiplication of IV by constant CST. */
621 iv_mult (struct rtx_iv
*iv
, rtx mby
)
623 machine_mode mode
= iv
->extend_mode
;
625 if (GET_MODE (mby
) != VOIDmode
626 && GET_MODE (mby
) != mode
)
629 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
631 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
632 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
636 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
637 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
643 /* Evaluates shift of IV by constant CST. */
646 iv_shift (struct rtx_iv
*iv
, rtx mby
)
648 machine_mode mode
= iv
->extend_mode
;
650 if (GET_MODE (mby
) != VOIDmode
651 && GET_MODE (mby
) != mode
)
654 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
656 iv
->base
= simplify_gen_binary (ASHIFT
, mode
, iv
->base
, mby
);
657 iv
->step
= simplify_gen_binary (ASHIFT
, mode
, iv
->step
, mby
);
661 iv
->delta
= simplify_gen_binary (ASHIFT
, mode
, iv
->delta
, mby
);
662 iv
->mult
= simplify_gen_binary (ASHIFT
, mode
, iv
->mult
, mby
);
668 /* The recursive part of get_biv_step. Gets the value of the single value
669 defined by DEF wrto initial value of REG inside loop, in shape described
673 get_biv_step_1 (df_ref def
, rtx reg
,
674 rtx
*inner_step
, machine_mode
*inner_mode
,
675 enum iv_extend_code
*extend
, machine_mode outer_mode
,
678 rtx set
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
679 rtx next
, nextr
, tmp
;
681 rtx_insn
*insn
= DF_REF_INSN (def
);
683 enum iv_grd_result res
;
685 set
= single_set (insn
);
689 rhs
= find_reg_equal_equiv_note (insn
);
695 code
= GET_CODE (rhs
);
708 if (code
== PLUS
&& CONSTANT_P (op0
))
710 tmp
= op0
; op0
= op1
; op1
= tmp
;
713 if (!simple_reg_p (op0
)
714 || !CONSTANT_P (op1
))
717 if (GET_MODE (rhs
) != outer_mode
)
719 /* ppc64 uses expressions like
721 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
723 this is equivalent to
725 (set x':DI (plus:DI y:DI 1))
726 (set x:SI (subreg:SI (x':DI)). */
727 if (GET_CODE (op0
) != SUBREG
)
729 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
738 if (GET_MODE (rhs
) != outer_mode
)
742 if (!simple_reg_p (op0
))
752 if (GET_CODE (next
) == SUBREG
)
754 if (!subreg_lowpart_p (next
))
757 nextr
= SUBREG_REG (next
);
758 if (GET_MODE (nextr
) != outer_mode
)
764 res
= iv_get_reaching_def (insn
, nextr
, &next_def
);
766 if (res
== GRD_INVALID
|| res
== GRD_INVARIANT
)
769 if (res
== GRD_MAYBE_BIV
)
771 if (!rtx_equal_p (nextr
, reg
))
774 *inner_step
= const0_rtx
;
775 *extend
= IV_UNKNOWN_EXTEND
;
776 *inner_mode
= outer_mode
;
777 *outer_step
= const0_rtx
;
779 else if (!get_biv_step_1 (next_def
, reg
,
780 inner_step
, inner_mode
, extend
, outer_mode
,
784 if (GET_CODE (next
) == SUBREG
)
786 machine_mode amode
= GET_MODE (next
);
788 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
792 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
793 *inner_step
, *outer_step
);
794 *outer_step
= const0_rtx
;
795 *extend
= IV_UNKNOWN_EXTEND
;
806 if (*inner_mode
== outer_mode
807 /* See comment in previous switch. */
808 || GET_MODE (rhs
) != outer_mode
)
809 *inner_step
= simplify_gen_binary (code
, outer_mode
,
812 *outer_step
= simplify_gen_binary (code
, outer_mode
,
818 gcc_assert (GET_MODE (op0
) == *inner_mode
819 && *extend
== IV_UNKNOWN_EXTEND
820 && *outer_step
== const0_rtx
);
822 *extend
= (code
== SIGN_EXTEND
) ? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
832 /* Gets the operation on register REG inside loop, in shape
834 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
836 If the operation cannot be described in this shape, return false.
837 LAST_DEF is the definition of REG that dominates loop latch. */
840 get_biv_step (df_ref last_def
, rtx reg
, rtx
*inner_step
,
841 machine_mode
*inner_mode
, enum iv_extend_code
*extend
,
842 machine_mode
*outer_mode
, rtx
*outer_step
)
844 *outer_mode
= GET_MODE (reg
);
846 if (!get_biv_step_1 (last_def
, reg
,
847 inner_step
, inner_mode
, extend
, *outer_mode
,
851 gcc_assert ((*inner_mode
== *outer_mode
) != (*extend
!= IV_UNKNOWN_EXTEND
));
852 gcc_assert (*inner_mode
!= *outer_mode
|| *outer_step
== const0_rtx
);
857 /* Records information that DEF is induction variable IV. */
860 record_iv (df_ref def
, struct rtx_iv
*iv
)
862 struct rtx_iv
*recorded_iv
= XNEW (struct rtx_iv
);
865 check_iv_ref_table_size ();
866 DF_REF_IV_SET (def
, recorded_iv
);
869 /* If DEF was already analyzed for bivness, store the description of the biv to
870 IV and return true. Otherwise return false. */
873 analyzed_for_bivness_p (rtx def
, struct rtx_iv
*iv
)
875 struct biv_entry
*biv
= bivs
->find_with_hash (def
, REGNO (def
));
885 record_biv (rtx def
, struct rtx_iv
*iv
)
887 struct biv_entry
*biv
= XNEW (struct biv_entry
);
888 biv_entry
**slot
= bivs
->find_slot_with_hash (def
, REGNO (def
), INSERT
);
890 biv
->regno
= REGNO (def
);
896 /* Determines whether DEF is a biv and if so, stores its description
900 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
902 rtx inner_step
, outer_step
;
903 machine_mode inner_mode
, outer_mode
;
904 enum iv_extend_code extend
;
909 fprintf (dump_file
, "Analyzing ");
910 print_rtl (dump_file
, def
);
911 fprintf (dump_file
, " for bivness.\n");
916 if (!CONSTANT_P (def
))
919 return iv_constant (iv
, def
, VOIDmode
);
922 if (!latch_dominating_def (def
, &last_def
))
925 fprintf (dump_file
, " not simple.\n");
930 return iv_constant (iv
, def
, VOIDmode
);
932 if (analyzed_for_bivness_p (def
, iv
))
935 fprintf (dump_file
, " already analysed.\n");
936 return iv
->base
!= NULL_RTX
;
939 if (!get_biv_step (last_def
, def
, &inner_step
, &inner_mode
, &extend
,
940 &outer_mode
, &outer_step
))
946 /* Loop transforms base to es (base + inner_step) + outer_step,
947 where es means extend of subreg between inner_mode and outer_mode.
948 The corresponding induction variable is
950 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
952 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
953 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
954 iv
->mode
= inner_mode
;
955 iv
->extend_mode
= outer_mode
;
957 iv
->mult
= const1_rtx
;
958 iv
->delta
= outer_step
;
959 iv
->first_special
= inner_mode
!= outer_mode
;
964 fprintf (dump_file
, " ");
965 dump_iv_info (dump_file
, iv
);
966 fprintf (dump_file
, "\n");
969 record_biv (def
, iv
);
970 return iv
->base
!= NULL_RTX
;
973 /* Analyzes expression RHS used at INSN and stores the result to *IV.
974 The mode of the induction variable is MODE. */
977 iv_analyze_expr (rtx_insn
*insn
, rtx rhs
, machine_mode mode
,
981 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
982 struct rtx_iv iv0
, iv1
;
983 enum rtx_code code
= GET_CODE (rhs
);
984 machine_mode omode
= mode
;
990 gcc_assert (GET_MODE (rhs
) == mode
|| GET_MODE (rhs
) == VOIDmode
);
996 if (!iv_analyze_op (insn
, rhs
, iv
))
999 if (iv
->mode
== VOIDmode
)
1002 iv
->extend_mode
= mode
;
1017 op0
= XEXP (rhs
, 0);
1018 omode
= GET_MODE (op0
);
1023 op0
= XEXP (rhs
, 0);
1024 op1
= XEXP (rhs
, 1);
1028 op0
= XEXP (rhs
, 0);
1029 mby
= XEXP (rhs
, 1);
1030 if (!CONSTANT_P (mby
))
1031 std::swap (op0
, mby
);
1032 if (!CONSTANT_P (mby
))
1037 op0
= XEXP (rhs
, 0);
1038 mby
= XEXP (rhs
, 1);
1039 if (!CONSTANT_P (mby
))
1048 && !iv_analyze_expr (insn
, op0
, omode
, &iv0
))
1052 && !iv_analyze_expr (insn
, op1
, omode
, &iv1
))
1058 if (!iv_extend (&iv0
, IV_SIGN_EXTEND
, mode
))
1063 if (!iv_extend (&iv0
, IV_ZERO_EXTEND
, mode
))
1074 if (!iv_add (&iv0
, &iv1
, code
))
1079 if (!iv_mult (&iv0
, mby
))
1084 if (!iv_shift (&iv0
, mby
))
1093 return iv
->base
!= NULL_RTX
;
1096 /* Analyzes iv DEF and stores the result to *IV. */
1099 iv_analyze_def (df_ref def
, struct rtx_iv
*iv
)
1101 rtx_insn
*insn
= DF_REF_INSN (def
);
1102 rtx reg
= DF_REF_REG (def
);
1107 fprintf (dump_file
, "Analyzing def of ");
1108 print_rtl (dump_file
, reg
);
1109 fprintf (dump_file
, " in insn ");
1110 print_rtl_single (dump_file
, insn
);
1113 check_iv_ref_table_size ();
1114 if (DF_REF_IV (def
))
1117 fprintf (dump_file
, " already analysed.\n");
1118 *iv
= *DF_REF_IV (def
);
1119 return iv
->base
!= NULL_RTX
;
1122 iv
->mode
= VOIDmode
;
1123 iv
->base
= NULL_RTX
;
1124 iv
->step
= NULL_RTX
;
1129 set
= single_set (insn
);
1133 if (!REG_P (SET_DEST (set
)))
1136 gcc_assert (SET_DEST (set
) == reg
);
1137 rhs
= find_reg_equal_equiv_note (insn
);
1139 rhs
= XEXP (rhs
, 0);
1141 rhs
= SET_SRC (set
);
1143 iv_analyze_expr (insn
, rhs
, GET_MODE (reg
), iv
);
1144 record_iv (def
, iv
);
1148 print_rtl (dump_file
, reg
);
1149 fprintf (dump_file
, " in insn ");
1150 print_rtl_single (dump_file
, insn
);
1151 fprintf (dump_file
, " is ");
1152 dump_iv_info (dump_file
, iv
);
1153 fprintf (dump_file
, "\n");
1156 return iv
->base
!= NULL_RTX
;
1159 /* Analyzes operand OP of INSN and stores the result to *IV. */
1162 iv_analyze_op (rtx_insn
*insn
, rtx op
, struct rtx_iv
*iv
)
1165 enum iv_grd_result res
;
1169 fprintf (dump_file
, "Analyzing operand ");
1170 print_rtl (dump_file
, op
);
1171 fprintf (dump_file
, " of insn ");
1172 print_rtl_single (dump_file
, insn
);
1175 if (function_invariant_p (op
))
1176 res
= GRD_INVARIANT
;
1177 else if (GET_CODE (op
) == SUBREG
)
1179 if (!subreg_lowpart_p (op
))
1182 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
1185 return iv_subreg (iv
, GET_MODE (op
));
1189 res
= iv_get_reaching_def (insn
, op
, &def
);
1190 if (res
== GRD_INVALID
)
1193 fprintf (dump_file
, " not simple.\n");
1198 if (res
== GRD_INVARIANT
)
1200 iv_constant (iv
, op
, VOIDmode
);
1204 fprintf (dump_file
, " ");
1205 dump_iv_info (dump_file
, iv
);
1206 fprintf (dump_file
, "\n");
1211 if (res
== GRD_MAYBE_BIV
)
1212 return iv_analyze_biv (op
, iv
);
1214 return iv_analyze_def (def
, iv
);
1217 /* Analyzes value VAL at INSN and stores the result to *IV. */
1220 iv_analyze (rtx_insn
*insn
, rtx val
, struct rtx_iv
*iv
)
1224 /* We must find the insn in that val is used, so that we get to UD chains.
1225 Since the function is sometimes called on result of get_condition,
1226 this does not necessarily have to be directly INSN; scan also the
1228 if (simple_reg_p (val
))
1230 if (GET_CODE (val
) == SUBREG
)
1231 reg
= SUBREG_REG (val
);
1235 while (!df_find_use (insn
, reg
))
1236 insn
= NEXT_INSN (insn
);
1239 return iv_analyze_op (insn
, val
, iv
);
1242 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1245 iv_analyze_result (rtx_insn
*insn
, rtx def
, struct rtx_iv
*iv
)
1249 adef
= df_find_def (insn
, def
);
1253 return iv_analyze_def (adef
, iv
);
1256 /* Checks whether definition of register REG in INSN is a basic induction
1257 variable. IV analysis must have been initialized (via a call to
1258 iv_analysis_loop_init) for this function to produce a result. */
1261 biv_p (rtx_insn
*insn
, rtx reg
)
1264 df_ref def
, last_def
;
1266 if (!simple_reg_p (reg
))
1269 def
= df_find_def (insn
, reg
);
1270 gcc_assert (def
!= NULL
);
1271 if (!latch_dominating_def (reg
, &last_def
))
1273 if (last_def
!= def
)
1276 if (!iv_analyze_biv (reg
, &iv
))
1279 return iv
.step
!= const0_rtx
;
1282 /* Calculates value of IV at ITERATION-th iteration. */
1285 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1289 /* We would need to generate some if_then_else patterns, and so far
1290 it is not needed anywhere. */
1291 gcc_assert (!iv
->first_special
);
1293 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1294 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1295 simplify_gen_binary (MULT
, iv
->extend_mode
,
1296 iv
->step
, iteration
));
1300 if (iv
->extend_mode
== iv
->mode
)
1303 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1305 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
1308 val
= simplify_gen_unary (iv_extend_to_rtx_code (iv
->extend
),
1309 iv
->extend_mode
, val
, iv
->mode
);
1310 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1311 simplify_gen_binary (MULT
, iv
->extend_mode
,
1317 /* Free the data for an induction variable analysis. */
1320 iv_analysis_done (void)
1326 df_finish_pass (true);
1329 free (iv_ref_table
);
1330 iv_ref_table
= NULL
;
1331 iv_ref_table_size
= 0;
1335 /* Computes inverse to X modulo (1 << MOD). */
1338 inverse (uint64_t x
, int mod
)
1341 ((uint64_t) 1 << (mod
- 1) << 1) - 1;
1345 for (i
= 0; i
< mod
- 1; i
++)
1347 rslt
= (rslt
* x
) & mask
;
1354 /* Checks whether any register in X is in set ALT. */
1357 altered_reg_used (const_rtx x
, bitmap alt
)
1359 subrtx_iterator::array_type array
;
1360 FOR_EACH_SUBRTX (iter
, array
, x
, NONCONST
)
1362 const_rtx x
= *iter
;
1363 if (REG_P (x
) && REGNO_REG_SET_P (alt
, REGNO (x
)))
1369 /* Marks registers altered by EXPR in set ALT. */
1372 mark_altered (rtx expr
, const_rtx by ATTRIBUTE_UNUSED
, void *alt
)
1374 if (GET_CODE (expr
) == SUBREG
)
1375 expr
= SUBREG_REG (expr
);
1379 SET_REGNO_REG_SET ((bitmap
) alt
, REGNO (expr
));
1382 /* Checks whether RHS is simple enough to process. */
1385 simple_rhs_p (rtx rhs
)
1389 if (function_invariant_p (rhs
)
1390 || (REG_P (rhs
) && !HARD_REGISTER_P (rhs
)))
1393 switch (GET_CODE (rhs
))
1398 op0
= XEXP (rhs
, 0);
1399 op1
= XEXP (rhs
, 1);
1400 /* Allow reg OP const and reg OP reg. */
1401 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
))
1402 && !function_invariant_p (op0
))
1404 if (!(REG_P (op1
) && !HARD_REGISTER_P (op1
))
1405 && !function_invariant_p (op1
))
1414 op0
= XEXP (rhs
, 0);
1415 op1
= XEXP (rhs
, 1);
1416 /* Allow reg OP const. */
1417 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
)))
1419 if (!function_invariant_p (op1
))
1429 /* If REGNO has a single definition, return its known value, otherwise return
1433 find_single_def_src (unsigned int regno
)
1441 adef
= DF_REG_DEF_CHAIN (regno
);
1442 if (adef
== NULL
|| DF_REF_NEXT_REG (adef
) != NULL
1443 || DF_REF_IS_ARTIFICIAL (adef
))
1446 set
= single_set (DF_REF_INSN (adef
));
1447 if (set
== NULL
|| !REG_P (SET_DEST (set
))
1448 || REGNO (SET_DEST (set
)) != regno
)
1451 note
= find_reg_equal_equiv_note (DF_REF_INSN (adef
));
1453 if (note
&& function_invariant_p (XEXP (note
, 0)))
1455 src
= XEXP (note
, 0);
1458 src
= SET_SRC (set
);
1462 regno
= REGNO (src
);
1467 if (!function_invariant_p (src
))
1473 /* If any registers in *EXPR that have a single definition, try to replace
1474 them with the known-equivalent values. */
1477 replace_single_def_regs (rtx
*expr
)
1479 subrtx_var_iterator::array_type array
;
1481 FOR_EACH_SUBRTX_VAR (iter
, array
, *expr
, NONCONST
)
1485 if (rtx new_x
= find_single_def_src (REGNO (x
)))
1487 *expr
= simplify_replace_rtx (*expr
, x
, new_x
);
1493 /* A subroutine of simplify_using_initial_values, this function examines INSN
1494 to see if it contains a suitable set that we can use to make a replacement.
1495 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1496 the set; return false otherwise. */
1499 suitable_set_for_replacement (rtx_insn
*insn
, rtx
*dest
, rtx
*src
)
1501 rtx set
= single_set (insn
);
1502 rtx lhs
= NULL_RTX
, rhs
;
1507 lhs
= SET_DEST (set
);
1511 rhs
= find_reg_equal_equiv_note (insn
);
1513 rhs
= XEXP (rhs
, 0);
1515 rhs
= SET_SRC (set
);
1517 if (!simple_rhs_p (rhs
))
1525 /* Using the data returned by suitable_set_for_replacement, replace DEST
1526 with SRC in *EXPR and return the new expression. Also call
1527 replace_single_def_regs if the replacement changed something. */
1529 replace_in_expr (rtx
*expr
, rtx dest
, rtx src
)
1532 *expr
= simplify_replace_rtx (*expr
, dest
, src
);
1535 replace_single_def_regs (expr
);
1538 /* Checks whether A implies B. */
1541 implies_p (rtx a
, rtx b
)
1543 rtx op0
, op1
, opb0
, opb1
;
1546 if (rtx_equal_p (a
, b
))
1549 if (GET_CODE (a
) == EQ
)
1555 || (GET_CODE (op0
) == SUBREG
1556 && REG_P (SUBREG_REG (op0
))))
1558 rtx r
= simplify_replace_rtx (b
, op0
, op1
);
1559 if (r
== const_true_rtx
)
1564 || (GET_CODE (op1
) == SUBREG
1565 && REG_P (SUBREG_REG (op1
))))
1567 rtx r
= simplify_replace_rtx (b
, op1
, op0
);
1568 if (r
== const_true_rtx
)
1573 if (b
== const_true_rtx
)
1576 if ((GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMM_COMPARE
1577 && GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMPARE
)
1578 || (GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMM_COMPARE
1579 && GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMPARE
))
1587 mode
= GET_MODE (op0
);
1588 if (mode
!= GET_MODE (opb0
))
1590 else if (mode
== VOIDmode
)
1592 mode
= GET_MODE (op1
);
1593 if (mode
!= GET_MODE (opb1
))
1597 /* A < B implies A + 1 <= B. */
1598 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1599 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1602 if (GET_CODE (a
) == GT
)
1603 std::swap (op0
, op1
);
1605 if (GET_CODE (b
) == GE
)
1606 std::swap (opb0
, opb1
);
1608 if (SCALAR_INT_MODE_P (mode
)
1609 && rtx_equal_p (op1
, opb1
)
1610 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1615 /* A < B or A > B imply A != B. TODO: Likewise
1616 A + n < B implies A != B + n if neither wraps. */
1617 if (GET_CODE (b
) == NE
1618 && (GET_CODE (a
) == GT
|| GET_CODE (a
) == GTU
1619 || GET_CODE (a
) == LT
|| GET_CODE (a
) == LTU
))
1621 if (rtx_equal_p (op0
, opb0
)
1622 && rtx_equal_p (op1
, opb1
))
1626 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1627 if (GET_CODE (a
) == NE
1628 && op1
== const0_rtx
)
1630 if ((GET_CODE (b
) == GTU
1631 && opb1
== const0_rtx
)
1632 || (GET_CODE (b
) == GEU
1633 && opb1
== const1_rtx
))
1634 return rtx_equal_p (op0
, opb0
);
1637 /* A != N is equivalent to A - (N + 1) <u -1. */
1638 if (GET_CODE (a
) == NE
1639 && CONST_INT_P (op1
)
1640 && GET_CODE (b
) == LTU
1641 && opb1
== constm1_rtx
1642 && GET_CODE (opb0
) == PLUS
1643 && CONST_INT_P (XEXP (opb0
, 1))
1644 /* Avoid overflows. */
1645 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1646 != ((unsigned HOST_WIDE_INT
)1
1647 << (HOST_BITS_PER_WIDE_INT
- 1)) - 1)
1648 && INTVAL (XEXP (opb0
, 1)) + 1 == -INTVAL (op1
))
1649 return rtx_equal_p (op0
, XEXP (opb0
, 0));
1651 /* Likewise, A != N implies A - N > 0. */
1652 if (GET_CODE (a
) == NE
1653 && CONST_INT_P (op1
))
1655 if (GET_CODE (b
) == GTU
1656 && GET_CODE (opb0
) == PLUS
1657 && opb1
== const0_rtx
1658 && CONST_INT_P (XEXP (opb0
, 1))
1659 /* Avoid overflows. */
1660 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1661 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1662 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1663 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1664 if (GET_CODE (b
) == GEU
1665 && GET_CODE (opb0
) == PLUS
1666 && opb1
== const1_rtx
1667 && CONST_INT_P (XEXP (opb0
, 1))
1668 /* Avoid overflows. */
1669 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1670 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1671 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1672 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1675 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1676 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == GE
)
1677 && CONST_INT_P (op1
)
1678 && ((GET_CODE (a
) == GT
&& op1
== constm1_rtx
)
1679 || INTVAL (op1
) >= 0)
1680 && GET_CODE (b
) == LTU
1681 && CONST_INT_P (opb1
)
1682 && rtx_equal_p (op0
, opb0
))
1683 return INTVAL (opb1
) < 0;
1688 /* Canonicalizes COND so that
1690 (1) Ensure that operands are ordered according to
1691 swap_commutative_operands_p.
1692 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1693 for GE, GEU, and LEU. */
1696 canon_condition (rtx cond
)
1702 code
= GET_CODE (cond
);
1703 op0
= XEXP (cond
, 0);
1704 op1
= XEXP (cond
, 1);
1706 if (swap_commutative_operands_p (op0
, op1
))
1708 code
= swap_condition (code
);
1709 std::swap (op0
, op1
);
1712 mode
= GET_MODE (op0
);
1713 if (mode
== VOIDmode
)
1714 mode
= GET_MODE (op1
);
1715 gcc_assert (mode
!= VOIDmode
);
1717 if (CONST_SCALAR_INT_P (op1
) && GET_MODE_CLASS (mode
) != MODE_CC
)
1719 rtx_mode_t
const_val (op1
, mode
);
1724 if (wi::ne_p (const_val
, wi::max_value (mode
, SIGNED
)))
1727 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1732 if (wi::ne_p (const_val
, wi::min_value (mode
, SIGNED
)))
1735 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1740 if (wi::ne_p (const_val
, -1))
1743 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1748 if (wi::ne_p (const_val
, 0))
1751 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1760 if (op0
!= XEXP (cond
, 0)
1761 || op1
!= XEXP (cond
, 1)
1762 || code
!= GET_CODE (cond
)
1763 || GET_MODE (cond
) != SImode
)
1764 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1769 /* Reverses CONDition; returns NULL if we cannot. */
1772 reversed_condition (rtx cond
)
1774 enum rtx_code reversed
;
1775 reversed
= reversed_comparison_code (cond
, NULL
);
1776 if (reversed
== UNKNOWN
)
1779 return gen_rtx_fmt_ee (reversed
,
1780 GET_MODE (cond
), XEXP (cond
, 0),
1784 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1785 set of altered regs. */
1788 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1790 rtx rev
, reve
, exp
= *expr
;
1792 /* If some register gets altered later, we do not really speak about its
1793 value at the time of comparison. */
1794 if (altered
&& altered_reg_used (cond
, altered
))
1797 if (GET_CODE (cond
) == EQ
1798 && REG_P (XEXP (cond
, 0)) && CONSTANT_P (XEXP (cond
, 1)))
1800 *expr
= simplify_replace_rtx (*expr
, XEXP (cond
, 0), XEXP (cond
, 1));
1804 if (!COMPARISON_P (exp
))
1807 rev
= reversed_condition (cond
);
1808 reve
= reversed_condition (exp
);
1810 cond
= canon_condition (cond
);
1811 exp
= canon_condition (exp
);
1813 rev
= canon_condition (rev
);
1815 reve
= canon_condition (reve
);
1817 if (rtx_equal_p (exp
, cond
))
1819 *expr
= const_true_rtx
;
1823 if (rev
&& rtx_equal_p (exp
, rev
))
1829 if (implies_p (cond
, exp
))
1831 *expr
= const_true_rtx
;
1835 if (reve
&& implies_p (cond
, reve
))
1841 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1843 if (rev
&& implies_p (exp
, rev
))
1849 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1850 if (rev
&& reve
&& implies_p (reve
, rev
))
1852 *expr
= const_true_rtx
;
1856 /* We would like to have some other tests here. TODO. */
1861 /* Use relationship between A and *B to eventually eliminate *B.
1862 OP is the operation we consider. */
1865 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1870 /* If A implies *B, we may replace *B by true. */
1871 if (implies_p (a
, *b
))
1872 *b
= const_true_rtx
;
1876 /* If *B implies A, we may replace *B by false. */
1877 if (implies_p (*b
, a
))
1886 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1887 operation we consider. */
1890 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1894 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1895 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1896 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1897 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1900 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1901 is a list, its elements are assumed to be combined using OP. */
1904 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1906 bool expression_valid
;
1907 rtx head
, tail
, last_valid_expr
;
1908 rtx_expr_list
*cond_list
;
1911 regset altered
, this_altered
;
1917 if (CONSTANT_P (*expr
))
1920 if (GET_CODE (*expr
) == EXPR_LIST
)
1922 head
= XEXP (*expr
, 0);
1923 tail
= XEXP (*expr
, 1);
1925 eliminate_implied_conditions (op
, &head
, tail
);
1930 neutral
= const_true_rtx
;
1935 neutral
= const0_rtx
;
1936 aggr
= const_true_rtx
;
1943 simplify_using_initial_values (loop
, UNKNOWN
, &head
);
1946 XEXP (*expr
, 0) = aggr
;
1947 XEXP (*expr
, 1) = NULL_RTX
;
1950 else if (head
== neutral
)
1953 simplify_using_initial_values (loop
, op
, expr
);
1956 simplify_using_initial_values (loop
, op
, &tail
);
1958 if (tail
&& XEXP (tail
, 0) == aggr
)
1964 XEXP (*expr
, 0) = head
;
1965 XEXP (*expr
, 1) = tail
;
1969 gcc_assert (op
== UNKNOWN
);
1971 replace_single_def_regs (expr
);
1972 if (CONSTANT_P (*expr
))
1975 e
= loop_preheader_edge (loop
);
1976 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1979 altered
= ALLOC_REG_SET (®_obstack
);
1980 this_altered
= ALLOC_REG_SET (®_obstack
);
1982 expression_valid
= true;
1983 last_valid_expr
= *expr
;
1987 insn
= BB_END (e
->src
);
1988 if (any_condjump_p (insn
))
1990 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false, true);
1992 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1993 cond
= reversed_condition (cond
);
1997 simplify_using_condition (cond
, expr
, altered
);
2001 if (CONSTANT_P (*expr
))
2003 for (note
= cond_list
; note
; note
= XEXP (note
, 1))
2005 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
2006 if (CONSTANT_P (*expr
))
2010 cond_list
= alloc_EXPR_LIST (0, cond
, cond_list
);
2014 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
2022 CLEAR_REG_SET (this_altered
);
2023 note_stores (PATTERN (insn
), mark_altered
, this_altered
);
2026 /* Kill all call clobbered registers. */
2028 hard_reg_set_iterator hrsi
;
2029 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call
,
2031 SET_REGNO_REG_SET (this_altered
, i
);
2034 if (suitable_set_for_replacement (insn
, &dest
, &src
))
2036 rtx_expr_list
**pnote
, **pnote_next
;
2038 replace_in_expr (expr
, dest
, src
);
2039 if (CONSTANT_P (*expr
))
2042 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2045 rtx old_cond
= XEXP (note
, 0);
2047 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2048 replace_in_expr (&XEXP (note
, 0), dest
, src
);
2050 /* We can no longer use a condition that has been simplified
2051 to a constant, and simplify_using_condition will abort if
2053 if (CONSTANT_P (XEXP (note
, 0)))
2055 *pnote
= *pnote_next
;
2057 free_EXPR_LIST_node (note
);
2059 /* Retry simplifications with this condition if either the
2060 expression or the condition changed. */
2061 else if (old_cond
!= XEXP (note
, 0) || old
!= *expr
)
2062 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
2067 rtx_expr_list
**pnote
, **pnote_next
;
2069 /* If we did not use this insn to make a replacement, any overlap
2070 between stores in this insn and our expression will cause the
2071 expression to become invalid. */
2072 if (altered_reg_used (*expr
, this_altered
))
2075 /* Likewise for the conditions. */
2076 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2079 rtx old_cond
= XEXP (note
, 0);
2081 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2082 if (altered_reg_used (old_cond
, this_altered
))
2084 *pnote
= *pnote_next
;
2086 free_EXPR_LIST_node (note
);
2091 if (CONSTANT_P (*expr
))
2094 IOR_REG_SET (altered
, this_altered
);
2096 /* If the expression now contains regs that have been altered, we
2097 can't return it to the caller. However, it is still valid for
2098 further simplification, so keep searching to see if we can
2099 eventually turn it into a constant. */
2100 if (altered_reg_used (*expr
, altered
))
2101 expression_valid
= false;
2102 if (expression_valid
)
2103 last_valid_expr
= *expr
;
2106 if (!single_pred_p (e
->src
)
2107 || single_pred (e
->src
) == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
2109 e
= single_pred_edge (e
->src
);
2113 free_EXPR_LIST_list (&cond_list
);
2114 if (!CONSTANT_P (*expr
))
2115 *expr
= last_valid_expr
;
2116 FREE_REG_SET (altered
);
2117 FREE_REG_SET (this_altered
);
2120 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2121 that IV occurs as left operands of comparison COND and its signedness
2122 is SIGNED_P to DESC. */
2125 shorten_into_mode (struct rtx_iv
*iv
, machine_mode mode
,
2126 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
2128 rtx mmin
, mmax
, cond_over
, cond_under
;
2130 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
2131 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
2133 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
2142 if (cond_under
!= const0_rtx
)
2144 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2145 if (cond_over
!= const0_rtx
)
2146 desc
->noloop_assumptions
=
2147 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
2154 if (cond_over
!= const0_rtx
)
2156 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2157 if (cond_under
!= const0_rtx
)
2158 desc
->noloop_assumptions
=
2159 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
2163 if (cond_over
!= const0_rtx
)
2165 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2166 if (cond_under
!= const0_rtx
)
2168 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2176 iv
->extend
= signed_p
? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
2179 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2180 subregs of the same mode if possible (sometimes it is necessary to add
2181 some assumptions to DESC). */
2184 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
2185 enum rtx_code cond
, struct niter_desc
*desc
)
2187 machine_mode comp_mode
;
2190 /* If the ivs behave specially in the first iteration, or are
2191 added/multiplied after extending, we ignore them. */
2192 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
2194 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
2197 /* If there is some extend, it must match signedness of the comparison. */
2202 if (iv0
->extend
== IV_ZERO_EXTEND
2203 || iv1
->extend
== IV_ZERO_EXTEND
)
2210 if (iv0
->extend
== IV_SIGN_EXTEND
2211 || iv1
->extend
== IV_SIGN_EXTEND
)
2217 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
2218 && iv1
->extend
!= IV_UNKNOWN_EXTEND
2219 && iv0
->extend
!= iv1
->extend
)
2223 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
)
2224 signed_p
= iv0
->extend
== IV_SIGN_EXTEND
;
2225 if (iv1
->extend
!= IV_UNKNOWN_EXTEND
)
2226 signed_p
= iv1
->extend
== IV_SIGN_EXTEND
;
2233 /* Values of both variables should be computed in the same mode. These
2234 might indeed be different, if we have comparison like
2236 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2238 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2239 in different modes. This does not seem impossible to handle, but
2240 it hardly ever occurs in practice.
2242 The only exception is the case when one of operands is invariant.
2243 For example pentium 3 generates comparisons like
2244 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2245 definitely do not want this prevent the optimization. */
2246 comp_mode
= iv0
->extend_mode
;
2247 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
2248 comp_mode
= iv1
->extend_mode
;
2250 if (iv0
->extend_mode
!= comp_mode
)
2252 if (iv0
->mode
!= iv0
->extend_mode
2253 || iv0
->step
!= const0_rtx
)
2256 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2257 comp_mode
, iv0
->base
, iv0
->mode
);
2258 iv0
->extend_mode
= comp_mode
;
2261 if (iv1
->extend_mode
!= comp_mode
)
2263 if (iv1
->mode
!= iv1
->extend_mode
2264 || iv1
->step
!= const0_rtx
)
2267 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2268 comp_mode
, iv1
->base
, iv1
->mode
);
2269 iv1
->extend_mode
= comp_mode
;
2272 /* Check that both ivs belong to a range of a single mode. If one of the
2273 operands is an invariant, we may need to shorten it into the common
2275 if (iv0
->mode
== iv0
->extend_mode
2276 && iv0
->step
== const0_rtx
2277 && iv0
->mode
!= iv1
->mode
)
2278 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
2280 if (iv1
->mode
== iv1
->extend_mode
2281 && iv1
->step
== const0_rtx
2282 && iv0
->mode
!= iv1
->mode
)
2283 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
2285 if (iv0
->mode
!= iv1
->mode
)
2288 desc
->mode
= iv0
->mode
;
2289 desc
->signed_p
= signed_p
;
2294 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2295 result. This function is called from iv_number_of_iterations with
2296 a number of fields in DESC already filled in. OLD_NITER is the original
2297 expression for the number of iterations, before we tried to simplify it. */
2300 determine_max_iter (struct loop
*loop
, struct niter_desc
*desc
, rtx old_niter
)
2302 rtx niter
= desc
->niter_expr
;
2303 rtx mmin
, mmax
, cmp
;
2305 uint64_t andmax
= 0;
2307 /* We used to look for constant operand 0 of AND,
2308 but canonicalization should always make this impossible. */
2309 gcc_checking_assert (GET_CODE (niter
) != AND
2310 || !CONST_INT_P (XEXP (niter
, 0)));
2312 if (GET_CODE (niter
) == AND
2313 && CONST_INT_P (XEXP (niter
, 1)))
2315 andmax
= UINTVAL (XEXP (niter
, 1));
2316 niter
= XEXP (niter
, 0);
2319 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
2320 nmax
= UINTVAL (mmax
) - UINTVAL (mmin
);
2322 if (GET_CODE (niter
) == UDIV
)
2324 if (!CONST_INT_P (XEXP (niter
, 1)))
2326 inc
= INTVAL (XEXP (niter
, 1));
2327 niter
= XEXP (niter
, 0);
2332 /* We could use a binary search here, but for now improving the upper
2333 bound by just one eliminates one important corner case. */
2334 cmp
= simplify_gen_relational (desc
->signed_p
? LT
: LTU
, VOIDmode
,
2335 desc
->mode
, old_niter
, mmax
);
2336 simplify_using_initial_values (loop
, UNKNOWN
, &cmp
);
2337 if (cmp
== const_true_rtx
)
2342 fprintf (dump_file
, ";; improved upper bound by one.\n");
2346 nmax
= MIN (nmax
, andmax
);
2348 fprintf (dump_file
, ";; Determined upper bound %" PRId64
".\n",
2353 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2354 the result into DESC. Very similar to determine_number_of_iterations
2355 (basically its rtl version), complicated by things like subregs. */
2358 iv_number_of_iterations (struct loop
*loop
, rtx_insn
*insn
, rtx condition
,
2359 struct niter_desc
*desc
)
2361 rtx op0
, op1
, delta
, step
, bound
, may_xform
, tmp
, tmp0
, tmp1
;
2362 struct rtx_iv iv0
, iv1
, tmp_iv
;
2363 rtx assumption
, may_not_xform
;
2365 machine_mode mode
, comp_mode
;
2366 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
2367 uint64_t s
, size
, d
, inv
, max
;
2368 int64_t up
, down
, inc
, step_val
;
2369 int was_sharp
= false;
2373 /* The meaning of these assumptions is this:
2375 then the rest of information does not have to be valid
2376 if noloop_assumptions then the loop does not roll
2377 if infinite then this exit is never used */
2379 desc
->assumptions
= NULL_RTX
;
2380 desc
->noloop_assumptions
= NULL_RTX
;
2381 desc
->infinite
= NULL_RTX
;
2382 desc
->simple_p
= true;
2384 desc
->const_iter
= false;
2385 desc
->niter_expr
= NULL_RTX
;
2387 cond
= GET_CODE (condition
);
2388 gcc_assert (COMPARISON_P (condition
));
2390 mode
= GET_MODE (XEXP (condition
, 0));
2391 if (mode
== VOIDmode
)
2392 mode
= GET_MODE (XEXP (condition
, 1));
2393 /* The constant comparisons should be folded. */
2394 gcc_assert (mode
!= VOIDmode
);
2396 /* We only handle integers or pointers. */
2397 if (GET_MODE_CLASS (mode
) != MODE_INT
2398 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
2401 op0
= XEXP (condition
, 0);
2402 if (!iv_analyze (insn
, op0
, &iv0
))
2404 if (iv0
.extend_mode
== VOIDmode
)
2405 iv0
.mode
= iv0
.extend_mode
= mode
;
2407 op1
= XEXP (condition
, 1);
2408 if (!iv_analyze (insn
, op1
, &iv1
))
2410 if (iv1
.extend_mode
== VOIDmode
)
2411 iv1
.mode
= iv1
.extend_mode
= mode
;
2413 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
2414 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
2417 /* Check condition and normalize it. */
2425 tmp_iv
= iv0
; iv0
= iv1
; iv1
= tmp_iv
;
2426 cond
= swap_condition (cond
);
2438 /* Handle extends. This is relatively nontrivial, so we only try in some
2439 easy cases, when we can canonicalize the ivs (possibly by adding some
2440 assumptions) to shape subreg (base + i * step). This function also fills
2441 in desc->mode and desc->signed_p. */
2443 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2446 comp_mode
= iv0
.extend_mode
;
2448 size
= GET_MODE_PRECISION (mode
);
2449 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2450 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2451 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2453 if (!CONST_INT_P (iv0
.step
) || !CONST_INT_P (iv1
.step
))
2456 /* We can take care of the case of two induction variables chasing each other
2457 if the test is NE. I have never seen a loop using it, but still it is
2459 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2464 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2465 iv1
.step
= const0_rtx
;
2468 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2469 iv1
.step
= lowpart_subreg (mode
, iv1
.step
, comp_mode
);
2471 /* This is either infinite loop or the one that ends immediately, depending
2472 on initial values. Unswitching should remove this kind of conditions. */
2473 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2478 if (iv0
.step
== const0_rtx
)
2479 step_val
= -INTVAL (iv1
.step
);
2481 step_val
= INTVAL (iv0
.step
);
2483 /* Ignore loops of while (i-- < 10) type. */
2487 step_is_pow2
= !(step_val
& (step_val
- 1));
2491 /* We do not care about whether the step is power of two in this
2493 step_is_pow2
= false;
2497 /* Some more condition normalization. We must record some assumptions
2498 due to overflows. */
2503 /* We want to take care only of non-sharp relationals; this is easy,
2504 as in cases the overflow would make the transformation unsafe
2505 the loop does not roll. Seemingly it would make more sense to want
2506 to take care of sharp relationals instead, as NE is more similar to
2507 them, but the problem is that here the transformation would be more
2508 difficult due to possibly infinite loops. */
2509 if (iv0
.step
== const0_rtx
)
2511 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2512 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2514 if (assumption
== const_true_rtx
)
2515 goto zero_iter_simplify
;
2516 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2517 iv0
.base
, const1_rtx
);
2521 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2522 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2524 if (assumption
== const_true_rtx
)
2525 goto zero_iter_simplify
;
2526 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2527 iv1
.base
, constm1_rtx
);
2530 if (assumption
!= const0_rtx
)
2531 desc
->noloop_assumptions
=
2532 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2533 cond
= (cond
== LT
) ? LE
: LEU
;
2535 /* It will be useful to be able to tell the difference once more in
2536 LE -> NE reduction. */
2542 /* Take care of trivially infinite loops. */
2545 if (iv0
.step
== const0_rtx
)
2547 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2548 if (rtx_equal_p (tmp
, mode_mmin
))
2551 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2552 /* Fill in the remaining fields somehow. */
2553 goto zero_iter_simplify
;
2558 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2559 if (rtx_equal_p (tmp
, mode_mmax
))
2562 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2563 /* Fill in the remaining fields somehow. */
2564 goto zero_iter_simplify
;
2569 /* If we can we want to take care of NE conditions instead of size
2570 comparisons, as they are much more friendly (most importantly
2571 this takes care of special handling of loops with step 1). We can
2572 do it if we first check that upper bound is greater or equal to
2573 lower bound, their difference is constant c modulo step and that
2574 there is not an overflow. */
2577 if (iv0
.step
== const0_rtx
)
2578 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2581 step
= lowpart_subreg (mode
, step
, comp_mode
);
2582 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2583 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2584 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2585 may_xform
= const0_rtx
;
2586 may_not_xform
= const_true_rtx
;
2588 if (CONST_INT_P (delta
))
2590 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2592 /* A special case. We have transformed condition of type
2593 for (i = 0; i < 4; i += 4)
2595 for (i = 0; i <= 3; i += 4)
2596 obviously if the test for overflow during that transformation
2597 passed, we cannot overflow here. Most importantly any
2598 loop with sharp end condition and step 1 falls into this
2599 category, so handling this case specially is definitely
2600 worth the troubles. */
2601 may_xform
= const_true_rtx
;
2603 else if (iv0
.step
== const0_rtx
)
2605 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2606 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2607 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2608 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2609 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2611 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2617 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2618 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2619 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2620 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2621 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2623 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2629 if (may_xform
!= const0_rtx
)
2631 /* We perform the transformation always provided that it is not
2632 completely senseless. This is OK, as we would need this assumption
2633 to determine the number of iterations anyway. */
2634 if (may_xform
!= const_true_rtx
)
2636 /* If the step is a power of two and the final value we have
2637 computed overflows, the cycle is infinite. Otherwise it
2638 is nontrivial to compute the number of iterations. */
2640 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2643 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2647 /* We are going to lose some information about upper bound on
2648 number of iterations in this step, so record the information
2650 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2651 if (CONST_INT_P (iv1
.base
))
2652 up
= INTVAL (iv1
.base
);
2654 up
= INTVAL (mode_mmax
) - inc
;
2655 down
= INTVAL (CONST_INT_P (iv0
.base
)
2658 max
= (uint64_t) (up
- down
) / inc
+ 1;
2660 && !desc
->assumptions
)
2661 record_niter_bound (loop
, max
, false, true);
2663 if (iv0
.step
== const0_rtx
)
2665 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2666 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2670 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2671 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2674 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2675 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2676 assumption
= simplify_gen_relational (reverse_condition (cond
),
2677 SImode
, mode
, tmp0
, tmp1
);
2678 if (assumption
== const_true_rtx
)
2679 goto zero_iter_simplify
;
2680 else if (assumption
!= const0_rtx
)
2681 desc
->noloop_assumptions
=
2682 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2687 /* Count the number of iterations. */
2690 /* Everything we do here is just arithmetics modulo size of mode. This
2691 makes us able to do more involved computations of number of iterations
2692 than in other cases. First transform the condition into shape
2693 s * i <> c, with s positive. */
2694 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2695 iv0
.base
= const0_rtx
;
2696 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2697 iv1
.step
= const0_rtx
;
2698 if (INTVAL (iv0
.step
) < 0)
2700 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, comp_mode
);
2701 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, comp_mode
);
2703 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2705 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2706 is infinite. Otherwise, the number of iterations is
2707 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2708 s
= INTVAL (iv0
.step
); d
= 1;
2715 bound
= GEN_INT (((uint64_t) 1 << (size
- 1 ) << 1) - 1);
2717 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2718 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, gen_int_mode (d
, mode
));
2719 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2720 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2722 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, gen_int_mode (d
, mode
));
2723 inv
= inverse (s
, size
);
2724 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, gen_int_mode (inv
, mode
));
2725 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2729 if (iv1
.step
== const0_rtx
)
2730 /* Condition in shape a + s * i <= b
2731 We must know that b + s does not overflow and a <= b + s and then we
2732 can compute number of iterations as (b + s - a) / s. (It might
2733 seem that we in fact could be more clever about testing the b + s
2734 overflow condition using some information about b - a mod s,
2735 but it was already taken into account during LE -> NE transform). */
2738 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2739 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2741 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2742 lowpart_subreg (mode
, step
,
2748 /* If s is power of 2, we know that the loop is infinite if
2749 a % s <= b % s and b + 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
, iv1
.base
, iv0
.step
);
2770 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2771 assumption
= simplify_gen_relational (reverse_condition (cond
),
2772 SImode
, mode
, tmp0
, tmp
);
2774 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2775 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2779 /* Condition in shape a <= b - s * i
2780 We must know that a - s does not overflow and a - s <= b and then
2781 we can again compute number of iterations as (b - (a - s)) / s. */
2782 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2783 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2784 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2786 bound
= simplify_gen_binary (PLUS
, mode
, mode_mmin
,
2787 lowpart_subreg (mode
, step
, comp_mode
));
2792 /* If s is power of 2, we know that the loop is infinite if
2793 a % s <= b % s and a - s overflows. */
2794 assumption
= simplify_gen_relational (reverse_condition (cond
),
2798 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2799 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2800 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2801 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2803 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2807 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2810 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2813 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2814 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2815 assumption
= simplify_gen_relational (reverse_condition (cond
),
2818 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2819 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2821 if (assumption
== const_true_rtx
)
2822 goto zero_iter_simplify
;
2823 else if (assumption
!= const0_rtx
)
2824 desc
->noloop_assumptions
=
2825 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2826 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2827 desc
->niter_expr
= delta
;
2830 old_niter
= desc
->niter_expr
;
2832 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2833 if (desc
->assumptions
2834 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2836 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2837 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2838 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2840 /* Rerun the simplification. Consider code (created by copying loop headers)
2852 The first pass determines that i = 0, the second pass uses it to eliminate
2853 noloop assumption. */
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
->noloop_assumptions
);
2860 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2861 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2863 if (desc
->noloop_assumptions
2864 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2867 if (CONST_INT_P (desc
->niter_expr
))
2869 uint64_t val
= INTVAL (desc
->niter_expr
);
2871 desc
->const_iter
= true;
2872 desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2874 && !desc
->assumptions
)
2875 record_niter_bound (loop
, desc
->niter
, false, true);
2879 max
= determine_max_iter (loop
, desc
, old_niter
);
2881 goto zero_iter_simplify
;
2883 && !desc
->assumptions
)
2884 record_niter_bound (loop
, max
, false, true);
2886 /* simplify_using_initial_values does a copy propagation on the registers
2887 in the expression for the number of iterations. This prolongs life
2888 ranges of registers and increases register pressure, and usually
2889 brings no gain (and if it happens to do, the cse pass will take care
2890 of it anyway). So prevent this behavior, unless it enabled us to
2891 derive that the number of iterations is a constant. */
2892 desc
->niter_expr
= old_niter
;
2898 /* Simplify the assumptions. */
2899 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2900 if (desc
->assumptions
2901 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2903 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2907 desc
->const_iter
= true;
2909 record_niter_bound (loop
, 0, true, true);
2910 desc
->noloop_assumptions
= NULL_RTX
;
2911 desc
->niter_expr
= const0_rtx
;
2915 desc
->simple_p
= false;
2919 /* Checks whether E is a simple exit from LOOP and stores its description
2923 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2925 basic_block exit_bb
;
2931 desc
->simple_p
= false;
2933 /* It must belong directly to the loop. */
2934 if (exit_bb
->loop_father
!= loop
)
2937 /* It must be tested (at least) once during any iteration. */
2938 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2941 /* It must end in a simple conditional jump. */
2942 if (!any_condjump_p (BB_END (exit_bb
)))
2945 ein
= EDGE_SUCC (exit_bb
, 0);
2947 ein
= EDGE_SUCC (exit_bb
, 1);
2950 desc
->in_edge
= ein
;
2952 /* Test whether the condition is suitable. */
2953 if (!(condition
= get_condition (BB_END (ein
->src
), &at
, false, false)))
2956 if (ein
->flags
& EDGE_FALLTHRU
)
2958 condition
= reversed_condition (condition
);
2963 /* Check that we are able to determine number of iterations and fill
2964 in information about it. */
2965 iv_number_of_iterations (loop
, at
, condition
, desc
);
2968 /* Finds a simple exit of LOOP and stores its description into DESC. */
2971 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2976 struct niter_desc act
;
2980 desc
->simple_p
= false;
2981 body
= get_loop_body (loop
);
2983 for (i
= 0; i
< loop
->num_nodes
; i
++)
2985 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
2987 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2990 check_simple_exit (loop
, e
, &act
);
2998 /* Prefer constant iterations; the less the better. */
3000 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
3003 /* Also if the actual exit may be infinite, while the old one
3004 not, prefer the old one. */
3005 if (act
.infinite
&& !desc
->infinite
)
3017 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
3018 fprintf (dump_file
, " simple exit %d -> %d\n",
3019 desc
->out_edge
->src
->index
,
3020 desc
->out_edge
->dest
->index
);
3021 if (desc
->assumptions
)
3023 fprintf (dump_file
, " assumptions: ");
3024 print_rtl (dump_file
, desc
->assumptions
);
3025 fprintf (dump_file
, "\n");
3027 if (desc
->noloop_assumptions
)
3029 fprintf (dump_file
, " does not roll if: ");
3030 print_rtl (dump_file
, desc
->noloop_assumptions
);
3031 fprintf (dump_file
, "\n");
3035 fprintf (dump_file
, " infinite if: ");
3036 print_rtl (dump_file
, desc
->infinite
);
3037 fprintf (dump_file
, "\n");
3040 fprintf (dump_file
, " number of iterations: ");
3041 print_rtl (dump_file
, desc
->niter_expr
);
3042 fprintf (dump_file
, "\n");
3044 fprintf (dump_file
, " upper bound: %li\n",
3045 (long)get_max_loop_iterations_int (loop
));
3046 fprintf (dump_file
, " realistic bound: %li\n",
3047 (long)get_estimated_loop_iterations_int (loop
));
3050 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
3056 /* Creates a simple loop description of LOOP if it was not computed
3060 get_simple_loop_desc (struct loop
*loop
)
3062 struct niter_desc
*desc
= simple_loop_desc (loop
);
3067 /* At least desc->infinite is not always initialized by
3068 find_simple_loop_exit. */
3069 desc
= ggc_cleared_alloc
<niter_desc
> ();
3070 iv_analysis_loop_init (loop
);
3071 find_simple_exit (loop
, desc
);
3072 loop
->simple_loop_desc
= desc
;
3074 if (desc
->simple_p
&& (desc
->assumptions
|| desc
->infinite
))
3076 const char *wording
;
3078 /* Assume that no overflow happens and that the loop is finite.
3079 We already warned at the tree level if we ran optimizations there. */
3080 if (!flag_tree_loop_optimize
&& warn_unsafe_loop_optimizations
)
3085 flag_unsafe_loop_optimizations
3086 ? N_("assuming that the loop is not infinite")
3087 : N_("cannot optimize possibly infinite loops");
3088 warning (OPT_Wunsafe_loop_optimizations
, "%s",
3091 if (desc
->assumptions
)
3094 flag_unsafe_loop_optimizations
3095 ? N_("assuming that the loop counter does not overflow")
3096 : N_("cannot optimize loop, the loop counter may overflow");
3097 warning (OPT_Wunsafe_loop_optimizations
, "%s",
3102 if (flag_unsafe_loop_optimizations
)
3104 desc
->assumptions
= NULL_RTX
;
3105 desc
->infinite
= NULL_RTX
;
3112 /* Releases simple loop description for LOOP. */
3115 free_simple_loop_desc (struct loop
*loop
)
3117 struct niter_desc
*desc
= simple_loop_desc (loop
);
3123 loop
->simple_loop_desc
= NULL
;