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"
60 #include "insn-config.h"
70 #include "diagnostic-core.h"
74 /* Possible return values of iv_get_reaching_def. */
78 /* More than one reaching def, or reaching def that does not
82 /* The use is trivial invariant of the loop, i.e. is not changed
86 /* The use is reached by initial value and a value from the
87 previous iteration. */
90 /* The use has single dominating def. */
94 /* Information about a biv. */
98 unsigned regno
; /* The register of the biv. */
99 struct rtx_iv iv
; /* Value of the biv. */
102 static bool clean_slate
= true;
104 static unsigned int iv_ref_table_size
= 0;
106 /* Table of rtx_ivs indexed by the df_ref uid field. */
107 static struct rtx_iv
** iv_ref_table
;
109 /* Induction variable stored at the reference. */
110 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
111 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
113 /* The current loop. */
115 static struct loop
*current_loop
;
117 /* Hashtable helper. */
119 struct biv_entry_hasher
: free_ptr_hash
<biv_entry
>
121 typedef rtx_def
*compare_type
;
122 static inline hashval_t
hash (const biv_entry
*);
123 static inline bool equal (const biv_entry
*, const rtx_def
*);
126 /* Returns hash value for biv B. */
129 biv_entry_hasher::hash (const biv_entry
*b
)
134 /* Compares biv B and register R. */
137 biv_entry_hasher::equal (const biv_entry
*b
, const rtx_def
*r
)
139 return b
->regno
== REGNO (r
);
142 /* Bivs of the current loop. */
144 static hash_table
<biv_entry_hasher
> *bivs
;
146 static bool iv_analyze_op (rtx_insn
*, rtx
, struct rtx_iv
*);
148 /* Return the RTX code corresponding to the IV extend code EXTEND. */
149 static inline enum rtx_code
150 iv_extend_to_rtx_code (enum iv_extend_code extend
)
158 case IV_UNKNOWN_EXTEND
:
164 /* Dumps information about IV to FILE. */
166 extern void dump_iv_info (FILE *, struct rtx_iv
*);
168 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
172 fprintf (file
, "not simple");
176 if (iv
->step
== const0_rtx
177 && !iv
->first_special
)
178 fprintf (file
, "invariant ");
180 print_rtl (file
, iv
->base
);
181 if (iv
->step
!= const0_rtx
)
183 fprintf (file
, " + ");
184 print_rtl (file
, iv
->step
);
185 fprintf (file
, " * iteration");
187 fprintf (file
, " (in %s)", GET_MODE_NAME (iv
->mode
));
189 if (iv
->mode
!= iv
->extend_mode
)
190 fprintf (file
, " %s to %s",
191 rtx_name
[iv_extend_to_rtx_code (iv
->extend
)],
192 GET_MODE_NAME (iv
->extend_mode
));
194 if (iv
->mult
!= const1_rtx
)
196 fprintf (file
, " * ");
197 print_rtl (file
, iv
->mult
);
199 if (iv
->delta
!= const0_rtx
)
201 fprintf (file
, " + ");
202 print_rtl (file
, iv
->delta
);
204 if (iv
->first_special
)
205 fprintf (file
, " (first special)");
208 /* Generates a subreg to get the least significant part of EXPR (in mode
209 INNER_MODE) to OUTER_MODE. */
212 lowpart_subreg (machine_mode outer_mode
, rtx expr
,
213 machine_mode inner_mode
)
215 return simplify_gen_subreg (outer_mode
, expr
, inner_mode
,
216 subreg_lowpart_offset (outer_mode
, inner_mode
));
220 check_iv_ref_table_size (void)
222 if (iv_ref_table_size
< DF_DEFS_TABLE_SIZE ())
224 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
225 iv_ref_table
= XRESIZEVEC (struct rtx_iv
*, iv_ref_table
, new_size
);
226 memset (&iv_ref_table
[iv_ref_table_size
], 0,
227 (new_size
- iv_ref_table_size
) * sizeof (struct rtx_iv
*));
228 iv_ref_table_size
= new_size
;
233 /* Checks whether REG is a well-behaved register. */
236 simple_reg_p (rtx reg
)
240 if (GET_CODE (reg
) == SUBREG
)
242 if (!subreg_lowpart_p (reg
))
244 reg
= SUBREG_REG (reg
);
251 if (HARD_REGISTER_NUM_P (r
))
254 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
260 /* Clears the information about ivs stored in df. */
265 unsigned i
, n_defs
= DF_DEFS_TABLE_SIZE ();
268 check_iv_ref_table_size ();
269 for (i
= 0; i
< n_defs
; i
++)
271 iv
= iv_ref_table
[i
];
275 iv_ref_table
[i
] = NULL
;
283 /* Prepare the data for an induction variable analysis of a LOOP. */
286 iv_analysis_loop_init (struct loop
*loop
)
290 /* Clear the information from the analysis of the previous loop. */
293 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
294 bivs
= new hash_table
<biv_entry_hasher
> (10);
300 /* Get rid of the ud chains before processing the rescans. Then add
302 df_remove_problem (df_chain
);
303 df_process_deferred_rescans ();
304 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
305 df_chain_add_problem (DF_UD_CHAIN
);
306 df_note_add_problem ();
307 df_analyze_loop (loop
);
309 df_dump_region (dump_file
);
311 check_iv_ref_table_size ();
314 /* Finds the definition of REG that dominates loop latch and stores
315 it to DEF. Returns false if there is not a single definition
316 dominating the latch. If REG has no definition in loop, DEF
317 is set to NULL and true is returned. */
320 latch_dominating_def (rtx reg
, df_ref
*def
)
322 df_ref single_rd
= NULL
, adef
;
323 unsigned regno
= REGNO (reg
);
324 struct df_rd_bb_info
*bb_info
= DF_RD_BB_INFO (current_loop
->latch
);
326 for (adef
= DF_REG_DEF_CHAIN (regno
); adef
; adef
= DF_REF_NEXT_REG (adef
))
328 if (!bitmap_bit_p (df
->blocks_to_analyze
, DF_REF_BBNO (adef
))
329 || !bitmap_bit_p (&bb_info
->out
, DF_REF_ID (adef
)))
332 /* More than one reaching definition. */
336 if (!just_once_each_iteration_p (current_loop
, DF_REF_BB (adef
)))
346 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
348 static enum iv_grd_result
349 iv_get_reaching_def (rtx_insn
*insn
, rtx reg
, df_ref
*def
)
352 basic_block def_bb
, use_bb
;
357 if (!simple_reg_p (reg
))
359 if (GET_CODE (reg
) == SUBREG
)
360 reg
= SUBREG_REG (reg
);
361 gcc_assert (REG_P (reg
));
363 use
= df_find_use (insn
, reg
);
364 gcc_assert (use
!= NULL
);
366 if (!DF_REF_CHAIN (use
))
367 return GRD_INVARIANT
;
369 /* More than one reaching def. */
370 if (DF_REF_CHAIN (use
)->next
)
373 adef
= DF_REF_CHAIN (use
)->ref
;
375 /* We do not handle setting only part of the register. */
376 if (DF_REF_FLAGS (adef
) & DF_REF_READ_WRITE
)
379 def_insn
= DF_REF_INSN (adef
);
380 def_bb
= DF_REF_BB (adef
);
381 use_bb
= BLOCK_FOR_INSN (insn
);
383 if (use_bb
== def_bb
)
384 dom_p
= (DF_INSN_LUID (def_insn
) < DF_INSN_LUID (insn
));
386 dom_p
= dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
);
391 return GRD_SINGLE_DOM
;
394 /* The definition does not dominate the use. This is still OK if
395 this may be a use of a biv, i.e. if the def_bb dominates loop
397 if (just_once_each_iteration_p (current_loop
, def_bb
))
398 return GRD_MAYBE_BIV
;
403 /* Sets IV to invariant CST in MODE. Always returns true (just for
404 consistency with other iv manipulation functions that may fail). */
407 iv_constant (struct rtx_iv
*iv
, rtx cst
, machine_mode mode
)
409 if (mode
== VOIDmode
)
410 mode
= GET_MODE (cst
);
414 iv
->step
= const0_rtx
;
415 iv
->first_special
= false;
416 iv
->extend
= IV_UNKNOWN_EXTEND
;
417 iv
->extend_mode
= iv
->mode
;
418 iv
->delta
= const0_rtx
;
419 iv
->mult
= const1_rtx
;
424 /* Evaluates application of subreg to MODE on IV. */
427 iv_subreg (struct rtx_iv
*iv
, machine_mode mode
)
429 /* If iv is invariant, just calculate the new value. */
430 if (iv
->step
== const0_rtx
431 && !iv
->first_special
)
433 rtx val
= get_iv_value (iv
, const0_rtx
);
434 val
= lowpart_subreg (mode
, val
,
435 iv
->extend
== IV_UNKNOWN_EXTEND
436 ? iv
->mode
: iv
->extend_mode
);
439 iv
->extend
= IV_UNKNOWN_EXTEND
;
440 iv
->mode
= iv
->extend_mode
= mode
;
441 iv
->delta
= const0_rtx
;
442 iv
->mult
= const1_rtx
;
446 if (iv
->extend_mode
== mode
)
449 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
452 iv
->extend
= IV_UNKNOWN_EXTEND
;
455 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
456 simplify_gen_binary (MULT
, iv
->extend_mode
,
457 iv
->base
, iv
->mult
));
458 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
459 iv
->mult
= const1_rtx
;
460 iv
->delta
= const0_rtx
;
461 iv
->first_special
= false;
466 /* Evaluates application of EXTEND to MODE on IV. */
469 iv_extend (struct rtx_iv
*iv
, enum iv_extend_code extend
, machine_mode mode
)
471 /* If iv is invariant, just calculate the new value. */
472 if (iv
->step
== const0_rtx
473 && !iv
->first_special
)
475 rtx val
= get_iv_value (iv
, const0_rtx
);
476 if (iv
->extend_mode
!= iv
->mode
477 && iv
->extend
!= IV_UNKNOWN_EXTEND
478 && iv
->extend
!= extend
)
479 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
480 val
= simplify_gen_unary (iv_extend_to_rtx_code (extend
), mode
,
483 ? iv
->extend_mode
: iv
->mode
);
485 iv
->extend
= IV_UNKNOWN_EXTEND
;
486 iv
->mode
= iv
->extend_mode
= mode
;
487 iv
->delta
= const0_rtx
;
488 iv
->mult
= const1_rtx
;
492 if (mode
!= iv
->extend_mode
)
495 if (iv
->extend
!= IV_UNKNOWN_EXTEND
496 && iv
->extend
!= extend
)
504 /* Evaluates negation of IV. */
507 iv_neg (struct rtx_iv
*iv
)
509 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
511 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
512 iv
->base
, iv
->extend_mode
);
513 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
514 iv
->step
, iv
->extend_mode
);
518 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
519 iv
->delta
, iv
->extend_mode
);
520 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
521 iv
->mult
, iv
->extend_mode
);
527 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
530 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
535 /* Extend the constant to extend_mode of the other operand if necessary. */
536 if (iv0
->extend
== IV_UNKNOWN_EXTEND
537 && iv0
->mode
== iv0
->extend_mode
538 && iv0
->step
== const0_rtx
539 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
541 iv0
->extend_mode
= iv1
->extend_mode
;
542 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
543 iv0
->base
, iv0
->mode
);
545 if (iv1
->extend
== IV_UNKNOWN_EXTEND
546 && iv1
->mode
== iv1
->extend_mode
547 && iv1
->step
== const0_rtx
548 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
550 iv1
->extend_mode
= iv0
->extend_mode
;
551 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
552 iv1
->base
, iv1
->mode
);
555 mode
= iv0
->extend_mode
;
556 if (mode
!= iv1
->extend_mode
)
559 if (iv0
->extend
== IV_UNKNOWN_EXTEND
560 && iv1
->extend
== IV_UNKNOWN_EXTEND
)
562 if (iv0
->mode
!= iv1
->mode
)
565 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
566 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
571 /* Handle addition of constant. */
572 if (iv1
->extend
== IV_UNKNOWN_EXTEND
574 && iv1
->step
== const0_rtx
)
576 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
580 if (iv0
->extend
== IV_UNKNOWN_EXTEND
582 && iv0
->step
== const0_rtx
)
590 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
597 /* Evaluates multiplication of IV by constant CST. */
600 iv_mult (struct rtx_iv
*iv
, rtx mby
)
602 machine_mode mode
= iv
->extend_mode
;
604 if (GET_MODE (mby
) != VOIDmode
605 && GET_MODE (mby
) != mode
)
608 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
610 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
611 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
615 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
616 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
622 /* Evaluates shift of IV by constant CST. */
625 iv_shift (struct rtx_iv
*iv
, rtx mby
)
627 machine_mode mode
= iv
->extend_mode
;
629 if (GET_MODE (mby
) != VOIDmode
630 && GET_MODE (mby
) != mode
)
633 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
635 iv
->base
= simplify_gen_binary (ASHIFT
, mode
, iv
->base
, mby
);
636 iv
->step
= simplify_gen_binary (ASHIFT
, mode
, iv
->step
, mby
);
640 iv
->delta
= simplify_gen_binary (ASHIFT
, mode
, iv
->delta
, mby
);
641 iv
->mult
= simplify_gen_binary (ASHIFT
, mode
, iv
->mult
, mby
);
647 /* The recursive part of get_biv_step. Gets the value of the single value
648 defined by DEF wrto initial value of REG inside loop, in shape described
652 get_biv_step_1 (df_ref def
, rtx reg
,
653 rtx
*inner_step
, machine_mode
*inner_mode
,
654 enum iv_extend_code
*extend
, machine_mode outer_mode
,
657 rtx set
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
660 rtx_insn
*insn
= DF_REF_INSN (def
);
662 enum iv_grd_result res
;
664 set
= single_set (insn
);
668 rhs
= find_reg_equal_equiv_note (insn
);
674 code
= GET_CODE (rhs
);
687 if (code
== PLUS
&& CONSTANT_P (op0
))
688 std::swap (op0
, op1
);
690 if (!simple_reg_p (op0
)
691 || !CONSTANT_P (op1
))
694 if (GET_MODE (rhs
) != outer_mode
)
696 /* ppc64 uses expressions like
698 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
700 this is equivalent to
702 (set x':DI (plus:DI y:DI 1))
703 (set x:SI (subreg:SI (x':DI)). */
704 if (GET_CODE (op0
) != SUBREG
)
706 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
715 if (GET_MODE (rhs
) != outer_mode
)
719 if (!simple_reg_p (op0
))
729 if (GET_CODE (next
) == SUBREG
)
731 if (!subreg_lowpart_p (next
))
734 nextr
= SUBREG_REG (next
);
735 if (GET_MODE (nextr
) != outer_mode
)
741 res
= iv_get_reaching_def (insn
, nextr
, &next_def
);
743 if (res
== GRD_INVALID
|| res
== GRD_INVARIANT
)
746 if (res
== GRD_MAYBE_BIV
)
748 if (!rtx_equal_p (nextr
, reg
))
751 *inner_step
= const0_rtx
;
752 *extend
= IV_UNKNOWN_EXTEND
;
753 *inner_mode
= outer_mode
;
754 *outer_step
= const0_rtx
;
756 else if (!get_biv_step_1 (next_def
, reg
,
757 inner_step
, inner_mode
, extend
, outer_mode
,
761 if (GET_CODE (next
) == SUBREG
)
763 machine_mode amode
= GET_MODE (next
);
765 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
769 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
770 *inner_step
, *outer_step
);
771 *outer_step
= const0_rtx
;
772 *extend
= IV_UNKNOWN_EXTEND
;
783 if (*inner_mode
== outer_mode
784 /* See comment in previous switch. */
785 || GET_MODE (rhs
) != outer_mode
)
786 *inner_step
= simplify_gen_binary (code
, outer_mode
,
789 *outer_step
= simplify_gen_binary (code
, outer_mode
,
795 gcc_assert (GET_MODE (op0
) == *inner_mode
796 && *extend
== IV_UNKNOWN_EXTEND
797 && *outer_step
== const0_rtx
);
799 *extend
= (code
== SIGN_EXTEND
) ? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
809 /* Gets the operation on register REG inside loop, in shape
811 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
813 If the operation cannot be described in this shape, return false.
814 LAST_DEF is the definition of REG that dominates loop latch. */
817 get_biv_step (df_ref last_def
, rtx reg
, rtx
*inner_step
,
818 machine_mode
*inner_mode
, enum iv_extend_code
*extend
,
819 machine_mode
*outer_mode
, rtx
*outer_step
)
821 *outer_mode
= GET_MODE (reg
);
823 if (!get_biv_step_1 (last_def
, reg
,
824 inner_step
, inner_mode
, extend
, *outer_mode
,
828 gcc_assert ((*inner_mode
== *outer_mode
) != (*extend
!= IV_UNKNOWN_EXTEND
));
829 gcc_assert (*inner_mode
!= *outer_mode
|| *outer_step
== const0_rtx
);
834 /* Records information that DEF is induction variable IV. */
837 record_iv (df_ref def
, struct rtx_iv
*iv
)
839 struct rtx_iv
*recorded_iv
= XNEW (struct rtx_iv
);
842 check_iv_ref_table_size ();
843 DF_REF_IV_SET (def
, recorded_iv
);
846 /* If DEF was already analyzed for bivness, store the description of the biv to
847 IV and return true. Otherwise return false. */
850 analyzed_for_bivness_p (rtx def
, struct rtx_iv
*iv
)
852 struct biv_entry
*biv
= bivs
->find_with_hash (def
, REGNO (def
));
862 record_biv (rtx def
, struct rtx_iv
*iv
)
864 struct biv_entry
*biv
= XNEW (struct biv_entry
);
865 biv_entry
**slot
= bivs
->find_slot_with_hash (def
, REGNO (def
), INSERT
);
867 biv
->regno
= REGNO (def
);
873 /* Determines whether DEF is a biv and if so, stores its description
877 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
879 rtx inner_step
, outer_step
;
880 machine_mode inner_mode
, outer_mode
;
881 enum iv_extend_code extend
;
886 fprintf (dump_file
, "Analyzing ");
887 print_rtl (dump_file
, def
);
888 fprintf (dump_file
, " for bivness.\n");
893 if (!CONSTANT_P (def
))
896 return iv_constant (iv
, def
, VOIDmode
);
899 if (!latch_dominating_def (def
, &last_def
))
902 fprintf (dump_file
, " not simple.\n");
907 return iv_constant (iv
, def
, VOIDmode
);
909 if (analyzed_for_bivness_p (def
, iv
))
912 fprintf (dump_file
, " already analysed.\n");
913 return iv
->base
!= NULL_RTX
;
916 if (!get_biv_step (last_def
, def
, &inner_step
, &inner_mode
, &extend
,
917 &outer_mode
, &outer_step
))
923 /* Loop transforms base to es (base + inner_step) + outer_step,
924 where es means extend of subreg between inner_mode and outer_mode.
925 The corresponding induction variable is
927 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
929 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
930 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
931 iv
->mode
= inner_mode
;
932 iv
->extend_mode
= outer_mode
;
934 iv
->mult
= const1_rtx
;
935 iv
->delta
= outer_step
;
936 iv
->first_special
= inner_mode
!= outer_mode
;
941 fprintf (dump_file
, " ");
942 dump_iv_info (dump_file
, iv
);
943 fprintf (dump_file
, "\n");
946 record_biv (def
, iv
);
947 return iv
->base
!= NULL_RTX
;
950 /* Analyzes expression RHS used at INSN and stores the result to *IV.
951 The mode of the induction variable is MODE. */
954 iv_analyze_expr (rtx_insn
*insn
, rtx rhs
, machine_mode mode
,
958 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
959 struct rtx_iv iv0
, iv1
;
960 enum rtx_code code
= GET_CODE (rhs
);
961 machine_mode omode
= mode
;
967 gcc_assert (GET_MODE (rhs
) == mode
|| GET_MODE (rhs
) == VOIDmode
);
973 if (!iv_analyze_op (insn
, rhs
, iv
))
976 if (iv
->mode
== VOIDmode
)
979 iv
->extend_mode
= mode
;
995 omode
= GET_MODE (op0
);
1000 op0
= XEXP (rhs
, 0);
1001 op1
= XEXP (rhs
, 1);
1005 op0
= XEXP (rhs
, 0);
1006 mby
= XEXP (rhs
, 1);
1007 if (!CONSTANT_P (mby
))
1008 std::swap (op0
, mby
);
1009 if (!CONSTANT_P (mby
))
1014 op0
= XEXP (rhs
, 0);
1015 mby
= XEXP (rhs
, 1);
1016 if (!CONSTANT_P (mby
))
1025 && !iv_analyze_expr (insn
, op0
, omode
, &iv0
))
1029 && !iv_analyze_expr (insn
, op1
, omode
, &iv1
))
1035 if (!iv_extend (&iv0
, IV_SIGN_EXTEND
, mode
))
1040 if (!iv_extend (&iv0
, IV_ZERO_EXTEND
, mode
))
1051 if (!iv_add (&iv0
, &iv1
, code
))
1056 if (!iv_mult (&iv0
, mby
))
1061 if (!iv_shift (&iv0
, mby
))
1070 return iv
->base
!= NULL_RTX
;
1073 /* Analyzes iv DEF and stores the result to *IV. */
1076 iv_analyze_def (df_ref def
, struct rtx_iv
*iv
)
1078 rtx_insn
*insn
= DF_REF_INSN (def
);
1079 rtx reg
= DF_REF_REG (def
);
1084 fprintf (dump_file
, "Analyzing def of ");
1085 print_rtl (dump_file
, reg
);
1086 fprintf (dump_file
, " in insn ");
1087 print_rtl_single (dump_file
, insn
);
1090 check_iv_ref_table_size ();
1091 if (DF_REF_IV (def
))
1094 fprintf (dump_file
, " already analysed.\n");
1095 *iv
= *DF_REF_IV (def
);
1096 return iv
->base
!= NULL_RTX
;
1099 iv
->mode
= VOIDmode
;
1100 iv
->base
= NULL_RTX
;
1101 iv
->step
= NULL_RTX
;
1106 set
= single_set (insn
);
1110 if (!REG_P (SET_DEST (set
)))
1113 gcc_assert (SET_DEST (set
) == reg
);
1114 rhs
= find_reg_equal_equiv_note (insn
);
1116 rhs
= XEXP (rhs
, 0);
1118 rhs
= SET_SRC (set
);
1120 iv_analyze_expr (insn
, rhs
, GET_MODE (reg
), iv
);
1121 record_iv (def
, iv
);
1125 print_rtl (dump_file
, reg
);
1126 fprintf (dump_file
, " in insn ");
1127 print_rtl_single (dump_file
, insn
);
1128 fprintf (dump_file
, " is ");
1129 dump_iv_info (dump_file
, iv
);
1130 fprintf (dump_file
, "\n");
1133 return iv
->base
!= NULL_RTX
;
1136 /* Analyzes operand OP of INSN and stores the result to *IV. */
1139 iv_analyze_op (rtx_insn
*insn
, rtx op
, struct rtx_iv
*iv
)
1142 enum iv_grd_result res
;
1146 fprintf (dump_file
, "Analyzing operand ");
1147 print_rtl (dump_file
, op
);
1148 fprintf (dump_file
, " of insn ");
1149 print_rtl_single (dump_file
, insn
);
1152 if (function_invariant_p (op
))
1153 res
= GRD_INVARIANT
;
1154 else if (GET_CODE (op
) == SUBREG
)
1156 if (!subreg_lowpart_p (op
))
1159 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
1162 return iv_subreg (iv
, GET_MODE (op
));
1166 res
= iv_get_reaching_def (insn
, op
, &def
);
1167 if (res
== GRD_INVALID
)
1170 fprintf (dump_file
, " not simple.\n");
1175 if (res
== GRD_INVARIANT
)
1177 iv_constant (iv
, op
, VOIDmode
);
1181 fprintf (dump_file
, " ");
1182 dump_iv_info (dump_file
, iv
);
1183 fprintf (dump_file
, "\n");
1188 if (res
== GRD_MAYBE_BIV
)
1189 return iv_analyze_biv (op
, iv
);
1191 return iv_analyze_def (def
, iv
);
1194 /* Analyzes value VAL at INSN and stores the result to *IV. */
1197 iv_analyze (rtx_insn
*insn
, rtx val
, struct rtx_iv
*iv
)
1201 /* We must find the insn in that val is used, so that we get to UD chains.
1202 Since the function is sometimes called on result of get_condition,
1203 this does not necessarily have to be directly INSN; scan also the
1205 if (simple_reg_p (val
))
1207 if (GET_CODE (val
) == SUBREG
)
1208 reg
= SUBREG_REG (val
);
1212 while (!df_find_use (insn
, reg
))
1213 insn
= NEXT_INSN (insn
);
1216 return iv_analyze_op (insn
, val
, iv
);
1219 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1222 iv_analyze_result (rtx_insn
*insn
, rtx def
, struct rtx_iv
*iv
)
1226 adef
= df_find_def (insn
, def
);
1230 return iv_analyze_def (adef
, iv
);
1233 /* Checks whether definition of register REG in INSN is a basic induction
1234 variable. IV analysis must have been initialized (via a call to
1235 iv_analysis_loop_init) for this function to produce a result. */
1238 biv_p (rtx_insn
*insn
, rtx reg
)
1241 df_ref def
, last_def
;
1243 if (!simple_reg_p (reg
))
1246 def
= df_find_def (insn
, reg
);
1247 gcc_assert (def
!= NULL
);
1248 if (!latch_dominating_def (reg
, &last_def
))
1250 if (last_def
!= def
)
1253 if (!iv_analyze_biv (reg
, &iv
))
1256 return iv
.step
!= const0_rtx
;
1259 /* Calculates value of IV at ITERATION-th iteration. */
1262 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1266 /* We would need to generate some if_then_else patterns, and so far
1267 it is not needed anywhere. */
1268 gcc_assert (!iv
->first_special
);
1270 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1271 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1272 simplify_gen_binary (MULT
, iv
->extend_mode
,
1273 iv
->step
, iteration
));
1277 if (iv
->extend_mode
== iv
->mode
)
1280 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1282 if (iv
->extend
== IV_UNKNOWN_EXTEND
)
1285 val
= simplify_gen_unary (iv_extend_to_rtx_code (iv
->extend
),
1286 iv
->extend_mode
, val
, iv
->mode
);
1287 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1288 simplify_gen_binary (MULT
, iv
->extend_mode
,
1294 /* Free the data for an induction variable analysis. */
1297 iv_analysis_done (void)
1303 df_finish_pass (true);
1306 free (iv_ref_table
);
1307 iv_ref_table
= NULL
;
1308 iv_ref_table_size
= 0;
1312 /* Computes inverse to X modulo (1 << MOD). */
1315 inverse (uint64_t x
, int mod
)
1318 ((uint64_t) 1 << (mod
- 1) << 1) - 1;
1322 for (i
= 0; i
< mod
- 1; i
++)
1324 rslt
= (rslt
* x
) & mask
;
1331 /* Checks whether any register in X is in set ALT. */
1334 altered_reg_used (const_rtx x
, bitmap alt
)
1336 subrtx_iterator::array_type array
;
1337 FOR_EACH_SUBRTX (iter
, array
, x
, NONCONST
)
1339 const_rtx x
= *iter
;
1340 if (REG_P (x
) && REGNO_REG_SET_P (alt
, REGNO (x
)))
1346 /* Marks registers altered by EXPR in set ALT. */
1349 mark_altered (rtx expr
, const_rtx by ATTRIBUTE_UNUSED
, void *alt
)
1351 if (GET_CODE (expr
) == SUBREG
)
1352 expr
= SUBREG_REG (expr
);
1356 SET_REGNO_REG_SET ((bitmap
) alt
, REGNO (expr
));
1359 /* Checks whether RHS is simple enough to process. */
1362 simple_rhs_p (rtx rhs
)
1366 if (function_invariant_p (rhs
)
1367 || (REG_P (rhs
) && !HARD_REGISTER_P (rhs
)))
1370 switch (GET_CODE (rhs
))
1375 op0
= XEXP (rhs
, 0);
1376 op1
= XEXP (rhs
, 1);
1377 /* Allow reg OP const and reg OP reg. */
1378 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
))
1379 && !function_invariant_p (op0
))
1381 if (!(REG_P (op1
) && !HARD_REGISTER_P (op1
))
1382 && !function_invariant_p (op1
))
1391 op0
= XEXP (rhs
, 0);
1392 op1
= XEXP (rhs
, 1);
1393 /* Allow reg OP const. */
1394 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
)))
1396 if (!function_invariant_p (op1
))
1406 /* If REGNO has a single definition, return its known value, otherwise return
1410 find_single_def_src (unsigned int regno
)
1418 adef
= DF_REG_DEF_CHAIN (regno
);
1419 if (adef
== NULL
|| DF_REF_NEXT_REG (adef
) != NULL
1420 || DF_REF_IS_ARTIFICIAL (adef
))
1423 set
= single_set (DF_REF_INSN (adef
));
1424 if (set
== NULL
|| !REG_P (SET_DEST (set
))
1425 || REGNO (SET_DEST (set
)) != regno
)
1428 note
= find_reg_equal_equiv_note (DF_REF_INSN (adef
));
1430 if (note
&& function_invariant_p (XEXP (note
, 0)))
1432 src
= XEXP (note
, 0);
1435 src
= SET_SRC (set
);
1439 regno
= REGNO (src
);
1444 if (!function_invariant_p (src
))
1450 /* If any registers in *EXPR that have a single definition, try to replace
1451 them with the known-equivalent values. */
1454 replace_single_def_regs (rtx
*expr
)
1456 subrtx_var_iterator::array_type array
;
1458 FOR_EACH_SUBRTX_VAR (iter
, array
, *expr
, NONCONST
)
1462 if (rtx new_x
= find_single_def_src (REGNO (x
)))
1464 *expr
= simplify_replace_rtx (*expr
, x
, new_x
);
1470 /* A subroutine of simplify_using_initial_values, this function examines INSN
1471 to see if it contains a suitable set that we can use to make a replacement.
1472 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1473 the set; return false otherwise. */
1476 suitable_set_for_replacement (rtx_insn
*insn
, rtx
*dest
, rtx
*src
)
1478 rtx set
= single_set (insn
);
1479 rtx lhs
= NULL_RTX
, rhs
;
1484 lhs
= SET_DEST (set
);
1488 rhs
= find_reg_equal_equiv_note (insn
);
1490 rhs
= XEXP (rhs
, 0);
1492 rhs
= SET_SRC (set
);
1494 if (!simple_rhs_p (rhs
))
1502 /* Using the data returned by suitable_set_for_replacement, replace DEST
1503 with SRC in *EXPR and return the new expression. Also call
1504 replace_single_def_regs if the replacement changed something. */
1506 replace_in_expr (rtx
*expr
, rtx dest
, rtx src
)
1509 *expr
= simplify_replace_rtx (*expr
, dest
, src
);
1512 replace_single_def_regs (expr
);
1515 /* Checks whether A implies B. */
1518 implies_p (rtx a
, rtx b
)
1520 rtx op0
, op1
, opb0
, opb1
;
1523 if (rtx_equal_p (a
, b
))
1526 if (GET_CODE (a
) == EQ
)
1532 || (GET_CODE (op0
) == SUBREG
1533 && REG_P (SUBREG_REG (op0
))))
1535 rtx r
= simplify_replace_rtx (b
, op0
, op1
);
1536 if (r
== const_true_rtx
)
1541 || (GET_CODE (op1
) == SUBREG
1542 && REG_P (SUBREG_REG (op1
))))
1544 rtx r
= simplify_replace_rtx (b
, op1
, op0
);
1545 if (r
== const_true_rtx
)
1550 if (b
== const_true_rtx
)
1553 if ((GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMM_COMPARE
1554 && GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMPARE
)
1555 || (GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMM_COMPARE
1556 && GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMPARE
))
1564 mode
= GET_MODE (op0
);
1565 if (mode
!= GET_MODE (opb0
))
1567 else if (mode
== VOIDmode
)
1569 mode
= GET_MODE (op1
);
1570 if (mode
!= GET_MODE (opb1
))
1574 /* A < B implies A + 1 <= B. */
1575 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1576 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1579 if (GET_CODE (a
) == GT
)
1580 std::swap (op0
, op1
);
1582 if (GET_CODE (b
) == GE
)
1583 std::swap (opb0
, opb1
);
1585 if (SCALAR_INT_MODE_P (mode
)
1586 && rtx_equal_p (op1
, opb1
)
1587 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1592 /* A < B or A > B imply A != B. TODO: Likewise
1593 A + n < B implies A != B + n if neither wraps. */
1594 if (GET_CODE (b
) == NE
1595 && (GET_CODE (a
) == GT
|| GET_CODE (a
) == GTU
1596 || GET_CODE (a
) == LT
|| GET_CODE (a
) == LTU
))
1598 if (rtx_equal_p (op0
, opb0
)
1599 && rtx_equal_p (op1
, opb1
))
1603 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1604 if (GET_CODE (a
) == NE
1605 && op1
== const0_rtx
)
1607 if ((GET_CODE (b
) == GTU
1608 && opb1
== const0_rtx
)
1609 || (GET_CODE (b
) == GEU
1610 && opb1
== const1_rtx
))
1611 return rtx_equal_p (op0
, opb0
);
1614 /* A != N is equivalent to A - (N + 1) <u -1. */
1615 if (GET_CODE (a
) == NE
1616 && CONST_INT_P (op1
)
1617 && GET_CODE (b
) == LTU
1618 && opb1
== constm1_rtx
1619 && GET_CODE (opb0
) == PLUS
1620 && CONST_INT_P (XEXP (opb0
, 1))
1621 /* Avoid overflows. */
1622 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1623 != ((unsigned HOST_WIDE_INT
)1
1624 << (HOST_BITS_PER_WIDE_INT
- 1)) - 1)
1625 && INTVAL (XEXP (opb0
, 1)) + 1 == -INTVAL (op1
))
1626 return rtx_equal_p (op0
, XEXP (opb0
, 0));
1628 /* Likewise, A != N implies A - N > 0. */
1629 if (GET_CODE (a
) == NE
1630 && CONST_INT_P (op1
))
1632 if (GET_CODE (b
) == GTU
1633 && GET_CODE (opb0
) == PLUS
1634 && opb1
== const0_rtx
1635 && CONST_INT_P (XEXP (opb0
, 1))
1636 /* Avoid overflows. */
1637 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1638 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1639 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1640 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1641 if (GET_CODE (b
) == GEU
1642 && GET_CODE (opb0
) == PLUS
1643 && opb1
== const1_rtx
1644 && CONST_INT_P (XEXP (opb0
, 1))
1645 /* Avoid overflows. */
1646 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1647 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1648 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1649 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1652 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1653 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == GE
)
1654 && CONST_INT_P (op1
)
1655 && ((GET_CODE (a
) == GT
&& op1
== constm1_rtx
)
1656 || INTVAL (op1
) >= 0)
1657 && GET_CODE (b
) == LTU
1658 && CONST_INT_P (opb1
)
1659 && rtx_equal_p (op0
, opb0
))
1660 return INTVAL (opb1
) < 0;
1665 /* Canonicalizes COND so that
1667 (1) Ensure that operands are ordered according to
1668 swap_commutative_operands_p.
1669 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1670 for GE, GEU, and LEU. */
1673 canon_condition (rtx cond
)
1679 code
= GET_CODE (cond
);
1680 op0
= XEXP (cond
, 0);
1681 op1
= XEXP (cond
, 1);
1683 if (swap_commutative_operands_p (op0
, op1
))
1685 code
= swap_condition (code
);
1686 std::swap (op0
, op1
);
1689 mode
= GET_MODE (op0
);
1690 if (mode
== VOIDmode
)
1691 mode
= GET_MODE (op1
);
1692 gcc_assert (mode
!= VOIDmode
);
1694 if (CONST_SCALAR_INT_P (op1
) && GET_MODE_CLASS (mode
) != MODE_CC
)
1696 rtx_mode_t
const_val (op1
, mode
);
1701 if (wi::ne_p (const_val
, wi::max_value (mode
, SIGNED
)))
1704 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1709 if (wi::ne_p (const_val
, wi::min_value (mode
, SIGNED
)))
1712 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1717 if (wi::ne_p (const_val
, -1))
1720 op1
= immed_wide_int_const (wi::add (const_val
, 1), mode
);
1725 if (wi::ne_p (const_val
, 0))
1728 op1
= immed_wide_int_const (wi::sub (const_val
, 1), mode
);
1737 if (op0
!= XEXP (cond
, 0)
1738 || op1
!= XEXP (cond
, 1)
1739 || code
!= GET_CODE (cond
)
1740 || GET_MODE (cond
) != SImode
)
1741 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1746 /* Reverses CONDition; returns NULL if we cannot. */
1749 reversed_condition (rtx cond
)
1751 enum rtx_code reversed
;
1752 reversed
= reversed_comparison_code (cond
, NULL
);
1753 if (reversed
== UNKNOWN
)
1756 return gen_rtx_fmt_ee (reversed
,
1757 GET_MODE (cond
), XEXP (cond
, 0),
1761 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1762 set of altered regs. */
1765 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1767 rtx rev
, reve
, exp
= *expr
;
1769 /* If some register gets altered later, we do not really speak about its
1770 value at the time of comparison. */
1771 if (altered
&& altered_reg_used (cond
, altered
))
1774 if (GET_CODE (cond
) == EQ
1775 && REG_P (XEXP (cond
, 0)) && CONSTANT_P (XEXP (cond
, 1)))
1777 *expr
= simplify_replace_rtx (*expr
, XEXP (cond
, 0), XEXP (cond
, 1));
1781 if (!COMPARISON_P (exp
))
1784 rev
= reversed_condition (cond
);
1785 reve
= reversed_condition (exp
);
1787 cond
= canon_condition (cond
);
1788 exp
= canon_condition (exp
);
1790 rev
= canon_condition (rev
);
1792 reve
= canon_condition (reve
);
1794 if (rtx_equal_p (exp
, cond
))
1796 *expr
= const_true_rtx
;
1800 if (rev
&& rtx_equal_p (exp
, rev
))
1806 if (implies_p (cond
, exp
))
1808 *expr
= const_true_rtx
;
1812 if (reve
&& implies_p (cond
, reve
))
1818 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1820 if (rev
&& implies_p (exp
, rev
))
1826 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1827 if (rev
&& reve
&& implies_p (reve
, rev
))
1829 *expr
= const_true_rtx
;
1833 /* We would like to have some other tests here. TODO. */
1838 /* Use relationship between A and *B to eventually eliminate *B.
1839 OP is the operation we consider. */
1842 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1847 /* If A implies *B, we may replace *B by true. */
1848 if (implies_p (a
, *b
))
1849 *b
= const_true_rtx
;
1853 /* If *B implies A, we may replace *B by false. */
1854 if (implies_p (*b
, a
))
1863 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1864 operation we consider. */
1867 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1871 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1872 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1873 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1874 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1877 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1878 is a list, its elements are assumed to be combined using OP. */
1881 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1883 bool expression_valid
;
1884 rtx head
, tail
, last_valid_expr
;
1885 rtx_expr_list
*cond_list
;
1888 regset altered
, this_altered
;
1894 if (CONSTANT_P (*expr
))
1897 if (GET_CODE (*expr
) == EXPR_LIST
)
1899 head
= XEXP (*expr
, 0);
1900 tail
= XEXP (*expr
, 1);
1902 eliminate_implied_conditions (op
, &head
, tail
);
1907 neutral
= const_true_rtx
;
1912 neutral
= const0_rtx
;
1913 aggr
= const_true_rtx
;
1920 simplify_using_initial_values (loop
, UNKNOWN
, &head
);
1923 XEXP (*expr
, 0) = aggr
;
1924 XEXP (*expr
, 1) = NULL_RTX
;
1927 else if (head
== neutral
)
1930 simplify_using_initial_values (loop
, op
, expr
);
1933 simplify_using_initial_values (loop
, op
, &tail
);
1935 if (tail
&& XEXP (tail
, 0) == aggr
)
1941 XEXP (*expr
, 0) = head
;
1942 XEXP (*expr
, 1) = tail
;
1946 gcc_assert (op
== UNKNOWN
);
1948 replace_single_def_regs (expr
);
1949 if (CONSTANT_P (*expr
))
1952 e
= loop_preheader_edge (loop
);
1953 if (e
->src
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
1956 altered
= ALLOC_REG_SET (®_obstack
);
1957 this_altered
= ALLOC_REG_SET (®_obstack
);
1959 expression_valid
= true;
1960 last_valid_expr
= *expr
;
1964 insn
= BB_END (e
->src
);
1965 if (any_condjump_p (insn
))
1967 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false, true);
1969 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1970 cond
= reversed_condition (cond
);
1974 simplify_using_condition (cond
, expr
, altered
);
1978 if (CONSTANT_P (*expr
))
1980 for (note
= cond_list
; note
; note
= XEXP (note
, 1))
1982 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
1983 if (CONSTANT_P (*expr
))
1987 cond_list
= alloc_EXPR_LIST (0, cond
, cond_list
);
1991 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
1999 CLEAR_REG_SET (this_altered
);
2000 note_stores (PATTERN (insn
), mark_altered
, this_altered
);
2003 /* Kill all call clobbered registers. */
2005 hard_reg_set_iterator hrsi
;
2006 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call
,
2008 SET_REGNO_REG_SET (this_altered
, i
);
2011 if (suitable_set_for_replacement (insn
, &dest
, &src
))
2013 rtx_expr_list
**pnote
, **pnote_next
;
2015 replace_in_expr (expr
, dest
, src
);
2016 if (CONSTANT_P (*expr
))
2019 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2021 rtx_expr_list
*note
= *pnote
;
2022 rtx old_cond
= XEXP (note
, 0);
2024 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2025 replace_in_expr (&XEXP (note
, 0), dest
, src
);
2027 /* We can no longer use a condition that has been simplified
2028 to a constant, and simplify_using_condition will abort if
2030 if (CONSTANT_P (XEXP (note
, 0)))
2032 *pnote
= *pnote_next
;
2034 free_EXPR_LIST_node (note
);
2036 /* Retry simplifications with this condition if either the
2037 expression or the condition changed. */
2038 else if (old_cond
!= XEXP (note
, 0) || old
!= *expr
)
2039 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
2044 rtx_expr_list
**pnote
, **pnote_next
;
2046 /* If we did not use this insn to make a replacement, any overlap
2047 between stores in this insn and our expression will cause the
2048 expression to become invalid. */
2049 if (altered_reg_used (*expr
, this_altered
))
2052 /* Likewise for the conditions. */
2053 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
2055 rtx_expr_list
*note
= *pnote
;
2056 rtx old_cond
= XEXP (note
, 0);
2058 pnote_next
= (rtx_expr_list
**)&XEXP (note
, 1);
2059 if (altered_reg_used (old_cond
, this_altered
))
2061 *pnote
= *pnote_next
;
2063 free_EXPR_LIST_node (note
);
2068 if (CONSTANT_P (*expr
))
2071 IOR_REG_SET (altered
, this_altered
);
2073 /* If the expression now contains regs that have been altered, we
2074 can't return it to the caller. However, it is still valid for
2075 further simplification, so keep searching to see if we can
2076 eventually turn it into a constant. */
2077 if (altered_reg_used (*expr
, altered
))
2078 expression_valid
= false;
2079 if (expression_valid
)
2080 last_valid_expr
= *expr
;
2083 if (!single_pred_p (e
->src
)
2084 || single_pred (e
->src
) == ENTRY_BLOCK_PTR_FOR_FN (cfun
))
2086 e
= single_pred_edge (e
->src
);
2090 free_EXPR_LIST_list (&cond_list
);
2091 if (!CONSTANT_P (*expr
))
2092 *expr
= last_valid_expr
;
2093 FREE_REG_SET (altered
);
2094 FREE_REG_SET (this_altered
);
2097 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2098 that IV occurs as left operands of comparison COND and its signedness
2099 is SIGNED_P to DESC. */
2102 shorten_into_mode (struct rtx_iv
*iv
, machine_mode mode
,
2103 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
2105 rtx mmin
, mmax
, cond_over
, cond_under
;
2107 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
2108 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
2110 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
2119 if (cond_under
!= const0_rtx
)
2121 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2122 if (cond_over
!= const0_rtx
)
2123 desc
->noloop_assumptions
=
2124 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
2131 if (cond_over
!= const0_rtx
)
2133 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2134 if (cond_under
!= const0_rtx
)
2135 desc
->noloop_assumptions
=
2136 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
2140 if (cond_over
!= const0_rtx
)
2142 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2143 if (cond_under
!= const0_rtx
)
2145 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2153 iv
->extend
= signed_p
? IV_SIGN_EXTEND
: IV_ZERO_EXTEND
;
2156 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2157 subregs of the same mode if possible (sometimes it is necessary to add
2158 some assumptions to DESC). */
2161 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
2162 enum rtx_code cond
, struct niter_desc
*desc
)
2164 machine_mode comp_mode
;
2167 /* If the ivs behave specially in the first iteration, or are
2168 added/multiplied after extending, we ignore them. */
2169 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
2171 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
2174 /* If there is some extend, it must match signedness of the comparison. */
2179 if (iv0
->extend
== IV_ZERO_EXTEND
2180 || iv1
->extend
== IV_ZERO_EXTEND
)
2187 if (iv0
->extend
== IV_SIGN_EXTEND
2188 || iv1
->extend
== IV_SIGN_EXTEND
)
2194 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
2195 && iv1
->extend
!= IV_UNKNOWN_EXTEND
2196 && iv0
->extend
!= iv1
->extend
)
2200 if (iv0
->extend
!= IV_UNKNOWN_EXTEND
)
2201 signed_p
= iv0
->extend
== IV_SIGN_EXTEND
;
2202 if (iv1
->extend
!= IV_UNKNOWN_EXTEND
)
2203 signed_p
= iv1
->extend
== IV_SIGN_EXTEND
;
2210 /* Values of both variables should be computed in the same mode. These
2211 might indeed be different, if we have comparison like
2213 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2215 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2216 in different modes. This does not seem impossible to handle, but
2217 it hardly ever occurs in practice.
2219 The only exception is the case when one of operands is invariant.
2220 For example pentium 3 generates comparisons like
2221 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2222 definitely do not want this prevent the optimization. */
2223 comp_mode
= iv0
->extend_mode
;
2224 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
2225 comp_mode
= iv1
->extend_mode
;
2227 if (iv0
->extend_mode
!= comp_mode
)
2229 if (iv0
->mode
!= iv0
->extend_mode
2230 || iv0
->step
!= const0_rtx
)
2233 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2234 comp_mode
, iv0
->base
, iv0
->mode
);
2235 iv0
->extend_mode
= comp_mode
;
2238 if (iv1
->extend_mode
!= comp_mode
)
2240 if (iv1
->mode
!= iv1
->extend_mode
2241 || iv1
->step
!= const0_rtx
)
2244 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2245 comp_mode
, iv1
->base
, iv1
->mode
);
2246 iv1
->extend_mode
= comp_mode
;
2249 /* Check that both ivs belong to a range of a single mode. If one of the
2250 operands is an invariant, we may need to shorten it into the common
2252 if (iv0
->mode
== iv0
->extend_mode
2253 && iv0
->step
== const0_rtx
2254 && iv0
->mode
!= iv1
->mode
)
2255 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
2257 if (iv1
->mode
== iv1
->extend_mode
2258 && iv1
->step
== const0_rtx
2259 && iv0
->mode
!= iv1
->mode
)
2260 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
2262 if (iv0
->mode
!= iv1
->mode
)
2265 desc
->mode
= iv0
->mode
;
2266 desc
->signed_p
= signed_p
;
2271 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2272 result. This function is called from iv_number_of_iterations with
2273 a number of fields in DESC already filled in. OLD_NITER is the original
2274 expression for the number of iterations, before we tried to simplify it. */
2277 determine_max_iter (struct loop
*loop
, struct niter_desc
*desc
, rtx old_niter
)
2279 rtx niter
= desc
->niter_expr
;
2280 rtx mmin
, mmax
, cmp
;
2282 uint64_t andmax
= 0;
2284 /* We used to look for constant operand 0 of AND,
2285 but canonicalization should always make this impossible. */
2286 gcc_checking_assert (GET_CODE (niter
) != AND
2287 || !CONST_INT_P (XEXP (niter
, 0)));
2289 if (GET_CODE (niter
) == AND
2290 && CONST_INT_P (XEXP (niter
, 1)))
2292 andmax
= UINTVAL (XEXP (niter
, 1));
2293 niter
= XEXP (niter
, 0);
2296 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
2297 nmax
= UINTVAL (mmax
) - UINTVAL (mmin
);
2299 if (GET_CODE (niter
) == UDIV
)
2301 if (!CONST_INT_P (XEXP (niter
, 1)))
2303 inc
= INTVAL (XEXP (niter
, 1));
2304 niter
= XEXP (niter
, 0);
2309 /* We could use a binary search here, but for now improving the upper
2310 bound by just one eliminates one important corner case. */
2311 cmp
= simplify_gen_relational (desc
->signed_p
? LT
: LTU
, VOIDmode
,
2312 desc
->mode
, old_niter
, mmax
);
2313 simplify_using_initial_values (loop
, UNKNOWN
, &cmp
);
2314 if (cmp
== const_true_rtx
)
2319 fprintf (dump_file
, ";; improved upper bound by one.\n");
2323 nmax
= MIN (nmax
, andmax
);
2325 fprintf (dump_file
, ";; Determined upper bound %" PRId64
".\n",
2330 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2331 the result into DESC. Very similar to determine_number_of_iterations
2332 (basically its rtl version), complicated by things like subregs. */
2335 iv_number_of_iterations (struct loop
*loop
, rtx_insn
*insn
, rtx condition
,
2336 struct niter_desc
*desc
)
2338 rtx op0
, op1
, delta
, step
, bound
, may_xform
, tmp
, tmp0
, tmp1
;
2339 struct rtx_iv iv0
, iv1
;
2340 rtx assumption
, may_not_xform
;
2342 machine_mode mode
, comp_mode
;
2343 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
2344 uint64_t s
, size
, d
, inv
, max
;
2345 int64_t up
, down
, inc
, step_val
;
2346 int was_sharp
= false;
2350 /* The meaning of these assumptions is this:
2352 then the rest of information does not have to be valid
2353 if noloop_assumptions then the loop does not roll
2354 if infinite then this exit is never used */
2356 desc
->assumptions
= NULL_RTX
;
2357 desc
->noloop_assumptions
= NULL_RTX
;
2358 desc
->infinite
= NULL_RTX
;
2359 desc
->simple_p
= true;
2361 desc
->const_iter
= false;
2362 desc
->niter_expr
= NULL_RTX
;
2364 cond
= GET_CODE (condition
);
2365 gcc_assert (COMPARISON_P (condition
));
2367 mode
= GET_MODE (XEXP (condition
, 0));
2368 if (mode
== VOIDmode
)
2369 mode
= GET_MODE (XEXP (condition
, 1));
2370 /* The constant comparisons should be folded. */
2371 gcc_assert (mode
!= VOIDmode
);
2373 /* We only handle integers or pointers. */
2374 if (GET_MODE_CLASS (mode
) != MODE_INT
2375 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
2378 op0
= XEXP (condition
, 0);
2379 if (!iv_analyze (insn
, op0
, &iv0
))
2381 if (iv0
.extend_mode
== VOIDmode
)
2382 iv0
.mode
= iv0
.extend_mode
= mode
;
2384 op1
= XEXP (condition
, 1);
2385 if (!iv_analyze (insn
, op1
, &iv1
))
2387 if (iv1
.extend_mode
== VOIDmode
)
2388 iv1
.mode
= iv1
.extend_mode
= mode
;
2390 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
2391 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
2394 /* Check condition and normalize it. */
2402 std::swap (iv0
, iv1
);
2403 cond
= swap_condition (cond
);
2415 /* Handle extends. This is relatively nontrivial, so we only try in some
2416 easy cases, when we can canonicalize the ivs (possibly by adding some
2417 assumptions) to shape subreg (base + i * step). This function also fills
2418 in desc->mode and desc->signed_p. */
2420 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2423 comp_mode
= iv0
.extend_mode
;
2425 size
= GET_MODE_PRECISION (mode
);
2426 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2427 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2428 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2430 if (!CONST_INT_P (iv0
.step
) || !CONST_INT_P (iv1
.step
))
2433 /* We can take care of the case of two induction variables chasing each other
2434 if the test is NE. I have never seen a loop using it, but still it is
2436 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2441 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2442 iv1
.step
= const0_rtx
;
2445 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2446 iv1
.step
= lowpart_subreg (mode
, iv1
.step
, comp_mode
);
2448 /* This is either infinite loop or the one that ends immediately, depending
2449 on initial values. Unswitching should remove this kind of conditions. */
2450 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2455 if (iv0
.step
== const0_rtx
)
2456 step_val
= -INTVAL (iv1
.step
);
2458 step_val
= INTVAL (iv0
.step
);
2460 /* Ignore loops of while (i-- < 10) type. */
2464 step_is_pow2
= !(step_val
& (step_val
- 1));
2468 /* We do not care about whether the step is power of two in this
2470 step_is_pow2
= false;
2474 /* Some more condition normalization. We must record some assumptions
2475 due to overflows. */
2480 /* We want to take care only of non-sharp relationals; this is easy,
2481 as in cases the overflow would make the transformation unsafe
2482 the loop does not roll. Seemingly it would make more sense to want
2483 to take care of sharp relationals instead, as NE is more similar to
2484 them, but the problem is that here the transformation would be more
2485 difficult due to possibly infinite loops. */
2486 if (iv0
.step
== const0_rtx
)
2488 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2489 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2491 if (assumption
== const_true_rtx
)
2492 goto zero_iter_simplify
;
2493 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2494 iv0
.base
, const1_rtx
);
2498 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2499 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2501 if (assumption
== const_true_rtx
)
2502 goto zero_iter_simplify
;
2503 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2504 iv1
.base
, constm1_rtx
);
2507 if (assumption
!= const0_rtx
)
2508 desc
->noloop_assumptions
=
2509 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2510 cond
= (cond
== LT
) ? LE
: LEU
;
2512 /* It will be useful to be able to tell the difference once more in
2513 LE -> NE reduction. */
2519 /* Take care of trivially infinite loops. */
2522 if (iv0
.step
== const0_rtx
)
2524 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2525 if (rtx_equal_p (tmp
, mode_mmin
))
2528 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2529 /* Fill in the remaining fields somehow. */
2530 goto zero_iter_simplify
;
2535 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2536 if (rtx_equal_p (tmp
, mode_mmax
))
2539 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2540 /* Fill in the remaining fields somehow. */
2541 goto zero_iter_simplify
;
2546 /* If we can we want to take care of NE conditions instead of size
2547 comparisons, as they are much more friendly (most importantly
2548 this takes care of special handling of loops with step 1). We can
2549 do it if we first check that upper bound is greater or equal to
2550 lower bound, their difference is constant c modulo step and that
2551 there is not an overflow. */
2554 if (iv0
.step
== const0_rtx
)
2555 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2558 step
= lowpart_subreg (mode
, step
, comp_mode
);
2559 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2560 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2561 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2562 may_xform
= const0_rtx
;
2563 may_not_xform
= const_true_rtx
;
2565 if (CONST_INT_P (delta
))
2567 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2569 /* A special case. We have transformed condition of type
2570 for (i = 0; i < 4; i += 4)
2572 for (i = 0; i <= 3; i += 4)
2573 obviously if the test for overflow during that transformation
2574 passed, we cannot overflow here. Most importantly any
2575 loop with sharp end condition and step 1 falls into this
2576 category, so handling this case specially is definitely
2577 worth the troubles. */
2578 may_xform
= const_true_rtx
;
2580 else if (iv0
.step
== const0_rtx
)
2582 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2583 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2584 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2585 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2586 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2588 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2594 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2595 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2596 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2597 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2598 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2600 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2606 if (may_xform
!= const0_rtx
)
2608 /* We perform the transformation always provided that it is not
2609 completely senseless. This is OK, as we would need this assumption
2610 to determine the number of iterations anyway. */
2611 if (may_xform
!= const_true_rtx
)
2613 /* If the step is a power of two and the final value we have
2614 computed overflows, the cycle is infinite. Otherwise it
2615 is nontrivial to compute the number of iterations. */
2617 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2620 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2624 /* We are going to lose some information about upper bound on
2625 number of iterations in this step, so record the information
2627 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2628 if (CONST_INT_P (iv1
.base
))
2629 up
= INTVAL (iv1
.base
);
2631 up
= INTVAL (mode_mmax
) - inc
;
2632 down
= INTVAL (CONST_INT_P (iv0
.base
)
2635 max
= (uint64_t) (up
- down
) / inc
+ 1;
2637 && !desc
->assumptions
)
2638 record_niter_bound (loop
, max
, false, true);
2640 if (iv0
.step
== const0_rtx
)
2642 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2643 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2647 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2648 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2651 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2652 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2653 assumption
= simplify_gen_relational (reverse_condition (cond
),
2654 SImode
, mode
, tmp0
, tmp1
);
2655 if (assumption
== const_true_rtx
)
2656 goto zero_iter_simplify
;
2657 else if (assumption
!= const0_rtx
)
2658 desc
->noloop_assumptions
=
2659 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2664 /* Count the number of iterations. */
2667 /* Everything we do here is just arithmetics modulo size of mode. This
2668 makes us able to do more involved computations of number of iterations
2669 than in other cases. First transform the condition into shape
2670 s * i <> c, with s positive. */
2671 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2672 iv0
.base
= const0_rtx
;
2673 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2674 iv1
.step
= const0_rtx
;
2675 if (INTVAL (iv0
.step
) < 0)
2677 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, comp_mode
);
2678 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, comp_mode
);
2680 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2682 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2683 is infinite. Otherwise, the number of iterations is
2684 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2685 s
= INTVAL (iv0
.step
); d
= 1;
2692 bound
= GEN_INT (((uint64_t) 1 << (size
- 1 ) << 1) - 1);
2694 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2695 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, gen_int_mode (d
, mode
));
2696 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2697 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2699 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, gen_int_mode (d
, mode
));
2700 inv
= inverse (s
, size
);
2701 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, gen_int_mode (inv
, mode
));
2702 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2706 if (iv1
.step
== const0_rtx
)
2707 /* Condition in shape a + s * i <= b
2708 We must know that b + s does not overflow and a <= b + s and then we
2709 can compute number of iterations as (b + s - a) / s. (It might
2710 seem that we in fact could be more clever about testing the b + s
2711 overflow condition using some information about b - a mod s,
2712 but it was already taken into account during LE -> NE transform). */
2715 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2716 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2718 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2719 lowpart_subreg (mode
, step
,
2725 /* If s is power of 2, we know that the loop is infinite if
2726 a % s <= b % s and b + s overflows. */
2727 assumption
= simplify_gen_relational (reverse_condition (cond
),
2731 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2732 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2733 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2734 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2736 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2740 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2743 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2746 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, iv0
.step
);
2747 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2748 assumption
= simplify_gen_relational (reverse_condition (cond
),
2749 SImode
, mode
, tmp0
, tmp
);
2751 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2752 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2756 /* Condition in shape a <= b - s * i
2757 We must know that a - s does not overflow and a - s <= b and then
2758 we can again compute number of iterations as (b - (a - s)) / s. */
2759 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2760 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2761 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2763 bound
= simplify_gen_binary (PLUS
, mode
, mode_mmin
,
2764 lowpart_subreg (mode
, step
, comp_mode
));
2769 /* If s is power of 2, we know that the loop is infinite if
2770 a % s <= b % s and a - s overflows. */
2771 assumption
= simplify_gen_relational (reverse_condition (cond
),
2775 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2776 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2777 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2778 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2780 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2784 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2787 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2790 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2791 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2792 assumption
= simplify_gen_relational (reverse_condition (cond
),
2795 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2796 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2798 if (assumption
== const_true_rtx
)
2799 goto zero_iter_simplify
;
2800 else if (assumption
!= const0_rtx
)
2801 desc
->noloop_assumptions
=
2802 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2803 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2804 desc
->niter_expr
= delta
;
2807 old_niter
= desc
->niter_expr
;
2809 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2810 if (desc
->assumptions
2811 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2813 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2814 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2815 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2817 /* Rerun the simplification. Consider code (created by copying loop headers)
2829 The first pass determines that i = 0, the second pass uses it to eliminate
2830 noloop assumption. */
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 if (desc
->noloop_assumptions
2841 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2844 if (CONST_INT_P (desc
->niter_expr
))
2846 uint64_t val
= INTVAL (desc
->niter_expr
);
2848 desc
->const_iter
= true;
2849 desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2851 && !desc
->assumptions
)
2852 record_niter_bound (loop
, desc
->niter
, false, true);
2856 max
= determine_max_iter (loop
, desc
, old_niter
);
2858 goto zero_iter_simplify
;
2860 && !desc
->assumptions
)
2861 record_niter_bound (loop
, max
, false, true);
2863 /* simplify_using_initial_values does a copy propagation on the registers
2864 in the expression for the number of iterations. This prolongs life
2865 ranges of registers and increases register pressure, and usually
2866 brings no gain (and if it happens to do, the cse pass will take care
2867 of it anyway). So prevent this behavior, unless it enabled us to
2868 derive that the number of iterations is a constant. */
2869 desc
->niter_expr
= old_niter
;
2875 /* Simplify the assumptions. */
2876 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2877 if (desc
->assumptions
2878 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2880 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2884 desc
->const_iter
= true;
2886 record_niter_bound (loop
, 0, true, true);
2887 desc
->noloop_assumptions
= NULL_RTX
;
2888 desc
->niter_expr
= const0_rtx
;
2892 desc
->simple_p
= false;
2896 /* Checks whether E is a simple exit from LOOP and stores its description
2900 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2902 basic_block exit_bb
;
2908 desc
->simple_p
= false;
2910 /* It must belong directly to the loop. */
2911 if (exit_bb
->loop_father
!= loop
)
2914 /* It must be tested (at least) once during any iteration. */
2915 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2918 /* It must end in a simple conditional jump. */
2919 if (!any_condjump_p (BB_END (exit_bb
)))
2922 ein
= EDGE_SUCC (exit_bb
, 0);
2924 ein
= EDGE_SUCC (exit_bb
, 1);
2927 desc
->in_edge
= ein
;
2929 /* Test whether the condition is suitable. */
2930 if (!(condition
= get_condition (BB_END (ein
->src
), &at
, false, false)))
2933 if (ein
->flags
& EDGE_FALLTHRU
)
2935 condition
= reversed_condition (condition
);
2940 /* Check that we are able to determine number of iterations and fill
2941 in information about it. */
2942 iv_number_of_iterations (loop
, at
, condition
, desc
);
2945 /* Finds a simple exit of LOOP and stores its description into DESC. */
2948 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2953 struct niter_desc act
;
2957 desc
->simple_p
= false;
2958 body
= get_loop_body (loop
);
2960 for (i
= 0; i
< loop
->num_nodes
; i
++)
2962 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
2964 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2967 check_simple_exit (loop
, e
, &act
);
2975 /* Prefer constant iterations; the less the better. */
2977 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
2980 /* Also if the actual exit may be infinite, while the old one
2981 not, prefer the old one. */
2982 if (act
.infinite
&& !desc
->infinite
)
2994 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
2995 fprintf (dump_file
, " simple exit %d -> %d\n",
2996 desc
->out_edge
->src
->index
,
2997 desc
->out_edge
->dest
->index
);
2998 if (desc
->assumptions
)
3000 fprintf (dump_file
, " assumptions: ");
3001 print_rtl (dump_file
, desc
->assumptions
);
3002 fprintf (dump_file
, "\n");
3004 if (desc
->noloop_assumptions
)
3006 fprintf (dump_file
, " does not roll if: ");
3007 print_rtl (dump_file
, desc
->noloop_assumptions
);
3008 fprintf (dump_file
, "\n");
3012 fprintf (dump_file
, " infinite if: ");
3013 print_rtl (dump_file
, desc
->infinite
);
3014 fprintf (dump_file
, "\n");
3017 fprintf (dump_file
, " number of iterations: ");
3018 print_rtl (dump_file
, desc
->niter_expr
);
3019 fprintf (dump_file
, "\n");
3021 fprintf (dump_file
, " upper bound: %li\n",
3022 (long)get_max_loop_iterations_int (loop
));
3023 fprintf (dump_file
, " realistic bound: %li\n",
3024 (long)get_estimated_loop_iterations_int (loop
));
3027 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
3033 /* Creates a simple loop description of LOOP if it was not computed
3037 get_simple_loop_desc (struct loop
*loop
)
3039 struct niter_desc
*desc
= simple_loop_desc (loop
);
3044 /* At least desc->infinite is not always initialized by
3045 find_simple_loop_exit. */
3046 desc
= ggc_cleared_alloc
<niter_desc
> ();
3047 iv_analysis_loop_init (loop
);
3048 find_simple_exit (loop
, desc
);
3049 loop
->simple_loop_desc
= desc
;
3051 if (desc
->simple_p
&& (desc
->assumptions
|| desc
->infinite
))
3053 const char *wording
;
3055 /* Assume that no overflow happens and that the loop is finite.
3056 We already warned at the tree level if we ran optimizations there. */
3057 if (!flag_tree_loop_optimize
&& warn_unsafe_loop_optimizations
)
3062 flag_unsafe_loop_optimizations
3063 ? N_("assuming that the loop is not infinite")
3064 : N_("cannot optimize possibly infinite loops");
3065 warning (OPT_Wunsafe_loop_optimizations
, "%s",
3068 if (desc
->assumptions
)
3071 flag_unsafe_loop_optimizations
3072 ? N_("assuming that the loop counter does not overflow")
3073 : N_("cannot optimize loop, the loop counter may overflow");
3074 warning (OPT_Wunsafe_loop_optimizations
, "%s",
3079 if (flag_unsafe_loop_optimizations
)
3081 desc
->assumptions
= NULL_RTX
;
3082 desc
->infinite
= NULL_RTX
;
3089 /* Releases simple loop description for LOOP. */
3092 free_simple_loop_desc (struct loop
*loop
)
3094 struct niter_desc
*desc
= simple_loop_desc (loop
);
3100 loop
->simple_loop_desc
= NULL
;