1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
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 variable is analyzed by walking the use-def chains. When a biv
27 is found, it is cached in the bivs hash table. When register is proved
28 to be a giv, its description 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.
48 iv_current_loop_df (): Returns the dataflow object for the current loop used
53 #include "coretypes.h"
56 #include "hard-reg-set.h"
58 #include "basic-block.h"
67 /* Possible return values of iv_get_reaching_def. */
71 /* More than one reaching def, or reaching def that does not
75 /* The use is trivial invariant of the loop, i.e. is not changed
79 /* The use is reached by initial value and a value from the
80 previous iteration. */
83 /* The use has single dominating def. */
87 /* Information about a biv. */
91 unsigned regno; /* The register of the biv. */
92 struct rtx_iv iv; /* Value of the biv. */
95 /* Induction variable stored at the reference. */
96 #define DF_REF_IV(REF) ((struct rtx_iv *) DF_REF_DATA (REF))
97 #define DF_REF_IV_SET(REF, IV) DF_REF_DATA (REF) = (IV)
99 /* The current loop. */
101 static struct loop *current_loop;
103 /* Dataflow information for the current loop. */
105 static struct df *df = NULL;
107 /* Bivs of the current loop. */
111 /* Return the dataflow object for the current loop. */
113 iv_current_loop_df (void)
118 static bool iv_analyze_op (rtx, rtx, struct rtx_iv *);
120 /* Dumps information about IV to FILE. */
122 extern void dump_iv_info (FILE *, struct rtx_iv *);
124 dump_iv_info (FILE *file, struct rtx_iv *iv)
128 fprintf (file, "not simple");
132 if (iv->step == const0_rtx
133 && !iv->first_special)
134 fprintf (file, "invariant ");
136 print_rtl (file, iv->base);
137 if (iv->step != const0_rtx)
139 fprintf (file, " + ");
140 print_rtl (file, iv->step);
141 fprintf (file, " * iteration");
143 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
145 if (iv->mode != iv->extend_mode)
146 fprintf (file, " %s to %s",
147 rtx_name[iv->extend],
148 GET_MODE_NAME (iv->extend_mode));
150 if (iv->mult != const1_rtx)
152 fprintf (file, " * ");
153 print_rtl (file, iv->mult);
155 if (iv->delta != const0_rtx)
157 fprintf (file, " + ");
158 print_rtl (file, iv->delta);
160 if (iv->first_special)
161 fprintf (file, " (first special)");
164 /* Generates a subreg to get the least significant part of EXPR (in mode
165 INNER_MODE) to OUTER_MODE. */
168 lowpart_subreg (enum machine_mode outer_mode, rtx expr,
169 enum machine_mode inner_mode)
171 return simplify_gen_subreg (outer_mode, expr, inner_mode,
172 subreg_lowpart_offset (outer_mode, inner_mode));
175 /* Checks whether REG is a well-behaved register. */
178 simple_reg_p (rtx reg)
182 if (GET_CODE (reg) == SUBREG)
184 if (!subreg_lowpart_p (reg))
186 reg = SUBREG_REG (reg);
193 if (HARD_REGISTER_NUM_P (r))
196 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
202 /* Clears the information about ivs stored in df. */
207 unsigned i, n_defs = DF_DEFS_SIZE (df);
211 for (i = 0; i < n_defs; i++)
213 def = DF_DEFS_GET (df, i);
214 iv = DF_REF_IV (def);
218 DF_REF_IV_SET (def, NULL);
224 /* Returns hash value for biv B. */
227 biv_hash (const void *b)
229 return ((const struct biv_entry *) b)->regno;
232 /* Compares biv B and register R. */
235 biv_eq (const void *b, const void *r)
237 return ((const struct biv_entry *) b)->regno == REGNO ((rtx) r);
240 /* Prepare the data for an induction variable analysis of a LOOP. */
243 iv_analysis_loop_init (struct loop *loop)
245 basic_block *body = get_loop_body_in_dom_order (loop), bb;
246 bitmap blocks = BITMAP_ALLOC (NULL);
248 bool first_time = (df == NULL);
252 /* Clear the information from the analysis of the previous loop. */
255 df = df_init (DF_HARD_REGS | DF_EQUIV_NOTES);
256 df_chain_add_problem (df, DF_UD_CHAIN);
257 bivs = htab_create (10, biv_hash, biv_eq, free);
259 for (i = 0; i < loop->num_nodes; i++)
262 bitmap_set_bit (blocks, bb->index);
264 df_set_blocks (df, blocks);
266 BITMAP_FREE (blocks);
270 /* Finds the definition of REG that dominates loop latch and stores
271 it to DEF. Returns false if there is not a single definition
272 dominating the latch. If REG has no definition in loop, DEF
273 is set to NULL and true is returned. */
276 latch_dominating_def (rtx reg, struct df_ref **def)
278 struct df_ref *single_rd = NULL, *adef;
279 unsigned regno = REGNO (reg);
280 struct df_reg_info *reg_info = DF_REG_DEF_GET (df, regno);
281 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (df, current_loop->latch);
283 for (adef = reg_info->reg_chain; adef; adef = adef->next_reg)
285 if (!bitmap_bit_p (bb_info->out, DF_REF_ID (adef)))
288 /* More than one reaching definition. */
292 if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
302 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
304 static enum iv_grd_result
305 iv_get_reaching_def (rtx insn, rtx reg, struct df_ref **def)
307 struct df_ref *use, *adef;
308 basic_block def_bb, use_bb;
313 if (!simple_reg_p (reg))
315 if (GET_CODE (reg) == SUBREG)
316 reg = SUBREG_REG (reg);
317 gcc_assert (REG_P (reg));
319 use = df_find_use (df, insn, reg);
320 gcc_assert (use != NULL);
322 if (!DF_REF_CHAIN (use))
323 return GRD_INVARIANT;
325 /* More than one reaching def. */
326 if (DF_REF_CHAIN (use)->next)
329 adef = DF_REF_CHAIN (use)->ref;
330 def_insn = DF_REF_INSN (adef);
331 def_bb = DF_REF_BB (adef);
332 use_bb = BLOCK_FOR_INSN (insn);
334 if (use_bb == def_bb)
335 dom_p = (DF_INSN_LUID (df, def_insn) < DF_INSN_LUID (df, insn));
337 dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
342 return GRD_SINGLE_DOM;
345 /* The definition does not dominate the use. This is still OK if
346 this may be a use of a biv, i.e. if the def_bb dominates loop
348 if (just_once_each_iteration_p (current_loop, def_bb))
349 return GRD_MAYBE_BIV;
354 /* Sets IV to invariant CST in MODE. Always returns true (just for
355 consistency with other iv manipulation functions that may fail). */
358 iv_constant (struct rtx_iv *iv, rtx cst, enum machine_mode mode)
360 if (mode == VOIDmode)
361 mode = GET_MODE (cst);
365 iv->step = const0_rtx;
366 iv->first_special = false;
367 iv->extend = UNKNOWN;
368 iv->extend_mode = iv->mode;
369 iv->delta = const0_rtx;
370 iv->mult = const1_rtx;
375 /* Evaluates application of subreg to MODE on IV. */
378 iv_subreg (struct rtx_iv *iv, enum machine_mode mode)
380 /* If iv is invariant, just calculate the new value. */
381 if (iv->step == const0_rtx
382 && !iv->first_special)
384 rtx val = get_iv_value (iv, const0_rtx);
385 val = lowpart_subreg (mode, val, iv->extend_mode);
388 iv->extend = UNKNOWN;
389 iv->mode = iv->extend_mode = mode;
390 iv->delta = const0_rtx;
391 iv->mult = const1_rtx;
395 if (iv->extend_mode == mode)
398 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
401 iv->extend = UNKNOWN;
404 iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
405 simplify_gen_binary (MULT, iv->extend_mode,
406 iv->base, iv->mult));
407 iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
408 iv->mult = const1_rtx;
409 iv->delta = const0_rtx;
410 iv->first_special = false;
415 /* Evaluates application of EXTEND to MODE on IV. */
418 iv_extend (struct rtx_iv *iv, enum rtx_code extend, enum machine_mode mode)
420 /* If iv is invariant, just calculate the new value. */
421 if (iv->step == const0_rtx
422 && !iv->first_special)
424 rtx val = get_iv_value (iv, const0_rtx);
425 val = simplify_gen_unary (extend, mode, val, iv->extend_mode);
428 iv->extend = UNKNOWN;
429 iv->mode = iv->extend_mode = mode;
430 iv->delta = const0_rtx;
431 iv->mult = const1_rtx;
435 if (mode != iv->extend_mode)
438 if (iv->extend != UNKNOWN
439 && iv->extend != extend)
447 /* Evaluates negation of IV. */
450 iv_neg (struct rtx_iv *iv)
452 if (iv->extend == UNKNOWN)
454 iv->base = simplify_gen_unary (NEG, iv->extend_mode,
455 iv->base, iv->extend_mode);
456 iv->step = simplify_gen_unary (NEG, iv->extend_mode,
457 iv->step, iv->extend_mode);
461 iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
462 iv->delta, iv->extend_mode);
463 iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
464 iv->mult, iv->extend_mode);
470 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
473 iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
475 enum machine_mode mode;
478 /* Extend the constant to extend_mode of the other operand if necessary. */
479 if (iv0->extend == UNKNOWN
480 && iv0->mode == iv0->extend_mode
481 && iv0->step == const0_rtx
482 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
484 iv0->extend_mode = iv1->extend_mode;
485 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
486 iv0->base, iv0->mode);
488 if (iv1->extend == UNKNOWN
489 && iv1->mode == iv1->extend_mode
490 && iv1->step == const0_rtx
491 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
493 iv1->extend_mode = iv0->extend_mode;
494 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
495 iv1->base, iv1->mode);
498 mode = iv0->extend_mode;
499 if (mode != iv1->extend_mode)
502 if (iv0->extend == UNKNOWN && iv1->extend == UNKNOWN)
504 if (iv0->mode != iv1->mode)
507 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
508 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
513 /* Handle addition of constant. */
514 if (iv1->extend == UNKNOWN
516 && iv1->step == const0_rtx)
518 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
522 if (iv0->extend == UNKNOWN
524 && iv0->step == const0_rtx)
532 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
539 /* Evaluates multiplication of IV by constant CST. */
542 iv_mult (struct rtx_iv *iv, rtx mby)
544 enum machine_mode mode = iv->extend_mode;
546 if (GET_MODE (mby) != VOIDmode
547 && GET_MODE (mby) != mode)
550 if (iv->extend == UNKNOWN)
552 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
553 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
557 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
558 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
564 /* Evaluates shift of IV by constant CST. */
567 iv_shift (struct rtx_iv *iv, rtx mby)
569 enum machine_mode mode = iv->extend_mode;
571 if (GET_MODE (mby) != VOIDmode
572 && GET_MODE (mby) != mode)
575 if (iv->extend == UNKNOWN)
577 iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
578 iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
582 iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
583 iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
589 /* The recursive part of get_biv_step. Gets the value of the single value
590 defined by DEF wrto initial value of REG inside loop, in shape described
594 get_biv_step_1 (struct df_ref *def, rtx reg,
595 rtx *inner_step, enum machine_mode *inner_mode,
596 enum rtx_code *extend, enum machine_mode outer_mode,
599 rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
600 rtx next, nextr, tmp;
602 rtx insn = DF_REF_INSN (def);
603 struct df_ref *next_def;
604 enum iv_grd_result res;
606 set = single_set (insn);
610 rhs = find_reg_equal_equiv_note (insn);
616 code = GET_CODE (rhs);
629 if (code == PLUS && CONSTANT_P (op0))
631 tmp = op0; op0 = op1; op1 = tmp;
634 if (!simple_reg_p (op0)
635 || !CONSTANT_P (op1))
638 if (GET_MODE (rhs) != outer_mode)
640 /* ppc64 uses expressions like
642 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
644 this is equivalent to
646 (set x':DI (plus:DI y:DI 1))
647 (set x:SI (subreg:SI (x':DI)). */
648 if (GET_CODE (op0) != SUBREG)
650 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
659 if (GET_MODE (rhs) != outer_mode)
663 if (!simple_reg_p (op0))
673 if (GET_CODE (next) == SUBREG)
675 if (!subreg_lowpart_p (next))
678 nextr = SUBREG_REG (next);
679 if (GET_MODE (nextr) != outer_mode)
685 res = iv_get_reaching_def (insn, nextr, &next_def);
687 if (res == GRD_INVALID || res == GRD_INVARIANT)
690 if (res == GRD_MAYBE_BIV)
692 if (!rtx_equal_p (nextr, reg))
695 *inner_step = const0_rtx;
697 *inner_mode = outer_mode;
698 *outer_step = const0_rtx;
700 else if (!get_biv_step_1 (next_def, reg,
701 inner_step, inner_mode, extend, outer_mode,
705 if (GET_CODE (next) == SUBREG)
707 enum machine_mode amode = GET_MODE (next);
709 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
713 *inner_step = simplify_gen_binary (PLUS, outer_mode,
714 *inner_step, *outer_step);
715 *outer_step = const0_rtx;
727 if (*inner_mode == outer_mode
728 /* See comment in previous switch. */
729 || GET_MODE (rhs) != outer_mode)
730 *inner_step = simplify_gen_binary (code, outer_mode,
733 *outer_step = simplify_gen_binary (code, outer_mode,
739 gcc_assert (GET_MODE (op0) == *inner_mode
740 && *extend == UNKNOWN
741 && *outer_step == const0_rtx);
753 /* Gets the operation on register REG inside loop, in shape
755 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
757 If the operation cannot be described in this shape, return false.
758 LAST_DEF is the definition of REG that dominates loop latch. */
761 get_biv_step (struct df_ref *last_def, rtx reg, rtx *inner_step,
762 enum machine_mode *inner_mode, enum rtx_code *extend,
763 enum machine_mode *outer_mode, rtx *outer_step)
765 *outer_mode = GET_MODE (reg);
767 if (!get_biv_step_1 (last_def, reg,
768 inner_step, inner_mode, extend, *outer_mode,
772 gcc_assert ((*inner_mode == *outer_mode) != (*extend != UNKNOWN));
773 gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
778 /* Records information that DEF is induction variable IV. */
781 record_iv (struct df_ref *def, struct rtx_iv *iv)
783 struct rtx_iv *recorded_iv = xmalloc (sizeof (struct rtx_iv));
786 DF_REF_IV_SET (def, recorded_iv);
789 /* If DEF was already analyzed for bivness, store the description of the biv to
790 IV and return true. Otherwise return false. */
793 analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
795 struct biv_entry *biv = htab_find_with_hash (bivs, def, REGNO (def));
805 record_biv (rtx def, struct rtx_iv *iv)
807 struct biv_entry *biv = xmalloc (sizeof (struct biv_entry));
808 void **slot = htab_find_slot_with_hash (bivs, def, REGNO (def), INSERT);
810 biv->regno = REGNO (def);
816 /* Determines whether DEF is a biv and if so, stores its description
820 iv_analyze_biv (rtx def, struct rtx_iv *iv)
822 rtx inner_step, outer_step;
823 enum machine_mode inner_mode, outer_mode;
824 enum rtx_code extend;
825 struct df_ref *last_def;
829 fprintf (dump_file, "Analyzing ");
830 print_rtl (dump_file, def);
831 fprintf (dump_file, " for bivness.\n");
836 if (!CONSTANT_P (def))
839 return iv_constant (iv, def, VOIDmode);
842 if (!latch_dominating_def (def, &last_def))
845 fprintf (dump_file, " not simple.\n");
850 return iv_constant (iv, def, VOIDmode);
852 if (analyzed_for_bivness_p (def, iv))
855 fprintf (dump_file, " already analysed.\n");
856 return iv->base != NULL_RTX;
859 if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
860 &outer_mode, &outer_step))
866 /* Loop transforms base to es (base + inner_step) + outer_step,
867 where es means extend of subreg between inner_mode and outer_mode.
868 The corresponding induction variable is
870 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
872 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
873 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
874 iv->mode = inner_mode;
875 iv->extend_mode = outer_mode;
877 iv->mult = const1_rtx;
878 iv->delta = outer_step;
879 iv->first_special = inner_mode != outer_mode;
884 fprintf (dump_file, " ");
885 dump_iv_info (dump_file, iv);
886 fprintf (dump_file, "\n");
889 record_biv (def, iv);
890 return iv->base != NULL_RTX;
893 /* Analyzes expression RHS used at INSN and stores the result to *IV.
894 The mode of the induction variable is MODE. */
897 iv_analyze_expr (rtx insn, rtx rhs, enum machine_mode mode, struct rtx_iv *iv)
899 rtx mby = NULL_RTX, tmp;
900 rtx op0 = NULL_RTX, op1 = NULL_RTX;
901 struct rtx_iv iv0, iv1;
902 enum rtx_code code = GET_CODE (rhs);
903 enum machine_mode omode = mode;
909 gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
915 if (!iv_analyze_op (insn, rhs, iv))
918 if (iv->mode == VOIDmode)
921 iv->extend_mode = mode;
937 omode = GET_MODE (op0);
949 if (!CONSTANT_P (mby))
955 if (!CONSTANT_P (mby))
962 if (!CONSTANT_P (mby))
971 && !iv_analyze_expr (insn, op0, omode, &iv0))
975 && !iv_analyze_expr (insn, op1, omode, &iv1))
982 if (!iv_extend (&iv0, code, mode))
993 if (!iv_add (&iv0, &iv1, code))
998 if (!iv_mult (&iv0, mby))
1003 if (!iv_shift (&iv0, mby))
1012 return iv->base != NULL_RTX;
1015 /* Analyzes iv DEF and stores the result to *IV. */
1018 iv_analyze_def (struct df_ref *def, struct rtx_iv *iv)
1020 rtx insn = DF_REF_INSN (def);
1021 rtx reg = DF_REF_REG (def);
1026 fprintf (dump_file, "Analysing def of ");
1027 print_rtl (dump_file, reg);
1028 fprintf (dump_file, " in insn ");
1029 print_rtl_single (dump_file, insn);
1032 if (DF_REF_IV (def))
1035 fprintf (dump_file, " already analysed.\n");
1036 *iv = *DF_REF_IV (def);
1037 return iv->base != NULL_RTX;
1040 iv->mode = VOIDmode;
1041 iv->base = NULL_RTX;
1042 iv->step = NULL_RTX;
1044 set = single_set (insn);
1045 if (!set || SET_DEST (set) != reg)
1048 rhs = find_reg_equal_equiv_note (insn);
1050 rhs = XEXP (rhs, 0);
1052 rhs = SET_SRC (set);
1054 iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
1055 record_iv (def, iv);
1059 print_rtl (dump_file, reg);
1060 fprintf (dump_file, " in insn ");
1061 print_rtl_single (dump_file, insn);
1062 fprintf (dump_file, " is ");
1063 dump_iv_info (dump_file, iv);
1064 fprintf (dump_file, "\n");
1067 return iv->base != NULL_RTX;
1070 /* Analyzes operand OP of INSN and stores the result to *IV. */
1073 iv_analyze_op (rtx insn, rtx op, struct rtx_iv *iv)
1075 struct df_ref *def = NULL;
1076 enum iv_grd_result res;
1080 fprintf (dump_file, "Analysing operand ");
1081 print_rtl (dump_file, op);
1082 fprintf (dump_file, " of insn ");
1083 print_rtl_single (dump_file, insn);
1086 if (CONSTANT_P (op))
1087 res = GRD_INVARIANT;
1088 else if (GET_CODE (op) == SUBREG)
1090 if (!subreg_lowpart_p (op))
1093 if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
1096 return iv_subreg (iv, GET_MODE (op));
1100 res = iv_get_reaching_def (insn, op, &def);
1101 if (res == GRD_INVALID)
1104 fprintf (dump_file, " not simple.\n");
1109 if (res == GRD_INVARIANT)
1111 iv_constant (iv, op, VOIDmode);
1115 fprintf (dump_file, " ");
1116 dump_iv_info (dump_file, iv);
1117 fprintf (dump_file, "\n");
1122 if (res == GRD_MAYBE_BIV)
1123 return iv_analyze_biv (op, iv);
1125 return iv_analyze_def (def, iv);
1128 /* Analyzes value VAL at INSN and stores the result to *IV. */
1131 iv_analyze (rtx insn, rtx val, struct rtx_iv *iv)
1135 /* We must find the insn in that val is used, so that we get to UD chains.
1136 Since the function is sometimes called on result of get_condition,
1137 this does not necessarily have to be directly INSN; scan also the
1139 if (simple_reg_p (val))
1141 if (GET_CODE (val) == SUBREG)
1142 reg = SUBREG_REG (val);
1146 while (!df_find_use (df, insn, reg))
1147 insn = NEXT_INSN (insn);
1150 return iv_analyze_op (insn, val, iv);
1153 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1156 iv_analyze_result (rtx insn, rtx def, struct rtx_iv *iv)
1158 struct df_ref *adef;
1160 adef = df_find_def (df, insn, def);
1164 return iv_analyze_def (adef, iv);
1167 /* Checks whether definition of register REG in INSN is a basic induction
1168 variable. IV analysis must have been initialized (via a call to
1169 iv_analysis_loop_init) for this function to produce a result. */
1172 biv_p (rtx insn, rtx reg)
1175 struct df_ref *def, *last_def;
1177 if (!simple_reg_p (reg))
1180 def = df_find_def (df, insn, reg);
1181 gcc_assert (def != NULL);
1182 if (!latch_dominating_def (reg, &last_def))
1184 if (last_def != def)
1187 if (!iv_analyze_biv (reg, &iv))
1190 return iv.step != const0_rtx;
1193 /* Calculates value of IV at ITERATION-th iteration. */
1196 get_iv_value (struct rtx_iv *iv, rtx iteration)
1200 /* We would need to generate some if_then_else patterns, and so far
1201 it is not needed anywhere. */
1202 gcc_assert (!iv->first_special);
1204 if (iv->step != const0_rtx && iteration != const0_rtx)
1205 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1206 simplify_gen_binary (MULT, iv->extend_mode,
1207 iv->step, iteration));
1211 if (iv->extend_mode == iv->mode)
1214 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
1216 if (iv->extend == UNKNOWN)
1219 val = simplify_gen_unary (iv->extend, iv->extend_mode, val, iv->mode);
1220 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1221 simplify_gen_binary (MULT, iv->extend_mode,
1227 /* Free the data for an induction variable analysis. */
1230 iv_analysis_done (void)
1242 /* Computes inverse to X modulo (1 << MOD). */
1244 static unsigned HOST_WIDEST_INT
1245 inverse (unsigned HOST_WIDEST_INT x, int mod)
1247 unsigned HOST_WIDEST_INT mask =
1248 ((unsigned HOST_WIDEST_INT) 1 << (mod - 1) << 1) - 1;
1249 unsigned HOST_WIDEST_INT rslt = 1;
1252 for (i = 0; i < mod - 1; i++)
1254 rslt = (rslt * x) & mask;
1261 /* Tries to estimate the maximum number of iterations. */
1263 static unsigned HOST_WIDEST_INT
1264 determine_max_iter (struct niter_desc *desc)
1266 rtx niter = desc->niter_expr;
1267 rtx mmin, mmax, left, right;
1268 unsigned HOST_WIDEST_INT nmax, inc;
1270 if (GET_CODE (niter) == AND
1271 && GET_CODE (XEXP (niter, 0)) == CONST_INT)
1273 nmax = INTVAL (XEXP (niter, 0));
1274 if (!(nmax & (nmax + 1)))
1276 desc->niter_max = nmax;
1281 get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
1282 nmax = INTVAL (mmax) - INTVAL (mmin);
1284 if (GET_CODE (niter) == UDIV)
1286 if (GET_CODE (XEXP (niter, 1)) != CONST_INT)
1288 desc->niter_max = nmax;
1291 inc = INTVAL (XEXP (niter, 1));
1292 niter = XEXP (niter, 0);
1297 if (GET_CODE (niter) == PLUS)
1299 left = XEXP (niter, 0);
1300 right = XEXP (niter, 0);
1302 if (GET_CODE (right) == CONST_INT)
1303 right = GEN_INT (-INTVAL (right));
1305 else if (GET_CODE (niter) == MINUS)
1307 left = XEXP (niter, 0);
1308 right = XEXP (niter, 0);
1316 if (GET_CODE (left) == CONST_INT)
1318 if (GET_CODE (right) == CONST_INT)
1320 nmax = INTVAL (mmax) - INTVAL (mmin);
1322 desc->niter_max = nmax / inc;
1326 /* Checks whether register *REG is in set ALT. Callback for for_each_rtx. */
1329 altered_reg_used (rtx *reg, void *alt)
1334 return REGNO_REG_SET_P (alt, REGNO (*reg));
1337 /* Marks registers altered by EXPR in set ALT. */
1340 mark_altered (rtx expr, rtx by ATTRIBUTE_UNUSED, void *alt)
1342 if (GET_CODE (expr) == SUBREG)
1343 expr = SUBREG_REG (expr);
1347 SET_REGNO_REG_SET (alt, REGNO (expr));
1350 /* Checks whether RHS is simple enough to process. */
1353 simple_rhs_p (rtx rhs)
1357 if (CONSTANT_P (rhs)
1361 switch (GET_CODE (rhs))
1365 op0 = XEXP (rhs, 0);
1366 op1 = XEXP (rhs, 1);
1367 /* Allow reg + const sets only. */
1368 if (REG_P (op0) && CONSTANT_P (op1))
1370 if (REG_P (op1) && CONSTANT_P (op0))
1380 /* Simplifies *EXPR using assignment in INSN. ALTERED is the set of registers
1384 simplify_using_assignment (rtx insn, rtx *expr, regset altered)
1386 rtx set = single_set (insn);
1387 rtx lhs = NULL_RTX, rhs;
1392 lhs = SET_DEST (set);
1394 || altered_reg_used (&lhs, altered))
1400 note_stores (PATTERN (insn), mark_altered, altered);
1405 /* Kill all call clobbered registers. */
1406 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1407 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1408 SET_REGNO_REG_SET (altered, i);
1414 rhs = find_reg_equal_equiv_note (insn);
1416 rhs = XEXP (rhs, 0);
1418 rhs = SET_SRC (set);
1420 if (!simple_rhs_p (rhs))
1423 if (for_each_rtx (&rhs, altered_reg_used, altered))
1426 *expr = simplify_replace_rtx (*expr, lhs, rhs);
1429 /* Checks whether A implies B. */
1432 implies_p (rtx a, rtx b)
1434 rtx op0, op1, opb0, opb1, r;
1435 enum machine_mode mode;
1437 if (GET_CODE (a) == EQ)
1444 r = simplify_replace_rtx (b, op0, op1);
1445 if (r == const_true_rtx)
1451 r = simplify_replace_rtx (b, op1, op0);
1452 if (r == const_true_rtx)
1457 /* A < B implies A + 1 <= B. */
1458 if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
1459 && (GET_CODE (b) == GE || GET_CODE (b) == LE))
1466 if (GET_CODE (a) == GT)
1473 if (GET_CODE (b) == GE)
1480 mode = GET_MODE (op0);
1481 if (mode != GET_MODE (opb0))
1483 else if (mode == VOIDmode)
1485 mode = GET_MODE (op1);
1486 if (mode != GET_MODE (opb1))
1490 if (mode != VOIDmode
1491 && rtx_equal_p (op1, opb1)
1492 && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
1499 /* Canonicalizes COND so that
1501 (1) Ensure that operands are ordered according to
1502 swap_commutative_operands_p.
1503 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1504 for GE, GEU, and LEU. */
1507 canon_condition (rtx cond)
1512 enum machine_mode mode;
1514 code = GET_CODE (cond);
1515 op0 = XEXP (cond, 0);
1516 op1 = XEXP (cond, 1);
1518 if (swap_commutative_operands_p (op0, op1))
1520 code = swap_condition (code);
1526 mode = GET_MODE (op0);
1527 if (mode == VOIDmode)
1528 mode = GET_MODE (op1);
1529 gcc_assert (mode != VOIDmode);
1531 if (GET_CODE (op1) == CONST_INT
1532 && GET_MODE_CLASS (mode) != MODE_CC
1533 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1535 HOST_WIDE_INT const_val = INTVAL (op1);
1536 unsigned HOST_WIDE_INT uconst_val = const_val;
1537 unsigned HOST_WIDE_INT max_val
1538 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode);
1543 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
1544 code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
1547 /* When cross-compiling, const_val might be sign-extended from
1548 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
1550 if ((HOST_WIDE_INT) (const_val & max_val)
1551 != (((HOST_WIDE_INT) 1
1552 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
1553 code = GT, op1 = gen_int_mode (const_val - 1, mode);
1557 if (uconst_val < max_val)
1558 code = LTU, op1 = gen_int_mode (uconst_val + 1, mode);
1562 if (uconst_val != 0)
1563 code = GTU, op1 = gen_int_mode (uconst_val - 1, mode);
1571 if (op0 != XEXP (cond, 0)
1572 || op1 != XEXP (cond, 1)
1573 || code != GET_CODE (cond)
1574 || GET_MODE (cond) != SImode)
1575 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1580 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1581 set of altered regs. */
1584 simplify_using_condition (rtx cond, rtx *expr, regset altered)
1586 rtx rev, reve, exp = *expr;
1588 if (!COMPARISON_P (exp))
1591 /* If some register gets altered later, we do not really speak about its
1592 value at the time of comparison. */
1594 && for_each_rtx (&cond, altered_reg_used, altered))
1597 rev = reversed_condition (cond);
1598 reve = reversed_condition (exp);
1600 cond = canon_condition (cond);
1601 exp = canon_condition (exp);
1603 rev = canon_condition (rev);
1605 reve = canon_condition (reve);
1607 if (rtx_equal_p (exp, cond))
1609 *expr = const_true_rtx;
1614 if (rev && rtx_equal_p (exp, rev))
1620 if (implies_p (cond, exp))
1622 *expr = const_true_rtx;
1626 if (reve && implies_p (cond, reve))
1632 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1634 if (rev && implies_p (exp, rev))
1640 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1641 if (rev && reve && implies_p (reve, rev))
1643 *expr = const_true_rtx;
1647 /* We would like to have some other tests here. TODO. */
1652 /* Use relationship between A and *B to eventually eliminate *B.
1653 OP is the operation we consider. */
1656 eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1661 /* If A implies *B, we may replace *B by true. */
1662 if (implies_p (a, *b))
1663 *b = const_true_rtx;
1667 /* If *B implies A, we may replace *B by false. */
1668 if (implies_p (*b, a))
1677 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1678 operation we consider. */
1681 eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1685 for (elt = tail; elt; elt = XEXP (elt, 1))
1686 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1687 for (elt = tail; elt; elt = XEXP (elt, 1))
1688 eliminate_implied_condition (op, XEXP (elt, 0), head);
1691 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1692 is a list, its elements are assumed to be combined using OP. */
1695 simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1697 rtx head, tail, insn;
1705 if (CONSTANT_P (*expr))
1708 if (GET_CODE (*expr) == EXPR_LIST)
1710 head = XEXP (*expr, 0);
1711 tail = XEXP (*expr, 1);
1713 eliminate_implied_conditions (op, &head, tail);
1718 neutral = const_true_rtx;
1723 neutral = const0_rtx;
1724 aggr = const_true_rtx;
1731 simplify_using_initial_values (loop, UNKNOWN, &head);
1734 XEXP (*expr, 0) = aggr;
1735 XEXP (*expr, 1) = NULL_RTX;
1738 else if (head == neutral)
1741 simplify_using_initial_values (loop, op, expr);
1744 simplify_using_initial_values (loop, op, &tail);
1746 if (tail && XEXP (tail, 0) == aggr)
1752 XEXP (*expr, 0) = head;
1753 XEXP (*expr, 1) = tail;
1757 gcc_assert (op == UNKNOWN);
1759 e = loop_preheader_edge (loop);
1760 if (e->src == ENTRY_BLOCK_PTR)
1763 altered = ALLOC_REG_SET (®_obstack);
1767 insn = BB_END (e->src);
1768 if (any_condjump_p (insn))
1770 rtx cond = get_condition (BB_END (e->src), NULL, false, true);
1772 if (cond && (e->flags & EDGE_FALLTHRU))
1773 cond = reversed_condition (cond);
1776 simplify_using_condition (cond, expr, altered);
1777 if (CONSTANT_P (*expr))
1779 FREE_REG_SET (altered);
1785 FOR_BB_INSNS_REVERSE (e->src, insn)
1790 simplify_using_assignment (insn, expr, altered);
1791 if (CONSTANT_P (*expr))
1793 FREE_REG_SET (altered);
1798 if (!single_pred_p (e->src)
1799 || single_pred (e->src) == ENTRY_BLOCK_PTR)
1801 e = single_pred_edge (e->src);
1804 FREE_REG_SET (altered);
1807 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
1808 that IV occurs as left operands of comparison COND and its signedness
1809 is SIGNED_P to DESC. */
1812 shorten_into_mode (struct rtx_iv *iv, enum machine_mode mode,
1813 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
1815 rtx mmin, mmax, cond_over, cond_under;
1817 get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
1818 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
1820 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
1829 if (cond_under != const0_rtx)
1831 alloc_EXPR_LIST (0, cond_under, desc->infinite);
1832 if (cond_over != const0_rtx)
1833 desc->noloop_assumptions =
1834 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
1841 if (cond_over != const0_rtx)
1843 alloc_EXPR_LIST (0, cond_over, desc->infinite);
1844 if (cond_under != const0_rtx)
1845 desc->noloop_assumptions =
1846 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
1850 if (cond_over != const0_rtx)
1852 alloc_EXPR_LIST (0, cond_over, desc->infinite);
1853 if (cond_under != const0_rtx)
1855 alloc_EXPR_LIST (0, cond_under, desc->infinite);
1863 iv->extend = signed_p ? SIGN_EXTEND : ZERO_EXTEND;
1866 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
1867 subregs of the same mode if possible (sometimes it is necessary to add
1868 some assumptions to DESC). */
1871 canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
1872 enum rtx_code cond, struct niter_desc *desc)
1874 enum machine_mode comp_mode;
1877 /* If the ivs behave specially in the first iteration, or are
1878 added/multiplied after extending, we ignore them. */
1879 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
1881 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
1884 /* If there is some extend, it must match signedness of the comparison. */
1889 if (iv0->extend == ZERO_EXTEND
1890 || iv1->extend == ZERO_EXTEND)
1897 if (iv0->extend == SIGN_EXTEND
1898 || iv1->extend == SIGN_EXTEND)
1904 if (iv0->extend != UNKNOWN
1905 && iv1->extend != UNKNOWN
1906 && iv0->extend != iv1->extend)
1910 if (iv0->extend != UNKNOWN)
1911 signed_p = iv0->extend == SIGN_EXTEND;
1912 if (iv1->extend != UNKNOWN)
1913 signed_p = iv1->extend == SIGN_EXTEND;
1920 /* Values of both variables should be computed in the same mode. These
1921 might indeed be different, if we have comparison like
1923 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
1925 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
1926 in different modes. This does not seem impossible to handle, but
1927 it hardly ever occurs in practice.
1929 The only exception is the case when one of operands is invariant.
1930 For example pentium 3 generates comparisons like
1931 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
1932 definitely do not want this prevent the optimization. */
1933 comp_mode = iv0->extend_mode;
1934 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
1935 comp_mode = iv1->extend_mode;
1937 if (iv0->extend_mode != comp_mode)
1939 if (iv0->mode != iv0->extend_mode
1940 || iv0->step != const0_rtx)
1943 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
1944 comp_mode, iv0->base, iv0->mode);
1945 iv0->extend_mode = comp_mode;
1948 if (iv1->extend_mode != comp_mode)
1950 if (iv1->mode != iv1->extend_mode
1951 || iv1->step != const0_rtx)
1954 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
1955 comp_mode, iv1->base, iv1->mode);
1956 iv1->extend_mode = comp_mode;
1959 /* Check that both ivs belong to a range of a single mode. If one of the
1960 operands is an invariant, we may need to shorten it into the common
1962 if (iv0->mode == iv0->extend_mode
1963 && iv0->step == const0_rtx
1964 && iv0->mode != iv1->mode)
1965 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
1967 if (iv1->mode == iv1->extend_mode
1968 && iv1->step == const0_rtx
1969 && iv0->mode != iv1->mode)
1970 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
1972 if (iv0->mode != iv1->mode)
1975 desc->mode = iv0->mode;
1976 desc->signed_p = signed_p;
1981 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
1982 the result into DESC. Very similar to determine_number_of_iterations
1983 (basically its rtl version), complicated by things like subregs. */
1986 iv_number_of_iterations (struct loop *loop, rtx insn, rtx condition,
1987 struct niter_desc *desc)
1989 rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
1990 struct rtx_iv iv0, iv1, tmp_iv;
1991 rtx assumption, may_not_xform;
1993 enum machine_mode mode, comp_mode;
1994 rtx mmin, mmax, mode_mmin, mode_mmax;
1995 unsigned HOST_WIDEST_INT s, size, d, inv;
1996 HOST_WIDEST_INT up, down, inc, step_val;
1997 int was_sharp = false;
2001 /* The meaning of these assumptions is this:
2003 then the rest of information does not have to be valid
2004 if noloop_assumptions then the loop does not roll
2005 if infinite then this exit is never used */
2007 desc->assumptions = NULL_RTX;
2008 desc->noloop_assumptions = NULL_RTX;
2009 desc->infinite = NULL_RTX;
2010 desc->simple_p = true;
2012 desc->const_iter = false;
2013 desc->niter_expr = NULL_RTX;
2014 desc->niter_max = 0;
2016 cond = GET_CODE (condition);
2017 gcc_assert (COMPARISON_P (condition));
2019 mode = GET_MODE (XEXP (condition, 0));
2020 if (mode == VOIDmode)
2021 mode = GET_MODE (XEXP (condition, 1));
2022 /* The constant comparisons should be folded. */
2023 gcc_assert (mode != VOIDmode);
2025 /* We only handle integers or pointers. */
2026 if (GET_MODE_CLASS (mode) != MODE_INT
2027 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
2030 op0 = XEXP (condition, 0);
2031 if (!iv_analyze (insn, op0, &iv0))
2033 if (iv0.extend_mode == VOIDmode)
2034 iv0.mode = iv0.extend_mode = mode;
2036 op1 = XEXP (condition, 1);
2037 if (!iv_analyze (insn, op1, &iv1))
2039 if (iv1.extend_mode == VOIDmode)
2040 iv1.mode = iv1.extend_mode = mode;
2042 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
2043 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
2046 /* Check condition and normalize it. */
2054 tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
2055 cond = swap_condition (cond);
2067 /* Handle extends. This is relatively nontrivial, so we only try in some
2068 easy cases, when we can canonicalize the ivs (possibly by adding some
2069 assumptions) to shape subreg (base + i * step). This function also fills
2070 in desc->mode and desc->signed_p. */
2072 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
2075 comp_mode = iv0.extend_mode;
2077 size = GET_MODE_BITSIZE (mode);
2078 get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
2079 mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
2080 mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
2082 if (GET_CODE (iv0.step) != CONST_INT || GET_CODE (iv1.step) != CONST_INT)
2085 /* We can take care of the case of two induction variables chasing each other
2086 if the test is NE. I have never seen a loop using it, but still it is
2088 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
2093 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2094 iv1.step = const0_rtx;
2097 /* This is either infinite loop or the one that ends immediately, depending
2098 on initial values. Unswitching should remove this kind of conditions. */
2099 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
2104 if (iv0.step == const0_rtx)
2105 step_val = -INTVAL (iv1.step);
2107 step_val = INTVAL (iv0.step);
2109 /* Ignore loops of while (i-- < 10) type. */
2113 step_is_pow2 = !(step_val & (step_val - 1));
2117 /* We do not care about whether the step is power of two in this
2119 step_is_pow2 = false;
2123 /* Some more condition normalization. We must record some assumptions
2124 due to overflows. */
2129 /* We want to take care only of non-sharp relationals; this is easy,
2130 as in cases the overflow would make the transformation unsafe
2131 the loop does not roll. Seemingly it would make more sense to want
2132 to take care of sharp relationals instead, as NE is more similar to
2133 them, but the problem is that here the transformation would be more
2134 difficult due to possibly infinite loops. */
2135 if (iv0.step == const0_rtx)
2137 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2138 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2140 if (assumption == const_true_rtx)
2141 goto zero_iter_simplify;
2142 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2143 iv0.base, const1_rtx);
2147 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2148 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2150 if (assumption == const_true_rtx)
2151 goto zero_iter_simplify;
2152 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2153 iv1.base, constm1_rtx);
2156 if (assumption != const0_rtx)
2157 desc->noloop_assumptions =
2158 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2159 cond = (cond == LT) ? LE : LEU;
2161 /* It will be useful to be able to tell the difference once more in
2162 LE -> NE reduction. */
2168 /* Take care of trivially infinite loops. */
2171 if (iv0.step == const0_rtx)
2173 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2174 if (rtx_equal_p (tmp, mode_mmin))
2177 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2178 /* Fill in the remaining fields somehow. */
2179 goto zero_iter_simplify;
2184 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2185 if (rtx_equal_p (tmp, mode_mmax))
2188 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2189 /* Fill in the remaining fields somehow. */
2190 goto zero_iter_simplify;
2195 /* If we can we want to take care of NE conditions instead of size
2196 comparisons, as they are much more friendly (most importantly
2197 this takes care of special handling of loops with step 1). We can
2198 do it if we first check that upper bound is greater or equal to
2199 lower bound, their difference is constant c modulo step and that
2200 there is not an overflow. */
2203 if (iv0.step == const0_rtx)
2204 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2207 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2208 delta = lowpart_subreg (mode, delta, comp_mode);
2209 delta = simplify_gen_binary (UMOD, mode, delta, step);
2210 may_xform = const0_rtx;
2211 may_not_xform = const_true_rtx;
2213 if (GET_CODE (delta) == CONST_INT)
2215 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2217 /* A special case. We have transformed condition of type
2218 for (i = 0; i < 4; i += 4)
2220 for (i = 0; i <= 3; i += 4)
2221 obviously if the test for overflow during that transformation
2222 passed, we cannot overflow here. Most importantly any
2223 loop with sharp end condition and step 1 falls into this
2224 category, so handling this case specially is definitely
2225 worth the troubles. */
2226 may_xform = const_true_rtx;
2228 else if (iv0.step == const0_rtx)
2230 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2231 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
2232 bound = lowpart_subreg (mode, bound, comp_mode);
2233 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2234 may_xform = simplify_gen_relational (cond, SImode, mode,
2236 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2242 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2243 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
2244 bound = lowpart_subreg (mode, bound, comp_mode);
2245 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2246 may_xform = simplify_gen_relational (cond, SImode, mode,
2248 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2254 if (may_xform != const0_rtx)
2256 /* We perform the transformation always provided that it is not
2257 completely senseless. This is OK, as we would need this assumption
2258 to determine the number of iterations anyway. */
2259 if (may_xform != const_true_rtx)
2261 /* If the step is a power of two and the final value we have
2262 computed overflows, the cycle is infinite. Otherwise it
2263 is nontrivial to compute the number of iterations. */
2265 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
2268 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2272 /* We are going to lose some information about upper bound on
2273 number of iterations in this step, so record the information
2275 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
2276 if (GET_CODE (iv1.base) == CONST_INT)
2277 up = INTVAL (iv1.base);
2279 up = INTVAL (mode_mmax) - inc;
2280 down = INTVAL (GET_CODE (iv0.base) == CONST_INT
2283 desc->niter_max = (up - down) / inc + 1;
2285 if (iv0.step == const0_rtx)
2287 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2288 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2292 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2293 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2296 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2297 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2298 assumption = simplify_gen_relational (reverse_condition (cond),
2299 SImode, mode, tmp0, tmp1);
2300 if (assumption == const_true_rtx)
2301 goto zero_iter_simplify;
2302 else if (assumption != const0_rtx)
2303 desc->noloop_assumptions =
2304 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2309 /* Count the number of iterations. */
2312 /* Everything we do here is just arithmetics modulo size of mode. This
2313 makes us able to do more involved computations of number of iterations
2314 than in other cases. First transform the condition into shape
2315 s * i <> c, with s positive. */
2316 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2317 iv0.base = const0_rtx;
2318 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2319 iv1.step = const0_rtx;
2320 if (INTVAL (iv0.step) < 0)
2322 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, mode);
2323 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, mode);
2325 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2327 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2328 is infinite. Otherwise, the number of iterations is
2329 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2330 s = INTVAL (iv0.step); d = 1;
2337 bound = GEN_INT (((unsigned HOST_WIDEST_INT) 1 << (size - 1 ) << 1) - 1);
2339 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2340 tmp = simplify_gen_binary (UMOD, mode, tmp1, GEN_INT (d));
2341 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2342 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2344 tmp = simplify_gen_binary (UDIV, mode, tmp1, GEN_INT (d));
2345 inv = inverse (s, size);
2346 tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
2347 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2351 if (iv1.step == const0_rtx)
2352 /* Condition in shape a + s * i <= b
2353 We must know that b + s does not overflow and a <= b + s and then we
2354 can compute number of iterations as (b + s - a) / s. (It might
2355 seem that we in fact could be more clever about testing the b + s
2356 overflow condition using some information about b - a mod s,
2357 but it was already taken into account during LE -> NE transform). */
2360 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2361 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2363 bound = simplify_gen_binary (MINUS, mode, mode_mmax,
2364 lowpart_subreg (mode, step,
2370 /* If s is power of 2, we know that the loop is infinite if
2371 a % s <= b % s and b + s overflows. */
2372 assumption = simplify_gen_relational (reverse_condition (cond),
2376 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2377 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2378 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2379 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2381 alloc_EXPR_LIST (0, assumption, desc->infinite);
2385 assumption = simplify_gen_relational (cond, SImode, mode,
2388 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2391 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
2392 tmp = lowpart_subreg (mode, tmp, comp_mode);
2393 assumption = simplify_gen_relational (reverse_condition (cond),
2394 SImode, mode, tmp0, tmp);
2396 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2397 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2401 /* Condition in shape a <= b - s * i
2402 We must know that a - s does not overflow and a - s <= b and then
2403 we can again compute number of iterations as (b - (a - s)) / s. */
2404 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
2405 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2406 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2408 bound = simplify_gen_binary (PLUS, mode, mode_mmin,
2409 lowpart_subreg (mode, step, comp_mode));
2414 /* If s is power of 2, we know that the loop is infinite if
2415 a % s <= b % s and a - s overflows. */
2416 assumption = simplify_gen_relational (reverse_condition (cond),
2420 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2421 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2422 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2423 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2425 alloc_EXPR_LIST (0, assumption, desc->infinite);
2429 assumption = simplify_gen_relational (cond, SImode, mode,
2432 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2435 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
2436 tmp = lowpart_subreg (mode, tmp, comp_mode);
2437 assumption = simplify_gen_relational (reverse_condition (cond),
2440 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2441 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2443 if (assumption == const_true_rtx)
2444 goto zero_iter_simplify;
2445 else if (assumption != const0_rtx)
2446 desc->noloop_assumptions =
2447 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2448 delta = simplify_gen_binary (UDIV, mode, delta, step);
2449 desc->niter_expr = delta;
2452 old_niter = desc->niter_expr;
2454 simplify_using_initial_values (loop, AND, &desc->assumptions);
2455 if (desc->assumptions
2456 && XEXP (desc->assumptions, 0) == const0_rtx)
2458 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2459 simplify_using_initial_values (loop, IOR, &desc->infinite);
2460 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2462 /* Rerun the simplification. Consider code (created by copying loop headers)
2474 The first pass determines that i = 0, the second pass uses it to eliminate
2475 noloop assumption. */
2477 simplify_using_initial_values (loop, AND, &desc->assumptions);
2478 if (desc->assumptions
2479 && XEXP (desc->assumptions, 0) == const0_rtx)
2481 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2482 simplify_using_initial_values (loop, IOR, &desc->infinite);
2483 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2485 if (desc->noloop_assumptions
2486 && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
2489 if (GET_CODE (desc->niter_expr) == CONST_INT)
2491 unsigned HOST_WIDEST_INT val = INTVAL (desc->niter_expr);
2493 desc->const_iter = true;
2494 desc->niter_max = desc->niter = val & GET_MODE_MASK (desc->mode);
2498 if (!desc->niter_max)
2499 desc->niter_max = determine_max_iter (desc);
2501 /* simplify_using_initial_values does a copy propagation on the registers
2502 in the expression for the number of iterations. This prolongs life
2503 ranges of registers and increases register pressure, and usually
2504 brings no gain (and if it happens to do, the cse pass will take care
2505 of it anyway). So prevent this behavior, unless it enabled us to
2506 derive that the number of iterations is a constant. */
2507 desc->niter_expr = old_niter;
2513 /* Simplify the assumptions. */
2514 simplify_using_initial_values (loop, AND, &desc->assumptions);
2515 if (desc->assumptions
2516 && XEXP (desc->assumptions, 0) == const0_rtx)
2518 simplify_using_initial_values (loop, IOR, &desc->infinite);
2522 desc->const_iter = true;
2524 desc->niter_max = 0;
2525 desc->noloop_assumptions = NULL_RTX;
2526 desc->niter_expr = const0_rtx;
2530 desc->simple_p = false;
2534 /* Checks whether E is a simple exit from LOOP and stores its description
2538 check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2540 basic_block exit_bb;
2545 desc->simple_p = false;
2547 /* It must belong directly to the loop. */
2548 if (exit_bb->loop_father != loop)
2551 /* It must be tested (at least) once during any iteration. */
2552 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
2555 /* It must end in a simple conditional jump. */
2556 if (!any_condjump_p (BB_END (exit_bb)))
2559 ein = EDGE_SUCC (exit_bb, 0);
2561 ein = EDGE_SUCC (exit_bb, 1);
2564 desc->in_edge = ein;
2566 /* Test whether the condition is suitable. */
2567 if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
2570 if (ein->flags & EDGE_FALLTHRU)
2572 condition = reversed_condition (condition);
2577 /* Check that we are able to determine number of iterations and fill
2578 in information about it. */
2579 iv_number_of_iterations (loop, at, condition, desc);
2582 /* Finds a simple exit of LOOP and stores its description into DESC. */
2585 find_simple_exit (struct loop *loop, struct niter_desc *desc)
2590 struct niter_desc act;
2594 desc->simple_p = false;
2595 body = get_loop_body (loop);
2597 for (i = 0; i < loop->num_nodes; i++)
2599 FOR_EACH_EDGE (e, ei, body[i]->succs)
2601 if (flow_bb_inside_loop_p (loop, e->dest))
2604 check_simple_exit (loop, e, &act);
2612 /* Prefer constant iterations; the less the better. */
2614 || (desc->const_iter && act.niter >= desc->niter))
2617 /* Also if the actual exit may be infinite, while the old one
2618 not, prefer the old one. */
2619 if (act.infinite && !desc->infinite)
2631 fprintf (dump_file, "Loop %d is simple:\n", loop->num);
2632 fprintf (dump_file, " simple exit %d -> %d\n",
2633 desc->out_edge->src->index,
2634 desc->out_edge->dest->index);
2635 if (desc->assumptions)
2637 fprintf (dump_file, " assumptions: ");
2638 print_rtl (dump_file, desc->assumptions);
2639 fprintf (dump_file, "\n");
2641 if (desc->noloop_assumptions)
2643 fprintf (dump_file, " does not roll if: ");
2644 print_rtl (dump_file, desc->noloop_assumptions);
2645 fprintf (dump_file, "\n");
2649 fprintf (dump_file, " infinite if: ");
2650 print_rtl (dump_file, desc->infinite);
2651 fprintf (dump_file, "\n");
2654 fprintf (dump_file, " number of iterations: ");
2655 print_rtl (dump_file, desc->niter_expr);
2656 fprintf (dump_file, "\n");
2658 fprintf (dump_file, " upper bound: ");
2659 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC, desc->niter_max);
2660 fprintf (dump_file, "\n");
2663 fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
2669 /* Creates a simple loop description of LOOP if it was not computed
2673 get_simple_loop_desc (struct loop *loop)
2675 struct niter_desc *desc = simple_loop_desc (loop);
2680 desc = xmalloc (sizeof (struct niter_desc));
2681 iv_analysis_loop_init (loop);
2682 find_simple_exit (loop, desc);
2685 if (desc->simple_p && (desc->assumptions || desc->infinite))
2687 const char *wording;
2689 /* Assume that no overflow happens and that the loop is finite.
2690 We already warned at the tree level if we ran optimizations there. */
2691 if (!flag_tree_loop_optimize && warn_unsafe_loop_optimizations)
2696 flag_unsafe_loop_optimizations
2697 ? N_("assuming that the loop is not infinite")
2698 : N_("cannot optimize possibly infinite loops");
2699 warning (OPT_Wunsafe_loop_optimizations, "%s",
2702 if (desc->assumptions)
2705 flag_unsafe_loop_optimizations
2706 ? N_("assuming that the loop counter does not overflow")
2707 : N_("cannot optimize loop, the loop counter may overflow");
2708 warning (OPT_Wunsafe_loop_optimizations, "%s",
2713 if (flag_unsafe_loop_optimizations)
2715 desc->assumptions = NULL_RTX;
2716 desc->infinite = NULL_RTX;
2723 /* Releases simple loop description for LOOP. */
2726 free_simple_loop_desc (struct loop *loop)
2728 struct niter_desc *desc = simple_loop_desc (loop);