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