OSDN Git Service

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