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