IA MCU psABI support: changes to libraries
[gcc.git] / gcc / loop-iv.c
1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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.
24
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.
29
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.
35
36 The available functions are:
37
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 */
49
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 "function.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "basic-block.h"
62 #include "cfgloop.h"
63 #include "symtab.h"
64 #include "flags.h"
65 #include "alias.h"
66 #include "tree.h"
67 #include "insn-config.h"
68 #include "expmed.h"
69 #include "dojump.h"
70 #include "explow.h"
71 #include "calls.h"
72 #include "emit-rtl.h"
73 #include "varasm.h"
74 #include "stmt.h"
75 #include "expr.h"
76 #include "intl.h"
77 #include "diagnostic-core.h"
78 #include "df.h"
79 #include "dumpfile.h"
80 #include "rtl-iter.h"
81
82 /* Possible return values of iv_get_reaching_def. */
83
84 enum iv_grd_result
85 {
86 /* More than one reaching def, or reaching def that does not
87 dominate the use. */
88 GRD_INVALID,
89
90 /* The use is trivial invariant of the loop, i.e. is not changed
91 inside the loop. */
92 GRD_INVARIANT,
93
94 /* The use is reached by initial value and a value from the
95 previous iteration. */
96 GRD_MAYBE_BIV,
97
98 /* The use has single dominating def. */
99 GRD_SINGLE_DOM
100 };
101
102 /* Information about a biv. */
103
104 struct biv_entry
105 {
106 unsigned regno; /* The register of the biv. */
107 struct rtx_iv iv; /* Value of the biv. */
108 };
109
110 static bool clean_slate = true;
111
112 static unsigned int iv_ref_table_size = 0;
113
114 /* Table of rtx_ivs indexed by the df_ref uid field. */
115 static struct rtx_iv ** iv_ref_table;
116
117 /* Induction variable stored at the reference. */
118 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
119 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
120
121 /* The current loop. */
122
123 static struct loop *current_loop;
124
125 /* Hashtable helper. */
126
127 struct biv_entry_hasher : free_ptr_hash <biv_entry>
128 {
129 typedef rtx_def *compare_type;
130 static inline hashval_t hash (const biv_entry *);
131 static inline bool equal (const biv_entry *, const rtx_def *);
132 };
133
134 /* Returns hash value for biv B. */
135
136 inline hashval_t
137 biv_entry_hasher::hash (const biv_entry *b)
138 {
139 return b->regno;
140 }
141
142 /* Compares biv B and register R. */
143
144 inline bool
145 biv_entry_hasher::equal (const biv_entry *b, const rtx_def *r)
146 {
147 return b->regno == REGNO (r);
148 }
149
150 /* Bivs of the current loop. */
151
152 static hash_table<biv_entry_hasher> *bivs;
153
154 static bool iv_analyze_op (rtx_insn *, rtx, struct rtx_iv *);
155
156 /* Return the RTX code corresponding to the IV extend code EXTEND. */
157 static inline enum rtx_code
158 iv_extend_to_rtx_code (enum iv_extend_code extend)
159 {
160 switch (extend)
161 {
162 case IV_SIGN_EXTEND:
163 return SIGN_EXTEND;
164 case IV_ZERO_EXTEND:
165 return ZERO_EXTEND;
166 case IV_UNKNOWN_EXTEND:
167 return UNKNOWN;
168 }
169 gcc_unreachable ();
170 }
171
172 /* Dumps information about IV to FILE. */
173
174 extern void dump_iv_info (FILE *, struct rtx_iv *);
175 void
176 dump_iv_info (FILE *file, struct rtx_iv *iv)
177 {
178 if (!iv->base)
179 {
180 fprintf (file, "not simple");
181 return;
182 }
183
184 if (iv->step == const0_rtx
185 && !iv->first_special)
186 fprintf (file, "invariant ");
187
188 print_rtl (file, iv->base);
189 if (iv->step != const0_rtx)
190 {
191 fprintf (file, " + ");
192 print_rtl (file, iv->step);
193 fprintf (file, " * iteration");
194 }
195 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
196
197 if (iv->mode != iv->extend_mode)
198 fprintf (file, " %s to %s",
199 rtx_name[iv_extend_to_rtx_code (iv->extend)],
200 GET_MODE_NAME (iv->extend_mode));
201
202 if (iv->mult != const1_rtx)
203 {
204 fprintf (file, " * ");
205 print_rtl (file, iv->mult);
206 }
207 if (iv->delta != const0_rtx)
208 {
209 fprintf (file, " + ");
210 print_rtl (file, iv->delta);
211 }
212 if (iv->first_special)
213 fprintf (file, " (first special)");
214 }
215
216 /* Generates a subreg to get the least significant part of EXPR (in mode
217 INNER_MODE) to OUTER_MODE. */
218
219 rtx
220 lowpart_subreg (machine_mode outer_mode, rtx expr,
221 machine_mode inner_mode)
222 {
223 return simplify_gen_subreg (outer_mode, expr, inner_mode,
224 subreg_lowpart_offset (outer_mode, inner_mode));
225 }
226
227 static void
228 check_iv_ref_table_size (void)
229 {
230 if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
231 {
232 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
233 iv_ref_table = XRESIZEVEC (struct rtx_iv *, iv_ref_table, new_size);
234 memset (&iv_ref_table[iv_ref_table_size], 0,
235 (new_size - iv_ref_table_size) * sizeof (struct rtx_iv *));
236 iv_ref_table_size = new_size;
237 }
238 }
239
240
241 /* Checks whether REG is a well-behaved register. */
242
243 static bool
244 simple_reg_p (rtx reg)
245 {
246 unsigned r;
247
248 if (GET_CODE (reg) == SUBREG)
249 {
250 if (!subreg_lowpart_p (reg))
251 return false;
252 reg = SUBREG_REG (reg);
253 }
254
255 if (!REG_P (reg))
256 return false;
257
258 r = REGNO (reg);
259 if (HARD_REGISTER_NUM_P (r))
260 return false;
261
262 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
263 return false;
264
265 return true;
266 }
267
268 /* Clears the information about ivs stored in df. */
269
270 static void
271 clear_iv_info (void)
272 {
273 unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
274 struct rtx_iv *iv;
275
276 check_iv_ref_table_size ();
277 for (i = 0; i < n_defs; i++)
278 {
279 iv = iv_ref_table[i];
280 if (iv)
281 {
282 free (iv);
283 iv_ref_table[i] = NULL;
284 }
285 }
286
287 bivs->empty ();
288 }
289
290
291 /* Prepare the data for an induction variable analysis of a LOOP. */
292
293 void
294 iv_analysis_loop_init (struct loop *loop)
295 {
296 current_loop = loop;
297
298 /* Clear the information from the analysis of the previous loop. */
299 if (clean_slate)
300 {
301 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
302 bivs = new hash_table<biv_entry_hasher> (10);
303 clean_slate = false;
304 }
305 else
306 clear_iv_info ();
307
308 /* Get rid of the ud chains before processing the rescans. Then add
309 the problem back. */
310 df_remove_problem (df_chain);
311 df_process_deferred_rescans ();
312 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
313 df_chain_add_problem (DF_UD_CHAIN);
314 df_note_add_problem ();
315 df_analyze_loop (loop);
316 if (dump_file)
317 df_dump_region (dump_file);
318
319 check_iv_ref_table_size ();
320 }
321
322 /* Finds the definition of REG that dominates loop latch and stores
323 it to DEF. Returns false if there is not a single definition
324 dominating the latch. If REG has no definition in loop, DEF
325 is set to NULL and true is returned. */
326
327 static bool
328 latch_dominating_def (rtx reg, df_ref *def)
329 {
330 df_ref single_rd = NULL, adef;
331 unsigned regno = REGNO (reg);
332 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
333
334 for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
335 {
336 if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
337 || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
338 continue;
339
340 /* More than one reaching definition. */
341 if (single_rd)
342 return false;
343
344 if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
345 return false;
346
347 single_rd = adef;
348 }
349
350 *def = single_rd;
351 return true;
352 }
353
354 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
355
356 static enum iv_grd_result
357 iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
358 {
359 df_ref use, adef;
360 basic_block def_bb, use_bb;
361 rtx_insn *def_insn;
362 bool dom_p;
363
364 *def = NULL;
365 if (!simple_reg_p (reg))
366 return GRD_INVALID;
367 if (GET_CODE (reg) == SUBREG)
368 reg = SUBREG_REG (reg);
369 gcc_assert (REG_P (reg));
370
371 use = df_find_use (insn, reg);
372 gcc_assert (use != NULL);
373
374 if (!DF_REF_CHAIN (use))
375 return GRD_INVARIANT;
376
377 /* More than one reaching def. */
378 if (DF_REF_CHAIN (use)->next)
379 return GRD_INVALID;
380
381 adef = DF_REF_CHAIN (use)->ref;
382
383 /* We do not handle setting only part of the register. */
384 if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
385 return GRD_INVALID;
386
387 def_insn = DF_REF_INSN (adef);
388 def_bb = DF_REF_BB (adef);
389 use_bb = BLOCK_FOR_INSN (insn);
390
391 if (use_bb == def_bb)
392 dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
393 else
394 dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
395
396 if (dom_p)
397 {
398 *def = adef;
399 return GRD_SINGLE_DOM;
400 }
401
402 /* The definition does not dominate the use. This is still OK if
403 this may be a use of a biv, i.e. if the def_bb dominates loop
404 latch. */
405 if (just_once_each_iteration_p (current_loop, def_bb))
406 return GRD_MAYBE_BIV;
407
408 return GRD_INVALID;
409 }
410
411 /* Sets IV to invariant CST in MODE. Always returns true (just for
412 consistency with other iv manipulation functions that may fail). */
413
414 static bool
415 iv_constant (struct rtx_iv *iv, rtx cst, machine_mode mode)
416 {
417 if (mode == VOIDmode)
418 mode = GET_MODE (cst);
419
420 iv->mode = mode;
421 iv->base = cst;
422 iv->step = const0_rtx;
423 iv->first_special = false;
424 iv->extend = IV_UNKNOWN_EXTEND;
425 iv->extend_mode = iv->mode;
426 iv->delta = const0_rtx;
427 iv->mult = const1_rtx;
428
429 return true;
430 }
431
432 /* Evaluates application of subreg to MODE on IV. */
433
434 static bool
435 iv_subreg (struct rtx_iv *iv, machine_mode mode)
436 {
437 /* If iv is invariant, just calculate the new value. */
438 if (iv->step == const0_rtx
439 && !iv->first_special)
440 {
441 rtx val = get_iv_value (iv, const0_rtx);
442 val = lowpart_subreg (mode, val,
443 iv->extend == IV_UNKNOWN_EXTEND
444 ? iv->mode : iv->extend_mode);
445
446 iv->base = val;
447 iv->extend = IV_UNKNOWN_EXTEND;
448 iv->mode = iv->extend_mode = mode;
449 iv->delta = const0_rtx;
450 iv->mult = const1_rtx;
451 return true;
452 }
453
454 if (iv->extend_mode == mode)
455 return true;
456
457 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
458 return false;
459
460 iv->extend = IV_UNKNOWN_EXTEND;
461 iv->mode = mode;
462
463 iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
464 simplify_gen_binary (MULT, iv->extend_mode,
465 iv->base, iv->mult));
466 iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
467 iv->mult = const1_rtx;
468 iv->delta = const0_rtx;
469 iv->first_special = false;
470
471 return true;
472 }
473
474 /* Evaluates application of EXTEND to MODE on IV. */
475
476 static bool
477 iv_extend (struct rtx_iv *iv, enum iv_extend_code extend, machine_mode mode)
478 {
479 /* If iv is invariant, just calculate the new value. */
480 if (iv->step == const0_rtx
481 && !iv->first_special)
482 {
483 rtx val = get_iv_value (iv, const0_rtx);
484 if (iv->extend_mode != iv->mode
485 && iv->extend != IV_UNKNOWN_EXTEND
486 && iv->extend != extend)
487 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
488 val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
489 val,
490 iv->extend == extend
491 ? iv->extend_mode : iv->mode);
492 iv->base = val;
493 iv->extend = IV_UNKNOWN_EXTEND;
494 iv->mode = iv->extend_mode = mode;
495 iv->delta = const0_rtx;
496 iv->mult = const1_rtx;
497 return true;
498 }
499
500 if (mode != iv->extend_mode)
501 return false;
502
503 if (iv->extend != IV_UNKNOWN_EXTEND
504 && iv->extend != extend)
505 return false;
506
507 iv->extend = extend;
508
509 return true;
510 }
511
512 /* Evaluates negation of IV. */
513
514 static bool
515 iv_neg (struct rtx_iv *iv)
516 {
517 if (iv->extend == IV_UNKNOWN_EXTEND)
518 {
519 iv->base = simplify_gen_unary (NEG, iv->extend_mode,
520 iv->base, iv->extend_mode);
521 iv->step = simplify_gen_unary (NEG, iv->extend_mode,
522 iv->step, iv->extend_mode);
523 }
524 else
525 {
526 iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
527 iv->delta, iv->extend_mode);
528 iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
529 iv->mult, iv->extend_mode);
530 }
531
532 return true;
533 }
534
535 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
536
537 static bool
538 iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
539 {
540 machine_mode mode;
541 rtx arg;
542
543 /* Extend the constant to extend_mode of the other operand if necessary. */
544 if (iv0->extend == IV_UNKNOWN_EXTEND
545 && iv0->mode == iv0->extend_mode
546 && iv0->step == const0_rtx
547 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
548 {
549 iv0->extend_mode = iv1->extend_mode;
550 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
551 iv0->base, iv0->mode);
552 }
553 if (iv1->extend == IV_UNKNOWN_EXTEND
554 && iv1->mode == iv1->extend_mode
555 && iv1->step == const0_rtx
556 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
557 {
558 iv1->extend_mode = iv0->extend_mode;
559 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
560 iv1->base, iv1->mode);
561 }
562
563 mode = iv0->extend_mode;
564 if (mode != iv1->extend_mode)
565 return false;
566
567 if (iv0->extend == IV_UNKNOWN_EXTEND
568 && iv1->extend == IV_UNKNOWN_EXTEND)
569 {
570 if (iv0->mode != iv1->mode)
571 return false;
572
573 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
574 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
575
576 return true;
577 }
578
579 /* Handle addition of constant. */
580 if (iv1->extend == IV_UNKNOWN_EXTEND
581 && iv1->mode == mode
582 && iv1->step == const0_rtx)
583 {
584 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
585 return true;
586 }
587
588 if (iv0->extend == IV_UNKNOWN_EXTEND
589 && iv0->mode == mode
590 && iv0->step == const0_rtx)
591 {
592 arg = iv0->base;
593 *iv0 = *iv1;
594 if (op == MINUS
595 && !iv_neg (iv0))
596 return false;
597
598 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
599 return true;
600 }
601
602 return false;
603 }
604
605 /* Evaluates multiplication of IV by constant CST. */
606
607 static bool
608 iv_mult (struct rtx_iv *iv, rtx mby)
609 {
610 machine_mode mode = iv->extend_mode;
611
612 if (GET_MODE (mby) != VOIDmode
613 && GET_MODE (mby) != mode)
614 return false;
615
616 if (iv->extend == IV_UNKNOWN_EXTEND)
617 {
618 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
619 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
620 }
621 else
622 {
623 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
624 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
625 }
626
627 return true;
628 }
629
630 /* Evaluates shift of IV by constant CST. */
631
632 static bool
633 iv_shift (struct rtx_iv *iv, rtx mby)
634 {
635 machine_mode mode = iv->extend_mode;
636
637 if (GET_MODE (mby) != VOIDmode
638 && GET_MODE (mby) != mode)
639 return false;
640
641 if (iv->extend == IV_UNKNOWN_EXTEND)
642 {
643 iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
644 iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
645 }
646 else
647 {
648 iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
649 iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
650 }
651
652 return true;
653 }
654
655 /* The recursive part of get_biv_step. Gets the value of the single value
656 defined by DEF wrto initial value of REG inside loop, in shape described
657 at get_biv_step. */
658
659 static bool
660 get_biv_step_1 (df_ref def, rtx reg,
661 rtx *inner_step, machine_mode *inner_mode,
662 enum iv_extend_code *extend, machine_mode outer_mode,
663 rtx *outer_step)
664 {
665 rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
666 rtx next, nextr;
667 enum rtx_code code;
668 rtx_insn *insn = DF_REF_INSN (def);
669 df_ref next_def;
670 enum iv_grd_result res;
671
672 set = single_set (insn);
673 if (!set)
674 return false;
675
676 rhs = find_reg_equal_equiv_note (insn);
677 if (rhs)
678 rhs = XEXP (rhs, 0);
679 else
680 rhs = SET_SRC (set);
681
682 code = GET_CODE (rhs);
683 switch (code)
684 {
685 case SUBREG:
686 case REG:
687 next = rhs;
688 break;
689
690 case PLUS:
691 case MINUS:
692 op0 = XEXP (rhs, 0);
693 op1 = XEXP (rhs, 1);
694
695 if (code == PLUS && CONSTANT_P (op0))
696 std::swap (op0, op1);
697
698 if (!simple_reg_p (op0)
699 || !CONSTANT_P (op1))
700 return false;
701
702 if (GET_MODE (rhs) != outer_mode)
703 {
704 /* ppc64 uses expressions like
705
706 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
707
708 this is equivalent to
709
710 (set x':DI (plus:DI y:DI 1))
711 (set x:SI (subreg:SI (x':DI)). */
712 if (GET_CODE (op0) != SUBREG)
713 return false;
714 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
715 return false;
716 }
717
718 next = op0;
719 break;
720
721 case SIGN_EXTEND:
722 case ZERO_EXTEND:
723 if (GET_MODE (rhs) != outer_mode)
724 return false;
725
726 op0 = XEXP (rhs, 0);
727 if (!simple_reg_p (op0))
728 return false;
729
730 next = op0;
731 break;
732
733 default:
734 return false;
735 }
736
737 if (GET_CODE (next) == SUBREG)
738 {
739 if (!subreg_lowpart_p (next))
740 return false;
741
742 nextr = SUBREG_REG (next);
743 if (GET_MODE (nextr) != outer_mode)
744 return false;
745 }
746 else
747 nextr = next;
748
749 res = iv_get_reaching_def (insn, nextr, &next_def);
750
751 if (res == GRD_INVALID || res == GRD_INVARIANT)
752 return false;
753
754 if (res == GRD_MAYBE_BIV)
755 {
756 if (!rtx_equal_p (nextr, reg))
757 return false;
758
759 *inner_step = const0_rtx;
760 *extend = IV_UNKNOWN_EXTEND;
761 *inner_mode = outer_mode;
762 *outer_step = const0_rtx;
763 }
764 else if (!get_biv_step_1 (next_def, reg,
765 inner_step, inner_mode, extend, outer_mode,
766 outer_step))
767 return false;
768
769 if (GET_CODE (next) == SUBREG)
770 {
771 machine_mode amode = GET_MODE (next);
772
773 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
774 return false;
775
776 *inner_mode = amode;
777 *inner_step = simplify_gen_binary (PLUS, outer_mode,
778 *inner_step, *outer_step);
779 *outer_step = const0_rtx;
780 *extend = IV_UNKNOWN_EXTEND;
781 }
782
783 switch (code)
784 {
785 case REG:
786 case SUBREG:
787 break;
788
789 case PLUS:
790 case MINUS:
791 if (*inner_mode == outer_mode
792 /* See comment in previous switch. */
793 || GET_MODE (rhs) != outer_mode)
794 *inner_step = simplify_gen_binary (code, outer_mode,
795 *inner_step, op1);
796 else
797 *outer_step = simplify_gen_binary (code, outer_mode,
798 *outer_step, op1);
799 break;
800
801 case SIGN_EXTEND:
802 case ZERO_EXTEND:
803 gcc_assert (GET_MODE (op0) == *inner_mode
804 && *extend == IV_UNKNOWN_EXTEND
805 && *outer_step == const0_rtx);
806
807 *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
808 break;
809
810 default:
811 return false;
812 }
813
814 return true;
815 }
816
817 /* Gets the operation on register REG inside loop, in shape
818
819 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
820
821 If the operation cannot be described in this shape, return false.
822 LAST_DEF is the definition of REG that dominates loop latch. */
823
824 static bool
825 get_biv_step (df_ref last_def, rtx reg, rtx *inner_step,
826 machine_mode *inner_mode, enum iv_extend_code *extend,
827 machine_mode *outer_mode, rtx *outer_step)
828 {
829 *outer_mode = GET_MODE (reg);
830
831 if (!get_biv_step_1 (last_def, reg,
832 inner_step, inner_mode, extend, *outer_mode,
833 outer_step))
834 return false;
835
836 gcc_assert ((*inner_mode == *outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
837 gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
838
839 return true;
840 }
841
842 /* Records information that DEF is induction variable IV. */
843
844 static void
845 record_iv (df_ref def, struct rtx_iv *iv)
846 {
847 struct rtx_iv *recorded_iv = XNEW (struct rtx_iv);
848
849 *recorded_iv = *iv;
850 check_iv_ref_table_size ();
851 DF_REF_IV_SET (def, recorded_iv);
852 }
853
854 /* If DEF was already analyzed for bivness, store the description of the biv to
855 IV and return true. Otherwise return false. */
856
857 static bool
858 analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
859 {
860 struct biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
861
862 if (!biv)
863 return false;
864
865 *iv = biv->iv;
866 return true;
867 }
868
869 static void
870 record_biv (rtx def, struct rtx_iv *iv)
871 {
872 struct biv_entry *biv = XNEW (struct biv_entry);
873 biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
874
875 biv->regno = REGNO (def);
876 biv->iv = *iv;
877 gcc_assert (!*slot);
878 *slot = biv;
879 }
880
881 /* Determines whether DEF is a biv and if so, stores its description
882 to *IV. */
883
884 static bool
885 iv_analyze_biv (rtx def, struct rtx_iv *iv)
886 {
887 rtx inner_step, outer_step;
888 machine_mode inner_mode, outer_mode;
889 enum iv_extend_code extend;
890 df_ref last_def;
891
892 if (dump_file)
893 {
894 fprintf (dump_file, "Analyzing ");
895 print_rtl (dump_file, def);
896 fprintf (dump_file, " for bivness.\n");
897 }
898
899 if (!REG_P (def))
900 {
901 if (!CONSTANT_P (def))
902 return false;
903
904 return iv_constant (iv, def, VOIDmode);
905 }
906
907 if (!latch_dominating_def (def, &last_def))
908 {
909 if (dump_file)
910 fprintf (dump_file, " not simple.\n");
911 return false;
912 }
913
914 if (!last_def)
915 return iv_constant (iv, def, VOIDmode);
916
917 if (analyzed_for_bivness_p (def, iv))
918 {
919 if (dump_file)
920 fprintf (dump_file, " already analysed.\n");
921 return iv->base != NULL_RTX;
922 }
923
924 if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
925 &outer_mode, &outer_step))
926 {
927 iv->base = NULL_RTX;
928 goto end;
929 }
930
931 /* Loop transforms base to es (base + inner_step) + outer_step,
932 where es means extend of subreg between inner_mode and outer_mode.
933 The corresponding induction variable is
934
935 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
936
937 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
938 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
939 iv->mode = inner_mode;
940 iv->extend_mode = outer_mode;
941 iv->extend = extend;
942 iv->mult = const1_rtx;
943 iv->delta = outer_step;
944 iv->first_special = inner_mode != outer_mode;
945
946 end:
947 if (dump_file)
948 {
949 fprintf (dump_file, " ");
950 dump_iv_info (dump_file, iv);
951 fprintf (dump_file, "\n");
952 }
953
954 record_biv (def, iv);
955 return iv->base != NULL_RTX;
956 }
957
958 /* Analyzes expression RHS used at INSN and stores the result to *IV.
959 The mode of the induction variable is MODE. */
960
961 bool
962 iv_analyze_expr (rtx_insn *insn, rtx rhs, machine_mode mode,
963 struct rtx_iv *iv)
964 {
965 rtx mby = NULL_RTX;
966 rtx op0 = NULL_RTX, op1 = NULL_RTX;
967 struct rtx_iv iv0, iv1;
968 enum rtx_code code = GET_CODE (rhs);
969 machine_mode omode = mode;
970
971 iv->mode = VOIDmode;
972 iv->base = NULL_RTX;
973 iv->step = NULL_RTX;
974
975 gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
976
977 if (CONSTANT_P (rhs)
978 || REG_P (rhs)
979 || code == SUBREG)
980 {
981 if (!iv_analyze_op (insn, rhs, iv))
982 return false;
983
984 if (iv->mode == VOIDmode)
985 {
986 iv->mode = mode;
987 iv->extend_mode = mode;
988 }
989
990 return true;
991 }
992
993 switch (code)
994 {
995 case REG:
996 op0 = rhs;
997 break;
998
999 case SIGN_EXTEND:
1000 case ZERO_EXTEND:
1001 case NEG:
1002 op0 = XEXP (rhs, 0);
1003 omode = GET_MODE (op0);
1004 break;
1005
1006 case PLUS:
1007 case MINUS:
1008 op0 = XEXP (rhs, 0);
1009 op1 = XEXP (rhs, 1);
1010 break;
1011
1012 case MULT:
1013 op0 = XEXP (rhs, 0);
1014 mby = XEXP (rhs, 1);
1015 if (!CONSTANT_P (mby))
1016 std::swap (op0, mby);
1017 if (!CONSTANT_P (mby))
1018 return false;
1019 break;
1020
1021 case ASHIFT:
1022 op0 = XEXP (rhs, 0);
1023 mby = XEXP (rhs, 1);
1024 if (!CONSTANT_P (mby))
1025 return false;
1026 break;
1027
1028 default:
1029 return false;
1030 }
1031
1032 if (op0
1033 && !iv_analyze_expr (insn, op0, omode, &iv0))
1034 return false;
1035
1036 if (op1
1037 && !iv_analyze_expr (insn, op1, omode, &iv1))
1038 return false;
1039
1040 switch (code)
1041 {
1042 case SIGN_EXTEND:
1043 if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
1044 return false;
1045 break;
1046
1047 case ZERO_EXTEND:
1048 if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
1049 return false;
1050 break;
1051
1052 case NEG:
1053 if (!iv_neg (&iv0))
1054 return false;
1055 break;
1056
1057 case PLUS:
1058 case MINUS:
1059 if (!iv_add (&iv0, &iv1, code))
1060 return false;
1061 break;
1062
1063 case MULT:
1064 if (!iv_mult (&iv0, mby))
1065 return false;
1066 break;
1067
1068 case ASHIFT:
1069 if (!iv_shift (&iv0, mby))
1070 return false;
1071 break;
1072
1073 default:
1074 break;
1075 }
1076
1077 *iv = iv0;
1078 return iv->base != NULL_RTX;
1079 }
1080
1081 /* Analyzes iv DEF and stores the result to *IV. */
1082
1083 static bool
1084 iv_analyze_def (df_ref def, struct rtx_iv *iv)
1085 {
1086 rtx_insn *insn = DF_REF_INSN (def);
1087 rtx reg = DF_REF_REG (def);
1088 rtx set, rhs;
1089
1090 if (dump_file)
1091 {
1092 fprintf (dump_file, "Analyzing def of ");
1093 print_rtl (dump_file, reg);
1094 fprintf (dump_file, " in insn ");
1095 print_rtl_single (dump_file, insn);
1096 }
1097
1098 check_iv_ref_table_size ();
1099 if (DF_REF_IV (def))
1100 {
1101 if (dump_file)
1102 fprintf (dump_file, " already analysed.\n");
1103 *iv = *DF_REF_IV (def);
1104 return iv->base != NULL_RTX;
1105 }
1106
1107 iv->mode = VOIDmode;
1108 iv->base = NULL_RTX;
1109 iv->step = NULL_RTX;
1110
1111 if (!REG_P (reg))
1112 return false;
1113
1114 set = single_set (insn);
1115 if (!set)
1116 return false;
1117
1118 if (!REG_P (SET_DEST (set)))
1119 return false;
1120
1121 gcc_assert (SET_DEST (set) == reg);
1122 rhs = find_reg_equal_equiv_note (insn);
1123 if (rhs)
1124 rhs = XEXP (rhs, 0);
1125 else
1126 rhs = SET_SRC (set);
1127
1128 iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
1129 record_iv (def, iv);
1130
1131 if (dump_file)
1132 {
1133 print_rtl (dump_file, reg);
1134 fprintf (dump_file, " in insn ");
1135 print_rtl_single (dump_file, insn);
1136 fprintf (dump_file, " is ");
1137 dump_iv_info (dump_file, iv);
1138 fprintf (dump_file, "\n");
1139 }
1140
1141 return iv->base != NULL_RTX;
1142 }
1143
1144 /* Analyzes operand OP of INSN and stores the result to *IV. */
1145
1146 static bool
1147 iv_analyze_op (rtx_insn *insn, rtx op, struct rtx_iv *iv)
1148 {
1149 df_ref def = NULL;
1150 enum iv_grd_result res;
1151
1152 if (dump_file)
1153 {
1154 fprintf (dump_file, "Analyzing operand ");
1155 print_rtl (dump_file, op);
1156 fprintf (dump_file, " of insn ");
1157 print_rtl_single (dump_file, insn);
1158 }
1159
1160 if (function_invariant_p (op))
1161 res = GRD_INVARIANT;
1162 else if (GET_CODE (op) == SUBREG)
1163 {
1164 if (!subreg_lowpart_p (op))
1165 return false;
1166
1167 if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
1168 return false;
1169
1170 return iv_subreg (iv, GET_MODE (op));
1171 }
1172 else
1173 {
1174 res = iv_get_reaching_def (insn, op, &def);
1175 if (res == GRD_INVALID)
1176 {
1177 if (dump_file)
1178 fprintf (dump_file, " not simple.\n");
1179 return false;
1180 }
1181 }
1182
1183 if (res == GRD_INVARIANT)
1184 {
1185 iv_constant (iv, op, VOIDmode);
1186
1187 if (dump_file)
1188 {
1189 fprintf (dump_file, " ");
1190 dump_iv_info (dump_file, iv);
1191 fprintf (dump_file, "\n");
1192 }
1193 return true;
1194 }
1195
1196 if (res == GRD_MAYBE_BIV)
1197 return iv_analyze_biv (op, iv);
1198
1199 return iv_analyze_def (def, iv);
1200 }
1201
1202 /* Analyzes value VAL at INSN and stores the result to *IV. */
1203
1204 bool
1205 iv_analyze (rtx_insn *insn, rtx val, struct rtx_iv *iv)
1206 {
1207 rtx reg;
1208
1209 /* We must find the insn in that val is used, so that we get to UD chains.
1210 Since the function is sometimes called on result of get_condition,
1211 this does not necessarily have to be directly INSN; scan also the
1212 following insns. */
1213 if (simple_reg_p (val))
1214 {
1215 if (GET_CODE (val) == SUBREG)
1216 reg = SUBREG_REG (val);
1217 else
1218 reg = val;
1219
1220 while (!df_find_use (insn, reg))
1221 insn = NEXT_INSN (insn);
1222 }
1223
1224 return iv_analyze_op (insn, val, iv);
1225 }
1226
1227 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1228
1229 bool
1230 iv_analyze_result (rtx_insn *insn, rtx def, struct rtx_iv *iv)
1231 {
1232 df_ref adef;
1233
1234 adef = df_find_def (insn, def);
1235 if (!adef)
1236 return false;
1237
1238 return iv_analyze_def (adef, iv);
1239 }
1240
1241 /* Checks whether definition of register REG in INSN is a basic induction
1242 variable. IV analysis must have been initialized (via a call to
1243 iv_analysis_loop_init) for this function to produce a result. */
1244
1245 bool
1246 biv_p (rtx_insn *insn, rtx reg)
1247 {
1248 struct rtx_iv iv;
1249 df_ref def, last_def;
1250
1251 if (!simple_reg_p (reg))
1252 return false;
1253
1254 def = df_find_def (insn, reg);
1255 gcc_assert (def != NULL);
1256 if (!latch_dominating_def (reg, &last_def))
1257 return false;
1258 if (last_def != def)
1259 return false;
1260
1261 if (!iv_analyze_biv (reg, &iv))
1262 return false;
1263
1264 return iv.step != const0_rtx;
1265 }
1266
1267 /* Calculates value of IV at ITERATION-th iteration. */
1268
1269 rtx
1270 get_iv_value (struct rtx_iv *iv, rtx iteration)
1271 {
1272 rtx val;
1273
1274 /* We would need to generate some if_then_else patterns, and so far
1275 it is not needed anywhere. */
1276 gcc_assert (!iv->first_special);
1277
1278 if (iv->step != const0_rtx && iteration != const0_rtx)
1279 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1280 simplify_gen_binary (MULT, iv->extend_mode,
1281 iv->step, iteration));
1282 else
1283 val = iv->base;
1284
1285 if (iv->extend_mode == iv->mode)
1286 return val;
1287
1288 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
1289
1290 if (iv->extend == IV_UNKNOWN_EXTEND)
1291 return val;
1292
1293 val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
1294 iv->extend_mode, val, iv->mode);
1295 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1296 simplify_gen_binary (MULT, iv->extend_mode,
1297 iv->mult, val));
1298
1299 return val;
1300 }
1301
1302 /* Free the data for an induction variable analysis. */
1303
1304 void
1305 iv_analysis_done (void)
1306 {
1307 if (!clean_slate)
1308 {
1309 clear_iv_info ();
1310 clean_slate = true;
1311 df_finish_pass (true);
1312 delete bivs;
1313 bivs = NULL;
1314 free (iv_ref_table);
1315 iv_ref_table = NULL;
1316 iv_ref_table_size = 0;
1317 }
1318 }
1319
1320 /* Computes inverse to X modulo (1 << MOD). */
1321
1322 static uint64_t
1323 inverse (uint64_t x, int mod)
1324 {
1325 uint64_t mask =
1326 ((uint64_t) 1 << (mod - 1) << 1) - 1;
1327 uint64_t rslt = 1;
1328 int i;
1329
1330 for (i = 0; i < mod - 1; i++)
1331 {
1332 rslt = (rslt * x) & mask;
1333 x = (x * x) & mask;
1334 }
1335
1336 return rslt;
1337 }
1338
1339 /* Checks whether any register in X is in set ALT. */
1340
1341 static bool
1342 altered_reg_used (const_rtx x, bitmap alt)
1343 {
1344 subrtx_iterator::array_type array;
1345 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
1346 {
1347 const_rtx x = *iter;
1348 if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
1349 return true;
1350 }
1351 return false;
1352 }
1353
1354 /* Marks registers altered by EXPR in set ALT. */
1355
1356 static void
1357 mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
1358 {
1359 if (GET_CODE (expr) == SUBREG)
1360 expr = SUBREG_REG (expr);
1361 if (!REG_P (expr))
1362 return;
1363
1364 SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
1365 }
1366
1367 /* Checks whether RHS is simple enough to process. */
1368
1369 static bool
1370 simple_rhs_p (rtx rhs)
1371 {
1372 rtx op0, op1;
1373
1374 if (function_invariant_p (rhs)
1375 || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
1376 return true;
1377
1378 switch (GET_CODE (rhs))
1379 {
1380 case PLUS:
1381 case MINUS:
1382 case AND:
1383 op0 = XEXP (rhs, 0);
1384 op1 = XEXP (rhs, 1);
1385 /* Allow reg OP const and reg OP reg. */
1386 if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
1387 && !function_invariant_p (op0))
1388 return false;
1389 if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
1390 && !function_invariant_p (op1))
1391 return false;
1392
1393 return true;
1394
1395 case ASHIFT:
1396 case ASHIFTRT:
1397 case LSHIFTRT:
1398 case MULT:
1399 op0 = XEXP (rhs, 0);
1400 op1 = XEXP (rhs, 1);
1401 /* Allow reg OP const. */
1402 if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
1403 return false;
1404 if (!function_invariant_p (op1))
1405 return false;
1406
1407 return true;
1408
1409 default:
1410 return false;
1411 }
1412 }
1413
1414 /* If REGNO has a single definition, return its known value, otherwise return
1415 null. */
1416
1417 static rtx
1418 find_single_def_src (unsigned int regno)
1419 {
1420 df_ref adef;
1421 rtx set, src;
1422
1423 for (;;)
1424 {
1425 rtx note;
1426 adef = DF_REG_DEF_CHAIN (regno);
1427 if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL
1428 || DF_REF_IS_ARTIFICIAL (adef))
1429 return NULL_RTX;
1430
1431 set = single_set (DF_REF_INSN (adef));
1432 if (set == NULL || !REG_P (SET_DEST (set))
1433 || REGNO (SET_DEST (set)) != regno)
1434 return NULL_RTX;
1435
1436 note = find_reg_equal_equiv_note (DF_REF_INSN (adef));
1437
1438 if (note && function_invariant_p (XEXP (note, 0)))
1439 {
1440 src = XEXP (note, 0);
1441 break;
1442 }
1443 src = SET_SRC (set);
1444
1445 if (REG_P (src))
1446 {
1447 regno = REGNO (src);
1448 continue;
1449 }
1450 break;
1451 }
1452 if (!function_invariant_p (src))
1453 return NULL_RTX;
1454
1455 return src;
1456 }
1457
1458 /* If any registers in *EXPR that have a single definition, try to replace
1459 them with the known-equivalent values. */
1460
1461 static void
1462 replace_single_def_regs (rtx *expr)
1463 {
1464 subrtx_var_iterator::array_type array;
1465 repeat:
1466 FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
1467 {
1468 rtx x = *iter;
1469 if (REG_P (x))
1470 if (rtx new_x = find_single_def_src (REGNO (x)))
1471 {
1472 *expr = simplify_replace_rtx (*expr, x, new_x);
1473 goto repeat;
1474 }
1475 }
1476 }
1477
1478 /* A subroutine of simplify_using_initial_values, this function examines INSN
1479 to see if it contains a suitable set that we can use to make a replacement.
1480 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1481 the set; return false otherwise. */
1482
1483 static bool
1484 suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
1485 {
1486 rtx set = single_set (insn);
1487 rtx lhs = NULL_RTX, rhs;
1488
1489 if (!set)
1490 return false;
1491
1492 lhs = SET_DEST (set);
1493 if (!REG_P (lhs))
1494 return false;
1495
1496 rhs = find_reg_equal_equiv_note (insn);
1497 if (rhs)
1498 rhs = XEXP (rhs, 0);
1499 else
1500 rhs = SET_SRC (set);
1501
1502 if (!simple_rhs_p (rhs))
1503 return false;
1504
1505 *dest = lhs;
1506 *src = rhs;
1507 return true;
1508 }
1509
1510 /* Using the data returned by suitable_set_for_replacement, replace DEST
1511 with SRC in *EXPR and return the new expression. Also call
1512 replace_single_def_regs if the replacement changed something. */
1513 static void
1514 replace_in_expr (rtx *expr, rtx dest, rtx src)
1515 {
1516 rtx old = *expr;
1517 *expr = simplify_replace_rtx (*expr, dest, src);
1518 if (old == *expr)
1519 return;
1520 replace_single_def_regs (expr);
1521 }
1522
1523 /* Checks whether A implies B. */
1524
1525 static bool
1526 implies_p (rtx a, rtx b)
1527 {
1528 rtx op0, op1, opb0, opb1;
1529 machine_mode mode;
1530
1531 if (rtx_equal_p (a, b))
1532 return true;
1533
1534 if (GET_CODE (a) == EQ)
1535 {
1536 op0 = XEXP (a, 0);
1537 op1 = XEXP (a, 1);
1538
1539 if (REG_P (op0)
1540 || (GET_CODE (op0) == SUBREG
1541 && REG_P (SUBREG_REG (op0))))
1542 {
1543 rtx r = simplify_replace_rtx (b, op0, op1);
1544 if (r == const_true_rtx)
1545 return true;
1546 }
1547
1548 if (REG_P (op1)
1549 || (GET_CODE (op1) == SUBREG
1550 && REG_P (SUBREG_REG (op1))))
1551 {
1552 rtx r = simplify_replace_rtx (b, op1, op0);
1553 if (r == const_true_rtx)
1554 return true;
1555 }
1556 }
1557
1558 if (b == const_true_rtx)
1559 return true;
1560
1561 if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
1562 && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
1563 || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
1564 && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
1565 return false;
1566
1567 op0 = XEXP (a, 0);
1568 op1 = XEXP (a, 1);
1569 opb0 = XEXP (b, 0);
1570 opb1 = XEXP (b, 1);
1571
1572 mode = GET_MODE (op0);
1573 if (mode != GET_MODE (opb0))
1574 mode = VOIDmode;
1575 else if (mode == VOIDmode)
1576 {
1577 mode = GET_MODE (op1);
1578 if (mode != GET_MODE (opb1))
1579 mode = VOIDmode;
1580 }
1581
1582 /* A < B implies A + 1 <= B. */
1583 if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
1584 && (GET_CODE (b) == GE || GET_CODE (b) == LE))
1585 {
1586
1587 if (GET_CODE (a) == GT)
1588 std::swap (op0, op1);
1589
1590 if (GET_CODE (b) == GE)
1591 std::swap (opb0, opb1);
1592
1593 if (SCALAR_INT_MODE_P (mode)
1594 && rtx_equal_p (op1, opb1)
1595 && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
1596 return true;
1597 return false;
1598 }
1599
1600 /* A < B or A > B imply A != B. TODO: Likewise
1601 A + n < B implies A != B + n if neither wraps. */
1602 if (GET_CODE (b) == NE
1603 && (GET_CODE (a) == GT || GET_CODE (a) == GTU
1604 || GET_CODE (a) == LT || GET_CODE (a) == LTU))
1605 {
1606 if (rtx_equal_p (op0, opb0)
1607 && rtx_equal_p (op1, opb1))
1608 return true;
1609 }
1610
1611 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1612 if (GET_CODE (a) == NE
1613 && op1 == const0_rtx)
1614 {
1615 if ((GET_CODE (b) == GTU
1616 && opb1 == const0_rtx)
1617 || (GET_CODE (b) == GEU
1618 && opb1 == const1_rtx))
1619 return rtx_equal_p (op0, opb0);
1620 }
1621
1622 /* A != N is equivalent to A - (N + 1) <u -1. */
1623 if (GET_CODE (a) == NE
1624 && CONST_INT_P (op1)
1625 && GET_CODE (b) == LTU
1626 && opb1 == constm1_rtx
1627 && GET_CODE (opb0) == PLUS
1628 && CONST_INT_P (XEXP (opb0, 1))
1629 /* Avoid overflows. */
1630 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1631 != ((unsigned HOST_WIDE_INT)1
1632 << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1633 && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
1634 return rtx_equal_p (op0, XEXP (opb0, 0));
1635
1636 /* Likewise, A != N implies A - N > 0. */
1637 if (GET_CODE (a) == NE
1638 && CONST_INT_P (op1))
1639 {
1640 if (GET_CODE (b) == GTU
1641 && GET_CODE (opb0) == PLUS
1642 && opb1 == const0_rtx
1643 && CONST_INT_P (XEXP (opb0, 1))
1644 /* Avoid overflows. */
1645 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1646 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1647 && rtx_equal_p (XEXP (opb0, 0), op0))
1648 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1649 if (GET_CODE (b) == GEU
1650 && GET_CODE (opb0) == PLUS
1651 && opb1 == const1_rtx
1652 && CONST_INT_P (XEXP (opb0, 1))
1653 /* Avoid overflows. */
1654 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1655 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1656 && rtx_equal_p (XEXP (opb0, 0), op0))
1657 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1658 }
1659
1660 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1661 if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
1662 && CONST_INT_P (op1)
1663 && ((GET_CODE (a) == GT && op1 == constm1_rtx)
1664 || INTVAL (op1) >= 0)
1665 && GET_CODE (b) == LTU
1666 && CONST_INT_P (opb1)
1667 && rtx_equal_p (op0, opb0))
1668 return INTVAL (opb1) < 0;
1669
1670 return false;
1671 }
1672
1673 /* Canonicalizes COND so that
1674
1675 (1) Ensure that operands are ordered according to
1676 swap_commutative_operands_p.
1677 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1678 for GE, GEU, and LEU. */
1679
1680 rtx
1681 canon_condition (rtx cond)
1682 {
1683 rtx op0, op1;
1684 enum rtx_code code;
1685 machine_mode mode;
1686
1687 code = GET_CODE (cond);
1688 op0 = XEXP (cond, 0);
1689 op1 = XEXP (cond, 1);
1690
1691 if (swap_commutative_operands_p (op0, op1))
1692 {
1693 code = swap_condition (code);
1694 std::swap (op0, op1);
1695 }
1696
1697 mode = GET_MODE (op0);
1698 if (mode == VOIDmode)
1699 mode = GET_MODE (op1);
1700 gcc_assert (mode != VOIDmode);
1701
1702 if (CONST_SCALAR_INT_P (op1) && GET_MODE_CLASS (mode) != MODE_CC)
1703 {
1704 rtx_mode_t const_val (op1, mode);
1705
1706 switch (code)
1707 {
1708 case LE:
1709 if (wi::ne_p (const_val, wi::max_value (mode, SIGNED)))
1710 {
1711 code = LT;
1712 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1713 }
1714 break;
1715
1716 case GE:
1717 if (wi::ne_p (const_val, wi::min_value (mode, SIGNED)))
1718 {
1719 code = GT;
1720 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1721 }
1722 break;
1723
1724 case LEU:
1725 if (wi::ne_p (const_val, -1))
1726 {
1727 code = LTU;
1728 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1729 }
1730 break;
1731
1732 case GEU:
1733 if (wi::ne_p (const_val, 0))
1734 {
1735 code = GTU;
1736 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1737 }
1738 break;
1739
1740 default:
1741 break;
1742 }
1743 }
1744
1745 if (op0 != XEXP (cond, 0)
1746 || op1 != XEXP (cond, 1)
1747 || code != GET_CODE (cond)
1748 || GET_MODE (cond) != SImode)
1749 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1750
1751 return cond;
1752 }
1753
1754 /* Reverses CONDition; returns NULL if we cannot. */
1755
1756 static rtx
1757 reversed_condition (rtx cond)
1758 {
1759 enum rtx_code reversed;
1760 reversed = reversed_comparison_code (cond, NULL);
1761 if (reversed == UNKNOWN)
1762 return NULL_RTX;
1763 else
1764 return gen_rtx_fmt_ee (reversed,
1765 GET_MODE (cond), XEXP (cond, 0),
1766 XEXP (cond, 1));
1767 }
1768
1769 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1770 set of altered regs. */
1771
1772 void
1773 simplify_using_condition (rtx cond, rtx *expr, regset altered)
1774 {
1775 rtx rev, reve, exp = *expr;
1776
1777 /* If some register gets altered later, we do not really speak about its
1778 value at the time of comparison. */
1779 if (altered && altered_reg_used (cond, altered))
1780 return;
1781
1782 if (GET_CODE (cond) == EQ
1783 && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
1784 {
1785 *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
1786 return;
1787 }
1788
1789 if (!COMPARISON_P (exp))
1790 return;
1791
1792 rev = reversed_condition (cond);
1793 reve = reversed_condition (exp);
1794
1795 cond = canon_condition (cond);
1796 exp = canon_condition (exp);
1797 if (rev)
1798 rev = canon_condition (rev);
1799 if (reve)
1800 reve = canon_condition (reve);
1801
1802 if (rtx_equal_p (exp, cond))
1803 {
1804 *expr = const_true_rtx;
1805 return;
1806 }
1807
1808 if (rev && rtx_equal_p (exp, rev))
1809 {
1810 *expr = const0_rtx;
1811 return;
1812 }
1813
1814 if (implies_p (cond, exp))
1815 {
1816 *expr = const_true_rtx;
1817 return;
1818 }
1819
1820 if (reve && implies_p (cond, reve))
1821 {
1822 *expr = const0_rtx;
1823 return;
1824 }
1825
1826 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1827 be false. */
1828 if (rev && implies_p (exp, rev))
1829 {
1830 *expr = const0_rtx;
1831 return;
1832 }
1833
1834 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1835 if (rev && reve && implies_p (reve, rev))
1836 {
1837 *expr = const_true_rtx;
1838 return;
1839 }
1840
1841 /* We would like to have some other tests here. TODO. */
1842
1843 return;
1844 }
1845
1846 /* Use relationship between A and *B to eventually eliminate *B.
1847 OP is the operation we consider. */
1848
1849 static void
1850 eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1851 {
1852 switch (op)
1853 {
1854 case AND:
1855 /* If A implies *B, we may replace *B by true. */
1856 if (implies_p (a, *b))
1857 *b = const_true_rtx;
1858 break;
1859
1860 case IOR:
1861 /* If *B implies A, we may replace *B by false. */
1862 if (implies_p (*b, a))
1863 *b = const0_rtx;
1864 break;
1865
1866 default:
1867 gcc_unreachable ();
1868 }
1869 }
1870
1871 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1872 operation we consider. */
1873
1874 static void
1875 eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1876 {
1877 rtx elt;
1878
1879 for (elt = tail; elt; elt = XEXP (elt, 1))
1880 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1881 for (elt = tail; elt; elt = XEXP (elt, 1))
1882 eliminate_implied_condition (op, XEXP (elt, 0), head);
1883 }
1884
1885 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1886 is a list, its elements are assumed to be combined using OP. */
1887
1888 static void
1889 simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1890 {
1891 bool expression_valid;
1892 rtx head, tail, last_valid_expr;
1893 rtx_expr_list *cond_list;
1894 rtx_insn *insn;
1895 rtx neutral, aggr;
1896 regset altered, this_altered;
1897 edge e;
1898
1899 if (!*expr)
1900 return;
1901
1902 if (CONSTANT_P (*expr))
1903 return;
1904
1905 if (GET_CODE (*expr) == EXPR_LIST)
1906 {
1907 head = XEXP (*expr, 0);
1908 tail = XEXP (*expr, 1);
1909
1910 eliminate_implied_conditions (op, &head, tail);
1911
1912 switch (op)
1913 {
1914 case AND:
1915 neutral = const_true_rtx;
1916 aggr = const0_rtx;
1917 break;
1918
1919 case IOR:
1920 neutral = const0_rtx;
1921 aggr = const_true_rtx;
1922 break;
1923
1924 default:
1925 gcc_unreachable ();
1926 }
1927
1928 simplify_using_initial_values (loop, UNKNOWN, &head);
1929 if (head == aggr)
1930 {
1931 XEXP (*expr, 0) = aggr;
1932 XEXP (*expr, 1) = NULL_RTX;
1933 return;
1934 }
1935 else if (head == neutral)
1936 {
1937 *expr = tail;
1938 simplify_using_initial_values (loop, op, expr);
1939 return;
1940 }
1941 simplify_using_initial_values (loop, op, &tail);
1942
1943 if (tail && XEXP (tail, 0) == aggr)
1944 {
1945 *expr = tail;
1946 return;
1947 }
1948
1949 XEXP (*expr, 0) = head;
1950 XEXP (*expr, 1) = tail;
1951 return;
1952 }
1953
1954 gcc_assert (op == UNKNOWN);
1955
1956 replace_single_def_regs (expr);
1957 if (CONSTANT_P (*expr))
1958 return;
1959
1960 e = loop_preheader_edge (loop);
1961 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1962 return;
1963
1964 altered = ALLOC_REG_SET (&reg_obstack);
1965 this_altered = ALLOC_REG_SET (&reg_obstack);
1966
1967 expression_valid = true;
1968 last_valid_expr = *expr;
1969 cond_list = NULL;
1970 while (1)
1971 {
1972 insn = BB_END (e->src);
1973 if (any_condjump_p (insn))
1974 {
1975 rtx cond = get_condition (BB_END (e->src), NULL, false, true);
1976
1977 if (cond && (e->flags & EDGE_FALLTHRU))
1978 cond = reversed_condition (cond);
1979 if (cond)
1980 {
1981 rtx old = *expr;
1982 simplify_using_condition (cond, expr, altered);
1983 if (old != *expr)
1984 {
1985 rtx note;
1986 if (CONSTANT_P (*expr))
1987 goto out;
1988 for (note = cond_list; note; note = XEXP (note, 1))
1989 {
1990 simplify_using_condition (XEXP (note, 0), expr, altered);
1991 if (CONSTANT_P (*expr))
1992 goto out;
1993 }
1994 }
1995 cond_list = alloc_EXPR_LIST (0, cond, cond_list);
1996 }
1997 }
1998
1999 FOR_BB_INSNS_REVERSE (e->src, insn)
2000 {
2001 rtx src, dest;
2002 rtx old = *expr;
2003
2004 if (!INSN_P (insn))
2005 continue;
2006
2007 CLEAR_REG_SET (this_altered);
2008 note_stores (PATTERN (insn), mark_altered, this_altered);
2009 if (CALL_P (insn))
2010 {
2011 /* Kill all call clobbered registers. */
2012 unsigned int i;
2013 hard_reg_set_iterator hrsi;
2014 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
2015 0, i, hrsi)
2016 SET_REGNO_REG_SET (this_altered, i);
2017 }
2018
2019 if (suitable_set_for_replacement (insn, &dest, &src))
2020 {
2021 rtx_expr_list **pnote, **pnote_next;
2022
2023 replace_in_expr (expr, dest, src);
2024 if (CONSTANT_P (*expr))
2025 goto out;
2026
2027 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2028 {
2029 rtx_expr_list *note = *pnote;
2030 rtx old_cond = XEXP (note, 0);
2031
2032 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2033 replace_in_expr (&XEXP (note, 0), dest, src);
2034
2035 /* We can no longer use a condition that has been simplified
2036 to a constant, and simplify_using_condition will abort if
2037 we try. */
2038 if (CONSTANT_P (XEXP (note, 0)))
2039 {
2040 *pnote = *pnote_next;
2041 pnote_next = pnote;
2042 free_EXPR_LIST_node (note);
2043 }
2044 /* Retry simplifications with this condition if either the
2045 expression or the condition changed. */
2046 else if (old_cond != XEXP (note, 0) || old != *expr)
2047 simplify_using_condition (XEXP (note, 0), expr, altered);
2048 }
2049 }
2050 else
2051 {
2052 rtx_expr_list **pnote, **pnote_next;
2053
2054 /* If we did not use this insn to make a replacement, any overlap
2055 between stores in this insn and our expression will cause the
2056 expression to become invalid. */
2057 if (altered_reg_used (*expr, this_altered))
2058 goto out;
2059
2060 /* Likewise for the conditions. */
2061 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2062 {
2063 rtx_expr_list *note = *pnote;
2064 rtx old_cond = XEXP (note, 0);
2065
2066 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2067 if (altered_reg_used (old_cond, this_altered))
2068 {
2069 *pnote = *pnote_next;
2070 pnote_next = pnote;
2071 free_EXPR_LIST_node (note);
2072 }
2073 }
2074 }
2075
2076 if (CONSTANT_P (*expr))
2077 goto out;
2078
2079 IOR_REG_SET (altered, this_altered);
2080
2081 /* If the expression now contains regs that have been altered, we
2082 can't return it to the caller. However, it is still valid for
2083 further simplification, so keep searching to see if we can
2084 eventually turn it into a constant. */
2085 if (altered_reg_used (*expr, altered))
2086 expression_valid = false;
2087 if (expression_valid)
2088 last_valid_expr = *expr;
2089 }
2090
2091 if (!single_pred_p (e->src)
2092 || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2093 break;
2094 e = single_pred_edge (e->src);
2095 }
2096
2097 out:
2098 free_EXPR_LIST_list (&cond_list);
2099 if (!CONSTANT_P (*expr))
2100 *expr = last_valid_expr;
2101 FREE_REG_SET (altered);
2102 FREE_REG_SET (this_altered);
2103 }
2104
2105 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2106 that IV occurs as left operands of comparison COND and its signedness
2107 is SIGNED_P to DESC. */
2108
2109 static void
2110 shorten_into_mode (struct rtx_iv *iv, machine_mode mode,
2111 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
2112 {
2113 rtx mmin, mmax, cond_over, cond_under;
2114
2115 get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
2116 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
2117 iv->base, mmin);
2118 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
2119 iv->base, mmax);
2120
2121 switch (cond)
2122 {
2123 case LE:
2124 case LT:
2125 case LEU:
2126 case LTU:
2127 if (cond_under != const0_rtx)
2128 desc->infinite =
2129 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2130 if (cond_over != const0_rtx)
2131 desc->noloop_assumptions =
2132 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
2133 break;
2134
2135 case GE:
2136 case GT:
2137 case GEU:
2138 case GTU:
2139 if (cond_over != const0_rtx)
2140 desc->infinite =
2141 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2142 if (cond_under != const0_rtx)
2143 desc->noloop_assumptions =
2144 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
2145 break;
2146
2147 case NE:
2148 if (cond_over != const0_rtx)
2149 desc->infinite =
2150 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2151 if (cond_under != const0_rtx)
2152 desc->infinite =
2153 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2154 break;
2155
2156 default:
2157 gcc_unreachable ();
2158 }
2159
2160 iv->mode = mode;
2161 iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
2162 }
2163
2164 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2165 subregs of the same mode if possible (sometimes it is necessary to add
2166 some assumptions to DESC). */
2167
2168 static bool
2169 canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
2170 enum rtx_code cond, struct niter_desc *desc)
2171 {
2172 machine_mode comp_mode;
2173 bool signed_p;
2174
2175 /* If the ivs behave specially in the first iteration, or are
2176 added/multiplied after extending, we ignore them. */
2177 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
2178 return false;
2179 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
2180 return false;
2181
2182 /* If there is some extend, it must match signedness of the comparison. */
2183 switch (cond)
2184 {
2185 case LE:
2186 case LT:
2187 if (iv0->extend == IV_ZERO_EXTEND
2188 || iv1->extend == IV_ZERO_EXTEND)
2189 return false;
2190 signed_p = true;
2191 break;
2192
2193 case LEU:
2194 case LTU:
2195 if (iv0->extend == IV_SIGN_EXTEND
2196 || iv1->extend == IV_SIGN_EXTEND)
2197 return false;
2198 signed_p = false;
2199 break;
2200
2201 case NE:
2202 if (iv0->extend != IV_UNKNOWN_EXTEND
2203 && iv1->extend != IV_UNKNOWN_EXTEND
2204 && iv0->extend != iv1->extend)
2205 return false;
2206
2207 signed_p = false;
2208 if (iv0->extend != IV_UNKNOWN_EXTEND)
2209 signed_p = iv0->extend == IV_SIGN_EXTEND;
2210 if (iv1->extend != IV_UNKNOWN_EXTEND)
2211 signed_p = iv1->extend == IV_SIGN_EXTEND;
2212 break;
2213
2214 default:
2215 gcc_unreachable ();
2216 }
2217
2218 /* Values of both variables should be computed in the same mode. These
2219 might indeed be different, if we have comparison like
2220
2221 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2222
2223 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2224 in different modes. This does not seem impossible to handle, but
2225 it hardly ever occurs in practice.
2226
2227 The only exception is the case when one of operands is invariant.
2228 For example pentium 3 generates comparisons like
2229 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2230 definitely do not want this prevent the optimization. */
2231 comp_mode = iv0->extend_mode;
2232 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
2233 comp_mode = iv1->extend_mode;
2234
2235 if (iv0->extend_mode != comp_mode)
2236 {
2237 if (iv0->mode != iv0->extend_mode
2238 || iv0->step != const0_rtx)
2239 return false;
2240
2241 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2242 comp_mode, iv0->base, iv0->mode);
2243 iv0->extend_mode = comp_mode;
2244 }
2245
2246 if (iv1->extend_mode != comp_mode)
2247 {
2248 if (iv1->mode != iv1->extend_mode
2249 || iv1->step != const0_rtx)
2250 return false;
2251
2252 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2253 comp_mode, iv1->base, iv1->mode);
2254 iv1->extend_mode = comp_mode;
2255 }
2256
2257 /* Check that both ivs belong to a range of a single mode. If one of the
2258 operands is an invariant, we may need to shorten it into the common
2259 mode. */
2260 if (iv0->mode == iv0->extend_mode
2261 && iv0->step == const0_rtx
2262 && iv0->mode != iv1->mode)
2263 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
2264
2265 if (iv1->mode == iv1->extend_mode
2266 && iv1->step == const0_rtx
2267 && iv0->mode != iv1->mode)
2268 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
2269
2270 if (iv0->mode != iv1->mode)
2271 return false;
2272
2273 desc->mode = iv0->mode;
2274 desc->signed_p = signed_p;
2275
2276 return true;
2277 }
2278
2279 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2280 result. This function is called from iv_number_of_iterations with
2281 a number of fields in DESC already filled in. OLD_NITER is the original
2282 expression for the number of iterations, before we tried to simplify it. */
2283
2284 static uint64_t
2285 determine_max_iter (struct loop *loop, struct niter_desc *desc, rtx old_niter)
2286 {
2287 rtx niter = desc->niter_expr;
2288 rtx mmin, mmax, cmp;
2289 uint64_t nmax, inc;
2290 uint64_t andmax = 0;
2291
2292 /* We used to look for constant operand 0 of AND,
2293 but canonicalization should always make this impossible. */
2294 gcc_checking_assert (GET_CODE (niter) != AND
2295 || !CONST_INT_P (XEXP (niter, 0)));
2296
2297 if (GET_CODE (niter) == AND
2298 && CONST_INT_P (XEXP (niter, 1)))
2299 {
2300 andmax = UINTVAL (XEXP (niter, 1));
2301 niter = XEXP (niter, 0);
2302 }
2303
2304 get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
2305 nmax = UINTVAL (mmax) - UINTVAL (mmin);
2306
2307 if (GET_CODE (niter) == UDIV)
2308 {
2309 if (!CONST_INT_P (XEXP (niter, 1)))
2310 return nmax;
2311 inc = INTVAL (XEXP (niter, 1));
2312 niter = XEXP (niter, 0);
2313 }
2314 else
2315 inc = 1;
2316
2317 /* We could use a binary search here, but for now improving the upper
2318 bound by just one eliminates one important corner case. */
2319 cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
2320 desc->mode, old_niter, mmax);
2321 simplify_using_initial_values (loop, UNKNOWN, &cmp);
2322 if (cmp == const_true_rtx)
2323 {
2324 nmax--;
2325
2326 if (dump_file)
2327 fprintf (dump_file, ";; improved upper bound by one.\n");
2328 }
2329 nmax /= inc;
2330 if (andmax)
2331 nmax = MIN (nmax, andmax);
2332 if (dump_file)
2333 fprintf (dump_file, ";; Determined upper bound %" PRId64".\n",
2334 nmax);
2335 return nmax;
2336 }
2337
2338 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2339 the result into DESC. Very similar to determine_number_of_iterations
2340 (basically its rtl version), complicated by things like subregs. */
2341
2342 static void
2343 iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
2344 struct niter_desc *desc)
2345 {
2346 rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
2347 struct rtx_iv iv0, iv1;
2348 rtx assumption, may_not_xform;
2349 enum rtx_code cond;
2350 machine_mode mode, comp_mode;
2351 rtx mmin, mmax, mode_mmin, mode_mmax;
2352 uint64_t s, size, d, inv, max;
2353 int64_t up, down, inc, step_val;
2354 int was_sharp = false;
2355 rtx old_niter;
2356 bool step_is_pow2;
2357
2358 /* The meaning of these assumptions is this:
2359 if !assumptions
2360 then the rest of information does not have to be valid
2361 if noloop_assumptions then the loop does not roll
2362 if infinite then this exit is never used */
2363
2364 desc->assumptions = NULL_RTX;
2365 desc->noloop_assumptions = NULL_RTX;
2366 desc->infinite = NULL_RTX;
2367 desc->simple_p = true;
2368
2369 desc->const_iter = false;
2370 desc->niter_expr = NULL_RTX;
2371
2372 cond = GET_CODE (condition);
2373 gcc_assert (COMPARISON_P (condition));
2374
2375 mode = GET_MODE (XEXP (condition, 0));
2376 if (mode == VOIDmode)
2377 mode = GET_MODE (XEXP (condition, 1));
2378 /* The constant comparisons should be folded. */
2379 gcc_assert (mode != VOIDmode);
2380
2381 /* We only handle integers or pointers. */
2382 if (GET_MODE_CLASS (mode) != MODE_INT
2383 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
2384 goto fail;
2385
2386 op0 = XEXP (condition, 0);
2387 if (!iv_analyze (insn, op0, &iv0))
2388 goto fail;
2389 if (iv0.extend_mode == VOIDmode)
2390 iv0.mode = iv0.extend_mode = mode;
2391
2392 op1 = XEXP (condition, 1);
2393 if (!iv_analyze (insn, op1, &iv1))
2394 goto fail;
2395 if (iv1.extend_mode == VOIDmode)
2396 iv1.mode = iv1.extend_mode = mode;
2397
2398 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
2399 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
2400 goto fail;
2401
2402 /* Check condition and normalize it. */
2403
2404 switch (cond)
2405 {
2406 case GE:
2407 case GT:
2408 case GEU:
2409 case GTU:
2410 std::swap (iv0, iv1);
2411 cond = swap_condition (cond);
2412 break;
2413 case NE:
2414 case LE:
2415 case LEU:
2416 case LT:
2417 case LTU:
2418 break;
2419 default:
2420 goto fail;
2421 }
2422
2423 /* Handle extends. This is relatively nontrivial, so we only try in some
2424 easy cases, when we can canonicalize the ivs (possibly by adding some
2425 assumptions) to shape subreg (base + i * step). This function also fills
2426 in desc->mode and desc->signed_p. */
2427
2428 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
2429 goto fail;
2430
2431 comp_mode = iv0.extend_mode;
2432 mode = iv0.mode;
2433 size = GET_MODE_PRECISION (mode);
2434 get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
2435 mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
2436 mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
2437
2438 if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
2439 goto fail;
2440
2441 /* We can take care of the case of two induction variables chasing each other
2442 if the test is NE. I have never seen a loop using it, but still it is
2443 cool. */
2444 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
2445 {
2446 if (cond != NE)
2447 goto fail;
2448
2449 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2450 iv1.step = const0_rtx;
2451 }
2452
2453 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2454 iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
2455
2456 /* This is either infinite loop or the one that ends immediately, depending
2457 on initial values. Unswitching should remove this kind of conditions. */
2458 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
2459 goto fail;
2460
2461 if (cond != NE)
2462 {
2463 if (iv0.step == const0_rtx)
2464 step_val = -INTVAL (iv1.step);
2465 else
2466 step_val = INTVAL (iv0.step);
2467
2468 /* Ignore loops of while (i-- < 10) type. */
2469 if (step_val < 0)
2470 goto fail;
2471
2472 step_is_pow2 = !(step_val & (step_val - 1));
2473 }
2474 else
2475 {
2476 /* We do not care about whether the step is power of two in this
2477 case. */
2478 step_is_pow2 = false;
2479 step_val = 0;
2480 }
2481
2482 /* Some more condition normalization. We must record some assumptions
2483 due to overflows. */
2484 switch (cond)
2485 {
2486 case LT:
2487 case LTU:
2488 /* We want to take care only of non-sharp relationals; this is easy,
2489 as in cases the overflow would make the transformation unsafe
2490 the loop does not roll. Seemingly it would make more sense to want
2491 to take care of sharp relationals instead, as NE is more similar to
2492 them, but the problem is that here the transformation would be more
2493 difficult due to possibly infinite loops. */
2494 if (iv0.step == const0_rtx)
2495 {
2496 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2497 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2498 mode_mmax);
2499 if (assumption == const_true_rtx)
2500 goto zero_iter_simplify;
2501 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2502 iv0.base, const1_rtx);
2503 }
2504 else
2505 {
2506 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2507 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2508 mode_mmin);
2509 if (assumption == const_true_rtx)
2510 goto zero_iter_simplify;
2511 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2512 iv1.base, constm1_rtx);
2513 }
2514
2515 if (assumption != const0_rtx)
2516 desc->noloop_assumptions =
2517 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2518 cond = (cond == LT) ? LE : LEU;
2519
2520 /* It will be useful to be able to tell the difference once more in
2521 LE -> NE reduction. */
2522 was_sharp = true;
2523 break;
2524 default: ;
2525 }
2526
2527 /* Take care of trivially infinite loops. */
2528 if (cond != NE)
2529 {
2530 if (iv0.step == const0_rtx)
2531 {
2532 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2533 if (rtx_equal_p (tmp, mode_mmin))
2534 {
2535 desc->infinite =
2536 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2537 /* Fill in the remaining fields somehow. */
2538 goto zero_iter_simplify;
2539 }
2540 }
2541 else
2542 {
2543 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2544 if (rtx_equal_p (tmp, mode_mmax))
2545 {
2546 desc->infinite =
2547 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2548 /* Fill in the remaining fields somehow. */
2549 goto zero_iter_simplify;
2550 }
2551 }
2552 }
2553
2554 /* If we can we want to take care of NE conditions instead of size
2555 comparisons, as they are much more friendly (most importantly
2556 this takes care of special handling of loops with step 1). We can
2557 do it if we first check that upper bound is greater or equal to
2558 lower bound, their difference is constant c modulo step and that
2559 there is not an overflow. */
2560 if (cond != NE)
2561 {
2562 if (iv0.step == const0_rtx)
2563 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2564 else
2565 step = iv0.step;
2566 step = lowpart_subreg (mode, step, comp_mode);
2567 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2568 delta = lowpart_subreg (mode, delta, comp_mode);
2569 delta = simplify_gen_binary (UMOD, mode, delta, step);
2570 may_xform = const0_rtx;
2571 may_not_xform = const_true_rtx;
2572
2573 if (CONST_INT_P (delta))
2574 {
2575 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2576 {
2577 /* A special case. We have transformed condition of type
2578 for (i = 0; i < 4; i += 4)
2579 into
2580 for (i = 0; i <= 3; i += 4)
2581 obviously if the test for overflow during that transformation
2582 passed, we cannot overflow here. Most importantly any
2583 loop with sharp end condition and step 1 falls into this
2584 category, so handling this case specially is definitely
2585 worth the troubles. */
2586 may_xform = const_true_rtx;
2587 }
2588 else if (iv0.step == const0_rtx)
2589 {
2590 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2591 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
2592 bound = lowpart_subreg (mode, bound, comp_mode);
2593 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2594 may_xform = simplify_gen_relational (cond, SImode, mode,
2595 bound, tmp);
2596 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2597 SImode, mode,
2598 bound, tmp);
2599 }
2600 else
2601 {
2602 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2603 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
2604 bound = lowpart_subreg (mode, bound, comp_mode);
2605 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2606 may_xform = simplify_gen_relational (cond, SImode, mode,
2607 tmp, bound);
2608 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2609 SImode, mode,
2610 tmp, bound);
2611 }
2612 }
2613
2614 if (may_xform != const0_rtx)
2615 {
2616 /* We perform the transformation always provided that it is not
2617 completely senseless. This is OK, as we would need this assumption
2618 to determine the number of iterations anyway. */
2619 if (may_xform != const_true_rtx)
2620 {
2621 /* If the step is a power of two and the final value we have
2622 computed overflows, the cycle is infinite. Otherwise it
2623 is nontrivial to compute the number of iterations. */
2624 if (step_is_pow2)
2625 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
2626 desc->infinite);
2627 else
2628 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2629 desc->assumptions);
2630 }
2631
2632 /* We are going to lose some information about upper bound on
2633 number of iterations in this step, so record the information
2634 here. */
2635 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
2636 if (CONST_INT_P (iv1.base))
2637 up = INTVAL (iv1.base);
2638 else
2639 up = INTVAL (mode_mmax) - inc;
2640 down = INTVAL (CONST_INT_P (iv0.base)
2641 ? iv0.base
2642 : mode_mmin);
2643 max = (uint64_t) (up - down) / inc + 1;
2644 if (!desc->infinite
2645 && !desc->assumptions)
2646 record_niter_bound (loop, max, false, true);
2647
2648 if (iv0.step == const0_rtx)
2649 {
2650 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2651 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2652 }
2653 else
2654 {
2655 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2656 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2657 }
2658
2659 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2660 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2661 assumption = simplify_gen_relational (reverse_condition (cond),
2662 SImode, mode, tmp0, tmp1);
2663 if (assumption == const_true_rtx)
2664 goto zero_iter_simplify;
2665 else if (assumption != const0_rtx)
2666 desc->noloop_assumptions =
2667 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2668 cond = NE;
2669 }
2670 }
2671
2672 /* Count the number of iterations. */
2673 if (cond == NE)
2674 {
2675 /* Everything we do here is just arithmetics modulo size of mode. This
2676 makes us able to do more involved computations of number of iterations
2677 than in other cases. First transform the condition into shape
2678 s * i <> c, with s positive. */
2679 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2680 iv0.base = const0_rtx;
2681 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2682 iv1.step = const0_rtx;
2683 if (INTVAL (iv0.step) < 0)
2684 {
2685 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
2686 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
2687 }
2688 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2689
2690 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2691 is infinite. Otherwise, the number of iterations is
2692 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2693 s = INTVAL (iv0.step); d = 1;
2694 while (s % 2 != 1)
2695 {
2696 s /= 2;
2697 d *= 2;
2698 size--;
2699 }
2700 bound = GEN_INT (((uint64_t) 1 << (size - 1 ) << 1) - 1);
2701
2702 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2703 tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
2704 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2705 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2706
2707 tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
2708 inv = inverse (s, size);
2709 tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
2710 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2711 }
2712 else
2713 {
2714 if (iv1.step == const0_rtx)
2715 /* Condition in shape a + s * i <= b
2716 We must know that b + s does not overflow and a <= b + s and then we
2717 can compute number of iterations as (b + s - a) / s. (It might
2718 seem that we in fact could be more clever about testing the b + s
2719 overflow condition using some information about b - a mod s,
2720 but it was already taken into account during LE -> NE transform). */
2721 {
2722 step = iv0.step;
2723 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2724 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2725
2726 bound = simplify_gen_binary (MINUS, mode, mode_mmax,
2727 lowpart_subreg (mode, step,
2728 comp_mode));
2729 if (step_is_pow2)
2730 {
2731 rtx t0, t1;
2732
2733 /* If s is power of 2, we know that the loop is infinite if
2734 a % s <= b % s and b + s overflows. */
2735 assumption = simplify_gen_relational (reverse_condition (cond),
2736 SImode, mode,
2737 tmp1, bound);
2738
2739 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2740 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2741 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2742 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2743 desc->infinite =
2744 alloc_EXPR_LIST (0, assumption, desc->infinite);
2745 }
2746 else
2747 {
2748 assumption = simplify_gen_relational (cond, SImode, mode,
2749 tmp1, bound);
2750 desc->assumptions =
2751 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2752 }
2753
2754 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
2755 tmp = lowpart_subreg (mode, tmp, comp_mode);
2756 assumption = simplify_gen_relational (reverse_condition (cond),
2757 SImode, mode, tmp0, tmp);
2758
2759 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2760 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2761 }
2762 else
2763 {
2764 /* Condition in shape a <= b - s * i
2765 We must know that a - s does not overflow and a - s <= b and then
2766 we can again compute number of iterations as (b - (a - s)) / s. */
2767 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
2768 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2769 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2770
2771 bound = simplify_gen_binary (PLUS, mode, mode_mmin,
2772 lowpart_subreg (mode, step, comp_mode));
2773 if (step_is_pow2)
2774 {
2775 rtx t0, t1;
2776
2777 /* If s is power of 2, we know that the loop is infinite if
2778 a % s <= b % s and a - s overflows. */
2779 assumption = simplify_gen_relational (reverse_condition (cond),
2780 SImode, mode,
2781 bound, tmp0);
2782
2783 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2784 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2785 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2786 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2787 desc->infinite =
2788 alloc_EXPR_LIST (0, assumption, desc->infinite);
2789 }
2790 else
2791 {
2792 assumption = simplify_gen_relational (cond, SImode, mode,
2793 bound, tmp0);
2794 desc->assumptions =
2795 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2796 }
2797
2798 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
2799 tmp = lowpart_subreg (mode, tmp, comp_mode);
2800 assumption = simplify_gen_relational (reverse_condition (cond),
2801 SImode, mode,
2802 tmp, tmp1);
2803 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2804 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2805 }
2806 if (assumption == const_true_rtx)
2807 goto zero_iter_simplify;
2808 else if (assumption != const0_rtx)
2809 desc->noloop_assumptions =
2810 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2811 delta = simplify_gen_binary (UDIV, mode, delta, step);
2812 desc->niter_expr = delta;
2813 }
2814
2815 old_niter = desc->niter_expr;
2816
2817 simplify_using_initial_values (loop, AND, &desc->assumptions);
2818 if (desc->assumptions
2819 && XEXP (desc->assumptions, 0) == const0_rtx)
2820 goto fail;
2821 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2822 simplify_using_initial_values (loop, IOR, &desc->infinite);
2823 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2824
2825 /* Rerun the simplification. Consider code (created by copying loop headers)
2826
2827 i = 0;
2828
2829 if (0 < n)
2830 {
2831 do
2832 {
2833 i++;
2834 } while (i < n);
2835 }
2836
2837 The first pass determines that i = 0, the second pass uses it to eliminate
2838 noloop assumption. */
2839
2840 simplify_using_initial_values (loop, AND, &desc->assumptions);
2841 if (desc->assumptions
2842 && XEXP (desc->assumptions, 0) == const0_rtx)
2843 goto fail;
2844 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2845 simplify_using_initial_values (loop, IOR, &desc->infinite);
2846 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2847
2848 if (desc->noloop_assumptions
2849 && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
2850 goto zero_iter;
2851
2852 if (CONST_INT_P (desc->niter_expr))
2853 {
2854 uint64_t val = INTVAL (desc->niter_expr);
2855
2856 desc->const_iter = true;
2857 desc->niter = val & GET_MODE_MASK (desc->mode);
2858 if (!desc->infinite
2859 && !desc->assumptions)
2860 record_niter_bound (loop, desc->niter, false, true);
2861 }
2862 else
2863 {
2864 max = determine_max_iter (loop, desc, old_niter);
2865 if (!max)
2866 goto zero_iter_simplify;
2867 if (!desc->infinite
2868 && !desc->assumptions)
2869 record_niter_bound (loop, max, false, true);
2870
2871 /* simplify_using_initial_values does a copy propagation on the registers
2872 in the expression for the number of iterations. This prolongs life
2873 ranges of registers and increases register pressure, and usually
2874 brings no gain (and if it happens to do, the cse pass will take care
2875 of it anyway). So prevent this behavior, unless it enabled us to
2876 derive that the number of iterations is a constant. */
2877 desc->niter_expr = old_niter;
2878 }
2879
2880 return;
2881
2882 zero_iter_simplify:
2883 /* Simplify the assumptions. */
2884 simplify_using_initial_values (loop, AND, &desc->assumptions);
2885 if (desc->assumptions
2886 && XEXP (desc->assumptions, 0) == const0_rtx)
2887 goto fail;
2888 simplify_using_initial_values (loop, IOR, &desc->infinite);
2889
2890 /* Fallthru. */
2891 zero_iter:
2892 desc->const_iter = true;
2893 desc->niter = 0;
2894 record_niter_bound (loop, 0, true, true);
2895 desc->noloop_assumptions = NULL_RTX;
2896 desc->niter_expr = const0_rtx;
2897 return;
2898
2899 fail:
2900 desc->simple_p = false;
2901 return;
2902 }
2903
2904 /* Checks whether E is a simple exit from LOOP and stores its description
2905 into DESC. */
2906
2907 static void
2908 check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2909 {
2910 basic_block exit_bb;
2911 rtx condition;
2912 rtx_insn *at;
2913 edge ein;
2914
2915 exit_bb = e->src;
2916 desc->simple_p = false;
2917
2918 /* It must belong directly to the loop. */
2919 if (exit_bb->loop_father != loop)
2920 return;
2921
2922 /* It must be tested (at least) once during any iteration. */
2923 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
2924 return;
2925
2926 /* It must end in a simple conditional jump. */
2927 if (!any_condjump_p (BB_END (exit_bb)))
2928 return;
2929
2930 ein = EDGE_SUCC (exit_bb, 0);
2931 if (ein == e)
2932 ein = EDGE_SUCC (exit_bb, 1);
2933
2934 desc->out_edge = e;
2935 desc->in_edge = ein;
2936
2937 /* Test whether the condition is suitable. */
2938 if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
2939 return;
2940
2941 if (ein->flags & EDGE_FALLTHRU)
2942 {
2943 condition = reversed_condition (condition);
2944 if (!condition)
2945 return;
2946 }
2947
2948 /* Check that we are able to determine number of iterations and fill
2949 in information about it. */
2950 iv_number_of_iterations (loop, at, condition, desc);
2951 }
2952
2953 /* Finds a simple exit of LOOP and stores its description into DESC. */
2954
2955 void
2956 find_simple_exit (struct loop *loop, struct niter_desc *desc)
2957 {
2958 unsigned i;
2959 basic_block *body;
2960 edge e;
2961 struct niter_desc act;
2962 bool any = false;
2963 edge_iterator ei;
2964
2965 desc->simple_p = false;
2966 body = get_loop_body (loop);
2967
2968 for (i = 0; i < loop->num_nodes; i++)
2969 {
2970 FOR_EACH_EDGE (e, ei, body[i]->succs)
2971 {
2972 if (flow_bb_inside_loop_p (loop, e->dest))
2973 continue;
2974
2975 check_simple_exit (loop, e, &act);
2976 if (!act.simple_p)
2977 continue;
2978
2979 if (!any)
2980 any = true;
2981 else
2982 {
2983 /* Prefer constant iterations; the less the better. */
2984 if (!act.const_iter
2985 || (desc->const_iter && act.niter >= desc->niter))
2986 continue;
2987
2988 /* Also if the actual exit may be infinite, while the old one
2989 not, prefer the old one. */
2990 if (act.infinite && !desc->infinite)
2991 continue;
2992 }
2993
2994 *desc = act;
2995 }
2996 }
2997
2998 if (dump_file)
2999 {
3000 if (desc->simple_p)
3001 {
3002 fprintf (dump_file, "Loop %d is simple:\n", loop->num);
3003 fprintf (dump_file, " simple exit %d -> %d\n",
3004 desc->out_edge->src->index,
3005 desc->out_edge->dest->index);
3006 if (desc->assumptions)
3007 {
3008 fprintf (dump_file, " assumptions: ");
3009 print_rtl (dump_file, desc->assumptions);
3010 fprintf (dump_file, "\n");
3011 }
3012 if (desc->noloop_assumptions)
3013 {
3014 fprintf (dump_file, " does not roll if: ");
3015 print_rtl (dump_file, desc->noloop_assumptions);
3016 fprintf (dump_file, "\n");
3017 }
3018 if (desc->infinite)
3019 {
3020 fprintf (dump_file, " infinite if: ");
3021 print_rtl (dump_file, desc->infinite);
3022 fprintf (dump_file, "\n");
3023 }
3024
3025 fprintf (dump_file, " number of iterations: ");
3026 print_rtl (dump_file, desc->niter_expr);
3027 fprintf (dump_file, "\n");
3028
3029 fprintf (dump_file, " upper bound: %li\n",
3030 (long)get_max_loop_iterations_int (loop));
3031 fprintf (dump_file, " realistic bound: %li\n",
3032 (long)get_estimated_loop_iterations_int (loop));
3033 }
3034 else
3035 fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
3036 }
3037
3038 free (body);
3039 }
3040
3041 /* Creates a simple loop description of LOOP if it was not computed
3042 already. */
3043
3044 struct niter_desc *
3045 get_simple_loop_desc (struct loop *loop)
3046 {
3047 struct niter_desc *desc = simple_loop_desc (loop);
3048
3049 if (desc)
3050 return desc;
3051
3052 /* At least desc->infinite is not always initialized by
3053 find_simple_loop_exit. */
3054 desc = ggc_cleared_alloc<niter_desc> ();
3055 iv_analysis_loop_init (loop);
3056 find_simple_exit (loop, desc);
3057 loop->simple_loop_desc = desc;
3058
3059 if (desc->simple_p && (desc->assumptions || desc->infinite))
3060 {
3061 const char *wording;
3062
3063 /* Assume that no overflow happens and that the loop is finite.
3064 We already warned at the tree level if we ran optimizations there. */
3065 if (!flag_tree_loop_optimize && warn_unsafe_loop_optimizations)
3066 {
3067 if (desc->infinite)
3068 {
3069 wording =
3070 flag_unsafe_loop_optimizations
3071 ? N_("assuming that the loop is not infinite")
3072 : N_("cannot optimize possibly infinite loops");
3073 warning (OPT_Wunsafe_loop_optimizations, "%s",
3074 gettext (wording));
3075 }
3076 if (desc->assumptions)
3077 {
3078 wording =
3079 flag_unsafe_loop_optimizations
3080 ? N_("assuming that the loop counter does not overflow")
3081 : N_("cannot optimize loop, the loop counter may overflow");
3082 warning (OPT_Wunsafe_loop_optimizations, "%s",
3083 gettext (wording));
3084 }
3085 }
3086
3087 if (flag_unsafe_loop_optimizations)
3088 {
3089 desc->assumptions = NULL_RTX;
3090 desc->infinite = NULL_RTX;
3091 }
3092 }
3093
3094 return desc;
3095 }
3096
3097 /* Releases simple loop description for LOOP. */
3098
3099 void
3100 free_simple_loop_desc (struct loop *loop)
3101 {
3102 struct niter_desc *desc = simple_loop_desc (loop);
3103
3104 if (!desc)
3105 return;
3106
3107 ggc_free (desc);
3108 loop->simple_loop_desc = NULL;
3109 }