]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/genpeep.c
gensupport.c (init_rtx_reader_args_cb): Start counting code generating patterns from...
[thirdparty/gcc.git] / gcc / genpeep.c
CommitLineData
32f30c53 1/* Generate code from machine description to perform peephole optimizations.
9dcd6f09 2 Copyright (C) 1987, 1989, 1992, 1997, 1998, 1999, 2000, 2003, 2004,
e714561a 3 2007, 2010, 2012 Free Software Foundation, Inc.
32f30c53 4
1322177d 5This file is part of GCC.
32f30c53 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
1322177d 10version.
32f30c53 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
32f30c53
TW
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
32f30c53
TW
20
21
4977bab6 22#include "bconfig.h"
0b93b64e 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
32f30c53 26#include "rtl.h"
f8b6598e 27#include "errors.h"
c88c0d42 28#include "gensupport.h"
32f30c53 29
32f30c53 30
32f30c53
TW
31/* While tree-walking an instruction pattern, we keep a chain
32 of these `struct link's to record how to get down to the
33 current position. In each one, POS is the operand number,
34 and if the operand is a vector VEC is the element number.
35 VEC is -1 if the operand is not a vector. */
36
37struct link
38{
39 struct link *next;
40 int pos;
41 int vecelt;
42};
43
32f30c53
TW
44static int max_opno;
45
46/* Number of operands used in current peephole definition. */
47
48static int n_operands;
49
e714561a 50static void gen_peephole (rtx, int);
3d7aafde
AJ
51static void match_rtx (rtx, struct link *, int);
52static void print_path (struct link *);
53static void print_code (RTX_CODE);
32f30c53
TW
54\f
55static void
e714561a 56gen_peephole (rtx peep, int insn_code_number)
32f30c53
TW
57{
58 int ninsns = XVECLEN (peep, 0);
59 int i;
60
61 n_operands = 0;
62
63 printf (" insn = ins1;\n");
32f30c53
TW
64
65 for (i = 0; i < ninsns; i++)
66 {
67 if (i > 0)
68 {
69 printf (" do { insn = NEXT_INSN (insn);\n");
70 printf (" if (insn == 0) goto L%d; }\n",
71 insn_code_number);
4b4bf941
JQ
72 printf (" while (NOTE_P (insn)\n");
73 printf ("\t || (NONJUMP_INSN_P (insn)\n");
32f30c53
TW
74 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
75 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
76
4b4bf941
JQ
77 printf (" if (LABEL_P (insn)\n\
78 || BARRIER_P (insn))\n goto L%d;\n",
3d7aafde 79 insn_code_number);
32f30c53
TW
80 }
81
32f30c53
TW
82 printf (" pat = PATTERN (insn);\n");
83
84 /* Walk the insn's pattern, remembering at all times the path
85 down to the walking point. */
86
9714cf43 87 match_rtx (XVECEXP (peep, 0, i), NULL, insn_code_number);
32f30c53
TW
88 }
89
90 /* We get this far if the pattern matches.
91 Now test the extra condition. */
92
93 if (XSTR (peep, 1) && XSTR (peep, 1)[0])
94 printf (" if (! (%s)) goto L%d;\n",
95 XSTR (peep, 1), insn_code_number);
96
97 /* If that matches, construct new pattern and put it in the first insn.
98 This new pattern will never be matched.
99 It exists only so that insn-extract can get the operands back.
100 So use a simple regular form: a PARALLEL containing a vector
101 of all the operands. */
102
38a448ca 103 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
32f30c53 104
32f30c53
TW
105 /* Record this define_peephole's insn code in the insn,
106 as if it had been recognized to match this. */
107 printf (" INSN_CODE (ins1) = %d;\n",
108 insn_code_number);
109
110 /* Delete the remaining insns. */
111 if (ninsns > 1)
112 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
113
114 /* See reload1.c for insertion of NOTE which guarantees that this
115 cannot be zero. */
116 printf (" return NEXT_INSN (insn);\n");
117
118 printf (" L%d:\n\n", insn_code_number);
119}
120\f
121static void
3d7aafde 122match_rtx (rtx x, struct link *path, int fail_label)
32f30c53 123{
b3694847
SS
124 RTX_CODE code;
125 int i;
126 int len;
127 const char *fmt;
32f30c53
TW
128 struct link link;
129
130 if (x == 0)
131 return;
132
133
134 code = GET_CODE (x);
135
136 switch (code)
137 {
138 case MATCH_OPERAND:
139 if (XINT (x, 0) > max_opno)
140 max_opno = XINT (x, 0);
141 if (XINT (x, 0) >= n_operands)
142 n_operands = 1 + XINT (x, 0);
143
144 printf (" x = ");
145 print_path (path);
146 printf (";\n");
147
148 printf (" operands[%d] = x;\n", XINT (x, 0));
149 if (XSTR (x, 1) && XSTR (x, 1)[0])
150 printf (" if (! %s (x, %smode)) goto L%d;\n",
151 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
152 return;
153
154 case MATCH_DUP:
693fb02f 155 case MATCH_PAR_DUP:
32f30c53
TW
156 printf (" x = ");
157 print_path (path);
158 printf (";\n");
159
160 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
161 XINT (x, 0), fail_label);
162 return;
163
164 case MATCH_OP_DUP:
165 printf (" x = ");
166 print_path (path);
167 printf (";\n");
168
169 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
170 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
171 XINT (x, 0), fail_label);
172 printf (" operands[%d] = x;\n", XINT (x, 0));
173 link.next = path;
174 link.vecelt = -1;
175 for (i = 0; i < XVECLEN (x, 1); i++)
176 {
177 link.pos = i;
178 match_rtx (XVECEXP (x, 1, i), &link, fail_label);
179 }
180 return;
181
182 case MATCH_OPERATOR:
183 if (XINT (x, 0) > max_opno)
184 max_opno = XINT (x, 0);
185 if (XINT (x, 0) >= n_operands)
186 n_operands = 1 + XINT (x, 0);
187
188 printf (" x = ");
189 print_path (path);
190 printf (";\n");
191
192 printf (" operands[%d] = x;\n", XINT (x, 0));
193 if (XSTR (x, 1) && XSTR (x, 1)[0])
194 printf (" if (! %s (x, %smode)) goto L%d;\n",
195 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
196 link.next = path;
197 link.vecelt = -1;
198 for (i = 0; i < XVECLEN (x, 2); i++)
199 {
200 link.pos = i;
201 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
202 }
203 return;
204
205 case MATCH_PARALLEL:
206 if (XINT (x, 0) > max_opno)
207 max_opno = XINT (x, 0);
208 if (XINT (x, 0) >= n_operands)
209 n_operands = 1 + XINT (x, 0);
210
211 printf (" x = ");
212 print_path (path);
213 printf (";\n");
214
215 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
216 printf (" operands[%d] = x;\n", XINT (x, 0));
217 if (XSTR (x, 1) && XSTR (x, 1)[0])
218 printf (" if (! %s (x, %smode)) goto L%d;\n",
219 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
220 link.next = path;
221 link.pos = 0;
222 for (i = 0; i < XVECLEN (x, 2); i++)
223 {
224 link.vecelt = i;
225 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
226 }
227 return;
228
38a448ca
RH
229 default:
230 break;
32f30c53
TW
231 }
232
233 printf (" x = ");
234 print_path (path);
235 printf (";\n");
236
237 printf (" if (GET_CODE (x) != ");
238 print_code (code);
239 printf (") goto L%d;\n", fail_label);
240
241 if (GET_MODE (x) != VOIDmode)
242 {
243 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
244 GET_MODE_NAME (GET_MODE (x)), fail_label);
245 }
246
247 link.next = path;
248 link.vecelt = -1;
249 fmt = GET_RTX_FORMAT (code);
250 len = GET_RTX_LENGTH (code);
251 for (i = 0; i < len; i++)
252 {
253 link.pos = i;
254 if (fmt[i] == 'e' || fmt[i] == 'u')
255 match_rtx (XEXP (x, i), &link, fail_label);
256 else if (fmt[i] == 'E')
257 {
258 int j;
259 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
260 i, XVECLEN (x, i), fail_label);
4231508a 261 for (j = 0; j < XVECLEN (x, i); j++)
32f30c53
TW
262 {
263 link.vecelt = j;
264 match_rtx (XVECEXP (x, i, j), &link, fail_label);
265 }
266 }
267 else if (fmt[i] == 'i')
268 {
269 /* Make sure that at run time `x' is the RTX we want to test. */
270 if (i != 0)
271 {
272 printf (" x = ");
273 print_path (path);
274 printf (";\n");
275 }
276
277 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
278 i, XINT (x, i), fail_label);
279 }
52a5538b
RS
280 else if (fmt[i] == 'w')
281 {
282 /* Make sure that at run time `x' is the RTX we want to test. */
283 if (i != 0)
284 {
285 printf (" x = ");
286 print_path (path);
287 printf (";\n");
288 }
289
76d31c63
JL
290 printf (" if (XWINT (x, %d) != ", i);
291 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
292 printf (") goto L%d;\n", fail_label);
52a5538b 293 }
32f30c53
TW
294 else if (fmt[i] == 's')
295 {
296 /* Make sure that at run time `x' is the RTX we want to test. */
297 if (i != 0)
298 {
299 printf (" x = ");
300 print_path (path);
301 printf (";\n");
302 }
303
304 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
305 i, XSTR (x, i), fail_label);
306 }
307 }
308}
309
310/* Given a PATH, representing a path down the instruction's
311 pattern from the root to a certain point, output code to
312 evaluate to the rtx at that point. */
313
314static void
3d7aafde 315print_path (struct link *path)
32f30c53
TW
316{
317 if (path == 0)
318 printf ("pat");
319 else if (path->vecelt >= 0)
320 {
321 printf ("XVECEXP (");
322 print_path (path->next);
323 printf (", %d, %d)", path->pos, path->vecelt);
324 }
325 else
326 {
327 printf ("XEXP (");
328 print_path (path->next);
329 printf (", %d)", path->pos);
330 }
331}
332\f
333static void
3d7aafde 334print_code (RTX_CODE code)
32f30c53 335{
b3694847 336 const char *p1;
32f30c53 337 for (p1 = GET_RTX_NAME (code); *p1; p1++)
92a438d1 338 putchar (TOUPPER(*p1));
32f30c53 339}
32f30c53 340
3d7aafde 341extern int main (int, char **);
c1b59dce 342
32f30c53 343int
3d7aafde 344main (int argc, char **argv)
32f30c53
TW
345{
346 rtx desc;
32f30c53
TW
347
348 max_opno = -1;
349
f8b6598e 350 progname = "genpeep";
32f30c53 351
600ab3fc 352 if (!init_rtx_reader_args (argc, argv))
c88c0d42 353 return (FATAL_EXIT_CODE);
32f30c53 354
32f30c53
TW
355 printf ("/* Generated automatically by the program `genpeep'\n\
356from the machine description file `md'. */\n\n");
357
358 printf ("#include \"config.h\"\n");
729da3f5 359 printf ("#include \"system.h\"\n");
4977bab6
ZW
360 printf ("#include \"coretypes.h\"\n");
361 printf ("#include \"tm.h\"\n");
7f7f8214 362 printf ("#include \"insn-config.h\"\n");
32f30c53 363 printf ("#include \"rtl.h\"\n");
bd9f1972 364 printf ("#include \"tm_p.h\"\n");
32f30c53
TW
365 printf ("#include \"regs.h\"\n");
366 printf ("#include \"output.h\"\n");
7f7f8214 367 printf ("#include \"recog.h\"\n");
279bb624
DE
368 printf ("#include \"except.h\"\n");
369 printf ("#include \"function.h\"\n");
79a3f089 370 printf ("#include \"diagnostic-core.h\"\n");
ce92b223 371 printf ("#include \"flags.h\"\n");
279bb624 372 printf ("#include \"tm-constrs.h\"\n\n");
32f30c53 373
ede7cd44 374 printf ("#ifdef HAVE_peephole\n");
32f30c53
TW
375 printf ("extern rtx peep_operand[];\n\n");
376 printf ("#define operands peep_operand\n\n");
377
6906ba40 378 printf ("rtx\npeephole (rtx ins1)\n{\n");
296433e1 379 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
32f30c53
TW
380
381 /* Early out: no peepholes for insns followed by barriers. */
382 printf (" if (NEXT_INSN (ins1)\n");
4b4bf941 383 printf (" && BARRIER_P (NEXT_INSN (ins1)))\n");
32f30c53
TW
384 printf (" return 0;\n\n");
385
386 /* Read the machine description. */
387
388 while (1)
389 {
e714561a
SB
390 int line_no;
391 int insn_code_number;
c88c0d42 392
e714561a 393 desc = read_md_rtx (&line_no, &insn_code_number);
c88c0d42 394 if (desc == NULL)
32f30c53 395 break;
32f30c53 396
e714561a
SB
397 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
398 gen_peephole (desc, insn_code_number);
32f30c53
TW
399 }
400
401 printf (" return 0;\n}\n\n");
402
403 if (max_opno == -1)
404 max_opno = 1;
405
406 printf ("rtx peep_operand[%d];\n", max_opno + 1);
ede7cd44 407 printf ("#endif\n");
32f30c53
TW
408
409 fflush (stdout);
c1b59dce 410 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
32f30c53 411}