]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/auto-inc-dec.c
[Ada] Define the -fdump-scos option in lang.opt
[thirdparty/gcc.git] / gcc / auto-inc-dec.c
CommitLineData
3072d30e 1/* Discovery of auto-inc and auto-dec instructions.
fbd26352 2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
3072d30e 3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
48e1416a 4
3072d30e 5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
8c4c00c1 9Software Foundation; either version 3, or (at your option) any later
3072d30e 10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
3072d30e 20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
9ef16211 24#include "backend.h"
7c29e30e 25#include "target.h"
3072d30e 26#include "rtl.h"
7c29e30e 27#include "tree.h"
28#include "predict.h"
9ef16211 29#include "df.h"
94ea8568 30#include "insn-config.h"
ad7b10a2 31#include "memmodel.h"
7c29e30e 32#include "emit-rtl.h"
33#include "recog.h"
7c29e30e 34#include "cfgrtl.h"
3072d30e 35#include "expr.h"
3072d30e 36#include "tree-pass.h"
3072d30e 37#include "dbgcnt.h"
397881d3 38#include "print-rtl.h"
f21023fe 39#include "valtrack.h"
3072d30e 40
41/* This pass was originally removed from flow.c. However there is
42 almost nothing that remains of that code.
43
44 There are (4) basic forms that are matched:
45
ca4bb7ac 46 (1) FORM_PRE_ADD
3072d30e 47 a <- b + c
48 ...
49 *a
50
51 becomes
52
53 a <- b
54 ...
55 *(a += c) pre
ca4bb7ac 56
f21023fe 57 or, alternately,
58
59 a <- b + c
60 ...
61 *b
62
63 becomes
64
65 a <- b
66 ...
67 *(a += c) post
68
69 This uses a post-add, but it's handled as FORM_PRE_ADD because
70 the "increment" insn appears before the memory access.
71
ca4bb7ac 72
73 (2) FORM_PRE_INC
3072d30e 74 a += c
75 ...
76 *a
77
78 becomes
79
f21023fe 80 ...
3072d30e 81 *(a += c) pre
ca4bb7ac 82
83
84 (3) FORM_POST_ADD
3072d30e 85 *a
86 ...
87 b <- a + c
88
48e1416a 89 (For this case to be true, b must not be assigned or used between
ca4bb7ac 90 the *a and the assignment to b. B must also be a Pmode reg.)
3072d30e 91
92 becomes
93
94 b <- a
3072d30e 95 *(b += c) post
f21023fe 96 ...
ca4bb7ac 97
98
99 (4) FORM_POST_INC
3072d30e 100 *a
101 ...
102 a <- a + c
103
104 becomes
105
106 *(a += c) post
f21023fe 107 ...
108
3072d30e 109
110 There are three types of values of c.
111
112 1) c is a constant equal to the width of the value being accessed by
113 the pointer. This is useful for machines that have
114 HAVE_PRE_INCREMENT, HAVE_POST_INCREMENT, HAVE_PRE_DECREMENT or
115 HAVE_POST_DECREMENT defined.
116
bef304b8 117 2) c is a constant not equal to the width of the value being accessed
3072d30e 118 by the pointer. This is useful for machines that have
119 HAVE_PRE_MODIFY_DISP, HAVE_POST_MODIFY_DISP defined.
120
48e1416a 121 3) c is a register. This is useful for machines that have
122 HAVE_PRE_MODIFY_REG, HAVE_POST_MODIFY_REG
123
3072d30e 124 The is one special case: if a already had an offset equal to it +-
125 its width and that offset is equal to -c when the increment was
126 before the ref or +c if the increment was after the ref, then if we
ca4bb7ac 127 can do the combination but switch the pre/post bit. */
3072d30e 128
3072d30e 129
130enum form
131{
132 FORM_PRE_ADD,
133 FORM_PRE_INC,
134 FORM_POST_ADD,
135 FORM_POST_INC,
136 FORM_last
137};
138
139/* The states of the second operands of mem refs and inc insns. If no
140 second operand of the mem_ref was found, it is assumed to just be
141 ZERO. SIZE is the size of the mode accessed in the memref. The
142 ANY is used for constants that are not +-size or 0. REG is used if
143 the forms are reg1 + reg2. */
144
48e1416a 145enum inc_state
3072d30e 146{
147 INC_ZERO, /* == 0 */
148 INC_NEG_SIZE, /* == +size */
149 INC_POS_SIZE, /* == -size */
150 INC_NEG_ANY, /* == some -constant */
151 INC_POS_ANY, /* == some +constant */
152 INC_REG, /* == some register */
153 INC_last
154};
155
156/* The eight forms that pre/post inc/dec can take. */
157enum gen_form
158{
159 NOTHING,
160 SIMPLE_PRE_INC, /* ++size */
161 SIMPLE_POST_INC, /* size++ */
162 SIMPLE_PRE_DEC, /* --size */
163 SIMPLE_POST_DEC, /* size-- */
164 DISP_PRE, /* ++con */
165 DISP_POST, /* con++ */
166 REG_PRE, /* ++reg */
167 REG_POST /* reg++ */
168};
169
170/* Tmp mem rtx for use in cost modeling. */
171static rtx mem_tmp;
172
173static enum inc_state
7f4685e5 174set_inc_state (HOST_WIDE_INT val, poly_int64 size)
3072d30e 175{
176 if (val == 0)
177 return INC_ZERO;
178 if (val < 0)
7f4685e5 179 return known_eq (val, -size) ? INC_NEG_SIZE : INC_NEG_ANY;
3072d30e 180 else
7f4685e5 181 return known_eq (val, size) ? INC_POS_SIZE : INC_POS_ANY;
3072d30e 182}
183
184/* The DECISION_TABLE that describes what form, if any, the increment
185 or decrement will take. It is a three dimensional table. The first
186 index is the type of constant or register found as the second
187 operand of the inc insn. The second index is the type of constant
188 or register found as the second operand of the memory reference (if
189 no second operand exists, 0 is used). The third index is the form
190 and location (relative to the mem reference) of inc insn. */
191
192static bool initialized = false;
193static enum gen_form decision_table[INC_last][INC_last][FORM_last];
194
195static void
196init_decision_table (void)
197{
198 enum gen_form value;
199
200 if (HAVE_PRE_INCREMENT || HAVE_PRE_MODIFY_DISP)
201 {
202 /* Prefer the simple form if both are available. */
203 value = (HAVE_PRE_INCREMENT) ? SIMPLE_PRE_INC : DISP_PRE;
204
205 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
206 decision_table[INC_POS_SIZE][INC_ZERO][FORM_PRE_INC] = value;
207
208 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_ADD] = value;
209 decision_table[INC_POS_SIZE][INC_POS_SIZE][FORM_POST_INC] = value;
210 }
211
212 if (HAVE_POST_INCREMENT || HAVE_POST_MODIFY_DISP)
213 {
214 /* Prefer the simple form if both are available. */
215 value = (HAVE_POST_INCREMENT) ? SIMPLE_POST_INC : DISP_POST;
216
217 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_ADD] = value;
218 decision_table[INC_POS_SIZE][INC_ZERO][FORM_POST_INC] = value;
219
220 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_ADD] = value;
221 decision_table[INC_POS_SIZE][INC_NEG_SIZE][FORM_PRE_INC] = value;
222 }
223
224 if (HAVE_PRE_DECREMENT || HAVE_PRE_MODIFY_DISP)
225 {
226 /* Prefer the simple form if both are available. */
227 value = (HAVE_PRE_DECREMENT) ? SIMPLE_PRE_DEC : DISP_PRE;
228
229 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_ADD] = value;
230 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_PRE_INC] = value;
231
232 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_ADD] = value;
233 decision_table[INC_NEG_SIZE][INC_NEG_SIZE][FORM_POST_INC] = value;
234 }
235
236 if (HAVE_POST_DECREMENT || HAVE_POST_MODIFY_DISP)
237 {
238 /* Prefer the simple form if both are available. */
239 value = (HAVE_POST_DECREMENT) ? SIMPLE_POST_DEC : DISP_POST;
240
241 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_ADD] = value;
242 decision_table[INC_NEG_SIZE][INC_ZERO][FORM_POST_INC] = value;
243
244 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_ADD] = value;
245 decision_table[INC_NEG_SIZE][INC_POS_SIZE][FORM_PRE_INC] = value;
246 }
247
248 if (HAVE_PRE_MODIFY_DISP)
249 {
250 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
251 decision_table[INC_POS_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
252
253 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_ADD] = DISP_PRE;
254 decision_table[INC_POS_ANY][INC_POS_ANY][FORM_POST_INC] = DISP_PRE;
255
256 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_ADD] = DISP_PRE;
257 decision_table[INC_NEG_ANY][INC_ZERO][FORM_PRE_INC] = DISP_PRE;
258
259 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_ADD] = DISP_PRE;
260 decision_table[INC_NEG_ANY][INC_NEG_ANY][FORM_POST_INC] = DISP_PRE;
261 }
262
263 if (HAVE_POST_MODIFY_DISP)
264 {
265 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
266 decision_table[INC_POS_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
267
268 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_ADD] = DISP_POST;
269 decision_table[INC_POS_ANY][INC_NEG_ANY][FORM_PRE_INC] = DISP_POST;
270
271 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_ADD] = DISP_POST;
272 decision_table[INC_NEG_ANY][INC_ZERO][FORM_POST_INC] = DISP_POST;
273
274 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_ADD] = DISP_POST;
275 decision_table[INC_NEG_ANY][INC_POS_ANY][FORM_PRE_INC] = DISP_POST;
276 }
277
278 /* This is much simpler than the other cases because we do not look
279 for the reg1-reg2 case. Note that we do not have a INC_POS_REG
280 and INC_NEG_REG states. Most of the use of such states would be
281 on a target that had an R1 - R2 update address form.
282
283 There is the remote possibility that you could also catch a = a +
284 b; *(a - b) as a postdecrement of (a + b). However, it is
285 unclear if *(a - b) would ever be generated on a machine that did
286 not have that kind of addressing mode. The IA-64 and RS6000 will
287 not do this, and I cannot speak for any other. If any
288 architecture does have an a-b update for, these cases should be
289 added. */
290 if (HAVE_PRE_MODIFY_REG)
291 {
292 decision_table[INC_REG][INC_ZERO][FORM_PRE_ADD] = REG_PRE;
293 decision_table[INC_REG][INC_ZERO][FORM_PRE_INC] = REG_PRE;
294
295 decision_table[INC_REG][INC_REG][FORM_POST_ADD] = REG_PRE;
296 decision_table[INC_REG][INC_REG][FORM_POST_INC] = REG_PRE;
297 }
298
299 if (HAVE_POST_MODIFY_REG)
300 {
301 decision_table[INC_REG][INC_ZERO][FORM_POST_ADD] = REG_POST;
302 decision_table[INC_REG][INC_ZERO][FORM_POST_INC] = REG_POST;
303 }
304
305 initialized = true;
306}
307
308/* Parsed fields of an inc insn of the form "reg_res = reg0+reg1" or
309 "reg_res = reg0+c". */
310
48e1416a 311static struct inc_insn
3072d30e 312{
4db65708 313 rtx_insn *insn; /* The insn being parsed. */
3072d30e 314 rtx pat; /* The pattern of the insn. */
315 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
316 enum form form;
317 rtx reg_res;
318 rtx reg0;
319 rtx reg1;
320 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
321 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
322} inc_insn;
323
324
325/* Dump the parsed inc insn to FILE. */
326
48e1416a 327static void
3072d30e 328dump_inc_insn (FILE *file)
329{
48e1416a 330 const char *f = ((inc_insn.form == FORM_PRE_ADD)
3072d30e 331 || (inc_insn.form == FORM_PRE_INC)) ? "pre" : "post";
332
333 dump_insn_slim (file, inc_insn.insn);
334
335 switch (inc_insn.form)
336 {
337 case FORM_PRE_ADD:
338 case FORM_POST_ADD:
339 if (inc_insn.reg1_is_const)
48e1416a 340 fprintf (file, "found %s add(%d) r[%d]=r[%d]+%d\n",
341 f, INSN_UID (inc_insn.insn),
342 REGNO (inc_insn.reg_res),
3072d30e 343 REGNO (inc_insn.reg0), (int) inc_insn.reg1_val);
344 else
48e1416a 345 fprintf (file, "found %s add(%d) r[%d]=r[%d]+r[%d]\n",
346 f, INSN_UID (inc_insn.insn),
347 REGNO (inc_insn.reg_res),
3072d30e 348 REGNO (inc_insn.reg0), REGNO (inc_insn.reg1));
349 break;
48e1416a 350
3072d30e 351 case FORM_PRE_INC:
352 case FORM_POST_INC:
353 if (inc_insn.reg1_is_const)
48e1416a 354 fprintf (file, "found %s inc(%d) r[%d]+=%d\n",
355 f, INSN_UID (inc_insn.insn),
3072d30e 356 REGNO (inc_insn.reg_res), (int) inc_insn.reg1_val);
357 else
48e1416a 358 fprintf (file, "found %s inc(%d) r[%d]+=r[%d]\n",
359 f, INSN_UID (inc_insn.insn),
3072d30e 360 REGNO (inc_insn.reg_res), REGNO (inc_insn.reg1));
361 break;
362
363 default:
364 break;
365 }
366}
367
368
369/* Parsed fields of a mem ref of the form "*(reg0+reg1)" or "*(reg0+c)". */
370
371static struct mem_insn
372{
4db65708 373 rtx_insn *insn; /* The insn being parsed. */
3072d30e 374 rtx pat; /* The pattern of the insn. */
375 rtx *mem_loc; /* The address of the field that holds the mem */
376 /* that is to be replaced. */
377 bool reg1_is_const; /* True if reg1 is const, false if reg1 is a reg. */
378 rtx reg0;
379 rtx reg1; /* This is either a reg or a const depending on
380 reg1_is_const. */
381 enum inc_state reg1_state;/* The form of the const if reg1 is a const. */
382 HOST_WIDE_INT reg1_val;/* Value if reg1 is const. */
383} mem_insn;
384
385
386/* Dump the parsed mem insn to FILE. */
387
48e1416a 388static void
3072d30e 389dump_mem_insn (FILE *file)
390{
391 dump_insn_slim (file, mem_insn.insn);
392
393 if (mem_insn.reg1_is_const)
48e1416a 394 fprintf (file, "found mem(%d) *(r[%d]+%d)\n",
395 INSN_UID (mem_insn.insn),
3072d30e 396 REGNO (mem_insn.reg0), (int) mem_insn.reg1_val);
397 else
48e1416a 398 fprintf (file, "found mem(%d) *(r[%d]+r[%d])\n",
399 INSN_UID (mem_insn.insn),
3072d30e 400 REGNO (mem_insn.reg0), REGNO (mem_insn.reg1));
401}
402
403
404/* The following three arrays contain pointers to instructions. They
405 are indexed by REGNO. At any point in the basic block where we are
406 looking these three arrays contain, respectively, the next insn
407 that uses REGNO, the next inc or add insn that uses REGNO and the
408 next insn that sets REGNO.
409
410 The arrays are not cleared when we move from block to block so
411 whenever an insn is retrieved from these arrays, it's block number
412 must be compared with the current block.
413*/
414
f21023fe 415static rtx_insn **reg_next_debug_use = NULL;
4db65708 416static rtx_insn **reg_next_use = NULL;
417static rtx_insn **reg_next_inc_use = NULL;
418static rtx_insn **reg_next_def = NULL;
3072d30e 419
420
421/* Move dead note that match PATTERN to TO_INSN from FROM_INSN. We do
422 not really care about moving any other notes from the inc or add
423 insn. Moving the REG_EQUAL and REG_EQUIV is clearly wrong and it
bef304b8 424 does not appear that there are any other kinds of relevant notes. */
3072d30e 425
48e1416a 426static void
4db65708 427move_dead_notes (rtx_insn *to_insn, rtx_insn *from_insn, rtx pattern)
3072d30e 428{
48e1416a 429 rtx note;
3072d30e 430 rtx next_note;
431 rtx prev_note = NULL;
432
433 for (note = REG_NOTES (from_insn); note; note = next_note)
434 {
435 next_note = XEXP (note, 1);
48e1416a 436
3072d30e 437 if ((REG_NOTE_KIND (note) == REG_DEAD)
438 && pattern == XEXP (note, 0))
439 {
440 XEXP (note, 1) = REG_NOTES (to_insn);
441 REG_NOTES (to_insn) = note;
442 if (prev_note)
443 XEXP (prev_note, 1) = next_note;
444 else
445 REG_NOTES (from_insn) = next_note;
446 }
447 else prev_note = note;
448 }
449}
450
3072d30e 451/* Change mem_insn.mem_loc so that uses NEW_ADDR which has an
452 increment of INC_REG. To have reached this point, the change is a
453 legitimate one from a dataflow point of view. The only questions
454 are is this a valid change to the instruction and is this a
455 profitable change to the instruction. */
456
457static bool
458attempt_change (rtx new_addr, rtx inc_reg)
459{
460 /* There are four cases: For the two cases that involve an add
461 instruction, we are going to have to delete the add and insert a
462 mov. We are going to assume that the mov is free. This is
463 fairly early in the backend and there are a lot of opportunities
464 for removing that move later. In particular, there is the case
465 where the move may be dead, this is what dead code elimination
466 passes are for. The two cases where we have an inc insn will be
467 handled mov free. */
468
90bd219d 469 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
4db65708 470 rtx_insn *mov_insn = NULL;
3072d30e 471 int regno;
472 rtx mem = *mem_insn.mem_loc;
3754d046 473 machine_mode mode = GET_MODE (mem);
23cc6082 474 int align = MEM_ALIGN (mem);
3072d30e 475 rtx new_mem;
476 int old_cost = 0;
477 int new_cost = 0;
f529eb25 478 bool speed = optimize_bb_for_speed_p (bb);
3072d30e 479
480 PUT_MODE (mem_tmp, mode);
481 XEXP (mem_tmp, 0) = new_addr;
23cc6082 482 set_mem_align (mem_tmp, align);
3072d30e 483
5ae4887d 484 old_cost = (set_src_cost (mem, mode, speed)
b72d459f 485 + set_rtx_cost (PATTERN (inc_insn.insn), speed));
48924509 486
5ae4887d 487 new_cost = set_src_cost (mem_tmp, mode, speed);
b9c74b4d 488
48924509 489 /* In the FORM_PRE_ADD and FORM_POST_ADD cases we emit an extra move
490 whose cost we should account for. */
491 if (inc_insn.form == FORM_PRE_ADD
492 || inc_insn.form == FORM_POST_ADD)
493 {
494 start_sequence ();
495 emit_move_insn (inc_insn.reg_res, inc_insn.reg0);
496 mov_insn = get_insns ();
497 end_sequence ();
498 new_cost += seq_cost (mov_insn, speed);
499 }
500
3072d30e 501 /* The first item of business is to see if this is profitable. */
502 if (old_cost < new_cost)
503 {
504 if (dump_file)
505 fprintf (dump_file, "cost failure old=%d new=%d\n", old_cost, new_cost);
506 return false;
507 }
508
9d75589a 509 /* Jump through a lot of hoops to keep the attributes up to date. We
3072d30e 510 do not want to call one of the change address variants that take
511 an offset even though we know the offset in many cases. These
512 assume you are changing where the address is pointing by the
513 offset. */
514 new_mem = replace_equiv_address_nv (mem, new_addr);
515 if (! validate_change (mem_insn.insn, mem_insn.mem_loc, new_mem, 0))
516 {
517 if (dump_file)
48e1416a 518 fprintf (dump_file, "validation failure\n");
3072d30e 519 return false;
520 }
521
522 /* From here to the end of the function we are committed to the
523 change, i.e. nothing fails. Generate any necessary movs, move
524 any regnotes, and fix up the reg_next_{use,inc_use,def}. */
525 switch (inc_insn.form)
526 {
527 case FORM_PRE_ADD:
a6879b1d 528 /* Replace the addition with a move. Do it at the location of
529 the addition since the operand of the addition may change
530 before the memory reference. */
48924509 531 gcc_assert (mov_insn);
532 emit_insn_before (mov_insn, inc_insn.insn);
d4ce94c1 533 regno = REGNO (inc_insn.reg0);
f21023fe 534 /* ??? Could REGNO possibly be used in MEM_INSN other than in
535 the MEM address, and still die there, so that move_dead_notes
536 would incorrectly move the note? */
d4ce94c1 537 if (reg_next_use[regno] == mem_insn.insn)
538 move_dead_notes (mov_insn, mem_insn.insn, inc_insn.reg0);
539 else
540 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
3072d30e 541
542 regno = REGNO (inc_insn.reg_res);
f21023fe 543 if (reg_next_debug_use && reg_next_debug_use[regno]
544 && BLOCK_FOR_INSN (reg_next_debug_use[regno]) == bb)
545 {
546 rtx adjres = gen_rtx_PLUS (GET_MODE (inc_insn.reg_res),
547 inc_insn.reg_res, inc_insn.reg1);
548 if (dump_file)
549 fprintf (dump_file, "adjusting debug insns\n");
550 propagate_for_debug (PREV_INSN (reg_next_debug_use[regno]),
551 mem_insn.insn,
552 inc_insn.reg_res, adjres, bb);
553 reg_next_debug_use[regno] = NULL;
554 }
3072d30e 555 reg_next_def[regno] = mov_insn;
556 reg_next_use[regno] = NULL;
f21023fe 557
3072d30e 558 regno = REGNO (inc_insn.reg0);
f21023fe 559 if (reg_next_debug_use && reg_next_debug_use[regno]
560 && BLOCK_FOR_INSN (reg_next_debug_use[regno]) == bb
561 && find_reg_note (mov_insn, REG_DEAD, inc_insn.reg0))
562 {
563 if (dump_file)
564 fprintf (dump_file, "remapping debug insns\n");
565 propagate_for_debug (PREV_INSN (reg_next_debug_use[regno]),
566 mem_insn.insn,
567 inc_insn.reg0, inc_insn.reg_res, bb);
568 reg_next_debug_use[regno] = NULL;
569 }
3072d30e 570 reg_next_use[regno] = mov_insn;
571 df_recompute_luids (bb);
572 break;
573
574 case FORM_POST_INC:
575 regno = REGNO (inc_insn.reg_res);
f21023fe 576 if (reg_next_debug_use && reg_next_debug_use[regno]
577 && BLOCK_FOR_INSN (reg_next_debug_use[regno]) == bb)
578 {
579 rtx adjres = gen_rtx_MINUS (GET_MODE (inc_insn.reg_res),
580 inc_insn.reg_res, inc_insn.reg1);
581 if (dump_file)
582 fprintf (dump_file, "adjusting debug insns\n");
583 propagate_for_debug (PREV_INSN (reg_next_debug_use[regno]),
584 inc_insn.insn,
585 inc_insn.reg_res, adjres, bb);
586 reg_next_debug_use[regno] = NULL;
587 }
3072d30e 588 if (reg_next_use[regno] == reg_next_inc_use[regno])
589 reg_next_inc_use[regno] = NULL;
590
591 /* Fallthru. */
592 case FORM_PRE_INC:
593 regno = REGNO (inc_insn.reg_res);
f21023fe 594 /* Despite the fall-through, we won't run this twice: we'll have
595 already cleared reg_next_debug_use[regno] before falling
596 through. */
597 if (reg_next_debug_use && reg_next_debug_use[regno]
598 && BLOCK_FOR_INSN (reg_next_debug_use[regno]) == bb)
599 {
600 rtx adjres = gen_rtx_PLUS (GET_MODE (inc_insn.reg_res),
601 inc_insn.reg_res, inc_insn.reg1);
602 if (dump_file)
603 fprintf (dump_file, "adjusting debug insns\n");
604 propagate_for_debug (PREV_INSN (reg_next_debug_use[regno]),
605 mem_insn.insn,
606 inc_insn.reg_res, adjres, bb);
607 if (DF_INSN_LUID (mem_insn.insn)
608 < DF_INSN_LUID (reg_next_debug_use[regno]))
609 reg_next_debug_use[regno] = NULL;
610 }
3072d30e 611 reg_next_def[regno] = mem_insn.insn;
612 reg_next_use[regno] = NULL;
613
614 break;
615
616 case FORM_POST_ADD:
48924509 617 gcc_assert (mov_insn);
618 emit_insn_before (mov_insn, mem_insn.insn);
3072d30e 619 move_dead_notes (mov_insn, inc_insn.insn, inc_insn.reg0);
620
621 /* Do not move anything to the mov insn because the instruction
622 pointer for the main iteration has not yet hit that. It is
623 still pointing to the mem insn. */
624 regno = REGNO (inc_insn.reg_res);
f21023fe 625 /* The pseudo is now set earlier, so it must have been dead in
626 that range, and dead registers cannot be referenced in debug
627 insns. */
628 gcc_assert (!(reg_next_debug_use && reg_next_debug_use[regno]
629 && BLOCK_FOR_INSN (reg_next_debug_use[regno]) == bb));
3072d30e 630 reg_next_def[regno] = mem_insn.insn;
631 reg_next_use[regno] = NULL;
632
633 regno = REGNO (inc_insn.reg0);
f21023fe 634 if (reg_next_debug_use && reg_next_debug_use[regno]
635 && BLOCK_FOR_INSN (reg_next_debug_use[regno]) == bb
636 && find_reg_note (mov_insn, REG_DEAD, inc_insn.reg0))
637 {
638 if (dump_file)
639 fprintf (dump_file, "remapping debug insns\n");
640 propagate_for_debug (PREV_INSN (reg_next_debug_use[regno]),
641 inc_insn.insn,
642 inc_insn.reg0, inc_insn.reg_res, bb);
643 reg_next_debug_use[regno] = NULL;
644 }
3072d30e 645 reg_next_use[regno] = mem_insn.insn;
646 if ((reg_next_use[regno] == reg_next_inc_use[regno])
647 || (reg_next_inc_use[regno] == inc_insn.insn))
648 reg_next_inc_use[regno] = NULL;
649 df_recompute_luids (bb);
650 break;
651
652 case FORM_last:
653 default:
654 gcc_unreachable ();
655 }
656
657 if (!inc_insn.reg1_is_const)
658 {
659 regno = REGNO (inc_insn.reg1);
660 reg_next_use[regno] = mem_insn.insn;
661 if ((reg_next_use[regno] == reg_next_inc_use[regno])
662 || (reg_next_inc_use[regno] == inc_insn.insn))
663 reg_next_inc_use[regno] = NULL;
664 }
665
666 delete_insn (inc_insn.insn);
667
668 if (dump_file && mov_insn)
669 {
670 fprintf (dump_file, "inserting mov ");
671 dump_insn_slim (dump_file, mov_insn);
672 }
673
674 /* Record that this insn has an implicit side effect. */
a1ddb869 675 add_reg_note (mem_insn.insn, REG_INC, inc_reg);
3072d30e 676
677 if (dump_file)
678 {
679 fprintf (dump_file, "****success ");
680 dump_insn_slim (dump_file, mem_insn.insn);
681 }
682
683 return true;
684}
685
686
687/* Try to combine the instruction in INC_INSN with the instruction in
688 MEM_INSN. First the form is determined using the DECISION_TABLE
f0b5f617 689 and the results of parsing the INC_INSN and the MEM_INSN.
3072d30e 690 Assuming the form is ok, a prototype new address is built which is
691 passed to ATTEMPT_CHANGE for final processing. */
692
48e1416a 693static bool
3072d30e 694try_merge (void)
695{
696 enum gen_form gen_form;
697 rtx mem = *mem_insn.mem_loc;
698 rtx inc_reg = inc_insn.form == FORM_POST_ADD ?
699 inc_insn.reg_res : mem_insn.reg0;
700
701 /* The width of the mem being accessed. */
52acb7ae 702 poly_int64 size = GET_MODE_SIZE (GET_MODE (mem));
4db65708 703 rtx_insn *last_insn = NULL;
3754d046 704 machine_mode reg_mode = GET_MODE (inc_reg);
3072d30e 705
706 switch (inc_insn.form)
707 {
708 case FORM_PRE_ADD:
709 case FORM_PRE_INC:
710 last_insn = mem_insn.insn;
711 break;
712 case FORM_POST_INC:
713 case FORM_POST_ADD:
714 last_insn = inc_insn.insn;
715 break;
716 case FORM_last:
717 default:
718 gcc_unreachable ();
719 }
720
721 /* Cannot handle auto inc of the stack. */
722 if (inc_reg == stack_pointer_rtx)
723 {
724 if (dump_file)
725 fprintf (dump_file, "cannot inc stack %d failure\n", REGNO (inc_reg));
726 return false;
727 }
728
729 /* Look to see if the inc register is dead after the memory
a6879b1d 730 reference. If it is, do not do the combination. */
3072d30e 731 if (find_regno_note (last_insn, REG_DEAD, REGNO (inc_reg)))
732 {
733 if (dump_file)
734 fprintf (dump_file, "dead failure %d\n", REGNO (inc_reg));
735 return false;
736 }
737
48e1416a 738 mem_insn.reg1_state = (mem_insn.reg1_is_const)
3072d30e 739 ? set_inc_state (mem_insn.reg1_val, size) : INC_REG;
740 inc_insn.reg1_state = (inc_insn.reg1_is_const)
741 ? set_inc_state (inc_insn.reg1_val, size) : INC_REG;
742
743 /* Now get the form that we are generating. */
48e1416a 744 gen_form = decision_table
3072d30e 745 [inc_insn.reg1_state][mem_insn.reg1_state][inc_insn.form];
746
747 if (dbg_cnt (auto_inc_dec) == false)
748 return false;
749
750 switch (gen_form)
751 {
752 default:
753 case NOTHING:
754 return false;
755
756 case SIMPLE_PRE_INC: /* ++size */
757 if (dump_file)
758 fprintf (dump_file, "trying SIMPLE_PRE_INC\n");
98155838 759 return attempt_change (gen_rtx_PRE_INC (reg_mode, inc_reg), inc_reg);
48e1416a 760
3072d30e 761 case SIMPLE_POST_INC: /* size++ */
762 if (dump_file)
763 fprintf (dump_file, "trying SIMPLE_POST_INC\n");
98155838 764 return attempt_change (gen_rtx_POST_INC (reg_mode, inc_reg), inc_reg);
48e1416a 765
3072d30e 766 case SIMPLE_PRE_DEC: /* --size */
767 if (dump_file)
768 fprintf (dump_file, "trying SIMPLE_PRE_DEC\n");
98155838 769 return attempt_change (gen_rtx_PRE_DEC (reg_mode, inc_reg), inc_reg);
48e1416a 770
3072d30e 771 case SIMPLE_POST_DEC: /* size-- */
772 if (dump_file)
773 fprintf (dump_file, "trying SIMPLE_POST_DEC\n");
98155838 774 return attempt_change (gen_rtx_POST_DEC (reg_mode, inc_reg), inc_reg);
48e1416a 775
3072d30e 776 case DISP_PRE: /* ++con */
777 if (dump_file)
778 fprintf (dump_file, "trying DISP_PRE\n");
98155838 779 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
3072d30e 780 inc_reg,
98155838 781 gen_rtx_PLUS (reg_mode,
3072d30e 782 inc_reg,
783 inc_insn.reg1)),
784 inc_reg);
48e1416a 785
3072d30e 786 case DISP_POST: /* con++ */
787 if (dump_file)
788 fprintf (dump_file, "trying POST_DISP\n");
98155838 789 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
3072d30e 790 inc_reg,
98155838 791 gen_rtx_PLUS (reg_mode,
3072d30e 792 inc_reg,
793 inc_insn.reg1)),
794 inc_reg);
48e1416a 795
3072d30e 796 case REG_PRE: /* ++reg */
797 if (dump_file)
798 fprintf (dump_file, "trying PRE_REG\n");
98155838 799 return attempt_change (gen_rtx_PRE_MODIFY (reg_mode,
3072d30e 800 inc_reg,
98155838 801 gen_rtx_PLUS (reg_mode,
3072d30e 802 inc_reg,
803 inc_insn.reg1)),
804 inc_reg);
48e1416a 805
3072d30e 806 case REG_POST: /* reg++ */
807 if (dump_file)
808 fprintf (dump_file, "trying POST_REG\n");
98155838 809 return attempt_change (gen_rtx_POST_MODIFY (reg_mode,
3072d30e 810 inc_reg,
98155838 811 gen_rtx_PLUS (reg_mode,
3072d30e 812 inc_reg,
813 inc_insn.reg1)),
814 inc_reg);
3072d30e 815 }
816}
817
818/* Return the next insn that uses (if reg_next_use is passed in
819 NEXT_ARRAY) or defines (if reg_next_def is passed in NEXT_ARRAY)
820 REGNO in BB. */
821
4db65708 822static rtx_insn *
823get_next_ref (int regno, basic_block bb, rtx_insn **next_array)
3072d30e 824{
4db65708 825 rtx_insn *insn = next_array[regno];
3072d30e 826
827 /* Lazy about cleaning out the next_arrays. */
90bd219d 828 if (insn && BLOCK_FOR_INSN (insn) != bb)
3072d30e 829 {
830 next_array[regno] = NULL;
831 insn = NULL;
832 }
833
834 return insn;
835}
836
837
3072d30e 838/* Return true if INSN is of a form "a = b op c" where a and b are
839 regs. op is + if c is a reg and +|- if c is a const. Fill in
48e1416a 840 INC_INSN with what is found.
841
3072d30e 842 This function is called in two contexts, if BEFORE_MEM is true,
843 this is called for each insn in the basic block. If BEFORE_MEM is
844 false, it is called for the instruction in the block that uses the
845 index register for some memory reference that is currently being
846 processed. */
847
848static bool
4db65708 849parse_add_or_inc (rtx_insn *insn, bool before_mem)
3072d30e 850{
851 rtx pat = single_set (insn);
852 if (!pat)
853 return false;
854
855 /* Result must be single reg. */
856 if (!REG_P (SET_DEST (pat)))
857 return false;
858
859 if ((GET_CODE (SET_SRC (pat)) != PLUS)
860 && (GET_CODE (SET_SRC (pat)) != MINUS))
861 return false;
862
863 if (!REG_P (XEXP (SET_SRC (pat), 0)))
864 return false;
865
866 inc_insn.insn = insn;
867 inc_insn.pat = pat;
868 inc_insn.reg_res = SET_DEST (pat);
869 inc_insn.reg0 = XEXP (SET_SRC (pat), 0);
5c31bc69 870
871 /* Block any auto increment of the frame pointer since it expands into
872 an addition and cannot be removed by copy propagation. */
873 if (inc_insn.reg0 == frame_pointer_rtx)
874 return false;
875
3072d30e 876 if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg0))
877 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
48e1416a 878 else
3072d30e 879 inc_insn.form = before_mem ? FORM_PRE_ADD : FORM_POST_ADD;
880
971ba038 881 if (CONST_INT_P (XEXP (SET_SRC (pat), 1)))
3072d30e 882 {
883 /* Process a = b + c where c is a const. */
884 inc_insn.reg1_is_const = true;
885 if (GET_CODE (SET_SRC (pat)) == PLUS)
886 {
887 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
888 inc_insn.reg1_val = INTVAL (inc_insn.reg1);
889 }
890 else
891 {
892 inc_insn.reg1_val = -INTVAL (XEXP (SET_SRC (pat), 1));
893 inc_insn.reg1 = GEN_INT (inc_insn.reg1_val);
894 }
895 return true;
896 }
897 else if ((HAVE_PRE_MODIFY_REG || HAVE_POST_MODIFY_REG)
898 && (REG_P (XEXP (SET_SRC (pat), 1)))
899 && GET_CODE (SET_SRC (pat)) == PLUS)
900 {
901 /* Process a = b + c where c is a reg. */
902 inc_insn.reg1 = XEXP (SET_SRC (pat), 1);
903 inc_insn.reg1_is_const = false;
48e1416a 904
905 if (inc_insn.form == FORM_PRE_INC
3072d30e 906 || inc_insn.form == FORM_POST_INC)
907 return true;
908 else if (rtx_equal_p (inc_insn.reg_res, inc_insn.reg1))
909 {
910 /* Reverse the two operands and turn *_ADD into *_INC since
911 a = c + a. */
a4f59596 912 std::swap (inc_insn.reg0, inc_insn.reg1);
3072d30e 913 inc_insn.form = before_mem ? FORM_PRE_INC : FORM_POST_INC;
914 return true;
915 }
48e1416a 916 else
3072d30e 917 return true;
918 }
919
920 return false;
921}
922
923
924/* A recursive function that checks all of the mem uses in
925 ADDRESS_OF_X to see if any single one of them is compatible with
d4ce94c1 926 what has been found in inc_insn. To avoid accidental matches, we
927 will only find MEMs with FINDREG, be it inc_insn.reg_res, be it
928 inc_insn.reg0.
3072d30e 929
48e1416a 930 -1 is returned for success. 0 is returned if nothing was found and
3072d30e 931 1 is returned for failure. */
932
933static int
d4ce94c1 934find_address (rtx *address_of_x, rtx findreg)
3072d30e 935{
936 rtx x = *address_of_x;
937 enum rtx_code code = GET_CODE (x);
938 const char *const fmt = GET_RTX_FORMAT (code);
939 int i;
940 int value = 0;
941 int tem;
942
d4ce94c1 943 if (code == MEM && findreg == inc_insn.reg_res
944 && rtx_equal_p (XEXP (x, 0), inc_insn.reg_res))
3072d30e 945 {
d4ce94c1 946 /* Match with *reg_res. */
3072d30e 947 mem_insn.mem_loc = address_of_x;
948 mem_insn.reg0 = inc_insn.reg_res;
949 mem_insn.reg1_is_const = true;
950 mem_insn.reg1_val = 0;
951 mem_insn.reg1 = GEN_INT (0);
952 return -1;
953 }
d4ce94c1 954 if (code == MEM && inc_insn.reg1_is_const && inc_insn.reg0
955 && findreg == inc_insn.reg0
956 && rtx_equal_p (XEXP (x, 0), inc_insn.reg0))
957 {
958 /* Match with *reg0, assumed to be equivalent to
959 *(reg_res - reg1_val); callers must check whether this is the case. */
960 mem_insn.mem_loc = address_of_x;
961 mem_insn.reg0 = inc_insn.reg_res;
962 mem_insn.reg1_is_const = true;
963 mem_insn.reg1_val = -inc_insn.reg1_val;
964 mem_insn.reg1 = GEN_INT (mem_insn.reg1_val);
965 return -1;
966 }
967 if (code == MEM && findreg == inc_insn.reg_res
968 && GET_CODE (XEXP (x, 0)) == PLUS
3072d30e 969 && rtx_equal_p (XEXP (XEXP (x, 0), 0), inc_insn.reg_res))
970 {
971 rtx b = XEXP (XEXP (x, 0), 1);
972 mem_insn.mem_loc = address_of_x;
973 mem_insn.reg0 = inc_insn.reg_res;
974 mem_insn.reg1 = b;
975 mem_insn.reg1_is_const = inc_insn.reg1_is_const;
971ba038 976 if (CONST_INT_P (b))
3072d30e 977 {
978 /* Match with *(reg0 + reg1) where reg1 is a const. */
979 HOST_WIDE_INT val = INTVAL (b);
48e1416a 980 if (inc_insn.reg1_is_const
3072d30e 981 && (inc_insn.reg1_val == val || inc_insn.reg1_val == -val))
982 {
983 mem_insn.reg1_val = val;
984 return -1;
985 }
986 }
48e1416a 987 else if (!inc_insn.reg1_is_const
988 && rtx_equal_p (inc_insn.reg1, b))
3072d30e 989 /* Match with *(reg0 + reg1). */
990 return -1;
991 }
992
993 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
994 {
995 /* If REG occurs inside a MEM used in a bit-field reference,
996 that is unacceptable. */
d4ce94c1 997 if (find_address (&XEXP (x, 0), findreg))
3072d30e 998 return 1;
999 }
1000
1001 if (x == inc_insn.reg_res)
1002 return 1;
1003
1004 /* Time for some deep diving. */
1005 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1006 {
1007 if (fmt[i] == 'e')
1008 {
d4ce94c1 1009 tem = find_address (&XEXP (x, i), findreg);
3072d30e 1010 /* If this is the first use, let it go so the rest of the
1011 insn can be checked. */
1012 if (value == 0)
1013 value = tem;
1014 else if (tem != 0)
1015 /* More than one match was found. */
1016 return 1;
1017 }
1018 else if (fmt[i] == 'E')
1019 {
1020 int j;
1021 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1022 {
d4ce94c1 1023 tem = find_address (&XVECEXP (x, i, j), findreg);
3072d30e 1024 /* If this is the first use, let it go so the rest of
1025 the insn can be checked. */
1026 if (value == 0)
1027 value = tem;
1028 else if (tem != 0)
1029 /* More than one match was found. */
1030 return 1;
1031 }
1032 }
1033 }
1034 return value;
1035}
1036
1037/* Once a suitable mem reference has been found and the MEM_INSN
1038 structure has been filled in, FIND_INC is called to see if there is
1039 a suitable add or inc insn that follows the mem reference and
1040 determine if it is suitable to merge.
1041
1042 In the case where the MEM_INSN has two registers in the reference,
1043 this function may be called recursively. The first time looking
1044 for an add of the first register, and if that fails, looking for an
1045 add of the second register. The FIRST_TRY parameter is used to
1046 only allow the parameters to be reversed once. */
1047
48e1416a 1048static bool
3072d30e 1049find_inc (bool first_try)
1050{
4db65708 1051 rtx_insn *insn;
90bd219d 1052 basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
4db65708 1053 rtx_insn *other_insn;
be10bb5a 1054 df_ref def;
3072d30e 1055
1056 /* Make sure this reg appears only once in this insn. */
1057 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
1058 {
1059 if (dump_file)
48e1416a 1060 fprintf (dump_file, "mem count failure\n");
3072d30e 1061 return false;
1062 }
1063
1064 if (dump_file)
1065 dump_mem_insn (dump_file);
1066
1067 /* Find the next use that is an inc. */
48e1416a 1068 insn = get_next_ref (REGNO (mem_insn.reg0),
90bd219d 1069 BLOCK_FOR_INSN (mem_insn.insn),
3072d30e 1070 reg_next_inc_use);
1071 if (!insn)
1072 return false;
1073
1074 /* Even though we know the next use is an add or inc because it came
1075 from the reg_next_inc_use, we must still reparse. */
1076 if (!parse_add_or_inc (insn, false))
1077 {
1078 /* Next use was not an add. Look for one extra case. It could be
1079 that we have:
48e1416a 1080
3072d30e 1081 *(a + b)
1082 ...= a;
1083 ...= b + a
48e1416a 1084
3072d30e 1085 if we reverse the operands in the mem ref we would
1086 find this. Only try it once though. */
1087 if (first_try && !mem_insn.reg1_is_const)
1088 {
a4f59596 1089 std::swap (mem_insn.reg0, mem_insn.reg1);
3072d30e 1090 return find_inc (false);
1091 }
1092 else
1093 return false;
1094 }
1095
48e1416a 1096 /* Need to assure that none of the operands of the inc instruction are
3072d30e 1097 assigned to by the mem insn. */
be10bb5a 1098 FOR_EACH_INSN_DEF (def, mem_insn.insn)
3072d30e 1099 {
3072d30e 1100 unsigned int regno = DF_REF_REGNO (def);
48e1416a 1101 if ((regno == REGNO (inc_insn.reg0))
3072d30e 1102 || (regno == REGNO (inc_insn.reg_res)))
1103 {
1104 if (dump_file)
1105 fprintf (dump_file, "inc conflicts with store failure.\n");
1106 return false;
1107 }
1108 if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
1109 {
1110 if (dump_file)
1111 fprintf (dump_file, "inc conflicts with store failure.\n");
1112 return false;
1113 }
1114 }
1115
1116 if (dump_file)
1117 dump_inc_insn (dump_file);
1118
1119 if (inc_insn.form == FORM_POST_ADD)
1120 {
1121 /* Make sure that there is no insn that assigns to inc_insn.res
1122 between the mem_insn and the inc_insn. */
4db65708 1123 rtx_insn *other_insn = get_next_ref (REGNO (inc_insn.reg_res),
1124 BLOCK_FOR_INSN (mem_insn.insn),
1125 reg_next_def);
3072d30e 1126 if (other_insn != inc_insn.insn)
1127 {
1128 if (dump_file)
48e1416a 1129 fprintf (dump_file,
3072d30e 1130 "result of add is assigned to between mem and inc insns.\n");
1131 return false;
1132 }
1133
48e1416a 1134 other_insn = get_next_ref (REGNO (inc_insn.reg_res),
90bd219d 1135 BLOCK_FOR_INSN (mem_insn.insn),
3072d30e 1136 reg_next_use);
48e1416a 1137 if (other_insn
3072d30e 1138 && (other_insn != inc_insn.insn)
1139 && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
1140 {
1141 if (dump_file)
48e1416a 1142 fprintf (dump_file,
3072d30e 1143 "result of add is used between mem and inc insns.\n");
1144 return false;
1145 }
1146
1147 /* For the post_add to work, the result_reg of the inc must not be
1148 used in the mem insn since this will become the new index
1149 register. */
6416852c 1150 if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
3072d30e 1151 {
1152 if (dump_file)
1153 fprintf (dump_file, "base reg replacement failure.\n");
1154 return false;
1155 }
1156 }
1157
1158 if (mem_insn.reg1_is_const)
1159 {
1160 if (mem_insn.reg1_val == 0)
1161 {
1162 if (!inc_insn.reg1_is_const)
1163 {
1164 /* The mem looks like *r0 and the rhs of the add has two
1165 registers. */
1166 int luid = DF_INSN_LUID (inc_insn.insn);
1167 if (inc_insn.form == FORM_POST_ADD)
1168 {
48e1416a 1169 /* The trick is that we are not going to increment r0,
3072d30e 1170 we are going to increment the result of the add insn.
1171 For this trick to be correct, the result reg of
1172 the inc must be a valid addressing reg. */
98155838 1173 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1174 if (GET_MODE (inc_insn.reg_res)
1175 != targetm.addr_space.address_mode (as))
3072d30e 1176 {
1177 if (dump_file)
1178 fprintf (dump_file, "base reg mode failure.\n");
1179 return false;
1180 }
1181
1182 /* We also need to make sure that the next use of
1183 inc result is after the inc. */
48e1416a 1184 other_insn
3072d30e 1185 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
1186 if (other_insn && luid > DF_INSN_LUID (other_insn))
1187 return false;
1188
1189 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
a4f59596 1190 std::swap (inc_insn.reg0, inc_insn.reg1);
3072d30e 1191 }
1192
48e1416a 1193 other_insn
3072d30e 1194 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1195 if (other_insn && luid > DF_INSN_LUID (other_insn))
1196 return false;
1197 }
1198 }
1199 /* Both the inc/add and the mem have a constant. Need to check
1200 that the constants are ok. */
1201 else if ((mem_insn.reg1_val != inc_insn.reg1_val)
1202 && (mem_insn.reg1_val != -inc_insn.reg1_val))
1203 return false;
1204 }
1205 else
1206 {
1207 /* The mem insn is of the form *(a + b) where a and b are both
1208 regs. It may be that in order to match the add or inc we
1209 need to treat it as if it was *(b + a). It may also be that
1210 the add is of the form a + c where c does not match b and
1211 then we just abandon this. */
48e1416a 1212
3072d30e 1213 int luid = DF_INSN_LUID (inc_insn.insn);
4db65708 1214 rtx_insn *other_insn;
48e1416a 1215
3072d30e 1216 /* Make sure this reg appears only once in this insn. */
1217 if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
1218 return false;
48e1416a 1219
3072d30e 1220 if (inc_insn.form == FORM_POST_ADD)
1221 {
1222 /* For this trick to be correct, the result reg of the inc
1223 must be a valid addressing reg. */
98155838 1224 addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
1225 if (GET_MODE (inc_insn.reg_res)
1226 != targetm.addr_space.address_mode (as))
3072d30e 1227 {
1228 if (dump_file)
1229 fprintf (dump_file, "base reg mode failure.\n");
1230 return false;
1231 }
1232
1233 if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1234 {
1235 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1236 {
1237 /* See comment above on find_inc (false) call. */
1238 if (first_try)
1239 {
a4f59596 1240 std::swap (mem_insn.reg0, mem_insn.reg1);
3072d30e 1241 return find_inc (false);
1242 }
1243 else
1244 return false;
1245 }
1246
bef304b8 1247 /* Need to check that there are no assignments to b
3072d30e 1248 before the add insn. */
48e1416a 1249 other_insn
3072d30e 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;
1253 /* All ok for the next step. */
1254 }
1255 else
1256 {
1257 /* We know that mem_insn.reg0 must equal inc_insn.reg1
1258 or else we would not have found the inc insn. */
a4f59596 1259 std::swap (mem_insn.reg0, mem_insn.reg1);
3072d30e 1260 if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
1261 {
1262 /* See comment above on find_inc (false) call. */
1263 if (first_try)
1264 return find_inc (false);
1265 else
1266 return false;
1267 }
1268 /* To have gotten here know that.
1269 *(b + a)
48e1416a 1270
3072d30e 1271 ... = (b + a)
48e1416a 1272
3072d30e 1273 We also know that the lhs of the inc is not b or a. We
1274 need to make sure that there are no assignments to b
48e1416a 1275 between the mem ref and the inc. */
1276
1277 other_insn
3072d30e 1278 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
1279 if (other_insn && luid > DF_INSN_LUID (other_insn))
1280 return false;
1281 }
1282
1283 /* Need to check that the next use of the add result is later than
1284 add insn since this will be the reg incremented. */
48e1416a 1285 other_insn
3072d30e 1286 = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
1287 if (other_insn && luid > DF_INSN_LUID (other_insn))
1288 return false;
1289 }
1290 else /* FORM_POST_INC. There is less to check here because we
48e1416a 1291 know that operands must line up. */
3072d30e 1292 {
1293 if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
1294 /* See comment above on find_inc (false) call. */
1295 {
1296 if (first_try)
1297 {
a4f59596 1298 std::swap (mem_insn.reg0, mem_insn.reg1);
3072d30e 1299 return find_inc (false);
1300 }
48e1416a 1301 else
3072d30e 1302 return false;
1303 }
48e1416a 1304
3072d30e 1305 /* To have gotten here know that.
1306 *(a + b)
48e1416a 1307
3072d30e 1308 ... = (a + b)
48e1416a 1309
3072d30e 1310 We also know that the lhs of the inc is not b. We need to make
1311 sure that there are no assignments to b between the mem ref and
1312 the inc. */
48e1416a 1313 other_insn
3072d30e 1314 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
1315 if (other_insn && luid > DF_INSN_LUID (other_insn))
1316 return false;
1317 }
1318 }
1319
1320 if (inc_insn.form == FORM_POST_INC)
1321 {
48e1416a 1322 other_insn
3072d30e 1323 = get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_use);
1324 /* When we found inc_insn, we were looking for the
1325 next add or inc, not the next insn that used the
1326 reg. Because we are going to increment the reg
1327 in this form, we need to make sure that there
6dfdc153 1328 were no intervening uses of reg. */
3072d30e 1329 if (inc_insn.insn != other_insn)
1330 return false;
1331 }
1332
1333 return try_merge ();
1334}
1335
1336
1337/* A recursive function that walks ADDRESS_OF_X to find all of the mem
1338 uses in pat that could be used as an auto inc or dec. It then
1339 calls FIND_INC for each one. */
1340
1341static bool
1342find_mem (rtx *address_of_x)
1343{
1344 rtx x = *address_of_x;
1345 enum rtx_code code = GET_CODE (x);
1346 const char *const fmt = GET_RTX_FORMAT (code);
1347 int i;
1348
1349 if (code == MEM && REG_P (XEXP (x, 0)))
1350 {
1351 /* Match with *reg0. */
1352 mem_insn.mem_loc = address_of_x;
1353 mem_insn.reg0 = XEXP (x, 0);
1354 mem_insn.reg1_is_const = true;
1355 mem_insn.reg1_val = 0;
1356 mem_insn.reg1 = GEN_INT (0);
1357 if (find_inc (true))
1358 return true;
1359 }
1360 if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
1361 && REG_P (XEXP (XEXP (x, 0), 0)))
1362 {
1363 rtx reg1 = XEXP (XEXP (x, 0), 1);
1364 mem_insn.mem_loc = address_of_x;
1365 mem_insn.reg0 = XEXP (XEXP (x, 0), 0);
1366 mem_insn.reg1 = reg1;
971ba038 1367 if (CONST_INT_P (reg1))
3072d30e 1368 {
1369 mem_insn.reg1_is_const = true;
1370 /* Match with *(reg0 + c) where c is a const. */
1371 mem_insn.reg1_val = INTVAL (reg1);
1372 if (find_inc (true))
1373 return true;
1374 }
1375 else if (REG_P (reg1))
1376 {
1377 /* Match with *(reg0 + reg1). */
1378 mem_insn.reg1_is_const = false;
1379 if (find_inc (true))
1380 return true;
1381 }
1382 }
1383
1384 if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
1385 {
1386 /* If REG occurs inside a MEM used in a bit-field reference,
1387 that is unacceptable. */
1388 return false;
1389 }
1390
1391 /* Time for some deep diving. */
1392 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1393 {
1394 if (fmt[i] == 'e')
1395 {
1396 if (find_mem (&XEXP (x, i)))
1397 return true;
1398 }
1399 else if (fmt[i] == 'E')
1400 {
1401 int j;
1402 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1403 if (find_mem (&XVECEXP (x, i, j)))
1404 return true;
1405 }
1406 }
1407 return false;
1408}
1409
1410
1411/* Try to combine all incs and decs by constant values with memory
1412 references in BB. */
1413
1414static void
1415merge_in_block (int max_reg, basic_block bb)
1416{
4db65708 1417 rtx_insn *insn;
1418 rtx_insn *curr;
3072d30e 1419 int success_in_block = 0;
1420
1421 if (dump_file)
1422 fprintf (dump_file, "\n\nstarting bb %d\n", bb->index);
1423
1424 FOR_BB_INSNS_REVERSE_SAFE (bb, insn, curr)
1425 {
3072d30e 1426 bool insn_is_add_or_inc = true;
1427
9845d120 1428 if (!NONDEBUG_INSN_P (insn))
f21023fe 1429 {
1430 if (DEBUG_BIND_INSN_P (insn))
1431 {
1432 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
1433 df_ref use;
1434
1435 if (dump_file)
1436 dump_insn_slim (dump_file, insn);
1437
1438 FOR_EACH_INSN_INFO_USE (use, insn_info)
1439 reg_next_debug_use[DF_REF_REGNO (use)] = insn;
1440 }
1441 continue;
1442 }
3072d30e 1443
1444 /* This continue is deliberate. We do not want the uses of the
48e1416a 1445 jump put into reg_next_use because it is not considered safe to
3072d30e 1446 combine a preincrement with a jump. */
1447 if (JUMP_P (insn))
1448 continue;
1449
1450 if (dump_file)
1451 dump_insn_slim (dump_file, insn);
1452
1453 /* Does this instruction increment or decrement a register? */
1454 if (parse_add_or_inc (insn, true))
1455 {
1456 int regno = REGNO (inc_insn.reg_res);
1457 /* Cannot handle case where there are three separate regs
1458 before a mem ref. Too many moves would be needed to be
1459 profitable. */
1460 if ((inc_insn.form == FORM_PRE_INC) || inc_insn.reg1_is_const)
1461 {
1462 mem_insn.insn = get_next_ref (regno, bb, reg_next_use);
1463 if (mem_insn.insn)
1464 {
1465 bool ok = true;
1466 if (!inc_insn.reg1_is_const)
1467 {
1468 /* We are only here if we are going to try a
1469 HAVE_*_MODIFY_REG type transformation. c is a
1470 reg and we must sure that the path from the
1471 inc_insn to the mem_insn.insn is both def and use
1472 clear of c because the inc insn is going to move
1473 into the mem_insn.insn. */
1474 int luid = DF_INSN_LUID (mem_insn.insn);
4db65708 1475 rtx_insn *other_insn
3072d30e 1476 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
48e1416a 1477
3072d30e 1478 if (other_insn && luid > DF_INSN_LUID (other_insn))
1479 ok = false;
48e1416a 1480
1481 other_insn
3072d30e 1482 = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
48e1416a 1483
3072d30e 1484 if (other_insn && luid > DF_INSN_LUID (other_insn))
1485 ok = false;
1486 }
48e1416a 1487
3072d30e 1488 if (dump_file)
1489 dump_inc_insn (dump_file);
48e1416a 1490
d4ce94c1 1491 if (ok && find_address (&PATTERN (mem_insn.insn),
1492 inc_insn.reg_res) == -1)
1493 {
1494 if (dump_file)
1495 dump_mem_insn (dump_file);
1496 if (try_merge ())
1497 {
1498 success_in_block++;
1499 insn_is_add_or_inc = false;
1500 }
1501 }
1502 }
1503
1504 if (insn_is_add_or_inc
1505 /* find_address will only recognize an address
1506 with a reg0 that's not reg_res when
1507 reg1_is_const, so cut it off early if we
1508 already know it won't match. */
1509 && inc_insn.reg1_is_const
1510 && inc_insn.reg0
1511 && inc_insn.reg0 != inc_insn.reg_res)
1512 {
1513 /* If we identified an inc_insn that uses two
1514 different pseudos, it's of the form
1515
1516 (set reg_res (plus reg0 reg1))
1517
1518 where reg1 is a constant (*).
1519
f21023fe 1520 The next use of reg_res was not identified by
d4ce94c1 1521 find_address as a mem_insn that we could turn
1522 into auto-inc, so see if we find a suitable
1523 MEM in the next use of reg0, as long as it's
1524 before any subsequent use of reg_res:
1525
1526 ... (mem (... reg0 ...)) ...
1527
1528 ... reg_res ...
1529
1530 In this case, we can turn the plus into a
1531 copy, and the reg0 in the MEM address into a
1532 post_inc of reg_res:
1533
1534 (set reg_res reg0)
1535
1536 ... (mem (... (post_add reg_res reg1) ...)) ...
1537
1538 reg_res will then have the correct value at
1539 subsequent uses, and reg0 will remain
1540 unchanged.
1541
1542 (*) We could support non-const reg1, but then
1543 we'd have to check that reg1 remains
1544 unchanged all the way to the modified MEM,
1545 and we'd have to extend find_address to
1546 represent a non-const negated reg1. */
1547 regno = REGNO (inc_insn.reg0);
1548 rtx_insn *reg0_use = get_next_ref (regno, bb,
1549 reg_next_use);
1550
1551 /* Give up if the next use of reg0 is after the next
1552 use of reg_res (same insn is ok; we might have
1553 found a MEM with reg_res before, and that failed,
1554 but now we try reg0, which might work), or defs
1555 of reg_res (same insn is not ok, we'd introduce
1556 another def in the same insn) or reg0. */
1557 if (reg0_use)
1558 {
1559 int luid = DF_INSN_LUID (reg0_use);
1560
1561 /* It might seem pointless to introduce an
1562 auto-inc if there's no subsequent use of
1563 reg_res (i.e., mem_insn.insn == NULL), but
1564 the next use might be in the next iteration
1565 of a loop, and it won't hurt if we make the
1566 change even if it's not needed. */
1567 if (mem_insn.insn
1568 && luid > DF_INSN_LUID (mem_insn.insn))
1569 reg0_use = NULL;
1570
1571 rtx_insn *other_insn
1572 = get_next_ref (REGNO (inc_insn.reg_res), bb,
1573 reg_next_def);
1574
1575 if (other_insn && luid >= DF_INSN_LUID (other_insn))
1576 reg0_use = NULL;
1577
1578 other_insn
1579 = get_next_ref (REGNO (inc_insn.reg0), bb,
1580 reg_next_def);
1581
1582 if (other_insn && luid > DF_INSN_LUID (other_insn))
1583 reg0_use = NULL;
1584 }
1585
1586 mem_insn.insn = reg0_use;
1587
1588 if (mem_insn.insn
1589 && find_address (&PATTERN (mem_insn.insn),
1590 inc_insn.reg0) == -1)
3072d30e 1591 {
1592 if (dump_file)
1593 dump_mem_insn (dump_file);
1594 if (try_merge ())
1595 {
1596 success_in_block++;
1597 insn_is_add_or_inc = false;
1598 }
1599 }
1600 }
1601 }
1602 }
1603 else
1604 {
1605 insn_is_add_or_inc = false;
1606 mem_insn.insn = insn;
1607 if (find_mem (&PATTERN (insn)))
1608 success_in_block++;
1609 }
48e1416a 1610
3072d30e 1611 /* If the inc insn was merged with a mem, the inc insn is gone
1612 and there is noting to update. */
e8403abd 1613 if (df_insn_info *insn_info = DF_INSN_INFO_GET (insn))
3072d30e 1614 {
be10bb5a 1615 df_ref def, use;
1616
3072d30e 1617 /* Need to update next use. */
be10bb5a 1618 FOR_EACH_INSN_INFO_DEF (def, insn_info)
3072d30e 1619 {
f21023fe 1620 if (reg_next_debug_use)
1621 reg_next_debug_use[DF_REF_REGNO (def)] = NULL;
3072d30e 1622 reg_next_use[DF_REF_REGNO (def)] = NULL;
1623 reg_next_inc_use[DF_REF_REGNO (def)] = NULL;
1624 reg_next_def[DF_REF_REGNO (def)] = insn;
1625 }
48e1416a 1626
be10bb5a 1627 FOR_EACH_INSN_INFO_USE (use, insn_info)
3072d30e 1628 {
f21023fe 1629 if (reg_next_debug_use)
1630 /* This may seem surprising, but we know we may only
1631 modify the value of a REG between an insn and the
1632 next nondebug use thereof. Any debug uses after
1633 the next nondebug use can be left alone, the REG
1634 will hold the expected value there. */
1635 reg_next_debug_use[DF_REF_REGNO (use)] = NULL;
3072d30e 1636 reg_next_use[DF_REF_REGNO (use)] = insn;
1637 if (insn_is_add_or_inc)
1638 reg_next_inc_use[DF_REF_REGNO (use)] = insn;
1639 else
1640 reg_next_inc_use[DF_REF_REGNO (use)] = NULL;
48e1416a 1641 }
3072d30e 1642 }
1643 else if (dump_file)
e8403abd 1644 fprintf (dump_file, "skipping update of deleted insn %d\n",
1645 INSN_UID (insn));
3072d30e 1646 }
1647
1648 /* If we were successful, try again. There may have been several
1649 opportunities that were interleaved. This is rare but
1650 gcc.c-torture/compile/pr17273.c actually exhibits this. */
1651 if (success_in_block)
1652 {
1653 /* In this case, we must clear these vectors since the trick of
1654 testing if the stale insn in the block will not work. */
f21023fe 1655 if (reg_next_debug_use)
1656 memset (reg_next_debug_use, 0, max_reg * sizeof (rtx));
9af5ce0c 1657 memset (reg_next_use, 0, max_reg * sizeof (rtx));
1658 memset (reg_next_inc_use, 0, max_reg * sizeof (rtx));
1659 memset (reg_next_def, 0, max_reg * sizeof (rtx));
3072d30e 1660 df_recompute_luids (bb);
1661 merge_in_block (max_reg, bb);
1662 }
1663}
1664
3072d30e 1665/* Discover auto-inc auto-dec instructions. */
1666
cbe8bda8 1667namespace {
1668
1669const pass_data pass_data_inc_dec =
3072d30e 1670{
cbe8bda8 1671 RTL_PASS, /* type */
1672 "auto_inc_dec", /* name */
1673 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 1674 TV_AUTO_INC_DEC, /* tv_id */
1675 0, /* properties_required */
1676 0, /* properties_provided */
1677 0, /* properties_destroyed */
1678 0, /* todo_flags_start */
1679 TODO_df_finish, /* todo_flags_finish */
3072d30e 1680};
cbe8bda8 1681
1682class pass_inc_dec : public rtl_opt_pass
1683{
1684public:
9af5ce0c 1685 pass_inc_dec (gcc::context *ctxt)
1686 : rtl_opt_pass (pass_data_inc_dec, ctxt)
cbe8bda8 1687 {}
1688
1689 /* opt_pass methods: */
31315c24 1690 virtual bool gate (function *)
1691 {
32aa77d9 1692 if (!AUTO_INC_DEC)
1693 return false;
1694
31315c24 1695 return (optimize > 0 && flag_auto_inc_dec);
31315c24 1696 }
1697
1698
65b0537f 1699 unsigned int execute (function *);
cbe8bda8 1700
1701}; // class pass_inc_dec
1702
65b0537f 1703unsigned int
1704pass_inc_dec::execute (function *fun ATTRIBUTE_UNUSED)
1705{
32aa77d9 1706 if (!AUTO_INC_DEC)
1707 return 0;
1708
65b0537f 1709 basic_block bb;
1710 int max_reg = max_reg_num ();
1711
1712 if (!initialized)
1713 init_decision_table ();
1714
1715 mem_tmp = gen_rtx_MEM (Pmode, NULL_RTX);
1716
1717 df_note_add_problem ();
1718 df_analyze ();
1719
f21023fe 1720 if (MAY_HAVE_DEBUG_BIND_INSNS)
1721 reg_next_debug_use = XCNEWVEC (rtx_insn *, max_reg);
1722 else
1723 /* An earlier function may have had debug binds. */
1724 reg_next_debug_use = NULL;
4db65708 1725 reg_next_use = XCNEWVEC (rtx_insn *, max_reg);
1726 reg_next_inc_use = XCNEWVEC (rtx_insn *, max_reg);
1727 reg_next_def = XCNEWVEC (rtx_insn *, max_reg);
65b0537f 1728 FOR_EACH_BB_FN (bb, fun)
1729 merge_in_block (max_reg, bb);
1730
f21023fe 1731 free (reg_next_debug_use);
65b0537f 1732 free (reg_next_use);
1733 free (reg_next_inc_use);
1734 free (reg_next_def);
1735
1736 mem_tmp = NULL;
32aa77d9 1737
65b0537f 1738 return 0;
1739}
1740
cbe8bda8 1741} // anon namespace
1742
1743rtl_opt_pass *
1744make_pass_inc_dec (gcc::context *ctxt)
1745{
1746 return new pass_inc_dec (ctxt);
1747}