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