1 /* Generate code from machine description to recognize rtl as insns. 2 Copyright (C) 1987-2017 Free Software Foundation, Inc. 3 4 This file is part of GCC. 5 6 GCC is free software; you can redistribute it and/or modify it 7 under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3, or (at your option) 9 any later version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 14 License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GCC; see the file COPYING3. If not see 18 <http://www.gnu.org/licenses/>. */ 19 20 21 /* This program is used to produce insn-recog.c, which contains a 22 function called `recog' plus its subroutines. These functions 23 contain a decision tree that recognizes whether an rtx, the 24 argument given to recog, is a valid instruction. 25 26 recog returns -1 if the rtx is not valid. If the rtx is valid, 27 recog returns a nonnegative number which is the insn code number 28 for the pattern that matched. This is the same as the order in the 29 machine description of the entry that matched. This number can be 30 used as an index into various insn_* tables, such as insn_template, 31 insn_outfun, and insn_n_operands (found in insn-output.c). 32 33 The third argument to recog is an optional pointer to an int. If 34 present, recog will accept a pattern if it matches except for 35 missing CLOBBER expressions at the end. In that case, the value 36 pointed to by the optional pointer will be set to the number of 37 CLOBBERs that need to be added (it should be initialized to zero by 38 the caller). If it is set nonzero, the caller should allocate a 39 PARALLEL of the appropriate size, copy the initial entries, and 40 call add_clobbers (found in insn-emit.c) to fill in the CLOBBERs. 41 42 This program also generates the function `split_insns', which 43 returns 0 if the rtl could not be split, or it returns the split 44 rtl as an INSN list. 45 46 This program also generates the function `peephole2_insns', which 47 returns 0 if the rtl could not be matched. If there was a match, 48 the new rtl is returned in an INSN list, and LAST_INSN will point 49 to the last recognized insn in the old sequence. 50 51 52 At a high level, the algorithm used in this file is as follows: 53 54 1. Build up a decision tree for each routine, using the following 55 approach to matching an rtx: 56 57 - First determine the "shape" of the rtx, based on GET_CODE, 58 XVECLEN and XINT. This phase examines SET_SRCs before SET_DESTs 59 since SET_SRCs tend to be more distinctive. It examines other 60 operands in numerical order, since the canonicalization rules 61 prefer putting complex operands of commutative operators first. 62 63 - Next check modes and predicates. This phase examines all 64 operands in numerical order, even for SETs, since the mode of a 65 SET_DEST is exact while the mode of a SET_SRC can be VOIDmode 66 for constant integers. 67 68 - Next check match_dups. 69 70 - Finally check the C condition and (where appropriate) pnum_clobbers. 71 72 2. Try to optimize the tree by removing redundant tests, CSEing tests, 73 folding tests together, etc. 74 75 3. Look for common subtrees and split them out into "pattern" routines. 76 These common subtrees can be identical or they can differ in mode, 77 code, or integer (usually an UNSPEC or UNSPEC_VOLATILE code). 78 In the latter case the users of the pattern routine pass the 79 appropriate mode, etc., as argument. For example, if two patterns 80 contain: 81 82 (plus:SI (match_operand:SI 1 "register_operand") 83 (match_operand:SI 2 "register_operand")) 84 85 we can split the associated matching code out into a subroutine. 86 If a pattern contains: 87 88 (minus:DI (match_operand:DI 1 "register_operand") 89 (match_operand:DI 2 "register_operand")) 90 91 then we can consider using the same matching routine for both 92 the plus and minus expressions, passing PLUS and SImode in the 93 former case and MINUS and DImode in the latter case. 94 95 The main aim of this phase is to reduce the compile time of the 96 insn-recog.c code and to reduce the amount of object code in 97 insn-recog.o. 98 99 4. Split the matching trees into functions, trying to limit the 100 size of each function to a sensible amount. 101 102 Again, the main aim of this phase is to reduce the compile time 103 of insn-recog.c. (It doesn't help with the size of insn-recog.o.) 104 105 5. Write out C++ code for each function. */ 106 107 #include "bconfig.h" 108 #define INCLUDE_ALGORITHM 109 #include "system.h" 110 #include "coretypes.h" 111 #include "tm.h" 112 #include "rtl.h" 113 #include "errors.h" 114 #include "read-md.h" 115 #include "gensupport.h" 116 117 #undef GENERATOR_FILE 118 enum true_rtx_doe { 119 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) TRUE_##ENUM, 120 #include "rtl.def" 121 #undef DEF_RTL_EXPR 122 FIRST_GENERATOR_RTX_CODE 123 }; 124 #define NUM_TRUE_RTX_CODE ((int) FIRST_GENERATOR_RTX_CODE) 125 #define GENERATOR_FILE 1 126 127 /* Debugging variables to control which optimizations are performed. 128 Note that disabling merge_states_p leads to very large output. */ 129 static const bool merge_states_p = true; 130 static const bool collapse_optional_decisions_p = true; 131 static const bool cse_tests_p = true; 132 static const bool simplify_tests_p = true; 133 static const bool use_operand_variables_p = true; 134 static const bool use_subroutines_p = true; 135 static const bool use_pattern_routines_p = true; 136 137 /* Whether to add comments for optional tests that we decided to keep. 138 Can be useful when debugging the generator itself but is noise when 139 debugging the generated code. */ 140 static const bool mark_optional_transitions_p = false; 141 142 /* Whether pattern routines should calculate positions relative to their 143 rtx parameter rather than use absolute positions. This e.g. allows 144 a pattern routine to be shared between a plain SET and a PARALLEL 145 that includes a SET. 146 147 In principle it sounds like this should be useful, especially for 148 recog_for_combine, where the plain SET form is generated automatically 149 from a PARALLEL of a single SET and some CLOBBERs. In practice it doesn't 150 seem to help much and leads to slightly bigger object files. */ 151 static const bool relative_patterns_p = false; 152 153 /* Whether pattern routines should be allowed to test whether pnum_clobbers 154 is null. This requires passing pnum_clobbers around as a parameter. */ 155 static const bool pattern_have_num_clobbers_p = true; 156 157 /* Whether pattern routines should be allowed to test .md file C conditions. 158 This requires passing insn around as a parameter, in case the C 159 condition refers to it. In practice this tends to lead to bigger 160 object files. */ 161 static const bool pattern_c_test_p = false; 162 163 /* Whether to require each parameter passed to a pattern routine to be 164 unique. Disabling this check for example allows unary operators with 165 matching modes (like NEG) and unary operators with mismatched modes 166 (like ZERO_EXTEND) to be matched by a single pattern. However, we then 167 often have cases where the same value is passed too many times. */ 168 static const bool force_unique_params_p = true; 169 170 /* The maximum (approximate) depth of block nesting that an individual 171 routine or subroutine should have. This limit is about keeping the 172 output readable rather than reducing compile time. */ 173 static const unsigned int MAX_DEPTH = 6; 174 175 /* The minimum number of pseudo-statements that a state must have before 176 we split it out into a subroutine. */ 177 static const unsigned int MIN_NUM_STATEMENTS = 5; 178 179 /* The number of pseudo-statements a state can have before we consider 180 splitting out substates into subroutines. This limit is about avoiding 181 compile-time problems with very big functions (and also about keeping 182 functions within --param optimization limits, etc.). */ 183 static const unsigned int MAX_NUM_STATEMENTS = 200; 184 185 /* The minimum number of pseudo-statements that can be used in a pattern 186 routine. */ 187 static const unsigned int MIN_COMBINE_COST = 4; 188 189 /* The maximum number of arguments that a pattern routine can have. 190 The idea is to prevent one pattern getting a ridiculous number of 191 arguments when it would be more beneficial to have a separate pattern 192 routine instead. */ 193 static const unsigned int MAX_PATTERN_PARAMS = 5; 194 195 /* The maximum operand number plus one. */ 196 int num_operands; 197 198 /* Ways of obtaining an rtx to be tested. */ 199 enum position_type { 200 /* PATTERN (peep2_next_insn (ARG)). */ 201 POS_PEEP2_INSN, 202 203 /* XEXP (BASE, ARG). */ 204 POS_XEXP, 205 206 /* XVECEXP (BASE, 0, ARG). */ 207 POS_XVECEXP0 208 }; 209 210 /* The position of an rtx relative to X0. Each useful position is 211 represented by exactly one instance of this structure. */ 212 struct position 213 { 214 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */ 215 struct position *base; 216 217 /* A position with the same BASE and TYPE, but with the next value 218 of ARG. */ 219 struct position *next; 220 221 /* A list of all POS_XEXP positions that use this one as their base, 222 chained by NEXT fields. The first entry represents XEXP (this, 0), 223 the second represents XEXP (this, 1), and so on. */ 224 struct position *xexps; 225 226 /* A list of POS_XVECEXP0 positions that use this one as their base, 227 chained by NEXT fields. The first entry represents XVECEXP (this, 0, 0), 228 the second represents XVECEXP (this, 0, 1), and so on. */ 229 struct position *xvecexp0s; 230 231 /* The type of position. */ 232 enum position_type type; 233 234 /* The argument to TYPE (shown as ARG in the position_type comments). */ 235 int arg; 236 237 /* The instruction to which the position belongs. */ 238 unsigned int insn_id; 239 240 /* The depth of this position relative to the instruction pattern. 241 E.g. if the instruction pattern is a SET, the SET itself has a 242 depth of 0 while the SET_DEST and SET_SRC have depths of 1. */ 243 unsigned int depth; 244 245 /* A unique identifier for this position. */ 246 unsigned int id; 247 }; 248 249 enum routine_type { 250 SUBPATTERN, RECOG, SPLIT, PEEPHOLE2 251 }; 252 253 /* The root position (x0). */ 254 static struct position root_pos; 255 256 /* The number of positions created. Also one higher than the maximum 257 position id. */ 258 static unsigned int num_positions = 1; 259 260 /* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position, 261 since we are given that instruction's pattern as x0. */ 262 static struct position *peep2_insn_pos_list = &root_pos; 263 264 /* Return a position with the given BASE, TYPE and ARG. NEXT_PTR 265 points to where the unique object that represents the position 266 should be stored. Create the object if it doesn't already exist, 267 otherwise reuse the object that is already there. */ 268 269 static struct position * 270 next_position (struct position **next_ptr, struct position *base, 271 enum position_type type, int arg) 272 { 273 struct position *pos; 274 275 pos = *next_ptr; 276 if (!pos) 277 { 278 pos = XCNEW (struct position); 279 pos->type = type; 280 pos->arg = arg; 281 if (type == POS_PEEP2_INSN) 282 { 283 pos->base = 0; 284 pos->insn_id = arg; 285 pos->depth = base->depth; 286 } 287 else 288 { 289 pos->base = base; 290 pos->insn_id = base->insn_id; 291 pos->depth = base->depth + 1; 292 } 293 pos->id = num_positions++; 294 *next_ptr = pos; 295 } 296 return pos; 297 } 298 299 /* Compare positions POS1 and POS2 lexicographically. */ 300 301 static int 302 compare_positions (struct position *pos1, struct position *pos2) 303 { 304 int diff; 305 306 diff = pos1->depth - pos2->depth; 307 if (diff < 0) 308 do 309 pos2 = pos2->base; 310 while (pos1->depth != pos2->depth); 311 else if (diff > 0) 312 do 313 pos1 = pos1->base; 314 while (pos1->depth != pos2->depth); 315 while (pos1 != pos2) 316 { 317 diff = (int) pos1->type - (int) pos2->type; 318 if (diff == 0) 319 diff = pos1->arg - pos2->arg; 320 pos1 = pos1->base; 321 pos2 = pos2->base; 322 } 323 return diff; 324 } 325 326 /* Return the most deeply-nested position that is common to both 327 POS1 and POS2. If the positions are from different instructions, 328 return the one with the lowest insn_id. */ 329 330 static struct position * 331 common_position (struct position *pos1, struct position *pos2) 332 { 333 if (pos1->insn_id != pos2->insn_id) 334 return pos1->insn_id < pos2->insn_id ? pos1 : pos2; 335 if (pos1->depth > pos2->depth) 336 std::swap (pos1, pos2); 337 while (pos1->depth != pos2->depth) 338 pos2 = pos2->base; 339 while (pos1 != pos2) 340 { 341 pos1 = pos1->base; 342 pos2 = pos2->base; 343 } 344 return pos1; 345 } 346 347 /* Search for and return operand N, stop when reaching node STOP. */ 348 349 static rtx 350 find_operand (rtx pattern, int n, rtx stop) 351 { 352 const char *fmt; 353 RTX_CODE code; 354 int i, j, len; 355 rtx r; 356 357 if (pattern == stop) 358 return stop; 359 360 code = GET_CODE (pattern); 361 if ((code == MATCH_SCRATCH 362 || code == MATCH_OPERAND 363 || code == MATCH_OPERATOR 364 || code == MATCH_PARALLEL) 365 && XINT (pattern, 0) == n) 366 return pattern; 367 368 fmt = GET_RTX_FORMAT (code); 369 len = GET_RTX_LENGTH (code); 370 for (i = 0; i < len; i++) 371 { 372 switch (fmt[i]) 373 { 374 case 'e': case 'u': 375 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX) 376 return r; 377 break; 378 379 case 'V': 380 if (! XVEC (pattern, i)) 381 break; 382 /* Fall through. */ 383 384 case 'E': 385 for (j = 0; j < XVECLEN (pattern, i); j++) 386 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop)) 387 != NULL_RTX) 388 return r; 389 break; 390 391 case 'i': case 'r': case 'w': case '0': case 's': 392 break; 393 394 default: 395 gcc_unreachable (); 396 } 397 } 398 399 return NULL; 400 } 401 402 /* Search for and return operand M, such that it has a matching 403 constraint for operand N. */ 404 405 static rtx 406 find_matching_operand (rtx pattern, int n) 407 { 408 const char *fmt; 409 RTX_CODE code; 410 int i, j, len; 411 rtx r; 412 413 code = GET_CODE (pattern); 414 if (code == MATCH_OPERAND 415 && (XSTR (pattern, 2)[0] == '0' + n 416 || (XSTR (pattern, 2)[0] == '%' 417 && XSTR (pattern, 2)[1] == '0' + n))) 418 return pattern; 419 420 fmt = GET_RTX_FORMAT (code); 421 len = GET_RTX_LENGTH (code); 422 for (i = 0; i < len; i++) 423 { 424 switch (fmt[i]) 425 { 426 case 'e': case 'u': 427 if ((r = find_matching_operand (XEXP (pattern, i), n))) 428 return r; 429 break; 430 431 case 'V': 432 if (! XVEC (pattern, i)) 433 break; 434 /* Fall through. */ 435 436 case 'E': 437 for (j = 0; j < XVECLEN (pattern, i); j++) 438 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n))) 439 return r; 440 break; 441 442 case 'i': case 'r': case 'w': case '0': case 's': 443 break; 444 445 default: 446 gcc_unreachable (); 447 } 448 } 449 450 return NULL; 451 } 452 453 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we 454 don't use the MATCH_OPERAND constraint, only the predicate. 455 This is confusing to folks doing new ports, so help them 456 not make the mistake. */ 457 458 static bool 459 constraints_supported_in_insn_p (rtx insn) 460 { 461 return !(GET_CODE (insn) == DEFINE_EXPAND 462 || GET_CODE (insn) == DEFINE_SPLIT 463 || GET_CODE (insn) == DEFINE_PEEPHOLE2); 464 } 465 466 /* Return the name of the predicate matched by MATCH_RTX. */ 467 468 static const char * 469 predicate_name (rtx match_rtx) 470 { 471 if (GET_CODE (match_rtx) == MATCH_SCRATCH) 472 return "scratch_operand"; 473 else 474 return XSTR (match_rtx, 1); 475 } 476 477 /* Return true if OPERAND is a MATCH_OPERAND using a special predicate 478 function. */ 479 480 static bool 481 special_predicate_operand_p (rtx operand) 482 { 483 if (GET_CODE (operand) == MATCH_OPERAND) 484 { 485 const char *pred_name = predicate_name (operand); 486 if (pred_name[0] != 0) 487 { 488 const struct pred_data *pred; 489 490 pred = lookup_predicate (pred_name); 491 return pred != NULL && pred->special; 492 } 493 } 494 495 return false; 496 } 497 498 /* Check for various errors in PATTERN, which is part of INFO. 499 SET is nonnull for a destination, and is the complete set pattern. 500 SET_CODE is '=' for normal sets, and '+' within a context that 501 requires in-out constraints. */ 502 503 static void 504 validate_pattern (rtx pattern, md_rtx_info *info, rtx set, int set_code) 505 { 506 const char *fmt; 507 RTX_CODE code; 508 size_t i, len; 509 int j; 510 511 code = GET_CODE (pattern); 512 switch (code) 513 { 514 case MATCH_SCRATCH: 515 { 516 const char constraints0 = XSTR (pattern, 1)[0]; 517 518 if (!constraints_supported_in_insn_p (info->def)) 519 { 520 if (constraints0) 521 { 522 error_at (info->loc, "constraints not supported in %s", 523 GET_RTX_NAME (GET_CODE (info->def))); 524 } 525 return; 526 } 527 528 /* If a MATCH_SCRATCH is used in a context requiring an write-only 529 or read/write register, validate that. */ 530 if (set_code == '=' 531 && constraints0 532 && constraints0 != '=' 533 && constraints0 != '+') 534 { 535 error_at (info->loc, "operand %d missing output reload", 536 XINT (pattern, 0)); 537 } 538 return; 539 } 540 case MATCH_DUP: 541 case MATCH_OP_DUP: 542 case MATCH_PAR_DUP: 543 if (find_operand (info->def, XINT (pattern, 0), pattern) == pattern) 544 error_at (info->loc, "operand %i duplicated before defined", 545 XINT (pattern, 0)); 546 break; 547 case MATCH_OPERAND: 548 case MATCH_OPERATOR: 549 { 550 const char *pred_name = XSTR (pattern, 1); 551 const struct pred_data *pred; 552 const char *c_test; 553 554 c_test = get_c_test (info->def); 555 556 if (pred_name[0] != 0) 557 { 558 pred = lookup_predicate (pred_name); 559 if (!pred) 560 error_at (info->loc, "unknown predicate '%s'", pred_name); 561 } 562 else 563 pred = 0; 564 565 if (code == MATCH_OPERAND) 566 { 567 const char *constraints = XSTR (pattern, 2); 568 const char constraints0 = constraints[0]; 569 570 if (!constraints_supported_in_insn_p (info->def)) 571 { 572 if (constraints0) 573 { 574 error_at (info->loc, "constraints not supported in %s", 575 GET_RTX_NAME (GET_CODE (info->def))); 576 } 577 } 578 579 /* A MATCH_OPERAND that is a SET should have an output reload. */ 580 else if (set && constraints0) 581 { 582 if (set_code == '+') 583 { 584 if (constraints0 == '+') 585 ; 586 /* If we've only got an output reload for this operand, 587 we'd better have a matching input operand. */ 588 else if (constraints0 == '=' 589 && find_matching_operand (info->def, 590 XINT (pattern, 0))) 591 ; 592 else 593 error_at (info->loc, "operand %d missing in-out reload", 594 XINT (pattern, 0)); 595 } 596 else if (constraints0 != '=' && constraints0 != '+') 597 error_at (info->loc, "operand %d missing output reload", 598 XINT (pattern, 0)); 599 } 600 601 /* For matching constraint in MATCH_OPERAND, the digit must be a 602 smaller number than the number of the operand that uses it in the 603 constraint. */ 604 while (1) 605 { 606 while (constraints[0] 607 && (constraints[0] == ' ' || constraints[0] == ',')) 608 constraints++; 609 if (!constraints[0]) 610 break; 611 612 if (constraints[0] >= '0' && constraints[0] <= '9') 613 { 614 int val; 615 616 sscanf (constraints, "%d", &val); 617 if (val >= XINT (pattern, 0)) 618 error_at (info->loc, "constraint digit %d is not" 619 " smaller than operand %d", 620 val, XINT (pattern, 0)); 621 } 622 623 while (constraints[0] && constraints[0] != ',') 624 constraints++; 625 } 626 } 627 628 /* Allowing non-lvalues in destinations -- particularly CONST_INT -- 629 while not likely to occur at runtime, results in less efficient 630 code from insn-recog.c. */ 631 if (set && pred && pred->allows_non_lvalue) 632 error_at (info->loc, "destination operand %d allows non-lvalue", 633 XINT (pattern, 0)); 634 635 /* A modeless MATCH_OPERAND can be handy when we can check for 636 multiple modes in the c_test. In most other cases, it is a 637 mistake. Only DEFINE_INSN is eligible, since SPLIT and 638 PEEP2 can FAIL within the output pattern. Exclude special 639 predicates, which check the mode themselves. Also exclude 640 predicates that allow only constants. Exclude the SET_DEST 641 of a call instruction, as that is a common idiom. */ 642 643 if (GET_MODE (pattern) == VOIDmode 644 && code == MATCH_OPERAND 645 && GET_CODE (info->def) == DEFINE_INSN 646 && pred 647 && !pred->special 648 && pred->allows_non_const 649 && strstr (c_test, "operands") == NULL 650 && ! (set 651 && GET_CODE (set) == SET 652 && GET_CODE (SET_SRC (set)) == CALL)) 653 message_at (info->loc, "warning: operand %d missing mode?", 654 XINT (pattern, 0)); 655 return; 656 } 657 658 case SET: 659 { 660 machine_mode dmode, smode; 661 rtx dest, src; 662 663 dest = SET_DEST (pattern); 664 src = SET_SRC (pattern); 665 666 /* STRICT_LOW_PART is a wrapper. Its argument is the real 667 destination, and it's mode should match the source. */ 668 if (GET_CODE (dest) == STRICT_LOW_PART) 669 dest = XEXP (dest, 0); 670 671 /* Find the referent for a DUP. */ 672 673 if (GET_CODE (dest) == MATCH_DUP 674 || GET_CODE (dest) == MATCH_OP_DUP 675 || GET_CODE (dest) == MATCH_PAR_DUP) 676 dest = find_operand (info->def, XINT (dest, 0), NULL); 677 678 if (GET_CODE (src) == MATCH_DUP 679 || GET_CODE (src) == MATCH_OP_DUP 680 || GET_CODE (src) == MATCH_PAR_DUP) 681 src = find_operand (info->def, XINT (src, 0), NULL); 682 683 dmode = GET_MODE (dest); 684 smode = GET_MODE (src); 685 686 /* Mode checking is not performed for special predicates. */ 687 if (special_predicate_operand_p (src) 688 || special_predicate_operand_p (dest)) 689 ; 690 691 /* The operands of a SET must have the same mode unless one 692 is VOIDmode. */ 693 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode) 694 error_at (info->loc, "mode mismatch in set: %smode vs %smode", 695 GET_MODE_NAME (dmode), GET_MODE_NAME (smode)); 696 697 /* If only one of the operands is VOIDmode, and PC or CC0 is 698 not involved, it's probably a mistake. */ 699 else if (dmode != smode 700 && GET_CODE (dest) != PC 701 && GET_CODE (dest) != CC0 702 && GET_CODE (src) != PC 703 && GET_CODE (src) != CC0 704 && !CONST_INT_P (src) 705 && !CONST_WIDE_INT_P (src) 706 && GET_CODE (src) != CALL) 707 { 708 const char *which; 709 which = (dmode == VOIDmode ? "destination" : "source"); 710 message_at (info->loc, "warning: %s missing a mode?", which); 711 } 712 713 if (dest != SET_DEST (pattern)) 714 validate_pattern (dest, info, pattern, '='); 715 validate_pattern (SET_DEST (pattern), info, pattern, '='); 716 validate_pattern (SET_SRC (pattern), info, NULL_RTX, 0); 717 return; 718 } 719 720 case CLOBBER: 721 validate_pattern (SET_DEST (pattern), info, pattern, '='); 722 return; 723 724 case ZERO_EXTRACT: 725 validate_pattern (XEXP (pattern, 0), info, set, set ? '+' : 0); 726 validate_pattern (XEXP (pattern, 1), info, NULL_RTX, 0); 727 validate_pattern (XEXP (pattern, 2), info, NULL_RTX, 0); 728 return; 729 730 case STRICT_LOW_PART: 731 validate_pattern (XEXP (pattern, 0), info, set, set ? '+' : 0); 732 return; 733 734 case LABEL_REF: 735 if (GET_MODE (XEXP (pattern, 0)) != VOIDmode) 736 error_at (info->loc, "operand to label_ref %smode not VOIDmode", 737 GET_MODE_NAME (GET_MODE (XEXP (pattern, 0)))); 738 break; 739 740 case VEC_SELECT: 741 if (GET_MODE (pattern) != VOIDmode) 742 { 743 enum machine_mode mode = GET_MODE (pattern); 744 enum machine_mode imode = GET_MODE (XEXP (pattern, 0)); 745 enum machine_mode emode 746 = VECTOR_MODE_P (mode) ? GET_MODE_INNER (mode) : mode; 747 if (GET_CODE (XEXP (pattern, 1)) == PARALLEL) 748 { 749 int expected = VECTOR_MODE_P (mode) ? GET_MODE_NUNITS (mode) : 1; 750 if (XVECLEN (XEXP (pattern, 1), 0) != expected) 751 error_at (info->loc, 752 "vec_select parallel with %d elements, expected %d", 753 XVECLEN (XEXP (pattern, 1), 0), expected); 754 } 755 if (imode != VOIDmode && !VECTOR_MODE_P (imode)) 756 error_at (info->loc, "%smode of first vec_select operand is not a " 757 "vector mode", GET_MODE_NAME (imode)); 758 else if (imode != VOIDmode && GET_MODE_INNER (imode) != emode) 759 error_at (info->loc, "element mode mismatch between vec_select " 760 "%smode and its operand %smode", 761 GET_MODE_NAME (emode), 762 GET_MODE_NAME (GET_MODE_INNER (imode))); 763 } 764 break; 765 766 default: 767 break; 768 } 769 770 fmt = GET_RTX_FORMAT (code); 771 len = GET_RTX_LENGTH (code); 772 for (i = 0; i < len; i++) 773 { 774 switch (fmt[i]) 775 { 776 case 'e': case 'u': 777 validate_pattern (XEXP (pattern, i), info, NULL_RTX, 0); 778 break; 779 780 case 'E': 781 for (j = 0; j < XVECLEN (pattern, i); j++) 782 validate_pattern (XVECEXP (pattern, i, j), info, NULL_RTX, 0); 783 break; 784 785 case 'i': case 'r': case 'w': case '0': case 's': 786 break; 787 788 default: 789 gcc_unreachable (); 790 } 791 } 792 } 793 794 /* Simple list structure for items of type T, for use when being part 795 of a list is an inherent property of T. T must have members equivalent 796 to "T *prev, *next;" and a function "void set_parent (list_head <T> *)" 797 to set the parent list. */ 798 template <typename T> 799 struct list_head 800 { 801 /* A range of linked items. */ 802 struct range 803 { 804 range (T *); 805 range (T *, T *); 806 807 T *start, *end; 808 void set_parent (list_head *); 809 }; 810 811 list_head (); 812 range release (); 813 void push_back (range); 814 range remove (range); 815 void replace (range, range); 816 T *singleton () const; 817 818 T *first, *last; 819 }; 820 821 /* Create a range [START_IN, START_IN]. */ 822 823 template <typename T> 824 list_head <T>::range::range (T *start_in) : start (start_in), end (start_in) {} 825 826 /* Create a range [START_IN, END_IN], linked by next and prev fields. */ 827 828 template <typename T> 829 list_head <T>::range::range (T *start_in, T *end_in) 830 : start (start_in), end (end_in) {} 831 832 template <typename T> 833 void 834 list_head <T>::range::set_parent (list_head <T> *owner) 835 { 836 for (T *item = start; item != end; item = item->next) 837 item->set_parent (owner); 838 end->set_parent (owner); 839 } 840 841 template <typename T> 842 list_head <T>::list_head () : first (0), last (0) {} 843 844 /* Add R to the end of the list. */ 845 846 template <typename T> 847 void 848 list_head <T>::push_back (range r) 849 { 850 if (last) 851 last->next = r.start; 852 else 853 first = r.start; 854 r.start->prev = last; 855 last = r.end; 856 r.set_parent (this); 857 } 858 859 /* Remove R from the list. R remains valid and can be inserted into 860 other lists. */ 861 862 template <typename T> 863 typename list_head <T>::range 864 list_head <T>::remove (range r) 865 { 866 if (r.start->prev) 867 r.start->prev->next = r.end->next; 868 else 869 first = r.end->next; 870 if (r.end->next) 871 r.end->next->prev = r.start->prev; 872 else 873 last = r.start->prev; 874 r.start->prev = 0; 875 r.end->next = 0; 876 r.set_parent (0); 877 return r; 878 } 879 880 /* Replace OLDR with NEWR. OLDR remains valid and can be inserted into 881 other lists. */ 882 883 template <typename T> 884 void 885 list_head <T>::replace (range oldr, range newr) 886 { 887 newr.start->prev = oldr.start->prev; 888 newr.end->next = oldr.end->next; 889 890 oldr.start->prev = 0; 891 oldr.end->next = 0; 892 oldr.set_parent (0); 893 894 if (newr.start->prev) 895 newr.start->prev->next = newr.start; 896 else 897 first = newr.start; 898 if (newr.end->next) 899 newr.end->next->prev = newr.end; 900 else 901 last = newr.end; 902 newr.set_parent (this); 903 } 904 905 /* Empty the list and return the previous contents as a range that can 906 be inserted into other lists. */ 907 908 template <typename T> 909 typename list_head <T>::range 910 list_head <T>::release () 911 { 912 range r (first, last); 913 first = 0; 914 last = 0; 915 r.set_parent (0); 916 return r; 917 } 918 919 /* If the list contains a single item, return that item, otherwise return 920 null. */ 921 922 template <typename T> 923 T * 924 list_head <T>::singleton () const 925 { 926 return first == last ? first : 0; 927 } 928 929 struct state; 930 931 /* Describes a possible successful return from a routine. */ 932 struct acceptance_type 933 { 934 /* The type of routine we're returning from. */ 935 routine_type type : 16; 936 937 /* True if this structure only really represents a partial match, 938 and if we must call a subroutine of type TYPE to complete the match. 939 In this case we'll call the subroutine and, if it succeeds, return 940 whatever the subroutine returned. 941 942 False if this structure presents a full match. */ 943 unsigned int partial_p : 1; 944 945 union 946 { 947 /* If PARTIAL_P, this is the number of the subroutine to call. */ 948 int subroutine_id; 949 950 /* Valid if !PARTIAL_P. */ 951 struct 952 { 953 /* The identifier of the matching pattern. For SUBPATTERNs this 954 value belongs to an ad-hoc routine-specific enum. For the 955 others it's the number of an .md file pattern. */ 956 int code; 957 union 958 { 959 /* For RECOG, the number of clobbers that must be added to the 960 pattern in order for it to match CODE. */ 961 int num_clobbers; 962 963 /* For PEEPHOLE2, the number of additional instructions that were 964 included in the optimization. */ 965 int match_len; 966 } u; 967 } full; 968 } u; 969 }; 970 971 bool 972 operator == (const acceptance_type &a, const acceptance_type &b) 973 { 974 if (a.partial_p != b.partial_p) 975 return false; 976 if (a.partial_p) 977 return a.u.subroutine_id == b.u.subroutine_id; 978 else 979 return a.u.full.code == b.u.full.code; 980 } 981 982 bool 983 operator != (const acceptance_type &a, const acceptance_type &b) 984 { 985 return !operator == (a, b); 986 } 987 988 /* Represents a parameter to a pattern routine. */ 989 struct parameter 990 { 991 /* The C type of parameter. */ 992 enum type_enum { 993 /* Represents an invalid parameter. */ 994 UNSET, 995 996 /* A machine_mode parameter. */ 997 MODE, 998 999 /* An rtx_code parameter. */ 1000 CODE, 1001 1002 /* An int parameter. */ 1003 INT, 1004 1005 /* An unsigned int parameter. */ 1006 UINT, 1007 1008 /* A HOST_WIDE_INT parameter. */ 1009 WIDE_INT 1010 }; 1011 1012 parameter (); 1013 parameter (type_enum, bool, uint64_t); 1014 1015 /* The type of the parameter. */ 1016 type_enum type; 1017 1018 /* True if the value passed is variable, false if it is constant. */ 1019 bool is_param; 1020 1021 /* If IS_PARAM, this is the number of the variable passed, for an "i%d" 1022 format string. If !IS_PARAM, this is the constant value passed. */ 1023 uint64_t value; 1024 }; 1025 1026 parameter::parameter () 1027 : type (UNSET), is_param (false), value (0) {} 1028 1029 parameter::parameter (type_enum type_in, bool is_param_in, uint64_t value_in) 1030 : type (type_in), is_param (is_param_in), value (value_in) {} 1031 1032 bool 1033 operator == (const parameter ¶m1, const parameter ¶m2) 1034 { 1035 return (param1.type == param2.type 1036 && param1.is_param == param2.is_param 1037 && param1.value == param2.value); 1038 } 1039 1040 bool 1041 operator != (const parameter ¶m1, const parameter ¶m2) 1042 { 1043 return !operator == (param1, param2); 1044 } 1045 1046 /* Represents a routine that matches a partial rtx pattern, returning 1047 an ad-hoc enum value on success and -1 on failure. The routine can 1048 be used by any subroutine type. The match can be parameterized by 1049 things like mode, code and UNSPEC number. */ 1050 struct pattern_routine 1051 { 1052 /* The state that implements the pattern. */ 1053 state *s; 1054 1055 /* The deepest root position from which S can access all the rtxes it needs. 1056 This is NULL if the pattern doesn't need an rtx input, usually because 1057 all matching is done on operands[] instead. */ 1058 position *pos; 1059 1060 /* A unique identifier for the routine. */ 1061 unsigned int pattern_id; 1062 1063 /* True if the routine takes pnum_clobbers as argument. */ 1064 bool pnum_clobbers_p; 1065 1066 /* True if the routine takes the enclosing instruction as argument. */ 1067 bool insn_p; 1068 1069 /* The types of the other parameters to the routine, if any. */ 1070 auto_vec <parameter::type_enum, MAX_PATTERN_PARAMS> param_types; 1071 }; 1072 1073 /* All defined patterns. */ 1074 static vec <pattern_routine *> patterns; 1075 1076 /* Represents one use of a pattern routine. */ 1077 struct pattern_use 1078 { 1079 /* The pattern routine to use. */ 1080 pattern_routine *routine; 1081 1082 /* The values to pass as parameters. This vector has the same length 1083 as ROUTINE->PARAM_TYPES. */ 1084 auto_vec <parameter, MAX_PATTERN_PARAMS> params; 1085 }; 1086 1087 /* Represents a test performed by a decision. */ 1088 struct rtx_test 1089 { 1090 rtx_test (); 1091 1092 /* The types of test that can be performed. Most of them take as input 1093 an rtx X. Some also take as input a transition label LABEL; the others 1094 are booleans for which the transition label is always "true". 1095 1096 The order of the enum isn't important. */ 1097 enum kind_enum { 1098 /* Check GET_CODE (X) == LABEL. */ 1099 CODE, 1100 1101 /* Check GET_MODE (X) == LABEL. */ 1102 MODE, 1103 1104 /* Check REGNO (X) == LABEL. */ 1105 REGNO_FIELD, 1106 1107 /* Check XINT (X, u.opno) == LABEL. */ 1108 INT_FIELD, 1109 1110 /* Check XWINT (X, u.opno) == LABEL. */ 1111 WIDE_INT_FIELD, 1112 1113 /* Check XVECLEN (X, 0) == LABEL. */ 1114 VECLEN, 1115 1116 /* Check peep2_current_count >= u.min_len. */ 1117 PEEP2_COUNT, 1118 1119 /* Check XVECLEN (X, 0) >= u.min_len. */ 1120 VECLEN_GE, 1121 1122 /* Check whether X is a cached const_int with value u.integer. */ 1123 SAVED_CONST_INT, 1124 1125 /* Check u.predicate.data (X, u.predicate.mode). */ 1126 PREDICATE, 1127 1128 /* Check rtx_equal_p (X, operands[u.opno]). */ 1129 DUPLICATE, 1130 1131 /* Check whether X matches pattern u.pattern. */ 1132 PATTERN, 1133 1134 /* Check whether pnum_clobbers is nonnull (RECOG only). */ 1135 HAVE_NUM_CLOBBERS, 1136 1137 /* Check whether general C test u.string holds. In general the condition 1138 needs access to "insn" and the full operand list. */ 1139 C_TEST, 1140 1141 /* Execute operands[u.opno] = X. (Always succeeds.) */ 1142 SET_OP, 1143 1144 /* Accept u.acceptance. Always succeeds for SUBPATTERN, RECOG and SPLIT. 1145 May fail for PEEPHOLE2 if the define_peephole2 C code executes FAIL. */ 1146 ACCEPT 1147 }; 1148 1149 /* The position of rtx X in the above description, relative to the 1150 incoming instruction "insn". The position is null if the test 1151 doesn't take an X as input. */ 1152 position *pos; 1153 1154 /* Which element of operands[] already contains POS, or -1 if no element 1155 is known to hold POS. */ 1156 int pos_operand; 1157 1158 /* The type of test and its parameters, as described above. */ 1159 kind_enum kind; 1160 union 1161 { 1162 int opno; 1163 int min_len; 1164 struct 1165 { 1166 bool is_param; 1167 int value; 1168 } integer; 1169 struct 1170 { 1171 const struct pred_data *data; 1172 /* True if the mode is taken from a machine_mode parameter 1173 to the routine rather than a constant machine_mode. If true, 1174 MODE is the number of the parameter (for an "i%d" format string), 1175 otherwise it is the mode itself. */ 1176 bool mode_is_param; 1177 unsigned int mode; 1178 } predicate; 1179 pattern_use *pattern; 1180 const char *string; 1181 acceptance_type acceptance; 1182 } u; 1183 1184 static rtx_test code (position *); 1185 static rtx_test mode (position *); 1186 static rtx_test regno_field (position *); 1187 static rtx_test int_field (position *, int); 1188 static rtx_test wide_int_field (position *, int); 1189 static rtx_test veclen (position *); 1190 static rtx_test peep2_count (int); 1191 static rtx_test veclen_ge (position *, int); 1192 static rtx_test predicate (position *, const pred_data *, machine_mode); 1193 static rtx_test duplicate (position *, int); 1194 static rtx_test pattern (position *, pattern_use *); 1195 static rtx_test have_num_clobbers (); 1196 static rtx_test c_test (const char *); 1197 static rtx_test set_op (position *, int); 1198 static rtx_test accept (const acceptance_type &); 1199 1200 bool terminal_p () const; 1201 bool single_outcome_p () const; 1202 1203 private: 1204 rtx_test (position *, kind_enum); 1205 }; 1206 1207 rtx_test::rtx_test () {} 1208 1209 rtx_test::rtx_test (position *pos_in, kind_enum kind_in) 1210 : pos (pos_in), pos_operand (-1), kind (kind_in) {} 1211 1212 rtx_test 1213 rtx_test::code (position *pos) 1214 { 1215 return rtx_test (pos, rtx_test::CODE); 1216 } 1217 1218 rtx_test 1219 rtx_test::mode (position *pos) 1220 { 1221 return rtx_test (pos, rtx_test::MODE); 1222 } 1223 1224 rtx_test 1225 rtx_test::regno_field (position *pos) 1226 { 1227 rtx_test res (pos, rtx_test::REGNO_FIELD); 1228 return res; 1229 } 1230 1231 rtx_test 1232 rtx_test::int_field (position *pos, int opno) 1233 { 1234 rtx_test res (pos, rtx_test::INT_FIELD); 1235 res.u.opno = opno; 1236 return res; 1237 } 1238 1239 rtx_test 1240 rtx_test::wide_int_field (position *pos, int opno) 1241 { 1242 rtx_test res (pos, rtx_test::WIDE_INT_FIELD); 1243 res.u.opno = opno; 1244 return res; 1245 } 1246 1247 rtx_test 1248 rtx_test::veclen (position *pos) 1249 { 1250 return rtx_test (pos, rtx_test::VECLEN); 1251 } 1252 1253 rtx_test 1254 rtx_test::peep2_count (int min_len) 1255 { 1256 rtx_test res (0, rtx_test::PEEP2_COUNT); 1257 res.u.min_len = min_len; 1258 return res; 1259 } 1260 1261 rtx_test 1262 rtx_test::veclen_ge (position *pos, int min_len) 1263 { 1264 rtx_test res (pos, rtx_test::VECLEN_GE); 1265 res.u.min_len = min_len; 1266 return res; 1267 } 1268 1269 rtx_test 1270 rtx_test::predicate (position *pos, const struct pred_data *data, 1271 machine_mode mode) 1272 { 1273 rtx_test res (pos, rtx_test::PREDICATE); 1274 res.u.predicate.data = data; 1275 res.u.predicate.mode_is_param = false; 1276 res.u.predicate.mode = mode; 1277 return res; 1278 } 1279 1280 rtx_test 1281 rtx_test::duplicate (position *pos, int opno) 1282 { 1283 rtx_test res (pos, rtx_test::DUPLICATE); 1284 res.u.opno = opno; 1285 return res; 1286 } 1287 1288 rtx_test 1289 rtx_test::pattern (position *pos, pattern_use *pattern) 1290 { 1291 rtx_test res (pos, rtx_test::PATTERN); 1292 res.u.pattern = pattern; 1293 return res; 1294 } 1295 1296 rtx_test 1297 rtx_test::have_num_clobbers () 1298 { 1299 return rtx_test (0, rtx_test::HAVE_NUM_CLOBBERS); 1300 } 1301 1302 rtx_test 1303 rtx_test::c_test (const char *string) 1304 { 1305 rtx_test res (0, rtx_test::C_TEST); 1306 res.u.string = string; 1307 return res; 1308 } 1309 1310 rtx_test 1311 rtx_test::set_op (position *pos, int opno) 1312 { 1313 rtx_test res (pos, rtx_test::SET_OP); 1314 res.u.opno = opno; 1315 return res; 1316 } 1317 1318 rtx_test 1319 rtx_test::accept (const acceptance_type &acceptance) 1320 { 1321 rtx_test res (0, rtx_test::ACCEPT); 1322 res.u.acceptance = acceptance; 1323 return res; 1324 } 1325 1326 /* Return true if the test represents an unconditionally successful match. */ 1327 1328 bool 1329 rtx_test::terminal_p () const 1330 { 1331 return kind == rtx_test::ACCEPT && u.acceptance.type != PEEPHOLE2; 1332 } 1333 1334 /* Return true if the test is a boolean that is always true. */ 1335 1336 bool 1337 rtx_test::single_outcome_p () const 1338 { 1339 return terminal_p () || kind == rtx_test::SET_OP; 1340 } 1341 1342 bool 1343 operator == (const rtx_test &a, const rtx_test &b) 1344 { 1345 if (a.pos != b.pos || a.kind != b.kind) 1346 return false; 1347 switch (a.kind) 1348 { 1349 case rtx_test::CODE: 1350 case rtx_test::MODE: 1351 case rtx_test::REGNO_FIELD: 1352 case rtx_test::VECLEN: 1353 case rtx_test::HAVE_NUM_CLOBBERS: 1354 return true; 1355 1356 case rtx_test::PEEP2_COUNT: 1357 case rtx_test::VECLEN_GE: 1358 return a.u.min_len == b.u.min_len; 1359 1360 case rtx_test::INT_FIELD: 1361 case rtx_test::WIDE_INT_FIELD: 1362 case rtx_test::DUPLICATE: 1363 case rtx_test::SET_OP: 1364 return a.u.opno == b.u.opno; 1365 1366 case rtx_test::SAVED_CONST_INT: 1367 return (a.u.integer.is_param == b.u.integer.is_param 1368 && a.u.integer.value == b.u.integer.value); 1369 1370 case rtx_test::PREDICATE: 1371 return (a.u.predicate.data == b.u.predicate.data 1372 && a.u.predicate.mode_is_param == b.u.predicate.mode_is_param 1373 && a.u.predicate.mode == b.u.predicate.mode); 1374 1375 case rtx_test::PATTERN: 1376 return (a.u.pattern->routine == b.u.pattern->routine 1377 && a.u.pattern->params == b.u.pattern->params); 1378 1379 case rtx_test::C_TEST: 1380 return strcmp (a.u.string, b.u.string) == 0; 1381 1382 case rtx_test::ACCEPT: 1383 return a.u.acceptance == b.u.acceptance; 1384 } 1385 gcc_unreachable (); 1386 } 1387 1388 bool 1389 operator != (const rtx_test &a, const rtx_test &b) 1390 { 1391 return !operator == (a, b); 1392 } 1393 1394 /* A simple set of transition labels. Most transitions have a singleton 1395 label, so try to make that case as efficient as possible. */ 1396 struct int_set : public auto_vec <uint64_t, 1> 1397 { 1398 typedef uint64_t *iterator; 1399 1400 int_set (); 1401 int_set (uint64_t); 1402 int_set (const int_set &); 1403 1404 int_set &operator = (const int_set &); 1405 1406 iterator begin (); 1407 iterator end (); 1408 }; 1409 1410 int_set::int_set () {} 1411 1412 int_set::int_set (uint64_t label) 1413 { 1414 safe_push (label); 1415 } 1416 1417 int_set::int_set (const int_set &other) 1418 { 1419 safe_splice (other); 1420 } 1421 1422 int_set & 1423 int_set::operator = (const int_set &other) 1424 { 1425 truncate (0); 1426 safe_splice (other); 1427 return *this; 1428 } 1429 1430 int_set::iterator 1431 int_set::begin () 1432 { 1433 return address (); 1434 } 1435 1436 int_set::iterator 1437 int_set::end () 1438 { 1439 return address () + length (); 1440 } 1441 1442 bool 1443 operator == (const int_set &a, const int_set &b) 1444 { 1445 if (a.length () != b.length ()) 1446 return false; 1447 for (unsigned int i = 0; i < a.length (); ++i) 1448 if (a[i] != b[i]) 1449 return false; 1450 return true; 1451 } 1452 1453 bool 1454 operator != (const int_set &a, const int_set &b) 1455 { 1456 return !operator == (a, b); 1457 } 1458 1459 struct decision; 1460 1461 /* Represents a transition between states, dependent on the result of 1462 a test T. */ 1463 struct transition 1464 { 1465 transition (const int_set &, state *, bool); 1466 1467 void set_parent (list_head <transition> *); 1468 1469 /* Links to other transitions for T. Always null for boolean tests. */ 1470 transition *prev, *next; 1471 1472 /* The transition should be taken when T has one of these values. 1473 E.g. for rtx_test::CODE this is a set of codes, while for booleans like 1474 rtx_test::PREDICATE it is always a singleton "true". The labels are 1475 sorted in ascending order. */ 1476 int_set labels; 1477 1478 /* The source decision. */ 1479 decision *from; 1480 1481 /* The target state. */ 1482 state *to; 1483 1484 /* True if TO would function correctly even if TEST wasn't performed. 1485 E.g. it isn't necessary to check whether GET_MODE (x1) is SImode 1486 before calling register_operand (x1, SImode), since register_operand 1487 performs its own mode check. However, checking GET_MODE can be a cheap 1488 way of disambiguating SImode and DImode register operands. */ 1489 bool optional; 1490 1491 /* True if LABELS contains parameter numbers rather than constants. 1492 E.g. if this is true for a rtx_test::CODE, the label is the number 1493 of an rtx_code parameter rather than an rtx_code itself. 1494 LABELS is always a singleton when this variable is true. */ 1495 bool is_param; 1496 }; 1497 1498 /* Represents a test and the action that should be taken on the result. 1499 If a transition exists for the test outcome, the machine switches 1500 to the transition's target state. If no suitable transition exists, 1501 the machine either falls through to the next decision or, if there are no 1502 more decisions to try, fails the match. */ 1503 struct decision : list_head <transition> 1504 { 1505 decision (const rtx_test &); 1506 1507 void set_parent (list_head <decision> *s); 1508 bool if_statement_p (uint64_t * = 0) const; 1509 1510 /* The state to which this decision belongs. */ 1511 state *s; 1512 1513 /* Links to other decisions in the same state. */ 1514 decision *prev, *next; 1515 1516 /* The test to perform. */ 1517 rtx_test test; 1518 }; 1519 1520 /* Represents one machine state. For each state the machine tries a list 1521 of decisions, in order, and acts on the first match. It fails without 1522 further backtracking if no decisions match. */ 1523 struct state : list_head <decision> 1524 { 1525 void set_parent (list_head <state> *) {} 1526 }; 1527 1528 transition::transition (const int_set &labels_in, state *to_in, 1529 bool optional_in) 1530 : prev (0), next (0), labels (labels_in), from (0), to (to_in), 1531 optional (optional_in), is_param (false) {} 1532 1533 /* Set the source decision of the transition. */ 1534 1535 void 1536 transition::set_parent (list_head <transition> *from_in) 1537 { 1538 from = static_cast <decision *> (from_in); 1539 } 1540 1541 decision::decision (const rtx_test &test_in) 1542 : prev (0), next (0), test (test_in) {} 1543 1544 /* Set the state to which this decision belongs. */ 1545 1546 void 1547 decision::set_parent (list_head <decision> *s_in) 1548 { 1549 s = static_cast <state *> (s_in); 1550 } 1551 1552 /* Return true if the decision has a single transition with a single label. 1553 If so, return the label in *LABEL if nonnull. */ 1554 1555 inline bool 1556 decision::if_statement_p (uint64_t *label) const 1557 { 1558 if (singleton () && first->labels.length () == 1) 1559 { 1560 if (label) 1561 *label = first->labels[0]; 1562 return true; 1563 } 1564 return false; 1565 } 1566 1567 /* Add to FROM a decision that performs TEST and has a single transition 1568 TRANS. */ 1569 1570 static void 1571 add_decision (state *from, const rtx_test &test, transition *trans) 1572 { 1573 decision *d = new decision (test); 1574 from->push_back (d); 1575 d->push_back (trans); 1576 } 1577 1578 /* Add a transition from FROM to a new, empty state that is taken 1579 when TEST == LABELS. OPTIONAL says whether the new transition 1580 should be optional. Return the new state. */ 1581 1582 static state * 1583 add_decision (state *from, const rtx_test &test, int_set labels, bool optional) 1584 { 1585 state *to = new state; 1586 add_decision (from, test, new transition (labels, to, optional)); 1587 return to; 1588 } 1589 1590 /* Insert a decision before decisions R to make them dependent on 1591 TEST == LABELS. OPTIONAL says whether the new transition should be 1592 optional. */ 1593 1594 static decision * 1595 insert_decision_before (state::range r, const rtx_test &test, 1596 const int_set &labels, bool optional) 1597 { 1598 decision *newd = new decision (test); 1599 state *news = new state; 1600 newd->push_back (new transition (labels, news, optional)); 1601 r.start->s->replace (r, newd); 1602 news->push_back (r); 1603 return newd; 1604 } 1605 1606 /* Remove any optional transitions from S that turned out not to be useful. */ 1607 1608 static void 1609 collapse_optional_decisions (state *s) 1610 { 1611 decision *d = s->first; 1612 while (d) 1613 { 1614 decision *next = d->next; 1615 for (transition *trans = d->first; trans; trans = trans->next) 1616 collapse_optional_decisions (trans->to); 1617 /* A decision with a single optional transition doesn't help 1618 partition the potential matches and so is unlikely to be 1619 worthwhile. In particular, if the decision that performs the 1620 test is the last in the state, the best it could do is reject 1621 an invalid pattern slightly earlier. If instead the decision 1622 is not the last in the state, the condition it tests could hold 1623 even for the later decisions in the state. The best it can do 1624 is save work in some cases where only the later decisions can 1625 succeed. 1626 1627 In both cases the optional transition would add extra work to 1628 successful matches when the tested condition holds. */ 1629 if (transition *trans = d->singleton ()) 1630 if (trans->optional) 1631 s->replace (d, trans->to->release ()); 1632 d = next; 1633 } 1634 } 1635 1636 /* Try to squash several separate tests into simpler ones. */ 1637 1638 static void 1639 simplify_tests (state *s) 1640 { 1641 for (decision *d = s->first; d; d = d->next) 1642 { 1643 uint64_t label; 1644 /* Convert checks for GET_CODE (x) == CONST_INT and XWINT (x, 0) == N 1645 into checks for const_int_rtx[N'], if N is suitably small. */ 1646 if (d->test.kind == rtx_test::CODE 1647 && d->if_statement_p (&label) 1648 && label == CONST_INT) 1649 if (decision *second = d->first->to->singleton ()) 1650 if (d->test.pos == second->test.pos 1651 && second->test.kind == rtx_test::WIDE_INT_FIELD 1652 && second->test.u.opno == 0 1653 && second->if_statement_p (&label) 1654 && IN_RANGE (int64_t (label), 1655 -MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT)) 1656 { 1657 d->test.kind = rtx_test::SAVED_CONST_INT; 1658 d->test.u.integer.is_param = false; 1659 d->test.u.integer.value = label; 1660 d->replace (d->first, second->release ()); 1661 d->first->labels[0] = true; 1662 } 1663 /* If we have a CODE test followed by a PREDICATE test, rely on 1664 the predicate to test the code. 1665 1666 This case exists for match_operators. We initially treat the 1667 CODE test for a match_operator as non-optional so that we can 1668 safely move down to its operands. It may turn out that all 1669 paths that reach that code test require the same predicate 1670 to be true. cse_tests will then put the predicate test in 1671 series with the code test. */ 1672 if (d->test.kind == rtx_test::CODE) 1673 if (transition *trans = d->singleton ()) 1674 { 1675 state *s = trans->to; 1676 while (decision *d2 = s->singleton ()) 1677 { 1678 if (d->test.pos != d2->test.pos) 1679 break; 1680 transition *trans2 = d2->singleton (); 1681 if (!trans2) 1682 break; 1683 if (d2->test.kind == rtx_test::PREDICATE) 1684 { 1685 d->test = d2->test; 1686 trans->labels = int_set (true); 1687 s->replace (d2, trans2->to->release ()); 1688 break; 1689 } 1690 s = trans2->to; 1691 } 1692 } 1693 for (transition *trans = d->first; trans; trans = trans->next) 1694 simplify_tests (trans->to); 1695 } 1696 } 1697 1698 /* Return true if all successful returns passing through D require the 1699 condition tested by COMMON to be true. 1700 1701 When returning true, add all transitions like COMMON in D to WHERE. 1702 WHERE may contain a partial result on failure. */ 1703 1704 static bool 1705 common_test_p (decision *d, transition *common, vec <transition *> *where) 1706 { 1707 if (d->test.kind == rtx_test::ACCEPT) 1708 /* We found a successful return that didn't require COMMON. */ 1709 return false; 1710 if (d->test == common->from->test) 1711 { 1712 transition *trans = d->singleton (); 1713 if (!trans 1714 || trans->optional != common->optional 1715 || trans->labels != common->labels) 1716 return false; 1717 where->safe_push (trans); 1718 return true; 1719 } 1720 for (transition *trans = d->first; trans; trans = trans->next) 1721 for (decision *subd = trans->to->first; subd; subd = subd->next) 1722 if (!common_test_p (subd, common, where)) 1723 return false; 1724 return true; 1725 } 1726 1727 /* Indicates that we have tested GET_CODE (X) for a particular rtx X. */ 1728 const unsigned char TESTED_CODE = 1; 1729 1730 /* Indicates that we have tested XVECLEN (X, 0) for a particular rtx X. */ 1731 const unsigned char TESTED_VECLEN = 2; 1732 1733 /* Represents a set of conditions that are known to hold. */ 1734 struct known_conditions 1735 { 1736 /* A mask of TESTED_ values for each position, indexed by the position's 1737 id field. */ 1738 auto_vec <unsigned char> position_tests; 1739 1740 /* Index N says whether operands[N] has been set. */ 1741 auto_vec <bool> set_operands; 1742 1743 /* A guranteed lower bound on the value of peep2_current_count. */ 1744 int peep2_count; 1745 }; 1746 1747 /* Return true if TEST can safely be performed at D, where 1748 the conditions in KC hold. TEST is known to occur along the 1749 first path from D (i.e. always following the first transition 1750 of the first decision). Any intervening tests can be used as 1751 negative proof that hoisting isn't safe, but only KC can be used 1752 as positive proof. */ 1753 1754 static bool 1755 safe_to_hoist_p (decision *d, const rtx_test &test, known_conditions *kc) 1756 { 1757 switch (test.kind) 1758 { 1759 case rtx_test::C_TEST: 1760 /* In general, C tests require everything else to have been 1761 verified and all operands to have been set up. */ 1762 return false; 1763 1764 case rtx_test::ACCEPT: 1765 /* Don't accept something before all conditions have been tested. */ 1766 return false; 1767 1768 case rtx_test::PREDICATE: 1769 /* Don't move a predicate over a test for VECLEN_GE, since the 1770 predicate used in a match_parallel can legitimately expect the 1771 length to be checked first. */ 1772 for (decision *subd = d; 1773 subd->test != test; 1774 subd = subd->first->to->first) 1775 if (subd->test.pos == test.pos 1776 && subd->test.kind == rtx_test::VECLEN_GE) 1777 return false; 1778 goto any_rtx; 1779 1780 case rtx_test::DUPLICATE: 1781 /* Don't test for a match_dup until the associated operand has 1782 been set. */ 1783 if (!kc->set_operands[test.u.opno]) 1784 return false; 1785 goto any_rtx; 1786 1787 case rtx_test::CODE: 1788 case rtx_test::MODE: 1789 case rtx_test::SAVED_CONST_INT: 1790 case rtx_test::SET_OP: 1791 any_rtx: 1792 /* Check whether it is safe to access the rtx under test. */ 1793 switch (test.pos->type) 1794 { 1795 case POS_PEEP2_INSN: 1796 return test.pos->arg < kc->peep2_count; 1797 1798 case POS_XEXP: 1799 return kc->position_tests[test.pos->base->id] & TESTED_CODE; 1800 1801 case POS_XVECEXP0: 1802 return kc->position_tests[test.pos->base->id] & TESTED_VECLEN; 1803 } 1804 gcc_unreachable (); 1805 1806 case rtx_test::REGNO_FIELD: 1807 case rtx_test::INT_FIELD: 1808 case rtx_test::WIDE_INT_FIELD: 1809 case rtx_test::VECLEN: 1810 case rtx_test::VECLEN_GE: 1811 /* These tests access a specific part of an rtx, so are only safe 1812 once we know what the rtx is. */ 1813 return kc->position_tests[test.pos->id] & TESTED_CODE; 1814 1815 case rtx_test::PEEP2_COUNT: 1816 case rtx_test::HAVE_NUM_CLOBBERS: 1817 /* These tests can be performed anywhere. */ 1818 return true; 1819 1820 case rtx_test::PATTERN: 1821 gcc_unreachable (); 1822 } 1823 gcc_unreachable (); 1824 } 1825 1826 /* Look for a transition that is taken by all successful returns from a range 1827 of decisions starting at OUTER and that would be better performed by 1828 OUTER's state instead. On success, store all instances of that transition 1829 in WHERE and return the last decision in the range. The range could 1830 just be OUTER, or it could include later decisions as well. 1831 1832 WITH_POSITION_P is true if only tests with position POS should be tried, 1833 false if any test should be tried. WORTHWHILE_SINGLE_P is true if the 1834 result is useful even when the range contains just a single decision 1835 with a single transition. KC are the conditions that are known to 1836 hold at OUTER. */ 1837 1838 static decision * 1839 find_common_test (decision *outer, bool with_position_p, 1840 position *pos, bool worthwhile_single_p, 1841 known_conditions *kc, vec <transition *> *where) 1842 { 1843 /* After this, WORTHWHILE_SINGLE_P indicates whether a range that contains 1844 just a single decision is useful, regardless of the number of 1845 transitions it has. */ 1846 if (!outer->singleton ()) 1847 worthwhile_single_p = true; 1848 /* Quick exit if we don't have enough decisions to form a worthwhile 1849 range. */ 1850 if (!worthwhile_single_p && !outer->next) 1851 return 0; 1852 /* Follow the first chain down, as one example of a path that needs 1853 to contain the common test. */ 1854 for (decision *d = outer; d; d = d->first->to->first) 1855 { 1856 transition *trans = d->singleton (); 1857 if (trans 1858 && (!with_position_p || d->test.pos == pos) 1859 && safe_to_hoist_p (outer, d->test, kc)) 1860 { 1861 if (common_test_p (outer, trans, where)) 1862 { 1863 if (!outer->next) 1864 /* We checked above whether the move is worthwhile. */ 1865 return outer; 1866 /* See how many decisions in OUTER's chain could reuse 1867 the same test. */ 1868 decision *outer_end = outer; 1869 do 1870 { 1871 unsigned int length = where->length (); 1872 if (!common_test_p (outer_end->next, trans, where)) 1873 { 1874 where->truncate (length); 1875 break; 1876 } 1877 outer_end = outer_end->next; 1878 } 1879 while (outer_end->next); 1880 /* It is worth moving TRANS if it can be shared by more than 1881 one decision. */ 1882 if (outer_end != outer || worthwhile_single_p) 1883 return outer_end; 1884 } 1885 where->truncate (0); 1886 } 1887 } 1888 return 0; 1889 } 1890 1891 /* Try to promote common subtests in S to a single, shared decision. 1892 Also try to bunch tests for the same position together. POS is the 1893 position of the rtx tested before reaching S. KC are the conditions 1894 that are known to hold on entry to S. */ 1895 1896 static void 1897 cse_tests (position *pos, state *s, known_conditions *kc) 1898 { 1899 for (decision *d = s->first; d; d = d->next) 1900 { 1901 auto_vec <transition *, 16> where; 1902 if (d->test.pos) 1903 { 1904 /* Try to find conditions that don't depend on a particular rtx, 1905 such as pnum_clobbers != NULL or peep2_current_count >= X. 1906 It's usually better to check these conditions as soon as 1907 possible, so the change is worthwhile even if there is 1908 only one copy of the test. */ 1909 decision *endd = find_common_test (d, true, 0, true, kc, &where); 1910 if (!endd && d->test.pos != pos) 1911 /* Try to find other conditions related to position POS 1912 before moving to the new position. Again, this is 1913 worthwhile even if there is only one copy of the test, 1914 since it means that fewer position variables are live 1915 at a given time. */ 1916 endd = find_common_test (d, true, pos, true, kc, &where); 1917 if (!endd) 1918 /* Try to find any condition that is used more than once. */ 1919 endd = find_common_test (d, false, 0, false, kc, &where); 1920 if (endd) 1921 { 1922 transition *common = where[0]; 1923 /* Replace [D, ENDD] with a test like COMMON. We'll recurse 1924 on the common test and see the original D again next time. */ 1925 d = insert_decision_before (state::range (d, endd), 1926 common->from->test, 1927 common->labels, 1928 common->optional); 1929 /* Remove the old tests. */ 1930 while (!where.is_empty ()) 1931 { 1932 transition *trans = where.pop (); 1933 trans->from->s->replace (trans->from, trans->to->release ()); 1934 } 1935 } 1936 } 1937 1938 /* Make sure that safe_to_hoist_p isn't being overly conservative. 1939 It should realize that D's test is safe in the current 1940 environment. */ 1941 gcc_assert (d->test.kind == rtx_test::C_TEST 1942 || d->test.kind == rtx_test::ACCEPT 1943 || safe_to_hoist_p (d, d->test, kc)); 1944 1945 /* D won't be changed any further by the current optimization. 1946 Recurse with the state temporarily updated to include D. */ 1947 int prev = 0; 1948 switch (d->test.kind) 1949 { 1950 case rtx_test::CODE: 1951 prev = kc->position_tests[d->test.pos->id]; 1952 kc->position_tests[d->test.pos->id] |= TESTED_CODE; 1953 break; 1954 1955 case rtx_test::VECLEN: 1956 case rtx_test::VECLEN_GE: 1957 prev = kc->position_tests[d->test.pos->id]; 1958 kc->position_tests[d->test.pos->id] |= TESTED_VECLEN; 1959 break; 1960 1961 case rtx_test::SET_OP: 1962 prev = kc->set_operands[d->test.u.opno]; 1963 gcc_assert (!prev); 1964 kc->set_operands[d->test.u.opno] = true; 1965 break; 1966 1967 case rtx_test::PEEP2_COUNT: 1968 prev = kc->peep2_count; 1969 kc->peep2_count = MAX (prev, d->test.u.min_len); 1970 break; 1971 1972 default: 1973 break; 1974 } 1975 for (transition *trans = d->first; trans; trans = trans->next) 1976 cse_tests (d->test.pos ? d->test.pos : pos, trans->to, kc); 1977 switch (d->test.kind) 1978 { 1979 case rtx_test::CODE: 1980 case rtx_test::VECLEN: 1981 case rtx_test::VECLEN_GE: 1982 kc->position_tests[d->test.pos->id] = prev; 1983 break; 1984 1985 case rtx_test::SET_OP: 1986 kc->set_operands[d->test.u.opno] = prev; 1987 break; 1988 1989 case rtx_test::PEEP2_COUNT: 1990 kc->peep2_count = prev; 1991 break; 1992 1993 default: 1994 break; 1995 } 1996 } 1997 } 1998 1999 /* Return the type of value that can be used to parameterize test KIND, 2000 or parameter::UNSET if none. */ 2001 2002 parameter::type_enum 2003 transition_parameter_type (rtx_test::kind_enum kind) 2004 { 2005 switch (kind) 2006 { 2007 case rtx_test::CODE: 2008 return parameter::CODE; 2009 2010 case rtx_test::MODE: 2011 return parameter::MODE; 2012 2013 case rtx_test::REGNO_FIELD: 2014 return parameter::UINT; 2015 2016 case rtx_test::INT_FIELD: 2017 case rtx_test::VECLEN: 2018 case rtx_test::PATTERN: 2019 return parameter::INT; 2020 2021 case rtx_test::WIDE_INT_FIELD: 2022 return parameter::WIDE_INT; 2023 2024 case rtx_test::PEEP2_COUNT: 2025 case rtx_test::VECLEN_GE: 2026 case rtx_test::SAVED_CONST_INT: 2027 case rtx_test::PREDICATE: 2028 case rtx_test::DUPLICATE: 2029 case rtx_test::HAVE_NUM_CLOBBERS: 2030 case rtx_test::C_TEST: 2031 case rtx_test::SET_OP: 2032 case rtx_test::ACCEPT: 2033 return parameter::UNSET; 2034 } 2035 gcc_unreachable (); 2036 } 2037 2038 /* Initialize the pos_operand fields of each state reachable from S. 2039 If OPERAND_POS[ID] >= 0, the position with id ID is stored in 2040 operands[OPERAND_POS[ID]] on entry to S. */ 2041 2042 static void 2043 find_operand_positions (state *s, vec <int> &operand_pos) 2044 { 2045 for (decision *d = s->first; d; d = d->next) 2046 { 2047 int this_operand = (d->test.pos ? operand_pos[d->test.pos->id] : -1); 2048 if (this_operand >= 0) 2049 d->test.pos_operand = this_operand; 2050 if (d->test.kind == rtx_test::SET_OP) 2051 operand_pos[d->test.pos->id] = d->test.u.opno; 2052 for (transition *trans = d->first; trans; trans = trans->next) 2053 find_operand_positions (trans->to, operand_pos); 2054 if (d->test.kind == rtx_test::SET_OP) 2055 operand_pos[d->test.pos->id] = this_operand; 2056 } 2057 } 2058 2059 /* Statistics about a matching routine. */ 2060 struct stats 2061 { 2062 stats (); 2063 2064 /* The total number of decisions in the routine, excluding trivial 2065 ones that never fail. */ 2066 unsigned int num_decisions; 2067 2068 /* The number of non-trivial decisions on the longest path through 2069 the routine, and the return value that contributes most to that 2070 long path. */ 2071 unsigned int longest_path; 2072 int longest_path_code; 2073 2074 /* The maximum number of times that a single call to the routine 2075 can backtrack, and the value returned at the end of that path. 2076 "Backtracking" here means failing one decision in state and 2077 going onto to the next. */ 2078 unsigned int longest_backtrack; 2079 int longest_backtrack_code; 2080 }; 2081 2082 stats::stats () 2083 : num_decisions (0), longest_path (0), longest_path_code (-1), 2084 longest_backtrack (0), longest_backtrack_code (-1) {} 2085 2086 /* Return statistics about S. */ 2087 2088 static stats 2089 get_stats (state *s) 2090 { 2091 stats for_s; 2092 unsigned int longest_path = 0; 2093 for (decision *d = s->first; d; d = d->next) 2094 { 2095 /* Work out the statistics for D. */ 2096 stats for_d; 2097 for (transition *trans = d->first; trans; trans = trans->next) 2098 { 2099 stats for_trans = get_stats (trans->to); 2100 for_d.num_decisions += for_trans.num_decisions; 2101 /* Each transition is mutually-exclusive, so just pick the 2102 longest of the individual paths. */ 2103 if (for_d.longest_path <= for_trans.longest_path) 2104 { 2105 for_d.longest_path = for_trans.longest_path; 2106 for_d.longest_path_code = for_trans.longest_path_code; 2107 } 2108 /* Likewise for backtracking. */ 2109 if (for_d.longest_backtrack <= for_trans.longest_backtrack) 2110 { 2111 for_d.longest_backtrack = for_trans.longest_backtrack; 2112 for_d.longest_backtrack_code = for_trans.longest_backtrack_code; 2113 } 2114 } 2115 2116 /* Account for D's test in its statistics. */ 2117 if (!d->test.single_outcome_p ()) 2118 { 2119 for_d.num_decisions += 1; 2120 for_d.longest_path += 1; 2121 } 2122 if (d->test.kind == rtx_test::ACCEPT) 2123 { 2124 for_d.longest_path_code = d->test.u.acceptance.u.full.code; 2125 for_d.longest_backtrack_code = d->test.u.acceptance.u.full.code; 2126 } 2127 2128 /* Keep a running count of the number of backtracks. */ 2129 if (d->prev) 2130 for_s.longest_backtrack += 1; 2131 2132 /* Accumulate D's statistics into S's. */ 2133 for_s.num_decisions += for_d.num_decisions; 2134 for_s.longest_path += for_d.longest_path; 2135 for_s.longest_backtrack += for_d.longest_backtrack; 2136 2137 /* Use the code from the decision with the longest individual path, 2138 since that's more likely to be useful if trying to make the 2139 path shorter. In the event of a tie, pick the later decision, 2140 since that's closer to the end of the path. */ 2141 if (longest_path <= for_d.longest_path) 2142 { 2143 longest_path = for_d.longest_path; 2144 for_s.longest_path_code = for_d.longest_path_code; 2145 } 2146 2147 /* Later decisions in a state are necessarily in a longer backtrack 2148 than earlier decisions. */ 2149 for_s.longest_backtrack_code = for_d.longest_backtrack_code; 2150 } 2151 return for_s; 2152 } 2153 2154 /* Optimize ROOT. Use TYPE to describe ROOT in status messages. */ 2155 2156 static void 2157 optimize_subroutine_group (const char *type, state *root) 2158 { 2159 /* Remove optional transitions that turned out not to be worthwhile. */ 2160 if (collapse_optional_decisions_p) 2161 collapse_optional_decisions (root); 2162 2163 /* Try to remove duplicated tests and to rearrange tests into a more 2164 logical order. */ 2165 if (cse_tests_p) 2166 { 2167 known_conditions kc; 2168 kc.position_tests.safe_grow_cleared (num_positions); 2169 kc.set_operands.safe_grow_cleared (num_operands); 2170 kc.peep2_count = 1; 2171 cse_tests (&root_pos, root, &kc); 2172 } 2173 2174 /* Try to simplify two or more tests into one. */ 2175 if (simplify_tests_p) 2176 simplify_tests (root); 2177 2178 /* Try to use operands[] instead of xN variables. */ 2179 if (use_operand_variables_p) 2180 { 2181 auto_vec <int> operand_pos (num_positions); 2182 for (unsigned int i = 0; i < num_positions; ++i) 2183 operand_pos.quick_push (-1); 2184 find_operand_positions (root, operand_pos); 2185 } 2186 2187 /* Print a summary of the new state. */ 2188 stats st = get_stats (root); 2189 fprintf (stderr, "Statistics for %s:\n", type); 2190 fprintf (stderr, " Number of decisions: %6d\n", st.num_decisions); 2191 fprintf (stderr, " longest path: %6d (code: %6d)\n", 2192 st.longest_path, st.longest_path_code); 2193 fprintf (stderr, " longest backtrack: %6d (code: %6d)\n", 2194 st.longest_backtrack, st.longest_backtrack_code); 2195 } 2196 2197 struct merge_pattern_info; 2198 2199 /* Represents a transition from one pattern to another. */ 2200 struct merge_pattern_transition 2201 { 2202 merge_pattern_transition (merge_pattern_info *); 2203 2204 /* The target pattern. */ 2205 merge_pattern_info *to; 2206 2207 /* The parameters that the source pattern passes to the target pattern. 2208 "parameter (TYPE, true, I)" represents parameter I of the source 2209 pattern. */ 2210 auto_vec <parameter, MAX_PATTERN_PARAMS> params; 2211 }; 2212 2213 merge_pattern_transition::merge_pattern_transition (merge_pattern_info *to_in) 2214 : to (to_in) 2215 { 2216 } 2217 2218 /* Represents a pattern that can might match several states. The pattern 2219 may replace parts of the test with a parameter value. It may also 2220 replace transition labels with parameters. */ 2221 struct merge_pattern_info 2222 { 2223 merge_pattern_info (unsigned int); 2224 2225 /* If PARAM_TEST_P, the state's singleton test should be generalized 2226 to use the runtime value of PARAMS[PARAM_TEST]. */ 2227 unsigned int param_test : 8; 2228 2229 /* If PARAM_TRANSITION_P, the state's single transition label should 2230 be replaced by the runtime value of PARAMS[PARAM_TRANSITION]. */ 2231 unsigned int param_transition : 8; 2232 2233 /* True if we have decided to generalize the root decision's test, 2234 as per PARAM_TEST. */ 2235 unsigned int param_test_p : 1; 2236 2237 /* Likewise for the root decision's transition, as per PARAM_TRANSITION. */ 2238 unsigned int param_transition_p : 1; 2239 2240 /* True if the contents of the structure are completely filled in. */ 2241 unsigned int complete_p : 1; 2242 2243 /* The number of pseudo-statements in the pattern. Used to decide 2244 whether it's big enough to break out into a subroutine. */ 2245 unsigned int num_statements; 2246 2247 /* The number of states that use this pattern. */ 2248 unsigned int num_users; 2249 2250 /* The number of distinct success values that the pattern returns. */ 2251 unsigned int num_results; 2252 2253 /* This array has one element for each runtime parameter to the pattern. 2254 PARAMS[I] gives the default value of parameter I, which is always 2255 constant. 2256 2257 These default parameters are used in cases where we match the 2258 pattern against some state S1, then add more parameters while 2259 matching against some state S2. S1 is then left passing fewer 2260 parameters than S2. The array gives us enough informatino to 2261 construct a full parameter list for S1 (see update_parameters). 2262 2263 If we decide to create a subroutine for this pattern, 2264 PARAMS[I].type determines the C type of parameter I. */ 2265 auto_vec <parameter, MAX_PATTERN_PARAMS> params; 2266 2267 /* All states that match this pattern must have the same number of 2268 transitions. TRANSITIONS[I] describes the subpattern for transition 2269 number I; it is null if transition I represents a successful return 2270 from the pattern. */ 2271 auto_vec <merge_pattern_transition *, 1> transitions; 2272 2273 /* The routine associated with the pattern, or null if we haven't generated 2274 one yet. */ 2275 pattern_routine *routine; 2276 }; 2277 2278 merge_pattern_info::merge_pattern_info (unsigned int num_transitions) 2279 : param_test (0), 2280 param_transition (0), 2281 param_test_p (false), 2282 param_transition_p (false), 2283 complete_p (false), 2284 num_statements (0), 2285 num_users (0), 2286 num_results (0), 2287 routine (0) 2288 { 2289 transitions.safe_grow_cleared (num_transitions); 2290 } 2291 2292 /* Describes one way of matching a particular state to a particular 2293 pattern. */ 2294 struct merge_state_result 2295 { 2296 merge_state_result (merge_pattern_info *, position *, merge_state_result *); 2297 2298 /* A pattern that matches the state. */ 2299 merge_pattern_info *pattern; 2300 2301 /* If we decide to use this match and create a subroutine for PATTERN, 2302 the state should pass the rtx at position ROOT to the pattern's 2303 rtx parameter. A null root means that the pattern doesn't need 2304 an rtx parameter; all the rtxes it matches come from elsewhere. */ 2305 position *root; 2306 2307 /* The parameters that should be passed to PATTERN for this state. 2308 If the array is shorter than PATTERN->params, the missing entries 2309 should be taken from the corresponding element of PATTERN->params. */ 2310 auto_vec <parameter, MAX_PATTERN_PARAMS> params; 2311 2312 /* An earlier match for the same state, or null if none. Patterns 2313 matched by earlier entries are smaller than PATTERN. */ 2314 merge_state_result *prev; 2315 }; 2316 2317 merge_state_result::merge_state_result (merge_pattern_info *pattern_in, 2318 position *root_in, 2319 merge_state_result *prev_in) 2320 : pattern (pattern_in), root (root_in), prev (prev_in) 2321 {} 2322 2323 /* Information about a state, used while trying to match it against 2324 a pattern. */ 2325 struct merge_state_info 2326 { 2327 merge_state_info (state *); 2328 2329 /* The state itself. */ 2330 state *s; 2331 2332 /* Index I gives information about the target of transition I. */ 2333 merge_state_info *to_states; 2334 2335 /* The number of transitions in S. */ 2336 unsigned int num_transitions; 2337 2338 /* True if the state has been deleted in favor of a call to a 2339 pattern routine. */ 2340 bool merged_p; 2341 2342 /* The previous state that might be a merge candidate for S, or null 2343 if no previous states could be merged with S. */ 2344 merge_state_info *prev_same_test; 2345 2346 /* A list of pattern matches for this state. */ 2347 merge_state_result *res; 2348 }; 2349 2350 merge_state_info::merge_state_info (state *s_in) 2351 : s (s_in), 2352 to_states (0), 2353 num_transitions (0), 2354 merged_p (false), 2355 prev_same_test (0), 2356 res (0) {} 2357 2358 /* True if PAT would be useful as a subroutine. */ 2359 2360 static bool 2361 useful_pattern_p (merge_pattern_info *pat) 2362 { 2363 return pat->num_statements >= MIN_COMBINE_COST; 2364 } 2365 2366 /* PAT2 is a subpattern of PAT1. Return true if PAT2 should be inlined 2367 into PAT1's C routine. */ 2368 2369 static bool 2370 same_pattern_p (merge_pattern_info *pat1, merge_pattern_info *pat2) 2371 { 2372 return pat1->num_users == pat2->num_users || !useful_pattern_p (pat2); 2373 } 2374 2375 /* PAT was previously matched against SINFO based on tentative matches 2376 for the target states of SINFO's state. Return true if the match 2377 still holds; that is, if the target states of SINFO's state still 2378 match the corresponding transitions of PAT. */ 2379 2380 static bool 2381 valid_result_p (merge_pattern_info *pat, merge_state_info *sinfo) 2382 { 2383 for (unsigned int j = 0; j < sinfo->num_transitions; ++j) 2384 if (merge_pattern_transition *ptrans = pat->transitions[j]) 2385 { 2386 merge_state_result *to_res = sinfo->to_states[j].res; 2387 if (!to_res || to_res->pattern != ptrans->to) 2388 return false; 2389 } 2390 return true; 2391 } 2392 2393 /* Remove any matches that are no longer valid from the head of SINFO's 2394 list of matches. */ 2395 2396 static void 2397 prune_invalid_results (merge_state_info *sinfo) 2398 { 2399 while (sinfo->res && !valid_result_p (sinfo->res->pattern, sinfo)) 2400 { 2401 sinfo->res = sinfo->res->prev; 2402 gcc_assert (sinfo->res); 2403 } 2404 } 2405 2406 /* Return true if PAT represents the biggest posssible match for SINFO; 2407 that is, if the next action of SINFO's state on return from PAT will 2408 be something that cannot be merged with any other state. */ 2409 2410 static bool 2411 complete_result_p (merge_pattern_info *pat, merge_state_info *sinfo) 2412 { 2413 for (unsigned int j = 0; j < sinfo->num_transitions; ++j) 2414 if (sinfo->to_states[j].res && !pat->transitions[j]) 2415 return false; 2416 return true; 2417 } 2418 2419 /* Update TO for any parameters that have been added to FROM since TO 2420 was last set. The extra parameters in FROM will be constants or 2421 instructions to duplicate earlier parameters. */ 2422 2423 static void 2424 update_parameters (vec <parameter> &to, const vec <parameter> &from) 2425 { 2426 for (unsigned int i = to.length (); i < from.length (); ++i) 2427 to.quick_push (from[i]); 2428 } 2429 2430 /* Return true if A and B can be tested by a single test. If the test 2431 can be parameterised, store the parameter value for A in *PARAMA and 2432 the parameter value for B in *PARAMB, otherwise leave PARAMA and 2433 PARAMB alone. */ 2434 2435 static bool 2436 compatible_tests_p (const rtx_test &a, const rtx_test &b, 2437 parameter *parama, parameter *paramb) 2438 { 2439 if (a.kind != b.kind) 2440 return false; 2441 switch (a.kind) 2442 { 2443 case rtx_test::PREDICATE: 2444 if (a.u.predicate.data != b.u.predicate.data) 2445 return false; 2446 *parama = parameter (parameter::MODE, false, a.u.predicate.mode); 2447 *paramb = parameter (parameter::MODE, false, b.u.predicate.mode); 2448 return true; 2449 2450 case rtx_test::SAVED_CONST_INT: 2451 *parama = parameter (parameter::INT, false, a.u.integer.value); 2452 *paramb = parameter (parameter::INT, false, b.u.integer.value); 2453 return true; 2454 2455 default: 2456 return a == b; 2457 } 2458 } 2459 2460 /* PARAMS is an array of the parameters that a state is going to pass 2461 to a pattern routine. It is still incomplete; index I has a kind of 2462 parameter::UNSET if we don't yet know what the state will pass 2463 as parameter I. Try to make parameter ID equal VALUE, returning 2464 true on success. */ 2465 2466 static bool 2467 set_parameter (vec <parameter> ¶ms, unsigned int id, 2468 const parameter &value) 2469 { 2470 if (params[id].type == parameter::UNSET) 2471 { 2472 if (force_unique_params_p) 2473 for (unsigned int i = 0; i < params.length (); ++i) 2474 if (params[i] == value) 2475 return false; 2476 params[id] = value; 2477 return true; 2478 } 2479 return params[id] == value; 2480 } 2481 2482 /* PARAMS2 is the "params" array for a pattern and PARAMS1 is the 2483 set of parameters that a particular state is going to pass to 2484 that pattern. 2485 2486 Try to extend PARAMS1 and PARAMS2 so that there is a parameter 2487 that is equal to PARAM1 for the state and has a default value of 2488 PARAM2. Parameters beginning at START were added as part of the 2489 same match and so may be reused. */ 2490 2491 static bool 2492 add_parameter (vec <parameter> ¶ms1, vec <parameter> ¶ms2, 2493 const parameter ¶m1, const parameter ¶m2, 2494 unsigned int start, unsigned int *res) 2495 { 2496 gcc_assert (params1.length () == params2.length ()); 2497 gcc_assert (!param1.is_param && !param2.is_param); 2498 2499 for (unsigned int i = start; i < params2.length (); ++i) 2500 if (params1[i] == param1 && params2[i] == param2) 2501 { 2502 *res = i; 2503 return true; 2504 } 2505 2506 if (force_unique_params_p) 2507 for (unsigned int i = 0; i < params2.length (); ++i) 2508 if (params1[i] == param1 || params2[i] == param2) 2509 return false; 2510 2511 if (params2.length () >= MAX_PATTERN_PARAMS) 2512 return false; 2513 2514 *res = params2.length (); 2515 params1.quick_push (param1); 2516 params2.quick_push (param2); 2517 return true; 2518 } 2519 2520 /* If *ROOTA is nonnull, return true if the same sequence of steps are 2521 required to reach A from *ROOTA as to reach B from ROOTB. If *ROOTA 2522 is null, update it if necessary in order to make the condition hold. */ 2523 2524 static bool 2525 merge_relative_positions (position **roota, position *a, 2526 position *rootb, position *b) 2527 { 2528 if (!relative_patterns_p) 2529 { 2530 if (a != b) 2531 return false; 2532 if (!*roota) 2533 { 2534 *roota = rootb; 2535 return true; 2536 } 2537 return *roota == rootb; 2538 } 2539 /* If B does not belong to the same instruction as ROOTB, we don't 2540 start with ROOTB but instead start with a call to peep2_next_insn. 2541 In that case the sequences for B and A are identical iff B and A 2542 are themselves identical. */ 2543 if (rootb->insn_id != b->insn_id) 2544 return a == b; 2545 while (rootb != b) 2546 { 2547 if (!a || b->type != a->type || b->arg != a->arg) 2548 return false; 2549 b = b->base; 2550 a = a->base; 2551 } 2552 if (!*roota) 2553 *roota = a; 2554 return *roota == a; 2555 } 2556 2557 /* A hasher of states that treats two states as "equal" if they might be 2558 merged (but trying to be more discriminating than "return true"). */ 2559 struct test_pattern_hasher : nofree_ptr_hash <merge_state_info> 2560 { 2561 static inline hashval_t hash (const value_type &); 2562 static inline bool equal (const value_type &, const compare_type &); 2563 }; 2564 2565 hashval_t 2566 test_pattern_hasher::hash (merge_state_info *const &sinfo) 2567 { 2568 inchash::hash h; 2569 decision *d = sinfo->s->singleton (); 2570 h.add_int (d->test.pos_operand + 1); 2571 if (!relative_patterns_p) 2572 h.add_int (d->test.pos ? d->test.pos->id + 1 : 0); 2573 h.add_int (d->test.kind); 2574 h.add_int (sinfo->num_transitions); 2575 return h.end (); 2576 } 2577 2578 bool 2579 test_pattern_hasher::equal (merge_state_info *const &sinfo1, 2580 merge_state_info *const &sinfo2) 2581 { 2582 decision *d1 = sinfo1->s->singleton (); 2583 decision *d2 = sinfo2->s->singleton (); 2584 gcc_assert (d1 && d2); 2585 2586 parameter new_param1, new_param2; 2587 return (d1->test.pos_operand == d2->test.pos_operand 2588 && (relative_patterns_p || d1->test.pos == d2->test.pos) 2589 && compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2) 2590 && sinfo1->num_transitions == sinfo2->num_transitions); 2591 } 2592 2593 /* Try to make the state described by SINFO1 use the same pattern as the 2594 state described by SINFO2. Return true on success. 2595 2596 SINFO1 and SINFO2 are known to have the same hash value. */ 2597 2598 static bool 2599 merge_patterns (merge_state_info *sinfo1, merge_state_info *sinfo2) 2600 { 2601 merge_state_result *res2 = sinfo2->res; 2602 merge_pattern_info *pat = res2->pattern; 2603 2604 /* Write to temporary arrays while matching, in case we have to abort 2605 half way through. */ 2606 auto_vec <parameter, MAX_PATTERN_PARAMS> params1; 2607 auto_vec <parameter, MAX_PATTERN_PARAMS> params2; 2608 params1.quick_grow_cleared (pat->params.length ()); 2609 params2.splice (pat->params); 2610 unsigned int start_param = params2.length (); 2611 2612 /* An array for recording changes to PAT->transitions[?].params. 2613 All changes involve replacing a constant parameter with some 2614 PAT->params[N], where N is the second element of the pending_param. */ 2615 typedef std::pair <parameter *, unsigned int> pending_param; 2616 auto_vec <pending_param, 32> pending_params; 2617 2618 decision *d1 = sinfo1->s->singleton (); 2619 decision *d2 = sinfo2->s->singleton (); 2620 gcc_assert (d1 && d2); 2621 2622 /* If D2 tests a position, SINFO1's root relative to D1 is the same 2623 as SINFO2's root relative to D2. */ 2624 position *root1 = 0; 2625 position *root2 = res2->root; 2626 if (d2->test.pos_operand < 0 2627 && d1->test.pos 2628 && !merge_relative_positions (&root1, d1->test.pos, 2629 root2, d2->test.pos)) 2630 return false; 2631 2632 /* Check whether the patterns have the same shape. */ 2633 unsigned int num_transitions = sinfo1->num_transitions; 2634 gcc_assert (num_transitions == sinfo2->num_transitions); 2635 for (unsigned int i = 0; i < num_transitions; ++i) 2636 if (merge_pattern_transition *ptrans = pat->transitions[i]) 2637 { 2638 merge_state_result *to1_res = sinfo1->to_states[i].res; 2639 merge_state_result *to2_res = sinfo2->to_states[i].res; 2640 merge_pattern_info *to_pat = ptrans->to; 2641 gcc_assert (to2_res && to2_res->pattern == to_pat); 2642 if (!to1_res || to1_res->pattern != to_pat) 2643 return false; 2644 if (to2_res->root 2645 && !merge_relative_positions (&root1, to1_res->root, 2646 root2, to2_res->root)) 2647 return false; 2648 /* Match the parameters that TO1_RES passes to TO_PAT with the 2649 parameters that PAT passes to TO_PAT. */ 2650 update_parameters (to1_res->params, to_pat->params); 2651 for (unsigned int j = 0; j < to1_res->params.length (); ++j) 2652 { 2653 const parameter ¶m1 = to1_res->params[j]; 2654 const parameter ¶m2 = ptrans->params[j]; 2655 gcc_assert (!param1.is_param); 2656 if (param2.is_param) 2657 { 2658 if (!set_parameter (params1, param2.value, param1)) 2659 return false; 2660 } 2661 else if (param1 != param2) 2662 { 2663 unsigned int id; 2664 if (!add_parameter (params1, params2, 2665 param1, param2, start_param, &id)) 2666 return false; 2667 /* Record that PAT should now pass parameter ID to TO_PAT, 2668 instead of the current contents of *PARAM2. We only 2669 make the change if the rest of the match succeeds. */ 2670 pending_params.safe_push 2671 (pending_param (&ptrans->params[j], id)); 2672 } 2673 } 2674 } 2675 2676 unsigned int param_test = pat->param_test; 2677 unsigned int param_transition = pat->param_transition; 2678 bool param_test_p = pat->param_test_p; 2679 bool param_transition_p = pat->param_transition_p; 2680 2681 /* If the tests don't match exactly, try to parameterize them. */ 2682 parameter new_param1, new_param2; 2683 if (!compatible_tests_p (d1->test, d2->test, &new_param1, &new_param2)) 2684 gcc_unreachable (); 2685 if (new_param1.type != parameter::UNSET) 2686 { 2687 /* If the test has not already been parameterized, all existing 2688 matches use constant NEW_PARAM2. */ 2689 if (param_test_p) 2690 { 2691 if (!set_parameter (params1, param_test, new_param1)) 2692 return false; 2693 } 2694 else if (new_param1 != new_param2) 2695 { 2696 if (!add_parameter (params1, params2, new_param1, new_param2, 2697 start_param, ¶m_test)) 2698 return false; 2699 param_test_p = true; 2700 } 2701 } 2702 2703 /* Match the transitions. */ 2704 transition *trans1 = d1->first; 2705 transition *trans2 = d2->first; 2706 for (unsigned int i = 0; i < num_transitions; ++i) 2707 { 2708 if (param_transition_p || trans1->labels != trans2->labels) 2709 { 2710 /* We can only generalize a single transition with a single 2711 label. */ 2712 if (num_transitions != 1 2713 || trans1->labels.length () != 1 2714 || trans2->labels.length () != 1) 2715 return false; 2716 2717 /* Although we can match wide-int fields, in practice it leads 2718 to some odd results for const_vectors. We end up 2719 parameterizing the first N const_ints of the vector 2720 and then (once we reach the maximum number of parameters) 2721 we go on to match the other elements exactly. */ 2722 if (d1->test.kind == rtx_test::WIDE_INT_FIELD) 2723 return false; 2724 2725 /* See whether the label has a generalizable type. */ 2726 parameter::type_enum param_type 2727 = transition_parameter_type (d1->test.kind); 2728 if (param_type == parameter::UNSET) 2729 return false; 2730 2731 /* Match the labels using parameters. */ 2732 new_param1 = parameter (param_type, false, trans1->labels[0]); 2733 if (param_transition_p) 2734 { 2735 if (!set_parameter (params1, param_transition, new_param1)) 2736 return false; 2737 } 2738 else 2739 { 2740 new_param2 = parameter (param_type, false, trans2->labels[0]); 2741 if (!add_parameter (params1, params2, new_param1, new_param2, 2742 start_param, ¶m_transition)) 2743 return false; 2744 param_transition_p = true; 2745 } 2746 } 2747 trans1 = trans1->next; 2748 trans2 = trans2->next; 2749 } 2750 2751 /* Set any unset parameters to their default values. This occurs if some 2752 other state needed something to be parameterized in order to match SINFO2, 2753 but SINFO1 on its own does not. */ 2754 for (unsigned int i = 0; i < params1.length (); ++i) 2755 if (params1[i].type == parameter::UNSET) 2756 params1[i] = params2[i]; 2757 2758 /* The match was successful. Commit all pending changes to PAT. */ 2759 update_parameters (pat->params, params2); 2760 { 2761 pending_param *pp; 2762 unsigned int i; 2763 FOR_EACH_VEC_ELT (pending_params, i, pp) 2764 *pp->first = parameter (pp->first->type, true, pp->second); 2765 } 2766 pat->param_test = param_test; 2767 pat->param_transition = param_transition; 2768 pat->param_test_p = param_test_p; 2769 pat->param_transition_p = param_transition_p; 2770 2771 /* Record the match of SINFO1. */ 2772 merge_state_result *new_res1 = new merge_state_result (pat, root1, 2773 sinfo1->res); 2774 new_res1->params.splice (params1); 2775 sinfo1->res = new_res1; 2776 return true; 2777 } 2778 2779 /* The number of states that were removed by calling pattern routines. */ 2780 static unsigned int pattern_use_states; 2781 2782 /* The number of states used while defining pattern routines. */ 2783 static unsigned int pattern_def_states; 2784 2785 /* Information used while constructing a use or definition of a pattern 2786 routine. */ 2787 struct create_pattern_info 2788 { 2789 /* The routine itself. */ 2790 pattern_routine *routine; 2791 2792 /* The first unclaimed return value for this particular use or definition. 2793 We walk the substates of uses and definitions in the same order 2794 so each return value always refers to the same position within 2795 the pattern. */ 2796 unsigned int next_result; 2797 }; 2798 2799 static void populate_pattern_routine (create_pattern_info *, 2800 merge_state_info *, state *, 2801 const vec <parameter> &); 2802 2803 /* SINFO matches a pattern for which we've decided to create a C routine. 2804 Return a decision that performs a call to the pattern routine, 2805 but leave the caller to add the transitions to it. Initialize CPI 2806 for this purpose. Also create a definition for the pattern routine, 2807 if it doesn't already have one. 2808 2809 PARAMS are the parameters that SINFO passes to its pattern. */ 2810 2811 static decision * 2812 init_pattern_use (create_pattern_info *cpi, merge_state_info *sinfo, 2813 const vec <parameter> ¶ms) 2814 { 2815 state *s = sinfo->s; 2816 merge_state_result *res = sinfo->res; 2817 merge_pattern_info *pat = res->pattern; 2818 cpi->routine = pat->routine; 2819 if (!cpi->routine) 2820 { 2821 /* We haven't defined the pattern routine yet, so create 2822 a definition now. */ 2823 pattern_routine *routine = new pattern_routine; 2824 pat->routine = routine; 2825 cpi->routine = routine; 2826 routine->s = new state; 2827 routine->insn_p = false; 2828 routine->pnum_clobbers_p = false; 2829 2830 /* Create an "idempotent" mapping of parameter I to parameter I. 2831 Also record the C type of each parameter to the routine. */ 2832 auto_vec <parameter, MAX_PATTERN_PARAMS> def_params; 2833 for (unsigned int i = 0; i < pat->params.length (); ++i) 2834 { 2835 def_params.quick_push (parameter (pat->params[i].type, true, i)); 2836 routine->param_types.quick_push (pat->params[i].type); 2837 } 2838 2839 /* Any of the states that match the pattern could be used to 2840 create the routine definition. We might as well use SINFO 2841 since it's already to hand. This means that all positions 2842 in the definition will be relative to RES->root. */ 2843 routine->pos = res->root; 2844 cpi->next_result = 0; 2845 populate_pattern_routine (cpi, sinfo, routine->s, def_params); 2846 gcc_assert (cpi->next_result == pat->num_results); 2847 2848 /* Add the routine to the global list, after the subroutines 2849 that it calls. */ 2850 routine->pattern_id = patterns.length (); 2851 patterns.safe_push (routine); 2852 } 2853 2854 /* Create a decision to call the routine, passing PARAMS to it. */ 2855 pattern_use *use = new pattern_use; 2856 use->routine = pat->routine; 2857 use->params.splice (params); 2858 decision *d = new decision (rtx_test::pattern (res->root, use)); 2859 2860 /* If the original decision could use an element of operands[] instead 2861 of an rtx variable, try to transfer it to the new decision. */ 2862 if (s->first->test.pos && res->root == s->first->test.pos) 2863 d->test.pos_operand = s->first->test.pos_operand; 2864 2865 cpi->next_result = 0; 2866 return d; 2867 } 2868 2869 /* Make S return the next unclaimed pattern routine result for CPI. */ 2870 2871 static void 2872 add_pattern_acceptance (create_pattern_info *cpi, state *s) 2873 { 2874 acceptance_type acceptance; 2875 acceptance.type = SUBPATTERN; 2876 acceptance.partial_p = false; 2877 acceptance.u.full.code = cpi->next_result; 2878 add_decision (s, rtx_test::accept (acceptance), true, false); 2879 cpi->next_result += 1; 2880 } 2881 2882 /* Initialize new empty state NEWS so that it implements SINFO's pattern 2883 (here referred to as "P"). P may be the top level of a pattern routine 2884 or a subpattern that should be inlined into its parent pattern's routine 2885 (as per same_pattern_p). The choice of SINFO for a top-level pattern is 2886 arbitrary; it could be any of the states that use P. The choice for 2887 subpatterns follows the choice for the parent pattern. 2888 2889 PARAMS gives the value of each parameter to P in terms of the parameters 2890 to the top-level pattern. If P itself is the top level pattern, PARAMS[I] 2891 is always "parameter (TYPE, true, I)". */ 2892 2893 static void 2894 populate_pattern_routine (create_pattern_info *cpi, merge_state_info *sinfo, 2895 state *news, const vec <parameter> ¶ms) 2896 { 2897 pattern_def_states += 1; 2898 2899 decision *d = sinfo->s->singleton (); 2900 merge_pattern_info *pat = sinfo->res->pattern; 2901 pattern_routine *routine = cpi->routine; 2902 2903 /* Create a copy of D's test for the pattern routine and generalize it 2904 as appropriate. */ 2905 decision *newd = new decision (d->test); 2906 gcc_assert (newd->test.pos_operand >= 0 2907 || !newd->test.pos 2908 || common_position (newd->test.pos, 2909 routine->pos) == routine->pos); 2910 if (pat->param_test_p) 2911 { 2912 const parameter ¶m = params[pat->param_test]; 2913 switch (newd->test.kind) 2914 { 2915 case rtx_test::PREDICATE: 2916 newd->test.u.predicate.mode_is_param = param.is_param; 2917 newd->test.u.predicate.mode = param.value; 2918 break; 2919 2920 case rtx_test::SAVED_CONST_INT: 2921 newd->test.u.integer.is_param = param.is_param; 2922 newd->test.u.integer.value = param.value; 2923 break; 2924 2925 default: 2926 gcc_unreachable (); 2927 break; 2928 } 2929 } 2930 if (d->test.kind == rtx_test::C_TEST) 2931 routine->insn_p = true; 2932 else if (d->test.kind == rtx_test::HAVE_NUM_CLOBBERS) 2933 routine->pnum_clobbers_p = true; 2934 news->push_back (newd); 2935 2936 /* Fill in the transitions of NEWD. */ 2937 unsigned int i = 0; 2938 for (transition *trans = d->first; trans; trans = trans->next) 2939 { 2940 /* Create a new state to act as the target of the new transition. */ 2941 state *to_news = new state; 2942 if (merge_pattern_transition *ptrans = pat->transitions[i]) 2943 { 2944 /* The pattern hasn't finished matching yet. Get the target 2945 pattern and the corresponding target state of SINFO. */ 2946 merge_pattern_info *to_pat = ptrans->to; 2947 merge_state_info *to = sinfo->to_states + i; 2948 gcc_assert (to->res->pattern == to_pat); 2949 gcc_assert (ptrans->params.length () == to_pat->params.length ()); 2950 2951 /* Express the parameters to TO_PAT in terms of the parameters 2952 to the top-level pattern. */ 2953 auto_vec <parameter, MAX_PATTERN_PARAMS> to_params; 2954 for (unsigned int j = 0; j < ptrans->params.length (); ++j) 2955 { 2956 const parameter ¶m = ptrans->params[j]; 2957 to_params.quick_push (param.is_param 2958 ? params[param.value] 2959 : param); 2960 } 2961 2962 if (same_pattern_p (pat, to_pat)) 2963 /* TO_PAT is part of the current routine, so just recurse. */ 2964 populate_pattern_routine (cpi, to, to_news, to_params); 2965 else 2966 { 2967 /* TO_PAT should be matched by calling a separate routine. */ 2968 create_pattern_info sub_cpi; 2969 decision *subd = init_pattern_use (&sub_cpi, to, to_params); 2970 routine->insn_p |= sub_cpi.routine->insn_p; 2971 routine->pnum_clobbers_p |= sub_cpi.routine->pnum_clobbers_p; 2972 2973 /* Add the pattern routine call to the new target state. */ 2974 to_news->push_back (subd); 2975 2976 /* Add a transition for each successful call result. */ 2977 for (unsigned int j = 0; j < to_pat->num_results; ++j) 2978 { 2979 state *res = new state; 2980 add_pattern_acceptance (cpi, res); 2981 subd->push_back (new transition (j, res, false)); 2982 } 2983 } 2984 } 2985 else 2986 /* This transition corresponds to a successful match. */ 2987 add_pattern_acceptance (cpi, to_news); 2988 2989 /* Create the transition itself, generalizing as necessary. */ 2990 transition *new_trans = new transition (trans->labels, to_news, 2991 trans->optional); 2992 if (pat->param_transition_p) 2993 { 2994 const parameter ¶m = params[pat->param_transition]; 2995 new_trans->is_param = param.is_param; 2996 new_trans->labels[0] = param.value; 2997 } 2998 newd->push_back (new_trans); 2999 i += 1; 3000 } 3001 } 3002 3003 /* USE is a decision that calls a pattern routine and SINFO is part of the 3004 original state tree that the call is supposed to replace. Add the 3005 transitions for SINFO and its substates to USE. */ 3006 3007 static void 3008 populate_pattern_use (create_pattern_info *cpi, decision *use, 3009 merge_state_info *sinfo) 3010 { 3011 pattern_use_states += 1; 3012 gcc_assert (!sinfo->merged_p); 3013 sinfo->merged_p = true; 3014 merge_state_result *res = sinfo->res; 3015 merge_pattern_info *pat = res->pattern; 3016 decision *d = sinfo->s->singleton (); 3017 unsigned int i = 0; 3018 for (transition *trans = d->first; trans; trans = trans->next) 3019 { 3020 if (pat->transitions[i]) 3021 /* The target state is also part of the pattern. */ 3022 populate_pattern_use (cpi, use, sinfo->to_states + i); 3023 else 3024 { 3025 /* The transition corresponds to a successful return from the 3026 pattern routine. */ 3027 use->push_back (new transition (cpi->next_result, trans->to, false)); 3028 cpi->next_result += 1; 3029 } 3030 i += 1; 3031 } 3032 } 3033 3034 /* We have decided to replace SINFO's state with a call to a pattern 3035 routine. Make the change, creating a definition of the pattern routine 3036 if it doesn't have one already. */ 3037 3038 static void 3039 use_pattern (merge_state_info *sinfo) 3040 { 3041 merge_state_result *res = sinfo->res; 3042 merge_pattern_info *pat = res->pattern; 3043 state *s = sinfo->s; 3044 3045 /* The pattern may have acquired new parameters after it was matched 3046 against SINFO. Update the parameters that SINFO passes accordingly. */ 3047 update_parameters (res->params, pat->params); 3048 3049 create_pattern_info cpi; 3050 decision *d = init_pattern_use (&cpi, sinfo, res->params); 3051 populate_pattern_use (&cpi, d, sinfo); 3052 s->release (); 3053 s->push_back (d); 3054 } 3055 3056 /* Look through the state trees in STATES for common patterns and 3057 split them into subroutines. */ 3058 3059 static void 3060 split_out_patterns (vec <merge_state_info> &states) 3061 { 3062 unsigned int first_transition = states.length (); 3063 hash_table <test_pattern_hasher> hashtab (128); 3064 /* Stage 1: Create an order in which parent states come before their child 3065 states and in which sibling states are at consecutive locations. 3066 Having consecutive sibling states allows merge_state_info to have 3067 a single to_states pointer. */ 3068 for (unsigned int i = 0; i < states.length (); ++i) 3069 for (decision *d = states[i].s->first; d; d = d->next) 3070 for (transition *trans = d->first; trans; trans = trans->next) 3071 { 3072 states.safe_push (trans->to); 3073 states[i].num_transitions += 1; 3074 } 3075 /* Stage 2: Now that the addresses are stable, set up the to_states 3076 pointers. Look for states that might be merged and enter them 3077 into the hash table. */ 3078 for (unsigned int i = 0; i < states.length (); ++i) 3079 { 3080 merge_state_info *sinfo = &states[i]; 3081 if (sinfo->num_transitions) 3082 { 3083 sinfo->to_states = &states[first_transition]; 3084 first_transition += sinfo->num_transitions; 3085 } 3086 /* For simplicity, we only try to merge states that have a single 3087 decision. This is in any case the best we can do for peephole2, 3088 since whether a peephole2 ACCEPT succeeds or not depends on the 3089 specific peephole2 pattern (which is unique to each ACCEPT 3090 and so couldn't be shared between states). */ 3091 if (decision *d = sinfo->s->singleton ()) 3092 /* ACCEPT states are unique, so don't even try to merge them. */ 3093 if (d->test.kind != rtx_test::ACCEPT 3094 && (pattern_have_num_clobbers_p 3095 || d->test.kind != rtx_test::HAVE_NUM_CLOBBERS) 3096 && (pattern_c_test_p 3097 || d->test.kind != rtx_test::C_TEST)) 3098 { 3099 merge_state_info **slot = hashtab.find_slot (sinfo, INSERT); 3100 sinfo->prev_same_test = *slot; 3101 *slot = sinfo; 3102 } 3103 } 3104 /* Stage 3: Walk backwards through the list of states and try to merge 3105 them. This is a greedy, bottom-up match; parent nodes can only start 3106 a new leaf pattern if they fail to match when combined with all child 3107 nodes that have matching patterns. 3108 3109 For each state we keep a list of potential matches, with each 3110 potential match being larger (and deeper) than the next match in 3111 the list. The final element in the list is a leaf pattern that 3112 matches just a single state. 3113 3114 Each candidate pattern created in this loop is unique -- it won't 3115 have been seen by an earlier iteration. We try to match each pattern 3116 with every state that appears earlier in STATES. 3117 3118 Because the patterns created in the loop are unique, any state 3119 that already has a match must have a final potential match that 3120 is different from any new leaf pattern. Therefore, when matching 3121 leaf patterns, we need only consider states whose list of matches 3122 is empty. 3123 3124 The non-leaf patterns that we try are as deep as possible 3125 and are an extension of the state's previous best candidate match (PB). 3126 We need only consider states whose current potential match is also PB; 3127 any states that don't match as much as PB cannnot match the new pattern, 3128 while any states that already match more than PB must be different from 3129 the new pattern. */ 3130 for (unsigned int i2 = states.length (); i2-- > 0; ) 3131 { 3132 merge_state_info *sinfo2 = &states[i2]; 3133 3134 /* Enforce the bottom-upness of the match: remove matches with later 3135 states if SINFO2's child states ended up finding a better match. */ 3136 prune_invalid_results (sinfo2); 3137 3138 /* Do nothing if the state doesn't match a later one and if there are 3139 no earlier states it could match. */ 3140 if (!sinfo2->res && !sinfo2->prev_same_test) 3141 continue; 3142 3143 merge_state_result *res2 = sinfo2->res; 3144 decision *d2 = sinfo2->s->singleton (); 3145 position *root2 = (d2->test.pos_operand < 0 ? d2->test.pos : 0); 3146 unsigned int num_transitions = sinfo2->num_transitions; 3147 3148 /* If RES2 is null then SINFO2's test in isolation has not been seen 3149 before. First try matching that on its own. */ 3150 if (!res2) 3151 { 3152 merge_pattern_info *new_pat 3153 = new merge_pattern_info (num_transitions); 3154 merge_state_result *new_res2 3155 = new merge_state_result (new_pat, root2, res2); 3156 sinfo2->res = new_res2; 3157 3158 new_pat->num_statements = !d2->test.single_outcome_p (); 3159 new_pat->num_results = num_transitions; 3160 bool matched_p = false; 3161 /* Look for states that don't currently match anything but 3162 can be made to match SINFO2 on its own. */ 3163 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1; 3164 sinfo1 = sinfo1->prev_same_test) 3165 if (!sinfo1->res && merge_patterns (sinfo1, sinfo2)) 3166 matched_p = true; 3167 if (!matched_p) 3168 { 3169 /* No other states match. */ 3170 sinfo2->res = res2; 3171 delete new_pat; 3172 delete new_res2; 3173 continue; 3174 } 3175 else 3176 res2 = new_res2; 3177 } 3178 3179 /* Keep the existing pattern if it's as good as anything we'd 3180 create for SINFO2. */ 3181 if (complete_result_p (res2->pattern, sinfo2)) 3182 { 3183 res2->pattern->num_users += 1; 3184 continue; 3185 } 3186 3187 /* Create a new pattern for SINFO2. */ 3188 merge_pattern_info *new_pat = new merge_pattern_info (num_transitions); 3189 merge_state_result *new_res2 3190 = new merge_state_result (new_pat, root2, res2); 3191 sinfo2->res = new_res2; 3192 3193 /* Fill in details about the pattern. */ 3194 new_pat->num_statements = !d2->test.single_outcome_p (); 3195 new_pat->num_results = 0; 3196 for (unsigned int j = 0; j < num_transitions; ++j) 3197 if (merge_state_result *to_res = sinfo2->to_states[j].res) 3198 { 3199 /* Count the target state as part of this pattern. 3200 First update the root position so that it can reach 3201 the target state's root. */ 3202 if (to_res->root) 3203 { 3204 if (new_res2->root) 3205 new_res2->root = common_position (new_res2->root, 3206 to_res->root); 3207 else 3208 new_res2->root = to_res->root; 3209 } 3210 merge_pattern_info *to_pat = to_res->pattern; 3211 merge_pattern_transition *ptrans 3212 = new merge_pattern_transition (to_pat); 3213 3214 /* TO_PAT may have acquired more parameters when matching 3215 states earlier in STATES than TO_RES's, but the list is 3216 now final. Make sure that TO_RES is up to date. */ 3217 update_parameters (to_res->params, to_pat->params); 3218 3219 /* Start out by assuming that every user of NEW_PAT will 3220 want to pass the same (constant) parameters as TO_RES. */ 3221 update_parameters (ptrans->params, to_res->params); 3222 3223 new_pat->transitions[j] = ptrans; 3224 new_pat->num_statements += to_pat->num_statements; 3225 new_pat->num_results += to_pat->num_results; 3226 } 3227 else 3228 /* The target state doesn't match anything and so is not part 3229 of the pattern. */ 3230 new_pat->num_results += 1; 3231 3232 /* See if any earlier states that match RES2's pattern also match 3233 NEW_PAT. */ 3234 bool matched_p = false; 3235 for (merge_state_info *sinfo1 = sinfo2->prev_same_test; sinfo1; 3236 sinfo1 = sinfo1->prev_same_test) 3237 { 3238 prune_invalid_results (sinfo1); 3239 if (sinfo1->res 3240 && sinfo1->res->pattern == res2->pattern 3241 && merge_patterns (sinfo1, sinfo2)) 3242 matched_p = true; 3243 } 3244 if (!matched_p) 3245 { 3246 /* Nothing else matches NEW_PAT, so go back to the previous 3247 pattern (possibly just a single-state one). */ 3248 sinfo2->res = res2; 3249 delete new_pat; 3250 delete new_res2; 3251 } 3252 /* Assume that SINFO2 will use RES. At this point we don't know 3253 whether earlier states that match the same pattern will use 3254 that match or a different one. */ 3255 sinfo2->res->pattern->num_users += 1; 3256 } 3257 /* Step 4: Finalize the choice of pattern for each state, ignoring 3258 patterns that were only used once. Update each pattern's size 3259 so that it doesn't include subpatterns that are going to be split 3260 out into subroutines. */ 3261 for (unsigned int i = 0; i < states.length (); ++i) 3262 { 3263 merge_state_info *sinfo = &states[i]; 3264 merge_state_result *res = sinfo->res; 3265 /* Wind past patterns that are only used by SINFO. */ 3266 while (res && res->pattern->num_users == 1) 3267 { 3268 res = res->prev; 3269 sinfo->res = res; 3270 if (res) 3271 res->pattern->num_users += 1; 3272 } 3273 if (!res) 3274 continue; 3275 3276 /* We have a shared pattern and are now committed to the match. */ 3277 merge_pattern_info *pat = res->pattern; 3278 gcc_assert (valid_result_p (pat, sinfo)); 3279 3280 if (!pat->complete_p) 3281 { 3282 /* Look for subpatterns that are going to be split out and remove 3283 them from the number of statements. */ 3284 for (unsigned int j = 0; j < sinfo->num_transitions; ++j) 3285 if (merge_pattern_transition *ptrans = pat->transitions[j]) 3286 { 3287 merge_pattern_info *to_pat = ptrans->to; 3288 if (!same_pattern_p (pat, to_pat)) 3289 pat->num_statements -= to_pat->num_statements; 3290 } 3291 pat->complete_p = true; 3292 } 3293 } 3294 /* Step 5: Split out the patterns. */ 3295 for (unsigned int i = 0; i < states.length (); ++i) 3296 { 3297 merge_state_info *sinfo = &states[i]; 3298 merge_state_result *res = sinfo->res; 3299 if (!sinfo->merged_p && res && useful_pattern_p (res->pattern)) 3300 use_pattern (sinfo); 3301 } 3302 fprintf (stderr, "Shared %d out of %d states by creating %d new states," 3303 " saving %d\n", 3304 pattern_use_states, states.length (), pattern_def_states, 3305 pattern_use_states - pattern_def_states); 3306 } 3307 3308 /* Information about a state tree that we're considering splitting into a 3309 subroutine. */ 3310 struct state_size 3311 { 3312 /* The number of pseudo-statements in the state tree. */ 3313 unsigned int num_statements; 3314 3315 /* The approximate number of nested "if" and "switch" statements that 3316 would be required if control could fall through to a later state. */ 3317 unsigned int depth; 3318 }; 3319 3320 /* Pairs a transition with information about its target state. */ 3321 typedef std::pair <transition *, state_size> subroutine_candidate; 3322 3323 /* Sort two subroutine_candidates so that the one with the largest 3324 number of statements comes last. */ 3325 3326 static int 3327 subroutine_candidate_cmp (const void *a, const void *b) 3328 { 3329 return int (((const subroutine_candidate *) a)->second.num_statements 3330 - ((const subroutine_candidate *) b)->second.num_statements); 3331 } 3332 3333 /* Turn S into a subroutine of type TYPE and add it to PROCS. Return a new 3334 state that performs a subroutine call to S. */ 3335 3336 static state * 3337 create_subroutine (routine_type type, state *s, vec <state *> &procs) 3338 { 3339 procs.safe_push (s); 3340 acceptance_type acceptance; 3341 acceptance.type = type; 3342 acceptance.partial_p = true; 3343 acceptance.u.subroutine_id = procs.length (); 3344 state *news = new state; 3345 add_decision (news, rtx_test::accept (acceptance), true, false); 3346 return news; 3347 } 3348 3349 /* Walk state tree S, of type TYPE, and look for subtrees that would be 3350 better split into subroutines. Accumulate all such subroutines in PROCS. 3351 Return the size of the new state tree (excluding subroutines). */ 3352 3353 static state_size 3354 find_subroutines (routine_type type, state *s, vec <state *> &procs) 3355 { 3356 auto_vec <subroutine_candidate, 16> candidates; 3357 state_size size; 3358 size.num_statements = 0; 3359 size.depth = 0; 3360 for (decision *d = s->first; d; d = d->next) 3361 { 3362 if (!d->test.single_outcome_p ()) 3363 size.num_statements += 1; 3364 for (transition *trans = d->first; trans; trans = trans->next) 3365 { 3366 /* Keep chains of simple decisions together if we know that no 3367 change of position is required. We'll output this chain as a 3368 single "if" statement, so it counts as a single nesting level. */ 3369 if (d->test.pos && d->if_statement_p ()) 3370 for (;;) 3371 { 3372 decision *newd = trans->to->singleton (); 3373 if (!newd 3374 || (newd->test.pos 3375 && newd->test.pos_operand < 0 3376 && newd->test.pos != d->test.pos) 3377 || !newd->if_statement_p ()) 3378 break; 3379 if (!newd->test.single_outcome_p ()) 3380 size.num_statements += 1; 3381 trans = newd->singleton (); 3382 if (newd->test.kind == rtx_test::SET_OP 3383 || newd->test.kind == rtx_test::ACCEPT) 3384 break; 3385 } 3386 /* The target of TRANS is a subroutine candidate. First recurse 3387 on it to see how big it is after subroutines have been 3388 split out. */ 3389 state_size to_size = find_subroutines (type, trans->to, procs); 3390 if (d->next && to_size.depth > MAX_DEPTH) 3391 /* Keeping the target state in the same routine would lead 3392 to an excessive nesting of "if" and "switch" statements. 3393 Split it out into a subroutine so that it can use 3394 inverted tests that return early on failure. */ 3395 trans->to = create_subroutine (type, trans->to, procs); 3396 else 3397 { 3398 size.num_statements += to_size.num_statements; 3399 if (to_size.num_statements < MIN_NUM_STATEMENTS) 3400 /* The target state is too small to be worth splitting. 3401 Keep it in the same routine as S. */ 3402 size.depth = MAX (size.depth, to_size.depth); 3403 else 3404 /* Assume for now that we'll keep the target state in the 3405 same routine as S, but record it as a subroutine candidate 3406 if S grows too big. */ 3407 candidates.safe_push (subroutine_candidate (trans, to_size)); 3408 } 3409 } 3410 } 3411 if (size.num_statements > MAX_NUM_STATEMENTS) 3412 { 3413 /* S is too big. Sort the subroutine candidates so that bigger ones 3414 are nearer the end. */ 3415 candidates.qsort (subroutine_candidate_cmp); 3416 while (!candidates.is_empty () 3417 && size.num_statements > MAX_NUM_STATEMENTS) 3418 { 3419 /* Peel off a candidate and force it into a subroutine. */ 3420 subroutine_candidate cand = candidates.pop (); 3421 size.num_statements -= cand.second.num_statements; 3422 cand.first->to = create_subroutine (type, cand.first->to, procs); 3423 } 3424 } 3425 /* Update the depth for subroutine candidates that we decided not to 3426 split out. */ 3427 for (unsigned int i = 0; i < candidates.length (); ++i) 3428 size.depth = MAX (size.depth, candidates[i].second.depth); 3429 size.depth += 1; 3430 return size; 3431 } 3432 3433 /* Return true if, for all X, PRED (X, MODE) implies that X has mode MODE. */ 3434 3435 static bool 3436 safe_predicate_mode (const struct pred_data *pred, machine_mode mode) 3437 { 3438 /* Scalar integer constants have VOIDmode. */ 3439 if (GET_MODE_CLASS (mode) == MODE_INT 3440 && (pred->codes[CONST_INT] 3441 || pred->codes[CONST_DOUBLE] 3442 || pred->codes[CONST_WIDE_INT] 3443 || pred->codes[LABEL_REF])) 3444 return false; 3445 3446 return !pred->special && mode != VOIDmode; 3447 } 3448 3449 /* Fill CODES with the set of codes that could be matched by PRED. */ 3450 3451 static void 3452 get_predicate_codes (const struct pred_data *pred, int_set *codes) 3453 { 3454 for (int i = 0; i < NUM_TRUE_RTX_CODE; ++i) 3455 if (!pred || pred->codes[i]) 3456 codes->safe_push (i); 3457 } 3458 3459 /* Return true if the first path through D1 tests the same thing as D2. */ 3460 3461 static bool 3462 has_same_test_p (decision *d1, decision *d2) 3463 { 3464 do 3465 { 3466 if (d1->test == d2->test) 3467 return true; 3468 d1 = d1->first->to->first; 3469 } 3470 while (d1); 3471 return false; 3472 } 3473 3474 /* Return true if D1 and D2 cannot match the same rtx. All states reachable 3475 from D2 have single decisions and all those decisions have single 3476 transitions. */ 3477 3478 static bool 3479 mutually_exclusive_p (decision *d1, decision *d2) 3480 { 3481 /* If one path through D1 fails to test the same thing as D2, assume 3482 that D2's test could be true for D1 and look for a later, more useful, 3483 test. This isn't as expensive as it looks in practice. */ 3484 while (!has_same_test_p (d1, d2)) 3485 { 3486 d2 = d2->singleton ()->to->singleton (); 3487 if (!d2) 3488 return false; 3489 } 3490 if (d1->test == d2->test) 3491 { 3492 /* Look for any transitions from D1 that have the same labels as 3493 the transition from D2. */ 3494 transition *trans2 = d2->singleton (); 3495 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next) 3496 { 3497 int_set::iterator i1 = trans1->labels.begin (); 3498 int_set::iterator end1 = trans1->labels.end (); 3499 int_set::iterator i2 = trans2->labels.begin (); 3500 int_set::iterator end2 = trans2->labels.end (); 3501 while (i1 != end1 && i2 != end2) 3502 if (*i1 < *i2) 3503 ++i1; 3504 else if (*i2 < *i1) 3505 ++i2; 3506 else 3507 { 3508 /* TRANS1 has some labels in common with TRANS2. Assume 3509 that D1 and D2 could match the same rtx if the target 3510 of TRANS1 could match the same rtx as D2. */ 3511 for (decision *subd1 = trans1->to->first; 3512 subd1; subd1 = subd1->next) 3513 if (!mutually_exclusive_p (subd1, d2)) 3514 return false; 3515 break; 3516 } 3517 } 3518 return true; 3519 } 3520 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next) 3521 for (decision *subd1 = trans1->to->first; subd1; subd1 = subd1->next) 3522 if (!mutually_exclusive_p (subd1, d2)) 3523 return false; 3524 return true; 3525 } 3526 3527 /* Try to merge S2's decision into D1, given that they have the same test. 3528 Fail only if EXCLUDE is nonnull and the new transition would have the 3529 same labels as *EXCLUDE. When returning true, set *NEXT_S1, *NEXT_S2 3530 and *NEXT_EXCLUDE as for merge_into_state_1, or set *NEXT_S2 to null 3531 if the merge is complete. */ 3532 3533 static bool 3534 merge_into_decision (decision *d1, state *s2, const int_set *exclude, 3535 state **next_s1, state **next_s2, 3536 const int_set **next_exclude) 3537 { 3538 decision *d2 = s2->singleton (); 3539 transition *trans2 = d2->singleton (); 3540 3541 /* Get a list of the transitions that intersect TRANS2. */ 3542 auto_vec <transition *, 32> intersecting; 3543 for (transition *trans1 = d1->first; trans1; trans1 = trans1->next) 3544 { 3545 int_set::iterator i1 = trans1->labels.begin (); 3546 int_set::iterator end1 = trans1->labels.end (); 3547 int_set::iterator i2 = trans2->labels.begin (); 3548 int_set::iterator end2 = trans2->labels.end (); 3549 bool trans1_is_subset = true; 3550 bool trans2_is_subset = true; 3551 bool intersect_p = false; 3552 while (i1 != end1 && i2 != end2) 3553 if (*i1 < *i2) 3554 { 3555 trans1_is_subset = false; 3556 ++i1; 3557 } 3558 else if (*i2 < *i1) 3559 { 3560 trans2_is_subset = false; 3561 ++i2; 3562 } 3563 else 3564 { 3565 intersect_p = true; 3566 ++i1; 3567 ++i2; 3568 } 3569 if (i1 != end1) 3570 trans1_is_subset = false; 3571 if (i2 != end2) 3572 trans2_is_subset = false; 3573 if (trans1_is_subset && trans2_is_subset) 3574 { 3575 /* There's already a transition that matches exactly. 3576 Merge the target states. */ 3577 trans1->optional &= trans2->optional; 3578 *next_s1 = trans1->to; 3579 *next_s2 = trans2->to; 3580 *next_exclude = 0; 3581 return true; 3582 } 3583 if (trans2_is_subset) 3584 { 3585 /* TRANS1 has all the labels that TRANS2 needs. Merge S2 into 3586 the target of TRANS1, but (to avoid infinite recursion) 3587 make sure that we don't end up creating another transition 3588 like TRANS1. */ 3589 *next_s1 = trans1->to; 3590 *next_s2 = s2; 3591 *next_exclude = &trans1->labels; 3592 return true; 3593 } 3594 if (intersect_p) 3595 intersecting.safe_push (trans1); 3596 } 3597 3598 if (intersecting.is_empty ()) 3599 { 3600 /* No existing labels intersect the new ones. We can just add 3601 TRANS2 itself. */ 3602 d1->push_back (d2->release ()); 3603 *next_s1 = 0; 3604 *next_s2 = 0; 3605 *next_exclude = 0; 3606 return true; 3607 } 3608 3609 /* Take the union of the labels in INTERSECTING and TRANS2. Store the 3610 result in COMBINED and use NEXT as a temporary. */ 3611 int_set tmp1 = trans2->labels, tmp2; 3612 int_set *combined = &tmp1, *next = &tmp2; 3613 for (unsigned int i = 0; i < intersecting.length (); ++i) 3614 { 3615 transition *trans1 = intersecting[i]; 3616 next->truncate (0); 3617 next->safe_grow (trans1->labels.length () + combined->length ()); 3618 int_set::iterator end 3619 = std::set_union (trans1->labels.begin (), trans1->labels.end (), 3620 combined->begin (), combined->end (), 3621 next->begin ()); 3622 next->truncate (end - next->begin ()); 3623 std::swap (next, combined); 3624 } 3625 3626 /* Stop now if we've been told not to create a transition with these 3627 labels. */ 3628 if (exclude && *combined == *exclude) 3629 return false; 3630 3631 /* Get the transition that should carry the new labels. */ 3632 transition *new_trans = intersecting[0]; 3633 if (intersecting.length () == 1) 3634 { 3635 /* We're merging with one existing transition whose labels are a 3636 subset of those required. If both transitions are optional, 3637 we can just expand the set of labels so that it's suitable 3638 for both transitions. It isn't worth preserving the original 3639 transitions since we know that they can't be merged; we would 3640 need to backtrack to S2 if TRANS1->to fails. In contrast, 3641 we might be able to merge the targets of the transitions 3642 without any backtracking. 3643 3644 If instead the existing transition is not optional, ensure that 3645 all target decisions are suitably protected. Some decisions 3646 might already have a more specific requirement than NEW_TRANS, 3647 in which case there's no point testing NEW_TRANS as well. E.g. this 3648 would have happened if a test for an (eq ...) rtx had been 3649 added to a decision that tested whether the code is suitable 3650 for comparison_operator. The original comparison_operator 3651 transition would have been non-optional and the (eq ...) test 3652 would be performed by a second decision in the target of that 3653 transition. 3654 3655 The remaining case -- keeping the original optional transition 3656 when adding a non-optional TRANS2 -- is a wash. Preserving 3657 the optional transition only helps if we later merge another 3658 state S3 that is mutually exclusive with S2 and whose labels 3659 belong to *COMBINED - TRANS1->labels. We can then test the 3660 original NEW_TRANS and S3 in the same decision. We keep the 3661 optional transition around for that case, but it occurs very 3662 rarely. */ 3663 gcc_assert (new_trans->labels != *combined); 3664 if (!new_trans->optional || !trans2->optional) 3665 { 3666 decision *start = 0; 3667 for (decision *end = new_trans->to->first; end; end = end->next) 3668 { 3669 if (!start && end->test != d1->test) 3670 /* END belongs to a range of decisions that need to be 3671 protected by NEW_TRANS. */ 3672 start = end; 3673 if (start && (!end->next || end->next->test == d1->test)) 3674 { 3675 /* Protect [START, END] with NEW_TRANS. The decisions 3676 move to NEW_S and NEW_D becomes part of NEW_TRANS->to. */ 3677 state *new_s = new state; 3678 decision *new_d = new decision (d1->test); 3679 new_d->push_back (new transition (new_trans->labels, new_s, 3680 new_trans->optional)); 3681 state::range r (start, end); 3682 new_trans->to->replace (r, new_d); 3683 new_s->push_back (r); 3684 3685 /* Continue with an empty range. */ 3686 start = 0; 3687 3688 /* Continue from the decision after NEW_D. */ 3689 end = new_d; 3690 } 3691 } 3692 } 3693 new_trans->optional = true; 3694 new_trans->labels = *combined; 3695 } 3696 else 3697 { 3698 /* We're merging more than one existing transition together. 3699 Those transitions are successfully dividing the matching space 3700 and so we want to preserve them, even if they're optional. 3701 3702 Create a new transition with the union set of labels and make 3703 it go to a state that has the original transitions. */ 3704 decision *new_d = new decision (d1->test); 3705 for (unsigned int i = 0; i < intersecting.length (); ++i) 3706 new_d->push_back (d1->remove (intersecting[i])); 3707 3708 state *new_s = new state; 3709 new_s->push_back (new_d); 3710 3711 new_trans = new transition (*combined, new_s, true); 3712 d1->push_back (new_trans); 3713 } 3714 3715 /* We now have an optional transition with labels *COMBINED. Decide 3716 whether we can use it as TRANS2 or whether we need to merge S2 3717 into the target of NEW_TRANS. */ 3718 gcc_assert (new_trans->optional); 3719 if (new_trans->labels == trans2->labels) 3720 { 3721 /* NEW_TRANS matches TRANS2. Just merge the target states. */ 3722 new_trans->optional = trans2->optional; 3723 *next_s1 = new_trans->to; 3724 *next_s2 = trans2->to; 3725 *next_exclude = 0; 3726 } 3727 else 3728 { 3729 /* Try to merge TRANS2 into the target of the overlapping transition, 3730 but (to prevent infinite recursion or excessive redundancy) without 3731 creating another transition of the same type. */ 3732 *next_s1 = new_trans->to; 3733 *next_s2 = s2; 3734 *next_exclude = &new_trans->labels; 3735 } 3736 return true; 3737 } 3738 3739 /* Make progress in merging S2 into S1, given that each state in S2 3740 has a single decision. If EXCLUDE is nonnull, avoid creating a new 3741 transition with the same test as S2's decision and with the labels 3742 in *EXCLUDE. 3743 3744 Return true if there is still work to do. When returning true, 3745 set *NEXT_S1, *NEXT_S2 and *NEXT_EXCLUDE to the values that 3746 S1, S2 and EXCLUDE should have next time round. 3747 3748 If S1 and S2 both match a particular rtx, give priority to S1. */ 3749 3750 static bool 3751 merge_into_state_1 (state *s1, state *s2, const int_set *exclude, 3752 state **next_s1, state **next_s2, 3753 const int_set **next_exclude) 3754 { 3755 decision *d2 = s2->singleton (); 3756 if (decision *d1 = s1->last) 3757 { 3758 if (d1->test.terminal_p ()) 3759 /* D1 is an unconditional return, so S2 can never match. This can 3760 sometimes be a bug in the .md description, but might also happen 3761 if genconditions forces some conditions to true for certain 3762 configurations. */ 3763 return false; 3764 3765 /* Go backwards through the decisions in S1, stopping once we find one 3766 that could match the same thing as S2. */ 3767 while (d1->prev && mutually_exclusive_p (d1, d2)) 3768 d1 = d1->prev; 3769 3770 /* Search forwards from that point, merging D2 into the first 3771 decision we can. */ 3772 for (; d1; d1 = d1->next) 3773 { 3774 /* If S2 performs some optional tests before testing the same thing 3775 as D1, those tests do not help to distinguish D1 and S2, so it's 3776 better to drop them. Search through such optional decisions 3777 until we find something that tests the same thing as D1. */ 3778 state *sub_s2 = s2; 3779 for (;;) 3780 { 3781 decision *sub_d2 = sub_s2->singleton (); 3782 if (d1->test == sub_d2->test) 3783 { 3784 /* Only apply EXCLUDE if we're testing the same thing 3785 as D2. */ 3786 const int_set *sub_exclude = (d2 == sub_d2 ? exclude : 0); 3787 3788 /* Try to merge SUB_S2 into D1. This can only fail if 3789 it would involve creating a new transition with 3790 labels SUB_EXCLUDE. */ 3791 if (merge_into_decision (d1, sub_s2, sub_exclude, 3792 next_s1, next_s2, next_exclude)) 3793 return *next_s2 != 0; 3794 3795 /* Can't merge with D1; try a later decision. */ 3796 break; 3797 } 3798 transition *sub_trans2 = sub_d2->singleton (); 3799 if (!sub_trans2->optional) 3800 /* Can't merge with D1; try a later decision. */ 3801 break; 3802 sub_s2 = sub_trans2->to; 3803 } 3804 } 3805 } 3806 3807 /* We can't merge D2 with any existing decision. Just add it to the end. */ 3808 s1->push_back (s2->release ()); 3809 return false; 3810 } 3811 3812 /* Merge S2 into S1. If they both match a particular rtx, give 3813 priority to S1. Each state in S2 has a single decision. */ 3814 3815 static void 3816 merge_into_state (state *s1, state *s2) 3817 { 3818 const int_set *exclude = 0; 3819 while (s2 && merge_into_state_1 (s1, s2, exclude, &s1, &s2, &exclude)) 3820 continue; 3821 } 3822 3823 /* Pairs a pattern that needs to be matched with the rtx position at 3824 which the pattern should occur. */ 3825 struct pattern_pos { 3826 pattern_pos () {} 3827 pattern_pos (rtx, position *); 3828 3829 rtx pattern; 3830 position *pos; 3831 }; 3832 3833 pattern_pos::pattern_pos (rtx pattern_in, position *pos_in) 3834 : pattern (pattern_in), pos (pos_in) 3835 {} 3836 3837 /* Compare entries according to their depth-first order. There shouldn't 3838 be two entries at the same position. */ 3839 3840 bool 3841 operator < (const pattern_pos &e1, const pattern_pos &e2) 3842 { 3843 int diff = compare_positions (e1.pos, e2.pos); 3844 gcc_assert (diff != 0 || e1.pattern == e2.pattern); 3845 return diff < 0; 3846 } 3847 3848 /* Add new decisions to S that check whether the rtx at position POS 3849 matches PATTERN. Return the state that is reached in that case. 3850 TOP_PATTERN is the overall pattern, as passed to match_pattern_1. */ 3851 3852 static state * 3853 match_pattern_2 (state *s, md_rtx_info *info, position *pos, rtx pattern) 3854 { 3855 auto_vec <pattern_pos, 32> worklist; 3856 auto_vec <pattern_pos, 32> pred_and_mode_tests; 3857 auto_vec <pattern_pos, 32> dup_tests; 3858 3859 worklist.safe_push (pattern_pos (pattern, pos)); 3860 while (!worklist.is_empty ()) 3861 { 3862 pattern_pos next = worklist.pop (); 3863 pattern = next.pattern; 3864 pos = next.pos; 3865 unsigned int reverse_s = worklist.length (); 3866 3867 enum rtx_code code = GET_CODE (pattern); 3868 switch (code) 3869 { 3870 case MATCH_OP_DUP: 3871 case MATCH_DUP: 3872 case MATCH_PAR_DUP: 3873 /* Add a test that the rtx matches the earlier one, but only 3874 after the structure and predicates have been checked. */ 3875 dup_tests.safe_push (pattern_pos (pattern, pos)); 3876 3877 /* Use the same code check as the original operand. */ 3878 pattern = find_operand (info->def, XINT (pattern, 0), NULL_RTX); 3879 /* Fall through. */ 3880 3881 case MATCH_PARALLEL: 3882 case MATCH_OPERAND: 3883 case MATCH_SCRATCH: 3884 case MATCH_OPERATOR: 3885 { 3886 const char *pred_name = predicate_name (pattern); 3887 const struct pred_data *pred = 0; 3888 if (pred_name[0] != 0) 3889 { 3890 pred = lookup_predicate (pred_name); 3891 /* Only report errors once per rtx. */ 3892 if (code == GET_CODE (pattern)) 3893 { 3894 if (!pred) 3895 error_at (info->loc, "unknown predicate '%s' used in %s", 3896 pred_name, GET_RTX_NAME (code)); 3897 else if (code == MATCH_PARALLEL 3898 && pred->singleton != PARALLEL) 3899 error_at (info->loc, "predicate '%s' used in" 3900 " match_parallel does not allow only PARALLEL", 3901 pred->name); 3902 } 3903 } 3904 3905 if (code == MATCH_PARALLEL || code == MATCH_PAR_DUP) 3906 { 3907 /* Check that we have a parallel with enough elements. */ 3908 s = add_decision (s, rtx_test::code (pos), PARALLEL, false); 3909 int min_len = XVECLEN (pattern, 2); 3910 s = add_decision (s, rtx_test::veclen_ge (pos, min_len), 3911 true, false); 3912 } 3913 else 3914 { 3915 /* Check that the rtx has one of codes accepted by the 3916 predicate. This is necessary when matching suboperands 3917 of a MATCH_OPERATOR or MATCH_OP_DUP, since we can't 3918 call XEXP (X, N) without checking that X has at least 3919 N+1 operands. */ 3920 int_set codes; 3921 get_predicate_codes (pred, &codes); 3922 bool need_codes = (pred 3923 && (code == MATCH_OPERATOR 3924 || code == MATCH_OP_DUP)); 3925 s = add_decision (s, rtx_test::code (pos), codes, !need_codes); 3926 } 3927 3928 /* Postpone the predicate check until we've checked the rest 3929 of the rtx structure. */ 3930 if (code == GET_CODE (pattern)) 3931 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos)); 3932 3933 /* If we need to match suboperands, add them to the worklist. */ 3934 if (code == MATCH_OPERATOR || code == MATCH_PARALLEL) 3935 { 3936 position **subpos_ptr; 3937 enum position_type pos_type; 3938 int i; 3939 if (code == MATCH_OPERATOR || code == MATCH_OP_DUP) 3940 { 3941 pos_type = POS_XEXP; 3942 subpos_ptr = &pos->xexps; 3943 i = (code == MATCH_OPERATOR ? 2 : 1); 3944 } 3945 else 3946 { 3947 pos_type = POS_XVECEXP0; 3948 subpos_ptr = &pos->xvecexp0s; 3949 i = 2; 3950 } 3951 for (int j = 0; j < XVECLEN (pattern, i); ++j) 3952 { 3953 position *subpos = next_position (subpos_ptr, pos, 3954 pos_type, j); 3955 worklist.safe_push (pattern_pos (XVECEXP (pattern, i, j), 3956 subpos)); 3957 subpos_ptr = &subpos->next; 3958 } 3959 } 3960 break; 3961 } 3962 3963 default: 3964 { 3965 /* Check that the rtx has the right code. */ 3966 s = add_decision (s, rtx_test::code (pos), code, false); 3967 3968 /* Queue a test for the mode if one is specified. */ 3969 if (GET_MODE (pattern) != VOIDmode) 3970 pred_and_mode_tests.safe_push (pattern_pos (pattern, pos)); 3971 3972 /* Push subrtxes onto the worklist. Match nonrtx operands now. */ 3973 const char *fmt = GET_RTX_FORMAT (code); 3974 position **subpos_ptr = &pos->xexps; 3975 for (size_t i = 0; fmt[i]; ++i) 3976 { 3977 position *subpos = next_position (subpos_ptr, pos, 3978 POS_XEXP, i); 3979 switch (fmt[i]) 3980 { 3981 case 'e': case 'u': 3982 worklist.safe_push (pattern_pos (XEXP (pattern, i), 3983 subpos)); 3984 break; 3985 3986 case 'E': 3987 { 3988 /* Make sure the vector has the right number of 3989 elements. */ 3990 int length = XVECLEN (pattern, i); 3991 s = add_decision (s, rtx_test::veclen (pos), 3992 length, false); 3993 3994 position **subpos2_ptr = &pos->xvecexp0s; 3995 for (int j = 0; j < length; j++) 3996 { 3997 position *subpos2 = next_position (subpos2_ptr, pos, 3998 POS_XVECEXP0, j); 3999 rtx x = XVECEXP (pattern, i, j); 4000 worklist.safe_push (pattern_pos (x, subpos2)); 4001 subpos2_ptr = &subpos2->next; 4002 } 4003 break; 4004 } 4005 4006 case 'i': 4007 /* Make sure that XINT (X, I) has the right value. */ 4008 s = add_decision (s, rtx_test::int_field (pos, i), 4009 XINT (pattern, i), false); 4010 break; 4011 4012 case 'r': 4013 /* Make sure that REGNO (X) has the right value. */ 4014 gcc_assert (i == 0); 4015 s = add_decision (s, rtx_test::regno_field (pos), 4016 REGNO (pattern), false); 4017 break; 4018 4019 case 'w': 4020 /* Make sure that XWINT (X, I) has the right value. */ 4021 s = add_decision (s, rtx_test::wide_int_field (pos, i), 4022 XWINT (pattern, 0), false); 4023 break; 4024 4025 case '0': 4026 break; 4027 4028 default: 4029 gcc_unreachable (); 4030 } 4031 subpos_ptr = &subpos->next; 4032 } 4033 } 4034 break; 4035 } 4036 /* Operands are pushed onto the worklist so that later indices are 4037 nearer the top. That's what we want for SETs, since a SET_SRC 4038 is a better discriminator than a SET_DEST. In other cases it's 4039 usually better to match earlier indices first. This is especially 4040 true of PARALLELs, where the first element tends to be the most 4041 individual. It's also true for commutative operators, where the 4042 canonicalization rules say that the more complex operand should 4043 come first. */ 4044 if (code != SET && worklist.length () > reverse_s) 4045 std::reverse (&worklist[0] + reverse_s, 4046 &worklist[0] + worklist.length ()); 4047 } 4048 4049 /* Sort the predicate and mode tests so that they're in depth-first order. 4050 The main goal of this is to put SET_SRC match_operands after SET_DEST 4051 match_operands and after mode checks for the enclosing SET_SRC operators 4052 (such as the mode of a PLUS in an addition instruction). The latter 4053 two types of test can determine the mode exactly, whereas a SET_SRC 4054 match_operand often has to cope with the possibility of the operand 4055 being a modeless constant integer. E.g. something that matches 4056 register_operand (x, SImode) never matches register_operand (x, DImode), 4057 but a const_int that matches immediate_operand (x, SImode) also matches 4058 immediate_operand (x, DImode). The register_operand cases can therefore 4059 be distinguished by a switch on the mode, but the immediate_operand 4060 cases can't. */ 4061 if (pred_and_mode_tests.length () > 1) 4062 std::sort (&pred_and_mode_tests[0], 4063 &pred_and_mode_tests[0] + pred_and_mode_tests.length ()); 4064 4065 /* Add the mode and predicate tests. */ 4066 pattern_pos *e; 4067 unsigned int i; 4068 FOR_EACH_VEC_ELT (pred_and_mode_tests, i, e) 4069 { 4070 switch (GET_CODE (e->pattern)) 4071 { 4072 case MATCH_PARALLEL: 4073 case MATCH_OPERAND: 4074 case MATCH_SCRATCH: 4075 case MATCH_OPERATOR: 4076 { 4077 int opno = XINT (e->pattern, 0); 4078 num_operands = MAX (num_operands, opno + 1); 4079 const char *pred_name = predicate_name (e->pattern); 4080 if (pred_name[0]) 4081 { 4082 const struct pred_data *pred = lookup_predicate (pred_name); 4083 /* Check the mode first, to distinguish things like SImode 4084 and DImode register_operands, as described above. */ 4085 machine_mode mode = GET_MODE (e->pattern); 4086 if (pred && safe_predicate_mode (pred, mode)) 4087 s = add_decision (s, rtx_test::mode (e->pos), mode, true); 4088 4089 /* Assign to operands[] first, so that the rtx usually doesn't 4090 need to be live across the call to the predicate. 4091 4092 This shouldn't cause a problem with dirtying the page, 4093 since we fully expect to assign to operands[] at some point, 4094 and since the caller usually writes to other parts of 4095 recog_data anyway. */ 4096 s = add_decision (s, rtx_test::set_op (e->pos, opno), 4097 true, false); 4098 s = add_decision (s, rtx_test::predicate (e->pos, pred, mode), 4099 true, false); 4100 } 4101 else 4102 /* Historically we've ignored the mode when there's no 4103 predicate. Just set up operands[] unconditionally. */ 4104 s = add_decision (s, rtx_test::set_op (e->pos, opno), 4105 true, false); 4106 break; 4107 } 4108 4109 default: 4110 s = add_decision (s, rtx_test::mode (e->pos), 4111 GET_MODE (e->pattern), false); 4112 break; 4113 } 4114 } 4115 4116 /* Finally add rtx_equal_p checks for duplicated operands. */ 4117 FOR_EACH_VEC_ELT (dup_tests, i, e) 4118 s = add_decision (s, rtx_test::duplicate (e->pos, XINT (e->pattern, 0)), 4119 true, false); 4120 return s; 4121 } 4122 4123 /* Add new decisions to S that make it return ACCEPTANCE if: 4124 4125 (1) the rtx doesn't match anything already matched by S 4126 (2) the rtx matches TOP_PATTERN and 4127 (3) the C test required by INFO->def is true 4128 4129 For peephole2, TOP_PATTERN is a SEQUENCE of the instruction patterns 4130 to match, otherwise it is a single instruction pattern. */ 4131 4132 static void 4133 match_pattern_1 (state *s, md_rtx_info *info, rtx pattern, 4134 acceptance_type acceptance) 4135 { 4136 if (acceptance.type == PEEPHOLE2) 4137 { 4138 /* Match each individual instruction. */ 4139 position **subpos_ptr = &peep2_insn_pos_list; 4140 int count = 0; 4141 for (int i = 0; i < XVECLEN (pattern, 0); ++i) 4142 { 4143 rtx x = XVECEXP (pattern, 0, i); 4144 position *subpos = next_position (subpos_ptr, &root_pos, 4145 POS_PEEP2_INSN, count); 4146 if (count > 0) 4147 s = add_decision (s, rtx_test::peep2_count (count + 1), 4148 true, false); 4149 s = match_pattern_2 (s, info, subpos, x); 4150 subpos_ptr = &subpos->next; 4151 count += 1; 4152 } 4153 acceptance.u.full.u.match_len = count - 1; 4154 } 4155 else 4156 { 4157 /* Make the rtx itself. */ 4158 s = match_pattern_2 (s, info, &root_pos, pattern); 4159 4160 /* If the match is only valid when extra clobbers are added, 4161 make sure we're able to pass that information to the caller. */ 4162 if (acceptance.type == RECOG && acceptance.u.full.u.num_clobbers) 4163 s = add_decision (s, rtx_test::have_num_clobbers (), true, false); 4164 } 4165 4166 /* Make sure that the C test is true. */ 4167 const char *c_test = get_c_test (info->def); 4168 if (maybe_eval_c_test (c_test) != 1) 4169 s = add_decision (s, rtx_test::c_test (c_test), true, false); 4170 4171 /* Accept the pattern. */ 4172 add_decision (s, rtx_test::accept (acceptance), true, false); 4173 } 4174 4175 /* Like match_pattern_1, but (if merge_states_p) try to merge the 4176 decisions with what's already in S, to reduce the amount of 4177 backtracking. */ 4178 4179 static void 4180 match_pattern (state *s, md_rtx_info *info, rtx pattern, 4181 acceptance_type acceptance) 4182 { 4183 if (merge_states_p) 4184 { 4185 state root; 4186 /* Add the decisions to a fresh state and then merge the full tree 4187 into the existing one. */ 4188 match_pattern_1 (&root, info, pattern, acceptance); 4189 merge_into_state (s, &root); 4190 } 4191 else 4192 match_pattern_1 (s, info, pattern, acceptance); 4193 } 4194 4195 /* Begin the output file. */ 4196 4197 static void 4198 write_header (void) 4199 { 4200 puts ("\ 4201 /* Generated automatically by the program `genrecog' from the target\n\ 4202 machine description file. */\n\ 4203 \n\ 4204 #include \"config.h\"\n\ 4205 #include \"system.h\"\n\ 4206 #include \"coretypes.h\"\n\ 4207 #include \"backend.h\"\n\ 4208 #include \"predict.h\"\n\ 4209 #include \"rtl.h\"\n\ 4210 #include \"memmodel.h\"\n\ 4211 #include \"tm_p.h\"\n\ 4212 #include \"emit-rtl.h\"\n\ 4213 #include \"insn-config.h\"\n\ 4214 #include \"recog.h\"\n\ 4215 #include \"output.h\"\n\ 4216 #include \"flags.h\"\n\ 4217 #include \"df.h\"\n\ 4218 #include \"resource.h\"\n\ 4219 #include \"diagnostic-core.h\"\n\ 4220 #include \"reload.h\"\n\ 4221 #include \"regs.h\"\n\ 4222 #include \"tm-constrs.h\"\n\ 4223 \n"); 4224 4225 puts ("\n\ 4226 /* `recog' contains a decision tree that recognizes whether the rtx\n\ 4227 X0 is a valid instruction.\n\ 4228 \n\ 4229 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\ 4230 returns a nonnegative number which is the insn code number for the\n\ 4231 pattern that matched. This is the same as the order in the machine\n\ 4232 description of the entry that matched. This number can be used as an\n\ 4233 index into `insn_data' and other tables.\n"); 4234 puts ("\ 4235 The third parameter to recog is an optional pointer to an int. If\n\ 4236 present, recog will accept a pattern if it matches except for missing\n\ 4237 CLOBBER expressions at the end. In that case, the value pointed to by\n\ 4238 the optional pointer will be set to the number of CLOBBERs that need\n\ 4239 to be added (it should be initialized to zero by the caller). If it"); 4240 puts ("\ 4241 is set nonzero, the caller should allocate a PARALLEL of the\n\ 4242 appropriate size, copy the initial entries, and call add_clobbers\n\ 4243 (found in insn-emit.c) to fill in the CLOBBERs.\n\ 4244 "); 4245 4246 puts ("\n\ 4247 The function split_insns returns 0 if the rtl could not\n\ 4248 be split or the split rtl as an INSN list if it can be.\n\ 4249 \n\ 4250 The function peephole2_insns returns 0 if the rtl could not\n\ 4251 be matched. If there was a match, the new rtl is returned in an INSN list,\n\ 4252 and LAST_INSN will point to the last recognized insn in the old sequence.\n\ 4253 */\n\n"); 4254 } 4255 4256 /* Return the C type of a parameter with type TYPE. */ 4257 4258 static const char * 4259 parameter_type_string (parameter::type_enum type) 4260 { 4261 switch (type) 4262 { 4263 case parameter::UNSET: 4264 break; 4265 4266 case parameter::CODE: 4267 return "rtx_code"; 4268 4269 case parameter::MODE: 4270 return "machine_mode"; 4271 4272 case parameter::INT: 4273 return "int"; 4274 4275 case parameter::UINT: 4276 return "unsigned int"; 4277 4278 case parameter::WIDE_INT: 4279 return "HOST_WIDE_INT"; 4280 } 4281 gcc_unreachable (); 4282 } 4283 4284 /* Return true if ACCEPTANCE requires only a single C statement even in 4285 a backtracking context. */ 4286 4287 static bool 4288 single_statement_p (const acceptance_type &acceptance) 4289 { 4290 if (acceptance.partial_p) 4291 /* We need to handle failures of the subroutine. */ 4292 return false; 4293 switch (acceptance.type) 4294 { 4295 case SUBPATTERN: 4296 case SPLIT: 4297 return true; 4298 4299 case RECOG: 4300 /* False if we need to assign to pnum_clobbers. */ 4301 return acceptance.u.full.u.num_clobbers == 0; 4302 4303 case PEEPHOLE2: 4304 /* We need to assign to pmatch_len_ and handle null returns from the 4305 peephole2 routine. */ 4306 return false; 4307 } 4308 gcc_unreachable (); 4309 } 4310 4311 /* Return the C failure value for a routine of type TYPE. */ 4312 4313 static const char * 4314 get_failure_return (routine_type type) 4315 { 4316 switch (type) 4317 { 4318 case SUBPATTERN: 4319 case RECOG: 4320 return "-1"; 4321 4322 case SPLIT: 4323 case PEEPHOLE2: 4324 return "NULL"; 4325 } 4326 gcc_unreachable (); 4327 } 4328 4329 /* Indicates whether a block of code always returns or whether it can fall 4330 through. */ 4331 4332 enum exit_state { 4333 ES_RETURNED, 4334 ES_FALLTHROUGH 4335 }; 4336 4337 /* Information used while writing out code. */ 4338 4339 struct output_state 4340 { 4341 /* The type of routine that we're generating. */ 4342 routine_type type; 4343 4344 /* Maps position ids to xN variable numbers. The entry is only valid if 4345 it is less than the length of VAR_TO_ID, but this holds for every position 4346 tested by a state when writing out that state. */ 4347 auto_vec <unsigned int> id_to_var; 4348 4349 /* Maps xN variable numbers to position ids. */ 4350 auto_vec <unsigned int> var_to_id; 4351 4352 /* Index N is true if variable xN has already been set. */ 4353 auto_vec <bool> seen_vars; 4354 }; 4355 4356 /* Return true if D is a call to a pattern routine and if there is some X 4357 such that the transition for pattern result N goes to a successful return 4358 with code X+N. When returning true, set *BASE_OUT to this X and *COUNT_OUT 4359 to the number of return values. (We know that every PATTERN decision has 4360 a transition for every successful return.) */ 4361 4362 static bool 4363 terminal_pattern_p (decision *d, unsigned int *base_out, 4364 unsigned int *count_out) 4365 { 4366 if (d->test.kind != rtx_test::PATTERN) 4367 return false; 4368 unsigned int base = 0; 4369 unsigned int count = 0; 4370 for (transition *trans = d->first; trans; trans = trans->next) 4371 { 4372 if (trans->is_param || trans->labels.length () != 1) 4373 return false; 4374 decision *subd = trans->to->singleton (); 4375 if (!subd || subd->test.kind != rtx_test::ACCEPT) 4376 return false; 4377 unsigned int this_base = (subd->test.u.acceptance.u.full.code 4378 - trans->labels[0]); 4379 if (trans == d->first) 4380 base = this_base; 4381 else if (base != this_base) 4382 return false; 4383 count += 1; 4384 } 4385 *base_out = base; 4386 *count_out = count; 4387 return true; 4388 } 4389 4390 /* Return true if TEST doesn't test an rtx or if the rtx it tests is 4391 already available in state OS. */ 4392 4393 static bool 4394 test_position_available_p (output_state *os, const rtx_test &test) 4395 { 4396 return (!test.pos 4397 || test.pos_operand >= 0 4398 || os->seen_vars[os->id_to_var[test.pos->id]]); 4399 } 4400 4401 /* Like printf, but print INDENT spaces at the beginning. */ 4402 4403 static void ATTRIBUTE_PRINTF_2 4404 printf_indent (unsigned int indent, const char *format, ...) 4405 { 4406 va_list ap; 4407 va_start (ap, format); 4408 printf ("%*s", indent, ""); 4409 vprintf (format, ap); 4410 va_end (ap); 4411 } 4412 4413 /* Emit code to initialize the variable associated with POS, if it isn't 4414 already valid in state OS. Indent each line by INDENT spaces. Update 4415 OS with the new state. */ 4416 4417 static void 4418 change_state (output_state *os, position *pos, unsigned int indent) 4419 { 4420 unsigned int var = os->id_to_var[pos->id]; 4421 gcc_assert (var < os->var_to_id.length () && os->var_to_id[var] == pos->id); 4422 if (os->seen_vars[var]) 4423 return; 4424 switch (pos->type) 4425 { 4426 case POS_PEEP2_INSN: 4427 printf_indent (indent, "x%d = PATTERN (peep2_next_insn (%d));\n", 4428 var, pos->arg); 4429 break; 4430 4431 case POS_XEXP: 4432 change_state (os, pos->base, indent); 4433 printf_indent (indent, "x%d = XEXP (x%d, %d);\n", 4434 var, os->id_to_var[pos->base->id], pos->arg); 4435 break; 4436 4437 case POS_XVECEXP0: 4438 change_state (os, pos->base, indent); 4439 printf_indent (indent, "x%d = XVECEXP (x%d, 0, %d);\n", 4440 var, os->id_to_var[pos->base->id], pos->arg); 4441 break; 4442 } 4443 os->seen_vars[var] = true; 4444 } 4445 4446 /* Print the enumerator constant for CODE -- the upcase version of 4447 the name. */ 4448 4449 static void 4450 print_code (enum rtx_code code) 4451 { 4452 const char *p; 4453 for (p = GET_RTX_NAME (code); *p; p++) 4454 putchar (TOUPPER (*p)); 4455 } 4456 4457 /* Emit a uint64_t as an integer constant expression. We need to take 4458 special care to avoid "decimal constant is so large that it is unsigned" 4459 warnings in the resulting code. */ 4460 4461 static void 4462 print_host_wide_int (uint64_t val) 4463 { 4464 uint64_t min = uint64_t (1) << (HOST_BITS_PER_WIDE_INT - 1); 4465 if (val == min) 4466 printf ("( HOST_WIDE_INT_C (" HOST_WIDE_INT_PRINT_DEC ") - 1)", val + 1); 4467 else 4468 printf (" HOST_WIDE_INT_C (" HOST_WIDE_INT_PRINT_DEC ")", val); 4469 } 4470 4471 /* Print the C expression for actual parameter PARAM. */ 4472 4473 static void 4474 print_parameter_value (const parameter ¶m) 4475 { 4476 if (param.is_param) 4477 printf ("i%d", (int) param.value + 1); 4478 else 4479 switch (param.type) 4480 { 4481 case parameter::UNSET: 4482 gcc_unreachable (); 4483 break; 4484 4485 case parameter::CODE: 4486 print_code ((enum rtx_code) param.value); 4487 break; 4488 4489 case parameter::MODE: 4490 printf ("%smode", GET_MODE_NAME ((machine_mode) param.value)); 4491 break; 4492 4493 case parameter::INT: 4494 printf ("%d", (int) param.value); 4495 break; 4496 4497 case parameter::UINT: 4498 printf ("%u", (unsigned int) param.value); 4499 break; 4500 4501 case parameter::WIDE_INT: 4502 print_host_wide_int (param.value); 4503 break; 4504 } 4505 } 4506 4507 /* Print the C expression for the rtx tested by TEST. */ 4508 4509 static void 4510 print_test_rtx (output_state *os, const rtx_test &test) 4511 { 4512 if (test.pos_operand >= 0) 4513 printf ("operands[%d]", test.pos_operand); 4514 else 4515 printf ("x%d", os->id_to_var[test.pos->id]); 4516 } 4517 4518 /* Print the C expression for non-boolean test TEST. */ 4519 4520 static void 4521 print_nonbool_test (output_state *os, const rtx_test &test) 4522 { 4523 switch (test.kind) 4524 { 4525 case rtx_test::CODE: 4526 printf ("GET_CODE ("); 4527 print_test_rtx (os, test); 4528 printf (")"); 4529 break; 4530 4531 case rtx_test::MODE: 4532 printf ("GET_MODE ("); 4533 print_test_rtx (os, test); 4534 printf (")"); 4535 break; 4536 4537 case rtx_test::VECLEN: 4538 printf ("XVECLEN ("); 4539 print_test_rtx (os, test); 4540 printf (", 0)"); 4541 break; 4542 4543 case rtx_test::INT_FIELD: 4544 printf ("XINT ("); 4545 print_test_rtx (os, test); 4546 printf (", %d)", test.u.opno); 4547 break; 4548 4549 case rtx_test::REGNO_FIELD: 4550 printf ("REGNO ("); 4551 print_test_rtx (os, test); 4552 printf (")"); 4553 break; 4554 4555 case rtx_test::WIDE_INT_FIELD: 4556 printf ("XWINT ("); 4557 print_test_rtx (os, test); 4558 printf (", %d)", test.u.opno); 4559 break; 4560 4561 case rtx_test::PATTERN: 4562 { 4563 pattern_routine *routine = test.u.pattern->routine; 4564 printf ("pattern%d (", routine->pattern_id); 4565 const char *sep = ""; 4566 if (test.pos) 4567 { 4568 print_test_rtx (os, test); 4569 sep = ", "; 4570 } 4571 if (routine->insn_p) 4572 { 4573 printf ("%sinsn", sep); 4574 sep = ", "; 4575 } 4576 if (routine->pnum_clobbers_p) 4577 { 4578 printf ("%spnum_clobbers", sep); 4579 sep = ", "; 4580 } 4581 for (unsigned int i = 0; i < test.u.pattern->params.length (); ++i) 4582 { 4583 fputs (sep, stdout); 4584 print_parameter_value (test.u.pattern->params[i]); 4585 sep = ", "; 4586 } 4587 printf (")"); 4588 break; 4589 } 4590 4591 case rtx_test::PEEP2_COUNT: 4592 case rtx_test::VECLEN_GE: 4593 case rtx_test::SAVED_CONST_INT: 4594 case rtx_test::DUPLICATE: 4595 case rtx_test::PREDICATE: 4596 case rtx_test::SET_OP: 4597 case rtx_test::HAVE_NUM_CLOBBERS: 4598 case rtx_test::C_TEST: 4599 case rtx_test::ACCEPT: 4600 gcc_unreachable (); 4601 } 4602 } 4603 4604 /* IS_PARAM and LABEL are taken from a transition whose source 4605 decision performs TEST. Print the C code for the label. */ 4606 4607 static void 4608 print_label_value (const rtx_test &test, bool is_param, uint64_t value) 4609 { 4610 print_parameter_value (parameter (transition_parameter_type (test.kind), 4611 is_param, value)); 4612 } 4613 4614 /* If IS_PARAM, print code to compare TEST with the C variable i<VALUE+1>. 4615 If !IS_PARAM, print code to compare TEST with the C constant VALUE. 4616 Test for inequality if INVERT_P, otherwise test for equality. */ 4617 4618 static void 4619 print_test (output_state *os, const rtx_test &test, bool is_param, 4620 uint64_t value, bool invert_p) 4621 { 4622 switch (test.kind) 4623 { 4624 /* Handle the non-boolean TESTs. */ 4625 case rtx_test::CODE: 4626 case rtx_test::MODE: 4627 case rtx_test::VECLEN: 4628 case rtx_test::REGNO_FIELD: 4629 case rtx_test::INT_FIELD: 4630 case rtx_test::WIDE_INT_FIELD: 4631 case rtx_test::PATTERN: 4632 print_nonbool_test (os, test); 4633 printf (" %s ", invert_p ? "!=" : "=="); 4634 print_label_value (test, is_param, value); 4635 break; 4636 4637 case rtx_test::SAVED_CONST_INT: 4638 gcc_assert (!is_param && value == 1); 4639 print_test_rtx (os, test); 4640 printf (" %s const_int_rtx[MAX_SAVED_CONST_INT + ", 4641 invert_p ? "!=" : "=="); 4642 print_parameter_value (parameter (parameter::INT, 4643 test.u.integer.is_param, 4644 test.u.integer.value)); 4645 printf ("]"); 4646 break; 4647 4648 case rtx_test::PEEP2_COUNT: 4649 gcc_assert (!is_param && value == 1); 4650 printf ("peep2_current_count %s %d", invert_p ? "<" : ">=", 4651 test.u.min_len); 4652 break; 4653 4654 case rtx_test::VECLEN_GE: 4655 gcc_assert (!is_param && value == 1); 4656 printf ("XVECLEN ("); 4657 print_test_rtx (os, test); 4658 printf (", 0) %s %d", invert_p ? "<" : ">=", test.u.min_len); 4659 break; 4660 4661 case rtx_test::PREDICATE: 4662 gcc_assert (!is_param && value == 1); 4663 printf ("%s%s (", invert_p ? "!" : "", test.u.predicate.data->name); 4664 print_test_rtx (os, test); 4665 printf (", "); 4666 print_parameter_value (parameter (parameter::MODE, 4667 test.u.predicate.mode_is_param, 4668 test.u.predicate.mode)); 4669 printf (")"); 4670 break; 4671 4672 case rtx_test::DUPLICATE: 4673 gcc_assert (!is_param && value == 1); 4674 printf ("%srtx_equal_p (", invert_p ? "!" : ""); 4675 print_test_rtx (os, test); 4676 printf (", operands[%d])", test.u.opno); 4677 break; 4678 4679 case rtx_test::HAVE_NUM_CLOBBERS: 4680 gcc_assert (!is_param && value == 1); 4681 printf ("pnum_clobbers %s NULL", invert_p ? "==" : "!="); 4682 break; 4683 4684 case rtx_test::C_TEST: 4685 gcc_assert (!is_param && value == 1); 4686 if (invert_p) 4687 printf ("!"); 4688 rtx_reader_ptr->print_c_condition (test.u.string); 4689 break; 4690 4691 case rtx_test::ACCEPT: 4692 case rtx_test::SET_OP: 4693 gcc_unreachable (); 4694 } 4695 } 4696 4697 static exit_state print_decision (output_state *, decision *, 4698 unsigned int, bool); 4699 4700 /* Print code to perform S, indent each line by INDENT spaces. 4701 IS_FINAL is true if there are no fallback decisions to test on failure; 4702 if the state fails then the entire routine fails. */ 4703 4704 static exit_state 4705 print_state (output_state *os, state *s, unsigned int indent, bool is_final) 4706 { 4707 exit_state es = ES_FALLTHROUGH; 4708 for (decision *d = s->first; d; d = d->next) 4709 es = print_decision (os, d, indent, is_final && !d->next); 4710 if (es != ES_RETURNED && is_final) 4711 { 4712 printf_indent (indent, "return %s;\n", get_failure_return (os->type)); 4713 es = ES_RETURNED; 4714 } 4715 return es; 4716 } 4717 4718 /* Print the code for subroutine call ACCEPTANCE (for which partial_p 4719 is known to be true). Return the C condition that indicates a successful 4720 match. */ 4721 4722 static const char * 4723 print_subroutine_call (const acceptance_type &acceptance) 4724 { 4725 switch (acceptance.type) 4726 { 4727 case SUBPATTERN: 4728 gcc_unreachable (); 4729 4730 case RECOG: 4731 printf ("recog_%d (x1, insn, pnum_clobbers)", 4732 acceptance.u.subroutine_id); 4733 return ">= 0"; 4734 4735 case SPLIT: 4736 printf ("split_%d (x1, insn)", acceptance.u.subroutine_id); 4737 return "!= NULL_RTX"; 4738 4739 case PEEPHOLE2: 4740 printf ("peephole2_%d (x1, insn, pmatch_len_)", 4741 acceptance.u.subroutine_id); 4742 return "!= NULL_RTX"; 4743 } 4744 gcc_unreachable (); 4745 } 4746 4747 /* Print code for the successful match described by ACCEPTANCE. 4748 INDENT and IS_FINAL are as for print_state. */ 4749 4750 static exit_state 4751 print_acceptance (const acceptance_type &acceptance, unsigned int indent, 4752 bool is_final) 4753 { 4754 if (acceptance.partial_p) 4755 { 4756 /* Defer the rest of the match to a subroutine. */ 4757 if (is_final) 4758 { 4759 printf_indent (indent, "return "); 4760 print_subroutine_call (acceptance); 4761 printf (";\n"); 4762 return ES_RETURNED; 4763 } 4764 else 4765 { 4766 printf_indent (indent, "res = "); 4767 const char *res_test = print_subroutine_call (acceptance); 4768 printf (";\n"); 4769 printf_indent (indent, "if (res %s)\n", res_test); 4770 printf_indent (indent + 2, "return res;\n"); 4771 return ES_FALLTHROUGH; 4772 } 4773 } 4774 switch (acceptance.type) 4775 { 4776 case SUBPATTERN: 4777 printf_indent (indent, "return %d;\n", acceptance.u.full.code); 4778 return ES_RETURNED; 4779 4780 case RECOG: 4781 if (acceptance.u.full.u.num_clobbers != 0) 4782 printf_indent (indent, "*pnum_clobbers = %d;\n", 4783 acceptance.u.full.u.num_clobbers); 4784 printf_indent (indent, "return %d; /* %s */\n", acceptance.u.full.code, 4785 get_insn_name (acceptance.u.full.code)); 4786 return ES_RETURNED; 4787 4788 case SPLIT: 4789 printf_indent (indent, "return gen_split_%d (insn, operands);\n", 4790 acceptance.u.full.code); 4791 return ES_RETURNED; 4792 4793 case PEEPHOLE2: 4794 printf_indent (indent, "*pmatch_len_ = %d;\n", 4795 acceptance.u.full.u.match_len); 4796 if (is_final) 4797 { 4798 printf_indent (indent, "return gen_peephole2_%d (insn, operands);\n", 4799 acceptance.u.full.code); 4800 return ES_RETURNED; 4801 } 4802 else 4803 { 4804 printf_indent (indent, "res = gen_peephole2_%d (insn, operands);\n", 4805 acceptance.u.full.code); 4806 printf_indent (indent, "if (res != NULL_RTX)\n"); 4807 printf_indent (indent + 2, "return res;\n"); 4808 return ES_FALLTHROUGH; 4809 } 4810 } 4811 gcc_unreachable (); 4812 } 4813 4814 /* Print code to perform D. INDENT and IS_FINAL are as for print_state. */ 4815 4816 static exit_state 4817 print_decision (output_state *os, decision *d, unsigned int indent, 4818 bool is_final) 4819 { 4820 uint64_t label; 4821 unsigned int base, count; 4822 4823 /* Make sure the rtx under test is available either in operands[] or 4824 in an xN variable. */ 4825 if (d->test.pos && d->test.pos_operand < 0) 4826 change_state (os, d->test.pos, indent); 4827 4828 /* Look for cases where a pattern routine P1 calls another pattern routine 4829 P2 and where P1 returns X + BASE whenever P2 returns X. If IS_FINAL 4830 is true and BASE is zero we can simply use: 4831 4832 return patternN (...); 4833 4834 Otherwise we can use: 4835 4836 res = patternN (...); 4837 if (res >= 0) 4838 return res + BASE; 4839 4840 However, if BASE is nonzero and patternN only returns 0 or -1, 4841 the usual "return BASE;" is better than "return res + BASE;". 4842 If BASE is zero, "return res;" should be better than "return 0;", 4843 since no assignment to the return register is required. */ 4844 if (os->type == SUBPATTERN 4845 && terminal_pattern_p (d, &base, &count) 4846 && (base == 0 || count > 1)) 4847 { 4848 if (is_final && base == 0) 4849 { 4850 printf_indent (indent, "return "); 4851 print_nonbool_test (os, d->test); 4852 printf ("; /* [-1, %d] */\n", count - 1); 4853 return ES_RETURNED; 4854 } 4855 else 4856 { 4857 printf_indent (indent, "res = "); 4858 print_nonbool_test (os, d->test); 4859 printf (";\n"); 4860 printf_indent (indent, "if (res >= 0)\n"); 4861 printf_indent (indent + 2, "return res"); 4862 if (base != 0) 4863 printf (" + %d", base); 4864 printf ("; /* [%d, %d] */\n", base, base + count - 1); 4865 return ES_FALLTHROUGH; 4866 } 4867 } 4868 else if (d->test.kind == rtx_test::ACCEPT) 4869 return print_acceptance (d->test.u.acceptance, indent, is_final); 4870 else if (d->test.kind == rtx_test::SET_OP) 4871 { 4872 printf_indent (indent, "operands[%d] = ", d->test.u.opno); 4873 print_test_rtx (os, d->test); 4874 printf (";\n"); 4875 return print_state (os, d->singleton ()->to, indent, is_final); 4876 } 4877 /* Handle decisions with a single transition and a single transition 4878 label. */ 4879 else if (d->if_statement_p (&label)) 4880 { 4881 transition *trans = d->singleton (); 4882 if (mark_optional_transitions_p && trans->optional) 4883 printf_indent (indent, "/* OPTIONAL IF */\n"); 4884 4885 /* Print the condition associated with TRANS. Invert it if IS_FINAL, 4886 so that we return immediately on failure and fall through on 4887 success. */ 4888 printf_indent (indent, "if ("); 4889 print_test (os, d->test, trans->is_param, label, is_final); 4890 4891 /* Look for following states that would be handled by this code 4892 on recursion. If they don't need any preparatory statements, 4893 include them in the current "if" statement rather than creating 4894 a new one. */ 4895 for (;;) 4896 { 4897 d = trans->to->singleton (); 4898 if (!d 4899 || d->test.kind == rtx_test::ACCEPT 4900 || d->test.kind == rtx_test::SET_OP 4901 || !d->if_statement_p (&label) 4902 || !test_position_available_p (os, d->test)) 4903 break; 4904 trans = d->first; 4905 printf ("\n"); 4906 if (mark_optional_transitions_p && trans->optional) 4907 printf_indent (indent + 4, "/* OPTIONAL IF */\n"); 4908 printf_indent (indent + 4, "%s ", is_final ? "||" : "&&"); 4909 print_test (os, d->test, trans->is_param, label, is_final); 4910 } 4911 printf (")\n"); 4912 4913 /* Print the conditional code with INDENT + 2 and the fallthrough 4914 code with indent INDENT. */ 4915 state *to = trans->to; 4916 if (is_final) 4917 { 4918 /* We inverted the condition above, so return failure in the 4919 "if" body and fall through to the target of the transition. */ 4920 printf_indent (indent + 2, "return %s;\n", 4921 get_failure_return (os->type)); 4922 return print_state (os, to, indent, is_final); 4923 } 4924 else if (to->singleton () 4925 && to->first->test.kind == rtx_test::ACCEPT 4926 && single_statement_p (to->first->test.u.acceptance)) 4927 { 4928 /* The target of the transition is a simple "return" statement. 4929 It doesn't need any braces and doesn't fall through. */ 4930 if (print_acceptance (to->first->test.u.acceptance, 4931 indent + 2, true) != ES_RETURNED) 4932 gcc_unreachable (); 4933 return ES_FALLTHROUGH; 4934 } 4935 else 4936 { 4937 /* The general case. Output code for the target of the transition 4938 in braces. This will not invalidate any of the xN variables 4939 that are already valid, but we mustn't rely on any that are 4940 set by the "if" body. */ 4941 auto_vec <bool, 32> old_seen; 4942 old_seen.safe_splice (os->seen_vars); 4943 4944 printf_indent (indent + 2, "{\n"); 4945 print_state (os, trans->to, indent + 4, is_final); 4946 printf_indent (indent + 2, "}\n"); 4947 4948 os->seen_vars.truncate (0); 4949 os->seen_vars.splice (old_seen); 4950 return ES_FALLTHROUGH; 4951 } 4952 } 4953 else 4954 { 4955 /* Output the decision as a switch statement. */ 4956 printf_indent (indent, "switch ("); 4957 print_nonbool_test (os, d->test); 4958 printf (")\n"); 4959 4960 /* Each case statement starts with the same set of valid variables. 4961 These are also the only variables will be valid on fallthrough. */ 4962 auto_vec <bool, 32> old_seen; 4963 old_seen.safe_splice (os->seen_vars); 4964 4965 printf_indent (indent + 2, "{\n"); 4966 for (transition *trans = d->first; trans; trans = trans->next) 4967 { 4968 gcc_assert (!trans->is_param); 4969 if (mark_optional_transitions_p && trans->optional) 4970 printf_indent (indent + 2, "/* OPTIONAL CASE */\n"); 4971 for (int_set::iterator j = trans->labels.begin (); 4972 j != trans->labels.end (); ++j) 4973 { 4974 printf_indent (indent + 2, "case "); 4975 print_label_value (d->test, trans->is_param, *j); 4976 printf (":\n"); 4977 } 4978 if (print_state (os, trans->to, indent + 4, is_final)) 4979 { 4980 /* The state can fall through. Add an explicit break. */ 4981 gcc_assert (!is_final); 4982 printf_indent (indent + 4, "break;\n"); 4983 } 4984 printf ("\n"); 4985 4986 /* Restore the original set of valid variables. */ 4987 os->seen_vars.truncate (0); 4988 os->seen_vars.splice (old_seen); 4989 } 4990 /* Add a default case. */ 4991 printf_indent (indent + 2, "default:\n"); 4992 if (is_final) 4993 printf_indent (indent + 4, "return %s;\n", 4994 get_failure_return (os->type)); 4995 else 4996 printf_indent (indent + 4, "break;\n"); 4997 printf_indent (indent + 2, "}\n"); 4998 return is_final ? ES_RETURNED : ES_FALLTHROUGH; 4999 } 5000 } 5001 5002 /* Make sure that OS has a position variable for POS. ROOT_P is true if 5003 POS is the root position for the routine. */ 5004 5005 static void 5006 assign_position_var (output_state *os, position *pos, bool root_p) 5007 { 5008 unsigned int idx = os->id_to_var[pos->id]; 5009 if (idx < os->var_to_id.length () && os->var_to_id[idx] == pos->id) 5010 return; 5011 if (!root_p && pos->type != POS_PEEP2_INSN) 5012 assign_position_var (os, pos->base, false); 5013 os->id_to_var[pos->id] = os->var_to_id.length (); 5014 os->var_to_id.safe_push (pos->id); 5015 } 5016 5017 /* Make sure that OS has the position variables required by S. */ 5018 5019 static void 5020 assign_position_vars (output_state *os, state *s) 5021 { 5022 for (decision *d = s->first; d; d = d->next) 5023 { 5024 /* Positions associated with operands can be read from the 5025 operands[] array. */ 5026 if (d->test.pos && d->test.pos_operand < 0) 5027 assign_position_var (os, d->test.pos, false); 5028 for (transition *trans = d->first; trans; trans = trans->next) 5029 assign_position_vars (os, trans->to); 5030 } 5031 } 5032 5033 /* Print the open brace and variable definitions for a routine that 5034 implements S. ROOT is the deepest rtx from which S can access all 5035 relevant parts of the first instruction it matches. Initialize OS 5036 so that every relevant position has an rtx variable xN and so that 5037 only ROOT's variable has a valid value. */ 5038 5039 static void 5040 print_subroutine_start (output_state *os, state *s, position *root) 5041 { 5042 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED" 5043 " = &recog_data.operand[0];\n"); 5044 os->var_to_id.truncate (0); 5045 os->seen_vars.truncate (0); 5046 if (root) 5047 { 5048 /* Create a fake entry for position 0 so that an id_to_var of 0 5049 is always invalid. This also makes the xN variables naturally 5050 1-based rather than 0-based. */ 5051 os->var_to_id.safe_push (num_positions); 5052 5053 /* Associate ROOT with x1. */ 5054 assign_position_var (os, root, true); 5055 5056 /* Assign xN variables to all other relevant positions. */ 5057 assign_position_vars (os, s); 5058 5059 /* Output the variable declarations (except for ROOT's, which is 5060 passed in as a parameter). */ 5061 unsigned int num_vars = os->var_to_id.length (); 5062 if (num_vars > 2) 5063 { 5064 for (unsigned int i = 2; i < num_vars; ++i) 5065 /* Print 8 rtx variables to a line. */ 5066 printf ("%s x%d", 5067 i == 2 ? " rtx" : (i - 2) % 8 == 0 ? ";\n rtx" : ",", i); 5068 printf (";\n"); 5069 } 5070 5071 /* Say that x1 is valid and the rest aren't. */ 5072 os->seen_vars.safe_grow_cleared (num_vars); 5073 os->seen_vars[1] = true; 5074 } 5075 if (os->type == SUBPATTERN || os->type == RECOG) 5076 printf (" int res ATTRIBUTE_UNUSED;\n"); 5077 else 5078 printf (" rtx_insn *res ATTRIBUTE_UNUSED;\n"); 5079 } 5080 5081 /* Output the definition of pattern routine ROUTINE. */ 5082 5083 static void 5084 print_pattern (output_state *os, pattern_routine *routine) 5085 { 5086 printf ("\nstatic int\npattern%d (", routine->pattern_id); 5087 const char *sep = ""; 5088 /* Add the top-level rtx parameter, if any. */ 5089 if (routine->pos) 5090 { 5091 printf ("%srtx x1", sep); 5092 sep = ", "; 5093 } 5094 /* Add the optional parameters. */ 5095 if (routine->insn_p) 5096 { 5097 /* We can't easily tell whether a C condition actually reads INSN, 5098 so add an ATTRIBUTE_UNUSED just in case. */ 5099 printf ("%srtx_insn *insn ATTRIBUTE_UNUSED", sep); 5100 sep = ", "; 5101 } 5102 if (routine->pnum_clobbers_p) 5103 { 5104 printf ("%sint *pnum_clobbers", sep); 5105 sep = ", "; 5106 } 5107 /* Add the "i" parameters. */ 5108 for (unsigned int i = 0; i < routine->param_types.length (); ++i) 5109 { 5110 printf ("%s%s i%d", sep, 5111 parameter_type_string (routine->param_types[i]), i + 1); 5112 sep = ", "; 5113 } 5114 printf (")\n"); 5115 os->type = SUBPATTERN; 5116 print_subroutine_start (os, routine->s, routine->pos); 5117 print_state (os, routine->s, 2, true); 5118 printf ("}\n"); 5119 } 5120 5121 /* Output a routine of type TYPE that implements S. PROC_ID is the 5122 number of the subroutine associated with S, or 0 if S is the main 5123 routine. */ 5124 5125 static void 5126 print_subroutine (output_state *os, state *s, int proc_id) 5127 { 5128 printf ("\n"); 5129 switch (os->type) 5130 { 5131 case SUBPATTERN: 5132 gcc_unreachable (); 5133 5134 case RECOG: 5135 if (proc_id) 5136 printf ("static int\nrecog_%d", proc_id); 5137 else 5138 printf ("int\nrecog"); 5139 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n" 5140 "\trtx_insn *insn ATTRIBUTE_UNUSED,\n" 5141 "\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n"); 5142 break; 5143 5144 case SPLIT: 5145 if (proc_id) 5146 printf ("static rtx_insn *\nsplit_%d", proc_id); 5147 else 5148 printf ("rtx_insn *\nsplit_insns"); 5149 printf (" (rtx x1 ATTRIBUTE_UNUSED, rtx_insn *insn ATTRIBUTE_UNUSED)\n"); 5150 break; 5151 5152 case PEEPHOLE2: 5153 if (proc_id) 5154 printf ("static rtx_insn *\npeephole2_%d", proc_id); 5155 else 5156 printf ("rtx_insn *\npeephole2_insns"); 5157 printf (" (rtx x1 ATTRIBUTE_UNUSED,\n" 5158 "\trtx_insn *insn ATTRIBUTE_UNUSED,\n" 5159 "\tint *pmatch_len_ ATTRIBUTE_UNUSED)\n"); 5160 break; 5161 } 5162 print_subroutine_start (os, s, &root_pos); 5163 if (proc_id == 0) 5164 { 5165 printf (" recog_data.insn = NULL;\n"); 5166 } 5167 print_state (os, s, 2, true); 5168 printf ("}\n"); 5169 } 5170 5171 /* Print out a routine of type TYPE that performs ROOT. */ 5172 5173 static void 5174 print_subroutine_group (output_state *os, routine_type type, state *root) 5175 { 5176 os->type = type; 5177 if (use_subroutines_p) 5178 { 5179 /* Split ROOT up into smaller pieces, both for readability and to 5180 help the compiler. */ 5181 auto_vec <state *> subroutines; 5182 find_subroutines (type, root, subroutines); 5183 5184 /* Output the subroutines (but not ROOT itself). */ 5185 unsigned int i; 5186 state *s; 5187 FOR_EACH_VEC_ELT (subroutines, i, s) 5188 print_subroutine (os, s, i + 1); 5189 } 5190 /* Output the main routine. */ 5191 print_subroutine (os, root, 0); 5192 } 5193 5194 /* Return the rtx pattern for the list of rtxes in a define_peephole2. */ 5195 5196 static rtx 5197 get_peephole2_pattern (md_rtx_info *info) 5198 { 5199 int i, j; 5200 rtvec vec = XVEC (info->def, 0); 5201 rtx pattern = rtx_alloc (SEQUENCE); 5202 XVEC (pattern, 0) = rtvec_alloc (GET_NUM_ELEM (vec)); 5203 for (i = j = 0; i < GET_NUM_ELEM (vec); i++) 5204 { 5205 rtx x = RTVEC_ELT (vec, i); 5206 /* Ignore scratch register requirements. */ 5207 if (GET_CODE (x) != MATCH_SCRATCH && GET_CODE (x) != MATCH_DUP) 5208 { 5209 XVECEXP (pattern, 0, j) = x; 5210 j++; 5211 } 5212 } 5213 XVECLEN (pattern, 0) = j; 5214 if (j == 0) 5215 error_at (info->loc, "empty define_peephole2"); 5216 return pattern; 5217 } 5218 5219 /* Return true if *PATTERN_PTR is a PARALLEL in which at least one trailing 5220 rtx can be added automatically by add_clobbers. If so, update 5221 *ACCEPTANCE_PTR so that its num_clobbers field contains the number 5222 of such trailing rtxes and update *PATTERN_PTR so that it contains 5223 the pattern without those rtxes. */ 5224 5225 static bool 5226 remove_clobbers (acceptance_type *acceptance_ptr, rtx *pattern_ptr) 5227 { 5228 int i; 5229 rtx new_pattern; 5230 5231 /* Find the last non-clobber in the parallel. */ 5232 rtx pattern = *pattern_ptr; 5233 for (i = XVECLEN (pattern, 0); i > 0; i--) 5234 { 5235 rtx x = XVECEXP (pattern, 0, i - 1); 5236 if (GET_CODE (x) != CLOBBER 5237 || (!REG_P (XEXP (x, 0)) 5238 && GET_CODE (XEXP (x, 0)) != MATCH_SCRATCH)) 5239 break; 5240 } 5241 5242 if (i == XVECLEN (pattern, 0)) 5243 return false; 5244 5245 /* Build a similar insn without the clobbers. */ 5246 if (i == 1) 5247 new_pattern = XVECEXP (pattern, 0, 0); 5248 else 5249 { 5250 new_pattern = rtx_alloc (PARALLEL); 5251 XVEC (new_pattern, 0) = rtvec_alloc (i); 5252 for (int j = 0; j < i; ++j) 5253 XVECEXP (new_pattern, 0, j) = XVECEXP (pattern, 0, j); 5254 } 5255 5256 /* Recognize it. */ 5257 acceptance_ptr->u.full.u.num_clobbers = XVECLEN (pattern, 0) - i; 5258 *pattern_ptr = new_pattern; 5259 return true; 5260 } 5261 5262 int 5263 main (int argc, const char **argv) 5264 { 5265 state insn_root, split_root, peephole2_root; 5266 5267 progname = "genrecog"; 5268 5269 if (!init_rtx_reader_args (argc, argv)) 5270 return (FATAL_EXIT_CODE); 5271 5272 write_header (); 5273 5274 /* Read the machine description. */ 5275 5276 md_rtx_info info; 5277 while (read_md_rtx (&info)) 5278 { 5279 rtx def = info.def; 5280 5281 acceptance_type acceptance; 5282 acceptance.partial_p = false; 5283 acceptance.u.full.code = info.index; 5284 5285 rtx pattern; 5286 switch (GET_CODE (def)) 5287 { 5288 case DEFINE_INSN: 5289 { 5290 /* Match the instruction in the original .md form. */ 5291 acceptance.type = RECOG; 5292 acceptance.u.full.u.num_clobbers = 0; 5293 pattern = add_implicit_parallel (XVEC (def, 1)); 5294 validate_pattern (pattern, &info, NULL_RTX, 0); 5295 match_pattern (&insn_root, &info, pattern, acceptance); 5296 5297 /* If the pattern is a PARALLEL with trailing CLOBBERs, 5298 allow recog_for_combine to match without the clobbers. */ 5299 if (GET_CODE (pattern) == PARALLEL 5300 && remove_clobbers (&acceptance, &pattern)) 5301 match_pattern (&insn_root, &info, pattern, acceptance); 5302 break; 5303 } 5304 5305 case DEFINE_SPLIT: 5306 acceptance.type = SPLIT; 5307 pattern = add_implicit_parallel (XVEC (def, 0)); 5308 validate_pattern (pattern, &info, NULL_RTX, 0); 5309 match_pattern (&split_root, &info, pattern, acceptance); 5310 5311 /* Declare the gen_split routine that we'll call if the 5312 pattern matches. The definition comes from insn-emit.c. */ 5313 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n", 5314 info.index); 5315 break; 5316 5317 case DEFINE_PEEPHOLE2: 5318 acceptance.type = PEEPHOLE2; 5319 pattern = get_peephole2_pattern (&info); 5320 validate_pattern (pattern, &info, NULL_RTX, 0); 5321 match_pattern (&peephole2_root, &info, pattern, acceptance); 5322 5323 /* Declare the gen_peephole2 routine that we'll call if the 5324 pattern matches. The definition comes from insn-emit.c. */ 5325 printf ("extern rtx_insn *gen_peephole2_%d (rtx_insn *, rtx *);\n", 5326 info.index); 5327 break; 5328 5329 default: 5330 /* do nothing */; 5331 } 5332 } 5333 5334 if (have_error) 5335 return FATAL_EXIT_CODE; 5336 5337 puts ("\n\n"); 5338 5339 /* Optimize each routine in turn. */ 5340 optimize_subroutine_group ("recog", &insn_root); 5341 optimize_subroutine_group ("split_insns", &split_root); 5342 optimize_subroutine_group ("peephole2_insns", &peephole2_root); 5343 5344 output_state os; 5345 os.id_to_var.safe_grow_cleared (num_positions); 5346 5347 if (use_pattern_routines_p) 5348 { 5349 /* Look for common patterns and split them out into subroutines. */ 5350 auto_vec <merge_state_info> states; 5351 states.safe_push (&insn_root); 5352 states.safe_push (&split_root); 5353 states.safe_push (&peephole2_root); 5354 split_out_patterns (states); 5355 5356 /* Print out the routines that we just created. */ 5357 unsigned int i; 5358 pattern_routine *routine; 5359 FOR_EACH_VEC_ELT (patterns, i, routine) 5360 print_pattern (&os, routine); 5361 } 5362 5363 /* Print out the matching routines. */ 5364 print_subroutine_group (&os, RECOG, &insn_root); 5365 print_subroutine_group (&os, SPLIT, &split_root); 5366 print_subroutine_group (&os, PEEPHOLE2, &peephole2_root); 5367 5368 fflush (stdout); 5369 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); 5370 } 5371