]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/genpeep.c
Merge basic-improvements-branch to trunk
[thirdparty/gcc.git] / gcc / genpeep.c
1 /* Generate code from machine description to perform peephole optimizations.
2 Copyright (C) 1987, 1989, 1992, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "gensupport.h"
30
31
32 /* While tree-walking an instruction pattern, we keep a chain
33 of these `struct link's to record how to get down to the
34 current position. In each one, POS is the operand number,
35 and if the operand is a vector VEC is the element number.
36 VEC is -1 if the operand is not a vector. */
37
38 struct link
39 {
40 struct link *next;
41 int pos;
42 int vecelt;
43 };
44
45 static int max_opno;
46
47 /* Number of operands used in current peephole definition. */
48
49 static int n_operands;
50
51 /* Peephole optimizations get insn codes just like insn patterns.
52 Count them so we know the code of the define_peephole we are handling. */
53
54 static int insn_code_number = 0;
55
56 static void gen_peephole PARAMS ((rtx));
57 static void match_rtx PARAMS ((rtx, struct link *, int));
58 static void print_path PARAMS ((struct link *));
59 static void print_code PARAMS ((RTX_CODE));
60 \f
61 static void
62 gen_peephole (peep)
63 rtx peep;
64 {
65 int ninsns = XVECLEN (peep, 0);
66 int i;
67
68 n_operands = 0;
69
70 printf (" insn = ins1;\n");
71 #if 0
72 printf (" want_jump = 0;\n");
73 #endif
74
75 for (i = 0; i < ninsns; i++)
76 {
77 if (i > 0)
78 {
79 printf (" do { insn = NEXT_INSN (insn);\n");
80 printf (" if (insn == 0) goto L%d; }\n",
81 insn_code_number);
82 printf (" while (GET_CODE (insn) == NOTE\n");
83 printf ("\t || (GET_CODE (insn) == INSN\n");
84 printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n");
85 printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
86
87 printf (" if (GET_CODE (insn) == CODE_LABEL\n\
88 || GET_CODE (insn) == BARRIER)\n goto L%d;\n",
89 insn_code_number);
90 }
91
92 #if 0
93 printf (" if (GET_CODE (insn) == JUMP_INSN)\n");
94 printf (" want_jump = JUMP_LABEL (insn);\n");
95 #endif
96
97 printf (" pat = PATTERN (insn);\n");
98
99 /* Walk the insn's pattern, remembering at all times the path
100 down to the walking point. */
101
102 match_rtx (XVECEXP (peep, 0, i), NULL, insn_code_number);
103 }
104
105 /* We get this far if the pattern matches.
106 Now test the extra condition. */
107
108 if (XSTR (peep, 1) && XSTR (peep, 1)[0])
109 printf (" if (! (%s)) goto L%d;\n",
110 XSTR (peep, 1), insn_code_number);
111
112 /* If that matches, construct new pattern and put it in the first insn.
113 This new pattern will never be matched.
114 It exists only so that insn-extract can get the operands back.
115 So use a simple regular form: a PARALLEL containing a vector
116 of all the operands. */
117
118 printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
119
120 #if 0
121 printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
122 printf (" {\n");
123 printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
124 printf (" delete_related_insns (ins1);\n");
125 printf (" ins1 = ins2;\n");
126 printf (" }\n");
127 #endif
128
129 /* Record this define_peephole's insn code in the insn,
130 as if it had been recognized to match this. */
131 printf (" INSN_CODE (ins1) = %d;\n",
132 insn_code_number);
133
134 /* Delete the remaining insns. */
135 if (ninsns > 1)
136 printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n");
137
138 /* See reload1.c for insertion of NOTE which guarantees that this
139 cannot be zero. */
140 printf (" return NEXT_INSN (insn);\n");
141
142 printf (" L%d:\n\n", insn_code_number);
143 }
144 \f
145 static void
146 match_rtx (x, path, fail_label)
147 rtx x;
148 struct link *path;
149 int fail_label;
150 {
151 RTX_CODE code;
152 int i;
153 int len;
154 const char *fmt;
155 struct link link;
156
157 if (x == 0)
158 return;
159
160
161 code = GET_CODE (x);
162
163 switch (code)
164 {
165 case MATCH_OPERAND:
166 if (XINT (x, 0) > max_opno)
167 max_opno = XINT (x, 0);
168 if (XINT (x, 0) >= n_operands)
169 n_operands = 1 + XINT (x, 0);
170
171 printf (" x = ");
172 print_path (path);
173 printf (";\n");
174
175 printf (" operands[%d] = x;\n", XINT (x, 0));
176 if (XSTR (x, 1) && XSTR (x, 1)[0])
177 printf (" if (! %s (x, %smode)) goto L%d;\n",
178 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
179 return;
180
181 case MATCH_DUP:
182 case MATCH_PAR_DUP:
183 printf (" x = ");
184 print_path (path);
185 printf (";\n");
186
187 printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
188 XINT (x, 0), fail_label);
189 return;
190
191 case MATCH_OP_DUP:
192 printf (" x = ");
193 print_path (path);
194 printf (";\n");
195
196 printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
197 printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
198 XINT (x, 0), fail_label);
199 printf (" operands[%d] = x;\n", XINT (x, 0));
200 link.next = path;
201 link.vecelt = -1;
202 for (i = 0; i < XVECLEN (x, 1); i++)
203 {
204 link.pos = i;
205 match_rtx (XVECEXP (x, 1, i), &link, fail_label);
206 }
207 return;
208
209 case MATCH_OPERATOR:
210 if (XINT (x, 0) > max_opno)
211 max_opno = XINT (x, 0);
212 if (XINT (x, 0) >= n_operands)
213 n_operands = 1 + XINT (x, 0);
214
215 printf (" x = ");
216 print_path (path);
217 printf (";\n");
218
219 printf (" operands[%d] = x;\n", XINT (x, 0));
220 if (XSTR (x, 1) && XSTR (x, 1)[0])
221 printf (" if (! %s (x, %smode)) goto L%d;\n",
222 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
223 link.next = path;
224 link.vecelt = -1;
225 for (i = 0; i < XVECLEN (x, 2); i++)
226 {
227 link.pos = i;
228 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
229 }
230 return;
231
232 case MATCH_PARALLEL:
233 if (XINT (x, 0) > max_opno)
234 max_opno = XINT (x, 0);
235 if (XINT (x, 0) >= n_operands)
236 n_operands = 1 + XINT (x, 0);
237
238 printf (" x = ");
239 print_path (path);
240 printf (";\n");
241
242 printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
243 printf (" operands[%d] = x;\n", XINT (x, 0));
244 if (XSTR (x, 1) && XSTR (x, 1)[0])
245 printf (" if (! %s (x, %smode)) goto L%d;\n",
246 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
247 link.next = path;
248 link.pos = 0;
249 for (i = 0; i < XVECLEN (x, 2); i++)
250 {
251 link.vecelt = i;
252 match_rtx (XVECEXP (x, 2, i), &link, fail_label);
253 }
254 return;
255
256 case ADDRESS:
257 match_rtx (XEXP (x, 0), path, fail_label);
258 return;
259
260 default:
261 break;
262 }
263
264 printf (" x = ");
265 print_path (path);
266 printf (";\n");
267
268 printf (" if (GET_CODE (x) != ");
269 print_code (code);
270 printf (") goto L%d;\n", fail_label);
271
272 if (GET_MODE (x) != VOIDmode)
273 {
274 printf (" if (GET_MODE (x) != %smode) goto L%d;\n",
275 GET_MODE_NAME (GET_MODE (x)), fail_label);
276 }
277
278 link.next = path;
279 link.vecelt = -1;
280 fmt = GET_RTX_FORMAT (code);
281 len = GET_RTX_LENGTH (code);
282 for (i = 0; i < len; i++)
283 {
284 link.pos = i;
285 if (fmt[i] == 'e' || fmt[i] == 'u')
286 match_rtx (XEXP (x, i), &link, fail_label);
287 else if (fmt[i] == 'E')
288 {
289 int j;
290 printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n",
291 i, XVECLEN (x, i), fail_label);
292 for (j = 0; j < XVECLEN (x, i); j++)
293 {
294 link.vecelt = j;
295 match_rtx (XVECEXP (x, i, j), &link, fail_label);
296 }
297 }
298 else if (fmt[i] == 'i')
299 {
300 /* Make sure that at run time `x' is the RTX we want to test. */
301 if (i != 0)
302 {
303 printf (" x = ");
304 print_path (path);
305 printf (";\n");
306 }
307
308 printf (" if (XINT (x, %d) != %d) goto L%d;\n",
309 i, XINT (x, i), fail_label);
310 }
311 else if (fmt[i] == 'w')
312 {
313 /* Make sure that at run time `x' is the RTX we want to test. */
314 if (i != 0)
315 {
316 printf (" x = ");
317 print_path (path);
318 printf (";\n");
319 }
320
321 printf (" if (XWINT (x, %d) != ", i);
322 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
323 printf (") goto L%d;\n", fail_label);
324 }
325 else if (fmt[i] == 's')
326 {
327 /* Make sure that at run time `x' is the RTX we want to test. */
328 if (i != 0)
329 {
330 printf (" x = ");
331 print_path (path);
332 printf (";\n");
333 }
334
335 printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
336 i, XSTR (x, i), fail_label);
337 }
338 }
339 }
340
341 /* Given a PATH, representing a path down the instruction's
342 pattern from the root to a certain point, output code to
343 evaluate to the rtx at that point. */
344
345 static void
346 print_path (path)
347 struct link *path;
348 {
349 if (path == 0)
350 printf ("pat");
351 else if (path->vecelt >= 0)
352 {
353 printf ("XVECEXP (");
354 print_path (path->next);
355 printf (", %d, %d)", path->pos, path->vecelt);
356 }
357 else
358 {
359 printf ("XEXP (");
360 print_path (path->next);
361 printf (", %d)", path->pos);
362 }
363 }
364 \f
365 static void
366 print_code (code)
367 RTX_CODE code;
368 {
369 const char *p1;
370 for (p1 = GET_RTX_NAME (code); *p1; p1++)
371 putchar (TOUPPER(*p1));
372 }
373
374 extern int main PARAMS ((int, char **));
375
376 int
377 main (argc, argv)
378 int argc;
379 char **argv;
380 {
381 rtx desc;
382
383 max_opno = -1;
384
385 progname = "genpeep";
386
387 if (argc <= 1)
388 fatal ("no input file name");
389
390 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
391 return (FATAL_EXIT_CODE);
392
393 printf ("/* Generated automatically by the program `genpeep'\n\
394 from the machine description file `md'. */\n\n");
395
396 printf ("#include \"config.h\"\n");
397 printf ("#include \"system.h\"\n");
398 printf ("#include \"coretypes.h\"\n");
399 printf ("#include \"tm.h\"\n");
400 printf ("#include \"insn-config.h\"\n");
401 printf ("#include \"rtl.h\"\n");
402 printf ("#include \"tm_p.h\"\n");
403 printf ("#include \"regs.h\"\n");
404 printf ("#include \"output.h\"\n");
405 printf ("#include \"real.h\"\n");
406 printf ("#include \"recog.h\"\n");
407 printf ("#include \"except.h\"\n\n");
408 printf ("#include \"function.h\"\n\n");
409
410 printf ("#ifdef HAVE_peephole\n");
411 printf ("extern rtx peep_operand[];\n\n");
412 printf ("#define operands peep_operand\n\n");
413
414 printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n");
415 printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n");
416
417 /* Early out: no peepholes for insns followed by barriers. */
418 printf (" if (NEXT_INSN (ins1)\n");
419 printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
420 printf (" return 0;\n\n");
421
422 /* Read the machine description. */
423
424 while (1)
425 {
426 int line_no, rtx_number = 0;
427
428 desc = read_md_rtx (&line_no, &rtx_number);
429 if (desc == NULL)
430 break;
431
432 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
433 {
434 gen_peephole (desc);
435 insn_code_number++;
436 }
437 if (GET_CODE (desc) == DEFINE_INSN
438 || GET_CODE (desc) == DEFINE_EXPAND
439 || GET_CODE (desc) == DEFINE_SPLIT
440 || GET_CODE (desc) == DEFINE_PEEPHOLE2)
441 {
442 insn_code_number++;
443 }
444 }
445
446 printf (" return 0;\n}\n\n");
447
448 if (max_opno == -1)
449 max_opno = 1;
450
451 printf ("rtx peep_operand[%d];\n", max_opno + 1);
452 printf ("#endif\n");
453
454 fflush (stdout);
455 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
456 }
457
458 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
459 const char *
460 get_insn_name (code)
461 int code ATTRIBUTE_UNUSED;
462 {
463 return NULL;
464 }