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