Commit | Line | Data |
---|---|---|
031bafc8 C |
1 | /* Generate code from machine description to perform peephole optimizations. |
2 | Copyright (C) 1987, 1989 Free Software Foundation, Inc. | |
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 1, 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 | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | ||
21 | #include <stdio.h> | |
22 | #include "config.h" | |
23 | #include "rtl.h" | |
24 | #include "obstack.h" | |
25 | ||
26 | struct obstack obstack; | |
27 | struct obstack *rtl_obstack = &obstack; | |
28 | ||
29 | #define obstack_chunk_alloc xmalloc | |
30 | #define obstack_chunk_free free | |
31 | extern int xmalloc (); | |
32 | extern void free (); | |
33 | ||
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 | ||
47 | void match_rtx (); | |
48 | void gen_exp (); | |
49 | void fatal (); | |
50 | void fancy_abort (); | |
51 | ||
52 | int max_opno; | |
53 | ||
54 | /* Number of operands used in current peephole definition. */ | |
55 | ||
56 | int n_operands; | |
57 | ||
58 | /* Peephole optimizations get insn codes just like insn patterns. | |
59 | Count them so we know the code of the define_peephole we are handling. */ | |
60 | ||
61 | int insn_code_number = 0; | |
62 | ||
63 | void print_path (); | |
64 | void print_code (); | |
65 | \f | |
66 | void | |
67 | gen_peephole (peep) | |
68 | rtx peep; | |
69 | { | |
70 | int ninsns = XVECLEN (peep, 0); | |
71 | int i; | |
72 | ||
73 | n_operands = 0; | |
74 | ||
75 | printf (" insn = ins1;\n"); | |
76 | #if 0 | |
77 | printf (" want_jump = 0;\n"); | |
78 | #endif | |
79 | ||
80 | for (i = 0; i < ninsns; i++) | |
81 | { | |
82 | if (i > 0) | |
83 | { | |
84 | printf (" do { insn = NEXT_INSN (insn);\n"); | |
85 | printf (" if (insn == 0) goto L%d; }\n", | |
86 | insn_code_number); | |
87 | printf (" while (GET_CODE (insn) == NOTE);\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 | ||
104 | match_rtx (XVECEXP (peep, 0, i), 0, insn_code_number); | |
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 | ||
120 | printf (" PATTERN (ins1) = gen_rtx (PARALLEL, VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands); | |
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 | 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; | |
156 | register char *fmt; | |
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: | |
184 | printf (" x = "); | |
185 | print_path (path); | |
186 | printf (";\n"); | |
187 | ||
188 | printf (" if (!rtx_equal_p (operands[%d], x)) goto L%d;\n", | |
189 | XINT (x, 0), fail_label); | |
190 | return; | |
191 | ||
192 | case MATCH_OPERATOR: | |
193 | if (XINT (x, 0) > max_opno) | |
194 | max_opno = XINT (x, 0); | |
195 | if (XINT (x, 0) >= n_operands) | |
196 | n_operands = 1 + XINT (x, 0); | |
197 | ||
198 | printf (" x = (rtx)"); | |
199 | print_path (path); | |
200 | printf (";\n"); | |
201 | ||
202 | printf (" operands[%d] = x;\n", XINT (x, 0)); | |
203 | if (XSTR (x, 1) && XSTR (x, 1)[0]) | |
204 | printf (" if (! %s (x, %smode)) goto L%d;\n", | |
205 | XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label); | |
206 | link.next = path; | |
207 | link.vecelt = -1; | |
208 | for (i = 0; i < XVECLEN (x, 2); i++) | |
209 | { | |
210 | link.pos = i; | |
211 | match_rtx (XVECEXP (x, 2, i), &link, fail_label); | |
212 | } | |
213 | return; | |
214 | ||
215 | case ADDRESS: | |
216 | match_rtx (XEXP (x, 0), path, fail_label); | |
217 | return; | |
218 | } | |
219 | ||
220 | printf (" x = "); | |
221 | print_path (path); | |
222 | printf (";\n"); | |
223 | ||
224 | printf (" if (GET_CODE (x) != "); | |
225 | print_code (code); | |
226 | printf (") goto L%d;\n", fail_label); | |
227 | ||
228 | if (GET_MODE (x) != VOIDmode) | |
229 | { | |
230 | printf (" if (GET_MODE (x) != %smode) goto L%d;\n", | |
231 | GET_MODE_NAME (GET_MODE (x)), fail_label); | |
232 | } | |
233 | ||
234 | link.next = path; | |
235 | link.vecelt = -1; | |
236 | fmt = GET_RTX_FORMAT (code); | |
237 | len = GET_RTX_LENGTH (code); | |
238 | for (i = 0; i < len; i++) | |
239 | { | |
240 | link.pos = i; | |
241 | if (fmt[i] == 'e' || fmt[i] == 'u') | |
242 | match_rtx (XEXP (x, i), &link, fail_label); | |
243 | else if (fmt[i] == 'E') | |
244 | { | |
245 | int j; | |
246 | printf (" if (XVECLEN (x, %d) != %d) goto L%d;\n", | |
247 | i, XVECLEN (x, i), fail_label); | |
248 | for (j = XVECLEN (x, i) - 1; j >= 0; j--) | |
249 | { | |
250 | link.vecelt = j; | |
251 | match_rtx (XVECEXP (x, i, j), &link, fail_label); | |
252 | } | |
253 | } | |
254 | else if (fmt[i] == 'i') | |
255 | { | |
256 | /* Make sure that at run time `x' is the RTX we want to test. */ | |
257 | if (i != 0) | |
258 | { | |
259 | printf (" x = "); | |
260 | print_path (path); | |
261 | printf (";\n"); | |
262 | } | |
263 | ||
264 | printf (" if (XINT (x, %d) != %d) goto L%d;\n", | |
265 | i, XINT (x, i), fail_label); | |
266 | } | |
267 | else if (fmt[i] == 's') | |
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 (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n", | |
278 | i, XSTR (x, i), fail_label); | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | /* Given a PATH, representing a path down the instruction's | |
284 | pattern from the root to a certain point, output code to | |
285 | evaluate to the rtx at that point. */ | |
286 | ||
287 | void | |
288 | print_path (path) | |
289 | struct link *path; | |
290 | { | |
291 | if (path == 0) | |
292 | printf ("pat"); | |
293 | else if (path->vecelt >= 0) | |
294 | { | |
295 | printf ("XVECEXP ("); | |
296 | print_path (path->next); | |
297 | printf (", %d, %d)", path->pos, path->vecelt); | |
298 | } | |
299 | else | |
300 | { | |
301 | printf ("XEXP ("); | |
302 | print_path (path->next); | |
303 | printf (", %d)", path->pos); | |
304 | } | |
305 | } | |
306 | \f | |
307 | void | |
308 | print_code (code) | |
309 | RTX_CODE code; | |
310 | { | |
311 | register char *p1; | |
312 | for (p1 = GET_RTX_NAME (code); *p1; p1++) | |
313 | { | |
314 | if (*p1 >= 'a' && *p1 <= 'z') | |
315 | putchar (*p1 + 'A' - 'a'); | |
316 | else | |
317 | putchar (*p1); | |
318 | } | |
319 | } | |
320 | \f | |
321 | int | |
322 | xmalloc (size) | |
323 | { | |
324 | register int val = malloc (size); | |
325 | ||
326 | if (val == 0) | |
327 | fatal ("virtual memory exhausted"); | |
328 | return val; | |
329 | } | |
330 | ||
331 | int | |
332 | xrealloc (ptr, size) | |
333 | char *ptr; | |
334 | int size; | |
335 | { | |
336 | int result = realloc (ptr, size); | |
337 | if (!result) | |
338 | fatal ("virtual memory exhausted"); | |
339 | return result; | |
340 | } | |
341 | ||
342 | void | |
343 | fatal (s, a1, a2) | |
344 | char *s; | |
345 | { | |
346 | fprintf (stderr, "genpeep: "); | |
347 | fprintf (stderr, s, a1, a2); | |
348 | fprintf (stderr, "\n"); | |
349 | exit (FATAL_EXIT_CODE); | |
350 | } | |
351 | ||
352 | /* More 'friendly' abort that prints the line and file. | |
353 | config.h can #define abort fancy_abort if you like that sort of thing. */ | |
354 | ||
355 | void | |
356 | fancy_abort () | |
357 | { | |
358 | fatal ("Internal gcc abort."); | |
359 | } | |
360 | \f | |
361 | int | |
362 | main (argc, argv) | |
363 | int argc; | |
364 | char **argv; | |
365 | { | |
366 | rtx desc; | |
367 | FILE *infile; | |
368 | extern rtx read_rtx (); | |
369 | register int c; | |
370 | ||
371 | max_opno = -1; | |
372 | ||
373 | obstack_init (rtl_obstack); | |
374 | ||
375 | if (argc <= 1) | |
376 | fatal ("No input file name."); | |
377 | ||
378 | infile = fopen (argv[1], "r"); | |
379 | if (infile == 0) | |
380 | { | |
381 | perror (argv[1]); | |
382 | exit (FATAL_EXIT_CODE); | |
383 | } | |
384 | ||
385 | init_rtl (); | |
386 | ||
387 | printf ("/* Generated automatically by the program `genpeep'\n\ | |
388 | from the machine description file `md'. */\n\n"); | |
389 | ||
390 | printf ("#include \"config.h\"\n"); | |
391 | printf ("#include \"rtl.h\"\n"); | |
392 | printf ("#include \"regs.h\"\n"); | |
393 | printf ("#include \"real.h\"\n\n"); | |
394 | ||
395 | printf ("extern rtx peep_operand[];\n\n"); | |
396 | printf ("#define operands peep_operand\n\n"); | |
397 | ||
398 | printf ("rtx\npeephole (ins1)\n rtx ins1;\n{\n"); | |
399 | printf (" rtx insn, x, pat;\n"); | |
400 | printf (" int i;\n\n"); | |
401 | ||
402 | /* Early out: no peepholes for insns followed by barriers. */ | |
403 | printf (" if (NEXT_INSN (ins1)\n"); | |
404 | printf (" && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n"); | |
405 | printf (" return 0;\n\n"); | |
406 | ||
407 | /* Read the machine description. */ | |
408 | ||
409 | while (1) | |
410 | { | |
411 | c = read_skip_spaces (infile); | |
412 | if (c == EOF) | |
413 | break; | |
414 | ungetc (c, infile); | |
415 | ||
416 | desc = read_rtx (infile); | |
417 | if (GET_CODE (desc) == DEFINE_PEEPHOLE) | |
418 | { | |
419 | gen_peephole (desc); | |
420 | insn_code_number++; | |
421 | } | |
422 | if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND) | |
423 | { | |
424 | insn_code_number++; | |
425 | } | |
426 | } | |
427 | ||
428 | printf (" return 0;\n}\n\n"); | |
429 | ||
430 | if (max_opno == -1) | |
431 | max_opno = 1; | |
432 | ||
433 | printf ("rtx peep_operand[%d];\n", max_opno + 1); | |
434 | ||
435 | fflush (stdout); | |
436 | exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); | |
437 | } |