2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / loop-iv.c
blobfdd3323a76fa1edabc13479b0fa11f812239a1f7
1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
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
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This is a simple analysis of induction variables of the loop. The major use
21 is for determining the number of iterations of a loop for loop unrolling,
22 doloop optimization and branch prediction. The iv information is computed
23 on demand.
25 Induction variables are analyzed by walking the use-def chains. When
26 a basic induction variable (biv) is found, it is cached in the bivs
27 hash table. When register is proved to be a biv, its description
28 is stored to DF_REF_DATA of the def reference.
30 The analysis works always with one loop -- you must call
31 iv_analysis_loop_init (loop) for it. All the other functions then work with
32 this loop. When you need to work with another loop, just call
33 iv_analysis_loop_init for it. When you no longer need iv analysis, call
34 iv_analysis_done () to clean up the memory.
36 The available functions are:
38 iv_analyze (insn, reg, iv): Stores the description of the induction variable
39 corresponding to the use of register REG in INSN to IV. Returns true if
40 REG is an induction variable in INSN. false otherwise.
41 If use of REG is not found in INSN, following insns are scanned (so that
42 we may call this function on insn returned by get_condition).
43 iv_analyze_result (insn, def, iv): Stores to IV the description of the iv
44 corresponding to DEF, which is a register defined in INSN.
45 iv_analyze_expr (insn, rhs, mode, iv): Stores to IV the description of iv
46 corresponding to expression EXPR evaluated at INSN. All registers used bu
47 EXPR must also be used in INSN.
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "rtl.h"
55 #include "hard-reg-set.h"
56 #include "obstack.h"
57 #include "predict.h"
58 #include "input.h"
59 #include "function.h"
60 #include "dominance.h"
61 #include "cfg.h"
62 #include "basic-block.h"
63 #include "cfgloop.h"
64 #include "symtab.h"
65 #include "flags.h"
66 #include "alias.h"
67 #include "tree.h"
68 #include "insn-config.h"
69 #include "expmed.h"
70 #include "dojump.h"
71 #include "explow.h"
72 #include "calls.h"
73 #include "emit-rtl.h"
74 #include "varasm.h"
75 #include "stmt.h"
76 #include "expr.h"
77 #include "intl.h"
78 #include "diagnostic-core.h"
79 #include "df.h"
80 #include "dumpfile.h"
81 #include "rtl-iter.h"
83 /* Possible return values of iv_get_reaching_def. */
85 enum iv_grd_result
87 /* More than one reaching def, or reaching def that does not
88 dominate the use. */
89 GRD_INVALID,
91 /* The use is trivial invariant of the loop, i.e. is not changed
92 inside the loop. */
93 GRD_INVARIANT,
95 /* The use is reached by initial value and a value from the
96 previous iteration. */
97 GRD_MAYBE_BIV,
99 /* The use has single dominating def. */
100 GRD_SINGLE_DOM
103 /* Information about a biv. */
105 struct biv_entry
107 unsigned regno; /* The register of the biv. */
108 struct rtx_iv iv; /* Value of the biv. */
111 static bool clean_slate = true;
113 static unsigned int iv_ref_table_size = 0;
115 /* Table of rtx_ivs indexed by the df_ref uid field. */
116 static struct rtx_iv ** iv_ref_table;
118 /* Induction variable stored at the reference. */
119 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
120 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
122 /* The current loop. */
124 static struct loop *current_loop;
126 /* Hashtable helper. */
128 struct biv_entry_hasher : typed_free_remove <biv_entry>
130 typedef biv_entry *value_type;
131 typedef rtx_def *compare_type;
132 static inline hashval_t hash (const biv_entry *);
133 static inline bool equal (const biv_entry *, const rtx_def *);
136 /* Returns hash value for biv B. */
138 inline hashval_t
139 biv_entry_hasher::hash (const biv_entry *b)
141 return b->regno;
144 /* Compares biv B and register R. */
146 inline bool
147 biv_entry_hasher::equal (const biv_entry *b, const rtx_def *r)
149 return b->regno == REGNO (r);
152 /* Bivs of the current loop. */
154 static hash_table<biv_entry_hasher> *bivs;
156 static bool iv_analyze_op (rtx_insn *, rtx, struct rtx_iv *);
158 /* Return the RTX code corresponding to the IV extend code EXTEND. */
159 static inline enum rtx_code
160 iv_extend_to_rtx_code (enum iv_extend_code extend)
162 switch (extend)
164 case IV_SIGN_EXTEND:
165 return SIGN_EXTEND;
166 case IV_ZERO_EXTEND:
167 return ZERO_EXTEND;
168 case IV_UNKNOWN_EXTEND:
169 return UNKNOWN;
171 gcc_unreachable ();
174 /* Dumps information about IV to FILE. */
176 extern void dump_iv_info (FILE *, struct rtx_iv *);
177 void
178 dump_iv_info (FILE *file, struct rtx_iv *iv)
180 if (!iv->base)
182 fprintf (file, "not simple");
183 return;
186 if (iv->step == const0_rtx
187 && !iv->first_special)
188 fprintf (file, "invariant ");
190 print_rtl (file, iv->base);
191 if (iv->step != const0_rtx)
193 fprintf (file, " + ");
194 print_rtl (file, iv->step);
195 fprintf (file, " * iteration");
197 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
199 if (iv->mode != iv->extend_mode)
200 fprintf (file, " %s to %s",
201 rtx_name[iv_extend_to_rtx_code (iv->extend)],
202 GET_MODE_NAME (iv->extend_mode));
204 if (iv->mult != const1_rtx)
206 fprintf (file, " * ");
207 print_rtl (file, iv->mult);
209 if (iv->delta != const0_rtx)
211 fprintf (file, " + ");
212 print_rtl (file, iv->delta);
214 if (iv->first_special)
215 fprintf (file, " (first special)");
218 /* Generates a subreg to get the least significant part of EXPR (in mode
219 INNER_MODE) to OUTER_MODE. */
222 lowpart_subreg (machine_mode outer_mode, rtx expr,
223 machine_mode inner_mode)
225 return simplify_gen_subreg (outer_mode, expr, inner_mode,
226 subreg_lowpart_offset (outer_mode, inner_mode));
229 static void
230 check_iv_ref_table_size (void)
232 if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
234 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
235 iv_ref_table = XRESIZEVEC (struct rtx_iv *, iv_ref_table, new_size);
236 memset (&iv_ref_table[iv_ref_table_size], 0,
237 (new_size - iv_ref_table_size) * sizeof (struct rtx_iv *));
238 iv_ref_table_size = new_size;
243 /* Checks whether REG is a well-behaved register. */
245 static bool
246 simple_reg_p (rtx reg)
248 unsigned r;
250 if (GET_CODE (reg) == SUBREG)
252 if (!subreg_lowpart_p (reg))
253 return false;
254 reg = SUBREG_REG (reg);
257 if (!REG_P (reg))
258 return false;
260 r = REGNO (reg);
261 if (HARD_REGISTER_NUM_P (r))
262 return false;
264 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
265 return false;
267 return true;
270 /* Clears the information about ivs stored in df. */
272 static void
273 clear_iv_info (void)
275 unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
276 struct rtx_iv *iv;
278 check_iv_ref_table_size ();
279 for (i = 0; i < n_defs; i++)
281 iv = iv_ref_table[i];
282 if (iv)
284 free (iv);
285 iv_ref_table[i] = NULL;
289 bivs->empty ();
293 /* Prepare the data for an induction variable analysis of a LOOP. */
295 void
296 iv_analysis_loop_init (struct loop *loop)
298 current_loop = loop;
300 /* Clear the information from the analysis of the previous loop. */
301 if (clean_slate)
303 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
304 bivs = new hash_table<biv_entry_hasher> (10);
305 clean_slate = false;
307 else
308 clear_iv_info ();
310 /* Get rid of the ud chains before processing the rescans. Then add
311 the problem back. */
312 df_remove_problem (df_chain);
313 df_process_deferred_rescans ();
314 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
315 df_chain_add_problem (DF_UD_CHAIN);
316 df_note_add_problem ();
317 df_analyze_loop (loop);
318 if (dump_file)
319 df_dump_region (dump_file);
321 check_iv_ref_table_size ();
324 /* Finds the definition of REG that dominates loop latch and stores
325 it to DEF. Returns false if there is not a single definition
326 dominating the latch. If REG has no definition in loop, DEF
327 is set to NULL and true is returned. */
329 static bool
330 latch_dominating_def (rtx reg, df_ref *def)
332 df_ref single_rd = NULL, adef;
333 unsigned regno = REGNO (reg);
334 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
336 for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
338 if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
339 || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
340 continue;
342 /* More than one reaching definition. */
343 if (single_rd)
344 return false;
346 if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
347 return false;
349 single_rd = adef;
352 *def = single_rd;
353 return true;
356 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
358 static enum iv_grd_result
359 iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
361 df_ref use, adef;
362 basic_block def_bb, use_bb;
363 rtx_insn *def_insn;
364 bool dom_p;
366 *def = NULL;
367 if (!simple_reg_p (reg))
368 return GRD_INVALID;
369 if (GET_CODE (reg) == SUBREG)
370 reg = SUBREG_REG (reg);
371 gcc_assert (REG_P (reg));
373 use = df_find_use (insn, reg);
374 gcc_assert (use != NULL);
376 if (!DF_REF_CHAIN (use))
377 return GRD_INVARIANT;
379 /* More than one reaching def. */
380 if (DF_REF_CHAIN (use)->next)
381 return GRD_INVALID;
383 adef = DF_REF_CHAIN (use)->ref;
385 /* We do not handle setting only part of the register. */
386 if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
387 return GRD_INVALID;
389 def_insn = DF_REF_INSN (adef);
390 def_bb = DF_REF_BB (adef);
391 use_bb = BLOCK_FOR_INSN (insn);
393 if (use_bb == def_bb)
394 dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
395 else
396 dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
398 if (dom_p)
400 *def = adef;
401 return GRD_SINGLE_DOM;
404 /* The definition does not dominate the use. This is still OK if
405 this may be a use of a biv, i.e. if the def_bb dominates loop
406 latch. */
407 if (just_once_each_iteration_p (current_loop, def_bb))
408 return GRD_MAYBE_BIV;
410 return GRD_INVALID;
413 /* Sets IV to invariant CST in MODE. Always returns true (just for
414 consistency with other iv manipulation functions that may fail). */
416 static bool
417 iv_constant (struct rtx_iv *iv, rtx cst, machine_mode mode)
419 if (mode == VOIDmode)
420 mode = GET_MODE (cst);
422 iv->mode = mode;
423 iv->base = cst;
424 iv->step = const0_rtx;
425 iv->first_special = false;
426 iv->extend = IV_UNKNOWN_EXTEND;
427 iv->extend_mode = iv->mode;
428 iv->delta = const0_rtx;
429 iv->mult = const1_rtx;
431 return true;
434 /* Evaluates application of subreg to MODE on IV. */
436 static bool
437 iv_subreg (struct rtx_iv *iv, machine_mode mode)
439 /* If iv is invariant, just calculate the new value. */
440 if (iv->step == const0_rtx
441 && !iv->first_special)
443 rtx val = get_iv_value (iv, const0_rtx);
444 val = lowpart_subreg (mode, val,
445 iv->extend == IV_UNKNOWN_EXTEND
446 ? iv->mode : iv->extend_mode);
448 iv->base = val;
449 iv->extend = IV_UNKNOWN_EXTEND;
450 iv->mode = iv->extend_mode = mode;
451 iv->delta = const0_rtx;
452 iv->mult = const1_rtx;
453 return true;
456 if (iv->extend_mode == mode)
457 return true;
459 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
460 return false;
462 iv->extend = IV_UNKNOWN_EXTEND;
463 iv->mode = mode;
465 iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
466 simplify_gen_binary (MULT, iv->extend_mode,
467 iv->base, iv->mult));
468 iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
469 iv->mult = const1_rtx;
470 iv->delta = const0_rtx;
471 iv->first_special = false;
473 return true;
476 /* Evaluates application of EXTEND to MODE on IV. */
478 static bool
479 iv_extend (struct rtx_iv *iv, enum iv_extend_code extend, machine_mode mode)
481 /* If iv is invariant, just calculate the new value. */
482 if (iv->step == const0_rtx
483 && !iv->first_special)
485 rtx val = get_iv_value (iv, const0_rtx);
486 if (iv->extend_mode != iv->mode
487 && iv->extend != IV_UNKNOWN_EXTEND
488 && iv->extend != extend)
489 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
490 val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
491 val,
492 iv->extend == extend
493 ? iv->extend_mode : iv->mode);
494 iv->base = val;
495 iv->extend = IV_UNKNOWN_EXTEND;
496 iv->mode = iv->extend_mode = mode;
497 iv->delta = const0_rtx;
498 iv->mult = const1_rtx;
499 return true;
502 if (mode != iv->extend_mode)
503 return false;
505 if (iv->extend != IV_UNKNOWN_EXTEND
506 && iv->extend != extend)
507 return false;
509 iv->extend = extend;
511 return true;
514 /* Evaluates negation of IV. */
516 static bool
517 iv_neg (struct rtx_iv *iv)
519 if (iv->extend == IV_UNKNOWN_EXTEND)
521 iv->base = simplify_gen_unary (NEG, iv->extend_mode,
522 iv->base, iv->extend_mode);
523 iv->step = simplify_gen_unary (NEG, iv->extend_mode,
524 iv->step, iv->extend_mode);
526 else
528 iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
529 iv->delta, iv->extend_mode);
530 iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
531 iv->mult, iv->extend_mode);
534 return true;
537 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
539 static bool
540 iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
542 machine_mode mode;
543 rtx arg;
545 /* Extend the constant to extend_mode of the other operand if necessary. */
546 if (iv0->extend == IV_UNKNOWN_EXTEND
547 && iv0->mode == iv0->extend_mode
548 && iv0->step == const0_rtx
549 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
551 iv0->extend_mode = iv1->extend_mode;
552 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
553 iv0->base, iv0->mode);
555 if (iv1->extend == IV_UNKNOWN_EXTEND
556 && iv1->mode == iv1->extend_mode
557 && iv1->step == const0_rtx
558 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
560 iv1->extend_mode = iv0->extend_mode;
561 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
562 iv1->base, iv1->mode);
565 mode = iv0->extend_mode;
566 if (mode != iv1->extend_mode)
567 return false;
569 if (iv0->extend == IV_UNKNOWN_EXTEND
570 && iv1->extend == IV_UNKNOWN_EXTEND)
572 if (iv0->mode != iv1->mode)
573 return false;
575 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
576 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
578 return true;
581 /* Handle addition of constant. */
582 if (iv1->extend == IV_UNKNOWN_EXTEND
583 && iv1->mode == mode
584 && iv1->step == const0_rtx)
586 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
587 return true;
590 if (iv0->extend == IV_UNKNOWN_EXTEND
591 && iv0->mode == mode
592 && iv0->step == const0_rtx)
594 arg = iv0->base;
595 *iv0 = *iv1;
596 if (op == MINUS
597 && !iv_neg (iv0))
598 return false;
600 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
601 return true;
604 return false;
607 /* Evaluates multiplication of IV by constant CST. */
609 static bool
610 iv_mult (struct rtx_iv *iv, rtx mby)
612 machine_mode mode = iv->extend_mode;
614 if (GET_MODE (mby) != VOIDmode
615 && GET_MODE (mby) != mode)
616 return false;
618 if (iv->extend == IV_UNKNOWN_EXTEND)
620 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
621 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
623 else
625 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
626 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
629 return true;
632 /* Evaluates shift of IV by constant CST. */
634 static bool
635 iv_shift (struct rtx_iv *iv, rtx mby)
637 machine_mode mode = iv->extend_mode;
639 if (GET_MODE (mby) != VOIDmode
640 && GET_MODE (mby) != mode)
641 return false;
643 if (iv->extend == IV_UNKNOWN_EXTEND)
645 iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
646 iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
648 else
650 iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
651 iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
654 return true;
657 /* The recursive part of get_biv_step. Gets the value of the single value
658 defined by DEF wrto initial value of REG inside loop, in shape described
659 at get_biv_step. */
661 static bool
662 get_biv_step_1 (df_ref def, rtx reg,
663 rtx *inner_step, machine_mode *inner_mode,
664 enum iv_extend_code *extend, machine_mode outer_mode,
665 rtx *outer_step)
667 rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
668 rtx next, nextr, tmp;
669 enum rtx_code code;
670 rtx_insn *insn = DF_REF_INSN (def);
671 df_ref next_def;
672 enum iv_grd_result res;
674 set = single_set (insn);
675 if (!set)
676 return false;
678 rhs = find_reg_equal_equiv_note (insn);
679 if (rhs)
680 rhs = XEXP (rhs, 0);
681 else
682 rhs = SET_SRC (set);
684 code = GET_CODE (rhs);
685 switch (code)
687 case SUBREG:
688 case REG:
689 next = rhs;
690 break;
692 case PLUS:
693 case MINUS:
694 op0 = XEXP (rhs, 0);
695 op1 = XEXP (rhs, 1);
697 if (code == PLUS && CONSTANT_P (op0))
699 tmp = op0; op0 = op1; op1 = tmp;
702 if (!simple_reg_p (op0)
703 || !CONSTANT_P (op1))
704 return false;
706 if (GET_MODE (rhs) != outer_mode)
708 /* ppc64 uses expressions like
710 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
712 this is equivalent to
714 (set x':DI (plus:DI y:DI 1))
715 (set x:SI (subreg:SI (x':DI)). */
716 if (GET_CODE (op0) != SUBREG)
717 return false;
718 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
719 return false;
722 next = op0;
723 break;
725 case SIGN_EXTEND:
726 case ZERO_EXTEND:
727 if (GET_MODE (rhs) != outer_mode)
728 return false;
730 op0 = XEXP (rhs, 0);
731 if (!simple_reg_p (op0))
732 return false;
734 next = op0;
735 break;
737 default:
738 return false;
741 if (GET_CODE (next) == SUBREG)
743 if (!subreg_lowpart_p (next))
744 return false;
746 nextr = SUBREG_REG (next);
747 if (GET_MODE (nextr) != outer_mode)
748 return false;
750 else
751 nextr = next;
753 res = iv_get_reaching_def (insn, nextr, &next_def);
755 if (res == GRD_INVALID || res == GRD_INVARIANT)
756 return false;
758 if (res == GRD_MAYBE_BIV)
760 if (!rtx_equal_p (nextr, reg))
761 return false;
763 *inner_step = const0_rtx;
764 *extend = IV_UNKNOWN_EXTEND;
765 *inner_mode = outer_mode;
766 *outer_step = const0_rtx;
768 else if (!get_biv_step_1 (next_def, reg,
769 inner_step, inner_mode, extend, outer_mode,
770 outer_step))
771 return false;
773 if (GET_CODE (next) == SUBREG)
775 machine_mode amode = GET_MODE (next);
777 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
778 return false;
780 *inner_mode = amode;
781 *inner_step = simplify_gen_binary (PLUS, outer_mode,
782 *inner_step, *outer_step);
783 *outer_step = const0_rtx;
784 *extend = IV_UNKNOWN_EXTEND;
787 switch (code)
789 case REG:
790 case SUBREG:
791 break;
793 case PLUS:
794 case MINUS:
795 if (*inner_mode == outer_mode
796 /* See comment in previous switch. */
797 || GET_MODE (rhs) != outer_mode)
798 *inner_step = simplify_gen_binary (code, outer_mode,
799 *inner_step, op1);
800 else
801 *outer_step = simplify_gen_binary (code, outer_mode,
802 *outer_step, op1);
803 break;
805 case SIGN_EXTEND:
806 case ZERO_EXTEND:
807 gcc_assert (GET_MODE (op0) == *inner_mode
808 && *extend == IV_UNKNOWN_EXTEND
809 && *outer_step == const0_rtx);
811 *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
812 break;
814 default:
815 return false;
818 return true;
821 /* Gets the operation on register REG inside loop, in shape
823 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
825 If the operation cannot be described in this shape, return false.
826 LAST_DEF is the definition of REG that dominates loop latch. */
828 static bool
829 get_biv_step (df_ref last_def, rtx reg, rtx *inner_step,
830 machine_mode *inner_mode, enum iv_extend_code *extend,
831 machine_mode *outer_mode, rtx *outer_step)
833 *outer_mode = GET_MODE (reg);
835 if (!get_biv_step_1 (last_def, reg,
836 inner_step, inner_mode, extend, *outer_mode,
837 outer_step))
838 return false;
840 gcc_assert ((*inner_mode == *outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
841 gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
843 return true;
846 /* Records information that DEF is induction variable IV. */
848 static void
849 record_iv (df_ref def, struct rtx_iv *iv)
851 struct rtx_iv *recorded_iv = XNEW (struct rtx_iv);
853 *recorded_iv = *iv;
854 check_iv_ref_table_size ();
855 DF_REF_IV_SET (def, recorded_iv);
858 /* If DEF was already analyzed for bivness, store the description of the biv to
859 IV and return true. Otherwise return false. */
861 static bool
862 analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
864 struct biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
866 if (!biv)
867 return false;
869 *iv = biv->iv;
870 return true;
873 static void
874 record_biv (rtx def, struct rtx_iv *iv)
876 struct biv_entry *biv = XNEW (struct biv_entry);
877 biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
879 biv->regno = REGNO (def);
880 biv->iv = *iv;
881 gcc_assert (!*slot);
882 *slot = biv;
885 /* Determines whether DEF is a biv and if so, stores its description
886 to *IV. */
888 static bool
889 iv_analyze_biv (rtx def, struct rtx_iv *iv)
891 rtx inner_step, outer_step;
892 machine_mode inner_mode, outer_mode;
893 enum iv_extend_code extend;
894 df_ref last_def;
896 if (dump_file)
898 fprintf (dump_file, "Analyzing ");
899 print_rtl (dump_file, def);
900 fprintf (dump_file, " for bivness.\n");
903 if (!REG_P (def))
905 if (!CONSTANT_P (def))
906 return false;
908 return iv_constant (iv, def, VOIDmode);
911 if (!latch_dominating_def (def, &last_def))
913 if (dump_file)
914 fprintf (dump_file, " not simple.\n");
915 return false;
918 if (!last_def)
919 return iv_constant (iv, def, VOIDmode);
921 if (analyzed_for_bivness_p (def, iv))
923 if (dump_file)
924 fprintf (dump_file, " already analysed.\n");
925 return iv->base != NULL_RTX;
928 if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
929 &outer_mode, &outer_step))
931 iv->base = NULL_RTX;
932 goto end;
935 /* Loop transforms base to es (base + inner_step) + outer_step,
936 where es means extend of subreg between inner_mode and outer_mode.
937 The corresponding induction variable is
939 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
941 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
942 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
943 iv->mode = inner_mode;
944 iv->extend_mode = outer_mode;
945 iv->extend = extend;
946 iv->mult = const1_rtx;
947 iv->delta = outer_step;
948 iv->first_special = inner_mode != outer_mode;
950 end:
951 if (dump_file)
953 fprintf (dump_file, " ");
954 dump_iv_info (dump_file, iv);
955 fprintf (dump_file, "\n");
958 record_biv (def, iv);
959 return iv->base != NULL_RTX;
962 /* Analyzes expression RHS used at INSN and stores the result to *IV.
963 The mode of the induction variable is MODE. */
965 bool
966 iv_analyze_expr (rtx_insn *insn, rtx rhs, machine_mode mode,
967 struct rtx_iv *iv)
969 rtx mby = NULL_RTX;
970 rtx op0 = NULL_RTX, op1 = NULL_RTX;
971 struct rtx_iv iv0, iv1;
972 enum rtx_code code = GET_CODE (rhs);
973 machine_mode omode = mode;
975 iv->mode = VOIDmode;
976 iv->base = NULL_RTX;
977 iv->step = NULL_RTX;
979 gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
981 if (CONSTANT_P (rhs)
982 || REG_P (rhs)
983 || code == SUBREG)
985 if (!iv_analyze_op (insn, rhs, iv))
986 return false;
988 if (iv->mode == VOIDmode)
990 iv->mode = mode;
991 iv->extend_mode = mode;
994 return true;
997 switch (code)
999 case REG:
1000 op0 = rhs;
1001 break;
1003 case SIGN_EXTEND:
1004 case ZERO_EXTEND:
1005 case NEG:
1006 op0 = XEXP (rhs, 0);
1007 omode = GET_MODE (op0);
1008 break;
1010 case PLUS:
1011 case MINUS:
1012 op0 = XEXP (rhs, 0);
1013 op1 = XEXP (rhs, 1);
1014 break;
1016 case MULT:
1017 op0 = XEXP (rhs, 0);
1018 mby = XEXP (rhs, 1);
1019 if (!CONSTANT_P (mby))
1020 std::swap (op0, mby);
1021 if (!CONSTANT_P (mby))
1022 return false;
1023 break;
1025 case ASHIFT:
1026 op0 = XEXP (rhs, 0);
1027 mby = XEXP (rhs, 1);
1028 if (!CONSTANT_P (mby))
1029 return false;
1030 break;
1032 default:
1033 return false;
1036 if (op0
1037 && !iv_analyze_expr (insn, op0, omode, &iv0))
1038 return false;
1040 if (op1
1041 && !iv_analyze_expr (insn, op1, omode, &iv1))
1042 return false;
1044 switch (code)
1046 case SIGN_EXTEND:
1047 if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
1048 return false;
1049 break;
1051 case ZERO_EXTEND:
1052 if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
1053 return false;
1054 break;
1056 case NEG:
1057 if (!iv_neg (&iv0))
1058 return false;
1059 break;
1061 case PLUS:
1062 case MINUS:
1063 if (!iv_add (&iv0, &iv1, code))
1064 return false;
1065 break;
1067 case MULT:
1068 if (!iv_mult (&iv0, mby))
1069 return false;
1070 break;
1072 case ASHIFT:
1073 if (!iv_shift (&iv0, mby))
1074 return false;
1075 break;
1077 default:
1078 break;
1081 *iv = iv0;
1082 return iv->base != NULL_RTX;
1085 /* Analyzes iv DEF and stores the result to *IV. */
1087 static bool
1088 iv_analyze_def (df_ref def, struct rtx_iv *iv)
1090 rtx_insn *insn = DF_REF_INSN (def);
1091 rtx reg = DF_REF_REG (def);
1092 rtx set, rhs;
1094 if (dump_file)
1096 fprintf (dump_file, "Analyzing def of ");
1097 print_rtl (dump_file, reg);
1098 fprintf (dump_file, " in insn ");
1099 print_rtl_single (dump_file, insn);
1102 check_iv_ref_table_size ();
1103 if (DF_REF_IV (def))
1105 if (dump_file)
1106 fprintf (dump_file, " already analysed.\n");
1107 *iv = *DF_REF_IV (def);
1108 return iv->base != NULL_RTX;
1111 iv->mode = VOIDmode;
1112 iv->base = NULL_RTX;
1113 iv->step = NULL_RTX;
1115 if (!REG_P (reg))
1116 return false;
1118 set = single_set (insn);
1119 if (!set)
1120 return false;
1122 if (!REG_P (SET_DEST (set)))
1123 return false;
1125 gcc_assert (SET_DEST (set) == reg);
1126 rhs = find_reg_equal_equiv_note (insn);
1127 if (rhs)
1128 rhs = XEXP (rhs, 0);
1129 else
1130 rhs = SET_SRC (set);
1132 iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
1133 record_iv (def, iv);
1135 if (dump_file)
1137 print_rtl (dump_file, reg);
1138 fprintf (dump_file, " in insn ");
1139 print_rtl_single (dump_file, insn);
1140 fprintf (dump_file, " is ");
1141 dump_iv_info (dump_file, iv);
1142 fprintf (dump_file, "\n");
1145 return iv->base != NULL_RTX;
1148 /* Analyzes operand OP of INSN and stores the result to *IV. */
1150 static bool
1151 iv_analyze_op (rtx_insn *insn, rtx op, struct rtx_iv *iv)
1153 df_ref def = NULL;
1154 enum iv_grd_result res;
1156 if (dump_file)
1158 fprintf (dump_file, "Analyzing operand ");
1159 print_rtl (dump_file, op);
1160 fprintf (dump_file, " of insn ");
1161 print_rtl_single (dump_file, insn);
1164 if (function_invariant_p (op))
1165 res = GRD_INVARIANT;
1166 else if (GET_CODE (op) == SUBREG)
1168 if (!subreg_lowpart_p (op))
1169 return false;
1171 if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
1172 return false;
1174 return iv_subreg (iv, GET_MODE (op));
1176 else
1178 res = iv_get_reaching_def (insn, op, &def);
1179 if (res == GRD_INVALID)
1181 if (dump_file)
1182 fprintf (dump_file, " not simple.\n");
1183 return false;
1187 if (res == GRD_INVARIANT)
1189 iv_constant (iv, op, VOIDmode);
1191 if (dump_file)
1193 fprintf (dump_file, " ");
1194 dump_iv_info (dump_file, iv);
1195 fprintf (dump_file, "\n");
1197 return true;
1200 if (res == GRD_MAYBE_BIV)
1201 return iv_analyze_biv (op, iv);
1203 return iv_analyze_def (def, iv);
1206 /* Analyzes value VAL at INSN and stores the result to *IV. */
1208 bool
1209 iv_analyze (rtx_insn *insn, rtx val, struct rtx_iv *iv)
1211 rtx reg;
1213 /* We must find the insn in that val is used, so that we get to UD chains.
1214 Since the function is sometimes called on result of get_condition,
1215 this does not necessarily have to be directly INSN; scan also the
1216 following insns. */
1217 if (simple_reg_p (val))
1219 if (GET_CODE (val) == SUBREG)
1220 reg = SUBREG_REG (val);
1221 else
1222 reg = val;
1224 while (!df_find_use (insn, reg))
1225 insn = NEXT_INSN (insn);
1228 return iv_analyze_op (insn, val, iv);
1231 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1233 bool
1234 iv_analyze_result (rtx_insn *insn, rtx def, struct rtx_iv *iv)
1236 df_ref adef;
1238 adef = df_find_def (insn, def);
1239 if (!adef)
1240 return false;
1242 return iv_analyze_def (adef, iv);
1245 /* Checks whether definition of register REG in INSN is a basic induction
1246 variable. IV analysis must have been initialized (via a call to
1247 iv_analysis_loop_init) for this function to produce a result. */
1249 bool
1250 biv_p (rtx_insn *insn, rtx reg)
1252 struct rtx_iv iv;
1253 df_ref def, last_def;
1255 if (!simple_reg_p (reg))
1256 return false;
1258 def = df_find_def (insn, reg);
1259 gcc_assert (def != NULL);
1260 if (!latch_dominating_def (reg, &last_def))
1261 return false;
1262 if (last_def != def)
1263 return false;
1265 if (!iv_analyze_biv (reg, &iv))
1266 return false;
1268 return iv.step != const0_rtx;
1271 /* Calculates value of IV at ITERATION-th iteration. */
1274 get_iv_value (struct rtx_iv *iv, rtx iteration)
1276 rtx val;
1278 /* We would need to generate some if_then_else patterns, and so far
1279 it is not needed anywhere. */
1280 gcc_assert (!iv->first_special);
1282 if (iv->step != const0_rtx && iteration != const0_rtx)
1283 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1284 simplify_gen_binary (MULT, iv->extend_mode,
1285 iv->step, iteration));
1286 else
1287 val = iv->base;
1289 if (iv->extend_mode == iv->mode)
1290 return val;
1292 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
1294 if (iv->extend == IV_UNKNOWN_EXTEND)
1295 return val;
1297 val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
1298 iv->extend_mode, val, iv->mode);
1299 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1300 simplify_gen_binary (MULT, iv->extend_mode,
1301 iv->mult, val));
1303 return val;
1306 /* Free the data for an induction variable analysis. */
1308 void
1309 iv_analysis_done (void)
1311 if (!clean_slate)
1313 clear_iv_info ();
1314 clean_slate = true;
1315 df_finish_pass (true);
1316 delete bivs;
1317 bivs = NULL;
1318 free (iv_ref_table);
1319 iv_ref_table = NULL;
1320 iv_ref_table_size = 0;
1324 /* Computes inverse to X modulo (1 << MOD). */
1326 static uint64_t
1327 inverse (uint64_t x, int mod)
1329 uint64_t mask =
1330 ((uint64_t) 1 << (mod - 1) << 1) - 1;
1331 uint64_t rslt = 1;
1332 int i;
1334 for (i = 0; i < mod - 1; i++)
1336 rslt = (rslt * x) & mask;
1337 x = (x * x) & mask;
1340 return rslt;
1343 /* Checks whether any register in X is in set ALT. */
1345 static bool
1346 altered_reg_used (const_rtx x, bitmap alt)
1348 subrtx_iterator::array_type array;
1349 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
1351 const_rtx x = *iter;
1352 if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
1353 return true;
1355 return false;
1358 /* Marks registers altered by EXPR in set ALT. */
1360 static void
1361 mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
1363 if (GET_CODE (expr) == SUBREG)
1364 expr = SUBREG_REG (expr);
1365 if (!REG_P (expr))
1366 return;
1368 SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
1371 /* Checks whether RHS is simple enough to process. */
1373 static bool
1374 simple_rhs_p (rtx rhs)
1376 rtx op0, op1;
1378 if (function_invariant_p (rhs)
1379 || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
1380 return true;
1382 switch (GET_CODE (rhs))
1384 case PLUS:
1385 case MINUS:
1386 case AND:
1387 op0 = XEXP (rhs, 0);
1388 op1 = XEXP (rhs, 1);
1389 /* Allow reg OP const and reg OP reg. */
1390 if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
1391 && !function_invariant_p (op0))
1392 return false;
1393 if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
1394 && !function_invariant_p (op1))
1395 return false;
1397 return true;
1399 case ASHIFT:
1400 case ASHIFTRT:
1401 case LSHIFTRT:
1402 case MULT:
1403 op0 = XEXP (rhs, 0);
1404 op1 = XEXP (rhs, 1);
1405 /* Allow reg OP const. */
1406 if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
1407 return false;
1408 if (!function_invariant_p (op1))
1409 return false;
1411 return true;
1413 default:
1414 return false;
1418 /* If REGNO has a single definition, return its known value, otherwise return
1419 null. */
1421 static rtx
1422 find_single_def_src (unsigned int regno)
1424 df_ref adef;
1425 rtx set, src;
1427 for (;;)
1429 rtx note;
1430 adef = DF_REG_DEF_CHAIN (regno);
1431 if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL
1432 || DF_REF_IS_ARTIFICIAL (adef))
1433 return NULL_RTX;
1435 set = single_set (DF_REF_INSN (adef));
1436 if (set == NULL || !REG_P (SET_DEST (set))
1437 || REGNO (SET_DEST (set)) != regno)
1438 return NULL_RTX;
1440 note = find_reg_equal_equiv_note (DF_REF_INSN (adef));
1442 if (note && function_invariant_p (XEXP (note, 0)))
1444 src = XEXP (note, 0);
1445 break;
1447 src = SET_SRC (set);
1449 if (REG_P (src))
1451 regno = REGNO (src);
1452 continue;
1454 break;
1456 if (!function_invariant_p (src))
1457 return NULL_RTX;
1459 return src;
1462 /* If any registers in *EXPR that have a single definition, try to replace
1463 them with the known-equivalent values. */
1465 static void
1466 replace_single_def_regs (rtx *expr)
1468 subrtx_var_iterator::array_type array;
1469 repeat:
1470 FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
1472 rtx x = *iter;
1473 if (REG_P (x))
1474 if (rtx new_x = find_single_def_src (REGNO (x)))
1476 *expr = simplify_replace_rtx (*expr, x, new_x);
1477 goto repeat;
1482 /* A subroutine of simplify_using_initial_values, this function examines INSN
1483 to see if it contains a suitable set that we can use to make a replacement.
1484 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1485 the set; return false otherwise. */
1487 static bool
1488 suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
1490 rtx set = single_set (insn);
1491 rtx lhs = NULL_RTX, rhs;
1493 if (!set)
1494 return false;
1496 lhs = SET_DEST (set);
1497 if (!REG_P (lhs))
1498 return false;
1500 rhs = find_reg_equal_equiv_note (insn);
1501 if (rhs)
1502 rhs = XEXP (rhs, 0);
1503 else
1504 rhs = SET_SRC (set);
1506 if (!simple_rhs_p (rhs))
1507 return false;
1509 *dest = lhs;
1510 *src = rhs;
1511 return true;
1514 /* Using the data returned by suitable_set_for_replacement, replace DEST
1515 with SRC in *EXPR and return the new expression. Also call
1516 replace_single_def_regs if the replacement changed something. */
1517 static void
1518 replace_in_expr (rtx *expr, rtx dest, rtx src)
1520 rtx old = *expr;
1521 *expr = simplify_replace_rtx (*expr, dest, src);
1522 if (old == *expr)
1523 return;
1524 replace_single_def_regs (expr);
1527 /* Checks whether A implies B. */
1529 static bool
1530 implies_p (rtx a, rtx b)
1532 rtx op0, op1, opb0, opb1;
1533 machine_mode mode;
1535 if (rtx_equal_p (a, b))
1536 return true;
1538 if (GET_CODE (a) == EQ)
1540 op0 = XEXP (a, 0);
1541 op1 = XEXP (a, 1);
1543 if (REG_P (op0)
1544 || (GET_CODE (op0) == SUBREG
1545 && REG_P (SUBREG_REG (op0))))
1547 rtx r = simplify_replace_rtx (b, op0, op1);
1548 if (r == const_true_rtx)
1549 return true;
1552 if (REG_P (op1)
1553 || (GET_CODE (op1) == SUBREG
1554 && REG_P (SUBREG_REG (op1))))
1556 rtx r = simplify_replace_rtx (b, op1, op0);
1557 if (r == const_true_rtx)
1558 return true;
1562 if (b == const_true_rtx)
1563 return true;
1565 if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
1566 && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
1567 || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
1568 && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
1569 return false;
1571 op0 = XEXP (a, 0);
1572 op1 = XEXP (a, 1);
1573 opb0 = XEXP (b, 0);
1574 opb1 = XEXP (b, 1);
1576 mode = GET_MODE (op0);
1577 if (mode != GET_MODE (opb0))
1578 mode = VOIDmode;
1579 else if (mode == VOIDmode)
1581 mode = GET_MODE (op1);
1582 if (mode != GET_MODE (opb1))
1583 mode = VOIDmode;
1586 /* A < B implies A + 1 <= B. */
1587 if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
1588 && (GET_CODE (b) == GE || GET_CODE (b) == LE))
1591 if (GET_CODE (a) == GT)
1592 std::swap (op0, op1);
1594 if (GET_CODE (b) == GE)
1595 std::swap (opb0, opb1);
1597 if (SCALAR_INT_MODE_P (mode)
1598 && rtx_equal_p (op1, opb1)
1599 && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
1600 return true;
1601 return false;
1604 /* A < B or A > B imply A != B. TODO: Likewise
1605 A + n < B implies A != B + n if neither wraps. */
1606 if (GET_CODE (b) == NE
1607 && (GET_CODE (a) == GT || GET_CODE (a) == GTU
1608 || GET_CODE (a) == LT || GET_CODE (a) == LTU))
1610 if (rtx_equal_p (op0, opb0)
1611 && rtx_equal_p (op1, opb1))
1612 return true;
1615 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1616 if (GET_CODE (a) == NE
1617 && op1 == const0_rtx)
1619 if ((GET_CODE (b) == GTU
1620 && opb1 == const0_rtx)
1621 || (GET_CODE (b) == GEU
1622 && opb1 == const1_rtx))
1623 return rtx_equal_p (op0, opb0);
1626 /* A != N is equivalent to A - (N + 1) <u -1. */
1627 if (GET_CODE (a) == NE
1628 && CONST_INT_P (op1)
1629 && GET_CODE (b) == LTU
1630 && opb1 == constm1_rtx
1631 && GET_CODE (opb0) == PLUS
1632 && CONST_INT_P (XEXP (opb0, 1))
1633 /* Avoid overflows. */
1634 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1635 != ((unsigned HOST_WIDE_INT)1
1636 << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1637 && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
1638 return rtx_equal_p (op0, XEXP (opb0, 0));
1640 /* Likewise, A != N implies A - N > 0. */
1641 if (GET_CODE (a) == NE
1642 && CONST_INT_P (op1))
1644 if (GET_CODE (b) == GTU
1645 && GET_CODE (opb0) == PLUS
1646 && opb1 == const0_rtx
1647 && CONST_INT_P (XEXP (opb0, 1))
1648 /* Avoid overflows. */
1649 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1650 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1651 && rtx_equal_p (XEXP (opb0, 0), op0))
1652 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1653 if (GET_CODE (b) == GEU
1654 && GET_CODE (opb0) == PLUS
1655 && opb1 == const1_rtx
1656 && CONST_INT_P (XEXP (opb0, 1))
1657 /* Avoid overflows. */
1658 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1659 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1660 && rtx_equal_p (XEXP (opb0, 0), op0))
1661 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1664 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1665 if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
1666 && CONST_INT_P (op1)
1667 && ((GET_CODE (a) == GT && op1 == constm1_rtx)
1668 || INTVAL (op1) >= 0)
1669 && GET_CODE (b) == LTU
1670 && CONST_INT_P (opb1)
1671 && rtx_equal_p (op0, opb0))
1672 return INTVAL (opb1) < 0;
1674 return false;
1677 /* Canonicalizes COND so that
1679 (1) Ensure that operands are ordered according to
1680 swap_commutative_operands_p.
1681 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1682 for GE, GEU, and LEU. */
1685 canon_condition (rtx cond)
1687 rtx op0, op1;
1688 enum rtx_code code;
1689 machine_mode mode;
1691 code = GET_CODE (cond);
1692 op0 = XEXP (cond, 0);
1693 op1 = XEXP (cond, 1);
1695 if (swap_commutative_operands_p (op0, op1))
1697 code = swap_condition (code);
1698 std::swap (op0, op1);
1701 mode = GET_MODE (op0);
1702 if (mode == VOIDmode)
1703 mode = GET_MODE (op1);
1704 gcc_assert (mode != VOIDmode);
1706 if (CONST_SCALAR_INT_P (op1) && GET_MODE_CLASS (mode) != MODE_CC)
1708 rtx_mode_t const_val (op1, mode);
1710 switch (code)
1712 case LE:
1713 if (wi::ne_p (const_val, wi::max_value (mode, SIGNED)))
1715 code = LT;
1716 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1718 break;
1720 case GE:
1721 if (wi::ne_p (const_val, wi::min_value (mode, SIGNED)))
1723 code = GT;
1724 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1726 break;
1728 case LEU:
1729 if (wi::ne_p (const_val, -1))
1731 code = LTU;
1732 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1734 break;
1736 case GEU:
1737 if (wi::ne_p (const_val, 0))
1739 code = GTU;
1740 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1742 break;
1744 default:
1745 break;
1749 if (op0 != XEXP (cond, 0)
1750 || op1 != XEXP (cond, 1)
1751 || code != GET_CODE (cond)
1752 || GET_MODE (cond) != SImode)
1753 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1755 return cond;
1758 /* Reverses CONDition; returns NULL if we cannot. */
1760 static rtx
1761 reversed_condition (rtx cond)
1763 enum rtx_code reversed;
1764 reversed = reversed_comparison_code (cond, NULL);
1765 if (reversed == UNKNOWN)
1766 return NULL_RTX;
1767 else
1768 return gen_rtx_fmt_ee (reversed,
1769 GET_MODE (cond), XEXP (cond, 0),
1770 XEXP (cond, 1));
1773 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1774 set of altered regs. */
1776 void
1777 simplify_using_condition (rtx cond, rtx *expr, regset altered)
1779 rtx rev, reve, exp = *expr;
1781 /* If some register gets altered later, we do not really speak about its
1782 value at the time of comparison. */
1783 if (altered && altered_reg_used (cond, altered))
1784 return;
1786 if (GET_CODE (cond) == EQ
1787 && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
1789 *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
1790 return;
1793 if (!COMPARISON_P (exp))
1794 return;
1796 rev = reversed_condition (cond);
1797 reve = reversed_condition (exp);
1799 cond = canon_condition (cond);
1800 exp = canon_condition (exp);
1801 if (rev)
1802 rev = canon_condition (rev);
1803 if (reve)
1804 reve = canon_condition (reve);
1806 if (rtx_equal_p (exp, cond))
1808 *expr = const_true_rtx;
1809 return;
1812 if (rev && rtx_equal_p (exp, rev))
1814 *expr = const0_rtx;
1815 return;
1818 if (implies_p (cond, exp))
1820 *expr = const_true_rtx;
1821 return;
1824 if (reve && implies_p (cond, reve))
1826 *expr = const0_rtx;
1827 return;
1830 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1831 be false. */
1832 if (rev && implies_p (exp, rev))
1834 *expr = const0_rtx;
1835 return;
1838 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1839 if (rev && reve && implies_p (reve, rev))
1841 *expr = const_true_rtx;
1842 return;
1845 /* We would like to have some other tests here. TODO. */
1847 return;
1850 /* Use relationship between A and *B to eventually eliminate *B.
1851 OP is the operation we consider. */
1853 static void
1854 eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1856 switch (op)
1858 case AND:
1859 /* If A implies *B, we may replace *B by true. */
1860 if (implies_p (a, *b))
1861 *b = const_true_rtx;
1862 break;
1864 case IOR:
1865 /* If *B implies A, we may replace *B by false. */
1866 if (implies_p (*b, a))
1867 *b = const0_rtx;
1868 break;
1870 default:
1871 gcc_unreachable ();
1875 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1876 operation we consider. */
1878 static void
1879 eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1881 rtx elt;
1883 for (elt = tail; elt; elt = XEXP (elt, 1))
1884 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1885 for (elt = tail; elt; elt = XEXP (elt, 1))
1886 eliminate_implied_condition (op, XEXP (elt, 0), head);
1889 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1890 is a list, its elements are assumed to be combined using OP. */
1892 static void
1893 simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1895 bool expression_valid;
1896 rtx head, tail, last_valid_expr;
1897 rtx_expr_list *cond_list;
1898 rtx_insn *insn;
1899 rtx neutral, aggr;
1900 regset altered, this_altered;
1901 edge e;
1903 if (!*expr)
1904 return;
1906 if (CONSTANT_P (*expr))
1907 return;
1909 if (GET_CODE (*expr) == EXPR_LIST)
1911 head = XEXP (*expr, 0);
1912 tail = XEXP (*expr, 1);
1914 eliminate_implied_conditions (op, &head, tail);
1916 switch (op)
1918 case AND:
1919 neutral = const_true_rtx;
1920 aggr = const0_rtx;
1921 break;
1923 case IOR:
1924 neutral = const0_rtx;
1925 aggr = const_true_rtx;
1926 break;
1928 default:
1929 gcc_unreachable ();
1932 simplify_using_initial_values (loop, UNKNOWN, &head);
1933 if (head == aggr)
1935 XEXP (*expr, 0) = aggr;
1936 XEXP (*expr, 1) = NULL_RTX;
1937 return;
1939 else if (head == neutral)
1941 *expr = tail;
1942 simplify_using_initial_values (loop, op, expr);
1943 return;
1945 simplify_using_initial_values (loop, op, &tail);
1947 if (tail && XEXP (tail, 0) == aggr)
1949 *expr = tail;
1950 return;
1953 XEXP (*expr, 0) = head;
1954 XEXP (*expr, 1) = tail;
1955 return;
1958 gcc_assert (op == UNKNOWN);
1960 replace_single_def_regs (expr);
1961 if (CONSTANT_P (*expr))
1962 return;
1964 e = loop_preheader_edge (loop);
1965 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1966 return;
1968 altered = ALLOC_REG_SET (&reg_obstack);
1969 this_altered = ALLOC_REG_SET (&reg_obstack);
1971 expression_valid = true;
1972 last_valid_expr = *expr;
1973 cond_list = NULL;
1974 while (1)
1976 insn = BB_END (e->src);
1977 if (any_condjump_p (insn))
1979 rtx cond = get_condition (BB_END (e->src), NULL, false, true);
1981 if (cond && (e->flags & EDGE_FALLTHRU))
1982 cond = reversed_condition (cond);
1983 if (cond)
1985 rtx old = *expr;
1986 simplify_using_condition (cond, expr, altered);
1987 if (old != *expr)
1989 rtx note;
1990 if (CONSTANT_P (*expr))
1991 goto out;
1992 for (note = cond_list; note; note = XEXP (note, 1))
1994 simplify_using_condition (XEXP (note, 0), expr, altered);
1995 if (CONSTANT_P (*expr))
1996 goto out;
1999 cond_list = alloc_EXPR_LIST (0, cond, cond_list);
2003 FOR_BB_INSNS_REVERSE (e->src, insn)
2005 rtx src, dest;
2006 rtx old = *expr;
2008 if (!INSN_P (insn))
2009 continue;
2011 CLEAR_REG_SET (this_altered);
2012 note_stores (PATTERN (insn), mark_altered, this_altered);
2013 if (CALL_P (insn))
2015 /* Kill all call clobbered registers. */
2016 unsigned int i;
2017 hard_reg_set_iterator hrsi;
2018 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
2019 0, i, hrsi)
2020 SET_REGNO_REG_SET (this_altered, i);
2023 if (suitable_set_for_replacement (insn, &dest, &src))
2025 rtx_expr_list **pnote, **pnote_next;
2027 replace_in_expr (expr, dest, src);
2028 if (CONSTANT_P (*expr))
2029 goto out;
2031 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2033 rtx_expr_list *note = *pnote;
2034 rtx old_cond = XEXP (note, 0);
2036 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2037 replace_in_expr (&XEXP (note, 0), dest, src);
2039 /* We can no longer use a condition that has been simplified
2040 to a constant, and simplify_using_condition will abort if
2041 we try. */
2042 if (CONSTANT_P (XEXP (note, 0)))
2044 *pnote = *pnote_next;
2045 pnote_next = pnote;
2046 free_EXPR_LIST_node (note);
2048 /* Retry simplifications with this condition if either the
2049 expression or the condition changed. */
2050 else if (old_cond != XEXP (note, 0) || old != *expr)
2051 simplify_using_condition (XEXP (note, 0), expr, altered);
2054 else
2056 rtx_expr_list **pnote, **pnote_next;
2058 /* If we did not use this insn to make a replacement, any overlap
2059 between stores in this insn and our expression will cause the
2060 expression to become invalid. */
2061 if (altered_reg_used (*expr, this_altered))
2062 goto out;
2064 /* Likewise for the conditions. */
2065 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2067 rtx_expr_list *note = *pnote;
2068 rtx old_cond = XEXP (note, 0);
2070 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2071 if (altered_reg_used (old_cond, this_altered))
2073 *pnote = *pnote_next;
2074 pnote_next = pnote;
2075 free_EXPR_LIST_node (note);
2080 if (CONSTANT_P (*expr))
2081 goto out;
2083 IOR_REG_SET (altered, this_altered);
2085 /* If the expression now contains regs that have been altered, we
2086 can't return it to the caller. However, it is still valid for
2087 further simplification, so keep searching to see if we can
2088 eventually turn it into a constant. */
2089 if (altered_reg_used (*expr, altered))
2090 expression_valid = false;
2091 if (expression_valid)
2092 last_valid_expr = *expr;
2095 if (!single_pred_p (e->src)
2096 || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2097 break;
2098 e = single_pred_edge (e->src);
2101 out:
2102 free_EXPR_LIST_list (&cond_list);
2103 if (!CONSTANT_P (*expr))
2104 *expr = last_valid_expr;
2105 FREE_REG_SET (altered);
2106 FREE_REG_SET (this_altered);
2109 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2110 that IV occurs as left operands of comparison COND and its signedness
2111 is SIGNED_P to DESC. */
2113 static void
2114 shorten_into_mode (struct rtx_iv *iv, machine_mode mode,
2115 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
2117 rtx mmin, mmax, cond_over, cond_under;
2119 get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
2120 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
2121 iv->base, mmin);
2122 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
2123 iv->base, mmax);
2125 switch (cond)
2127 case LE:
2128 case LT:
2129 case LEU:
2130 case LTU:
2131 if (cond_under != const0_rtx)
2132 desc->infinite =
2133 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2134 if (cond_over != const0_rtx)
2135 desc->noloop_assumptions =
2136 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
2137 break;
2139 case GE:
2140 case GT:
2141 case GEU:
2142 case GTU:
2143 if (cond_over != const0_rtx)
2144 desc->infinite =
2145 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2146 if (cond_under != const0_rtx)
2147 desc->noloop_assumptions =
2148 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
2149 break;
2151 case NE:
2152 if (cond_over != const0_rtx)
2153 desc->infinite =
2154 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2155 if (cond_under != const0_rtx)
2156 desc->infinite =
2157 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2158 break;
2160 default:
2161 gcc_unreachable ();
2164 iv->mode = mode;
2165 iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
2168 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2169 subregs of the same mode if possible (sometimes it is necessary to add
2170 some assumptions to DESC). */
2172 static bool
2173 canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
2174 enum rtx_code cond, struct niter_desc *desc)
2176 machine_mode comp_mode;
2177 bool signed_p;
2179 /* If the ivs behave specially in the first iteration, or are
2180 added/multiplied after extending, we ignore them. */
2181 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
2182 return false;
2183 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
2184 return false;
2186 /* If there is some extend, it must match signedness of the comparison. */
2187 switch (cond)
2189 case LE:
2190 case LT:
2191 if (iv0->extend == IV_ZERO_EXTEND
2192 || iv1->extend == IV_ZERO_EXTEND)
2193 return false;
2194 signed_p = true;
2195 break;
2197 case LEU:
2198 case LTU:
2199 if (iv0->extend == IV_SIGN_EXTEND
2200 || iv1->extend == IV_SIGN_EXTEND)
2201 return false;
2202 signed_p = false;
2203 break;
2205 case NE:
2206 if (iv0->extend != IV_UNKNOWN_EXTEND
2207 && iv1->extend != IV_UNKNOWN_EXTEND
2208 && iv0->extend != iv1->extend)
2209 return false;
2211 signed_p = false;
2212 if (iv0->extend != IV_UNKNOWN_EXTEND)
2213 signed_p = iv0->extend == IV_SIGN_EXTEND;
2214 if (iv1->extend != IV_UNKNOWN_EXTEND)
2215 signed_p = iv1->extend == IV_SIGN_EXTEND;
2216 break;
2218 default:
2219 gcc_unreachable ();
2222 /* Values of both variables should be computed in the same mode. These
2223 might indeed be different, if we have comparison like
2225 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2227 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2228 in different modes. This does not seem impossible to handle, but
2229 it hardly ever occurs in practice.
2231 The only exception is the case when one of operands is invariant.
2232 For example pentium 3 generates comparisons like
2233 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2234 definitely do not want this prevent the optimization. */
2235 comp_mode = iv0->extend_mode;
2236 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
2237 comp_mode = iv1->extend_mode;
2239 if (iv0->extend_mode != comp_mode)
2241 if (iv0->mode != iv0->extend_mode
2242 || iv0->step != const0_rtx)
2243 return false;
2245 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2246 comp_mode, iv0->base, iv0->mode);
2247 iv0->extend_mode = comp_mode;
2250 if (iv1->extend_mode != comp_mode)
2252 if (iv1->mode != iv1->extend_mode
2253 || iv1->step != const0_rtx)
2254 return false;
2256 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2257 comp_mode, iv1->base, iv1->mode);
2258 iv1->extend_mode = comp_mode;
2261 /* Check that both ivs belong to a range of a single mode. If one of the
2262 operands is an invariant, we may need to shorten it into the common
2263 mode. */
2264 if (iv0->mode == iv0->extend_mode
2265 && iv0->step == const0_rtx
2266 && iv0->mode != iv1->mode)
2267 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
2269 if (iv1->mode == iv1->extend_mode
2270 && iv1->step == const0_rtx
2271 && iv0->mode != iv1->mode)
2272 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
2274 if (iv0->mode != iv1->mode)
2275 return false;
2277 desc->mode = iv0->mode;
2278 desc->signed_p = signed_p;
2280 return true;
2283 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2284 result. This function is called from iv_number_of_iterations with
2285 a number of fields in DESC already filled in. OLD_NITER is the original
2286 expression for the number of iterations, before we tried to simplify it. */
2288 static uint64_t
2289 determine_max_iter (struct loop *loop, struct niter_desc *desc, rtx old_niter)
2291 rtx niter = desc->niter_expr;
2292 rtx mmin, mmax, cmp;
2293 uint64_t nmax, inc;
2294 uint64_t andmax = 0;
2296 /* We used to look for constant operand 0 of AND,
2297 but canonicalization should always make this impossible. */
2298 gcc_checking_assert (GET_CODE (niter) != AND
2299 || !CONST_INT_P (XEXP (niter, 0)));
2301 if (GET_CODE (niter) == AND
2302 && CONST_INT_P (XEXP (niter, 1)))
2304 andmax = UINTVAL (XEXP (niter, 1));
2305 niter = XEXP (niter, 0);
2308 get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
2309 nmax = UINTVAL (mmax) - UINTVAL (mmin);
2311 if (GET_CODE (niter) == UDIV)
2313 if (!CONST_INT_P (XEXP (niter, 1)))
2314 return nmax;
2315 inc = INTVAL (XEXP (niter, 1));
2316 niter = XEXP (niter, 0);
2318 else
2319 inc = 1;
2321 /* We could use a binary search here, but for now improving the upper
2322 bound by just one eliminates one important corner case. */
2323 cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
2324 desc->mode, old_niter, mmax);
2325 simplify_using_initial_values (loop, UNKNOWN, &cmp);
2326 if (cmp == const_true_rtx)
2328 nmax--;
2330 if (dump_file)
2331 fprintf (dump_file, ";; improved upper bound by one.\n");
2333 nmax /= inc;
2334 if (andmax)
2335 nmax = MIN (nmax, andmax);
2336 if (dump_file)
2337 fprintf (dump_file, ";; Determined upper bound %" PRId64".\n",
2338 nmax);
2339 return nmax;
2342 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2343 the result into DESC. Very similar to determine_number_of_iterations
2344 (basically its rtl version), complicated by things like subregs. */
2346 static void
2347 iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
2348 struct niter_desc *desc)
2350 rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
2351 struct rtx_iv iv0, iv1, tmp_iv;
2352 rtx assumption, may_not_xform;
2353 enum rtx_code cond;
2354 machine_mode mode, comp_mode;
2355 rtx mmin, mmax, mode_mmin, mode_mmax;
2356 uint64_t s, size, d, inv, max;
2357 int64_t up, down, inc, step_val;
2358 int was_sharp = false;
2359 rtx old_niter;
2360 bool step_is_pow2;
2362 /* The meaning of these assumptions is this:
2363 if !assumptions
2364 then the rest of information does not have to be valid
2365 if noloop_assumptions then the loop does not roll
2366 if infinite then this exit is never used */
2368 desc->assumptions = NULL_RTX;
2369 desc->noloop_assumptions = NULL_RTX;
2370 desc->infinite = NULL_RTX;
2371 desc->simple_p = true;
2373 desc->const_iter = false;
2374 desc->niter_expr = NULL_RTX;
2376 cond = GET_CODE (condition);
2377 gcc_assert (COMPARISON_P (condition));
2379 mode = GET_MODE (XEXP (condition, 0));
2380 if (mode == VOIDmode)
2381 mode = GET_MODE (XEXP (condition, 1));
2382 /* The constant comparisons should be folded. */
2383 gcc_assert (mode != VOIDmode);
2385 /* We only handle integers or pointers. */
2386 if (GET_MODE_CLASS (mode) != MODE_INT
2387 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
2388 goto fail;
2390 op0 = XEXP (condition, 0);
2391 if (!iv_analyze (insn, op0, &iv0))
2392 goto fail;
2393 if (iv0.extend_mode == VOIDmode)
2394 iv0.mode = iv0.extend_mode = mode;
2396 op1 = XEXP (condition, 1);
2397 if (!iv_analyze (insn, op1, &iv1))
2398 goto fail;
2399 if (iv1.extend_mode == VOIDmode)
2400 iv1.mode = iv1.extend_mode = mode;
2402 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
2403 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
2404 goto fail;
2406 /* Check condition and normalize it. */
2408 switch (cond)
2410 case GE:
2411 case GT:
2412 case GEU:
2413 case GTU:
2414 tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
2415 cond = swap_condition (cond);
2416 break;
2417 case NE:
2418 case LE:
2419 case LEU:
2420 case LT:
2421 case LTU:
2422 break;
2423 default:
2424 goto fail;
2427 /* Handle extends. This is relatively nontrivial, so we only try in some
2428 easy cases, when we can canonicalize the ivs (possibly by adding some
2429 assumptions) to shape subreg (base + i * step). This function also fills
2430 in desc->mode and desc->signed_p. */
2432 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
2433 goto fail;
2435 comp_mode = iv0.extend_mode;
2436 mode = iv0.mode;
2437 size = GET_MODE_PRECISION (mode);
2438 get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
2439 mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
2440 mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
2442 if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
2443 goto fail;
2445 /* We can take care of the case of two induction variables chasing each other
2446 if the test is NE. I have never seen a loop using it, but still it is
2447 cool. */
2448 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
2450 if (cond != NE)
2451 goto fail;
2453 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2454 iv1.step = const0_rtx;
2457 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2458 iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
2460 /* This is either infinite loop or the one that ends immediately, depending
2461 on initial values. Unswitching should remove this kind of conditions. */
2462 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
2463 goto fail;
2465 if (cond != NE)
2467 if (iv0.step == const0_rtx)
2468 step_val = -INTVAL (iv1.step);
2469 else
2470 step_val = INTVAL (iv0.step);
2472 /* Ignore loops of while (i-- < 10) type. */
2473 if (step_val < 0)
2474 goto fail;
2476 step_is_pow2 = !(step_val & (step_val - 1));
2478 else
2480 /* We do not care about whether the step is power of two in this
2481 case. */
2482 step_is_pow2 = false;
2483 step_val = 0;
2486 /* Some more condition normalization. We must record some assumptions
2487 due to overflows. */
2488 switch (cond)
2490 case LT:
2491 case LTU:
2492 /* We want to take care only of non-sharp relationals; this is easy,
2493 as in cases the overflow would make the transformation unsafe
2494 the loop does not roll. Seemingly it would make more sense to want
2495 to take care of sharp relationals instead, as NE is more similar to
2496 them, but the problem is that here the transformation would be more
2497 difficult due to possibly infinite loops. */
2498 if (iv0.step == const0_rtx)
2500 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2501 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2502 mode_mmax);
2503 if (assumption == const_true_rtx)
2504 goto zero_iter_simplify;
2505 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2506 iv0.base, const1_rtx);
2508 else
2510 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2511 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2512 mode_mmin);
2513 if (assumption == const_true_rtx)
2514 goto zero_iter_simplify;
2515 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2516 iv1.base, constm1_rtx);
2519 if (assumption != const0_rtx)
2520 desc->noloop_assumptions =
2521 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2522 cond = (cond == LT) ? LE : LEU;
2524 /* It will be useful to be able to tell the difference once more in
2525 LE -> NE reduction. */
2526 was_sharp = true;
2527 break;
2528 default: ;
2531 /* Take care of trivially infinite loops. */
2532 if (cond != NE)
2534 if (iv0.step == const0_rtx)
2536 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2537 if (rtx_equal_p (tmp, mode_mmin))
2539 desc->infinite =
2540 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2541 /* Fill in the remaining fields somehow. */
2542 goto zero_iter_simplify;
2545 else
2547 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2548 if (rtx_equal_p (tmp, mode_mmax))
2550 desc->infinite =
2551 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2552 /* Fill in the remaining fields somehow. */
2553 goto zero_iter_simplify;
2558 /* If we can we want to take care of NE conditions instead of size
2559 comparisons, as they are much more friendly (most importantly
2560 this takes care of special handling of loops with step 1). We can
2561 do it if we first check that upper bound is greater or equal to
2562 lower bound, their difference is constant c modulo step and that
2563 there is not an overflow. */
2564 if (cond != NE)
2566 if (iv0.step == const0_rtx)
2567 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2568 else
2569 step = iv0.step;
2570 step = lowpart_subreg (mode, step, comp_mode);
2571 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2572 delta = lowpart_subreg (mode, delta, comp_mode);
2573 delta = simplify_gen_binary (UMOD, mode, delta, step);
2574 may_xform = const0_rtx;
2575 may_not_xform = const_true_rtx;
2577 if (CONST_INT_P (delta))
2579 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2581 /* A special case. We have transformed condition of type
2582 for (i = 0; i < 4; i += 4)
2583 into
2584 for (i = 0; i <= 3; i += 4)
2585 obviously if the test for overflow during that transformation
2586 passed, we cannot overflow here. Most importantly any
2587 loop with sharp end condition and step 1 falls into this
2588 category, so handling this case specially is definitely
2589 worth the troubles. */
2590 may_xform = const_true_rtx;
2592 else if (iv0.step == const0_rtx)
2594 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2595 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
2596 bound = lowpart_subreg (mode, bound, comp_mode);
2597 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2598 may_xform = simplify_gen_relational (cond, SImode, mode,
2599 bound, tmp);
2600 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2601 SImode, mode,
2602 bound, tmp);
2604 else
2606 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2607 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
2608 bound = lowpart_subreg (mode, bound, comp_mode);
2609 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2610 may_xform = simplify_gen_relational (cond, SImode, mode,
2611 tmp, bound);
2612 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2613 SImode, mode,
2614 tmp, bound);
2618 if (may_xform != const0_rtx)
2620 /* We perform the transformation always provided that it is not
2621 completely senseless. This is OK, as we would need this assumption
2622 to determine the number of iterations anyway. */
2623 if (may_xform != const_true_rtx)
2625 /* If the step is a power of two and the final value we have
2626 computed overflows, the cycle is infinite. Otherwise it
2627 is nontrivial to compute the number of iterations. */
2628 if (step_is_pow2)
2629 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
2630 desc->infinite);
2631 else
2632 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2633 desc->assumptions);
2636 /* We are going to lose some information about upper bound on
2637 number of iterations in this step, so record the information
2638 here. */
2639 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
2640 if (CONST_INT_P (iv1.base))
2641 up = INTVAL (iv1.base);
2642 else
2643 up = INTVAL (mode_mmax) - inc;
2644 down = INTVAL (CONST_INT_P (iv0.base)
2645 ? iv0.base
2646 : mode_mmin);
2647 max = (uint64_t) (up - down) / inc + 1;
2648 if (!desc->infinite
2649 && !desc->assumptions)
2650 record_niter_bound (loop, max, false, true);
2652 if (iv0.step == const0_rtx)
2654 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2655 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2657 else
2659 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2660 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2663 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2664 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2665 assumption = simplify_gen_relational (reverse_condition (cond),
2666 SImode, mode, tmp0, tmp1);
2667 if (assumption == const_true_rtx)
2668 goto zero_iter_simplify;
2669 else if (assumption != const0_rtx)
2670 desc->noloop_assumptions =
2671 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2672 cond = NE;
2676 /* Count the number of iterations. */
2677 if (cond == NE)
2679 /* Everything we do here is just arithmetics modulo size of mode. This
2680 makes us able to do more involved computations of number of iterations
2681 than in other cases. First transform the condition into shape
2682 s * i <> c, with s positive. */
2683 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2684 iv0.base = const0_rtx;
2685 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2686 iv1.step = const0_rtx;
2687 if (INTVAL (iv0.step) < 0)
2689 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
2690 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
2692 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2694 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2695 is infinite. Otherwise, the number of iterations is
2696 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2697 s = INTVAL (iv0.step); d = 1;
2698 while (s % 2 != 1)
2700 s /= 2;
2701 d *= 2;
2702 size--;
2704 bound = GEN_INT (((uint64_t) 1 << (size - 1 ) << 1) - 1);
2706 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2707 tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
2708 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2709 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2711 tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
2712 inv = inverse (s, size);
2713 tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
2714 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2716 else
2718 if (iv1.step == const0_rtx)
2719 /* Condition in shape a + s * i <= b
2720 We must know that b + s does not overflow and a <= b + s and then we
2721 can compute number of iterations as (b + s - a) / s. (It might
2722 seem that we in fact could be more clever about testing the b + s
2723 overflow condition using some information about b - a mod s,
2724 but it was already taken into account during LE -> NE transform). */
2726 step = iv0.step;
2727 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2728 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2730 bound = simplify_gen_binary (MINUS, mode, mode_mmax,
2731 lowpart_subreg (mode, step,
2732 comp_mode));
2733 if (step_is_pow2)
2735 rtx t0, t1;
2737 /* If s is power of 2, we know that the loop is infinite if
2738 a % s <= b % s and b + s overflows. */
2739 assumption = simplify_gen_relational (reverse_condition (cond),
2740 SImode, mode,
2741 tmp1, bound);
2743 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2744 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2745 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2746 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2747 desc->infinite =
2748 alloc_EXPR_LIST (0, assumption, desc->infinite);
2750 else
2752 assumption = simplify_gen_relational (cond, SImode, mode,
2753 tmp1, bound);
2754 desc->assumptions =
2755 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2758 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
2759 tmp = lowpart_subreg (mode, tmp, comp_mode);
2760 assumption = simplify_gen_relational (reverse_condition (cond),
2761 SImode, mode, tmp0, tmp);
2763 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2764 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2766 else
2768 /* Condition in shape a <= b - s * i
2769 We must know that a - s does not overflow and a - s <= b and then
2770 we can again compute number of iterations as (b - (a - s)) / s. */
2771 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
2772 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2773 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2775 bound = simplify_gen_binary (PLUS, mode, mode_mmin,
2776 lowpart_subreg (mode, step, comp_mode));
2777 if (step_is_pow2)
2779 rtx t0, t1;
2781 /* If s is power of 2, we know that the loop is infinite if
2782 a % s <= b % s and a - s overflows. */
2783 assumption = simplify_gen_relational (reverse_condition (cond),
2784 SImode, mode,
2785 bound, tmp0);
2787 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2788 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2789 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2790 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2791 desc->infinite =
2792 alloc_EXPR_LIST (0, assumption, desc->infinite);
2794 else
2796 assumption = simplify_gen_relational (cond, SImode, mode,
2797 bound, tmp0);
2798 desc->assumptions =
2799 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2802 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
2803 tmp = lowpart_subreg (mode, tmp, comp_mode);
2804 assumption = simplify_gen_relational (reverse_condition (cond),
2805 SImode, mode,
2806 tmp, tmp1);
2807 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2808 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2810 if (assumption == const_true_rtx)
2811 goto zero_iter_simplify;
2812 else if (assumption != const0_rtx)
2813 desc->noloop_assumptions =
2814 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2815 delta = simplify_gen_binary (UDIV, mode, delta, step);
2816 desc->niter_expr = delta;
2819 old_niter = desc->niter_expr;
2821 simplify_using_initial_values (loop, AND, &desc->assumptions);
2822 if (desc->assumptions
2823 && XEXP (desc->assumptions, 0) == const0_rtx)
2824 goto fail;
2825 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2826 simplify_using_initial_values (loop, IOR, &desc->infinite);
2827 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2829 /* Rerun the simplification. Consider code (created by copying loop headers)
2831 i = 0;
2833 if (0 < n)
2837 i++;
2838 } while (i < n);
2841 The first pass determines that i = 0, the second pass uses it to eliminate
2842 noloop assumption. */
2844 simplify_using_initial_values (loop, AND, &desc->assumptions);
2845 if (desc->assumptions
2846 && XEXP (desc->assumptions, 0) == const0_rtx)
2847 goto fail;
2848 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2849 simplify_using_initial_values (loop, IOR, &desc->infinite);
2850 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2852 if (desc->noloop_assumptions
2853 && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
2854 goto zero_iter;
2856 if (CONST_INT_P (desc->niter_expr))
2858 uint64_t val = INTVAL (desc->niter_expr);
2860 desc->const_iter = true;
2861 desc->niter = val & GET_MODE_MASK (desc->mode);
2862 if (!desc->infinite
2863 && !desc->assumptions)
2864 record_niter_bound (loop, desc->niter, false, true);
2866 else
2868 max = determine_max_iter (loop, desc, old_niter);
2869 if (!max)
2870 goto zero_iter_simplify;
2871 if (!desc->infinite
2872 && !desc->assumptions)
2873 record_niter_bound (loop, max, false, true);
2875 /* simplify_using_initial_values does a copy propagation on the registers
2876 in the expression for the number of iterations. This prolongs life
2877 ranges of registers and increases register pressure, and usually
2878 brings no gain (and if it happens to do, the cse pass will take care
2879 of it anyway). So prevent this behavior, unless it enabled us to
2880 derive that the number of iterations is a constant. */
2881 desc->niter_expr = old_niter;
2884 return;
2886 zero_iter_simplify:
2887 /* Simplify the assumptions. */
2888 simplify_using_initial_values (loop, AND, &desc->assumptions);
2889 if (desc->assumptions
2890 && XEXP (desc->assumptions, 0) == const0_rtx)
2891 goto fail;
2892 simplify_using_initial_values (loop, IOR, &desc->infinite);
2894 /* Fallthru. */
2895 zero_iter:
2896 desc->const_iter = true;
2897 desc->niter = 0;
2898 record_niter_bound (loop, 0, true, true);
2899 desc->noloop_assumptions = NULL_RTX;
2900 desc->niter_expr = const0_rtx;
2901 return;
2903 fail:
2904 desc->simple_p = false;
2905 return;
2908 /* Checks whether E is a simple exit from LOOP and stores its description
2909 into DESC. */
2911 static void
2912 check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2914 basic_block exit_bb;
2915 rtx condition;
2916 rtx_insn *at;
2917 edge ein;
2919 exit_bb = e->src;
2920 desc->simple_p = false;
2922 /* It must belong directly to the loop. */
2923 if (exit_bb->loop_father != loop)
2924 return;
2926 /* It must be tested (at least) once during any iteration. */
2927 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
2928 return;
2930 /* It must end in a simple conditional jump. */
2931 if (!any_condjump_p (BB_END (exit_bb)))
2932 return;
2934 ein = EDGE_SUCC (exit_bb, 0);
2935 if (ein == e)
2936 ein = EDGE_SUCC (exit_bb, 1);
2938 desc->out_edge = e;
2939 desc->in_edge = ein;
2941 /* Test whether the condition is suitable. */
2942 if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
2943 return;
2945 if (ein->flags & EDGE_FALLTHRU)
2947 condition = reversed_condition (condition);
2948 if (!condition)
2949 return;
2952 /* Check that we are able to determine number of iterations and fill
2953 in information about it. */
2954 iv_number_of_iterations (loop, at, condition, desc);
2957 /* Finds a simple exit of LOOP and stores its description into DESC. */
2959 void
2960 find_simple_exit (struct loop *loop, struct niter_desc *desc)
2962 unsigned i;
2963 basic_block *body;
2964 edge e;
2965 struct niter_desc act;
2966 bool any = false;
2967 edge_iterator ei;
2969 desc->simple_p = false;
2970 body = get_loop_body (loop);
2972 for (i = 0; i < loop->num_nodes; i++)
2974 FOR_EACH_EDGE (e, ei, body[i]->succs)
2976 if (flow_bb_inside_loop_p (loop, e->dest))
2977 continue;
2979 check_simple_exit (loop, e, &act);
2980 if (!act.simple_p)
2981 continue;
2983 if (!any)
2984 any = true;
2985 else
2987 /* Prefer constant iterations; the less the better. */
2988 if (!act.const_iter
2989 || (desc->const_iter && act.niter >= desc->niter))
2990 continue;
2992 /* Also if the actual exit may be infinite, while the old one
2993 not, prefer the old one. */
2994 if (act.infinite && !desc->infinite)
2995 continue;
2998 *desc = act;
3002 if (dump_file)
3004 if (desc->simple_p)
3006 fprintf (dump_file, "Loop %d is simple:\n", loop->num);
3007 fprintf (dump_file, " simple exit %d -> %d\n",
3008 desc->out_edge->src->index,
3009 desc->out_edge->dest->index);
3010 if (desc->assumptions)
3012 fprintf (dump_file, " assumptions: ");
3013 print_rtl (dump_file, desc->assumptions);
3014 fprintf (dump_file, "\n");
3016 if (desc->noloop_assumptions)
3018 fprintf (dump_file, " does not roll if: ");
3019 print_rtl (dump_file, desc->noloop_assumptions);
3020 fprintf (dump_file, "\n");
3022 if (desc->infinite)
3024 fprintf (dump_file, " infinite if: ");
3025 print_rtl (dump_file, desc->infinite);
3026 fprintf (dump_file, "\n");
3029 fprintf (dump_file, " number of iterations: ");
3030 print_rtl (dump_file, desc->niter_expr);
3031 fprintf (dump_file, "\n");
3033 fprintf (dump_file, " upper bound: %li\n",
3034 (long)get_max_loop_iterations_int (loop));
3035 fprintf (dump_file, " realistic bound: %li\n",
3036 (long)get_estimated_loop_iterations_int (loop));
3038 else
3039 fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
3042 free (body);
3045 /* Creates a simple loop description of LOOP if it was not computed
3046 already. */
3048 struct niter_desc *
3049 get_simple_loop_desc (struct loop *loop)
3051 struct niter_desc *desc = simple_loop_desc (loop);
3053 if (desc)
3054 return desc;
3056 /* At least desc->infinite is not always initialized by
3057 find_simple_loop_exit. */
3058 desc = ggc_cleared_alloc<niter_desc> ();
3059 iv_analysis_loop_init (loop);
3060 find_simple_exit (loop, desc);
3061 loop->simple_loop_desc = desc;
3063 if (desc->simple_p && (desc->assumptions || desc->infinite))
3065 const char *wording;
3067 /* Assume that no overflow happens and that the loop is finite.
3068 We already warned at the tree level if we ran optimizations there. */
3069 if (!flag_tree_loop_optimize && warn_unsafe_loop_optimizations)
3071 if (desc->infinite)
3073 wording =
3074 flag_unsafe_loop_optimizations
3075 ? N_("assuming that the loop is not infinite")
3076 : N_("cannot optimize possibly infinite loops");
3077 warning (OPT_Wunsafe_loop_optimizations, "%s",
3078 gettext (wording));
3080 if (desc->assumptions)
3082 wording =
3083 flag_unsafe_loop_optimizations
3084 ? N_("assuming that the loop counter does not overflow")
3085 : N_("cannot optimize loop, the loop counter may overflow");
3086 warning (OPT_Wunsafe_loop_optimizations, "%s",
3087 gettext (wording));
3091 if (flag_unsafe_loop_optimizations)
3093 desc->assumptions = NULL_RTX;
3094 desc->infinite = NULL_RTX;
3098 return desc;
3101 /* Releases simple loop description for LOOP. */
3103 void
3104 free_simple_loop_desc (struct loop *loop)
3106 struct niter_desc *desc = simple_loop_desc (loop);
3108 if (!desc)
3109 return;
3111 ggc_free (desc);
3112 loop->simple_loop_desc = NULL;