1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This is a simple analysis of induction variables of the loop. The major use
22 is for determining the number of iterations of a loop for loop unrolling,
23 doloop optimization and branch prediction. The iv information is computed
26 Induction variables are analyzed by walking the use-def chains. When
27 a basic induction variable (biv) is found, it is cached in the bivs
28 hash table. When register is proved to be a biv, its description
29 is stored to DF_REF_DATA of the def reference.
31 The analysis works always with one loop -- you must call
32 iv_analysis_loop_init (loop) for it. All the other functions then work with
33 this loop. When you need to work with another loop, just call
34 iv_analysis_loop_init for it. When you no longer need iv analysis, call
35 iv_analysis_done () to clean up the memory.
37 The available functions are:
39 iv_analyze (insn, reg, iv): Stores the description of the induction variable
40 corresponding to the use of register REG in INSN to IV. Returns true if
41 REG is an induction variable in INSN. false otherwise.
42 If use of REG is not found in INSN, following insns are scanned (so that
43 we may call this function on insn returned by get_condition).
44 iv_analyze_result (insn, def, iv): Stores to IV the description of the iv
45 corresponding to DEF, which is a register defined in INSN.
46 iv_analyze_expr (insn, rhs, mode, iv): Stores to IV the description of iv
47 corresponding to expression EXPR evaluated at INSN. All registers used bu
48 EXPR must also be used in INSN.
53 #include "coretypes.h"
56 #include "hard-reg-set.h"
58 #include "basic-block.h"
63 #include "diagnostic-core.h"
68 /* Possible return values of iv_get_reaching_def. */
72 /* More than one reaching def, or reaching def that does not
76 /* The use is trivial invariant of the loop, i.e. is not changed
80 /* The use is reached by initial value and a value from the
81 previous iteration. */
84 /* The use has single dominating def. */
88 /* Information about a biv. */
92 unsigned regno
; /* The register of the biv. */
93 struct rtx_iv iv
; /* Value of the biv. */
96 static bool clean_slate
= true;
98 static unsigned int iv_ref_table_size
= 0;
100 /* Table of rtx_ivs indexed by the df_ref uid field. */
101 static struct rtx_iv
** iv_ref_table
;
103 /* Induction variable stored at the reference. */
104 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID(REF)]
105 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID(REF)] = (IV)
107 /* The current loop. */
109 static struct loop
*current_loop
;
111 /* Bivs of the current loop. */
115 static bool iv_analyze_op (rtx
, rtx
, struct rtx_iv
*);
117 /* Dumps information about IV to FILE. */
119 extern void dump_iv_info (FILE *, struct rtx_iv
*);
121 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
125 fprintf (file
, "not simple");
129 if (iv
->step
== const0_rtx
130 && !iv
->first_special
)
131 fprintf (file
, "invariant ");
133 print_rtl (file
, iv
->base
);
134 if (iv
->step
!= const0_rtx
)
136 fprintf (file
, " + ");
137 print_rtl (file
, iv
->step
);
138 fprintf (file
, " * iteration");
140 fprintf (file
, " (in %s)", GET_MODE_NAME (iv
->mode
));
142 if (iv
->mode
!= iv
->extend_mode
)
143 fprintf (file
, " %s to %s",
144 rtx_name
[iv
->extend
],
145 GET_MODE_NAME (iv
->extend_mode
));
147 if (iv
->mult
!= const1_rtx
)
149 fprintf (file
, " * ");
150 print_rtl (file
, iv
->mult
);
152 if (iv
->delta
!= const0_rtx
)
154 fprintf (file
, " + ");
155 print_rtl (file
, iv
->delta
);
157 if (iv
->first_special
)
158 fprintf (file
, " (first special)");
161 /* Generates a subreg to get the least significant part of EXPR (in mode
162 INNER_MODE) to OUTER_MODE. */
165 lowpart_subreg (enum machine_mode outer_mode
, rtx expr
,
166 enum machine_mode inner_mode
)
168 return simplify_gen_subreg (outer_mode
, expr
, inner_mode
,
169 subreg_lowpart_offset (outer_mode
, inner_mode
));
173 check_iv_ref_table_size (void)
175 if (iv_ref_table_size
< DF_DEFS_TABLE_SIZE())
177 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
178 iv_ref_table
= XRESIZEVEC (struct rtx_iv
*, iv_ref_table
, new_size
);
179 memset (&iv_ref_table
[iv_ref_table_size
], 0,
180 (new_size
- iv_ref_table_size
) * sizeof (struct rtx_iv
*));
181 iv_ref_table_size
= new_size
;
186 /* Checks whether REG is a well-behaved register. */
189 simple_reg_p (rtx reg
)
193 if (GET_CODE (reg
) == SUBREG
)
195 if (!subreg_lowpart_p (reg
))
197 reg
= SUBREG_REG (reg
);
204 if (HARD_REGISTER_NUM_P (r
))
207 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
213 /* Clears the information about ivs stored in df. */
218 unsigned i
, n_defs
= DF_DEFS_TABLE_SIZE ();
221 check_iv_ref_table_size ();
222 for (i
= 0; i
< n_defs
; i
++)
224 iv
= iv_ref_table
[i
];
228 iv_ref_table
[i
] = NULL
;
235 /* Returns hash value for biv B. */
238 biv_hash (const void *b
)
240 return ((const struct biv_entry
*) b
)->regno
;
243 /* Compares biv B and register R. */
246 biv_eq (const void *b
, const void *r
)
248 return ((const struct biv_entry
*) b
)->regno
== REGNO ((const_rtx
) r
);
251 /* Prepare the data for an induction variable analysis of a LOOP. */
254 iv_analysis_loop_init (struct loop
*loop
)
256 basic_block
*body
= get_loop_body_in_dom_order (loop
), bb
;
257 bitmap blocks
= BITMAP_ALLOC (NULL
);
262 /* Clear the information from the analysis of the previous loop. */
265 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
266 bivs
= htab_create (10, biv_hash
, biv_eq
, free
);
272 for (i
= 0; i
< loop
->num_nodes
; i
++)
275 bitmap_set_bit (blocks
, bb
->index
);
277 /* Get rid of the ud chains before processing the rescans. Then add
279 df_remove_problem (df_chain
);
280 df_process_deferred_rescans ();
281 df_chain_add_problem (DF_UD_CHAIN
);
282 df_note_add_problem ();
283 df_set_blocks (blocks
);
286 df_dump_region (dump_file
);
288 check_iv_ref_table_size ();
289 BITMAP_FREE (blocks
);
293 /* Finds the definition of REG that dominates loop latch and stores
294 it to DEF. Returns false if there is not a single definition
295 dominating the latch. If REG has no definition in loop, DEF
296 is set to NULL and true is returned. */
299 latch_dominating_def (rtx reg
, df_ref
*def
)
301 df_ref single_rd
= NULL
, adef
;
302 unsigned regno
= REGNO (reg
);
303 struct df_rd_bb_info
*bb_info
= DF_RD_BB_INFO (current_loop
->latch
);
305 for (adef
= DF_REG_DEF_CHAIN (regno
); adef
; adef
= DF_REF_NEXT_REG (adef
))
307 if (!bitmap_bit_p (df
->blocks_to_analyze
, DF_REF_BBNO (adef
))
308 || !bitmap_bit_p (&bb_info
->out
, DF_REF_ID (adef
)))
311 /* More than one reaching definition. */
315 if (!just_once_each_iteration_p (current_loop
, DF_REF_BB (adef
)))
325 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
327 static enum iv_grd_result
328 iv_get_reaching_def (rtx insn
, rtx reg
, df_ref
*def
)
331 basic_block def_bb
, use_bb
;
336 if (!simple_reg_p (reg
))
338 if (GET_CODE (reg
) == SUBREG
)
339 reg
= SUBREG_REG (reg
);
340 gcc_assert (REG_P (reg
));
342 use
= df_find_use (insn
, reg
);
343 gcc_assert (use
!= NULL
);
345 if (!DF_REF_CHAIN (use
))
346 return GRD_INVARIANT
;
348 /* More than one reaching def. */
349 if (DF_REF_CHAIN (use
)->next
)
352 adef
= DF_REF_CHAIN (use
)->ref
;
354 /* We do not handle setting only part of the register. */
355 if (DF_REF_FLAGS (adef
) & DF_REF_READ_WRITE
)
358 def_insn
= DF_REF_INSN (adef
);
359 def_bb
= DF_REF_BB (adef
);
360 use_bb
= BLOCK_FOR_INSN (insn
);
362 if (use_bb
== def_bb
)
363 dom_p
= (DF_INSN_LUID (def_insn
) < DF_INSN_LUID (insn
));
365 dom_p
= dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
);
370 return GRD_SINGLE_DOM
;
373 /* The definition does not dominate the use. This is still OK if
374 this may be a use of a biv, i.e. if the def_bb dominates loop
376 if (just_once_each_iteration_p (current_loop
, def_bb
))
377 return GRD_MAYBE_BIV
;
382 /* Sets IV to invariant CST in MODE. Always returns true (just for
383 consistency with other iv manipulation functions that may fail). */
386 iv_constant (struct rtx_iv
*iv
, rtx cst
, enum machine_mode mode
)
388 if (mode
== VOIDmode
)
389 mode
= GET_MODE (cst
);
393 iv
->step
= const0_rtx
;
394 iv
->first_special
= false;
395 iv
->extend
= UNKNOWN
;
396 iv
->extend_mode
= iv
->mode
;
397 iv
->delta
= const0_rtx
;
398 iv
->mult
= const1_rtx
;
403 /* Evaluates application of subreg to MODE on IV. */
406 iv_subreg (struct rtx_iv
*iv
, enum machine_mode mode
)
408 /* If iv is invariant, just calculate the new value. */
409 if (iv
->step
== const0_rtx
410 && !iv
->first_special
)
412 rtx val
= get_iv_value (iv
, const0_rtx
);
413 val
= lowpart_subreg (mode
, val
, iv
->extend_mode
);
416 iv
->extend
= UNKNOWN
;
417 iv
->mode
= iv
->extend_mode
= mode
;
418 iv
->delta
= const0_rtx
;
419 iv
->mult
= const1_rtx
;
423 if (iv
->extend_mode
== mode
)
426 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
429 iv
->extend
= UNKNOWN
;
432 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
433 simplify_gen_binary (MULT
, iv
->extend_mode
,
434 iv
->base
, iv
->mult
));
435 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
436 iv
->mult
= const1_rtx
;
437 iv
->delta
= const0_rtx
;
438 iv
->first_special
= false;
443 /* Evaluates application of EXTEND to MODE on IV. */
446 iv_extend (struct rtx_iv
*iv
, enum rtx_code extend
, enum machine_mode mode
)
448 /* If iv is invariant, just calculate the new value. */
449 if (iv
->step
== const0_rtx
450 && !iv
->first_special
)
452 rtx val
= get_iv_value (iv
, const0_rtx
);
453 val
= simplify_gen_unary (extend
, mode
, val
, iv
->extend_mode
);
456 iv
->extend
= UNKNOWN
;
457 iv
->mode
= iv
->extend_mode
= mode
;
458 iv
->delta
= const0_rtx
;
459 iv
->mult
= const1_rtx
;
463 if (mode
!= iv
->extend_mode
)
466 if (iv
->extend
!= UNKNOWN
467 && iv
->extend
!= extend
)
475 /* Evaluates negation of IV. */
478 iv_neg (struct rtx_iv
*iv
)
480 if (iv
->extend
== UNKNOWN
)
482 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
483 iv
->base
, iv
->extend_mode
);
484 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
485 iv
->step
, iv
->extend_mode
);
489 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
490 iv
->delta
, iv
->extend_mode
);
491 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
492 iv
->mult
, iv
->extend_mode
);
498 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
501 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
503 enum machine_mode mode
;
506 /* Extend the constant to extend_mode of the other operand if necessary. */
507 if (iv0
->extend
== UNKNOWN
508 && iv0
->mode
== iv0
->extend_mode
509 && iv0
->step
== const0_rtx
510 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
512 iv0
->extend_mode
= iv1
->extend_mode
;
513 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
514 iv0
->base
, iv0
->mode
);
516 if (iv1
->extend
== UNKNOWN
517 && iv1
->mode
== iv1
->extend_mode
518 && iv1
->step
== const0_rtx
519 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
521 iv1
->extend_mode
= iv0
->extend_mode
;
522 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
523 iv1
->base
, iv1
->mode
);
526 mode
= iv0
->extend_mode
;
527 if (mode
!= iv1
->extend_mode
)
530 if (iv0
->extend
== UNKNOWN
&& iv1
->extend
== UNKNOWN
)
532 if (iv0
->mode
!= iv1
->mode
)
535 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
536 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
541 /* Handle addition of constant. */
542 if (iv1
->extend
== UNKNOWN
544 && iv1
->step
== const0_rtx
)
546 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
550 if (iv0
->extend
== UNKNOWN
552 && iv0
->step
== const0_rtx
)
560 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
567 /* Evaluates multiplication of IV by constant CST. */
570 iv_mult (struct rtx_iv
*iv
, rtx mby
)
572 enum machine_mode mode
= iv
->extend_mode
;
574 if (GET_MODE (mby
) != VOIDmode
575 && GET_MODE (mby
) != mode
)
578 if (iv
->extend
== UNKNOWN
)
580 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
581 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
585 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
586 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
592 /* Evaluates shift of IV by constant CST. */
595 iv_shift (struct rtx_iv
*iv
, rtx mby
)
597 enum machine_mode mode
= iv
->extend_mode
;
599 if (GET_MODE (mby
) != VOIDmode
600 && GET_MODE (mby
) != mode
)
603 if (iv
->extend
== UNKNOWN
)
605 iv
->base
= simplify_gen_binary (ASHIFT
, mode
, iv
->base
, mby
);
606 iv
->step
= simplify_gen_binary (ASHIFT
, mode
, iv
->step
, mby
);
610 iv
->delta
= simplify_gen_binary (ASHIFT
, mode
, iv
->delta
, mby
);
611 iv
->mult
= simplify_gen_binary (ASHIFT
, mode
, iv
->mult
, mby
);
617 /* The recursive part of get_biv_step. Gets the value of the single value
618 defined by DEF wrto initial value of REG inside loop, in shape described
622 get_biv_step_1 (df_ref def
, rtx reg
,
623 rtx
*inner_step
, enum machine_mode
*inner_mode
,
624 enum rtx_code
*extend
, enum machine_mode outer_mode
,
627 rtx set
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
628 rtx next
, nextr
, tmp
;
630 rtx insn
= DF_REF_INSN (def
);
632 enum iv_grd_result res
;
634 set
= single_set (insn
);
638 rhs
= find_reg_equal_equiv_note (insn
);
644 code
= GET_CODE (rhs
);
657 if (code
== PLUS
&& CONSTANT_P (op0
))
659 tmp
= op0
; op0
= op1
; op1
= tmp
;
662 if (!simple_reg_p (op0
)
663 || !CONSTANT_P (op1
))
666 if (GET_MODE (rhs
) != outer_mode
)
668 /* ppc64 uses expressions like
670 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
672 this is equivalent to
674 (set x':DI (plus:DI y:DI 1))
675 (set x:SI (subreg:SI (x':DI)). */
676 if (GET_CODE (op0
) != SUBREG
)
678 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
687 if (GET_MODE (rhs
) != outer_mode
)
691 if (!simple_reg_p (op0
))
701 if (GET_CODE (next
) == SUBREG
)
703 if (!subreg_lowpart_p (next
))
706 nextr
= SUBREG_REG (next
);
707 if (GET_MODE (nextr
) != outer_mode
)
713 res
= iv_get_reaching_def (insn
, nextr
, &next_def
);
715 if (res
== GRD_INVALID
|| res
== GRD_INVARIANT
)
718 if (res
== GRD_MAYBE_BIV
)
720 if (!rtx_equal_p (nextr
, reg
))
723 *inner_step
= const0_rtx
;
725 *inner_mode
= outer_mode
;
726 *outer_step
= const0_rtx
;
728 else if (!get_biv_step_1 (next_def
, reg
,
729 inner_step
, inner_mode
, extend
, outer_mode
,
733 if (GET_CODE (next
) == SUBREG
)
735 enum machine_mode amode
= GET_MODE (next
);
737 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
741 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
742 *inner_step
, *outer_step
);
743 *outer_step
= const0_rtx
;
755 if (*inner_mode
== outer_mode
756 /* See comment in previous switch. */
757 || GET_MODE (rhs
) != outer_mode
)
758 *inner_step
= simplify_gen_binary (code
, outer_mode
,
761 *outer_step
= simplify_gen_binary (code
, outer_mode
,
767 gcc_assert (GET_MODE (op0
) == *inner_mode
768 && *extend
== UNKNOWN
769 && *outer_step
== const0_rtx
);
781 /* Gets the operation on register REG inside loop, in shape
783 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
785 If the operation cannot be described in this shape, return false.
786 LAST_DEF is the definition of REG that dominates loop latch. */
789 get_biv_step (df_ref last_def
, rtx reg
, rtx
*inner_step
,
790 enum machine_mode
*inner_mode
, enum rtx_code
*extend
,
791 enum machine_mode
*outer_mode
, rtx
*outer_step
)
793 *outer_mode
= GET_MODE (reg
);
795 if (!get_biv_step_1 (last_def
, reg
,
796 inner_step
, inner_mode
, extend
, *outer_mode
,
800 gcc_assert ((*inner_mode
== *outer_mode
) != (*extend
!= UNKNOWN
));
801 gcc_assert (*inner_mode
!= *outer_mode
|| *outer_step
== const0_rtx
);
806 /* Records information that DEF is induction variable IV. */
809 record_iv (df_ref def
, struct rtx_iv
*iv
)
811 struct rtx_iv
*recorded_iv
= XNEW (struct rtx_iv
);
814 check_iv_ref_table_size ();
815 DF_REF_IV_SET (def
, recorded_iv
);
818 /* If DEF was already analyzed for bivness, store the description of the biv to
819 IV and return true. Otherwise return false. */
822 analyzed_for_bivness_p (rtx def
, struct rtx_iv
*iv
)
824 struct biv_entry
*biv
=
825 (struct biv_entry
*) htab_find_with_hash (bivs
, def
, REGNO (def
));
835 record_biv (rtx def
, struct rtx_iv
*iv
)
837 struct biv_entry
*biv
= XNEW (struct biv_entry
);
838 void **slot
= htab_find_slot_with_hash (bivs
, def
, REGNO (def
), INSERT
);
840 biv
->regno
= REGNO (def
);
846 /* Determines whether DEF is a biv and if so, stores its description
850 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
852 rtx inner_step
, outer_step
;
853 enum machine_mode inner_mode
, outer_mode
;
854 enum rtx_code extend
;
859 fprintf (dump_file
, "Analyzing ");
860 print_rtl (dump_file
, def
);
861 fprintf (dump_file
, " for bivness.\n");
866 if (!CONSTANT_P (def
))
869 return iv_constant (iv
, def
, VOIDmode
);
872 if (!latch_dominating_def (def
, &last_def
))
875 fprintf (dump_file
, " not simple.\n");
880 return iv_constant (iv
, def
, VOIDmode
);
882 if (analyzed_for_bivness_p (def
, iv
))
885 fprintf (dump_file
, " already analysed.\n");
886 return iv
->base
!= NULL_RTX
;
889 if (!get_biv_step (last_def
, def
, &inner_step
, &inner_mode
, &extend
,
890 &outer_mode
, &outer_step
))
896 /* Loop transforms base to es (base + inner_step) + outer_step,
897 where es means extend of subreg between inner_mode and outer_mode.
898 The corresponding induction variable is
900 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
902 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
903 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
904 iv
->mode
= inner_mode
;
905 iv
->extend_mode
= outer_mode
;
907 iv
->mult
= const1_rtx
;
908 iv
->delta
= outer_step
;
909 iv
->first_special
= inner_mode
!= outer_mode
;
914 fprintf (dump_file
, " ");
915 dump_iv_info (dump_file
, iv
);
916 fprintf (dump_file
, "\n");
919 record_biv (def
, iv
);
920 return iv
->base
!= NULL_RTX
;
923 /* Analyzes expression RHS used at INSN and stores the result to *IV.
924 The mode of the induction variable is MODE. */
927 iv_analyze_expr (rtx insn
, rtx rhs
, enum machine_mode mode
, struct rtx_iv
*iv
)
929 rtx mby
= NULL_RTX
, tmp
;
930 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
931 struct rtx_iv iv0
, iv1
;
932 enum rtx_code code
= GET_CODE (rhs
);
933 enum machine_mode omode
= mode
;
939 gcc_assert (GET_MODE (rhs
) == mode
|| GET_MODE (rhs
) == VOIDmode
);
945 if (!iv_analyze_op (insn
, rhs
, iv
))
948 if (iv
->mode
== VOIDmode
)
951 iv
->extend_mode
= mode
;
967 omode
= GET_MODE (op0
);
979 if (!CONSTANT_P (mby
))
985 if (!CONSTANT_P (mby
))
992 if (!CONSTANT_P (mby
))
1001 && !iv_analyze_expr (insn
, op0
, omode
, &iv0
))
1005 && !iv_analyze_expr (insn
, op1
, omode
, &iv1
))
1012 if (!iv_extend (&iv0
, code
, mode
))
1023 if (!iv_add (&iv0
, &iv1
, code
))
1028 if (!iv_mult (&iv0
, mby
))
1033 if (!iv_shift (&iv0
, mby
))
1042 return iv
->base
!= NULL_RTX
;
1045 /* Analyzes iv DEF and stores the result to *IV. */
1048 iv_analyze_def (df_ref def
, struct rtx_iv
*iv
)
1050 rtx insn
= DF_REF_INSN (def
);
1051 rtx reg
= DF_REF_REG (def
);
1056 fprintf (dump_file
, "Analyzing def of ");
1057 print_rtl (dump_file
, reg
);
1058 fprintf (dump_file
, " in insn ");
1059 print_rtl_single (dump_file
, insn
);
1062 check_iv_ref_table_size ();
1063 if (DF_REF_IV (def
))
1066 fprintf (dump_file
, " already analysed.\n");
1067 *iv
= *DF_REF_IV (def
);
1068 return iv
->base
!= NULL_RTX
;
1071 iv
->mode
= VOIDmode
;
1072 iv
->base
= NULL_RTX
;
1073 iv
->step
= NULL_RTX
;
1078 set
= single_set (insn
);
1082 if (!REG_P (SET_DEST (set
)))
1085 gcc_assert (SET_DEST (set
) == reg
);
1086 rhs
= find_reg_equal_equiv_note (insn
);
1088 rhs
= XEXP (rhs
, 0);
1090 rhs
= SET_SRC (set
);
1092 iv_analyze_expr (insn
, rhs
, GET_MODE (reg
), iv
);
1093 record_iv (def
, iv
);
1097 print_rtl (dump_file
, reg
);
1098 fprintf (dump_file
, " in insn ");
1099 print_rtl_single (dump_file
, insn
);
1100 fprintf (dump_file
, " is ");
1101 dump_iv_info (dump_file
, iv
);
1102 fprintf (dump_file
, "\n");
1105 return iv
->base
!= NULL_RTX
;
1108 /* Analyzes operand OP of INSN and stores the result to *IV. */
1111 iv_analyze_op (rtx insn
, rtx op
, struct rtx_iv
*iv
)
1114 enum iv_grd_result res
;
1118 fprintf (dump_file
, "Analyzing operand ");
1119 print_rtl (dump_file
, op
);
1120 fprintf (dump_file
, " of insn ");
1121 print_rtl_single (dump_file
, insn
);
1124 if (function_invariant_p (op
))
1125 res
= GRD_INVARIANT
;
1126 else if (GET_CODE (op
) == SUBREG
)
1128 if (!subreg_lowpart_p (op
))
1131 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
1134 return iv_subreg (iv
, GET_MODE (op
));
1138 res
= iv_get_reaching_def (insn
, op
, &def
);
1139 if (res
== GRD_INVALID
)
1142 fprintf (dump_file
, " not simple.\n");
1147 if (res
== GRD_INVARIANT
)
1149 iv_constant (iv
, op
, VOIDmode
);
1153 fprintf (dump_file
, " ");
1154 dump_iv_info (dump_file
, iv
);
1155 fprintf (dump_file
, "\n");
1160 if (res
== GRD_MAYBE_BIV
)
1161 return iv_analyze_biv (op
, iv
);
1163 return iv_analyze_def (def
, iv
);
1166 /* Analyzes value VAL at INSN and stores the result to *IV. */
1169 iv_analyze (rtx insn
, rtx val
, struct rtx_iv
*iv
)
1173 /* We must find the insn in that val is used, so that we get to UD chains.
1174 Since the function is sometimes called on result of get_condition,
1175 this does not necessarily have to be directly INSN; scan also the
1177 if (simple_reg_p (val
))
1179 if (GET_CODE (val
) == SUBREG
)
1180 reg
= SUBREG_REG (val
);
1184 while (!df_find_use (insn
, reg
))
1185 insn
= NEXT_INSN (insn
);
1188 return iv_analyze_op (insn
, val
, iv
);
1191 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1194 iv_analyze_result (rtx insn
, rtx def
, struct rtx_iv
*iv
)
1198 adef
= df_find_def (insn
, def
);
1202 return iv_analyze_def (adef
, iv
);
1205 /* Checks whether definition of register REG in INSN is a basic induction
1206 variable. IV analysis must have been initialized (via a call to
1207 iv_analysis_loop_init) for this function to produce a result. */
1210 biv_p (rtx insn
, rtx reg
)
1213 df_ref def
, last_def
;
1215 if (!simple_reg_p (reg
))
1218 def
= df_find_def (insn
, reg
);
1219 gcc_assert (def
!= NULL
);
1220 if (!latch_dominating_def (reg
, &last_def
))
1222 if (last_def
!= def
)
1225 if (!iv_analyze_biv (reg
, &iv
))
1228 return iv
.step
!= const0_rtx
;
1231 /* Calculates value of IV at ITERATION-th iteration. */
1234 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1238 /* We would need to generate some if_then_else patterns, and so far
1239 it is not needed anywhere. */
1240 gcc_assert (!iv
->first_special
);
1242 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1243 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1244 simplify_gen_binary (MULT
, iv
->extend_mode
,
1245 iv
->step
, iteration
));
1249 if (iv
->extend_mode
== iv
->mode
)
1252 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1254 if (iv
->extend
== UNKNOWN
)
1257 val
= simplify_gen_unary (iv
->extend
, iv
->extend_mode
, val
, iv
->mode
);
1258 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1259 simplify_gen_binary (MULT
, iv
->extend_mode
,
1265 /* Free the data for an induction variable analysis. */
1268 iv_analysis_done (void)
1274 df_finish_pass (true);
1276 free (iv_ref_table
);
1277 iv_ref_table
= NULL
;
1278 iv_ref_table_size
= 0;
1283 /* Computes inverse to X modulo (1 << MOD). */
1285 static unsigned HOST_WIDEST_INT
1286 inverse (unsigned HOST_WIDEST_INT x
, int mod
)
1288 unsigned HOST_WIDEST_INT mask
=
1289 ((unsigned HOST_WIDEST_INT
) 1 << (mod
- 1) << 1) - 1;
1290 unsigned HOST_WIDEST_INT rslt
= 1;
1293 for (i
= 0; i
< mod
- 1; i
++)
1295 rslt
= (rslt
* x
) & mask
;
1302 /* Checks whether register *REG is in set ALT. Callback for for_each_rtx. */
1305 altered_reg_used (rtx
*reg
, void *alt
)
1310 return REGNO_REG_SET_P ((bitmap
) alt
, REGNO (*reg
));
1313 /* Marks registers altered by EXPR in set ALT. */
1316 mark_altered (rtx expr
, const_rtx by ATTRIBUTE_UNUSED
, void *alt
)
1318 if (GET_CODE (expr
) == SUBREG
)
1319 expr
= SUBREG_REG (expr
);
1323 SET_REGNO_REG_SET ((bitmap
) alt
, REGNO (expr
));
1326 /* Checks whether RHS is simple enough to process. */
1329 simple_rhs_p (rtx rhs
)
1333 if (function_invariant_p (rhs
)
1334 || (REG_P (rhs
) && !HARD_REGISTER_P (rhs
)))
1337 switch (GET_CODE (rhs
))
1342 op0
= XEXP (rhs
, 0);
1343 op1
= XEXP (rhs
, 1);
1344 /* Allow reg OP const and reg OP reg. */
1345 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
))
1346 && !function_invariant_p (op0
))
1348 if (!(REG_P (op1
) && !HARD_REGISTER_P (op1
))
1349 && !function_invariant_p (op1
))
1358 op0
= XEXP (rhs
, 0);
1359 op1
= XEXP (rhs
, 1);
1360 /* Allow reg OP const. */
1361 if (!(REG_P (op0
) && !HARD_REGISTER_P (op0
)))
1363 if (!function_invariant_p (op1
))
1373 /* If REG has a single definition, replace it with its known value in EXPR.
1374 Callback for for_each_rtx. */
1377 replace_single_def_regs (rtx
*reg
, void *expr1
)
1382 rtx
*expr
= (rtx
*)expr1
;
1387 regno
= REGNO (*reg
);
1391 adef
= DF_REG_DEF_CHAIN (regno
);
1392 if (adef
== NULL
|| DF_REF_NEXT_REG (adef
) != NULL
1393 || DF_REF_IS_ARTIFICIAL (adef
))
1396 set
= single_set (DF_REF_INSN (adef
));
1397 if (set
== NULL
|| !REG_P (SET_DEST (set
))
1398 || REGNO (SET_DEST (set
)) != regno
)
1401 note
= find_reg_equal_equiv_note (DF_REF_INSN (adef
));
1403 if (note
&& function_invariant_p (XEXP (note
, 0)))
1405 src
= XEXP (note
, 0);
1408 src
= SET_SRC (set
);
1412 regno
= REGNO (src
);
1417 if (!function_invariant_p (src
))
1420 *expr
= simplify_replace_rtx (*expr
, *reg
, src
);
1424 /* A subroutine of simplify_using_initial_values, this function examines INSN
1425 to see if it contains a suitable set that we can use to make a replacement.
1426 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1427 the set; return false otherwise. */
1430 suitable_set_for_replacement (rtx insn
, rtx
*dest
, rtx
*src
)
1432 rtx set
= single_set (insn
);
1433 rtx lhs
= NULL_RTX
, rhs
;
1438 lhs
= SET_DEST (set
);
1442 rhs
= find_reg_equal_equiv_note (insn
);
1444 rhs
= XEXP (rhs
, 0);
1446 rhs
= SET_SRC (set
);
1448 if (!simple_rhs_p (rhs
))
1456 /* Using the data returned by suitable_set_for_replacement, replace DEST
1457 with SRC in *EXPR and return the new expression. Also call
1458 replace_single_def_regs if the replacement changed something. */
1460 replace_in_expr (rtx
*expr
, rtx dest
, rtx src
)
1463 *expr
= simplify_replace_rtx (*expr
, dest
, src
);
1466 while (for_each_rtx (expr
, replace_single_def_regs
, expr
) != 0)
1470 /* Checks whether A implies B. */
1473 implies_p (rtx a
, rtx b
)
1475 rtx op0
, op1
, opb0
, opb1
, r
;
1476 enum machine_mode mode
;
1478 if (GET_CODE (a
) == EQ
)
1485 r
= simplify_replace_rtx (b
, op0
, op1
);
1486 if (r
== const_true_rtx
)
1492 r
= simplify_replace_rtx (b
, op1
, op0
);
1493 if (r
== const_true_rtx
)
1498 if (b
== const_true_rtx
)
1501 if ((GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMM_COMPARE
1502 && GET_RTX_CLASS (GET_CODE (a
)) != RTX_COMPARE
)
1503 || (GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMM_COMPARE
1504 && GET_RTX_CLASS (GET_CODE (b
)) != RTX_COMPARE
))
1512 mode
= GET_MODE (op0
);
1513 if (mode
!= GET_MODE (opb0
))
1515 else if (mode
== VOIDmode
)
1517 mode
= GET_MODE (op1
);
1518 if (mode
!= GET_MODE (opb1
))
1522 /* A < B implies A + 1 <= B. */
1523 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1524 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1527 if (GET_CODE (a
) == GT
)
1534 if (GET_CODE (b
) == GE
)
1541 if (SCALAR_INT_MODE_P (mode
)
1542 && rtx_equal_p (op1
, opb1
)
1543 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1548 /* A < B or A > B imply A != B. TODO: Likewise
1549 A + n < B implies A != B + n if neither wraps. */
1550 if (GET_CODE (b
) == NE
1551 && (GET_CODE (a
) == GT
|| GET_CODE (a
) == GTU
1552 || GET_CODE (a
) == LT
|| GET_CODE (a
) == LTU
))
1554 if (rtx_equal_p (op0
, opb0
)
1555 && rtx_equal_p (op1
, opb1
))
1559 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1560 if (GET_CODE (a
) == NE
1561 && op1
== const0_rtx
)
1563 if ((GET_CODE (b
) == GTU
1564 && opb1
== const0_rtx
)
1565 || (GET_CODE (b
) == GEU
1566 && opb1
== const1_rtx
))
1567 return rtx_equal_p (op0
, opb0
);
1570 /* A != N is equivalent to A - (N + 1) <u -1. */
1571 if (GET_CODE (a
) == NE
1572 && CONST_INT_P (op1
)
1573 && GET_CODE (b
) == LTU
1574 && opb1
== constm1_rtx
1575 && GET_CODE (opb0
) == PLUS
1576 && CONST_INT_P (XEXP (opb0
, 1))
1577 /* Avoid overflows. */
1578 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1579 != ((unsigned HOST_WIDE_INT
)1
1580 << (HOST_BITS_PER_WIDE_INT
- 1)) - 1)
1581 && INTVAL (XEXP (opb0
, 1)) + 1 == -INTVAL (op1
))
1582 return rtx_equal_p (op0
, XEXP (opb0
, 0));
1584 /* Likewise, A != N implies A - N > 0. */
1585 if (GET_CODE (a
) == NE
1586 && CONST_INT_P (op1
))
1588 if (GET_CODE (b
) == GTU
1589 && GET_CODE (opb0
) == PLUS
1590 && opb1
== const0_rtx
1591 && CONST_INT_P (XEXP (opb0
, 1))
1592 /* Avoid overflows. */
1593 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1594 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1595 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1596 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1597 if (GET_CODE (b
) == GEU
1598 && GET_CODE (opb0
) == PLUS
1599 && opb1
== const1_rtx
1600 && CONST_INT_P (XEXP (opb0
, 1))
1601 /* Avoid overflows. */
1602 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (opb0
, 1))
1603 != ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
1604 && rtx_equal_p (XEXP (opb0
, 0), op0
))
1605 return INTVAL (op1
) == -INTVAL (XEXP (opb0
, 1));
1608 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1609 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == GE
)
1610 && CONST_INT_P (op1
)
1611 && ((GET_CODE (a
) == GT
&& op1
== constm1_rtx
)
1612 || INTVAL (op1
) >= 0)
1613 && GET_CODE (b
) == LTU
1614 && CONST_INT_P (opb1
)
1615 && rtx_equal_p (op0
, opb0
))
1616 return INTVAL (opb1
) < 0;
1621 /* Canonicalizes COND so that
1623 (1) Ensure that operands are ordered according to
1624 swap_commutative_operands_p.
1625 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1626 for GE, GEU, and LEU. */
1629 canon_condition (rtx cond
)
1634 enum machine_mode mode
;
1636 code
= GET_CODE (cond
);
1637 op0
= XEXP (cond
, 0);
1638 op1
= XEXP (cond
, 1);
1640 if (swap_commutative_operands_p (op0
, op1
))
1642 code
= swap_condition (code
);
1648 mode
= GET_MODE (op0
);
1649 if (mode
== VOIDmode
)
1650 mode
= GET_MODE (op1
);
1651 gcc_assert (mode
!= VOIDmode
);
1653 if (CONST_INT_P (op1
)
1654 && GET_MODE_CLASS (mode
) != MODE_CC
1655 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
1657 HOST_WIDE_INT const_val
= INTVAL (op1
);
1658 unsigned HOST_WIDE_INT uconst_val
= const_val
;
1659 unsigned HOST_WIDE_INT max_val
1660 = (unsigned HOST_WIDE_INT
) GET_MODE_MASK (mode
);
1665 if ((unsigned HOST_WIDE_INT
) const_val
!= max_val
>> 1)
1666 code
= LT
, op1
= gen_int_mode (const_val
+ 1, GET_MODE (op0
));
1669 /* When cross-compiling, const_val might be sign-extended from
1670 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
1672 if ((HOST_WIDE_INT
) (const_val
& max_val
)
1673 != (((HOST_WIDE_INT
) 1
1674 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
1675 code
= GT
, op1
= gen_int_mode (const_val
- 1, mode
);
1679 if (uconst_val
< max_val
)
1680 code
= LTU
, op1
= gen_int_mode (uconst_val
+ 1, mode
);
1684 if (uconst_val
!= 0)
1685 code
= GTU
, op1
= gen_int_mode (uconst_val
- 1, mode
);
1693 if (op0
!= XEXP (cond
, 0)
1694 || op1
!= XEXP (cond
, 1)
1695 || code
!= GET_CODE (cond
)
1696 || GET_MODE (cond
) != SImode
)
1697 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1702 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1703 set of altered regs. */
1706 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1708 rtx rev
, reve
, exp
= *expr
;
1710 /* If some register gets altered later, we do not really speak about its
1711 value at the time of comparison. */
1713 && for_each_rtx (&cond
, altered_reg_used
, altered
))
1716 if (GET_CODE (cond
) == EQ
1717 && REG_P (XEXP (cond
, 0)) && CONSTANT_P (XEXP (cond
, 1)))
1719 *expr
= simplify_replace_rtx (*expr
, XEXP (cond
, 0), XEXP (cond
, 1));
1723 if (!COMPARISON_P (exp
))
1726 rev
= reversed_condition (cond
);
1727 reve
= reversed_condition (exp
);
1729 cond
= canon_condition (cond
);
1730 exp
= canon_condition (exp
);
1732 rev
= canon_condition (rev
);
1734 reve
= canon_condition (reve
);
1736 if (rtx_equal_p (exp
, cond
))
1738 *expr
= const_true_rtx
;
1742 if (rev
&& rtx_equal_p (exp
, rev
))
1748 if (implies_p (cond
, exp
))
1750 *expr
= const_true_rtx
;
1754 if (reve
&& implies_p (cond
, reve
))
1760 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1762 if (rev
&& implies_p (exp
, rev
))
1768 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1769 if (rev
&& reve
&& implies_p (reve
, rev
))
1771 *expr
= const_true_rtx
;
1775 /* We would like to have some other tests here. TODO. */
1780 /* Use relationship between A and *B to eventually eliminate *B.
1781 OP is the operation we consider. */
1784 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1789 /* If A implies *B, we may replace *B by true. */
1790 if (implies_p (a
, *b
))
1791 *b
= const_true_rtx
;
1795 /* If *B implies A, we may replace *B by false. */
1796 if (implies_p (*b
, a
))
1805 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1806 operation we consider. */
1809 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1813 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1814 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1815 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1816 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1819 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1820 is a list, its elements are assumed to be combined using OP. */
1823 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1825 bool expression_valid
;
1826 rtx head
, tail
, insn
, cond_list
, last_valid_expr
;
1828 regset altered
, this_altered
;
1834 if (CONSTANT_P (*expr
))
1837 if (GET_CODE (*expr
) == EXPR_LIST
)
1839 head
= XEXP (*expr
, 0);
1840 tail
= XEXP (*expr
, 1);
1842 eliminate_implied_conditions (op
, &head
, tail
);
1847 neutral
= const_true_rtx
;
1852 neutral
= const0_rtx
;
1853 aggr
= const_true_rtx
;
1860 simplify_using_initial_values (loop
, UNKNOWN
, &head
);
1863 XEXP (*expr
, 0) = aggr
;
1864 XEXP (*expr
, 1) = NULL_RTX
;
1867 else if (head
== neutral
)
1870 simplify_using_initial_values (loop
, op
, expr
);
1873 simplify_using_initial_values (loop
, op
, &tail
);
1875 if (tail
&& XEXP (tail
, 0) == aggr
)
1881 XEXP (*expr
, 0) = head
;
1882 XEXP (*expr
, 1) = tail
;
1886 gcc_assert (op
== UNKNOWN
);
1889 if (for_each_rtx (expr
, replace_single_def_regs
, expr
) == 0)
1891 if (CONSTANT_P (*expr
))
1894 e
= loop_preheader_edge (loop
);
1895 if (e
->src
== ENTRY_BLOCK_PTR
)
1898 altered
= ALLOC_REG_SET (®_obstack
);
1899 this_altered
= ALLOC_REG_SET (®_obstack
);
1901 expression_valid
= true;
1902 last_valid_expr
= *expr
;
1903 cond_list
= NULL_RTX
;
1906 insn
= BB_END (e
->src
);
1907 if (any_condjump_p (insn
))
1909 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false, true);
1911 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1912 cond
= reversed_condition (cond
);
1916 simplify_using_condition (cond
, expr
, altered
);
1920 if (CONSTANT_P (*expr
))
1922 for (note
= cond_list
; note
; note
= XEXP (note
, 1))
1924 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
1925 if (CONSTANT_P (*expr
))
1929 cond_list
= alloc_EXPR_LIST (0, cond
, cond_list
);
1933 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
1941 CLEAR_REG_SET (this_altered
);
1942 note_stores (PATTERN (insn
), mark_altered
, this_altered
);
1947 /* Kill all call clobbered registers. */
1948 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1949 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
1950 SET_REGNO_REG_SET (this_altered
, i
);
1953 if (suitable_set_for_replacement (insn
, &dest
, &src
))
1955 rtx
*pnote
, *pnote_next
;
1957 replace_in_expr (expr
, dest
, src
);
1958 if (CONSTANT_P (*expr
))
1961 for (pnote
= &cond_list
; *pnote
; pnote
= pnote_next
)
1964 rtx old_cond
= XEXP (note
, 0);
1966 pnote_next
= &XEXP (note
, 1);
1967 replace_in_expr (&XEXP (note
, 0), dest
, src
);
1969 /* We can no longer use a condition that has been simplified
1970 to a constant, and simplify_using_condition will abort if
1972 if (CONSTANT_P (XEXP (note
, 0)))
1974 *pnote
= *pnote_next
;
1976 free_EXPR_LIST_node (note
);
1978 /* Retry simplifications with this condition if either the
1979 expression or the condition changed. */
1980 else if (old_cond
!= XEXP (note
, 0) || old
!= *expr
)
1981 simplify_using_condition (XEXP (note
, 0), expr
, altered
);
1985 /* If we did not use this insn to make a replacement, any overlap
1986 between stores in this insn and our expression will cause the
1987 expression to become invalid. */
1988 if (for_each_rtx (expr
, altered_reg_used
, this_altered
))
1991 if (CONSTANT_P (*expr
))
1994 IOR_REG_SET (altered
, this_altered
);
1996 /* If the expression now contains regs that have been altered, we
1997 can't return it to the caller. However, it is still valid for
1998 further simplification, so keep searching to see if we can
1999 eventually turn it into a constant. */
2000 if (for_each_rtx (expr
, altered_reg_used
, altered
))
2001 expression_valid
= false;
2002 if (expression_valid
)
2003 last_valid_expr
= *expr
;
2006 if (!single_pred_p (e
->src
)
2007 || single_pred (e
->src
) == ENTRY_BLOCK_PTR
)
2009 e
= single_pred_edge (e
->src
);
2013 free_EXPR_LIST_list (&cond_list
);
2014 if (!CONSTANT_P (*expr
))
2015 *expr
= last_valid_expr
;
2016 FREE_REG_SET (altered
);
2017 FREE_REG_SET (this_altered
);
2020 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2021 that IV occurs as left operands of comparison COND and its signedness
2022 is SIGNED_P to DESC. */
2025 shorten_into_mode (struct rtx_iv
*iv
, enum machine_mode mode
,
2026 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
2028 rtx mmin
, mmax
, cond_over
, cond_under
;
2030 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
2031 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
2033 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
2042 if (cond_under
!= const0_rtx
)
2044 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2045 if (cond_over
!= const0_rtx
)
2046 desc
->noloop_assumptions
=
2047 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
2054 if (cond_over
!= const0_rtx
)
2056 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2057 if (cond_under
!= const0_rtx
)
2058 desc
->noloop_assumptions
=
2059 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
2063 if (cond_over
!= const0_rtx
)
2065 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
2066 if (cond_under
!= const0_rtx
)
2068 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
2076 iv
->extend
= signed_p
? SIGN_EXTEND
: ZERO_EXTEND
;
2079 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2080 subregs of the same mode if possible (sometimes it is necessary to add
2081 some assumptions to DESC). */
2084 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
2085 enum rtx_code cond
, struct niter_desc
*desc
)
2087 enum machine_mode comp_mode
;
2090 /* If the ivs behave specially in the first iteration, or are
2091 added/multiplied after extending, we ignore them. */
2092 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
2094 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
2097 /* If there is some extend, it must match signedness of the comparison. */
2102 if (iv0
->extend
== ZERO_EXTEND
2103 || iv1
->extend
== ZERO_EXTEND
)
2110 if (iv0
->extend
== SIGN_EXTEND
2111 || iv1
->extend
== SIGN_EXTEND
)
2117 if (iv0
->extend
!= UNKNOWN
2118 && iv1
->extend
!= UNKNOWN
2119 && iv0
->extend
!= iv1
->extend
)
2123 if (iv0
->extend
!= UNKNOWN
)
2124 signed_p
= iv0
->extend
== SIGN_EXTEND
;
2125 if (iv1
->extend
!= UNKNOWN
)
2126 signed_p
= iv1
->extend
== SIGN_EXTEND
;
2133 /* Values of both variables should be computed in the same mode. These
2134 might indeed be different, if we have comparison like
2136 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2138 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2139 in different modes. This does not seem impossible to handle, but
2140 it hardly ever occurs in practice.
2142 The only exception is the case when one of operands is invariant.
2143 For example pentium 3 generates comparisons like
2144 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2145 definitely do not want this prevent the optimization. */
2146 comp_mode
= iv0
->extend_mode
;
2147 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
2148 comp_mode
= iv1
->extend_mode
;
2150 if (iv0
->extend_mode
!= comp_mode
)
2152 if (iv0
->mode
!= iv0
->extend_mode
2153 || iv0
->step
!= const0_rtx
)
2156 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2157 comp_mode
, iv0
->base
, iv0
->mode
);
2158 iv0
->extend_mode
= comp_mode
;
2161 if (iv1
->extend_mode
!= comp_mode
)
2163 if (iv1
->mode
!= iv1
->extend_mode
2164 || iv1
->step
!= const0_rtx
)
2167 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
2168 comp_mode
, iv1
->base
, iv1
->mode
);
2169 iv1
->extend_mode
= comp_mode
;
2172 /* Check that both ivs belong to a range of a single mode. If one of the
2173 operands is an invariant, we may need to shorten it into the common
2175 if (iv0
->mode
== iv0
->extend_mode
2176 && iv0
->step
== const0_rtx
2177 && iv0
->mode
!= iv1
->mode
)
2178 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
2180 if (iv1
->mode
== iv1
->extend_mode
2181 && iv1
->step
== const0_rtx
2182 && iv0
->mode
!= iv1
->mode
)
2183 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
2185 if (iv0
->mode
!= iv1
->mode
)
2188 desc
->mode
= iv0
->mode
;
2189 desc
->signed_p
= signed_p
;
2194 /* Tries to estimate the maximum number of iterations in LOOP, and store the
2195 result in DESC. This function is called from iv_number_of_iterations with
2196 a number of fields in DESC already filled in. OLD_NITER is the original
2197 expression for the number of iterations, before we tried to simplify it. */
2199 static unsigned HOST_WIDEST_INT
2200 determine_max_iter (struct loop
*loop
, struct niter_desc
*desc
, rtx old_niter
)
2202 rtx niter
= desc
->niter_expr
;
2203 rtx mmin
, mmax
, cmp
;
2204 unsigned HOST_WIDEST_INT nmax
, inc
;
2206 if (GET_CODE (niter
) == AND
2207 && CONST_INT_P (XEXP (niter
, 0)))
2209 nmax
= INTVAL (XEXP (niter
, 0));
2210 if (!(nmax
& (nmax
+ 1)))
2212 desc
->niter_max
= nmax
;
2217 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
2218 nmax
= INTVAL (mmax
) - INTVAL (mmin
);
2220 if (GET_CODE (niter
) == UDIV
)
2222 if (!CONST_INT_P (XEXP (niter
, 1)))
2224 desc
->niter_max
= nmax
;
2227 inc
= INTVAL (XEXP (niter
, 1));
2228 niter
= XEXP (niter
, 0);
2233 /* We could use a binary search here, but for now improving the upper
2234 bound by just one eliminates one important corner case. */
2235 cmp
= simplify_gen_relational (desc
->signed_p
? LT
: LTU
, VOIDmode
,
2236 desc
->mode
, old_niter
, mmax
);
2237 simplify_using_initial_values (loop
, UNKNOWN
, &cmp
);
2238 if (cmp
== const_true_rtx
)
2243 fprintf (dump_file
, ";; improved upper bound by one.\n");
2245 desc
->niter_max
= nmax
/ inc
;
2249 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2250 the result into DESC. Very similar to determine_number_of_iterations
2251 (basically its rtl version), complicated by things like subregs. */
2254 iv_number_of_iterations (struct loop
*loop
, rtx insn
, rtx condition
,
2255 struct niter_desc
*desc
)
2257 rtx op0
, op1
, delta
, step
, bound
, may_xform
, tmp
, tmp0
, tmp1
;
2258 struct rtx_iv iv0
, iv1
, tmp_iv
;
2259 rtx assumption
, may_not_xform
;
2261 enum machine_mode mode
, comp_mode
;
2262 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
2263 unsigned HOST_WIDEST_INT s
, size
, d
, inv
;
2264 HOST_WIDEST_INT up
, down
, inc
, step_val
;
2265 int was_sharp
= false;
2269 /* The meaning of these assumptions is this:
2271 then the rest of information does not have to be valid
2272 if noloop_assumptions then the loop does not roll
2273 if infinite then this exit is never used */
2275 desc
->assumptions
= NULL_RTX
;
2276 desc
->noloop_assumptions
= NULL_RTX
;
2277 desc
->infinite
= NULL_RTX
;
2278 desc
->simple_p
= true;
2280 desc
->const_iter
= false;
2281 desc
->niter_expr
= NULL_RTX
;
2282 desc
->niter_max
= 0;
2284 cond
= GET_CODE (condition
);
2285 gcc_assert (COMPARISON_P (condition
));
2287 mode
= GET_MODE (XEXP (condition
, 0));
2288 if (mode
== VOIDmode
)
2289 mode
= GET_MODE (XEXP (condition
, 1));
2290 /* The constant comparisons should be folded. */
2291 gcc_assert (mode
!= VOIDmode
);
2293 /* We only handle integers or pointers. */
2294 if (GET_MODE_CLASS (mode
) != MODE_INT
2295 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
2298 op0
= XEXP (condition
, 0);
2299 if (!iv_analyze (insn
, op0
, &iv0
))
2301 if (iv0
.extend_mode
== VOIDmode
)
2302 iv0
.mode
= iv0
.extend_mode
= mode
;
2304 op1
= XEXP (condition
, 1);
2305 if (!iv_analyze (insn
, op1
, &iv1
))
2307 if (iv1
.extend_mode
== VOIDmode
)
2308 iv1
.mode
= iv1
.extend_mode
= mode
;
2310 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
2311 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
2314 /* Check condition and normalize it. */
2322 tmp_iv
= iv0
; iv0
= iv1
; iv1
= tmp_iv
;
2323 cond
= swap_condition (cond
);
2335 /* Handle extends. This is relatively nontrivial, so we only try in some
2336 easy cases, when we can canonicalize the ivs (possibly by adding some
2337 assumptions) to shape subreg (base + i * step). This function also fills
2338 in desc->mode and desc->signed_p. */
2340 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2343 comp_mode
= iv0
.extend_mode
;
2345 size
= GET_MODE_BITSIZE (mode
);
2346 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2347 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2348 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2350 if (!CONST_INT_P (iv0
.step
) || !CONST_INT_P (iv1
.step
))
2353 /* We can take care of the case of two induction variables chasing each other
2354 if the test is NE. I have never seen a loop using it, but still it is
2356 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2361 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2362 iv1
.step
= const0_rtx
;
2365 /* This is either infinite loop or the one that ends immediately, depending
2366 on initial values. Unswitching should remove this kind of conditions. */
2367 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2372 if (iv0
.step
== const0_rtx
)
2373 step_val
= -INTVAL (iv1
.step
);
2375 step_val
= INTVAL (iv0
.step
);
2377 /* Ignore loops of while (i-- < 10) type. */
2381 step_is_pow2
= !(step_val
& (step_val
- 1));
2385 /* We do not care about whether the step is power of two in this
2387 step_is_pow2
= false;
2391 /* Some more condition normalization. We must record some assumptions
2392 due to overflows. */
2397 /* We want to take care only of non-sharp relationals; this is easy,
2398 as in cases the overflow would make the transformation unsafe
2399 the loop does not roll. Seemingly it would make more sense to want
2400 to take care of sharp relationals instead, as NE is more similar to
2401 them, but the problem is that here the transformation would be more
2402 difficult due to possibly infinite loops. */
2403 if (iv0
.step
== const0_rtx
)
2405 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2406 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2408 if (assumption
== const_true_rtx
)
2409 goto zero_iter_simplify
;
2410 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2411 iv0
.base
, const1_rtx
);
2415 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2416 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2418 if (assumption
== const_true_rtx
)
2419 goto zero_iter_simplify
;
2420 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2421 iv1
.base
, constm1_rtx
);
2424 if (assumption
!= const0_rtx
)
2425 desc
->noloop_assumptions
=
2426 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2427 cond
= (cond
== LT
) ? LE
: LEU
;
2429 /* It will be useful to be able to tell the difference once more in
2430 LE -> NE reduction. */
2436 /* Take care of trivially infinite loops. */
2439 if (iv0
.step
== const0_rtx
)
2441 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2442 if (rtx_equal_p (tmp
, mode_mmin
))
2445 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2446 /* Fill in the remaining fields somehow. */
2447 goto zero_iter_simplify
;
2452 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2453 if (rtx_equal_p (tmp
, mode_mmax
))
2456 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2457 /* Fill in the remaining fields somehow. */
2458 goto zero_iter_simplify
;
2463 /* If we can we want to take care of NE conditions instead of size
2464 comparisons, as they are much more friendly (most importantly
2465 this takes care of special handling of loops with step 1). We can
2466 do it if we first check that upper bound is greater or equal to
2467 lower bound, their difference is constant c modulo step and that
2468 there is not an overflow. */
2471 if (iv0
.step
== const0_rtx
)
2472 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2475 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2476 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2477 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2478 may_xform
= const0_rtx
;
2479 may_not_xform
= const_true_rtx
;
2481 if (CONST_INT_P (delta
))
2483 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2485 /* A special case. We have transformed condition of type
2486 for (i = 0; i < 4; i += 4)
2488 for (i = 0; i <= 3; i += 4)
2489 obviously if the test for overflow during that transformation
2490 passed, we cannot overflow here. Most importantly any
2491 loop with sharp end condition and step 1 falls into this
2492 category, so handling this case specially is definitely
2493 worth the troubles. */
2494 may_xform
= const_true_rtx
;
2496 else if (iv0
.step
== const0_rtx
)
2498 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2499 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2500 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2501 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2502 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2504 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2510 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2511 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2512 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2513 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2514 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2516 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2522 if (may_xform
!= const0_rtx
)
2524 /* We perform the transformation always provided that it is not
2525 completely senseless. This is OK, as we would need this assumption
2526 to determine the number of iterations anyway. */
2527 if (may_xform
!= const_true_rtx
)
2529 /* If the step is a power of two and the final value we have
2530 computed overflows, the cycle is infinite. Otherwise it
2531 is nontrivial to compute the number of iterations. */
2533 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2536 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2540 /* We are going to lose some information about upper bound on
2541 number of iterations in this step, so record the information
2543 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2544 if (CONST_INT_P (iv1
.base
))
2545 up
= INTVAL (iv1
.base
);
2547 up
= INTVAL (mode_mmax
) - inc
;
2548 down
= INTVAL (CONST_INT_P (iv0
.base
)
2551 desc
->niter_max
= (up
- down
) / inc
+ 1;
2553 if (iv0
.step
== const0_rtx
)
2555 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2556 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2560 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2561 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2564 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2565 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2566 assumption
= simplify_gen_relational (reverse_condition (cond
),
2567 SImode
, mode
, tmp0
, tmp1
);
2568 if (assumption
== const_true_rtx
)
2569 goto zero_iter_simplify
;
2570 else if (assumption
!= const0_rtx
)
2571 desc
->noloop_assumptions
=
2572 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2577 /* Count the number of iterations. */
2580 /* Everything we do here is just arithmetics modulo size of mode. This
2581 makes us able to do more involved computations of number of iterations
2582 than in other cases. First transform the condition into shape
2583 s * i <> c, with s positive. */
2584 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2585 iv0
.base
= const0_rtx
;
2586 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2587 iv1
.step
= const0_rtx
;
2588 if (INTVAL (iv0
.step
) < 0)
2590 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, mode
);
2591 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, mode
);
2593 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2595 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2596 is infinite. Otherwise, the number of iterations is
2597 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2598 s
= INTVAL (iv0
.step
); d
= 1;
2605 bound
= GEN_INT (((unsigned HOST_WIDEST_INT
) 1 << (size
- 1 ) << 1) - 1);
2607 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2608 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, GEN_INT (d
));
2609 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2610 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2612 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, GEN_INT (d
));
2613 inv
= inverse (s
, size
);
2614 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, gen_int_mode (inv
, mode
));
2615 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2619 if (iv1
.step
== const0_rtx
)
2620 /* Condition in shape a + s * i <= b
2621 We must know that b + s does not overflow and a <= b + s and then we
2622 can compute number of iterations as (b + s - a) / s. (It might
2623 seem that we in fact could be more clever about testing the b + s
2624 overflow condition using some information about b - a mod s,
2625 but it was already taken into account during LE -> NE transform). */
2628 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2629 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2631 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2632 lowpart_subreg (mode
, step
,
2638 /* If s is power of 2, we know that the loop is infinite if
2639 a % s <= b % s and b + s overflows. */
2640 assumption
= simplify_gen_relational (reverse_condition (cond
),
2644 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2645 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2646 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2647 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2649 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2653 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2656 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2659 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, iv0
.step
);
2660 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2661 assumption
= simplify_gen_relational (reverse_condition (cond
),
2662 SImode
, mode
, tmp0
, tmp
);
2664 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2665 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2669 /* Condition in shape a <= b - s * i
2670 We must know that a - s does not overflow and a - s <= b and then
2671 we can again compute number of iterations as (b - (a - s)) / s. */
2672 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2673 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2674 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2676 bound
= simplify_gen_binary (PLUS
, mode
, mode_mmin
,
2677 lowpart_subreg (mode
, step
, comp_mode
));
2682 /* If s is power of 2, we know that the loop is infinite if
2683 a % s <= b % s and a - s overflows. */
2684 assumption
= simplify_gen_relational (reverse_condition (cond
),
2688 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2689 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2690 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2691 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2693 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2697 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2700 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2703 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2704 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2705 assumption
= simplify_gen_relational (reverse_condition (cond
),
2708 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2709 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2711 if (assumption
== const_true_rtx
)
2712 goto zero_iter_simplify
;
2713 else if (assumption
!= const0_rtx
)
2714 desc
->noloop_assumptions
=
2715 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2716 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2717 desc
->niter_expr
= delta
;
2720 old_niter
= desc
->niter_expr
;
2722 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2723 if (desc
->assumptions
2724 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2726 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2727 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2728 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2730 /* Rerun the simplification. Consider code (created by copying loop headers)
2742 The first pass determines that i = 0, the second pass uses it to eliminate
2743 noloop assumption. */
2745 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2746 if (desc
->assumptions
2747 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2749 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2750 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2751 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2753 if (desc
->noloop_assumptions
2754 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2757 if (CONST_INT_P (desc
->niter_expr
))
2759 unsigned HOST_WIDEST_INT val
= INTVAL (desc
->niter_expr
);
2761 desc
->const_iter
= true;
2762 desc
->niter_max
= desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2766 if (!desc
->niter_max
)
2767 desc
->niter_max
= determine_max_iter (loop
, desc
, old_niter
);
2769 /* simplify_using_initial_values does a copy propagation on the registers
2770 in the expression for the number of iterations. This prolongs life
2771 ranges of registers and increases register pressure, and usually
2772 brings no gain (and if it happens to do, the cse pass will take care
2773 of it anyway). So prevent this behavior, unless it enabled us to
2774 derive that the number of iterations is a constant. */
2775 desc
->niter_expr
= old_niter
;
2781 /* Simplify the assumptions. */
2782 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2783 if (desc
->assumptions
2784 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2786 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2790 desc
->const_iter
= true;
2792 desc
->niter_max
= 0;
2793 desc
->noloop_assumptions
= NULL_RTX
;
2794 desc
->niter_expr
= const0_rtx
;
2798 desc
->simple_p
= false;
2802 /* Checks whether E is a simple exit from LOOP and stores its description
2806 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2808 basic_block exit_bb
;
2813 desc
->simple_p
= false;
2815 /* It must belong directly to the loop. */
2816 if (exit_bb
->loop_father
!= loop
)
2819 /* It must be tested (at least) once during any iteration. */
2820 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2823 /* It must end in a simple conditional jump. */
2824 if (!any_condjump_p (BB_END (exit_bb
)))
2827 ein
= EDGE_SUCC (exit_bb
, 0);
2829 ein
= EDGE_SUCC (exit_bb
, 1);
2832 desc
->in_edge
= ein
;
2834 /* Test whether the condition is suitable. */
2835 if (!(condition
= get_condition (BB_END (ein
->src
), &at
, false, false)))
2838 if (ein
->flags
& EDGE_FALLTHRU
)
2840 condition
= reversed_condition (condition
);
2845 /* Check that we are able to determine number of iterations and fill
2846 in information about it. */
2847 iv_number_of_iterations (loop
, at
, condition
, desc
);
2850 /* Finds a simple exit of LOOP and stores its description into DESC. */
2853 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2858 struct niter_desc act
;
2862 desc
->simple_p
= false;
2863 body
= get_loop_body (loop
);
2865 for (i
= 0; i
< loop
->num_nodes
; i
++)
2867 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
2869 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2872 check_simple_exit (loop
, e
, &act
);
2880 /* Prefer constant iterations; the less the better. */
2882 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
2885 /* Also if the actual exit may be infinite, while the old one
2886 not, prefer the old one. */
2887 if (act
.infinite
&& !desc
->infinite
)
2899 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
2900 fprintf (dump_file
, " simple exit %d -> %d\n",
2901 desc
->out_edge
->src
->index
,
2902 desc
->out_edge
->dest
->index
);
2903 if (desc
->assumptions
)
2905 fprintf (dump_file
, " assumptions: ");
2906 print_rtl (dump_file
, desc
->assumptions
);
2907 fprintf (dump_file
, "\n");
2909 if (desc
->noloop_assumptions
)
2911 fprintf (dump_file
, " does not roll if: ");
2912 print_rtl (dump_file
, desc
->noloop_assumptions
);
2913 fprintf (dump_file
, "\n");
2917 fprintf (dump_file
, " infinite if: ");
2918 print_rtl (dump_file
, desc
->infinite
);
2919 fprintf (dump_file
, "\n");
2922 fprintf (dump_file
, " number of iterations: ");
2923 print_rtl (dump_file
, desc
->niter_expr
);
2924 fprintf (dump_file
, "\n");
2926 fprintf (dump_file
, " upper bound: ");
2927 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, desc
->niter_max
);
2928 fprintf (dump_file
, "\n");
2931 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
2937 /* Creates a simple loop description of LOOP if it was not computed
2941 get_simple_loop_desc (struct loop
*loop
)
2943 struct niter_desc
*desc
= simple_loop_desc (loop
);
2948 /* At least desc->infinite is not always initialized by
2949 find_simple_loop_exit. */
2950 desc
= XCNEW (struct niter_desc
);
2951 iv_analysis_loop_init (loop
);
2952 find_simple_exit (loop
, desc
);
2955 if (desc
->simple_p
&& (desc
->assumptions
|| desc
->infinite
))
2957 const char *wording
;
2959 /* Assume that no overflow happens and that the loop is finite.
2960 We already warned at the tree level if we ran optimizations there. */
2961 if (!flag_tree_loop_optimize
&& warn_unsafe_loop_optimizations
)
2966 flag_unsafe_loop_optimizations
2967 ? N_("assuming that the loop is not infinite")
2968 : N_("cannot optimize possibly infinite loops");
2969 warning (OPT_Wunsafe_loop_optimizations
, "%s",
2972 if (desc
->assumptions
)
2975 flag_unsafe_loop_optimizations
2976 ? N_("assuming that the loop counter does not overflow")
2977 : N_("cannot optimize loop, the loop counter may overflow");
2978 warning (OPT_Wunsafe_loop_optimizations
, "%s",
2983 if (flag_unsafe_loop_optimizations
)
2985 desc
->assumptions
= NULL_RTX
;
2986 desc
->infinite
= NULL_RTX
;
2993 /* Releases simple loop description for LOOP. */
2996 free_simple_loop_desc (struct loop
*loop
)
2998 struct niter_desc
*desc
= simple_loop_desc (loop
);