PR tree-optimization/77901
[official-gcc.git] / gcc / auto-inc-dec.c
blobeb2dc21f801c51084468b45d2a1d28643bdc927d
1 /* Discovery of auto-inc and auto-dec instructions.
2 Copyright (C) 2006-2016 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "predict.h"
29 #include "df.h"
30 #include "insn-config.h"
31 #include "emit-rtl.h"
32 #include "recog.h"
33 #include "cfgrtl.h"
34 #include "expr.h"
35 #include "tree-pass.h"
36 #include "dbgcnt.h"
37 #include "print-rtl.h"
39 /* This pass was originally removed from flow.c. However there is
40 almost nothing that remains of that code.
42 There are (4) basic forms that are matched:
44 (1) FORM_PRE_ADD
45 a <- b + c
46 ...
49 becomes
51 a <- b
52 ...
53 *(a += c) pre
56 (2) FORM_PRE_INC
57 a += c
58 ...
61 becomes
63 *(a += c) pre
66 (3) FORM_POST_ADD
68 ...
69 b <- a + c
71 (For this case to be true, b must not be assigned or used between
72 the *a and the assignment to b. B must also be a Pmode reg.)
74 becomes
76 b <- a
77 ...
78 *(b += c) post
81 (4) FORM_POST_INC
83 ...
84 a <- a + c
86 becomes
88 *(a += c) post
90 There are three types of values of c.
92 1) c is a constant equal to the width of the value being accessed by
93 the pointer. This is useful for machines that have
94 HAVE_PRE_INCREMENT, HAVE_POST_INCREMENT, HAVE_PRE_DECREMENT or
95 HAVE_POST_DECREMENT defined.
97 2) c is a constant not equal to the width of the value being accessed
98 by the pointer. This is useful for machines that have
99 HAVE_PRE_MODIFY_DISP, HAVE_POST_MODIFY_DISP defined.
101 3) c is a register. This is useful for machines that have
102 HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
104 The is one special case: if a already had an offset equal to it +-
105 its width and that offset is equal to -c when the increment was
106 before the ref or +c if the increment was after the ref, then if we
107 can do the combination but switch the pre/post bit. */
110 enum form
112 FORM_PRE_ADD,
113 FORM_PRE_INC,
114 FORM_POST_ADD,
115 FORM_POST_INC,
116 FORM_last
119 /* The states of the second operands of mem refs and inc insns. If no
120 second operand of the mem_ref was found, it is assumed to just be
121 ZERO. SIZE is the size of the mode accessed in the memref. The
122 ANY is used for constants that are not +-size or 0. REG is used if
123 the forms are reg1 + reg2. */
125 enum inc_state
127 INC_ZERO, /* == 0 */
128 INC_NEG_SIZE, /* == +size */
129 INC_POS_SIZE, /* == -size */
130 INC_NEG_ANY, /* == some -constant */
131 INC_POS_ANY, /* == some +constant */
132 INC_REG, /* == some register */
133 INC_last
136 /* The eight forms that pre/post inc/dec can take. */
137 enum gen_form
139 NOTHING,
140 SIMPLE_PRE_INC, /* ++size */
141 SIMPLE_POST_INC, /* size++ */
142 SIMPLE_PRE_DEC, /* --size */
143 SIMPLE_POST_DEC, /* size-- */
144 DISP_PRE, /* ++con */
145 DISP_POST, /* con++ */
146 REG_PRE, /* ++reg */
147 REG_POST /* reg++ */
150 /* Tmp mem rtx for use in cost modeling. */
151 static rtx mem_tmp;
153 static enum inc_state
154 set_inc_state (HOST_WIDE_INT val, int size)
156 if (val == 0)
157 return INC_ZERO;
158 if (val < 0)
159 return (val == -size) ? INC_NEG_SIZE : INC_NEG_ANY;
160 else
161 return (val == size) ? INC_POS_SIZE : INC_POS_ANY;
164 /* The DECISION_TABLE that describes what form, if any, the increment
165 or decrement will take. It is a three dimensional table. The first
166 index is the type of constant or register found as the second
167 operand of the inc insn. The second index is the type of constant
168 or register found as the second operand of the memory reference (if
169 no second operand exists, 0 is used). The third index is the form
170 and location (relative to the mem reference) of inc insn. */
172 static bool initialized = false;
173 static enum gen_form decision_table[INC_last][INC_last][FORM_last];
175 static void
176 init_decision_table (void)
178 enum gen_form value;
180 if (HAVE_PRE_INCREMENT || HAVE_PRE_MODIFY_DISP)
182 /* Prefer the simple form if both are available. */
183 value = (HAVE_PRE_INCREMENT) ? SIMPLE_PRE_INC : DISP_PRE;
185 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
186 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_INC] = value;
188 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_ADD] = value;
189 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_INC] = value;
192 if (HAVE_POST_INCREMENT || HAVE_POST_MODIFY_DISP)
194 /* Prefer the simple form if both are available. */
195 value = (HAVE_POST_INCREMENT) ? SIMPLE_POST_INC : DISP_POST;
197 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_ADD] = value;
198 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_INC] = value;
200 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_ADD] = value;
201 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_INC] = value;
204 if (HAVE_PRE_DECREMENT || HAVE_PRE_MODIFY_DISP)
206 /* Prefer the simple form if both are available. */
207 value = (HAVE_PRE_DECREMENT) ? SIMPLE_PRE_DEC : DISP_PRE;
209 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
210 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_INC] = value;
212 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_ADD] = value;
213 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_INC] = value;
216 if (HAVE_POST_DECREMENT || HAVE_POST_MODIFY_DISP)
218 /* Prefer the simple form if both are available. */
219 value = (HAVE_POST_DECREMENT) ? SIMPLE_POST_DEC : DISP_POST;
221 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_ADD] = value;
222 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_INC] = value;
224 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_ADD] = value;
225 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_INC] = value;
228 if (HAVE_PRE_MODIFY_DISP)
230 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
231 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
233 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_ADD] = DISP_PRE;
234 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_INC] = DISP_PRE;
236 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
237 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
239 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_ADD] = DISP_PRE;
240 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_INC] = DISP_PRE;
243 if (HAVE_POST_MODIFY_DISP)
245 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
246 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
248 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_ADD] = DISP_POST;
249 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_INC] = DISP_POST;
251 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
252 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
254 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_ADD] = DISP_POST;
255 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_INC] = DISP_POST;
258 /* This is much simpler than the other cases because we do not look
259 for the reg1-reg2 case. Note that we do not have a INC_POS_REG
260 and INC_NEG_REG states. Most of the use of such states would be
261 on a target that had an R1 - R2 update address form.
263 There is the remote possibility that you could also catch a = a +
264 b; *(a - b) as a postdecrement of (a + b). However, it is
265 unclear if *(a - b) would ever be generated on a machine that did
266 not have that kind of addressing mode. The IA-64 and RS6000 will
267 not do this, and I cannot speak for any other. If any
268 architecture does have an a-b update for, these cases should be
269 added. */
270 if (HAVE_PRE_MODIFY_REG)
272 decision_table[INC_REG][INC_ZERO][FORM_PRE_ADD] = REG_PRE;
273 decision_table[INC_REG][INC_ZERO][FORM_PRE_INC] = REG_PRE;
275 decision_table[INC_REG][INC_REG][FORM_POST_ADD] = REG_PRE;
276 decision_table[INC_REG][INC_REG][FORM_POST_INC] = REG_PRE;
279 if (HAVE_POST_MODIFY_REG)
281 decision_table[INC_REG][INC_ZERO][FORM_POST_ADD] = REG_POST;
282 decision_table[INC_REG][INC_ZERO][FORM_POST_INC] = REG_POST;
285 initialized = true;
288 /* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
289 "reg_res = reg0+c". */
291 static struct inc_insn
293 rtx_insn *insn; /* The insn being parsed. */
294 rtx pat; /* The pattern of the insn. */
295 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
296 enum form form;
297 rtx reg_res;
298 rtx reg0;
299 rtx reg1;
300 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
301 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
302 } inc_insn;
305 /* Dump the parsed inc insn to FILE. */
307 static void
308 dump_inc_insn (FILE *file)
310 const char *f = ((inc_insn.form == FORM_PRE_ADD)
311 || (inc_insn.form == FORM_PRE_INC)) ? "pre" : "post";
313 dump_insn_slim (file, inc_insn.insn);
315 switch (inc_insn.form)
317 case FORM_PRE_ADD:
318 case FORM_POST_ADD:
319 if (inc_insn.reg1_is_const)
320 fprintf (file, "found %s add(%d) r[%d]=r[%d]+%d\n",
321 f, INSN_UID (inc_insn.insn),
322 REGNO (inc_insn.reg_res),
323 REGNO (inc_insn.reg0), (int) inc_insn.reg1_val);
324 else
325 fprintf (file, "found %s add(%d) r[%d]=r[%d]+r[%d]\n",
326 f, INSN_UID (inc_insn.insn),
327 REGNO (inc_insn.reg_res),
328 REGNO (inc_insn.reg0), REGNO (inc_insn.reg1));
329 break;
331 case FORM_PRE_INC:
332 case FORM_POST_INC:
333 if (inc_insn.reg1_is_const)
334 fprintf (file, "found %s inc(%d) r[%d]+=%d\n",
335 f, INSN_UID (inc_insn.insn),
336 REGNO (inc_insn.reg_res), (int) inc_insn.reg1_val);
337 else
338 fprintf (file, "found %s inc(%d) r[%d]+=r[%d]\n",
339 f, INSN_UID (inc_insn.insn),
340 REGNO (inc_insn.reg_res), REGNO (inc_insn.reg1));
341 break;
343 default:
344 break;
349 /* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
351 static struct mem_insn
353 rtx_insn *insn; /* The insn being parsed. */
354 rtx pat; /* The pattern of the insn. */
355 rtx *mem_loc; /* The address of the field that holds the mem */
356 /* that is to be replaced. */
357 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
358 rtx reg0;
359 rtx reg1; /* This is either a reg or a const depending on
360 reg1_is_const. */
361 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
362 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
363 } mem_insn;
366 /* Dump the parsed mem insn to FILE. */
368 static void
369 dump_mem_insn (FILE *file)
371 dump_insn_slim (file, mem_insn.insn);
373 if (mem_insn.reg1_is_const)
374 fprintf (file, "found mem(%d) *(r[%d]+%d)\n",
375 INSN_UID (mem_insn.insn),
376 REGNO (mem_insn.reg0), (int) mem_insn.reg1_val);
377 else
378 fprintf (file, "found mem(%d) *(r[%d]+r[%d])\n",
379 INSN_UID (mem_insn.insn),
380 REGNO (mem_insn.reg0), REGNO (mem_insn.reg1));
384 /* The following three arrays contain pointers to instructions. They
385 are indexed by REGNO. At any point in the basic block where we are
386 looking these three arrays contain, respectively, the next insn
387 that uses REGNO, the next inc or add insn that uses REGNO and the
388 next insn that sets REGNO.
390 The arrays are not cleared when we move from block to block so
391 whenever an insn is retrieved from these arrays, it's block number
392 must be compared with the current block.
395 static rtx_insn **reg_next_use = NULL;
396 static rtx_insn **reg_next_inc_use = NULL;
397 static rtx_insn **reg_next_def = NULL;
400 /* Move dead note that match PATTERN to TO_INSN from FROM_INSN. We do
401 not really care about moving any other notes from the inc or add
402 insn. Moving the REG_EQUAL and REG_EQUIV is clearly wrong and it
403 does not appear that there are any other kinds of relevant notes. */
405 static void
406 move_dead_notes (rtx_insn *to_insn, rtx_insn *from_insn, rtx pattern)
408 rtx note;
409 rtx next_note;
410 rtx prev_note = NULL;
412 for (note = REG_NOTES (from_insn); note; note = next_note)
414 next_note = XEXP (note, 1);
416 if ((REG_NOTE_KIND (note) == REG_DEAD)
417 && pattern == XEXP (note, 0))
419 XEXP (note, 1) = REG_NOTES (to_insn);
420 REG_NOTES (to_insn) = note;
421 if (prev_note)
422 XEXP (prev_note, 1) = next_note;
423 else
424 REG_NOTES (from_insn) = next_note;
426 else prev_note = note;
430 /* Change mem_insn.mem_loc so that uses NEW_ADDR which has an
431 increment of INC_REG. To have reached this point, the change is a
432 legitimate one from a dataflow point of view. The only questions
433 are is this a valid change to the instruction and is this a
434 profitable change to the instruction. */
436 static bool
437 attempt_change (rtx new_addr, rtx inc_reg)
439 /* There are four cases: For the two cases that involve an add
440 instruction, we are going to have to delete the add and insert a
441 mov. We are going to assume that the mov is free. This is
442 fairly early in the backend and there are a lot of opportunities
443 for removing that move later. In particular, there is the case
444 where the move may be dead, this is what dead code elimination
445 passes are for. The two cases where we have an inc insn will be
446 handled mov free. */
448 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
449 rtx_insn *mov_insn = NULL;
450 int regno;
451 rtx mem = *mem_insn.mem_loc;
452 machine_mode mode = GET_MODE (mem);
453 rtx new_mem;
454 int old_cost = 0;
455 int new_cost = 0;
456 bool speed = optimize_bb_for_speed_p (bb);
458 PUT_MODE (mem_tmp, mode);
459 XEXP (mem_tmp, 0) = new_addr;
461 old_cost = (set_src_cost (mem, mode, speed)
462 + set_rtx_cost (PATTERN (inc_insn.insn), speed));
464 new_cost = set_src_cost (mem_tmp, mode, speed);
466 /* In the FORM_PRE_ADD and FORM_POST_ADD cases we emit an extra move
467 whose cost we should account for. */
468 if (inc_insn.form == FORM_PRE_ADD
469 || inc_insn.form == FORM_POST_ADD)
471 start_sequence ();
472 emit_move_insn (inc_insn.reg_res, inc_insn.reg0);
473 mov_insn = get_insns ();
474 end_sequence ();
475 new_cost += seq_cost (mov_insn, speed);
478 /* The first item of business is to see if this is profitable. */
479 if (old_cost < new_cost)
481 if (dump_file)
482 fprintf (dump_file, "cost failure old=%d new=%d\n", old_cost, new_cost);
483 return false;
486 /* Jump through a lot of hoops to keep the attributes up to date. We
487 do not want to call one of the change address variants that take
488 an offset even though we know the offset in many cases. These
489 assume you are changing where the address is pointing by the
490 offset. */
491 new_mem = replace_equiv_address_nv (mem, new_addr);
492 if (! validate_change (mem_insn.insn, mem_insn.mem_loc, new_mem, 0))
494 if (dump_file)
495 fprintf (dump_file, "validation failure\n");
496 return false;
499 /* From here to the end of the function we are committed to the
500 change, i.e. nothing fails. Generate any necessary movs, move
501 any regnotes, and fix up the reg_next_{use,inc_use,def}. */
502 switch (inc_insn.form)
504 case FORM_PRE_ADD:
505 /* Replace the addition with a move. Do it at the location of
506 the addition since the operand of the addition may change
507 before the memory reference. */
508 gcc_assert (mov_insn);
509 emit_insn_before (mov_insn, inc_insn.insn);
510 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
512 regno = REGNO (inc_insn.reg_res);
513 reg_next_def[regno] = mov_insn;
514 reg_next_use[regno] = NULL;
515 regno = REGNO (inc_insn.reg0);
516 reg_next_use[regno] = mov_insn;
517 df_recompute_luids (bb);
518 break;
520 case FORM_POST_INC:
521 regno = REGNO (inc_insn.reg_res);
522 if (reg_next_use[regno] == reg_next_inc_use[regno])
523 reg_next_inc_use[regno] = NULL;
525 /* Fallthru. */
526 case FORM_PRE_INC:
527 regno = REGNO (inc_insn.reg_res);
528 reg_next_def[regno] = mem_insn.insn;
529 reg_next_use[regno] = NULL;
531 break;
533 case FORM_POST_ADD:
534 gcc_assert (mov_insn);
535 emit_insn_before (mov_insn, mem_insn.insn);
536 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
538 /* Do not move anything to the mov insn because the instruction
539 pointer for the main iteration has not yet hit that. It is
540 still pointing to the mem insn. */
541 regno = REGNO (inc_insn.reg_res);
542 reg_next_def[regno] = mem_insn.insn;
543 reg_next_use[regno] = NULL;
545 regno = REGNO (inc_insn.reg0);
546 reg_next_use[regno] = mem_insn.insn;
547 if ((reg_next_use[regno] == reg_next_inc_use[regno])
548 || (reg_next_inc_use[regno] == inc_insn.insn))
549 reg_next_inc_use[regno] = NULL;
550 df_recompute_luids (bb);
551 break;
553 case FORM_last:
554 default:
555 gcc_unreachable ();
558 if (!inc_insn.reg1_is_const)
560 regno = REGNO (inc_insn.reg1);
561 reg_next_use[regno] = mem_insn.insn;
562 if ((reg_next_use[regno] == reg_next_inc_use[regno])
563 || (reg_next_inc_use[regno] == inc_insn.insn))
564 reg_next_inc_use[regno] = NULL;
567 delete_insn (inc_insn.insn);
569 if (dump_file && mov_insn)
571 fprintf (dump_file, "inserting mov ");
572 dump_insn_slim (dump_file, mov_insn);
575 /* Record that this insn has an implicit side effect. */
576 add_reg_note (mem_insn.insn, REG_INC, inc_reg);
578 if (dump_file)
580 fprintf (dump_file, "****success ");
581 dump_insn_slim (dump_file, mem_insn.insn);
584 return true;
588 /* Try to combine the instruction in INC_INSN with the instruction in
589 MEM_INSN. First the form is determined using the DECISION_TABLE
590 and the results of parsing the INC_INSN and the MEM_INSN.
591 Assuming the form is ok, a prototype new address is built which is
592 passed to ATTEMPT_CHANGE for final processing. */
594 static bool
595 try_merge (void)
597 enum gen_form gen_form;
598 rtx mem = *mem_insn.mem_loc;
599 rtx inc_reg = inc_insn.form == FORM_POST_ADD ?
600 inc_insn.reg_res : mem_insn.reg0;
602 /* The width of the mem being accessed. */
603 int size = GET_MODE_SIZE (GET_MODE (mem));
604 rtx_insn *last_insn = NULL;
605 machine_mode reg_mode = GET_MODE (inc_reg);
607 switch (inc_insn.form)
609 case FORM_PRE_ADD:
610 case FORM_PRE_INC:
611 last_insn = mem_insn.insn;
612 break;
613 case FORM_POST_INC:
614 case FORM_POST_ADD:
615 last_insn = inc_insn.insn;
616 break;
617 case FORM_last:
618 default:
619 gcc_unreachable ();
622 /* Cannot handle auto inc of the stack. */
623 if (inc_reg == stack_pointer_rtx)
625 if (dump_file)
626 fprintf (dump_file, "cannot inc stack %d failure\n", REGNO (inc_reg));
627 return false;
630 /* Look to see if the inc register is dead after the memory
631 reference. If it is, do not do the combination. */
632 if (find_regno_note (last_insn, REG_DEAD, REGNO (inc_reg)))
634 if (dump_file)
635 fprintf (dump_file, "dead failure %d\n", REGNO (inc_reg));
636 return false;
639 mem_insn.reg1_state = (mem_insn.reg1_is_const)
640 ? set_inc_state (mem_insn.reg1_val, size) : INC_REG;
641 inc_insn.reg1_state = (inc_insn.reg1_is_const)
642 ? set_inc_state (inc_insn.reg1_val, size) : INC_REG;
644 /* Now get the form that we are generating. */
645 gen_form = decision_table
646 [inc_insn.reg1_state][mem_insn.reg1_state][inc_insn.form];
648 if (dbg_cnt (auto_inc_dec) == false)
649 return false;
651 switch (gen_form)
653 default:
654 case NOTHING:
655 return false;
657 case SIMPLE_PRE_INC: /* ++size */
658 if (dump_file)
659 fprintf (dump_file, "trying SIMPLE_PRE_INC\n");
660 return attempt_change (gen_rtx_PRE_INC (reg_mode, inc_reg), inc_reg);
662 case SIMPLE_POST_INC: /* size++ */
663 if (dump_file)
664 fprintf (dump_file, "trying SIMPLE_POST_INC\n");
665 return attempt_change (gen_rtx_POST_INC (reg_mode, inc_reg), inc_reg);
667 case SIMPLE_PRE_DEC: /* --size */
668 if (dump_file)
669 fprintf (dump_file, "trying SIMPLE_PRE_DEC\n");
670 return attempt_change (gen_rtx_PRE_DEC (reg_mode, inc_reg), inc_reg);
672 case SIMPLE_POST_DEC: /* size-- */
673 if (dump_file)
674 fprintf (dump_file, "trying SIMPLE_POST_DEC\n");
675 return attempt_change (gen_rtx_POST_DEC (reg_mode, inc_reg), inc_reg);
677 case DISP_PRE: /* ++con */
678 if (dump_file)
679 fprintf (dump_file, "trying DISP_PRE\n");
680 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
681 inc_reg,
682 gen_rtx_PLUS (reg_mode,
683 inc_reg,
684 inc_insn.reg1)),
685 inc_reg);
687 case DISP_POST: /* con++ */
688 if (dump_file)
689 fprintf (dump_file, "trying POST_DISP\n");
690 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
691 inc_reg,
692 gen_rtx_PLUS (reg_mode,
693 inc_reg,
694 inc_insn.reg1)),
695 inc_reg);
697 case REG_PRE: /* ++reg */
698 if (dump_file)
699 fprintf (dump_file, "trying PRE_REG\n");
700 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
701 inc_reg,
702 gen_rtx_PLUS (reg_mode,
703 inc_reg,
704 inc_insn.reg1)),
705 inc_reg);
707 case REG_POST: /* reg++ */
708 if (dump_file)
709 fprintf (dump_file, "trying POST_REG\n");
710 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
711 inc_reg,
712 gen_rtx_PLUS (reg_mode,
713 inc_reg,
714 inc_insn.reg1)),
715 inc_reg);
719 /* Return the next insn that uses (if reg_next_use is passed in
720 NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
721 REGNO in BB. */
723 static rtx_insn *
724 get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
726 rtx_insn *insn = next_array[regno];
728 /* Lazy about cleaning out the next_arrays. */
729 if (insn && BLOCK_FOR_INSN (insn) != bb)
731 next_array[regno] = NULL;
732 insn = NULL;
735 return insn;
739 /* Return true if INSN is of a form "a = b op c" where a and b are
740 regs. op is + if c is a reg and +|- if c is a const. Fill in
741 INC_INSN with what is found.
743 This function is called in two contexts, if BEFORE_MEM is true,
744 this is called for each insn in the basic block. If BEFORE_MEM is
745 false, it is called for the instruction in the block that uses the
746 index register for some memory reference that is currently being
747 processed. */
749 static bool
750 parse_add_or_inc (rtx_insn *insn, bool before_mem)
752 rtx pat = single_set (insn);
753 if (!pat)
754 return false;
756 /* Result must be single reg. */
757 if (!REG_P (SET_DEST (pat)))
758 return false;
760 if ((GET_CODE (SET_SRC (pat)) != PLUS)
761 && (GET_CODE (SET_SRC (pat)) != MINUS))
762 return false;
764 if (!REG_P (XEXP (SET_SRC (pat), 0)))
765 return false;
767 inc_insn.insn = insn;
768 inc_insn.pat = pat;
769 inc_insn.reg_res = SET_DEST (pat);
770 inc_insn.reg0 = XEXP (SET_SRC (pat), 0);
771 if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg0))
772 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
773 else
774 inc_insn.form = before_mem ? FORM_PRE_ADD : FORM_POST_ADD;
776 if (CONST_INT_P (XEXP (SET_SRC (pat), 1)))
778 /* Process a = b + c where c is a const. */
779 inc_insn.reg1_is_const = true;
780 if (GET_CODE (SET_SRC (pat)) == PLUS)
782 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
783 inc_insn.reg1_val = INTVAL (inc_insn.reg1);
785 else
787 inc_insn.reg1_val = -INTVAL (XEXP (SET_SRC (pat), 1));
788 inc_insn.reg1 = GEN_INT (inc_insn.reg1_val);
790 return true;
792 else if ((HAVE_PRE_MODIFY_REG || HAVE_POST_MODIFY_REG)
793 && (REG_P (XEXP (SET_SRC (pat), 1)))
794 && GET_CODE (SET_SRC (pat)) == PLUS)
796 /* Process a = b + c where c is a reg. */
797 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
798 inc_insn.reg1_is_const = false;
800 if (inc_insn.form == FORM_PRE_INC
801 || inc_insn.form == FORM_POST_INC)
802 return true;
803 else if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg1))
805 /* Reverse the two operands and turn *_ADD into *_INC since
806 a = c + a. */
807 std::swap (inc_insn.reg0, inc_insn.reg1);
808 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
809 return true;
811 else
812 return true;
815 return false;
819 /* A recursive function that checks all of the mem uses in
820 ADDRESS_OF_X to see if any single one of them is compatible with
821 what has been found in inc_insn.
823 -1 is returned for success. 0 is returned if nothing was found and
824 1 is returned for failure. */
826 static int
827 find_address (rtx *address_of_x)
829 rtx x = *address_of_x;
830 enum rtx_code code = GET_CODE (x);
831 const char *const fmt = GET_RTX_FORMAT (code);
832 int i;
833 int value = 0;
834 int tem;
836 if (code == MEM && rtx_equal_p (XEXP (x, 0), inc_insn.reg_res))
838 /* Match with *reg0. */
839 mem_insn.mem_loc = address_of_x;
840 mem_insn.reg0 = inc_insn.reg_res;
841 mem_insn.reg1_is_const = true;
842 mem_insn.reg1_val = 0;
843 mem_insn.reg1 = GEN_INT (0);
844 return -1;
846 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
847 && rtx_equal_p (XEXP (XEXP (x, 0), 0), inc_insn.reg_res))
849 rtx b = XEXP (XEXP (x, 0), 1);
850 mem_insn.mem_loc = address_of_x;
851 mem_insn.reg0 = inc_insn.reg_res;
852 mem_insn.reg1 = b;
853 mem_insn.reg1_is_const = inc_insn.reg1_is_const;
854 if (CONST_INT_P (b))
856 /* Match with *(reg0 + reg1) where reg1 is a const. */
857 HOST_WIDE_INT val = INTVAL (b);
858 if (inc_insn.reg1_is_const
859 && (inc_insn.reg1_val == val || inc_insn.reg1_val == -val))
861 mem_insn.reg1_val = val;
862 return -1;
865 else if (!inc_insn.reg1_is_const
866 && rtx_equal_p (inc_insn.reg1, b))
867 /* Match with *(reg0 + reg1). */
868 return -1;
871 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
873 /* If REG occurs inside a MEM used in a bit-field reference,
874 that is unacceptable. */
875 if (find_address (&XEXP (x, 0)))
876 return 1;
879 if (x == inc_insn.reg_res)
880 return 1;
882 /* Time for some deep diving. */
883 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
885 if (fmt[i] == 'e')
887 tem = find_address (&XEXP (x, i));
888 /* If this is the first use, let it go so the rest of the
889 insn can be checked. */
890 if (value == 0)
891 value = tem;
892 else if (tem != 0)
893 /* More than one match was found. */
894 return 1;
896 else if (fmt[i] == 'E')
898 int j;
899 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
901 tem = find_address (&XVECEXP (x, i, j));
902 /* If this is the first use, let it go so the rest of
903 the insn can be checked. */
904 if (value == 0)
905 value = tem;
906 else if (tem != 0)
907 /* More than one match was found. */
908 return 1;
912 return value;
915 /* Once a suitable mem reference has been found and the MEM_INSN
916 structure has been filled in, FIND_INC is called to see if there is
917 a suitable add or inc insn that follows the mem reference and
918 determine if it is suitable to merge.
920 In the case where the MEM_INSN has two registers in the reference,
921 this function may be called recursively. The first time looking
922 for an add of the first register, and if that fails, looking for an
923 add of the second register. The FIRST_TRY parameter is used to
924 only allow the parameters to be reversed once. */
926 static bool
927 find_inc (bool first_try)
929 rtx_insn *insn;
930 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
931 rtx_insn *other_insn;
932 df_ref def;
934 /* Make sure this reg appears only once in this insn. */
935 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
937 if (dump_file)
938 fprintf (dump_file, "mem count failure\n");
939 return false;
942 if (dump_file)
943 dump_mem_insn (dump_file);
945 /* Find the next use that is an inc. */
946 insn = get_next_ref (REGNO (mem_insn.reg0),
947 BLOCK_FOR_INSN (mem_insn.insn),
948 reg_next_inc_use);
949 if (!insn)
950 return false;
952 /* Even though we know the next use is an add or inc because it came
953 from the reg_next_inc_use, we must still reparse. */
954 if (!parse_add_or_inc (insn, false))
956 /* Next use was not an add. Look for one extra case. It could be
957 that we have:
959 *(a + b)
960 ...= a;
961 ...= b + a
963 if we reverse the operands in the mem ref we would
964 find this. Only try it once though. */
965 if (first_try && !mem_insn.reg1_is_const)
967 std::swap (mem_insn.reg0, mem_insn.reg1);
968 return find_inc (false);
970 else
971 return false;
974 /* Need to assure that none of the operands of the inc instruction are
975 assigned to by the mem insn. */
976 FOR_EACH_INSN_DEF (def, mem_insn.insn)
978 unsigned int regno = DF_REF_REGNO (def);
979 if ((regno == REGNO (inc_insn.reg0))
980 || (regno == REGNO (inc_insn.reg_res)))
982 if (dump_file)
983 fprintf (dump_file, "inc conflicts with store failure.\n");
984 return false;
986 if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
988 if (dump_file)
989 fprintf (dump_file, "inc conflicts with store failure.\n");
990 return false;
994 if (dump_file)
995 dump_inc_insn (dump_file);
997 if (inc_insn.form == FORM_POST_ADD)
999 /* Make sure that there is no insn that assigns to inc_insn.res
1000 between the mem_insn and the inc_insn. */
1001 rtx_insn *other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1002 BLOCK_FOR_INSN (mem_insn.insn),
1003 reg_next_def);
1004 if (other_insn != inc_insn.insn)
1006 if (dump_file)
1007 fprintf (dump_file,
1008 "result of add is assigned to between mem and inc insns.\n");
1009 return false;
1012 other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1013 BLOCK_FOR_INSN (mem_insn.insn),
1014 reg_next_use);
1015 if (other_insn
1016 && (other_insn != inc_insn.insn)
1017 && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
1019 if (dump_file)
1020 fprintf (dump_file,
1021 "result of add is used between mem and inc insns.\n");
1022 return false;
1025 /* For the post_add to work, the result_reg of the inc must not be
1026 used in the mem insn since this will become the new index
1027 register. */
1028 if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
1030 if (dump_file)
1031 fprintf (dump_file, "base reg replacement failure.\n");
1032 return false;
1036 if (mem_insn.reg1_is_const)
1038 if (mem_insn.reg1_val == 0)
1040 if (!inc_insn.reg1_is_const)
1042 /* The mem looks like *r0 and the rhs of the add has two
1043 registers. */
1044 int luid = DF_INSN_LUID (inc_insn.insn);
1045 if (inc_insn.form == FORM_POST_ADD)
1047 /* The trick is that we are not going to increment r0,
1048 we are going to increment the result of the add insn.
1049 For this trick to be correct, the result reg of
1050 the inc must be a valid addressing reg. */
1051 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1052 if (GET_MODE (inc_insn.reg_res)
1053 != targetm.addr_space.address_mode (as))
1055 if (dump_file)
1056 fprintf (dump_file, "base reg mode failure.\n");
1057 return false;
1060 /* We also need to make sure that the next use of
1061 inc result is after the inc. */
1062 other_insn
1063 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1064 if (other_insn && luid > DF_INSN_LUID (other_insn))
1065 return false;
1067 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1068 std::swap (inc_insn.reg0, inc_insn.reg1);
1071 other_insn
1072 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1073 if (other_insn && luid > DF_INSN_LUID (other_insn))
1074 return false;
1077 /* Both the inc/add and the mem have a constant. Need to check
1078 that the constants are ok. */
1079 else if ((mem_insn.reg1_val != inc_insn.reg1_val)
1080 && (mem_insn.reg1_val != -inc_insn.reg1_val))
1081 return false;
1083 else
1085 /* The mem insn is of the form *(a + b) where a and b are both
1086 regs. It may be that in order to match the add or inc we
1087 need to treat it as if it was *(b + a). It may also be that
1088 the add is of the form a + c where c does not match b and
1089 then we just abandon this. */
1091 int luid = DF_INSN_LUID (inc_insn.insn);
1092 rtx_insn *other_insn;
1094 /* Make sure this reg appears only once in this insn. */
1095 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
1096 return false;
1098 if (inc_insn.form == FORM_POST_ADD)
1100 /* For this trick to be correct, the result reg of the inc
1101 must be a valid addressing reg. */
1102 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1103 if (GET_MODE (inc_insn.reg_res)
1104 != targetm.addr_space.address_mode (as))
1106 if (dump_file)
1107 fprintf (dump_file, "base reg mode failure.\n");
1108 return false;
1111 if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1113 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1115 /* See comment above on find_inc (false) call. */
1116 if (first_try)
1118 std::swap (mem_insn.reg0, mem_insn.reg1);
1119 return find_inc (false);
1121 else
1122 return false;
1125 /* Need to check that there are no assignments to b
1126 before the add insn. */
1127 other_insn
1128 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1129 if (other_insn && luid > DF_INSN_LUID (other_insn))
1130 return false;
1131 /* All ok for the next step. */
1133 else
1135 /* We know that mem_insn.reg0 must equal inc_insn.reg1
1136 or else we would not have found the inc insn. */
1137 std::swap (mem_insn.reg0, mem_insn.reg1);
1138 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1140 /* See comment above on find_inc (false) call. */
1141 if (first_try)
1142 return find_inc (false);
1143 else
1144 return false;
1146 /* To have gotten here know that.
1147 *(b + a)
1149 ... = (b + a)
1151 We also know that the lhs of the inc is not b or a. We
1152 need to make sure that there are no assignments to b
1153 between the mem ref and the inc. */
1155 other_insn
1156 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
1157 if (other_insn && luid > DF_INSN_LUID (other_insn))
1158 return false;
1161 /* Need to check that the next use of the add result is later than
1162 add insn since this will be the reg incremented. */
1163 other_insn
1164 = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
1165 if (other_insn && luid > DF_INSN_LUID (other_insn))
1166 return false;
1168 else /* FORM_POST_INC. There is less to check here because we
1169 know that operands must line up. */
1171 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1172 /* See comment above on find_inc (false) call. */
1174 if (first_try)
1176 std::swap (mem_insn.reg0, mem_insn.reg1);
1177 return find_inc (false);
1179 else
1180 return false;
1183 /* To have gotten here know that.
1184 *(a + b)
1186 ... = (a + b)
1188 We also know that the lhs of the inc is not b. We need to make
1189 sure that there are no assignments to b between the mem ref and
1190 the inc. */
1191 other_insn
1192 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1193 if (other_insn && luid > DF_INSN_LUID (other_insn))
1194 return false;
1198 if (inc_insn.form == FORM_POST_INC)
1200 other_insn
1201 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_use);
1202 /* When we found inc_insn, we were looking for the
1203 next add or inc, not the next insn that used the
1204 reg. Because we are going to increment the reg
1205 in this form, we need to make sure that there
1206 were no intervening uses of reg. */
1207 if (inc_insn.insn != other_insn)
1208 return false;
1211 return try_merge ();
1215 /* A recursive function that walks ADDRESS_OF_X to find all of the mem
1216 uses in pat that could be used as an auto inc or dec. It then
1217 calls FIND_INC for each one. */
1219 static bool
1220 find_mem (rtx *address_of_x)
1222 rtx x = *address_of_x;
1223 enum rtx_code code = GET_CODE (x);
1224 const char *const fmt = GET_RTX_FORMAT (code);
1225 int i;
1227 if (code == MEM && REG_P (XEXP (x, 0)))
1229 /* Match with *reg0. */
1230 mem_insn.mem_loc = address_of_x;
1231 mem_insn.reg0 = XEXP (x, 0);
1232 mem_insn.reg1_is_const = true;
1233 mem_insn.reg1_val = 0;
1234 mem_insn.reg1 = GEN_INT (0);
1235 if (find_inc (true))
1236 return true;
1238 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
1239 && REG_P (XEXP (XEXP (x, 0), 0)))
1241 rtx reg1 = XEXP (XEXP (x, 0), 1);
1242 mem_insn.mem_loc = address_of_x;
1243 mem_insn.reg0 = XEXP (XEXP (x, 0), 0);
1244 mem_insn.reg1 = reg1;
1245 if (CONST_INT_P (reg1))
1247 mem_insn.reg1_is_const = true;
1248 /* Match with *(reg0 + c) where c is a const. */
1249 mem_insn.reg1_val = INTVAL (reg1);
1250 if (find_inc (true))
1251 return true;
1253 else if (REG_P (reg1))
1255 /* Match with *(reg0 + reg1). */
1256 mem_insn.reg1_is_const = false;
1257 if (find_inc (true))
1258 return true;
1262 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
1264 /* If REG occurs inside a MEM used in a bit-field reference,
1265 that is unacceptable. */
1266 return false;
1269 /* Time for some deep diving. */
1270 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1272 if (fmt[i] == 'e')
1274 if (find_mem (&XEXP (x, i)))
1275 return true;
1277 else if (fmt[i] == 'E')
1279 int j;
1280 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1281 if (find_mem (&XVECEXP (x, i, j)))
1282 return true;
1285 return false;
1289 /* Try to combine all incs and decs by constant values with memory
1290 references in BB. */
1292 static void
1293 merge_in_block (int max_reg, basic_block bb)
1295 rtx_insn *insn;
1296 rtx_insn *curr;
1297 int success_in_block = 0;
1299 if (dump_file)
1300 fprintf (dump_file, "\n\nstarting bb %d\n", bb->index);
1302 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, curr)
1304 bool insn_is_add_or_inc = true;
1306 if (!NONDEBUG_INSN_P (insn))
1307 continue;
1309 /* This continue is deliberate. We do not want the uses of the
1310 jump put into reg_next_use because it is not considered safe to
1311 combine a preincrement with a jump. */
1312 if (JUMP_P (insn))
1313 continue;
1315 if (dump_file)
1316 dump_insn_slim (dump_file, insn);
1318 /* Does this instruction increment or decrement a register? */
1319 if (parse_add_or_inc (insn, true))
1321 int regno = REGNO (inc_insn.reg_res);
1322 /* Cannot handle case where there are three separate regs
1323 before a mem ref. Too many moves would be needed to be
1324 profitable. */
1325 if ((inc_insn.form == FORM_PRE_INC) || inc_insn.reg1_is_const)
1327 mem_insn.insn = get_next_ref (regno, bb, reg_next_use);
1328 if (mem_insn.insn)
1330 bool ok = true;
1331 if (!inc_insn.reg1_is_const)
1333 /* We are only here if we are going to try a
1334 HAVE_*_MODIFY_REG type transformation. c is a
1335 reg and we must sure that the path from the
1336 inc_insn to the mem_insn.insn is both def and use
1337 clear of c because the inc insn is going to move
1338 into the mem_insn.insn. */
1339 int luid = DF_INSN_LUID (mem_insn.insn);
1340 rtx_insn *other_insn
1341 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1343 if (other_insn && luid > DF_INSN_LUID (other_insn))
1344 ok = false;
1346 other_insn
1347 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1349 if (other_insn && luid > DF_INSN_LUID (other_insn))
1350 ok = false;
1353 if (dump_file)
1354 dump_inc_insn (dump_file);
1356 if (ok && find_address (&PATTERN (mem_insn.insn)) == -1)
1358 if (dump_file)
1359 dump_mem_insn (dump_file);
1360 if (try_merge ())
1362 success_in_block++;
1363 insn_is_add_or_inc = false;
1369 else
1371 insn_is_add_or_inc = false;
1372 mem_insn.insn = insn;
1373 if (find_mem (&PATTERN (insn)))
1374 success_in_block++;
1377 /* If the inc insn was merged with a mem, the inc insn is gone
1378 and there is noting to update. */
1379 if (df_insn_info *insn_info = DF_INSN_INFO_GET (insn))
1381 df_ref def, use;
1383 /* Need to update next use. */
1384 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1386 reg_next_use[DF_REF_REGNO (def)] = NULL;
1387 reg_next_inc_use[DF_REF_REGNO (def)] = NULL;
1388 reg_next_def[DF_REF_REGNO (def)] = insn;
1391 FOR_EACH_INSN_INFO_USE (use, insn_info)
1393 reg_next_use[DF_REF_REGNO (use)] = insn;
1394 if (insn_is_add_or_inc)
1395 reg_next_inc_use[DF_REF_REGNO (use)] = insn;
1396 else
1397 reg_next_inc_use[DF_REF_REGNO (use)] = NULL;
1400 else if (dump_file)
1401 fprintf (dump_file, "skipping update of deleted insn %d\n",
1402 INSN_UID (insn));
1405 /* If we were successful, try again. There may have been several
1406 opportunities that were interleaved. This is rare but
1407 gcc.c-torture/compile/pr17273.c actually exhibits this. */
1408 if (success_in_block)
1410 /* In this case, we must clear these vectors since the trick of
1411 testing if the stale insn in the block will not work. */
1412 memset (reg_next_use, 0, max_reg * sizeof (rtx));
1413 memset (reg_next_inc_use, 0, max_reg * sizeof (rtx));
1414 memset (reg_next_def, 0, max_reg * sizeof (rtx));
1415 df_recompute_luids (bb);
1416 merge_in_block (max_reg, bb);
1420 /* Discover auto-inc auto-dec instructions. */
1422 namespace {
1424 const pass_data pass_data_inc_dec =
1426 RTL_PASS, /* type */
1427 "auto_inc_dec", /* name */
1428 OPTGROUP_NONE, /* optinfo_flags */
1429 TV_AUTO_INC_DEC, /* tv_id */
1430 0, /* properties_required */
1431 0, /* properties_provided */
1432 0, /* properties_destroyed */
1433 0, /* todo_flags_start */
1434 TODO_df_finish, /* todo_flags_finish */
1437 class pass_inc_dec : public rtl_opt_pass
1439 public:
1440 pass_inc_dec (gcc::context *ctxt)
1441 : rtl_opt_pass (pass_data_inc_dec, ctxt)
1444 /* opt_pass methods: */
1445 virtual bool gate (function *)
1447 if (!AUTO_INC_DEC)
1448 return false;
1450 return (optimize > 0 && flag_auto_inc_dec);
1454 unsigned int execute (function *);
1456 }; // class pass_inc_dec
1458 unsigned int
1459 pass_inc_dec::execute (function *fun ATTRIBUTE_UNUSED)
1461 if (!AUTO_INC_DEC)
1462 return 0;
1464 basic_block bb;
1465 int max_reg = max_reg_num ();
1467 if (!initialized)
1468 init_decision_table ();
1470 mem_tmp = gen_rtx_MEM (Pmode, NULL_RTX);
1472 df_note_add_problem ();
1473 df_analyze ();
1475 reg_next_use = XCNEWVEC (rtx_insn *, max_reg);
1476 reg_next_inc_use = XCNEWVEC (rtx_insn *, max_reg);
1477 reg_next_def = XCNEWVEC (rtx_insn *, max_reg);
1478 FOR_EACH_BB_FN (bb, fun)
1479 merge_in_block (max_reg, bb);
1481 free (reg_next_use);
1482 free (reg_next_inc_use);
1483 free (reg_next_def);
1485 mem_tmp = NULL;
1487 return 0;
1490 } // anon namespace
1492 rtl_opt_pass *
1493 make_pass_inc_dec (gcc::context *ctxt)
1495 return new pass_inc_dec (ctxt);