alias.c: Remove unused headers.
[gcc.git] / gcc / auto-inc-dec.c
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>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "predict.h"
29 #include "df.h"
30 #include "insn-config.h"
31 #include "emit-rtl.h"
32 #include "recog.h"
33 #include "cfgrtl.h"
34 #include "expr.h"
35 #include "tree-pass.h"
36 #include "dbgcnt.h"
37 #include "print-rtl.h"
38
39 /* This pass was originally removed from flow.c. However there is
40 almost nothing that remains of that code.
41
42 There are (4) basic forms that are matched:
43
44 (1) FORM_PRE_ADD
45 a <- b + c
46 ...
47 *a
48
49 becomes
50
51 a <- b
52 ...
53 *(a += c) pre
54
55
56 (2) FORM_PRE_INC
57 a += c
58 ...
59 *a
60
61 becomes
62
63 *(a += c) pre
64
65
66 (3) FORM_POST_ADD
67 *a
68 ...
69 b <- a + c
70
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.)
73
74 becomes
75
76 b <- a
77 ...
78 *(b += c) post
79
80
81 (4) FORM_POST_INC
82 *a
83 ...
84 a <- a + c
85
86 becomes
87
88 *(a += c) post
89
90 There are three types of values of c.
91
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.
96
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.
100
101 3) c is a register. This is useful for machines that have
102 HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
103
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. */
108
109
110 enum form
111 {
112 FORM_PRE_ADD,
113 FORM_PRE_INC,
114 FORM_POST_ADD,
115 FORM_POST_INC,
116 FORM_last
117 };
118
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. */
124
125 enum inc_state
126 {
127 INC_ZERO, /* == 0 */
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 */
133 INC_last
134 };
135
136 /* The eight forms that pre/post inc/dec can take. */
137 enum gen_form
138 {
139 NOTHING,
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++ */
146 REG_PRE, /* ++reg */
147 REG_POST /* reg++ */
148 };
149
150 /* Tmp mem rtx for use in cost modeling. */
151 static rtx mem_tmp;
152
153 static enum inc_state
154 set_inc_state (HOST_WIDE_INT val, int size)
155 {
156 if (val == 0)
157 return INC_ZERO;
158 if (val < 0)
159 return (val == -size) ? INC_NEG_SIZE : INC_NEG_ANY;
160 else
161 return (val == size) ? INC_POS_SIZE : INC_POS_ANY;
162 }
163
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. */
171
172 static bool initialized = false;
173 static enum gen_form decision_table[INC_last][INC_last][FORM_last];
174
175 static void
176 init_decision_table (void)
177 {
178 enum gen_form value;
179
180 if (HAVE_PRE_INCREMENT || HAVE_PRE_MODIFY_DISP)
181 {
182 /* Prefer the simple form if both are available. */
183 value = (HAVE_PRE_INCREMENT) ? SIMPLE_PRE_INC : DISP_PRE;
184
185 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
186 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_INC] = value;
187
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;
190 }
191
192 if (HAVE_POST_INCREMENT || HAVE_POST_MODIFY_DISP)
193 {
194 /* Prefer the simple form if both are available. */
195 value = (HAVE_POST_INCREMENT) ? SIMPLE_POST_INC : DISP_POST;
196
197 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_ADD] = value;
198 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_INC] = value;
199
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;
202 }
203
204 if (HAVE_PRE_DECREMENT || HAVE_PRE_MODIFY_DISP)
205 {
206 /* Prefer the simple form if both are available. */
207 value = (HAVE_PRE_DECREMENT) ? SIMPLE_PRE_DEC : DISP_PRE;
208
209 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
210 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_INC] = value;
211
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;
214 }
215
216 if (HAVE_POST_DECREMENT || HAVE_POST_MODIFY_DISP)
217 {
218 /* Prefer the simple form if both are available. */
219 value = (HAVE_POST_DECREMENT) ? SIMPLE_POST_DEC : DISP_POST;
220
221 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_ADD] = value;
222 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_INC] = value;
223
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;
226 }
227
228 if (HAVE_PRE_MODIFY_DISP)
229 {
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;
232
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;
235
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;
238
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;
241 }
242
243 if (HAVE_POST_MODIFY_DISP)
244 {
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;
247
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;
250
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;
253
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;
256 }
257
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.
262
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
269 added. */
270 if (HAVE_PRE_MODIFY_REG)
271 {
272 decision_table[INC_REG][INC_ZERO][FORM_PRE_ADD] = REG_PRE;
273 decision_table[INC_REG][INC_ZERO][FORM_PRE_INC] = REG_PRE;
274
275 decision_table[INC_REG][INC_REG][FORM_POST_ADD] = REG_PRE;
276 decision_table[INC_REG][INC_REG][FORM_POST_INC] = REG_PRE;
277 }
278
279 if (HAVE_POST_MODIFY_REG)
280 {
281 decision_table[INC_REG][INC_ZERO][FORM_POST_ADD] = REG_POST;
282 decision_table[INC_REG][INC_ZERO][FORM_POST_INC] = REG_POST;
283 }
284
285 initialized = true;
286 }
287
288 /* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
289 "reg_res = reg0+c". */
290
291 static struct inc_insn
292 {
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. */
296 enum form form;
297 rtx reg_res;
298 rtx reg0;
299 rtx reg1;
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. */
302 } inc_insn;
303
304
305 /* Dump the parsed inc insn to FILE. */
306
307 static void
308 dump_inc_insn (FILE *file)
309 {
310 const char *f = ((inc_insn.form == FORM_PRE_ADD)
311 || (inc_insn.form == FORM_PRE_INC)) ? "pre" : "post";
312
313 dump_insn_slim (file, inc_insn.insn);
314
315 switch (inc_insn.form)
316 {
317 case FORM_PRE_ADD:
318 case FORM_POST_ADD:
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);
324 else
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));
329 break;
330
331 case FORM_PRE_INC:
332 case FORM_POST_INC:
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);
337 else
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));
341 break;
342
343 default:
344 break;
345 }
346 }
347
348
349 /* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
350
351 static struct mem_insn
352 {
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. */
358 rtx reg0;
359 rtx reg1; /* This is either a reg or a const depending on
360 reg1_is_const. */
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. */
363 } mem_insn;
364
365
366 /* Dump the parsed mem insn to FILE. */
367
368 static void
369 dump_mem_insn (FILE *file)
370 {
371 dump_insn_slim (file, mem_insn.insn);
372
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);
377 else
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));
381 }
382
383
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.
389
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.
393 */
394
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;
398
399
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. */
404
405 static void
406 move_dead_notes (rtx_insn *to_insn, rtx_insn *from_insn, rtx pattern)
407 {
408 rtx note;
409 rtx next_note;
410 rtx prev_note = NULL;
411
412 for (note = REG_NOTES (from_insn); note; note = next_note)
413 {
414 next_note = XEXP (note, 1);
415
416 if ((REG_NOTE_KIND (note) == REG_DEAD)
417 && pattern == XEXP (note, 0))
418 {
419 XEXP (note, 1) = REG_NOTES (to_insn);
420 REG_NOTES (to_insn) = note;
421 if (prev_note)
422 XEXP (prev_note, 1) = next_note;
423 else
424 REG_NOTES (from_insn) = next_note;
425 }
426 else prev_note = note;
427 }
428 }
429
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. */
435
436 static bool
437 attempt_change (rtx new_addr, rtx inc_reg)
438 {
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
446 handled mov free. */
447
448 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
449 rtx_insn *mov_insn = NULL;
450 int regno;
451 rtx mem = *mem_insn.mem_loc;
452 machine_mode mode = GET_MODE (mem);
453 rtx new_mem;
454 int old_cost = 0;
455 int new_cost = 0;
456 bool speed = optimize_bb_for_speed_p (bb);
457
458 PUT_MODE (mem_tmp, mode);
459 XEXP (mem_tmp, 0) = new_addr;
460
461 old_cost = (set_src_cost (mem, mode, speed)
462 + set_rtx_cost (PATTERN (inc_insn.insn), speed));
463
464 new_cost = set_src_cost (mem_tmp, mode, speed);
465
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)
470 {
471 start_sequence ();
472 emit_move_insn (inc_insn.reg_res, inc_insn.reg0);
473 mov_insn = get_insns ();
474 end_sequence ();
475 new_cost += seq_cost (mov_insn, speed);
476 }
477
478 /* The first item of business is to see if this is profitable. */
479 if (old_cost < new_cost)
480 {
481 if (dump_file)
482 fprintf (dump_file, "cost failure old=%d new=%d\n", old_cost, new_cost);
483 return false;
484 }
485
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
490 offset. */
491 new_mem = replace_equiv_address_nv (mem, new_addr);
492 if (! validate_change (mem_insn.insn, mem_insn.mem_loc, new_mem, 0))
493 {
494 if (dump_file)
495 fprintf (dump_file, "validation failure\n");
496 return false;
497 }
498
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)
503 {
504 case FORM_PRE_ADD:
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);
511
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);
518 break;
519
520 case FORM_POST_INC:
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;
524
525 /* Fallthru. */
526 case FORM_PRE_INC:
527 regno = REGNO (inc_insn.reg_res);
528 reg_next_def[regno] = mem_insn.insn;
529 reg_next_use[regno] = NULL;
530
531 break;
532
533 case FORM_POST_ADD:
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);
537
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;
544
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);
551 break;
552
553 case FORM_last:
554 default:
555 gcc_unreachable ();
556 }
557
558 if (!inc_insn.reg1_is_const)
559 {
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;
565 }
566
567 delete_insn (inc_insn.insn);
568
569 if (dump_file && mov_insn)
570 {
571 fprintf (dump_file, "inserting mov ");
572 dump_insn_slim (dump_file, mov_insn);
573 }
574
575 /* Record that this insn has an implicit side effect. */
576 add_reg_note (mem_insn.insn, REG_INC, inc_reg);
577
578 if (dump_file)
579 {
580 fprintf (dump_file, "****success ");
581 dump_insn_slim (dump_file, mem_insn.insn);
582 }
583
584 return true;
585 }
586
587
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. */
593
594 static bool
595 try_merge (void)
596 {
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;
601
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);
606
607 switch (inc_insn.form)
608 {
609 case FORM_PRE_ADD:
610 case FORM_PRE_INC:
611 last_insn = mem_insn.insn;
612 break;
613 case FORM_POST_INC:
614 case FORM_POST_ADD:
615 last_insn = inc_insn.insn;
616 break;
617 case FORM_last:
618 default:
619 gcc_unreachable ();
620 }
621
622 /* Cannot handle auto inc of the stack. */
623 if (inc_reg == stack_pointer_rtx)
624 {
625 if (dump_file)
626 fprintf (dump_file, "cannot inc stack %d failure\n", REGNO (inc_reg));
627 return false;
628 }
629
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)))
633 {
634 if (dump_file)
635 fprintf (dump_file, "dead failure %d\n", REGNO (inc_reg));
636 return false;
637 }
638
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;
643
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];
647
648 if (dbg_cnt (auto_inc_dec) == false)
649 return false;
650
651 switch (gen_form)
652 {
653 default:
654 case NOTHING:
655 return false;
656
657 case SIMPLE_PRE_INC: /* ++size */
658 if (dump_file)
659 fprintf (dump_file, "trying SIMPLE_PRE_INC\n");
660 return attempt_change (gen_rtx_PRE_INC (reg_mode, inc_reg), inc_reg);
661 break;
662
663 case SIMPLE_POST_INC: /* size++ */
664 if (dump_file)
665 fprintf (dump_file, "trying SIMPLE_POST_INC\n");
666 return attempt_change (gen_rtx_POST_INC (reg_mode, inc_reg), inc_reg);
667 break;
668
669 case SIMPLE_PRE_DEC: /* --size */
670 if (dump_file)
671 fprintf (dump_file, "trying SIMPLE_PRE_DEC\n");
672 return attempt_change (gen_rtx_PRE_DEC (reg_mode, inc_reg), inc_reg);
673 break;
674
675 case SIMPLE_POST_DEC: /* size-- */
676 if (dump_file)
677 fprintf (dump_file, "trying SIMPLE_POST_DEC\n");
678 return attempt_change (gen_rtx_POST_DEC (reg_mode, inc_reg), inc_reg);
679 break;
680
681 case DISP_PRE: /* ++con */
682 if (dump_file)
683 fprintf (dump_file, "trying DISP_PRE\n");
684 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
685 inc_reg,
686 gen_rtx_PLUS (reg_mode,
687 inc_reg,
688 inc_insn.reg1)),
689 inc_reg);
690 break;
691
692 case DISP_POST: /* con++ */
693 if (dump_file)
694 fprintf (dump_file, "trying POST_DISP\n");
695 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
696 inc_reg,
697 gen_rtx_PLUS (reg_mode,
698 inc_reg,
699 inc_insn.reg1)),
700 inc_reg);
701 break;
702
703 case REG_PRE: /* ++reg */
704 if (dump_file)
705 fprintf (dump_file, "trying PRE_REG\n");
706 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
707 inc_reg,
708 gen_rtx_PLUS (reg_mode,
709 inc_reg,
710 inc_insn.reg1)),
711 inc_reg);
712 break;
713
714 case REG_POST: /* reg++ */
715 if (dump_file)
716 fprintf (dump_file, "trying POST_REG\n");
717 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
718 inc_reg,
719 gen_rtx_PLUS (reg_mode,
720 inc_reg,
721 inc_insn.reg1)),
722 inc_reg);
723 break;
724 }
725 }
726
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)
729 REGNO in BB. */
730
731 static rtx_insn *
732 get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
733 {
734 rtx_insn *insn = next_array[regno];
735
736 /* Lazy about cleaning out the next_arrays. */
737 if (insn && BLOCK_FOR_INSN (insn) != bb)
738 {
739 next_array[regno] = NULL;
740 insn = NULL;
741 }
742
743 return insn;
744 }
745
746
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.
750
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
755 processed. */
756
757 static bool
758 parse_add_or_inc (rtx_insn *insn, bool before_mem)
759 {
760 rtx pat = single_set (insn);
761 if (!pat)
762 return false;
763
764 /* Result must be single reg. */
765 if (!REG_P (SET_DEST (pat)))
766 return false;
767
768 if ((GET_CODE (SET_SRC (pat)) != PLUS)
769 && (GET_CODE (SET_SRC (pat)) != MINUS))
770 return false;
771
772 if (!REG_P (XEXP (SET_SRC (pat), 0)))
773 return false;
774
775 inc_insn.insn = insn;
776 inc_insn.pat = pat;
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;
781 else
782 inc_insn.form = before_mem ? FORM_PRE_ADD : FORM_POST_ADD;
783
784 if (CONST_INT_P (XEXP (SET_SRC (pat), 1)))
785 {
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)
789 {
790 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
791 inc_insn.reg1_val = INTVAL (inc_insn.reg1);
792 }
793 else
794 {
795 inc_insn.reg1_val = -INTVAL (XEXP (SET_SRC (pat), 1));
796 inc_insn.reg1 = GEN_INT (inc_insn.reg1_val);
797 }
798 return true;
799 }
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)
803 {
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;
807
808 if (inc_insn.form == FORM_PRE_INC
809 || inc_insn.form == FORM_POST_INC)
810 return true;
811 else if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg1))
812 {
813 /* Reverse the two operands and turn *_ADD into *_INC since
814 a = c + a. */
815 std::swap (inc_insn.reg0, inc_insn.reg1);
816 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
817 return true;
818 }
819 else
820 return true;
821 }
822
823 return false;
824 }
825
826
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.
830
831 -1 is returned for success. 0 is returned if nothing was found and
832 1 is returned for failure. */
833
834 static int
835 find_address (rtx *address_of_x)
836 {
837 rtx x = *address_of_x;
838 enum rtx_code code = GET_CODE (x);
839 const char *const fmt = GET_RTX_FORMAT (code);
840 int i;
841 int value = 0;
842 int tem;
843
844 if (code == MEM && rtx_equal_p (XEXP (x, 0), inc_insn.reg_res))
845 {
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);
852 return -1;
853 }
854 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
855 && rtx_equal_p (XEXP (XEXP (x, 0), 0), inc_insn.reg_res))
856 {
857 rtx b = XEXP (XEXP (x, 0), 1);
858 mem_insn.mem_loc = address_of_x;
859 mem_insn.reg0 = inc_insn.reg_res;
860 mem_insn.reg1 = b;
861 mem_insn.reg1_is_const = inc_insn.reg1_is_const;
862 if (CONST_INT_P (b))
863 {
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))
868 {
869 mem_insn.reg1_val = val;
870 return -1;
871 }
872 }
873 else if (!inc_insn.reg1_is_const
874 && rtx_equal_p (inc_insn.reg1, b))
875 /* Match with *(reg0 + reg1). */
876 return -1;
877 }
878
879 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
880 {
881 /* If REG occurs inside a MEM used in a bit-field reference,
882 that is unacceptable. */
883 if (find_address (&XEXP (x, 0)))
884 return 1;
885 }
886
887 if (x == inc_insn.reg_res)
888 return 1;
889
890 /* Time for some deep diving. */
891 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
892 {
893 if (fmt[i] == 'e')
894 {
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. */
898 if (value == 0)
899 value = tem;
900 else if (tem != 0)
901 /* More than one match was found. */
902 return 1;
903 }
904 else if (fmt[i] == 'E')
905 {
906 int j;
907 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
908 {
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. */
912 if (value == 0)
913 value = tem;
914 else if (tem != 0)
915 /* More than one match was found. */
916 return 1;
917 }
918 }
919 }
920 return value;
921 }
922
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.
927
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. */
933
934 static bool
935 find_inc (bool first_try)
936 {
937 rtx_insn *insn;
938 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
939 rtx_insn *other_insn;
940 df_ref def;
941
942 /* Make sure this reg appears only once in this insn. */
943 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
944 {
945 if (dump_file)
946 fprintf (dump_file, "mem count failure\n");
947 return false;
948 }
949
950 if (dump_file)
951 dump_mem_insn (dump_file);
952
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),
956 reg_next_inc_use);
957 if (!insn)
958 return false;
959
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))
963 {
964 /* Next use was not an add. Look for one extra case. It could be
965 that we have:
966
967 *(a + b)
968 ...= a;
969 ...= b + a
970
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)
974 {
975 std::swap (mem_insn.reg0, mem_insn.reg1);
976 return find_inc (false);
977 }
978 else
979 return false;
980 }
981
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)
985 {
986 unsigned int regno = DF_REF_REGNO (def);
987 if ((regno == REGNO (inc_insn.reg0))
988 || (regno == REGNO (inc_insn.reg_res)))
989 {
990 if (dump_file)
991 fprintf (dump_file, "inc conflicts with store failure.\n");
992 return false;
993 }
994 if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
995 {
996 if (dump_file)
997 fprintf (dump_file, "inc conflicts with store failure.\n");
998 return false;
999 }
1000 }
1001
1002 if (dump_file)
1003 dump_inc_insn (dump_file);
1004
1005 if (inc_insn.form == FORM_POST_ADD)
1006 {
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),
1011 reg_next_def);
1012 if (other_insn != inc_insn.insn)
1013 {
1014 if (dump_file)
1015 fprintf (dump_file,
1016 "result of add is assigned to between mem and inc insns.\n");
1017 return false;
1018 }
1019
1020 other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1021 BLOCK_FOR_INSN (mem_insn.insn),
1022 reg_next_use);
1023 if (other_insn
1024 && (other_insn != inc_insn.insn)
1025 && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
1026 {
1027 if (dump_file)
1028 fprintf (dump_file,
1029 "result of add is used between mem and inc insns.\n");
1030 return false;
1031 }
1032
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
1035 register. */
1036 if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
1037 {
1038 if (dump_file)
1039 fprintf (dump_file, "base reg replacement failure.\n");
1040 return false;
1041 }
1042 }
1043
1044 if (mem_insn.reg1_is_const)
1045 {
1046 if (mem_insn.reg1_val == 0)
1047 {
1048 if (!inc_insn.reg1_is_const)
1049 {
1050 /* The mem looks like *r0 and the rhs of the add has two
1051 registers. */
1052 int luid = DF_INSN_LUID (inc_insn.insn);
1053 if (inc_insn.form == FORM_POST_ADD)
1054 {
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))
1062 {
1063 if (dump_file)
1064 fprintf (dump_file, "base reg mode failure.\n");
1065 return false;
1066 }
1067
1068 /* We also need to make sure that the next use of
1069 inc result is after the inc. */
1070 other_insn
1071 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1072 if (other_insn && luid > DF_INSN_LUID (other_insn))
1073 return false;
1074
1075 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1076 std::swap (inc_insn.reg0, inc_insn.reg1);
1077 }
1078
1079 other_insn
1080 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1081 if (other_insn && luid > DF_INSN_LUID (other_insn))
1082 return false;
1083 }
1084 }
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))
1089 return false;
1090 }
1091 else
1092 {
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. */
1098
1099 int luid = DF_INSN_LUID (inc_insn.insn);
1100 rtx_insn *other_insn;
1101
1102 /* Make sure this reg appears only once in this insn. */
1103 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
1104 return false;
1105
1106 if (inc_insn.form == FORM_POST_ADD)
1107 {
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))
1113 {
1114 if (dump_file)
1115 fprintf (dump_file, "base reg mode failure.\n");
1116 return false;
1117 }
1118
1119 if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1120 {
1121 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1122 {
1123 /* See comment above on find_inc (false) call. */
1124 if (first_try)
1125 {
1126 std::swap (mem_insn.reg0, mem_insn.reg1);
1127 return find_inc (false);
1128 }
1129 else
1130 return false;
1131 }
1132
1133 /* Need to check that there are no assignments to b
1134 before the add insn. */
1135 other_insn
1136 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1137 if (other_insn && luid > DF_INSN_LUID (other_insn))
1138 return false;
1139 /* All ok for the next step. */
1140 }
1141 else
1142 {
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))
1147 {
1148 /* See comment above on find_inc (false) call. */
1149 if (first_try)
1150 return find_inc (false);
1151 else
1152 return false;
1153 }
1154 /* To have gotten here know that.
1155 *(b + a)
1156
1157 ... = (b + a)
1158
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. */
1162
1163 other_insn
1164 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
1165 if (other_insn && luid > DF_INSN_LUID (other_insn))
1166 return false;
1167 }
1168
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. */
1171 other_insn
1172 = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
1173 if (other_insn && luid > DF_INSN_LUID (other_insn))
1174 return false;
1175 }
1176 else /* FORM_POST_INC. There is less to check here because we
1177 know that operands must line up. */
1178 {
1179 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1180 /* See comment above on find_inc (false) call. */
1181 {
1182 if (first_try)
1183 {
1184 std::swap (mem_insn.reg0, mem_insn.reg1);
1185 return find_inc (false);
1186 }
1187 else
1188 return false;
1189 }
1190
1191 /* To have gotten here know that.
1192 *(a + b)
1193
1194 ... = (a + b)
1195
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
1198 the inc. */
1199 other_insn
1200 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1201 if (other_insn && luid > DF_INSN_LUID (other_insn))
1202 return false;
1203 }
1204 }
1205
1206 if (inc_insn.form == FORM_POST_INC)
1207 {
1208 other_insn
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)
1216 return false;
1217 }
1218
1219 return try_merge ();
1220 }
1221
1222
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. */
1226
1227 static bool
1228 find_mem (rtx *address_of_x)
1229 {
1230 rtx x = *address_of_x;
1231 enum rtx_code code = GET_CODE (x);
1232 const char *const fmt = GET_RTX_FORMAT (code);
1233 int i;
1234
1235 if (code == MEM && REG_P (XEXP (x, 0)))
1236 {
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))
1244 return true;
1245 }
1246 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
1247 && REG_P (XEXP (XEXP (x, 0), 0)))
1248 {
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))
1254 {
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))
1259 return true;
1260 }
1261 else if (REG_P (reg1))
1262 {
1263 /* Match with *(reg0 + reg1). */
1264 mem_insn.reg1_is_const = false;
1265 if (find_inc (true))
1266 return true;
1267 }
1268 }
1269
1270 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
1271 {
1272 /* If REG occurs inside a MEM used in a bit-field reference,
1273 that is unacceptable. */
1274 return false;
1275 }
1276
1277 /* Time for some deep diving. */
1278 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1279 {
1280 if (fmt[i] == 'e')
1281 {
1282 if (find_mem (&XEXP (x, i)))
1283 return true;
1284 }
1285 else if (fmt[i] == 'E')
1286 {
1287 int j;
1288 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1289 if (find_mem (&XVECEXP (x, i, j)))
1290 return true;
1291 }
1292 }
1293 return false;
1294 }
1295
1296
1297 /* Try to combine all incs and decs by constant values with memory
1298 references in BB. */
1299
1300 static void
1301 merge_in_block (int max_reg, basic_block bb)
1302 {
1303 rtx_insn *insn;
1304 rtx_insn *curr;
1305 int success_in_block = 0;
1306
1307 if (dump_file)
1308 fprintf (dump_file, "\n\nstarting bb %d\n", bb->index);
1309
1310 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, curr)
1311 {
1312 bool insn_is_add_or_inc = true;
1313
1314 if (!NONDEBUG_INSN_P (insn))
1315 continue;
1316
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. */
1320 if (JUMP_P (insn))
1321 continue;
1322
1323 if (dump_file)
1324 dump_insn_slim (dump_file, insn);
1325
1326 /* Does this instruction increment or decrement a register? */
1327 if (parse_add_or_inc (insn, true))
1328 {
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
1332 profitable. */
1333 if ((inc_insn.form == FORM_PRE_INC) || inc_insn.reg1_is_const)
1334 {
1335 mem_insn.insn = get_next_ref (regno, bb, reg_next_use);
1336 if (mem_insn.insn)
1337 {
1338 bool ok = true;
1339 if (!inc_insn.reg1_is_const)
1340 {
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);
1350
1351 if (other_insn && luid > DF_INSN_LUID (other_insn))
1352 ok = false;
1353
1354 other_insn
1355 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1356
1357 if (other_insn && luid > DF_INSN_LUID (other_insn))
1358 ok = false;
1359 }
1360
1361 if (dump_file)
1362 dump_inc_insn (dump_file);
1363
1364 if (ok && find_address (&PATTERN (mem_insn.insn)) == -1)
1365 {
1366 if (dump_file)
1367 dump_mem_insn (dump_file);
1368 if (try_merge ())
1369 {
1370 success_in_block++;
1371 insn_is_add_or_inc = false;
1372 }
1373 }
1374 }
1375 }
1376 }
1377 else
1378 {
1379 insn_is_add_or_inc = false;
1380 mem_insn.insn = insn;
1381 if (find_mem (&PATTERN (insn)))
1382 success_in_block++;
1383 }
1384
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))
1388 {
1389 df_ref def, use;
1390
1391 /* Need to update next use. */
1392 FOR_EACH_INSN_INFO_DEF (def, insn_info)
1393 {
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;
1397 }
1398
1399 FOR_EACH_INSN_INFO_USE (use, insn_info)
1400 {
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;
1404 else
1405 reg_next_inc_use[DF_REF_REGNO (use)] = NULL;
1406 }
1407 }
1408 else if (dump_file)
1409 fprintf (dump_file, "skipping update of deleted insn %d\n",
1410 INSN_UID (insn));
1411 }
1412
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)
1417 {
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);
1425 }
1426 }
1427
1428 /* Discover auto-inc auto-dec instructions. */
1429
1430 namespace {
1431
1432 const pass_data pass_data_inc_dec =
1433 {
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 */
1443 };
1444
1445 class pass_inc_dec : public rtl_opt_pass
1446 {
1447 public:
1448 pass_inc_dec (gcc::context *ctxt)
1449 : rtl_opt_pass (pass_data_inc_dec, ctxt)
1450 {}
1451
1452 /* opt_pass methods: */
1453 virtual bool gate (function *)
1454 {
1455 if (!AUTO_INC_DEC)
1456 return false;
1457
1458 return (optimize > 0 && flag_auto_inc_dec);
1459 }
1460
1461
1462 unsigned int execute (function *);
1463
1464 }; // class pass_inc_dec
1465
1466 unsigned int
1467 pass_inc_dec::execute (function *fun ATTRIBUTE_UNUSED)
1468 {
1469 if (!AUTO_INC_DEC)
1470 return 0;
1471
1472 basic_block bb;
1473 int max_reg = max_reg_num ();
1474
1475 if (!initialized)
1476 init_decision_table ();
1477
1478 mem_tmp = gen_rtx_MEM (Pmode, NULL_RTX);
1479
1480 df_note_add_problem ();
1481 df_analyze ();
1482
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);
1488
1489 free (reg_next_use);
1490 free (reg_next_inc_use);
1491 free (reg_next_def);
1492
1493 mem_tmp = NULL;
1494
1495 return 0;
1496 }
1497
1498 } // anon namespace
1499
1500 rtl_opt_pass *
1501 make_pass_inc_dec (gcc::context *ctxt)
1502 {
1503 return new pass_inc_dec (ctxt);
1504 }