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
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
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/>. */
23 #include "coretypes.h"
30 #include "insn-config.h"
35 #include "tree-pass.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:
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.)
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. */
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. */
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 */
136 /* The eight forms that pre/post inc/dec can take. */
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++ */
150 /* Tmp mem rtx for use in cost modeling. */
153 static enum inc_state
154 set_inc_state (HOST_WIDE_INT val
, int size
)
159 return (val
== -size
) ? INC_NEG_SIZE
: INC_NEG_ANY
;
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
];
176 init_decision_table (void)
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
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
;
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. */
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. */
305 /* Dump the parsed inc insn to FILE. */
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
)
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
);
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
));
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
);
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
));
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. */
359 rtx reg1
; /* This is either a reg or a const depending on
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. */
366 /* Dump the parsed mem insn to FILE. */
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
);
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. */
406 move_dead_notes (rtx_insn
*to_insn
, rtx_insn
*from_insn
, rtx pattern
)
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
;
422 XEXP (prev_note
, 1) = next_note
;
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. */
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
448 basic_block bb
= BLOCK_FOR_INSN (mem_insn
.insn
);
449 rtx_insn
*mov_insn
= NULL
;
451 rtx mem
= *mem_insn
.mem_loc
;
452 machine_mode mode
= GET_MODE (mem
);
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
)
472 emit_move_insn (inc_insn
.reg_res
, inc_insn
.reg0
);
473 mov_insn
= get_insns ();
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
)
482 fprintf (dump_file
, "cost failure old=%d new=%d\n", old_cost
, new_cost
);
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
491 new_mem
= replace_equiv_address_nv (mem
, new_addr
);
492 if (! validate_change (mem_insn
.insn
, mem_insn
.mem_loc
, new_mem
, 0))
495 fprintf (dump_file
, "validation failure\n");
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
)
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
);
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
;
527 regno
= REGNO (inc_insn
.reg_res
);
528 reg_next_def
[regno
] = mem_insn
.insn
;
529 reg_next_use
[regno
] = NULL
;
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
);
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
);
580 fprintf (dump_file
, "****success ");
581 dump_insn_slim (dump_file
, mem_insn
.insn
);
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. */
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
)
611 last_insn
= mem_insn
.insn
;
615 last_insn
= inc_insn
.insn
;
622 /* Cannot handle auto inc of the stack. */
623 if (inc_reg
== stack_pointer_rtx
)
626 fprintf (dump_file
, "cannot inc stack %d failure\n", REGNO (inc_reg
));
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
)))
635 fprintf (dump_file
, "dead failure %d\n", REGNO (inc_reg
));
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)
657 case SIMPLE_PRE_INC
: /* ++size */
659 fprintf (dump_file
, "trying SIMPLE_PRE_INC\n");
660 return attempt_change (gen_rtx_PRE_INC (reg_mode
, inc_reg
), inc_reg
);
663 case SIMPLE_POST_INC
: /* size++ */
665 fprintf (dump_file
, "trying SIMPLE_POST_INC\n");
666 return attempt_change (gen_rtx_POST_INC (reg_mode
, inc_reg
), inc_reg
);
669 case SIMPLE_PRE_DEC
: /* --size */
671 fprintf (dump_file
, "trying SIMPLE_PRE_DEC\n");
672 return attempt_change (gen_rtx_PRE_DEC (reg_mode
, inc_reg
), inc_reg
);
675 case SIMPLE_POST_DEC
: /* size-- */
677 fprintf (dump_file
, "trying SIMPLE_POST_DEC\n");
678 return attempt_change (gen_rtx_POST_DEC (reg_mode
, inc_reg
), inc_reg
);
681 case DISP_PRE
: /* ++con */
683 fprintf (dump_file
, "trying DISP_PRE\n");
684 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode
,
686 gen_rtx_PLUS (reg_mode
,
692 case DISP_POST
: /* con++ */
694 fprintf (dump_file
, "trying POST_DISP\n");
695 return attempt_change (gen_rtx_POST_MODIFY (reg_mode
,
697 gen_rtx_PLUS (reg_mode
,
703 case REG_PRE
: /* ++reg */
705 fprintf (dump_file
, "trying PRE_REG\n");
706 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode
,
708 gen_rtx_PLUS (reg_mode
,
714 case REG_POST
: /* reg++ */
716 fprintf (dump_file
, "trying POST_REG\n");
717 return attempt_change (gen_rtx_POST_MODIFY (reg_mode
,
719 gen_rtx_PLUS (reg_mode
,
727 /* Return the next insn that uses (if reg_next_use is passed in
728 NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
732 get_next_ref (int regno
, basic_block bb
, rtx_insn
**next_array
)
734 rtx_insn
*insn
= next_array
[regno
];
736 /* Lazy about cleaning out the next_arrays. */
737 if (insn
&& BLOCK_FOR_INSN (insn
) != bb
)
739 next_array
[regno
] = NULL
;
747 /* Return true if INSN is of a form "a = b op c" where a and b are
748 regs. op is + if c is a reg and +|- if c is a const. Fill in
749 INC_INSN with what is found.
751 This function is called in two contexts, if BEFORE_MEM is true,
752 this is called for each insn in the basic block. If BEFORE_MEM is
753 false, it is called for the instruction in the block that uses the
754 index register for some memory reference that is currently being
758 parse_add_or_inc (rtx_insn
*insn
, bool before_mem
)
760 rtx pat
= single_set (insn
);
764 /* Result must be single reg. */
765 if (!REG_P (SET_DEST (pat
)))
768 if ((GET_CODE (SET_SRC (pat
)) != PLUS
)
769 && (GET_CODE (SET_SRC (pat
)) != MINUS
))
772 if (!REG_P (XEXP (SET_SRC (pat
), 0)))
775 inc_insn
.insn
= insn
;
777 inc_insn
.reg_res
= SET_DEST (pat
);
778 inc_insn
.reg0
= XEXP (SET_SRC (pat
), 0);
779 if (rtx_equal_p (inc_insn
.reg_res
, inc_insn
.reg0
))
780 inc_insn
.form
= before_mem
? FORM_PRE_INC
: FORM_POST_INC
;
782 inc_insn
.form
= before_mem
? FORM_PRE_ADD
: FORM_POST_ADD
;
784 if (CONST_INT_P (XEXP (SET_SRC (pat
), 1)))
786 /* Process a = b + c where c is a const. */
787 inc_insn
.reg1_is_const
= true;
788 if (GET_CODE (SET_SRC (pat
)) == PLUS
)
790 inc_insn
.reg1
= XEXP (SET_SRC (pat
), 1);
791 inc_insn
.reg1_val
= INTVAL (inc_insn
.reg1
);
795 inc_insn
.reg1_val
= -INTVAL (XEXP (SET_SRC (pat
), 1));
796 inc_insn
.reg1
= GEN_INT (inc_insn
.reg1_val
);
800 else if ((HAVE_PRE_MODIFY_REG
|| HAVE_POST_MODIFY_REG
)
801 && (REG_P (XEXP (SET_SRC (pat
), 1)))
802 && GET_CODE (SET_SRC (pat
)) == PLUS
)
804 /* Process a = b + c where c is a reg. */
805 inc_insn
.reg1
= XEXP (SET_SRC (pat
), 1);
806 inc_insn
.reg1_is_const
= false;
808 if (inc_insn
.form
== FORM_PRE_INC
809 || inc_insn
.form
== FORM_POST_INC
)
811 else if (rtx_equal_p (inc_insn
.reg_res
, inc_insn
.reg1
))
813 /* Reverse the two operands and turn *_ADD into *_INC since
815 std::swap (inc_insn
.reg0
, inc_insn
.reg1
);
816 inc_insn
.form
= before_mem
? FORM_PRE_INC
: FORM_POST_INC
;
827 /* A recursive function that checks all of the mem uses in
828 ADDRESS_OF_X to see if any single one of them is compatible with
829 what has been found in inc_insn.
831 -1 is returned for success. 0 is returned if nothing was found and
832 1 is returned for failure. */
835 find_address (rtx
*address_of_x
)
837 rtx x
= *address_of_x
;
838 enum rtx_code code
= GET_CODE (x
);
839 const char *const fmt
= GET_RTX_FORMAT (code
);
844 if (code
== MEM
&& rtx_equal_p (XEXP (x
, 0), inc_insn
.reg_res
))
846 /* Match with *reg0. */
847 mem_insn
.mem_loc
= address_of_x
;
848 mem_insn
.reg0
= inc_insn
.reg_res
;
849 mem_insn
.reg1_is_const
= true;
850 mem_insn
.reg1_val
= 0;
851 mem_insn
.reg1
= GEN_INT (0);
854 if (code
== MEM
&& GET_CODE (XEXP (x
, 0)) == PLUS
855 && rtx_equal_p (XEXP (XEXP (x
, 0), 0), inc_insn
.reg_res
))
857 rtx b
= XEXP (XEXP (x
, 0), 1);
858 mem_insn
.mem_loc
= address_of_x
;
859 mem_insn
.reg0
= inc_insn
.reg_res
;
861 mem_insn
.reg1_is_const
= inc_insn
.reg1_is_const
;
864 /* Match with *(reg0 + reg1) where reg1 is a const. */
865 HOST_WIDE_INT val
= INTVAL (b
);
866 if (inc_insn
.reg1_is_const
867 && (inc_insn
.reg1_val
== val
|| inc_insn
.reg1_val
== -val
))
869 mem_insn
.reg1_val
= val
;
873 else if (!inc_insn
.reg1_is_const
874 && rtx_equal_p (inc_insn
.reg1
, b
))
875 /* Match with *(reg0 + reg1). */
879 if (code
== SIGN_EXTRACT
|| code
== ZERO_EXTRACT
)
881 /* If REG occurs inside a MEM used in a bit-field reference,
882 that is unacceptable. */
883 if (find_address (&XEXP (x
, 0)))
887 if (x
== inc_insn
.reg_res
)
890 /* Time for some deep diving. */
891 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
895 tem
= find_address (&XEXP (x
, i
));
896 /* If this is the first use, let it go so the rest of the
897 insn can be checked. */
901 /* More than one match was found. */
904 else if (fmt
[i
] == 'E')
907 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
909 tem
= find_address (&XVECEXP (x
, i
, j
));
910 /* If this is the first use, let it go so the rest of
911 the insn can be checked. */
915 /* More than one match was found. */
923 /* Once a suitable mem reference has been found and the MEM_INSN
924 structure has been filled in, FIND_INC is called to see if there is
925 a suitable add or inc insn that follows the mem reference and
926 determine if it is suitable to merge.
928 In the case where the MEM_INSN has two registers in the reference,
929 this function may be called recursively. The first time looking
930 for an add of the first register, and if that fails, looking for an
931 add of the second register. The FIRST_TRY parameter is used to
932 only allow the parameters to be reversed once. */
935 find_inc (bool first_try
)
938 basic_block bb
= BLOCK_FOR_INSN (mem_insn
.insn
);
939 rtx_insn
*other_insn
;
942 /* Make sure this reg appears only once in this insn. */
943 if (count_occurrences (PATTERN (mem_insn
.insn
), mem_insn
.reg0
, 1) != 1)
946 fprintf (dump_file
, "mem count failure\n");
951 dump_mem_insn (dump_file
);
953 /* Find the next use that is an inc. */
954 insn
= get_next_ref (REGNO (mem_insn
.reg0
),
955 BLOCK_FOR_INSN (mem_insn
.insn
),
960 /* Even though we know the next use is an add or inc because it came
961 from the reg_next_inc_use, we must still reparse. */
962 if (!parse_add_or_inc (insn
, false))
964 /* Next use was not an add. Look for one extra case. It could be
971 if we reverse the operands in the mem ref we would
972 find this. Only try it once though. */
973 if (first_try
&& !mem_insn
.reg1_is_const
)
975 std::swap (mem_insn
.reg0
, mem_insn
.reg1
);
976 return find_inc (false);
982 /* Need to assure that none of the operands of the inc instruction are
983 assigned to by the mem insn. */
984 FOR_EACH_INSN_DEF (def
, mem_insn
.insn
)
986 unsigned int regno
= DF_REF_REGNO (def
);
987 if ((regno
== REGNO (inc_insn
.reg0
))
988 || (regno
== REGNO (inc_insn
.reg_res
)))
991 fprintf (dump_file
, "inc conflicts with store failure.\n");
994 if (!inc_insn
.reg1_is_const
&& (regno
== REGNO (inc_insn
.reg1
)))
997 fprintf (dump_file
, "inc conflicts with store failure.\n");
1003 dump_inc_insn (dump_file
);
1005 if (inc_insn
.form
== FORM_POST_ADD
)
1007 /* Make sure that there is no insn that assigns to inc_insn.res
1008 between the mem_insn and the inc_insn. */
1009 rtx_insn
*other_insn
= get_next_ref (REGNO (inc_insn
.reg_res
),
1010 BLOCK_FOR_INSN (mem_insn
.insn
),
1012 if (other_insn
!= inc_insn
.insn
)
1016 "result of add is assigned to between mem and inc insns.\n");
1020 other_insn
= get_next_ref (REGNO (inc_insn
.reg_res
),
1021 BLOCK_FOR_INSN (mem_insn
.insn
),
1024 && (other_insn
!= inc_insn
.insn
)
1025 && (DF_INSN_LUID (inc_insn
.insn
) > DF_INSN_LUID (other_insn
)))
1029 "result of add is used between mem and inc insns.\n");
1033 /* For the post_add to work, the result_reg of the inc must not be
1034 used in the mem insn since this will become the new index
1036 if (reg_overlap_mentioned_p (inc_insn
.reg_res
, PATTERN (mem_insn
.insn
)))
1039 fprintf (dump_file
, "base reg replacement failure.\n");
1044 if (mem_insn
.reg1_is_const
)
1046 if (mem_insn
.reg1_val
== 0)
1048 if (!inc_insn
.reg1_is_const
)
1050 /* The mem looks like *r0 and the rhs of the add has two
1052 int luid
= DF_INSN_LUID (inc_insn
.insn
);
1053 if (inc_insn
.form
== FORM_POST_ADD
)
1055 /* The trick is that we are not going to increment r0,
1056 we are going to increment the result of the add insn.
1057 For this trick to be correct, the result reg of
1058 the inc must be a valid addressing reg. */
1059 addr_space_t as
= MEM_ADDR_SPACE (*mem_insn
.mem_loc
);
1060 if (GET_MODE (inc_insn
.reg_res
)
1061 != targetm
.addr_space
.address_mode (as
))
1064 fprintf (dump_file
, "base reg mode failure.\n");
1068 /* We also need to make sure that the next use of
1069 inc result is after the inc. */
1071 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_use
);
1072 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1075 if (!rtx_equal_p (mem_insn
.reg0
, inc_insn
.reg0
))
1076 std::swap (inc_insn
.reg0
, inc_insn
.reg1
);
1080 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1081 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1085 /* Both the inc/add and the mem have a constant. Need to check
1086 that the constants are ok. */
1087 else if ((mem_insn
.reg1_val
!= inc_insn
.reg1_val
)
1088 && (mem_insn
.reg1_val
!= -inc_insn
.reg1_val
))
1093 /* The mem insn is of the form *(a + b) where a and b are both
1094 regs. It may be that in order to match the add or inc we
1095 need to treat it as if it was *(b + a). It may also be that
1096 the add is of the form a + c where c does not match b and
1097 then we just abandon this. */
1099 int luid
= DF_INSN_LUID (inc_insn
.insn
);
1100 rtx_insn
*other_insn
;
1102 /* Make sure this reg appears only once in this insn. */
1103 if (count_occurrences (PATTERN (mem_insn
.insn
), mem_insn
.reg1
, 1) != 1)
1106 if (inc_insn
.form
== FORM_POST_ADD
)
1108 /* For this trick to be correct, the result reg of the inc
1109 must be a valid addressing reg. */
1110 addr_space_t as
= MEM_ADDR_SPACE (*mem_insn
.mem_loc
);
1111 if (GET_MODE (inc_insn
.reg_res
)
1112 != targetm
.addr_space
.address_mode (as
))
1115 fprintf (dump_file
, "base reg mode failure.\n");
1119 if (rtx_equal_p (mem_insn
.reg0
, inc_insn
.reg0
))
1121 if (!rtx_equal_p (mem_insn
.reg1
, inc_insn
.reg1
))
1123 /* See comment above on find_inc (false) call. */
1126 std::swap (mem_insn
.reg0
, mem_insn
.reg1
);
1127 return find_inc (false);
1133 /* Need to check that there are no assignments to b
1134 before the add insn. */
1136 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1137 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1139 /* All ok for the next step. */
1143 /* We know that mem_insn.reg0 must equal inc_insn.reg1
1144 or else we would not have found the inc insn. */
1145 std::swap (mem_insn
.reg0
, mem_insn
.reg1
);
1146 if (!rtx_equal_p (mem_insn
.reg0
, inc_insn
.reg0
))
1148 /* See comment above on find_inc (false) call. */
1150 return find_inc (false);
1154 /* To have gotten here know that.
1159 We also know that the lhs of the inc is not b or a. We
1160 need to make sure that there are no assignments to b
1161 between the mem ref and the inc. */
1164 = get_next_ref (REGNO (inc_insn
.reg0
), bb
, reg_next_def
);
1165 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1169 /* Need to check that the next use of the add result is later than
1170 add insn since this will be the reg incremented. */
1172 = get_next_ref (REGNO (inc_insn
.reg_res
), bb
, reg_next_use
);
1173 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1176 else /* FORM_POST_INC. There is less to check here because we
1177 know that operands must line up. */
1179 if (!rtx_equal_p (mem_insn
.reg1
, inc_insn
.reg1
))
1180 /* See comment above on find_inc (false) call. */
1184 std::swap (mem_insn
.reg0
, mem_insn
.reg1
);
1185 return find_inc (false);
1191 /* To have gotten here know that.
1196 We also know that the lhs of the inc is not b. We need to make
1197 sure that there are no assignments to b between the mem ref and
1200 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1201 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1206 if (inc_insn
.form
== FORM_POST_INC
)
1209 = get_next_ref (REGNO (inc_insn
.reg0
), bb
, reg_next_use
);
1210 /* When we found inc_insn, we were looking for the
1211 next add or inc, not the next insn that used the
1212 reg. Because we are going to increment the reg
1213 in this form, we need to make sure that there
1214 were no intervening uses of reg. */
1215 if (inc_insn
.insn
!= other_insn
)
1219 return try_merge ();
1223 /* A recursive function that walks ADDRESS_OF_X to find all of the mem
1224 uses in pat that could be used as an auto inc or dec. It then
1225 calls FIND_INC for each one. */
1228 find_mem (rtx
*address_of_x
)
1230 rtx x
= *address_of_x
;
1231 enum rtx_code code
= GET_CODE (x
);
1232 const char *const fmt
= GET_RTX_FORMAT (code
);
1235 if (code
== MEM
&& REG_P (XEXP (x
, 0)))
1237 /* Match with *reg0. */
1238 mem_insn
.mem_loc
= address_of_x
;
1239 mem_insn
.reg0
= XEXP (x
, 0);
1240 mem_insn
.reg1_is_const
= true;
1241 mem_insn
.reg1_val
= 0;
1242 mem_insn
.reg1
= GEN_INT (0);
1243 if (find_inc (true))
1246 if (code
== MEM
&& GET_CODE (XEXP (x
, 0)) == PLUS
1247 && REG_P (XEXP (XEXP (x
, 0), 0)))
1249 rtx reg1
= XEXP (XEXP (x
, 0), 1);
1250 mem_insn
.mem_loc
= address_of_x
;
1251 mem_insn
.reg0
= XEXP (XEXP (x
, 0), 0);
1252 mem_insn
.reg1
= reg1
;
1253 if (CONST_INT_P (reg1
))
1255 mem_insn
.reg1_is_const
= true;
1256 /* Match with *(reg0 + c) where c is a const. */
1257 mem_insn
.reg1_val
= INTVAL (reg1
);
1258 if (find_inc (true))
1261 else if (REG_P (reg1
))
1263 /* Match with *(reg0 + reg1). */
1264 mem_insn
.reg1_is_const
= false;
1265 if (find_inc (true))
1270 if (code
== SIGN_EXTRACT
|| code
== ZERO_EXTRACT
)
1272 /* If REG occurs inside a MEM used in a bit-field reference,
1273 that is unacceptable. */
1277 /* Time for some deep diving. */
1278 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1282 if (find_mem (&XEXP (x
, i
)))
1285 else if (fmt
[i
] == 'E')
1288 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1289 if (find_mem (&XVECEXP (x
, i
, j
)))
1297 /* Try to combine all incs and decs by constant values with memory
1298 references in BB. */
1301 merge_in_block (int max_reg
, basic_block bb
)
1305 int success_in_block
= 0;
1308 fprintf (dump_file
, "\n\nstarting bb %d\n", bb
->index
);
1310 FOR_BB_INSNS_REVERSE_SAFE (bb
, insn
, curr
)
1312 bool insn_is_add_or_inc
= true;
1314 if (!NONDEBUG_INSN_P (insn
))
1317 /* This continue is deliberate. We do not want the uses of the
1318 jump put into reg_next_use because it is not considered safe to
1319 combine a preincrement with a jump. */
1324 dump_insn_slim (dump_file
, insn
);
1326 /* Does this instruction increment or decrement a register? */
1327 if (parse_add_or_inc (insn
, true))
1329 int regno
= REGNO (inc_insn
.reg_res
);
1330 /* Cannot handle case where there are three separate regs
1331 before a mem ref. Too many moves would be needed to be
1333 if ((inc_insn
.form
== FORM_PRE_INC
) || inc_insn
.reg1_is_const
)
1335 mem_insn
.insn
= get_next_ref (regno
, bb
, reg_next_use
);
1339 if (!inc_insn
.reg1_is_const
)
1341 /* We are only here if we are going to try a
1342 HAVE_*_MODIFY_REG type transformation. c is a
1343 reg and we must sure that the path from the
1344 inc_insn to the mem_insn.insn is both def and use
1345 clear of c because the inc insn is going to move
1346 into the mem_insn.insn. */
1347 int luid
= DF_INSN_LUID (mem_insn
.insn
);
1348 rtx_insn
*other_insn
1349 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_use
);
1351 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1355 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1357 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1362 dump_inc_insn (dump_file
);
1364 if (ok
&& find_address (&PATTERN (mem_insn
.insn
)) == -1)
1367 dump_mem_insn (dump_file
);
1371 insn_is_add_or_inc
= false;
1379 insn_is_add_or_inc
= false;
1380 mem_insn
.insn
= insn
;
1381 if (find_mem (&PATTERN (insn
)))
1385 /* If the inc insn was merged with a mem, the inc insn is gone
1386 and there is noting to update. */
1387 if (df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
))
1391 /* Need to update next use. */
1392 FOR_EACH_INSN_INFO_DEF (def
, insn_info
)
1394 reg_next_use
[DF_REF_REGNO (def
)] = NULL
;
1395 reg_next_inc_use
[DF_REF_REGNO (def
)] = NULL
;
1396 reg_next_def
[DF_REF_REGNO (def
)] = insn
;
1399 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
1401 reg_next_use
[DF_REF_REGNO (use
)] = insn
;
1402 if (insn_is_add_or_inc
)
1403 reg_next_inc_use
[DF_REF_REGNO (use
)] = insn
;
1405 reg_next_inc_use
[DF_REF_REGNO (use
)] = NULL
;
1409 fprintf (dump_file
, "skipping update of deleted insn %d\n",
1413 /* If we were successful, try again. There may have been several
1414 opportunities that were interleaved. This is rare but
1415 gcc.c-torture/compile/pr17273.c actually exhibits this. */
1416 if (success_in_block
)
1418 /* In this case, we must clear these vectors since the trick of
1419 testing if the stale insn in the block will not work. */
1420 memset (reg_next_use
, 0, max_reg
* sizeof (rtx
));
1421 memset (reg_next_inc_use
, 0, max_reg
* sizeof (rtx
));
1422 memset (reg_next_def
, 0, max_reg
* sizeof (rtx
));
1423 df_recompute_luids (bb
);
1424 merge_in_block (max_reg
, bb
);
1428 /* Discover auto-inc auto-dec instructions. */
1432 const pass_data pass_data_inc_dec
=
1434 RTL_PASS
, /* type */
1435 "auto_inc_dec", /* name */
1436 OPTGROUP_NONE
, /* optinfo_flags */
1437 TV_AUTO_INC_DEC
, /* tv_id */
1438 0, /* properties_required */
1439 0, /* properties_provided */
1440 0, /* properties_destroyed */
1441 0, /* todo_flags_start */
1442 TODO_df_finish
, /* todo_flags_finish */
1445 class pass_inc_dec
: public rtl_opt_pass
1448 pass_inc_dec (gcc::context
*ctxt
)
1449 : rtl_opt_pass (pass_data_inc_dec
, ctxt
)
1452 /* opt_pass methods: */
1453 virtual bool gate (function
*)
1458 return (optimize
> 0 && flag_auto_inc_dec
);
1462 unsigned int execute (function
*);
1464 }; // class pass_inc_dec
1467 pass_inc_dec::execute (function
*fun ATTRIBUTE_UNUSED
)
1473 int max_reg
= max_reg_num ();
1476 init_decision_table ();
1478 mem_tmp
= gen_rtx_MEM (Pmode
, NULL_RTX
);
1480 df_note_add_problem ();
1483 reg_next_use
= XCNEWVEC (rtx_insn
*, max_reg
);
1484 reg_next_inc_use
= XCNEWVEC (rtx_insn
*, max_reg
);
1485 reg_next_def
= XCNEWVEC (rtx_insn
*, max_reg
);
1486 FOR_EACH_BB_FN (bb
, fun
)
1487 merge_in_block (max_reg
, bb
);
1489 free (reg_next_use
);
1490 free (reg_next_inc_use
);
1491 free (reg_next_def
);
1501 make_pass_inc_dec (gcc::context
*ctxt
)
1503 return new pass_inc_dec (ctxt
);