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