1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004, 2005, 2006, 2007 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"
57 #include "basic-block.h"
66 /* Possible return values of iv_get_reaching_def. */
70 /* More than one reaching def, or reaching def that does not
74 /* The use is trivial invariant of the loop, i.e. is not changed
78 /* The use is reached by initial value and a value from the
79 previous iteration. */
82 /* The use has single dominating def. */
86 /* Information about a biv. */
90 unsigned regno
; /* The register of the biv. */
91 struct rtx_iv iv
; /* Value of the biv. */
94 static bool clean_slate
= true;
96 static unsigned int iv_ref_table_size
= 0;
98 /* Table of rtx_ivs indexed by the df_ref uid field. */
99 static struct rtx_iv
** iv_ref_table
;
101 /* Induction variable stored at the reference. */
102 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID(REF)]
103 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID(REF)] = (IV)
105 /* The current loop. */
107 static struct loop
*current_loop
;
109 /* Bivs of the current loop. */
113 static bool iv_analyze_op (rtx
, rtx
, struct rtx_iv
*);
115 /* Dumps information about IV to FILE. */
117 extern void dump_iv_info (FILE *, struct rtx_iv
*);
119 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
123 fprintf (file
, "not simple");
127 if (iv
->step
== const0_rtx
128 && !iv
->first_special
)
129 fprintf (file
, "invariant ");
131 print_rtl (file
, iv
->base
);
132 if (iv
->step
!= const0_rtx
)
134 fprintf (file
, " + ");
135 print_rtl (file
, iv
->step
);
136 fprintf (file
, " * iteration");
138 fprintf (file
, " (in %s)", GET_MODE_NAME (iv
->mode
));
140 if (iv
->mode
!= iv
->extend_mode
)
141 fprintf (file
, " %s to %s",
142 rtx_name
[iv
->extend
],
143 GET_MODE_NAME (iv
->extend_mode
));
145 if (iv
->mult
!= const1_rtx
)
147 fprintf (file
, " * ");
148 print_rtl (file
, iv
->mult
);
150 if (iv
->delta
!= const0_rtx
)
152 fprintf (file
, " + ");
153 print_rtl (file
, iv
->delta
);
155 if (iv
->first_special
)
156 fprintf (file
, " (first special)");
159 /* Generates a subreg to get the least significant part of EXPR (in mode
160 INNER_MODE) to OUTER_MODE. */
163 lowpart_subreg (enum machine_mode outer_mode
, rtx expr
,
164 enum machine_mode inner_mode
)
166 return simplify_gen_subreg (outer_mode
, expr
, inner_mode
,
167 subreg_lowpart_offset (outer_mode
, inner_mode
));
171 check_iv_ref_table_size (void)
173 if (iv_ref_table_size
< DF_DEFS_TABLE_SIZE())
175 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
176 iv_ref_table
= xrealloc (iv_ref_table
,
177 sizeof (struct rtx_iv
*) * new_size
);
178 memset (&iv_ref_table
[iv_ref_table_size
], 0,
179 (new_size
- iv_ref_table_size
) * sizeof (struct rtx_iv
*));
180 iv_ref_table_size
= new_size
;
185 /* Checks whether REG is a well-behaved register. */
188 simple_reg_p (rtx reg
)
192 if (GET_CODE (reg
) == SUBREG
)
194 if (!subreg_lowpart_p (reg
))
196 reg
= SUBREG_REG (reg
);
203 if (HARD_REGISTER_NUM_P (r
))
206 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
212 /* Clears the information about ivs stored in df. */
217 unsigned i
, n_defs
= DF_DEFS_TABLE_SIZE ();
220 check_iv_ref_table_size ();
221 for (i
= 0; i
< n_defs
; i
++)
223 iv
= iv_ref_table
[i
];
227 iv_ref_table
[i
] = NULL
;
234 /* Returns hash value for biv B. */
237 biv_hash (const void *b
)
239 return ((const struct biv_entry
*) b
)->regno
;
242 /* Compares biv B and register R. */
245 biv_eq (const void *b
, const void *r
)
247 return ((const struct biv_entry
*) b
)->regno
== REGNO ((const_rtx
) r
);
250 /* Prepare the data for an induction variable analysis of a LOOP. */
253 iv_analysis_loop_init (struct loop
*loop
)
255 basic_block
*body
= get_loop_body_in_dom_order (loop
), bb
;
256 bitmap blocks
= BITMAP_ALLOC (NULL
);
261 /* Clear the information from the analysis of the previous loop. */
264 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
265 bivs
= htab_create (10, biv_hash
, biv_eq
, free
);
271 for (i
= 0; i
< loop
->num_nodes
; i
++)
274 bitmap_set_bit (blocks
, bb
->index
);
276 /* Get rid of the ud chains before processing the rescans. Then add
278 df_remove_problem (df_chain
);
279 df_process_deferred_rescans ();
280 df_chain_add_problem (DF_UD_CHAIN
);
281 df_set_flags (DF_RD_NO_TRIM
);
282 df_set_blocks (blocks
);
285 df_dump_region (dump_file
);
287 check_iv_ref_table_size ();
288 BITMAP_FREE (blocks
);
292 /* Finds the definition of REG that dominates loop latch and stores
293 it to DEF. Returns false if there is not a single definition
294 dominating the latch. If REG has no definition in loop, DEF
295 is set to NULL and true is returned. */
298 latch_dominating_def (rtx reg
, struct df_ref
**def
)
300 struct df_ref
*single_rd
= NULL
, *adef
;
301 unsigned regno
= REGNO (reg
);
302 struct df_rd_bb_info
*bb_info
= DF_RD_BB_INFO (current_loop
->latch
);
304 for (adef
= DF_REG_DEF_CHAIN (regno
); adef
; adef
= adef
->next_reg
)
306 if (!bitmap_bit_p (df
->blocks_to_analyze
, DF_REF_BB (adef
)->index
)
307 || !bitmap_bit_p (bb_info
->out
, DF_REF_ID (adef
)))
310 /* More than one reaching definition. */
314 if (!just_once_each_iteration_p (current_loop
, DF_REF_BB (adef
)))
324 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
326 static enum iv_grd_result
327 iv_get_reaching_def (rtx insn
, rtx reg
, struct df_ref
**def
)
329 struct df_ref
*use
, *adef
;
330 basic_block def_bb
, use_bb
;
335 if (!simple_reg_p (reg
))
337 if (GET_CODE (reg
) == SUBREG
)
338 reg
= SUBREG_REG (reg
);
339 gcc_assert (REG_P (reg
));
341 use
= df_find_use (insn
, reg
);
342 gcc_assert (use
!= NULL
);
344 if (!DF_REF_CHAIN (use
))
345 return GRD_INVARIANT
;
347 /* More than one reaching def. */
348 if (DF_REF_CHAIN (use
)->next
)
351 adef
= DF_REF_CHAIN (use
)->ref
;
353 /* We do not handle setting only part of the register. */
354 if (adef
->flags
& DF_REF_READ_WRITE
)
357 def_insn
= DF_REF_INSN (adef
);
358 def_bb
= DF_REF_BB (adef
);
359 use_bb
= BLOCK_FOR_INSN (insn
);
361 if (use_bb
== def_bb
)
362 dom_p
= (DF_INSN_LUID (def_insn
) < DF_INSN_LUID (insn
));
364 dom_p
= dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
);
369 return GRD_SINGLE_DOM
;
372 /* The definition does not dominate the use. This is still OK if
373 this may be a use of a biv, i.e. if the def_bb dominates loop
375 if (just_once_each_iteration_p (current_loop
, def_bb
))
376 return GRD_MAYBE_BIV
;
381 /* Sets IV to invariant CST in MODE. Always returns true (just for
382 consistency with other iv manipulation functions that may fail). */
385 iv_constant (struct rtx_iv
*iv
, rtx cst
, enum machine_mode mode
)
387 if (mode
== VOIDmode
)
388 mode
= GET_MODE (cst
);
392 iv
->step
= const0_rtx
;
393 iv
->first_special
= false;
394 iv
->extend
= UNKNOWN
;
395 iv
->extend_mode
= iv
->mode
;
396 iv
->delta
= const0_rtx
;
397 iv
->mult
= const1_rtx
;
402 /* Evaluates application of subreg to MODE on IV. */
405 iv_subreg (struct rtx_iv
*iv
, enum machine_mode mode
)
407 /* If iv is invariant, just calculate the new value. */
408 if (iv
->step
== const0_rtx
409 && !iv
->first_special
)
411 rtx val
= get_iv_value (iv
, const0_rtx
);
412 val
= lowpart_subreg (mode
, val
, iv
->extend_mode
);
415 iv
->extend
= UNKNOWN
;
416 iv
->mode
= iv
->extend_mode
= mode
;
417 iv
->delta
= const0_rtx
;
418 iv
->mult
= const1_rtx
;
422 if (iv
->extend_mode
== mode
)
425 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
428 iv
->extend
= UNKNOWN
;
431 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
432 simplify_gen_binary (MULT
, iv
->extend_mode
,
433 iv
->base
, iv
->mult
));
434 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
435 iv
->mult
= const1_rtx
;
436 iv
->delta
= const0_rtx
;
437 iv
->first_special
= false;
442 /* Evaluates application of EXTEND to MODE on IV. */
445 iv_extend (struct rtx_iv
*iv
, enum rtx_code extend
, enum machine_mode mode
)
447 /* If iv is invariant, just calculate the new value. */
448 if (iv
->step
== const0_rtx
449 && !iv
->first_special
)
451 rtx val
= get_iv_value (iv
, const0_rtx
);
452 val
= simplify_gen_unary (extend
, mode
, val
, iv
->extend_mode
);
455 iv
->extend
= UNKNOWN
;
456 iv
->mode
= iv
->extend_mode
= mode
;
457 iv
->delta
= const0_rtx
;
458 iv
->mult
= const1_rtx
;
462 if (mode
!= iv
->extend_mode
)
465 if (iv
->extend
!= UNKNOWN
466 && iv
->extend
!= extend
)
474 /* Evaluates negation of IV. */
477 iv_neg (struct rtx_iv
*iv
)
479 if (iv
->extend
== UNKNOWN
)
481 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
482 iv
->base
, iv
->extend_mode
);
483 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
484 iv
->step
, iv
->extend_mode
);
488 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
489 iv
->delta
, iv
->extend_mode
);
490 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
491 iv
->mult
, iv
->extend_mode
);
497 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
500 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
502 enum machine_mode mode
;
505 /* Extend the constant to extend_mode of the other operand if necessary. */
506 if (iv0
->extend
== UNKNOWN
507 && iv0
->mode
== iv0
->extend_mode
508 && iv0
->step
== const0_rtx
509 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
511 iv0
->extend_mode
= iv1
->extend_mode
;
512 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
513 iv0
->base
, iv0
->mode
);
515 if (iv1
->extend
== UNKNOWN
516 && iv1
->mode
== iv1
->extend_mode
517 && iv1
->step
== const0_rtx
518 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
520 iv1
->extend_mode
= iv0
->extend_mode
;
521 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
522 iv1
->base
, iv1
->mode
);
525 mode
= iv0
->extend_mode
;
526 if (mode
!= iv1
->extend_mode
)
529 if (iv0
->extend
== UNKNOWN
&& iv1
->extend
== UNKNOWN
)
531 if (iv0
->mode
!= iv1
->mode
)
534 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
535 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
540 /* Handle addition of constant. */
541 if (iv1
->extend
== UNKNOWN
543 && iv1
->step
== const0_rtx
)
545 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
549 if (iv0
->extend
== UNKNOWN
551 && iv0
->step
== const0_rtx
)
559 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
566 /* Evaluates multiplication of IV by constant CST. */
569 iv_mult (struct rtx_iv
*iv
, rtx mby
)
571 enum machine_mode mode
= iv
->extend_mode
;
573 if (GET_MODE (mby
) != VOIDmode
574 && GET_MODE (mby
) != mode
)
577 if (iv
->extend
== UNKNOWN
)
579 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
580 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
584 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
585 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
591 /* Evaluates shift of IV by constant CST. */
594 iv_shift (struct rtx_iv
*iv
, rtx mby
)
596 enum machine_mode mode
= iv
->extend_mode
;
598 if (GET_MODE (mby
) != VOIDmode
599 && GET_MODE (mby
) != mode
)
602 if (iv
->extend
== UNKNOWN
)
604 iv
->base
= simplify_gen_binary (ASHIFT
, mode
, iv
->base
, mby
);
605 iv
->step
= simplify_gen_binary (ASHIFT
, mode
, iv
->step
, mby
);
609 iv
->delta
= simplify_gen_binary (ASHIFT
, mode
, iv
->delta
, mby
);
610 iv
->mult
= simplify_gen_binary (ASHIFT
, mode
, iv
->mult
, mby
);
616 /* The recursive part of get_biv_step. Gets the value of the single value
617 defined by DEF wrto initial value of REG inside loop, in shape described
621 get_biv_step_1 (struct df_ref
*def
, rtx reg
,
622 rtx
*inner_step
, enum machine_mode
*inner_mode
,
623 enum rtx_code
*extend
, enum machine_mode outer_mode
,
626 rtx set
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
627 rtx next
, nextr
, tmp
;
629 rtx insn
= DF_REF_INSN (def
);
630 struct df_ref
*next_def
;
631 enum iv_grd_result res
;
633 set
= single_set (insn
);
637 rhs
= find_reg_equal_equiv_note (insn
);
643 code
= GET_CODE (rhs
);
656 if (code
== PLUS
&& CONSTANT_P (op0
))
658 tmp
= op0
; op0
= op1
; op1
= tmp
;
661 if (!simple_reg_p (op0
)
662 || !CONSTANT_P (op1
))
665 if (GET_MODE (rhs
) != outer_mode
)
667 /* ppc64 uses expressions like
669 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
671 this is equivalent to
673 (set x':DI (plus:DI y:DI 1))
674 (set x:SI (subreg:SI (x':DI)). */
675 if (GET_CODE (op0
) != SUBREG
)
677 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
686 if (GET_MODE (rhs
) != outer_mode
)
690 if (!simple_reg_p (op0
))
700 if (GET_CODE (next
) == SUBREG
)
702 if (!subreg_lowpart_p (next
))
705 nextr
= SUBREG_REG (next
);
706 if (GET_MODE (nextr
) != outer_mode
)
712 res
= iv_get_reaching_def (insn
, nextr
, &next_def
);
714 if (res
== GRD_INVALID
|| res
== GRD_INVARIANT
)
717 if (res
== GRD_MAYBE_BIV
)
719 if (!rtx_equal_p (nextr
, reg
))
722 *inner_step
= const0_rtx
;
724 *inner_mode
= outer_mode
;
725 *outer_step
= const0_rtx
;
727 else if (!get_biv_step_1 (next_def
, reg
,
728 inner_step
, inner_mode
, extend
, outer_mode
,
732 if (GET_CODE (next
) == SUBREG
)
734 enum machine_mode amode
= GET_MODE (next
);
736 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
740 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
741 *inner_step
, *outer_step
);
742 *outer_step
= const0_rtx
;
754 if (*inner_mode
== outer_mode
755 /* See comment in previous switch. */
756 || GET_MODE (rhs
) != outer_mode
)
757 *inner_step
= simplify_gen_binary (code
, outer_mode
,
760 *outer_step
= simplify_gen_binary (code
, outer_mode
,
766 gcc_assert (GET_MODE (op0
) == *inner_mode
767 && *extend
== UNKNOWN
768 && *outer_step
== const0_rtx
);
780 /* Gets the operation on register REG inside loop, in shape
782 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
784 If the operation cannot be described in this shape, return false.
785 LAST_DEF is the definition of REG that dominates loop latch. */
788 get_biv_step (struct df_ref
*last_def
, rtx reg
, rtx
*inner_step
,
789 enum machine_mode
*inner_mode
, enum rtx_code
*extend
,
790 enum machine_mode
*outer_mode
, rtx
*outer_step
)
792 *outer_mode
= GET_MODE (reg
);
794 if (!get_biv_step_1 (last_def
, reg
,
795 inner_step
, inner_mode
, extend
, *outer_mode
,
799 gcc_assert ((*inner_mode
== *outer_mode
) != (*extend
!= UNKNOWN
));
800 gcc_assert (*inner_mode
!= *outer_mode
|| *outer_step
== const0_rtx
);
805 /* Records information that DEF is induction variable IV. */
808 record_iv (struct df_ref
*def
, struct rtx_iv
*iv
)
810 struct rtx_iv
*recorded_iv
= XNEW (struct rtx_iv
);
813 check_iv_ref_table_size ();
814 DF_REF_IV_SET (def
, recorded_iv
);
817 /* If DEF was already analyzed for bivness, store the description of the biv to
818 IV and return true. Otherwise return false. */
821 analyzed_for_bivness_p (rtx def
, struct rtx_iv
*iv
)
823 struct biv_entry
*biv
= htab_find_with_hash (bivs
, def
, REGNO (def
));
833 record_biv (rtx def
, struct rtx_iv
*iv
)
835 struct biv_entry
*biv
= XNEW (struct biv_entry
);
836 void **slot
= htab_find_slot_with_hash (bivs
, def
, REGNO (def
), INSERT
);
838 biv
->regno
= REGNO (def
);
844 /* Determines whether DEF is a biv and if so, stores its description
848 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
850 rtx inner_step
, outer_step
;
851 enum machine_mode inner_mode
, outer_mode
;
852 enum rtx_code extend
;
853 struct df_ref
*last_def
;
857 fprintf (dump_file
, "Analyzing ");
858 print_rtl (dump_file
, def
);
859 fprintf (dump_file
, " for bivness.\n");
864 if (!CONSTANT_P (def
))
867 return iv_constant (iv
, def
, VOIDmode
);
870 if (!latch_dominating_def (def
, &last_def
))
873 fprintf (dump_file
, " not simple.\n");
878 return iv_constant (iv
, def
, VOIDmode
);
880 if (analyzed_for_bivness_p (def
, iv
))
883 fprintf (dump_file
, " already analysed.\n");
884 return iv
->base
!= NULL_RTX
;
887 if (!get_biv_step (last_def
, def
, &inner_step
, &inner_mode
, &extend
,
888 &outer_mode
, &outer_step
))
894 /* Loop transforms base to es (base + inner_step) + outer_step,
895 where es means extend of subreg between inner_mode and outer_mode.
896 The corresponding induction variable is
898 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
900 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
901 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
902 iv
->mode
= inner_mode
;
903 iv
->extend_mode
= outer_mode
;
905 iv
->mult
= const1_rtx
;
906 iv
->delta
= outer_step
;
907 iv
->first_special
= inner_mode
!= outer_mode
;
912 fprintf (dump_file
, " ");
913 dump_iv_info (dump_file
, iv
);
914 fprintf (dump_file
, "\n");
917 record_biv (def
, iv
);
918 return iv
->base
!= NULL_RTX
;
921 /* Analyzes expression RHS used at INSN and stores the result to *IV.
922 The mode of the induction variable is MODE. */
925 iv_analyze_expr (rtx insn
, rtx rhs
, enum machine_mode mode
, struct rtx_iv
*iv
)
927 rtx mby
= NULL_RTX
, tmp
;
928 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
929 struct rtx_iv iv0
, iv1
;
930 enum rtx_code code
= GET_CODE (rhs
);
931 enum machine_mode omode
= mode
;
937 gcc_assert (GET_MODE (rhs
) == mode
|| GET_MODE (rhs
) == VOIDmode
);
943 if (!iv_analyze_op (insn
, rhs
, iv
))
946 if (iv
->mode
== VOIDmode
)
949 iv
->extend_mode
= mode
;
965 omode
= GET_MODE (op0
);
977 if (!CONSTANT_P (mby
))
983 if (!CONSTANT_P (mby
))
990 if (!CONSTANT_P (mby
))
999 && !iv_analyze_expr (insn
, op0
, omode
, &iv0
))
1003 && !iv_analyze_expr (insn
, op1
, omode
, &iv1
))
1010 if (!iv_extend (&iv0
, code
, mode
))
1021 if (!iv_add (&iv0
, &iv1
, code
))
1026 if (!iv_mult (&iv0
, mby
))
1031 if (!iv_shift (&iv0
, mby
))
1040 return iv
->base
!= NULL_RTX
;
1043 /* Analyzes iv DEF and stores the result to *IV. */
1046 iv_analyze_def (struct df_ref
*def
, struct rtx_iv
*iv
)
1048 rtx insn
= DF_REF_INSN (def
);
1049 rtx reg
= DF_REF_REG (def
);
1054 fprintf (dump_file
, "Analyzing def of ");
1055 print_rtl (dump_file
, reg
);
1056 fprintf (dump_file
, " in insn ");
1057 print_rtl_single (dump_file
, insn
);
1060 check_iv_ref_table_size ();
1061 if (DF_REF_IV (def
))
1064 fprintf (dump_file
, " already analysed.\n");
1065 *iv
= *DF_REF_IV (def
);
1066 return iv
->base
!= NULL_RTX
;
1069 iv
->mode
= VOIDmode
;
1070 iv
->base
= NULL_RTX
;
1071 iv
->step
= NULL_RTX
;
1076 set
= single_set (insn
);
1080 if (!REG_P (SET_DEST (set
)))
1083 gcc_assert (SET_DEST (set
) == reg
);
1084 rhs
= find_reg_equal_equiv_note (insn
);
1086 rhs
= XEXP (rhs
, 0);
1088 rhs
= SET_SRC (set
);
1090 iv_analyze_expr (insn
, rhs
, GET_MODE (reg
), iv
);
1091 record_iv (def
, iv
);
1095 print_rtl (dump_file
, reg
);
1096 fprintf (dump_file
, " in insn ");
1097 print_rtl_single (dump_file
, insn
);
1098 fprintf (dump_file
, " is ");
1099 dump_iv_info (dump_file
, iv
);
1100 fprintf (dump_file
, "\n");
1103 return iv
->base
!= NULL_RTX
;
1106 /* Analyzes operand OP of INSN and stores the result to *IV. */
1109 iv_analyze_op (rtx insn
, rtx op
, struct rtx_iv
*iv
)
1111 struct df_ref
*def
= NULL
;
1112 enum iv_grd_result res
;
1116 fprintf (dump_file
, "Analyzing operand ");
1117 print_rtl (dump_file
, op
);
1118 fprintf (dump_file
, " of insn ");
1119 print_rtl_single (dump_file
, insn
);
1122 if (CONSTANT_P (op
))
1123 res
= GRD_INVARIANT
;
1124 else if (GET_CODE (op
) == SUBREG
)
1126 if (!subreg_lowpart_p (op
))
1129 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
1132 return iv_subreg (iv
, GET_MODE (op
));
1136 res
= iv_get_reaching_def (insn
, op
, &def
);
1137 if (res
== GRD_INVALID
)
1140 fprintf (dump_file
, " not simple.\n");
1145 if (res
== GRD_INVARIANT
)
1147 iv_constant (iv
, op
, VOIDmode
);
1151 fprintf (dump_file
, " ");
1152 dump_iv_info (dump_file
, iv
);
1153 fprintf (dump_file
, "\n");
1158 if (res
== GRD_MAYBE_BIV
)
1159 return iv_analyze_biv (op
, iv
);
1161 return iv_analyze_def (def
, iv
);
1164 /* Analyzes value VAL at INSN and stores the result to *IV. */
1167 iv_analyze (rtx insn
, rtx val
, struct rtx_iv
*iv
)
1171 /* We must find the insn in that val is used, so that we get to UD chains.
1172 Since the function is sometimes called on result of get_condition,
1173 this does not necessarily have to be directly INSN; scan also the
1175 if (simple_reg_p (val
))
1177 if (GET_CODE (val
) == SUBREG
)
1178 reg
= SUBREG_REG (val
);
1182 while (!df_find_use (insn
, reg
))
1183 insn
= NEXT_INSN (insn
);
1186 return iv_analyze_op (insn
, val
, iv
);
1189 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1192 iv_analyze_result (rtx insn
, rtx def
, struct rtx_iv
*iv
)
1194 struct df_ref
*adef
;
1196 adef
= df_find_def (insn
, def
);
1200 return iv_analyze_def (adef
, iv
);
1203 /* Checks whether definition of register REG in INSN is a basic induction
1204 variable. IV analysis must have been initialized (via a call to
1205 iv_analysis_loop_init) for this function to produce a result. */
1208 biv_p (rtx insn
, rtx reg
)
1211 struct df_ref
*def
, *last_def
;
1213 if (!simple_reg_p (reg
))
1216 def
= df_find_def (insn
, reg
);
1217 gcc_assert (def
!= NULL
);
1218 if (!latch_dominating_def (reg
, &last_def
))
1220 if (last_def
!= def
)
1223 if (!iv_analyze_biv (reg
, &iv
))
1226 return iv
.step
!= const0_rtx
;
1229 /* Calculates value of IV at ITERATION-th iteration. */
1232 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1236 /* We would need to generate some if_then_else patterns, and so far
1237 it is not needed anywhere. */
1238 gcc_assert (!iv
->first_special
);
1240 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1241 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1242 simplify_gen_binary (MULT
, iv
->extend_mode
,
1243 iv
->step
, iteration
));
1247 if (iv
->extend_mode
== iv
->mode
)
1250 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1252 if (iv
->extend
== UNKNOWN
)
1255 val
= simplify_gen_unary (iv
->extend
, iv
->extend_mode
, val
, iv
->mode
);
1256 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1257 simplify_gen_binary (MULT
, iv
->extend_mode
,
1263 /* Free the data for an induction variable analysis. */
1266 iv_analysis_done (void)
1272 df_finish_pass (true);
1274 free (iv_ref_table
);
1275 iv_ref_table
= NULL
;
1276 iv_ref_table_size
= 0;
1281 /* Computes inverse to X modulo (1 << MOD). */
1283 static unsigned HOST_WIDEST_INT
1284 inverse (unsigned HOST_WIDEST_INT x
, int mod
)
1286 unsigned HOST_WIDEST_INT mask
=
1287 ((unsigned HOST_WIDEST_INT
) 1 << (mod
- 1) << 1) - 1;
1288 unsigned HOST_WIDEST_INT rslt
= 1;
1291 for (i
= 0; i
< mod
- 1; i
++)
1293 rslt
= (rslt
* x
) & mask
;
1300 /* Checks whether register *REG is in set ALT. Callback for for_each_rtx. */
1303 altered_reg_used (rtx
*reg
, void *alt
)
1308 return REGNO_REG_SET_P (alt
, REGNO (*reg
));
1311 /* Marks registers altered by EXPR in set ALT. */
1314 mark_altered (rtx expr
, const_rtx by ATTRIBUTE_UNUSED
, void *alt
)
1316 if (GET_CODE (expr
) == SUBREG
)
1317 expr
= SUBREG_REG (expr
);
1321 SET_REGNO_REG_SET (alt
, REGNO (expr
));
1324 /* Checks whether RHS is simple enough to process. */
1327 simple_rhs_p (rtx rhs
)
1331 if (CONSTANT_P (rhs
)
1332 || (REG_P (rhs
) && !HARD_REGISTER_P (rhs
)))
1335 switch (GET_CODE (rhs
))
1339 op0
= XEXP (rhs
, 0);
1340 op1
= XEXP (rhs
, 1);
1341 /* Allow reg + const sets only. */
1342 if (REG_P (op0
) && !HARD_REGISTER_P (op0
) && CONSTANT_P (op1
))
1344 if (REG_P (op1
) && !HARD_REGISTER_P (op1
) && CONSTANT_P (op0
))
1354 /* Simplifies *EXPR using assignment in INSN. ALTERED is the set of registers
1358 simplify_using_assignment (rtx insn
, rtx
*expr
, regset altered
)
1360 rtx set
= single_set (insn
);
1361 rtx lhs
= NULL_RTX
, rhs
;
1366 lhs
= SET_DEST (set
);
1368 || altered_reg_used (&lhs
, altered
))
1374 note_stores (PATTERN (insn
), mark_altered
, altered
);
1379 /* Kill all call clobbered registers. */
1380 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1381 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
1382 SET_REGNO_REG_SET (altered
, i
);
1388 rhs
= find_reg_equal_equiv_note (insn
);
1390 rhs
= XEXP (rhs
, 0);
1392 rhs
= SET_SRC (set
);
1394 if (!simple_rhs_p (rhs
))
1397 if (for_each_rtx (&rhs
, altered_reg_used
, altered
))
1400 *expr
= simplify_replace_rtx (*expr
, lhs
, rhs
);
1403 /* Checks whether A implies B. */
1406 implies_p (rtx a
, rtx b
)
1408 rtx op0
, op1
, opb0
, opb1
, r
;
1409 enum machine_mode mode
;
1411 if (GET_CODE (a
) == EQ
)
1418 r
= simplify_replace_rtx (b
, op0
, op1
);
1419 if (r
== const_true_rtx
)
1425 r
= simplify_replace_rtx (b
, op1
, op0
);
1426 if (r
== const_true_rtx
)
1431 if (b
== const_true_rtx
)
1434 if ((GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMM_COMPARE
1435 && GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMPARE
)
1436 || (GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMM_COMPARE
1437 && GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMPARE
))
1445 mode
= GET_MODE (op0
);
1446 if (mode
!= GET_MODE (opb0
))
1448 else if (mode
== VOIDmode
)
1450 mode
= GET_MODE (op1
);
1451 if (mode
!= GET_MODE (opb1
))
1455 /* A < B implies A + 1 <= B. */
1456 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1457 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1460 if (GET_CODE (a
) == GT
)
1467 if (GET_CODE (b
) == GE
)
1474 if (SCALAR_INT_MODE_P (mode
)
1475 && rtx_equal_p (op1
, opb1
)
1476 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1481 /* A < B or A > B imply A != B. TODO: Likewise
1482 A + n < B implies A != B + n if neither wraps. */
1483 if (GET_CODE (b
) == NE
1484 && (GET_CODE (a
) == GT
|| GET_CODE (a
) == GTU
1485 || GET_CODE (a
) == LT
|| GET_CODE (a
) == LTU
))
1487 if (rtx_equal_p (op0
, opb0
)
1488 && rtx_equal_p (op1
, opb1
))
1492 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1493 if (GET_CODE (a
) == NE
1494 && op1
== const0_rtx
)
1496 if ((GET_CODE (b
) == GTU
1497 && opb1
== const0_rtx
)
1498 || (GET_CODE (b
) == GEU
1499 && opb1
== const1_rtx
))
1500 return rtx_equal_p (op0
, opb0
);
1503 /* A != N is equivalent to A - (N + 1) <u -1. */
1504 if (GET_CODE (a
) == NE
1505 && GET_CODE (op1
) == CONST_INT
1506 && GET_CODE (b
) == LTU
1507 && opb1
== constm1_rtx
1508 && GET_CODE (opb0
) == PLUS
1509 && GET_CODE (XEXP (opb0
, 1)) == CONST_INT
1510 /* Avoid overflows. */
1511 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1512 != ((unsigned HOST_WIDE_INT
)1
1513 << (HOST_BITS_PER_WIDE_INT
- 1)) - 1)
1514 && INTVAL (XEXP (opb0
, 1)) + 1 == -INTVAL (op1
))
1515 return rtx_equal_p (op0
, XEXP (opb0
, 0));
1517 /* Likewise, A != N implies A - N > 0. */
1518 if (GET_CODE (a
) == NE
1519 && GET_CODE (op1
) == CONST_INT
)
1521 if (GET_CODE (b
) == GTU
1522 && GET_CODE (opb0
) == PLUS
1523 && opb1
== const0_rtx
1524 && GET_CODE (XEXP (opb0
, 1)) == CONST_INT
1525 /* Avoid overflows. */
1526 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1527 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1528 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1529 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1530 if (GET_CODE (b
) == GEU
1531 && GET_CODE (opb0
) == PLUS
1532 && opb1
== const1_rtx
1533 && GET_CODE (XEXP (opb0
, 1)) == CONST_INT
1534 /* Avoid overflows. */
1535 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1536 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1537 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1538 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1541 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1542 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == GE
)
1543 && GET_CODE (op1
) == CONST_INT
1544 && ((GET_CODE (a
) == GT
&& op1
== constm1_rtx
)
1545 || INTVAL (op1
) >= 0)
1546 && GET_CODE (b
) == LTU
1547 && GET_CODE (opb1
) == CONST_INT
)
1548 return INTVAL (opb1
) < 0;
1553 /* Canonicalizes COND so that
1555 (1) Ensure that operands are ordered according to
1556 swap_commutative_operands_p.
1557 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1558 for GE, GEU, and LEU. */
1561 canon_condition (rtx cond
)
1566 enum machine_mode mode
;
1568 code
= GET_CODE (cond
);
1569 op0
= XEXP (cond
, 0);
1570 op1
= XEXP (cond
, 1);
1572 if (swap_commutative_operands_p (op0
, op1
))
1574 code
= swap_condition (code
);
1580 mode
= GET_MODE (op0
);
1581 if (mode
== VOIDmode
)
1582 mode
= GET_MODE (op1
);
1583 gcc_assert (mode
!= VOIDmode
);
1585 if (GET_CODE (op1
) == CONST_INT
1586 && GET_MODE_CLASS (mode
) != MODE_CC
1587 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
1589 HOST_WIDE_INT const_val
= INTVAL (op1
);
1590 unsigned HOST_WIDE_INT uconst_val
= const_val
;
1591 unsigned HOST_WIDE_INT max_val
1592 = (unsigned HOST_WIDE_INT
) GET_MODE_MASK (mode
);
1597 if ((unsigned HOST_WIDE_INT
) const_val
!= max_val
>> 1)
1598 code
= LT
, op1
= gen_int_mode (const_val
+ 1, GET_MODE (op0
));
1601 /* When cross-compiling, const_val might be sign-extended from
1602 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
1604 if ((HOST_WIDE_INT
) (const_val
& max_val
)
1605 != (((HOST_WIDE_INT
) 1
1606 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
1607 code
= GT
, op1
= gen_int_mode (const_val
- 1, mode
);
1611 if (uconst_val
< max_val
)
1612 code
= LTU
, op1
= gen_int_mode (uconst_val
+ 1, mode
);
1616 if (uconst_val
!= 0)
1617 code
= GTU
, op1
= gen_int_mode (uconst_val
- 1, mode
);
1625 if (op0
!= XEXP (cond
, 0)
1626 || op1
!= XEXP (cond
, 1)
1627 || code
!= GET_CODE (cond
)
1628 || GET_MODE (cond
) != SImode
)
1629 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1634 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1635 set of altered regs. */
1638 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1640 rtx rev
, reve
, exp
= *expr
;
1642 if (!COMPARISON_P (exp
))
1645 /* If some register gets altered later, we do not really speak about its
1646 value at the time of comparison. */
1648 && for_each_rtx (&cond
, altered_reg_used
, altered
))
1651 rev
= reversed_condition (cond
);
1652 reve
= reversed_condition (exp
);
1654 cond
= canon_condition (cond
);
1655 exp
= canon_condition (exp
);
1657 rev
= canon_condition (rev
);
1659 reve
= canon_condition (reve
);
1661 if (rtx_equal_p (exp
, cond
))
1663 *expr
= const_true_rtx
;
1668 if (rev
&& rtx_equal_p (exp
, rev
))
1674 if (implies_p (cond
, exp
))
1676 *expr
= const_true_rtx
;
1680 if (reve
&& implies_p (cond
, reve
))
1686 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1688 if (rev
&& implies_p (exp
, rev
))
1694 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1695 if (rev
&& reve
&& implies_p (reve
, rev
))
1697 *expr
= const_true_rtx
;
1701 /* We would like to have some other tests here. TODO. */
1706 /* Use relationship between A and *B to eventually eliminate *B.
1707 OP is the operation we consider. */
1710 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1715 /* If A implies *B, we may replace *B by true. */
1716 if (implies_p (a
, *b
))
1717 *b
= const_true_rtx
;
1721 /* If *B implies A, we may replace *B by false. */
1722 if (implies_p (*b
, a
))
1731 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1732 operation we consider. */
1735 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1739 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1740 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1741 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1742 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1745 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1746 is a list, its elements are assumed to be combined using OP. */
1749 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1751 rtx head
, tail
, insn
;
1759 if (CONSTANT_P (*expr
))
1762 if (GET_CODE (*expr
) == EXPR_LIST
)
1764 head
= XEXP (*expr
, 0);
1765 tail
= XEXP (*expr
, 1);
1767 eliminate_implied_conditions (op
, &head
, tail
);
1772 neutral
= const_true_rtx
;
1777 neutral
= const0_rtx
;
1778 aggr
= const_true_rtx
;
1785 simplify_using_initial_values (loop
, UNKNOWN
, &head
);
1788 XEXP (*expr
, 0) = aggr
;
1789 XEXP (*expr
, 1) = NULL_RTX
;
1792 else if (head
== neutral
)
1795 simplify_using_initial_values (loop
, op
, expr
);
1798 simplify_using_initial_values (loop
, op
, &tail
);
1800 if (tail
&& XEXP (tail
, 0) == aggr
)
1806 XEXP (*expr
, 0) = head
;
1807 XEXP (*expr
, 1) = tail
;
1811 gcc_assert (op
== UNKNOWN
);
1813 e
= loop_preheader_edge (loop
);
1814 if (e
->src
== ENTRY_BLOCK_PTR
)
1817 altered
= ALLOC_REG_SET (®_obstack
);
1821 insn
= BB_END (e
->src
);
1822 if (any_condjump_p (insn
))
1824 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false, true);
1826 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1827 cond
= reversed_condition (cond
);
1830 simplify_using_condition (cond
, expr
, altered
);
1831 if (CONSTANT_P (*expr
))
1833 FREE_REG_SET (altered
);
1839 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
1844 simplify_using_assignment (insn
, expr
, altered
);
1845 if (CONSTANT_P (*expr
))
1847 FREE_REG_SET (altered
);
1850 if (for_each_rtx (expr
, altered_reg_used
, altered
))
1852 FREE_REG_SET (altered
);
1857 if (!single_pred_p (e
->src
)
1858 || single_pred (e
->src
) == ENTRY_BLOCK_PTR
)
1860 e
= single_pred_edge (e
->src
);
1863 FREE_REG_SET (altered
);
1866 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
1867 that IV occurs as left operands of comparison COND and its signedness
1868 is SIGNED_P to DESC. */
1871 shorten_into_mode (struct rtx_iv
*iv
, enum machine_mode mode
,
1872 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
1874 rtx mmin
, mmax
, cond_over
, cond_under
;
1876 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
1877 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
1879 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
1888 if (cond_under
!= const0_rtx
)
1890 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
1891 if (cond_over
!= const0_rtx
)
1892 desc
->noloop_assumptions
=
1893 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
1900 if (cond_over
!= const0_rtx
)
1902 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
1903 if (cond_under
!= const0_rtx
)
1904 desc
->noloop_assumptions
=
1905 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
1909 if (cond_over
!= const0_rtx
)
1911 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
1912 if (cond_under
!= const0_rtx
)
1914 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
1922 iv
->extend
= signed_p
? SIGN_EXTEND
: ZERO_EXTEND
;
1925 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
1926 subregs of the same mode if possible (sometimes it is necessary to add
1927 some assumptions to DESC). */
1930 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
1931 enum rtx_code cond
, struct niter_desc
*desc
)
1933 enum machine_mode comp_mode
;
1936 /* If the ivs behave specially in the first iteration, or are
1937 added/multiplied after extending, we ignore them. */
1938 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
1940 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
1943 /* If there is some extend, it must match signedness of the comparison. */
1948 if (iv0
->extend
== ZERO_EXTEND
1949 || iv1
->extend
== ZERO_EXTEND
)
1956 if (iv0
->extend
== SIGN_EXTEND
1957 || iv1
->extend
== SIGN_EXTEND
)
1963 if (iv0
->extend
!= UNKNOWN
1964 && iv1
->extend
!= UNKNOWN
1965 && iv0
->extend
!= iv1
->extend
)
1969 if (iv0
->extend
!= UNKNOWN
)
1970 signed_p
= iv0
->extend
== SIGN_EXTEND
;
1971 if (iv1
->extend
!= UNKNOWN
)
1972 signed_p
= iv1
->extend
== SIGN_EXTEND
;
1979 /* Values of both variables should be computed in the same mode. These
1980 might indeed be different, if we have comparison like
1982 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
1984 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
1985 in different modes. This does not seem impossible to handle, but
1986 it hardly ever occurs in practice.
1988 The only exception is the case when one of operands is invariant.
1989 For example pentium 3 generates comparisons like
1990 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
1991 definitely do not want this prevent the optimization. */
1992 comp_mode
= iv0
->extend_mode
;
1993 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
1994 comp_mode
= iv1
->extend_mode
;
1996 if (iv0
->extend_mode
!= comp_mode
)
1998 if (iv0
->mode
!= iv0
->extend_mode
1999 || iv0
->step
!= const0_rtx
)
2002 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2003 comp_mode
, iv0
->base
, iv0
->mode
);
2004 iv0
->extend_mode
= comp_mode
;
2007 if (iv1
->extend_mode
!= comp_mode
)
2009 if (iv1
->mode
!= iv1
->extend_mode
2010 || iv1
->step
!= const0_rtx
)
2013 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2014 comp_mode
, iv1
->base
, iv1
->mode
);
2015 iv1
->extend_mode
= comp_mode
;
2018 /* Check that both ivs belong to a range of a single mode. If one of the
2019 operands is an invariant, we may need to shorten it into the common
2021 if (iv0
->mode
== iv0
->extend_mode
2022 && iv0
->step
== const0_rtx
2023 && iv0
->mode
!= iv1
->mode
)
2024 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
2026 if (iv1
->mode
== iv1
->extend_mode
2027 && iv1
->step
== const0_rtx
2028 && iv0
->mode
!= iv1
->mode
)
2029 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
2031 if (iv0
->mode
!= iv1
->mode
)
2034 desc
->mode
= iv0
->mode
;
2035 desc
->signed_p
= signed_p
;
2040 /* Tries to estimate the maximum number of iterations. */
2042 static unsigned HOST_WIDEST_INT
2043 determine_max_iter (struct loop
*loop
, struct niter_desc
*desc
)
2045 rtx niter
= desc
->niter_expr
;
2046 rtx mmin
, mmax
, cmp
;
2047 unsigned HOST_WIDEST_INT nmax
, inc
;
2049 if (GET_CODE (niter
) == AND
2050 && GET_CODE (XEXP (niter
, 0)) == CONST_INT
)
2052 nmax
= INTVAL (XEXP (niter
, 0));
2053 if (!(nmax
& (nmax
+ 1)))
2055 desc
->niter_max
= nmax
;
2060 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
2061 nmax
= INTVAL (mmax
) - INTVAL (mmin
);
2063 if (GET_CODE (niter
) == UDIV
)
2065 if (GET_CODE (XEXP (niter
, 1)) != CONST_INT
)
2067 desc
->niter_max
= nmax
;
2070 inc
= INTVAL (XEXP (niter
, 1));
2071 niter
= XEXP (niter
, 0);
2076 /* We could use a binary search here, but for now improving the upper
2077 bound by just one eliminates one important corner case. */
2078 cmp
= gen_rtx_fmt_ee (desc
->signed_p
? LT
: LTU
, VOIDmode
, niter
, mmax
);
2079 simplify_using_initial_values (loop
, UNKNOWN
, &cmp
);
2080 if (cmp
== const_true_rtx
)
2085 fprintf (dump_file
, ";; improved upper bound by one.\n");
2087 desc
->niter_max
= nmax
/ inc
;
2091 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2092 the result into DESC. Very similar to determine_number_of_iterations
2093 (basically its rtl version), complicated by things like subregs. */
2096 iv_number_of_iterations (struct loop
*loop
, rtx insn
, rtx condition
,
2097 struct niter_desc
*desc
)
2099 rtx op0
, op1
, delta
, step
, bound
, may_xform
, tmp
, tmp0
, tmp1
;
2100 struct rtx_iv iv0
, iv1
, tmp_iv
;
2101 rtx assumption
, may_not_xform
;
2103 enum machine_mode mode
, comp_mode
;
2104 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
2105 unsigned HOST_WIDEST_INT s
, size
, d
, inv
;
2106 HOST_WIDEST_INT up
, down
, inc
, step_val
;
2107 int was_sharp
= false;
2111 /* The meaning of these assumptions is this:
2113 then the rest of information does not have to be valid
2114 if noloop_assumptions then the loop does not roll
2115 if infinite then this exit is never used */
2117 desc
->assumptions
= NULL_RTX
;
2118 desc
->noloop_assumptions
= NULL_RTX
;
2119 desc
->infinite
= NULL_RTX
;
2120 desc
->simple_p
= true;
2122 desc
->const_iter
= false;
2123 desc
->niter_expr
= NULL_RTX
;
2124 desc
->niter_max
= 0;
2126 cond
= GET_CODE (condition
);
2127 gcc_assert (COMPARISON_P (condition
));
2129 mode
= GET_MODE (XEXP (condition
, 0));
2130 if (mode
== VOIDmode
)
2131 mode
= GET_MODE (XEXP (condition
, 1));
2132 /* The constant comparisons should be folded. */
2133 gcc_assert (mode
!= VOIDmode
);
2135 /* We only handle integers or pointers. */
2136 if (GET_MODE_CLASS (mode
) != MODE_INT
2137 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
2140 op0
= XEXP (condition
, 0);
2141 if (!iv_analyze (insn
, op0
, &iv0
))
2143 if (iv0
.extend_mode
== VOIDmode
)
2144 iv0
.mode
= iv0
.extend_mode
= mode
;
2146 op1
= XEXP (condition
, 1);
2147 if (!iv_analyze (insn
, op1
, &iv1
))
2149 if (iv1
.extend_mode
== VOIDmode
)
2150 iv1
.mode
= iv1
.extend_mode
= mode
;
2152 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
2153 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
2156 /* Check condition and normalize it. */
2164 tmp_iv
= iv0
; iv0
= iv1
; iv1
= tmp_iv
;
2165 cond
= swap_condition (cond
);
2177 /* Handle extends. This is relatively nontrivial, so we only try in some
2178 easy cases, when we can canonicalize the ivs (possibly by adding some
2179 assumptions) to shape subreg (base + i * step). This function also fills
2180 in desc->mode and desc->signed_p. */
2182 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2185 comp_mode
= iv0
.extend_mode
;
2187 size
= GET_MODE_BITSIZE (mode
);
2188 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2189 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2190 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2192 if (GET_CODE (iv0
.step
) != CONST_INT
|| GET_CODE (iv1
.step
) != CONST_INT
)
2195 /* We can take care of the case of two induction variables chasing each other
2196 if the test is NE. I have never seen a loop using it, but still it is
2198 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2203 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2204 iv1
.step
= const0_rtx
;
2207 /* This is either infinite loop or the one that ends immediately, depending
2208 on initial values. Unswitching should remove this kind of conditions. */
2209 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2214 if (iv0
.step
== const0_rtx
)
2215 step_val
= -INTVAL (iv1
.step
);
2217 step_val
= INTVAL (iv0
.step
);
2219 /* Ignore loops of while (i-- < 10) type. */
2223 step_is_pow2
= !(step_val
& (step_val
- 1));
2227 /* We do not care about whether the step is power of two in this
2229 step_is_pow2
= false;
2233 /* Some more condition normalization. We must record some assumptions
2234 due to overflows. */
2239 /* We want to take care only of non-sharp relationals; this is easy,
2240 as in cases the overflow would make the transformation unsafe
2241 the loop does not roll. Seemingly it would make more sense to want
2242 to take care of sharp relationals instead, as NE is more similar to
2243 them, but the problem is that here the transformation would be more
2244 difficult due to possibly infinite loops. */
2245 if (iv0
.step
== const0_rtx
)
2247 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2248 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2250 if (assumption
== const_true_rtx
)
2251 goto zero_iter_simplify
;
2252 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2253 iv0
.base
, const1_rtx
);
2257 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2258 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2260 if (assumption
== const_true_rtx
)
2261 goto zero_iter_simplify
;
2262 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2263 iv1
.base
, constm1_rtx
);
2266 if (assumption
!= const0_rtx
)
2267 desc
->noloop_assumptions
=
2268 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2269 cond
= (cond
== LT
) ? LE
: LEU
;
2271 /* It will be useful to be able to tell the difference once more in
2272 LE -> NE reduction. */
2278 /* Take care of trivially infinite loops. */
2281 if (iv0
.step
== const0_rtx
)
2283 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2284 if (rtx_equal_p (tmp
, mode_mmin
))
2287 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2288 /* Fill in the remaining fields somehow. */
2289 goto zero_iter_simplify
;
2294 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2295 if (rtx_equal_p (tmp
, mode_mmax
))
2298 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2299 /* Fill in the remaining fields somehow. */
2300 goto zero_iter_simplify
;
2305 /* If we can we want to take care of NE conditions instead of size
2306 comparisons, as they are much more friendly (most importantly
2307 this takes care of special handling of loops with step 1). We can
2308 do it if we first check that upper bound is greater or equal to
2309 lower bound, their difference is constant c modulo step and that
2310 there is not an overflow. */
2313 if (iv0
.step
== const0_rtx
)
2314 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2317 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2318 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2319 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2320 may_xform
= const0_rtx
;
2321 may_not_xform
= const_true_rtx
;
2323 if (GET_CODE (delta
) == CONST_INT
)
2325 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2327 /* A special case. We have transformed condition of type
2328 for (i = 0; i < 4; i += 4)
2330 for (i = 0; i <= 3; i += 4)
2331 obviously if the test for overflow during that transformation
2332 passed, we cannot overflow here. Most importantly any
2333 loop with sharp end condition and step 1 falls into this
2334 category, so handling this case specially is definitely
2335 worth the troubles. */
2336 may_xform
= const_true_rtx
;
2338 else if (iv0
.step
== const0_rtx
)
2340 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2341 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2342 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2343 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2344 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2346 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2352 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2353 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2354 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2355 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2356 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2358 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2364 if (may_xform
!= const0_rtx
)
2366 /* We perform the transformation always provided that it is not
2367 completely senseless. This is OK, as we would need this assumption
2368 to determine the number of iterations anyway. */
2369 if (may_xform
!= const_true_rtx
)
2371 /* If the step is a power of two and the final value we have
2372 computed overflows, the cycle is infinite. Otherwise it
2373 is nontrivial to compute the number of iterations. */
2375 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2378 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2382 /* We are going to lose some information about upper bound on
2383 number of iterations in this step, so record the information
2385 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2386 if (GET_CODE (iv1
.base
) == CONST_INT
)
2387 up
= INTVAL (iv1
.base
);
2389 up
= INTVAL (mode_mmax
) - inc
;
2390 down
= INTVAL (GET_CODE (iv0
.base
) == CONST_INT
2393 desc
->niter_max
= (up
- down
) / inc
+ 1;
2395 if (iv0
.step
== const0_rtx
)
2397 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2398 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2402 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2403 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2406 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2407 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2408 assumption
= simplify_gen_relational (reverse_condition (cond
),
2409 SImode
, mode
, tmp0
, tmp1
);
2410 if (assumption
== const_true_rtx
)
2411 goto zero_iter_simplify
;
2412 else if (assumption
!= const0_rtx
)
2413 desc
->noloop_assumptions
=
2414 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2419 /* Count the number of iterations. */
2422 /* Everything we do here is just arithmetics modulo size of mode. This
2423 makes us able to do more involved computations of number of iterations
2424 than in other cases. First transform the condition into shape
2425 s * i <> c, with s positive. */
2426 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2427 iv0
.base
= const0_rtx
;
2428 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2429 iv1
.step
= const0_rtx
;
2430 if (INTVAL (iv0
.step
) < 0)
2432 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, mode
);
2433 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, mode
);
2435 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2437 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2438 is infinite. Otherwise, the number of iterations is
2439 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2440 s
= INTVAL (iv0
.step
); d
= 1;
2447 bound
= GEN_INT (((unsigned HOST_WIDEST_INT
) 1 << (size
- 1 ) << 1) - 1);
2449 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2450 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, GEN_INT (d
));
2451 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2452 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2454 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, GEN_INT (d
));
2455 inv
= inverse (s
, size
);
2456 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, gen_int_mode (inv
, mode
));
2457 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2461 if (iv1
.step
== const0_rtx
)
2462 /* Condition in shape a + s * i <= b
2463 We must know that b + s does not overflow and a <= b + s and then we
2464 can compute number of iterations as (b + s - a) / s. (It might
2465 seem that we in fact could be more clever about testing the b + s
2466 overflow condition using some information about b - a mod s,
2467 but it was already taken into account during LE -> NE transform). */
2470 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2471 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2473 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2474 lowpart_subreg (mode
, step
,
2480 /* If s is power of 2, we know that the loop is infinite if
2481 a % s <= b % s and b + s overflows. */
2482 assumption
= simplify_gen_relational (reverse_condition (cond
),
2486 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2487 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2488 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2489 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2491 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2495 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2498 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2501 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, iv0
.step
);
2502 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2503 assumption
= simplify_gen_relational (reverse_condition (cond
),
2504 SImode
, mode
, tmp0
, tmp
);
2506 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2507 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2511 /* Condition in shape a <= b - s * i
2512 We must know that a - s does not overflow and a - s <= b and then
2513 we can again compute number of iterations as (b - (a - s)) / s. */
2514 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2515 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2516 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2518 bound
= simplify_gen_binary (PLUS
, mode
, mode_mmin
,
2519 lowpart_subreg (mode
, step
, comp_mode
));
2524 /* If s is power of 2, we know that the loop is infinite if
2525 a % s <= b % s and a - s overflows. */
2526 assumption
= simplify_gen_relational (reverse_condition (cond
),
2530 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2531 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2532 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2533 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2535 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2539 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2542 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2545 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2546 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2547 assumption
= simplify_gen_relational (reverse_condition (cond
),
2550 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2551 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2553 if (assumption
== const_true_rtx
)
2554 goto zero_iter_simplify
;
2555 else if (assumption
!= const0_rtx
)
2556 desc
->noloop_assumptions
=
2557 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2558 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2559 desc
->niter_expr
= delta
;
2562 old_niter
= desc
->niter_expr
;
2564 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2565 if (desc
->assumptions
2566 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2568 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2569 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2570 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2572 /* Rerun the simplification. Consider code (created by copying loop headers)
2584 The first pass determines that i = 0, the second pass uses it to eliminate
2585 noloop assumption. */
2587 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2588 if (desc
->assumptions
2589 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2591 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2592 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2593 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2595 if (desc
->noloop_assumptions
2596 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2599 if (GET_CODE (desc
->niter_expr
) == CONST_INT
)
2601 unsigned HOST_WIDEST_INT val
= INTVAL (desc
->niter_expr
);
2603 desc
->const_iter
= true;
2604 desc
->niter_max
= desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2608 if (!desc
->niter_max
)
2609 desc
->niter_max
= determine_max_iter (loop
, desc
);
2611 /* simplify_using_initial_values does a copy propagation on the registers
2612 in the expression for the number of iterations. This prolongs life
2613 ranges of registers and increases register pressure, and usually
2614 brings no gain (and if it happens to do, the cse pass will take care
2615 of it anyway). So prevent this behavior, unless it enabled us to
2616 derive that the number of iterations is a constant. */
2617 desc
->niter_expr
= old_niter
;
2623 /* Simplify the assumptions. */
2624 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2625 if (desc
->assumptions
2626 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2628 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2632 desc
->const_iter
= true;
2634 desc
->niter_max
= 0;
2635 desc
->noloop_assumptions
= NULL_RTX
;
2636 desc
->niter_expr
= const0_rtx
;
2640 desc
->simple_p
= false;
2644 /* Checks whether E is a simple exit from LOOP and stores its description
2648 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2650 basic_block exit_bb
;
2655 desc
->simple_p
= false;
2657 /* It must belong directly to the loop. */
2658 if (exit_bb
->loop_father
!= loop
)
2661 /* It must be tested (at least) once during any iteration. */
2662 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2665 /* It must end in a simple conditional jump. */
2666 if (!any_condjump_p (BB_END (exit_bb
)))
2669 ein
= EDGE_SUCC (exit_bb
, 0);
2671 ein
= EDGE_SUCC (exit_bb
, 1);
2674 desc
->in_edge
= ein
;
2676 /* Test whether the condition is suitable. */
2677 if (!(condition
= get_condition (BB_END (ein
->src
), &at
, false, false)))
2680 if (ein
->flags
& EDGE_FALLTHRU
)
2682 condition
= reversed_condition (condition
);
2687 /* Check that we are able to determine number of iterations and fill
2688 in information about it. */
2689 iv_number_of_iterations (loop
, at
, condition
, desc
);
2692 /* Finds a simple exit of LOOP and stores its description into DESC. */
2695 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2700 struct niter_desc act
;
2704 desc
->simple_p
= false;
2705 body
= get_loop_body (loop
);
2707 for (i
= 0; i
< loop
->num_nodes
; i
++)
2709 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
2711 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2714 check_simple_exit (loop
, e
, &act
);
2722 /* Prefer constant iterations; the less the better. */
2724 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
2727 /* Also if the actual exit may be infinite, while the old one
2728 not, prefer the old one. */
2729 if (act
.infinite
&& !desc
->infinite
)
2741 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
2742 fprintf (dump_file
, " simple exit %d -> %d\n",
2743 desc
->out_edge
->src
->index
,
2744 desc
->out_edge
->dest
->index
);
2745 if (desc
->assumptions
)
2747 fprintf (dump_file
, " assumptions: ");
2748 print_rtl (dump_file
, desc
->assumptions
);
2749 fprintf (dump_file
, "\n");
2751 if (desc
->noloop_assumptions
)
2753 fprintf (dump_file
, " does not roll if: ");
2754 print_rtl (dump_file
, desc
->noloop_assumptions
);
2755 fprintf (dump_file
, "\n");
2759 fprintf (dump_file
, " infinite if: ");
2760 print_rtl (dump_file
, desc
->infinite
);
2761 fprintf (dump_file
, "\n");
2764 fprintf (dump_file
, " number of iterations: ");
2765 print_rtl (dump_file
, desc
->niter_expr
);
2766 fprintf (dump_file
, "\n");
2768 fprintf (dump_file
, " upper bound: ");
2769 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, desc
->niter_max
);
2770 fprintf (dump_file
, "\n");
2773 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
2779 /* Creates a simple loop description of LOOP if it was not computed
2783 get_simple_loop_desc (struct loop
*loop
)
2785 struct niter_desc
*desc
= simple_loop_desc (loop
);
2790 desc
= XNEW (struct niter_desc
);
2791 iv_analysis_loop_init (loop
);
2792 find_simple_exit (loop
, desc
);
2795 if (desc
->simple_p
&& (desc
->assumptions
|| desc
->infinite
))
2797 const char *wording
;
2799 /* Assume that no overflow happens and that the loop is finite.
2800 We already warned at the tree level if we ran optimizations there. */
2801 if (!flag_tree_loop_optimize
&& warn_unsafe_loop_optimizations
)
2806 flag_unsafe_loop_optimizations
2807 ? N_("assuming that the loop is not infinite")
2808 : N_("cannot optimize possibly infinite loops");
2809 warning (OPT_Wunsafe_loop_optimizations
, "%s",
2812 if (desc
->assumptions
)
2815 flag_unsafe_loop_optimizations
2816 ? N_("assuming that the loop counter does not overflow")
2817 : N_("cannot optimize loop, the loop counter may overflow");
2818 warning (OPT_Wunsafe_loop_optimizations
, "%s",
2823 if (flag_unsafe_loop_optimizations
)
2825 desc
->assumptions
= NULL_RTX
;
2826 desc
->infinite
= NULL_RTX
;
2833 /* Releases simple loop description for LOOP. */
2836 free_simple_loop_desc (struct loop
*loop
)
2838 struct niter_desc
*desc
= simple_loop_desc (loop
);