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