1 /* Discovery of auto-inc and auto-dec instructions.
2 Copyright (C) 2006, 2007, 2008, 2009 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"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "insn-config.h"
40 #include "tree-pass.h"
44 /* This pass was originally removed from flow.c. However there is
45 almost nothing that remains of that code.
47 There are (4) basic forms that are matched:
69 for this case to be true, b must not be assigned or used between
70 the *a and the assignment to b. B must also be a Pmode reg.
85 There are three types of values of c.
87 1) c is a constant equal to the width of the value being accessed by
88 the pointer. This is useful for machines that have
89 HAVE_PRE_INCREMENT, HAVE_POST_INCREMENT, HAVE_PRE_DECREMENT or
90 HAVE_POST_DECREMENT defined.
92 2) c is a constant not equal to the width of the value being accessed
93 by the pointer. This is useful for machines that have
94 HAVE_PRE_MODIFY_DISP, HAVE_POST_MODIFY_DISP defined.
96 3) c is a register. This is useful for machines that have
97 HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
99 The is one special case: if a already had an offset equal to it +-
100 its width and that offset is equal to -c when the increment was
101 before the ref or +c if the increment was after the ref, then if we
102 can do the combination but switch the pre/post bit.
132 for this case to be true, b must not be assigned or used between
133 the *a and the assignment to b. B must also be a Pmode reg.
163 /* The states of the second operands of mem refs and inc insns. If no
164 second operand of the mem_ref was found, it is assumed to just be
165 ZERO. SIZE is the size of the mode accessed in the memref. The
166 ANY is used for constants that are not +-size or 0. REG is used if
167 the forms are reg1 + reg2. */
172 INC_NEG_SIZE
, /* == +size */
173 INC_POS_SIZE
, /* == -size */
174 INC_NEG_ANY
, /* == some -constant */
175 INC_POS_ANY
, /* == some +constant */
176 INC_REG
, /* == some register */
180 /* The eight forms that pre/post inc/dec can take. */
184 SIMPLE_PRE_INC
, /* ++size */
185 SIMPLE_POST_INC
, /* size++ */
186 SIMPLE_PRE_DEC
, /* --size */
187 SIMPLE_POST_DEC
, /* size-- */
188 DISP_PRE
, /* ++con */
189 DISP_POST
, /* con++ */
194 /* Tmp mem rtx for use in cost modeling. */
197 static enum inc_state
198 set_inc_state (HOST_WIDE_INT val
, int size
)
203 return (val
== -size
) ? INC_NEG_SIZE
: INC_NEG_ANY
;
205 return (val
== size
) ? INC_POS_SIZE
: INC_POS_ANY
;
208 /* The DECISION_TABLE that describes what form, if any, the increment
209 or decrement will take. It is a three dimensional table. The first
210 index is the type of constant or register found as the second
211 operand of the inc insn. The second index is the type of constant
212 or register found as the second operand of the memory reference (if
213 no second operand exists, 0 is used). The third index is the form
214 and location (relative to the mem reference) of inc insn. */
216 static bool initialized
= false;
217 static enum gen_form decision_table
[INC_last
][INC_last
][FORM_last
];
220 init_decision_table (void)
224 if (HAVE_PRE_INCREMENT
|| HAVE_PRE_MODIFY_DISP
)
226 /* Prefer the simple form if both are available. */
227 value
= (HAVE_PRE_INCREMENT
) ? SIMPLE_PRE_INC
: DISP_PRE
;
229 decision_table
[INC_POS_SIZE
][INC_ZERO
][FORM_PRE_ADD
] = value
;
230 decision_table
[INC_POS_SIZE
][INC_ZERO
][FORM_PRE_INC
] = value
;
232 decision_table
[INC_POS_SIZE
][INC_POS_SIZE
][FORM_POST_ADD
] = value
;
233 decision_table
[INC_POS_SIZE
][INC_POS_SIZE
][FORM_POST_INC
] = value
;
236 if (HAVE_POST_INCREMENT
|| HAVE_POST_MODIFY_DISP
)
238 /* Prefer the simple form if both are available. */
239 value
= (HAVE_POST_INCREMENT
) ? SIMPLE_POST_INC
: DISP_POST
;
241 decision_table
[INC_POS_SIZE
][INC_ZERO
][FORM_POST_ADD
] = value
;
242 decision_table
[INC_POS_SIZE
][INC_ZERO
][FORM_POST_INC
] = value
;
244 decision_table
[INC_POS_SIZE
][INC_NEG_SIZE
][FORM_PRE_ADD
] = value
;
245 decision_table
[INC_POS_SIZE
][INC_NEG_SIZE
][FORM_PRE_INC
] = value
;
248 if (HAVE_PRE_DECREMENT
|| HAVE_PRE_MODIFY_DISP
)
250 /* Prefer the simple form if both are available. */
251 value
= (HAVE_PRE_DECREMENT
) ? SIMPLE_PRE_DEC
: DISP_PRE
;
253 decision_table
[INC_NEG_SIZE
][INC_ZERO
][FORM_PRE_ADD
] = value
;
254 decision_table
[INC_NEG_SIZE
][INC_ZERO
][FORM_PRE_INC
] = value
;
256 decision_table
[INC_NEG_SIZE
][INC_NEG_SIZE
][FORM_POST_ADD
] = value
;
257 decision_table
[INC_NEG_SIZE
][INC_NEG_SIZE
][FORM_POST_INC
] = value
;
260 if (HAVE_POST_DECREMENT
|| HAVE_POST_MODIFY_DISP
)
262 /* Prefer the simple form if both are available. */
263 value
= (HAVE_POST_DECREMENT
) ? SIMPLE_POST_DEC
: DISP_POST
;
265 decision_table
[INC_NEG_SIZE
][INC_ZERO
][FORM_POST_ADD
] = value
;
266 decision_table
[INC_NEG_SIZE
][INC_ZERO
][FORM_POST_INC
] = value
;
268 decision_table
[INC_NEG_SIZE
][INC_POS_SIZE
][FORM_PRE_ADD
] = value
;
269 decision_table
[INC_NEG_SIZE
][INC_POS_SIZE
][FORM_PRE_INC
] = value
;
272 if (HAVE_PRE_MODIFY_DISP
)
274 decision_table
[INC_POS_ANY
][INC_ZERO
][FORM_PRE_ADD
] = DISP_PRE
;
275 decision_table
[INC_POS_ANY
][INC_ZERO
][FORM_PRE_INC
] = DISP_PRE
;
277 decision_table
[INC_POS_ANY
][INC_POS_ANY
][FORM_POST_ADD
] = DISP_PRE
;
278 decision_table
[INC_POS_ANY
][INC_POS_ANY
][FORM_POST_INC
] = DISP_PRE
;
280 decision_table
[INC_NEG_ANY
][INC_ZERO
][FORM_PRE_ADD
] = DISP_PRE
;
281 decision_table
[INC_NEG_ANY
][INC_ZERO
][FORM_PRE_INC
] = DISP_PRE
;
283 decision_table
[INC_NEG_ANY
][INC_NEG_ANY
][FORM_POST_ADD
] = DISP_PRE
;
284 decision_table
[INC_NEG_ANY
][INC_NEG_ANY
][FORM_POST_INC
] = DISP_PRE
;
287 if (HAVE_POST_MODIFY_DISP
)
289 decision_table
[INC_POS_ANY
][INC_ZERO
][FORM_POST_ADD
] = DISP_POST
;
290 decision_table
[INC_POS_ANY
][INC_ZERO
][FORM_POST_INC
] = DISP_POST
;
292 decision_table
[INC_POS_ANY
][INC_NEG_ANY
][FORM_PRE_ADD
] = DISP_POST
;
293 decision_table
[INC_POS_ANY
][INC_NEG_ANY
][FORM_PRE_INC
] = DISP_POST
;
295 decision_table
[INC_NEG_ANY
][INC_ZERO
][FORM_POST_ADD
] = DISP_POST
;
296 decision_table
[INC_NEG_ANY
][INC_ZERO
][FORM_POST_INC
] = DISP_POST
;
298 decision_table
[INC_NEG_ANY
][INC_POS_ANY
][FORM_PRE_ADD
] = DISP_POST
;
299 decision_table
[INC_NEG_ANY
][INC_POS_ANY
][FORM_PRE_INC
] = DISP_POST
;
302 /* This is much simpler than the other cases because we do not look
303 for the reg1-reg2 case. Note that we do not have a INC_POS_REG
304 and INC_NEG_REG states. Most of the use of such states would be
305 on a target that had an R1 - R2 update address form.
307 There is the remote possibility that you could also catch a = a +
308 b; *(a - b) as a postdecrement of (a + b). However, it is
309 unclear if *(a - b) would ever be generated on a machine that did
310 not have that kind of addressing mode. The IA-64 and RS6000 will
311 not do this, and I cannot speak for any other. If any
312 architecture does have an a-b update for, these cases should be
314 if (HAVE_PRE_MODIFY_REG
)
316 decision_table
[INC_REG
][INC_ZERO
][FORM_PRE_ADD
] = REG_PRE
;
317 decision_table
[INC_REG
][INC_ZERO
][FORM_PRE_INC
] = REG_PRE
;
319 decision_table
[INC_REG
][INC_REG
][FORM_POST_ADD
] = REG_PRE
;
320 decision_table
[INC_REG
][INC_REG
][FORM_POST_INC
] = REG_PRE
;
323 if (HAVE_POST_MODIFY_REG
)
325 decision_table
[INC_REG
][INC_ZERO
][FORM_POST_ADD
] = REG_POST
;
326 decision_table
[INC_REG
][INC_ZERO
][FORM_POST_INC
] = REG_POST
;
332 /* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
333 "reg_res = reg0+c". */
335 static struct inc_insn
337 rtx insn
; /* The insn being parsed. */
338 rtx pat
; /* The pattern of the insn. */
339 bool reg1_is_const
; /* True if reg1 is const, false if reg1 is a reg. */
344 enum inc_state reg1_state
;/* The form of the const if reg1 is a const. */
345 HOST_WIDE_INT reg1_val
;/* Value if reg1 is const. */
349 /* Dump the parsed inc insn to FILE. */
352 dump_inc_insn (FILE *file
)
354 const char *f
= ((inc_insn
.form
== FORM_PRE_ADD
)
355 || (inc_insn
.form
== FORM_PRE_INC
)) ? "pre" : "post";
357 dump_insn_slim (file
, inc_insn
.insn
);
359 switch (inc_insn
.form
)
363 if (inc_insn
.reg1_is_const
)
364 fprintf (file
, "found %s add(%d) r[%d]=r[%d]+%d\n",
365 f
, INSN_UID (inc_insn
.insn
),
366 REGNO (inc_insn
.reg_res
),
367 REGNO (inc_insn
.reg0
), (int) inc_insn
.reg1_val
);
369 fprintf (file
, "found %s add(%d) r[%d]=r[%d]+r[%d]\n",
370 f
, INSN_UID (inc_insn
.insn
),
371 REGNO (inc_insn
.reg_res
),
372 REGNO (inc_insn
.reg0
), REGNO (inc_insn
.reg1
));
377 if (inc_insn
.reg1_is_const
)
378 fprintf (file
, "found %s inc(%d) r[%d]+=%d\n",
379 f
, INSN_UID (inc_insn
.insn
),
380 REGNO (inc_insn
.reg_res
), (int) inc_insn
.reg1_val
);
382 fprintf (file
, "found %s inc(%d) r[%d]+=r[%d]\n",
383 f
, INSN_UID (inc_insn
.insn
),
384 REGNO (inc_insn
.reg_res
), REGNO (inc_insn
.reg1
));
393 /* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
395 static struct mem_insn
397 rtx insn
; /* The insn being parsed. */
398 rtx pat
; /* The pattern of the insn. */
399 rtx
*mem_loc
; /* The address of the field that holds the mem */
400 /* that is to be replaced. */
401 bool reg1_is_const
; /* True if reg1 is const, false if reg1 is a reg. */
403 rtx reg1
; /* This is either a reg or a const depending on
405 enum inc_state reg1_state
;/* The form of the const if reg1 is a const. */
406 HOST_WIDE_INT reg1_val
;/* Value if reg1 is const. */
410 /* Dump the parsed mem insn to FILE. */
413 dump_mem_insn (FILE *file
)
415 dump_insn_slim (file
, mem_insn
.insn
);
417 if (mem_insn
.reg1_is_const
)
418 fprintf (file
, "found mem(%d) *(r[%d]+%d)\n",
419 INSN_UID (mem_insn
.insn
),
420 REGNO (mem_insn
.reg0
), (int) mem_insn
.reg1_val
);
422 fprintf (file
, "found mem(%d) *(r[%d]+r[%d])\n",
423 INSN_UID (mem_insn
.insn
),
424 REGNO (mem_insn
.reg0
), REGNO (mem_insn
.reg1
));
428 /* The following three arrays contain pointers to instructions. They
429 are indexed by REGNO. At any point in the basic block where we are
430 looking these three arrays contain, respectively, the next insn
431 that uses REGNO, the next inc or add insn that uses REGNO and the
432 next insn that sets REGNO.
434 The arrays are not cleared when we move from block to block so
435 whenever an insn is retrieved from these arrays, it's block number
436 must be compared with the current block.
439 static rtx
*reg_next_use
= NULL
;
440 static rtx
*reg_next_inc_use
= NULL
;
441 static rtx
*reg_next_def
= NULL
;
444 /* Move dead note that match PATTERN to TO_INSN from FROM_INSN. We do
445 not really care about moving any other notes from the inc or add
446 insn. Moving the REG_EQUAL and REG_EQUIV is clearly wrong and it
447 does not appear that there are any other kinds of relevant notes. */
450 move_dead_notes (rtx to_insn
, rtx from_insn
, rtx pattern
)
454 rtx prev_note
= NULL
;
456 for (note
= REG_NOTES (from_insn
); note
; note
= next_note
)
458 next_note
= XEXP (note
, 1);
460 if ((REG_NOTE_KIND (note
) == REG_DEAD
)
461 && pattern
== XEXP (note
, 0))
463 XEXP (note
, 1) = REG_NOTES (to_insn
);
464 REG_NOTES (to_insn
) = note
;
466 XEXP (prev_note
, 1) = next_note
;
468 REG_NOTES (from_insn
) = next_note
;
470 else prev_note
= note
;
475 /* Create a mov insn DEST_REG <- SRC_REG and insert it before
479 insert_move_insn_before (rtx next_insn
, rtx dest_reg
, rtx src_reg
)
484 emit_move_insn (dest_reg
, src_reg
);
485 insns
= get_insns ();
487 emit_insn_before (insns
, next_insn
);
492 /* Change mem_insn.mem_loc so that uses NEW_ADDR which has an
493 increment of INC_REG. To have reached this point, the change is a
494 legitimate one from a dataflow point of view. The only questions
495 are is this a valid change to the instruction and is this a
496 profitable change to the instruction. */
499 attempt_change (rtx new_addr
, rtx inc_reg
)
501 /* There are four cases: For the two cases that involve an add
502 instruction, we are going to have to delete the add and insert a
503 mov. We are going to assume that the mov is free. This is
504 fairly early in the backend and there are a lot of opportunities
505 for removing that move later. In particular, there is the case
506 where the move may be dead, this is what dead code elimination
507 passes are for. The two cases where we have an inc insn will be
510 basic_block bb
= BASIC_BLOCK (BLOCK_NUM (mem_insn
.insn
));
513 rtx mem
= *mem_insn
.mem_loc
;
514 enum machine_mode mode
= GET_MODE (mem
);
518 bool speed
= optimize_bb_for_speed_p (bb
);
520 PUT_MODE (mem_tmp
, mode
);
521 XEXP (mem_tmp
, 0) = new_addr
;
523 old_cost
= rtx_cost (mem
, 0, speed
)
524 + rtx_cost (PATTERN (inc_insn
.insn
), 0, speed
);
525 new_cost
= rtx_cost (mem_tmp
, 0, speed
);
527 /* The first item of business is to see if this is profitable. */
528 if (old_cost
< new_cost
)
531 fprintf (dump_file
, "cost failure old=%d new=%d\n", old_cost
, new_cost
);
535 /* Jump thru a lot of hoops to keep the attributes up to date. We
536 do not want to call one of the change address variants that take
537 an offset even though we know the offset in many cases. These
538 assume you are changing where the address is pointing by the
540 new_mem
= replace_equiv_address_nv (mem
, new_addr
);
541 if (! validate_change (mem_insn
.insn
, mem_insn
.mem_loc
, new_mem
, 0))
544 fprintf (dump_file
, "validation failure\n");
548 /* From here to the end of the function we are committed to the
549 change, i.e. nothing fails. Generate any necessary movs, move
550 any regnotes, and fix up the reg_next_{use,inc_use,def}. */
551 switch (inc_insn
.form
)
554 /* Replace the addition with a move. Do it at the location of
555 the addition since the operand of the addition may change
556 before the memory reference. */
557 mov_insn
= insert_move_insn_before (inc_insn
.insn
,
558 inc_insn
.reg_res
, inc_insn
.reg0
);
559 move_dead_notes (mov_insn
, inc_insn
.insn
, inc_insn
.reg0
);
561 regno
= REGNO (inc_insn
.reg_res
);
562 reg_next_def
[regno
] = mov_insn
;
563 reg_next_use
[regno
] = NULL
;
564 regno
= REGNO (inc_insn
.reg0
);
565 reg_next_use
[regno
] = mov_insn
;
566 df_recompute_luids (bb
);
570 regno
= REGNO (inc_insn
.reg_res
);
571 if (reg_next_use
[regno
] == reg_next_inc_use
[regno
])
572 reg_next_inc_use
[regno
] = NULL
;
576 regno
= REGNO (inc_insn
.reg_res
);
577 reg_next_def
[regno
] = mem_insn
.insn
;
578 reg_next_use
[regno
] = NULL
;
583 mov_insn
= insert_move_insn_before (mem_insn
.insn
,
584 inc_insn
.reg_res
, inc_insn
.reg0
);
585 move_dead_notes (mov_insn
, inc_insn
.insn
, inc_insn
.reg0
);
587 /* Do not move anything to the mov insn because the instruction
588 pointer for the main iteration has not yet hit that. It is
589 still pointing to the mem insn. */
590 regno
= REGNO (inc_insn
.reg_res
);
591 reg_next_def
[regno
] = mem_insn
.insn
;
592 reg_next_use
[regno
] = NULL
;
594 regno
= REGNO (inc_insn
.reg0
);
595 reg_next_use
[regno
] = mem_insn
.insn
;
596 if ((reg_next_use
[regno
] == reg_next_inc_use
[regno
])
597 || (reg_next_inc_use
[regno
] == inc_insn
.insn
))
598 reg_next_inc_use
[regno
] = NULL
;
599 df_recompute_luids (bb
);
607 if (!inc_insn
.reg1_is_const
)
609 regno
= REGNO (inc_insn
.reg1
);
610 reg_next_use
[regno
] = mem_insn
.insn
;
611 if ((reg_next_use
[regno
] == reg_next_inc_use
[regno
])
612 || (reg_next_inc_use
[regno
] == inc_insn
.insn
))
613 reg_next_inc_use
[regno
] = NULL
;
616 delete_insn (inc_insn
.insn
);
618 if (dump_file
&& mov_insn
)
620 fprintf (dump_file
, "inserting mov ");
621 dump_insn_slim (dump_file
, mov_insn
);
624 /* Record that this insn has an implicit side effect. */
625 add_reg_note (mem_insn
.insn
, REG_INC
, inc_reg
);
629 fprintf (dump_file
, "****success ");
630 dump_insn_slim (dump_file
, mem_insn
.insn
);
637 /* Try to combine the instruction in INC_INSN with the instruction in
638 MEM_INSN. First the form is determined using the DECISION_TABLE
639 and the results of parsing the INC_INSN and the MEM_INSN.
640 Assuming the form is ok, a prototype new address is built which is
641 passed to ATTEMPT_CHANGE for final processing. */
646 enum gen_form gen_form
;
647 rtx mem
= *mem_insn
.mem_loc
;
648 rtx inc_reg
= inc_insn
.form
== FORM_POST_ADD
?
649 inc_insn
.reg_res
: mem_insn
.reg0
;
651 /* The width of the mem being accessed. */
652 int size
= GET_MODE_SIZE (GET_MODE (mem
));
653 rtx last_insn
= NULL
;
655 switch (inc_insn
.form
)
659 last_insn
= mem_insn
.insn
;
663 last_insn
= inc_insn
.insn
;
670 /* Cannot handle auto inc of the stack. */
671 if (inc_reg
== stack_pointer_rtx
)
674 fprintf (dump_file
, "cannot inc stack %d failure\n", REGNO (inc_reg
));
678 /* Look to see if the inc register is dead after the memory
679 reference. If it is, do not do the combination. */
680 if (find_regno_note (last_insn
, REG_DEAD
, REGNO (inc_reg
)))
683 fprintf (dump_file
, "dead failure %d\n", REGNO (inc_reg
));
687 mem_insn
.reg1_state
= (mem_insn
.reg1_is_const
)
688 ? set_inc_state (mem_insn
.reg1_val
, size
) : INC_REG
;
689 inc_insn
.reg1_state
= (inc_insn
.reg1_is_const
)
690 ? set_inc_state (inc_insn
.reg1_val
, size
) : INC_REG
;
692 /* Now get the form that we are generating. */
693 gen_form
= decision_table
694 [inc_insn
.reg1_state
][mem_insn
.reg1_state
][inc_insn
.form
];
696 if (dbg_cnt (auto_inc_dec
) == false)
705 case SIMPLE_PRE_INC
: /* ++size */
707 fprintf (dump_file
, "trying SIMPLE_PRE_INC\n");
708 return attempt_change (gen_rtx_PRE_INC (Pmode
, inc_reg
), inc_reg
);
711 case SIMPLE_POST_INC
: /* size++ */
713 fprintf (dump_file
, "trying SIMPLE_POST_INC\n");
714 return attempt_change (gen_rtx_POST_INC (Pmode
, inc_reg
), inc_reg
);
717 case SIMPLE_PRE_DEC
: /* --size */
719 fprintf (dump_file
, "trying SIMPLE_PRE_DEC\n");
720 return attempt_change (gen_rtx_PRE_DEC (Pmode
, inc_reg
), inc_reg
);
723 case SIMPLE_POST_DEC
: /* size-- */
725 fprintf (dump_file
, "trying SIMPLE_POST_DEC\n");
726 return attempt_change (gen_rtx_POST_DEC (Pmode
, inc_reg
), inc_reg
);
729 case DISP_PRE
: /* ++con */
731 fprintf (dump_file
, "trying DISP_PRE\n");
732 return attempt_change (gen_rtx_PRE_MODIFY (Pmode
,
740 case DISP_POST
: /* con++ */
742 fprintf (dump_file
, "trying POST_DISP\n");
743 return attempt_change (gen_rtx_POST_MODIFY (Pmode
,
751 case REG_PRE
: /* ++reg */
753 fprintf (dump_file
, "trying PRE_REG\n");
754 return attempt_change (gen_rtx_PRE_MODIFY (Pmode
,
762 case REG_POST
: /* reg++ */
764 fprintf (dump_file
, "trying POST_REG\n");
765 return attempt_change (gen_rtx_POST_MODIFY (Pmode
,
775 /* Return the next insn that uses (if reg_next_use is passed in
776 NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
780 get_next_ref (int regno
, basic_block bb
, rtx
*next_array
)
782 rtx insn
= next_array
[regno
];
784 /* Lazy about cleaning out the next_arrays. */
785 if (insn
&& BASIC_BLOCK (BLOCK_NUM (insn
)) != bb
)
787 next_array
[regno
] = NULL
;
795 /* Reverse the operands in a mem insn. */
800 rtx tmp
= mem_insn
.reg1
;
801 mem_insn
.reg1
= mem_insn
.reg0
;
806 /* Reverse the operands in a inc insn. */
811 rtx tmp
= inc_insn
.reg1
;
812 inc_insn
.reg1
= inc_insn
.reg0
;
817 /* Return true if INSN is of a form "a = b op c" where a and b are
818 regs. op is + if c is a reg and +|- if c is a const. Fill in
819 INC_INSN with what is found.
821 This function is called in two contexts, if BEFORE_MEM is true,
822 this is called for each insn in the basic block. If BEFORE_MEM is
823 false, it is called for the instruction in the block that uses the
824 index register for some memory reference that is currently being
828 parse_add_or_inc (rtx insn
, bool before_mem
)
830 rtx pat
= single_set (insn
);
834 /* Result must be single reg. */
835 if (!REG_P (SET_DEST (pat
)))
838 if ((GET_CODE (SET_SRC (pat
)) != PLUS
)
839 && (GET_CODE (SET_SRC (pat
)) != MINUS
))
842 if (!REG_P (XEXP (SET_SRC (pat
), 0)))
845 inc_insn
.insn
= insn
;
847 inc_insn
.reg_res
= SET_DEST (pat
);
848 inc_insn
.reg0
= XEXP (SET_SRC (pat
), 0);
849 if (rtx_equal_p (inc_insn
.reg_res
, inc_insn
.reg0
))
850 inc_insn
.form
= before_mem
? FORM_PRE_INC
: FORM_POST_INC
;
852 inc_insn
.form
= before_mem
? FORM_PRE_ADD
: FORM_POST_ADD
;
854 if (GET_CODE (XEXP (SET_SRC (pat
), 1)) == CONST_INT
)
856 /* Process a = b + c where c is a const. */
857 inc_insn
.reg1_is_const
= true;
858 if (GET_CODE (SET_SRC (pat
)) == PLUS
)
860 inc_insn
.reg1
= XEXP (SET_SRC (pat
), 1);
861 inc_insn
.reg1_val
= INTVAL (inc_insn
.reg1
);
865 inc_insn
.reg1_val
= -INTVAL (XEXP (SET_SRC (pat
), 1));
866 inc_insn
.reg1
= GEN_INT (inc_insn
.reg1_val
);
870 else if ((HAVE_PRE_MODIFY_REG
|| HAVE_POST_MODIFY_REG
)
871 && (REG_P (XEXP (SET_SRC (pat
), 1)))
872 && GET_CODE (SET_SRC (pat
)) == PLUS
)
874 /* Process a = b + c where c is a reg. */
875 inc_insn
.reg1
= XEXP (SET_SRC (pat
), 1);
876 inc_insn
.reg1_is_const
= false;
878 if (inc_insn
.form
== FORM_PRE_INC
879 || inc_insn
.form
== FORM_POST_INC
)
881 else if (rtx_equal_p (inc_insn
.reg_res
, inc_insn
.reg1
))
883 /* Reverse the two operands and turn *_ADD into *_INC since
886 inc_insn
.form
= before_mem
? FORM_PRE_INC
: FORM_POST_INC
;
897 /* A recursive function that checks all of the mem uses in
898 ADDRESS_OF_X to see if any single one of them is compatible with
899 what has been found in inc_insn.
901 -1 is returned for success. 0 is returned if nothing was found and
902 1 is returned for failure. */
905 find_address (rtx
*address_of_x
)
907 rtx x
= *address_of_x
;
908 enum rtx_code code
= GET_CODE (x
);
909 const char *const fmt
= GET_RTX_FORMAT (code
);
914 if (code
== MEM
&& rtx_equal_p (XEXP (x
, 0), inc_insn
.reg_res
))
916 /* Match with *reg0. */
917 mem_insn
.mem_loc
= address_of_x
;
918 mem_insn
.reg0
= inc_insn
.reg_res
;
919 mem_insn
.reg1_is_const
= true;
920 mem_insn
.reg1_val
= 0;
921 mem_insn
.reg1
= GEN_INT (0);
924 if (code
== MEM
&& GET_CODE (XEXP (x
, 0)) == PLUS
925 && rtx_equal_p (XEXP (XEXP (x
, 0), 0), inc_insn
.reg_res
))
927 rtx b
= XEXP (XEXP (x
, 0), 1);
928 mem_insn
.mem_loc
= address_of_x
;
929 mem_insn
.reg0
= inc_insn
.reg_res
;
931 mem_insn
.reg1_is_const
= inc_insn
.reg1_is_const
;
932 if (GET_CODE (b
) == CONST_INT
)
934 /* Match with *(reg0 + reg1) where reg1 is a const. */
935 HOST_WIDE_INT val
= INTVAL (b
);
936 if (inc_insn
.reg1_is_const
937 && (inc_insn
.reg1_val
== val
|| inc_insn
.reg1_val
== -val
))
939 mem_insn
.reg1_val
= val
;
943 else if (!inc_insn
.reg1_is_const
944 && rtx_equal_p (inc_insn
.reg1
, b
))
945 /* Match with *(reg0 + reg1). */
949 if (code
== SIGN_EXTRACT
|| code
== ZERO_EXTRACT
)
951 /* If REG occurs inside a MEM used in a bit-field reference,
952 that is unacceptable. */
953 if (find_address (&XEXP (x
, 0)))
957 if (x
== inc_insn
.reg_res
)
960 /* Time for some deep diving. */
961 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
965 tem
= find_address (&XEXP (x
, i
));
966 /* If this is the first use, let it go so the rest of the
967 insn can be checked. */
971 /* More than one match was found. */
974 else if (fmt
[i
] == 'E')
977 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
979 tem
= find_address (&XVECEXP (x
, i
, j
));
980 /* If this is the first use, let it go so the rest of
981 the insn can be checked. */
985 /* More than one match was found. */
993 /* Once a suitable mem reference has been found and the MEM_INSN
994 structure has been filled in, FIND_INC is called to see if there is
995 a suitable add or inc insn that follows the mem reference and
996 determine if it is suitable to merge.
998 In the case where the MEM_INSN has two registers in the reference,
999 this function may be called recursively. The first time looking
1000 for an add of the first register, and if that fails, looking for an
1001 add of the second register. The FIRST_TRY parameter is used to
1002 only allow the parameters to be reversed once. */
1005 find_inc (bool first_try
)
1008 basic_block bb
= BASIC_BLOCK (BLOCK_NUM (mem_insn
.insn
));
1012 /* Make sure this reg appears only once in this insn. */
1013 if (count_occurrences (PATTERN (mem_insn
.insn
), mem_insn
.reg0
, 1) != 1)
1016 fprintf (dump_file
, "mem count failure\n");
1021 dump_mem_insn (dump_file
);
1023 /* Find the next use that is an inc. */
1024 insn
= get_next_ref (REGNO (mem_insn
.reg0
),
1025 BASIC_BLOCK (BLOCK_NUM (mem_insn
.insn
)),
1030 /* Even though we know the next use is an add or inc because it came
1031 from the reg_next_inc_use, we must still reparse. */
1032 if (!parse_add_or_inc (insn
, false))
1034 /* Next use was not an add. Look for one extra case. It could be
1041 if we reverse the operands in the mem ref we would
1042 find this. Only try it once though. */
1043 if (first_try
&& !mem_insn
.reg1_is_const
)
1046 return find_inc (false);
1052 /* Need to assure that none of the operands of the inc instruction are
1053 assigned to by the mem insn. */
1054 for (def_rec
= DF_INSN_DEFS (mem_insn
.insn
); *def_rec
; def_rec
++)
1056 df_ref def
= *def_rec
;
1057 unsigned int regno
= DF_REF_REGNO (def
);
1058 if ((regno
== REGNO (inc_insn
.reg0
))
1059 || (regno
== REGNO (inc_insn
.reg_res
)))
1062 fprintf (dump_file
, "inc conflicts with store failure.\n");
1065 if (!inc_insn
.reg1_is_const
&& (regno
== REGNO (inc_insn
.reg1
)))
1068 fprintf (dump_file
, "inc conflicts with store failure.\n");
1074 dump_inc_insn (dump_file
);
1076 if (inc_insn
.form
== FORM_POST_ADD
)
1078 /* Make sure that there is no insn that assigns to inc_insn.res
1079 between the mem_insn and the inc_insn. */
1080 rtx other_insn
= get_next_ref (REGNO (inc_insn
.reg_res
),
1081 BASIC_BLOCK (BLOCK_NUM (mem_insn
.insn
)),
1083 if (other_insn
!= inc_insn
.insn
)
1087 "result of add is assigned to between mem and inc insns.\n");
1091 other_insn
= get_next_ref (REGNO (inc_insn
.reg_res
),
1092 BASIC_BLOCK (BLOCK_NUM (mem_insn
.insn
)),
1095 && (other_insn
!= inc_insn
.insn
)
1096 && (DF_INSN_LUID (inc_insn
.insn
) > DF_INSN_LUID (other_insn
)))
1100 "result of add is used between mem and inc insns.\n");
1104 /* For the post_add to work, the result_reg of the inc must not be
1105 used in the mem insn since this will become the new index
1107 if (count_occurrences (PATTERN (mem_insn
.insn
), inc_insn
.reg_res
, 1) != 0)
1110 fprintf (dump_file
, "base reg replacement failure.\n");
1115 if (mem_insn
.reg1_is_const
)
1117 if (mem_insn
.reg1_val
== 0)
1119 if (!inc_insn
.reg1_is_const
)
1121 /* The mem looks like *r0 and the rhs of the add has two
1123 int luid
= DF_INSN_LUID (inc_insn
.insn
);
1124 if (inc_insn
.form
== FORM_POST_ADD
)
1126 /* The trick is that we are not going to increment r0,
1127 we are going to increment the result of the add insn.
1128 For this trick to be correct, the result reg of
1129 the inc must be a valid addressing reg. */
1130 if (GET_MODE (inc_insn
.reg_res
) != Pmode
)
1133 fprintf (dump_file
, "base reg mode failure.\n");
1137 /* We also need to make sure that the next use of
1138 inc result is after the inc. */
1140 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_use
);
1141 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1144 if (!rtx_equal_p (mem_insn
.reg0
, inc_insn
.reg0
))
1149 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1150 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1154 /* Both the inc/add and the mem have a constant. Need to check
1155 that the constants are ok. */
1156 else if ((mem_insn
.reg1_val
!= inc_insn
.reg1_val
)
1157 && (mem_insn
.reg1_val
!= -inc_insn
.reg1_val
))
1162 /* The mem insn is of the form *(a + b) where a and b are both
1163 regs. It may be that in order to match the add or inc we
1164 need to treat it as if it was *(b + a). It may also be that
1165 the add is of the form a + c where c does not match b and
1166 then we just abandon this. */
1168 int luid
= DF_INSN_LUID (inc_insn
.insn
);
1171 /* Make sure this reg appears only once in this insn. */
1172 if (count_occurrences (PATTERN (mem_insn
.insn
), mem_insn
.reg1
, 1) != 1)
1175 if (inc_insn
.form
== FORM_POST_ADD
)
1177 /* For this trick to be correct, the result reg of the inc
1178 must be a valid addressing reg. */
1179 if (GET_MODE (inc_insn
.reg_res
) != Pmode
)
1182 fprintf (dump_file
, "base reg mode failure.\n");
1186 if (rtx_equal_p (mem_insn
.reg0
, inc_insn
.reg0
))
1188 if (!rtx_equal_p (mem_insn
.reg1
, inc_insn
.reg1
))
1190 /* See comment above on find_inc (false) call. */
1194 return find_inc (false);
1200 /* Need to check that there are no assignments to b
1201 before the add insn. */
1203 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1204 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1206 /* All ok for the next step. */
1210 /* We know that mem_insn.reg0 must equal inc_insn.reg1
1211 or else we would not have found the inc insn. */
1213 if (!rtx_equal_p (mem_insn
.reg0
, inc_insn
.reg0
))
1215 /* See comment above on find_inc (false) call. */
1217 return find_inc (false);
1221 /* To have gotten here know that.
1226 We also know that the lhs of the inc is not b or a. We
1227 need to make sure that there are no assignments to b
1228 between the mem ref and the inc. */
1231 = get_next_ref (REGNO (inc_insn
.reg0
), bb
, reg_next_def
);
1232 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1236 /* Need to check that the next use of the add result is later than
1237 add insn since this will be the reg incremented. */
1239 = get_next_ref (REGNO (inc_insn
.reg_res
), bb
, reg_next_use
);
1240 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1243 else /* FORM_POST_INC. There is less to check here because we
1244 know that operands must line up. */
1246 if (!rtx_equal_p (mem_insn
.reg1
, inc_insn
.reg1
))
1247 /* See comment above on find_inc (false) call. */
1252 return find_inc (false);
1258 /* To have gotten here know that.
1263 We also know that the lhs of the inc is not b. We need to make
1264 sure that there are no assignments to b between the mem ref and
1267 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1268 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1273 if (inc_insn
.form
== FORM_POST_INC
)
1276 = get_next_ref (REGNO (inc_insn
.reg0
), bb
, reg_next_use
);
1277 /* When we found inc_insn, we were looking for the
1278 next add or inc, not the next insn that used the
1279 reg. Because we are going to increment the reg
1280 in this form, we need to make sure that there
1281 were no intervening uses of reg. */
1282 if (inc_insn
.insn
!= other_insn
)
1286 return try_merge ();
1290 /* A recursive function that walks ADDRESS_OF_X to find all of the mem
1291 uses in pat that could be used as an auto inc or dec. It then
1292 calls FIND_INC for each one. */
1295 find_mem (rtx
*address_of_x
)
1297 rtx x
= *address_of_x
;
1298 enum rtx_code code
= GET_CODE (x
);
1299 const char *const fmt
= GET_RTX_FORMAT (code
);
1302 if (code
== MEM
&& REG_P (XEXP (x
, 0)))
1304 /* Match with *reg0. */
1305 mem_insn
.mem_loc
= address_of_x
;
1306 mem_insn
.reg0
= XEXP (x
, 0);
1307 mem_insn
.reg1_is_const
= true;
1308 mem_insn
.reg1_val
= 0;
1309 mem_insn
.reg1
= GEN_INT (0);
1310 if (find_inc (true))
1313 if (code
== MEM
&& GET_CODE (XEXP (x
, 0)) == PLUS
1314 && REG_P (XEXP (XEXP (x
, 0), 0)))
1316 rtx reg1
= XEXP (XEXP (x
, 0), 1);
1317 mem_insn
.mem_loc
= address_of_x
;
1318 mem_insn
.reg0
= XEXP (XEXP (x
, 0), 0);
1319 mem_insn
.reg1
= reg1
;
1320 if (GET_CODE (reg1
) == CONST_INT
)
1322 mem_insn
.reg1_is_const
= true;
1323 /* Match with *(reg0 + c) where c is a const. */
1324 mem_insn
.reg1_val
= INTVAL (reg1
);
1325 if (find_inc (true))
1328 else if (REG_P (reg1
))
1330 /* Match with *(reg0 + reg1). */
1331 mem_insn
.reg1_is_const
= false;
1332 if (find_inc (true))
1337 if (code
== SIGN_EXTRACT
|| code
== ZERO_EXTRACT
)
1339 /* If REG occurs inside a MEM used in a bit-field reference,
1340 that is unacceptable. */
1344 /* Time for some deep diving. */
1345 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1349 if (find_mem (&XEXP (x
, i
)))
1352 else if (fmt
[i
] == 'E')
1355 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1356 if (find_mem (&XVECEXP (x
, i
, j
)))
1364 /* Try to combine all incs and decs by constant values with memory
1365 references in BB. */
1368 merge_in_block (int max_reg
, basic_block bb
)
1372 int success_in_block
= 0;
1375 fprintf (dump_file
, "\n\nstarting bb %d\n", bb
->index
);
1377 FOR_BB_INSNS_REVERSE_SAFE (bb
, insn
, curr
)
1379 unsigned int uid
= INSN_UID (insn
);
1380 bool insn_is_add_or_inc
= true;
1385 /* This continue is deliberate. We do not want the uses of the
1386 jump put into reg_next_use because it is not considered safe to
1387 combine a preincrement with a jump. */
1392 dump_insn_slim (dump_file
, insn
);
1394 /* Does this instruction increment or decrement a register? */
1395 if (parse_add_or_inc (insn
, true))
1397 int regno
= REGNO (inc_insn
.reg_res
);
1398 /* Cannot handle case where there are three separate regs
1399 before a mem ref. Too many moves would be needed to be
1401 if ((inc_insn
.form
== FORM_PRE_INC
) || inc_insn
.reg1_is_const
)
1403 mem_insn
.insn
= get_next_ref (regno
, bb
, reg_next_use
);
1407 if (!inc_insn
.reg1_is_const
)
1409 /* We are only here if we are going to try a
1410 HAVE_*_MODIFY_REG type transformation. c is a
1411 reg and we must sure that the path from the
1412 inc_insn to the mem_insn.insn is both def and use
1413 clear of c because the inc insn is going to move
1414 into the mem_insn.insn. */
1415 int luid
= DF_INSN_LUID (mem_insn
.insn
);
1417 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_use
);
1419 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1423 = get_next_ref (REGNO (inc_insn
.reg1
), bb
, reg_next_def
);
1425 if (other_insn
&& luid
> DF_INSN_LUID (other_insn
))
1430 dump_inc_insn (dump_file
);
1432 if (ok
&& find_address (&PATTERN (mem_insn
.insn
)) == -1)
1435 dump_mem_insn (dump_file
);
1439 insn_is_add_or_inc
= false;
1447 insn_is_add_or_inc
= false;
1448 mem_insn
.insn
= insn
;
1449 if (find_mem (&PATTERN (insn
)))
1453 /* If the inc insn was merged with a mem, the inc insn is gone
1454 and there is noting to update. */
1455 if (DF_INSN_UID_GET(uid
))
1459 /* Need to update next use. */
1460 for (def_rec
= DF_INSN_UID_DEFS (uid
); *def_rec
; def_rec
++)
1462 df_ref def
= *def_rec
;
1463 reg_next_use
[DF_REF_REGNO (def
)] = NULL
;
1464 reg_next_inc_use
[DF_REF_REGNO (def
)] = NULL
;
1465 reg_next_def
[DF_REF_REGNO (def
)] = insn
;
1468 for (use_rec
= DF_INSN_UID_USES (uid
); *use_rec
; use_rec
++)
1470 df_ref use
= *use_rec
;
1471 reg_next_use
[DF_REF_REGNO (use
)] = insn
;
1472 if (insn_is_add_or_inc
)
1473 reg_next_inc_use
[DF_REF_REGNO (use
)] = insn
;
1475 reg_next_inc_use
[DF_REF_REGNO (use
)] = NULL
;
1479 fprintf (dump_file
, "skipping update of deleted insn %d\n", uid
);
1482 /* If we were successful, try again. There may have been several
1483 opportunities that were interleaved. This is rare but
1484 gcc.c-torture/compile/pr17273.c actually exhibits this. */
1485 if (success_in_block
)
1487 /* In this case, we must clear these vectors since the trick of
1488 testing if the stale insn in the block will not work. */
1489 memset (reg_next_use
, 0, max_reg
* sizeof(rtx
));
1490 memset (reg_next_inc_use
, 0, max_reg
* sizeof(rtx
));
1491 memset (reg_next_def
, 0, max_reg
* sizeof(rtx
));
1492 df_recompute_luids (bb
);
1493 merge_in_block (max_reg
, bb
);
1500 rest_of_handle_auto_inc_dec (void)
1504 int max_reg
= max_reg_num ();
1507 init_decision_table ();
1509 mem_tmp
= gen_rtx_MEM (Pmode
, NULL_RTX
);
1511 df_note_add_problem ();
1514 reg_next_use
= XCNEWVEC (rtx
, max_reg
);
1515 reg_next_inc_use
= XCNEWVEC (rtx
, max_reg
);
1516 reg_next_def
= XCNEWVEC (rtx
, max_reg
);
1518 merge_in_block (max_reg
, bb
);
1520 free (reg_next_use
);
1521 free (reg_next_inc_use
);
1522 free (reg_next_def
);
1530 /* Discover auto-inc auto-dec instructions. */
1533 gate_auto_inc_dec (void)
1536 return (optimize
> 0 && flag_auto_inc_dec
);
1543 struct rtl_opt_pass pass_inc_dec
=
1547 "auto_inc_dec", /* name */
1548 gate_auto_inc_dec
, /* gate */
1549 rest_of_handle_auto_inc_dec
, /* execute */
1552 0, /* static_pass_number */
1553 TV_AUTO_INC_DEC
, /* tv_id */
1554 0, /* properties_required */
1555 0, /* properties_provided */
1556 0, /* properties_destroyed */
1557 0, /* todo_flags_start */
1559 TODO_df_finish
, /* todo_flags_finish */