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