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