libgomp: Use pthread mutexes in the nvptx plugin.
[official-gcc.git] / gcc / auto-inc-dec.c
blob50258d5b8f8a7bf3ab7e12b90c9c41766e3dee68
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "rtl.h"
36 #include "tm_p.h"
37 #include "hard-reg-set.h"
38 #include "predict.h"
39 #include "vec.h"
40 #include "hashtab.h"
41 #include "hash-set.h"
42 #include "machmode.h"
43 #include "input.h"
44 #include "function.h"
45 #include "dominance.h"
46 #include "cfg.h"
47 #include "cfgrtl.h"
48 #include "basic-block.h"
49 #include "insn-config.h"
50 #include "regs.h"
51 #include "flags.h"
52 #include "except.h"
53 #include "diagnostic-core.h"
54 #include "recog.h"
55 #include "expr.h"
56 #include "tree-pass.h"
57 #include "df.h"
58 #include "dbgcnt.h"
59 #include "target.h"
61 /* This pass was originally removed from flow.c. However there is
62 almost nothing that remains of that code.
64 There are (4) basic forms that are matched:
66 (1) FORM_PRE_ADD
67 a <- b + c
68 ...
71 becomes
73 a <- b
74 ...
75 *(a += c) pre
78 (2) FORM_PRE_INC
79 a += c
80 ...
83 becomes
85 *(a += c) pre
88 (3) FORM_POST_ADD
90 ...
91 b <- a + c
93 (For this case to be true, b must not be assigned or used between
94 the *a and the assignment to b. B must also be a Pmode reg.)
96 becomes
98 b <- a
99 ...
100 *(b += c) post
103 (4) FORM_POST_INC
106 a <- a + c
108 becomes
110 *(a += c) post
112 There are three types of values of c.
114 1) c is a constant equal to the width of the value being accessed by
115 the pointer. This is useful for machines that have
116 HAVE_PRE_INCREMENT, HAVE_POST_INCREMENT, HAVE_PRE_DECREMENT or
117 HAVE_POST_DECREMENT defined.
119 2) c is a constant not equal to the width of the value being accessed
120 by the pointer. This is useful for machines that have
121 HAVE_PRE_MODIFY_DISP, HAVE_POST_MODIFY_DISP defined.
123 3) c is a register. This is useful for machines that have
124 HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
126 The is one special case: if a already had an offset equal to it +-
127 its width and that offset is equal to -c when the increment was
128 before the ref or +c if the increment was after the ref, then if we
129 can do the combination but switch the pre/post bit. */
131 #ifdef AUTO_INC_DEC
133 enum form
135 FORM_PRE_ADD,
136 FORM_PRE_INC,
137 FORM_POST_ADD,
138 FORM_POST_INC,
139 FORM_last
142 /* The states of the second operands of mem refs and inc insns. If no
143 second operand of the mem_ref was found, it is assumed to just be
144 ZERO. SIZE is the size of the mode accessed in the memref. The
145 ANY is used for constants that are not +-size or 0. REG is used if
146 the forms are reg1 + reg2. */
148 enum inc_state
150 INC_ZERO, /* == 0 */
151 INC_NEG_SIZE, /* == +size */
152 INC_POS_SIZE, /* == -size */
153 INC_NEG_ANY, /* == some -constant */
154 INC_POS_ANY, /* == some +constant */
155 INC_REG, /* == some register */
156 INC_last
159 /* The eight forms that pre/post inc/dec can take. */
160 enum gen_form
162 NOTHING,
163 SIMPLE_PRE_INC, /* ++size */
164 SIMPLE_POST_INC, /* size++ */
165 SIMPLE_PRE_DEC, /* --size */
166 SIMPLE_POST_DEC, /* size-- */
167 DISP_PRE, /* ++con */
168 DISP_POST, /* con++ */
169 REG_PRE, /* ++reg */
170 REG_POST /* reg++ */
173 /* Tmp mem rtx for use in cost modeling. */
174 static rtx mem_tmp;
176 static enum inc_state
177 set_inc_state (HOST_WIDE_INT val, int size)
179 if (val == 0)
180 return INC_ZERO;
181 if (val < 0)
182 return (val == -size) ? INC_NEG_SIZE : INC_NEG_ANY;
183 else
184 return (val == size) ? INC_POS_SIZE : INC_POS_ANY;
187 /* The DECISION_TABLE that describes what form, if any, the increment
188 or decrement will take. It is a three dimensional table. The first
189 index is the type of constant or register found as the second
190 operand of the inc insn. The second index is the type of constant
191 or register found as the second operand of the memory reference (if
192 no second operand exists, 0 is used). The third index is the form
193 and location (relative to the mem reference) of inc insn. */
195 static bool initialized = false;
196 static enum gen_form decision_table[INC_last][INC_last][FORM_last];
198 static void
199 init_decision_table (void)
201 enum gen_form value;
203 if (HAVE_PRE_INCREMENT || HAVE_PRE_MODIFY_DISP)
205 /* Prefer the simple form if both are available. */
206 value = (HAVE_PRE_INCREMENT) ? SIMPLE_PRE_INC : DISP_PRE;
208 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
209 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_INC] = value;
211 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_ADD] = value;
212 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_INC] = value;
215 if (HAVE_POST_INCREMENT || HAVE_POST_MODIFY_DISP)
217 /* Prefer the simple form if both are available. */
218 value = (HAVE_POST_INCREMENT) ? SIMPLE_POST_INC : DISP_POST;
220 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_ADD] = value;
221 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_INC] = value;
223 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_ADD] = value;
224 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_INC] = value;
227 if (HAVE_PRE_DECREMENT || HAVE_PRE_MODIFY_DISP)
229 /* Prefer the simple form if both are available. */
230 value = (HAVE_PRE_DECREMENT) ? SIMPLE_PRE_DEC : DISP_PRE;
232 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
233 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_INC] = value;
235 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_ADD] = value;
236 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_INC] = value;
239 if (HAVE_POST_DECREMENT || HAVE_POST_MODIFY_DISP)
241 /* Prefer the simple form if both are available. */
242 value = (HAVE_POST_DECREMENT) ? SIMPLE_POST_DEC : DISP_POST;
244 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_ADD] = value;
245 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_INC] = value;
247 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_ADD] = value;
248 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_INC] = value;
251 if (HAVE_PRE_MODIFY_DISP)
253 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
254 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
256 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_ADD] = DISP_PRE;
257 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_INC] = DISP_PRE;
259 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
260 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
262 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_ADD] = DISP_PRE;
263 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_INC] = DISP_PRE;
266 if (HAVE_POST_MODIFY_DISP)
268 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
269 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
271 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_ADD] = DISP_POST;
272 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_INC] = DISP_POST;
274 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
275 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
277 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_ADD] = DISP_POST;
278 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_INC] = DISP_POST;
281 /* This is much simpler than the other cases because we do not look
282 for the reg1-reg2 case. Note that we do not have a INC_POS_REG
283 and INC_NEG_REG states. Most of the use of such states would be
284 on a target that had an R1 - R2 update address form.
286 There is the remote possibility that you could also catch a = a +
287 b; *(a - b) as a postdecrement of (a + b). However, it is
288 unclear if *(a - b) would ever be generated on a machine that did
289 not have that kind of addressing mode. The IA-64 and RS6000 will
290 not do this, and I cannot speak for any other. If any
291 architecture does have an a-b update for, these cases should be
292 added. */
293 if (HAVE_PRE_MODIFY_REG)
295 decision_table[INC_REG][INC_ZERO][FORM_PRE_ADD] = REG_PRE;
296 decision_table[INC_REG][INC_ZERO][FORM_PRE_INC] = REG_PRE;
298 decision_table[INC_REG][INC_REG][FORM_POST_ADD] = REG_PRE;
299 decision_table[INC_REG][INC_REG][FORM_POST_INC] = REG_PRE;
302 if (HAVE_POST_MODIFY_REG)
304 decision_table[INC_REG][INC_ZERO][FORM_POST_ADD] = REG_POST;
305 decision_table[INC_REG][INC_ZERO][FORM_POST_INC] = REG_POST;
308 initialized = true;
311 /* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
312 "reg_res = reg0+c". */
314 static struct inc_insn
316 rtx_insn *insn; /* The insn being parsed. */
317 rtx pat; /* The pattern of the insn. */
318 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
319 enum form form;
320 rtx reg_res;
321 rtx reg0;
322 rtx reg1;
323 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
324 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
325 } inc_insn;
328 /* Dump the parsed inc insn to FILE. */
330 static void
331 dump_inc_insn (FILE *file)
333 const char *f = ((inc_insn.form == FORM_PRE_ADD)
334 || (inc_insn.form == FORM_PRE_INC)) ? "pre" : "post";
336 dump_insn_slim (file, inc_insn.insn);
338 switch (inc_insn.form)
340 case FORM_PRE_ADD:
341 case FORM_POST_ADD:
342 if (inc_insn.reg1_is_const)
343 fprintf (file, "found %s add(%d) r[%d]=r[%d]+%d\n",
344 f, INSN_UID (inc_insn.insn),
345 REGNO (inc_insn.reg_res),
346 REGNO (inc_insn.reg0), (int) inc_insn.reg1_val);
347 else
348 fprintf (file, "found %s add(%d) r[%d]=r[%d]+r[%d]\n",
349 f, INSN_UID (inc_insn.insn),
350 REGNO (inc_insn.reg_res),
351 REGNO (inc_insn.reg0), REGNO (inc_insn.reg1));
352 break;
354 case FORM_PRE_INC:
355 case FORM_POST_INC:
356 if (inc_insn.reg1_is_const)
357 fprintf (file, "found %s inc(%d) r[%d]+=%d\n",
358 f, INSN_UID (inc_insn.insn),
359 REGNO (inc_insn.reg_res), (int) inc_insn.reg1_val);
360 else
361 fprintf (file, "found %s inc(%d) r[%d]+=r[%d]\n",
362 f, INSN_UID (inc_insn.insn),
363 REGNO (inc_insn.reg_res), REGNO (inc_insn.reg1));
364 break;
366 default:
367 break;
372 /* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
374 static struct mem_insn
376 rtx_insn *insn; /* The insn being parsed. */
377 rtx pat; /* The pattern of the insn. */
378 rtx *mem_loc; /* The address of the field that holds the mem */
379 /* that is to be replaced. */
380 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
381 rtx reg0;
382 rtx reg1; /* This is either a reg or a const depending on
383 reg1_is_const. */
384 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
385 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
386 } mem_insn;
389 /* Dump the parsed mem insn to FILE. */
391 static void
392 dump_mem_insn (FILE *file)
394 dump_insn_slim (file, mem_insn.insn);
396 if (mem_insn.reg1_is_const)
397 fprintf (file, "found mem(%d) *(r[%d]+%d)\n",
398 INSN_UID (mem_insn.insn),
399 REGNO (mem_insn.reg0), (int) mem_insn.reg1_val);
400 else
401 fprintf (file, "found mem(%d) *(r[%d]+r[%d])\n",
402 INSN_UID (mem_insn.insn),
403 REGNO (mem_insn.reg0), REGNO (mem_insn.reg1));
407 /* The following three arrays contain pointers to instructions. They
408 are indexed by REGNO. At any point in the basic block where we are
409 looking these three arrays contain, respectively, the next insn
410 that uses REGNO, the next inc or add insn that uses REGNO and the
411 next insn that sets REGNO.
413 The arrays are not cleared when we move from block to block so
414 whenever an insn is retrieved from these arrays, it's block number
415 must be compared with the current block.
418 static rtx_insn **reg_next_use = NULL;
419 static rtx_insn **reg_next_inc_use = NULL;
420 static rtx_insn **reg_next_def = NULL;
423 /* Move dead note that match PATTERN to TO_INSN from FROM_INSN. We do
424 not really care about moving any other notes from the inc or add
425 insn. Moving the REG_EQUAL and REG_EQUIV is clearly wrong and it
426 does not appear that there are any other kinds of relevant notes. */
428 static void
429 move_dead_notes (rtx_insn *to_insn, rtx_insn *from_insn, rtx pattern)
431 rtx note;
432 rtx next_note;
433 rtx prev_note = NULL;
435 for (note = REG_NOTES (from_insn); note; note = next_note)
437 next_note = XEXP (note, 1);
439 if ((REG_NOTE_KIND (note) == REG_DEAD)
440 && pattern == XEXP (note, 0))
442 XEXP (note, 1) = REG_NOTES (to_insn);
443 REG_NOTES (to_insn) = note;
444 if (prev_note)
445 XEXP (prev_note, 1) = next_note;
446 else
447 REG_NOTES (from_insn) = next_note;
449 else prev_note = note;
454 /* Create a mov insn DEST_REG <- SRC_REG and insert it before
455 NEXT_INSN. */
457 static rtx_insn *
458 insert_move_insn_before (rtx_insn *next_insn, rtx dest_reg, rtx src_reg)
460 rtx_insn *insns;
462 start_sequence ();
463 emit_move_insn (dest_reg, src_reg);
464 insns = get_insns ();
465 end_sequence ();
466 emit_insn_before (insns, next_insn);
467 return insns;
471 /* Change mem_insn.mem_loc so that uses NEW_ADDR which has an
472 increment of INC_REG. To have reached this point, the change is a
473 legitimate one from a dataflow point of view. The only questions
474 are is this a valid change to the instruction and is this a
475 profitable change to the instruction. */
477 static bool
478 attempt_change (rtx new_addr, rtx inc_reg)
480 /* There are four cases: For the two cases that involve an add
481 instruction, we are going to have to delete the add and insert a
482 mov. We are going to assume that the mov is free. This is
483 fairly early in the backend and there are a lot of opportunities
484 for removing that move later. In particular, there is the case
485 where the move may be dead, this is what dead code elimination
486 passes are for. The two cases where we have an inc insn will be
487 handled mov free. */
489 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
490 rtx_insn *mov_insn = NULL;
491 int regno;
492 rtx mem = *mem_insn.mem_loc;
493 machine_mode mode = GET_MODE (mem);
494 rtx new_mem;
495 int old_cost = 0;
496 int new_cost = 0;
497 bool speed = optimize_bb_for_speed_p (bb);
499 PUT_MODE (mem_tmp, mode);
500 XEXP (mem_tmp, 0) = new_addr;
502 old_cost = (set_src_cost (mem, speed)
503 + set_rtx_cost (PATTERN (inc_insn.insn), speed));
504 new_cost = set_src_cost (mem_tmp, speed);
506 /* The first item of business is to see if this is profitable. */
507 if (old_cost < new_cost)
509 if (dump_file)
510 fprintf (dump_file, "cost failure old=%d new=%d\n", old_cost, new_cost);
511 return false;
514 /* Jump through a lot of hoops to keep the attributes up to date. We
515 do not want to call one of the change address variants that take
516 an offset even though we know the offset in many cases. These
517 assume you are changing where the address is pointing by the
518 offset. */
519 new_mem = replace_equiv_address_nv (mem, new_addr);
520 if (! validate_change (mem_insn.insn, mem_insn.mem_loc, new_mem, 0))
522 if (dump_file)
523 fprintf (dump_file, "validation failure\n");
524 return false;
527 /* From here to the end of the function we are committed to the
528 change, i.e. nothing fails. Generate any necessary movs, move
529 any regnotes, and fix up the reg_next_{use,inc_use,def}. */
530 switch (inc_insn.form)
532 case FORM_PRE_ADD:
533 /* Replace the addition with a move. Do it at the location of
534 the addition since the operand of the addition may change
535 before the memory reference. */
536 mov_insn = insert_move_insn_before (inc_insn.insn,
537 inc_insn.reg_res, inc_insn.reg0);
538 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
540 regno = REGNO (inc_insn.reg_res);
541 reg_next_def[regno] = mov_insn;
542 reg_next_use[regno] = NULL;
543 regno = REGNO (inc_insn.reg0);
544 reg_next_use[regno] = mov_insn;
545 df_recompute_luids (bb);
546 break;
548 case FORM_POST_INC:
549 regno = REGNO (inc_insn.reg_res);
550 if (reg_next_use[regno] == reg_next_inc_use[regno])
551 reg_next_inc_use[regno] = NULL;
553 /* Fallthru. */
554 case FORM_PRE_INC:
555 regno = REGNO (inc_insn.reg_res);
556 reg_next_def[regno] = mem_insn.insn;
557 reg_next_use[regno] = NULL;
559 break;
561 case FORM_POST_ADD:
562 mov_insn = insert_move_insn_before (mem_insn.insn,
563 inc_insn.reg_res, inc_insn.reg0);
564 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
566 /* Do not move anything to the mov insn because the instruction
567 pointer for the main iteration has not yet hit that. It is
568 still pointing to the mem insn. */
569 regno = REGNO (inc_insn.reg_res);
570 reg_next_def[regno] = mem_insn.insn;
571 reg_next_use[regno] = NULL;
573 regno = REGNO (inc_insn.reg0);
574 reg_next_use[regno] = mem_insn.insn;
575 if ((reg_next_use[regno] == reg_next_inc_use[regno])
576 || (reg_next_inc_use[regno] == inc_insn.insn))
577 reg_next_inc_use[regno] = NULL;
578 df_recompute_luids (bb);
579 break;
581 case FORM_last:
582 default:
583 gcc_unreachable ();
586 if (!inc_insn.reg1_is_const)
588 regno = REGNO (inc_insn.reg1);
589 reg_next_use[regno] = mem_insn.insn;
590 if ((reg_next_use[regno] == reg_next_inc_use[regno])
591 || (reg_next_inc_use[regno] == inc_insn.insn))
592 reg_next_inc_use[regno] = NULL;
595 delete_insn (inc_insn.insn);
597 if (dump_file && mov_insn)
599 fprintf (dump_file, "inserting mov ");
600 dump_insn_slim (dump_file, mov_insn);
603 /* Record that this insn has an implicit side effect. */
604 add_reg_note (mem_insn.insn, REG_INC, inc_reg);
606 if (dump_file)
608 fprintf (dump_file, "****success ");
609 dump_insn_slim (dump_file, mem_insn.insn);
612 return true;
616 /* Try to combine the instruction in INC_INSN with the instruction in
617 MEM_INSN. First the form is determined using the DECISION_TABLE
618 and the results of parsing the INC_INSN and the MEM_INSN.
619 Assuming the form is ok, a prototype new address is built which is
620 passed to ATTEMPT_CHANGE for final processing. */
622 static bool
623 try_merge (void)
625 enum gen_form gen_form;
626 rtx mem = *mem_insn.mem_loc;
627 rtx inc_reg = inc_insn.form == FORM_POST_ADD ?
628 inc_insn.reg_res : mem_insn.reg0;
630 /* The width of the mem being accessed. */
631 int size = GET_MODE_SIZE (GET_MODE (mem));
632 rtx_insn *last_insn = NULL;
633 machine_mode reg_mode = GET_MODE (inc_reg);
635 switch (inc_insn.form)
637 case FORM_PRE_ADD:
638 case FORM_PRE_INC:
639 last_insn = mem_insn.insn;
640 break;
641 case FORM_POST_INC:
642 case FORM_POST_ADD:
643 last_insn = inc_insn.insn;
644 break;
645 case FORM_last:
646 default:
647 gcc_unreachable ();
650 /* Cannot handle auto inc of the stack. */
651 if (inc_reg == stack_pointer_rtx)
653 if (dump_file)
654 fprintf (dump_file, "cannot inc stack %d failure\n", REGNO (inc_reg));
655 return false;
658 /* Look to see if the inc register is dead after the memory
659 reference. If it is, do not do the combination. */
660 if (find_regno_note (last_insn, REG_DEAD, REGNO (inc_reg)))
662 if (dump_file)
663 fprintf (dump_file, "dead failure %d\n", REGNO (inc_reg));
664 return false;
667 mem_insn.reg1_state = (mem_insn.reg1_is_const)
668 ? set_inc_state (mem_insn.reg1_val, size) : INC_REG;
669 inc_insn.reg1_state = (inc_insn.reg1_is_const)
670 ? set_inc_state (inc_insn.reg1_val, size) : INC_REG;
672 /* Now get the form that we are generating. */
673 gen_form = decision_table
674 [inc_insn.reg1_state][mem_insn.reg1_state][inc_insn.form];
676 if (dbg_cnt (auto_inc_dec) == false)
677 return false;
679 switch (gen_form)
681 default:
682 case NOTHING:
683 return false;
685 case SIMPLE_PRE_INC: /* ++size */
686 if (dump_file)
687 fprintf (dump_file, "trying SIMPLE_PRE_INC\n");
688 return attempt_change (gen_rtx_PRE_INC (reg_mode, inc_reg), inc_reg);
689 break;
691 case SIMPLE_POST_INC: /* size++ */
692 if (dump_file)
693 fprintf (dump_file, "trying SIMPLE_POST_INC\n");
694 return attempt_change (gen_rtx_POST_INC (reg_mode, inc_reg), inc_reg);
695 break;
697 case SIMPLE_PRE_DEC: /* --size */
698 if (dump_file)
699 fprintf (dump_file, "trying SIMPLE_PRE_DEC\n");
700 return attempt_change (gen_rtx_PRE_DEC (reg_mode, inc_reg), inc_reg);
701 break;
703 case SIMPLE_POST_DEC: /* size-- */
704 if (dump_file)
705 fprintf (dump_file, "trying SIMPLE_POST_DEC\n");
706 return attempt_change (gen_rtx_POST_DEC (reg_mode, inc_reg), inc_reg);
707 break;
709 case DISP_PRE: /* ++con */
710 if (dump_file)
711 fprintf (dump_file, "trying DISP_PRE\n");
712 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
713 inc_reg,
714 gen_rtx_PLUS (reg_mode,
715 inc_reg,
716 inc_insn.reg1)),
717 inc_reg);
718 break;
720 case DISP_POST: /* con++ */
721 if (dump_file)
722 fprintf (dump_file, "trying POST_DISP\n");
723 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
724 inc_reg,
725 gen_rtx_PLUS (reg_mode,
726 inc_reg,
727 inc_insn.reg1)),
728 inc_reg);
729 break;
731 case REG_PRE: /* ++reg */
732 if (dump_file)
733 fprintf (dump_file, "trying PRE_REG\n");
734 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
735 inc_reg,
736 gen_rtx_PLUS (reg_mode,
737 inc_reg,
738 inc_insn.reg1)),
739 inc_reg);
740 break;
742 case REG_POST: /* reg++ */
743 if (dump_file)
744 fprintf (dump_file, "trying POST_REG\n");
745 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
746 inc_reg,
747 gen_rtx_PLUS (reg_mode,
748 inc_reg,
749 inc_insn.reg1)),
750 inc_reg);
751 break;
755 /* Return the next insn that uses (if reg_next_use is passed in
756 NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
757 REGNO in BB. */
759 static rtx_insn *
760 get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
762 rtx_insn *insn = next_array[regno];
764 /* Lazy about cleaning out the next_arrays. */
765 if (insn && BLOCK_FOR_INSN (insn) != bb)
767 next_array[regno] = NULL;
768 insn = NULL;
771 return insn;
775 /* Reverse the operands in a mem insn. */
777 static void
778 reverse_mem (void)
780 rtx tmp = mem_insn.reg1;
781 mem_insn.reg1 = mem_insn.reg0;
782 mem_insn.reg0 = tmp;
786 /* Reverse the operands in a inc insn. */
788 static void
789 reverse_inc (void)
791 rtx tmp = inc_insn.reg1;
792 inc_insn.reg1 = inc_insn.reg0;
793 inc_insn.reg0 = tmp;
797 /* Return true if INSN is of a form "a = b op c" where a and b are
798 regs. op is + if c is a reg and +|- if c is a const. Fill in
799 INC_INSN with what is found.
801 This function is called in two contexts, if BEFORE_MEM is true,
802 this is called for each insn in the basic block. If BEFORE_MEM is
803 false, it is called for the instruction in the block that uses the
804 index register for some memory reference that is currently being
805 processed. */
807 static bool
808 parse_add_or_inc (rtx_insn *insn, bool before_mem)
810 rtx pat = single_set (insn);
811 if (!pat)
812 return false;
814 /* Result must be single reg. */
815 if (!REG_P (SET_DEST (pat)))
816 return false;
818 if ((GET_CODE (SET_SRC (pat)) != PLUS)
819 && (GET_CODE (SET_SRC (pat)) != MINUS))
820 return false;
822 if (!REG_P (XEXP (SET_SRC (pat), 0)))
823 return false;
825 inc_insn.insn = insn;
826 inc_insn.pat = pat;
827 inc_insn.reg_res = SET_DEST (pat);
828 inc_insn.reg0 = XEXP (SET_SRC (pat), 0);
829 if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg0))
830 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
831 else
832 inc_insn.form = before_mem ? FORM_PRE_ADD : FORM_POST_ADD;
834 if (CONST_INT_P (XEXP (SET_SRC (pat), 1)))
836 /* Process a = b + c where c is a const. */
837 inc_insn.reg1_is_const = true;
838 if (GET_CODE (SET_SRC (pat)) == PLUS)
840 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
841 inc_insn.reg1_val = INTVAL (inc_insn.reg1);
843 else
845 inc_insn.reg1_val = -INTVAL (XEXP (SET_SRC (pat), 1));
846 inc_insn.reg1 = GEN_INT (inc_insn.reg1_val);
848 return true;
850 else if ((HAVE_PRE_MODIFY_REG || HAVE_POST_MODIFY_REG)
851 && (REG_P (XEXP (SET_SRC (pat), 1)))
852 && GET_CODE (SET_SRC (pat)) == PLUS)
854 /* Process a = b + c where c is a reg. */
855 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
856 inc_insn.reg1_is_const = false;
858 if (inc_insn.form == FORM_PRE_INC
859 || inc_insn.form == FORM_POST_INC)
860 return true;
861 else if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg1))
863 /* Reverse the two operands and turn *_ADD into *_INC since
864 a = c + a. */
865 reverse_inc ();
866 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
867 return true;
869 else
870 return true;
873 return false;
877 /* A recursive function that checks all of the mem uses in
878 ADDRESS_OF_X to see if any single one of them is compatible with
879 what has been found in inc_insn.
881 -1 is returned for success. 0 is returned if nothing was found and
882 1 is returned for failure. */
884 static int
885 find_address (rtx *address_of_x)
887 rtx x = *address_of_x;
888 enum rtx_code code = GET_CODE (x);
889 const char *const fmt = GET_RTX_FORMAT (code);
890 int i;
891 int value = 0;
892 int tem;
894 if (code == MEM && rtx_equal_p (XEXP (x, 0), inc_insn.reg_res))
896 /* Match with *reg0. */
897 mem_insn.mem_loc = address_of_x;
898 mem_insn.reg0 = inc_insn.reg_res;
899 mem_insn.reg1_is_const = true;
900 mem_insn.reg1_val = 0;
901 mem_insn.reg1 = GEN_INT (0);
902 return -1;
904 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
905 && rtx_equal_p (XEXP (XEXP (x, 0), 0), inc_insn.reg_res))
907 rtx b = XEXP (XEXP (x, 0), 1);
908 mem_insn.mem_loc = address_of_x;
909 mem_insn.reg0 = inc_insn.reg_res;
910 mem_insn.reg1 = b;
911 mem_insn.reg1_is_const = inc_insn.reg1_is_const;
912 if (CONST_INT_P (b))
914 /* Match with *(reg0 + reg1) where reg1 is a const. */
915 HOST_WIDE_INT val = INTVAL (b);
916 if (inc_insn.reg1_is_const
917 && (inc_insn.reg1_val == val || inc_insn.reg1_val == -val))
919 mem_insn.reg1_val = val;
920 return -1;
923 else if (!inc_insn.reg1_is_const
924 && rtx_equal_p (inc_insn.reg1, b))
925 /* Match with *(reg0 + reg1). */
926 return -1;
929 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
931 /* If REG occurs inside a MEM used in a bit-field reference,
932 that is unacceptable. */
933 if (find_address (&XEXP (x, 0)))
934 return 1;
937 if (x == inc_insn.reg_res)
938 return 1;
940 /* Time for some deep diving. */
941 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
943 if (fmt[i] == 'e')
945 tem = find_address (&XEXP (x, i));
946 /* If this is the first use, let it go so the rest of the
947 insn can be checked. */
948 if (value == 0)
949 value = tem;
950 else if (tem != 0)
951 /* More than one match was found. */
952 return 1;
954 else if (fmt[i] == 'E')
956 int j;
957 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
959 tem = find_address (&XVECEXP (x, i, j));
960 /* If this is the first use, let it go so the rest of
961 the insn can be checked. */
962 if (value == 0)
963 value = tem;
964 else if (tem != 0)
965 /* More than one match was found. */
966 return 1;
970 return value;
973 /* Once a suitable mem reference has been found and the MEM_INSN
974 structure has been filled in, FIND_INC is called to see if there is
975 a suitable add or inc insn that follows the mem reference and
976 determine if it is suitable to merge.
978 In the case where the MEM_INSN has two registers in the reference,
979 this function may be called recursively. The first time looking
980 for an add of the first register, and if that fails, looking for an
981 add of the second register. The FIRST_TRY parameter is used to
982 only allow the parameters to be reversed once. */
984 static bool
985 find_inc (bool first_try)
987 rtx_insn *insn;
988 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
989 rtx_insn *other_insn;
990 df_ref def;
992 /* Make sure this reg appears only once in this insn. */
993 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
995 if (dump_file)
996 fprintf (dump_file, "mem count failure\n");
997 return false;
1000 if (dump_file)
1001 dump_mem_insn (dump_file);
1003 /* Find the next use that is an inc. */
1004 insn = get_next_ref (REGNO (mem_insn.reg0),
1005 BLOCK_FOR_INSN (mem_insn.insn),
1006 reg_next_inc_use);
1007 if (!insn)
1008 return false;
1010 /* Even though we know the next use is an add or inc because it came
1011 from the reg_next_inc_use, we must still reparse. */
1012 if (!parse_add_or_inc (insn, false))
1014 /* Next use was not an add. Look for one extra case. It could be
1015 that we have:
1017 *(a + b)
1018 ...= a;
1019 ...= b + a
1021 if we reverse the operands in the mem ref we would
1022 find this. Only try it once though. */
1023 if (first_try && !mem_insn.reg1_is_const)
1025 reverse_mem ();
1026 return find_inc (false);
1028 else
1029 return false;
1032 /* Need to assure that none of the operands of the inc instruction are
1033 assigned to by the mem insn. */
1034 FOR_EACH_INSN_DEF (def, mem_insn.insn)
1036 unsigned int regno = DF_REF_REGNO (def);
1037 if ((regno == REGNO (inc_insn.reg0))
1038 || (regno == REGNO (inc_insn.reg_res)))
1040 if (dump_file)
1041 fprintf (dump_file, "inc conflicts with store failure.\n");
1042 return false;
1044 if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
1046 if (dump_file)
1047 fprintf (dump_file, "inc conflicts with store failure.\n");
1048 return false;
1052 if (dump_file)
1053 dump_inc_insn (dump_file);
1055 if (inc_insn.form == FORM_POST_ADD)
1057 /* Make sure that there is no insn that assigns to inc_insn.res
1058 between the mem_insn and the inc_insn. */
1059 rtx_insn *other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1060 BLOCK_FOR_INSN (mem_insn.insn),
1061 reg_next_def);
1062 if (other_insn != inc_insn.insn)
1064 if (dump_file)
1065 fprintf (dump_file,
1066 "result of add is assigned to between mem and inc insns.\n");
1067 return false;
1070 other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1071 BLOCK_FOR_INSN (mem_insn.insn),
1072 reg_next_use);
1073 if (other_insn
1074 && (other_insn != inc_insn.insn)
1075 && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
1077 if (dump_file)
1078 fprintf (dump_file,
1079 "result of add is used between mem and inc insns.\n");
1080 return false;
1083 /* For the post_add to work, the result_reg of the inc must not be
1084 used in the mem insn since this will become the new index
1085 register. */
1086 if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
1088 if (dump_file)
1089 fprintf (dump_file, "base reg replacement failure.\n");
1090 return false;
1094 if (mem_insn.reg1_is_const)
1096 if (mem_insn.reg1_val == 0)
1098 if (!inc_insn.reg1_is_const)
1100 /* The mem looks like *r0 and the rhs of the add has two
1101 registers. */
1102 int luid = DF_INSN_LUID (inc_insn.insn);
1103 if (inc_insn.form == FORM_POST_ADD)
1105 /* The trick is that we are not going to increment r0,
1106 we are going to increment the result of the add insn.
1107 For this trick to be correct, the result reg of
1108 the inc must be a valid addressing reg. */
1109 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1110 if (GET_MODE (inc_insn.reg_res)
1111 != targetm.addr_space.address_mode (as))
1113 if (dump_file)
1114 fprintf (dump_file, "base reg mode failure.\n");
1115 return false;
1118 /* We also need to make sure that the next use of
1119 inc result is after the inc. */
1120 other_insn
1121 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1122 if (other_insn && luid > DF_INSN_LUID (other_insn))
1123 return false;
1125 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1126 reverse_inc ();
1129 other_insn
1130 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1131 if (other_insn && luid > DF_INSN_LUID (other_insn))
1132 return false;
1135 /* Both the inc/add and the mem have a constant. Need to check
1136 that the constants are ok. */
1137 else if ((mem_insn.reg1_val != inc_insn.reg1_val)
1138 && (mem_insn.reg1_val != -inc_insn.reg1_val))
1139 return false;
1141 else
1143 /* The mem insn is of the form *(a + b) where a and b are both
1144 regs. It may be that in order to match the add or inc we
1145 need to treat it as if it was *(b + a). It may also be that
1146 the add is of the form a + c where c does not match b and
1147 then we just abandon this. */
1149 int luid = DF_INSN_LUID (inc_insn.insn);
1150 rtx_insn *other_insn;
1152 /* Make sure this reg appears only once in this insn. */
1153 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
1154 return false;
1156 if (inc_insn.form == FORM_POST_ADD)
1158 /* For this trick to be correct, the result reg of the inc
1159 must be a valid addressing reg. */
1160 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1161 if (GET_MODE (inc_insn.reg_res)
1162 != targetm.addr_space.address_mode (as))
1164 if (dump_file)
1165 fprintf (dump_file, "base reg mode failure.\n");
1166 return false;
1169 if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1171 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1173 /* See comment above on find_inc (false) call. */
1174 if (first_try)
1176 reverse_mem ();
1177 return find_inc (false);
1179 else
1180 return false;
1183 /* Need to check that there are no assignments to b
1184 before the add insn. */
1185 other_insn
1186 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1187 if (other_insn && luid > DF_INSN_LUID (other_insn))
1188 return false;
1189 /* All ok for the next step. */
1191 else
1193 /* We know that mem_insn.reg0 must equal inc_insn.reg1
1194 or else we would not have found the inc insn. */
1195 reverse_mem ();
1196 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1198 /* See comment above on find_inc (false) call. */
1199 if (first_try)
1200 return find_inc (false);
1201 else
1202 return false;
1204 /* To have gotten here know that.
1205 *(b + a)
1207 ... = (b + a)
1209 We also know that the lhs of the inc is not b or a. We
1210 need to make sure that there are no assignments to b
1211 between the mem ref and the inc. */
1213 other_insn
1214 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
1215 if (other_insn && luid > DF_INSN_LUID (other_insn))
1216 return false;
1219 /* Need to check that the next use of the add result is later than
1220 add insn since this will be the reg incremented. */
1221 other_insn
1222 = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
1223 if (other_insn && luid > DF_INSN_LUID (other_insn))
1224 return false;
1226 else /* FORM_POST_INC. There is less to check here because we
1227 know that operands must line up. */
1229 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1230 /* See comment above on find_inc (false) call. */
1232 if (first_try)
1234 reverse_mem ();
1235 return find_inc (false);
1237 else
1238 return false;
1241 /* To have gotten here know that.
1242 *(a + b)
1244 ... = (a + b)
1246 We also know that the lhs of the inc is not b. We need to make
1247 sure that there are no assignments to b between the mem ref and
1248 the inc. */
1249 other_insn
1250 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1251 if (other_insn && luid > DF_INSN_LUID (other_insn))
1252 return false;
1256 if (inc_insn.form == FORM_POST_INC)
1258 other_insn
1259 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_use);
1260 /* When we found inc_insn, we were looking for the
1261 next add or inc, not the next insn that used the
1262 reg. Because we are going to increment the reg
1263 in this form, we need to make sure that there
1264 were no intervening uses of reg. */
1265 if (inc_insn.insn != other_insn)
1266 return false;
1269 return try_merge ();
1273 /* A recursive function that walks ADDRESS_OF_X to find all of the mem
1274 uses in pat that could be used as an auto inc or dec. It then
1275 calls FIND_INC for each one. */
1277 static bool
1278 find_mem (rtx *address_of_x)
1280 rtx x = *address_of_x;
1281 enum rtx_code code = GET_CODE (x);
1282 const char *const fmt = GET_RTX_FORMAT (code);
1283 int i;
1285 if (code == MEM && REG_P (XEXP (x, 0)))
1287 /* Match with *reg0. */
1288 mem_insn.mem_loc = address_of_x;
1289 mem_insn.reg0 = XEXP (x, 0);
1290 mem_insn.reg1_is_const = true;
1291 mem_insn.reg1_val = 0;
1292 mem_insn.reg1 = GEN_INT (0);
1293 if (find_inc (true))
1294 return true;
1296 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
1297 && REG_P (XEXP (XEXP (x, 0), 0)))
1299 rtx reg1 = XEXP (XEXP (x, 0), 1);
1300 mem_insn.mem_loc = address_of_x;
1301 mem_insn.reg0 = XEXP (XEXP (x, 0), 0);
1302 mem_insn.reg1 = reg1;
1303 if (CONST_INT_P (reg1))
1305 mem_insn.reg1_is_const = true;
1306 /* Match with *(reg0 + c) where c is a const. */
1307 mem_insn.reg1_val = INTVAL (reg1);
1308 if (find_inc (true))
1309 return true;
1311 else if (REG_P (reg1))
1313 /* Match with *(reg0 + reg1). */
1314 mem_insn.reg1_is_const = false;
1315 if (find_inc (true))
1316 return true;
1320 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
1322 /* If REG occurs inside a MEM used in a bit-field reference,
1323 that is unacceptable. */
1324 return false;
1327 /* Time for some deep diving. */
1328 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1330 if (fmt[i] == 'e')
1332 if (find_mem (&XEXP (x, i)))
1333 return true;
1335 else if (fmt[i] == 'E')
1337 int j;
1338 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1339 if (find_mem (&XVECEXP (x, i, j)))
1340 return true;
1343 return false;
1347 /* Try to combine all incs and decs by constant values with memory
1348 references in BB. */
1350 static void
1351 merge_in_block (int max_reg, basic_block bb)
1353 rtx_insn *insn;
1354 rtx_insn *curr;
1355 int success_in_block = 0;
1357 if (dump_file)
1358 fprintf (dump_file, "\n\nstarting bb %d\n", bb->index);
1360 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, curr)
1362 bool insn_is_add_or_inc = true;
1364 if (!NONDEBUG_INSN_P (insn))
1365 continue;
1367 /* This continue is deliberate. We do not want the uses of the
1368 jump put into reg_next_use because it is not considered safe to
1369 combine a preincrement with a jump. */
1370 if (JUMP_P (insn))
1371 continue;
1373 if (dump_file)
1374 dump_insn_slim (dump_file, insn);
1376 /* Does this instruction increment or decrement a register? */
1377 if (parse_add_or_inc (insn, true))
1379 int regno = REGNO (inc_insn.reg_res);
1380 /* Cannot handle case where there are three separate regs
1381 before a mem ref. Too many moves would be needed to be
1382 profitable. */
1383 if ((inc_insn.form == FORM_PRE_INC) || inc_insn.reg1_is_const)
1385 mem_insn.insn = get_next_ref (regno, bb, reg_next_use);
1386 if (mem_insn.insn)
1388 bool ok = true;
1389 if (!inc_insn.reg1_is_const)
1391 /* We are only here if we are going to try a
1392 HAVE_*_MODIFY_REG type transformation. c is a
1393 reg and we must sure that the path from the
1394 inc_insn to the mem_insn.insn is both def and use
1395 clear of c because the inc insn is going to move
1396 into the mem_insn.insn. */
1397 int luid = DF_INSN_LUID (mem_insn.insn);
1398 rtx_insn *other_insn
1399 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1401 if (other_insn && luid > DF_INSN_LUID (other_insn))
1402 ok = false;
1404 other_insn
1405 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1407 if (other_insn && luid > DF_INSN_LUID (other_insn))
1408 ok = false;
1411 if (dump_file)
1412 dump_inc_insn (dump_file);
1414 if (ok && find_address (&PATTERN (mem_insn.insn)) == -1)
1416 if (dump_file)
1417 dump_mem_insn (dump_file);
1418 if (try_merge ())
1420 success_in_block++;
1421 insn_is_add_or_inc = false;
1427 else
1429 insn_is_add_or_inc = false;
1430 mem_insn.insn = insn;
1431 if (find_mem (&PATTERN (insn)))
1432 success_in_block++;
1435 /* If the inc insn was merged with a mem, the inc insn is gone
1436 and there is noting to update. */
1437 if (df_insn_info *insn_info = DF_INSN_INFO_GET (insn))
1439 df_ref def, use;
1441 /* Need to update next use. */
1442 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1444 reg_next_use[DF_REF_REGNO (def)] = NULL;
1445 reg_next_inc_use[DF_REF_REGNO (def)] = NULL;
1446 reg_next_def[DF_REF_REGNO (def)] = insn;
1449 FOR_EACH_INSN_INFO_USE (use, insn_info)
1451 reg_next_use[DF_REF_REGNO (use)] = insn;
1452 if (insn_is_add_or_inc)
1453 reg_next_inc_use[DF_REF_REGNO (use)] = insn;
1454 else
1455 reg_next_inc_use[DF_REF_REGNO (use)] = NULL;
1458 else if (dump_file)
1459 fprintf (dump_file, "skipping update of deleted insn %d\n",
1460 INSN_UID (insn));
1463 /* If we were successful, try again. There may have been several
1464 opportunities that were interleaved. This is rare but
1465 gcc.c-torture/compile/pr17273.c actually exhibits this. */
1466 if (success_in_block)
1468 /* In this case, we must clear these vectors since the trick of
1469 testing if the stale insn in the block will not work. */
1470 memset (reg_next_use, 0, max_reg * sizeof (rtx));
1471 memset (reg_next_inc_use, 0, max_reg * sizeof (rtx));
1472 memset (reg_next_def, 0, max_reg * sizeof (rtx));
1473 df_recompute_luids (bb);
1474 merge_in_block (max_reg, bb);
1478 #endif
1480 /* Discover auto-inc auto-dec instructions. */
1482 namespace {
1484 const pass_data pass_data_inc_dec =
1486 RTL_PASS, /* type */
1487 "auto_inc_dec", /* name */
1488 OPTGROUP_NONE, /* optinfo_flags */
1489 TV_AUTO_INC_DEC, /* tv_id */
1490 0, /* properties_required */
1491 0, /* properties_provided */
1492 0, /* properties_destroyed */
1493 0, /* todo_flags_start */
1494 TODO_df_finish, /* todo_flags_finish */
1497 class pass_inc_dec : public rtl_opt_pass
1499 public:
1500 pass_inc_dec (gcc::context *ctxt)
1501 : rtl_opt_pass (pass_data_inc_dec, ctxt)
1504 /* opt_pass methods: */
1505 virtual bool gate (function *)
1507 #ifdef AUTO_INC_DEC
1508 return (optimize > 0 && flag_auto_inc_dec);
1509 #else
1510 return false;
1511 #endif
1515 unsigned int execute (function *);
1517 }; // class pass_inc_dec
1519 unsigned int
1520 pass_inc_dec::execute (function *fun ATTRIBUTE_UNUSED)
1522 #ifdef AUTO_INC_DEC
1523 basic_block bb;
1524 int max_reg = max_reg_num ();
1526 if (!initialized)
1527 init_decision_table ();
1529 mem_tmp = gen_rtx_MEM (Pmode, NULL_RTX);
1531 df_note_add_problem ();
1532 df_analyze ();
1534 reg_next_use = XCNEWVEC (rtx_insn *, max_reg);
1535 reg_next_inc_use = XCNEWVEC (rtx_insn *, max_reg);
1536 reg_next_def = XCNEWVEC (rtx_insn *, max_reg);
1537 FOR_EACH_BB_FN (bb, fun)
1538 merge_in_block (max_reg, bb);
1540 free (reg_next_use);
1541 free (reg_next_inc_use);
1542 free (reg_next_def);
1544 mem_tmp = NULL;
1545 #endif
1546 return 0;
1549 } // anon namespace
1551 rtl_opt_pass *
1552 make_pass_inc_dec (gcc::context *ctxt)
1554 return new pass_inc_dec (ctxt);