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