]>
Commit | Line | Data |
---|---|---|
3d2cf79f RB |
1 | /* Generate pattern matching and transform code shared between |
2 | GENERIC and GIMPLE folding code from match-and-simplify description. | |
3 | ||
818ab71a | 4 | Copyright (C) 2014-2016 Free Software Foundation, Inc. |
3d2cf79f RB |
5 | Contributed by Richard Biener <rguenther@suse.de> |
6 | and Prathamesh Kulkarni <bilbotheelffriend@gmail.com> | |
7 | ||
8 | This file is part of GCC. | |
9 | ||
10 | GCC is free software; you can redistribute it and/or modify it under | |
11 | the terms of the GNU General Public License as published by the Free | |
12 | Software Foundation; either version 3, or (at your option) any later | |
13 | version. | |
14 | ||
15 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
16 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 | for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with GCC; see the file COPYING3. If not see | |
22 | <http://www.gnu.org/licenses/>. */ | |
23 | ||
24 | #include "bconfig.h" | |
3d2cf79f RB |
25 | #include "system.h" |
26 | #include "coretypes.h" | |
27 | #include <cpplib.h> | |
28 | #include "errors.h" | |
3d2cf79f | 29 | #include "hash-table.h" |
1c9b0448 | 30 | #include "hash-set.h" |
3d2cf79f RB |
31 | #include "is-a.h" |
32 | ||
33 | ||
f1308e4b RB |
34 | /* Stubs for GGC referenced through instantiations triggered by hash-map. */ |
35 | void *ggc_internal_cleared_alloc (size_t, void (*)(void *), | |
e7d1000c | 36 | size_t, size_t MEM_STAT_DECL) |
f1308e4b RB |
37 | { |
38 | return NULL; | |
39 | } | |
40 | void ggc_free (void *) | |
41 | { | |
42 | } | |
43 | ||
44 | ||
53a19317 RB |
45 | /* Global state. */ |
46 | ||
47 | /* Verboseness. 0 is quiet, 1 adds some warnings, 2 is for debugging. */ | |
48 | unsigned verbose; | |
49 | ||
50 | ||
3d2cf79f RB |
51 | /* libccp helpers. */ |
52 | ||
53 | static struct line_maps *line_table; | |
54 | ||
8a645150 DM |
55 | /* The rich_location class within libcpp requires a way to expand |
56 | source_location instances, and relies on the client code | |
57 | providing a symbol named | |
58 | linemap_client_expand_location_to_spelling_point | |
59 | to do this. | |
60 | ||
61 | This is the implementation for genmatch. */ | |
62 | ||
63 | expanded_location | |
64 | linemap_client_expand_location_to_spelling_point (source_location loc) | |
65 | { | |
66 | const struct line_map_ordinary *map; | |
67 | loc = linemap_resolve_location (line_table, loc, LRK_SPELLING_LOCATION, &map); | |
68 | return linemap_expand_location (line_table, map, loc); | |
69 | } | |
70 | ||
3d2cf79f RB |
71 | static bool |
72 | #if GCC_VERSION >= 4001 | |
8a645150 | 73 | __attribute__((format (printf, 5, 0))) |
3d2cf79f | 74 | #endif |
8a645150 DM |
75 | error_cb (cpp_reader *, int errtype, int, rich_location *richloc, |
76 | const char *msg, va_list *ap) | |
3d2cf79f | 77 | { |
0e50b624 | 78 | const line_map_ordinary *map; |
8a645150 | 79 | source_location location = richloc->get_loc (); |
3d2cf79f RB |
80 | linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map); |
81 | expanded_location loc = linemap_expand_location (line_table, map, location); | |
72eb311d RB |
82 | fprintf (stderr, "%s:%d:%d %s: ", loc.file, loc.line, loc.column, |
83 | (errtype == CPP_DL_WARNING) ? "warning" : "error"); | |
3d2cf79f RB |
84 | vfprintf (stderr, msg, *ap); |
85 | fprintf (stderr, "\n"); | |
86 | FILE *f = fopen (loc.file, "r"); | |
87 | if (f) | |
88 | { | |
89 | char buf[128]; | |
90 | while (loc.line > 0) | |
91 | { | |
92 | if (!fgets (buf, 128, f)) | |
93 | goto notfound; | |
94 | if (buf[strlen (buf) - 1] != '\n') | |
95 | { | |
96 | if (loc.line > 1) | |
97 | loc.line++; | |
98 | } | |
99 | loc.line--; | |
100 | } | |
101 | fprintf (stderr, "%s", buf); | |
102 | for (int i = 0; i < loc.column - 1; ++i) | |
103 | fputc (' ', stderr); | |
104 | fputc ('^', stderr); | |
105 | fputc ('\n', stderr); | |
106 | notfound: | |
107 | fclose (f); | |
108 | } | |
72eb311d RB |
109 | |
110 | if (errtype == CPP_DL_FATAL) | |
111 | exit (1); | |
112 | return false; | |
3d2cf79f RB |
113 | } |
114 | ||
115 | static void | |
116 | #if GCC_VERSION >= 4001 | |
117 | __attribute__((format (printf, 2, 3))) | |
118 | #endif | |
119 | fatal_at (const cpp_token *tk, const char *msg, ...) | |
120 | { | |
ebedc9a3 | 121 | rich_location richloc (line_table, tk->src_loc); |
3d2cf79f RB |
122 | va_list ap; |
123 | va_start (ap, msg); | |
8a645150 | 124 | error_cb (NULL, CPP_DL_FATAL, 0, &richloc, msg, &ap); |
3d2cf79f RB |
125 | va_end (ap); |
126 | } | |
127 | ||
1c9b0448 RB |
128 | static void |
129 | #if GCC_VERSION >= 4001 | |
130 | __attribute__((format (printf, 2, 3))) | |
131 | #endif | |
132 | fatal_at (source_location loc, const char *msg, ...) | |
133 | { | |
ebedc9a3 | 134 | rich_location richloc (line_table, loc); |
1c9b0448 RB |
135 | va_list ap; |
136 | va_start (ap, msg); | |
8a645150 | 137 | error_cb (NULL, CPP_DL_FATAL, 0, &richloc, msg, &ap); |
1c9b0448 RB |
138 | va_end (ap); |
139 | } | |
140 | ||
72eb311d RB |
141 | static void |
142 | #if GCC_VERSION >= 4001 | |
143 | __attribute__((format (printf, 2, 3))) | |
144 | #endif | |
145 | warning_at (const cpp_token *tk, const char *msg, ...) | |
146 | { | |
ebedc9a3 | 147 | rich_location richloc (line_table, tk->src_loc); |
72eb311d RB |
148 | va_list ap; |
149 | va_start (ap, msg); | |
8a645150 | 150 | error_cb (NULL, CPP_DL_WARNING, 0, &richloc, msg, &ap); |
72eb311d RB |
151 | va_end (ap); |
152 | } | |
153 | ||
53a19317 RB |
154 | static void |
155 | #if GCC_VERSION >= 4001 | |
156 | __attribute__((format (printf, 2, 3))) | |
157 | #endif | |
158 | warning_at (source_location loc, const char *msg, ...) | |
159 | { | |
ebedc9a3 | 160 | rich_location richloc (line_table, loc); |
53a19317 RB |
161 | va_list ap; |
162 | va_start (ap, msg); | |
8a645150 | 163 | error_cb (NULL, CPP_DL_WARNING, 0, &richloc, msg, &ap); |
53a19317 RB |
164 | va_end (ap); |
165 | } | |
166 | ||
c551c21d MM |
167 | /* Like fprintf, but print INDENT spaces at the beginning. */ |
168 | ||
169 | static void | |
170 | #if GCC_VERSION >= 4001 | |
171 | __attribute__((format (printf, 3, 4))) | |
172 | #endif | |
173 | fprintf_indent (FILE *f, unsigned int indent, const char *format, ...) | |
174 | { | |
175 | va_list ap; | |
176 | for (; indent >= 8; indent -= 8) | |
177 | fputc ('\t', f); | |
178 | fprintf (f, "%*s", indent, ""); | |
179 | va_start (ap, format); | |
180 | vfprintf (f, format, ap); | |
181 | va_end (ap); | |
182 | } | |
183 | ||
3d2cf79f RB |
184 | static void |
185 | output_line_directive (FILE *f, source_location location, | |
186 | bool dumpfile = false) | |
187 | { | |
0e50b624 | 188 | const line_map_ordinary *map; |
3d2cf79f RB |
189 | linemap_resolve_location (line_table, location, LRK_SPELLING_LOCATION, &map); |
190 | expanded_location loc = linemap_expand_location (line_table, map, location); | |
191 | if (dumpfile) | |
192 | { | |
193 | /* When writing to a dumpfile only dump the filename. */ | |
194 | const char *file = strrchr (loc.file, DIR_SEPARATOR); | |
195 | if (!file) | |
196 | file = loc.file; | |
197 | else | |
198 | ++file; | |
199 | fprintf (f, "%s:%d", file, loc.line); | |
200 | } | |
201 | else | |
202 | /* Other gen programs really output line directives here, at least for | |
203 | development it's right now more convenient to have line information | |
204 | from the generated file. Still keep the directives as comment for now | |
205 | to easily back-point to the meta-description. */ | |
206 | fprintf (f, "/* #line %d \"%s\" */\n", loc.line, loc.file); | |
207 | } | |
208 | ||
209 | ||
210 | /* Pull in tree codes and builtin function codes from their | |
211 | definition files. */ | |
212 | ||
213 | #define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM, | |
214 | enum tree_code { | |
215 | #include "tree.def" | |
216 | CONVERT0, | |
217 | CONVERT1, | |
218 | CONVERT2, | |
39791822 RB |
219 | VIEW_CONVERT0, |
220 | VIEW_CONVERT1, | |
221 | VIEW_CONVERT2, | |
3d2cf79f RB |
222 | MAX_TREE_CODES |
223 | }; | |
224 | #undef DEFTREECODE | |
225 | ||
226 | #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) ENUM, | |
227 | enum built_in_function { | |
228 | #include "builtins.def" | |
229 | END_BUILTINS | |
230 | }; | |
3d2cf79f | 231 | |
c9e926ce RS |
232 | #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) IFN_##CODE, |
233 | enum internal_fn { | |
234 | #include "internal-fn.def" | |
235 | IFN_LAST | |
236 | }; | |
237 | ||
805a5406 RB |
238 | /* Return true if CODE represents a commutative tree code. Otherwise |
239 | return false. */ | |
240 | bool | |
241 | commutative_tree_code (enum tree_code code) | |
242 | { | |
243 | switch (code) | |
244 | { | |
245 | case PLUS_EXPR: | |
246 | case MULT_EXPR: | |
247 | case MULT_HIGHPART_EXPR: | |
248 | case MIN_EXPR: | |
249 | case MAX_EXPR: | |
250 | case BIT_IOR_EXPR: | |
251 | case BIT_XOR_EXPR: | |
252 | case BIT_AND_EXPR: | |
253 | case NE_EXPR: | |
254 | case EQ_EXPR: | |
255 | case UNORDERED_EXPR: | |
256 | case ORDERED_EXPR: | |
257 | case UNEQ_EXPR: | |
258 | case LTGT_EXPR: | |
259 | case TRUTH_AND_EXPR: | |
260 | case TRUTH_XOR_EXPR: | |
261 | case TRUTH_OR_EXPR: | |
262 | case WIDEN_MULT_EXPR: | |
263 | case VEC_WIDEN_MULT_HI_EXPR: | |
264 | case VEC_WIDEN_MULT_LO_EXPR: | |
265 | case VEC_WIDEN_MULT_EVEN_EXPR: | |
266 | case VEC_WIDEN_MULT_ODD_EXPR: | |
267 | return true; | |
268 | ||
269 | default: | |
270 | break; | |
271 | } | |
272 | return false; | |
273 | } | |
274 | ||
275 | /* Return true if CODE represents a ternary tree code for which the | |
276 | first two operands are commutative. Otherwise return false. */ | |
277 | bool | |
278 | commutative_ternary_tree_code (enum tree_code code) | |
279 | { | |
280 | switch (code) | |
281 | { | |
282 | case WIDEN_MULT_PLUS_EXPR: | |
283 | case WIDEN_MULT_MINUS_EXPR: | |
284 | case DOT_PROD_EXPR: | |
285 | case FMA_EXPR: | |
286 | return true; | |
287 | ||
288 | default: | |
289 | break; | |
290 | } | |
291 | return false; | |
292 | } | |
293 | ||
e04d2a35 RB |
294 | /* Return true if CODE is a comparison. */ |
295 | ||
296 | bool | |
297 | comparison_code_p (enum tree_code code) | |
298 | { | |
299 | switch (code) | |
300 | { | |
301 | case EQ_EXPR: | |
302 | case NE_EXPR: | |
303 | case ORDERED_EXPR: | |
304 | case UNORDERED_EXPR: | |
305 | case LTGT_EXPR: | |
306 | case UNEQ_EXPR: | |
307 | case GT_EXPR: | |
308 | case GE_EXPR: | |
309 | case LT_EXPR: | |
310 | case LE_EXPR: | |
311 | case UNGT_EXPR: | |
312 | case UNGE_EXPR: | |
313 | case UNLT_EXPR: | |
314 | case UNLE_EXPR: | |
315 | return true; | |
316 | ||
317 | default: | |
318 | break; | |
319 | } | |
320 | return false; | |
321 | } | |
322 | ||
3d2cf79f RB |
323 | |
324 | /* Base class for all identifiers the parser knows. */ | |
325 | ||
8d67ee55 | 326 | struct id_base : nofree_ptr_hash<id_base> |
3d2cf79f | 327 | { |
fa74b47a | 328 | enum id_kind { CODE, FN, PREDICATE, USER, NULL_ID } kind; |
3d2cf79f RB |
329 | |
330 | id_base (id_kind, const char *, int = -1); | |
331 | ||
332 | hashval_t hashval; | |
333 | int nargs; | |
334 | const char *id; | |
335 | ||
336 | /* hash_table support. */ | |
67f58944 TS |
337 | static inline hashval_t hash (const id_base *); |
338 | static inline int equal (const id_base *, const id_base *); | |
3d2cf79f RB |
339 | }; |
340 | ||
341 | inline hashval_t | |
67f58944 | 342 | id_base::hash (const id_base *op) |
3d2cf79f RB |
343 | { |
344 | return op->hashval; | |
345 | } | |
346 | ||
347 | inline int | |
67f58944 TS |
348 | id_base::equal (const id_base *op1, |
349 | const id_base *op2) | |
3d2cf79f RB |
350 | { |
351 | return (op1->hashval == op2->hashval | |
352 | && strcmp (op1->id, op2->id) == 0); | |
353 | } | |
354 | ||
fa74b47a RS |
355 | /* The special id "null", which matches nothing. */ |
356 | static id_base *null_id; | |
357 | ||
3d2cf79f RB |
358 | /* Hashtable of known pattern operators. This is pre-seeded from |
359 | all known tree codes and all known builtin function ids. */ | |
360 | static hash_table<id_base> *operators; | |
361 | ||
362 | id_base::id_base (id_kind kind_, const char *id_, int nargs_) | |
363 | { | |
364 | kind = kind_; | |
365 | id = id_; | |
366 | nargs = nargs_; | |
367 | hashval = htab_hash_string (id); | |
368 | } | |
369 | ||
370 | /* Identifier that maps to a tree code. */ | |
371 | ||
372 | struct operator_id : public id_base | |
373 | { | |
374 | operator_id (enum tree_code code_, const char *id_, unsigned nargs_, | |
375 | const char *tcc_) | |
376 | : id_base (id_base::CODE, id_, nargs_), code (code_), tcc (tcc_) {} | |
377 | enum tree_code code; | |
378 | const char *tcc; | |
379 | }; | |
380 | ||
c9e926ce | 381 | /* Identifier that maps to a builtin or internal function code. */ |
3d2cf79f RB |
382 | |
383 | struct fn_id : public id_base | |
384 | { | |
385 | fn_id (enum built_in_function fn_, const char *id_) | |
386 | : id_base (id_base::FN, id_), fn (fn_) {} | |
c9e926ce RS |
387 | fn_id (enum internal_fn fn_, const char *id_) |
388 | : id_base (id_base::FN, id_), fn (int (END_BUILTINS) + int (fn_)) {} | |
389 | unsigned int fn; | |
3d2cf79f RB |
390 | }; |
391 | ||
392 | struct simplify; | |
393 | ||
394 | /* Identifier that maps to a user-defined predicate. */ | |
395 | ||
396 | struct predicate_id : public id_base | |
397 | { | |
398 | predicate_id (const char *id_) | |
399 | : id_base (id_base::PREDICATE, id_), matchers (vNULL) {} | |
400 | vec<simplify *> matchers; | |
401 | }; | |
402 | ||
403 | /* Identifier that maps to a operator defined by a 'for' directive. */ | |
404 | ||
405 | struct user_id : public id_base | |
406 | { | |
72eb311d RB |
407 | user_id (const char *id_, bool is_oper_list_ = false) |
408 | : id_base (id_base::USER, id_), substitutes (vNULL), | |
409 | used (false), is_oper_list (is_oper_list_) {} | |
3d2cf79f | 410 | vec<id_base *> substitutes; |
72eb311d RB |
411 | bool used; |
412 | bool is_oper_list; | |
3d2cf79f RB |
413 | }; |
414 | ||
415 | template<> | |
416 | template<> | |
417 | inline bool | |
418 | is_a_helper <fn_id *>::test (id_base *id) | |
419 | { | |
420 | return id->kind == id_base::FN; | |
421 | } | |
422 | ||
423 | template<> | |
424 | template<> | |
425 | inline bool | |
426 | is_a_helper <operator_id *>::test (id_base *id) | |
427 | { | |
428 | return id->kind == id_base::CODE; | |
429 | } | |
430 | ||
431 | template<> | |
432 | template<> | |
433 | inline bool | |
434 | is_a_helper <predicate_id *>::test (id_base *id) | |
435 | { | |
436 | return id->kind == id_base::PREDICATE; | |
437 | } | |
438 | ||
439 | template<> | |
440 | template<> | |
441 | inline bool | |
442 | is_a_helper <user_id *>::test (id_base *id) | |
443 | { | |
444 | return id->kind == id_base::USER; | |
445 | } | |
446 | ||
447 | /* Add a predicate identifier to the hash. */ | |
448 | ||
449 | static predicate_id * | |
450 | add_predicate (const char *id) | |
451 | { | |
452 | predicate_id *p = new predicate_id (id); | |
453 | id_base **slot = operators->find_slot_with_hash (p, p->hashval, INSERT); | |
454 | if (*slot) | |
455 | fatal ("duplicate id definition"); | |
456 | *slot = p; | |
457 | return p; | |
458 | } | |
459 | ||
460 | /* Add a tree code identifier to the hash. */ | |
461 | ||
462 | static void | |
463 | add_operator (enum tree_code code, const char *id, | |
464 | const char *tcc, unsigned nargs) | |
465 | { | |
466 | if (strcmp (tcc, "tcc_unary") != 0 | |
467 | && strcmp (tcc, "tcc_binary") != 0 | |
468 | && strcmp (tcc, "tcc_comparison") != 0 | |
469 | && strcmp (tcc, "tcc_expression") != 0 | |
470 | /* For {REAL,IMAG}PART_EXPR and VIEW_CONVERT_EXPR. */ | |
471 | && strcmp (tcc, "tcc_reference") != 0 | |
472 | /* To have INTEGER_CST and friends as "predicate operators". */ | |
10230017 RB |
473 | && strcmp (tcc, "tcc_constant") != 0 |
474 | /* And allow CONSTRUCTOR for vector initializers. */ | |
96a111a3 RB |
475 | && !(code == CONSTRUCTOR) |
476 | /* Allow SSA_NAME as predicate operator. */ | |
477 | && !(code == SSA_NAME)) | |
3d2cf79f | 478 | return; |
99e943a2 RB |
479 | /* Treat ADDR_EXPR as atom, thus don't allow matching its operand. */ |
480 | if (code == ADDR_EXPR) | |
481 | nargs = 0; | |
3d2cf79f RB |
482 | operator_id *op = new operator_id (code, id, nargs, tcc); |
483 | id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT); | |
484 | if (*slot) | |
485 | fatal ("duplicate id definition"); | |
486 | *slot = op; | |
487 | } | |
488 | ||
c9e926ce RS |
489 | /* Add a built-in or internal function identifier to the hash. ID is |
490 | the name of its CFN_* enumeration value. */ | |
3d2cf79f | 491 | |
c9e926ce | 492 | template <typename T> |
3d2cf79f | 493 | static void |
c9e926ce | 494 | add_function (T code, const char *id) |
3d2cf79f RB |
495 | { |
496 | fn_id *fn = new fn_id (code, id); | |
497 | id_base **slot = operators->find_slot_with_hash (fn, fn->hashval, INSERT); | |
498 | if (*slot) | |
499 | fatal ("duplicate id definition"); | |
500 | *slot = fn; | |
501 | } | |
502 | ||
503 | /* Helper for easy comparing ID with tree code CODE. */ | |
504 | ||
505 | static bool | |
506 | operator==(id_base &id, enum tree_code code) | |
507 | { | |
508 | if (operator_id *oid = dyn_cast <operator_id *> (&id)) | |
509 | return oid->code == code; | |
510 | return false; | |
511 | } | |
512 | ||
fa74b47a | 513 | /* Lookup the identifier ID. Allow "null" if ALLOW_NULL. */ |
3d2cf79f RB |
514 | |
515 | id_base * | |
fa74b47a | 516 | get_operator (const char *id, bool allow_null = false) |
3d2cf79f | 517 | { |
fa74b47a RS |
518 | if (allow_null && strcmp (id, "null") == 0) |
519 | return null_id; | |
520 | ||
3d2cf79f RB |
521 | id_base tem (id_base::CODE, id); |
522 | ||
523 | id_base *op = operators->find_with_hash (&tem, tem.hashval); | |
524 | if (op) | |
72eb311d RB |
525 | { |
526 | /* If this is a user-defined identifier track whether it was used. */ | |
527 | if (user_id *uid = dyn_cast<user_id *> (op)) | |
528 | uid->used = true; | |
529 | return op; | |
530 | } | |
3d2cf79f | 531 | |
c9e926ce RS |
532 | char *id2; |
533 | bool all_upper = true; | |
534 | bool all_lower = true; | |
535 | for (unsigned int i = 0; id[i]; ++i) | |
536 | if (ISUPPER (id[i])) | |
537 | all_lower = false; | |
538 | else if (ISLOWER (id[i])) | |
539 | all_upper = false; | |
540 | if (all_lower) | |
3d2cf79f | 541 | { |
c9e926ce RS |
542 | /* Try in caps with _EXPR appended. */ |
543 | id2 = ACONCAT ((id, "_EXPR", NULL)); | |
544 | for (unsigned int i = 0; id2[i]; ++i) | |
545 | id2[i] = TOUPPER (id2[i]); | |
3d2cf79f | 546 | } |
c9e926ce RS |
547 | else if (all_upper && strncmp (id, "IFN_", 4) == 0) |
548 | /* Try CFN_ instead of IFN_. */ | |
549 | id2 = ACONCAT (("CFN_", id + 4, NULL)); | |
550 | else if (all_upper && strncmp (id, "BUILT_IN_", 9) == 0) | |
551 | /* Try prepending CFN_. */ | |
552 | id2 = ACONCAT (("CFN_", id, NULL)); | |
553 | else | |
554 | return NULL; | |
3d2cf79f | 555 | |
3d2cf79f | 556 | new (&tem) id_base (id_base::CODE, id2); |
c9e926ce | 557 | return operators->find_with_hash (&tem, tem.hashval); |
3d2cf79f RB |
558 | } |
559 | ||
e04d2a35 RB |
560 | /* Return the comparison operators that results if the operands are |
561 | swapped. This is safe for floating-point. */ | |
562 | ||
563 | id_base * | |
564 | swap_tree_comparison (operator_id *p) | |
565 | { | |
566 | switch (p->code) | |
567 | { | |
568 | case EQ_EXPR: | |
569 | case NE_EXPR: | |
570 | case ORDERED_EXPR: | |
571 | case UNORDERED_EXPR: | |
572 | case LTGT_EXPR: | |
573 | case UNEQ_EXPR: | |
574 | return p; | |
575 | case GT_EXPR: | |
576 | return get_operator ("LT_EXPR"); | |
577 | case GE_EXPR: | |
578 | return get_operator ("LE_EXPR"); | |
579 | case LT_EXPR: | |
580 | return get_operator ("GT_EXPR"); | |
581 | case LE_EXPR: | |
582 | return get_operator ("GE_EXPR"); | |
583 | case UNGT_EXPR: | |
584 | return get_operator ("UNLT_EXPR"); | |
585 | case UNGE_EXPR: | |
586 | return get_operator ("UNLE_EXPR"); | |
587 | case UNLT_EXPR: | |
588 | return get_operator ("UNGT_EXPR"); | |
589 | case UNLE_EXPR: | |
590 | return get_operator ("UNGE_EXPR"); | |
591 | default: | |
592 | gcc_unreachable (); | |
593 | } | |
594 | } | |
595 | ||
fb5c464a | 596 | typedef hash_map<nofree_string_hash, unsigned> cid_map_t; |
f1308e4b | 597 | |
3d2cf79f RB |
598 | |
599 | /* The AST produced by parsing of the pattern definitions. */ | |
600 | ||
601 | struct dt_operand; | |
47b25362 | 602 | struct capture_info; |
3d2cf79f RB |
603 | |
604 | /* The base class for operands. */ | |
605 | ||
606 | struct operand { | |
8fdc6c67 | 607 | enum op_type { OP_PREDICATE, OP_EXPR, OP_CAPTURE, OP_C_EXPR, OP_IF, OP_WITH }; |
0889c52f RB |
608 | operand (enum op_type type_, source_location loc_) |
609 | : type (type_), location (loc_) {} | |
3d2cf79f | 610 | enum op_type type; |
0889c52f | 611 | source_location location; |
c551c21d | 612 | virtual void gen_transform (FILE *, int, const char *, bool, int, |
47b25362 RB |
613 | const char *, capture_info *, |
614 | dt_operand ** = 0, | |
a3ca1bc5 | 615 | int = 0) |
3d2cf79f RB |
616 | { gcc_unreachable (); } |
617 | }; | |
618 | ||
619 | /* A predicate operand. Predicates are leafs in the AST. */ | |
620 | ||
621 | struct predicate : public operand | |
622 | { | |
0889c52f RB |
623 | predicate (predicate_id *p_, source_location loc) |
624 | : operand (OP_PREDICATE, loc), p (p_) {} | |
3d2cf79f RB |
625 | predicate_id *p; |
626 | }; | |
627 | ||
628 | /* An operand that constitutes an expression. Expressions include | |
629 | function calls and user-defined predicate invocations. */ | |
630 | ||
631 | struct expr : public operand | |
632 | { | |
0889c52f RB |
633 | expr (id_base *operation_, source_location loc, bool is_commutative_ = false) |
634 | : operand (OP_EXPR, loc), operation (operation_), | |
47b25362 | 635 | ops (vNULL), expr_type (NULL), is_commutative (is_commutative_), |
44fc0a51 RB |
636 | is_generic (false), force_single_use (false) {} |
637 | expr (expr *e) | |
0889c52f | 638 | : operand (OP_EXPR, e->location), operation (e->operation), |
44fc0a51 RB |
639 | ops (vNULL), expr_type (e->expr_type), is_commutative (e->is_commutative), |
640 | is_generic (e->is_generic), force_single_use (e->force_single_use) {} | |
3d2cf79f RB |
641 | void append_op (operand *op) { ops.safe_push (op); } |
642 | /* The operator and its operands. */ | |
643 | id_base *operation; | |
644 | vec<operand *> ops; | |
645 | /* An explicitely specified type - used exclusively for conversions. */ | |
646 | const char *expr_type; | |
647 | /* Whether the operation is to be applied commutatively. This is | |
648 | later lowered to two separate patterns. */ | |
649 | bool is_commutative; | |
47b25362 RB |
650 | /* Whether the expression is expected to be in GENERIC form. */ |
651 | bool is_generic; | |
44fc0a51 RB |
652 | /* Whether pushing any stmt to the sequence should be conditional |
653 | on this expression having a single-use. */ | |
654 | bool force_single_use; | |
c551c21d | 655 | virtual void gen_transform (FILE *f, int, const char *, bool, int, |
47b25362 | 656 | const char *, capture_info *, |
a3ca1bc5 | 657 | dt_operand ** = 0, int = 0); |
3d2cf79f RB |
658 | }; |
659 | ||
660 | /* An operator that is represented by native C code. This is always | |
661 | a leaf operand in the AST. This class is also used to represent | |
662 | the code to be generated for 'if' and 'with' expressions. */ | |
663 | ||
664 | struct c_expr : public operand | |
665 | { | |
666 | /* A mapping of an identifier and its replacement. Used to apply | |
667 | 'for' lowering. */ | |
668 | struct id_tab { | |
669 | const char *id; | |
670 | const char *oper; | |
671 | id_tab (const char *id_, const char *oper_): id (id_), oper (oper_) {} | |
672 | }; | |
673 | ||
0889c52f RB |
674 | c_expr (cpp_reader *r_, source_location loc, |
675 | vec<cpp_token> code_, unsigned nr_stmts_, | |
f1308e4b | 676 | vec<id_tab> ids_, cid_map_t *capture_ids_) |
0889c52f RB |
677 | : operand (OP_C_EXPR, loc), r (r_), code (code_), |
678 | capture_ids (capture_ids_), nr_stmts (nr_stmts_), ids (ids_) {} | |
3d2cf79f RB |
679 | /* cpplib tokens and state to transform this back to source. */ |
680 | cpp_reader *r; | |
681 | vec<cpp_token> code; | |
f1308e4b | 682 | cid_map_t *capture_ids; |
3d2cf79f RB |
683 | /* The number of statements parsed (well, the number of ';'s). */ |
684 | unsigned nr_stmts; | |
685 | /* The identifier replacement vector. */ | |
686 | vec<id_tab> ids; | |
c551c21d | 687 | virtual void gen_transform (FILE *f, int, const char *, bool, int, |
47b25362 | 688 | const char *, capture_info *, |
a3ca1bc5 | 689 | dt_operand ** = 0, int = 0); |
3d2cf79f RB |
690 | }; |
691 | ||
692 | /* A wrapper around another operand that captures its value. */ | |
693 | ||
694 | struct capture : public operand | |
695 | { | |
0889c52f RB |
696 | capture (source_location loc, unsigned where_, operand *what_) |
697 | : operand (OP_CAPTURE, loc), where (where_), what (what_) {} | |
3d2cf79f RB |
698 | /* Identifier index for the value. */ |
699 | unsigned where; | |
700 | /* The captured value. */ | |
701 | operand *what; | |
c551c21d | 702 | virtual void gen_transform (FILE *f, int, const char *, bool, int, |
47b25362 | 703 | const char *, capture_info *, |
a3ca1bc5 | 704 | dt_operand ** = 0, int = 0); |
3d2cf79f RB |
705 | }; |
706 | ||
8fdc6c67 RB |
707 | /* if expression. */ |
708 | ||
709 | struct if_expr : public operand | |
710 | { | |
0889c52f RB |
711 | if_expr (source_location loc) |
712 | : operand (OP_IF, loc), cond (NULL), trueexpr (NULL), falseexpr (NULL) {} | |
8fdc6c67 RB |
713 | c_expr *cond; |
714 | operand *trueexpr; | |
715 | operand *falseexpr; | |
716 | }; | |
717 | ||
718 | /* with expression. */ | |
719 | ||
720 | struct with_expr : public operand | |
721 | { | |
0889c52f RB |
722 | with_expr (source_location loc) |
723 | : operand (OP_WITH, loc), with (NULL), subexpr (NULL) {} | |
8fdc6c67 RB |
724 | c_expr *with; |
725 | operand *subexpr; | |
726 | }; | |
727 | ||
3d2cf79f RB |
728 | template<> |
729 | template<> | |
730 | inline bool | |
731 | is_a_helper <capture *>::test (operand *op) | |
732 | { | |
733 | return op->type == operand::OP_CAPTURE; | |
734 | } | |
735 | ||
736 | template<> | |
737 | template<> | |
738 | inline bool | |
739 | is_a_helper <predicate *>::test (operand *op) | |
740 | { | |
741 | return op->type == operand::OP_PREDICATE; | |
742 | } | |
743 | ||
744 | template<> | |
745 | template<> | |
746 | inline bool | |
747 | is_a_helper <c_expr *>::test (operand *op) | |
748 | { | |
749 | return op->type == operand::OP_C_EXPR; | |
750 | } | |
751 | ||
752 | template<> | |
753 | template<> | |
754 | inline bool | |
755 | is_a_helper <expr *>::test (operand *op) | |
756 | { | |
757 | return op->type == operand::OP_EXPR; | |
758 | } | |
759 | ||
8fdc6c67 RB |
760 | template<> |
761 | template<> | |
762 | inline bool | |
763 | is_a_helper <if_expr *>::test (operand *op) | |
764 | { | |
765 | return op->type == operand::OP_IF; | |
766 | } | |
3d2cf79f | 767 | |
8fdc6c67 RB |
768 | template<> |
769 | template<> | |
770 | inline bool | |
771 | is_a_helper <with_expr *>::test (operand *op) | |
3d2cf79f | 772 | { |
8fdc6c67 RB |
773 | return op->type == operand::OP_WITH; |
774 | } | |
3d2cf79f RB |
775 | |
776 | /* The main class of a pattern and its transform. This is used to | |
777 | represent both (simplify ...) and (match ...) kinds. The AST | |
778 | duplicates all outer 'if' and 'for' expressions here so each | |
779 | simplify can exist in isolation. */ | |
780 | ||
781 | struct simplify | |
782 | { | |
8fdc6c67 RB |
783 | enum simplify_kind { SIMPLIFY, MATCH }; |
784 | ||
0889c52f | 785 | simplify (simplify_kind kind_, operand *match_, operand *result_, |
8fdc6c67 | 786 | vec<vec<user_id *> > for_vec_, cid_map_t *capture_ids_) |
0889c52f | 787 | : kind (kind_), match (match_), result (result_), |
d4b71b95 | 788 | for_vec (for_vec_), for_subst_vec (vNULL), |
f1308e4b | 789 | capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {} |
3d2cf79f | 790 | |
8fdc6c67 | 791 | simplify_kind kind; |
3d2cf79f RB |
792 | /* The expression that is matched against the GENERIC or GIMPLE IL. */ |
793 | operand *match; | |
8fdc6c67 RB |
794 | /* For a (simplify ...) an expression with ifs and withs with the expression |
795 | produced when the pattern applies in the leafs. | |
796 | For a (match ...) the leafs are either empty if it is a simple predicate | |
797 | or the single expression specifying the matched operands. */ | |
3d2cf79f | 798 | struct operand *result; |
3d2cf79f RB |
799 | /* Collected 'for' expression operators that have to be replaced |
800 | in the lowering phase. */ | |
801 | vec<vec<user_id *> > for_vec; | |
d4b71b95 | 802 | vec<std::pair<user_id *, id_base *> > for_subst_vec; |
3d2cf79f | 803 | /* A map of capture identifiers to indexes. */ |
f1308e4b | 804 | cid_map_t *capture_ids; |
3d2cf79f RB |
805 | int capture_max; |
806 | }; | |
807 | ||
808 | /* Debugging routines for dumping the AST. */ | |
809 | ||
810 | DEBUG_FUNCTION void | |
811 | print_operand (operand *o, FILE *f = stderr, bool flattened = false) | |
812 | { | |
813 | if (capture *c = dyn_cast<capture *> (o)) | |
814 | { | |
3d2cf79f | 815 | if (c->what && flattened == false) |
eee7b6c4 RB |
816 | print_operand (c->what, f, flattened); |
817 | fprintf (f, "@%u", c->where); | |
3d2cf79f RB |
818 | } |
819 | ||
820 | else if (predicate *p = dyn_cast<predicate *> (o)) | |
821 | fprintf (f, "%s", p->p->id); | |
822 | ||
823 | else if (is_a<c_expr *> (o)) | |
824 | fprintf (f, "c_expr"); | |
825 | ||
826 | else if (expr *e = dyn_cast<expr *> (o)) | |
827 | { | |
eee7b6c4 RB |
828 | if (e->ops.length () == 0) |
829 | fprintf (f, "%s", e->operation->id); | |
830 | else | |
3d2cf79f | 831 | { |
eee7b6c4 RB |
832 | fprintf (f, "(%s", e->operation->id); |
833 | ||
834 | if (flattened == false) | |
3d2cf79f | 835 | { |
eee7b6c4 RB |
836 | for (unsigned i = 0; i < e->ops.length (); ++i) |
837 | { | |
838 | putc (' ', f); | |
839 | print_operand (e->ops[i], f, flattened); | |
840 | } | |
3d2cf79f | 841 | } |
eee7b6c4 | 842 | putc (')', f); |
3d2cf79f | 843 | } |
3d2cf79f RB |
844 | } |
845 | ||
846 | else | |
847 | gcc_unreachable (); | |
848 | } | |
849 | ||
850 | DEBUG_FUNCTION void | |
851 | print_matches (struct simplify *s, FILE *f = stderr) | |
852 | { | |
853 | fprintf (f, "for expression: "); | |
854 | print_operand (s->match, f); | |
855 | putc ('\n', f); | |
856 | } | |
857 | ||
858 | ||
859 | /* AST lowering. */ | |
860 | ||
861 | /* Lowering of commutative operators. */ | |
862 | ||
863 | static void | |
864 | cartesian_product (const vec< vec<operand *> >& ops_vector, | |
865 | vec< vec<operand *> >& result, vec<operand *>& v, unsigned n) | |
866 | { | |
867 | if (n == ops_vector.length ()) | |
868 | { | |
869 | vec<operand *> xv = v.copy (); | |
870 | result.safe_push (xv); | |
871 | return; | |
872 | } | |
873 | ||
874 | for (unsigned i = 0; i < ops_vector[n].length (); ++i) | |
875 | { | |
876 | v[n] = ops_vector[n][i]; | |
877 | cartesian_product (ops_vector, result, v, n + 1); | |
878 | } | |
879 | } | |
880 | ||
881 | /* Lower OP to two operands in case it is marked as commutative. */ | |
882 | ||
883 | static vec<operand *> | |
e04d2a35 | 884 | commutate (operand *op, vec<vec<user_id *> > &for_vec) |
3d2cf79f RB |
885 | { |
886 | vec<operand *> ret = vNULL; | |
887 | ||
888 | if (capture *c = dyn_cast <capture *> (op)) | |
889 | { | |
890 | if (!c->what) | |
891 | { | |
892 | ret.safe_push (op); | |
893 | return ret; | |
894 | } | |
e04d2a35 | 895 | vec<operand *> v = commutate (c->what, for_vec); |
3d2cf79f RB |
896 | for (unsigned i = 0; i < v.length (); ++i) |
897 | { | |
0889c52f | 898 | capture *nc = new capture (c->location, c->where, v[i]); |
3d2cf79f RB |
899 | ret.safe_push (nc); |
900 | } | |
901 | return ret; | |
902 | } | |
903 | ||
904 | expr *e = dyn_cast <expr *> (op); | |
905 | if (!e || e->ops.length () == 0) | |
906 | { | |
907 | ret.safe_push (op); | |
908 | return ret; | |
909 | } | |
910 | ||
911 | vec< vec<operand *> > ops_vector = vNULL; | |
912 | for (unsigned i = 0; i < e->ops.length (); ++i) | |
e04d2a35 | 913 | ops_vector.safe_push (commutate (e->ops[i], for_vec)); |
3d2cf79f RB |
914 | |
915 | auto_vec< vec<operand *> > result; | |
916 | auto_vec<operand *> v (e->ops.length ()); | |
917 | v.quick_grow_cleared (e->ops.length ()); | |
918 | cartesian_product (ops_vector, result, v, 0); | |
919 | ||
920 | ||
921 | for (unsigned i = 0; i < result.length (); ++i) | |
922 | { | |
44fc0a51 RB |
923 | expr *ne = new expr (e); |
924 | ne->is_commutative = false; | |
3d2cf79f RB |
925 | for (unsigned j = 0; j < result[i].length (); ++j) |
926 | ne->append_op (result[i][j]); | |
927 | ret.safe_push (ne); | |
928 | } | |
929 | ||
930 | if (!e->is_commutative) | |
931 | return ret; | |
932 | ||
933 | for (unsigned i = 0; i < result.length (); ++i) | |
934 | { | |
44fc0a51 | 935 | expr *ne = new expr (e); |
e04d2a35 RB |
936 | if (operator_id *p = dyn_cast <operator_id *> (ne->operation)) |
937 | { | |
938 | if (comparison_code_p (p->code)) | |
939 | ne->operation = swap_tree_comparison (p); | |
940 | } | |
941 | else if (user_id *p = dyn_cast <user_id *> (ne->operation)) | |
942 | { | |
943 | bool found_compare = false; | |
944 | for (unsigned j = 0; j < p->substitutes.length (); ++j) | |
945 | if (operator_id *q = dyn_cast <operator_id *> (p->substitutes[j])) | |
946 | { | |
947 | if (comparison_code_p (q->code) | |
948 | && swap_tree_comparison (q) != q) | |
949 | { | |
950 | found_compare = true; | |
951 | break; | |
952 | } | |
953 | } | |
954 | if (found_compare) | |
955 | { | |
956 | user_id *newop = new user_id ("<internal>"); | |
957 | for (unsigned j = 0; j < p->substitutes.length (); ++j) | |
958 | { | |
959 | id_base *subst = p->substitutes[j]; | |
960 | if (operator_id *q = dyn_cast <operator_id *> (subst)) | |
961 | { | |
962 | if (comparison_code_p (q->code)) | |
963 | subst = swap_tree_comparison (q); | |
964 | } | |
965 | newop->substitutes.safe_push (subst); | |
966 | } | |
967 | ne->operation = newop; | |
968 | /* Search for 'p' inside the for vector and push 'newop' | |
969 | to the same level. */ | |
970 | for (unsigned j = 0; newop && j < for_vec.length (); ++j) | |
971 | for (unsigned k = 0; k < for_vec[j].length (); ++k) | |
972 | if (for_vec[j][k] == p) | |
973 | { | |
974 | for_vec[j].safe_push (newop); | |
975 | newop = NULL; | |
976 | break; | |
977 | } | |
978 | } | |
979 | } | |
44fc0a51 | 980 | ne->is_commutative = false; |
3d2cf79f RB |
981 | // result[i].length () is 2 since e->operation is binary |
982 | for (unsigned j = result[i].length (); j; --j) | |
983 | ne->append_op (result[i][j-1]); | |
984 | ret.safe_push (ne); | |
985 | } | |
986 | ||
987 | return ret; | |
988 | } | |
989 | ||
990 | /* Lower operations marked as commutative in the AST of S and push | |
991 | the resulting patterns to SIMPLIFIERS. */ | |
992 | ||
993 | static void | |
994 | lower_commutative (simplify *s, vec<simplify *>& simplifiers) | |
995 | { | |
e04d2a35 | 996 | vec<operand *> matchers = commutate (s->match, s->for_vec); |
3d2cf79f RB |
997 | for (unsigned i = 0; i < matchers.length (); ++i) |
998 | { | |
0889c52f | 999 | simplify *ns = new simplify (s->kind, matchers[i], s->result, |
3d2cf79f RB |
1000 | s->for_vec, s->capture_ids); |
1001 | simplifiers.safe_push (ns); | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | /* Strip conditional conversios using operator OPER from O and its | |
1006 | children if STRIP, else replace them with an unconditional convert. */ | |
1007 | ||
1008 | operand * | |
39791822 RB |
1009 | lower_opt_convert (operand *o, enum tree_code oper, |
1010 | enum tree_code to_oper, bool strip) | |
3d2cf79f RB |
1011 | { |
1012 | if (capture *c = dyn_cast<capture *> (o)) | |
1013 | { | |
1014 | if (c->what) | |
0889c52f | 1015 | return new capture (c->location, c->where, |
39791822 | 1016 | lower_opt_convert (c->what, oper, to_oper, strip)); |
3d2cf79f RB |
1017 | else |
1018 | return c; | |
1019 | } | |
1020 | ||
1021 | expr *e = dyn_cast<expr *> (o); | |
1022 | if (!e) | |
1023 | return o; | |
1024 | ||
1025 | if (*e->operation == oper) | |
1026 | { | |
1027 | if (strip) | |
39791822 | 1028 | return lower_opt_convert (e->ops[0], oper, to_oper, strip); |
3d2cf79f | 1029 | |
44fc0a51 RB |
1030 | expr *ne = new expr (e); |
1031 | ne->operation = (to_oper == CONVERT_EXPR | |
1032 | ? get_operator ("CONVERT_EXPR") | |
1033 | : get_operator ("VIEW_CONVERT_EXPR")); | |
39791822 | 1034 | ne->append_op (lower_opt_convert (e->ops[0], oper, to_oper, strip)); |
3d2cf79f RB |
1035 | return ne; |
1036 | } | |
1037 | ||
44fc0a51 | 1038 | expr *ne = new expr (e); |
3d2cf79f | 1039 | for (unsigned i = 0; i < e->ops.length (); ++i) |
39791822 | 1040 | ne->append_op (lower_opt_convert (e->ops[i], oper, to_oper, strip)); |
3d2cf79f RB |
1041 | |
1042 | return ne; | |
1043 | } | |
1044 | ||
1045 | /* Determine whether O or its children uses the conditional conversion | |
1046 | operator OPER. */ | |
1047 | ||
1048 | static bool | |
1049 | has_opt_convert (operand *o, enum tree_code oper) | |
1050 | { | |
1051 | if (capture *c = dyn_cast<capture *> (o)) | |
1052 | { | |
1053 | if (c->what) | |
1054 | return has_opt_convert (c->what, oper); | |
1055 | else | |
1056 | return false; | |
1057 | } | |
1058 | ||
1059 | expr *e = dyn_cast<expr *> (o); | |
1060 | if (!e) | |
1061 | return false; | |
1062 | ||
1063 | if (*e->operation == oper) | |
1064 | return true; | |
1065 | ||
1066 | for (unsigned i = 0; i < e->ops.length (); ++i) | |
1067 | if (has_opt_convert (e->ops[i], oper)) | |
1068 | return true; | |
1069 | ||
1070 | return false; | |
1071 | } | |
1072 | ||
1073 | /* Lower conditional convert operators in O, expanding it to a vector | |
1074 | if required. */ | |
1075 | ||
1076 | static vec<operand *> | |
1077 | lower_opt_convert (operand *o) | |
1078 | { | |
1079 | vec<operand *> v1 = vNULL, v2; | |
1080 | ||
1081 | v1.safe_push (o); | |
1082 | ||
39791822 RB |
1083 | enum tree_code opers[] |
1084 | = { CONVERT0, CONVERT_EXPR, | |
1085 | CONVERT1, CONVERT_EXPR, | |
1086 | CONVERT2, CONVERT_EXPR, | |
1087 | VIEW_CONVERT0, VIEW_CONVERT_EXPR, | |
1088 | VIEW_CONVERT1, VIEW_CONVERT_EXPR, | |
1089 | VIEW_CONVERT2, VIEW_CONVERT_EXPR }; | |
3d2cf79f RB |
1090 | |
1091 | /* Conditional converts are lowered to a pattern with the | |
1092 | conversion and one without. The three different conditional | |
1093 | convert codes are lowered separately. */ | |
1094 | ||
39791822 | 1095 | for (unsigned i = 0; i < sizeof (opers) / sizeof (enum tree_code); i += 2) |
3d2cf79f RB |
1096 | { |
1097 | v2 = vNULL; | |
1098 | for (unsigned j = 0; j < v1.length (); ++j) | |
1099 | if (has_opt_convert (v1[j], opers[i])) | |
1100 | { | |
39791822 RB |
1101 | v2.safe_push (lower_opt_convert (v1[j], |
1102 | opers[i], opers[i+1], false)); | |
1103 | v2.safe_push (lower_opt_convert (v1[j], | |
1104 | opers[i], opers[i+1], true)); | |
3d2cf79f RB |
1105 | } |
1106 | ||
1107 | if (v2 != vNULL) | |
1108 | { | |
1109 | v1 = vNULL; | |
1110 | for (unsigned j = 0; j < v2.length (); ++j) | |
1111 | v1.safe_push (v2[j]); | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | return v1; | |
1116 | } | |
1117 | ||
1118 | /* Lower conditional convert operators in the AST of S and push | |
1119 | the resulting multiple patterns to SIMPLIFIERS. */ | |
1120 | ||
1121 | static void | |
1122 | lower_opt_convert (simplify *s, vec<simplify *>& simplifiers) | |
1123 | { | |
1124 | vec<operand *> matchers = lower_opt_convert (s->match); | |
1125 | for (unsigned i = 0; i < matchers.length (); ++i) | |
1126 | { | |
0889c52f | 1127 | simplify *ns = new simplify (s->kind, matchers[i], s->result, |
3d2cf79f RB |
1128 | s->for_vec, s->capture_ids); |
1129 | simplifiers.safe_push (ns); | |
1130 | } | |
1131 | } | |
1132 | ||
47b25362 RB |
1133 | /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a |
1134 | GENERIC and a GIMPLE variant. */ | |
1135 | ||
1136 | static vec<operand *> | |
1137 | lower_cond (operand *o) | |
1138 | { | |
1139 | vec<operand *> ro = vNULL; | |
1140 | ||
1141 | if (capture *c = dyn_cast<capture *> (o)) | |
1142 | { | |
1143 | if (c->what) | |
1144 | { | |
1145 | vec<operand *> lop = vNULL; | |
1146 | lop = lower_cond (c->what); | |
1147 | ||
1148 | for (unsigned i = 0; i < lop.length (); ++i) | |
0889c52f | 1149 | ro.safe_push (new capture (c->location, c->where, lop[i])); |
47b25362 RB |
1150 | return ro; |
1151 | } | |
1152 | } | |
1153 | ||
1154 | expr *e = dyn_cast<expr *> (o); | |
1155 | if (!e || e->ops.length () == 0) | |
1156 | { | |
1157 | ro.safe_push (o); | |
1158 | return ro; | |
1159 | } | |
1160 | ||
1161 | vec< vec<operand *> > ops_vector = vNULL; | |
1162 | for (unsigned i = 0; i < e->ops.length (); ++i) | |
1163 | ops_vector.safe_push (lower_cond (e->ops[i])); | |
1164 | ||
1165 | auto_vec< vec<operand *> > result; | |
1166 | auto_vec<operand *> v (e->ops.length ()); | |
1167 | v.quick_grow_cleared (e->ops.length ()); | |
1168 | cartesian_product (ops_vector, result, v, 0); | |
1169 | ||
1170 | for (unsigned i = 0; i < result.length (); ++i) | |
1171 | { | |
44fc0a51 | 1172 | expr *ne = new expr (e); |
47b25362 RB |
1173 | for (unsigned j = 0; j < result[i].length (); ++j) |
1174 | ne->append_op (result[i][j]); | |
1175 | ro.safe_push (ne); | |
1176 | /* If this is a COND with a captured expression or an | |
1177 | expression with two operands then also match a GENERIC | |
1178 | form on the compare. */ | |
1179 | if ((*e->operation == COND_EXPR | |
1180 | || *e->operation == VEC_COND_EXPR) | |
1181 | && ((is_a <capture *> (e->ops[0]) | |
1182 | && as_a <capture *> (e->ops[0])->what | |
1183 | && is_a <expr *> (as_a <capture *> (e->ops[0])->what) | |
1184 | && as_a <expr *> | |
1185 | (as_a <capture *> (e->ops[0])->what)->ops.length () == 2) | |
1186 | || (is_a <expr *> (e->ops[0]) | |
1187 | && as_a <expr *> (e->ops[0])->ops.length () == 2))) | |
1188 | { | |
44fc0a51 | 1189 | expr *ne = new expr (e); |
47b25362 RB |
1190 | for (unsigned j = 0; j < result[i].length (); ++j) |
1191 | ne->append_op (result[i][j]); | |
1192 | if (capture *c = dyn_cast <capture *> (ne->ops[0])) | |
1193 | { | |
1194 | expr *ocmp = as_a <expr *> (c->what); | |
44fc0a51 | 1195 | expr *cmp = new expr (ocmp); |
47b25362 RB |
1196 | for (unsigned j = 0; j < ocmp->ops.length (); ++j) |
1197 | cmp->append_op (ocmp->ops[j]); | |
1198 | cmp->is_generic = true; | |
0889c52f | 1199 | ne->ops[0] = new capture (c->location, c->where, cmp); |
47b25362 RB |
1200 | } |
1201 | else | |
1202 | { | |
1203 | expr *ocmp = as_a <expr *> (ne->ops[0]); | |
44fc0a51 | 1204 | expr *cmp = new expr (ocmp); |
47b25362 RB |
1205 | for (unsigned j = 0; j < ocmp->ops.length (); ++j) |
1206 | cmp->append_op (ocmp->ops[j]); | |
1207 | cmp->is_generic = true; | |
1208 | ne->ops[0] = cmp; | |
1209 | } | |
1210 | ro.safe_push (ne); | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | return ro; | |
1215 | } | |
1216 | ||
1217 | /* Lower the compare operand of COND_EXPRs and VEC_COND_EXPRs to a | |
1218 | GENERIC and a GIMPLE variant. */ | |
1219 | ||
1220 | static void | |
1221 | lower_cond (simplify *s, vec<simplify *>& simplifiers) | |
1222 | { | |
1223 | vec<operand *> matchers = lower_cond (s->match); | |
1224 | for (unsigned i = 0; i < matchers.length (); ++i) | |
1225 | { | |
0889c52f | 1226 | simplify *ns = new simplify (s->kind, matchers[i], s->result, |
47b25362 RB |
1227 | s->for_vec, s->capture_ids); |
1228 | simplifiers.safe_push (ns); | |
1229 | } | |
1230 | } | |
1231 | ||
fa74b47a RS |
1232 | /* Return true if O refers to ID. */ |
1233 | ||
1234 | bool | |
1235 | contains_id (operand *o, user_id *id) | |
1236 | { | |
1237 | if (capture *c = dyn_cast<capture *> (o)) | |
1238 | return c->what && contains_id (c->what, id); | |
1239 | ||
1240 | if (expr *e = dyn_cast<expr *> (o)) | |
1241 | { | |
1242 | if (e->operation == id) | |
1243 | return true; | |
1244 | for (unsigned i = 0; i < e->ops.length (); ++i) | |
1245 | if (contains_id (e->ops[i], id)) | |
1246 | return true; | |
1247 | return false; | |
1248 | } | |
1249 | ||
1250 | if (with_expr *w = dyn_cast <with_expr *> (o)) | |
1251 | return (contains_id (w->with, id) | |
1252 | || contains_id (w->subexpr, id)); | |
1253 | ||
1254 | if (if_expr *ife = dyn_cast <if_expr *> (o)) | |
1255 | return (contains_id (ife->cond, id) | |
1256 | || contains_id (ife->trueexpr, id) | |
1257 | || (ife->falseexpr && contains_id (ife->falseexpr, id))); | |
1258 | ||
1259 | if (c_expr *ce = dyn_cast<c_expr *> (o)) | |
1260 | return ce->capture_ids && ce->capture_ids->get (id->id); | |
1261 | ||
1262 | return false; | |
1263 | } | |
1264 | ||
1265 | ||
3d2cf79f RB |
1266 | /* In AST operand O replace operator ID with operator WITH. */ |
1267 | ||
1268 | operand * | |
1269 | replace_id (operand *o, user_id *id, id_base *with) | |
1270 | { | |
1271 | /* Deep-copy captures and expressions, replacing operations as | |
1272 | needed. */ | |
1273 | if (capture *c = dyn_cast<capture *> (o)) | |
1274 | { | |
1275 | if (!c->what) | |
1276 | return c; | |
0889c52f RB |
1277 | return new capture (c->location, c->where, |
1278 | replace_id (c->what, id, with)); | |
3d2cf79f RB |
1279 | } |
1280 | else if (expr *e = dyn_cast<expr *> (o)) | |
1281 | { | |
44fc0a51 RB |
1282 | expr *ne = new expr (e); |
1283 | if (e->operation == id) | |
1284 | ne->operation = with; | |
3d2cf79f RB |
1285 | for (unsigned i = 0; i < e->ops.length (); ++i) |
1286 | ne->append_op (replace_id (e->ops[i], id, with)); | |
1287 | return ne; | |
1288 | } | |
8fdc6c67 RB |
1289 | else if (with_expr *w = dyn_cast <with_expr *> (o)) |
1290 | { | |
0889c52f | 1291 | with_expr *nw = new with_expr (w->location); |
8fdc6c67 RB |
1292 | nw->with = as_a <c_expr *> (replace_id (w->with, id, with)); |
1293 | nw->subexpr = replace_id (w->subexpr, id, with); | |
1294 | return nw; | |
1295 | } | |
1296 | else if (if_expr *ife = dyn_cast <if_expr *> (o)) | |
1297 | { | |
0889c52f | 1298 | if_expr *nife = new if_expr (ife->location); |
8fdc6c67 RB |
1299 | nife->cond = as_a <c_expr *> (replace_id (ife->cond, id, with)); |
1300 | nife->trueexpr = replace_id (ife->trueexpr, id, with); | |
1301 | if (ife->falseexpr) | |
1302 | nife->falseexpr = replace_id (ife->falseexpr, id, with); | |
1303 | return nife; | |
1304 | } | |
3d2cf79f RB |
1305 | |
1306 | /* For c_expr we simply record a string replacement table which is | |
1307 | applied at code-generation time. */ | |
1308 | if (c_expr *ce = dyn_cast<c_expr *> (o)) | |
1309 | { | |
1310 | vec<c_expr::id_tab> ids = ce->ids.copy (); | |
1311 | ids.safe_push (c_expr::id_tab (id->id, with->id)); | |
0889c52f RB |
1312 | return new c_expr (ce->r, ce->location, |
1313 | ce->code, ce->nr_stmts, ids, ce->capture_ids); | |
3d2cf79f RB |
1314 | } |
1315 | ||
1316 | return o; | |
1317 | } | |
1318 | ||
d4b71b95 RB |
1319 | /* Return true if the binary operator OP is ok for delayed substitution |
1320 | during for lowering. */ | |
1321 | ||
1322 | static bool | |
1323 | binary_ok (operator_id *op) | |
1324 | { | |
1325 | switch (op->code) | |
1326 | { | |
1327 | case PLUS_EXPR: | |
1328 | case MINUS_EXPR: | |
1329 | case MULT_EXPR: | |
1330 | case TRUNC_DIV_EXPR: | |
1331 | case CEIL_DIV_EXPR: | |
1332 | case FLOOR_DIV_EXPR: | |
1333 | case ROUND_DIV_EXPR: | |
1334 | case TRUNC_MOD_EXPR: | |
1335 | case CEIL_MOD_EXPR: | |
1336 | case FLOOR_MOD_EXPR: | |
1337 | case ROUND_MOD_EXPR: | |
1338 | case RDIV_EXPR: | |
1339 | case EXACT_DIV_EXPR: | |
1340 | case MIN_EXPR: | |
1341 | case MAX_EXPR: | |
1342 | case BIT_IOR_EXPR: | |
1343 | case BIT_XOR_EXPR: | |
1344 | case BIT_AND_EXPR: | |
1345 | return true; | |
1346 | default: | |
1347 | return false; | |
1348 | } | |
1349 | } | |
1350 | ||
3d2cf79f RB |
1351 | /* Lower recorded fors for SIN and output to SIMPLIFIERS. */ |
1352 | ||
1353 | static void | |
1354 | lower_for (simplify *sin, vec<simplify *>& simplifiers) | |
1355 | { | |
1356 | vec<vec<user_id *> >& for_vec = sin->for_vec; | |
1357 | unsigned worklist_start = 0; | |
1358 | auto_vec<simplify *> worklist; | |
1359 | worklist.safe_push (sin); | |
1360 | ||
1361 | /* Lower each recorded for separately, operating on the | |
1362 | set of simplifiers created by the previous one. | |
1363 | Lower inner-to-outer so inner for substitutes can refer | |
1364 | to operators replaced by outer fors. */ | |
1365 | for (int fi = for_vec.length () - 1; fi >= 0; --fi) | |
1366 | { | |
1367 | vec<user_id *>& ids = for_vec[fi]; | |
1368 | unsigned n_ids = ids.length (); | |
1369 | unsigned max_n_opers = 0; | |
d4b71b95 | 1370 | bool can_delay_subst = (sin->kind == simplify::SIMPLIFY); |
3d2cf79f | 1371 | for (unsigned i = 0; i < n_ids; ++i) |
d4b71b95 RB |
1372 | { |
1373 | if (ids[i]->substitutes.length () > max_n_opers) | |
1374 | max_n_opers = ids[i]->substitutes.length (); | |
1375 | /* Require that all substitutes are of the same kind so that | |
1376 | if we delay substitution to the result op code generation | |
1377 | can look at the first substitute for deciding things like | |
1378 | types of operands. */ | |
1379 | enum id_base::id_kind kind = ids[i]->substitutes[0]->kind; | |
1380 | for (unsigned j = 0; j < ids[i]->substitutes.length (); ++j) | |
1381 | if (ids[i]->substitutes[j]->kind != kind) | |
1382 | can_delay_subst = false; | |
1383 | else if (operator_id *op | |
1384 | = dyn_cast <operator_id *> (ids[i]->substitutes[j])) | |
1385 | { | |
1386 | operator_id *op0 | |
1387 | = as_a <operator_id *> (ids[i]->substitutes[0]); | |
1388 | if (strcmp (op->tcc, "tcc_comparison") == 0 | |
1389 | && strcmp (op0->tcc, "tcc_comparison") == 0) | |
1390 | ; | |
1391 | /* Unfortunately we can't just allow all tcc_binary. */ | |
1392 | else if (strcmp (op->tcc, "tcc_binary") == 0 | |
1393 | && strcmp (op0->tcc, "tcc_binary") == 0 | |
1394 | && binary_ok (op) | |
1395 | && binary_ok (op0)) | |
1396 | ; | |
1397 | else if ((strcmp (op->id + 1, "SHIFT_EXPR") == 0 | |
1398 | || strcmp (op->id + 1, "ROTATE_EXPR") == 0) | |
1399 | && (strcmp (op0->id + 1, "SHIFT_EXPR") == 0 | |
1400 | || strcmp (op0->id + 1, "ROTATE_EXPR") == 0)) | |
1401 | ; | |
1402 | else | |
1403 | can_delay_subst = false; | |
1404 | } | |
1405 | else if (is_a <fn_id *> (ids[i]->substitutes[j])) | |
1406 | ; | |
1407 | else | |
1408 | can_delay_subst = false; | |
1409 | } | |
3d2cf79f RB |
1410 | |
1411 | unsigned worklist_end = worklist.length (); | |
1412 | for (unsigned si = worklist_start; si < worklist_end; ++si) | |
1413 | { | |
1414 | simplify *s = worklist[si]; | |
1415 | for (unsigned j = 0; j < max_n_opers; ++j) | |
1416 | { | |
1417 | operand *match_op = s->match; | |
1418 | operand *result_op = s->result; | |
8c681247 | 1419 | auto_vec<std::pair<user_id *, id_base *> > subst (n_ids); |
fa74b47a | 1420 | bool skip = false; |
3d2cf79f RB |
1421 | for (unsigned i = 0; i < n_ids; ++i) |
1422 | { | |
1423 | user_id *id = ids[i]; | |
1424 | id_base *oper = id->substitutes[j % id->substitutes.length ()]; | |
fa74b47a RS |
1425 | if (oper == null_id |
1426 | && (contains_id (match_op, id) | |
1427 | || contains_id (result_op, id))) | |
1428 | { | |
1429 | skip = true; | |
1430 | break; | |
1431 | } | |
d4b71b95 | 1432 | subst.quick_push (std::make_pair (id, oper)); |
3d2cf79f | 1433 | match_op = replace_id (match_op, id, oper); |
d4b71b95 RB |
1434 | if (result_op |
1435 | && !can_delay_subst) | |
3d2cf79f | 1436 | result_op = replace_id (result_op, id, oper); |
3d2cf79f | 1437 | } |
fa74b47a | 1438 | if (skip) |
8c681247 TS |
1439 | continue; |
1440 | ||
0889c52f | 1441 | simplify *ns = new simplify (s->kind, match_op, result_op, |
8fdc6c67 | 1442 | vNULL, s->capture_ids); |
d4b71b95 RB |
1443 | ns->for_subst_vec.safe_splice (s->for_subst_vec); |
1444 | if (result_op | |
1445 | && can_delay_subst) | |
1446 | ns->for_subst_vec.safe_splice (subst); | |
8c681247 | 1447 | |
3d2cf79f RB |
1448 | worklist.safe_push (ns); |
1449 | } | |
1450 | } | |
1451 | worklist_start = worklist_end; | |
1452 | } | |
1453 | ||
1454 | /* Copy out the result from the last for lowering. */ | |
1455 | for (unsigned i = worklist_start; i < worklist.length (); ++i) | |
1456 | simplifiers.safe_push (worklist[i]); | |
1457 | } | |
1458 | ||
1459 | /* Lower the AST for everything in SIMPLIFIERS. */ | |
1460 | ||
1461 | static void | |
47b25362 | 1462 | lower (vec<simplify *>& simplifiers, bool gimple) |
3d2cf79f | 1463 | { |
47b25362 | 1464 | auto_vec<simplify *> out_simplifiers; |
3d2cf79f | 1465 | for (unsigned i = 0; i < simplifiers.length (); ++i) |
47b25362 RB |
1466 | lower_opt_convert (simplifiers[i], out_simplifiers); |
1467 | ||
1468 | simplifiers.truncate (0); | |
1469 | for (unsigned i = 0; i < out_simplifiers.length (); ++i) | |
1470 | lower_commutative (out_simplifiers[i], simplifiers); | |
1471 | ||
1472 | out_simplifiers.truncate (0); | |
1473 | if (gimple) | |
1474 | for (unsigned i = 0; i < simplifiers.length (); ++i) | |
1475 | lower_cond (simplifiers[i], out_simplifiers); | |
1476 | else | |
1477 | out_simplifiers.safe_splice (simplifiers); | |
3d2cf79f | 1478 | |
3d2cf79f RB |
1479 | |
1480 | simplifiers.truncate (0); | |
47b25362 RB |
1481 | for (unsigned i = 0; i < out_simplifiers.length (); ++i) |
1482 | lower_for (out_simplifiers[i], simplifiers); | |
3d2cf79f RB |
1483 | } |
1484 | ||
1485 | ||
1486 | ||
1487 | ||
1488 | /* The decision tree built for generating GIMPLE and GENERIC pattern | |
1489 | matching code. It represents the 'match' expression of all | |
1490 | simplifies and has those as its leafs. */ | |
1491 | ||
03038b8b RB |
1492 | struct dt_simplify; |
1493 | ||
1494 | /* A hash-map collecting semantically equivalent leafs in the decision | |
1495 | tree for splitting out to separate functions. */ | |
1496 | struct sinfo | |
1497 | { | |
1498 | dt_simplify *s; | |
1499 | ||
1500 | const char *fname; | |
1501 | unsigned cnt; | |
1502 | }; | |
1503 | ||
76b6ddbf TS |
1504 | struct sinfo_hashmap_traits : simple_hashmap_traits<pointer_hash<dt_simplify>, |
1505 | sinfo *> | |
03038b8b RB |
1506 | { |
1507 | static inline hashval_t hash (const key_type &); | |
1508 | static inline bool equal_keys (const key_type &, const key_type &); | |
1509 | template <typename T> static inline void remove (T &) {} | |
1510 | }; | |
1511 | ||
1512 | typedef hash_map<void * /* unused */, sinfo *, sinfo_hashmap_traits> | |
1513 | sinfo_map_t; | |
1514 | ||
1515 | ||
3d2cf79f RB |
1516 | /* Decision tree base class, used for DT_TRUE and DT_NODE. */ |
1517 | ||
1518 | struct dt_node | |
1519 | { | |
1520 | enum dt_type { DT_NODE, DT_OPERAND, DT_TRUE, DT_MATCH, DT_SIMPLIFY }; | |
1521 | ||
1522 | enum dt_type type; | |
1523 | unsigned level; | |
1524 | vec<dt_node *> kids; | |
1525 | ||
803835de RB |
1526 | /* Statistics. */ |
1527 | unsigned num_leafs; | |
1528 | unsigned total_size; | |
1529 | unsigned max_level; | |
1530 | ||
3d2cf79f RB |
1531 | dt_node (enum dt_type type_): type (type_), level (0), kids (vNULL) {} |
1532 | ||
1533 | dt_node *append_node (dt_node *); | |
1534 | dt_node *append_op (operand *, dt_node *parent = 0, unsigned pos = 0); | |
1535 | dt_node *append_true_op (dt_node *parent = 0, unsigned pos = 0); | |
1536 | dt_node *append_match_op (dt_operand *, dt_node *parent = 0, unsigned pos = 0); | |
1537 | dt_node *append_simplify (simplify *, unsigned, dt_operand **); | |
1538 | ||
c551c21d | 1539 | virtual void gen (FILE *, int, bool) {} |
3d2cf79f | 1540 | |
c551c21d MM |
1541 | void gen_kids (FILE *, int, bool); |
1542 | void gen_kids_1 (FILE *, int, bool, | |
7e015fce RB |
1543 | vec<dt_operand *>, vec<dt_operand *>, vec<dt_operand *>, |
1544 | vec<dt_operand *>, vec<dt_operand *>, vec<dt_node *>); | |
803835de | 1545 | |
03038b8b | 1546 | void analyze (sinfo_map_t &); |
3d2cf79f RB |
1547 | }; |
1548 | ||
1549 | /* Generic decision tree node used for DT_OPERAND and DT_MATCH. */ | |
1550 | ||
1551 | struct dt_operand : public dt_node | |
1552 | { | |
1553 | operand *op; | |
1554 | dt_operand *match_dop; | |
1555 | dt_operand *parent; | |
1556 | unsigned pos; | |
1557 | ||
1558 | dt_operand (enum dt_type type, operand *op_, dt_operand *match_dop_, | |
1559 | dt_operand *parent_ = 0, unsigned pos_ = 0) | |
1560 | : dt_node (type), op (op_), match_dop (match_dop_), | |
1561 | parent (parent_), pos (pos_) {} | |
1562 | ||
c551c21d MM |
1563 | void gen (FILE *, int, bool); |
1564 | unsigned gen_predicate (FILE *, int, const char *, bool); | |
1565 | unsigned gen_match_op (FILE *, int, const char *); | |
3d2cf79f | 1566 | |
c551c21d MM |
1567 | unsigned gen_gimple_expr (FILE *, int); |
1568 | unsigned gen_generic_expr (FILE *, int, const char *); | |
3d2cf79f RB |
1569 | |
1570 | char *get_name (char *); | |
1571 | void gen_opname (char *, unsigned); | |
1572 | }; | |
1573 | ||
1574 | /* Leaf node of the decision tree, used for DT_SIMPLIFY. */ | |
1575 | ||
1576 | struct dt_simplify : public dt_node | |
1577 | { | |
1578 | simplify *s; | |
1579 | unsigned pattern_no; | |
1580 | dt_operand **indexes; | |
03038b8b | 1581 | sinfo *info; |
3d2cf79f RB |
1582 | |
1583 | dt_simplify (simplify *s_, unsigned pattern_no_, dt_operand **indexes_) | |
1584 | : dt_node (DT_SIMPLIFY), s (s_), pattern_no (pattern_no_), | |
03038b8b | 1585 | indexes (indexes_), info (NULL) {} |
3d2cf79f | 1586 | |
8fdc6c67 | 1587 | void gen_1 (FILE *, int, bool, operand *); |
c551c21d | 1588 | void gen (FILE *f, int, bool); |
3d2cf79f RB |
1589 | }; |
1590 | ||
1591 | template<> | |
1592 | template<> | |
1593 | inline bool | |
1594 | is_a_helper <dt_operand *>::test (dt_node *n) | |
1595 | { | |
1596 | return (n->type == dt_node::DT_OPERAND | |
1597 | || n->type == dt_node::DT_MATCH); | |
1598 | } | |
1599 | ||
03038b8b RB |
1600 | template<> |
1601 | template<> | |
1602 | inline bool | |
1603 | is_a_helper <dt_simplify *>::test (dt_node *n) | |
1604 | { | |
1605 | return n->type == dt_node::DT_SIMPLIFY; | |
1606 | } | |
1607 | ||
1608 | ||
1609 | ||
3d2cf79f RB |
1610 | /* A container for the actual decision tree. */ |
1611 | ||
1612 | struct decision_tree | |
1613 | { | |
1614 | dt_node *root; | |
1615 | ||
1616 | void insert (struct simplify *, unsigned); | |
b21ce9ce | 1617 | void gen (FILE *f, bool gimple); |
3d2cf79f RB |
1618 | void print (FILE *f = stderr); |
1619 | ||
1620 | decision_tree () { root = new dt_node (dt_node::DT_NODE); } | |
1621 | ||
1622 | static dt_node *insert_operand (dt_node *, operand *, dt_operand **indexes, | |
1623 | unsigned pos = 0, dt_node *parent = 0); | |
1624 | static dt_node *find_node (vec<dt_node *>&, dt_node *); | |
1625 | static bool cmp_node (dt_node *, dt_node *); | |
1626 | static void print_node (dt_node *, FILE *f = stderr, unsigned = 0); | |
1627 | }; | |
1628 | ||
1629 | /* Compare two AST operands O1 and O2 and return true if they are equal. */ | |
1630 | ||
1631 | bool | |
1632 | cmp_operand (operand *o1, operand *o2) | |
1633 | { | |
1634 | if (!o1 || !o2 || o1->type != o2->type) | |
1635 | return false; | |
1636 | ||
1637 | if (o1->type == operand::OP_PREDICATE) | |
1638 | { | |
1639 | predicate *p1 = as_a<predicate *>(o1); | |
1640 | predicate *p2 = as_a<predicate *>(o2); | |
1641 | return p1->p == p2->p; | |
1642 | } | |
1643 | else if (o1->type == operand::OP_EXPR) | |
1644 | { | |
1645 | expr *e1 = static_cast<expr *>(o1); | |
1646 | expr *e2 = static_cast<expr *>(o2); | |
47b25362 RB |
1647 | return (e1->operation == e2->operation |
1648 | && e1->is_generic == e2->is_generic); | |
3d2cf79f RB |
1649 | } |
1650 | else | |
1651 | return false; | |
1652 | } | |
1653 | ||
1654 | /* Compare two decision tree nodes N1 and N2 and return true if they | |
1655 | are equal. */ | |
1656 | ||
1657 | bool | |
1658 | decision_tree::cmp_node (dt_node *n1, dt_node *n2) | |
1659 | { | |
1660 | if (!n1 || !n2 || n1->type != n2->type) | |
1661 | return false; | |
1662 | ||
7e015fce | 1663 | if (n1 == n2) |
3d2cf79f RB |
1664 | return true; |
1665 | ||
7e015fce RB |
1666 | if (n1->type == dt_node::DT_TRUE) |
1667 | return false; | |
1668 | ||
3d2cf79f RB |
1669 | if (n1->type == dt_node::DT_OPERAND) |
1670 | return cmp_operand ((as_a<dt_operand *> (n1))->op, | |
1671 | (as_a<dt_operand *> (n2))->op); | |
1672 | else if (n1->type == dt_node::DT_MATCH) | |
1673 | return ((as_a<dt_operand *> (n1))->match_dop | |
1674 | == (as_a<dt_operand *> (n2))->match_dop); | |
1675 | return false; | |
1676 | } | |
1677 | ||
1678 | /* Search OPS for a decision tree node like P and return it if found. */ | |
1679 | ||
1680 | dt_node * | |
1681 | decision_tree::find_node (vec<dt_node *>& ops, dt_node *p) | |
1682 | { | |
7e015fce RB |
1683 | /* We can merge adjacent DT_TRUE. */ |
1684 | if (p->type == dt_node::DT_TRUE | |
1685 | && !ops.is_empty () | |
1686 | && ops.last ()->type == dt_node::DT_TRUE) | |
1687 | return ops.last (); | |
1688 | for (int i = ops.length () - 1; i >= 0; --i) | |
1689 | { | |
1690 | /* But we can't merge across DT_TRUE nodes as they serve as | |
1691 | pattern order barriers to make sure that patterns apply | |
1692 | in order of appearance in case multiple matches are possible. */ | |
1693 | if (ops[i]->type == dt_node::DT_TRUE) | |
1694 | return NULL; | |
1695 | if (decision_tree::cmp_node (ops[i], p)) | |
1696 | return ops[i]; | |
1697 | } | |
3d2cf79f RB |
1698 | return NULL; |
1699 | } | |
1700 | ||
1701 | /* Append N to the decision tree if it there is not already an existing | |
1702 | identical child. */ | |
1703 | ||
1704 | dt_node * | |
1705 | dt_node::append_node (dt_node *n) | |
1706 | { | |
1707 | dt_node *kid; | |
1708 | ||
1709 | kid = decision_tree::find_node (kids, n); | |
1710 | if (kid) | |
1711 | return kid; | |
1712 | ||
1713 | kids.safe_push (n); | |
1714 | n->level = this->level + 1; | |
1715 | ||
3d2cf79f RB |
1716 | return n; |
1717 | } | |
1718 | ||
1719 | /* Append OP to the decision tree. */ | |
1720 | ||
1721 | dt_node * | |
1722 | dt_node::append_op (operand *op, dt_node *parent, unsigned pos) | |
1723 | { | |
1724 | dt_operand *parent_ = safe_as_a<dt_operand *> (parent); | |
1725 | dt_operand *n = new dt_operand (DT_OPERAND, op, 0, parent_, pos); | |
1726 | return append_node (n); | |
1727 | } | |
1728 | ||
1729 | /* Append a DT_TRUE decision tree node. */ | |
1730 | ||
1731 | dt_node * | |
1732 | dt_node::append_true_op (dt_node *parent, unsigned pos) | |
1733 | { | |
5609420f | 1734 | dt_operand *parent_ = safe_as_a<dt_operand *> (parent); |
3d2cf79f RB |
1735 | dt_operand *n = new dt_operand (DT_TRUE, 0, 0, parent_, pos); |
1736 | return append_node (n); | |
1737 | } | |
1738 | ||
1739 | /* Append a DT_MATCH decision tree node. */ | |
1740 | ||
1741 | dt_node * | |
1742 | dt_node::append_match_op (dt_operand *match_dop, dt_node *parent, unsigned pos) | |
1743 | { | |
1744 | dt_operand *parent_ = as_a<dt_operand *> (parent); | |
1745 | dt_operand *n = new dt_operand (DT_MATCH, 0, match_dop, parent_, pos); | |
1746 | return append_node (n); | |
1747 | } | |
1748 | ||
1749 | /* Append S to the decision tree. */ | |
1750 | ||
1751 | dt_node * | |
1752 | dt_node::append_simplify (simplify *s, unsigned pattern_no, | |
1753 | dt_operand **indexes) | |
1754 | { | |
1755 | dt_simplify *n = new dt_simplify (s, pattern_no, indexes); | |
eee7b6c4 RB |
1756 | for (unsigned i = 0; i < kids.length (); ++i) |
1757 | if (dt_simplify *s2 = dyn_cast <dt_simplify *> (kids[i])) | |
1758 | { | |
1759 | warning_at (s->match->location, "duplicate pattern"); | |
1760 | warning_at (s2->s->match->location, "previous pattern defined here"); | |
1761 | print_operand (s->match, stderr); | |
1762 | fprintf (stderr, "\n"); | |
1763 | } | |
3d2cf79f RB |
1764 | return append_node (n); |
1765 | } | |
1766 | ||
803835de RB |
1767 | /* Analyze the node and its children. */ |
1768 | ||
1769 | void | |
03038b8b | 1770 | dt_node::analyze (sinfo_map_t &map) |
803835de RB |
1771 | { |
1772 | num_leafs = 0; | |
1773 | total_size = 1; | |
1774 | max_level = level; | |
1775 | ||
1776 | if (type == DT_SIMPLIFY) | |
1777 | { | |
03038b8b RB |
1778 | /* Populate the map of equivalent simplifies. */ |
1779 | dt_simplify *s = as_a <dt_simplify *> (this); | |
1780 | bool existed; | |
1781 | sinfo *&si = map.get_or_insert (s, &existed); | |
1782 | if (!existed) | |
1783 | { | |
1784 | si = new sinfo; | |
1785 | si->s = s; | |
1786 | si->cnt = 1; | |
1787 | si->fname = NULL; | |
1788 | } | |
1789 | else | |
1790 | si->cnt++; | |
1791 | s->info = si; | |
803835de RB |
1792 | num_leafs = 1; |
1793 | return; | |
1794 | } | |
1795 | ||
1796 | for (unsigned i = 0; i < kids.length (); ++i) | |
1797 | { | |
03038b8b | 1798 | kids[i]->analyze (map); |
803835de RB |
1799 | num_leafs += kids[i]->num_leafs; |
1800 | total_size += kids[i]->total_size; | |
1801 | max_level = MAX (max_level, kids[i]->max_level); | |
1802 | } | |
1803 | } | |
1804 | ||
3d2cf79f RB |
1805 | /* Insert O into the decision tree and return the decision tree node found |
1806 | or created. */ | |
1807 | ||
1808 | dt_node * | |
1809 | decision_tree::insert_operand (dt_node *p, operand *o, dt_operand **indexes, | |
1810 | unsigned pos, dt_node *parent) | |
1811 | { | |
1812 | dt_node *q, *elm = 0; | |
1813 | ||
1814 | if (capture *c = dyn_cast<capture *> (o)) | |
1815 | { | |
1816 | unsigned capt_index = c->where; | |
1817 | ||
1818 | if (indexes[capt_index] == 0) | |
1819 | { | |
1820 | if (c->what) | |
1821 | q = insert_operand (p, c->what, indexes, pos, parent); | |
1822 | else | |
1823 | { | |
1824 | q = elm = p->append_true_op (parent, pos); | |
1825 | goto at_assert_elm; | |
1826 | } | |
1827 | // get to the last capture | |
1828 | for (operand *what = c->what; | |
1829 | what && is_a<capture *> (what); | |
1830 | c = as_a<capture *> (what), what = c->what) | |
1831 | ; | |
1832 | ||
1833 | if (!c->what) | |
1834 | { | |
1835 | unsigned cc_index = c->where; | |
1836 | dt_operand *match_op = indexes[cc_index]; | |
1837 | ||
1838 | dt_operand temp (dt_node::DT_TRUE, 0, 0); | |
1839 | elm = decision_tree::find_node (p->kids, &temp); | |
1840 | ||
1841 | if (elm == 0) | |
1842 | { | |
1843 | dt_operand temp (dt_node::DT_MATCH, 0, match_op); | |
1844 | elm = decision_tree::find_node (p->kids, &temp); | |
1845 | } | |
1846 | } | |
1847 | else | |
1848 | { | |
1849 | dt_operand temp (dt_node::DT_OPERAND, c->what, 0); | |
1850 | elm = decision_tree::find_node (p->kids, &temp); | |
1851 | } | |
1852 | ||
1853 | at_assert_elm: | |
1854 | gcc_assert (elm->type == dt_node::DT_TRUE | |
1855 | || elm->type == dt_node::DT_OPERAND | |
1856 | || elm->type == dt_node::DT_MATCH); | |
1857 | indexes[capt_index] = static_cast<dt_operand *> (elm); | |
1858 | return q; | |
1859 | } | |
1860 | else | |
1861 | { | |
1862 | p = p->append_match_op (indexes[capt_index], parent, pos); | |
1863 | if (c->what) | |
1864 | return insert_operand (p, c->what, indexes, 0, p); | |
1865 | else | |
1866 | return p; | |
1867 | } | |
1868 | } | |
1869 | p = p->append_op (o, parent, pos); | |
1870 | q = p; | |
1871 | ||
1872 | if (expr *e = dyn_cast <expr *>(o)) | |
1873 | { | |
1874 | for (unsigned i = 0; i < e->ops.length (); ++i) | |
1875 | q = decision_tree::insert_operand (q, e->ops[i], indexes, i, p); | |
1876 | } | |
1877 | ||
1878 | return q; | |
1879 | } | |
1880 | ||
1881 | /* Insert S into the decision tree. */ | |
1882 | ||
1883 | void | |
1884 | decision_tree::insert (struct simplify *s, unsigned pattern_no) | |
1885 | { | |
3d2cf79f RB |
1886 | dt_operand **indexes = XCNEWVEC (dt_operand *, s->capture_max + 1); |
1887 | dt_node *p = decision_tree::insert_operand (root, s->match, indexes); | |
1888 | p->append_simplify (s, pattern_no, indexes); | |
1889 | } | |
1890 | ||
1891 | /* Debug functions to dump the decision tree. */ | |
1892 | ||
1893 | DEBUG_FUNCTION void | |
1894 | decision_tree::print_node (dt_node *p, FILE *f, unsigned indent) | |
1895 | { | |
1896 | if (p->type == dt_node::DT_NODE) | |
1897 | fprintf (f, "root"); | |
1898 | else | |
1899 | { | |
1900 | fprintf (f, "|"); | |
1901 | for (unsigned i = 0; i < indent; i++) | |
1902 | fprintf (f, "-"); | |
1903 | ||
1904 | if (p->type == dt_node::DT_OPERAND) | |
1905 | { | |
1906 | dt_operand *dop = static_cast<dt_operand *>(p); | |
1907 | print_operand (dop->op, f, true); | |
1908 | } | |
1909 | else if (p->type == dt_node::DT_TRUE) | |
1910 | fprintf (f, "true"); | |
1911 | else if (p->type == dt_node::DT_MATCH) | |
1912 | fprintf (f, "match (%p)", (void *)((as_a<dt_operand *>(p))->match_dop)); | |
1913 | else if (p->type == dt_node::DT_SIMPLIFY) | |
1914 | { | |
1915 | dt_simplify *s = static_cast<dt_simplify *> (p); | |
1916 | fprintf (f, "simplify_%u { ", s->pattern_no); | |
1917 | for (int i = 0; i <= s->s->capture_max; ++i) | |
1918 | fprintf (f, "%p, ", (void *) s->indexes[i]); | |
1919 | fprintf (f, " } "); | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | fprintf (stderr, " (%p), %u, %u\n", (void *) p, p->level, p->kids.length ()); | |
1924 | ||
1925 | for (unsigned i = 0; i < p->kids.length (); ++i) | |
1926 | decision_tree::print_node (p->kids[i], f, indent + 2); | |
1927 | } | |
1928 | ||
1929 | DEBUG_FUNCTION void | |
1930 | decision_tree::print (FILE *f) | |
1931 | { | |
1932 | return decision_tree::print_node (root, f); | |
1933 | } | |
1934 | ||
1935 | ||
47b25362 RB |
1936 | /* For GENERIC we have to take care of wrapping multiple-used |
1937 | expressions with side-effects in save_expr and preserve side-effects | |
1938 | of expressions with omit_one_operand. Analyze captures in | |
1939 | match, result and with expressions and perform early-outs | |
1940 | on the outermost match expression operands for cases we cannot | |
1941 | handle. */ | |
1942 | ||
1943 | struct capture_info | |
1944 | { | |
53a19317 | 1945 | capture_info (simplify *s, operand *, bool); |
47b25362 | 1946 | void walk_match (operand *o, unsigned toplevel_arg, bool, bool); |
8fdc6c67 | 1947 | bool walk_result (operand *o, bool, operand *); |
47b25362 RB |
1948 | void walk_c_expr (c_expr *); |
1949 | ||
1950 | struct cinfo | |
1951 | { | |
1952 | bool expr_p; | |
1953 | bool cse_p; | |
1954 | bool force_no_side_effects_p; | |
44fc0a51 | 1955 | bool force_single_use; |
47b25362 RB |
1956 | bool cond_expr_cond_p; |
1957 | unsigned long toplevel_msk; | |
2d3f4bf7 RB |
1958 | unsigned match_use_count; |
1959 | unsigned result_use_count; | |
fa138f6e | 1960 | unsigned same_as; |
53a19317 | 1961 | capture *c; |
47b25362 RB |
1962 | }; |
1963 | ||
1964 | auto_vec<cinfo> info; | |
1965 | unsigned long force_no_side_effects; | |
53a19317 | 1966 | bool gimple; |
47b25362 RB |
1967 | }; |
1968 | ||
1969 | /* Analyze captures in S. */ | |
1970 | ||
53a19317 | 1971 | capture_info::capture_info (simplify *s, operand *result, bool gimple_) |
47b25362 | 1972 | { |
53a19317 RB |
1973 | gimple = gimple_; |
1974 | ||
47b25362 | 1975 | expr *e; |
8fdc6c67 | 1976 | if (s->kind == simplify::MATCH) |
47b25362 RB |
1977 | { |
1978 | force_no_side_effects = -1; | |
1979 | return; | |
1980 | } | |
1981 | ||
1982 | force_no_side_effects = 0; | |
1983 | info.safe_grow_cleared (s->capture_max + 1); | |
fa138f6e RB |
1984 | for (int i = 0; i <= s->capture_max; ++i) |
1985 | info[i].same_as = i; | |
1986 | ||
47b25362 RB |
1987 | e = as_a <expr *> (s->match); |
1988 | for (unsigned i = 0; i < e->ops.length (); ++i) | |
1989 | walk_match (e->ops[i], i, | |
1990 | (i != 0 && *e->operation == COND_EXPR) | |
1991 | || *e->operation == TRUTH_ANDIF_EXPR | |
1992 | || *e->operation == TRUTH_ORIF_EXPR, | |
1993 | i == 0 | |
1994 | && (*e->operation == COND_EXPR | |
1995 | || *e->operation == VEC_COND_EXPR)); | |
1996 | ||
8fdc6c67 | 1997 | walk_result (s->result, false, result); |
47b25362 RB |
1998 | } |
1999 | ||
2000 | /* Analyze captures in the match expression piece O. */ | |
2001 | ||
2002 | void | |
2003 | capture_info::walk_match (operand *o, unsigned toplevel_arg, | |
2004 | bool conditional_p, bool cond_expr_cond_p) | |
2005 | { | |
2006 | if (capture *c = dyn_cast <capture *> (o)) | |
2007 | { | |
2d799646 | 2008 | unsigned where = c->where; |
2d3f4bf7 | 2009 | info[where].match_use_count++; |
2d799646 RB |
2010 | info[where].toplevel_msk |= 1 << toplevel_arg; |
2011 | info[where].force_no_side_effects_p |= conditional_p; | |
2012 | info[where].cond_expr_cond_p |= cond_expr_cond_p; | |
53a19317 RB |
2013 | if (!info[where].c) |
2014 | info[where].c = c; | |
2d799646 RB |
2015 | if (!c->what) |
2016 | return; | |
2017 | /* Recurse to exprs and captures. */ | |
2018 | if (is_a <capture *> (c->what) | |
2019 | || is_a <expr *> (c->what)) | |
2020 | walk_match (c->what, toplevel_arg, conditional_p, false); | |
2021 | /* We need to look past multiple captures to find a captured | |
2022 | expression as with conditional converts two captures | |
fa138f6e RB |
2023 | can be collapsed onto the same expression. Also collect |
2024 | what captures capture the same thing. */ | |
2d799646 | 2025 | while (c->what && is_a <capture *> (c->what)) |
fa138f6e RB |
2026 | { |
2027 | c = as_a <capture *> (c->what); | |
2028 | if (info[c->where].same_as != c->where | |
2029 | && info[c->where].same_as != info[where].same_as) | |
2030 | fatal_at (c->location, "cannot handle this collapsed capture"); | |
2031 | info[c->where].same_as = info[where].same_as; | |
2032 | } | |
2d799646 | 2033 | /* Mark expr (non-leaf) captures and forced single-use exprs. */ |
44fc0a51 | 2034 | expr *e; |
47b25362 | 2035 | if (c->what |
44fc0a51 | 2036 | && (e = dyn_cast <expr *> (c->what))) |
47b25362 | 2037 | { |
2d799646 RB |
2038 | info[where].expr_p = true; |
2039 | info[where].force_single_use |= e->force_single_use; | |
47b25362 RB |
2040 | } |
2041 | } | |
2042 | else if (expr *e = dyn_cast <expr *> (o)) | |
2043 | { | |
2044 | for (unsigned i = 0; i < e->ops.length (); ++i) | |
2045 | { | |
2046 | bool cond_p = conditional_p; | |
2047 | bool cond_expr_cond_p = false; | |
2048 | if (i != 0 && *e->operation == COND_EXPR) | |
2049 | cond_p = true; | |
2050 | else if (*e->operation == TRUTH_ANDIF_EXPR | |
2051 | || *e->operation == TRUTH_ORIF_EXPR) | |
2052 | cond_p = true; | |
2053 | if (i == 0 | |
2054 | && (*e->operation == COND_EXPR | |
2055 | || *e->operation == VEC_COND_EXPR)) | |
2056 | cond_expr_cond_p = true; | |
2057 | walk_match (e->ops[i], toplevel_arg, cond_p, cond_expr_cond_p); | |
2058 | } | |
2059 | } | |
2060 | else if (is_a <predicate *> (o)) | |
2061 | { | |
2062 | /* Mark non-captured leafs toplevel arg for checking. */ | |
2063 | force_no_side_effects |= 1 << toplevel_arg; | |
53a19317 RB |
2064 | if (verbose >= 1 |
2065 | && !gimple) | |
2066 | warning_at (o->location, | |
2067 | "forcing no side-effects on possibly lost leaf"); | |
47b25362 RB |
2068 | } |
2069 | else | |
2070 | gcc_unreachable (); | |
2071 | } | |
2072 | ||
8fdc6c67 RB |
2073 | /* Analyze captures in the result expression piece O. Return true |
2074 | if RESULT was visited in one of the children. Only visit | |
2075 | non-if/with children if they are rooted on RESULT. */ | |
47b25362 | 2076 | |
8fdc6c67 RB |
2077 | bool |
2078 | capture_info::walk_result (operand *o, bool conditional_p, operand *result) | |
47b25362 RB |
2079 | { |
2080 | if (capture *c = dyn_cast <capture *> (o)) | |
2081 | { | |
fa138f6e RB |
2082 | unsigned where = info[c->where].same_as; |
2083 | info[where].result_use_count++; | |
47b25362 RB |
2084 | /* If we substitute an expression capture we don't know |
2085 | which captures this will end up using (well, we don't | |
2086 | compute that). Force the uses to be side-effect free | |
2087 | which means forcing the toplevels that reach the | |
2088 | expression side-effect free. */ | |
fa138f6e RB |
2089 | if (info[where].expr_p) |
2090 | force_no_side_effects |= info[where].toplevel_msk; | |
026c3cfd | 2091 | /* Mark CSE capture uses as forced to have no side-effects. */ |
47b25362 RB |
2092 | if (c->what |
2093 | && is_a <expr *> (c->what)) | |
2094 | { | |
fa138f6e | 2095 | info[where].cse_p = true; |
8fdc6c67 | 2096 | walk_result (c->what, true, result); |
47b25362 RB |
2097 | } |
2098 | } | |
2099 | else if (expr *e = dyn_cast <expr *> (o)) | |
2100 | { | |
d4b71b95 RB |
2101 | id_base *opr = e->operation; |
2102 | if (user_id *uid = dyn_cast <user_id *> (opr)) | |
2103 | opr = uid->substitutes[0]; | |
47b25362 RB |
2104 | for (unsigned i = 0; i < e->ops.length (); ++i) |
2105 | { | |
2106 | bool cond_p = conditional_p; | |
2107 | if (i != 0 && *e->operation == COND_EXPR) | |
2108 | cond_p = true; | |
2109 | else if (*e->operation == TRUTH_ANDIF_EXPR | |
2110 | || *e->operation == TRUTH_ORIF_EXPR) | |
2111 | cond_p = true; | |
8fdc6c67 RB |
2112 | walk_result (e->ops[i], cond_p, result); |
2113 | } | |
2114 | } | |
2115 | else if (if_expr *e = dyn_cast <if_expr *> (o)) | |
2116 | { | |
2117 | /* 'if' conditions should be all fine. */ | |
2118 | if (e->trueexpr == result) | |
2119 | { | |
2120 | walk_result (e->trueexpr, false, result); | |
2121 | return true; | |
2122 | } | |
2123 | if (e->falseexpr == result) | |
2124 | { | |
2125 | walk_result (e->falseexpr, false, result); | |
2126 | return true; | |
47b25362 | 2127 | } |
8fdc6c67 RB |
2128 | bool res = false; |
2129 | if (is_a <if_expr *> (e->trueexpr) | |
2130 | || is_a <with_expr *> (e->trueexpr)) | |
2131 | res |= walk_result (e->trueexpr, false, result); | |
2132 | if (e->falseexpr | |
2133 | && (is_a <if_expr *> (e->falseexpr) | |
2134 | || is_a <with_expr *> (e->falseexpr))) | |
2135 | res |= walk_result (e->falseexpr, false, result); | |
2136 | return res; | |
2137 | } | |
2138 | else if (with_expr *e = dyn_cast <with_expr *> (o)) | |
2139 | { | |
2140 | bool res = (e->subexpr == result); | |
2141 | if (res | |
2142 | || is_a <if_expr *> (e->subexpr) | |
2143 | || is_a <with_expr *> (e->subexpr)) | |
2144 | res |= walk_result (e->subexpr, false, result); | |
2145 | if (res) | |
2146 | walk_c_expr (e->with); | |
2147 | return res; | |
47b25362 RB |
2148 | } |
2149 | else if (c_expr *e = dyn_cast <c_expr *> (o)) | |
2150 | walk_c_expr (e); | |
2151 | else | |
2152 | gcc_unreachable (); | |
8fdc6c67 RB |
2153 | |
2154 | return false; | |
47b25362 RB |
2155 | } |
2156 | ||
2157 | /* Look for captures in the C expr E. */ | |
2158 | ||
2159 | void | |
2160 | capture_info::walk_c_expr (c_expr *e) | |
2161 | { | |
53a19317 RB |
2162 | /* Give up for C exprs mentioning captures not inside TREE_TYPE, |
2163 | TREE_REAL_CST, TREE_CODE or a predicate where they cannot | |
2164 | really escape through. */ | |
47b25362 RB |
2165 | unsigned p_depth = 0; |
2166 | for (unsigned i = 0; i < e->code.length (); ++i) | |
2167 | { | |
2168 | const cpp_token *t = &e->code[i]; | |
2169 | const cpp_token *n = i < e->code.length () - 1 ? &e->code[i+1] : NULL; | |
53a19317 | 2170 | id_base *id; |
47b25362 | 2171 | if (t->type == CPP_NAME |
53a19317 RB |
2172 | && (strcmp ((const char *)CPP_HASHNODE |
2173 | (t->val.node.node)->ident.str, "TREE_TYPE") == 0 | |
2174 | || strcmp ((const char *)CPP_HASHNODE | |
2175 | (t->val.node.node)->ident.str, "TREE_CODE") == 0 | |
2176 | || strcmp ((const char *)CPP_HASHNODE | |
2177 | (t->val.node.node)->ident.str, "TREE_REAL_CST") == 0 | |
2178 | || ((id = get_operator ((const char *)CPP_HASHNODE | |
2179 | (t->val.node.node)->ident.str)) | |
2180 | && is_a <predicate_id *> (id))) | |
47b25362 RB |
2181 | && n->type == CPP_OPEN_PAREN) |
2182 | p_depth++; | |
2183 | else if (t->type == CPP_CLOSE_PAREN | |
2184 | && p_depth > 0) | |
2185 | p_depth--; | |
2186 | else if (p_depth == 0 | |
2187 | && t->type == CPP_ATSIGN | |
2188 | && (n->type == CPP_NUMBER | |
2189 | || n->type == CPP_NAME) | |
2190 | && !(n->flags & PREV_WHITE)) | |
2191 | { | |
2192 | const char *id; | |
2193 | if (n->type == CPP_NUMBER) | |
2194 | id = (const char *)n->val.str.text; | |
2195 | else | |
2196 | id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str; | |
fa138f6e RB |
2197 | unsigned where = *e->capture_ids->get(id); |
2198 | info[info[where].same_as].force_no_side_effects_p = true; | |
53a19317 RB |
2199 | if (verbose >= 1 |
2200 | && !gimple) | |
2201 | warning_at (t, "capture escapes"); | |
47b25362 RB |
2202 | } |
2203 | } | |
2204 | } | |
2205 | ||
3d2cf79f RB |
2206 | |
2207 | /* Code generation off the decision tree and the refered AST nodes. */ | |
2208 | ||
2209 | bool | |
2210 | is_conversion (id_base *op) | |
2211 | { | |
2212 | return (*op == CONVERT_EXPR | |
2213 | || *op == NOP_EXPR | |
2214 | || *op == FLOAT_EXPR | |
2215 | || *op == FIX_TRUNC_EXPR | |
2216 | || *op == VIEW_CONVERT_EXPR); | |
2217 | } | |
2218 | ||
2219 | /* Get the type to be used for generating operands of OP from the | |
2220 | various sources. */ | |
2221 | ||
2222 | static const char * | |
2223 | get_operand_type (id_base *op, const char *in_type, | |
2224 | const char *expr_type, | |
2225 | const char *other_oprnd_type) | |
2226 | { | |
2227 | /* Generally operands whose type does not match the type of the | |
2228 | expression generated need to know their types but match and | |
2229 | thus can fall back to 'other_oprnd_type'. */ | |
2230 | if (is_conversion (op)) | |
2231 | return other_oprnd_type; | |
2232 | else if (*op == REALPART_EXPR | |
2233 | || *op == IMAGPART_EXPR) | |
2234 | return other_oprnd_type; | |
2235 | else if (is_a <operator_id *> (op) | |
2236 | && strcmp (as_a <operator_id *> (op)->tcc, "tcc_comparison") == 0) | |
2237 | return other_oprnd_type; | |
2238 | else | |
2239 | { | |
2240 | /* Otherwise all types should match - choose one in order of | |
2241 | preference. */ | |
2242 | if (expr_type) | |
2243 | return expr_type; | |
2244 | else if (in_type) | |
2245 | return in_type; | |
2246 | else | |
2247 | return other_oprnd_type; | |
2248 | } | |
2249 | } | |
2250 | ||
2251 | /* Generate transform code for an expression. */ | |
2252 | ||
2253 | void | |
c551c21d MM |
2254 | expr::gen_transform (FILE *f, int indent, const char *dest, bool gimple, |
2255 | int depth, const char *in_type, capture_info *cinfo, | |
a3ca1bc5 | 2256 | dt_operand **indexes, int) |
3d2cf79f | 2257 | { |
d4b71b95 RB |
2258 | id_base *opr = operation; |
2259 | /* When we delay operator substituting during lowering of fors we | |
2260 | make sure that for code-gen purposes the effects of each substitute | |
2261 | are the same. Thus just look at that. */ | |
2262 | if (user_id *uid = dyn_cast <user_id *> (opr)) | |
2263 | opr = uid->substitutes[0]; | |
2264 | ||
2265 | bool conversion_p = is_conversion (opr); | |
3d2cf79f RB |
2266 | const char *type = expr_type; |
2267 | char optype[64]; | |
2268 | if (type) | |
2269 | /* If there was a type specification in the pattern use it. */ | |
2270 | ; | |
2271 | else if (conversion_p) | |
2272 | /* For conversions we need to build the expression using the | |
2273 | outer type passed in. */ | |
2274 | type = in_type; | |
d4b71b95 RB |
2275 | else if (*opr == REALPART_EXPR |
2276 | || *opr == IMAGPART_EXPR) | |
3d2cf79f RB |
2277 | { |
2278 | /* __real and __imag use the component type of its operand. */ | |
2279 | sprintf (optype, "TREE_TYPE (TREE_TYPE (ops%d[0]))", depth); | |
2280 | type = optype; | |
2281 | } | |
d4b71b95 RB |
2282 | else if (is_a <operator_id *> (opr) |
2283 | && !strcmp (as_a <operator_id *> (opr)->tcc, "tcc_comparison")) | |
3d2cf79f RB |
2284 | { |
2285 | /* comparisons use boolean_type_node (or what gets in), but | |
2286 | their operands need to figure out the types themselves. */ | |
30934c5d RB |
2287 | if (in_type) |
2288 | type = in_type; | |
2289 | else | |
2290 | { | |
2291 | sprintf (optype, "boolean_type_node"); | |
2292 | type = optype; | |
2293 | } | |
2294 | in_type = NULL; | |
3d2cf79f | 2295 | } |
d4b71b95 RB |
2296 | else if (*opr == COND_EXPR |
2297 | || *opr == VEC_COND_EXPR) | |
7026b8df MG |
2298 | { |
2299 | /* Conditions are of the same type as their first alternative. */ | |
2300 | sprintf (optype, "TREE_TYPE (ops%d[1])", depth); | |
2301 | type = optype; | |
2302 | } | |
3d2cf79f RB |
2303 | else |
2304 | { | |
2305 | /* Other operations are of the same type as their first operand. */ | |
2306 | sprintf (optype, "TREE_TYPE (ops%d[0])", depth); | |
2307 | type = optype; | |
2308 | } | |
2309 | if (!type) | |
38b52b2f | 2310 | fatal_at (location, "cannot determine type of operand"); |
3d2cf79f | 2311 | |
c551c21d MM |
2312 | fprintf_indent (f, indent, "{\n"); |
2313 | indent += 2; | |
2314 | fprintf_indent (f, indent, "tree ops%d[%u], res;\n", depth, ops.length ()); | |
3d2cf79f RB |
2315 | char op0type[64]; |
2316 | snprintf (op0type, 64, "TREE_TYPE (ops%d[0])", depth); | |
2317 | for (unsigned i = 0; i < ops.length (); ++i) | |
2318 | { | |
2319 | char dest[32]; | |
c551c21d | 2320 | snprintf (dest, 32, "ops%d[%u]", depth, i); |
3d2cf79f | 2321 | const char *optype |
d4b71b95 | 2322 | = get_operand_type (opr, in_type, expr_type, |
3d2cf79f | 2323 | i == 0 ? NULL : op0type); |
c551c21d MM |
2324 | ops[i]->gen_transform (f, indent, dest, gimple, depth + 1, optype, |
2325 | cinfo, indexes, | |
a3ca1bc5 RB |
2326 | (*opr == COND_EXPR |
2327 | || *opr == VEC_COND_EXPR) && i == 0 ? 1 : 2); | |
3d2cf79f RB |
2328 | } |
2329 | ||
d4b71b95 | 2330 | const char *opr_name; |
32dfd2e0 | 2331 | if (*operation == CONVERT_EXPR) |
d4b71b95 | 2332 | opr_name = "NOP_EXPR"; |
32dfd2e0 | 2333 | else |
d4b71b95 | 2334 | opr_name = operation->id; |
32dfd2e0 | 2335 | |
3d2cf79f RB |
2336 | if (gimple) |
2337 | { | |
d4b71b95 | 2338 | if (*opr == CONVERT_EXPR) |
c551c21d MM |
2339 | { |
2340 | fprintf_indent (f, indent, | |
2341 | "if (%s != TREE_TYPE (ops%d[0])\n", | |
2342 | type, depth); | |
2343 | fprintf_indent (f, indent, | |
2344 | " && !useless_type_conversion_p (%s, TREE_TYPE (ops%d[0])))\n", | |
2345 | type, depth); | |
2346 | fprintf_indent (f, indent + 2, "{\n"); | |
2347 | indent += 4; | |
2348 | } | |
c09a3914 RB |
2349 | /* ??? Building a stmt can fail for various reasons here, seq being |
2350 | NULL or the stmt referencing SSA names occuring in abnormal PHIs. | |
2351 | So if we fail here we should continue matching other patterns. */ | |
d4b71b95 | 2352 | fprintf_indent (f, indent, "code_helper tem_code = %s;\n", opr_name); |
c551c21d | 2353 | fprintf_indent (f, indent, "tree tem_ops[3] = { "); |
3d2cf79f | 2354 | for (unsigned i = 0; i < ops.length (); ++i) |
c09a3914 RB |
2355 | fprintf (f, "ops%d[%u]%s", depth, i, |
2356 | i == ops.length () - 1 ? " };\n" : ", "); | |
c551c21d MM |
2357 | fprintf_indent (f, indent, |
2358 | "gimple_resimplify%d (lseq, &tem_code, %s, tem_ops, valueize);\n", | |
2359 | ops.length (), type); | |
2360 | fprintf_indent (f, indent, | |
2361 | "res = maybe_push_res_to_seq (tem_code, %s, tem_ops, lseq);\n", | |
2362 | type); | |
2363 | fprintf_indent (f, indent, | |
2364 | "if (!res) return false;\n"); | |
d4b71b95 | 2365 | if (*opr == CONVERT_EXPR) |
c551c21d MM |
2366 | { |
2367 | indent -= 4; | |
2368 | fprintf_indent (f, indent, " }\n"); | |
2369 | fprintf_indent (f, indent, "else\n"); | |
2370 | fprintf_indent (f, indent, " res = ops%d[0];\n", depth); | |
2371 | } | |
3d2cf79f RB |
2372 | } |
2373 | else | |
2374 | { | |
d4b71b95 | 2375 | if (*opr == CONVERT_EXPR) |
c551c21d MM |
2376 | { |
2377 | fprintf_indent (f, indent, "if (TREE_TYPE (ops%d[0]) != %s)\n", | |
2378 | depth, type); | |
2379 | indent += 2; | |
2380 | } | |
d4b71b95 | 2381 | if (opr->kind == id_base::CODE) |
c551c21d | 2382 | fprintf_indent (f, indent, "res = fold_build%d_loc (loc, %s, %s", |
d4b71b95 | 2383 | ops.length(), opr_name, type); |
3d2cf79f | 2384 | else |
e4cdf6a8 RB |
2385 | { |
2386 | fprintf_indent (f, indent, "{\n"); | |
c9e926ce RS |
2387 | fprintf_indent (f, indent, " res = maybe_build_call_expr_loc (loc, " |
2388 | "%s, %s, %d", opr_name, type, ops.length()); | |
e4cdf6a8 | 2389 | } |
3d2cf79f RB |
2390 | for (unsigned i = 0; i < ops.length (); ++i) |
2391 | fprintf (f, ", ops%d[%u]", depth, i); | |
2392 | fprintf (f, ");\n"); | |
e4cdf6a8 | 2393 | if (opr->kind != id_base::CODE) |
c9e926ce RS |
2394 | { |
2395 | fprintf_indent (f, indent, " if (!res)\n"); | |
2396 | fprintf_indent (f, indent, " return NULL_TREE;\n"); | |
2397 | fprintf_indent (f, indent, "}\n"); | |
2398 | } | |
d4b71b95 | 2399 | if (*opr == CONVERT_EXPR) |
c551c21d MM |
2400 | { |
2401 | indent -= 2; | |
2402 | fprintf_indent (f, indent, "else\n"); | |
2403 | fprintf_indent (f, indent, " res = ops%d[0];\n", depth); | |
2404 | } | |
3d2cf79f | 2405 | } |
c551c21d MM |
2406 | fprintf_indent (f, indent, "%s = res;\n", dest); |
2407 | indent -= 2; | |
2408 | fprintf_indent (f, indent, "}\n"); | |
3d2cf79f RB |
2409 | } |
2410 | ||
2411 | /* Generate code for a c_expr which is either the expression inside | |
2412 | an if statement or a sequence of statements which computes a | |
2413 | result to be stored to DEST. */ | |
2414 | ||
2415 | void | |
c551c21d | 2416 | c_expr::gen_transform (FILE *f, int indent, const char *dest, |
47b25362 | 2417 | bool, int, const char *, capture_info *, |
a3ca1bc5 | 2418 | dt_operand **, int) |
3d2cf79f RB |
2419 | { |
2420 | if (dest && nr_stmts == 1) | |
c551c21d | 2421 | fprintf_indent (f, indent, "%s = ", dest); |
3d2cf79f RB |
2422 | |
2423 | unsigned stmt_nr = 1; | |
2424 | for (unsigned i = 0; i < code.length (); ++i) | |
2425 | { | |
2426 | const cpp_token *token = &code[i]; | |
2427 | ||
2428 | /* Replace captures for code-gen. */ | |
2429 | if (token->type == CPP_ATSIGN) | |
2430 | { | |
2431 | const cpp_token *n = &code[i+1]; | |
2432 | if ((n->type == CPP_NUMBER | |
2433 | || n->type == CPP_NAME) | |
2434 | && !(n->flags & PREV_WHITE)) | |
2435 | { | |
2436 | if (token->flags & PREV_WHITE) | |
2437 | fputc (' ', f); | |
2438 | const char *id; | |
2439 | if (n->type == CPP_NUMBER) | |
2440 | id = (const char *)n->val.str.text; | |
2441 | else | |
2442 | id = (const char *)CPP_HASHNODE (n->val.node.node)->ident.str; | |
c56f494f RB |
2443 | unsigned *cid = capture_ids->get (id); |
2444 | if (!cid) | |
2445 | fatal_at (token, "unknown capture id"); | |
2446 | fprintf (f, "captures[%u]", *cid); | |
3d2cf79f RB |
2447 | ++i; |
2448 | continue; | |
2449 | } | |
2450 | } | |
2451 | ||
2452 | if (token->flags & PREV_WHITE) | |
2453 | fputc (' ', f); | |
2454 | ||
2455 | if (token->type == CPP_NAME) | |
2456 | { | |
2457 | const char *id = (const char *) NODE_NAME (token->val.node.node); | |
2458 | unsigned j; | |
2459 | for (j = 0; j < ids.length (); ++j) | |
2460 | { | |
2461 | if (strcmp (id, ids[j].id) == 0) | |
2462 | { | |
2463 | fprintf (f, "%s", ids[j].oper); | |
2464 | break; | |
2465 | } | |
2466 | } | |
2467 | if (j < ids.length ()) | |
2468 | continue; | |
2469 | } | |
2470 | ||
2471 | /* Output the token as string. */ | |
2472 | char *tk = (char *)cpp_token_as_text (r, token); | |
2473 | fputs (tk, f); | |
2474 | ||
2475 | if (token->type == CPP_SEMICOLON) | |
2476 | { | |
2477 | stmt_nr++; | |
c551c21d | 2478 | fputc ('\n', f); |
3d2cf79f | 2479 | if (dest && stmt_nr == nr_stmts) |
c551c21d | 2480 | fprintf_indent (f, indent, "%s = ", dest); |
3d2cf79f RB |
2481 | } |
2482 | } | |
2483 | } | |
2484 | ||
2485 | /* Generate transform code for a capture. */ | |
2486 | ||
2487 | void | |
c551c21d MM |
2488 | capture::gen_transform (FILE *f, int indent, const char *dest, bool gimple, |
2489 | int depth, const char *in_type, capture_info *cinfo, | |
a3ca1bc5 | 2490 | dt_operand **indexes, int cond_handling) |
3d2cf79f RB |
2491 | { |
2492 | if (what && is_a<expr *> (what)) | |
2493 | { | |
2494 | if (indexes[where] == 0) | |
2495 | { | |
2496 | char buf[20]; | |
2497 | sprintf (buf, "captures[%u]", where); | |
c551c21d MM |
2498 | what->gen_transform (f, indent, buf, gimple, depth, in_type, |
2499 | cinfo, NULL); | |
3d2cf79f RB |
2500 | } |
2501 | } | |
2502 | ||
c551c21d | 2503 | fprintf_indent (f, indent, "%s = captures[%u];\n", dest, where); |
47b25362 RB |
2504 | |
2505 | /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal | |
a3ca1bc5 RB |
2506 | with substituting a capture of that. */ |
2507 | if (gimple | |
2508 | && cond_handling != 0 | |
47b25362 | 2509 | && cinfo->info[where].cond_expr_cond_p) |
c551c21d | 2510 | { |
a3ca1bc5 RB |
2511 | /* If substituting into a cond_expr condition, unshare. */ |
2512 | if (cond_handling == 1) | |
2513 | fprintf_indent (f, indent, "%s = unshare_expr (%s);\n", dest, dest); | |
2514 | /* If substituting elsewhere we might need to decompose it. */ | |
2515 | else if (cond_handling == 2) | |
2516 | { | |
2517 | /* ??? Returning false here will also not allow any other patterns | |
2518 | to match unless this generator was split out. */ | |
2519 | fprintf_indent (f, indent, "if (COMPARISON_CLASS_P (%s))\n", dest); | |
2520 | fprintf_indent (f, indent, " {\n"); | |
2521 | fprintf_indent (f, indent, " if (!seq) return false;\n"); | |
2522 | fprintf_indent (f, indent, " %s = gimple_build (seq," | |
2523 | " TREE_CODE (%s)," | |
2524 | " TREE_TYPE (%s), TREE_OPERAND (%s, 0)," | |
2525 | " TREE_OPERAND (%s, 1));\n", | |
2526 | dest, dest, dest, dest, dest); | |
2527 | fprintf_indent (f, indent, " }\n"); | |
2528 | } | |
c551c21d | 2529 | } |
3d2cf79f RB |
2530 | } |
2531 | ||
2532 | /* Return the name of the operand representing the decision tree node. | |
2533 | Use NAME as space to generate it. */ | |
2534 | ||
2535 | char * | |
2536 | dt_operand::get_name (char *name) | |
2537 | { | |
2538 | if (!parent) | |
2539 | sprintf (name, "t"); | |
2540 | else if (parent->level == 1) | |
2541 | sprintf (name, "op%u", pos); | |
2542 | else if (parent->type == dt_node::DT_MATCH) | |
2543 | return parent->get_name (name); | |
2544 | else | |
2545 | sprintf (name, "o%u%u", parent->level, pos); | |
2546 | return name; | |
2547 | } | |
2548 | ||
2549 | /* Fill NAME with the operand name at position POS. */ | |
2550 | ||
2551 | void | |
2552 | dt_operand::gen_opname (char *name, unsigned pos) | |
2553 | { | |
2554 | if (!parent) | |
2555 | sprintf (name, "op%u", pos); | |
2556 | else | |
2557 | sprintf (name, "o%u%u", level, pos); | |
2558 | } | |
2559 | ||
2560 | /* Generate matching code for the decision tree operand which is | |
2561 | a predicate. */ | |
2562 | ||
2563 | unsigned | |
c551c21d | 2564 | dt_operand::gen_predicate (FILE *f, int indent, const char *opname, bool gimple) |
3d2cf79f RB |
2565 | { |
2566 | predicate *p = as_a <predicate *> (op); | |
2567 | ||
2568 | if (p->p->matchers.exists ()) | |
2569 | { | |
2570 | /* If this is a predicate generated from a pattern mangle its | |
2571 | name and pass on the valueize hook. */ | |
2572 | if (gimple) | |
c551c21d MM |
2573 | fprintf_indent (f, indent, "if (gimple_%s (%s, valueize))\n", |
2574 | p->p->id, opname); | |
3d2cf79f | 2575 | else |
c551c21d | 2576 | fprintf_indent (f, indent, "if (tree_%s (%s))\n", p->p->id, opname); |
3d2cf79f RB |
2577 | } |
2578 | else | |
c551c21d MM |
2579 | fprintf_indent (f, indent, "if (%s (%s))\n", p->p->id, opname); |
2580 | fprintf_indent (f, indent + 2, "{\n"); | |
3d2cf79f RB |
2581 | return 1; |
2582 | } | |
2583 | ||
2584 | /* Generate matching code for the decision tree operand which is | |
2585 | a capture-match. */ | |
2586 | ||
2587 | unsigned | |
c551c21d | 2588 | dt_operand::gen_match_op (FILE *f, int indent, const char *opname) |
3d2cf79f RB |
2589 | { |
2590 | char match_opname[20]; | |
2591 | match_dop->get_name (match_opname); | |
c551c21d MM |
2592 | fprintf_indent (f, indent, "if (%s == %s || operand_equal_p (%s, %s, 0))\n", |
2593 | opname, match_opname, opname, match_opname); | |
2594 | fprintf_indent (f, indent + 2, "{\n"); | |
3d2cf79f RB |
2595 | return 1; |
2596 | } | |
2597 | ||
2598 | /* Generate GIMPLE matching code for the decision tree operand. */ | |
2599 | ||
2600 | unsigned | |
c551c21d | 2601 | dt_operand::gen_gimple_expr (FILE *f, int indent) |
3d2cf79f RB |
2602 | { |
2603 | expr *e = static_cast<expr *> (op); | |
2604 | id_base *id = e->operation; | |
2605 | unsigned n_ops = e->ops.length (); | |
2606 | ||
2607 | for (unsigned i = 0; i < n_ops; ++i) | |
2608 | { | |
2609 | char child_opname[20]; | |
2610 | gen_opname (child_opname, i); | |
2611 | ||
2612 | if (id->kind == id_base::CODE) | |
2613 | { | |
47b25362 RB |
2614 | if (e->is_generic |
2615 | || *id == REALPART_EXPR || *id == IMAGPART_EXPR | |
3d2cf79f RB |
2616 | || *id == BIT_FIELD_REF || *id == VIEW_CONVERT_EXPR) |
2617 | { | |
2618 | /* ??? If this is a memory operation we can't (and should not) | |
2619 | match this. The only sensible operand types are | |
2620 | SSA names and invariants. */ | |
c551c21d | 2621 | fprintf_indent (f, indent, |
c8136c36 | 2622 | "tree %s = TREE_OPERAND (gimple_assign_rhs1 (def), %i);\n", |
c551c21d MM |
2623 | child_opname, i); |
2624 | fprintf_indent (f, indent, | |
2625 | "if ((TREE_CODE (%s) == SSA_NAME\n", | |
2626 | child_opname); | |
2627 | fprintf_indent (f, indent, | |
2628 | " || is_gimple_min_invariant (%s))\n", | |
2629 | child_opname); | |
2630 | fprintf_indent (f, indent, | |
2631 | " && (%s = do_valueize (valueize, %s)))\n", | |
2632 | child_opname, child_opname); | |
2633 | fprintf_indent (f, indent, | |
2634 | " {\n"); | |
2635 | indent += 4; | |
3d2cf79f RB |
2636 | continue; |
2637 | } | |
2638 | else | |
c551c21d | 2639 | fprintf_indent (f, indent, |
c8136c36 | 2640 | "tree %s = gimple_assign_rhs%u (def);\n", |
c551c21d | 2641 | child_opname, i + 1); |
3d2cf79f RB |
2642 | } |
2643 | else | |
c551c21d | 2644 | fprintf_indent (f, indent, |
c8136c36 | 2645 | "tree %s = gimple_call_arg (def, %u);\n", |
c551c21d MM |
2646 | child_opname, i); |
2647 | fprintf_indent (f, indent, | |
2648 | "if ((%s = do_valueize (valueize, %s)))\n", | |
2649 | child_opname, child_opname); | |
2650 | fprintf_indent (f, indent, " {\n"); | |
2651 | indent += 4; | |
3d2cf79f | 2652 | } |
805a5406 RB |
2653 | /* While the toplevel operands are canonicalized by the caller |
2654 | after valueizing operands of sub-expressions we have to | |
2655 | re-canonicalize operand order. */ | |
2656 | if (operator_id *code = dyn_cast <operator_id *> (id)) | |
2657 | { | |
2658 | /* ??? We can't canonicalize tcc_comparison operands here | |
2659 | because that requires changing the comparison code which | |
2660 | we already matched... */ | |
2661 | if (commutative_tree_code (code->code) | |
2662 | || commutative_ternary_tree_code (code->code)) | |
2663 | { | |
2664 | char child_opname0[20], child_opname1[20]; | |
2665 | gen_opname (child_opname0, 0); | |
2666 | gen_opname (child_opname1, 1); | |
c551c21d MM |
2667 | fprintf_indent (f, indent, |
2668 | "if (tree_swap_operands_p (%s, %s, false))\n", | |
2669 | child_opname0, child_opname1); | |
2670 | fprintf_indent (f, indent, | |
2671 | " std::swap (%s, %s);\n", | |
2672 | child_opname0, child_opname1); | |
805a5406 RB |
2673 | } |
2674 | } | |
3d2cf79f RB |
2675 | |
2676 | return n_ops; | |
2677 | } | |
2678 | ||
2679 | /* Generate GENERIC matching code for the decision tree operand. */ | |
2680 | ||
2681 | unsigned | |
c551c21d | 2682 | dt_operand::gen_generic_expr (FILE *f, int indent, const char *opname) |
3d2cf79f RB |
2683 | { |
2684 | expr *e = static_cast<expr *> (op); | |
2685 | unsigned n_ops = e->ops.length (); | |
2686 | ||
2687 | for (unsigned i = 0; i < n_ops; ++i) | |
2688 | { | |
2689 | char child_opname[20]; | |
2690 | gen_opname (child_opname, i); | |
2691 | ||
2692 | if (e->operation->kind == id_base::CODE) | |
c551c21d MM |
2693 | fprintf_indent (f, indent, "tree %s = TREE_OPERAND (%s, %u);\n", |
2694 | child_opname, opname, i); | |
3d2cf79f | 2695 | else |
c551c21d MM |
2696 | fprintf_indent (f, indent, "tree %s = CALL_EXPR_ARG (%s, %u);\n", |
2697 | child_opname, opname, i); | |
3d2cf79f RB |
2698 | } |
2699 | ||
2700 | return 0; | |
2701 | } | |
2702 | ||
2703 | /* Generate matching code for the children of the decision tree node. */ | |
2704 | ||
2705 | void | |
c551c21d | 2706 | dt_node::gen_kids (FILE *f, int indent, bool gimple) |
3d2cf79f RB |
2707 | { |
2708 | auto_vec<dt_operand *> gimple_exprs; | |
2709 | auto_vec<dt_operand *> generic_exprs; | |
2710 | auto_vec<dt_operand *> fns; | |
2711 | auto_vec<dt_operand *> generic_fns; | |
2712 | auto_vec<dt_operand *> preds; | |
2713 | auto_vec<dt_node *> others; | |
3d2cf79f RB |
2714 | |
2715 | for (unsigned i = 0; i < kids.length (); ++i) | |
2716 | { | |
2717 | if (kids[i]->type == dt_node::DT_OPERAND) | |
2718 | { | |
2719 | dt_operand *op = as_a<dt_operand *> (kids[i]); | |
2720 | if (expr *e = dyn_cast <expr *> (op->op)) | |
2721 | { | |
10230017 RB |
2722 | if (e->ops.length () == 0 |
2723 | && (!gimple || !(*e->operation == CONSTRUCTOR))) | |
3d2cf79f RB |
2724 | generic_exprs.safe_push (op); |
2725 | else if (e->operation->kind == id_base::FN) | |
2726 | { | |
2727 | if (gimple) | |
2728 | fns.safe_push (op); | |
2729 | else | |
2730 | generic_fns.safe_push (op); | |
2731 | } | |
2732 | else if (e->operation->kind == id_base::PREDICATE) | |
2733 | preds.safe_push (op); | |
2734 | else | |
2735 | { | |
c954de7f | 2736 | if (gimple && !e->is_generic) |
3d2cf79f RB |
2737 | gimple_exprs.safe_push (op); |
2738 | else | |
2739 | generic_exprs.safe_push (op); | |
2740 | } | |
2741 | } | |
2742 | else if (op->op->type == operand::OP_PREDICATE) | |
2743 | others.safe_push (kids[i]); | |
2744 | else | |
2745 | gcc_unreachable (); | |
2746 | } | |
69746629 | 2747 | else if (kids[i]->type == dt_node::DT_SIMPLIFY) |
3d2cf79f | 2748 | others.safe_push (kids[i]); |
69746629 RB |
2749 | else if (kids[i]->type == dt_node::DT_MATCH |
2750 | || kids[i]->type == dt_node::DT_TRUE) | |
7e015fce RB |
2751 | { |
2752 | /* A DT_TRUE operand serves as a barrier - generate code now | |
69746629 RB |
2753 | for what we have collected sofar. |
2754 | Like DT_TRUE, DT_MATCH serves as a barrier as it can cause | |
2755 | dependent matches to get out-of-order. Generate code now | |
7e015fce | 2756 | for what we have collected sofar. */ |
c551c21d | 2757 | gen_kids_1 (f, indent, gimple, gimple_exprs, generic_exprs, |
7e015fce RB |
2758 | fns, generic_fns, preds, others); |
2759 | /* And output the true operand itself. */ | |
c551c21d | 2760 | kids[i]->gen (f, indent, gimple); |
7e015fce RB |
2761 | gimple_exprs.truncate (0); |
2762 | generic_exprs.truncate (0); | |
2763 | fns.truncate (0); | |
2764 | generic_fns.truncate (0); | |
2765 | preds.truncate (0); | |
2766 | others.truncate (0); | |
2767 | } | |
3d2cf79f RB |
2768 | else |
2769 | gcc_unreachable (); | |
2770 | } | |
2771 | ||
7e015fce | 2772 | /* Generate code for the remains. */ |
c551c21d | 2773 | gen_kids_1 (f, indent, gimple, gimple_exprs, generic_exprs, |
7e015fce RB |
2774 | fns, generic_fns, preds, others); |
2775 | } | |
2776 | ||
2777 | /* Generate matching code for the children of the decision tree node. */ | |
2778 | ||
2779 | void | |
c551c21d | 2780 | dt_node::gen_kids_1 (FILE *f, int indent, bool gimple, |
7e015fce RB |
2781 | vec<dt_operand *> gimple_exprs, |
2782 | vec<dt_operand *> generic_exprs, | |
2783 | vec<dt_operand *> fns, | |
2784 | vec<dt_operand *> generic_fns, | |
2785 | vec<dt_operand *> preds, | |
2786 | vec<dt_node *> others) | |
2787 | { | |
3d2cf79f RB |
2788 | char buf[128]; |
2789 | char *kid_opname = buf; | |
2790 | ||
2791 | unsigned exprs_len = gimple_exprs.length (); | |
2792 | unsigned gexprs_len = generic_exprs.length (); | |
2793 | unsigned fns_len = fns.length (); | |
2794 | unsigned gfns_len = generic_fns.length (); | |
2795 | ||
2796 | if (exprs_len || fns_len || gexprs_len || gfns_len) | |
2797 | { | |
2798 | if (exprs_len) | |
2799 | gimple_exprs[0]->get_name (kid_opname); | |
2800 | else if (fns_len) | |
2801 | fns[0]->get_name (kid_opname); | |
2802 | else if (gfns_len) | |
2803 | generic_fns[0]->get_name (kid_opname); | |
2804 | else | |
2805 | generic_exprs[0]->get_name (kid_opname); | |
2806 | ||
c551c21d MM |
2807 | fprintf_indent (f, indent, "switch (TREE_CODE (%s))\n", kid_opname); |
2808 | fprintf_indent (f, indent, " {\n"); | |
1651e9b1 | 2809 | indent += 2; |
3d2cf79f RB |
2810 | } |
2811 | ||
2812 | if (exprs_len || fns_len) | |
2813 | { | |
c551c21d MM |
2814 | fprintf_indent (f, indent, |
2815 | "case SSA_NAME:\n"); | |
2816 | fprintf_indent (f, indent, | |
2817 | " if (do_valueize (valueize, %s) != NULL_TREE)\n", | |
2818 | kid_opname); | |
2819 | fprintf_indent (f, indent, | |
2820 | " {\n"); | |
2821 | fprintf_indent (f, indent, | |
355fe088 | 2822 | " gimple *def_stmt = SSA_NAME_DEF_STMT (%s);\n", |
c551c21d MM |
2823 | kid_opname); |
2824 | ||
2825 | indent += 6; | |
3d2cf79f RB |
2826 | if (exprs_len) |
2827 | { | |
c551c21d | 2828 | fprintf_indent (f, indent, |
c8136c36 | 2829 | "if (gassign *def = dyn_cast <gassign *> (def_stmt))\n"); |
c551c21d | 2830 | fprintf_indent (f, indent, |
c8136c36 | 2831 | " switch (gimple_assign_rhs_code (def))\n"); |
c551c21d MM |
2832 | indent += 4; |
2833 | fprintf_indent (f, indent, "{\n"); | |
3d2cf79f RB |
2834 | for (unsigned i = 0; i < exprs_len; ++i) |
2835 | { | |
2836 | expr *e = as_a <expr *> (gimple_exprs[i]->op); | |
2837 | id_base *op = e->operation; | |
2838 | if (*op == CONVERT_EXPR || *op == NOP_EXPR) | |
1651e9b1 | 2839 | fprintf_indent (f, indent, "CASE_CONVERT:\n"); |
3d2cf79f | 2840 | else |
1651e9b1 RB |
2841 | fprintf_indent (f, indent, "case %s:\n", op->id); |
2842 | fprintf_indent (f, indent, " {\n"); | |
2843 | gimple_exprs[i]->gen (f, indent + 4, true); | |
2844 | fprintf_indent (f, indent, " break;\n"); | |
2845 | fprintf_indent (f, indent, " }\n"); | |
3d2cf79f | 2846 | } |
1651e9b1 | 2847 | fprintf_indent (f, indent, "default:;\n"); |
c551c21d | 2848 | fprintf_indent (f, indent, "}\n"); |
1ec1fa94 | 2849 | indent -= 4; |
3d2cf79f RB |
2850 | } |
2851 | ||
2852 | if (fns_len) | |
2853 | { | |
c8136c36 | 2854 | fprintf_indent (f, indent, |
c9e926ce RS |
2855 | "%sif (gcall *def = dyn_cast <gcall *>" |
2856 | " (def_stmt))\n", | |
c8136c36 | 2857 | exprs_len ? "else " : ""); |
c551c21d | 2858 | fprintf_indent (f, indent, |
c9e926ce | 2859 | " switch (gimple_call_combined_fn (def))\n"); |
3d2cf79f | 2860 | |
c9e926ce RS |
2861 | indent += 4; |
2862 | fprintf_indent (f, indent, "{\n"); | |
3d2cf79f RB |
2863 | for (unsigned i = 0; i < fns_len; ++i) |
2864 | { | |
2865 | expr *e = as_a <expr *>(fns[i]->op); | |
c551c21d MM |
2866 | fprintf_indent (f, indent, "case %s:\n", e->operation->id); |
2867 | fprintf_indent (f, indent, " {\n"); | |
2868 | fns[i]->gen (f, indent + 4, true); | |
2869 | fprintf_indent (f, indent, " break;\n"); | |
2870 | fprintf_indent (f, indent, " }\n"); | |
3d2cf79f RB |
2871 | } |
2872 | ||
c551c21d | 2873 | fprintf_indent (f, indent, "default:;\n"); |
1ec1fa94 | 2874 | fprintf_indent (f, indent, "}\n"); |
c9e926ce | 2875 | indent -= 4; |
3d2cf79f RB |
2876 | } |
2877 | ||
c551c21d MM |
2878 | indent -= 6; |
2879 | fprintf_indent (f, indent, " }\n"); | |
2880 | fprintf_indent (f, indent, " break;\n"); | |
3d2cf79f RB |
2881 | } |
2882 | ||
2883 | for (unsigned i = 0; i < generic_exprs.length (); ++i) | |
2884 | { | |
2885 | expr *e = as_a <expr *>(generic_exprs[i]->op); | |
2886 | id_base *op = e->operation; | |
2887 | if (*op == CONVERT_EXPR || *op == NOP_EXPR) | |
c551c21d | 2888 | fprintf_indent (f, indent, "CASE_CONVERT:\n"); |
3d2cf79f | 2889 | else |
c551c21d MM |
2890 | fprintf_indent (f, indent, "case %s:\n", op->id); |
2891 | fprintf_indent (f, indent, " {\n"); | |
2892 | generic_exprs[i]->gen (f, indent + 4, gimple); | |
2893 | fprintf_indent (f, indent, " break;\n"); | |
2894 | fprintf_indent (f, indent, " }\n"); | |
3d2cf79f RB |
2895 | } |
2896 | ||
2897 | if (gfns_len) | |
2898 | { | |
c551c21d MM |
2899 | fprintf_indent (f, indent, |
2900 | "case CALL_EXPR:\n"); | |
2901 | fprintf_indent (f, indent, | |
c9e926ce | 2902 | " switch (get_call_combined_fn (%s))\n", |
c551c21d MM |
2903 | kid_opname); |
2904 | fprintf_indent (f, indent, | |
c9e926ce RS |
2905 | " {\n"); |
2906 | indent += 4; | |
3d2cf79f RB |
2907 | |
2908 | for (unsigned j = 0; j < generic_fns.length (); ++j) | |
2909 | { | |
2910 | expr *e = as_a <expr *>(generic_fns[j]->op); | |
2911 | gcc_assert (e->operation->kind == id_base::FN); | |
2912 | ||
c551c21d MM |
2913 | fprintf_indent (f, indent, "case %s:\n", e->operation->id); |
2914 | fprintf_indent (f, indent, " {\n"); | |
2915 | generic_fns[j]->gen (f, indent + 4, false); | |
2916 | fprintf_indent (f, indent, " break;\n"); | |
2917 | fprintf_indent (f, indent, " }\n"); | |
3d2cf79f | 2918 | } |
c9e926ce | 2919 | fprintf_indent (f, indent, "default:;\n"); |
3d2cf79f | 2920 | |
c9e926ce RS |
2921 | indent -= 4; |
2922 | fprintf_indent (f, indent, " }\n"); | |
2923 | fprintf_indent (f, indent, " break;\n"); | |
3d2cf79f RB |
2924 | } |
2925 | ||
2926 | /* Close switch (TREE_CODE ()). */ | |
2927 | if (exprs_len || fns_len || gexprs_len || gfns_len) | |
c551c21d MM |
2928 | { |
2929 | indent -= 4; | |
2930 | fprintf_indent (f, indent, " default:;\n"); | |
1ec1fa94 | 2931 | fprintf_indent (f, indent, " }\n"); |
c551c21d | 2932 | } |
3d2cf79f RB |
2933 | |
2934 | for (unsigned i = 0; i < preds.length (); ++i) | |
2935 | { | |
2936 | expr *e = as_a <expr *> (preds[i]->op); | |
2937 | predicate_id *p = as_a <predicate_id *> (e->operation); | |
2938 | preds[i]->get_name (kid_opname); | |
c551c21d MM |
2939 | fprintf_indent (f, indent, "tree %s_pops[%d];\n", kid_opname, p->nargs); |
2940 | fprintf_indent (f, indent, "if (%s_%s (%s, %s_pops%s))\n", | |
3d2cf79f RB |
2941 | gimple ? "gimple" : "tree", |
2942 | p->id, kid_opname, kid_opname, | |
2943 | gimple ? ", valueize" : ""); | |
c551c21d | 2944 | fprintf_indent (f, indent, " {\n"); |
3d2cf79f RB |
2945 | for (int j = 0; j < p->nargs; ++j) |
2946 | { | |
2947 | char child_opname[20]; | |
2948 | preds[i]->gen_opname (child_opname, j); | |
c551c21d MM |
2949 | fprintf_indent (f, indent + 4, "tree %s = %s_pops[%d];\n", |
2950 | child_opname, kid_opname, j); | |
3d2cf79f | 2951 | } |
c551c21d | 2952 | preds[i]->gen_kids (f, indent + 4, gimple); |
3d2cf79f RB |
2953 | fprintf (f, "}\n"); |
2954 | } | |
2955 | ||
2956 | for (unsigned i = 0; i < others.length (); ++i) | |
c551c21d | 2957 | others[i]->gen (f, indent, gimple); |
3d2cf79f RB |
2958 | } |
2959 | ||
2960 | /* Generate matching code for the decision tree operand. */ | |
2961 | ||
2962 | void | |
c551c21d | 2963 | dt_operand::gen (FILE *f, int indent, bool gimple) |
3d2cf79f RB |
2964 | { |
2965 | char opname[20]; | |
2966 | get_name (opname); | |
2967 | ||
2968 | unsigned n_braces = 0; | |
2969 | ||
2970 | if (type == DT_OPERAND) | |
2971 | switch (op->type) | |
2972 | { | |
2973 | case operand::OP_PREDICATE: | |
c551c21d | 2974 | n_braces = gen_predicate (f, indent, opname, gimple); |
3d2cf79f RB |
2975 | break; |
2976 | ||
2977 | case operand::OP_EXPR: | |
2978 | if (gimple) | |
c551c21d | 2979 | n_braces = gen_gimple_expr (f, indent); |
3d2cf79f | 2980 | else |
c551c21d | 2981 | n_braces = gen_generic_expr (f, indent, opname); |
3d2cf79f RB |
2982 | break; |
2983 | ||
2984 | default: | |
2985 | gcc_unreachable (); | |
2986 | } | |
2987 | else if (type == DT_TRUE) | |
2988 | ; | |
2989 | else if (type == DT_MATCH) | |
c551c21d | 2990 | n_braces = gen_match_op (f, indent, opname); |
3d2cf79f RB |
2991 | else |
2992 | gcc_unreachable (); | |
2993 | ||
c551c21d MM |
2994 | indent += 4 * n_braces; |
2995 | gen_kids (f, indent, gimple); | |
3d2cf79f RB |
2996 | |
2997 | for (unsigned i = 0; i < n_braces; ++i) | |
c551c21d MM |
2998 | { |
2999 | indent -= 4; | |
3000 | if (indent < 0) | |
3001 | indent = 0; | |
3002 | fprintf_indent (f, indent, " }\n"); | |
3003 | } | |
3d2cf79f RB |
3004 | } |
3005 | ||
e0ee10ed | 3006 | |
3d2cf79f RB |
3007 | /* Generate code for the '(if ...)', '(with ..)' and actual transform |
3008 | step of a '(simplify ...)' or '(match ...)'. This handles everything | |
8fdc6c67 RB |
3009 | that is not part of the decision tree (simplify->match). |
3010 | Main recursive worker. */ | |
3d2cf79f RB |
3011 | |
3012 | void | |
8fdc6c67 | 3013 | dt_simplify::gen_1 (FILE *f, int indent, bool gimple, operand *result) |
3d2cf79f | 3014 | { |
8fdc6c67 | 3015 | if (result) |
3d2cf79f | 3016 | { |
8fdc6c67 | 3017 | if (with_expr *w = dyn_cast <with_expr *> (result)) |
3d2cf79f | 3018 | { |
8fdc6c67 RB |
3019 | fprintf_indent (f, indent, "{\n"); |
3020 | indent += 4; | |
0889c52f | 3021 | output_line_directive (f, w->location); |
8fdc6c67 RB |
3022 | w->with->gen_transform (f, indent, NULL, true, 1, "type", NULL); |
3023 | gen_1 (f, indent, gimple, w->subexpr); | |
3024 | indent -= 4; | |
3025 | fprintf_indent (f, indent, "}\n"); | |
3026 | return; | |
3027 | } | |
3028 | else if (if_expr *ife = dyn_cast <if_expr *> (result)) | |
3029 | { | |
0889c52f | 3030 | output_line_directive (f, ife->location); |
8fdc6c67 RB |
3031 | fprintf_indent (f, indent, "if ("); |
3032 | ife->cond->gen_transform (f, indent, NULL, true, 1, "type", NULL); | |
3033 | fprintf (f, ")\n"); | |
3034 | fprintf_indent (f, indent + 2, "{\n"); | |
3035 | indent += 4; | |
3036 | gen_1 (f, indent, gimple, ife->trueexpr); | |
3037 | indent -= 4; | |
3038 | fprintf_indent (f, indent + 2, "}\n"); | |
3039 | if (ife->falseexpr) | |
3d2cf79f | 3040 | { |
8fdc6c67 RB |
3041 | fprintf_indent (f, indent, "else\n"); |
3042 | fprintf_indent (f, indent + 2, "{\n"); | |
c551c21d | 3043 | indent += 4; |
8fdc6c67 RB |
3044 | gen_1 (f, indent, gimple, ife->falseexpr); |
3045 | indent -= 4; | |
3046 | fprintf_indent (f, indent + 2, "}\n"); | |
3d2cf79f | 3047 | } |
8fdc6c67 | 3048 | return; |
3d2cf79f | 3049 | } |
3d2cf79f RB |
3050 | } |
3051 | ||
e0ee10ed RB |
3052 | /* Analyze captures and perform early-outs on the incoming arguments |
3053 | that cover cases we cannot handle. */ | |
53a19317 | 3054 | capture_info cinfo (s, result, gimple); |
8fdc6c67 | 3055 | if (s->kind == simplify::SIMPLIFY) |
e0ee10ed | 3056 | { |
44fc0a51 RB |
3057 | if (!gimple) |
3058 | { | |
3059 | for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i) | |
3060 | if (cinfo.force_no_side_effects & (1 << i)) | |
53a19317 RB |
3061 | { |
3062 | fprintf_indent (f, indent, | |
3063 | "if (TREE_SIDE_EFFECTS (op%d)) return NULL_TREE;\n", | |
3064 | i); | |
3065 | if (verbose >= 1) | |
3066 | warning_at (as_a <expr *> (s->match)->ops[i]->location, | |
3067 | "forcing toplevel operand to have no " | |
3068 | "side-effects"); | |
3069 | } | |
44fc0a51 RB |
3070 | for (int i = 0; i <= s->capture_max; ++i) |
3071 | if (cinfo.info[i].cse_p) | |
3072 | ; | |
3073 | else if (cinfo.info[i].force_no_side_effects_p | |
3074 | && (cinfo.info[i].toplevel_msk | |
3075 | & cinfo.force_no_side_effects) == 0) | |
53a19317 RB |
3076 | { |
3077 | fprintf_indent (f, indent, | |
3078 | "if (TREE_SIDE_EFFECTS (captures[%d])) " | |
3079 | "return NULL_TREE;\n", i); | |
3080 | if (verbose >= 1) | |
3081 | warning_at (cinfo.info[i].c->location, | |
3082 | "forcing captured operand to have no " | |
3083 | "side-effects"); | |
3084 | } | |
44fc0a51 RB |
3085 | else if ((cinfo.info[i].toplevel_msk |
3086 | & cinfo.force_no_side_effects) != 0) | |
3087 | /* Mark capture as having no side-effects if we had to verify | |
3088 | that via forced toplevel operand checks. */ | |
3089 | cinfo.info[i].force_no_side_effects_p = true; | |
3090 | } | |
3091 | if (gimple) | |
3092 | { | |
3093 | /* Force single-use restriction by only allowing simple | |
3094 | results via setting seq to NULL. */ | |
c551c21d | 3095 | fprintf_indent (f, indent, "gimple_seq *lseq = seq;\n"); |
44fc0a51 RB |
3096 | bool first_p = true; |
3097 | for (int i = 0; i <= s->capture_max; ++i) | |
3098 | if (cinfo.info[i].force_single_use) | |
3099 | { | |
3100 | if (first_p) | |
3101 | { | |
c551c21d MM |
3102 | fprintf_indent (f, indent, "if (lseq\n"); |
3103 | fprintf_indent (f, indent, " && ("); | |
44fc0a51 RB |
3104 | first_p = false; |
3105 | } | |
3106 | else | |
c551c21d MM |
3107 | { |
3108 | fprintf (f, "\n"); | |
3109 | fprintf_indent (f, indent, " || "); | |
3110 | } | |
44fc0a51 RB |
3111 | fprintf (f, "!single_use (captures[%d])", i); |
3112 | } | |
3113 | if (!first_p) | |
c551c21d MM |
3114 | { |
3115 | fprintf (f, "))\n"); | |
3116 | fprintf_indent (f, indent, " lseq = NULL;\n"); | |
3117 | } | |
44fc0a51 | 3118 | } |
e0ee10ed RB |
3119 | } |
3120 | ||
c551c21d | 3121 | fprintf_indent (f, indent, "if (dump_file && (dump_flags & TDF_DETAILS)) " |
3d2cf79f | 3122 | "fprintf (dump_file, \"Applying pattern "); |
0889c52f RB |
3123 | output_line_directive (f, |
3124 | result ? result->location : s->match->location, true); | |
3d2cf79f RB |
3125 | fprintf (f, ", %%s:%%d\\n\", __FILE__, __LINE__);\n"); |
3126 | ||
e0ee10ed | 3127 | if (!result) |
3d2cf79f RB |
3128 | { |
3129 | /* If there is no result then this is a predicate implementation. */ | |
c551c21d | 3130 | fprintf_indent (f, indent, "return true;\n"); |
3d2cf79f RB |
3131 | } |
3132 | else if (gimple) | |
3133 | { | |
e0ee10ed RB |
3134 | /* For GIMPLE simply drop NON_LVALUE_EXPR (which only appears |
3135 | in outermost position). */ | |
3136 | if (result->type == operand::OP_EXPR | |
3137 | && *as_a <expr *> (result)->operation == NON_LVALUE_EXPR) | |
3138 | result = as_a <expr *> (result)->ops[0]; | |
3139 | if (result->type == operand::OP_EXPR) | |
3d2cf79f | 3140 | { |
e0ee10ed | 3141 | expr *e = as_a <expr *> (result); |
d4b71b95 RB |
3142 | id_base *opr = e->operation; |
3143 | bool is_predicate = false; | |
3144 | /* When we delay operator substituting during lowering of fors we | |
3145 | make sure that for code-gen purposes the effects of each substitute | |
3146 | are the same. Thus just look at that. */ | |
3147 | if (user_id *uid = dyn_cast <user_id *> (opr)) | |
3148 | opr = uid->substitutes[0]; | |
3149 | else if (is_a <predicate_id *> (opr)) | |
3150 | is_predicate = true; | |
3d2cf79f | 3151 | if (!is_predicate) |
c551c21d MM |
3152 | fprintf_indent (f, indent, "*res_code = %s;\n", |
3153 | *e->operation == CONVERT_EXPR | |
3154 | ? "NOP_EXPR" : e->operation->id); | |
3d2cf79f RB |
3155 | for (unsigned j = 0; j < e->ops.length (); ++j) |
3156 | { | |
3157 | char dest[32]; | |
c551c21d | 3158 | snprintf (dest, 32, "res_ops[%d]", j); |
3d2cf79f | 3159 | const char *optype |
d4b71b95 | 3160 | = get_operand_type (opr, |
3d2cf79f | 3161 | "type", e->expr_type, |
c551c21d | 3162 | j == 0 ? NULL : "TREE_TYPE (res_ops[0])"); |
47b25362 | 3163 | /* We need to expand GENERIC conditions we captured from |
a3ca1bc5 RB |
3164 | COND_EXPRs and we need to unshare them when substituting |
3165 | into COND_EXPRs. */ | |
3166 | int cond_handling = 0; | |
3167 | if (!is_predicate) | |
3168 | cond_handling = ((*opr == COND_EXPR | |
3169 | || *opr == VEC_COND_EXPR) && j == 0) ? 1 : 2; | |
c551c21d | 3170 | e->ops[j]->gen_transform (f, indent, dest, true, 1, optype, |
a3ca1bc5 | 3171 | &cinfo, indexes, cond_handling); |
3d2cf79f RB |
3172 | } |
3173 | ||
3174 | /* Re-fold the toplevel result. It's basically an embedded | |
3175 | gimple_build w/o actually building the stmt. */ | |
3176 | if (!is_predicate) | |
c551c21d MM |
3177 | fprintf_indent (f, indent, |
3178 | "gimple_resimplify%d (lseq, res_code, type, " | |
3179 | "res_ops, valueize);\n", e->ops.length ()); | |
3d2cf79f | 3180 | } |
e0ee10ed RB |
3181 | else if (result->type == operand::OP_CAPTURE |
3182 | || result->type == operand::OP_C_EXPR) | |
3d2cf79f | 3183 | { |
c551c21d | 3184 | result->gen_transform (f, indent, "res_ops[0]", true, 1, "type", |
a3ca1bc5 | 3185 | &cinfo, indexes); |
c551c21d | 3186 | fprintf_indent (f, indent, "*res_code = TREE_CODE (res_ops[0]);\n"); |
47b25362 RB |
3187 | if (is_a <capture *> (result) |
3188 | && cinfo.info[as_a <capture *> (result)->where].cond_expr_cond_p) | |
3189 | { | |
3190 | /* ??? Stupid tcc_comparison GENERIC trees in COND_EXPRs. Deal | |
3191 | with substituting a capture of that. */ | |
c551c21d MM |
3192 | fprintf_indent (f, indent, |
3193 | "if (COMPARISON_CLASS_P (res_ops[0]))\n"); | |
3194 | fprintf_indent (f, indent, | |
3195 | " {\n"); | |
3196 | fprintf_indent (f, indent, | |
3197 | " tree tem = res_ops[0];\n"); | |
3198 | fprintf_indent (f, indent, | |
3199 | " res_ops[0] = TREE_OPERAND (tem, 0);\n"); | |
3200 | fprintf_indent (f, indent, | |
3201 | " res_ops[1] = TREE_OPERAND (tem, 1);\n"); | |
3202 | fprintf_indent (f, indent, | |
3203 | " }\n"); | |
47b25362 | 3204 | } |
3d2cf79f RB |
3205 | } |
3206 | else | |
3207 | gcc_unreachable (); | |
c551c21d | 3208 | fprintf_indent (f, indent, "return true;\n"); |
3d2cf79f RB |
3209 | } |
3210 | else /* GENERIC */ | |
3211 | { | |
e0ee10ed RB |
3212 | bool is_predicate = false; |
3213 | if (result->type == operand::OP_EXPR) | |
3d2cf79f | 3214 | { |
e0ee10ed | 3215 | expr *e = as_a <expr *> (result); |
d4b71b95 RB |
3216 | id_base *opr = e->operation; |
3217 | /* When we delay operator substituting during lowering of fors we | |
3218 | make sure that for code-gen purposes the effects of each substitute | |
3219 | are the same. Thus just look at that. */ | |
3220 | if (user_id *uid = dyn_cast <user_id *> (opr)) | |
3221 | opr = uid->substitutes[0]; | |
3222 | else if (is_a <predicate_id *> (opr)) | |
3223 | is_predicate = true; | |
e0ee10ed | 3224 | /* Search for captures used multiple times in the result expression |
2d3f4bf7 RB |
3225 | and wrap them in a SAVE_EXPR. Allow as many uses as in the |
3226 | original expression. */ | |
e0ee10ed RB |
3227 | if (!is_predicate) |
3228 | for (int i = 0; i < s->capture_max + 1; ++i) | |
3229 | { | |
2d3f4bf7 RB |
3230 | if (cinfo.info[i].same_as != (unsigned)i |
3231 | || cinfo.info[i].cse_p) | |
fa138f6e | 3232 | continue; |
2d3f4bf7 RB |
3233 | if (cinfo.info[i].result_use_count |
3234 | > cinfo.info[i].match_use_count) | |
6be52f62 | 3235 | fprintf_indent (f, indent, |
7b2eca00 RB |
3236 | "if (! tree_invariant_p (captures[%d])) " |
3237 | "return NULL_TREE;\n", i); | |
e0ee10ed | 3238 | } |
3d2cf79f RB |
3239 | for (unsigned j = 0; j < e->ops.length (); ++j) |
3240 | { | |
3241 | char dest[32]; | |
3242 | if (is_predicate) | |
3243 | snprintf (dest, 32, "res_ops[%d]", j); | |
3244 | else | |
3245 | { | |
c551c21d MM |
3246 | fprintf_indent (f, indent, "tree res_op%d;\n", j); |
3247 | snprintf (dest, 32, "res_op%d", j); | |
3d2cf79f RB |
3248 | } |
3249 | const char *optype | |
d4b71b95 | 3250 | = get_operand_type (opr, |
3d2cf79f RB |
3251 | "type", e->expr_type, |
3252 | j == 0 | |
3253 | ? NULL : "TREE_TYPE (res_op0)"); | |
c551c21d | 3254 | e->ops[j]->gen_transform (f, indent, dest, false, 1, optype, |
47b25362 | 3255 | &cinfo, indexes); |
3d2cf79f | 3256 | } |
e0ee10ed | 3257 | if (is_predicate) |
c551c21d | 3258 | fprintf_indent (f, indent, "return true;\n"); |
3d2cf79f RB |
3259 | else |
3260 | { | |
c551c21d | 3261 | fprintf_indent (f, indent, "tree res;\n"); |
e0ee10ed RB |
3262 | /* Re-fold the toplevel result. Use non_lvalue to |
3263 | build NON_LVALUE_EXPRs so they get properly | |
3264 | ignored when in GIMPLE form. */ | |
d4b71b95 | 3265 | if (*opr == NON_LVALUE_EXPR) |
c551c21d MM |
3266 | fprintf_indent (f, indent, |
3267 | "res = non_lvalue_loc (loc, res_op0);\n"); | |
3d2cf79f | 3268 | else |
e0ee10ed | 3269 | { |
d4b71b95 | 3270 | if (is_a <operator_id *> (opr)) |
c551c21d MM |
3271 | fprintf_indent (f, indent, |
3272 | "res = fold_build%d_loc (loc, %s, type", | |
3273 | e->ops.length (), | |
3274 | *e->operation == CONVERT_EXPR | |
3275 | ? "NOP_EXPR" : e->operation->id); | |
e0ee10ed | 3276 | else |
c9e926ce RS |
3277 | fprintf_indent (f, indent, |
3278 | "res = maybe_build_call_expr_loc (loc, " | |
3279 | "%s, type, %d", e->operation->id, | |
3280 | e->ops.length()); | |
e0ee10ed RB |
3281 | for (unsigned j = 0; j < e->ops.length (); ++j) |
3282 | fprintf (f, ", res_op%d", j); | |
3283 | fprintf (f, ");\n"); | |
e4cdf6a8 | 3284 | if (!is_a <operator_id *> (opr)) |
c9e926ce RS |
3285 | { |
3286 | fprintf_indent (f, indent, "if (!res)\n"); | |
3287 | fprintf_indent (f, indent, " return NULL_TREE;\n"); | |
3288 | } | |
e0ee10ed | 3289 | } |
3d2cf79f RB |
3290 | } |
3291 | } | |
e0ee10ed RB |
3292 | else if (result->type == operand::OP_CAPTURE |
3293 | || result->type == operand::OP_C_EXPR) | |
3294 | ||
3d2cf79f | 3295 | { |
c551c21d | 3296 | fprintf_indent (f, indent, "tree res;\n"); |
8fdc6c67 | 3297 | result->gen_transform (f, indent, "res", false, 1, "type", |
47b25362 | 3298 | &cinfo, indexes); |
3d2cf79f RB |
3299 | } |
3300 | else | |
3301 | gcc_unreachable (); | |
e0ee10ed RB |
3302 | if (!is_predicate) |
3303 | { | |
3304 | /* Search for captures not used in the result expression and dependent | |
3305 | on TREE_SIDE_EFFECTS emit omit_one_operand. */ | |
3306 | for (int i = 0; i < s->capture_max + 1; ++i) | |
3307 | { | |
fa138f6e RB |
3308 | if (cinfo.info[i].same_as != (unsigned)i) |
3309 | continue; | |
e0ee10ed RB |
3310 | if (!cinfo.info[i].force_no_side_effects_p |
3311 | && !cinfo.info[i].expr_p | |
3312 | && cinfo.info[i].result_use_count == 0) | |
c551c21d MM |
3313 | { |
3314 | fprintf_indent (f, indent, | |
3315 | "if (TREE_SIDE_EFFECTS (captures[%d]))\n", | |
3316 | i); | |
3317 | fprintf_indent (f, indent + 2, | |
3318 | "res = build2_loc (loc, COMPOUND_EXPR, type, " | |
3319 | "fold_ignored_result (captures[%d]), res);\n", | |
3320 | i); | |
3321 | } | |
e0ee10ed | 3322 | } |
c551c21d | 3323 | fprintf_indent (f, indent, "return res;\n"); |
e0ee10ed | 3324 | } |
3d2cf79f | 3325 | } |
8fdc6c67 | 3326 | } |
3d2cf79f | 3327 | |
8fdc6c67 RB |
3328 | /* Generate code for the '(if ...)', '(with ..)' and actual transform |
3329 | step of a '(simplify ...)' or '(match ...)'. This handles everything | |
3330 | that is not part of the decision tree (simplify->match). */ | |
3331 | ||
3332 | void | |
3333 | dt_simplify::gen (FILE *f, int indent, bool gimple) | |
3334 | { | |
3335 | fprintf_indent (f, indent, "{\n"); | |
3336 | indent += 2; | |
0889c52f RB |
3337 | output_line_directive (f, |
3338 | s->result ? s->result->location : s->match->location); | |
8fdc6c67 | 3339 | if (s->capture_max >= 0) |
1d2fdec6 RB |
3340 | { |
3341 | char opname[20]; | |
3342 | fprintf_indent (f, indent, "tree captures[%u] ATTRIBUTE_UNUSED = { %s", | |
3343 | s->capture_max + 1, indexes[0]->get_name (opname)); | |
8fdc6c67 | 3344 | |
1d2fdec6 | 3345 | for (int i = 1; i <= s->capture_max; ++i) |
96285749 RS |
3346 | { |
3347 | if (!indexes[i]) | |
3348 | break; | |
3349 | fprintf (f, ", %s", indexes[i]->get_name (opname)); | |
3350 | } | |
1d2fdec6 RB |
3351 | fprintf (f, " };\n"); |
3352 | } | |
8fdc6c67 | 3353 | |
03038b8b RB |
3354 | /* If we have a split-out function for the actual transform, call it. */ |
3355 | if (info && info->fname) | |
3356 | { | |
3357 | if (gimple) | |
3358 | { | |
3359 | fprintf_indent (f, indent, "if (%s (res_code, res_ops, seq, " | |
d4b71b95 RB |
3360 | "valueize, type, captures", info->fname); |
3361 | for (unsigned i = 0; i < s->for_subst_vec.length (); ++i) | |
e04d2a35 RB |
3362 | if (s->for_subst_vec[i].first->used) |
3363 | fprintf (f, ", %s", s->for_subst_vec[i].second->id); | |
d4b71b95 | 3364 | fprintf (f, "))\n"); |
03038b8b RB |
3365 | fprintf_indent (f, indent, " return true;\n"); |
3366 | } | |
3367 | else | |
3368 | { | |
3369 | fprintf_indent (f, indent, "tree res = %s (loc, type", | |
3370 | info->fname); | |
3371 | for (unsigned i = 0; i < as_a <expr *> (s->match)->ops.length (); ++i) | |
3372 | fprintf (f, ", op%d", i); | |
d4b71b95 RB |
3373 | fprintf (f, ", captures"); |
3374 | for (unsigned i = 0; i < s->for_subst_vec.length (); ++i) | |
e04d2a35 RB |
3375 | { |
3376 | if (s->for_subst_vec[i].first->used) | |
3377 | fprintf (f, ", %s", s->for_subst_vec[i].second->id); | |
3378 | } | |
d4b71b95 | 3379 | fprintf (f, ");\n"); |
03038b8b RB |
3380 | fprintf_indent (f, indent, "if (res) return res;\n"); |
3381 | } | |
3382 | } | |
3383 | else | |
d4b71b95 RB |
3384 | { |
3385 | for (unsigned i = 0; i < s->for_subst_vec.length (); ++i) | |
3386 | { | |
e04d2a35 RB |
3387 | if (! s->for_subst_vec[i].first->used) |
3388 | continue; | |
d4b71b95 RB |
3389 | if (is_a <operator_id *> (s->for_subst_vec[i].second)) |
3390 | fprintf_indent (f, indent, "enum tree_code %s = %s;\n", | |
3391 | s->for_subst_vec[i].first->id, | |
3392 | s->for_subst_vec[i].second->id); | |
3393 | else if (is_a <fn_id *> (s->for_subst_vec[i].second)) | |
c9e926ce | 3394 | fprintf_indent (f, indent, "combined_fn %s = %s;\n", |
d4b71b95 RB |
3395 | s->for_subst_vec[i].first->id, |
3396 | s->for_subst_vec[i].second->id); | |
3397 | else | |
3398 | gcc_unreachable (); | |
3399 | } | |
3400 | gen_1 (f, indent, gimple, s->result); | |
3401 | } | |
3d2cf79f | 3402 | |
c551c21d MM |
3403 | indent -= 2; |
3404 | fprintf_indent (f, indent, "}\n"); | |
3d2cf79f RB |
3405 | } |
3406 | ||
03038b8b RB |
3407 | |
3408 | /* Hash function for finding equivalent transforms. */ | |
3409 | ||
3410 | hashval_t | |
3411 | sinfo_hashmap_traits::hash (const key_type &v) | |
3412 | { | |
3413 | /* Only bother to compare those originating from the same source pattern. */ | |
3414 | return v->s->result->location; | |
3415 | } | |
3416 | ||
3417 | /* Compare function for finding equivalent transforms. */ | |
3418 | ||
3419 | static bool | |
3420 | compare_op (operand *o1, simplify *s1, operand *o2, simplify *s2) | |
3421 | { | |
3422 | if (o1->type != o2->type) | |
3423 | return false; | |
3424 | ||
3425 | switch (o1->type) | |
3426 | { | |
3427 | case operand::OP_IF: | |
3428 | { | |
3429 | if_expr *if1 = as_a <if_expr *> (o1); | |
3430 | if_expr *if2 = as_a <if_expr *> (o2); | |
3431 | /* ??? Properly compare c-exprs. */ | |
3432 | if (if1->cond != if2->cond) | |
3433 | return false; | |
3434 | if (!compare_op (if1->trueexpr, s1, if2->trueexpr, s2)) | |
3435 | return false; | |
3436 | if (if1->falseexpr != if2->falseexpr | |
3437 | || (if1->falseexpr | |
3438 | && !compare_op (if1->falseexpr, s1, if2->falseexpr, s2))) | |
3439 | return false; | |
3440 | return true; | |
3441 | } | |
3442 | case operand::OP_WITH: | |
3443 | { | |
3444 | with_expr *with1 = as_a <with_expr *> (o1); | |
3445 | with_expr *with2 = as_a <with_expr *> (o2); | |
3446 | if (with1->with != with2->with) | |
3447 | return false; | |
3448 | return compare_op (with1->subexpr, s1, with2->subexpr, s2); | |
3449 | } | |
3450 | default:; | |
3451 | } | |
3452 | ||
3453 | /* We've hit a result. Time to compare capture-infos - this is required | |
3454 | in addition to the conservative pointer-equivalency of the result IL. */ | |
3455 | capture_info cinfo1 (s1, o1, true); | |
3456 | capture_info cinfo2 (s2, o2, true); | |
3457 | ||
3458 | if (cinfo1.force_no_side_effects != cinfo2.force_no_side_effects | |
3459 | || cinfo1.info.length () != cinfo2.info.length ()) | |
3460 | return false; | |
3461 | ||
3462 | for (unsigned i = 0; i < cinfo1.info.length (); ++i) | |
3463 | { | |
3464 | if (cinfo1.info[i].expr_p != cinfo2.info[i].expr_p | |
3465 | || cinfo1.info[i].cse_p != cinfo2.info[i].cse_p | |
3466 | || (cinfo1.info[i].force_no_side_effects_p | |
3467 | != cinfo2.info[i].force_no_side_effects_p) | |
3468 | || cinfo1.info[i].force_single_use != cinfo2.info[i].force_single_use | |
3469 | || cinfo1.info[i].cond_expr_cond_p != cinfo2.info[i].cond_expr_cond_p | |
3470 | /* toplevel_msk is an optimization */ | |
3471 | || cinfo1.info[i].result_use_count != cinfo2.info[i].result_use_count | |
3472 | || cinfo1.info[i].same_as != cinfo2.info[i].same_as | |
3473 | /* the pointer back to the capture is for diagnostics only */) | |
3474 | return false; | |
3475 | } | |
3476 | ||
3477 | /* ??? Deep-compare the actual result. */ | |
3478 | return o1 == o2; | |
3479 | } | |
3480 | ||
3481 | bool | |
3482 | sinfo_hashmap_traits::equal_keys (const key_type &v, | |
3483 | const key_type &candidate) | |
3484 | { | |
3485 | return compare_op (v->s->result, v->s, candidate->s->result, candidate->s); | |
3486 | } | |
3487 | ||
3488 | ||
3d2cf79f RB |
3489 | /* Main entry to generate code for matching GIMPLE IL off the decision |
3490 | tree. */ | |
3491 | ||
3492 | void | |
b21ce9ce | 3493 | decision_tree::gen (FILE *f, bool gimple) |
3d2cf79f | 3494 | { |
03038b8b RB |
3495 | sinfo_map_t si; |
3496 | ||
3497 | root->analyze (si); | |
803835de | 3498 | |
b21ce9ce RB |
3499 | fprintf (stderr, "%s decision tree has %u leafs, maximum depth %u and " |
3500 | "a total number of %u nodes\n", | |
3501 | gimple ? "GIMPLE" : "GENERIC", | |
3502 | root->num_leafs, root->max_level, root->total_size); | |
803835de | 3503 | |
03038b8b RB |
3504 | /* First split out the transform part of equal leafs. */ |
3505 | unsigned rcnt = 0; | |
3506 | unsigned fcnt = 1; | |
3507 | for (sinfo_map_t::iterator iter = si.begin (); | |
3508 | iter != si.end (); ++iter) | |
3509 | { | |
3510 | sinfo *s = (*iter).second; | |
3511 | /* Do not split out single uses. */ | |
3512 | if (s->cnt <= 1) | |
3513 | continue; | |
3514 | ||
3515 | rcnt += s->cnt - 1; | |
3516 | if (verbose >= 1) | |
3517 | { | |
3518 | fprintf (stderr, "found %u uses of", s->cnt); | |
3519 | output_line_directive (stderr, s->s->s->result->location); | |
3520 | } | |
3521 | ||
3522 | /* Generate a split out function with the leaf transform code. */ | |
3523 | s->fname = xasprintf ("%s_simplify_%u", gimple ? "gimple" : "generic", | |
3524 | fcnt++); | |
3525 | if (gimple) | |
3526 | fprintf (f, "\nstatic bool\n" | |
3527 | "%s (code_helper *res_code, tree *res_ops,\n" | |
3528 | " gimple_seq *seq, tree (*valueize)(tree) " | |
3529 | "ATTRIBUTE_UNUSED,\n" | |
3530 | " tree ARG_UNUSED (type), tree *ARG_UNUSED " | |
d4b71b95 | 3531 | "(captures)\n", |
03038b8b RB |
3532 | s->fname); |
3533 | else | |
3534 | { | |
3535 | fprintf (f, "\nstatic tree\n" | |
3536 | "%s (location_t ARG_UNUSED (loc), tree ARG_UNUSED (type),\n", | |
3537 | (*iter).second->fname); | |
3538 | for (unsigned i = 0; | |
3539 | i < as_a <expr *>(s->s->s->match)->ops.length (); ++i) | |
3540 | fprintf (f, " tree ARG_UNUSED (op%d),", i); | |
d4b71b95 RB |
3541 | fprintf (f, " tree *captures\n"); |
3542 | } | |
3543 | for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i) | |
3544 | { | |
e04d2a35 RB |
3545 | if (! s->s->s->for_subst_vec[i].first->used) |
3546 | continue; | |
d4b71b95 RB |
3547 | if (is_a <operator_id *> (s->s->s->for_subst_vec[i].second)) |
3548 | fprintf (f, ", enum tree_code ARG_UNUSED (%s)", | |
3549 | s->s->s->for_subst_vec[i].first->id); | |
3550 | else if (is_a <fn_id *> (s->s->s->for_subst_vec[i].second)) | |
c9e926ce | 3551 | fprintf (f, ", combined_fn ARG_UNUSED (%s)", |
d4b71b95 | 3552 | s->s->s->for_subst_vec[i].first->id); |
03038b8b RB |
3553 | } |
3554 | ||
d4b71b95 | 3555 | fprintf (f, ")\n{\n"); |
03038b8b RB |
3556 | s->s->gen_1 (f, 2, gimple, s->s->s->result); |
3557 | if (gimple) | |
3558 | fprintf (f, " return false;\n"); | |
3559 | else | |
3560 | fprintf (f, " return NULL_TREE;\n"); | |
3561 | fprintf (f, "}\n"); | |
3562 | } | |
3563 | fprintf (stderr, "removed %u duplicate tails\n", rcnt); | |
3564 | ||
3d2cf79f RB |
3565 | for (unsigned n = 1; n <= 3; ++n) |
3566 | { | |
0fd357f2 RB |
3567 | /* First generate split-out functions. */ |
3568 | for (unsigned i = 0; i < root->kids.length (); i++) | |
3569 | { | |
3570 | dt_operand *dop = static_cast<dt_operand *>(root->kids[i]); | |
3571 | expr *e = static_cast<expr *>(dop->op); | |
3572 | if (e->ops.length () != n | |
3573 | /* Builtin simplifications are somewhat premature on | |
b21ce9ce | 3574 | GENERIC. The following drops patterns with outermost |
0fd357f2 RB |
3575 | calls. It's easy to emit overloads for function code |
3576 | though if necessary. */ | |
b21ce9ce RB |
3577 | || (!gimple |
3578 | && e->operation->kind != id_base::CODE)) | |
0fd357f2 RB |
3579 | continue; |
3580 | ||
b21ce9ce RB |
3581 | if (gimple) |
3582 | fprintf (f, "\nstatic bool\n" | |
3583 | "gimple_simplify_%s (code_helper *res_code, tree *res_ops,\n" | |
3584 | " gimple_seq *seq, tree (*valueize)(tree) " | |
3585 | "ATTRIBUTE_UNUSED,\n" | |
3586 | " code_helper ARG_UNUSED (code), tree " | |
3587 | "ARG_UNUSED (type)\n", | |
3588 | e->operation->id); | |
3589 | else | |
3590 | fprintf (f, "\nstatic tree\n" | |
3591 | "generic_simplify_%s (location_t ARG_UNUSED (loc), enum " | |
3592 | "tree_code ARG_UNUSED (code), tree ARG_UNUSED (type)", | |
3593 | e->operation->id); | |
0fd357f2 RB |
3594 | for (unsigned i = 0; i < n; ++i) |
3595 | fprintf (f, ", tree op%d", i); | |
3596 | fprintf (f, ")\n"); | |
3597 | fprintf (f, "{\n"); | |
b21ce9ce RB |
3598 | dop->gen_kids (f, 2, gimple); |
3599 | if (gimple) | |
3600 | fprintf (f, " return false;\n"); | |
3601 | else | |
3602 | fprintf (f, " return NULL_TREE;\n"); | |
0fd357f2 RB |
3603 | fprintf (f, "}\n"); |
3604 | } | |
3605 | ||
3606 | /* Then generate the main entry with the outermost switch and | |
3607 | tail-calls to the split-out functions. */ | |
b21ce9ce RB |
3608 | if (gimple) |
3609 | fprintf (f, "\nstatic bool\n" | |
3610 | "gimple_simplify (code_helper *res_code, tree *res_ops,\n" | |
3611 | " gimple_seq *seq, tree (*valueize)(tree),\n" | |
3612 | " code_helper code, tree type"); | |
3613 | else | |
3614 | fprintf (f, "\ntree\n" | |
3615 | "generic_simplify (location_t loc, enum tree_code code, " | |
3616 | "tree type ATTRIBUTE_UNUSED"); | |
3d2cf79f RB |
3617 | for (unsigned i = 0; i < n; ++i) |
3618 | fprintf (f, ", tree op%d", i); | |
3619 | fprintf (f, ")\n"); | |
3620 | fprintf (f, "{\n"); | |
3621 | ||
b21ce9ce RB |
3622 | if (gimple) |
3623 | fprintf (f, " switch (code.get_rep())\n" | |
3624 | " {\n"); | |
3625 | else | |
3626 | fprintf (f, " switch (code)\n" | |
3627 | " {\n"); | |
3d2cf79f RB |
3628 | for (unsigned i = 0; i < root->kids.length (); i++) |
3629 | { | |
3630 | dt_operand *dop = static_cast<dt_operand *>(root->kids[i]); | |
3631 | expr *e = static_cast<expr *>(dop->op); | |
3632 | if (e->ops.length () != n | |
3633 | /* Builtin simplifications are somewhat premature on | |
b21ce9ce | 3634 | GENERIC. The following drops patterns with outermost |
3d2cf79f RB |
3635 | calls. It's easy to emit overloads for function code |
3636 | though if necessary. */ | |
b21ce9ce RB |
3637 | || (!gimple |
3638 | && e->operation->kind != id_base::CODE)) | |
3d2cf79f RB |
3639 | continue; |
3640 | ||
b21ce9ce RB |
3641 | if (*e->operation == CONVERT_EXPR |
3642 | || *e->operation == NOP_EXPR) | |
1651e9b1 | 3643 | fprintf (f, " CASE_CONVERT:\n"); |
3d2cf79f | 3644 | else |
b21ce9ce RB |
3645 | fprintf (f, " case %s%s:\n", |
3646 | is_a <fn_id *> (e->operation) ? "-" : "", | |
3647 | e->operation->id); | |
3648 | if (gimple) | |
3649 | fprintf (f, " return gimple_simplify_%s (res_code, res_ops, " | |
3650 | "seq, valueize, code, type", e->operation->id); | |
3651 | else | |
3652 | fprintf (f, " return generic_simplify_%s (loc, code, type", | |
3653 | e->operation->id); | |
0fd357f2 RB |
3654 | for (unsigned i = 0; i < n; ++i) |
3655 | fprintf (f, ", op%d", i); | |
3656 | fprintf (f, ");\n"); | |
3d2cf79f | 3657 | } |
b21ce9ce RB |
3658 | fprintf (f, " default:;\n" |
3659 | " }\n"); | |
3d2cf79f | 3660 | |
b21ce9ce RB |
3661 | if (gimple) |
3662 | fprintf (f, " return false;\n"); | |
3663 | else | |
3664 | fprintf (f, " return NULL_TREE;\n"); | |
3d2cf79f RB |
3665 | fprintf (f, "}\n"); |
3666 | } | |
3667 | } | |
3668 | ||
3669 | /* Output code to implement the predicate P from the decision tree DT. */ | |
3670 | ||
3671 | void | |
3672 | write_predicate (FILE *f, predicate_id *p, decision_tree &dt, bool gimple) | |
3673 | { | |
3674 | fprintf (f, "\nbool\n" | |
3675 | "%s%s (tree t%s%s)\n" | |
3676 | "{\n", gimple ? "gimple_" : "tree_", p->id, | |
3677 | p->nargs > 0 ? ", tree *res_ops" : "", | |
6b6aa8d3 | 3678 | gimple ? ", tree (*valueize)(tree) ATTRIBUTE_UNUSED" : ""); |
3d2cf79f | 3679 | /* Conveniently make 'type' available. */ |
c551c21d | 3680 | fprintf_indent (f, 2, "tree type = TREE_TYPE (t);\n"); |
3d2cf79f RB |
3681 | |
3682 | if (!gimple) | |
c551c21d MM |
3683 | fprintf_indent (f, 2, "if (TREE_SIDE_EFFECTS (t)) return false;\n"); |
3684 | dt.root->gen_kids (f, 2, gimple); | |
3d2cf79f | 3685 | |
c551c21d | 3686 | fprintf_indent (f, 2, "return false;\n" |
3d2cf79f RB |
3687 | "}\n"); |
3688 | } | |
3689 | ||
3690 | /* Write the common header for the GIMPLE/GENERIC IL matching routines. */ | |
3691 | ||
3692 | static void | |
3693 | write_header (FILE *f, const char *head) | |
3694 | { | |
3695 | fprintf (f, "/* Generated automatically by the program `genmatch' from\n"); | |
3696 | fprintf (f, " a IL pattern matching and simplification description. */\n"); | |
3697 | ||
3698 | /* Include the header instead of writing it awkwardly quoted here. */ | |
3699 | fprintf (f, "\n#include \"%s\"\n", head); | |
3700 | } | |
3701 | ||
3702 | ||
3703 | ||
3704 | /* AST parsing. */ | |
3705 | ||
3706 | class parser | |
3707 | { | |
3708 | public: | |
3709 | parser (cpp_reader *); | |
3710 | ||
3711 | private: | |
3712 | const cpp_token *next (); | |
64d3a1f0 RB |
3713 | const cpp_token *peek (unsigned = 1); |
3714 | const cpp_token *peek_ident (const char * = NULL, unsigned = 1); | |
3d2cf79f | 3715 | const cpp_token *expect (enum cpp_ttype); |
64d3a1f0 | 3716 | const cpp_token *eat_token (enum cpp_ttype); |
3d2cf79f RB |
3717 | const char *get_string (); |
3718 | const char *get_ident (); | |
64d3a1f0 | 3719 | const cpp_token *eat_ident (const char *); |
3d2cf79f RB |
3720 | const char *get_number (); |
3721 | ||
3722 | id_base *parse_operation (); | |
c56f494f | 3723 | operand *parse_capture (operand *, bool); |
3d2cf79f RB |
3724 | operand *parse_expr (); |
3725 | c_expr *parse_c_expr (cpp_ttype); | |
3726 | operand *parse_op (); | |
3727 | ||
1c9b0448 RB |
3728 | void record_operlist (source_location, user_id *); |
3729 | ||
3d2cf79f | 3730 | void parse_pattern (); |
8fdc6c67 RB |
3731 | operand *parse_result (operand *, predicate_id *); |
3732 | void push_simplify (simplify::simplify_kind, | |
0889c52f | 3733 | vec<simplify *>&, operand *, operand *); |
8fdc6c67 | 3734 | void parse_simplify (simplify::simplify_kind, |
0889c52f | 3735 | vec<simplify *>&, predicate_id *, operand *); |
3d2cf79f RB |
3736 | void parse_for (source_location); |
3737 | void parse_if (source_location); | |
3738 | void parse_predicates (source_location); | |
72eb311d | 3739 | void parse_operator_list (source_location); |
3d2cf79f RB |
3740 | |
3741 | cpp_reader *r; | |
8fdc6c67 | 3742 | vec<c_expr *> active_ifs; |
3d2cf79f | 3743 | vec<vec<user_id *> > active_fors; |
1c9b0448 RB |
3744 | hash_set<user_id *> *oper_lists_set; |
3745 | vec<user_id *> oper_lists; | |
3d2cf79f | 3746 | |
f1308e4b | 3747 | cid_map_t *capture_ids; |
3d2cf79f RB |
3748 | |
3749 | public: | |
3750 | vec<simplify *> simplifiers; | |
3751 | vec<predicate_id *> user_predicates; | |
72eb311d | 3752 | bool parsing_match_operand; |
3d2cf79f RB |
3753 | }; |
3754 | ||
3755 | /* Lexing helpers. */ | |
3756 | ||
3757 | /* Read the next non-whitespace token from R. */ | |
3758 | ||
3759 | const cpp_token * | |
3760 | parser::next () | |
3761 | { | |
3762 | const cpp_token *token; | |
3763 | do | |
3764 | { | |
3765 | token = cpp_get_token (r); | |
3766 | } | |
3767 | while (token->type == CPP_PADDING | |
3768 | && token->type != CPP_EOF); | |
3769 | return token; | |
3770 | } | |
3771 | ||
3772 | /* Peek at the next non-whitespace token from R. */ | |
3773 | ||
3774 | const cpp_token * | |
64d3a1f0 | 3775 | parser::peek (unsigned num) |
3d2cf79f RB |
3776 | { |
3777 | const cpp_token *token; | |
3778 | unsigned i = 0; | |
3779 | do | |
3780 | { | |
3781 | token = cpp_peek_token (r, i++); | |
3782 | } | |
64d3a1f0 RB |
3783 | while ((token->type == CPP_PADDING |
3784 | && token->type != CPP_EOF) | |
3785 | || (--num > 0)); | |
3d2cf79f RB |
3786 | /* If we peek at EOF this is a fatal error as it leaves the |
3787 | cpp_reader in unusable state. Assume we really wanted a | |
3788 | token and thus this EOF is unexpected. */ | |
3789 | if (token->type == CPP_EOF) | |
3790 | fatal_at (token, "unexpected end of file"); | |
3791 | return token; | |
3792 | } | |
3793 | ||
3794 | /* Peek at the next identifier token (or return NULL if the next | |
3795 | token is not an identifier or equal to ID if supplied). */ | |
3796 | ||
3797 | const cpp_token * | |
64d3a1f0 | 3798 | parser::peek_ident (const char *id, unsigned num) |
3d2cf79f | 3799 | { |
64d3a1f0 | 3800 | const cpp_token *token = peek (num); |
3d2cf79f RB |
3801 | if (token->type != CPP_NAME) |
3802 | return 0; | |
3803 | ||
3804 | if (id == 0) | |
3805 | return token; | |
3806 | ||
3807 | const char *t = (const char *) CPP_HASHNODE (token->val.node.node)->ident.str; | |
3808 | if (strcmp (id, t) == 0) | |
3809 | return token; | |
3810 | ||
3811 | return 0; | |
3812 | } | |
3813 | ||
3814 | /* Read the next token from R and assert it is of type TK. */ | |
3815 | ||
3816 | const cpp_token * | |
3817 | parser::expect (enum cpp_ttype tk) | |
3818 | { | |
3819 | const cpp_token *token = next (); | |
3820 | if (token->type != tk) | |
3821 | fatal_at (token, "expected %s, got %s", | |
3822 | cpp_type2name (tk, 0), cpp_type2name (token->type, 0)); | |
3823 | ||
3824 | return token; | |
3825 | } | |
3826 | ||
3827 | /* Consume the next token from R and assert it is of type TK. */ | |
3828 | ||
64d3a1f0 | 3829 | const cpp_token * |
3d2cf79f RB |
3830 | parser::eat_token (enum cpp_ttype tk) |
3831 | { | |
64d3a1f0 | 3832 | return expect (tk); |
3d2cf79f RB |
3833 | } |
3834 | ||
3835 | /* Read the next token from R and assert it is of type CPP_STRING and | |
3836 | return its value. */ | |
3837 | ||
3838 | const char * | |
3839 | parser::get_string () | |
3840 | { | |
3841 | const cpp_token *token = expect (CPP_STRING); | |
3842 | return (const char *)token->val.str.text; | |
3843 | } | |
3844 | ||
3845 | /* Read the next token from R and assert it is of type CPP_NAME and | |
3846 | return its value. */ | |
3847 | ||
3848 | const char * | |
3849 | parser::get_ident () | |
3850 | { | |
3851 | const cpp_token *token = expect (CPP_NAME); | |
3852 | return (const char *)CPP_HASHNODE (token->val.node.node)->ident.str; | |
3853 | } | |
3854 | ||
3855 | /* Eat an identifier token with value S from R. */ | |
3856 | ||
64d3a1f0 | 3857 | const cpp_token * |
3d2cf79f RB |
3858 | parser::eat_ident (const char *s) |
3859 | { | |
3860 | const cpp_token *token = peek (); | |
3861 | const char *t = get_ident (); | |
3862 | if (strcmp (s, t) != 0) | |
3863 | fatal_at (token, "expected '%s' got '%s'\n", s, t); | |
64d3a1f0 | 3864 | return token; |
3d2cf79f RB |
3865 | } |
3866 | ||
3867 | /* Read the next token from R and assert it is of type CPP_NUMBER and | |
3868 | return its value. */ | |
3869 | ||
3870 | const char * | |
3871 | parser::get_number () | |
3872 | { | |
3873 | const cpp_token *token = expect (CPP_NUMBER); | |
3874 | return (const char *)token->val.str.text; | |
3875 | } | |
3876 | ||
3877 | ||
1c9b0448 RB |
3878 | /* Record an operator-list use for transparent for handling. */ |
3879 | ||
3880 | void | |
3881 | parser::record_operlist (source_location loc, user_id *p) | |
3882 | { | |
3883 | if (!oper_lists_set->add (p)) | |
3884 | { | |
3885 | if (!oper_lists.is_empty () | |
3886 | && oper_lists[0]->substitutes.length () != p->substitutes.length ()) | |
3887 | fatal_at (loc, "User-defined operator list does not have the " | |
3888 | "same number of entries as others used in the pattern"); | |
3889 | oper_lists.safe_push (p); | |
3890 | } | |
3891 | } | |
3892 | ||
3d2cf79f RB |
3893 | /* Parse the operator ID, special-casing convert?, convert1? and |
3894 | convert2? */ | |
3895 | ||
3896 | id_base * | |
3897 | parser::parse_operation () | |
3898 | { | |
3899 | const cpp_token *id_tok = peek (); | |
3900 | const char *id = get_ident (); | |
3901 | const cpp_token *token = peek (); | |
3902 | if (strcmp (id, "convert0") == 0) | |
3903 | fatal_at (id_tok, "use 'convert?' here"); | |
39791822 RB |
3904 | else if (strcmp (id, "view_convert0") == 0) |
3905 | fatal_at (id_tok, "use 'view_convert?' here"); | |
3d2cf79f RB |
3906 | if (token->type == CPP_QUERY |
3907 | && !(token->flags & PREV_WHITE)) | |
3908 | { | |
3909 | if (strcmp (id, "convert") == 0) | |
3910 | id = "convert0"; | |
39791822 | 3911 | else if (strcmp (id, "convert1") == 0) |
3d2cf79f | 3912 | ; |
39791822 RB |
3913 | else if (strcmp (id, "convert2") == 0) |
3914 | ; | |
3915 | else if (strcmp (id, "view_convert") == 0) | |
3916 | id = "view_convert0"; | |
3917 | else if (strcmp (id, "view_convert1") == 0) | |
3918 | ; | |
3919 | else if (strcmp (id, "view_convert2") == 0) | |
3d2cf79f RB |
3920 | ; |
3921 | else | |
3922 | fatal_at (id_tok, "non-convert operator conditionalized"); | |
72eb311d RB |
3923 | |
3924 | if (!parsing_match_operand) | |
3925 | fatal_at (id_tok, "conditional convert can only be used in " | |
3926 | "match expression"); | |
3d2cf79f RB |
3927 | eat_token (CPP_QUERY); |
3928 | } | |
39791822 RB |
3929 | else if (strcmp (id, "convert1") == 0 |
3930 | || strcmp (id, "convert2") == 0 | |
3931 | || strcmp (id, "view_convert1") == 0 | |
3932 | || strcmp (id, "view_convert2") == 0) | |
3d2cf79f RB |
3933 | fatal_at (id_tok, "expected '?' after conditional operator"); |
3934 | id_base *op = get_operator (id); | |
3935 | if (!op) | |
3936 | fatal_at (id_tok, "unknown operator %s", id); | |
72eb311d RB |
3937 | |
3938 | user_id *p = dyn_cast<user_id *> (op); | |
3939 | if (p && p->is_oper_list) | |
94cbafd1 PK |
3940 | { |
3941 | if (active_fors.length() == 0) | |
3942 | record_operlist (id_tok->src_loc, p); | |
3943 | else | |
3944 | fatal_at (id_tok, "operator-list %s cannot be exapnded inside 'for'", id); | |
3945 | } | |
3d2cf79f RB |
3946 | return op; |
3947 | } | |
3948 | ||
3949 | /* Parse a capture. | |
3950 | capture = '@'<number> */ | |
3951 | ||
3952 | struct operand * | |
c56f494f | 3953 | parser::parse_capture (operand *op, bool require_existing) |
3d2cf79f | 3954 | { |
0889c52f | 3955 | source_location src_loc = eat_token (CPP_ATSIGN)->src_loc; |
3d2cf79f | 3956 | const cpp_token *token = peek (); |
72eb311d | 3957 | const char *id = NULL; |
3d2cf79f RB |
3958 | if (token->type == CPP_NUMBER) |
3959 | id = get_number (); | |
3960 | else if (token->type == CPP_NAME) | |
3961 | id = get_ident (); | |
3962 | else | |
3963 | fatal_at (token, "expected number or identifier"); | |
f1308e4b RB |
3964 | unsigned next_id = capture_ids->elements (); |
3965 | bool existed; | |
3966 | unsigned &num = capture_ids->get_or_insert (id, &existed); | |
3967 | if (!existed) | |
c56f494f RB |
3968 | { |
3969 | if (require_existing) | |
3970 | fatal_at (src_loc, "unknown capture id"); | |
3971 | num = next_id; | |
3972 | } | |
0889c52f | 3973 | return new capture (src_loc, num, op); |
3d2cf79f RB |
3974 | } |
3975 | ||
3976 | /* Parse an expression | |
3977 | expr = '(' <operation>[capture][flag][type] <operand>... ')' */ | |
3978 | ||
3979 | struct operand * | |
3980 | parser::parse_expr () | |
3981 | { | |
3d2cf79f | 3982 | const cpp_token *token = peek (); |
0889c52f RB |
3983 | expr *e = new expr (parse_operation (), token->src_loc); |
3984 | token = peek (); | |
3d2cf79f RB |
3985 | operand *op; |
3986 | bool is_commutative = false; | |
44fc0a51 | 3987 | bool force_capture = false; |
3d2cf79f RB |
3988 | const char *expr_type = NULL; |
3989 | ||
3990 | if (token->type == CPP_COLON | |
3991 | && !(token->flags & PREV_WHITE)) | |
3992 | { | |
3993 | eat_token (CPP_COLON); | |
3994 | token = peek (); | |
3995 | if (token->type == CPP_NAME | |
3996 | && !(token->flags & PREV_WHITE)) | |
3997 | { | |
3998 | const char *s = get_ident (); | |
44fc0a51 RB |
3999 | if (!parsing_match_operand) |
4000 | expr_type = s; | |
4001 | else | |
72eb311d | 4002 | { |
44fc0a51 RB |
4003 | const char *sp = s; |
4004 | while (*sp) | |
4005 | { | |
4006 | if (*sp == 'c') | |
e04d2a35 RB |
4007 | { |
4008 | if (operator_id *p | |
4009 | = dyn_cast<operator_id *> (e->operation)) | |
4010 | { | |
4011 | if (!commutative_tree_code (p->code) | |
4012 | && !comparison_code_p (p->code)) | |
4013 | fatal_at (token, "operation is not commutative"); | |
4014 | } | |
4015 | else if (user_id *p = dyn_cast<user_id *> (e->operation)) | |
4016 | for (unsigned i = 0; | |
4017 | i < p->substitutes.length (); ++i) | |
4018 | { | |
4019 | if (operator_id *q | |
4020 | = dyn_cast<operator_id *> (p->substitutes[i])) | |
4021 | { | |
4022 | if (!commutative_tree_code (q->code) | |
4023 | && !comparison_code_p (q->code)) | |
4024 | fatal_at (token, "operation %s is not " | |
4025 | "commutative", q->id); | |
4026 | } | |
4027 | } | |
4028 | is_commutative = true; | |
4029 | } | |
4030 | else if (*sp == 'C') | |
44fc0a51 RB |
4031 | is_commutative = true; |
4032 | else if (*sp == 's') | |
4033 | { | |
4034 | e->force_single_use = true; | |
4035 | force_capture = true; | |
4036 | } | |
4037 | else | |
4038 | fatal_at (token, "flag %c not recognized", *sp); | |
4039 | sp++; | |
4040 | } | |
72eb311d | 4041 | } |
3d2cf79f RB |
4042 | token = peek (); |
4043 | } | |
4044 | else | |
4045 | fatal_at (token, "expected flag or type specifying identifier"); | |
4046 | } | |
4047 | ||
4048 | if (token->type == CPP_ATSIGN | |
4049 | && !(token->flags & PREV_WHITE)) | |
96285749 | 4050 | op = parse_capture (e, false); |
44fc0a51 RB |
4051 | else if (force_capture) |
4052 | { | |
4053 | unsigned num = capture_ids->elements (); | |
3d7b83b6 MS |
4054 | /* Big enough for a 32-bit UINT_MAX plus prefix. */ |
4055 | char id[13]; | |
44fc0a51 RB |
4056 | bool existed; |
4057 | sprintf (id, "__%u", num); | |
4058 | capture_ids->get_or_insert (xstrdup (id), &existed); | |
4059 | if (existed) | |
4060 | fatal_at (token, "reserved capture id '%s' already used", id); | |
0889c52f | 4061 | op = new capture (token->src_loc, num, e); |
44fc0a51 | 4062 | } |
3d2cf79f RB |
4063 | else |
4064 | op = e; | |
4065 | do | |
4066 | { | |
4067 | const cpp_token *token = peek (); | |
4068 | if (token->type == CPP_CLOSE_PAREN) | |
4069 | { | |
4070 | if (e->operation->nargs != -1 | |
4071 | && e->operation->nargs != (int) e->ops.length ()) | |
4072 | fatal_at (token, "'%s' expects %u operands, not %u", | |
4073 | e->operation->id, e->operation->nargs, e->ops.length ()); | |
4074 | if (is_commutative) | |
4075 | { | |
4076 | if (e->ops.length () == 2) | |
4077 | e->is_commutative = true; | |
4078 | else | |
4079 | fatal_at (token, "only binary operators or function with " | |
4080 | "two arguments can be marked commutative"); | |
4081 | } | |
4082 | e->expr_type = expr_type; | |
4083 | return op; | |
4084 | } | |
2e96ac06 RB |
4085 | else if (!(token->flags & PREV_WHITE)) |
4086 | fatal_at (token, "expected expression operand"); | |
4087 | ||
3d2cf79f RB |
4088 | e->append_op (parse_op ()); |
4089 | } | |
4090 | while (1); | |
4091 | } | |
4092 | ||
4093 | /* Lex native C code delimited by START recording the preprocessing tokens | |
4094 | for later processing. | |
4095 | c_expr = ('{'|'(') <pp token>... ('}'|')') */ | |
4096 | ||
4097 | c_expr * | |
4098 | parser::parse_c_expr (cpp_ttype start) | |
4099 | { | |
4100 | const cpp_token *token; | |
4101 | cpp_ttype end; | |
4102 | unsigned opencnt; | |
4103 | vec<cpp_token> code = vNULL; | |
4104 | unsigned nr_stmts = 0; | |
0889c52f | 4105 | source_location loc = eat_token (start)->src_loc; |
3d2cf79f RB |
4106 | if (start == CPP_OPEN_PAREN) |
4107 | end = CPP_CLOSE_PAREN; | |
4108 | else if (start == CPP_OPEN_BRACE) | |
4109 | end = CPP_CLOSE_BRACE; | |
4110 | else | |
4111 | gcc_unreachable (); | |
4112 | opencnt = 1; | |
4113 | do | |
4114 | { | |
4115 | token = next (); | |
4116 | ||
4117 | /* Count brace pairs to find the end of the expr to match. */ | |
4118 | if (token->type == start) | |
4119 | opencnt++; | |
4120 | else if (token->type == end | |
4121 | && --opencnt == 0) | |
4122 | break; | |
4123 | ||
4124 | /* This is a lame way of counting the number of statements. */ | |
4125 | if (token->type == CPP_SEMICOLON) | |
4126 | nr_stmts++; | |
4127 | ||
72eb311d RB |
4128 | /* If this is possibly a user-defined identifier mark it used. */ |
4129 | if (token->type == CPP_NAME) | |
1c9b0448 RB |
4130 | { |
4131 | id_base *idb = get_operator ((const char *)CPP_HASHNODE | |
4132 | (token->val.node.node)->ident.str); | |
4133 | user_id *p; | |
4134 | if (idb && (p = dyn_cast<user_id *> (idb)) && p->is_oper_list) | |
4135 | record_operlist (token->src_loc, p); | |
4136 | } | |
72eb311d | 4137 | |
3d2cf79f RB |
4138 | /* Record the token. */ |
4139 | code.safe_push (*token); | |
4140 | } | |
4141 | while (1); | |
0889c52f | 4142 | return new c_expr (r, loc, code, nr_stmts, vNULL, capture_ids); |
3d2cf79f RB |
4143 | } |
4144 | ||
4145 | /* Parse an operand which is either an expression, a predicate or | |
4146 | a standalone capture. | |
4147 | op = predicate | expr | c_expr | capture */ | |
4148 | ||
4149 | struct operand * | |
4150 | parser::parse_op () | |
4151 | { | |
4152 | const cpp_token *token = peek (); | |
4153 | struct operand *op = NULL; | |
4154 | if (token->type == CPP_OPEN_PAREN) | |
4155 | { | |
4156 | eat_token (CPP_OPEN_PAREN); | |
4157 | op = parse_expr (); | |
4158 | eat_token (CPP_CLOSE_PAREN); | |
4159 | } | |
4160 | else if (token->type == CPP_OPEN_BRACE) | |
4161 | { | |
4162 | op = parse_c_expr (CPP_OPEN_BRACE); | |
4163 | } | |
4164 | else | |
4165 | { | |
4166 | /* Remaining ops are either empty or predicates */ | |
4167 | if (token->type == CPP_NAME) | |
4168 | { | |
4169 | const char *id = get_ident (); | |
4170 | id_base *opr = get_operator (id); | |
4171 | if (!opr) | |
4172 | fatal_at (token, "expected predicate name"); | |
4173 | if (operator_id *code = dyn_cast <operator_id *> (opr)) | |
4174 | { | |
4175 | if (code->nargs != 0) | |
4176 | fatal_at (token, "using an operator with operands as predicate"); | |
4177 | /* Parse the zero-operand operator "predicates" as | |
4178 | expression. */ | |
0889c52f | 4179 | op = new expr (opr, token->src_loc); |
3d2cf79f | 4180 | } |
10230017 RB |
4181 | else if (user_id *code = dyn_cast <user_id *> (opr)) |
4182 | { | |
4183 | if (code->nargs != 0) | |
4184 | fatal_at (token, "using an operator with operands as predicate"); | |
4185 | /* Parse the zero-operand operator "predicates" as | |
4186 | expression. */ | |
0889c52f | 4187 | op = new expr (opr, token->src_loc); |
10230017 | 4188 | } |
3d2cf79f | 4189 | else if (predicate_id *p = dyn_cast <predicate_id *> (opr)) |
0889c52f | 4190 | op = new predicate (p, token->src_loc); |
3d2cf79f RB |
4191 | else |
4192 | fatal_at (token, "using an unsupported operator as predicate"); | |
72eb311d RB |
4193 | if (!parsing_match_operand) |
4194 | fatal_at (token, "predicates are only allowed in match expression"); | |
3d2cf79f RB |
4195 | token = peek (); |
4196 | if (token->flags & PREV_WHITE) | |
4197 | return op; | |
4198 | } | |
4199 | else if (token->type != CPP_COLON | |
4200 | && token->type != CPP_ATSIGN) | |
4201 | fatal_at (token, "expected expression or predicate"); | |
4202 | /* optionally followed by a capture and a predicate. */ | |
4203 | if (token->type == CPP_COLON) | |
4204 | fatal_at (token, "not implemented: predicate on leaf operand"); | |
4205 | if (token->type == CPP_ATSIGN) | |
c56f494f | 4206 | op = parse_capture (op, !parsing_match_operand); |
3d2cf79f RB |
4207 | } |
4208 | ||
4209 | return op; | |
4210 | } | |
4211 | ||
1c9b0448 RB |
4212 | /* Create a new simplify from the current parsing state and MATCH, |
4213 | MATCH_LOC, RESULT and RESULT_LOC and push it to SIMPLIFIERS. */ | |
4214 | ||
4215 | void | |
8fdc6c67 RB |
4216 | parser::push_simplify (simplify::simplify_kind kind, |
4217 | vec<simplify *>& simplifiers, | |
0889c52f | 4218 | operand *match, operand *result) |
1c9b0448 | 4219 | { |
026c3cfd | 4220 | /* Build and push a temporary for operator list uses in expressions. */ |
1c9b0448 RB |
4221 | if (!oper_lists.is_empty ()) |
4222 | active_fors.safe_push (oper_lists); | |
4223 | ||
4224 | simplifiers.safe_push | |
0889c52f | 4225 | (new simplify (kind, match, result, |
8fdc6c67 | 4226 | active_fors.copy (), capture_ids)); |
1c9b0448 RB |
4227 | |
4228 | if (!oper_lists.is_empty ()) | |
4229 | active_fors.pop (); | |
4230 | } | |
4231 | ||
3d2cf79f | 4232 | /* Parse |
3d2cf79f RB |
4233 | <result-op> = <op> | <if> | <with> |
4234 | <if> = '(' 'if' '(' <c-expr> ')' <result-op> ')' | |
4235 | <with> = '(' 'with' '{' <c-expr> '}' <result-op> ')' | |
8fdc6c67 RB |
4236 | and return it. */ |
4237 | ||
4238 | operand * | |
4239 | parser::parse_result (operand *result, predicate_id *matcher) | |
4240 | { | |
4241 | const cpp_token *token = peek (); | |
4242 | if (token->type != CPP_OPEN_PAREN) | |
4243 | return parse_op (); | |
4244 | ||
4245 | eat_token (CPP_OPEN_PAREN); | |
4246 | if (peek_ident ("if")) | |
4247 | { | |
4248 | eat_ident ("if"); | |
0889c52f | 4249 | if_expr *ife = new if_expr (token->src_loc); |
8fdc6c67 RB |
4250 | ife->cond = parse_c_expr (CPP_OPEN_PAREN); |
4251 | if (peek ()->type == CPP_OPEN_PAREN) | |
4252 | { | |
4253 | ife->trueexpr = parse_result (result, matcher); | |
4254 | if (peek ()->type == CPP_OPEN_PAREN) | |
4255 | ife->falseexpr = parse_result (result, matcher); | |
4256 | else if (peek ()->type != CPP_CLOSE_PAREN) | |
4257 | ife->falseexpr = parse_op (); | |
4258 | } | |
4259 | else if (peek ()->type != CPP_CLOSE_PAREN) | |
4260 | { | |
4261 | ife->trueexpr = parse_op (); | |
4262 | if (peek ()->type == CPP_OPEN_PAREN) | |
4263 | ife->falseexpr = parse_result (result, matcher); | |
4264 | else if (peek ()->type != CPP_CLOSE_PAREN) | |
4265 | ife->falseexpr = parse_op (); | |
4266 | } | |
4267 | /* If this if is immediately closed then it contains a | |
4268 | manual matcher or is part of a predicate definition. */ | |
4269 | else /* if (peek ()->type == CPP_CLOSE_PAREN) */ | |
4270 | { | |
4271 | if (!matcher) | |
4272 | fatal_at (peek (), "manual transform not implemented"); | |
d3b0b692 | 4273 | ife->trueexpr = result; |
8fdc6c67 RB |
4274 | } |
4275 | eat_token (CPP_CLOSE_PAREN); | |
4276 | return ife; | |
4277 | } | |
4278 | else if (peek_ident ("with")) | |
4279 | { | |
4280 | eat_ident ("with"); | |
0889c52f | 4281 | with_expr *withe = new with_expr (token->src_loc); |
8fdc6c67 RB |
4282 | /* Parse (with c-expr expr) as (if-with (true) expr). */ |
4283 | withe->with = parse_c_expr (CPP_OPEN_BRACE); | |
4284 | withe->with->nr_stmts = 0; | |
4285 | withe->subexpr = parse_result (result, matcher); | |
4286 | eat_token (CPP_CLOSE_PAREN); | |
4287 | return withe; | |
4288 | } | |
64d3a1f0 RB |
4289 | else if (peek_ident ("switch")) |
4290 | { | |
4291 | token = eat_ident ("switch"); | |
0889c52f | 4292 | source_location ifloc = eat_token (CPP_OPEN_PAREN)->src_loc; |
64d3a1f0 | 4293 | eat_ident ("if"); |
0889c52f | 4294 | if_expr *ife = new if_expr (ifloc); |
64d3a1f0 RB |
4295 | operand *res = ife; |
4296 | ife->cond = parse_c_expr (CPP_OPEN_PAREN); | |
4297 | if (peek ()->type == CPP_OPEN_PAREN) | |
4298 | ife->trueexpr = parse_result (result, matcher); | |
4299 | else | |
4300 | ife->trueexpr = parse_op (); | |
4301 | eat_token (CPP_CLOSE_PAREN); | |
4302 | if (peek ()->type != CPP_OPEN_PAREN | |
4303 | || !peek_ident ("if", 2)) | |
4304 | fatal_at (token, "switch can be implemented with a single if"); | |
4305 | while (peek ()->type != CPP_CLOSE_PAREN) | |
4306 | { | |
4307 | if (peek ()->type == CPP_OPEN_PAREN) | |
4308 | { | |
4309 | if (peek_ident ("if", 2)) | |
4310 | { | |
0889c52f | 4311 | ifloc = eat_token (CPP_OPEN_PAREN)->src_loc; |
64d3a1f0 | 4312 | eat_ident ("if"); |
0889c52f | 4313 | ife->falseexpr = new if_expr (ifloc); |
64d3a1f0 RB |
4314 | ife = as_a <if_expr *> (ife->falseexpr); |
4315 | ife->cond = parse_c_expr (CPP_OPEN_PAREN); | |
4316 | if (peek ()->type == CPP_OPEN_PAREN) | |
4317 | ife->trueexpr = parse_result (result, matcher); | |
4318 | else | |
4319 | ife->trueexpr = parse_op (); | |
4320 | eat_token (CPP_CLOSE_PAREN); | |
4321 | } | |
4322 | else | |
4323 | { | |
4324 | /* switch default clause */ | |
4325 | ife->falseexpr = parse_result (result, matcher); | |
4326 | eat_token (CPP_CLOSE_PAREN); | |
4327 | return res; | |
4328 | } | |
4329 | } | |
4330 | else | |
4331 | { | |
4332 | /* switch default clause */ | |
4333 | ife->falseexpr = parse_op (); | |
4334 | eat_token (CPP_CLOSE_PAREN); | |
4335 | return res; | |
4336 | } | |
4337 | } | |
4338 | eat_token (CPP_CLOSE_PAREN); | |
4339 | return res; | |
4340 | } | |
8fdc6c67 RB |
4341 | else |
4342 | { | |
4343 | operand *op = result; | |
4344 | if (!matcher) | |
4345 | op = parse_expr (); | |
4346 | eat_token (CPP_CLOSE_PAREN); | |
4347 | return op; | |
4348 | } | |
4349 | } | |
4350 | ||
4351 | /* Parse | |
4352 | simplify = 'simplify' <expr> <result-op> | |
4353 | or | |
4354 | match = 'match' <ident> <expr> [<result-op>] | |
3d2cf79f RB |
4355 | and fill SIMPLIFIERS with the results. */ |
4356 | ||
4357 | void | |
8fdc6c67 | 4358 | parser::parse_simplify (simplify::simplify_kind kind, |
3d2cf79f | 4359 | vec<simplify *>& simplifiers, predicate_id *matcher, |
8fdc6c67 | 4360 | operand *result) |
3d2cf79f RB |
4361 | { |
4362 | /* Reset the capture map. */ | |
d0af2c65 RB |
4363 | if (!capture_ids) |
4364 | capture_ids = new cid_map_t; | |
1c9b0448 RB |
4365 | /* Reset oper_lists and set. */ |
4366 | hash_set <user_id *> olist; | |
4367 | oper_lists_set = &olist; | |
4368 | oper_lists = vNULL; | |
3d2cf79f RB |
4369 | |
4370 | const cpp_token *loc = peek (); | |
72eb311d | 4371 | parsing_match_operand = true; |
3d2cf79f | 4372 | struct operand *match = parse_op (); |
72eb311d | 4373 | parsing_match_operand = false; |
3d2cf79f RB |
4374 | if (match->type == operand::OP_CAPTURE && !matcher) |
4375 | fatal_at (loc, "outermost expression cannot be captured"); | |
4376 | if (match->type == operand::OP_EXPR | |
4377 | && is_a <predicate_id *> (as_a <expr *> (match)->operation)) | |
4378 | fatal_at (loc, "outermost expression cannot be a predicate"); | |
4379 | ||
8fdc6c67 RB |
4380 | /* Splice active_ifs onto result and continue parsing the |
4381 | "then" expr. */ | |
4382 | if_expr *active_if = NULL; | |
4383 | for (int i = active_ifs.length (); i > 0; --i) | |
4384 | { | |
0889c52f | 4385 | if_expr *ifc = new if_expr (active_ifs[i-1]->location); |
8fdc6c67 RB |
4386 | ifc->cond = active_ifs[i-1]; |
4387 | ifc->trueexpr = active_if; | |
4388 | active_if = ifc; | |
4389 | } | |
4390 | if_expr *outermost_if = active_if; | |
4391 | while (active_if && active_if->trueexpr) | |
4392 | active_if = as_a <if_expr *> (active_if->trueexpr); | |
4393 | ||
3d2cf79f RB |
4394 | const cpp_token *token = peek (); |
4395 | ||
4396 | /* If this if is immediately closed then it is part of a predicate | |
4397 | definition. Push it. */ | |
4398 | if (token->type == CPP_CLOSE_PAREN) | |
4399 | { | |
4400 | if (!matcher) | |
4401 | fatal_at (token, "expected transform expression"); | |
8fdc6c67 RB |
4402 | if (active_if) |
4403 | { | |
4404 | active_if->trueexpr = result; | |
4405 | result = outermost_if; | |
4406 | } | |
0889c52f | 4407 | push_simplify (kind, simplifiers, match, result); |
3d2cf79f RB |
4408 | return; |
4409 | } | |
4410 | ||
8fdc6c67 RB |
4411 | operand *tem = parse_result (result, matcher); |
4412 | if (active_if) | |
3d2cf79f | 4413 | { |
8fdc6c67 RB |
4414 | active_if->trueexpr = tem; |
4415 | result = outermost_if; | |
3d2cf79f | 4416 | } |
8fdc6c67 RB |
4417 | else |
4418 | result = tem; | |
4419 | ||
0889c52f | 4420 | push_simplify (kind, simplifiers, match, result); |
3d2cf79f RB |
4421 | } |
4422 | ||
4423 | /* Parsing of the outer control structures. */ | |
4424 | ||
4425 | /* Parse a for expression | |
4426 | for = '(' 'for' <subst>... <pattern> ')' | |
4427 | subst = <ident> '(' <ident>... ')' */ | |
4428 | ||
4429 | void | |
4430 | parser::parse_for (source_location) | |
4431 | { | |
72eb311d | 4432 | auto_vec<const cpp_token *> user_id_tokens; |
3d2cf79f RB |
4433 | vec<user_id *> user_ids = vNULL; |
4434 | const cpp_token *token; | |
4435 | unsigned min_n_opers = 0, max_n_opers = 0; | |
4436 | ||
4437 | while (1) | |
4438 | { | |
72eb311d RB |
4439 | token = peek (); |
4440 | if (token->type != CPP_NAME) | |
3d2cf79f RB |
4441 | break; |
4442 | ||
4443 | /* Insert the user defined operators into the operator hash. */ | |
4444 | const char *id = get_ident (); | |
fa74b47a | 4445 | if (get_operator (id, true) != NULL) |
72eb311d | 4446 | fatal_at (token, "operator already defined"); |
3d2cf79f RB |
4447 | user_id *op = new user_id (id); |
4448 | id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT); | |
3d2cf79f RB |
4449 | *slot = op; |
4450 | user_ids.safe_push (op); | |
72eb311d | 4451 | user_id_tokens.safe_push (token); |
3d2cf79f RB |
4452 | |
4453 | eat_token (CPP_OPEN_PAREN); | |
4454 | ||
4455 | int arity = -1; | |
4456 | while ((token = peek_ident ()) != 0) | |
4457 | { | |
4458 | const char *oper = get_ident (); | |
fa74b47a | 4459 | id_base *idb = get_operator (oper, true); |
3d2cf79f RB |
4460 | if (idb == NULL) |
4461 | fatal_at (token, "no such operator '%s'", oper); | |
39791822 RB |
4462 | if (*idb == CONVERT0 || *idb == CONVERT1 || *idb == CONVERT2 |
4463 | || *idb == VIEW_CONVERT0 || *idb == VIEW_CONVERT1 | |
4464 | || *idb == VIEW_CONVERT2) | |
3d2cf79f RB |
4465 | fatal_at (token, "conditional operators cannot be used inside for"); |
4466 | ||
4467 | if (arity == -1) | |
4468 | arity = idb->nargs; | |
4469 | else if (idb->nargs == -1) | |
4470 | ; | |
4471 | else if (idb->nargs != arity) | |
4472 | fatal_at (token, "operator '%s' with arity %d does not match " | |
4473 | "others with arity %d", oper, idb->nargs, arity); | |
4474 | ||
72eb311d | 4475 | user_id *p = dyn_cast<user_id *> (idb); |
7523ca9b PK |
4476 | if (p) |
4477 | { | |
4478 | if (p->is_oper_list) | |
4479 | op->substitutes.safe_splice (p->substitutes); | |
4480 | else | |
4481 | fatal_at (token, "iterator cannot be used as operator-list"); | |
4482 | } | |
72eb311d RB |
4483 | else |
4484 | op->substitutes.safe_push (idb); | |
3d2cf79f RB |
4485 | } |
4486 | op->nargs = arity; | |
4487 | token = expect (CPP_CLOSE_PAREN); | |
4488 | ||
4489 | unsigned nsubstitutes = op->substitutes.length (); | |
4490 | if (nsubstitutes == 0) | |
4491 | fatal_at (token, "A user-defined operator must have at least " | |
4492 | "one substitution"); | |
4493 | if (max_n_opers == 0) | |
4494 | { | |
4495 | min_n_opers = nsubstitutes; | |
4496 | max_n_opers = nsubstitutes; | |
4497 | } | |
4498 | else | |
4499 | { | |
4500 | if (nsubstitutes % min_n_opers != 0 | |
4501 | && min_n_opers % nsubstitutes != 0) | |
4502 | fatal_at (token, "All user-defined identifiers must have a " | |
4503 | "multiple number of operator substitutions of the " | |
4504 | "smallest number of substitutions"); | |
4505 | if (nsubstitutes < min_n_opers) | |
4506 | min_n_opers = nsubstitutes; | |
4507 | else if (nsubstitutes > max_n_opers) | |
4508 | max_n_opers = nsubstitutes; | |
4509 | } | |
4510 | } | |
4511 | ||
4512 | unsigned n_ids = user_ids.length (); | |
4513 | if (n_ids == 0) | |
4514 | fatal_at (token, "for requires at least one user-defined identifier"); | |
4515 | ||
4516 | token = peek (); | |
4517 | if (token->type == CPP_CLOSE_PAREN) | |
4518 | fatal_at (token, "no pattern defined in for"); | |
4519 | ||
4520 | active_fors.safe_push (user_ids); | |
4521 | while (1) | |
4522 | { | |
4523 | token = peek (); | |
4524 | if (token->type == CPP_CLOSE_PAREN) | |
4525 | break; | |
4526 | parse_pattern (); | |
4527 | } | |
4528 | active_fors.pop (); | |
4529 | ||
4530 | /* Remove user-defined operators from the hash again. */ | |
4531 | for (unsigned i = 0; i < user_ids.length (); ++i) | |
72eb311d RB |
4532 | { |
4533 | if (!user_ids[i]->used) | |
4534 | warning_at (user_id_tokens[i], | |
4535 | "operator %s defined but not used", user_ids[i]->id); | |
4536 | operators->remove_elt (user_ids[i]); | |
4537 | } | |
4538 | } | |
4539 | ||
4540 | /* Parse an identifier associated with a list of operators. | |
4541 | oprs = '(' 'define_operator_list' <ident> <ident>... ')' */ | |
4542 | ||
4543 | void | |
4544 | parser::parse_operator_list (source_location) | |
4545 | { | |
4546 | const cpp_token *token = peek (); | |
4547 | const char *id = get_ident (); | |
4548 | ||
fa74b47a | 4549 | if (get_operator (id, true) != 0) |
72eb311d RB |
4550 | fatal_at (token, "operator %s already defined", id); |
4551 | ||
4552 | user_id *op = new user_id (id, true); | |
4553 | int arity = -1; | |
4554 | ||
4555 | while ((token = peek_ident ()) != 0) | |
4556 | { | |
4557 | token = peek (); | |
4558 | const char *oper = get_ident (); | |
fa74b47a | 4559 | id_base *idb = get_operator (oper, true); |
72eb311d RB |
4560 | |
4561 | if (idb == 0) | |
4562 | fatal_at (token, "no such operator '%s'", oper); | |
4563 | ||
4564 | if (arity == -1) | |
4565 | arity = idb->nargs; | |
4566 | else if (idb->nargs == -1) | |
4567 | ; | |
4568 | else if (arity != idb->nargs) | |
4569 | fatal_at (token, "operator '%s' with arity %d does not match " | |
4570 | "others with arity %d", oper, idb->nargs, arity); | |
4571 | ||
4572 | /* We allow composition of multiple operator lists. */ | |
4573 | if (user_id *p = dyn_cast<user_id *> (idb)) | |
4574 | op->substitutes.safe_splice (p->substitutes); | |
4575 | else | |
4576 | op->substitutes.safe_push (idb); | |
4577 | } | |
4578 | ||
b78be014 PK |
4579 | // Check that there is no junk after id-list |
4580 | token = peek(); | |
4581 | if (token->type != CPP_CLOSE_PAREN) | |
4582 | fatal_at (token, "expected identifier got %s", cpp_type2name (token->type, 0)); | |
4583 | ||
72eb311d RB |
4584 | if (op->substitutes.length () == 0) |
4585 | fatal_at (token, "operator-list cannot be empty"); | |
4586 | ||
4587 | op->nargs = arity; | |
4588 | id_base **slot = operators->find_slot_with_hash (op, op->hashval, INSERT); | |
4589 | *slot = op; | |
3d2cf79f RB |
4590 | } |
4591 | ||
4592 | /* Parse an outer if expression. | |
4593 | if = '(' 'if' '(' <c-expr> ')' <pattern> ')' */ | |
4594 | ||
4595 | void | |
8fdc6c67 | 4596 | parser::parse_if (source_location) |
3d2cf79f | 4597 | { |
8fdc6c67 | 4598 | c_expr *ifexpr = parse_c_expr (CPP_OPEN_PAREN); |
3d2cf79f RB |
4599 | |
4600 | const cpp_token *token = peek (); | |
4601 | if (token->type == CPP_CLOSE_PAREN) | |
4602 | fatal_at (token, "no pattern defined in if"); | |
4603 | ||
8fdc6c67 | 4604 | active_ifs.safe_push (ifexpr); |
3d2cf79f RB |
4605 | while (1) |
4606 | { | |
4607 | const cpp_token *token = peek (); | |
4608 | if (token->type == CPP_CLOSE_PAREN) | |
4609 | break; | |
4610 | ||
4611 | parse_pattern (); | |
4612 | } | |
4613 | active_ifs.pop (); | |
4614 | } | |
4615 | ||
4616 | /* Parse a list of predefined predicate identifiers. | |
4617 | preds = '(' 'define_predicates' <ident>... ')' */ | |
4618 | ||
4619 | void | |
4620 | parser::parse_predicates (source_location) | |
4621 | { | |
4622 | do | |
4623 | { | |
4624 | const cpp_token *token = peek (); | |
4625 | if (token->type != CPP_NAME) | |
4626 | break; | |
4627 | ||
4628 | add_predicate (get_ident ()); | |
4629 | } | |
4630 | while (1); | |
4631 | } | |
4632 | ||
4633 | /* Parse outer control structures. | |
4634 | pattern = <preds>|<for>|<if>|<simplify>|<match> */ | |
4635 | ||
4636 | void | |
4637 | parser::parse_pattern () | |
4638 | { | |
4639 | /* All clauses start with '('. */ | |
4640 | eat_token (CPP_OPEN_PAREN); | |
4641 | const cpp_token *token = peek (); | |
4642 | const char *id = get_ident (); | |
4643 | if (strcmp (id, "simplify") == 0) | |
d0af2c65 | 4644 | { |
0889c52f | 4645 | parse_simplify (simplify::SIMPLIFY, simplifiers, NULL, NULL); |
d0af2c65 RB |
4646 | capture_ids = NULL; |
4647 | } | |
3d2cf79f RB |
4648 | else if (strcmp (id, "match") == 0) |
4649 | { | |
4650 | bool with_args = false; | |
0889c52f | 4651 | source_location e_loc = peek ()->src_loc; |
3d2cf79f RB |
4652 | if (peek ()->type == CPP_OPEN_PAREN) |
4653 | { | |
4654 | eat_token (CPP_OPEN_PAREN); | |
4655 | with_args = true; | |
4656 | } | |
4657 | const char *name = get_ident (); | |
4658 | id_base *id = get_operator (name); | |
4659 | predicate_id *p; | |
4660 | if (!id) | |
4661 | { | |
4662 | p = add_predicate (name); | |
4663 | user_predicates.safe_push (p); | |
4664 | } | |
4665 | else if ((p = dyn_cast <predicate_id *> (id))) | |
4666 | ; | |
4667 | else | |
4668 | fatal_at (token, "cannot add a match to a non-predicate ID"); | |
4669 | /* Parse (match <id> <arg>... (match-expr)) here. */ | |
4670 | expr *e = NULL; | |
4671 | if (with_args) | |
4672 | { | |
d0af2c65 | 4673 | capture_ids = new cid_map_t; |
0889c52f | 4674 | e = new expr (p, e_loc); |
3d2cf79f | 4675 | while (peek ()->type == CPP_ATSIGN) |
c56f494f | 4676 | e->append_op (parse_capture (NULL, false)); |
3d2cf79f RB |
4677 | eat_token (CPP_CLOSE_PAREN); |
4678 | } | |
4679 | if (p->nargs != -1 | |
4680 | && ((e && e->ops.length () != (unsigned)p->nargs) | |
4681 | || (!e && p->nargs != 0))) | |
4682 | fatal_at (token, "non-matching number of match operands"); | |
4683 | p->nargs = e ? e->ops.length () : 0; | |
0889c52f | 4684 | parse_simplify (simplify::MATCH, p->matchers, p, e); |
d0af2c65 | 4685 | capture_ids = NULL; |
3d2cf79f RB |
4686 | } |
4687 | else if (strcmp (id, "for") == 0) | |
4688 | parse_for (token->src_loc); | |
4689 | else if (strcmp (id, "if") == 0) | |
4690 | parse_if (token->src_loc); | |
4691 | else if (strcmp (id, "define_predicates") == 0) | |
4692 | { | |
4693 | if (active_ifs.length () > 0 | |
4694 | || active_fors.length () > 0) | |
4695 | fatal_at (token, "define_predicates inside if or for is not supported"); | |
4696 | parse_predicates (token->src_loc); | |
4697 | } | |
72eb311d RB |
4698 | else if (strcmp (id, "define_operator_list") == 0) |
4699 | { | |
4700 | if (active_ifs.length () > 0 | |
4701 | || active_fors.length () > 0) | |
4702 | fatal_at (token, "operator-list inside if or for is not supported"); | |
4703 | parse_operator_list (token->src_loc); | |
4704 | } | |
3d2cf79f RB |
4705 | else |
4706 | fatal_at (token, "expected %s'simplify', 'match', 'for' or 'if'", | |
4707 | active_ifs.length () == 0 && active_fors.length () == 0 | |
4708 | ? "'define_predicates', " : ""); | |
4709 | ||
4710 | eat_token (CPP_CLOSE_PAREN); | |
4711 | } | |
4712 | ||
4713 | /* Main entry of the parser. Repeatedly parse outer control structures. */ | |
4714 | ||
4715 | parser::parser (cpp_reader *r_) | |
4716 | { | |
4717 | r = r_; | |
4718 | active_ifs = vNULL; | |
4719 | active_fors = vNULL; | |
4720 | simplifiers = vNULL; | |
1c9b0448 RB |
4721 | oper_lists_set = NULL; |
4722 | oper_lists = vNULL; | |
d0af2c65 | 4723 | capture_ids = NULL; |
3d2cf79f | 4724 | user_predicates = vNULL; |
72eb311d | 4725 | parsing_match_operand = false; |
3d2cf79f RB |
4726 | |
4727 | const cpp_token *token = next (); | |
4728 | while (token->type != CPP_EOF) | |
4729 | { | |
4730 | _cpp_backup_tokens (r, 1); | |
4731 | parse_pattern (); | |
4732 | token = next (); | |
4733 | } | |
4734 | } | |
4735 | ||
4736 | ||
4737 | /* Helper for the linemap code. */ | |
4738 | ||
4739 | static size_t | |
4740 | round_alloc_size (size_t s) | |
4741 | { | |
4742 | return s; | |
4743 | } | |
4744 | ||
4745 | ||
4746 | /* The genmatch generator progam. It reads from a pattern description | |
4747 | and outputs GIMPLE or GENERIC IL matching and simplification routines. */ | |
4748 | ||
4749 | int | |
4750 | main (int argc, char **argv) | |
4751 | { | |
4752 | cpp_reader *r; | |
4753 | ||
4754 | progname = "genmatch"; | |
4755 | ||
4756 | if (argc < 2) | |
4757 | return 1; | |
4758 | ||
4759 | bool gimple = true; | |
3d2cf79f RB |
4760 | char *input = argv[argc-1]; |
4761 | for (int i = 1; i < argc - 1; ++i) | |
4762 | { | |
4763 | if (strcmp (argv[i], "--gimple") == 0) | |
4764 | gimple = true; | |
4765 | else if (strcmp (argv[i], "--generic") == 0) | |
4766 | gimple = false; | |
4767 | else if (strcmp (argv[i], "-v") == 0) | |
53a19317 RB |
4768 | verbose = 1; |
4769 | else if (strcmp (argv[i], "-vv") == 0) | |
4770 | verbose = 2; | |
3d2cf79f RB |
4771 | else |
4772 | { | |
4773 | fprintf (stderr, "Usage: genmatch " | |
53a19317 | 4774 | "[--gimple] [--generic] [-v[v]] input\n"); |
3d2cf79f RB |
4775 | return 1; |
4776 | } | |
4777 | } | |
4778 | ||
4779 | line_table = XCNEW (struct line_maps); | |
4780 | linemap_init (line_table, 0); | |
4781 | line_table->reallocator = xrealloc; | |
4782 | line_table->round_alloc_size = round_alloc_size; | |
4783 | ||
4784 | r = cpp_create_reader (CLK_GNUC99, NULL, line_table); | |
4785 | cpp_callbacks *cb = cpp_get_callbacks (r); | |
4786 | cb->error = error_cb; | |
4787 | ||
b1dc4a20 RS |
4788 | /* Add the build directory to the #include "" search path. */ |
4789 | cpp_dir *dir = XCNEW (cpp_dir); | |
4790 | dir->name = getpwd (); | |
4791 | if (!dir->name) | |
4792 | dir->name = ASTRDUP ("."); | |
4793 | cpp_set_include_chains (r, dir, NULL, false); | |
4794 | ||
3d2cf79f RB |
4795 | if (!cpp_read_main_file (r, input)) |
4796 | return 1; | |
4797 | cpp_define (r, gimple ? "GIMPLE=1": "GENERIC=1"); | |
4798 | cpp_define (r, gimple ? "GENERIC=0": "GIMPLE=0"); | |
4799 | ||
fa74b47a RS |
4800 | null_id = new id_base (id_base::NULL_ID, "null"); |
4801 | ||
3d2cf79f RB |
4802 | /* Pre-seed operators. */ |
4803 | operators = new hash_table<id_base> (1024); | |
4804 | #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \ | |
4805 | add_operator (SYM, # SYM, # TYPE, NARGS); | |
4806 | #define END_OF_BASE_TREE_CODES | |
4807 | #include "tree.def" | |
c9e926ce RS |
4808 | add_operator (CONVERT0, "convert0", "tcc_unary", 1); |
4809 | add_operator (CONVERT1, "convert1", "tcc_unary", 1); | |
4810 | add_operator (CONVERT2, "convert2", "tcc_unary", 1); | |
4811 | add_operator (VIEW_CONVERT0, "view_convert0", "tcc_unary", 1); | |
4812 | add_operator (VIEW_CONVERT1, "view_convert1", "tcc_unary", 1); | |
4813 | add_operator (VIEW_CONVERT2, "view_convert2", "tcc_unary", 1); | |
3d2cf79f RB |
4814 | #undef END_OF_BASE_TREE_CODES |
4815 | #undef DEFTREECODE | |
4816 | ||
4817 | /* Pre-seed builtin functions. | |
4818 | ??? Cannot use N (name) as that is targetm.emultls.get_address | |
4819 | for BUILT_IN_EMUTLS_GET_ADDRESS ... */ | |
4820 | #define DEF_BUILTIN(ENUM, N, C, T, LT, B, F, NA, AT, IM, COND) \ | |
c9e926ce | 4821 | add_function (ENUM, "CFN_" # ENUM); |
3d2cf79f | 4822 | #include "builtins.def" |
3d2cf79f | 4823 | |
c9e926ce RS |
4824 | #define DEF_INTERNAL_FN(CODE, NAME, FNSPEC) \ |
4825 | add_function (IFN_##CODE, "CFN_" #CODE); | |
4826 | #include "internal-fn.def" | |
4827 | ||
3d2cf79f RB |
4828 | /* Parse ahead! */ |
4829 | parser p (r); | |
4830 | ||
4831 | if (gimple) | |
4832 | write_header (stdout, "gimple-match-head.c"); | |
4833 | else | |
4834 | write_header (stdout, "generic-match-head.c"); | |
4835 | ||
4836 | /* Go over all predicates defined with patterns and perform | |
4837 | lowering and code generation. */ | |
4838 | for (unsigned i = 0; i < p.user_predicates.length (); ++i) | |
4839 | { | |
4840 | predicate_id *pred = p.user_predicates[i]; | |
47b25362 | 4841 | lower (pred->matchers, gimple); |
3d2cf79f | 4842 | |
53a19317 | 4843 | if (verbose == 2) |
3d2cf79f RB |
4844 | for (unsigned i = 0; i < pred->matchers.length (); ++i) |
4845 | print_matches (pred->matchers[i]); | |
4846 | ||
4847 | decision_tree dt; | |
4848 | for (unsigned i = 0; i < pred->matchers.length (); ++i) | |
4849 | dt.insert (pred->matchers[i], i); | |
4850 | ||
53a19317 | 4851 | if (verbose == 2) |
3d2cf79f RB |
4852 | dt.print (stderr); |
4853 | ||
4854 | write_predicate (stdout, pred, dt, gimple); | |
4855 | } | |
4856 | ||
4857 | /* Lower the main simplifiers and generate code for them. */ | |
47b25362 | 4858 | lower (p.simplifiers, gimple); |
3d2cf79f | 4859 | |
53a19317 | 4860 | if (verbose == 2) |
3d2cf79f RB |
4861 | for (unsigned i = 0; i < p.simplifiers.length (); ++i) |
4862 | print_matches (p.simplifiers[i]); | |
4863 | ||
4864 | decision_tree dt; | |
4865 | for (unsigned i = 0; i < p.simplifiers.length (); ++i) | |
4866 | dt.insert (p.simplifiers[i], i); | |
4867 | ||
53a19317 | 4868 | if (verbose == 2) |
3d2cf79f RB |
4869 | dt.print (stderr); |
4870 | ||
b21ce9ce | 4871 | dt.gen (stdout, gimple); |
3d2cf79f RB |
4872 | |
4873 | /* Finalize. */ | |
4874 | cpp_finish (r, NULL); | |
4875 | cpp_destroy (r); | |
4876 | ||
4877 | delete operators; | |
4878 | ||
4879 | return 0; | |
4880 | } |