1 /* Generate code from machine description to recognize rtl as insns. 2 Copyright (C) 1987-2013 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 #include "bconfig.h" 52 #include "system.h" 53 #include "coretypes.h" 54 #include "tm.h" 55 #include "rtl.h" 56 #include "errors.h" 57 #include "read-md.h" 58 #include "gensupport.h" 59 60 #define OUTPUT_LABEL(INDENT_STRING, LABEL_NUMBER) \ 61 printf("%sL%d: ATTRIBUTE_UNUSED_LABEL\n", (INDENT_STRING), (LABEL_NUMBER)) 62 63 /* Ways of obtaining an rtx to be tested. */ 64 enum position_type { 65 /* PATTERN (peep2_next_insn (ARG)). */ 66 POS_PEEP2_INSN, 67 68 /* XEXP (BASE, ARG). */ 69 POS_XEXP, 70 71 /* XVECEXP (BASE, 0, ARG). */ 72 POS_XVECEXP0 73 }; 74 75 /* The position of an rtx relative to X0. Each useful position is 76 represented by exactly one instance of this structure. */ 77 struct position 78 { 79 /* The parent rtx. This is the root position for POS_PEEP2_INSNs. */ 80 struct position *base; 81 82 /* A position with the same BASE and TYPE, but with the next value 83 of ARG. */ 84 struct position *next; 85 86 /* A list of all POS_XEXP positions that use this one as their base, 87 chained by NEXT fields. The first entry represents XEXP (this, 0), 88 the second represents XEXP (this, 1), and so on. */ 89 struct position *xexps; 90 91 /* A list of POS_XVECEXP0 positions that use this one as their base, 92 chained by NEXT fields. The first entry represents XVECEXP (this, 0, 0), 93 the second represents XVECEXP (this, 0, 1), and so on. */ 94 struct position *xvecexp0s; 95 96 /* The type of position. */ 97 enum position_type type; 98 99 /* The argument to TYPE (shown as ARG in the position_type comments). */ 100 int arg; 101 102 /* The depth of this position, with 0 as the root. */ 103 int depth; 104 }; 105 106 /* A listhead of decision trees. The alternatives to a node are kept 107 in a doubly-linked list so we can easily add nodes to the proper 108 place when merging. */ 109 110 struct decision_head 111 { 112 struct decision *first; 113 struct decision *last; 114 }; 115 116 /* These types are roughly in the order in which we'd like to test them. */ 117 enum decision_type 118 { 119 DT_num_insns, 120 DT_mode, DT_code, DT_veclen, 121 DT_elt_zero_int, DT_elt_one_int, DT_elt_zero_wide, DT_elt_zero_wide_safe, 122 DT_const_int, 123 DT_veclen_ge, DT_dup, DT_pred, DT_c_test, 124 DT_accept_op, DT_accept_insn 125 }; 126 127 /* A single test. The two accept types aren't tests per-se, but 128 their equality (or lack thereof) does affect tree merging so 129 it is convenient to keep them here. */ 130 131 struct decision_test 132 { 133 /* A linked list through the tests attached to a node. */ 134 struct decision_test *next; 135 136 enum decision_type type; 137 138 union 139 { 140 int num_insns; /* Number if insn in a define_peephole2. */ 141 enum machine_mode mode; /* Machine mode of node. */ 142 RTX_CODE code; /* Code to test. */ 143 144 struct 145 { 146 const char *name; /* Predicate to call. */ 147 const struct pred_data *data; 148 /* Optimization hints for this predicate. */ 149 enum machine_mode mode; /* Machine mode for node. */ 150 } pred; 151 152 const char *c_test; /* Additional test to perform. */ 153 int veclen; /* Length of vector. */ 154 int dup; /* Number of operand to compare against. */ 155 HOST_WIDE_INT intval; /* Value for XINT for XWINT. */ 156 int opno; /* Operand number matched. */ 157 158 struct { 159 int code_number; /* Insn number matched. */ 160 int lineno; /* Line number of the insn. */ 161 int num_clobbers_to_add; /* Number of CLOBBERs to be added. */ 162 } insn; 163 } u; 164 }; 165 166 /* Data structure for decision tree for recognizing legitimate insns. */ 167 168 struct decision 169 { 170 struct decision_head success; /* Nodes to test on success. */ 171 struct decision *next; /* Node to test on failure. */ 172 struct decision *prev; /* Node whose failure tests us. */ 173 struct decision *afterward; /* Node to test on success, 174 but failure of successor nodes. */ 175 176 struct position *position; /* Position in pattern. */ 177 178 struct decision_test *tests; /* The tests for this node. */ 179 180 int number; /* Node number, used for labels */ 181 int subroutine_number; /* Number of subroutine this node starts */ 182 int need_label; /* Label needs to be output. */ 183 }; 184 185 #define SUBROUTINE_THRESHOLD 100 186 187 static int next_subroutine_number; 188 189 /* We can write three types of subroutines: One for insn recognition, 190 one to split insns, and one for peephole-type optimizations. This 191 defines which type is being written. */ 192 193 enum routine_type { 194 RECOG, SPLIT, PEEPHOLE2 195 }; 196 197 #define IS_SPLIT(X) ((X) != RECOG) 198 199 /* Next available node number for tree nodes. */ 200 201 static int next_number; 202 203 /* Next number to use as an insn_code. */ 204 205 static int next_insn_code; 206 207 /* Record the highest depth we ever have so we know how many variables to 208 allocate in each subroutine we make. */ 209 210 static int max_depth; 211 212 /* The line number of the start of the pattern currently being processed. */ 213 static int pattern_lineno; 214 215 /* The root position (x0). */ 216 static struct position root_pos; 217 218 /* A list of all POS_PEEP2_INSNs. The entry for insn 0 is the root position, 219 since we are given that instruction's pattern as x0. */ 220 static struct position *peep2_insn_pos_list = &root_pos; 221 222 extern void debug_decision 223 (struct decision *); 224 extern void debug_decision_list 225 (struct decision *); 226 227 /* Return a position with the given BASE, TYPE and ARG. NEXT_PTR 228 points to where the unique object that represents the position 229 should be stored. Create the object if it doesn't already exist, 230 otherwise reuse the object that is already there. */ 231 232 static struct position * 233 next_position (struct position **next_ptr, struct position *base, 234 enum position_type type, int arg) 235 { 236 struct position *pos; 237 238 pos = *next_ptr; 239 if (!pos) 240 { 241 pos = XCNEW (struct position); 242 pos->base = base; 243 pos->type = type; 244 pos->arg = arg; 245 pos->depth = base->depth + 1; 246 *next_ptr = pos; 247 } 248 return pos; 249 } 250 251 /* Compare positions POS1 and POS2 lexicographically. */ 252 253 static int 254 compare_positions (struct position *pos1, struct position *pos2) 255 { 256 int diff; 257 258 diff = pos1->depth - pos2->depth; 259 if (diff < 0) 260 do 261 pos2 = pos2->base; 262 while (pos1->depth != pos2->depth); 263 else if (diff > 0) 264 do 265 pos1 = pos1->base; 266 while (pos1->depth != pos2->depth); 267 while (pos1 != pos2) 268 { 269 diff = (int) pos1->type - (int) pos2->type; 270 if (diff == 0) 271 diff = pos1->arg - pos2->arg; 272 pos1 = pos1->base; 273 pos2 = pos2->base; 274 } 275 return diff; 276 } 277 278 /* Create a new node in sequence after LAST. */ 279 280 static struct decision * 281 new_decision (struct position *pos, struct decision_head *last) 282 { 283 struct decision *new_decision = XCNEW (struct decision); 284 285 new_decision->success = *last; 286 new_decision->position = pos; 287 new_decision->number = next_number++; 288 289 last->first = last->last = new_decision; 290 return new_decision; 291 } 292 293 /* Create a new test and link it in at PLACE. */ 294 295 static struct decision_test * 296 new_decision_test (enum decision_type type, struct decision_test ***pplace) 297 { 298 struct decision_test **place = *pplace; 299 struct decision_test *test; 300 301 test = XNEW (struct decision_test); 302 test->next = *place; 303 test->type = type; 304 *place = test; 305 306 place = &test->next; 307 *pplace = place; 308 309 return test; 310 } 311 312 /* Search for and return operand N, stop when reaching node STOP. */ 313 314 static rtx 315 find_operand (rtx pattern, int n, rtx stop) 316 { 317 const char *fmt; 318 RTX_CODE code; 319 int i, j, len; 320 rtx r; 321 322 if (pattern == stop) 323 return stop; 324 325 code = GET_CODE (pattern); 326 if ((code == MATCH_SCRATCH 327 || code == MATCH_OPERAND 328 || code == MATCH_OPERATOR 329 || code == MATCH_PARALLEL) 330 && XINT (pattern, 0) == n) 331 return pattern; 332 333 fmt = GET_RTX_FORMAT (code); 334 len = GET_RTX_LENGTH (code); 335 for (i = 0; i < len; i++) 336 { 337 switch (fmt[i]) 338 { 339 case 'e': case 'u': 340 if ((r = find_operand (XEXP (pattern, i), n, stop)) != NULL_RTX) 341 return r; 342 break; 343 344 case 'V': 345 if (! XVEC (pattern, i)) 346 break; 347 /* Fall through. */ 348 349 case 'E': 350 for (j = 0; j < XVECLEN (pattern, i); j++) 351 if ((r = find_operand (XVECEXP (pattern, i, j), n, stop)) 352 != NULL_RTX) 353 return r; 354 break; 355 356 case 'i': case 'w': case '0': case 's': 357 break; 358 359 default: 360 gcc_unreachable (); 361 } 362 } 363 364 return NULL; 365 } 366 367 /* Search for and return operand M, such that it has a matching 368 constraint for operand N. */ 369 370 static rtx 371 find_matching_operand (rtx pattern, int n) 372 { 373 const char *fmt; 374 RTX_CODE code; 375 int i, j, len; 376 rtx r; 377 378 code = GET_CODE (pattern); 379 if (code == MATCH_OPERAND 380 && (XSTR (pattern, 2)[0] == '0' + n 381 || (XSTR (pattern, 2)[0] == '%' 382 && XSTR (pattern, 2)[1] == '0' + n))) 383 return pattern; 384 385 fmt = GET_RTX_FORMAT (code); 386 len = GET_RTX_LENGTH (code); 387 for (i = 0; i < len; i++) 388 { 389 switch (fmt[i]) 390 { 391 case 'e': case 'u': 392 if ((r = find_matching_operand (XEXP (pattern, i), n))) 393 return r; 394 break; 395 396 case 'V': 397 if (! XVEC (pattern, i)) 398 break; 399 /* Fall through. */ 400 401 case 'E': 402 for (j = 0; j < XVECLEN (pattern, i); j++) 403 if ((r = find_matching_operand (XVECEXP (pattern, i, j), n))) 404 return r; 405 break; 406 407 case 'i': case 'w': case '0': case 's': 408 break; 409 410 default: 411 gcc_unreachable (); 412 } 413 } 414 415 return NULL; 416 } 417 418 419 /* Check for various errors in patterns. SET is nonnull for a destination, 420 and is the complete set pattern. SET_CODE is '=' for normal sets, and 421 '+' within a context that requires in-out constraints. */ 422 423 static void 424 validate_pattern (rtx pattern, rtx insn, rtx set, int set_code) 425 { 426 const char *fmt; 427 RTX_CODE code; 428 size_t i, len; 429 int j; 430 431 code = GET_CODE (pattern); 432 switch (code) 433 { 434 case MATCH_SCRATCH: 435 return; 436 case MATCH_DUP: 437 case MATCH_OP_DUP: 438 case MATCH_PAR_DUP: 439 if (find_operand (insn, XINT (pattern, 0), pattern) == pattern) 440 error_with_line (pattern_lineno, 441 "operand %i duplicated before defined", 442 XINT (pattern, 0)); 443 break; 444 case MATCH_OPERAND: 445 case MATCH_OPERATOR: 446 { 447 const char *pred_name = XSTR (pattern, 1); 448 const struct pred_data *pred; 449 const char *c_test; 450 451 if (GET_CODE (insn) == DEFINE_INSN) 452 c_test = XSTR (insn, 2); 453 else 454 c_test = XSTR (insn, 1); 455 456 if (pred_name[0] != 0) 457 { 458 pred = lookup_predicate (pred_name); 459 if (!pred) 460 message_with_line (pattern_lineno, 461 "warning: unknown predicate '%s'", 462 pred_name); 463 } 464 else 465 pred = 0; 466 467 if (code == MATCH_OPERAND) 468 { 469 const char constraints0 = XSTR (pattern, 2)[0]; 470 471 /* In DEFINE_EXPAND, DEFINE_SPLIT, and DEFINE_PEEPHOLE2, we 472 don't use the MATCH_OPERAND constraint, only the predicate. 473 This is confusing to folks doing new ports, so help them 474 not make the mistake. */ 475 if (GET_CODE (insn) == DEFINE_EXPAND 476 || GET_CODE (insn) == DEFINE_SPLIT 477 || GET_CODE (insn) == DEFINE_PEEPHOLE2) 478 { 479 if (constraints0) 480 message_with_line (pattern_lineno, 481 "warning: constraints not supported in %s", 482 rtx_name[GET_CODE (insn)]); 483 } 484 485 /* A MATCH_OPERAND that is a SET should have an output reload. */ 486 else if (set && constraints0) 487 { 488 if (set_code == '+') 489 { 490 if (constraints0 == '+') 491 ; 492 /* If we've only got an output reload for this operand, 493 we'd better have a matching input operand. */ 494 else if (constraints0 == '=' 495 && find_matching_operand (insn, XINT (pattern, 0))) 496 ; 497 else 498 error_with_line (pattern_lineno, 499 "operand %d missing in-out reload", 500 XINT (pattern, 0)); 501 } 502 else if (constraints0 != '=' && constraints0 != '+') 503 error_with_line (pattern_lineno, 504 "operand %d missing output reload", 505 XINT (pattern, 0)); 506 } 507 } 508 509 /* Allowing non-lvalues in destinations -- particularly CONST_INT -- 510 while not likely to occur at runtime, results in less efficient 511 code from insn-recog.c. */ 512 if (set && pred && pred->allows_non_lvalue) 513 message_with_line (pattern_lineno, 514 "warning: destination operand %d " 515 "allows non-lvalue", 516 XINT (pattern, 0)); 517 518 /* A modeless MATCH_OPERAND can be handy when we can check for 519 multiple modes in the c_test. In most other cases, it is a 520 mistake. Only DEFINE_INSN is eligible, since SPLIT and 521 PEEP2 can FAIL within the output pattern. Exclude special 522 predicates, which check the mode themselves. Also exclude 523 predicates that allow only constants. Exclude the SET_DEST 524 of a call instruction, as that is a common idiom. */ 525 526 if (GET_MODE (pattern) == VOIDmode 527 && code == MATCH_OPERAND 528 && GET_CODE (insn) == DEFINE_INSN 529 && pred 530 && !pred->special 531 && pred->allows_non_const 532 && strstr (c_test, "operands") == NULL 533 && ! (set 534 && GET_CODE (set) == SET 535 && GET_CODE (SET_SRC (set)) == CALL)) 536 message_with_line (pattern_lineno, 537 "warning: operand %d missing mode?", 538 XINT (pattern, 0)); 539 return; 540 } 541 542 case SET: 543 { 544 enum machine_mode dmode, smode; 545 rtx dest, src; 546 547 dest = SET_DEST (pattern); 548 src = SET_SRC (pattern); 549 550 /* STRICT_LOW_PART is a wrapper. Its argument is the real 551 destination, and it's mode should match the source. */ 552 if (GET_CODE (dest) == STRICT_LOW_PART) 553 dest = XEXP (dest, 0); 554 555 /* Find the referent for a DUP. */ 556 557 if (GET_CODE (dest) == MATCH_DUP 558 || GET_CODE (dest) == MATCH_OP_DUP 559 || GET_CODE (dest) == MATCH_PAR_DUP) 560 dest = find_operand (insn, XINT (dest, 0), NULL); 561 562 if (GET_CODE (src) == MATCH_DUP 563 || GET_CODE (src) == MATCH_OP_DUP 564 || GET_CODE (src) == MATCH_PAR_DUP) 565 src = find_operand (insn, XINT (src, 0), NULL); 566 567 dmode = GET_MODE (dest); 568 smode = GET_MODE (src); 569 570 /* The mode of an ADDRESS_OPERAND is the mode of the memory 571 reference, not the mode of the address. */ 572 if (GET_CODE (src) == MATCH_OPERAND 573 && ! strcmp (XSTR (src, 1), "address_operand")) 574 ; 575 576 /* The operands of a SET must have the same mode unless one 577 is VOIDmode. */ 578 else if (dmode != VOIDmode && smode != VOIDmode && dmode != smode) 579 error_with_line (pattern_lineno, 580 "mode mismatch in set: %smode vs %smode", 581 GET_MODE_NAME (dmode), GET_MODE_NAME (smode)); 582 583 /* If only one of the operands is VOIDmode, and PC or CC0 is 584 not involved, it's probably a mistake. */ 585 else if (dmode != smode 586 && GET_CODE (dest) != PC 587 && GET_CODE (dest) != CC0 588 && GET_CODE (src) != PC 589 && GET_CODE (src) != CC0 590 && !CONST_INT_P (src) 591 && GET_CODE (src) != CALL) 592 { 593 const char *which; 594 which = (dmode == VOIDmode ? "destination" : "source"); 595 message_with_line (pattern_lineno, 596 "warning: %s missing a mode?", which); 597 } 598 599 if (dest != SET_DEST (pattern)) 600 validate_pattern (dest, insn, pattern, '='); 601 validate_pattern (SET_DEST (pattern), insn, pattern, '='); 602 validate_pattern (SET_SRC (pattern), insn, NULL_RTX, 0); 603 return; 604 } 605 606 case CLOBBER: 607 validate_pattern (SET_DEST (pattern), insn, pattern, '='); 608 return; 609 610 case ZERO_EXTRACT: 611 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0); 612 validate_pattern (XEXP (pattern, 1), insn, NULL_RTX, 0); 613 validate_pattern (XEXP (pattern, 2), insn, NULL_RTX, 0); 614 return; 615 616 case STRICT_LOW_PART: 617 validate_pattern (XEXP (pattern, 0), insn, set, set ? '+' : 0); 618 return; 619 620 case LABEL_REF: 621 if (GET_MODE (XEXP (pattern, 0)) != VOIDmode) 622 error_with_line (pattern_lineno, 623 "operand to label_ref %smode not VOIDmode", 624 GET_MODE_NAME (GET_MODE (XEXP (pattern, 0)))); 625 break; 626 627 default: 628 break; 629 } 630 631 fmt = GET_RTX_FORMAT (code); 632 len = GET_RTX_LENGTH (code); 633 for (i = 0; i < len; i++) 634 { 635 switch (fmt[i]) 636 { 637 case 'e': case 'u': 638 validate_pattern (XEXP (pattern, i), insn, NULL_RTX, 0); 639 break; 640 641 case 'E': 642 for (j = 0; j < XVECLEN (pattern, i); j++) 643 validate_pattern (XVECEXP (pattern, i, j), insn, NULL_RTX, 0); 644 break; 645 646 case 'i': case 'w': case '0': case 's': 647 break; 648 649 default: 650 gcc_unreachable (); 651 } 652 } 653 } 654 655 /* Create a chain of nodes to verify that an rtl expression matches 656 PATTERN. 657 658 LAST is a pointer to the listhead in the previous node in the chain (or 659 in the calling function, for the first node). 660 661 POSITION is the current position in the insn. 662 663 INSN_TYPE is the type of insn for which we are emitting code. 664 665 A pointer to the final node in the chain is returned. */ 666 667 static struct decision * 668 add_to_sequence (rtx pattern, struct decision_head *last, 669 struct position *pos, enum routine_type insn_type, int top) 670 { 671 RTX_CODE code; 672 struct decision *this_decision, *sub; 673 struct decision_test *test; 674 struct decision_test **place; 675 struct position *subpos, **subpos_ptr; 676 size_t i; 677 const char *fmt; 678 int len; 679 enum machine_mode mode; 680 enum position_type pos_type; 681 682 if (pos->depth > max_depth) 683 max_depth = pos->depth; 684 685 sub = this_decision = new_decision (pos, last); 686 place = &this_decision->tests; 687 688 mode = GET_MODE (pattern); 689 code = GET_CODE (pattern); 690 691 switch (code) 692 { 693 case PARALLEL: 694 /* Toplevel peephole pattern. */ 695 if (insn_type == PEEPHOLE2 && top) 696 { 697 int num_insns; 698 699 /* Check we have sufficient insns. This avoids complications 700 because we then know peep2_next_insn never fails. */ 701 num_insns = XVECLEN (pattern, 0); 702 if (num_insns > 1) 703 { 704 test = new_decision_test (DT_num_insns, &place); 705 test->u.num_insns = num_insns; 706 last = &sub->success; 707 } 708 else 709 { 710 /* We don't need the node we just created -- unlink it. */ 711 last->first = last->last = NULL; 712 } 713 714 subpos_ptr = &peep2_insn_pos_list; 715 for (i = 0; i < (size_t) XVECLEN (pattern, 0); i++) 716 { 717 subpos = next_position (subpos_ptr, &root_pos, 718 POS_PEEP2_INSN, i); 719 sub = add_to_sequence (XVECEXP (pattern, 0, i), 720 last, subpos, insn_type, 0); 721 last = &sub->success; 722 subpos_ptr = &subpos->next; 723 } 724 goto ret; 725 } 726 727 /* Else nothing special. */ 728 break; 729 730 case MATCH_PARALLEL: 731 /* The explicit patterns within a match_parallel enforce a minimum 732 length on the vector. The match_parallel predicate may allow 733 for more elements. We do need to check for this minimum here 734 or the code generated to match the internals may reference data 735 beyond the end of the vector. */ 736 test = new_decision_test (DT_veclen_ge, &place); 737 test->u.veclen = XVECLEN (pattern, 2); 738 /* Fall through. */ 739 740 case MATCH_OPERAND: 741 case MATCH_SCRATCH: 742 case MATCH_OPERATOR: 743 { 744 RTX_CODE was_code = code; 745 const char *pred_name; 746 bool allows_const_int = true; 747 748 if (code == MATCH_SCRATCH) 749 { 750 pred_name = "scratch_operand"; 751 code = UNKNOWN; 752 } 753 else 754 { 755 pred_name = XSTR (pattern, 1); 756 if (code == MATCH_PARALLEL) 757 code = PARALLEL; 758 else 759 code = UNKNOWN; 760 } 761 762 if (pred_name[0] != 0) 763 { 764 const struct pred_data *pred; 765 766 test = new_decision_test (DT_pred, &place); 767 test->u.pred.name = pred_name; 768 test->u.pred.mode = mode; 769 770 /* See if we know about this predicate. 771 If we do, remember it for use below. 772 773 We can optimize the generated code a little if either 774 (a) the predicate only accepts one code, or (b) the 775 predicate does not allow CONST_INT, in which case it 776 can match only if the modes match. */ 777 pred = lookup_predicate (pred_name); 778 if (pred) 779 { 780 test->u.pred.data = pred; 781 allows_const_int = pred->codes[CONST_INT]; 782 if (was_code == MATCH_PARALLEL 783 && pred->singleton != PARALLEL) 784 message_with_line (pattern_lineno, 785 "predicate '%s' used in match_parallel " 786 "does not allow only PARALLEL", pred->name); 787 else 788 code = pred->singleton; 789 } 790 else 791 message_with_line (pattern_lineno, 792 "warning: unknown predicate '%s' in '%s' expression", 793 pred_name, GET_RTX_NAME (was_code)); 794 } 795 796 /* Can't enforce a mode if we allow const_int. */ 797 if (allows_const_int) 798 mode = VOIDmode; 799 800 /* Accept the operand, i.e. record it in `operands'. */ 801 test = new_decision_test (DT_accept_op, &place); 802 test->u.opno = XINT (pattern, 0); 803 804 if (was_code == MATCH_OPERATOR || was_code == MATCH_PARALLEL) 805 { 806 if (was_code == MATCH_OPERATOR) 807 { 808 pos_type = POS_XEXP; 809 subpos_ptr = &pos->xexps; 810 } 811 else 812 { 813 pos_type = POS_XVECEXP0; 814 subpos_ptr = &pos->xvecexp0s; 815 } 816 for (i = 0; i < (size_t) XVECLEN (pattern, 2); i++) 817 { 818 subpos = next_position (subpos_ptr, pos, pos_type, i); 819 sub = add_to_sequence (XVECEXP (pattern, 2, i), 820 &sub->success, subpos, insn_type, 0); 821 subpos_ptr = &subpos->next; 822 } 823 } 824 goto fini; 825 } 826 827 case MATCH_OP_DUP: 828 code = UNKNOWN; 829 830 test = new_decision_test (DT_dup, &place); 831 test->u.dup = XINT (pattern, 0); 832 833 test = new_decision_test (DT_accept_op, &place); 834 test->u.opno = XINT (pattern, 0); 835 836 subpos_ptr = &pos->xexps; 837 for (i = 0; i < (size_t) XVECLEN (pattern, 1); i++) 838 { 839 subpos = next_position (subpos_ptr, pos, POS_XEXP, i); 840 sub = add_to_sequence (XVECEXP (pattern, 1, i), 841 &sub->success, subpos, insn_type, 0); 842 subpos_ptr = &subpos->next; 843 } 844 goto fini; 845 846 case MATCH_DUP: 847 case MATCH_PAR_DUP: 848 code = UNKNOWN; 849 850 test = new_decision_test (DT_dup, &place); 851 test->u.dup = XINT (pattern, 0); 852 goto fini; 853 854 default: 855 break; 856 } 857 858 fmt = GET_RTX_FORMAT (code); 859 len = GET_RTX_LENGTH (code); 860 861 /* Do tests against the current node first. */ 862 for (i = 0; i < (size_t) len; i++) 863 { 864 if (fmt[i] == 'i') 865 { 866 gcc_assert (i < 2); 867 868 if (!i) 869 { 870 test = new_decision_test (DT_elt_zero_int, &place); 871 test->u.intval = XINT (pattern, i); 872 } 873 else 874 { 875 test = new_decision_test (DT_elt_one_int, &place); 876 test->u.intval = XINT (pattern, i); 877 } 878 } 879 else if (fmt[i] == 'w') 880 { 881 /* If this value actually fits in an int, we can use a switch 882 statement here, so indicate that. */ 883 enum decision_type type 884 = ((int) XWINT (pattern, i) == XWINT (pattern, i)) 885 ? DT_elt_zero_wide_safe : DT_elt_zero_wide; 886 887 gcc_assert (!i); 888 889 test = new_decision_test (type, &place); 890 test->u.intval = XWINT (pattern, i); 891 } 892 else if (fmt[i] == 'E') 893 { 894 gcc_assert (!i); 895 896 test = new_decision_test (DT_veclen, &place); 897 test->u.veclen = XVECLEN (pattern, i); 898 } 899 } 900 901 /* Now test our sub-patterns. */ 902 subpos_ptr = &pos->xexps; 903 for (i = 0; i < (size_t) len; i++) 904 { 905 subpos = next_position (subpos_ptr, pos, POS_XEXP, i); 906 switch (fmt[i]) 907 { 908 case 'e': case 'u': 909 sub = add_to_sequence (XEXP (pattern, i), &sub->success, 910 subpos, insn_type, 0); 911 break; 912 913 case 'E': 914 { 915 struct position *subpos2, **subpos2_ptr; 916 int j; 917 918 subpos2_ptr = &pos->xvecexp0s; 919 for (j = 0; j < XVECLEN (pattern, i); j++) 920 { 921 subpos2 = next_position (subpos2_ptr, pos, POS_XVECEXP0, j); 922 sub = add_to_sequence (XVECEXP (pattern, i, j), 923 &sub->success, subpos2, insn_type, 0); 924 subpos2_ptr = &subpos2->next; 925 } 926 break; 927 } 928 929 case 'i': case 'w': 930 /* Handled above. */ 931 break; 932 case '0': 933 break; 934 935 default: 936 gcc_unreachable (); 937 } 938 subpos_ptr = &subpos->next; 939 } 940 941 fini: 942 /* Insert nodes testing mode and code, if they're still relevant, 943 before any of the nodes we may have added above. */ 944 if (code != UNKNOWN) 945 { 946 place = &this_decision->tests; 947 test = new_decision_test (DT_code, &place); 948 test->u.code = code; 949 } 950 951 if (mode != VOIDmode) 952 { 953 place = &this_decision->tests; 954 test = new_decision_test (DT_mode, &place); 955 test->u.mode = mode; 956 } 957 958 /* If we didn't insert any tests or accept nodes, hork. */ 959 gcc_assert (this_decision->tests); 960 961 ret: 962 return sub; 963 } 964 965 /* A subroutine of maybe_both_true; examines only one test. 966 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */ 967 968 static int 969 maybe_both_true_2 (struct decision_test *d1, struct decision_test *d2) 970 { 971 if (d1->type == d2->type) 972 { 973 switch (d1->type) 974 { 975 case DT_num_insns: 976 if (d1->u.num_insns == d2->u.num_insns) 977 return 1; 978 else 979 return -1; 980 981 case DT_mode: 982 return d1->u.mode == d2->u.mode; 983 984 case DT_code: 985 return d1->u.code == d2->u.code; 986 987 case DT_veclen: 988 return d1->u.veclen == d2->u.veclen; 989 990 case DT_elt_zero_int: 991 case DT_elt_one_int: 992 case DT_elt_zero_wide: 993 case DT_elt_zero_wide_safe: 994 return d1->u.intval == d2->u.intval; 995 996 default: 997 break; 998 } 999 } 1000 1001 /* If either has a predicate that we know something about, set 1002 things up so that D1 is the one that always has a known 1003 predicate. Then see if they have any codes in common. */ 1004 1005 if (d1->type == DT_pred || d2->type == DT_pred) 1006 { 1007 if (d2->type == DT_pred) 1008 { 1009 struct decision_test *tmp; 1010 tmp = d1, d1 = d2, d2 = tmp; 1011 } 1012 1013 /* If D2 tests a mode, see if it matches D1. */ 1014 if (d1->u.pred.mode != VOIDmode) 1015 { 1016 if (d2->type == DT_mode) 1017 { 1018 if (d1->u.pred.mode != d2->u.mode 1019 /* The mode of an address_operand predicate is the 1020 mode of the memory, not the operand. It can only 1021 be used for testing the predicate, so we must 1022 ignore it here. */ 1023 && strcmp (d1->u.pred.name, "address_operand") != 0) 1024 return 0; 1025 } 1026 /* Don't check two predicate modes here, because if both predicates 1027 accept CONST_INT, then both can still be true even if the modes 1028 are different. If they don't accept CONST_INT, there will be a 1029 separate DT_mode that will make maybe_both_true_1 return 0. */ 1030 } 1031 1032 if (d1->u.pred.data) 1033 { 1034 /* If D2 tests a code, see if it is in the list of valid 1035 codes for D1's predicate. */ 1036 if (d2->type == DT_code) 1037 { 1038 if (!d1->u.pred.data->codes[d2->u.code]) 1039 return 0; 1040 } 1041 1042 /* Otherwise see if the predicates have any codes in common. */ 1043 else if (d2->type == DT_pred && d2->u.pred.data) 1044 { 1045 bool common = false; 1046 int c; 1047 1048 for (c = 0; c < NUM_RTX_CODE; c++) 1049 if (d1->u.pred.data->codes[c] && d2->u.pred.data->codes[c]) 1050 { 1051 common = true; 1052 break; 1053 } 1054 1055 if (!common) 1056 return 0; 1057 } 1058 } 1059 } 1060 1061 /* Tests vs veclen may be known when strict equality is involved. */ 1062 if (d1->type == DT_veclen && d2->type == DT_veclen_ge) 1063 return d1->u.veclen >= d2->u.veclen; 1064 if (d1->type == DT_veclen_ge && d2->type == DT_veclen) 1065 return d2->u.veclen >= d1->u.veclen; 1066 1067 return -1; 1068 } 1069 1070 /* A subroutine of maybe_both_true; examines all the tests for a given node. 1071 Returns > 0 for "definitely both true" and < 0 for "maybe both true". */ 1072 1073 static int 1074 maybe_both_true_1 (struct decision_test *d1, struct decision_test *d2) 1075 { 1076 struct decision_test *t1, *t2; 1077 1078 /* A match_operand with no predicate can match anything. Recognize 1079 this by the existence of a lone DT_accept_op test. */ 1080 if (d1->type == DT_accept_op || d2->type == DT_accept_op) 1081 return 1; 1082 1083 /* Eliminate pairs of tests while they can exactly match. */ 1084 while (d1 && d2 && d1->type == d2->type) 1085 { 1086 if (maybe_both_true_2 (d1, d2) == 0) 1087 return 0; 1088 d1 = d1->next, d2 = d2->next; 1089 } 1090 1091 /* After that, consider all pairs. */ 1092 for (t1 = d1; t1 ; t1 = t1->next) 1093 for (t2 = d2; t2 ; t2 = t2->next) 1094 if (maybe_both_true_2 (t1, t2) == 0) 1095 return 0; 1096 1097 return -1; 1098 } 1099 1100 /* Return 0 if we can prove that there is no RTL that can match both 1101 D1 and D2. Otherwise, return 1 (it may be that there is an RTL that 1102 can match both or just that we couldn't prove there wasn't such an RTL). 1103 1104 TOPLEVEL is nonzero if we are to only look at the top level and not 1105 recursively descend. */ 1106 1107 static int 1108 maybe_both_true (struct decision *d1, struct decision *d2, 1109 int toplevel) 1110 { 1111 struct decision *p1, *p2; 1112 int cmp; 1113 1114 /* Don't compare strings on the different positions in insn. Doing so 1115 is incorrect and results in false matches from constructs like 1116 1117 [(set (subreg:HI (match_operand:SI "register_operand" "r") 0) 1118 (subreg:HI (match_operand:SI "register_operand" "r") 0))] 1119 vs 1120 [(set (match_operand:HI "register_operand" "r") 1121 (match_operand:HI "register_operand" "r"))] 1122 1123 If we are presented with such, we are recursing through the remainder 1124 of a node's success nodes (from the loop at the end of this function). 1125 Skip forward until we come to a position that matches. 1126 1127 Due to the way positions are constructed, we know that iterating 1128 forward from the lexically lower position will run into the lexically 1129 higher position and not the other way around. This saves a bit 1130 of effort. */ 1131 1132 cmp = compare_positions (d1->position, d2->position); 1133 if (cmp != 0) 1134 { 1135 gcc_assert (!toplevel); 1136 1137 /* If the d2->position was lexically lower, swap. */ 1138 if (cmp > 0) 1139 p1 = d1, d1 = d2, d2 = p1; 1140 1141 if (d1->success.first == 0) 1142 return 1; 1143 for (p1 = d1->success.first; p1; p1 = p1->next) 1144 if (maybe_both_true (p1, d2, 0)) 1145 return 1; 1146 1147 return 0; 1148 } 1149 1150 /* Test the current level. */ 1151 cmp = maybe_both_true_1 (d1->tests, d2->tests); 1152 if (cmp >= 0) 1153 return cmp; 1154 1155 /* We can't prove that D1 and D2 cannot both be true. If we are only 1156 to check the top level, return 1. Otherwise, see if we can prove 1157 that all choices in both successors are mutually exclusive. If 1158 either does not have any successors, we can't prove they can't both 1159 be true. */ 1160 1161 if (toplevel || d1->success.first == 0 || d2->success.first == 0) 1162 return 1; 1163 1164 for (p1 = d1->success.first; p1; p1 = p1->next) 1165 for (p2 = d2->success.first; p2; p2 = p2->next) 1166 if (maybe_both_true (p1, p2, 0)) 1167 return 1; 1168 1169 return 0; 1170 } 1171 1172 /* A subroutine of nodes_identical. Examine two tests for equivalence. */ 1173 1174 static int 1175 nodes_identical_1 (struct decision_test *d1, struct decision_test *d2) 1176 { 1177 switch (d1->type) 1178 { 1179 case DT_num_insns: 1180 return d1->u.num_insns == d2->u.num_insns; 1181 1182 case DT_mode: 1183 return d1->u.mode == d2->u.mode; 1184 1185 case DT_code: 1186 return d1->u.code == d2->u.code; 1187 1188 case DT_pred: 1189 return (d1->u.pred.mode == d2->u.pred.mode 1190 && strcmp (d1->u.pred.name, d2->u.pred.name) == 0); 1191 1192 case DT_c_test: 1193 return strcmp (d1->u.c_test, d2->u.c_test) == 0; 1194 1195 case DT_veclen: 1196 case DT_veclen_ge: 1197 return d1->u.veclen == d2->u.veclen; 1198 1199 case DT_dup: 1200 return d1->u.dup == d2->u.dup; 1201 1202 case DT_elt_zero_int: 1203 case DT_elt_one_int: 1204 case DT_elt_zero_wide: 1205 case DT_elt_zero_wide_safe: 1206 return d1->u.intval == d2->u.intval; 1207 1208 case DT_accept_op: 1209 return d1->u.opno == d2->u.opno; 1210 1211 case DT_accept_insn: 1212 /* Differences will be handled in merge_accept_insn. */ 1213 return 1; 1214 1215 default: 1216 gcc_unreachable (); 1217 } 1218 } 1219 1220 /* True iff the two nodes are identical (on one level only). Due 1221 to the way these lists are constructed, we shouldn't have to 1222 consider different orderings on the tests. */ 1223 1224 static int 1225 nodes_identical (struct decision *d1, struct decision *d2) 1226 { 1227 struct decision_test *t1, *t2; 1228 1229 for (t1 = d1->tests, t2 = d2->tests; t1 && t2; t1 = t1->next, t2 = t2->next) 1230 { 1231 if (t1->type != t2->type) 1232 return 0; 1233 if (! nodes_identical_1 (t1, t2)) 1234 return 0; 1235 } 1236 1237 /* For success, they should now both be null. */ 1238 if (t1 != t2) 1239 return 0; 1240 1241 /* Check that their subnodes are at the same position, as any one set 1242 of sibling decisions must be at the same position. Allowing this 1243 requires complications to find_afterward and when change_state is 1244 invoked. */ 1245 if (d1->success.first 1246 && d2->success.first 1247 && d1->success.first->position != d2->success.first->position) 1248 return 0; 1249 1250 return 1; 1251 } 1252 1253 /* A subroutine of merge_trees; given two nodes that have been declared 1254 identical, cope with two insn accept states. If they differ in the 1255 number of clobbers, then the conflict was created by make_insn_sequence 1256 and we can drop the with-clobbers version on the floor. If both 1257 nodes have no additional clobbers, we have found an ambiguity in the 1258 source machine description. */ 1259 1260 static void 1261 merge_accept_insn (struct decision *oldd, struct decision *addd) 1262 { 1263 struct decision_test *old, *add; 1264 1265 for (old = oldd->tests; old; old = old->next) 1266 if (old->type == DT_accept_insn) 1267 break; 1268 if (old == NULL) 1269 return; 1270 1271 for (add = addd->tests; add; add = add->next) 1272 if (add->type == DT_accept_insn) 1273 break; 1274 if (add == NULL) 1275 return; 1276 1277 /* If one node is for a normal insn and the second is for the base 1278 insn with clobbers stripped off, the second node should be ignored. */ 1279 1280 if (old->u.insn.num_clobbers_to_add == 0 1281 && add->u.insn.num_clobbers_to_add > 0) 1282 { 1283 /* Nothing to do here. */ 1284 } 1285 else if (old->u.insn.num_clobbers_to_add > 0 1286 && add->u.insn.num_clobbers_to_add == 0) 1287 { 1288 /* In this case, replace OLD with ADD. */ 1289 old->u.insn = add->u.insn; 1290 } 1291 else 1292 { 1293 error_with_line (add->u.insn.lineno, "`%s' matches `%s'", 1294 get_insn_name (add->u.insn.code_number), 1295 get_insn_name (old->u.insn.code_number)); 1296 message_with_line (old->u.insn.lineno, "previous definition of `%s'", 1297 get_insn_name (old->u.insn.code_number)); 1298 } 1299 } 1300 1301 /* Merge two decision trees OLDH and ADDH, modifying OLDH destructively. */ 1302 1303 static void 1304 merge_trees (struct decision_head *oldh, struct decision_head *addh) 1305 { 1306 struct decision *next, *add; 1307 1308 if (addh->first == 0) 1309 return; 1310 if (oldh->first == 0) 1311 { 1312 *oldh = *addh; 1313 return; 1314 } 1315 1316 /* Trying to merge bits at different positions isn't possible. */ 1317 gcc_assert (oldh->first->position == addh->first->position); 1318 1319 for (add = addh->first; add ; add = next) 1320 { 1321 struct decision *old, *insert_before = NULL; 1322 1323 next = add->next; 1324 1325 /* The semantics of pattern matching state that the tests are 1326 done in the order given in the MD file so that if an insn 1327 matches two patterns, the first one will be used. However, 1328 in practice, most, if not all, patterns are unambiguous so 1329 that their order is independent. In that case, we can merge 1330 identical tests and group all similar modes and codes together. 1331 1332 Scan starting from the end of OLDH until we reach a point 1333 where we reach the head of the list or where we pass a 1334 pattern that could also be true if NEW is true. If we find 1335 an identical pattern, we can merge them. Also, record the 1336 last node that tests the same code and mode and the last one 1337 that tests just the same mode. 1338 1339 If we have no match, place NEW after the closest match we found. */ 1340 1341 for (old = oldh->last; old; old = old->prev) 1342 { 1343 if (nodes_identical (old, add)) 1344 { 1345 merge_accept_insn (old, add); 1346 merge_trees (&old->success, &add->success); 1347 goto merged_nodes; 1348 } 1349 1350 if (maybe_both_true (old, add, 0)) 1351 break; 1352 1353 /* Insert the nodes in DT test type order, which is roughly 1354 how expensive/important the test is. Given that the tests 1355 are also ordered within the list, examining the first is 1356 sufficient. */ 1357 if ((int) add->tests->type < (int) old->tests->type) 1358 insert_before = old; 1359 } 1360 1361 if (insert_before == NULL) 1362 { 1363 add->next = NULL; 1364 add->prev = oldh->last; 1365 oldh->last->next = add; 1366 oldh->last = add; 1367 } 1368 else 1369 { 1370 if ((add->prev = insert_before->prev) != NULL) 1371 add->prev->next = add; 1372 else 1373 oldh->first = add; 1374 add->next = insert_before; 1375 insert_before->prev = add; 1376 } 1377 1378 merged_nodes:; 1379 } 1380 } 1381 1382 /* Walk the tree looking for sub-nodes that perform common tests. 1383 Factor out the common test into a new node. This enables us 1384 (depending on the test type) to emit switch statements later. */ 1385 1386 static void 1387 factor_tests (struct decision_head *head) 1388 { 1389 struct decision *first, *next; 1390 1391 for (first = head->first; first && first->next; first = next) 1392 { 1393 enum decision_type type; 1394 struct decision *new_dec, *old_last; 1395 1396 type = first->tests->type; 1397 next = first->next; 1398 1399 /* Want at least two compatible sequential nodes. */ 1400 if (next->tests->type != type) 1401 continue; 1402 1403 /* Don't want all node types, just those we can turn into 1404 switch statements. */ 1405 if (type != DT_mode 1406 && type != DT_code 1407 && type != DT_veclen 1408 && type != DT_elt_zero_int 1409 && type != DT_elt_one_int 1410 && type != DT_elt_zero_wide_safe) 1411 continue; 1412 1413 /* If we'd been performing more than one test, create a new node 1414 below our first test. */ 1415 if (first->tests->next != NULL) 1416 { 1417 new_dec = new_decision (first->position, &first->success); 1418 new_dec->tests = first->tests->next; 1419 first->tests->next = NULL; 1420 } 1421 1422 /* Crop the node tree off after our first test. */ 1423 first->next = NULL; 1424 old_last = head->last; 1425 head->last = first; 1426 1427 /* For each compatible test, adjust to perform only one test in 1428 the top level node, then merge the node back into the tree. */ 1429 do 1430 { 1431 struct decision_head h; 1432 1433 if (next->tests->next != NULL) 1434 { 1435 new_dec = new_decision (next->position, &next->success); 1436 new_dec->tests = next->tests->next; 1437 next->tests->next = NULL; 1438 } 1439 new_dec = next; 1440 next = next->next; 1441 new_dec->next = NULL; 1442 h.first = h.last = new_dec; 1443 1444 merge_trees (head, &h); 1445 } 1446 while (next && next->tests->type == type); 1447 1448 /* After we run out of compatible tests, graft the remaining nodes 1449 back onto the tree. */ 1450 if (next) 1451 { 1452 next->prev = head->last; 1453 head->last->next = next; 1454 head->last = old_last; 1455 } 1456 } 1457 1458 /* Recurse. */ 1459 for (first = head->first; first; first = first->next) 1460 factor_tests (&first->success); 1461 } 1462 1463 /* After factoring, try to simplify the tests on any one node. 1464 Tests that are useful for switch statements are recognizable 1465 by having only a single test on a node -- we'll be manipulating 1466 nodes with multiple tests: 1467 1468 If we have mode tests or code tests that are redundant with 1469 predicates, remove them. */ 1470 1471 static void 1472 simplify_tests (struct decision_head *head) 1473 { 1474 struct decision *tree; 1475 1476 for (tree = head->first; tree; tree = tree->next) 1477 { 1478 struct decision_test *a, *b; 1479 1480 a = tree->tests; 1481 b = a->next; 1482 if (b == NULL) 1483 continue; 1484 1485 /* Find a predicate node. */ 1486 while (b && b->type != DT_pred) 1487 b = b->next; 1488 if (b) 1489 { 1490 /* Due to how these tests are constructed, we don't even need 1491 to check that the mode and code are compatible -- they were 1492 generated from the predicate in the first place. */ 1493 while (a->type == DT_mode || a->type == DT_code) 1494 a = a->next; 1495 tree->tests = a; 1496 } 1497 } 1498 1499 /* Recurse. */ 1500 for (tree = head->first; tree; tree = tree->next) 1501 simplify_tests (&tree->success); 1502 } 1503 1504 /* Count the number of subnodes of HEAD. If the number is high enough, 1505 make the first node in HEAD start a separate subroutine in the C code 1506 that is generated. */ 1507 1508 static int 1509 break_out_subroutines (struct decision_head *head, int initial) 1510 { 1511 int size = 0; 1512 struct decision *sub; 1513 1514 for (sub = head->first; sub; sub = sub->next) 1515 size += 1 + break_out_subroutines (&sub->success, 0); 1516 1517 if (size > SUBROUTINE_THRESHOLD && ! initial) 1518 { 1519 head->first->subroutine_number = ++next_subroutine_number; 1520 size = 1; 1521 } 1522 return size; 1523 } 1524 1525 /* For each node p, find the next alternative that might be true 1526 when p is true. */ 1527 1528 static void 1529 find_afterward (struct decision_head *head, struct decision *real_afterward) 1530 { 1531 struct decision *p, *q, *afterward; 1532 1533 /* We can't propagate alternatives across subroutine boundaries. 1534 This is not incorrect, merely a minor optimization loss. */ 1535 1536 p = head->first; 1537 afterward = (p->subroutine_number > 0 ? NULL : real_afterward); 1538 1539 for ( ; p ; p = p->next) 1540 { 1541 /* Find the next node that might be true if this one fails. */ 1542 for (q = p->next; q ; q = q->next) 1543 if (maybe_both_true (p, q, 1)) 1544 break; 1545 1546 /* If we reached the end of the list without finding one, 1547 use the incoming afterward position. */ 1548 if (!q) 1549 q = afterward; 1550 p->afterward = q; 1551 if (q) 1552 q->need_label = 1; 1553 } 1554 1555 /* Recurse. */ 1556 for (p = head->first; p ; p = p->next) 1557 if (p->success.first) 1558 find_afterward (&p->success, p->afterward); 1559 1560 /* When we are generating a subroutine, record the real afterward 1561 position in the first node where write_tree can find it, and we 1562 can do the right thing at the subroutine call site. */ 1563 p = head->first; 1564 if (p->subroutine_number > 0) 1565 p->afterward = real_afterward; 1566 } 1567 1568 /* Assuming that the state of argument is denoted by OLDPOS, take whatever 1569 actions are necessary to move to NEWPOS. If we fail to move to the 1570 new state, branch to node AFTERWARD if nonzero, otherwise return. 1571 1572 Failure to move to the new state can only occur if we are trying to 1573 match multiple insns and we try to step past the end of the stream. */ 1574 1575 static void 1576 change_state (struct position *oldpos, struct position *newpos, 1577 const char *indent) 1578 { 1579 while (oldpos->depth > newpos->depth) 1580 oldpos = oldpos->base; 1581 1582 if (oldpos != newpos) 1583 switch (newpos->type) 1584 { 1585 case POS_PEEP2_INSN: 1586 printf ("%stem = peep2_next_insn (%d);\n", indent, newpos->arg); 1587 printf ("%sx%d = PATTERN (tem);\n", indent, newpos->depth); 1588 break; 1589 1590 case POS_XEXP: 1591 change_state (oldpos, newpos->base, indent); 1592 printf ("%sx%d = XEXP (x%d, %d);\n", 1593 indent, newpos->depth, newpos->depth - 1, newpos->arg); 1594 break; 1595 1596 case POS_XVECEXP0: 1597 change_state (oldpos, newpos->base, indent); 1598 printf ("%sx%d = XVECEXP (x%d, 0, %d);\n", 1599 indent, newpos->depth, newpos->depth - 1, newpos->arg); 1600 break; 1601 } 1602 } 1603 1604 /* Print the enumerator constant for CODE -- the upcase version of 1605 the name. */ 1606 1607 static void 1608 print_code (enum rtx_code code) 1609 { 1610 const char *p; 1611 for (p = GET_RTX_NAME (code); *p; p++) 1612 putchar (TOUPPER (*p)); 1613 } 1614 1615 /* Emit code to cross an afterward link -- change state and branch. */ 1616 1617 static void 1618 write_afterward (struct decision *start, struct decision *afterward, 1619 const char *indent) 1620 { 1621 if (!afterward || start->subroutine_number > 0) 1622 printf("%sgoto ret0;\n", indent); 1623 else 1624 { 1625 change_state (start->position, afterward->position, indent); 1626 printf ("%sgoto L%d;\n", indent, afterward->number); 1627 } 1628 } 1629 1630 /* Emit a HOST_WIDE_INT as an integer constant expression. We need to take 1631 special care to avoid "decimal constant is so large that it is unsigned" 1632 warnings in the resulting code. */ 1633 1634 static void 1635 print_host_wide_int (HOST_WIDE_INT val) 1636 { 1637 /* XXX: the "min" below is computed for build, not host!!! */ 1638 HOST_WIDE_INT min = (unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT-1); 1639 if (val == min) 1640 printf ("(HOST_WIDE_INT_CONSTANT (" HOST_WIDE_INT_PRINT_DEC ")-1)", 1641 val + 1); 1642 else 1643 printf ("HOST_WIDE_INT_CONSTANT (" HOST_WIDE_INT_PRINT_DEC")", val); 1644 } 1645 1646 /* Emit a switch statement, if possible, for an initial sequence of 1647 nodes at START. Return the first node yet untested. */ 1648 1649 static struct decision * 1650 write_switch (struct decision *start, int depth) 1651 { 1652 struct decision *p = start; 1653 enum decision_type type = p->tests->type; 1654 struct decision *needs_label = NULL; 1655 1656 /* If we have two or more nodes in sequence that test the same one 1657 thing, we may be able to use a switch statement. */ 1658 1659 if (!p->next 1660 || p->tests->next 1661 || p->next->tests->type != type 1662 || p->next->tests->next 1663 || nodes_identical_1 (p->tests, p->next->tests)) 1664 return p; 1665 1666 /* DT_code is special in that we can do interesting things with 1667 known predicates at the same time. */ 1668 if (type == DT_code) 1669 { 1670 char codemap[NUM_RTX_CODE]; 1671 struct decision *ret; 1672 RTX_CODE code; 1673 1674 memset (codemap, 0, sizeof(codemap)); 1675 1676 printf (" switch (GET_CODE (x%d))\n {\n", depth); 1677 code = p->tests->u.code; 1678 do 1679 { 1680 if (p != start && p->need_label && needs_label == NULL) 1681 needs_label = p; 1682 1683 printf (" case "); 1684 print_code (code); 1685 printf (":\n goto L%d;\n", p->success.first->number); 1686 p->success.first->need_label = 1; 1687 1688 codemap[code] = 1; 1689 p = p->next; 1690 } 1691 while (p 1692 && ! p->tests->next 1693 && p->tests->type == DT_code 1694 && ! codemap[code = p->tests->u.code]); 1695 1696 /* If P is testing a predicate that we know about and we haven't 1697 seen any of the codes that are valid for the predicate, we can 1698 write a series of "case" statement, one for each possible code. 1699 Since we are already in a switch, these redundant tests are very 1700 cheap and will reduce the number of predicates called. */ 1701 1702 /* Note that while we write out cases for these predicates here, 1703 we don't actually write the test here, as it gets kinda messy. 1704 It is trivial to leave this to later by telling our caller that 1705 we only processed the CODE tests. */ 1706 if (needs_label != NULL) 1707 ret = needs_label; 1708 else 1709 ret = p; 1710 1711 while (p && p->tests->type == DT_pred && p->tests->u.pred.data) 1712 { 1713 const struct pred_data *data = p->tests->u.pred.data; 1714 int c; 1715 1716 for (c = 0; c < NUM_RTX_CODE; c++) 1717 if (codemap[c] && data->codes[c]) 1718 goto pred_done; 1719 1720 for (c = 0; c < NUM_RTX_CODE; c++) 1721 if (data->codes[c]) 1722 { 1723 fputs (" case ", stdout); 1724 print_code ((enum rtx_code) c); 1725 fputs (":\n", stdout); 1726 codemap[c] = 1; 1727 } 1728 1729 printf (" goto L%d;\n", p->number); 1730 p->need_label = 1; 1731 p = p->next; 1732 } 1733 1734 pred_done: 1735 /* Make the default case skip the predicates we managed to match. */ 1736 1737 printf (" default:\n"); 1738 if (p != ret) 1739 { 1740 if (p) 1741 { 1742 printf (" goto L%d;\n", p->number); 1743 p->need_label = 1; 1744 } 1745 else 1746 write_afterward (start, start->afterward, " "); 1747 } 1748 else 1749 printf (" break;\n"); 1750 printf (" }\n"); 1751 1752 return ret; 1753 } 1754 else if (type == DT_mode 1755 || type == DT_veclen 1756 || type == DT_elt_zero_int 1757 || type == DT_elt_one_int 1758 || type == DT_elt_zero_wide_safe) 1759 { 1760 const char *indent = ""; 1761 1762 /* We cast switch parameter to integer, so we must ensure that the value 1763 fits. */ 1764 if (type == DT_elt_zero_wide_safe) 1765 { 1766 indent = " "; 1767 printf(" if ((int) XWINT (x%d, 0) == XWINT (x%d, 0))\n", depth, depth); 1768 } 1769 printf ("%s switch (", indent); 1770 switch (type) 1771 { 1772 case DT_mode: 1773 printf ("GET_MODE (x%d)", depth); 1774 break; 1775 case DT_veclen: 1776 printf ("XVECLEN (x%d, 0)", depth); 1777 break; 1778 case DT_elt_zero_int: 1779 printf ("XINT (x%d, 0)", depth); 1780 break; 1781 case DT_elt_one_int: 1782 printf ("XINT (x%d, 1)", depth); 1783 break; 1784 case DT_elt_zero_wide_safe: 1785 /* Convert result of XWINT to int for portability since some C 1786 compilers won't do it and some will. */ 1787 printf ("(int) XWINT (x%d, 0)", depth); 1788 break; 1789 default: 1790 gcc_unreachable (); 1791 } 1792 printf (")\n%s {\n", indent); 1793 1794 do 1795 { 1796 /* Merge trees will not unify identical nodes if their 1797 sub-nodes are at different levels. Thus we must check 1798 for duplicate cases. */ 1799 struct decision *q; 1800 for (q = start; q != p; q = q->next) 1801 if (nodes_identical_1 (p->tests, q->tests)) 1802 goto case_done; 1803 1804 if (p != start && p->need_label && needs_label == NULL) 1805 needs_label = p; 1806 1807 printf ("%s case ", indent); 1808 switch (type) 1809 { 1810 case DT_mode: 1811 printf ("%smode", GET_MODE_NAME (p->tests->u.mode)); 1812 break; 1813 case DT_veclen: 1814 printf ("%d", p->tests->u.veclen); 1815 break; 1816 case DT_elt_zero_int: 1817 case DT_elt_one_int: 1818 case DT_elt_zero_wide: 1819 case DT_elt_zero_wide_safe: 1820 print_host_wide_int (p->tests->u.intval); 1821 break; 1822 default: 1823 gcc_unreachable (); 1824 } 1825 printf (":\n%s goto L%d;\n", indent, p->success.first->number); 1826 p->success.first->need_label = 1; 1827 1828 p = p->next; 1829 } 1830 while (p && p->tests->type == type && !p->tests->next); 1831 1832 case_done: 1833 printf ("%s default:\n%s break;\n%s }\n", 1834 indent, indent, indent); 1835 1836 return needs_label != NULL ? needs_label : p; 1837 } 1838 else 1839 { 1840 /* None of the other tests are amenable. */ 1841 return p; 1842 } 1843 } 1844 1845 /* Emit code for one test. */ 1846 1847 static void 1848 write_cond (struct decision_test *p, int depth, 1849 enum routine_type subroutine_type) 1850 { 1851 switch (p->type) 1852 { 1853 case DT_num_insns: 1854 printf ("peep2_current_count >= %d", p->u.num_insns); 1855 break; 1856 1857 case DT_mode: 1858 printf ("GET_MODE (x%d) == %smode", depth, GET_MODE_NAME (p->u.mode)); 1859 break; 1860 1861 case DT_code: 1862 printf ("GET_CODE (x%d) == ", depth); 1863 print_code (p->u.code); 1864 break; 1865 1866 case DT_veclen: 1867 printf ("XVECLEN (x%d, 0) == %d", depth, p->u.veclen); 1868 break; 1869 1870 case DT_elt_zero_int: 1871 printf ("XINT (x%d, 0) == %d", depth, (int) p->u.intval); 1872 break; 1873 1874 case DT_elt_one_int: 1875 printf ("XINT (x%d, 1) == %d", depth, (int) p->u.intval); 1876 break; 1877 1878 case DT_elt_zero_wide: 1879 case DT_elt_zero_wide_safe: 1880 printf ("XWINT (x%d, 0) == ", depth); 1881 print_host_wide_int (p->u.intval); 1882 break; 1883 1884 case DT_const_int: 1885 printf ("x%d == const_int_rtx[MAX_SAVED_CONST_INT + (%d)]", 1886 depth, (int) p->u.intval); 1887 break; 1888 1889 case DT_veclen_ge: 1890 printf ("XVECLEN (x%d, 0) >= %d", depth, p->u.veclen); 1891 break; 1892 1893 case DT_dup: 1894 printf ("rtx_equal_p (x%d, operands[%d])", depth, p->u.dup); 1895 break; 1896 1897 case DT_pred: 1898 printf ("%s (x%d, %smode)", p->u.pred.name, depth, 1899 GET_MODE_NAME (p->u.pred.mode)); 1900 break; 1901 1902 case DT_c_test: 1903 print_c_condition (p->u.c_test); 1904 break; 1905 1906 case DT_accept_insn: 1907 gcc_assert (subroutine_type == RECOG); 1908 gcc_assert (p->u.insn.num_clobbers_to_add); 1909 printf ("pnum_clobbers != NULL"); 1910 break; 1911 1912 default: 1913 gcc_unreachable (); 1914 } 1915 } 1916 1917 /* Emit code for one action. The previous tests have succeeded; 1918 TEST is the last of the chain. In the normal case we simply 1919 perform a state change. For the `accept' tests we must do more work. */ 1920 1921 static void 1922 write_action (struct decision *p, struct decision_test *test, 1923 int depth, int uncond, struct decision *success, 1924 enum routine_type subroutine_type) 1925 { 1926 const char *indent; 1927 int want_close = 0; 1928 1929 if (uncond) 1930 indent = " "; 1931 else if (test->type == DT_accept_op || test->type == DT_accept_insn) 1932 { 1933 fputs (" {\n", stdout); 1934 indent = " "; 1935 want_close = 1; 1936 } 1937 else 1938 indent = " "; 1939 1940 if (test->type == DT_accept_op) 1941 { 1942 printf("%soperands[%d] = x%d;\n", indent, test->u.opno, depth); 1943 1944 /* Only allow DT_accept_insn to follow. */ 1945 if (test->next) 1946 { 1947 test = test->next; 1948 gcc_assert (test->type == DT_accept_insn); 1949 } 1950 } 1951 1952 /* Sanity check that we're now at the end of the list of tests. */ 1953 gcc_assert (!test->next); 1954 1955 if (test->type == DT_accept_insn) 1956 { 1957 switch (subroutine_type) 1958 { 1959 case RECOG: 1960 if (test->u.insn.num_clobbers_to_add != 0) 1961 printf ("%s*pnum_clobbers = %d;\n", 1962 indent, test->u.insn.num_clobbers_to_add); 1963 printf ("%sreturn %d; /* %s */\n", indent, 1964 test->u.insn.code_number, 1965 get_insn_name (test->u.insn.code_number)); 1966 break; 1967 1968 case SPLIT: 1969 printf ("%sreturn gen_split_%d (insn, operands);\n", 1970 indent, test->u.insn.code_number); 1971 break; 1972 1973 case PEEPHOLE2: 1974 { 1975 int match_len = 0; 1976 struct position *pos; 1977 1978 for (pos = p->position; pos; pos = pos->base) 1979 if (pos->type == POS_PEEP2_INSN) 1980 { 1981 match_len = pos->arg; 1982 break; 1983 } 1984 printf ("%s*_pmatch_len = %d;\n", indent, match_len); 1985 printf ("%stem = gen_peephole2_%d (insn, operands);\n", 1986 indent, test->u.insn.code_number); 1987 printf ("%sif (tem != 0)\n%s return tem;\n", indent, indent); 1988 } 1989 break; 1990 1991 default: 1992 gcc_unreachable (); 1993 } 1994 } 1995 else 1996 { 1997 printf("%sgoto L%d;\n", indent, success->number); 1998 success->need_label = 1; 1999 } 2000 2001 if (want_close) 2002 fputs (" }\n", stdout); 2003 } 2004 2005 /* Return 1 if the test is always true and has no fallthru path. Return -1 2006 if the test does have a fallthru path, but requires that the condition be 2007 terminated. Otherwise return 0 for a normal test. */ 2008 /* ??? is_unconditional is a stupid name for a tri-state function. */ 2009 2010 static int 2011 is_unconditional (struct decision_test *t, enum routine_type subroutine_type) 2012 { 2013 if (t->type == DT_accept_op) 2014 return 1; 2015 2016 if (t->type == DT_accept_insn) 2017 { 2018 switch (subroutine_type) 2019 { 2020 case RECOG: 2021 return (t->u.insn.num_clobbers_to_add == 0); 2022 case SPLIT: 2023 return 1; 2024 case PEEPHOLE2: 2025 return -1; 2026 default: 2027 gcc_unreachable (); 2028 } 2029 } 2030 2031 return 0; 2032 } 2033 2034 /* Emit code for one node -- the conditional and the accompanying action. 2035 Return true if there is no fallthru path. */ 2036 2037 static int 2038 write_node (struct decision *p, int depth, 2039 enum routine_type subroutine_type) 2040 { 2041 struct decision_test *test, *last_test; 2042 int uncond; 2043 2044 /* Scan the tests and simplify comparisons against small 2045 constants. */ 2046 for (test = p->tests; test; test = test->next) 2047 { 2048 if (test->type == DT_code 2049 && test->u.code == CONST_INT 2050 && test->next 2051 && test->next->type == DT_elt_zero_wide_safe 2052 && -MAX_SAVED_CONST_INT <= test->next->u.intval 2053 && test->next->u.intval <= MAX_SAVED_CONST_INT) 2054 { 2055 test->type = DT_const_int; 2056 test->u.intval = test->next->u.intval; 2057 test->next = test->next->next; 2058 } 2059 } 2060 2061 last_test = test = p->tests; 2062 uncond = is_unconditional (test, subroutine_type); 2063 if (uncond == 0) 2064 { 2065 printf (" if ("); 2066 write_cond (test, depth, subroutine_type); 2067 2068 while ((test = test->next) != NULL) 2069 { 2070 last_test = test; 2071 if (is_unconditional (test, subroutine_type)) 2072 break; 2073 2074 printf ("\n && "); 2075 write_cond (test, depth, subroutine_type); 2076 } 2077 2078 printf (")\n"); 2079 } 2080 2081 write_action (p, last_test, depth, uncond, p->success.first, subroutine_type); 2082 2083 return uncond > 0; 2084 } 2085 2086 /* Emit code for all of the sibling nodes of HEAD. */ 2087 2088 static void 2089 write_tree_1 (struct decision_head *head, int depth, 2090 enum routine_type subroutine_type) 2091 { 2092 struct decision *p, *next; 2093 int uncond = 0; 2094 2095 for (p = head->first; p ; p = next) 2096 { 2097 /* The label for the first element was printed in write_tree. */ 2098 if (p != head->first && p->need_label) 2099 OUTPUT_LABEL (" ", p->number); 2100 2101 /* Attempt to write a switch statement for a whole sequence. */ 2102 next = write_switch (p, depth); 2103 if (p != next) 2104 uncond = 0; 2105 else 2106 { 2107 /* Failed -- fall back and write one node. */ 2108 uncond = write_node (p, depth, subroutine_type); 2109 next = p->next; 2110 } 2111 } 2112 2113 /* Finished with this chain. Close a fallthru path by branching 2114 to the afterward node. */ 2115 if (! uncond) 2116 write_afterward (head->last, head->last->afterward, " "); 2117 } 2118 2119 /* Write out the decision tree starting at HEAD. PREVPOS is the 2120 position at the node that branched to this node. */ 2121 2122 static void 2123 write_tree (struct decision_head *head, struct position *prevpos, 2124 enum routine_type type, int initial) 2125 { 2126 struct decision *p = head->first; 2127 2128 putchar ('\n'); 2129 if (p->need_label) 2130 OUTPUT_LABEL (" ", p->number); 2131 2132 if (! initial && p->subroutine_number > 0) 2133 { 2134 static const char * const name_prefix[] = { 2135 "recog", "split", "peephole2" 2136 }; 2137 2138 static const char * const call_suffix[] = { 2139 ", pnum_clobbers", "", ", _pmatch_len" 2140 }; 2141 2142 /* This node has been broken out into a separate subroutine. 2143 Call it, test the result, and branch accordingly. */ 2144 2145 if (p->afterward) 2146 { 2147 printf (" tem = %s_%d (x0, insn%s);\n", 2148 name_prefix[type], p->subroutine_number, call_suffix[type]); 2149 if (IS_SPLIT (type)) 2150 printf (" if (tem != 0)\n return tem;\n"); 2151 else 2152 printf (" if (tem >= 0)\n return tem;\n"); 2153 2154 change_state (p->position, p->afterward->position, " "); 2155 printf (" goto L%d;\n", p->afterward->number); 2156 } 2157 else 2158 { 2159 printf (" return %s_%d (x0, insn%s);\n", 2160 name_prefix[type], p->subroutine_number, call_suffix[type]); 2161 } 2162 } 2163 else 2164 { 2165 change_state (prevpos, p->position, " "); 2166 write_tree_1 (head, p->position->depth, type); 2167 2168 for (p = head->first; p; p = p->next) 2169 if (p->success.first) 2170 write_tree (&p->success, p->position, type, 0); 2171 } 2172 } 2173 2174 /* Write out a subroutine of type TYPE to do comparisons starting at 2175 node TREE. */ 2176 2177 static void 2178 write_subroutine (struct decision_head *head, enum routine_type type) 2179 { 2180 int subfunction = head->first ? head->first->subroutine_number : 0; 2181 const char *s_or_e; 2182 char extension[32]; 2183 int i; 2184 2185 s_or_e = subfunction ? "static " : ""; 2186 2187 if (subfunction) 2188 sprintf (extension, "_%d", subfunction); 2189 else if (type == RECOG) 2190 extension[0] = '\0'; 2191 else 2192 strcpy (extension, "_insns"); 2193 2194 switch (type) 2195 { 2196 case RECOG: 2197 printf ("%sint\n\ 2198 recog%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *pnum_clobbers ATTRIBUTE_UNUSED)\n", s_or_e, extension); 2199 break; 2200 case SPLIT: 2201 printf ("%srtx\n\ 2202 split%s (rtx x0 ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED)\n", 2203 s_or_e, extension); 2204 break; 2205 case PEEPHOLE2: 2206 printf ("%srtx\n\ 2207 peephole2%s (rtx x0 ATTRIBUTE_UNUSED,\n\trtx insn ATTRIBUTE_UNUSED,\n\tint *_pmatch_len ATTRIBUTE_UNUSED)\n", 2208 s_or_e, extension); 2209 break; 2210 } 2211 2212 printf ("{\n rtx * const operands ATTRIBUTE_UNUSED = &recog_data.operand[0];\n"); 2213 for (i = 1; i <= max_depth; i++) 2214 printf (" rtx x%d ATTRIBUTE_UNUSED;\n", i); 2215 2216 printf (" %s tem ATTRIBUTE_UNUSED;\n", IS_SPLIT (type) ? "rtx" : "int"); 2217 2218 if (!subfunction) 2219 printf (" recog_data.insn = NULL_RTX;\n"); 2220 2221 if (head->first) 2222 write_tree (head, &root_pos, type, 1); 2223 else 2224 printf (" goto ret0;\n"); 2225 2226 printf (" ret0:\n return %d;\n}\n\n", IS_SPLIT (type) ? 0 : -1); 2227 } 2228 2229 /* In break_out_subroutines, we discovered the boundaries for the 2230 subroutines, but did not write them out. Do so now. */ 2231 2232 static void 2233 write_subroutines (struct decision_head *head, enum routine_type type) 2234 { 2235 struct decision *p; 2236 2237 for (p = head->first; p ; p = p->next) 2238 if (p->success.first) 2239 write_subroutines (&p->success, type); 2240 2241 if (head->first->subroutine_number > 0) 2242 write_subroutine (head, type); 2243 } 2244 2245 /* Begin the output file. */ 2246 2247 static void 2248 write_header (void) 2249 { 2250 puts ("\ 2251 /* Generated automatically by the program `genrecog' from the target\n\ 2252 machine description file. */\n\ 2253 \n\ 2254 #include \"config.h\"\n\ 2255 #include \"system.h\"\n\ 2256 #include \"coretypes.h\"\n\ 2257 #include \"tm.h\"\n\ 2258 #include \"rtl.h\"\n\ 2259 #include \"tm_p.h\"\n\ 2260 #include \"function.h\"\n\ 2261 #include \"insn-config.h\"\n\ 2262 #include \"recog.h\"\n\ 2263 #include \"output.h\"\n\ 2264 #include \"flags.h\"\n\ 2265 #include \"hard-reg-set.h\"\n\ 2266 #include \"resource.h\"\n\ 2267 #include \"diagnostic-core.h\"\n\ 2268 #include \"reload.h\"\n\ 2269 #include \"regs.h\"\n\ 2270 #include \"tm-constrs.h\"\n\ 2271 \n"); 2272 2273 puts ("\n\ 2274 /* `recog' contains a decision tree that recognizes whether the rtx\n\ 2275 X0 is a valid instruction.\n\ 2276 \n\ 2277 recog returns -1 if the rtx is not valid. If the rtx is valid, recog\n\ 2278 returns a nonnegative number which is the insn code number for the\n\ 2279 pattern that matched. This is the same as the order in the machine\n\ 2280 description of the entry that matched. This number can be used as an\n\ 2281 index into `insn_data' and other tables.\n"); 2282 puts ("\ 2283 The third argument to recog is an optional pointer to an int. If\n\ 2284 present, recog will accept a pattern if it matches except for missing\n\ 2285 CLOBBER expressions at the end. In that case, the value pointed to by\n\ 2286 the optional pointer will be set to the number of CLOBBERs that need\n\ 2287 to be added (it should be initialized to zero by the caller). If it"); 2288 puts ("\ 2289 is set nonzero, the caller should allocate a PARALLEL of the\n\ 2290 appropriate size, copy the initial entries, and call add_clobbers\n\ 2291 (found in insn-emit.c) to fill in the CLOBBERs.\n\ 2292 "); 2293 2294 puts ("\n\ 2295 The function split_insns returns 0 if the rtl could not\n\ 2296 be split or the split rtl as an INSN list if it can be.\n\ 2297 \n\ 2298 The function peephole2_insns returns 0 if the rtl could not\n\ 2299 be matched. If there was a match, the new rtl is returned in an INSN list,\n\ 2300 and LAST_INSN will point to the last recognized insn in the old sequence.\n\ 2301 */\n\n"); 2302 } 2303 2304 2305 /* Construct and return a sequence of decisions 2306 that will recognize INSN. 2307 2308 TYPE says what type of routine we are recognizing (RECOG or SPLIT). */ 2309 2310 static struct decision_head 2311 make_insn_sequence (rtx insn, enum routine_type type) 2312 { 2313 rtx x; 2314 const char *c_test = XSTR (insn, type == RECOG ? 2 : 1); 2315 int truth = maybe_eval_c_test (c_test); 2316 struct decision *last; 2317 struct decision_test *test, **place; 2318 struct decision_head head; 2319 struct position *c_test_pos, **pos_ptr; 2320 2321 /* We should never see an insn whose C test is false at compile time. */ 2322 gcc_assert (truth); 2323 2324 c_test_pos = &root_pos; 2325 if (type == PEEPHOLE2) 2326 { 2327 int i, j; 2328 2329 /* peephole2 gets special treatment: 2330 - X always gets an outer parallel even if it's only one entry 2331 - we remove all traces of outer-level match_scratch and match_dup 2332 expressions here. */ 2333 x = rtx_alloc (PARALLEL); 2334 PUT_MODE (x, VOIDmode); 2335 XVEC (x, 0) = rtvec_alloc (XVECLEN (insn, 0)); 2336 pos_ptr = &peep2_insn_pos_list; 2337 for (i = j = 0; i < XVECLEN (insn, 0); i++) 2338 { 2339 rtx tmp = XVECEXP (insn, 0, i); 2340 if (GET_CODE (tmp) != MATCH_SCRATCH && GET_CODE (tmp) != MATCH_DUP) 2341 { 2342 c_test_pos = next_position (pos_ptr, &root_pos, 2343 POS_PEEP2_INSN, j); 2344 XVECEXP (x, 0, j) = tmp; 2345 j++; 2346 pos_ptr = &c_test_pos->next; 2347 } 2348 } 2349 XVECLEN (x, 0) = j; 2350 } 2351 else if (XVECLEN (insn, type == RECOG) == 1) 2352 x = XVECEXP (insn, type == RECOG, 0); 2353 else 2354 { 2355 x = rtx_alloc (PARALLEL); 2356 XVEC (x, 0) = XVEC (insn, type == RECOG); 2357 PUT_MODE (x, VOIDmode); 2358 } 2359 2360 validate_pattern (x, insn, NULL_RTX, 0); 2361 2362 memset(&head, 0, sizeof(head)); 2363 last = add_to_sequence (x, &head, &root_pos, type, 1); 2364 2365 /* Find the end of the test chain on the last node. */ 2366 for (test = last->tests; test->next; test = test->next) 2367 continue; 2368 place = &test->next; 2369 2370 /* Skip the C test if it's known to be true at compile time. */ 2371 if (truth == -1) 2372 { 2373 /* Need a new node if we have another test to add. */ 2374 if (test->type == DT_accept_op) 2375 { 2376 last = new_decision (c_test_pos, &last->success); 2377 place = &last->tests; 2378 } 2379 test = new_decision_test (DT_c_test, &place); 2380 test->u.c_test = c_test; 2381 } 2382 2383 test = new_decision_test (DT_accept_insn, &place); 2384 test->u.insn.code_number = next_insn_code; 2385 test->u.insn.lineno = pattern_lineno; 2386 test->u.insn.num_clobbers_to_add = 0; 2387 2388 switch (type) 2389 { 2390 case RECOG: 2391 /* If this is a DEFINE_INSN and X is a PARALLEL, see if it ends 2392 with a group of CLOBBERs of (hard) registers or MATCH_SCRATCHes. 2393 If so, set up to recognize the pattern without these CLOBBERs. */ 2394 2395 if (GET_CODE (x) == PARALLEL) 2396 { 2397 int i; 2398 2399 /* Find the last non-clobber in the parallel. */ 2400 for (i = XVECLEN (x, 0); i > 0; i--) 2401 { 2402 rtx y = XVECEXP (x, 0, i - 1); 2403 if (GET_CODE (y) != CLOBBER 2404 || (!REG_P (XEXP (y, 0)) 2405 && GET_CODE (XEXP (y, 0)) != MATCH_SCRATCH)) 2406 break; 2407 } 2408 2409 if (i != XVECLEN (x, 0)) 2410 { 2411 rtx new_rtx; 2412 struct decision_head clobber_head; 2413 2414 /* Build a similar insn without the clobbers. */ 2415 if (i == 1) 2416 new_rtx = XVECEXP (x, 0, 0); 2417 else 2418 { 2419 int j; 2420 2421 new_rtx = rtx_alloc (PARALLEL); 2422 XVEC (new_rtx, 0) = rtvec_alloc (i); 2423 for (j = i - 1; j >= 0; j--) 2424 XVECEXP (new_rtx, 0, j) = XVECEXP (x, 0, j); 2425 } 2426 2427 /* Recognize it. */ 2428 memset (&clobber_head, 0, sizeof(clobber_head)); 2429 last = add_to_sequence (new_rtx, &clobber_head, &root_pos, 2430 type, 1); 2431 2432 /* Find the end of the test chain on the last node. */ 2433 for (test = last->tests; test->next; test = test->next) 2434 continue; 2435 2436 /* We definitely have a new test to add -- create a new 2437 node if needed. */ 2438 place = &test->next; 2439 if (test->type == DT_accept_op) 2440 { 2441 last = new_decision (&root_pos, &last->success); 2442 place = &last->tests; 2443 } 2444 2445 /* Skip the C test if it's known to be true at compile 2446 time. */ 2447 if (truth == -1) 2448 { 2449 test = new_decision_test (DT_c_test, &place); 2450 test->u.c_test = c_test; 2451 } 2452 2453 test = new_decision_test (DT_accept_insn, &place); 2454 test->u.insn.code_number = next_insn_code; 2455 test->u.insn.lineno = pattern_lineno; 2456 test->u.insn.num_clobbers_to_add = XVECLEN (x, 0) - i; 2457 2458 merge_trees (&head, &clobber_head); 2459 } 2460 } 2461 break; 2462 2463 case SPLIT: 2464 /* Define the subroutine we will call below and emit in genemit. */ 2465 printf ("extern rtx gen_split_%d (rtx, rtx *);\n", next_insn_code); 2466 break; 2467 2468 case PEEPHOLE2: 2469 /* Define the subroutine we will call below and emit in genemit. */ 2470 printf ("extern rtx gen_peephole2_%d (rtx, rtx *);\n", 2471 next_insn_code); 2472 break; 2473 } 2474 2475 return head; 2476 } 2477 2478 static void 2479 process_tree (struct decision_head *head, enum routine_type subroutine_type) 2480 { 2481 if (head->first == NULL) 2482 { 2483 /* We can elide peephole2_insns, but not recog or split_insns. */ 2484 if (subroutine_type == PEEPHOLE2) 2485 return; 2486 } 2487 else 2488 { 2489 factor_tests (head); 2490 2491 next_subroutine_number = 0; 2492 break_out_subroutines (head, 1); 2493 find_afterward (head, NULL); 2494 2495 /* We run this after find_afterward, because find_afterward needs 2496 the redundant DT_mode tests on predicates to determine whether 2497 two tests can both be true or not. */ 2498 simplify_tests(head); 2499 2500 write_subroutines (head, subroutine_type); 2501 } 2502 2503 write_subroutine (head, subroutine_type); 2504 } 2505 2506 extern int main (int, char **); 2507 2508 int 2509 main (int argc, char **argv) 2510 { 2511 rtx desc; 2512 struct decision_head recog_tree, split_tree, peephole2_tree, h; 2513 2514 progname = "genrecog"; 2515 2516 memset (&recog_tree, 0, sizeof recog_tree); 2517 memset (&split_tree, 0, sizeof split_tree); 2518 memset (&peephole2_tree, 0, sizeof peephole2_tree); 2519 2520 if (!init_rtx_reader_args (argc, argv)) 2521 return (FATAL_EXIT_CODE); 2522 2523 next_insn_code = 0; 2524 2525 write_header (); 2526 2527 /* Read the machine description. */ 2528 2529 while (1) 2530 { 2531 desc = read_md_rtx (&pattern_lineno, &next_insn_code); 2532 if (desc == NULL) 2533 break; 2534 2535 switch (GET_CODE (desc)) 2536 { 2537 case DEFINE_INSN: 2538 h = make_insn_sequence (desc, RECOG); 2539 merge_trees (&recog_tree, &h); 2540 break; 2541 2542 case DEFINE_SPLIT: 2543 h = make_insn_sequence (desc, SPLIT); 2544 merge_trees (&split_tree, &h); 2545 break; 2546 2547 case DEFINE_PEEPHOLE2: 2548 h = make_insn_sequence (desc, PEEPHOLE2); 2549 merge_trees (&peephole2_tree, &h); 2550 2551 default: 2552 /* do nothing */; 2553 } 2554 } 2555 2556 if (have_error) 2557 return FATAL_EXIT_CODE; 2558 2559 puts ("\n\n"); 2560 2561 process_tree (&recog_tree, RECOG); 2562 process_tree (&split_tree, SPLIT); 2563 process_tree (&peephole2_tree, PEEPHOLE2); 2564 2565 fflush (stdout); 2566 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); 2567 } 2568 2569 static void 2570 debug_decision_2 (struct decision_test *test) 2571 { 2572 switch (test->type) 2573 { 2574 case DT_num_insns: 2575 fprintf (stderr, "num_insns=%d", test->u.num_insns); 2576 break; 2577 case DT_mode: 2578 fprintf (stderr, "mode=%s", GET_MODE_NAME (test->u.mode)); 2579 break; 2580 case DT_code: 2581 fprintf (stderr, "code=%s", GET_RTX_NAME (test->u.code)); 2582 break; 2583 case DT_veclen: 2584 fprintf (stderr, "veclen=%d", test->u.veclen); 2585 break; 2586 case DT_elt_zero_int: 2587 fprintf (stderr, "elt0_i=%d", (int) test->u.intval); 2588 break; 2589 case DT_elt_one_int: 2590 fprintf (stderr, "elt1_i=%d", (int) test->u.intval); 2591 break; 2592 case DT_elt_zero_wide: 2593 fprintf (stderr, "elt0_w=" HOST_WIDE_INT_PRINT_DEC, test->u.intval); 2594 break; 2595 case DT_elt_zero_wide_safe: 2596 fprintf (stderr, "elt0_ws=" HOST_WIDE_INT_PRINT_DEC, test->u.intval); 2597 break; 2598 case DT_veclen_ge: 2599 fprintf (stderr, "veclen>=%d", test->u.veclen); 2600 break; 2601 case DT_dup: 2602 fprintf (stderr, "dup=%d", test->u.dup); 2603 break; 2604 case DT_pred: 2605 fprintf (stderr, "pred=(%s,%s)", 2606 test->u.pred.name, GET_MODE_NAME(test->u.pred.mode)); 2607 break; 2608 case DT_c_test: 2609 { 2610 char sub[16+4]; 2611 strncpy (sub, test->u.c_test, sizeof(sub)); 2612 memcpy (sub+16, "...", 4); 2613 fprintf (stderr, "c_test=\"%s\"", sub); 2614 } 2615 break; 2616 case DT_accept_op: 2617 fprintf (stderr, "A_op=%d", test->u.opno); 2618 break; 2619 case DT_accept_insn: 2620 fprintf (stderr, "A_insn=(%d,%d)", 2621 test->u.insn.code_number, test->u.insn.num_clobbers_to_add); 2622 break; 2623 2624 default: 2625 gcc_unreachable (); 2626 } 2627 } 2628 2629 static void 2630 debug_decision_1 (struct decision *d, int indent) 2631 { 2632 int i; 2633 struct decision_test *test; 2634 2635 if (d == NULL) 2636 { 2637 for (i = 0; i < indent; ++i) 2638 putc (' ', stderr); 2639 fputs ("(nil)\n", stderr); 2640 return; 2641 } 2642 2643 for (i = 0; i < indent; ++i) 2644 putc (' ', stderr); 2645 2646 putc ('{', stderr); 2647 test = d->tests; 2648 if (test) 2649 { 2650 debug_decision_2 (test); 2651 while ((test = test->next) != NULL) 2652 { 2653 fputs (" + ", stderr); 2654 debug_decision_2 (test); 2655 } 2656 } 2657 fprintf (stderr, "} %d n %d a %d\n", d->number, 2658 (d->next ? d->next->number : -1), 2659 (d->afterward ? d->afterward->number : -1)); 2660 } 2661 2662 static void 2663 debug_decision_0 (struct decision *d, int indent, int maxdepth) 2664 { 2665 struct decision *n; 2666 int i; 2667 2668 if (maxdepth < 0) 2669 return; 2670 if (d == NULL) 2671 { 2672 for (i = 0; i < indent; ++i) 2673 putc (' ', stderr); 2674 fputs ("(nil)\n", stderr); 2675 return; 2676 } 2677 2678 debug_decision_1 (d, indent); 2679 for (n = d->success.first; n ; n = n->next) 2680 debug_decision_0 (n, indent + 2, maxdepth - 1); 2681 } 2682 2683 DEBUG_FUNCTION void 2684 debug_decision (struct decision *d) 2685 { 2686 debug_decision_0 (d, 0, 1000000); 2687 } 2688 2689 DEBUG_FUNCTION void 2690 debug_decision_list (struct decision *d) 2691 { 2692 while (d) 2693 { 2694 debug_decision_0 (d, 0, 0); 2695 d = d->next; 2696 } 2697 } 2698