2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / auto-inc-dec.c
blobdf52229322d79e17bc07d73067c1488760c7fdb6
1 /* Discovery of auto-inc and auto-dec instructions.
2 Copyright (C) 2006-2015 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 "tm.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "hard-reg-set.h"
31 #include "predict.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "cfgrtl.h"
36 #include "basic-block.h"
37 #include "insn-config.h"
38 #include "regs.h"
39 #include "flags.h"
40 #include "except.h"
41 #include "diagnostic-core.h"
42 #include "recog.h"
43 #include "expmed.h"
44 #include "dojump.h"
45 #include "explow.h"
46 #include "calls.h"
47 #include "emit-rtl.h"
48 #include "varasm.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "tree-pass.h"
52 #include "df.h"
53 #include "dbgcnt.h"
54 #include "target.h"
56 /* This pass was originally removed from flow.c. However there is
57 almost nothing that remains of that code.
59 There are (4) basic forms that are matched:
61 (1) FORM_PRE_ADD
62 a <- b + c
63 ...
66 becomes
68 a <- b
69 ...
70 *(a += c) pre
73 (2) FORM_PRE_INC
74 a += c
75 ...
78 becomes
80 *(a += c) pre
83 (3) FORM_POST_ADD
85 ...
86 b <- a + c
88 (For this case to be true, b must not be assigned or used between
89 the *a and the assignment to b. B must also be a Pmode reg.)
91 becomes
93 b <- a
94 ...
95 *(b += c) post
98 (4) FORM_POST_INC
101 a <- a + c
103 becomes
105 *(a += c) post
107 There are three types of values of c.
109 1) c is a constant equal to the width of the value being accessed by
110 the pointer. This is useful for machines that have
111 HAVE_PRE_INCREMENT, HAVE_POST_INCREMENT, HAVE_PRE_DECREMENT or
112 HAVE_POST_DECREMENT defined.
114 2) c is a constant not equal to the width of the value being accessed
115 by the pointer. This is useful for machines that have
116 HAVE_PRE_MODIFY_DISP, HAVE_POST_MODIFY_DISP defined.
118 3) c is a register. This is useful for machines that have
119 HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
121 The is one special case: if a already had an offset equal to it +-
122 its width and that offset is equal to -c when the increment was
123 before the ref or +c if the increment was after the ref, then if we
124 can do the combination but switch the pre/post bit. */
126 #ifdef AUTO_INC_DEC
128 enum form
130 FORM_PRE_ADD,
131 FORM_PRE_INC,
132 FORM_POST_ADD,
133 FORM_POST_INC,
134 FORM_last
137 /* The states of the second operands of mem refs and inc insns. If no
138 second operand of the mem_ref was found, it is assumed to just be
139 ZERO. SIZE is the size of the mode accessed in the memref. The
140 ANY is used for constants that are not +-size or 0. REG is used if
141 the forms are reg1 + reg2. */
143 enum inc_state
145 INC_ZERO, /* == 0 */
146 INC_NEG_SIZE, /* == +size */
147 INC_POS_SIZE, /* == -size */
148 INC_NEG_ANY, /* == some -constant */
149 INC_POS_ANY, /* == some +constant */
150 INC_REG, /* == some register */
151 INC_last
154 /* The eight forms that pre/post inc/dec can take. */
155 enum gen_form
157 NOTHING,
158 SIMPLE_PRE_INC, /* ++size */
159 SIMPLE_POST_INC, /* size++ */
160 SIMPLE_PRE_DEC, /* --size */
161 SIMPLE_POST_DEC, /* size-- */
162 DISP_PRE, /* ++con */
163 DISP_POST, /* con++ */
164 REG_PRE, /* ++reg */
165 REG_POST /* reg++ */
168 /* Tmp mem rtx for use in cost modeling. */
169 static rtx mem_tmp;
171 static enum inc_state
172 set_inc_state (HOST_WIDE_INT val, int size)
174 if (val == 0)
175 return INC_ZERO;
176 if (val < 0)
177 return (val == -size) ? INC_NEG_SIZE : INC_NEG_ANY;
178 else
179 return (val == size) ? INC_POS_SIZE : INC_POS_ANY;
182 /* The DECISION_TABLE that describes what form, if any, the increment
183 or decrement will take. It is a three dimensional table. The first
184 index is the type of constant or register found as the second
185 operand of the inc insn. The second index is the type of constant
186 or register found as the second operand of the memory reference (if
187 no second operand exists, 0 is used). The third index is the form
188 and location (relative to the mem reference) of inc insn. */
190 static bool initialized = false;
191 static enum gen_form decision_table[INC_last][INC_last][FORM_last];
193 static void
194 init_decision_table (void)
196 enum gen_form value;
198 if (HAVE_PRE_INCREMENT || HAVE_PRE_MODIFY_DISP)
200 /* Prefer the simple form if both are available. */
201 value = (HAVE_PRE_INCREMENT) ? SIMPLE_PRE_INC : DISP_PRE;
203 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
204 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_INC] = value;
206 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_ADD] = value;
207 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_INC] = value;
210 if (HAVE_POST_INCREMENT || HAVE_POST_MODIFY_DISP)
212 /* Prefer the simple form if both are available. */
213 value = (HAVE_POST_INCREMENT) ? SIMPLE_POST_INC : DISP_POST;
215 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_ADD] = value;
216 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_INC] = value;
218 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_ADD] = value;
219 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_INC] = value;
222 if (HAVE_PRE_DECREMENT || HAVE_PRE_MODIFY_DISP)
224 /* Prefer the simple form if both are available. */
225 value = (HAVE_PRE_DECREMENT) ? SIMPLE_PRE_DEC : DISP_PRE;
227 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
228 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_INC] = value;
230 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_ADD] = value;
231 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_INC] = value;
234 if (HAVE_POST_DECREMENT || HAVE_POST_MODIFY_DISP)
236 /* Prefer the simple form if both are available. */
237 value = (HAVE_POST_DECREMENT) ? SIMPLE_POST_DEC : DISP_POST;
239 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_ADD] = value;
240 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_INC] = value;
242 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_ADD] = value;
243 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_INC] = value;
246 if (HAVE_PRE_MODIFY_DISP)
248 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
249 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
251 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_ADD] = DISP_PRE;
252 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_INC] = DISP_PRE;
254 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
255 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
257 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_ADD] = DISP_PRE;
258 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_INC] = DISP_PRE;
261 if (HAVE_POST_MODIFY_DISP)
263 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
264 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
266 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_ADD] = DISP_POST;
267 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_INC] = DISP_POST;
269 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
270 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
272 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_ADD] = DISP_POST;
273 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_INC] = DISP_POST;
276 /* This is much simpler than the other cases because we do not look
277 for the reg1-reg2 case. Note that we do not have a INC_POS_REG
278 and INC_NEG_REG states. Most of the use of such states would be
279 on a target that had an R1 - R2 update address form.
281 There is the remote possibility that you could also catch a = a +
282 b; *(a - b) as a postdecrement of (a + b). However, it is
283 unclear if *(a - b) would ever be generated on a machine that did
284 not have that kind of addressing mode. The IA-64 and RS6000 will
285 not do this, and I cannot speak for any other. If any
286 architecture does have an a-b update for, these cases should be
287 added. */
288 if (HAVE_PRE_MODIFY_REG)
290 decision_table[INC_REG][INC_ZERO][FORM_PRE_ADD] = REG_PRE;
291 decision_table[INC_REG][INC_ZERO][FORM_PRE_INC] = REG_PRE;
293 decision_table[INC_REG][INC_REG][FORM_POST_ADD] = REG_PRE;
294 decision_table[INC_REG][INC_REG][FORM_POST_INC] = REG_PRE;
297 if (HAVE_POST_MODIFY_REG)
299 decision_table[INC_REG][INC_ZERO][FORM_POST_ADD] = REG_POST;
300 decision_table[INC_REG][INC_ZERO][FORM_POST_INC] = REG_POST;
303 initialized = true;
306 /* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
307 "reg_res = reg0+c". */
309 static struct inc_insn
311 rtx_insn *insn; /* The insn being parsed. */
312 rtx pat; /* The pattern of the insn. */
313 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
314 enum form form;
315 rtx reg_res;
316 rtx reg0;
317 rtx reg1;
318 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
319 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
320 } inc_insn;
323 /* Dump the parsed inc insn to FILE. */
325 static void
326 dump_inc_insn (FILE *file)
328 const char *f = ((inc_insn.form == FORM_PRE_ADD)
329 || (inc_insn.form == FORM_PRE_INC)) ? "pre" : "post";
331 dump_insn_slim (file, inc_insn.insn);
333 switch (inc_insn.form)
335 case FORM_PRE_ADD:
336 case FORM_POST_ADD:
337 if (inc_insn.reg1_is_const)
338 fprintf (file, "found %s add(%d) r[%d]=r[%d]+%d\n",
339 f, INSN_UID (inc_insn.insn),
340 REGNO (inc_insn.reg_res),
341 REGNO (inc_insn.reg0), (int) inc_insn.reg1_val);
342 else
343 fprintf (file, "found %s add(%d) r[%d]=r[%d]+r[%d]\n",
344 f, INSN_UID (inc_insn.insn),
345 REGNO (inc_insn.reg_res),
346 REGNO (inc_insn.reg0), REGNO (inc_insn.reg1));
347 break;
349 case FORM_PRE_INC:
350 case FORM_POST_INC:
351 if (inc_insn.reg1_is_const)
352 fprintf (file, "found %s inc(%d) r[%d]+=%d\n",
353 f, INSN_UID (inc_insn.insn),
354 REGNO (inc_insn.reg_res), (int) inc_insn.reg1_val);
355 else
356 fprintf (file, "found %s inc(%d) r[%d]+=r[%d]\n",
357 f, INSN_UID (inc_insn.insn),
358 REGNO (inc_insn.reg_res), REGNO (inc_insn.reg1));
359 break;
361 default:
362 break;
367 /* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
369 static struct mem_insn
371 rtx_insn *insn; /* The insn being parsed. */
372 rtx pat; /* The pattern of the insn. */
373 rtx *mem_loc; /* The address of the field that holds the mem */
374 /* that is to be replaced. */
375 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
376 rtx reg0;
377 rtx reg1; /* This is either a reg or a const depending on
378 reg1_is_const. */
379 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
380 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
381 } mem_insn;
384 /* Dump the parsed mem insn to FILE. */
386 static void
387 dump_mem_insn (FILE *file)
389 dump_insn_slim (file, mem_insn.insn);
391 if (mem_insn.reg1_is_const)
392 fprintf (file, "found mem(%d) *(r[%d]+%d)\n",
393 INSN_UID (mem_insn.insn),
394 REGNO (mem_insn.reg0), (int) mem_insn.reg1_val);
395 else
396 fprintf (file, "found mem(%d) *(r[%d]+r[%d])\n",
397 INSN_UID (mem_insn.insn),
398 REGNO (mem_insn.reg0), REGNO (mem_insn.reg1));
402 /* The following three arrays contain pointers to instructions. They
403 are indexed by REGNO. At any point in the basic block where we are
404 looking these three arrays contain, respectively, the next insn
405 that uses REGNO, the next inc or add insn that uses REGNO and the
406 next insn that sets REGNO.
408 The arrays are not cleared when we move from block to block so
409 whenever an insn is retrieved from these arrays, it's block number
410 must be compared with the current block.
413 static rtx_insn **reg_next_use = NULL;
414 static rtx_insn **reg_next_inc_use = NULL;
415 static rtx_insn **reg_next_def = NULL;
418 /* Move dead note that match PATTERN to TO_INSN from FROM_INSN. We do
419 not really care about moving any other notes from the inc or add
420 insn. Moving the REG_EQUAL and REG_EQUIV is clearly wrong and it
421 does not appear that there are any other kinds of relevant notes. */
423 static void
424 move_dead_notes (rtx_insn *to_insn, rtx_insn *from_insn, rtx pattern)
426 rtx note;
427 rtx next_note;
428 rtx prev_note = NULL;
430 for (note = REG_NOTES (from_insn); note; note = next_note)
432 next_note = XEXP (note, 1);
434 if ((REG_NOTE_KIND (note) == REG_DEAD)
435 && pattern == XEXP (note, 0))
437 XEXP (note, 1) = REG_NOTES (to_insn);
438 REG_NOTES (to_insn) = note;
439 if (prev_note)
440 XEXP (prev_note, 1) = next_note;
441 else
442 REG_NOTES (from_insn) = next_note;
444 else prev_note = note;
449 /* Create a mov insn DEST_REG <- SRC_REG and insert it before
450 NEXT_INSN. */
452 static rtx_insn *
453 insert_move_insn_before (rtx_insn *next_insn, rtx dest_reg, rtx src_reg)
455 rtx_insn *insns;
457 start_sequence ();
458 emit_move_insn (dest_reg, src_reg);
459 insns = get_insns ();
460 end_sequence ();
461 emit_insn_before (insns, next_insn);
462 return insns;
466 /* Change mem_insn.mem_loc so that uses NEW_ADDR which has an
467 increment of INC_REG. To have reached this point, the change is a
468 legitimate one from a dataflow point of view. The only questions
469 are is this a valid change to the instruction and is this a
470 profitable change to the instruction. */
472 static bool
473 attempt_change (rtx new_addr, rtx inc_reg)
475 /* There are four cases: For the two cases that involve an add
476 instruction, we are going to have to delete the add and insert a
477 mov. We are going to assume that the mov is free. This is
478 fairly early in the backend and there are a lot of opportunities
479 for removing that move later. In particular, there is the case
480 where the move may be dead, this is what dead code elimination
481 passes are for. The two cases where we have an inc insn will be
482 handled mov free. */
484 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
485 rtx_insn *mov_insn = NULL;
486 int regno;
487 rtx mem = *mem_insn.mem_loc;
488 machine_mode mode = GET_MODE (mem);
489 rtx new_mem;
490 int old_cost = 0;
491 int new_cost = 0;
492 bool speed = optimize_bb_for_speed_p (bb);
494 PUT_MODE (mem_tmp, mode);
495 XEXP (mem_tmp, 0) = new_addr;
497 old_cost = (set_src_cost (mem, speed)
498 + set_rtx_cost (PATTERN (inc_insn.insn), speed));
499 new_cost = set_src_cost (mem_tmp, speed);
501 /* The first item of business is to see if this is profitable. */
502 if (old_cost < new_cost)
504 if (dump_file)
505 fprintf (dump_file, "cost failure old=%d new=%d\n", old_cost, new_cost);
506 return false;
509 /* Jump through a lot of hoops to keep the attributes up to date. We
510 do not want to call one of the change address variants that take
511 an offset even though we know the offset in many cases. These
512 assume you are changing where the address is pointing by the
513 offset. */
514 new_mem = replace_equiv_address_nv (mem, new_addr);
515 if (! validate_change (mem_insn.insn, mem_insn.mem_loc, new_mem, 0))
517 if (dump_file)
518 fprintf (dump_file, "validation failure\n");
519 return false;
522 /* From here to the end of the function we are committed to the
523 change, i.e. nothing fails. Generate any necessary movs, move
524 any regnotes, and fix up the reg_next_{use,inc_use,def}. */
525 switch (inc_insn.form)
527 case FORM_PRE_ADD:
528 /* Replace the addition with a move. Do it at the location of
529 the addition since the operand of the addition may change
530 before the memory reference. */
531 mov_insn = insert_move_insn_before (inc_insn.insn,
532 inc_insn.reg_res, inc_insn.reg0);
533 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
535 regno = REGNO (inc_insn.reg_res);
536 reg_next_def[regno] = mov_insn;
537 reg_next_use[regno] = NULL;
538 regno = REGNO (inc_insn.reg0);
539 reg_next_use[regno] = mov_insn;
540 df_recompute_luids (bb);
541 break;
543 case FORM_POST_INC:
544 regno = REGNO (inc_insn.reg_res);
545 if (reg_next_use[regno] == reg_next_inc_use[regno])
546 reg_next_inc_use[regno] = NULL;
548 /* Fallthru. */
549 case FORM_PRE_INC:
550 regno = REGNO (inc_insn.reg_res);
551 reg_next_def[regno] = mem_insn.insn;
552 reg_next_use[regno] = NULL;
554 break;
556 case FORM_POST_ADD:
557 mov_insn = insert_move_insn_before (mem_insn.insn,
558 inc_insn.reg_res, inc_insn.reg0);
559 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
561 /* Do not move anything to the mov insn because the instruction
562 pointer for the main iteration has not yet hit that. It is
563 still pointing to the mem insn. */
564 regno = REGNO (inc_insn.reg_res);
565 reg_next_def[regno] = mem_insn.insn;
566 reg_next_use[regno] = NULL;
568 regno = REGNO (inc_insn.reg0);
569 reg_next_use[regno] = mem_insn.insn;
570 if ((reg_next_use[regno] == reg_next_inc_use[regno])
571 || (reg_next_inc_use[regno] == inc_insn.insn))
572 reg_next_inc_use[regno] = NULL;
573 df_recompute_luids (bb);
574 break;
576 case FORM_last:
577 default:
578 gcc_unreachable ();
581 if (!inc_insn.reg1_is_const)
583 regno = REGNO (inc_insn.reg1);
584 reg_next_use[regno] = mem_insn.insn;
585 if ((reg_next_use[regno] == reg_next_inc_use[regno])
586 || (reg_next_inc_use[regno] == inc_insn.insn))
587 reg_next_inc_use[regno] = NULL;
590 delete_insn (inc_insn.insn);
592 if (dump_file && mov_insn)
594 fprintf (dump_file, "inserting mov ");
595 dump_insn_slim (dump_file, mov_insn);
598 /* Record that this insn has an implicit side effect. */
599 add_reg_note (mem_insn.insn, REG_INC, inc_reg);
601 if (dump_file)
603 fprintf (dump_file, "****success ");
604 dump_insn_slim (dump_file, mem_insn.insn);
607 return true;
611 /* Try to combine the instruction in INC_INSN with the instruction in
612 MEM_INSN. First the form is determined using the DECISION_TABLE
613 and the results of parsing the INC_INSN and the MEM_INSN.
614 Assuming the form is ok, a prototype new address is built which is
615 passed to ATTEMPT_CHANGE for final processing. */
617 static bool
618 try_merge (void)
620 enum gen_form gen_form;
621 rtx mem = *mem_insn.mem_loc;
622 rtx inc_reg = inc_insn.form == FORM_POST_ADD ?
623 inc_insn.reg_res : mem_insn.reg0;
625 /* The width of the mem being accessed. */
626 int size = GET_MODE_SIZE (GET_MODE (mem));
627 rtx_insn *last_insn = NULL;
628 machine_mode reg_mode = GET_MODE (inc_reg);
630 switch (inc_insn.form)
632 case FORM_PRE_ADD:
633 case FORM_PRE_INC:
634 last_insn = mem_insn.insn;
635 break;
636 case FORM_POST_INC:
637 case FORM_POST_ADD:
638 last_insn = inc_insn.insn;
639 break;
640 case FORM_last:
641 default:
642 gcc_unreachable ();
645 /* Cannot handle auto inc of the stack. */
646 if (inc_reg == stack_pointer_rtx)
648 if (dump_file)
649 fprintf (dump_file, "cannot inc stack %d failure\n", REGNO (inc_reg));
650 return false;
653 /* Look to see if the inc register is dead after the memory
654 reference. If it is, do not do the combination. */
655 if (find_regno_note (last_insn, REG_DEAD, REGNO (inc_reg)))
657 if (dump_file)
658 fprintf (dump_file, "dead failure %d\n", REGNO (inc_reg));
659 return false;
662 mem_insn.reg1_state = (mem_insn.reg1_is_const)
663 ? set_inc_state (mem_insn.reg1_val, size) : INC_REG;
664 inc_insn.reg1_state = (inc_insn.reg1_is_const)
665 ? set_inc_state (inc_insn.reg1_val, size) : INC_REG;
667 /* Now get the form that we are generating. */
668 gen_form = decision_table
669 [inc_insn.reg1_state][mem_insn.reg1_state][inc_insn.form];
671 if (dbg_cnt (auto_inc_dec) == false)
672 return false;
674 switch (gen_form)
676 default:
677 case NOTHING:
678 return false;
680 case SIMPLE_PRE_INC: /* ++size */
681 if (dump_file)
682 fprintf (dump_file, "trying SIMPLE_PRE_INC\n");
683 return attempt_change (gen_rtx_PRE_INC (reg_mode, inc_reg), inc_reg);
684 break;
686 case SIMPLE_POST_INC: /* size++ */
687 if (dump_file)
688 fprintf (dump_file, "trying SIMPLE_POST_INC\n");
689 return attempt_change (gen_rtx_POST_INC (reg_mode, inc_reg), inc_reg);
690 break;
692 case SIMPLE_PRE_DEC: /* --size */
693 if (dump_file)
694 fprintf (dump_file, "trying SIMPLE_PRE_DEC\n");
695 return attempt_change (gen_rtx_PRE_DEC (reg_mode, inc_reg), inc_reg);
696 break;
698 case SIMPLE_POST_DEC: /* size-- */
699 if (dump_file)
700 fprintf (dump_file, "trying SIMPLE_POST_DEC\n");
701 return attempt_change (gen_rtx_POST_DEC (reg_mode, inc_reg), inc_reg);
702 break;
704 case DISP_PRE: /* ++con */
705 if (dump_file)
706 fprintf (dump_file, "trying DISP_PRE\n");
707 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
708 inc_reg,
709 gen_rtx_PLUS (reg_mode,
710 inc_reg,
711 inc_insn.reg1)),
712 inc_reg);
713 break;
715 case DISP_POST: /* con++ */
716 if (dump_file)
717 fprintf (dump_file, "trying POST_DISP\n");
718 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
719 inc_reg,
720 gen_rtx_PLUS (reg_mode,
721 inc_reg,
722 inc_insn.reg1)),
723 inc_reg);
724 break;
726 case REG_PRE: /* ++reg */
727 if (dump_file)
728 fprintf (dump_file, "trying PRE_REG\n");
729 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
730 inc_reg,
731 gen_rtx_PLUS (reg_mode,
732 inc_reg,
733 inc_insn.reg1)),
734 inc_reg);
735 break;
737 case REG_POST: /* reg++ */
738 if (dump_file)
739 fprintf (dump_file, "trying POST_REG\n");
740 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
741 inc_reg,
742 gen_rtx_PLUS (reg_mode,
743 inc_reg,
744 inc_insn.reg1)),
745 inc_reg);
746 break;
750 /* Return the next insn that uses (if reg_next_use is passed in
751 NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
752 REGNO in BB. */
754 static rtx_insn *
755 get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
757 rtx_insn *insn = next_array[regno];
759 /* Lazy about cleaning out the next_arrays. */
760 if (insn && BLOCK_FOR_INSN (insn) != bb)
762 next_array[regno] = NULL;
763 insn = NULL;
766 return insn;
770 /* Return true if INSN is of a form "a = b op c" where a and b are
771 regs. op is + if c is a reg and +|- if c is a const. Fill in
772 INC_INSN with what is found.
774 This function is called in two contexts, if BEFORE_MEM is true,
775 this is called for each insn in the basic block. If BEFORE_MEM is
776 false, it is called for the instruction in the block that uses the
777 index register for some memory reference that is currently being
778 processed. */
780 static bool
781 parse_add_or_inc (rtx_insn *insn, bool before_mem)
783 rtx pat = single_set (insn);
784 if (!pat)
785 return false;
787 /* Result must be single reg. */
788 if (!REG_P (SET_DEST (pat)))
789 return false;
791 if ((GET_CODE (SET_SRC (pat)) != PLUS)
792 && (GET_CODE (SET_SRC (pat)) != MINUS))
793 return false;
795 if (!REG_P (XEXP (SET_SRC (pat), 0)))
796 return false;
798 inc_insn.insn = insn;
799 inc_insn.pat = pat;
800 inc_insn.reg_res = SET_DEST (pat);
801 inc_insn.reg0 = XEXP (SET_SRC (pat), 0);
802 if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg0))
803 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
804 else
805 inc_insn.form = before_mem ? FORM_PRE_ADD : FORM_POST_ADD;
807 if (CONST_INT_P (XEXP (SET_SRC (pat), 1)))
809 /* Process a = b + c where c is a const. */
810 inc_insn.reg1_is_const = true;
811 if (GET_CODE (SET_SRC (pat)) == PLUS)
813 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
814 inc_insn.reg1_val = INTVAL (inc_insn.reg1);
816 else
818 inc_insn.reg1_val = -INTVAL (XEXP (SET_SRC (pat), 1));
819 inc_insn.reg1 = GEN_INT (inc_insn.reg1_val);
821 return true;
823 else if ((HAVE_PRE_MODIFY_REG || HAVE_POST_MODIFY_REG)
824 && (REG_P (XEXP (SET_SRC (pat), 1)))
825 && GET_CODE (SET_SRC (pat)) == PLUS)
827 /* Process a = b + c where c is a reg. */
828 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
829 inc_insn.reg1_is_const = false;
831 if (inc_insn.form == FORM_PRE_INC
832 || inc_insn.form == FORM_POST_INC)
833 return true;
834 else if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg1))
836 /* Reverse the two operands and turn *_ADD into *_INC since
837 a = c + a. */
838 std::swap (inc_insn.reg0, inc_insn.reg1);
839 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
840 return true;
842 else
843 return true;
846 return false;
850 /* A recursive function that checks all of the mem uses in
851 ADDRESS_OF_X to see if any single one of them is compatible with
852 what has been found in inc_insn.
854 -1 is returned for success. 0 is returned if nothing was found and
855 1 is returned for failure. */
857 static int
858 find_address (rtx *address_of_x)
860 rtx x = *address_of_x;
861 enum rtx_code code = GET_CODE (x);
862 const char *const fmt = GET_RTX_FORMAT (code);
863 int i;
864 int value = 0;
865 int tem;
867 if (code == MEM && rtx_equal_p (XEXP (x, 0), inc_insn.reg_res))
869 /* Match with *reg0. */
870 mem_insn.mem_loc = address_of_x;
871 mem_insn.reg0 = inc_insn.reg_res;
872 mem_insn.reg1_is_const = true;
873 mem_insn.reg1_val = 0;
874 mem_insn.reg1 = GEN_INT (0);
875 return -1;
877 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
878 && rtx_equal_p (XEXP (XEXP (x, 0), 0), inc_insn.reg_res))
880 rtx b = XEXP (XEXP (x, 0), 1);
881 mem_insn.mem_loc = address_of_x;
882 mem_insn.reg0 = inc_insn.reg_res;
883 mem_insn.reg1 = b;
884 mem_insn.reg1_is_const = inc_insn.reg1_is_const;
885 if (CONST_INT_P (b))
887 /* Match with *(reg0 + reg1) where reg1 is a const. */
888 HOST_WIDE_INT val = INTVAL (b);
889 if (inc_insn.reg1_is_const
890 && (inc_insn.reg1_val == val || inc_insn.reg1_val == -val))
892 mem_insn.reg1_val = val;
893 return -1;
896 else if (!inc_insn.reg1_is_const
897 && rtx_equal_p (inc_insn.reg1, b))
898 /* Match with *(reg0 + reg1). */
899 return -1;
902 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
904 /* If REG occurs inside a MEM used in a bit-field reference,
905 that is unacceptable. */
906 if (find_address (&XEXP (x, 0)))
907 return 1;
910 if (x == inc_insn.reg_res)
911 return 1;
913 /* Time for some deep diving. */
914 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
916 if (fmt[i] == 'e')
918 tem = find_address (&XEXP (x, i));
919 /* If this is the first use, let it go so the rest of the
920 insn can be checked. */
921 if (value == 0)
922 value = tem;
923 else if (tem != 0)
924 /* More than one match was found. */
925 return 1;
927 else if (fmt[i] == 'E')
929 int j;
930 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
932 tem = find_address (&XVECEXP (x, i, j));
933 /* If this is the first use, let it go so the rest of
934 the insn can be checked. */
935 if (value == 0)
936 value = tem;
937 else if (tem != 0)
938 /* More than one match was found. */
939 return 1;
943 return value;
946 /* Once a suitable mem reference has been found and the MEM_INSN
947 structure has been filled in, FIND_INC is called to see if there is
948 a suitable add or inc insn that follows the mem reference and
949 determine if it is suitable to merge.
951 In the case where the MEM_INSN has two registers in the reference,
952 this function may be called recursively. The first time looking
953 for an add of the first register, and if that fails, looking for an
954 add of the second register. The FIRST_TRY parameter is used to
955 only allow the parameters to be reversed once. */
957 static bool
958 find_inc (bool first_try)
960 rtx_insn *insn;
961 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
962 rtx_insn *other_insn;
963 df_ref def;
965 /* Make sure this reg appears only once in this insn. */
966 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
968 if (dump_file)
969 fprintf (dump_file, "mem count failure\n");
970 return false;
973 if (dump_file)
974 dump_mem_insn (dump_file);
976 /* Find the next use that is an inc. */
977 insn = get_next_ref (REGNO (mem_insn.reg0),
978 BLOCK_FOR_INSN (mem_insn.insn),
979 reg_next_inc_use);
980 if (!insn)
981 return false;
983 /* Even though we know the next use is an add or inc because it came
984 from the reg_next_inc_use, we must still reparse. */
985 if (!parse_add_or_inc (insn, false))
987 /* Next use was not an add. Look for one extra case. It could be
988 that we have:
990 *(a + b)
991 ...= a;
992 ...= b + a
994 if we reverse the operands in the mem ref we would
995 find this. Only try it once though. */
996 if (first_try && !mem_insn.reg1_is_const)
998 std::swap (mem_insn.reg0, mem_insn.reg1);
999 return find_inc (false);
1001 else
1002 return false;
1005 /* Need to assure that none of the operands of the inc instruction are
1006 assigned to by the mem insn. */
1007 FOR_EACH_INSN_DEF (def, mem_insn.insn)
1009 unsigned int regno = DF_REF_REGNO (def);
1010 if ((regno == REGNO (inc_insn.reg0))
1011 || (regno == REGNO (inc_insn.reg_res)))
1013 if (dump_file)
1014 fprintf (dump_file, "inc conflicts with store failure.\n");
1015 return false;
1017 if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
1019 if (dump_file)
1020 fprintf (dump_file, "inc conflicts with store failure.\n");
1021 return false;
1025 if (dump_file)
1026 dump_inc_insn (dump_file);
1028 if (inc_insn.form == FORM_POST_ADD)
1030 /* Make sure that there is no insn that assigns to inc_insn.res
1031 between the mem_insn and the inc_insn. */
1032 rtx_insn *other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1033 BLOCK_FOR_INSN (mem_insn.insn),
1034 reg_next_def);
1035 if (other_insn != inc_insn.insn)
1037 if (dump_file)
1038 fprintf (dump_file,
1039 "result of add is assigned to between mem and inc insns.\n");
1040 return false;
1043 other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1044 BLOCK_FOR_INSN (mem_insn.insn),
1045 reg_next_use);
1046 if (other_insn
1047 && (other_insn != inc_insn.insn)
1048 && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
1050 if (dump_file)
1051 fprintf (dump_file,
1052 "result of add is used between mem and inc insns.\n");
1053 return false;
1056 /* For the post_add to work, the result_reg of the inc must not be
1057 used in the mem insn since this will become the new index
1058 register. */
1059 if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
1061 if (dump_file)
1062 fprintf (dump_file, "base reg replacement failure.\n");
1063 return false;
1067 if (mem_insn.reg1_is_const)
1069 if (mem_insn.reg1_val == 0)
1071 if (!inc_insn.reg1_is_const)
1073 /* The mem looks like *r0 and the rhs of the add has two
1074 registers. */
1075 int luid = DF_INSN_LUID (inc_insn.insn);
1076 if (inc_insn.form == FORM_POST_ADD)
1078 /* The trick is that we are not going to increment r0,
1079 we are going to increment the result of the add insn.
1080 For this trick to be correct, the result reg of
1081 the inc must be a valid addressing reg. */
1082 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1083 if (GET_MODE (inc_insn.reg_res)
1084 != targetm.addr_space.address_mode (as))
1086 if (dump_file)
1087 fprintf (dump_file, "base reg mode failure.\n");
1088 return false;
1091 /* We also need to make sure that the next use of
1092 inc result is after the inc. */
1093 other_insn
1094 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1095 if (other_insn && luid > DF_INSN_LUID (other_insn))
1096 return false;
1098 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1099 std::swap (inc_insn.reg0, inc_insn.reg1);
1102 other_insn
1103 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1104 if (other_insn && luid > DF_INSN_LUID (other_insn))
1105 return false;
1108 /* Both the inc/add and the mem have a constant. Need to check
1109 that the constants are ok. */
1110 else if ((mem_insn.reg1_val != inc_insn.reg1_val)
1111 && (mem_insn.reg1_val != -inc_insn.reg1_val))
1112 return false;
1114 else
1116 /* The mem insn is of the form *(a + b) where a and b are both
1117 regs. It may be that in order to match the add or inc we
1118 need to treat it as if it was *(b + a). It may also be that
1119 the add is of the form a + c where c does not match b and
1120 then we just abandon this. */
1122 int luid = DF_INSN_LUID (inc_insn.insn);
1123 rtx_insn *other_insn;
1125 /* Make sure this reg appears only once in this insn. */
1126 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
1127 return false;
1129 if (inc_insn.form == FORM_POST_ADD)
1131 /* For this trick to be correct, the result reg of the inc
1132 must be a valid addressing reg. */
1133 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1134 if (GET_MODE (inc_insn.reg_res)
1135 != targetm.addr_space.address_mode (as))
1137 if (dump_file)
1138 fprintf (dump_file, "base reg mode failure.\n");
1139 return false;
1142 if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1144 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1146 /* See comment above on find_inc (false) call. */
1147 if (first_try)
1149 std::swap (mem_insn.reg0, mem_insn.reg1);
1150 return find_inc (false);
1152 else
1153 return false;
1156 /* Need to check that there are no assignments to b
1157 before the add insn. */
1158 other_insn
1159 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1160 if (other_insn && luid > DF_INSN_LUID (other_insn))
1161 return false;
1162 /* All ok for the next step. */
1164 else
1166 /* We know that mem_insn.reg0 must equal inc_insn.reg1
1167 or else we would not have found the inc insn. */
1168 std::swap (mem_insn.reg0, mem_insn.reg1);
1169 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1171 /* See comment above on find_inc (false) call. */
1172 if (first_try)
1173 return find_inc (false);
1174 else
1175 return false;
1177 /* To have gotten here know that.
1178 *(b + a)
1180 ... = (b + a)
1182 We also know that the lhs of the inc is not b or a. We
1183 need to make sure that there are no assignments to b
1184 between the mem ref and the inc. */
1186 other_insn
1187 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
1188 if (other_insn && luid > DF_INSN_LUID (other_insn))
1189 return false;
1192 /* Need to check that the next use of the add result is later than
1193 add insn since this will be the reg incremented. */
1194 other_insn
1195 = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
1196 if (other_insn && luid > DF_INSN_LUID (other_insn))
1197 return false;
1199 else /* FORM_POST_INC. There is less to check here because we
1200 know that operands must line up. */
1202 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1203 /* See comment above on find_inc (false) call. */
1205 if (first_try)
1207 std::swap (mem_insn.reg0, mem_insn.reg1);
1208 return find_inc (false);
1210 else
1211 return false;
1214 /* To have gotten here know that.
1215 *(a + b)
1217 ... = (a + b)
1219 We also know that the lhs of the inc is not b. We need to make
1220 sure that there are no assignments to b between the mem ref and
1221 the inc. */
1222 other_insn
1223 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1224 if (other_insn && luid > DF_INSN_LUID (other_insn))
1225 return false;
1229 if (inc_insn.form == FORM_POST_INC)
1231 other_insn
1232 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_use);
1233 /* When we found inc_insn, we were looking for the
1234 next add or inc, not the next insn that used the
1235 reg. Because we are going to increment the reg
1236 in this form, we need to make sure that there
1237 were no intervening uses of reg. */
1238 if (inc_insn.insn != other_insn)
1239 return false;
1242 return try_merge ();
1246 /* A recursive function that walks ADDRESS_OF_X to find all of the mem
1247 uses in pat that could be used as an auto inc or dec. It then
1248 calls FIND_INC for each one. */
1250 static bool
1251 find_mem (rtx *address_of_x)
1253 rtx x = *address_of_x;
1254 enum rtx_code code = GET_CODE (x);
1255 const char *const fmt = GET_RTX_FORMAT (code);
1256 int i;
1258 if (code == MEM && REG_P (XEXP (x, 0)))
1260 /* Match with *reg0. */
1261 mem_insn.mem_loc = address_of_x;
1262 mem_insn.reg0 = XEXP (x, 0);
1263 mem_insn.reg1_is_const = true;
1264 mem_insn.reg1_val = 0;
1265 mem_insn.reg1 = GEN_INT (0);
1266 if (find_inc (true))
1267 return true;
1269 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
1270 && REG_P (XEXP (XEXP (x, 0), 0)))
1272 rtx reg1 = XEXP (XEXP (x, 0), 1);
1273 mem_insn.mem_loc = address_of_x;
1274 mem_insn.reg0 = XEXP (XEXP (x, 0), 0);
1275 mem_insn.reg1 = reg1;
1276 if (CONST_INT_P (reg1))
1278 mem_insn.reg1_is_const = true;
1279 /* Match with *(reg0 + c) where c is a const. */
1280 mem_insn.reg1_val = INTVAL (reg1);
1281 if (find_inc (true))
1282 return true;
1284 else if (REG_P (reg1))
1286 /* Match with *(reg0 + reg1). */
1287 mem_insn.reg1_is_const = false;
1288 if (find_inc (true))
1289 return true;
1293 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
1295 /* If REG occurs inside a MEM used in a bit-field reference,
1296 that is unacceptable. */
1297 return false;
1300 /* Time for some deep diving. */
1301 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1303 if (fmt[i] == 'e')
1305 if (find_mem (&XEXP (x, i)))
1306 return true;
1308 else if (fmt[i] == 'E')
1310 int j;
1311 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1312 if (find_mem (&XVECEXP (x, i, j)))
1313 return true;
1316 return false;
1320 /* Try to combine all incs and decs by constant values with memory
1321 references in BB. */
1323 static void
1324 merge_in_block (int max_reg, basic_block bb)
1326 rtx_insn *insn;
1327 rtx_insn *curr;
1328 int success_in_block = 0;
1330 if (dump_file)
1331 fprintf (dump_file, "\n\nstarting bb %d\n", bb->index);
1333 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, curr)
1335 bool insn_is_add_or_inc = true;
1337 if (!NONDEBUG_INSN_P (insn))
1338 continue;
1340 /* This continue is deliberate. We do not want the uses of the
1341 jump put into reg_next_use because it is not considered safe to
1342 combine a preincrement with a jump. */
1343 if (JUMP_P (insn))
1344 continue;
1346 if (dump_file)
1347 dump_insn_slim (dump_file, insn);
1349 /* Does this instruction increment or decrement a register? */
1350 if (parse_add_or_inc (insn, true))
1352 int regno = REGNO (inc_insn.reg_res);
1353 /* Cannot handle case where there are three separate regs
1354 before a mem ref. Too many moves would be needed to be
1355 profitable. */
1356 if ((inc_insn.form == FORM_PRE_INC) || inc_insn.reg1_is_const)
1358 mem_insn.insn = get_next_ref (regno, bb, reg_next_use);
1359 if (mem_insn.insn)
1361 bool ok = true;
1362 if (!inc_insn.reg1_is_const)
1364 /* We are only here if we are going to try a
1365 HAVE_*_MODIFY_REG type transformation. c is a
1366 reg and we must sure that the path from the
1367 inc_insn to the mem_insn.insn is both def and use
1368 clear of c because the inc insn is going to move
1369 into the mem_insn.insn. */
1370 int luid = DF_INSN_LUID (mem_insn.insn);
1371 rtx_insn *other_insn
1372 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1374 if (other_insn && luid > DF_INSN_LUID (other_insn))
1375 ok = false;
1377 other_insn
1378 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1380 if (other_insn && luid > DF_INSN_LUID (other_insn))
1381 ok = false;
1384 if (dump_file)
1385 dump_inc_insn (dump_file);
1387 if (ok && find_address (&PATTERN (mem_insn.insn)) == -1)
1389 if (dump_file)
1390 dump_mem_insn (dump_file);
1391 if (try_merge ())
1393 success_in_block++;
1394 insn_is_add_or_inc = false;
1400 else
1402 insn_is_add_or_inc = false;
1403 mem_insn.insn = insn;
1404 if (find_mem (&PATTERN (insn)))
1405 success_in_block++;
1408 /* If the inc insn was merged with a mem, the inc insn is gone
1409 and there is noting to update. */
1410 if (df_insn_info *insn_info = DF_INSN_INFO_GET (insn))
1412 df_ref def, use;
1414 /* Need to update next use. */
1415 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1417 reg_next_use[DF_REF_REGNO (def)] = NULL;
1418 reg_next_inc_use[DF_REF_REGNO (def)] = NULL;
1419 reg_next_def[DF_REF_REGNO (def)] = insn;
1422 FOR_EACH_INSN_INFO_USE (use, insn_info)
1424 reg_next_use[DF_REF_REGNO (use)] = insn;
1425 if (insn_is_add_or_inc)
1426 reg_next_inc_use[DF_REF_REGNO (use)] = insn;
1427 else
1428 reg_next_inc_use[DF_REF_REGNO (use)] = NULL;
1431 else if (dump_file)
1432 fprintf (dump_file, "skipping update of deleted insn %d\n",
1433 INSN_UID (insn));
1436 /* If we were successful, try again. There may have been several
1437 opportunities that were interleaved. This is rare but
1438 gcc.c-torture/compile/pr17273.c actually exhibits this. */
1439 if (success_in_block)
1441 /* In this case, we must clear these vectors since the trick of
1442 testing if the stale insn in the block will not work. */
1443 memset (reg_next_use, 0, max_reg * sizeof (rtx));
1444 memset (reg_next_inc_use, 0, max_reg * sizeof (rtx));
1445 memset (reg_next_def, 0, max_reg * sizeof (rtx));
1446 df_recompute_luids (bb);
1447 merge_in_block (max_reg, bb);
1451 #endif
1453 /* Discover auto-inc auto-dec instructions. */
1455 namespace {
1457 const pass_data pass_data_inc_dec =
1459 RTL_PASS, /* type */
1460 "auto_inc_dec", /* name */
1461 OPTGROUP_NONE, /* optinfo_flags */
1462 TV_AUTO_INC_DEC, /* tv_id */
1463 0, /* properties_required */
1464 0, /* properties_provided */
1465 0, /* properties_destroyed */
1466 0, /* todo_flags_start */
1467 TODO_df_finish, /* todo_flags_finish */
1470 class pass_inc_dec : public rtl_opt_pass
1472 public:
1473 pass_inc_dec (gcc::context *ctxt)
1474 : rtl_opt_pass (pass_data_inc_dec, ctxt)
1477 /* opt_pass methods: */
1478 virtual bool gate (function *)
1480 #ifdef AUTO_INC_DEC
1481 return (optimize > 0 && flag_auto_inc_dec);
1482 #else
1483 return false;
1484 #endif
1488 unsigned int execute (function *);
1490 }; // class pass_inc_dec
1492 unsigned int
1493 pass_inc_dec::execute (function *fun ATTRIBUTE_UNUSED)
1495 #ifdef AUTO_INC_DEC
1496 basic_block bb;
1497 int max_reg = max_reg_num ();
1499 if (!initialized)
1500 init_decision_table ();
1502 mem_tmp = gen_rtx_MEM (Pmode, NULL_RTX);
1504 df_note_add_problem ();
1505 df_analyze ();
1507 reg_next_use = XCNEWVEC (rtx_insn *, max_reg);
1508 reg_next_inc_use = XCNEWVEC (rtx_insn *, max_reg);
1509 reg_next_def = XCNEWVEC (rtx_insn *, max_reg);
1510 FOR_EACH_BB_FN (bb, fun)
1511 merge_in_block (max_reg, bb);
1513 free (reg_next_use);
1514 free (reg_next_inc_use);
1515 free (reg_next_def);
1517 mem_tmp = NULL;
1518 #endif
1519 return 0;
1522 } // anon namespace
1524 rtl_opt_pass *
1525 make_pass_inc_dec (gcc::context *ctxt)
1527 return new pass_inc_dec (ctxt);