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