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