2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / auto-inc-dec.c
blob0bdf91a507ddb730e59e7c09be04a0617a809fb5
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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "hard-reg-set.h"
32 #include "predict.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "cfgrtl.h"
37 #include "basic-block.h"
38 #include "insn-config.h"
39 #include "regs.h"
40 #include "flags.h"
41 #include "except.h"
42 #include "diagnostic-core.h"
43 #include "recog.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "varasm.h"
50 #include "stmt.h"
51 #include "expr.h"
52 #include "tree-pass.h"
53 #include "df.h"
54 #include "dbgcnt.h"
55 #include "target.h"
57 /* This pass was originally removed from flow.c. However there is
58 almost nothing that remains of that code.
60 There are (4) basic forms that are matched:
62 (1) FORM_PRE_ADD
63 a <- b + c
64 ...
67 becomes
69 a <- b
70 ...
71 *(a += c) pre
74 (2) FORM_PRE_INC
75 a += c
76 ...
79 becomes
81 *(a += c) pre
84 (3) FORM_POST_ADD
86 ...
87 b <- a + c
89 (For this case to be true, b must not be assigned or used between
90 the *a and the assignment to b. B must also be a Pmode reg.)
92 becomes
94 b <- a
95 ...
96 *(b += c) post
99 (4) FORM_POST_INC
102 a <- a + c
104 becomes
106 *(a += c) post
108 There are three types of values of c.
110 1) c is a constant equal to the width of the value being accessed by
111 the pointer. This is useful for machines that have
112 HAVE_PRE_INCREMENT, HAVE_POST_INCREMENT, HAVE_PRE_DECREMENT or
113 HAVE_POST_DECREMENT defined.
115 2) c is a constant not equal to the width of the value being accessed
116 by the pointer. This is useful for machines that have
117 HAVE_PRE_MODIFY_DISP, HAVE_POST_MODIFY_DISP defined.
119 3) c is a register. This is useful for machines that have
120 HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
122 The is one special case: if a already had an offset equal to it +-
123 its width and that offset is equal to -c when the increment was
124 before the ref or +c if the increment was after the ref, then if we
125 can do the combination but switch the pre/post bit. */
127 #ifdef AUTO_INC_DEC
129 enum form
131 FORM_PRE_ADD,
132 FORM_PRE_INC,
133 FORM_POST_ADD,
134 FORM_POST_INC,
135 FORM_last
138 /* The states of the second operands of mem refs and inc insns. If no
139 second operand of the mem_ref was found, it is assumed to just be
140 ZERO. SIZE is the size of the mode accessed in the memref. The
141 ANY is used for constants that are not +-size or 0. REG is used if
142 the forms are reg1 + reg2. */
144 enum inc_state
146 INC_ZERO, /* == 0 */
147 INC_NEG_SIZE, /* == +size */
148 INC_POS_SIZE, /* == -size */
149 INC_NEG_ANY, /* == some -constant */
150 INC_POS_ANY, /* == some +constant */
151 INC_REG, /* == some register */
152 INC_last
155 /* The eight forms that pre/post inc/dec can take. */
156 enum gen_form
158 NOTHING,
159 SIMPLE_PRE_INC, /* ++size */
160 SIMPLE_POST_INC, /* size++ */
161 SIMPLE_PRE_DEC, /* --size */
162 SIMPLE_POST_DEC, /* size-- */
163 DISP_PRE, /* ++con */
164 DISP_POST, /* con++ */
165 REG_PRE, /* ++reg */
166 REG_POST /* reg++ */
169 /* Tmp mem rtx for use in cost modeling. */
170 static rtx mem_tmp;
172 static enum inc_state
173 set_inc_state (HOST_WIDE_INT val, int size)
175 if (val == 0)
176 return INC_ZERO;
177 if (val < 0)
178 return (val == -size) ? INC_NEG_SIZE : INC_NEG_ANY;
179 else
180 return (val == size) ? INC_POS_SIZE : INC_POS_ANY;
183 /* The DECISION_TABLE that describes what form, if any, the increment
184 or decrement will take. It is a three dimensional table. The first
185 index is the type of constant or register found as the second
186 operand of the inc insn. The second index is the type of constant
187 or register found as the second operand of the memory reference (if
188 no second operand exists, 0 is used). The third index is the form
189 and location (relative to the mem reference) of inc insn. */
191 static bool initialized = false;
192 static enum gen_form decision_table[INC_last][INC_last][FORM_last];
194 static void
195 init_decision_table (void)
197 enum gen_form value;
199 if (HAVE_PRE_INCREMENT || HAVE_PRE_MODIFY_DISP)
201 /* Prefer the simple form if both are available. */
202 value = (HAVE_PRE_INCREMENT) ? SIMPLE_PRE_INC : DISP_PRE;
204 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
205 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_INC] = value;
207 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_ADD] = value;
208 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_INC] = value;
211 if (HAVE_POST_INCREMENT || HAVE_POST_MODIFY_DISP)
213 /* Prefer the simple form if both are available. */
214 value = (HAVE_POST_INCREMENT) ? SIMPLE_POST_INC : DISP_POST;
216 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_ADD] = value;
217 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_INC] = value;
219 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_ADD] = value;
220 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_INC] = value;
223 if (HAVE_PRE_DECREMENT || HAVE_PRE_MODIFY_DISP)
225 /* Prefer the simple form if both are available. */
226 value = (HAVE_PRE_DECREMENT) ? SIMPLE_PRE_DEC : DISP_PRE;
228 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
229 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_INC] = value;
231 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_ADD] = value;
232 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_INC] = value;
235 if (HAVE_POST_DECREMENT || HAVE_POST_MODIFY_DISP)
237 /* Prefer the simple form if both are available. */
238 value = (HAVE_POST_DECREMENT) ? SIMPLE_POST_DEC : DISP_POST;
240 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_ADD] = value;
241 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_INC] = value;
243 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_ADD] = value;
244 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_INC] = value;
247 if (HAVE_PRE_MODIFY_DISP)
249 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
250 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
252 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_ADD] = DISP_PRE;
253 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_INC] = DISP_PRE;
255 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
256 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
258 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_ADD] = DISP_PRE;
259 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_INC] = DISP_PRE;
262 if (HAVE_POST_MODIFY_DISP)
264 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
265 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
267 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_ADD] = DISP_POST;
268 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_INC] = DISP_POST;
270 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
271 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
273 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_ADD] = DISP_POST;
274 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_INC] = DISP_POST;
277 /* This is much simpler than the other cases because we do not look
278 for the reg1-reg2 case. Note that we do not have a INC_POS_REG
279 and INC_NEG_REG states. Most of the use of such states would be
280 on a target that had an R1 - R2 update address form.
282 There is the remote possibility that you could also catch a = a +
283 b; *(a - b) as a postdecrement of (a + b). However, it is
284 unclear if *(a - b) would ever be generated on a machine that did
285 not have that kind of addressing mode. The IA-64 and RS6000 will
286 not do this, and I cannot speak for any other. If any
287 architecture does have an a-b update for, these cases should be
288 added. */
289 if (HAVE_PRE_MODIFY_REG)
291 decision_table[INC_REG][INC_ZERO][FORM_PRE_ADD] = REG_PRE;
292 decision_table[INC_REG][INC_ZERO][FORM_PRE_INC] = REG_PRE;
294 decision_table[INC_REG][INC_REG][FORM_POST_ADD] = REG_PRE;
295 decision_table[INC_REG][INC_REG][FORM_POST_INC] = REG_PRE;
298 if (HAVE_POST_MODIFY_REG)
300 decision_table[INC_REG][INC_ZERO][FORM_POST_ADD] = REG_POST;
301 decision_table[INC_REG][INC_ZERO][FORM_POST_INC] = REG_POST;
304 initialized = true;
307 /* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
308 "reg_res = reg0+c". */
310 static struct inc_insn
312 rtx_insn *insn; /* The insn being parsed. */
313 rtx pat; /* The pattern of the insn. */
314 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
315 enum form form;
316 rtx reg_res;
317 rtx reg0;
318 rtx reg1;
319 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
320 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
321 } inc_insn;
324 /* Dump the parsed inc insn to FILE. */
326 static void
327 dump_inc_insn (FILE *file)
329 const char *f = ((inc_insn.form == FORM_PRE_ADD)
330 || (inc_insn.form == FORM_PRE_INC)) ? "pre" : "post";
332 dump_insn_slim (file, inc_insn.insn);
334 switch (inc_insn.form)
336 case FORM_PRE_ADD:
337 case FORM_POST_ADD:
338 if (inc_insn.reg1_is_const)
339 fprintf (file, "found %s add(%d) r[%d]=r[%d]+%d\n",
340 f, INSN_UID (inc_insn.insn),
341 REGNO (inc_insn.reg_res),
342 REGNO (inc_insn.reg0), (int) inc_insn.reg1_val);
343 else
344 fprintf (file, "found %s add(%d) r[%d]=r[%d]+r[%d]\n",
345 f, INSN_UID (inc_insn.insn),
346 REGNO (inc_insn.reg_res),
347 REGNO (inc_insn.reg0), REGNO (inc_insn.reg1));
348 break;
350 case FORM_PRE_INC:
351 case FORM_POST_INC:
352 if (inc_insn.reg1_is_const)
353 fprintf (file, "found %s inc(%d) r[%d]+=%d\n",
354 f, INSN_UID (inc_insn.insn),
355 REGNO (inc_insn.reg_res), (int) inc_insn.reg1_val);
356 else
357 fprintf (file, "found %s inc(%d) r[%d]+=r[%d]\n",
358 f, INSN_UID (inc_insn.insn),
359 REGNO (inc_insn.reg_res), REGNO (inc_insn.reg1));
360 break;
362 default:
363 break;
368 /* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
370 static struct mem_insn
372 rtx_insn *insn; /* The insn being parsed. */
373 rtx pat; /* The pattern of the insn. */
374 rtx *mem_loc; /* The address of the field that holds the mem */
375 /* that is to be replaced. */
376 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
377 rtx reg0;
378 rtx reg1; /* This is either a reg or a const depending on
379 reg1_is_const. */
380 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
381 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
382 } mem_insn;
385 /* Dump the parsed mem insn to FILE. */
387 static void
388 dump_mem_insn (FILE *file)
390 dump_insn_slim (file, mem_insn.insn);
392 if (mem_insn.reg1_is_const)
393 fprintf (file, "found mem(%d) *(r[%d]+%d)\n",
394 INSN_UID (mem_insn.insn),
395 REGNO (mem_insn.reg0), (int) mem_insn.reg1_val);
396 else
397 fprintf (file, "found mem(%d) *(r[%d]+r[%d])\n",
398 INSN_UID (mem_insn.insn),
399 REGNO (mem_insn.reg0), REGNO (mem_insn.reg1));
403 /* The following three arrays contain pointers to instructions. They
404 are indexed by REGNO. At any point in the basic block where we are
405 looking these three arrays contain, respectively, the next insn
406 that uses REGNO, the next inc or add insn that uses REGNO and the
407 next insn that sets REGNO.
409 The arrays are not cleared when we move from block to block so
410 whenever an insn is retrieved from these arrays, it's block number
411 must be compared with the current block.
414 static rtx_insn **reg_next_use = NULL;
415 static rtx_insn **reg_next_inc_use = NULL;
416 static rtx_insn **reg_next_def = NULL;
419 /* Move dead note that match PATTERN to TO_INSN from FROM_INSN. We do
420 not really care about moving any other notes from the inc or add
421 insn. Moving the REG_EQUAL and REG_EQUIV is clearly wrong and it
422 does not appear that there are any other kinds of relevant notes. */
424 static void
425 move_dead_notes (rtx_insn *to_insn, rtx_insn *from_insn, rtx pattern)
427 rtx note;
428 rtx next_note;
429 rtx prev_note = NULL;
431 for (note = REG_NOTES (from_insn); note; note = next_note)
433 next_note = XEXP (note, 1);
435 if ((REG_NOTE_KIND (note) == REG_DEAD)
436 && pattern == XEXP (note, 0))
438 XEXP (note, 1) = REG_NOTES (to_insn);
439 REG_NOTES (to_insn) = note;
440 if (prev_note)
441 XEXP (prev_note, 1) = next_note;
442 else
443 REG_NOTES (from_insn) = next_note;
445 else prev_note = note;
450 /* Create a mov insn DEST_REG <- SRC_REG and insert it before
451 NEXT_INSN. */
453 static rtx_insn *
454 insert_move_insn_before (rtx_insn *next_insn, rtx dest_reg, rtx src_reg)
456 rtx_insn *insns;
458 start_sequence ();
459 emit_move_insn (dest_reg, src_reg);
460 insns = get_insns ();
461 end_sequence ();
462 emit_insn_before (insns, next_insn);
463 return insns;
467 /* Change mem_insn.mem_loc so that uses NEW_ADDR which has an
468 increment of INC_REG. To have reached this point, the change is a
469 legitimate one from a dataflow point of view. The only questions
470 are is this a valid change to the instruction and is this a
471 profitable change to the instruction. */
473 static bool
474 attempt_change (rtx new_addr, rtx inc_reg)
476 /* There are four cases: For the two cases that involve an add
477 instruction, we are going to have to delete the add and insert a
478 mov. We are going to assume that the mov is free. This is
479 fairly early in the backend and there are a lot of opportunities
480 for removing that move later. In particular, there is the case
481 where the move may be dead, this is what dead code elimination
482 passes are for. The two cases where we have an inc insn will be
483 handled mov free. */
485 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
486 rtx_insn *mov_insn = NULL;
487 int regno;
488 rtx mem = *mem_insn.mem_loc;
489 machine_mode mode = GET_MODE (mem);
490 rtx new_mem;
491 int old_cost = 0;
492 int new_cost = 0;
493 bool speed = optimize_bb_for_speed_p (bb);
495 PUT_MODE (mem_tmp, mode);
496 XEXP (mem_tmp, 0) = new_addr;
498 old_cost = (set_src_cost (mem, speed)
499 + set_rtx_cost (PATTERN (inc_insn.insn), speed));
500 new_cost = set_src_cost (mem_tmp, speed);
502 /* The first item of business is to see if this is profitable. */
503 if (old_cost < new_cost)
505 if (dump_file)
506 fprintf (dump_file, "cost failure old=%d new=%d\n", old_cost, new_cost);
507 return false;
510 /* Jump through a lot of hoops to keep the attributes up to date. We
511 do not want to call one of the change address variants that take
512 an offset even though we know the offset in many cases. These
513 assume you are changing where the address is pointing by the
514 offset. */
515 new_mem = replace_equiv_address_nv (mem, new_addr);
516 if (! validate_change (mem_insn.insn, mem_insn.mem_loc, new_mem, 0))
518 if (dump_file)
519 fprintf (dump_file, "validation failure\n");
520 return false;
523 /* From here to the end of the function we are committed to the
524 change, i.e. nothing fails. Generate any necessary movs, move
525 any regnotes, and fix up the reg_next_{use,inc_use,def}. */
526 switch (inc_insn.form)
528 case FORM_PRE_ADD:
529 /* Replace the addition with a move. Do it at the location of
530 the addition since the operand of the addition may change
531 before the memory reference. */
532 mov_insn = insert_move_insn_before (inc_insn.insn,
533 inc_insn.reg_res, inc_insn.reg0);
534 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
536 regno = REGNO (inc_insn.reg_res);
537 reg_next_def[regno] = mov_insn;
538 reg_next_use[regno] = NULL;
539 regno = REGNO (inc_insn.reg0);
540 reg_next_use[regno] = mov_insn;
541 df_recompute_luids (bb);
542 break;
544 case FORM_POST_INC:
545 regno = REGNO (inc_insn.reg_res);
546 if (reg_next_use[regno] == reg_next_inc_use[regno])
547 reg_next_inc_use[regno] = NULL;
549 /* Fallthru. */
550 case FORM_PRE_INC:
551 regno = REGNO (inc_insn.reg_res);
552 reg_next_def[regno] = mem_insn.insn;
553 reg_next_use[regno] = NULL;
555 break;
557 case FORM_POST_ADD:
558 mov_insn = insert_move_insn_before (mem_insn.insn,
559 inc_insn.reg_res, inc_insn.reg0);
560 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
562 /* Do not move anything to the mov insn because the instruction
563 pointer for the main iteration has not yet hit that. It is
564 still pointing to the mem insn. */
565 regno = REGNO (inc_insn.reg_res);
566 reg_next_def[regno] = mem_insn.insn;
567 reg_next_use[regno] = NULL;
569 regno = REGNO (inc_insn.reg0);
570 reg_next_use[regno] = mem_insn.insn;
571 if ((reg_next_use[regno] == reg_next_inc_use[regno])
572 || (reg_next_inc_use[regno] == inc_insn.insn))
573 reg_next_inc_use[regno] = NULL;
574 df_recompute_luids (bb);
575 break;
577 case FORM_last:
578 default:
579 gcc_unreachable ();
582 if (!inc_insn.reg1_is_const)
584 regno = REGNO (inc_insn.reg1);
585 reg_next_use[regno] = mem_insn.insn;
586 if ((reg_next_use[regno] == reg_next_inc_use[regno])
587 || (reg_next_inc_use[regno] == inc_insn.insn))
588 reg_next_inc_use[regno] = NULL;
591 delete_insn (inc_insn.insn);
593 if (dump_file && mov_insn)
595 fprintf (dump_file, "inserting mov ");
596 dump_insn_slim (dump_file, mov_insn);
599 /* Record that this insn has an implicit side effect. */
600 add_reg_note (mem_insn.insn, REG_INC, inc_reg);
602 if (dump_file)
604 fprintf (dump_file, "****success ");
605 dump_insn_slim (dump_file, mem_insn.insn);
608 return true;
612 /* Try to combine the instruction in INC_INSN with the instruction in
613 MEM_INSN. First the form is determined using the DECISION_TABLE
614 and the results of parsing the INC_INSN and the MEM_INSN.
615 Assuming the form is ok, a prototype new address is built which is
616 passed to ATTEMPT_CHANGE for final processing. */
618 static bool
619 try_merge (void)
621 enum gen_form gen_form;
622 rtx mem = *mem_insn.mem_loc;
623 rtx inc_reg = inc_insn.form == FORM_POST_ADD ?
624 inc_insn.reg_res : mem_insn.reg0;
626 /* The width of the mem being accessed. */
627 int size = GET_MODE_SIZE (GET_MODE (mem));
628 rtx_insn *last_insn = NULL;
629 machine_mode reg_mode = GET_MODE (inc_reg);
631 switch (inc_insn.form)
633 case FORM_PRE_ADD:
634 case FORM_PRE_INC:
635 last_insn = mem_insn.insn;
636 break;
637 case FORM_POST_INC:
638 case FORM_POST_ADD:
639 last_insn = inc_insn.insn;
640 break;
641 case FORM_last:
642 default:
643 gcc_unreachable ();
646 /* Cannot handle auto inc of the stack. */
647 if (inc_reg == stack_pointer_rtx)
649 if (dump_file)
650 fprintf (dump_file, "cannot inc stack %d failure\n", REGNO (inc_reg));
651 return false;
654 /* Look to see if the inc register is dead after the memory
655 reference. If it is, do not do the combination. */
656 if (find_regno_note (last_insn, REG_DEAD, REGNO (inc_reg)))
658 if (dump_file)
659 fprintf (dump_file, "dead failure %d\n", REGNO (inc_reg));
660 return false;
663 mem_insn.reg1_state = (mem_insn.reg1_is_const)
664 ? set_inc_state (mem_insn.reg1_val, size) : INC_REG;
665 inc_insn.reg1_state = (inc_insn.reg1_is_const)
666 ? set_inc_state (inc_insn.reg1_val, size) : INC_REG;
668 /* Now get the form that we are generating. */
669 gen_form = decision_table
670 [inc_insn.reg1_state][mem_insn.reg1_state][inc_insn.form];
672 if (dbg_cnt (auto_inc_dec) == false)
673 return false;
675 switch (gen_form)
677 default:
678 case NOTHING:
679 return false;
681 case SIMPLE_PRE_INC: /* ++size */
682 if (dump_file)
683 fprintf (dump_file, "trying SIMPLE_PRE_INC\n");
684 return attempt_change (gen_rtx_PRE_INC (reg_mode, inc_reg), inc_reg);
685 break;
687 case SIMPLE_POST_INC: /* size++ */
688 if (dump_file)
689 fprintf (dump_file, "trying SIMPLE_POST_INC\n");
690 return attempt_change (gen_rtx_POST_INC (reg_mode, inc_reg), inc_reg);
691 break;
693 case SIMPLE_PRE_DEC: /* --size */
694 if (dump_file)
695 fprintf (dump_file, "trying SIMPLE_PRE_DEC\n");
696 return attempt_change (gen_rtx_PRE_DEC (reg_mode, inc_reg), inc_reg);
697 break;
699 case SIMPLE_POST_DEC: /* size-- */
700 if (dump_file)
701 fprintf (dump_file, "trying SIMPLE_POST_DEC\n");
702 return attempt_change (gen_rtx_POST_DEC (reg_mode, inc_reg), inc_reg);
703 break;
705 case DISP_PRE: /* ++con */
706 if (dump_file)
707 fprintf (dump_file, "trying DISP_PRE\n");
708 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
709 inc_reg,
710 gen_rtx_PLUS (reg_mode,
711 inc_reg,
712 inc_insn.reg1)),
713 inc_reg);
714 break;
716 case DISP_POST: /* con++ */
717 if (dump_file)
718 fprintf (dump_file, "trying POST_DISP\n");
719 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
720 inc_reg,
721 gen_rtx_PLUS (reg_mode,
722 inc_reg,
723 inc_insn.reg1)),
724 inc_reg);
725 break;
727 case REG_PRE: /* ++reg */
728 if (dump_file)
729 fprintf (dump_file, "trying PRE_REG\n");
730 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
731 inc_reg,
732 gen_rtx_PLUS (reg_mode,
733 inc_reg,
734 inc_insn.reg1)),
735 inc_reg);
736 break;
738 case REG_POST: /* reg++ */
739 if (dump_file)
740 fprintf (dump_file, "trying POST_REG\n");
741 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
742 inc_reg,
743 gen_rtx_PLUS (reg_mode,
744 inc_reg,
745 inc_insn.reg1)),
746 inc_reg);
747 break;
751 /* Return the next insn that uses (if reg_next_use is passed in
752 NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
753 REGNO in BB. */
755 static rtx_insn *
756 get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
758 rtx_insn *insn = next_array[regno];
760 /* Lazy about cleaning out the next_arrays. */
761 if (insn && BLOCK_FOR_INSN (insn) != bb)
763 next_array[regno] = NULL;
764 insn = NULL;
767 return insn;
771 /* Reverse the operands in a mem insn. */
773 static void
774 reverse_mem (void)
776 rtx tmp = mem_insn.reg1;
777 mem_insn.reg1 = mem_insn.reg0;
778 mem_insn.reg0 = tmp;
782 /* Reverse the operands in a inc insn. */
784 static void
785 reverse_inc (void)
787 rtx tmp = inc_insn.reg1;
788 inc_insn.reg1 = inc_insn.reg0;
789 inc_insn.reg0 = tmp;
793 /* Return true if INSN is of a form "a = b op c" where a and b are
794 regs. op is + if c is a reg and +|- if c is a const. Fill in
795 INC_INSN with what is found.
797 This function is called in two contexts, if BEFORE_MEM is true,
798 this is called for each insn in the basic block. If BEFORE_MEM is
799 false, it is called for the instruction in the block that uses the
800 index register for some memory reference that is currently being
801 processed. */
803 static bool
804 parse_add_or_inc (rtx_insn *insn, bool before_mem)
806 rtx pat = single_set (insn);
807 if (!pat)
808 return false;
810 /* Result must be single reg. */
811 if (!REG_P (SET_DEST (pat)))
812 return false;
814 if ((GET_CODE (SET_SRC (pat)) != PLUS)
815 && (GET_CODE (SET_SRC (pat)) != MINUS))
816 return false;
818 if (!REG_P (XEXP (SET_SRC (pat), 0)))
819 return false;
821 inc_insn.insn = insn;
822 inc_insn.pat = pat;
823 inc_insn.reg_res = SET_DEST (pat);
824 inc_insn.reg0 = XEXP (SET_SRC (pat), 0);
825 if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg0))
826 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
827 else
828 inc_insn.form = before_mem ? FORM_PRE_ADD : FORM_POST_ADD;
830 if (CONST_INT_P (XEXP (SET_SRC (pat), 1)))
832 /* Process a = b + c where c is a const. */
833 inc_insn.reg1_is_const = true;
834 if (GET_CODE (SET_SRC (pat)) == PLUS)
836 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
837 inc_insn.reg1_val = INTVAL (inc_insn.reg1);
839 else
841 inc_insn.reg1_val = -INTVAL (XEXP (SET_SRC (pat), 1));
842 inc_insn.reg1 = GEN_INT (inc_insn.reg1_val);
844 return true;
846 else if ((HAVE_PRE_MODIFY_REG || HAVE_POST_MODIFY_REG)
847 && (REG_P (XEXP (SET_SRC (pat), 1)))
848 && GET_CODE (SET_SRC (pat)) == PLUS)
850 /* Process a = b + c where c is a reg. */
851 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
852 inc_insn.reg1_is_const = false;
854 if (inc_insn.form == FORM_PRE_INC
855 || inc_insn.form == FORM_POST_INC)
856 return true;
857 else if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg1))
859 /* Reverse the two operands and turn *_ADD into *_INC since
860 a = c + a. */
861 reverse_inc ();
862 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
863 return true;
865 else
866 return true;
869 return false;
873 /* A recursive function that checks all of the mem uses in
874 ADDRESS_OF_X to see if any single one of them is compatible with
875 what has been found in inc_insn.
877 -1 is returned for success. 0 is returned if nothing was found and
878 1 is returned for failure. */
880 static int
881 find_address (rtx *address_of_x)
883 rtx x = *address_of_x;
884 enum rtx_code code = GET_CODE (x);
885 const char *const fmt = GET_RTX_FORMAT (code);
886 int i;
887 int value = 0;
888 int tem;
890 if (code == MEM && rtx_equal_p (XEXP (x, 0), inc_insn.reg_res))
892 /* Match with *reg0. */
893 mem_insn.mem_loc = address_of_x;
894 mem_insn.reg0 = inc_insn.reg_res;
895 mem_insn.reg1_is_const = true;
896 mem_insn.reg1_val = 0;
897 mem_insn.reg1 = GEN_INT (0);
898 return -1;
900 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
901 && rtx_equal_p (XEXP (XEXP (x, 0), 0), inc_insn.reg_res))
903 rtx b = XEXP (XEXP (x, 0), 1);
904 mem_insn.mem_loc = address_of_x;
905 mem_insn.reg0 = inc_insn.reg_res;
906 mem_insn.reg1 = b;
907 mem_insn.reg1_is_const = inc_insn.reg1_is_const;
908 if (CONST_INT_P (b))
910 /* Match with *(reg0 + reg1) where reg1 is a const. */
911 HOST_WIDE_INT val = INTVAL (b);
912 if (inc_insn.reg1_is_const
913 && (inc_insn.reg1_val == val || inc_insn.reg1_val == -val))
915 mem_insn.reg1_val = val;
916 return -1;
919 else if (!inc_insn.reg1_is_const
920 && rtx_equal_p (inc_insn.reg1, b))
921 /* Match with *(reg0 + reg1). */
922 return -1;
925 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
927 /* If REG occurs inside a MEM used in a bit-field reference,
928 that is unacceptable. */
929 if (find_address (&XEXP (x, 0)))
930 return 1;
933 if (x == inc_insn.reg_res)
934 return 1;
936 /* Time for some deep diving. */
937 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
939 if (fmt[i] == 'e')
941 tem = find_address (&XEXP (x, i));
942 /* If this is the first use, let it go so the rest of the
943 insn can be checked. */
944 if (value == 0)
945 value = tem;
946 else if (tem != 0)
947 /* More than one match was found. */
948 return 1;
950 else if (fmt[i] == 'E')
952 int j;
953 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
955 tem = find_address (&XVECEXP (x, i, j));
956 /* If this is the first use, let it go so the rest of
957 the insn can be checked. */
958 if (value == 0)
959 value = tem;
960 else if (tem != 0)
961 /* More than one match was found. */
962 return 1;
966 return value;
969 /* Once a suitable mem reference has been found and the MEM_INSN
970 structure has been filled in, FIND_INC is called to see if there is
971 a suitable add or inc insn that follows the mem reference and
972 determine if it is suitable to merge.
974 In the case where the MEM_INSN has two registers in the reference,
975 this function may be called recursively. The first time looking
976 for an add of the first register, and if that fails, looking for an
977 add of the second register. The FIRST_TRY parameter is used to
978 only allow the parameters to be reversed once. */
980 static bool
981 find_inc (bool first_try)
983 rtx_insn *insn;
984 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
985 rtx_insn *other_insn;
986 df_ref def;
988 /* Make sure this reg appears only once in this insn. */
989 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
991 if (dump_file)
992 fprintf (dump_file, "mem count failure\n");
993 return false;
996 if (dump_file)
997 dump_mem_insn (dump_file);
999 /* Find the next use that is an inc. */
1000 insn = get_next_ref (REGNO (mem_insn.reg0),
1001 BLOCK_FOR_INSN (mem_insn.insn),
1002 reg_next_inc_use);
1003 if (!insn)
1004 return false;
1006 /* Even though we know the next use is an add or inc because it came
1007 from the reg_next_inc_use, we must still reparse. */
1008 if (!parse_add_or_inc (insn, false))
1010 /* Next use was not an add. Look for one extra case. It could be
1011 that we have:
1013 *(a + b)
1014 ...= a;
1015 ...= b + a
1017 if we reverse the operands in the mem ref we would
1018 find this. Only try it once though. */
1019 if (first_try && !mem_insn.reg1_is_const)
1021 reverse_mem ();
1022 return find_inc (false);
1024 else
1025 return false;
1028 /* Need to assure that none of the operands of the inc instruction are
1029 assigned to by the mem insn. */
1030 FOR_EACH_INSN_DEF (def, mem_insn.insn)
1032 unsigned int regno = DF_REF_REGNO (def);
1033 if ((regno == REGNO (inc_insn.reg0))
1034 || (regno == REGNO (inc_insn.reg_res)))
1036 if (dump_file)
1037 fprintf (dump_file, "inc conflicts with store failure.\n");
1038 return false;
1040 if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
1042 if (dump_file)
1043 fprintf (dump_file, "inc conflicts with store failure.\n");
1044 return false;
1048 if (dump_file)
1049 dump_inc_insn (dump_file);
1051 if (inc_insn.form == FORM_POST_ADD)
1053 /* Make sure that there is no insn that assigns to inc_insn.res
1054 between the mem_insn and the inc_insn. */
1055 rtx_insn *other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1056 BLOCK_FOR_INSN (mem_insn.insn),
1057 reg_next_def);
1058 if (other_insn != inc_insn.insn)
1060 if (dump_file)
1061 fprintf (dump_file,
1062 "result of add is assigned to between mem and inc insns.\n");
1063 return false;
1066 other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1067 BLOCK_FOR_INSN (mem_insn.insn),
1068 reg_next_use);
1069 if (other_insn
1070 && (other_insn != inc_insn.insn)
1071 && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
1073 if (dump_file)
1074 fprintf (dump_file,
1075 "result of add is used between mem and inc insns.\n");
1076 return false;
1079 /* For the post_add to work, the result_reg of the inc must not be
1080 used in the mem insn since this will become the new index
1081 register. */
1082 if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
1084 if (dump_file)
1085 fprintf (dump_file, "base reg replacement failure.\n");
1086 return false;
1090 if (mem_insn.reg1_is_const)
1092 if (mem_insn.reg1_val == 0)
1094 if (!inc_insn.reg1_is_const)
1096 /* The mem looks like *r0 and the rhs of the add has two
1097 registers. */
1098 int luid = DF_INSN_LUID (inc_insn.insn);
1099 if (inc_insn.form == FORM_POST_ADD)
1101 /* The trick is that we are not going to increment r0,
1102 we are going to increment the result of the add insn.
1103 For this trick to be correct, the result reg of
1104 the inc must be a valid addressing reg. */
1105 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1106 if (GET_MODE (inc_insn.reg_res)
1107 != targetm.addr_space.address_mode (as))
1109 if (dump_file)
1110 fprintf (dump_file, "base reg mode failure.\n");
1111 return false;
1114 /* We also need to make sure that the next use of
1115 inc result is after the inc. */
1116 other_insn
1117 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1118 if (other_insn && luid > DF_INSN_LUID (other_insn))
1119 return false;
1121 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1122 reverse_inc ();
1125 other_insn
1126 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1127 if (other_insn && luid > DF_INSN_LUID (other_insn))
1128 return false;
1131 /* Both the inc/add and the mem have a constant. Need to check
1132 that the constants are ok. */
1133 else if ((mem_insn.reg1_val != inc_insn.reg1_val)
1134 && (mem_insn.reg1_val != -inc_insn.reg1_val))
1135 return false;
1137 else
1139 /* The mem insn is of the form *(a + b) where a and b are both
1140 regs. It may be that in order to match the add or inc we
1141 need to treat it as if it was *(b + a). It may also be that
1142 the add is of the form a + c where c does not match b and
1143 then we just abandon this. */
1145 int luid = DF_INSN_LUID (inc_insn.insn);
1146 rtx_insn *other_insn;
1148 /* Make sure this reg appears only once in this insn. */
1149 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
1150 return false;
1152 if (inc_insn.form == FORM_POST_ADD)
1154 /* For this trick to be correct, the result reg of the inc
1155 must be a valid addressing reg. */
1156 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1157 if (GET_MODE (inc_insn.reg_res)
1158 != targetm.addr_space.address_mode (as))
1160 if (dump_file)
1161 fprintf (dump_file, "base reg mode failure.\n");
1162 return false;
1165 if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1167 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1169 /* See comment above on find_inc (false) call. */
1170 if (first_try)
1172 reverse_mem ();
1173 return find_inc (false);
1175 else
1176 return false;
1179 /* Need to check that there are no assignments to b
1180 before the add insn. */
1181 other_insn
1182 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1183 if (other_insn && luid > DF_INSN_LUID (other_insn))
1184 return false;
1185 /* All ok for the next step. */
1187 else
1189 /* We know that mem_insn.reg0 must equal inc_insn.reg1
1190 or else we would not have found the inc insn. */
1191 reverse_mem ();
1192 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1194 /* See comment above on find_inc (false) call. */
1195 if (first_try)
1196 return find_inc (false);
1197 else
1198 return false;
1200 /* To have gotten here know that.
1201 *(b + a)
1203 ... = (b + a)
1205 We also know that the lhs of the inc is not b or a. We
1206 need to make sure that there are no assignments to b
1207 between the mem ref and the inc. */
1209 other_insn
1210 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
1211 if (other_insn && luid > DF_INSN_LUID (other_insn))
1212 return false;
1215 /* Need to check that the next use of the add result is later than
1216 add insn since this will be the reg incremented. */
1217 other_insn
1218 = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
1219 if (other_insn && luid > DF_INSN_LUID (other_insn))
1220 return false;
1222 else /* FORM_POST_INC. There is less to check here because we
1223 know that operands must line up. */
1225 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1226 /* See comment above on find_inc (false) call. */
1228 if (first_try)
1230 reverse_mem ();
1231 return find_inc (false);
1233 else
1234 return false;
1237 /* To have gotten here know that.
1238 *(a + b)
1240 ... = (a + b)
1242 We also know that the lhs of the inc is not b. We need to make
1243 sure that there are no assignments to b between the mem ref and
1244 the inc. */
1245 other_insn
1246 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1247 if (other_insn && luid > DF_INSN_LUID (other_insn))
1248 return false;
1252 if (inc_insn.form == FORM_POST_INC)
1254 other_insn
1255 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_use);
1256 /* When we found inc_insn, we were looking for the
1257 next add or inc, not the next insn that used the
1258 reg. Because we are going to increment the reg
1259 in this form, we need to make sure that there
1260 were no intervening uses of reg. */
1261 if (inc_insn.insn != other_insn)
1262 return false;
1265 return try_merge ();
1269 /* A recursive function that walks ADDRESS_OF_X to find all of the mem
1270 uses in pat that could be used as an auto inc or dec. It then
1271 calls FIND_INC for each one. */
1273 static bool
1274 find_mem (rtx *address_of_x)
1276 rtx x = *address_of_x;
1277 enum rtx_code code = GET_CODE (x);
1278 const char *const fmt = GET_RTX_FORMAT (code);
1279 int i;
1281 if (code == MEM && REG_P (XEXP (x, 0)))
1283 /* Match with *reg0. */
1284 mem_insn.mem_loc = address_of_x;
1285 mem_insn.reg0 = XEXP (x, 0);
1286 mem_insn.reg1_is_const = true;
1287 mem_insn.reg1_val = 0;
1288 mem_insn.reg1 = GEN_INT (0);
1289 if (find_inc (true))
1290 return true;
1292 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
1293 && REG_P (XEXP (XEXP (x, 0), 0)))
1295 rtx reg1 = XEXP (XEXP (x, 0), 1);
1296 mem_insn.mem_loc = address_of_x;
1297 mem_insn.reg0 = XEXP (XEXP (x, 0), 0);
1298 mem_insn.reg1 = reg1;
1299 if (CONST_INT_P (reg1))
1301 mem_insn.reg1_is_const = true;
1302 /* Match with *(reg0 + c) where c is a const. */
1303 mem_insn.reg1_val = INTVAL (reg1);
1304 if (find_inc (true))
1305 return true;
1307 else if (REG_P (reg1))
1309 /* Match with *(reg0 + reg1). */
1310 mem_insn.reg1_is_const = false;
1311 if (find_inc (true))
1312 return true;
1316 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
1318 /* If REG occurs inside a MEM used in a bit-field reference,
1319 that is unacceptable. */
1320 return false;
1323 /* Time for some deep diving. */
1324 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1326 if (fmt[i] == 'e')
1328 if (find_mem (&XEXP (x, i)))
1329 return true;
1331 else if (fmt[i] == 'E')
1333 int j;
1334 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1335 if (find_mem (&XVECEXP (x, i, j)))
1336 return true;
1339 return false;
1343 /* Try to combine all incs and decs by constant values with memory
1344 references in BB. */
1346 static void
1347 merge_in_block (int max_reg, basic_block bb)
1349 rtx_insn *insn;
1350 rtx_insn *curr;
1351 int success_in_block = 0;
1353 if (dump_file)
1354 fprintf (dump_file, "\n\nstarting bb %d\n", bb->index);
1356 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, curr)
1358 bool insn_is_add_or_inc = true;
1360 if (!NONDEBUG_INSN_P (insn))
1361 continue;
1363 /* This continue is deliberate. We do not want the uses of the
1364 jump put into reg_next_use because it is not considered safe to
1365 combine a preincrement with a jump. */
1366 if (JUMP_P (insn))
1367 continue;
1369 if (dump_file)
1370 dump_insn_slim (dump_file, insn);
1372 /* Does this instruction increment or decrement a register? */
1373 if (parse_add_or_inc (insn, true))
1375 int regno = REGNO (inc_insn.reg_res);
1376 /* Cannot handle case where there are three separate regs
1377 before a mem ref. Too many moves would be needed to be
1378 profitable. */
1379 if ((inc_insn.form == FORM_PRE_INC) || inc_insn.reg1_is_const)
1381 mem_insn.insn = get_next_ref (regno, bb, reg_next_use);
1382 if (mem_insn.insn)
1384 bool ok = true;
1385 if (!inc_insn.reg1_is_const)
1387 /* We are only here if we are going to try a
1388 HAVE_*_MODIFY_REG type transformation. c is a
1389 reg and we must sure that the path from the
1390 inc_insn to the mem_insn.insn is both def and use
1391 clear of c because the inc insn is going to move
1392 into the mem_insn.insn. */
1393 int luid = DF_INSN_LUID (mem_insn.insn);
1394 rtx_insn *other_insn
1395 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1397 if (other_insn && luid > DF_INSN_LUID (other_insn))
1398 ok = false;
1400 other_insn
1401 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1403 if (other_insn && luid > DF_INSN_LUID (other_insn))
1404 ok = false;
1407 if (dump_file)
1408 dump_inc_insn (dump_file);
1410 if (ok && find_address (&PATTERN (mem_insn.insn)) == -1)
1412 if (dump_file)
1413 dump_mem_insn (dump_file);
1414 if (try_merge ())
1416 success_in_block++;
1417 insn_is_add_or_inc = false;
1423 else
1425 insn_is_add_or_inc = false;
1426 mem_insn.insn = insn;
1427 if (find_mem (&PATTERN (insn)))
1428 success_in_block++;
1431 /* If the inc insn was merged with a mem, the inc insn is gone
1432 and there is noting to update. */
1433 if (df_insn_info *insn_info = DF_INSN_INFO_GET (insn))
1435 df_ref def, use;
1437 /* Need to update next use. */
1438 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1440 reg_next_use[DF_REF_REGNO (def)] = NULL;
1441 reg_next_inc_use[DF_REF_REGNO (def)] = NULL;
1442 reg_next_def[DF_REF_REGNO (def)] = insn;
1445 FOR_EACH_INSN_INFO_USE (use, insn_info)
1447 reg_next_use[DF_REF_REGNO (use)] = insn;
1448 if (insn_is_add_or_inc)
1449 reg_next_inc_use[DF_REF_REGNO (use)] = insn;
1450 else
1451 reg_next_inc_use[DF_REF_REGNO (use)] = NULL;
1454 else if (dump_file)
1455 fprintf (dump_file, "skipping update of deleted insn %d\n",
1456 INSN_UID (insn));
1459 /* If we were successful, try again. There may have been several
1460 opportunities that were interleaved. This is rare but
1461 gcc.c-torture/compile/pr17273.c actually exhibits this. */
1462 if (success_in_block)
1464 /* In this case, we must clear these vectors since the trick of
1465 testing if the stale insn in the block will not work. */
1466 memset (reg_next_use, 0, max_reg * sizeof (rtx));
1467 memset (reg_next_inc_use, 0, max_reg * sizeof (rtx));
1468 memset (reg_next_def, 0, max_reg * sizeof (rtx));
1469 df_recompute_luids (bb);
1470 merge_in_block (max_reg, bb);
1474 #endif
1476 /* Discover auto-inc auto-dec instructions. */
1478 namespace {
1480 const pass_data pass_data_inc_dec =
1482 RTL_PASS, /* type */
1483 "auto_inc_dec", /* name */
1484 OPTGROUP_NONE, /* optinfo_flags */
1485 TV_AUTO_INC_DEC, /* tv_id */
1486 0, /* properties_required */
1487 0, /* properties_provided */
1488 0, /* properties_destroyed */
1489 0, /* todo_flags_start */
1490 TODO_df_finish, /* todo_flags_finish */
1493 class pass_inc_dec : public rtl_opt_pass
1495 public:
1496 pass_inc_dec (gcc::context *ctxt)
1497 : rtl_opt_pass (pass_data_inc_dec, ctxt)
1500 /* opt_pass methods: */
1501 virtual bool gate (function *)
1503 #ifdef AUTO_INC_DEC
1504 return (optimize > 0 && flag_auto_inc_dec);
1505 #else
1506 return false;
1507 #endif
1511 unsigned int execute (function *);
1513 }; // class pass_inc_dec
1515 unsigned int
1516 pass_inc_dec::execute (function *fun ATTRIBUTE_UNUSED)
1518 #ifdef AUTO_INC_DEC
1519 basic_block bb;
1520 int max_reg = max_reg_num ();
1522 if (!initialized)
1523 init_decision_table ();
1525 mem_tmp = gen_rtx_MEM (Pmode, NULL_RTX);
1527 df_note_add_problem ();
1528 df_analyze ();
1530 reg_next_use = XCNEWVEC (rtx_insn *, max_reg);
1531 reg_next_inc_use = XCNEWVEC (rtx_insn *, max_reg);
1532 reg_next_def = XCNEWVEC (rtx_insn *, max_reg);
1533 FOR_EACH_BB_FN (bb, fun)
1534 merge_in_block (max_reg, bb);
1536 free (reg_next_use);
1537 free (reg_next_inc_use);
1538 free (reg_next_def);
1540 mem_tmp = NULL;
1541 #endif
1542 return 0;
1545 } // anon namespace
1547 rtl_opt_pass *
1548 make_pass_inc_dec (gcc::context *ctxt)
1550 return new pass_inc_dec (ctxt);