]>
Commit | Line | Data |
---|---|---|
32f30c53 | 1 | /* Generate code from machine description to perform peephole optimizations. |
34627ce6 | 2 | Copyright (C) 1987, 89, 92, 97, 98, 1999 Free Software Foundation, Inc. |
32f30c53 TW |
3 | |
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
a35311b0 RK |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
32f30c53 TW |
20 | |
21 | ||
20f92396 | 22 | #include "hconfig.h" |
0b93b64e | 23 | #include "system.h" |
32f30c53 TW |
24 | #include "rtl.h" |
25 | #include "obstack.h" | |
f8b6598e | 26 | #include "errors.h" |
32f30c53 TW |
27 | |
28 | static struct obstack obstack; | |
29 | struct obstack *rtl_obstack = &obstack; | |
30 | ||
31 | #define obstack_chunk_alloc xmalloc | |
32 | #define obstack_chunk_free free | |
33 | ||
32f30c53 TW |
34 | /* While tree-walking an instruction pattern, we keep a chain |
35 | of these `struct link's to record how to get down to the | |
36 | current position. In each one, POS is the operand number, | |
37 | and if the operand is a vector VEC is the element number. | |
38 | VEC is -1 if the operand is not a vector. */ | |
39 | ||
40 | struct link | |
41 | { | |
42 | struct link *next; | |
43 | int pos; | |
44 | int vecelt; | |
45 | }; | |
46 | ||
32f30c53 TW |
47 | static int max_opno; |
48 | ||
49 | /* Number of operands used in current peephole definition. */ | |
50 | ||
51 | static int n_operands; | |
52 | ||
53 | /* Peephole optimizations get insn codes just like insn patterns. | |
54 | Count them so we know the code of the define_peephole we are handling. */ | |
55 | ||
56 | static int insn_code_number = 0; | |
57 | ||
56c0e996 BS |
58 | static void gen_peephole PROTO((rtx)); |
59 | static void match_rtx PROTO((rtx, struct link *, int)); | |
0b93b64e KG |
60 | static void print_path PROTO((struct link *)); |
61 | static void print_code PROTO((RTX_CODE)); | |
32f30c53 TW |
62 | \f |
63 | static void | |
64 | gen_peephole (peep) | |
65 | rtx peep; | |
66 | { | |
67 | int ninsns = XVECLEN (peep, 0); | |
68 | int i; | |
69 | ||
70 | n_operands = 0; | |
71 | ||
72 | printf (" insn = ins1;\n"); | |
73 | #if 0 | |
74 | printf (" want_jump = 0;\n"); | |
75 | #endif | |
76 | ||
77 | for (i = 0; i < ninsns; i++) | |
78 | { | |
79 | if (i > 0) | |
80 | { | |
81 | printf (" do { insn = NEXT_INSN (insn);\n"); | |
82 | printf (" if (insn == 0) goto L%d; }\n", | |
83 | insn_code_number); | |
84 | printf (" while (GET_CODE (insn) == NOTE\n"); | |
85 | printf ("\t || (GET_CODE (insn) == INSN\n"); | |
86 | printf ("\t && (GET_CODE (PATTERN (insn)) == USE\n"); | |
87 | printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n"); | |
88 | ||
89 | printf (" if (GET_CODE (insn) == CODE_LABEL\n\ | |
90 | || GET_CODE (insn) == BARRIER)\n goto L%d;\n", | |
91 | insn_code_number); | |
92 | } | |
93 | ||
94 | #if 0 | |
95 | printf (" if (GET_CODE (insn) == JUMP_INSN)\n"); | |
96 | printf (" want_jump = JUMP_LABEL (insn);\n"); | |
97 | #endif | |
98 | ||
99 | printf (" pat = PATTERN (insn);\n"); | |
100 | ||
101 | /* Walk the insn's pattern, remembering at all times the path | |
102 | down to the walking point. */ | |
103 | ||
5f4f0e22 | 104 | match_rtx (XVECEXP (peep, 0, i), NULL_PTR, insn_code_number); |
32f30c53 TW |
105 | } |
106 | ||
107 | /* We get this far if the pattern matches. | |
108 | Now test the extra condition. */ | |
109 | ||
110 | if (XSTR (peep, 1) && XSTR (peep, 1)[0]) | |
111 | printf (" if (! (%s)) goto L%d;\n", | |
112 | XSTR (peep, 1), insn_code_number); | |
113 | ||
114 | /* If that matches, construct new pattern and put it in the first insn. | |
115 | This new pattern will never be matched. | |
116 | It exists only so that insn-extract can get the operands back. | |
117 | So use a simple regular form: a PARALLEL containing a vector | |
118 | of all the operands. */ | |
119 | ||
38a448ca | 120 | printf (" PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands); |
32f30c53 TW |
121 | |
122 | #if 0 | |
123 | printf (" if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n"); | |
124 | printf (" {\n"); | |
125 | printf (" rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n"); | |
126 | printf (" delete_insn (ins1);\n"); | |
127 | printf (" ins1 = ins2;\n"); | |
128 | printf (" }\n"); | |
129 | #endif | |
130 | ||
131 | /* Record this define_peephole's insn code in the insn, | |
132 | as if it had been recognized to match this. */ | |
133 | printf (" INSN_CODE (ins1) = %d;\n", | |
134 | insn_code_number); | |
135 | ||
136 | /* Delete the remaining insns. */ | |
137 | if (ninsns > 1) | |
138 | printf (" delete_for_peephole (NEXT_INSN (ins1), insn);\n"); | |
139 | ||
140 | /* See reload1.c for insertion of NOTE which guarantees that this | |
141 | cannot be zero. */ | |
142 | printf (" return NEXT_INSN (insn);\n"); | |
143 | ||
144 | printf (" L%d:\n\n", insn_code_number); | |
145 | } | |
146 | \f | |
147 | static void | |
148 | match_rtx (x, path, fail_label) | |
149 | rtx x; | |
150 | struct link *path; | |
151 | int fail_label; | |
152 | { | |
153 | register RTX_CODE code; | |
154 | register int i; | |
155 | register int len; | |
6f7d635c | 156 | register const char *fmt; |
32f30c53 TW |
157 | struct link link; |
158 | ||
159 | if (x == 0) | |
160 | return; | |
161 | ||
162 | ||
163 | code = GET_CODE (x); | |
164 | ||
165 | switch (code) | |
166 | { | |
167 | case MATCH_OPERAND: | |
168 | if (XINT (x, 0) > max_opno) | |
169 | max_opno = XINT (x, 0); | |
170 | if (XINT (x, 0) >= n_operands) | |
171 | n_operands = 1 + XINT (x, 0); | |
172 | ||
173 | printf (" x = "); | |
174 | print_path (path); | |
175 | printf (";\n"); | |
176 | ||
177 | printf (" operands[%d] = x;\n", XINT (x, 0)); | |
178 | if (XSTR (x, 1) && XSTR (x, 1)[0]) | |
179 | printf (" if (! %s (x, %smode)) goto L%d;\n", | |
180 | XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label); | |
181 | return; | |
182 | ||
183 | case MATCH_DUP: | |
693fb02f | 184 | case MATCH_PAR_DUP: |
32f30c53 TW |
185 | printf (" x = "); |
186 | print_path (path); | |
187 | printf (";\n"); | |
188 | ||
189 | printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n", | |
190 | XINT (x, 0), fail_label); | |
191 | return; | |
192 | ||
193 | case MATCH_OP_DUP: | |
194 | printf (" x = "); | |
195 | print_path (path); | |
196 | printf (";\n"); | |
197 | ||
198 | printf (" if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0)); | |
199 | printf (" || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n", | |
200 | XINT (x, 0), fail_label); | |
201 | printf (" operands[%d] = x;\n", XINT (x, 0)); | |
202 | link.next = path; | |
203 | link.vecelt = -1; | |
204 | for (i = 0; i < XVECLEN (x, 1); i++) | |
205 | { | |
206 | link.pos = i; | |
207 | match_rtx (XVECEXP (x, 1, i), &link, fail_label); | |
208 | } | |
209 | return; | |
210 | ||
211 | case MATCH_OPERATOR: | |
212 | if (XINT (x, 0) > max_opno) | |
213 | max_opno = XINT (x, 0); | |
214 | if (XINT (x, 0) >= n_operands) | |
215 | n_operands = 1 + XINT (x, 0); | |
216 | ||
217 | printf (" x = "); | |
218 | print_path (path); | |
219 | printf (";\n"); | |
220 | ||
221 | printf (" operands[%d] = x;\n", XINT (x, 0)); | |
222 | if (XSTR (x, 1) && XSTR (x, 1)[0]) | |
223 | printf (" if (! %s (x, %smode)) goto L%d;\n", | |
224 | XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label); | |
225 | link.next = path; | |
226 | link.vecelt = -1; | |
227 | for (i = 0; i < XVECLEN (x, 2); i++) | |
228 | { | |
229 | link.pos = i; | |
230 | match_rtx (XVECEXP (x, 2, i), &link, fail_label); | |
231 | } | |
232 | return; | |
233 | ||
234 | case MATCH_PARALLEL: | |
235 | if (XINT (x, 0) > max_opno) | |
236 | max_opno = XINT (x, 0); | |
237 | if (XINT (x, 0) >= n_operands) | |
238 | n_operands = 1 + XINT (x, 0); | |
239 | ||
240 | printf (" x = "); | |
241 | print_path (path); | |
242 | printf (";\n"); | |
243 | ||
244 | printf (" if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label); | |
245 | printf (" operands[%d] = x;\n", XINT (x, 0)); | |
246 | if (XSTR (x, 1) && XSTR (x, 1)[0]) | |
247 | printf (" if (! %s (x, %smode)) goto L%d;\n", | |
248 | XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label); | |
249 | link.next = path; | |
250 | link.pos = 0; | |
251 | for (i = 0; i < XVECLEN (x, 2); i++) | |
252 | { | |
253 | link.vecelt = i; | |
254 | match_rtx (XVECEXP (x, 2, i), &link, fail_label); | |
255 | } | |
256 | return; | |
257 | ||
258 | case ADDRESS: | |
259 | match_rtx (XEXP (x, 0), path, fail_label); | |
260 | return; | |
38a448ca RH |
261 | |
262 | default: | |
263 | break; | |
32f30c53 TW |
264 | } |
265 | ||
266 | printf (" x = "); | |
267 | print_path (path); | |
268 | printf (";\n"); | |
269 | ||
270 | printf (" if (GET_CODE (x) != "); | |
271 | print_code (code); | |
272 | printf (") goto L%d;\n", fail_label); | |
273 | ||
274 | if (GET_MODE (x) != VOIDmode) | |
275 | { | |
276 | printf (" if (GET_MODE (x) != %smode) goto L%d;\n", | |
277 | GET_MODE_NAME (GET_MODE (x)), fail_label); | |
278 | } | |
279 | ||
280 | link.next = path; | |
281 | link.vecelt = -1; | |
282 | fmt = GET_RTX_FORMAT (code); | |
283 | len = GET_RTX_LENGTH (code); | |
284 | for (i = 0; i < len; i++) | |
285 | { | |
286 | link.pos = i; | |
287 | if (fmt[i] == 'e' || fmt[i] == 'u') | |
288 | match_rtx (XEXP (x, i), &link, fail_label); | |
289 | else if (fmt[i] == 'E') | |
290 | { | |
291 | int j; | |
292 | printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n", | |
293 | i, XVECLEN (x, i), fail_label); | |
4231508a | 294 | for (j = 0; j < XVECLEN (x, i); j++) |
32f30c53 TW |
295 | { |
296 | link.vecelt = j; | |
297 | match_rtx (XVECEXP (x, i, j), &link, fail_label); | |
298 | } | |
299 | } | |
300 | else if (fmt[i] == 'i') | |
301 | { | |
302 | /* Make sure that at run time `x' is the RTX we want to test. */ | |
303 | if (i != 0) | |
304 | { | |
305 | printf (" x = "); | |
306 | print_path (path); | |
307 | printf (";\n"); | |
308 | } | |
309 | ||
310 | printf (" if (XINT (x, %d) != %d) goto L%d;\n", | |
311 | i, XINT (x, i), fail_label); | |
312 | } | |
52a5538b RS |
313 | else if (fmt[i] == 'w') |
314 | { | |
315 | /* Make sure that at run time `x' is the RTX we want to test. */ | |
316 | if (i != 0) | |
317 | { | |
318 | printf (" x = "); | |
319 | print_path (path); | |
320 | printf (";\n"); | |
321 | } | |
322 | ||
76d31c63 JL |
323 | printf (" if (XWINT (x, %d) != ", i); |
324 | printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i)); | |
325 | printf (") goto L%d;\n", fail_label); | |
52a5538b | 326 | } |
32f30c53 TW |
327 | else if (fmt[i] == 's') |
328 | { | |
329 | /* Make sure that at run time `x' is the RTX we want to test. */ | |
330 | if (i != 0) | |
331 | { | |
332 | printf (" x = "); | |
333 | print_path (path); | |
334 | printf (";\n"); | |
335 | } | |
336 | ||
337 | printf (" if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n", | |
338 | i, XSTR (x, i), fail_label); | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | /* Given a PATH, representing a path down the instruction's | |
344 | pattern from the root to a certain point, output code to | |
345 | evaluate to the rtx at that point. */ | |
346 | ||
347 | static void | |
348 | print_path (path) | |
349 | struct link *path; | |
350 | { | |
351 | if (path == 0) | |
352 | printf ("pat"); | |
353 | else if (path->vecelt >= 0) | |
354 | { | |
355 | printf ("XVECEXP ("); | |
356 | print_path (path->next); | |
357 | printf (", %d, %d)", path->pos, path->vecelt); | |
358 | } | |
359 | else | |
360 | { | |
361 | printf ("XEXP ("); | |
362 | print_path (path->next); | |
363 | printf (", %d)", path->pos); | |
364 | } | |
365 | } | |
366 | \f | |
367 | static void | |
368 | print_code (code) | |
369 | RTX_CODE code; | |
370 | { | |
5f06c983 | 371 | register const char *p1; |
32f30c53 | 372 | for (p1 = GET_RTX_NAME (code); *p1; p1++) |
92a438d1 | 373 | putchar (TOUPPER(*p1)); |
32f30c53 TW |
374 | } |
375 | \f | |
2778b98d | 376 | PTR |
32f30c53 | 377 | xmalloc (size) |
2778b98d | 378 | size_t size; |
32f30c53 | 379 | { |
2778b98d | 380 | register PTR val = (PTR) malloc (size); |
32f30c53 TW |
381 | |
382 | if (val == 0) | |
383 | fatal ("virtual memory exhausted"); | |
384 | return val; | |
385 | } | |
386 | ||
2778b98d | 387 | PTR |
470b68c0 RH |
388 | xrealloc (old, size) |
389 | PTR old; | |
2778b98d | 390 | size_t size; |
32f30c53 | 391 | { |
470b68c0 | 392 | register PTR ptr; |
09d83d25 | 393 | if (old) |
470b68c0 RH |
394 | ptr = (PTR) realloc (old, size); |
395 | else | |
396 | ptr = (PTR) malloc (size); | |
397 | if (!ptr) | |
32f30c53 | 398 | fatal ("virtual memory exhausted"); |
470b68c0 | 399 | return ptr; |
32f30c53 TW |
400 | } |
401 | ||
c1b59dce KG |
402 | extern int main PROTO ((int, char **)); |
403 | ||
32f30c53 TW |
404 | int |
405 | main (argc, argv) | |
406 | int argc; | |
407 | char **argv; | |
408 | { | |
409 | rtx desc; | |
410 | FILE *infile; | |
32f30c53 TW |
411 | register int c; |
412 | ||
413 | max_opno = -1; | |
414 | ||
f8b6598e | 415 | progname = "genpeep"; |
32f30c53 TW |
416 | obstack_init (rtl_obstack); |
417 | ||
418 | if (argc <= 1) | |
419 | fatal ("No input file name."); | |
420 | ||
421 | infile = fopen (argv[1], "r"); | |
422 | if (infile == 0) | |
423 | { | |
424 | perror (argv[1]); | |
c1b59dce | 425 | return (FATAL_EXIT_CODE); |
32f30c53 TW |
426 | } |
427 | ||
32f30c53 TW |
428 | printf ("/* Generated automatically by the program `genpeep'\n\ |
429 | from the machine description file `md'. */\n\n"); | |
430 | ||
431 | printf ("#include \"config.h\"\n"); | |
729da3f5 | 432 | printf ("#include \"system.h\"\n"); |
7f7f8214 | 433 | printf ("#include \"insn-config.h\"\n"); |
32f30c53 TW |
434 | printf ("#include \"rtl.h\"\n"); |
435 | printf ("#include \"regs.h\"\n"); | |
436 | printf ("#include \"output.h\"\n"); | |
41fe085f | 437 | printf ("#include \"real.h\"\n"); |
7f7f8214 | 438 | printf ("#include \"recog.h\"\n"); |
41fe085f | 439 | printf ("#include \"except.h\"\n\n"); |
ad171e80 | 440 | printf ("#include \"function.h\"\n\n"); |
32f30c53 | 441 | |
ede7cd44 | 442 | printf ("#ifdef HAVE_peephole\n"); |
32f30c53 TW |
443 | printf ("extern rtx peep_operand[];\n\n"); |
444 | printf ("#define operands peep_operand\n\n"); | |
445 | ||
446 | printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n"); | |
296433e1 | 447 | printf (" rtx insn ATTRIBUTE_UNUSED, x ATTRIBUTE_UNUSED, pat ATTRIBUTE_UNUSED;\n\n"); |
32f30c53 TW |
448 | |
449 | /* Early out: no peepholes for insns followed by barriers. */ | |
450 | printf (" if (NEXT_INSN (ins1)\n"); | |
451 | printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n"); | |
452 | printf (" return 0;\n\n"); | |
453 | ||
454 | /* Read the machine description. */ | |
455 | ||
456 | while (1) | |
457 | { | |
458 | c = read_skip_spaces (infile); | |
459 | if (c == EOF) | |
460 | break; | |
461 | ungetc (c, infile); | |
462 | ||
463 | desc = read_rtx (infile); | |
464 | if (GET_CODE (desc) == DEFINE_PEEPHOLE) | |
465 | { | |
466 | gen_peephole (desc); | |
467 | insn_code_number++; | |
468 | } | |
469 | if (GET_CODE (desc) == DEFINE_INSN | |
470 | || GET_CODE (desc) == DEFINE_EXPAND | |
471 | || GET_CODE (desc) == DEFINE_SPLIT) | |
472 | { | |
473 | insn_code_number++; | |
474 | } | |
475 | } | |
476 | ||
477 | printf (" return 0;\n}\n\n"); | |
478 | ||
479 | if (max_opno == -1) | |
480 | max_opno = 1; | |
481 | ||
482 | printf ("rtx peep_operand[%d];\n", max_opno + 1); | |
ede7cd44 | 483 | printf ("#endif\n"); |
32f30c53 TW |
484 | |
485 | fflush (stdout); | |
c1b59dce | 486 | return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); |
32f30c53 | 487 | } |
a995e389 RH |
488 | |
489 | /* Define this so we can link with print-rtl.o to get debug_rtx function. */ | |
490 | const char * | |
491 | get_insn_name (code) | |
c1b59dce | 492 | int code ATTRIBUTE_UNUSED; |
a995e389 RH |
493 | { |
494 | return NULL; | |
495 | } |